xref: /freebsd/usr.sbin/dumpcis/readcis.c (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
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 __FBSDID("$FreeBSD$");
31 
32 /*
33  * Code cleanup, bug-fix and extension
34  * by Tatsumi Hosokawa <hosokawa@mt.cs.keio.ac.jp>
35  */
36 
37 #include <assert.h>
38 #include <err.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #include "cardinfo.h"
45 #include "cis.h"
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 #define LENGTH_ANY 255
55 
56 static struct tuple_info tuple_info[] = {
57 	{"Null tuple", CIS_NULL, 0},
58 	{"Common memory descriptor", CIS_MEM_COMMON, LENGTH_ANY},
59 	{"Long link to next chain for CardBus", CIS_LONGLINK_CB, LENGTH_ANY},
60 	{"Indirect access", CIS_INDIRECT, LENGTH_ANY},
61 	{"Configuration map for CardBus", CIS_CONF_MAP_CB, LENGTH_ANY},
62 	{"Configuration entry for CardBus", CIS_CONFIG_CB, LENGTH_ANY},
63 	{"Long link to next chain for MFC", CIS_LONGLINK_MFC, LENGTH_ANY},
64 	{"Base address register for CardBus", CIS_BAR, 6},
65 	{"Checksum", CIS_CHECKSUM, 5},
66 	{"Long link to attribute memory", CIS_LONGLINK_A, 4},
67 	{"Long link to common memory", CIS_LONGLINK_C, 4},
68 	{"Link target", CIS_LINKTARGET, 3},
69 	{"No link", CIS_NOLINK, 0},
70 	{"Version 1 info", CIS_INFO_V1, LENGTH_ANY},
71 	{"Alternate language string", CIS_ALTSTR, LENGTH_ANY},
72 	{"Attribute memory descriptor", CIS_MEM_ATTR, LENGTH_ANY},
73 	{"JEDEC descr for common memory", CIS_JEDEC_C, LENGTH_ANY},
74 	{"JEDEC descr for attribute memory", CIS_JEDEC_A, LENGTH_ANY},
75 	{"Configuration map", CIS_CONF_MAP, LENGTH_ANY},
76 	{"Configuration entry", CIS_CONFIG, LENGTH_ANY},
77 	{"Other conditions for common memory", CIS_DEVICE_OC, LENGTH_ANY},
78 	{"Other conditions for attribute memory", CIS_DEVICE_OA, LENGTH_ANY},
79 	{"Geometry info for common memory", CIS_DEVICEGEO, LENGTH_ANY},
80 	{"Geometry info for attribute memory", CIS_DEVICEGEO_A, LENGTH_ANY},
81 	{"Manufacturer ID", CIS_MANUF_ID, 4},
82 	{"Functional ID", CIS_FUNC_ID, 2},
83 	{"Functional EXT", CIS_FUNC_EXT, LENGTH_ANY},
84 	{"Software interleave", CIS_SW_INTERLV, 2},
85 	{"Version 2 Info", CIS_VERS_2, LENGTH_ANY},
86 	{"Data format", CIS_FORMAT, LENGTH_ANY},
87 	{"Geometry", CIS_GEOMETRY, 4},
88 	{"Byte order", CIS_BYTEORDER, 2},
89 	{"Card init date", CIS_DATE, 4},
90 	{"Battery replacement", CIS_BATTERY, 4},
91 	{"Organization", CIS_ORG, LENGTH_ANY},
92 	{"Terminator", CIS_END, 0},
93 	{0, 0, 0}
94 };
95 
96 static void *
97 xmalloc(int sz)
98 {
99 	void   *p;
100 
101 	sz = (sz + 7) & ~7;
102 	p = malloc(sz);
103 	if (p == NULL)
104 		errx(1, "malloc");
105 	bzero(p, sz);
106 	return (p);
107 }
108 
109 /*
110  *	After reading the tuples, decode the relevant ones.
111  */
112 struct tuple_list *
113 readcis(int fd)
114 {
115 
116 	return (read_tuples(fd));
117 }
118 
119 /*
120  *	free_cis - delete cis entry.
121  */
122 void
123 freecis(struct tuple_list *tlist)
124 {
125 	struct tuple_list *tl;
126 	struct tuple *tp;
127 
128 	while ((tl = tlist) != 0) {
129 		tlist = tl->next;
130 		while ((tp = tl->tuples) != 0) {
131 			tl->tuples = tp->next;
132 			free(tp->data);
133 			free(tp);
134 		}
135 		free(tl);
136 	}
137 }
138 
139 /*
140  *	Parse variable length value.
141  */
142 u_int
143 parse_num(int sz, u_char *p, u_char **q, int ofs)
144 {
145 	u_int num = 0;
146 
147 	switch (sz) {
148 	case 0:
149 	case 0x10:
150 		break;
151 	case 1:
152 	case 0x11:
153 		num = (*p++) + ofs;
154 		break;
155 	case 2:
156 	case 0x12:
157 		num = tpl16(p) + ofs;
158 		p += 2;
159 		break;
160 	case 0x13:
161 		num = tpl24(p) + ofs;
162 		p += 3;
163 		break;
164 	case 3:
165 	case 0x14:
166 		num = tpl32(p) + ofs;
167 		p += 4;
168 		break;
169 	}
170 	if (q)
171 		*q = p;
172 	return num;
173 }
174 
175 /*
176  *	Read the tuples from the card.
177  *	The processing of tuples is as follows:
178  *		- Read tuples at attribute memory, offset 0.
179  *		- If a CIS_END is the first tuple, look for
180  *		  a tuple list at common memory offset 0; this list
181  *		  must start with a LINKTARGET.
182  *		- If a long link tuple was encountered, execute the long
183  *		  link.
184  *		- If a no-link tuple was seen, terminate processing.
185  *		- If no no-link tuple exists, and no long link tuple
186  *		  exists while processing the primary tuple list,
187  *		  then look for a LINKTARGET tuple in common memory.
188  *		- If a long link tuple is found in any list, then process
189  *		  it. Only one link is allowed per list.
190  */
191 static struct tuple_list *tlist;
192 
193 static struct tuple_list *
194 read_tuples(int fd)
195 {
196 	struct tuple_list *tl = 0, *last_tl;
197 	struct tuple *tp;
198 	int     flag;
199 	off_t   offs;
200 
201 	tlist = 0;
202 	last_tl = tlist = read_one_tuplelist(fd, MDF_ATTR, (off_t) 0);
203 
204 	/* Now start processing the links (if any). */
205 	do {
206 		flag = MDF_ATTR;
207 		tp = find_tuple_in_list(last_tl, CIS_LONGLINK_A);
208 		if (tp == NULL) {
209 			flag = 0;
210 			tp = find_tuple_in_list(last_tl, CIS_LONGLINK_C);
211 		}
212 
213 		if (tp == NULL || tp->length != 4)
214 			break;
215 
216 		offs = (uint32_t)tpl32(tp->data);
217 #ifdef	DEBUG
218 		printf("Checking long link at %zd (%s memory)\n",
219 		    offs, flag ? "Attribute" : "Common");
220 #endif
221 		/*
222 		 * If a link was found, it looks sane read the tuple list from it.
223 		 */
224 		if (offs > 0 && offs < 32 * 1024 && ck_linktarget(fd, offs, flag)) {
225 			tl = read_one_tuplelist(fd, flag, offs);
226 			last_tl->next = tl;
227 			last_tl = tl;
228 		}
229 	} while (tl);
230 
231 	/*
232 	 * If the primary list had no NOLINK tuple, and no LINKTARGET, then try
233 	 * to read a tuple list at common memory (offset 0).
234 	 */
235 	if (find_tuple_in_list(tlist, CIS_NOLINK) == 0 &&
236 	    find_tuple_in_list(tlist, CIS_LINKTARGET) == 0 &&
237 	    ck_linktarget(fd, (off_t) 0, 0)) {
238 #ifdef	DEBUG
239 		printf("Reading long link at 0 (Common memory)\n");
240 #endif
241 		tlist->next = read_one_tuplelist(fd, 0, 0);
242 	}
243 	return (tlist);
244 }
245 
246 /*
247  *	Read one tuple list from the card.
248  */
249 static struct tuple_list *
250 read_one_tuplelist(int fd, int flags, off_t offs)
251 {
252 	struct tuple *tp, *last_tp = 0;
253 	struct tuple_list *tl;
254 	struct tuple_info *tinfo;
255 	int     total = 0;
256 	unsigned char code, length;
257 
258 	/* Check to see if this memory has already been scanned. */
259 	for (tl = tlist; tl; tl = tl->next)
260 		if (tl->offs == offs && tl->flags == (flags & MDF_ATTR))
261 			return (0);
262 	tl = xmalloc(sizeof(*tl));
263 	tl->offs = offs;
264 	tl->flags = flags & MDF_ATTR;
265 	if (ioctl(fd, PIOCRWFLAG, &flags) < 0)
266 		err(1, "Setting flag to rad %s memory failed",
267 		    flags ? "attribute" : "common");
268 	if (lseek(fd, offs, SEEK_SET) < 0)
269 		err(1, "Unable to seek to memory offset %ju",
270 		    (uintmax_t)offs);
271 	do {
272 		if (read(fd, &code, 1) != 1)
273 			errx(1, "CIS code read");
274 		total++;
275 		if (code == CIS_NULL)
276 			continue;
277 		tp = xmalloc(sizeof(*tp));
278 		tp->code = code;
279 		if (code == CIS_END)
280 			length = 0;
281 		else {
282 			if (read(fd, &length, 1) != 1)
283 				errx(1, "CIS len read");
284 			total++;
285 		}
286 #ifdef	DEBUG
287 		printf("Tuple code = 0x%x, len = %d\n", code, length);
288 #endif
289 
290 		/*
291 		 * A length of 255 is invalid, all others are valid. Treat a
292 		 * length of 255 as the end of the list. Some cards don't have a
293 		 * CIS_END at the end. These work on other systems because the
294 		 * end of the CIS eventually sees an area that's not decoded and
295 		 * read back as 0xff.
296 		 */
297 		if (length == 0xFF) {
298 			length = 0;
299 			code = CIS_END;
300 		}
301 		assert(length < 0xff);
302 
303 		/*
304 		 * Check the tuple, and ignore it if it isn't in the table
305 		 * or the length is illegal.
306 		 */
307 		tinfo = get_tuple_info(code);
308 		if (tinfo == NULL || (tinfo->length != LENGTH_ANY && tinfo->length > length)) {
309 			printf("code %s (%d) ignored\n", tuple_name(code), code);
310 			continue;
311 		}
312 		tp->length = length;
313 		if (length != 0) {
314 			total += length;
315 			tp->data = xmalloc(length);
316 			if (read(fd, tp->data, length) != length)
317 				errx(1, "Can't read CIS data");
318 		}
319 
320 		if (last_tp != NULL)
321 			last_tp->next = tp;
322 		if (tl->tuples == NULL) {
323 			tl->tuples = tp;
324 			tp->next = NULL;
325 		}
326 		last_tp = tp;
327 	} while (code != CIS_END && total < 1024);
328 	return (tl);
329 }
330 
331 /*
332  *	return true if the offset points to a LINKTARGET tuple.
333  */
334 static int
335 ck_linktarget(int fd, off_t offs, int flag)
336 {
337 	char    blk[5];
338 
339 	if (ioctl(fd, PIOCRWFLAG, &flag) < 0)
340 		err(1, "Setting flag to rad %s memory failed",
341 		    flag ? "attribute" : "common");
342 	if (lseek(fd, offs, SEEK_SET) < 0)
343 		err(1, "Unable to seek to memory offset %ju",
344 		    (uintmax_t)offs);
345 	if (read(fd, blk, 5) != 5)
346 		return (0);
347 	if (blk[0] == CIS_LINKTARGET &&
348 	    blk[1] == 0x3 &&
349 	    blk[2] == 'C' &&
350 	    blk[3] == 'I' &&
351 	    blk[4] == 'S')
352 		return (1);
353 	return (0);
354 }
355 
356 /*
357  *	find_tuple_in_list - find a tuple within a
358  *	single tuple list.
359  */
360 static struct tuple *
361 find_tuple_in_list(struct tuple_list *tl, unsigned char code)
362 {
363 	struct tuple *tp;
364 
365 	for (tp = tl->tuples; tp; tp = tp->next)
366 		if (tp->code == code)
367 			break;
368 	return (tp);
369 }
370 
371 /*
372  *	return table entry for code.
373  */
374 static struct tuple_info *
375 get_tuple_info(unsigned char code)
376 {
377 	struct tuple_info *tp;
378 
379 	for (tp = tuple_info; tp->name; tp++)
380 		if (tp->code == code)
381 			return (tp);
382 	return (0);
383 }
384 
385 const char *
386 tuple_name(unsigned char code)
387 {
388 	struct tuple_info *tp;
389 
390 	tp = get_tuple_info(code);
391 	if (tp)
392 		return (tp->name);
393 	return ("Unknown");
394 }
395