2 * Copyright (c) 1995 Andrew McRae. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 static const char rcsid[] =
33 * Code cleanup, bug-fix and extension
34 * by Tatsumi Hosokawa <hosokawa@mt.cs.keio.ac.jp>
47 static int ck_linktarget(int, off_t, int);
48 static struct tuple_list *read_one_tuplelist(int, int, off_t);
49 static struct tuple_list *read_tuples(int);
50 static struct tuple *find_tuple_in_list(struct tuple_list *, unsigned char);
51 static struct tuple_info *get_tuple_info(unsigned char);
53 static struct tuple_info tuple_info[] = {
54 {"Null tuple", CIS_NULL, 0},
55 {"Common memory descriptor", CIS_MEM_COMMON, 255},
56 {"Long link to next chain for CardBus", CIS_LONGLINK_CB, 255},
57 {"Indirect access", CIS_INDIRECT, 255},
58 {"Configuration map for CardBus", CIS_CONF_MAP_CB, 255},
59 {"Configuration entry for CardBus", CIS_CONFIG_CB, 255},
60 {"Long link to next chain for MFC", CIS_LONGLINK_MFC, 255},
61 {"Base address register for CardBus", CIS_BAR, 6},
62 {"Checksum", CIS_CHECKSUM, 5},
63 {"Long link to attribute memory", CIS_LONGLINK_A, 4},
64 {"Long link to common memory", CIS_LONGLINK_C, 4},
65 {"Link target", CIS_LINKTARGET, 3},
66 {"No link", CIS_NOLINK, 0},
67 {"Version 1 info", CIS_INFO_V1, 255},
68 {"Alternate language string", CIS_ALTSTR, 255},
69 {"Attribute memory descriptor", CIS_MEM_ATTR, 255},
70 {"JEDEC descr for common memory", CIS_JEDEC_C, 255},
71 {"JEDEC descr for attribute memory", CIS_JEDEC_A, 255},
72 {"Configuration map", CIS_CONF_MAP, 255},
73 {"Configuration entry", CIS_CONFIG, 255},
74 {"Other conditions for common memory", CIS_DEVICE_OC, 255},
75 {"Other conditions for attribute memory", CIS_DEVICE_OA, 255},
76 {"Geometry info for common memory", CIS_DEVICEGEO, 255},
77 {"Geometry info for attribute memory", CIS_DEVICEGEO_A, 255},
78 {"Manufacturer ID", CIS_MANUF_ID, 4},
79 {"Functional ID", CIS_FUNC_ID, 2},
80 {"Functional EXT", CIS_FUNC_EXT, 255},
81 {"Software interleave", CIS_SW_INTERLV, 2},
82 {"Version 2 Info", CIS_VERS_2, 255},
83 {"Data format", CIS_FORMAT, 255},
84 {"Geometry", CIS_GEOMETRY, 4},
85 {"Byte order", CIS_BYTEORDER, 2},
86 {"Card init date", CIS_DATE, 4},
87 {"Battery replacement", CIS_BATTERY, 4},
88 {"Organization", CIS_ORG, 255},
89 {"Terminator", CIS_END, 0},
108 * After reading the tuples, decode the relevant ones.
114 return (read_tuples(fd));
118 * free_cis - delete cis entry.
121 freecis(struct tuple_list *tlist)
123 struct tuple_list *tl;
126 while ((tl = tlist) != 0) {
128 while ((tp = tl->tuples) != 0) {
129 tl->tuples = tp->next;
138 * Parse variable length value.
141 parse_num(int sz, u_char *p, u_char **q, int ofs)
155 num = tpl16(p) + ofs;
159 num = tpl24(p) + ofs;
164 num = tpl32(p) + ofs;
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
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.
189 static struct tuple_list *tlist;
191 static struct tuple_list *
194 struct tuple_list *tl = 0, *last_tl;
200 last_tl = tlist = read_one_tuplelist(fd, MDF_ATTR, (off_t) 0);
202 /* Now start processing the links (if any). */
205 tp = find_tuple_in_list(last_tl, CIS_LONGLINK_A);
208 tp = find_tuple_in_list(last_tl, CIS_LONGLINK_C);
210 if (tp && tp->length == 4) {
211 offs = tpl32(tp->data);
213 printf("Checking long link at %zd (%s memory)\n",
214 offs, flag ? "Attribute" : "Common");
216 /* If a link was found, read the tuple list from it. */
217 if (ck_linktarget(fd, offs, flag)) {
218 tl = read_one_tuplelist(fd, flag, offs);
227 * If the primary list had no NOLINK tuple, and no LINKTARGET,
228 * then try to read a tuple list at common memory (offset 0).
230 if (find_tuple_in_list(tlist, CIS_NOLINK) == 0 &&
231 find_tuple_in_list(tlist, CIS_LINKTARGET) == 0 &&
232 ck_linktarget(fd, (off_t) 0, 0)) {
235 printf("Reading long link at %zd (%s memory)\n",
236 offs, flag ? "Attribute" : "Common");
238 tlist->next = read_one_tuplelist(fd, 0, offs);
244 * Read one tuple list from the card.
246 static struct tuple_list *
247 read_one_tuplelist(int fd, int flags, off_t offs)
249 struct tuple *tp, *last_tp = 0;
250 struct tuple_list *tl;
251 struct tuple_info *tinfo;
253 unsigned char code, length;
255 /* Check to see if this memory has already been scanned. */
256 for (tl = tlist; tl; tl = tl->next)
257 if (tl->offs == offs && tl->flags == (flags & MDF_ATTR))
259 tl = xmalloc(sizeof(*tl));
261 tl->flags = flags & MDF_ATTR;
262 ioctl(fd, PIOCRWFLAG, &flags);
263 lseek(fd, offs, SEEK_SET);
265 if (read(fd, &code, 1) != 1) {
266 warn("CIS code read");
270 if (code == CIS_NULL)
272 tp = xmalloc(sizeof(*tp));
277 if (read(fd, &length, 1) != 1) {
278 warn("CIS len read");
285 printf("Tuple code = 0x%x, len = %d\n", code, length);
287 if (length == 0xFF) {
288 length = tp->length = 0;
293 tp->data = xmalloc(length);
294 if (read(fd, tp->data, length) != length) {
301 * Check the tuple, and ignore it if it isn't in the table
302 * or the length is illegal.
304 tinfo = get_tuple_info(code);
305 if (tinfo != NULL && (tinfo->length != 255 && tinfo->length > length)) {
306 printf("code %s (%d) ignored\n", tuple_name(code), code);
309 if (tl->tuples == NULL)
314 } while (code != CIS_END && total < 1024);
319 * return true if the offset points to a LINKTARGET tuple.
322 ck_linktarget(int fd, off_t offs, int flag)
326 ioctl(fd, PIOCRWFLAG, &flag);
327 lseek(fd, offs, SEEK_SET);
328 if (read(fd, blk, 5) != 5)
330 if (blk[0] == CIS_LINKTARGET &&
340 * find_tuple_in_list - find a tuple within a
343 static struct tuple *
344 find_tuple_in_list(struct tuple_list *tl, unsigned char code)
348 for (tp = tl->tuples; tp; tp = tp->next)
349 if (tp->code == code)
355 * return table entry for code.
357 static struct tuple_info *
358 get_tuple_info(unsigned char code)
360 struct tuple_info *tp;
362 for (tp = tuple_info; tp->name; tp++)
363 if (tp->code == code)
369 tuple_name(unsigned char code)
371 struct tuple_info *tp;
373 tp = get_tuple_info(code);