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 #ifndef lint 30 static const char rcsid[] = 31 "$FreeBSD$"; 32 #endif /* not lint */ 33 34 /* 35 * Code cleanup, bug-fix and extension 36 * by Tatsumi Hosokawa <hosokawa@mt.cs.keio.ac.jp> 37 */ 38 39 #include <err.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <unistd.h> 44 45 #include "cardinfo.h" 46 #include "cis.h" 47 #include "readcis.h" 48 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); 54 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}, 92 {0, 0, 0} 93 }; 94 95 static void * 96 xmalloc(int sz) 97 { 98 void *p; 99 100 sz = (sz + 7) & ~7; 101 p = malloc(sz); 102 if (p) 103 bzero(p, sz); 104 else 105 errx(1, "malloc"); 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 == 0) { 209 flag = 0; 210 tp = find_tuple_in_list(last_tl, CIS_LONGLINK_C); 211 } 212 if (tp && tp->length == 4) { 213 offs = tpl32(tp->data); 214 #ifdef DEBUG 215 printf("Checking long link at %zd (%s memory)\n", 216 offs, flag ? "Attribute" : "Common"); 217 #endif 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); 221 last_tl->next = tl; 222 last_tl = tl; 223 } 224 } else 225 tl = 0; 226 } while (tl); 227 228 /* 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). 231 */ 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)) { 235 offs = 0; 236 #ifdef DEBUG 237 printf("Reading long link at %zd (%s memory)\n", 238 offs, flag ? "Attribute" : "Common"); 239 #endif 240 tlist->next = read_one_tuplelist(fd, 0, offs); 241 } 242 return (tlist); 243 } 244 245 /* 246 * Read one tuple list from the card. 247 */ 248 static struct tuple_list * 249 read_one_tuplelist(int fd, int flags, off_t offs) 250 { 251 struct tuple *tp, *last_tp = 0; 252 struct tuple_list *tl; 253 struct tuple_info *tinfo; 254 int total = 0; 255 unsigned char code, length; 256 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)) 260 return (0); 261 tl = xmalloc(sizeof(*tl)); 262 tl->offs = offs; 263 tl->flags = flags & MDF_ATTR; 264 ioctl(fd, PIOCRWFLAG, &flags); 265 lseek(fd, offs, SEEK_SET); 266 do { 267 if (read(fd, &code, 1) != 1) { 268 warn("CIS code read"); 269 break; 270 } 271 total++; 272 if (code == CIS_NULL) 273 continue; 274 tp = xmalloc(sizeof(*tp)); 275 tp->code = code; 276 if (code == CIS_END) 277 length = 0; 278 else { 279 if (read(fd, &length, 1) != 1) { 280 warn("CIS len read"); 281 break; 282 } 283 total++; 284 } 285 tp->length = length; 286 #ifdef DEBUG 287 printf("Tuple code = 0x%x, len = %d\n", code, length); 288 #endif 289 if (length == 0xFF) { 290 length = tp->length = 0; 291 code = CIS_END; 292 } 293 if (length != 0) { 294 total += length; 295 tp->data = xmalloc(length); 296 if (read(fd, tp->data, length) != length) { 297 warn("CIS read"); 298 break; 299 } 300 } 301 302 /* 303 * Check the tuple, and ignore it if it isn't in the table 304 * or the length is illegal. 305 */ 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); 309 tp->code = CIS_NULL; 310 } 311 if (tl->tuples == NULL) 312 tl->tuples = tp; 313 else 314 last_tp->next = tp; 315 last_tp = tp; 316 } while (code != CIS_END && total < 1024); 317 return (tl); 318 } 319 320 /* 321 * return true if the offset points to a LINKTARGET tuple. 322 */ 323 static int 324 ck_linktarget(int fd, off_t offs, int flag) 325 { 326 char blk[5]; 327 328 ioctl(fd, PIOCRWFLAG, &flag); 329 lseek(fd, offs, SEEK_SET); 330 if (read(fd, blk, 5) != 5) 331 return (0); 332 if (blk[0] == CIS_LINKTARGET && 333 blk[1] == 0x3 && 334 blk[2] == 'C' && 335 blk[3] == 'I' && 336 blk[4] == 'S') 337 return (1); 338 return (0); 339 } 340 341 /* 342 * find_tuple_in_list - find a tuple within a 343 * single tuple list. 344 */ 345 static struct tuple * 346 find_tuple_in_list(struct tuple_list *tl, unsigned char code) 347 { 348 struct tuple *tp; 349 350 for (tp = tl->tuples; tp; tp = tp->next) 351 if (tp->code == code) 352 break; 353 return (tp); 354 } 355 356 /* 357 * return table entry for code. 358 */ 359 static struct tuple_info * 360 get_tuple_info(unsigned char code) 361 { 362 struct tuple_info *tp; 363 364 for (tp = tuple_info; tp->name; tp++) 365 if (tp->code == code) 366 return (tp); 367 return (0); 368 } 369 370 const char * 371 tuple_name(unsigned char code) 372 { 373 struct tuple_info *tp; 374 375 tp = get_tuple_info(code); 376 if (tp) 377 return (tp->name); 378 return ("Unknown"); 379 } 380