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