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