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.
30 static const char rcsid[] =
35 * Code cleanup, bug-fix and extension
36 * by Tatsumi Hosokawa <hosokawa@mt.cs.keio.ac.jp>
49 static int ck_linktarget(int, off_t, int);
50 static struct tuple_list *read_one_tuplelist(int, int, off_t);
51 static struct tuple_list *read_tuples(int);
52 static struct tuple *find_tuple_in_list(struct tuple_list *, unsigned char);
53 static struct tuple_info *get_tuple_info(unsigned char);
55 static struct tuple_info tuple_info[] = {
56 {"Null tuple", CIS_NULL, 0},
57 {"Common memory descriptor", CIS_MEM_COMMON, 255},
58 {"Long link to next chain for CardBus", CIS_LONGLINK_CB, 255},
59 {"Indirect access", CIS_INDIRECT, 255},
60 {"Configuration map for CardBus", CIS_CONF_MAP_CB, 255},
61 {"Configuration entry for CardBus", CIS_CONFIG_CB, 255},
62 {"Long link to next chain for MFC", CIS_LONGLINK_MFC, 255},
63 {"Base address register for CardBus", CIS_BAR, 6},
64 {"Checksum", CIS_CHECKSUM, 5},
65 {"Long link to attribute memory", CIS_LONGLINK_A, 4},
66 {"Long link to common memory", CIS_LONGLINK_C, 4},
67 {"Link target", CIS_LINKTARGET, 3},
68 {"No link", CIS_NOLINK, 0},
69 {"Version 1 info", CIS_INFO_V1, 255},
70 {"Alternate language string", CIS_ALTSTR, 255},
71 {"Attribute memory descriptor", CIS_MEM_ATTR, 255},
72 {"JEDEC descr for common memory", CIS_JEDEC_C, 255},
73 {"JEDEC descr for attribute memory", CIS_JEDEC_A, 255},
74 {"Configuration map", CIS_CONF_MAP, 255},
75 {"Configuration entry", CIS_CONFIG, 255},
76 {"Other conditions for common memory", CIS_DEVICE_OC, 255},
77 {"Other conditions for attribute memory", CIS_DEVICE_OA, 255},
78 {"Geometry info for common memory", CIS_DEVICEGEO, 255},
79 {"Geometry info for attribute memory", CIS_DEVICEGEO_A, 255},
80 {"Manufacturer ID", CIS_MANUF_ID, 4},
81 {"Functional ID", CIS_FUNC_ID, 2},
82 {"Functional EXT", CIS_FUNC_EXT, 255},
83 {"Software interleave", CIS_SW_INTERLV, 2},
84 {"Version 2 Info", CIS_VERS_2, 255},
85 {"Data format", CIS_FORMAT, 255},
86 {"Geometry", CIS_GEOMETRY, 4},
87 {"Byte order", CIS_BYTEORDER, 2},
88 {"Card init date", CIS_DATE, 4},
89 {"Battery replacement", CIS_BATTERY, 4},
90 {"Organization", CIS_ORG, 255},
91 {"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);
212 if (tp && tp->length == 4) {
213 offs = tpl32(tp->data);
215 printf("Checking long link at %zd (%s memory)\n",
216 offs, flag ? "Attribute" : "Common");
218 /* If a link was found, read the tuple list from it. */
219 if (ck_linktarget(fd, offs, flag)) {
220 tl = read_one_tuplelist(fd, flag, offs);
229 * If the primary list had no NOLINK tuple, and no LINKTARGET,
230 * then try to read a tuple list at common memory (offset 0).
232 if (find_tuple_in_list(tlist, CIS_NOLINK) == 0 &&
233 find_tuple_in_list(tlist, CIS_LINKTARGET) == 0 &&
234 ck_linktarget(fd, (off_t) 0, 0)) {
237 printf("Reading long link at %zd (%s memory)\n",
238 offs, flag ? "Attribute" : "Common");
240 tlist->next = read_one_tuplelist(fd, 0, offs);
246 * Read one tuple list from the card.
248 static struct tuple_list *
249 read_one_tuplelist(int fd, int flags, off_t offs)
251 struct tuple *tp, *last_tp = 0;
252 struct tuple_list *tl;
253 struct tuple_info *tinfo;
255 unsigned char code, length;
257 /* Check to see if this memory has already been scanned. */
258 for (tl = tlist; tl; tl = tl->next)
259 if (tl->offs == offs && tl->flags == (flags & MDF_ATTR))
261 tl = xmalloc(sizeof(*tl));
263 tl->flags = flags & MDF_ATTR;
264 ioctl(fd, PIOCRWFLAG, &flags);
265 lseek(fd, offs, SEEK_SET);
267 if (read(fd, &code, 1) != 1) {
268 warn("CIS code read");
272 if (code == CIS_NULL)
274 tp = xmalloc(sizeof(*tp));
279 if (read(fd, &length, 1) != 1) {
280 warn("CIS len read");
287 printf("Tuple code = 0x%x, len = %d\n", code, length);
289 if (length == 0xFF) {
290 length = tp->length = 0;
295 tp->data = xmalloc(length);
296 if (read(fd, tp->data, length) != length) {
303 * Check the tuple, and ignore it if it isn't in the table
304 * or the length is illegal.
306 tinfo = get_tuple_info(code);
307 if (tinfo != NULL && (tinfo->length != 255 && tinfo->length > length)) {
308 printf("code %s (%d) ignored\n", tuple_name(code), code);
311 if (tl->tuples == NULL)
316 } while (code != CIS_END && total < 1024);
321 * return true if the offset points to a LINKTARGET tuple.
324 ck_linktarget(int fd, off_t offs, int flag)
328 ioctl(fd, PIOCRWFLAG, &flag);
329 lseek(fd, offs, SEEK_SET);
330 if (read(fd, blk, 5) != 5)
332 if (blk[0] == CIS_LINKTARGET &&
342 * find_tuple_in_list - find a tuple within a
345 static struct tuple *
346 find_tuple_in_list(struct tuple_list *tl, unsigned char code)
350 for (tp = tl->tuples; tp; tp = tp->next)
351 if (tp->code == code)
357 * return table entry for code.
359 static struct tuple_info *
360 get_tuple_info(unsigned char code)
362 struct tuple_info *tp;
364 for (tp = tuple_info; tp->name; tp++)
365 if (tp->code == code)
371 tuple_name(unsigned char code)
373 struct tuple_info *tp;
375 tp = get_tuple_info(code);