xref: /freebsd/usr.sbin/dumpcis/readcis.c (revision 54ebdd631db8c0bba2baab0155f603a8b5cf014a)
1 /*
2  * Copyright (c) 1995 Andrew McRae.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef lint
28 static const char rcsid[] =
29   "$FreeBSD$";
30 #endif /* not lint */
31 
32 /*
33  * Code cleanup, bug-fix and extension
34  * by Tatsumi Hosokawa <hosokawa@mt.cs.keio.ac.jp>
35  */
36 
37 #include <err.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #include <pccard/cardinfo.h>
44 #include <pccard/cis.h>
45 
46 #include "readcis.h"
47 
48 static int ck_linktarget(int, off_t, int);
49 static struct tuple_list *read_one_tuplelist(int, int, off_t);
50 static struct tuple_list *read_tuples(int);
51 static struct tuple *find_tuple_in_list(struct tuple_list *, unsigned char);
52 static struct tuple_info *get_tuple_info(unsigned char);
53 
54 static struct tuple_info tuple_info[] = {
55 	{"Null tuple", CIS_NULL, 0},
56 	{"Common memory descriptor", CIS_MEM_COMMON, 255},
57 	{"Long link to next chain for CardBus", CIS_LONGLINK_CB, 255},
58 	{"Indirect access", CIS_INDIRECT, 255},
59 	{"Configuration map for CardBus", CIS_CONF_MAP_CB, 255},
60 	{"Configuration entry for CardBus", CIS_CONFIG_CB, 255},
61 	{"Long link to next chain for MFC", CIS_LONGLINK_MFC, 255},
62 	{"Base address register for CardBus", CIS_BAR, 6},
63 	{"Checksum", CIS_CHECKSUM, 5},
64 	{"Long link to attribute memory", CIS_LONGLINK_A, 4},
65 	{"Long link to common memory", CIS_LONGLINK_C, 4},
66 	{"Link target", CIS_LINKTARGET, 3},
67 	{"No link", CIS_NOLINK, 0},
68 	{"Version 1 info", CIS_INFO_V1, 255},
69 	{"Alternate language string", CIS_ALTSTR, 255},
70 	{"Attribute memory descriptor", CIS_MEM_ATTR, 255},
71 	{"JEDEC descr for common memory", CIS_JEDEC_C, 255},
72 	{"JEDEC descr for attribute memory", CIS_JEDEC_A, 255},
73 	{"Configuration map", CIS_CONF_MAP, 255},
74 	{"Configuration entry", CIS_CONFIG, 255},
75 	{"Other conditions for common memory", CIS_DEVICE_OC, 255},
76 	{"Other conditions for attribute memory", CIS_DEVICE_OA, 255},
77 	{"Geometry info for common memory", CIS_DEVICEGEO, 255},
78 	{"Geometry info for attribute memory", CIS_DEVICEGEO_A, 255},
79 	{"Manufacturer ID", CIS_MANUF_ID, 4},
80 	{"Functional ID", CIS_FUNC_ID, 2},
81 	{"Functional EXT", CIS_FUNC_EXT, 255},
82 	{"Software interleave", CIS_SW_INTERLV, 2},
83 	{"Version 2 Info", CIS_VERS_2, 255},
84 	{"Data format", CIS_FORMAT, 255},
85 	{"Geometry", CIS_GEOMETRY, 4},
86 	{"Byte order", CIS_BYTEORDER, 2},
87 	{"Card init date", CIS_DATE, 4},
88 	{"Battery replacement", CIS_BATTERY, 4},
89 	{"Organization", CIS_ORG, 255},
90 	{"Terminator", CIS_END, 0},
91 	{0, 0, 0}
92 };
93 
94 static void *
95 xmalloc(int sz)
96 {
97 	void   *p;
98 
99 	sz = (sz + 7) & ~7;
100 	p = malloc(sz);
101 	if (p)
102 		bzero(p, sz);
103 	else
104 		errx(1, "malloc");
105 	return (p);
106 }
107 
108 /*
109  *	After reading the tuples, decode the relevant ones.
110  */
111 struct tuple_list *
112 readcis(int fd)
113 {
114 
115 	return (read_tuples(fd));
116 }
117 
118 /*
119  *	free_cis - delete cis entry.
120  */
121 void
122 freecis(struct tuple_list *tlist)
123 {
124 	struct tuple_list *tl;
125 	struct tuple *tp;
126 
127 	while ((tl = tlist) != 0) {
128 		tlist = tl->next;
129 		while ((tp = tl->tuples) != 0) {
130 			tl->tuples = tp->next;
131 			free(tp->data);
132 			free(tp);
133 		}
134 		free(tl);
135 	}
136 }
137 
138 /*
139  *	Parse variable length value.
140  */
141 u_int
142 parse_num(int sz, u_char *p, u_char **q, int ofs)
143 {
144 	u_int num = 0;
145 
146 	switch (sz) {
147 	case 0:
148 	case 0x10:
149 		break;
150 	case 1:
151 	case 0x11:
152 		num = (*p++) + ofs;
153 		break;
154 	case 2:
155 	case 0x12:
156 		num = tpl16(p) + ofs;
157 		p += 2;
158 		break;
159 	case 0x13:
160 		num = tpl24(p) + ofs;
161 		p += 3;
162 		break;
163 	case 3:
164 	case 0x14:
165 		num = tpl32(p) + ofs;
166 		p += 4;
167 		break;
168 	}
169 	if (q)
170 		*q = p;
171 	return num;
172 }
173 
174 /*
175  *	Read the tuples from the card.
176  *	The processing of tuples is as follows:
177  *		- Read tuples at attribute memory, offset 0.
178  *		- If a CIS_END is the first tuple, look for
179  *		  a tuple list at common memory offset 0; this list
180  *		  must start with a LINKTARGET.
181  *		- If a long link tuple was encountered, execute the long
182  *		  link.
183  *		- If a no-link tuple was seen, terminate processing.
184  *		- If no no-link tuple exists, and no long link tuple
185  *		  exists while processing the primary tuple list,
186  *		  then look for a LINKTARGET tuple in common memory.
187  *		- If a long link tuple is found in any list, then process
188  *		  it. Only one link is allowed per list.
189  */
190 static struct tuple_list *tlist;
191 
192 static struct tuple_list *
193 read_tuples(int fd)
194 {
195 	struct tuple_list *tl = 0, *last_tl;
196 	struct tuple *tp;
197 	int     flag;
198 	off_t   offs;
199 
200 	tlist = 0;
201 	last_tl = tlist = read_one_tuplelist(fd, MDF_ATTR, (off_t) 0);
202 
203 	/* Now start processing the links (if any). */
204 	do {
205 		flag = MDF_ATTR;
206 		tp = find_tuple_in_list(last_tl, CIS_LONGLINK_A);
207 		if (tp == 0) {
208 			flag = 0;
209 			tp = find_tuple_in_list(last_tl, CIS_LONGLINK_C);
210 		}
211 		if (tp && tp->length == 4) {
212 			offs = tpl32(tp->data);
213 #ifdef	DEBUG
214 			printf("Checking long link at %zd (%s memory)\n",
215 			    offs, flag ? "Attribute" : "Common");
216 #endif
217 			/* If a link was found, read the tuple list from it. */
218 			if (ck_linktarget(fd, offs, flag)) {
219 				tl = read_one_tuplelist(fd, flag, offs);
220 				last_tl->next = tl;
221 				last_tl = tl;
222 			}
223 		} else
224 			tl = 0;
225 	} while (tl);
226 
227 	/*
228 	 * If the primary list had no NOLINK tuple, and no LINKTARGET,
229 	 * then try to read a tuple list at common memory (offset 0).
230 	 */
231 	if (find_tuple_in_list(tlist, CIS_NOLINK) == 0 &&
232 	    find_tuple_in_list(tlist, CIS_LINKTARGET) == 0 &&
233 	    ck_linktarget(fd, (off_t) 0, 0)) {
234 		offs = 0;
235 #ifdef	DEBUG
236 		printf("Reading long link at %zd (%s memory)\n",
237 		    offs, flag ? "Attribute" : "Common");
238 #endif
239 		tlist->next = read_one_tuplelist(fd, 0, offs);
240 	}
241 	return (tlist);
242 }
243 
244 /*
245  *	Read one tuple list from the card.
246  */
247 static struct tuple_list *
248 read_one_tuplelist(int fd, int flags, off_t offs)
249 {
250 	struct tuple *tp, *last_tp = 0;
251 	struct tuple_list *tl;
252 	struct tuple_info *tinfo;
253 	int     total = 0;
254 	unsigned char code, length;
255 
256 	/* Check to see if this memory has already been scanned. */
257 	for (tl = tlist; tl; tl = tl->next)
258 		if (tl->offs == offs && tl->flags == (flags & MDF_ATTR))
259 			return (0);
260 	tl = xmalloc(sizeof(*tl));
261 	tl->offs = offs;
262 	tl->flags = flags & MDF_ATTR;
263 	ioctl(fd, PIOCRWFLAG, &flags);
264 	lseek(fd, offs, SEEK_SET);
265 	do {
266 		if (read(fd, &code, 1) != 1) {
267 			warn("CIS code read");
268 			break;
269 		}
270 		total++;
271 		if (code == CIS_NULL)
272 			continue;
273 		tp = xmalloc(sizeof(*tp));
274 		tp->code = code;
275 		if (code == CIS_END)
276 			length = 0;
277 		else {
278 			if (read(fd, &length, 1) != 1) {
279 				warn("CIS len read");
280 				break;
281 			}
282 			total++;
283 		}
284 		tp->length = length;
285 #ifdef	DEBUG
286 		printf("Tuple code = 0x%x, len = %d\n", code, length);
287 #endif
288 		if (length == 0xFF) {
289 			length = tp->length = 0;
290 			code = CIS_END;
291 		}
292 		if (length != 0) {
293 			total += length;
294 			tp->data = xmalloc(length);
295 			if (read(fd, tp->data, length) != length) {
296 				warn("CIS read");
297 				break;
298 			}
299 		}
300 
301 		/*
302 		 * Check the tuple, and ignore it if it isn't in the table
303 		 * or the length is illegal.
304 		 */
305 		tinfo = get_tuple_info(code);
306 		if (tinfo != NULL && (tinfo->length != 255 && tinfo->length > length)) {
307 			printf("code %s (%d) ignored\n", tuple_name(code), code);
308 			tp->code = CIS_NULL;
309 		}
310 		if (tl->tuples == NULL)
311 			tl->tuples = tp;
312 		else
313 			last_tp->next = tp;
314 		last_tp = tp;
315 	} while (code != CIS_END && total < 1024);
316 	return (tl);
317 }
318 
319 /*
320  *	return true if the offset points to a LINKTARGET tuple.
321  */
322 static int
323 ck_linktarget(int fd, off_t offs, int flag)
324 {
325 	char    blk[5];
326 
327 	ioctl(fd, PIOCRWFLAG, &flag);
328 	lseek(fd, offs, SEEK_SET);
329 	if (read(fd, blk, 5) != 5)
330 		return (0);
331 	if (blk[0] == CIS_LINKTARGET &&
332 	    blk[1] == 0x3 &&
333 	    blk[2] == 'C' &&
334 	    blk[3] == 'I' &&
335 	    blk[4] == 'S')
336 		return (1);
337 	return (0);
338 }
339 
340 /*
341  *	find_tuple_in_list - find a tuple within a
342  *	single tuple list.
343  */
344 static struct tuple *
345 find_tuple_in_list(struct tuple_list *tl, unsigned char code)
346 {
347 	struct tuple *tp;
348 
349 	for (tp = tl->tuples; tp; tp = tp->next)
350 		if (tp->code == code)
351 			break;
352 	return (tp);
353 }
354 
355 /*
356  *	return table entry for code.
357  */
358 static struct tuple_info *
359 get_tuple_info(unsigned char code)
360 {
361 	struct tuple_info *tp;
362 
363 	for (tp = tuple_info; tp->name; tp++)
364 		if (tp->code == code)
365 			return (tp);
366 	return (0);
367 }
368 
369 const char *
370 tuple_name(unsigned char code)
371 {
372 	struct tuple_info *tp;
373 
374 	tp = get_tuple_info(code);
375 	if (tp)
376 		return (tp->name);
377 	return ("Unknown");
378 }
379