xref: /illumos-gate/usr/src/cmd/eeprom/sparc/loadlogo.c (revision 9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
23 
24 
25 /*
26  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #include <stdio.h>
31 
32 extern int _error(int do_perror, char *fmt, ...);
33 #define	NO_PERROR	0
34 #define	PERROR		1
35 
36 /*
37  * Icon loader for eeprom command.
38  *
39  * Based on libsuntool/icon/icon_load.c 10.10 88/02/08
40  * See <suntool/icon_load.h> for icon file format.
41  */
42 
43 int
44 loadlogo(char *name, int w, int h, char *logo)
45 {
46 	FILE *f;
47 	int c = 0;
48 	unsigned int val;
49 	int icw = 64, ich = 64, bits = 16;
50 	int count;
51 
52 	if (!(f = fopen(name, "r")))
53 		return (_error(PERROR, "cannot open %s", name));
54 
55 	do {
56 		int nval;
57 		char slash;
58 
59 		if ((c = fscanf(f, "%*[^DFHVW*]")) == EOF)
60 			break;
61 
62 		switch (c = getc(f)) {
63 		case 'D':
64 			if ((c = fscanf(f, "epth=%d", &nval)) == 1 &&
65 			    nval != 1)
66 				goto badform;
67 			break;
68 		case 'F':
69 			if ((c = fscanf(f, "ormat_version=%d", &nval)) == 1 &&
70 			    nval != 1)
71 				goto badform;
72 			break;
73 		case 'H':
74 			c = fscanf(f, "eight=%d", &ich);
75 			break;
76 		case 'V':
77 			c = fscanf(f, "alid_bits_per_item=%d", &bits);
78 			break;
79 		case 'W':
80 			c = fscanf(f, "idth=%d", &icw);
81 			break;
82 		case '*':
83 			c = fscanf(f, "%c", &slash);
84 			if (slash == '/')
85 				goto eoh; /* end of header */
86 			else
87 				(void) ungetc(slash, f);
88 			break;
89 		}
90 	} while (c != EOF);
91 
92 eoh:
93 	if (c == EOF ||
94 	    icw != w || ich != h ||
95 	    bits != 16 && bits != 32) {
96 badform:
97 		(void) fclose(f);
98 		return (_error(NO_PERROR, "header format error in %s", name));
99 	}
100 
101 	for (count = ((w + (bits - 1)) / bits) * h; count > 0; count--) {
102 		c = fscanf(f, " 0x%x,", &val);
103 		if (c == 0 || c == EOF)
104 			break;
105 
106 		switch (bits) {
107 		case 32:
108 			*logo++ = val >> 24;
109 			*logo++ = val >> 16;
110 			/* FALLTHRU */
111 		case 16:
112 			*logo++ = val >> 8;
113 			*logo++ = val;
114 			break;
115 		}
116 	}
117 
118 	(void) fclose(f);
119 
120 	if (count > 0)
121 		return (_error(NO_PERROR, "data format error in %s", name));
122 
123 	return (0);
124 }
125