xref: /freebsd/usr.sbin/dumpcis/readcis.c (revision 1d386b48)
11de7b4b8SPedro F. Giffuni /*-
21de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
31de7b4b8SPedro F. Giffuni  *
40738c00eSWarner Losh  * Copyright (c) 1995 Andrew McRae.  All rights reserved.
50738c00eSWarner Losh  *
60738c00eSWarner Losh  * Redistribution and use in source and binary forms, with or without
70738c00eSWarner Losh  * modification, are permitted provided that the following conditions
80738c00eSWarner Losh  * are met:
90738c00eSWarner Losh  * 1. Redistributions of source code must retain the above copyright
100738c00eSWarner Losh  *    notice, this list of conditions and the following disclaimer.
110738c00eSWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
120738c00eSWarner Losh  *    notice, this list of conditions and the following disclaimer in the
130738c00eSWarner Losh  *    documentation and/or other materials provided with the distribution.
140738c00eSWarner Losh  * 3. The name of the author may not be used to endorse or promote products
150738c00eSWarner Losh  *    derived from this software without specific prior written permission.
160738c00eSWarner Losh  *
170738c00eSWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
180738c00eSWarner Losh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
190738c00eSWarner Losh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
200738c00eSWarner Losh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
210738c00eSWarner Losh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
220738c00eSWarner Losh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
230738c00eSWarner Losh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
240738c00eSWarner Losh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
250738c00eSWarner Losh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
260738c00eSWarner Losh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
270738c00eSWarner Losh  */
280738c00eSWarner Losh 
295066eaebSWarner Losh #include <sys/cdefs.h>
300738c00eSWarner Losh /*
310738c00eSWarner Losh  * Code cleanup, bug-fix and extension
320738c00eSWarner Losh  * by Tatsumi Hosokawa <hosokawa@mt.cs.keio.ac.jp>
330738c00eSWarner Losh  */
340738c00eSWarner Losh 
355066eaebSWarner Losh #include <assert.h>
360738c00eSWarner Losh #include <err.h>
370738c00eSWarner Losh #include <stdio.h>
380738c00eSWarner Losh #include <stdlib.h>
390738c00eSWarner Losh #include <string.h>
400738c00eSWarner Losh #include <unistd.h>
410738c00eSWarner Losh 
42f7911ae5SWarner Losh #include "cardinfo.h"
43f7911ae5SWarner Losh #include "cis.h"
440738c00eSWarner Losh #include "readcis.h"
450738c00eSWarner Losh 
460738c00eSWarner Losh static int ck_linktarget(int, off_t, int);
470738c00eSWarner Losh static struct tuple_list *read_one_tuplelist(int, int, off_t);
480738c00eSWarner Losh static struct tuple_list *read_tuples(int);
490738c00eSWarner Losh static struct tuple *find_tuple_in_list(struct tuple_list *, unsigned char);
500738c00eSWarner Losh static struct tuple_info *get_tuple_info(unsigned char);
510738c00eSWarner Losh 
525066eaebSWarner Losh #define LENGTH_ANY 255
535066eaebSWarner Losh 
540738c00eSWarner Losh static struct tuple_info tuple_info[] = {
556b88fc62SWarner Losh 	{"Null tuple", CIS_NULL, 0},
565066eaebSWarner Losh 	{"Common memory descriptor", CIS_MEM_COMMON, LENGTH_ANY},
575066eaebSWarner Losh 	{"Long link to next chain for CardBus", CIS_LONGLINK_CB, LENGTH_ANY},
585066eaebSWarner Losh 	{"Indirect access", CIS_INDIRECT, LENGTH_ANY},
595066eaebSWarner Losh 	{"Configuration map for CardBus", CIS_CONF_MAP_CB, LENGTH_ANY},
605066eaebSWarner Losh 	{"Configuration entry for CardBus", CIS_CONFIG_CB, LENGTH_ANY},
615066eaebSWarner Losh 	{"Long link to next chain for MFC", CIS_LONGLINK_MFC, LENGTH_ANY},
626b88fc62SWarner Losh 	{"Base address register for CardBus", CIS_BAR, 6},
636b88fc62SWarner Losh 	{"Checksum", CIS_CHECKSUM, 5},
646b88fc62SWarner Losh 	{"Long link to attribute memory", CIS_LONGLINK_A, 4},
656b88fc62SWarner Losh 	{"Long link to common memory", CIS_LONGLINK_C, 4},
666b88fc62SWarner Losh 	{"Link target", CIS_LINKTARGET, 3},
676b88fc62SWarner Losh 	{"No link", CIS_NOLINK, 0},
685066eaebSWarner Losh 	{"Version 1 info", CIS_INFO_V1, LENGTH_ANY},
695066eaebSWarner Losh 	{"Alternate language string", CIS_ALTSTR, LENGTH_ANY},
705066eaebSWarner Losh 	{"Attribute memory descriptor", CIS_MEM_ATTR, LENGTH_ANY},
715066eaebSWarner Losh 	{"JEDEC descr for common memory", CIS_JEDEC_C, LENGTH_ANY},
725066eaebSWarner Losh 	{"JEDEC descr for attribute memory", CIS_JEDEC_A, LENGTH_ANY},
735066eaebSWarner Losh 	{"Configuration map", CIS_CONF_MAP, LENGTH_ANY},
745066eaebSWarner Losh 	{"Configuration entry", CIS_CONFIG, LENGTH_ANY},
755066eaebSWarner Losh 	{"Other conditions for common memory", CIS_DEVICE_OC, LENGTH_ANY},
765066eaebSWarner Losh 	{"Other conditions for attribute memory", CIS_DEVICE_OA, LENGTH_ANY},
775066eaebSWarner Losh 	{"Geometry info for common memory", CIS_DEVICEGEO, LENGTH_ANY},
785066eaebSWarner Losh 	{"Geometry info for attribute memory", CIS_DEVICEGEO_A, LENGTH_ANY},
796b88fc62SWarner Losh 	{"Manufacturer ID", CIS_MANUF_ID, 4},
806b88fc62SWarner Losh 	{"Functional ID", CIS_FUNC_ID, 2},
815066eaebSWarner Losh 	{"Functional EXT", CIS_FUNC_EXT, LENGTH_ANY},
826b88fc62SWarner Losh 	{"Software interleave", CIS_SW_INTERLV, 2},
835066eaebSWarner Losh 	{"Version 2 Info", CIS_VERS_2, LENGTH_ANY},
845066eaebSWarner Losh 	{"Data format", CIS_FORMAT, LENGTH_ANY},
856b88fc62SWarner Losh 	{"Geometry", CIS_GEOMETRY, 4},
866b88fc62SWarner Losh 	{"Byte order", CIS_BYTEORDER, 2},
876b88fc62SWarner Losh 	{"Card init date", CIS_DATE, 4},
886b88fc62SWarner Losh 	{"Battery replacement", CIS_BATTERY, 4},
895066eaebSWarner Losh 	{"Organization", CIS_ORG, LENGTH_ANY},
906b88fc62SWarner Losh 	{"Terminator", CIS_END, 0},
910738c00eSWarner Losh 	{0, 0, 0}
920738c00eSWarner Losh };
930738c00eSWarner Losh 
94c1d393d2SWarner Losh static void *
xmalloc(int sz)95c1d393d2SWarner Losh xmalloc(int sz)
96c1d393d2SWarner Losh {
97c1d393d2SWarner Losh 	void   *p;
98c1d393d2SWarner Losh 
99c1d393d2SWarner Losh 	sz = (sz + 7) & ~7;
100c1d393d2SWarner Losh 	p = malloc(sz);
1015066eaebSWarner Losh 	if (p == NULL)
102c1d393d2SWarner Losh 		errx(1, "malloc");
1035066eaebSWarner Losh 	bzero(p, sz);
104c1d393d2SWarner Losh 	return (p);
105c1d393d2SWarner Losh }
106c1d393d2SWarner Losh 
1070738c00eSWarner Losh /*
1080738c00eSWarner Losh  *	After reading the tuples, decode the relevant ones.
1090738c00eSWarner Losh  */
1102d134deaSWarner Losh struct tuple_list *
readcis(int fd)1110738c00eSWarner Losh readcis(int fd)
1120738c00eSWarner Losh {
1130738c00eSWarner Losh 
1142d134deaSWarner Losh 	return (read_tuples(fd));
1150738c00eSWarner Losh }
1160738c00eSWarner Losh 
1170738c00eSWarner Losh /*
1180738c00eSWarner Losh  *	free_cis - delete cis entry.
1190738c00eSWarner Losh  */
1200738c00eSWarner Losh void
freecis(struct tuple_list * tlist)1212d134deaSWarner Losh freecis(struct tuple_list *tlist)
1220738c00eSWarner Losh {
1230738c00eSWarner Losh 	struct tuple_list *tl;
1242d134deaSWarner Losh 	struct tuple *tp;
1250738c00eSWarner Losh 
1262d134deaSWarner Losh 	while ((tl = tlist) != 0) {
1272d134deaSWarner Losh 		tlist = tl->next;
1280738c00eSWarner Losh 		while ((tp = tl->tuples) != 0) {
1290738c00eSWarner Losh 			tl->tuples = tp->next;
1300738c00eSWarner Losh 			free(tp->data);
1312d134deaSWarner Losh 			free(tp);
1320738c00eSWarner Losh 		}
1332d134deaSWarner Losh 		free(tl);
1340738c00eSWarner Losh 	}
1350738c00eSWarner Losh }
1360738c00eSWarner Losh 
1370738c00eSWarner Losh /*
1380738c00eSWarner Losh  *	Parse variable length value.
1390738c00eSWarner Losh  */
1400738c00eSWarner Losh u_int
parse_num(int sz,u_char * p,u_char ** q,int ofs)1410738c00eSWarner Losh parse_num(int sz, u_char *p, u_char **q, int ofs)
1420738c00eSWarner Losh {
1430738c00eSWarner Losh 	u_int num = 0;
1440738c00eSWarner Losh 
1450738c00eSWarner Losh 	switch (sz) {
1460738c00eSWarner Losh 	case 0:
1470738c00eSWarner Losh 	case 0x10:
1480738c00eSWarner Losh 		break;
1490738c00eSWarner Losh 	case 1:
1500738c00eSWarner Losh 	case 0x11:
1510738c00eSWarner Losh 		num = (*p++) + ofs;
1520738c00eSWarner Losh 		break;
1530738c00eSWarner Losh 	case 2:
1540738c00eSWarner Losh 	case 0x12:
1550738c00eSWarner Losh 		num = tpl16(p) + ofs;
1560738c00eSWarner Losh 		p += 2;
1570738c00eSWarner Losh 		break;
1580738c00eSWarner Losh 	case 0x13:
1590738c00eSWarner Losh 		num = tpl24(p) + ofs;
1600738c00eSWarner Losh 		p += 3;
1610738c00eSWarner Losh 		break;
1620738c00eSWarner Losh 	case 3:
1630738c00eSWarner Losh 	case 0x14:
1640738c00eSWarner Losh 		num = tpl32(p) + ofs;
1650738c00eSWarner Losh 		p += 4;
1660738c00eSWarner Losh 		break;
1670738c00eSWarner Losh 	}
1680738c00eSWarner Losh 	if (q)
1690738c00eSWarner Losh 		*q = p;
1700738c00eSWarner Losh 	return num;
1710738c00eSWarner Losh }
1720738c00eSWarner Losh 
1730738c00eSWarner Losh /*
1740738c00eSWarner Losh  *	Read the tuples from the card.
1750738c00eSWarner Losh  *	The processing of tuples is as follows:
1760738c00eSWarner Losh  *		- Read tuples at attribute memory, offset 0.
1770738c00eSWarner Losh  *		- If a CIS_END is the first tuple, look for
1780738c00eSWarner Losh  *		  a tuple list at common memory offset 0; this list
1790738c00eSWarner Losh  *		  must start with a LINKTARGET.
1800738c00eSWarner Losh  *		- If a long link tuple was encountered, execute the long
1810738c00eSWarner Losh  *		  link.
1820738c00eSWarner Losh  *		- If a no-link tuple was seen, terminate processing.
1830738c00eSWarner Losh  *		- If no no-link tuple exists, and no long link tuple
1840738c00eSWarner Losh  *		  exists while processing the primary tuple list,
1850738c00eSWarner Losh  *		  then look for a LINKTARGET tuple in common memory.
1860738c00eSWarner Losh  *		- If a long link tuple is found in any list, then process
1870738c00eSWarner Losh  *		  it. Only one link is allowed per list.
1880738c00eSWarner Losh  */
1890738c00eSWarner Losh static struct tuple_list *tlist;
1900738c00eSWarner Losh 
1910738c00eSWarner Losh static struct tuple_list *
read_tuples(int fd)1920738c00eSWarner Losh read_tuples(int fd)
1930738c00eSWarner Losh {
1940738c00eSWarner Losh 	struct tuple_list *tl = 0, *last_tl;
1950738c00eSWarner Losh 	struct tuple *tp;
1960738c00eSWarner Losh 	int     flag;
1970738c00eSWarner Losh 	off_t   offs;
1980738c00eSWarner Losh 
1990738c00eSWarner Losh 	tlist = 0;
2000738c00eSWarner Losh 	last_tl = tlist = read_one_tuplelist(fd, MDF_ATTR, (off_t) 0);
2010738c00eSWarner Losh 
2020738c00eSWarner Losh 	/* Now start processing the links (if any). */
2030738c00eSWarner Losh 	do {
2040738c00eSWarner Losh 		flag = MDF_ATTR;
2050738c00eSWarner Losh 		tp = find_tuple_in_list(last_tl, CIS_LONGLINK_A);
2065066eaebSWarner Losh 		if (tp == NULL) {
2070738c00eSWarner Losh 			flag = 0;
2080738c00eSWarner Losh 			tp = find_tuple_in_list(last_tl, CIS_LONGLINK_C);
2090738c00eSWarner Losh 		}
2105066eaebSWarner Losh 
2115066eaebSWarner Losh 		if (tp == NULL || tp->length != 4)
2125066eaebSWarner Losh 			break;
2135066eaebSWarner Losh 
2145066eaebSWarner Losh 		offs = (uint32_t)tpl32(tp->data);
2150738c00eSWarner Losh #ifdef	DEBUG
2162d134deaSWarner Losh 		printf("Checking long link at %zd (%s memory)\n",
2170738c00eSWarner Losh 		    offs, flag ? "Attribute" : "Common");
2180738c00eSWarner Losh #endif
2195066eaebSWarner Losh 		/*
2205066eaebSWarner Losh 		 * If a link was found, it looks sane read the tuple list from it.
2215066eaebSWarner Losh 		 */
2225066eaebSWarner Losh 		if (offs > 0 && offs < 32 * 1024 && ck_linktarget(fd, offs, flag)) {
2230738c00eSWarner Losh 			tl = read_one_tuplelist(fd, flag, offs);
2240738c00eSWarner Losh 			last_tl->next = tl;
2250738c00eSWarner Losh 			last_tl = tl;
2260738c00eSWarner Losh 		}
2270738c00eSWarner Losh 	} while (tl);
2280738c00eSWarner Losh 
2290738c00eSWarner Losh 	/*
2305066eaebSWarner Losh 	 * If the primary list had no NOLINK tuple, and no LINKTARGET, then try
2315066eaebSWarner Losh 	 * to read a tuple list at common memory (offset 0).
2320738c00eSWarner Losh 	 */
2336b88fc62SWarner Losh 	if (find_tuple_in_list(tlist, CIS_NOLINK) == 0 &&
2346b88fc62SWarner Losh 	    find_tuple_in_list(tlist, CIS_LINKTARGET) == 0 &&
2350738c00eSWarner Losh 	    ck_linktarget(fd, (off_t) 0, 0)) {
2360738c00eSWarner Losh #ifdef	DEBUG
2375066eaebSWarner Losh 		printf("Reading long link at 0 (Common memory)\n");
2380738c00eSWarner Losh #endif
2395066eaebSWarner Losh 		tlist->next = read_one_tuplelist(fd, 0, 0);
2400738c00eSWarner Losh 	}
2410738c00eSWarner Losh 	return (tlist);
2420738c00eSWarner Losh }
2430738c00eSWarner Losh 
2440738c00eSWarner Losh /*
2450738c00eSWarner Losh  *	Read one tuple list from the card.
2460738c00eSWarner Losh  */
2470738c00eSWarner Losh static struct tuple_list *
read_one_tuplelist(int fd,int flags,off_t offs)2480738c00eSWarner Losh read_one_tuplelist(int fd, int flags, off_t offs)
2490738c00eSWarner Losh {
2500738c00eSWarner Losh 	struct tuple *tp, *last_tp = 0;
2510738c00eSWarner Losh 	struct tuple_list *tl;
2520738c00eSWarner Losh 	struct tuple_info *tinfo;
2530738c00eSWarner Losh 	int     total = 0;
2540738c00eSWarner Losh 	unsigned char code, length;
2550738c00eSWarner Losh 
2560738c00eSWarner Losh 	/* Check to see if this memory has already been scanned. */
2570738c00eSWarner Losh 	for (tl = tlist; tl; tl = tl->next)
2580738c00eSWarner Losh 		if (tl->offs == offs && tl->flags == (flags & MDF_ATTR))
2590738c00eSWarner Losh 			return (0);
2600738c00eSWarner Losh 	tl = xmalloc(sizeof(*tl));
2610738c00eSWarner Losh 	tl->offs = offs;
2620738c00eSWarner Losh 	tl->flags = flags & MDF_ATTR;
2635066eaebSWarner Losh 	if (ioctl(fd, PIOCRWFLAG, &flags) < 0)
2645066eaebSWarner Losh 		err(1, "Setting flag to rad %s memory failed",
2655066eaebSWarner Losh 		    flags ? "attribute" : "common");
2665066eaebSWarner Losh 	if (lseek(fd, offs, SEEK_SET) < 0)
2675066eaebSWarner Losh 		err(1, "Unable to seek to memory offset %ju",
2685066eaebSWarner Losh 		    (uintmax_t)offs);
2690738c00eSWarner Losh 	do {
2705066eaebSWarner Losh 		if (read(fd, &code, 1) != 1)
2715066eaebSWarner Losh 			errx(1, "CIS code read");
2720738c00eSWarner Losh 		total++;
2730738c00eSWarner Losh 		if (code == CIS_NULL)
2740738c00eSWarner Losh 			continue;
2750738c00eSWarner Losh 		tp = xmalloc(sizeof(*tp));
2760738c00eSWarner Losh 		tp->code = code;
2770738c00eSWarner Losh 		if (code == CIS_END)
2780738c00eSWarner Losh 			length = 0;
2790738c00eSWarner Losh 		else {
2805066eaebSWarner Losh 			if (read(fd, &length, 1) != 1)
2815066eaebSWarner Losh 				errx(1, "CIS len read");
2820738c00eSWarner Losh 			total++;
2830738c00eSWarner Losh 		}
2840738c00eSWarner Losh #ifdef	DEBUG
2850738c00eSWarner Losh 		printf("Tuple code = 0x%x, len = %d\n", code, length);
2860738c00eSWarner Losh #endif
2875066eaebSWarner Losh 
2885066eaebSWarner Losh 		/*
2895066eaebSWarner Losh 		 * A length of 255 is invalid, all others are valid. Treat a
2905066eaebSWarner Losh 		 * length of 255 as the end of the list. Some cards don't have a
2915066eaebSWarner Losh 		 * CIS_END at the end. These work on other systems because the
2925066eaebSWarner Losh 		 * end of the CIS eventually sees an area that's not decoded and
2935066eaebSWarner Losh 		 * read back as 0xff.
2945066eaebSWarner Losh 		 */
2950738c00eSWarner Losh 		if (length == 0xFF) {
2965066eaebSWarner Losh 			length = 0;
2970738c00eSWarner Losh 			code = CIS_END;
2980738c00eSWarner Losh 		}
2995066eaebSWarner Losh 		assert(length < 0xff);
3000738c00eSWarner Losh 
3010738c00eSWarner Losh 		/*
3020738c00eSWarner Losh 		 * Check the tuple, and ignore it if it isn't in the table
3030738c00eSWarner Losh 		 * or the length is illegal.
3040738c00eSWarner Losh 		 */
3050738c00eSWarner Losh 		tinfo = get_tuple_info(code);
3065066eaebSWarner Losh 		if (tinfo == NULL || (tinfo->length != LENGTH_ANY && tinfo->length > length)) {
3072d134deaSWarner Losh 			printf("code %s (%d) ignored\n", tuple_name(code), code);
3085066eaebSWarner Losh 			continue;
3090738c00eSWarner Losh 		}
3105066eaebSWarner Losh 		tp->length = length;
3115066eaebSWarner Losh 		if (length != 0) {
3125066eaebSWarner Losh 			total += length;
3135066eaebSWarner Losh 			tp->data = xmalloc(length);
3145066eaebSWarner Losh 			if (read(fd, tp->data, length) != length)
3155066eaebSWarner Losh 				errx(1, "Can't read CIS data");
3165066eaebSWarner Losh 		}
3175066eaebSWarner Losh 
3185066eaebSWarner Losh 		if (last_tp != NULL)
3190738c00eSWarner Losh 			last_tp->next = tp;
3205066eaebSWarner Losh 		if (tl->tuples == NULL) {
3215066eaebSWarner Losh 			tl->tuples = tp;
3225066eaebSWarner Losh 			tp->next = NULL;
3235066eaebSWarner Losh 		}
3240738c00eSWarner Losh 		last_tp = tp;
3250738c00eSWarner Losh 	} while (code != CIS_END && total < 1024);
3260738c00eSWarner Losh 	return (tl);
3270738c00eSWarner Losh }
3280738c00eSWarner Losh 
3290738c00eSWarner Losh /*
3300738c00eSWarner Losh  *	return true if the offset points to a LINKTARGET tuple.
3310738c00eSWarner Losh  */
3320738c00eSWarner Losh static int
ck_linktarget(int fd,off_t offs,int flag)3330738c00eSWarner Losh ck_linktarget(int fd, off_t offs, int flag)
3340738c00eSWarner Losh {
3350738c00eSWarner Losh 	char    blk[5];
3360738c00eSWarner Losh 
337be5f9a5aSLi-Wen Hsu 	if (ioctl(fd, PIOCRWFLAG, &flag) < 0)
3385066eaebSWarner Losh 		err(1, "Setting flag to rad %s memory failed",
339be5f9a5aSLi-Wen Hsu 		    flag ? "attribute" : "common");
3405066eaebSWarner Losh 	if (lseek(fd, offs, SEEK_SET) < 0)
3415066eaebSWarner Losh 		err(1, "Unable to seek to memory offset %ju",
3425066eaebSWarner Losh 		    (uintmax_t)offs);
343c1d393d2SWarner Losh 	if (read(fd, blk, 5) != 5)
3440738c00eSWarner Losh 		return (0);
3456b88fc62SWarner Losh 	if (blk[0] == CIS_LINKTARGET &&
3460738c00eSWarner Losh 	    blk[1] == 0x3 &&
3470738c00eSWarner Losh 	    blk[2] == 'C' &&
3480738c00eSWarner Losh 	    blk[3] == 'I' &&
3490738c00eSWarner Losh 	    blk[4] == 'S')
3500738c00eSWarner Losh 		return (1);
3510738c00eSWarner Losh 	return (0);
3520738c00eSWarner Losh }
3530738c00eSWarner Losh 
3540738c00eSWarner Losh /*
3550738c00eSWarner Losh  *	find_tuple_in_list - find a tuple within a
3560738c00eSWarner Losh  *	single tuple list.
3570738c00eSWarner Losh  */
3580738c00eSWarner Losh static struct tuple *
find_tuple_in_list(struct tuple_list * tl,unsigned char code)3590738c00eSWarner Losh find_tuple_in_list(struct tuple_list *tl, unsigned char code)
3600738c00eSWarner Losh {
3610738c00eSWarner Losh 	struct tuple *tp;
3620738c00eSWarner Losh 
3630738c00eSWarner Losh 	for (tp = tl->tuples; tp; tp = tp->next)
3640738c00eSWarner Losh 		if (tp->code == code)
3650738c00eSWarner Losh 			break;
3660738c00eSWarner Losh 	return (tp);
3670738c00eSWarner Losh }
3680738c00eSWarner Losh 
3690738c00eSWarner Losh /*
3700738c00eSWarner Losh  *	return table entry for code.
3710738c00eSWarner Losh  */
3720738c00eSWarner Losh static struct tuple_info *
get_tuple_info(unsigned char code)3730738c00eSWarner Losh get_tuple_info(unsigned char code)
3740738c00eSWarner Losh {
3750738c00eSWarner Losh 	struct tuple_info *tp;
3760738c00eSWarner Losh 
3770738c00eSWarner Losh 	for (tp = tuple_info; tp->name; tp++)
3780738c00eSWarner Losh 		if (tp->code == code)
3790738c00eSWarner Losh 			return (tp);
3800738c00eSWarner Losh 	return (0);
3810738c00eSWarner Losh }
3820738c00eSWarner Losh 
383c1d393d2SWarner Losh const char *
tuple_name(unsigned char code)3840738c00eSWarner Losh tuple_name(unsigned char code)
3850738c00eSWarner Losh {
3860738c00eSWarner Losh 	struct tuple_info *tp;
3870738c00eSWarner Losh 
3880738c00eSWarner Losh 	tp = get_tuple_info(code);
3890738c00eSWarner Losh 	if (tp)
3900738c00eSWarner Losh 		return (tp->name);
3910738c00eSWarner Losh 	return ("Unknown");
3920738c00eSWarner Losh }
393