xref: /titanic_44/usr/src/cmd/eeprom/sparc/loadlogo.c (revision ae115bc77f6fcde83175c75b4206dc2e50747966)
1*ae115bc7Smrj /*
2*ae115bc7Smrj  * CDDL HEADER START
3*ae115bc7Smrj  *
4*ae115bc7Smrj  * The contents of this file are subject to the terms of the
5*ae115bc7Smrj  * Common Development and Distribution License, Version 1.0 only
6*ae115bc7Smrj  * (the "License").  You may not use this file except in compliance
7*ae115bc7Smrj  * with the License.
8*ae115bc7Smrj  *
9*ae115bc7Smrj  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*ae115bc7Smrj  * or http://www.opensolaris.org/os/licensing.
11*ae115bc7Smrj  * See the License for the specific language governing permissions
12*ae115bc7Smrj  * and limitations under the License.
13*ae115bc7Smrj  *
14*ae115bc7Smrj  * When distributing Covered Code, include this CDDL HEADER in each
15*ae115bc7Smrj  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*ae115bc7Smrj  * If applicable, add the following below this CDDL HEADER, with the
17*ae115bc7Smrj  * fields enclosed by brackets "[]" replaced with your own identifying
18*ae115bc7Smrj  * information: Portions Copyright [yyyy] [name of copyright owner]
19*ae115bc7Smrj  *
20*ae115bc7Smrj  * CDDL HEADER END
21*ae115bc7Smrj  */
22*ae115bc7Smrj #pragma ident	"%Z%%M%	%I%	%E% SMI"
23*ae115bc7Smrj 
24*ae115bc7Smrj 
25*ae115bc7Smrj /*
26*ae115bc7Smrj  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
27*ae115bc7Smrj  * Use is subject to license terms.
28*ae115bc7Smrj  */
29*ae115bc7Smrj 
30*ae115bc7Smrj #include <stdio.h>
31*ae115bc7Smrj 
32*ae115bc7Smrj extern int _error(int do_perror, char *fmt, ...);
33*ae115bc7Smrj #define	NO_PERROR	0
34*ae115bc7Smrj #define	PERROR		1
35*ae115bc7Smrj 
36*ae115bc7Smrj /*
37*ae115bc7Smrj  * Icon loader for eeprom command.
38*ae115bc7Smrj  *
39*ae115bc7Smrj  * Based on libsuntool/icon/icon_load.c 10.10 88/02/08
40*ae115bc7Smrj  * See <suntool/icon_load.h> for icon file format.
41*ae115bc7Smrj  */
42*ae115bc7Smrj 
43*ae115bc7Smrj int
loadlogo(char * name,int w,int h,char * logo)44*ae115bc7Smrj loadlogo(char *name, int w, int h, char *logo)
45*ae115bc7Smrj {
46*ae115bc7Smrj 	FILE *f;
47*ae115bc7Smrj 	int c = 0;
48*ae115bc7Smrj 	unsigned int val;
49*ae115bc7Smrj 	int icw = 64, ich = 64, bits = 16;
50*ae115bc7Smrj 	int count;
51*ae115bc7Smrj 
52*ae115bc7Smrj 	if (!(f = fopen(name, "r")))
53*ae115bc7Smrj 		return (_error(PERROR, "cannot open %s", name));
54*ae115bc7Smrj 
55*ae115bc7Smrj 	do {
56*ae115bc7Smrj 		int nval;
57*ae115bc7Smrj 		char slash;
58*ae115bc7Smrj 
59*ae115bc7Smrj 		if ((c = fscanf(f, "%*[^DFHVW*]")) == EOF)
60*ae115bc7Smrj 			break;
61*ae115bc7Smrj 
62*ae115bc7Smrj 		switch (c = getc(f)) {
63*ae115bc7Smrj 		case 'D':
64*ae115bc7Smrj 			if ((c = fscanf(f, "epth=%d", &nval)) == 1 &&
65*ae115bc7Smrj 			    nval != 1)
66*ae115bc7Smrj 				goto badform;
67*ae115bc7Smrj 			break;
68*ae115bc7Smrj 		case 'F':
69*ae115bc7Smrj 			if ((c = fscanf(f, "ormat_version=%d", &nval)) == 1 &&
70*ae115bc7Smrj 			    nval != 1)
71*ae115bc7Smrj 				goto badform;
72*ae115bc7Smrj 			break;
73*ae115bc7Smrj 		case 'H':
74*ae115bc7Smrj 			c = fscanf(f, "eight=%d", &ich);
75*ae115bc7Smrj 			break;
76*ae115bc7Smrj 		case 'V':
77*ae115bc7Smrj 			c = fscanf(f, "alid_bits_per_item=%d", &bits);
78*ae115bc7Smrj 			break;
79*ae115bc7Smrj 		case 'W':
80*ae115bc7Smrj 			c = fscanf(f, "idth=%d", &icw);
81*ae115bc7Smrj 			break;
82*ae115bc7Smrj 		case '*':
83*ae115bc7Smrj 			c = fscanf(f, "%c", &slash);
84*ae115bc7Smrj 			if (slash == '/')
85*ae115bc7Smrj 				goto eoh; /* end of header */
86*ae115bc7Smrj 			else
87*ae115bc7Smrj 				(void) ungetc(slash, f);
88*ae115bc7Smrj 			break;
89*ae115bc7Smrj 		}
90*ae115bc7Smrj 	} while (c != EOF);
91*ae115bc7Smrj 
92*ae115bc7Smrj eoh:
93*ae115bc7Smrj 	if (c == EOF ||
94*ae115bc7Smrj 	    icw != w || ich != h ||
95*ae115bc7Smrj 	    bits != 16 && bits != 32) {
96*ae115bc7Smrj badform:
97*ae115bc7Smrj 		(void) fclose(f);
98*ae115bc7Smrj 		return (_error(NO_PERROR, "header format error in %s", name));
99*ae115bc7Smrj 	}
100*ae115bc7Smrj 
101*ae115bc7Smrj 	for (count = ((w + (bits - 1)) / bits) * h; count > 0; count--) {
102*ae115bc7Smrj 		c = fscanf(f, " 0x%x,", &val);
103*ae115bc7Smrj 		if (c == 0 || c == EOF)
104*ae115bc7Smrj 			break;
105*ae115bc7Smrj 
106*ae115bc7Smrj 		switch (bits) {
107*ae115bc7Smrj 		case 32:
108*ae115bc7Smrj 			*logo++ = val >> 24;
109*ae115bc7Smrj 			*logo++ = val >> 16;
110*ae115bc7Smrj 			/* FALLTHRU */
111*ae115bc7Smrj 		case 16:
112*ae115bc7Smrj 			*logo++ = val >> 8;
113*ae115bc7Smrj 			*logo++ = val;
114*ae115bc7Smrj 			break;
115*ae115bc7Smrj 		}
116*ae115bc7Smrj 	}
117*ae115bc7Smrj 
118*ae115bc7Smrj 	(void) fclose(f);
119*ae115bc7Smrj 
120*ae115bc7Smrj 	if (count > 0)
121*ae115bc7Smrj 		return (_error(NO_PERROR, "data format error in %s", name));
122*ae115bc7Smrj 
123*ae115bc7Smrj 	return (0);
124*ae115bc7Smrj }
125