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
30static const char rcsid[] =
31  "$FreeBSD: stable/11/usr.sbin/dumpcis/printcis.c 330449 2018-03-05 07:26:05Z eadler $";
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#include <sys/ioctl.h>
45
46#include "cis.h"
47#include "readcis.h"
48
49static void   dump_config_map(struct tuple *tp);
50static void   dump_cis_config(struct tuple *tp);
51static void   dump_other_cond(u_char *p, int len);
52static void   dump_device_desc(u_char *p, int len, const char *type);
53static void   dump_info_v1(u_char *p, int len);
54static void   dump_longlink_mfc(u_char *p, int len);
55static void   dump_bar(u_char *p, int len);
56static void   dump_device_geo(u_char *p, int len);
57static void   dump_func_id(u_char *p);
58static void   dump_serial_ext(u_char *p, int len);
59static void   dump_disk_ext(u_char *p, int len);
60static void   dump_network_ext(u_char *p, int len);
61static void   dump_info_v2(u_char *p, int len);
62static void   dump_org(u_char *p, int len);
63
64void
65dumpcis(struct tuple_list *tlist)
66{
67	struct tuple *tp;
68	struct tuple_list *tl;
69	int     count = 0, sz, ad, i;
70	u_char *p;
71	int func = 0;
72
73	for (tl = tlist; tl; tl = tl->next)
74		for (tp = tl->tuples; tp; tp = tp->next) {
75			printf("Tuple #%d, code = 0x%x (%s), length = %d\n",
76			    ++count, tp->code, tuple_name(tp->code), tp->length);
77			p = tp->data;
78			sz = tp->length;
79			ad = 0;
80			while (sz > 0) {
81				printf("    %03x: ", ad);
82				for (i = 0; i < ((sz < 16) ? sz : 16); i++)
83					printf(" %02x", p[i]);
84				printf("\n");
85				sz -= 16;
86				p += 16;
87				ad += 16;
88			}
89			switch (tp->code) {
90			default:
91				break;
92			case CIS_MEM_COMMON:	/* 0x01 */
93				dump_device_desc(tp->data, tp->length, "Common");
94				break;
95			case CIS_CONF_MAP_CB:	/* 0x04 */
96				dump_config_map(tp);
97				break;
98			case CIS_CONFIG_CB:	/* 0x05 */
99				dump_cis_config(tp);
100				break;
101			case CIS_LONGLINK_MFC:	/* 0x06 */
102				dump_longlink_mfc(tp->data, tp->length);
103				break;
104			case CIS_BAR:		/* 0x07 */
105				dump_bar(tp->data, tp->length);
106				break;
107			case CIS_CHECKSUM:	/* 0x10 */
108				printf("\tChecksum from offset %d, length %d, value is 0x%x\n",
109				       tpl16(tp->data),
110				       tpl16(tp->data + 2),
111				       tp->data[4]);
112				break;
113			case CIS_LONGLINK_A:	/* 0x11 */
114				printf("\tLong link to attribute memory, address 0x%x\n",
115				       tpl32(tp->data));
116				break;
117			case CIS_LONGLINK_C:	/* 0x12 */
118				printf("\tLong link to common memory, address 0x%x\n",
119				       tpl32(tp->data));
120				break;
121			case CIS_INFO_V1:	/* 0x15 */
122				dump_info_v1(tp->data, tp->length);
123				break;
124			case CIS_ALTSTR:	/* 0x16 */
125				break;
126			case CIS_MEM_ATTR:	/* 0x17 */
127				dump_device_desc(tp->data, tp->length, "Attribute");
128				break;
129			case CIS_JEDEC_C:	/* 0x18 */
130			case CIS_JEDEC_A:	/* 0x19 */
131				break;
132			case CIS_CONF_MAP:	/* 0x1A */
133				dump_config_map(tp);
134				break;
135			case CIS_CONFIG:	/* 0x1B */
136				dump_cis_config(tp);
137				break;
138			case CIS_DEVICE_OC:	/* 0x1C */
139			case CIS_DEVICE_OA:	/* 0x1D */
140				dump_other_cond(tp->data, tp->length);
141				break;
142			case CIS_DEVICEGEO:	/* 0x1E */
143			case CIS_DEVICEGEO_A:	/* 0x1F */
144				dump_device_geo(tp->data, tp->length);
145				break;
146			case CIS_MANUF_ID:	/* 0x20 */
147				printf("\tPCMCIA ID = 0x%x, OEM ID = 0x%x\n",
148				       tpl16(tp->data),
149				       tpl16(tp->data + 2));
150				break;
151			case CIS_FUNC_ID:	/* 0x21 */
152				func = tp->data[0];
153				dump_func_id(tp->data);
154				break;
155			case CIS_FUNC_EXT:	/* 0x22 */
156				switch (func) {
157				case 2:
158					dump_serial_ext(tp->data, tp->length);
159					break;
160				case 4:
161					dump_disk_ext(tp->data, tp->length);
162					break;
163				case 6:
164					dump_network_ext(tp->data, tp->length);
165					break;
166				}
167				break;
168			case CIS_VERS_2:	/* 0x40 */
169				dump_info_v2(tp->data, tp->length);
170				break;
171			case CIS_ORG:		/* 0x46 */
172				dump_org(tp->data, tp->length);
173				break;
174			}
175		}
176}
177
178/*
179 *	CIS_CONF_MAP   : Dump configuration map tuple.
180 *	CIS_CONF_MAP_CB: Dump configuration map for CardBus
181 */
182static void
183dump_config_map(struct tuple *tp)
184{
185	u_char *p = tp->data, x;
186	int     rlen, mlen = 0;
187	int     i;
188
189	rlen = (p[0] & 3) + 1;
190	if (tp->code == CIS_CONF_MAP)
191		mlen = ((p[0] >> 2) & 3) + 1;
192	if (tp->length < rlen + mlen + 2) {
193		printf("\tWrong length for configuration map tuple\n");
194		return;
195	}
196	printf("\tReg len = %d, config register addr = 0x%x, last config = 0x%x\n",
197	       rlen, parse_num(rlen | 0x10, p + 2, &p, 0), p[1]);
198	if (mlen) {
199		printf("\tRegisters: ");
200		for (i = 0; i < mlen; i++, p++) {
201			for (x = 0x1; x; x <<= 1)
202				printf("%c", x & *p ? 'X' : '-');
203			putchar(' ');
204		}
205	}
206	i = tp->length - (rlen + mlen + 2);
207	if (i) {
208		if (!mlen)
209			putchar('\t');
210		printf("%d bytes in subtuples", i);
211	}
212	if (mlen || i)
213		putchar('\n');
214}
215
216/*
217 *	Dump power descriptor.
218 *	call from dump_cis_config()
219 */
220static int
221print_pwr_desc(u_char *p)
222{
223	int     len = 1, i;
224	u_char mask;
225	const char  **expp;
226	static const char *pname[] =
227	{"Nominal operating supply voltage",
228	 "Minimum operating supply voltage",
229	 "Maximum operating supply voltage",
230	 "Continuous supply current",
231	 "Max current average over 1 second",
232	 "Max current average over 10 ms",
233	 "Power down supply current",
234	 "Reserved"
235	};
236	static const char *vexp[] =
237	{"10uV", "100uV", "1mV", "10mV", "100mV", "1V", "10V", "100V"};
238	static const char *cexp[] =
239	{"10nA", "1uA", "10uA", "100uA", "1mA", "10mA", "100mA", "1A"};
240	static const char *mant[] =
241	{"1", "1.2", "1.3", "1.5", "2", "2.5", "3", "3.5", "4", "4.5",
242	"5", "5.5", "6", "7", "8", "9"};
243
244	mask = *p++;
245	expp = vexp;
246	for (i = 0; i < 8; i++)
247		if (mask & (1 << i)) {
248			len++;
249			if (i >= 3)
250				expp = cexp;
251			printf("\t\t%s: ", pname[i]);
252			printf("%s x %s",
253			    mant[(*p >> 3) & 0xF],
254			    expp[*p & 7]);
255			while (*p & 0x80) {
256				len++;
257				p++;
258				printf(", ext = 0x%x", *p);
259			}
260			printf("\n");
261			p++;
262		}
263	return (len);
264}
265
266/*
267 *	print_ext_speed - Print extended speed.
268 *	call from dump_cis_config(), dump_device_desc()
269 */
270static void
271print_ext_speed(u_char x, int scale)
272{
273	static const char *mant[] =
274	{"Reserved", "1.0", "1.2", "1.3", "1.5", "2.0", "2.5", "3.0",
275	"3.5", "4.0", "4.5", "5.0", "5.5", "6.0", "7.0", "8.0"};
276	static const char *exp[] =
277	{"1 ns", "10 ns", "100 ns", "1 us", "10 us", "100 us",
278	"1 ms", "10 ms"};
279	static const char *scale_name[] =
280	{"None", "10", "100", "1,000", "10,000", "100,000",
281	"1,000,000", "10,000,000"};
282
283	printf("Speed = %s x %s", mant[(x >> 3) & 0xF], exp[x & 7]);
284	if (scale)
285		printf(", scaled by %s", scale_name[scale & 7]);
286}
287
288/*
289 *	Print variable length value.
290 *	call from print_io_map(), print_mem_map()
291 */
292static int
293print_num(int sz, const char *fmt, u_char *p, int ofs)
294{
295	switch (sz) {
296	case 0:
297	case 0x10:
298		return 0;
299	case 1:
300	case 0x11:
301		printf(fmt, *p + ofs);
302		return 1;
303	case 2:
304	case 0x12:
305		printf(fmt, tpl16(p) + ofs);
306		return 2;
307	case 0x13:
308		printf(fmt, tpl24(p) + ofs);
309		return 3;
310	case 3:
311	case 0x14:
312		printf(fmt, tpl32(p) + ofs);
313		return 4;
314	}
315	errx(1, "print_num(0x%x): Illegal arguments", sz);
316/*NOTREACHED*/
317}
318
319/*
320 *	Print I/O mapping sub-tuple.
321 *	call from dump_cis_config()
322 */
323static u_char *
324print_io_map(u_char *p, u_char *q)
325{
326	int i, j;
327	u_char c;
328
329	if (q <= p)
330		goto err;
331	if (CIS_IO_ADDR(*p))	/* I/O address line */
332		printf("\tCard decodes %d address lines",
333			CIS_IO_ADDR(*p));
334	else
335		printf("\tCard provides address decode");
336
337	/* 8/16 bit I/O */
338	switch (*p & (CIS_IO_8BIT | CIS_IO_16BIT)) {
339	case CIS_IO_8BIT:
340		printf(", 8 Bit I/O only");
341		break;
342	case CIS_IO_16BIT:
343		printf(", limited 8/16 Bit I/O");
344		break;
345	case (CIS_IO_8BIT | CIS_IO_16BIT):
346		printf(", full 8/16 Bit I/O");
347		break;
348	}
349	putchar('\n');
350
351	/* I/O block sub-tuple exist */
352	if (*p++ & CIS_IO_RANGE) {
353		if (q <= p)
354			goto err;
355		c = *p++;
356		/* calculate byte length */
357		j = CIS_IO_ADSZ(c) + CIS_IO_BLKSZ(c);
358		if (CIS_IO_ADSZ(c) == 3)
359			j++;
360		if (CIS_IO_BLKSZ(c) == 3)
361			j++;
362		/* number of I/O block sub-tuples */
363		for (i = 0; i <= CIS_IO_BLKS(c); i++) {
364			if (q - p < j)
365				goto err;
366			printf("\t\tI/O address # %d: ", i + 1);
367			/* start block address */
368			p += print_num(CIS_IO_ADSZ(c),
369				       "block start = 0x%x", p, 0);
370			/* block size */
371			p += print_num(CIS_IO_BLKSZ(c),
372				       " block length = 0x%x", p, 1);
373			putchar('\n');
374		}
375	}
376	return p;
377
378 err:	/* warning */
379	printf("\tWrong length for I/O mapping sub-tuple\n");
380	return p;
381}
382
383/*
384 *	Print IRQ sub-tuple.
385 *	call from dump_cis_config()
386 */
387static u_char *
388print_irq_map(u_char *p, u_char *q)
389{
390	int i, j;
391	u_char c;
392
393	if (q <= p)
394		goto err;
395	printf("\t\tIRQ modes:");
396	c = ' ';
397	if (*p & CIS_IRQ_LEVEL) { /* Level triggered interrupts */
398		printf(" Level");
399		c = ',';
400	}
401	if (*p & CIS_IRQ_PULSE) { /* Pulse triggered requests */
402		printf("%c Pulse", c);
403		c = ',';
404	}
405	if (*p & CIS_IRQ_SHARING) /* Interrupt sharing */
406		printf("%c Shared", c);
407	putchar('\n');
408
409	/* IRQ mask values exist */
410	if (*p & CIS_IRQ_MASK) {
411		if (q - p < 3)
412			goto err;
413		i = tpl16(p + 1); /* IRQ mask */
414		printf("\t\tIRQs: ");
415		if (*p & 1)
416			printf(" NMI");
417		if (*p & 0x2)
418			printf(" IOCK");
419		if (*p & 0x4)
420			printf(" BERR");
421		if (*p & 0x8)
422			printf(" VEND");
423		for (j = 0; j < 16; j++)
424			if (i & (1 << j))
425				printf(" %d", j);
426		putchar('\n');
427		p += 3;
428	} else {
429		printf("\t\tIRQ level = %d\n", CIS_IRQ_IRQN(*p));
430		p++;
431	}
432	return p;
433
434 err:	/* warning */
435	printf("\tWrong length for IRQ sub-tuple\n");
436	return p;
437}
438
439/*
440 *	Print memory map sub-tuple.
441 *	call from dump_cis_config()
442 */
443static u_char *
444print_mem_map(u_char feat, u_char *p, u_char *q)
445{
446	int i, j;
447	u_char c;
448
449	switch (CIS_FEAT_MEMORY(feat)) {
450
451	case CIS_FEAT_MEM_NONE:	/* No memory block */
452		break;
453	case CIS_FEAT_MEM_LEN:	/* Specify memory length */
454		if (q - p < 2)
455			goto err;
456		printf("\tMemory space length = 0x%x\n", tpl16(p));
457		p += 2;
458		break;
459	case CIS_FEAT_MEM_ADDR:	/* Memory address and length */
460		if (q - p < 4)
461			goto err;
462		printf("\tMemory space address = 0x%x, length = 0x%x\n",
463		       tpl16(p + 2), tpl16(p));
464		p += 4;
465		break;
466	case CIS_FEAT_MEM_WIN:	/* Memory descriptors. */
467		if (q <= p)
468			goto err;
469		c = *p++;
470		/* calculate byte length */
471		j = CIS_MEM_LENSZ(c) + CIS_MEM_ADDRSZ(c);
472		if (c & CIS_MEM_HOST)
473			j += CIS_MEM_ADDRSZ(c);
474		/* number of memory block */
475		for (i = 0; i < CIS_MEM_WINS(c); i++) {
476			if (q - p < j)
477				goto err;
478			printf("\tMemory descriptor %d\n\t\t", i + 1);
479			/* memory length */
480			p += print_num(CIS_MEM_LENSZ(c) | 0x10,
481				       " blk length = 0x%x00", p, 0);
482			/* card address */
483			p += print_num(CIS_MEM_ADDRSZ(c) | 0x10,
484				       " card addr = 0x%x00", p, 0);
485			if (c & CIS_MEM_HOST) /* Host address value exist */
486				p += print_num(CIS_MEM_ADDRSZ(c) | 0x10,
487					       " host addr = 0x%x00", p, 0);
488			putchar('\n');
489		}
490		break;
491	}
492	return p;
493
494 err:	/* warning */
495	printf("\tWrong length for memory mapping sub-tuple\n");
496	return p;
497}
498
499/*
500 *	CIS_CONFIG   : Dump a config entry.
501 *	CIS_CONFIG_CB: Dump a configuration entry for CardBus
502 */
503static void
504dump_cis_config(struct tuple *tp)
505{
506	u_char *p, *q, feat;
507	int     i, j;
508	char    c;
509
510	p = tp->data;
511	q = p + tp->length;
512	printf("\tConfig index = 0x%x%s\n", *p & 0x3F,
513	       *p & 0x40 ? "(default)" : "");
514
515	/* Interface byte exists */
516	if (tp->code == CIS_CONFIG && (*p & 0x80)) {
517		p++;
518		printf("\tInterface byte = 0x%x ", *p);
519		switch (*p & 0xF) { /* Interface type */
520		default:
521			printf("(reserved)");
522			break;
523		case 0:
524			printf("(memory)");
525			break;
526		case 1:
527			printf("(I/O)");
528			break;
529		case 4:
530		case 5:
531		case 6:
532		case 7:
533		case 8:
534			printf("(custom)");
535			break;
536		}
537		c = ' ';
538		if (*p & 0x10) { /* Battery voltage detect */
539			printf(" BVD1/2 active");
540			c = ',';
541		}
542		if (*p & 0x20) { /* Write protect active */
543			printf("%c card WP active", c);	/* Write protect */
544			c = ',';
545		}
546		if (*p & 0x40) { /* RdyBsy active bit */
547			printf("%c +RDY/-BSY active", c);
548			c = ',';
549		}
550		if (*p & 0x80)	/* Wait signal required */
551			printf("%c wait signal supported", c);
552		printf("\n");
553	}
554
555	/* features byte */
556	p++;
557	feat = *p++;
558
559	/* Power structure sub-tuple */
560	switch (CIS_FEAT_POWER(feat)) {	/* Power sub-tuple(s) exists */
561	case 0:
562		break;
563	case 1:
564		printf("\tVcc pwr:\n");
565		p += print_pwr_desc(p);
566		break;
567	case 2:
568		printf("\tVcc pwr:\n");
569		p += print_pwr_desc(p);
570		printf("\tVpp pwr:\n");
571		p += print_pwr_desc(p);
572		break;
573	case 3:
574		printf("\tVcc pwr:\n");
575		p += print_pwr_desc(p);
576		printf("\tVpp1 pwr:\n");
577		p += print_pwr_desc(p);
578		printf("\tVpp2 pwr:\n");
579		p += print_pwr_desc(p);
580		break;
581	}
582
583	/* Timing sub-tuple */
584	if (tp->code == CIS_CONFIG &&
585	    (feat & CIS_FEAT_TIMING)) {	/* Timing sub-tuple exists */
586		i = *p++;
587		j = CIS_WAIT_SCALE(i);
588		if (j != 3) {
589			printf("\tWait scale ");
590			print_ext_speed(*p++, j);
591			printf("\n");
592		}
593		j = CIS_READY_SCALE(i);
594		if (j != 7) {
595			printf("\tRDY/BSY scale ");
596			print_ext_speed(*p++, j);
597			printf("\n");
598		}
599		j = CIS_RESERVED_SCALE(i);
600		if (j != 7) {
601			printf("\tExternal scale ");
602			print_ext_speed(*p++, j);
603			printf("\n");
604		}
605	}
606
607	/* I/O mapping sub-tuple */
608	if (feat & CIS_FEAT_I_O) { /* I/O space sub-tuple exists */
609		if (tp->code == CIS_CONFIG)
610			p = print_io_map(p, q);
611		else {		/* CIS_CONFIG_CB */
612			printf("\tI/O base:");
613			for (i = 0; i < 8; i++)
614				if (*p & (1 << i))
615					printf(" %d", i);
616			putchar('\n');
617			p++;
618		}
619	}
620
621	/* IRQ descriptor sub-tuple */
622	if (feat & CIS_FEAT_IRQ) /* IRQ sub-tuple exists */
623		p = print_irq_map(p, q);
624
625	/* Memory map sub-tuple */
626	if (CIS_FEAT_MEMORY(feat)) { /* Memory space sub-tuple(s) exists */
627		if (tp->code == CIS_CONFIG)
628			p = print_mem_map(feat, p, q);
629		else {		/* CIS_CONFIG_CB */
630			printf("\tMemory base:");
631			for (i = 0; i < 8; i++)
632				if (*p & (1 << i))
633					printf(" %d", i);
634			putchar('\n');
635			p++;
636		}
637	}
638
639	/* Misc sub-tuple */
640	if (feat & CIS_FEAT_MISC) { /* Miscellaneous sub-tuple exists */
641		if (tp->code == CIS_CONFIG) {
642			printf("\tMax twin cards = %d\n", *p & 7);
643			printf("\tMisc attr:%s%s%s",
644			       (*p & 8) ? " (Audio-BVD2)" : "",
645			       (*p & 0x10) ? " (Read-only)" : "",
646			       (*p & 0x20) ? " (Power down supported)" : "");
647			if (*p++ & 0x80) {
648				printf(" (Ext byte = 0x%x)", *p);
649				p++;
650			}
651			putchar('\n');
652		}
653		else {		/* CIS_CONFIG_CB */
654			printf("\tMisc attr:");
655			printf("%s%s%s%s%s%s%s",
656			       (*p & 1) ? " (Master)" : "",
657			       (*p & 2) ? " (Invalidate)" : "",
658			       (*p & 4) ? " (VGA palette)" : "",
659			       (*p & 8) ? " (Parity)" : "",
660			       (*p & 0x10) ? " (Wait)" : "",
661			       (*p & 0x20) ? " (Serr)" : "",
662			       (*p & 0x40) ? " (Fast back)" : "");
663			if (*p++ & 0x80) {
664				printf("%s%s",
665				       (*p & 1) ? " (Binary audio)" : "",
666				       (*p & 2) ? " (pwm audio)" : "");
667				p++;
668			}
669			putchar('\n');
670		}
671	}
672}
673
674/*
675 *	CIS_DEVICE_OC, CIS_DEVICE_OA:
676 *		Dump other conditions for common/attribute memory
677 */
678static void
679dump_other_cond(u_char *p, int len)
680{
681	if (p[0] && len > 0) {
682		printf("\t");
683		if (p[0] & 1)
684			printf("(MWAIT)");
685		if (p[0] & 2)
686			printf(" (3V card)");
687		if (p[0] & 0x80)
688			printf(" (Extension bytes follow)");
689		printf("\n");
690	}
691}
692
693/*
694 *	CIS_MEM_COMMON, CIS_MEM_ATTR:
695 *		Common / Attribute memory descripter
696 */
697static void
698dump_device_desc(u_char *p, int len, const char *type)
699{
700	static const char *un_name[] =
701	{"512b", "2Kb", "8Kb", "32Kb", "128Kb", "512Kb", "2Mb", "reserved"};
702	static const char *speed[] =
703	{"No speed", "250nS", "200nS", "150nS",
704	"100nS", "Reserved", "Reserved"};
705	static const char *dev[] =
706	{"No device", "Mask ROM", "OTPROM", "UV EPROM",
707	 "EEPROM", "FLASH EEPROM", "SRAM", "DRAM",
708	 "Reserved", "Reserved", "Reserved", "Reserved",
709	 "Reserved", "Function specific", "Extended",
710	"Reserved"};
711	int     count = 0;
712
713	while (*p != 0xFF && len > 0) {
714		u_char x;
715
716		x = *p++;
717		len -= 2;
718		if (count++ == 0)
719			printf("\t%s memory device information:\n", type);
720		printf("\t\tDevice number %d, type %s, WPS = %s\n",
721		    count, dev[x >> 4], (x & 0x8) ? "ON" : "OFF");
722		if ((x & 7) == 7) {
723			len--;
724			if (*p) {
725				printf("\t\t");
726				print_ext_speed(*p, 0);
727				while (*p & 0x80) {
728					p++;
729					len--;
730				}
731			}
732			p++;
733		} else
734			printf("\t\tSpeed = %s", speed[x & 7]);
735		printf(", Memory block size = %s, %d units\n",
736		    un_name[*p & 7], (*p >> 3) + 1);
737		p++;
738	}
739}
740
741/*
742 *	CIS_INFO_V1: Print version-1 info
743 */
744static void
745dump_info_v1(u_char *p, int len)
746{
747	if (len < 2) {
748		printf("\tWrong length for version-1 info tuple\n");
749		return;
750	}
751	printf("\tVersion = %d.%d", p[0], p[1]);
752	p += 2;
753	len -= 2;
754	if (len > 1 && *p != 0xff) {
755		printf(", Manuf = [%s]", p);
756		while (*p++ && --len > 0);
757	}
758	if (len > 1 && *p != 0xff) {
759		printf(", card vers = [%s]", p);
760		while (*p++ && --len > 0);
761	} else {
762		printf("\n\tWrong length for version-1 info tuple\n");
763		return;
764	}
765	putchar('\n');
766	if (len > 1 && *p != 0xff) {
767		printf("\tAddit. info = [%.*s]", len, p);
768		while (*p++ && --len > 0);
769		if (len > 1 && *p != 0xff)
770			printf(",[%.*s]", len, p);
771		putchar('\n');
772	}
773}
774
775/*
776 *	CIS_FUNC_ID: Functional ID
777 */
778static void
779dump_func_id(u_char *p)
780{
781	static const char *id[] = {
782		"Multifunction card",
783		"Memory card",
784		"Serial port/modem",
785		"Parallel port",
786		"Fixed disk card",
787		"Video adapter",
788		"Network/LAN adapter",
789		"AIMS",
790		"SCSI card",
791		"Security"
792	};
793
794	printf("\t%s%s%s\n",
795	       (*p <= 9) ? id[*p] : "Unknown function",
796	       (p[1] & 1) ? " - POST initialize" : "",
797	       (p[1] & 2) ? " - Card has ROM" : "");
798}
799
800/*
801 *	CIS_FUNC_EXT: Dump functional extension tuple.
802 *		(Serial port/modem)
803 */
804static void
805dump_serial_ext(u_char *p, int len)
806{
807	static const char *type[] = {
808		"", "Modem", "Data", "Fax", "Voice", "Data modem",
809		"Fax/modem", "Voice", " (Data)", " (Fax)", " (Voice)"
810	};
811
812	if (len < 1)
813		return;
814	switch (p[0]) {
815	case 0:			/* Serial */
816	case 8:			/* Data */
817	case 9:			/* Fax */
818	case 10:		/* Voice */
819		printf("\tSerial interface extension:%s\n", type[*p]);
820		if (len < 4)
821			goto err;
822		switch (p[1] & 0x1F) {
823		default:
824			printf("\t\tUnknown device");
825			break;
826		case 0:
827			printf("\t\t8250 UART");
828			break;
829		case 1:
830			printf("\t\t16450 UART");
831			break;
832		case 2:
833			printf("\t\t16550 UART");
834			break;
835		}
836		printf(", Parity - %s%s%s%s\n",
837		       (p[2] & 1) ? "Space," : "",
838		       (p[2] & 2) ? "Mark," : "",
839		       (p[2] & 4) ? "Odd," : "",
840		       (p[2] & 8) ? "Even" : "");
841		printf("\t\tData bit - %s%s%s%s Stop bit - %s%s%s\n",
842		       (p[3] & 1) ? "5bit," : "",
843		       (p[3] & 2) ? "6bit," : "",
844		       (p[3] & 4) ? "7bit," : "",
845		       (p[3] & 8) ? "8bit," : "",
846		       (p[3] & 0x10) ? "1bit," : "",
847		       (p[3] & 0x20) ? "1.5bit," : "",
848		       (p[3] & 0x40) ? "2bit" : "");
849		break;
850	case 1:			/* Serial */
851	case 5:			/* Data */
852	case 6:			/* Fax */
853	case 7:			/* Voice */
854		printf("\t%s interface capabilities:\n", type[*p]);
855		if (len < 9)
856			goto err;
857		break;
858	case 2:			/* Data */
859		printf("\tData modem services available:\n");
860		break;
861	case 0x13:		/* Fax1 */
862	case 0x23:		/* Fax2 */
863	case 0x33:		/* Fax3 */
864		printf("\tFax%d/modem services available:\n", *p >> 4);
865		break;
866	case 0x84:		/* Voice */
867		printf("\tVoice services available:\n");
868		break;
869	err:	/* warning */
870		printf("\tWrong length for serial extension tuple\n");
871		return;
872	}
873}
874
875/*
876 *	CIS_FUNC_EXT: Dump functional extension tuple.
877 *		(Fixed disk card)
878 */
879static void
880dump_disk_ext(u_char *p, int len)
881{
882	if (len < 1)
883		return;
884	switch (p[0]) {
885	case 1:			/* IDE interface */
886		if (len < 2)
887			goto err;
888		printf("\tDisk interface: %s\n",
889		       (p[1] & 1) ? "IDE" : "Undefined");
890		break;
891	case 2:			/* Master */
892	case 3:			/* Slave */
893		if (len < 3)
894			goto err;
895		printf("\tDisk features: %s, %s%s\n",
896		       (p[1] & 0x04) ? "Silicon" : "Rotating",
897		       (p[1] & 0x08) ? "Unique, " : "",
898		       (p[1] & 0x10) ? "Dual" : "Single");
899		if (p[2] & 0x7f)
900			printf("\t\t%s%s%s%s%s%s%s\n",
901			       (p[2] & 0x01) ? "Sleep, " : "",
902			       (p[2] & 0x02) ? "Standby, " : "",
903			       (p[2] & 0x04) ? "Idle, " : "",
904			       (p[2] & 0x08) ? "Low power, " : "",
905			       (p[2] & 0x10) ? "Reg inhibit, " : "",
906			       (p[2] & 0x20) ? "Index, " : "",
907			       (p[2] & 0x40) ? "Iois16" : "");
908		break;
909	err:	/* warning */
910		printf("\tWrong length for fixed disk extension tuple\n");
911		return;
912	}
913}
914
915static void
916print_speed(u_int i)
917{
918	if (i < 1000)
919		printf("%u bits/sec", i);
920	else if (i < 1000000)
921		printf("%u kb/sec", i / 1000);
922	else
923		printf("%u Mb/sec", i / 1000000);
924}
925
926/*
927 *	CIS_FUNC_EXT: Dump functional extension tuple.
928 *		(Network/LAN adapter)
929 */
930static void
931dump_network_ext(u_char *p, int len)
932{
933	static const char *tech[] = {
934		"Undefined", "ARCnet", "Ethernet", "Token ring",
935		"Localtalk", "FDDI/CDDI", "ATM", "Wireless"
936	};
937	static const char *media[] = {
938		"Undefined", "UTP", "STP", "Thin coax",
939		"THICK coax", "Fiber", "900 MHz", "2.4 GHz",
940		"5.4 GHz", "Diffuse Infrared", "Point to point Infrared"
941	};
942	u_int i = 0;
943
944	if (len < 1)
945		return;
946	switch (p[0]) {
947	case 1:			/* Network technology */
948		if (len < 2)
949			goto err;
950		printf("\tNetwork technology: %s\n", tech[p[1] & 7]);
951		break;
952	case 2:			/* Network speed */
953		if (len < 5)
954			goto err;
955		printf("\tNetwork speed: ");
956		print_speed(tpl32(p + 1));
957		putchar('\n');
958		break;
959	case 3:			/* Network media */
960		if (len < 2)
961			goto err;
962		if (p[1] <= 10)
963			i = p[1];
964		printf("\tNetwork media: %s\n", media[i]);
965		break;
966	case 4:			/* Node ID */
967		if (len <= 2 || len < p[1] + 2)
968			goto err;
969		printf("\tNetwork node ID:");
970		for (i = 0; i < p[1]; i++)
971			printf(" %02x", p[i + 2]);
972		putchar('\n');
973		break;
974	case 5:			/* Connector type */
975		if (len < 2)
976			goto err;
977		printf("\tNetwork connector: %s connector standard\n",
978		       (p[1] == 0) ? "open" : "closed");
979		break;
980	err:	/* warning */
981		printf("\tWrong length for network extension tuple\n");
982		return;
983	}
984}
985
986/*
987 *	CIS_LONGLINK_MFC: Long link to next chain for Multi function card
988 */
989static void
990dump_longlink_mfc(u_char *p, int len)
991{
992	u_int i, n = *p++;
993
994	--len;
995	for (i = 0; i < n; i++) {
996		if (len < 5) {
997			printf("\tWrong length for long link MFC tuple\n");
998			return;
999		}
1000		printf("\tFunction %d: %s memory, address 0x%x\n",
1001		       i, (*p ? "common" : "attribute"), tpl32(p + 1));
1002		p += 5;
1003		len -= 5;
1004	}
1005}
1006
1007/*
1008 *	CIS_DEVICEGEO, CIS_DEVICEGEO_A:
1009 *		Geometry info for common/attribute memory
1010 */
1011static void
1012dump_device_geo(u_char *p, int len)
1013{
1014	while (len >= 6) {
1015		printf("\twidth = %d, erase = 0x%x, read = 0x%x, write = 0x%x\n"
1016		       "\t\tpartition = 0x%x, interleave = 0x%x\n",
1017		       p[0], 1 << (p[1] - 1),
1018		       1 << (p[2] - 1), 1 << (p[3] - 1),
1019		       1 << (p[4] - 1), 1 << (p[5] - 1));
1020		len -= 6;
1021	}
1022}
1023
1024/*
1025 *	CIS_INFO_V2: Print version-2 info
1026 */
1027static void
1028dump_info_v2(u_char *p, int len)
1029{
1030	if (len < 9) {
1031		printf("\tWrong length for version-2 info tuple\n");
1032		return;
1033	}
1034	printf("\tVersion = 0x%x, compliance = 0x%x, dindex = 0x%x\n",
1035	       p[0], p[1], tpl16(p + 2));
1036	printf("\tVspec8 = 0x%x, vspec9 = 0x%x, nhdr = %d\n",
1037	       p[6], p[7], p[8]);
1038	p += 9;
1039	len -= 9;
1040	if (len <= 1 || *p == 0xff)
1041		return;
1042	printf("\tVendor = [%.*s]", len, p);
1043	while (*p++ && --len > 0);
1044	if (len > 1 && *p != 0xff)
1045		printf(", info = [%.*s]", len, p);
1046	putchar('\n');
1047}
1048
1049/*
1050 *	CIS_ORG: Organization
1051 */
1052static void
1053dump_org(u_char *p, int len)
1054{
1055	if (len < 1) {
1056		printf("\tWrong length for organization tuple\n");
1057		return;
1058	}
1059	switch (*p) {
1060	case 0:
1061		printf("\tFilesystem");
1062		break;
1063	case 1:
1064		printf("\tApp specific");
1065		break;
1066	case 2:
1067		printf("\tCode");
1068		break;
1069	default:
1070		if (*p < 0x80)
1071			printf("\tReserved");
1072		else
1073			printf("\tVendor specific");
1074		break;
1075	}
1076	printf(" [%.*s]\n", len - 1, p + 1);
1077}
1078
1079static void
1080print_size(u_int i)
1081{
1082	if (i < 1024)
1083		printf("%ubits", i);
1084	else if (i < 1024*1024)
1085		printf("%ukb", i / 1024);
1086	else
1087		printf("%uMb", i / (1024*1024));
1088}
1089
1090/*
1091 *	CIS_BAR: Base address register for CardBus
1092 */
1093static void
1094dump_bar(u_char *p, int len)
1095{
1096	if (len < 6) {
1097		printf("\tWrong length for BAR tuple\n");
1098		return;
1099	}
1100	printf("\tBAR %d: size = ", *p & 7);
1101	print_size(tpl32(p + 2));
1102	printf(", %s%s%s%s\n",
1103	       (*p & 0x10) ? "I/O" : "Memory",
1104	       (*p & 0x20) ? ", Prefetch" : "",
1105	       (*p & 0x40) ? ", Cacheable" : "",
1106	       (*p & 0x80) ? ", <1Mb" : "");
1107}
1108