2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1995 Andrew McRae. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * Code cleanup, bug-fix and extension
34 * by Tatsumi Hosokawa <hosokawa@mt.cs.keio.ac.jp>
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);
54 #define LENGTH_ANY 255
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},
110 * After reading the tuples, decode the relevant ones.
116 return (read_tuples(fd));
120 * free_cis - delete cis entry.
123 freecis(struct tuple_list *tlist)
125 struct tuple_list *tl;
128 while ((tl = tlist) != 0) {
130 while ((tp = tl->tuples) != 0) {
131 tl->tuples = tp->next;
140 * Parse variable length value.
143 parse_num(int sz, u_char *p, u_char **q, int ofs)
157 num = tpl16(p) + ofs;
161 num = tpl24(p) + ofs;
166 num = tpl32(p) + ofs;
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
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.
191 static struct tuple_list *tlist;
193 static struct tuple_list *
196 struct tuple_list *tl = 0, *last_tl;
202 last_tl = tlist = read_one_tuplelist(fd, MDF_ATTR, (off_t) 0);
204 /* Now start processing the links (if any). */
207 tp = find_tuple_in_list(last_tl, CIS_LONGLINK_A);
210 tp = find_tuple_in_list(last_tl, CIS_LONGLINK_C);
213 if (tp == NULL || tp->length != 4)
216 offs = (uint32_t)tpl32(tp->data);
218 printf("Checking long link at %zd (%s memory)\n",
219 offs, flag ? "Attribute" : "Common");
222 * If a link was found, it looks sane read the tuple list from it.
224 if (offs > 0 && offs < 32 * 1024 && ck_linktarget(fd, offs, flag)) {
225 tl = read_one_tuplelist(fd, flag, offs);
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).
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)) {
239 printf("Reading long link at 0 (Common memory)\n");
241 tlist->next = read_one_tuplelist(fd, 0, 0);
247 * Read one tuple list from the card.
249 static struct tuple_list *
250 read_one_tuplelist(int fd, int flags, off_t offs)
252 struct tuple *tp, *last_tp = 0;
253 struct tuple_list *tl;
254 struct tuple_info *tinfo;
256 unsigned char code, length;
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))
262 tl = xmalloc(sizeof(*tl));
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",
272 if (read(fd, &code, 1) != 1)
273 errx(1, "CIS code read");
275 if (code == CIS_NULL)
277 tp = xmalloc(sizeof(*tp));
282 if (read(fd, &length, 1) != 1)
283 errx(1, "CIS len read");
287 printf("Tuple code = 0x%x, len = %d\n", code, length);
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
297 if (length == 0xFF) {
301 assert(length < 0xff);
304 * Check the tuple, and ignore it if it isn't in the table
305 * or the length is illegal.
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);
315 tp->data = xmalloc(length);
316 if (read(fd, tp->data, length) != length)
317 errx(1, "Can't read CIS data");
322 if (tl->tuples == NULL) {
327 } while (code != CIS_END && total < 1024);
332 * return true if the offset points to a LINKTARGET tuple.
335 ck_linktarget(int fd, off_t offs, int flag)
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",
345 if (read(fd, blk, 5) != 5)
347 if (blk[0] == CIS_LINKTARGET &&
357 * find_tuple_in_list - find a tuple within a
360 static struct tuple *
361 find_tuple_in_list(struct tuple_list *tl, unsigned char code)
365 for (tp = tl->tuples; tp; tp = tp->next)
366 if (tp->code == code)
372 * return table entry for code.
374 static struct tuple_info *
375 get_tuple_info(unsigned char code)
377 struct tuple_info *tp;
379 for (tp = tuple_info; tp->name; tp++)
380 if (tp->code == code)
386 tuple_name(unsigned char code)
388 struct tuple_info *tp;
390 tp = get_tuple_info(code);