Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / pccard / pccardd / readcis.c
1 /*
2  * Copyright (c) 1995 Andrew McRae.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
14  *
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.
25  *
26  * $FreeBSD: src/usr.sbin/pccard/pccardd/readcis.c,v 1.17.2.4 2001/05/23 21:53:50 dmlb Exp $
27  * $DragonFly: src/usr.sbin/pccard/pccardd/Attic/readcis.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
28  */
29
30 /*
31  * Code cleanup, bug-fix and extension
32  * by Tatsumi Hosokawa <hosokawa@mt.cs.keio.ac.jp>
33  */
34
35 #include <err.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <sys/ioctl.h>
41
42 #include <pccard/cardinfo.h>
43 #include <pccard/cis.h>
44
45 #include "readcis.h"
46
47 #ifdef RATOCLAN
48 static int      rex5588 = 0;
49 #endif
50
51 static int read_attr(int, char *, int);
52 static int ck_linktarget(int, off_t, int);
53 static void cis_info(struct cis *, unsigned char *, int);
54 static void device_desc(unsigned char *, int, struct dev_mem *);
55 static void config_map(struct cis *, unsigned char *, int);
56 static void cis_config(struct cis *, unsigned char *, int);
57 static void cis_func_id(struct cis *, unsigned char *, int);
58 static void cis_network_ext(struct cis *, unsigned char *, int);
59 static struct tuple_list *read_one_tuplelist(int, int, off_t);
60 static struct tuple_list *read_tuples(int);
61 static struct tuple *find_tuple_in_list(struct tuple_list *, unsigned char);
62 static struct tuple_info *get_tuple_info(unsigned char);
63
64 static struct tuple_info tuple_info[] = {
65         {"Null tuple", 0x00, 0},
66         {"Common memory descriptor", 0x01, 255},
67         {"Long link to next chain for CardBus", 0x02, 255},
68         {"Indirect access", 0x03, 255},
69         {"Configuration map for CardBus", 0x04, 255},
70         {"Configuration entry for CardBus", 0x05, 255},
71         {"Long link to next chain for MFC", 0x06, 255},
72         {"Base address register for CardBus", 0x07, 6},
73         {"Checksum", 0x10, 5},
74         {"Long link to attribute memory", 0x11, 4},
75         {"Long link to common memory", 0x12, 4},
76         {"Link target", 0x13, 3},
77         {"No link", 0x14, 0},
78         {"Version 1 info", 0x15, 255},
79         {"Alternate language string", 0x16, 255},
80         {"Attribute memory descriptor", 0x17, 255},
81         {"JEDEC descr for common memory", 0x18, 255},
82         {"JEDEC descr for attribute memory", 0x19, 255},
83         {"Configuration map", 0x1A, 255},
84         {"Configuration entry", 0x1B, 255},
85         {"Other conditions for common memory", 0x1C, 255},
86         {"Other conditions for attribute memory", 0x1D, 255},
87         {"Geometry info for common memory", 0x1E, 255},
88         {"Geometry info for attribute memory", 0x1F, 255},
89         {"Manufacturer ID", 0x20, 4},
90         {"Functional ID", 0x21, 2},
91         {"Functional EXT", 0x22, 255},
92         {"Software interleave", 0x23, 2},
93         {"Version 2 Info", 0x40, 255},
94         {"Data format", 0x41, 255},
95         {"Geometry", 0x42, 4},
96         {"Byte order", 0x43, 2},
97         {"Card init date", 0x44, 4},
98         {"Battery replacement", 0x45, 4},
99         {"Organization", 0x46, 255},
100         {"Terminator", 0xFF, 0},
101         {0, 0, 0}
102 };
103
104 /*
105  *      After reading the tuples, decode the relevant ones.
106  */
107 struct cis *
108 readcis(int fd)
109 {
110         struct tuple_list *tl;
111         struct tuple *tp;
112         struct cis *cp;
113
114         cp = xmalloc(sizeof(*cp));
115         cp->tlist = read_tuples(fd);
116         if (cp->tlist == 0)
117                 return (NULL);
118
119         for (tl = cp->tlist; tl; tl = tl->next)
120                 for (tp = tl->tuples; tp; tp = tp->next) {
121 #if 0
122                         printf("tuple code = 0x%02x, data is\n", tp->code);
123                         dump(tp->data, tp->length);
124 #endif
125                         switch (tp->code) {
126                         case CIS_MEM_COMMON:    /* 0x01 */
127                                 device_desc(tp->data, tp->length, &cp->common_mem);
128                                 break;
129                         case CIS_INFO_V1:       /* 0x15 */
130                                 cis_info(cp, tp->data, tp->length);
131                                 break;
132                         case CIS_MEM_ATTR:      /* 0x17 */
133                                 device_desc(tp->data, tp->length, &cp->attr_mem);
134                                 break;
135                         case CIS_CONF_MAP:      /* 0x1A */
136                                 config_map(cp, tp->data, tp->length);
137                                 break;
138                         case CIS_CONFIG:        /* 0x1B */
139                                 cis_config(cp, tp->data, tp->length);
140                                 break;
141                         case CIS_FUNC_ID:       /* 0x21 */
142                                 cis_func_id(cp, tp->data, tp->length);
143                                 break;
144                         case CIS_FUNC_EXT:      /* 0x22 */
145                                 if (cp->func_id1 == 6)  /* LAN adaptor */
146                                         cis_network_ext(cp, tp->data, tp->length);
147                                 break;
148                         }
149                 }
150         return (cp);
151 }
152
153 /*
154  *      free_cis - delete cis entry.
155  */
156 void
157 freecis(struct cis *cp)
158 {
159         struct cis_ioblk *io;
160         struct cis_memblk *mem;
161         struct cis_config *conf;
162         struct tuple *tp;
163         struct tuple_list *tl;
164
165         while ((tl = cp->tlist) != 0) {
166                 cp->tlist = tl->next;
167                 while ((tp = tl->tuples) != 0) {
168                         tl->tuples = tp->next;
169                         if (tp->data)
170                                 free(tp->data);
171                 }
172         }
173
174         while ((conf = cp->conf) != 0) {
175                 cp->conf = conf->next;
176                 while ((io = conf->io) != 0) {
177                         conf->io = io->next;
178                         free(io);
179                 }
180                 while ((mem = conf->mem) != 0) {
181                         conf->mem = mem->next;
182                         free(mem);
183                 }
184                 free(conf);
185         }
186         free(cp);
187 }
188
189 /*
190  *      Fills in CIS version data.
191  */
192 static void
193 cis_info(struct cis *cp, unsigned char *p, int len)
194 {
195         cp->maj_v = *p++;
196         cp->min_v = *p++;
197         len -= 2;
198         if (cp->manuf) {
199                 free(cp->manuf);
200                 cp->manuf = NULL;
201         }
202         if (len > 1 && *p != 0xff) {
203                 cp->manuf = strdup(p);
204                 while (*p++ && --len > 0);
205         }
206         if (cp->vers) {
207                 free(cp->vers);
208                 cp->vers = NULL;
209         }
210         if (len > 1 && *p != 0xff) {
211                 cp->vers = strdup(p);
212                 while (*p++ && --len > 0);
213         } else {
214                 cp->vers = strdup("?");
215         }
216         if (cp->add_info1) {
217                 free(cp->add_info1);
218                 cp->add_info1 = NULL;
219         }
220         if (len > 1 && *p != 0xff) {
221                 cp->add_info1 = strdup(p);
222                 while (*p++ && --len > 0);
223         }
224         if (cp->add_info2) {
225                 free(cp->add_info2);
226                 cp->add_info2 = NULL;
227         }
228         if (len > 1 && *p != 0xff)
229                 cp->add_info2 = strdup(p);
230 }
231
232 /*
233  *      Fills in CIS function ID.
234  */
235 static void
236 cis_func_id(struct cis *cp, unsigned char *p, int len)
237 {
238         cp->func_id1 = *p++;
239         cp->func_id2 = *p++;
240 }
241
242 static void
243 cis_network_ext(struct cis *cp, unsigned char *p, int len)
244 {
245         int i;
246
247         switch (p[0]) {
248         case 4:         /* Node ID */
249                 if (len <= 2 || len < p[1] + 2)
250                         return;
251
252                 if (cp->lan_nid)
253                         free(cp->lan_nid);
254                 cp->lan_nid = xmalloc(p[1]);
255
256                 for (i = 0; i <= p[1]; i++)
257                         cp->lan_nid[i] = p[i + 1];
258                 break;
259         }
260 }
261
262 /*
263  *      "FUJITSU LAN Card (FMV-J182)" has broken CIS
264  */
265 static int
266 fmvj182_check(unsigned char *p)
267 {
268         char    manuf[BUFSIZ], vers[BUFSIZ];
269
270         p++;                    /* major version */
271         p++;                    /* minor version */
272         strncpy(manuf, p, sizeof(manuf) - 1);
273         while (*p++);
274         strncpy(vers, p, sizeof(vers) - 1);
275         if (!strcmp(manuf, "FUJITSU") && !strcmp(vers, "LAN Card(FMV-J182)"))
276                 return 1;
277         else
278                 return 0;
279 }
280
281 #ifdef RATOCLAN
282 /*
283  *      "RATOC LAN Card (REX-5588)" has broken CIS
284  */
285 static int
286 rex5588_check(unsigned char *p)
287 {
288         char    manuf[BUFSIZ], vers[BUFSIZ];
289
290         p++;                    /* major version */
291         p++;                    /* minor version */
292         strncpy(manuf, p, sizeof(manuf) - 1);
293         while (*p++);
294         strncpy(vers, p, sizeof(manuf) - 1);
295         if (!strcmp(manuf, "PCMCIA LAN MBH10304  ES"))
296                 return 1;
297         else
298                 return 0;
299 }
300 #endif
301
302 #ifdef HSSYNTH
303 /*
304  *      Broken CIS for "HITACHI MICROCOMPUTER SYSTEM LTD." "MSSHVPC02"
305  */
306 static int
307 hss_check(unsigned char *p)
308 {
309         char    manuf[BUFSIZ], vers[BUFSIZ];
310
311         p++;                    /* major version */
312         p++;                    /* minor version */
313         strncpy(manuf, p, sizeof(manuf) - 1);
314         while (*p++);
315         strncpy(vers, p, sizeof(vers) - 1);
316         if (!strcmp(manuf, "HITACHI MICROCOMPUTER SYSTEMS LTD.")
317          && !strcmp(vers, "MSSHVPC02"))
318                 return 1;
319         else
320                 return 0;
321 }
322 #endif  /* HSSYNTH */
323
324 /*
325  *      device_desc - decode device descriptor.
326  */
327 static void
328 device_desc(unsigned char *p, int len, struct dev_mem *dp)
329 {
330         while (len > 0 && *p != 0xFF) {
331                 dp->valid = 1;
332                 dp->type = (*p & 0xF0) >> 4;
333                 dp->wps = !!(*p & 0x8);
334                 dp->speed = *p & 7;
335                 p++;
336                 if (*p != 0xFF) {
337                         dp->addr = (*p >> 3) & 0xF;
338                         dp->units = *p & 7;
339                 }
340                 p++;
341                 len -= 2;
342         }
343 }
344
345 /*
346  *      configuration map of card control register.
347  */
348 static void
349 config_map(struct cis *cp, unsigned char *p, int len)
350 {
351         unsigned char *p1;
352         int rlen = (*p & 3) + 1;
353
354         p1 = p + 1;
355         cp->last_config = *p1++ & 0x3F;
356         cp->reg_addr = parse_num(rlen | 0x10, p1, &p1, 0);
357         cp->ccrs = *p1;
358 }
359
360 /*
361  *      Parse variable length value.
362  */
363 u_int
364 parse_num(int sz, u_char *p, u_char **q, int ofs)
365 {
366         u_int num = 0;
367
368         switch (sz) {   
369         case 0:
370         case 0x10:
371                 break;
372         case 1:
373         case 0x11:
374                 num = (*p++) + ofs;
375                 break;
376         case 2:
377         case 0x12:
378                 num = tpl16(p) + ofs;
379                 p += 2;
380                 break;
381         case 0x13:
382                 num = tpl24(p) + ofs;
383                 p += 3;
384                 break;
385         case 3:
386         case 0x14:
387                 num = tpl32(p) + ofs;
388                 p += 4;
389                 break;
390         }
391         if (q)
392                 *q = p;
393         return num;
394 }
395
396 /*
397  *      CIS config entry - Decode and build configuration entry.
398  */
399 static void
400 cis_config(struct cis *cp, unsigned char *p, int len)
401 {
402         int     x;
403         int     i, j;
404         struct cis_config *conf, *last;
405         unsigned char feat;
406
407         conf = xmalloc(sizeof(*conf));
408         if ((last = cp->conf) != 0) {
409                 while (last->next)
410                         last = last->next;
411                 last->next = conf;
412         } else
413                 cp->conf = conf;
414         conf->id = *p & 0x3F;   /* Config index */
415 #ifdef RATOCLAN
416         if (rex5588 && conf->id >= 0x08 && conf->id <= 0x1d)
417                 conf->id |= 0x20;
418 #endif
419         if (*p & 0x40)          /* Default flag */
420                 cp->def_config = conf;
421         if (*p++ & 0x80)
422                 p++;            /* Interface byte skip */
423         feat = *p++;            /* Features byte */
424         for (i = 0; i < CIS_FEAT_POWER(feat); i++) {
425                 unsigned char parms = *p++;
426
427                 conf->pwr = 1;
428                 for (j = 0; j < 8; j++)
429                         if (parms & (1 << j))
430                                 while (*p++ & 0x80);
431         }
432         if (feat & CIS_FEAT_TIMING) {
433                 conf->timing = 1;
434                 i = *p++;
435                 if (CIS_WAIT_SCALE(i) != 3)
436                         p++;
437                 if (CIS_READY_SCALE(i) != 7)
438                         p++;
439                 if (CIS_RESERVED_SCALE(i) != 7)
440                         p++;
441         }
442         if (feat & CIS_FEAT_I_O) {
443                 conf->iospace = 1;
444                 if (CIS_IO_RANGE & *p)
445                         conf->io_blks = CIS_IO_BLKS(p[1]) + 1;
446                 conf->io_addr = CIS_IO_ADDR(*p);
447                 conf->io_bus = (*p >> 5) & 3; /* CIS_IO_8BIT | CIS_IO_16BIT */
448                 if (*p++ & CIS_IO_RANGE) {
449                         struct cis_ioblk *io;
450                         struct cis_ioblk *last_io = NULL;
451
452                         i = CIS_IO_ADSZ(*p);
453                         j = CIS_IO_BLKSZ(*p++);
454                         for (x = 0; x < conf->io_blks; x++) {
455                                 io = xmalloc(sizeof(*io));
456                                 if (last_io)
457                                         last_io->next = io;
458                                 else
459                                         conf->io = io;
460                                 last_io = io;
461                                 io->addr = parse_num(i, p, &p, 0);
462                                 io->size = parse_num(j, p, &p, 1);
463                         }
464                 }
465         }
466         if (feat & CIS_FEAT_IRQ) {
467                 conf->irq = 1;
468                 conf->irqlevel = *p & 0xF;
469                 conf->irq_flags = *p & 0xF0;
470                 if (*p++ & CIS_IRQ_MASK) {
471                         conf->irq_mask = tpl16(p);
472                         p += 2;
473                 }
474         }
475         switch (CIS_FEAT_MEMORY(feat)) {
476         case CIS_FEAT_MEM_NONE:
477                 break;
478         case CIS_FEAT_MEM_LEN:
479                 conf->memspace = 1;
480                 conf->mem = xmalloc(sizeof(*conf->mem));
481                 conf->mem->length = tpl16(p) << 8;
482                 break;
483         case CIS_FEAT_MEM_ADDR:
484                 conf->memspace = 1;
485                 conf->mem = xmalloc(sizeof(*conf->mem));
486                 conf->mem->length = tpl16(p) << 8;
487                 conf->mem->address = tpl16(p + 2) << 8;
488                 break;
489         case CIS_FEAT_MEM_WIN: {
490                 struct cis_memblk *mem;
491                 struct cis_memblk *last_mem = NULL;
492
493                 conf->memspace = 1;
494                 x = *p++;
495                 conf->memwins = CIS_MEM_WINS(x);
496                 for (i = 0; i < conf->memwins; i++) {
497                         mem = xmalloc(sizeof(*mem));
498                         if (last_mem)
499                                 last_mem->next = mem;
500                         else
501                                 conf->mem = mem;
502                         last_mem = mem;
503                         mem->length = parse_num(CIS_MEM_LENSZ(x) | 0x10, p, &p, 0) << 8;
504                         mem->address = parse_num(CIS_MEM_ADDRSZ(x) | 0x10, p, &p, 0) << 8;
505                         if (x & CIS_MEM_HOST) {
506                                 mem->host_address = parse_num(CIS_MEM_ADDRSZ(x) | 0x10,
507                                                               p, &p, 0) << 8;
508                         }
509                 }
510                 break;
511             }
512         }
513         if (feat & CIS_FEAT_MISC) {
514                 conf->misc_valid = 1;
515                 conf->misc = *p++;
516         }
517 }
518
519 /*
520  *      Read the tuples from the card.
521  *      The processing of tuples is as follows:
522  *              - Read tuples at attribute memory, offset 0.
523  *              - If a CIS_END is the first tuple, look for
524  *                a tuple list at common memory offset 0; this list
525  *                must start with a LINKTARGET.
526  *              - If a long link tuple was encountered, execute the long
527  *                link.
528  *              - If a no-link tuple was seen, terminate processing.
529  *              - If no no-link tuple exists, and no long link tuple
530  *                exists while processing the primary tuple list,
531  *                then look for a LINKTARGET tuple in common memory.
532  *              - If a long link tuple is found in any list, then process
533  *                it. Only one link is allowed per list.
534  */
535 static struct tuple_list *tlist;
536
537 static struct tuple_list *
538 read_tuples(int fd)
539 {
540         struct tuple_list *tl = 0, *last_tl;
541         struct tuple *tp;
542         int     flag;
543         off_t   offs;
544
545         tlist = 0;
546         last_tl = tlist = read_one_tuplelist(fd, MDF_ATTR, (off_t) 0);
547
548         /* Now start processing the links (if any). */
549         do {
550                 flag = MDF_ATTR;
551                 tp = find_tuple_in_list(last_tl, CIS_LONGLINK_A);
552                 if (tp == 0) {
553                         flag = 0;
554                         tp = find_tuple_in_list(last_tl, CIS_LONGLINK_C);
555                 }
556                 if (tp && tp->length == 4) {
557                         offs = tpl32(tp->data);
558 #ifdef  DEBUG
559                         printf("Checking long link at %qd (%s memory)\n",
560                             offs, flag ? "Attribute" : "Common");
561 #endif
562                         /* If a link was found, read the tuple list from it. */
563                         if (ck_linktarget(fd, offs, flag)) {
564                                 tl = read_one_tuplelist(fd, flag, offs);
565                                 last_tl->next = tl;
566                                 last_tl = tl;
567                         }
568                 } else
569                         tl = 0;
570         } while (tl);
571
572         /*
573          * If the primary list had no NOLINK tuple, and no LINKTARGET,
574          * then try to read a tuple list at common memory (offset 0).
575          */
576         if (find_tuple_in_list(tlist, CIS_NOLINK) == 0 && tlist->next == 0 &&
577             ck_linktarget(fd, (off_t) 0, 0)) {
578 #ifdef  DEBUG
579                 printf("Reading long link at %qd (%s memory)\n",
580                     offs, flag ? "Attribute" : "Common");
581 #endif
582                 tlist->next = read_one_tuplelist(fd, 0, (off_t) 0);
583         }
584         return (tlist);
585 }
586
587 /*
588  *      Read one tuple list from the card.
589  */
590 static struct tuple_list *
591 read_one_tuplelist(int fd, int flags, off_t offs)
592 {
593         struct tuple *tp, *last_tp = 0;
594         struct tuple_list *tl;
595         struct tuple_info *tinfo;
596         int     total = 0;
597         unsigned char code, length;
598         int     fmvj182 = 0;
599 #ifdef HSSYNTH
600         int     hss = 0;
601 #endif  /* HSSYNTH */
602
603         /* Check to see if this memory has already been scanned. */
604         for (tl = tlist; tl; tl = tl->next)
605                 if (tl->offs == offs && tl->flags == (flags & MDF_ATTR))
606                         return (0);
607         tl = xmalloc(sizeof(*tl));
608         tl->offs = offs;
609         tl->flags = flags & MDF_ATTR;
610         ioctl(fd, PIOCRWFLAG, &flags);
611         lseek(fd, offs, SEEK_SET);
612         do {
613                 if (read_attr(fd, &code, 1) != 1) {
614                         warn("CIS code read");
615                         break;
616                 }
617                 total++;
618                 if (code == CIS_NULL)
619                         continue;
620                 tp = xmalloc(sizeof(*tp));
621                 tp->code = code;
622                 if (code == CIS_END)
623                         length = 0;
624                 else {
625                         if (read_attr(fd, &length, 1) != 1) {
626                                 warn("CIS len read");
627                                 break;
628                         }
629                         total++;
630                         if (fmvj182 && (code == 0x1b) && (length == 25))
631                                 length = 31;
632                 }
633                 tp->length = length;
634 #ifdef  DEBUG
635                 printf("Tuple code = 0x%x, len = %d\n", code, length);
636 #endif
637                 if (length == 0xFF) {
638                         length = tp->length = 0;
639                         code = CIS_END;
640                 }
641                 if (length != 0) {
642                         total += length;
643                         tp->data = xmalloc(length);
644                         if (read_attr(fd, tp->data, length) != length) {
645                                 warn("CIS read");
646                                 break;
647                         }
648                 }
649
650                 /*
651                  * Check the tuple, and ignore it if it isn't in the table
652                  * or the length is illegal.
653                  */
654                 tinfo = get_tuple_info(code);
655                 if (code == CIS_INFO_V1) {
656                         /* Hack for broken CIS of FMV-J182 Ethernet card */
657                         fmvj182 = fmvj182_check(tp->data);
658 #ifdef RATOCLAN
659                         /* Hack for RATOC LAN card */
660                         rex5588 = rex5588_check(tp->data);
661 #endif /* RATOCLAN */
662 #ifdef  HSSYNTH
663                         /* Hack for Hitachi Speech Synthesis card */
664                         hss = hss_check(tp->data);
665 #endif  /* HSSYNTH */
666                 }
667                 if (tinfo == NULL || (tinfo->length != 255 && tinfo->length > length)) {
668                         printf("code %s ignored\n", tuple_name(code));
669                         tp->code = CIS_NULL;
670                 }
671                 if (tl->tuples == NULL)
672                         tl->tuples = tp;
673                 else
674                         last_tp->next = tp;
675                 last_tp = tp;
676         } while (code != CIS_END && total < 1024);
677         return (tl);
678 }
679
680 /*
681  *      return true if the offset points to a LINKTARGET tuple.
682  */
683 static int
684 ck_linktarget(int fd, off_t offs, int flag)
685 {
686         char    blk[5];
687
688         ioctl(fd, PIOCRWFLAG, &flag);
689         lseek(fd, offs, SEEK_SET);
690         if (read_attr(fd, blk, 5) != 5)
691                 return (0);
692         if (blk[0] == 0x13 &&
693             blk[1] == 0x3 &&
694             blk[2] == 'C' &&
695             blk[3] == 'I' &&
696             blk[4] == 'S')
697                 return (1);
698         return (0);
699 }
700
701 /*
702  *      find_tuple_in_list - find a tuple within a
703  *      single tuple list.
704  */
705 static struct tuple *
706 find_tuple_in_list(struct tuple_list *tl, unsigned char code)
707 {
708         struct tuple *tp;
709
710         for (tp = tl->tuples; tp; tp = tp->next)
711                 if (tp->code == code)
712                         break;
713         return (tp);
714 }
715
716 static int
717 read_attr(int fd, char *bp, int len)
718 {
719         char    blk[1024], *p = blk;
720         int     i, l;
721
722         if (len > sizeof(blk) / 2)
723                 len = sizeof(blk) / 2;
724         l = i = read(fd, blk, len * 2);
725         if (i <= 0) {
726                 printf("Read return %d bytes (expected %d)\n", i, len * 2);
727                 return (i);
728         }
729         while (i > 0) {
730                 *bp++ = *p++;
731                 p++;
732                 i -= 2;
733         }
734         return (l / 2);
735 }
736
737 /*
738  *      return table entry for code.
739  */
740 static struct tuple_info *
741 get_tuple_info(unsigned char code)
742 {
743         struct tuple_info *tp;
744
745         for (tp = tuple_info; tp->name; tp++)
746                 if (tp->code == code)
747                         return (tp);
748         printf("Code %d not found\n", code);
749         return (0);
750 }
751
752 char *
753 tuple_name(unsigned char code)
754 {
755         struct tuple_info *tp;
756
757         tp = get_tuple_info(code);
758         if (tp)
759                 return (tp->name);
760         return ("Unknown");
761 }