Home | History | Annotate | Line # | Download | only in std
ieee1212.c revision 1.1.2.2
      1  1.1.2.2  nathanw /*	$NetBSD: ieee1212.c,v 1.1.2.2 2002/02/28 04:14:25 nathanw Exp $	*/
      2  1.1.2.2  nathanw 
      3  1.1.2.2  nathanw /*
      4  1.1.2.2  nathanw  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  1.1.2.2  nathanw  * All rights reserved.
      6  1.1.2.2  nathanw  *
      7  1.1.2.2  nathanw  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1.2.2  nathanw  * by James Chacon.
      9  1.1.2.2  nathanw  *
     10  1.1.2.2  nathanw  * Redistribution and use in source and binary forms, with or without
     11  1.1.2.2  nathanw  * modification, are permitted provided that the following conditions
     12  1.1.2.2  nathanw  * are met:
     13  1.1.2.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     14  1.1.2.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     15  1.1.2.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.2.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.2.2  nathanw  *    documentation and/or other materials provided with the distribution.
     18  1.1.2.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     19  1.1.2.2  nathanw  *    must display the following acknowledgement:
     20  1.1.2.2  nathanw  *	This product includes software developed by the NetBSD
     21  1.1.2.2  nathanw  *	Foundation, Inc. and its contributors.
     22  1.1.2.2  nathanw  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1.2.2  nathanw  *    contributors may be used to endorse or promote products derived
     24  1.1.2.2  nathanw  *    from this software without specific prior written permission.
     25  1.1.2.2  nathanw  *
     26  1.1.2.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1.2.2  nathanw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1.2.2  nathanw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1.2.2  nathanw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1.2.2  nathanw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1.2.2  nathanw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1.2.2  nathanw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1.2.2  nathanw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1.2.2  nathanw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1.2.2  nathanw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1.2.2  nathanw  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1.2.2  nathanw  */
     38  1.1.2.2  nathanw 
     39  1.1.2.2  nathanw #include <sys/param.h>
     40  1.1.2.2  nathanw #include <sys/systm.h>
     41  1.1.2.2  nathanw #include <sys/device.h>
     42  1.1.2.2  nathanw #include <sys/kernel.h>
     43  1.1.2.2  nathanw #include <sys/malloc.h>
     44  1.1.2.2  nathanw 
     45  1.1.2.2  nathanw #include <dev/std/ieee1212reg.h>
     46  1.1.2.2  nathanw #include <dev/std/ieee1212var.h>
     47  1.1.2.2  nathanw 
     48  1.1.2.2  nathanw static const char * const p1212_keytype_strings[] = P1212_KEYTYPE_STRINGS ;
     49  1.1.2.2  nathanw static const char * const p1212_keyvalue_strings[] = P1212_KEYVALUE_STRINGS ;
     50  1.1.2.2  nathanw 
     51  1.1.2.2  nathanw static u_int16_t p1212_calc_crc(u_int32_t, u_int32_t *, int, int);
     52  1.1.2.2  nathanw static int p1212_parse_directory(struct p1212_dir *, u_int32_t *, u_int32_t);
     53  1.1.2.2  nathanw static struct p1212_leafdata *p1212_parse_leaf(u_int32_t *);
     54  1.1.2.2  nathanw static int p1212_parse_textdir(struct p1212_com *, u_int32_t *);
     55  1.1.2.2  nathanw static struct p1212_textdata *p1212_parse_text_desc(u_int32_t *);
     56  1.1.2.2  nathanw static void p1212_print_node(struct p1212_key *, void *);
     57  1.1.2.2  nathanw static int p1212_validate_offset(u_int16_t, u_int32_t);
     58  1.1.2.2  nathanw static int p1212_validate_immed(u_int16_t, u_int32_t);
     59  1.1.2.2  nathanw static int p1212_validate_leaf(u_int16_t, u_int32_t);
     60  1.1.2.2  nathanw static int p1212_validate_dir(u_int16_t, u_int32_t);
     61  1.1.2.2  nathanw 
     62  1.1.2.2  nathanw #ifdef P1212_DEBUG
     63  1.1.2.2  nathanw #define DPRINTF(x)      if (p1212debug) printf x
     64  1.1.2.2  nathanw #define DPRINTFN(n,x)   if (p1212debug>(n)) printf x
     65  1.1.2.2  nathanw int     p1212debug = 1;
     66  1.1.2.2  nathanw #else
     67  1.1.2.2  nathanw #define DPRINTF(x)
     68  1.1.2.2  nathanw #define DPRINTFN(n,x)
     69  1.1.2.2  nathanw #endif
     70  1.1.2.2  nathanw 
     71  1.1.2.2  nathanw /*
     72  1.1.2.2  nathanw  * Routines to parse the ROM into a tree that's usable. Also verify integrity
     73  1.1.2.2  nathanw  * vs. the P1212 standard
     74  1.1.2.2  nathanw  */
     75  1.1.2.2  nathanw 
     76  1.1.2.2  nathanw /*
     77  1.1.2.2  nathanw  * A buffer of u_int32_t's and a size in quads gets passed in. The output will
     78  1.1.2.2  nathanw  * return -1 on error, or 0 on success and possibly reset *size to a larger
     79  1.1.2.2  nathanw  * value.
     80  1.1.2.2  nathanw  *
     81  1.1.2.2  nathanw  * NOTE: Rom's are guarentee'd per the ISO spec to be contiguous but only the
     82  1.1.2.2  nathanw  * first 1k is directly mapped. Anything past 1k is supposed to use a loop
     83  1.1.2.2  nathanw  * around the indirect registers to read in the rom. This code only assumes the
     84  1.1.2.2  nathanw  * buffer passed in represents a total rom regardless of end size. It is the
     85  1.1.2.2  nathanw  * callers responsibility to treat a size > 1024 as a special case.
     86  1.1.2.2  nathanw  */
     87  1.1.2.2  nathanw 
     88  1.1.2.2  nathanw int
     89  1.1.2.2  nathanw p1212_iscomplete(u_int32_t *t, u_int32_t *size)
     90  1.1.2.2  nathanw {
     91  1.1.2.2  nathanw 	u_int16_t infolen, crclen, len;
     92  1.1.2.2  nathanw 	u_int32_t newlen, offset, test;
     93  1.1.2.2  nathanw 	int complete, i, numdirs, type, val, *dirs;
     94  1.1.2.2  nathanw 
     95  1.1.2.2  nathanw 	dirs = NULL;
     96  1.1.2.2  nathanw 
     97  1.1.2.2  nathanw 	if (*size == 0) {
     98  1.1.2.2  nathanw 		DPRINTF(("Invalid size for ROM: %d\n", (unsigned int)*size));
     99  1.1.2.2  nathanw 		return -1;
    100  1.1.2.2  nathanw 	}
    101  1.1.2.2  nathanw 
    102  1.1.2.2  nathanw 	infolen = P1212_ROMFMT_GET_INFOLEN((ntohl(t[0])));
    103  1.1.2.2  nathanw 	if (infolen <= 1) {
    104  1.1.2.2  nathanw 		DPRINTF(("ROM not initialized or minimal ROM: Info "
    105  1.1.2.2  nathanw 		    "length: %d\n", infolen));
    106  1.1.2.2  nathanw 		return -1;
    107  1.1.2.2  nathanw 	}
    108  1.1.2.2  nathanw 	crclen = P1212_ROMFMT_GET_CRCLEN((ntohl(t[0])));
    109  1.1.2.2  nathanw 	if (crclen < infolen) {
    110  1.1.2.2  nathanw 		DPRINTF(("CRC len less than info len. CRC len: %d, "
    111  1.1.2.2  nathanw 		    "Info len: %d\n", crclen, infolen));
    112  1.1.2.2  nathanw 		return -1;
    113  1.1.2.2  nathanw 	}
    114  1.1.2.2  nathanw 
    115  1.1.2.2  nathanw 	/*
    116  1.1.2.2  nathanw 	 * Now loop through it to check if all the offsets referenced are
    117  1.1.2.2  nathanw 	 * within the image stored so far. If not, get those as well.
    118  1.1.2.2  nathanw 	 */
    119  1.1.2.2  nathanw 
    120  1.1.2.2  nathanw 	offset = P1212_ROMFMT_GET_INFOLEN((ntohl(t[0]))) + 1;
    121  1.1.2.2  nathanw 
    122  1.1.2.2  nathanw 	/*
    123  1.1.2.2  nathanw 	 * Make sure at least the bus info block is in memory + the root dir
    124  1.1.2.2  nathanw 	 * header quad. Add 1 here since offset is an array offset and size is
    125  1.1.2.2  nathanw 	 * the total array size we want. If this is getting the root dir
    126  1.1.2.2  nathanw 	 * then add another since infolen doesn't end on the root dir entry but
    127  1.1.2.2  nathanw 	 * right before it.
    128  1.1.2.2  nathanw 	 */
    129  1.1.2.2  nathanw 
    130  1.1.2.2  nathanw 	if ((*size == 1) || (*size < (offset + 1))) {
    131  1.1.2.2  nathanw 		*size = (crclen > infolen) ? crclen : infolen;
    132  1.1.2.2  nathanw 		if (crclen == infolen)
    133  1.1.2.2  nathanw 			(*size)++;
    134  1.1.2.2  nathanw 		(*size)++;
    135  1.1.2.2  nathanw 		return 0;
    136  1.1.2.2  nathanw 	}
    137  1.1.2.2  nathanw 
    138  1.1.2.2  nathanw 	complete = 0;
    139  1.1.2.2  nathanw 	numdirs = 0;
    140  1.1.2.2  nathanw 	newlen = 0;
    141  1.1.2.2  nathanw 
    142  1.1.2.2  nathanw 	while (!complete) {
    143  1.1.2.2  nathanw 
    144  1.1.2.2  nathanw 		/*
    145  1.1.2.2  nathanw 		 * Make sure the whole directory is in memory. If not, bail now
    146  1.1.2.2  nathanw 		 * and read it in.
    147  1.1.2.2  nathanw 		 */
    148  1.1.2.2  nathanw 
    149  1.1.2.2  nathanw 		newlen = P1212_DIRENT_GET_LEN((ntohl(t[offset])));
    150  1.1.2.2  nathanw 		if ((offset + newlen + 1) > *size) {
    151  1.1.2.2  nathanw 			newlen += offset + 1;
    152  1.1.2.2  nathanw 			break;
    153  1.1.2.2  nathanw 		}
    154  1.1.2.2  nathanw 
    155  1.1.2.2  nathanw 		if (newlen == 0) {
    156  1.1.2.2  nathanw 			DPRINTF(("Impossible directory length of 0!\n"));
    157  1.1.2.2  nathanw 			return -1;
    158  1.1.2.2  nathanw 		}
    159  1.1.2.2  nathanw 
    160  1.1.2.2  nathanw 		/*
    161  1.1.2.2  nathanw 		 * Starting with the first byte of the directory, read through
    162  1.1.2.2  nathanw 		 * and check the values found. On offsets and directories read
    163  1.1.2.2  nathanw 		 * them in if appropriate (always for offsets, if not in memory
    164  1.1.2.2  nathanw 		 * for leaf/directories).
    165  1.1.2.2  nathanw 		 */
    166  1.1.2.2  nathanw 
    167  1.1.2.2  nathanw 		offset++;
    168  1.1.2.2  nathanw 		len = newlen;
    169  1.1.2.2  nathanw 		newlen = 0;
    170  1.1.2.2  nathanw 		for (i = 0; i < len; i++) {
    171  1.1.2.2  nathanw 			type = P1212_DIRENT_GET_KEYTYPE((ntohl(t[offset + i])));
    172  1.1.2.2  nathanw 			val = P1212_DIRENT_GET_VALUE((ntohl(t[offset + i])));
    173  1.1.2.2  nathanw 			switch (type) {
    174  1.1.2.2  nathanw 			case P1212_KEYTYPE_Immediate:
    175  1.1.2.2  nathanw 			case P1212_KEYTYPE_Offset:
    176  1.1.2.2  nathanw 				break;
    177  1.1.2.2  nathanw 			case P1212_KEYTYPE_Leaf:
    178  1.1.2.2  nathanw 
    179  1.1.2.2  nathanw 				/*
    180  1.1.2.2  nathanw 				 * If a leaf is found, and it's beyond the
    181  1.1.2.2  nathanw 				 * current rom length and it's beyond the
    182  1.1.2.2  nathanw 				 * current newlen setting,
    183  1.1.2.2  nathanw 				 * then set newlen accordingly.
    184  1.1.2.2  nathanw 				 */
    185  1.1.2.2  nathanw 
    186  1.1.2.2  nathanw 				test = offset + i + val + 1;
    187  1.1.2.2  nathanw 				if ((test > *size) && (test > newlen)) {
    188  1.1.2.2  nathanw 					newlen = test;
    189  1.1.2.2  nathanw 					break;
    190  1.1.2.2  nathanw 				}
    191  1.1.2.2  nathanw 
    192  1.1.2.2  nathanw 				/*
    193  1.1.2.2  nathanw 				 * For leaf nodes just make sure the whole leaf
    194  1.1.2.2  nathanw 				 * length is in the buffer. There's no data
    195  1.1.2.2  nathanw 				 * inside of them that can refer to outside
    196  1.1.2.2  nathanw 				 * nodes. (Uless it's vendor specific and then
    197  1.1.2.2  nathanw 				 * you're on your own anyways).
    198  1.1.2.2  nathanw 				 */
    199  1.1.2.2  nathanw 
    200  1.1.2.2  nathanw 				test--;
    201  1.1.2.2  nathanw 				infolen = P1212_DIRENT_GET_LEN((ntohl(t[test])));
    202  1.1.2.2  nathanw 				test++;
    203  1.1.2.2  nathanw 				test += infolen;
    204  1.1.2.2  nathanw 				if ((test > *size) && (test > newlen)) {
    205  1.1.2.2  nathanw 					newlen = test;
    206  1.1.2.2  nathanw 				}
    207  1.1.2.2  nathanw 				break;
    208  1.1.2.2  nathanw 
    209  1.1.2.2  nathanw 			case P1212_KEYTYPE_Directory:
    210  1.1.2.2  nathanw 
    211  1.1.2.2  nathanw 				/* Make sure the first quad is in memory. */
    212  1.1.2.2  nathanw 
    213  1.1.2.2  nathanw 				test = offset + i + val + 1;
    214  1.1.2.2  nathanw 				if ((test > *size) && (test > newlen)) {
    215  1.1.2.2  nathanw 					newlen = test;
    216  1.1.2.2  nathanw 					break;
    217  1.1.2.2  nathanw 				}
    218  1.1.2.2  nathanw 
    219  1.1.2.2  nathanw 				/*
    220  1.1.2.2  nathanw 				 * Can't just walk the ROM looking at type codes
    221  1.1.2.2  nathanw 				 * since these are only valid on directory
    222  1.1.2.2  nathanw 				 * entries. So save any directories
    223  1.1.2.2  nathanw 				 * we find into a queue and the bottom of the
    224  1.1.2.2  nathanw 				 * while loop will pop the last one off and walk
    225  1.1.2.2  nathanw 				 * that directory.
    226  1.1.2.2  nathanw 				 */
    227  1.1.2.2  nathanw 
    228  1.1.2.2  nathanw 				test--;
    229  1.1.2.2  nathanw 				dirs = realloc(dirs,
    230  1.1.2.2  nathanw 				    sizeof(int) * (numdirs + 1), M_DEVBUF,
    231  1.1.2.2  nathanw 				    M_WAITOK);
    232  1.1.2.2  nathanw 				dirs[numdirs++] = test;
    233  1.1.2.2  nathanw 				break;
    234  1.1.2.2  nathanw 			default:
    235  1.1.2.2  nathanw 				panic("Impossible type code: 0x%04hx",
    236  1.1.2.2  nathanw 				    (unsigned short)type);
    237  1.1.2.2  nathanw 				break;
    238  1.1.2.2  nathanw 			}
    239  1.1.2.2  nathanw 		}
    240  1.1.2.2  nathanw 
    241  1.1.2.2  nathanw 		if (newlen) {
    242  1.1.2.2  nathanw 			/* Cleanup. */
    243  1.1.2.2  nathanw 			if (dirs)
    244  1.1.2.2  nathanw 				free(dirs, M_DEVBUF);
    245  1.1.2.2  nathanw 			break;
    246  1.1.2.2  nathanw 		}
    247  1.1.2.2  nathanw 		if (dirs) {
    248  1.1.2.2  nathanw 			offset = dirs[--numdirs];
    249  1.1.2.2  nathanw 			dirs = realloc(dirs, sizeof(int) * numdirs, M_DEVBUF,
    250  1.1.2.2  nathanw 			    M_WAITOK);
    251  1.1.2.2  nathanw 		} else
    252  1.1.2.2  nathanw 			complete = 1;
    253  1.1.2.2  nathanw 	}
    254  1.1.2.2  nathanw 
    255  1.1.2.2  nathanw 	if (newlen)
    256  1.1.2.2  nathanw 		*size = newlen;
    257  1.1.2.2  nathanw 	return 0;
    258  1.1.2.2  nathanw 
    259  1.1.2.2  nathanw }
    260  1.1.2.2  nathanw 
    261  1.1.2.2  nathanw struct p1212_rom *
    262  1.1.2.2  nathanw p1212_parse(u_int32_t *t, u_int32_t size, u_int32_t mask)
    263  1.1.2.2  nathanw {
    264  1.1.2.2  nathanw 
    265  1.1.2.2  nathanw 	u_int16_t crc, romcrc, crc1;
    266  1.1.2.2  nathanw 	u_int32_t next, check;
    267  1.1.2.2  nathanw 	struct p1212_rom *rom;
    268  1.1.2.2  nathanw 	int i;
    269  1.1.2.2  nathanw 
    270  1.1.2.2  nathanw 	check = size;
    271  1.1.2.2  nathanw 
    272  1.1.2.2  nathanw 	if (p1212_iscomplete(t, &check) == -1) {
    273  1.1.2.2  nathanw 		DPRINTF(("ROM is not complete\n"));
    274  1.1.2.2  nathanw 		return NULL;
    275  1.1.2.2  nathanw 	}
    276  1.1.2.2  nathanw 	if (check != size) {
    277  1.1.2.2  nathanw 		DPRINTF(("ROM is not complete (check != size)\n"));
    278  1.1.2.2  nathanw 		return NULL;
    279  1.1.2.2  nathanw 	}
    280  1.1.2.2  nathanw 
    281  1.1.2.2  nathanw 	/* Calculate both a good and known bad crc. */
    282  1.1.2.2  nathanw 
    283  1.1.2.2  nathanw 	/* CRC's are calculated from everything except the first quad. */
    284  1.1.2.2  nathanw 
    285  1.1.2.2  nathanw 	crc = p1212_calc_crc(0, &t[1], P1212_ROMFMT_GET_CRCLEN((ntohl(t[0]))),
    286  1.1.2.2  nathanw 		0);
    287  1.1.2.2  nathanw 
    288  1.1.2.2  nathanw 	romcrc = P1212_ROMFMT_GET_CRC((ntohl(t[0])));
    289  1.1.2.2  nathanw 	if (crc != romcrc) {
    290  1.1.2.2  nathanw 		crc1 = p1212_calc_crc(0, &t[1],
    291  1.1.2.2  nathanw 		    P1212_ROMFMT_GET_CRCLEN((ntohl(t[0]))), 1);
    292  1.1.2.2  nathanw 		if (crc1 != romcrc) {
    293  1.1.2.2  nathanw 			DPRINTF(("Invalid ROM: CRC: 0x%04hx, Calculated "
    294  1.1.2.2  nathanw 			    "CRC: 0x%04hx, CRC1: 0x%04hx\n",
    295  1.1.2.2  nathanw 			    (unsigned short)romcrc, (unsigned short)crc,
    296  1.1.2.2  nathanw 			    (unsigned short)crc1));
    297  1.1.2.2  nathanw 			return NULL;
    298  1.1.2.2  nathanw 		}
    299  1.1.2.2  nathanw 	}
    300  1.1.2.2  nathanw 
    301  1.1.2.2  nathanw 	/* Now, walk the ROM. */
    302  1.1.2.2  nathanw 
    303  1.1.2.2  nathanw 	/* Get the initial offset for the root dir. */
    304  1.1.2.2  nathanw 
    305  1.1.2.2  nathanw 	rom = malloc(sizeof(struct p1212_rom), M_DEVBUF, M_WAITOK);
    306  1.1.2.2  nathanw 	rom->len = P1212_ROMFMT_GET_INFOLEN((ntohl(t[0])));
    307  1.1.2.2  nathanw 	next = rom->len + 1;
    308  1.1.2.2  nathanw 
    309  1.1.2.2  nathanw 	if ((rom->len < 1) || (rom->len > size)) {
    310  1.1.2.2  nathanw 		DPRINTF(("Invalid ROM info length: %d\n", rom->len));
    311  1.1.2.2  nathanw 		free(rom, M_DEVBUF);
    312  1.1.2.2  nathanw 		return NULL;
    313  1.1.2.2  nathanw 	}
    314  1.1.2.2  nathanw 
    315  1.1.2.2  nathanw 	/* Exclude the quad which covers the bus name. */
    316  1.1.2.2  nathanw 	rom->len--;
    317  1.1.2.2  nathanw 
    318  1.1.2.2  nathanw 	if (rom->len) {
    319  1.1.2.2  nathanw 		rom->data = malloc(sizeof(u_int32_t) * rom->len, M_DEVBUF,
    320  1.1.2.2  nathanw 		    M_WAITOK);
    321  1.1.2.2  nathanw 		/* Add 2 to account for info/crc and bus name we're skipping. */
    322  1.1.2.2  nathanw 		for (i = 0; i < rom->len; i++)
    323  1.1.2.2  nathanw 			rom->data[i] = t[i + 2];
    324  1.1.2.2  nathanw 	}
    325  1.1.2.2  nathanw 
    326  1.1.2.2  nathanw 	/* The name field is always 4 bytes and always the 2nd field. */
    327  1.1.2.2  nathanw 	strncpy(rom->name, (char *)&t[1], 4);
    328  1.1.2.2  nathanw 	rom->name[4] = 0;
    329  1.1.2.2  nathanw 
    330  1.1.2.2  nathanw 	/*
    331  1.1.2.2  nathanw 	 * Fill out the root directory. All these values are hardcoded so the
    332  1.1.2.2  nathanw 	 * parse/print/match routines have a standard layout to work against.
    333  1.1.2.2  nathanw 	 */
    334  1.1.2.2  nathanw 
    335  1.1.2.2  nathanw 	rom->root = malloc(sizeof(struct p1212_dir), M_DEVBUF, M_WAITOK|M_ZERO);
    336  1.1.2.2  nathanw 	rom->root->com.key.key_type = P1212_KEYTYPE_Directory;
    337  1.1.2.2  nathanw 	rom->root->com.key.key_value = 0;
    338  1.1.2.2  nathanw 	rom->root->com.key.key = (u_int8_t)P1212_KEYTYPE_Directory;
    339  1.1.2.2  nathanw 	rom->root->com.key.val = 0;
    340  1.1.2.2  nathanw 	TAILQ_INIT(&rom->root->data_root);
    341  1.1.2.2  nathanw 	TAILQ_INIT(&rom->root->subdir_root);
    342  1.1.2.2  nathanw 
    343  1.1.2.2  nathanw 	if (p1212_parse_directory(rom->root, &t[next], mask)) {
    344  1.1.2.2  nathanw 		DPRINTF(("Parse error in ROM. Bailing\n"));
    345  1.1.2.2  nathanw 		p1212_free(rom);
    346  1.1.2.2  nathanw 		return NULL;
    347  1.1.2.2  nathanw 	}
    348  1.1.2.2  nathanw 	return rom;
    349  1.1.2.2  nathanw }
    350  1.1.2.2  nathanw 
    351  1.1.2.2  nathanw static int
    352  1.1.2.2  nathanw p1212_parse_directory(struct p1212_dir *root, u_int32_t *addr, u_int32_t mask)
    353  1.1.2.2  nathanw {
    354  1.1.2.2  nathanw 	struct p1212_dir *dir, *sdir;
    355  1.1.2.2  nathanw 	struct p1212_data *data;
    356  1.1.2.2  nathanw 	struct p1212_com *com;
    357  1.1.2.2  nathanw 	u_int32_t *t, desc;
    358  1.1.2.2  nathanw 	u_int16_t crclen, crc, crc1, romcrc;
    359  1.1.2.2  nathanw 	u_int8_t type, val;
    360  1.1.2.2  nathanw 	unsigned long size;
    361  1.1.2.2  nathanw 	int i, module_vendor_flag, module_sw_flag, node_sw_flag, unit_sw_flag;
    362  1.1.2.2  nathanw 	int node_capabilities_flag, offset, unit_location_flag, unitdir_cnt;
    363  1.1.2.2  nathanw 	int leafoff;
    364  1.1.2.2  nathanw 
    365  1.1.2.2  nathanw 	t = addr;
    366  1.1.2.2  nathanw 	dir = root;
    367  1.1.2.2  nathanw 
    368  1.1.2.2  nathanw 	module_vendor_flag = 0;
    369  1.1.2.2  nathanw 	module_sw_flag = 0;
    370  1.1.2.2  nathanw 	node_sw_flag = 0;
    371  1.1.2.2  nathanw 	node_capabilities_flag = 0;
    372  1.1.2.2  nathanw 	unitdir_cnt = 0;
    373  1.1.2.2  nathanw 	offset = 0;
    374  1.1.2.2  nathanw 
    375  1.1.2.2  nathanw 	while (dir) {
    376  1.1.2.2  nathanw 		dir->match = 0;
    377  1.1.2.2  nathanw 		crclen = P1212_DIRENT_GET_LEN((ntohl(t[offset])));
    378  1.1.2.2  nathanw 		romcrc = P1212_DIRENT_GET_CRC((ntohl(t[offset])));
    379  1.1.2.2  nathanw 
    380  1.1.2.2  nathanw 		crc = p1212_calc_crc(0, &t[offset + 1], crclen, 0);
    381  1.1.2.2  nathanw 		if (crc != romcrc) {
    382  1.1.2.2  nathanw 			crc1 = p1212_calc_crc(0, &t[offset + 1], crclen, 1);
    383  1.1.2.2  nathanw 			if (crc1 != romcrc) {
    384  1.1.2.2  nathanw 				DPRINTF(("Invalid ROM: CRC: 0x%04hx, "
    385  1.1.2.2  nathanw 					    "Calculated CRC: "
    386  1.1.2.2  nathanw 					    "0x%04hx, CRC1: 0x%04hx\n",
    387  1.1.2.2  nathanw 					    (unsigned short)romcrc,
    388  1.1.2.2  nathanw 					    (unsigned short)crc,
    389  1.1.2.2  nathanw 					    (unsigned short)crc1));
    390  1.1.2.2  nathanw 				return 1;
    391  1.1.2.2  nathanw 			}
    392  1.1.2.2  nathanw 		}
    393  1.1.2.2  nathanw 		com = NULL;
    394  1.1.2.2  nathanw 		unit_sw_flag = 0;
    395  1.1.2.2  nathanw 		unit_location_flag = 0;
    396  1.1.2.2  nathanw 		offset++;
    397  1.1.2.2  nathanw 
    398  1.1.2.2  nathanw 		if ((dir->parent == NULL) && dir->com.key.val) {
    399  1.1.2.2  nathanw 			DPRINTF(("Invalid root dir. key.val is 0x%0x and not"
    400  1.1.2.2  nathanw 			    " 0x0\n", dir->com.key.val));
    401  1.1.2.2  nathanw 			return 1;
    402  1.1.2.2  nathanw 		}
    403  1.1.2.2  nathanw 
    404  1.1.2.2  nathanw 		for (i = offset; i < (offset + crclen); i++) {
    405  1.1.2.2  nathanw 			desc = ntohl(t[i]);
    406  1.1.2.2  nathanw 			type = P1212_DIRENT_GET_KEYTYPE(desc);
    407  1.1.2.2  nathanw 			val = P1212_DIRENT_GET_KEYVALUE(desc);
    408  1.1.2.2  nathanw 
    409  1.1.2.2  nathanw 			/*
    410  1.1.2.2  nathanw 			 * Sanity check for valid types/locations/etc.
    411  1.1.2.2  nathanw 			 *
    412  1.1.2.2  nathanw 			 * See pages 79-100 of
    413  1.1.2.2  nathanw 			 * ISO/IEC 13213:1194 (ANSI/IEEE Std 1212, 1994 edition)
    414  1.1.2.2  nathanw 			 * for specifics.
    415  1.1.2.2  nathanw 			 *
    416  1.1.2.2  nathanw 			 * XXX: These all really should be broken out into
    417  1.1.2.2  nathanw 			 * subroutines as it's grown large and complicated
    418  1.1.2.2  nathanw 			 * in certain cases.
    419  1.1.2.2  nathanw 			 */
    420  1.1.2.2  nathanw 
    421  1.1.2.2  nathanw 			switch (val) {
    422  1.1.2.2  nathanw 			case P1212_KEYVALUE_Unit_Spec_Id:
    423  1.1.2.2  nathanw 			case P1212_KEYVALUE_Unit_Sw_Version:
    424  1.1.2.2  nathanw 			case P1212_KEYVALUE_Unit_Dependent_Info:
    425  1.1.2.2  nathanw 			case P1212_KEYVALUE_Unit_Location:
    426  1.1.2.2  nathanw 			case P1212_KEYVALUE_Unit_Poll_Mask:
    427  1.1.2.2  nathanw 				if (dir->parent == NULL) {
    428  1.1.2.2  nathanw 					DPRINTF(("Invalid ROM: %s is not "
    429  1.1.2.2  nathanw 					    "valid in the root directory.\n",
    430  1.1.2.2  nathanw 					    p1212_keyvalue_strings[val]));
    431  1.1.2.2  nathanw 					return 1;
    432  1.1.2.2  nathanw 				}
    433  1.1.2.2  nathanw 				break;
    434  1.1.2.2  nathanw 			default:
    435  1.1.2.2  nathanw 				if (dir->com.key.val ==
    436  1.1.2.2  nathanw 				    P1212_KEYVALUE_Unit_Directory) {
    437  1.1.2.2  nathanw 					DPRINTF(("Invalid ROM: %s is "
    438  1.1.2.2  nathanw 					    "not valid in a unit directory.\n",
    439  1.1.2.2  nathanw 					    p1212_keyvalue_strings[val]));
    440  1.1.2.2  nathanw 					return 1;
    441  1.1.2.2  nathanw 				}
    442  1.1.2.2  nathanw 				break;
    443  1.1.2.2  nathanw 			}
    444  1.1.2.2  nathanw 
    445  1.1.2.2  nathanw 			switch (type) {
    446  1.1.2.2  nathanw 			case P1212_KEYTYPE_Immediate:
    447  1.1.2.2  nathanw 				if (p1212_validate_immed(val, mask)) {
    448  1.1.2.2  nathanw 					DPRINTF(("Invalid ROM: Can't have an "
    449  1.1.2.2  nathanw 					    "immediate type with %s value. Key "
    450  1.1.2.2  nathanw 					    "used at location 0x%0x in ROM\n",
    451  1.1.2.2  nathanw 					    p1212_keyvalue_strings[val],
    452  1.1.2.2  nathanw 					    (unsigned int)(&t[i]-&addr[0])));
    453  1.1.2.2  nathanw 					return 1;
    454  1.1.2.2  nathanw 				}
    455  1.1.2.2  nathanw 				break;
    456  1.1.2.2  nathanw 			case P1212_KEYTYPE_Offset:
    457  1.1.2.2  nathanw 				if (p1212_validate_offset(val, mask)) {
    458  1.1.2.2  nathanw 					DPRINTF(("Invalid ROM: Can't have "
    459  1.1.2.2  nathanw 				            "an offset type with key %s."
    460  1.1.2.2  nathanw 					    " Used at location 0x%0x in ROM\n",
    461  1.1.2.2  nathanw 					    p1212_keyvalue_strings[val],
    462  1.1.2.2  nathanw 					    (unsigned int)(&t[i]-&addr[0])));
    463  1.1.2.2  nathanw 					return 1;
    464  1.1.2.2  nathanw 				}
    465  1.1.2.2  nathanw 				break;
    466  1.1.2.2  nathanw 			case P1212_KEYTYPE_Leaf:
    467  1.1.2.2  nathanw 				if (p1212_validate_leaf(val, mask)) {
    468  1.1.2.2  nathanw 					DPRINTF(("Invalid ROM: Can't have a "
    469  1.1.2.2  nathanw 					    "leaf type with %s value. Key "
    470  1.1.2.2  nathanw 					    "used at location 0x%0x in ROM\n",
    471  1.1.2.2  nathanw 					    p1212_keyvalue_strings[val],
    472  1.1.2.2  nathanw 					    (unsigned int)(&t[i]-&addr[0])));
    473  1.1.2.2  nathanw 					return 1;
    474  1.1.2.2  nathanw 				}
    475  1.1.2.2  nathanw 				break;
    476  1.1.2.2  nathanw 			case P1212_KEYTYPE_Directory:
    477  1.1.2.2  nathanw 				if (p1212_validate_dir(val, mask)) {
    478  1.1.2.2  nathanw 					DPRINTF(("Invalid ROM: Can't have a "
    479  1.1.2.2  nathanw 					    "directory type with %s value. Key "
    480  1.1.2.2  nathanw 					    "used at location 0x%0x in ROM\n",
    481  1.1.2.2  nathanw 					    p1212_keyvalue_strings[val],
    482  1.1.2.2  nathanw 					    (unsigned int)(&t[i]-&addr[0])));
    483  1.1.2.2  nathanw 					return 1;
    484  1.1.2.2  nathanw 				}
    485  1.1.2.2  nathanw 				break;
    486  1.1.2.2  nathanw 			default:
    487  1.1.2.2  nathanw 				panic("Impossible type code: 0x%04hx",
    488  1.1.2.2  nathanw 				    (unsigned short)type);
    489  1.1.2.2  nathanw 				break;
    490  1.1.2.2  nathanw 			}
    491  1.1.2.2  nathanw 
    492  1.1.2.2  nathanw 			/* Note flags for required fields. */
    493  1.1.2.2  nathanw 
    494  1.1.2.2  nathanw 			if (val == P1212_KEYVALUE_Module_Vendor_Id) {
    495  1.1.2.2  nathanw 				module_vendor_flag = 1;
    496  1.1.2.2  nathanw 			}
    497  1.1.2.2  nathanw 
    498  1.1.2.2  nathanw 			if (val == P1212_KEYVALUE_Node_Capabilities) {
    499  1.1.2.2  nathanw 				node_capabilities_flag = 1;
    500  1.1.2.2  nathanw 			}
    501  1.1.2.2  nathanw 
    502  1.1.2.2  nathanw 			if (val == P1212_KEYVALUE_Unit_Sw_Version)
    503  1.1.2.2  nathanw 				unit_sw_flag = 1;
    504  1.1.2.2  nathanw 
    505  1.1.2.2  nathanw 			if (val == P1212_KEYVALUE_Unit_Location)
    506  1.1.2.2  nathanw 				unit_location_flag = 1;
    507  1.1.2.2  nathanw 
    508  1.1.2.2  nathanw 			/*
    509  1.1.2.2  nathanw 			 * This is just easier to spell out. You can't have
    510  1.1.2.2  nathanw 			 * a module sw version if you include a node sw version
    511  1.1.2.2  nathanw 			 * and vice-versa. Both aren't allowed if you have unit
    512  1.1.2.2  nathanw 			 * dirs.
    513  1.1.2.2  nathanw 			 */
    514  1.1.2.2  nathanw 
    515  1.1.2.2  nathanw 			if (val == P1212_KEYVALUE_Module_Sw_Version) {
    516  1.1.2.2  nathanw 				if (node_sw_flag) {
    517  1.1.2.2  nathanw 					DPRINTF(("Can't have a module software "
    518  1.1.2.2  nathanw 				            "version along with a node "
    519  1.1.2.2  nathanw 					    "software version entry\n"));
    520  1.1.2.2  nathanw 					return 1;
    521  1.1.2.2  nathanw 				}
    522  1.1.2.2  nathanw 				if (unitdir_cnt) {
    523  1.1.2.2  nathanw 					DPRINTF(("Can't have unit directories "
    524  1.1.2.2  nathanw 					    "with module software version "
    525  1.1.2.2  nathanw 					    "defined.\n"));
    526  1.1.2.2  nathanw 					return 1;
    527  1.1.2.2  nathanw 				}
    528  1.1.2.2  nathanw 				module_sw_flag = 1;
    529  1.1.2.2  nathanw 			}
    530  1.1.2.2  nathanw 
    531  1.1.2.2  nathanw 			if (val == P1212_KEYVALUE_Node_Sw_Version) {
    532  1.1.2.2  nathanw 				if (module_sw_flag) {
    533  1.1.2.2  nathanw 					DPRINTF(("Can't have a node software "
    534  1.1.2.2  nathanw 				            "version along with a module "
    535  1.1.2.2  nathanw 					    "software version entry\n"));
    536  1.1.2.2  nathanw 					return 1;
    537  1.1.2.2  nathanw 				}
    538  1.1.2.2  nathanw 				if (unitdir_cnt) {
    539  1.1.2.2  nathanw 					DPRINTF(("Can't have unit directories "
    540  1.1.2.2  nathanw 					    "with node software version "
    541  1.1.2.2  nathanw 					    "defined.\n"));
    542  1.1.2.2  nathanw 					return 1;
    543  1.1.2.2  nathanw 				}
    544  1.1.2.2  nathanw 				node_sw_flag = 1;
    545  1.1.2.2  nathanw 			}
    546  1.1.2.2  nathanw 
    547  1.1.2.2  nathanw 			if (val == P1212_KEYVALUE_Unit_Directory) {
    548  1.1.2.2  nathanw 				if (module_sw_flag || node_sw_flag) {
    549  1.1.2.2  nathanw 					DPRINTF(("Can't have unit directories "
    550  1.1.2.2  nathanw 					    "with either module or node "
    551  1.1.2.2  nathanw 					    "software version defined.\n"));
    552  1.1.2.2  nathanw 					return 1;
    553  1.1.2.2  nathanw 				}
    554  1.1.2.2  nathanw 				unitdir_cnt++;
    555  1.1.2.2  nathanw 			}
    556  1.1.2.2  nathanw 
    557  1.1.2.2  nathanw 			/*
    558  1.1.2.2  nathanw 			 * Text descriptors are special. They describe the
    559  1.1.2.2  nathanw 			 * last entry they follow. So they need to be included
    560  1.1.2.2  nathanw 			 * with it's struct and there's nothing in the spec
    561  1.1.2.2  nathanw 			 * preventing one from putting text descriptors after
    562  1.1.2.2  nathanw 			 * directory descriptors. Also they can be a single
    563  1.1.2.2  nathanw 			 * value or a list of them in a directory format so
    564  1.1.2.2  nathanw 			 * account for either. Finally if they're in a directory
    565  1.1.2.2  nathanw 			 * those can be the only types in a directory.
    566  1.1.2.2  nathanw 			 */
    567  1.1.2.2  nathanw 
    568  1.1.2.2  nathanw 			if (val == P1212_KEYVALUE_Textual_Descriptor) {
    569  1.1.2.2  nathanw 
    570  1.1.2.2  nathanw 				size = sizeof(struct p1212_textdata *);
    571  1.1.2.2  nathanw 				leafoff = P1212_DIRENT_GET_VALUE(desc);
    572  1.1.2.2  nathanw 				leafoff += i;
    573  1.1.2.2  nathanw 
    574  1.1.2.2  nathanw 				if (com == NULL) {
    575  1.1.2.2  nathanw 					DPRINTF(("Can't have a text descriptor "
    576  1.1.2.2  nathanw 					    "as the first entry in a "
    577  1.1.2.2  nathanw 					    "directory\n"));
    578  1.1.2.2  nathanw 					return 1;
    579  1.1.2.2  nathanw 				}
    580  1.1.2.2  nathanw 
    581  1.1.2.2  nathanw 				if (com->textcnt != 0) {
    582  1.1.2.2  nathanw 					DPRINTF(("Text descriptors can't "
    583  1.1.2.2  nathanw 					    "follow each other in a "
    584  1.1.2.2  nathanw 					    "directory\n"));
    585  1.1.2.2  nathanw 					return 1;
    586  1.1.2.2  nathanw 				}
    587  1.1.2.2  nathanw 
    588  1.1.2.2  nathanw 				if (type == P1212_KEYTYPE_Leaf) {
    589  1.1.2.2  nathanw 					com->text =
    590  1.1.2.2  nathanw 					    malloc(size, M_DEVBUF, M_WAITOK);
    591  1.1.2.2  nathanw 					com->text[0] =
    592  1.1.2.2  nathanw 					    p1212_parse_text_desc(&t[leafoff]);
    593  1.1.2.2  nathanw 					if (com->text[0] == NULL) {
    594  1.1.2.2  nathanw 						DPRINTF(("Got an error parsing "
    595  1.1.2.2  nathanw 						    "text descriptor at offset "
    596  1.1.2.2  nathanw 						    "0x%0x\n",
    597  1.1.2.2  nathanw 						    &t[leafoff]-&addr[0]));
    598  1.1.2.2  nathanw 						free(com->text, M_DEVBUF);
    599  1.1.2.2  nathanw 						return 1;
    600  1.1.2.2  nathanw 					}
    601  1.1.2.2  nathanw 					com->textcnt = 1;
    602  1.1.2.2  nathanw 				} else {
    603  1.1.2.2  nathanw 					i = p1212_parse_textdir(com,
    604  1.1.2.2  nathanw 						&t[leafoff]);
    605  1.1.2.2  nathanw 					if (i)
    606  1.1.2.2  nathanw 						return 1;
    607  1.1.2.2  nathanw 				}
    608  1.1.2.2  nathanw 			}
    609  1.1.2.2  nathanw 
    610  1.1.2.2  nathanw 			if ((type != P1212_KEYTYPE_Directory) &&
    611  1.1.2.2  nathanw 			    (val != P1212_KEYVALUE_Textual_Descriptor)) {
    612  1.1.2.2  nathanw 				data = malloc(sizeof(struct p1212_data),
    613  1.1.2.2  nathanw 				    M_DEVBUF, M_WAITOK|M_ZERO);
    614  1.1.2.2  nathanw 				data->com.key.key_type = type;
    615  1.1.2.2  nathanw 				data->com.key.key_value = val;
    616  1.1.2.2  nathanw 				data->com.key.key =
    617  1.1.2.2  nathanw 				    P1212_DIRENT_GET_KEY((ntohl(t[i])));
    618  1.1.2.2  nathanw 				data->com.key.val =
    619  1.1.2.2  nathanw 				    P1212_DIRENT_GET_VALUE((ntohl(t[i])));
    620  1.1.2.2  nathanw 				com = &data->com;
    621  1.1.2.2  nathanw 
    622  1.1.2.2  nathanw 				/*
    623  1.1.2.2  nathanw 				 * Don't try and read the offset. It may be
    624  1.1.2.2  nathanw 				 * a register or something special. Generally
    625  1.1.2.2  nathanw 				 * these are node specific so let the upper
    626  1.1.2.2  nathanw 				 * level code figure it out.
    627  1.1.2.2  nathanw 				 */
    628  1.1.2.2  nathanw 
    629  1.1.2.2  nathanw 				if ((type == P1212_KEYTYPE_Immediate) ||
    630  1.1.2.2  nathanw 				    (type == P1212_KEYTYPE_Offset))
    631  1.1.2.2  nathanw 					data->val = data->com.key.val;
    632  1.1.2.2  nathanw 
    633  1.1.2.2  nathanw 				data->leafdata = NULL;
    634  1.1.2.2  nathanw 				TAILQ_INSERT_TAIL(&dir->data_root, data, data);
    635  1.1.2.2  nathanw 
    636  1.1.2.2  nathanw 				if (type == P1212_KEYTYPE_Leaf) {
    637  1.1.2.2  nathanw 					leafoff = i + data->com.key.val;
    638  1.1.2.2  nathanw 					data->leafdata =
    639  1.1.2.2  nathanw 					    p1212_parse_leaf(&t[leafoff]);
    640  1.1.2.2  nathanw 					if (data->leafdata == NULL) {
    641  1.1.2.2  nathanw 						DPRINTF(("Error parsing leaf\n"));
    642  1.1.2.2  nathanw 						return 1;
    643  1.1.2.2  nathanw 					}
    644  1.1.2.2  nathanw 				}
    645  1.1.2.2  nathanw 			}
    646  1.1.2.2  nathanw 			if (type == P1212_KEYTYPE_Directory) {
    647  1.1.2.2  nathanw 
    648  1.1.2.2  nathanw 				sdir = malloc(sizeof(struct p1212_dir),
    649  1.1.2.2  nathanw 					M_DEVBUF, M_WAITOK|M_ZERO);
    650  1.1.2.2  nathanw 				sdir->parent = dir;
    651  1.1.2.2  nathanw 				sdir->com.key.key_type = type;
    652  1.1.2.2  nathanw 				sdir->com.key.key_value = val;
    653  1.1.2.2  nathanw 				sdir->com.key.key =
    654  1.1.2.2  nathanw 				    P1212_DIRENT_GET_KEY((ntohl(t[i])));
    655  1.1.2.2  nathanw 				sdir->com.key.val =
    656  1.1.2.2  nathanw 				    P1212_DIRENT_GET_VALUE((ntohl(t[i])));
    657  1.1.2.2  nathanw 				com = &sdir->com;
    658  1.1.2.2  nathanw 				sdir->match = sdir->com.key.val + i;
    659  1.1.2.2  nathanw 				TAILQ_INIT(&sdir->data_root);
    660  1.1.2.2  nathanw 				TAILQ_INIT(&sdir->subdir_root);
    661  1.1.2.2  nathanw 				TAILQ_INSERT_TAIL(&dir->subdir_root, sdir, dir);
    662  1.1.2.2  nathanw 			}
    663  1.1.2.2  nathanw 		}
    664  1.1.2.2  nathanw 
    665  1.1.2.2  nathanw 		/* More validity checks. */
    666  1.1.2.2  nathanw 
    667  1.1.2.2  nathanw 		if (dir->parent == NULL) {
    668  1.1.2.2  nathanw 			if (module_vendor_flag == 0) {
    669  1.1.2.2  nathanw 				DPRINTF(("Missing module vendor entry in root "
    670  1.1.2.2  nathanw 				    "directory.\n"));
    671  1.1.2.2  nathanw 				return 1;
    672  1.1.2.2  nathanw 			}
    673  1.1.2.2  nathanw 			if (node_capabilities_flag == 0) {
    674  1.1.2.2  nathanw 				DPRINTF(("Missing node capabilities entry in "
    675  1.1.2.2  nathanw 				    "root directory.\n"));
    676  1.1.2.2  nathanw 				return 1;
    677  1.1.2.2  nathanw 			}
    678  1.1.2.2  nathanw 		} else {
    679  1.1.2.2  nathanw 			if ((unitdir_cnt > 1) && (unit_location_flag == 0)) {
    680  1.1.2.2  nathanw 				DPRINTF(("Must have a unit location in each "
    681  1.1.2.2  nathanw 				    "unit directory when more than one unit "
    682  1.1.2.2  nathanw 				    "directory exists.\n"));
    683  1.1.2.2  nathanw 				return 1;
    684  1.1.2.2  nathanw 			}
    685  1.1.2.2  nathanw 		}
    686  1.1.2.2  nathanw 
    687  1.1.2.2  nathanw 		/*
    688  1.1.2.2  nathanw 		 * Ok, done with this directory and it's sanity checked. Now
    689  1.1.2.2  nathanw 		 * loop through and either find an unparsed subdir or one
    690  1.1.2.2  nathanw 		 * farther back up the chain.
    691  1.1.2.2  nathanw 		 */
    692  1.1.2.2  nathanw 
    693  1.1.2.2  nathanw 		if (!TAILQ_EMPTY(&dir->subdir_root)) {
    694  1.1.2.2  nathanw 			sdir = TAILQ_FIRST(&dir->subdir_root);
    695  1.1.2.2  nathanw 		} else {
    696  1.1.2.2  nathanw 			do {
    697  1.1.2.2  nathanw 				sdir = TAILQ_NEXT(dir, dir);
    698  1.1.2.2  nathanw 				if (sdir == NULL) {
    699  1.1.2.2  nathanw 					dir = dir->parent;
    700  1.1.2.2  nathanw 				}
    701  1.1.2.2  nathanw 			} while ((sdir == NULL) && (dir != NULL));
    702  1.1.2.2  nathanw 		}
    703  1.1.2.2  nathanw 		if (dir) {
    704  1.1.2.2  nathanw 			dir = sdir;
    705  1.1.2.2  nathanw 			if (!dir->match) {
    706  1.1.2.2  nathanw 				DPRINTF(("Invalid subdir..Has no offset\n"));
    707  1.1.2.2  nathanw 				return 1;
    708  1.1.2.2  nathanw 			}
    709  1.1.2.2  nathanw 			offset = dir->match;
    710  1.1.2.2  nathanw 		}
    711  1.1.2.2  nathanw 	}
    712  1.1.2.2  nathanw 	return 0;
    713  1.1.2.2  nathanw }
    714  1.1.2.2  nathanw 
    715  1.1.2.2  nathanw static struct p1212_leafdata *
    716  1.1.2.2  nathanw p1212_parse_leaf(u_int32_t *t)
    717  1.1.2.2  nathanw {
    718  1.1.2.2  nathanw 	u_int16_t crclen, crc, crc1, romcrc;
    719  1.1.2.2  nathanw 	struct p1212_leafdata *leafdata;
    720  1.1.2.2  nathanw 	int i;
    721  1.1.2.2  nathanw 
    722  1.1.2.2  nathanw 	crclen = P1212_DIRENT_GET_LEN((ntohl(t[0])));
    723  1.1.2.2  nathanw 	romcrc = P1212_DIRENT_GET_CRC((ntohl(t[0])));
    724  1.1.2.2  nathanw 	crc = p1212_calc_crc(0, &t[1], crclen, 0);
    725  1.1.2.2  nathanw 	crc1 = p1212_calc_crc(0,&t[1], crclen, 1);
    726  1.1.2.2  nathanw 	if ((crc != romcrc) && (crc1 != romcrc)) {
    727  1.1.2.2  nathanw 		DPRINTF(("Invalid ROM: CRC: 0x%04hx, Calculated CRC: "
    728  1.1.2.2  nathanw 		    "0x%04hx, CRC1: 0x%04hx\n", (unsigned short)romcrc,
    729  1.1.2.2  nathanw 		    (unsigned short)crc, (unsigned short)crc1));
    730  1.1.2.2  nathanw 		return NULL;
    731  1.1.2.2  nathanw 	}
    732  1.1.2.2  nathanw 	t++;
    733  1.1.2.2  nathanw 
    734  1.1.2.2  nathanw 	/*
    735  1.1.2.2  nathanw 	 * Most of these are vendor specific so don't bother trying to map them
    736  1.1.2.2  nathanw 	 * out. Anything which needs them later on can extract them.
    737  1.1.2.2  nathanw 	 */
    738  1.1.2.2  nathanw 
    739  1.1.2.2  nathanw 	leafdata = malloc(sizeof(struct p1212_leafdata), M_DEVBUF, M_WAITOK);
    740  1.1.2.2  nathanw 	leafdata->data = malloc((sizeof(u_int32_t) * crclen), M_DEVBUF,
    741  1.1.2.2  nathanw 	    M_WAITOK);
    742  1.1.2.2  nathanw 	leafdata->len = crclen;
    743  1.1.2.2  nathanw 	for (i = 0; i < crclen; i++)
    744  1.1.2.2  nathanw 		leafdata->data[i] = t[i];
    745  1.1.2.2  nathanw 	return leafdata;
    746  1.1.2.2  nathanw }
    747  1.1.2.2  nathanw 
    748  1.1.2.2  nathanw static int
    749  1.1.2.2  nathanw p1212_parse_textdir(struct p1212_com *com, u_int32_t *addr)
    750  1.1.2.2  nathanw {
    751  1.1.2.2  nathanw 	u_int32_t *t, entry, new;
    752  1.1.2.2  nathanw 	u_int16_t crclen, crc, crc1, romcrc;
    753  1.1.2.2  nathanw 	u_int8_t type, val;
    754  1.1.2.2  nathanw 
    755  1.1.2.2  nathanw 	int i, size;
    756  1.1.2.2  nathanw 
    757  1.1.2.2  nathanw 	/*
    758  1.1.2.2  nathanw 	 * A bit more complicated. A directory for a text descriptor can
    759  1.1.2.2  nathanw 	 * contain text descriptor leaf nodes only.
    760  1.1.2.2  nathanw 	 */
    761  1.1.2.2  nathanw 
    762  1.1.2.2  nathanw 	com->text = NULL;
    763  1.1.2.2  nathanw 	size = sizeof(struct p1212_text *);
    764  1.1.2.2  nathanw 
    765  1.1.2.2  nathanw 	crclen = P1212_DIRENT_GET_LEN((ntohl(t[0])));
    766  1.1.2.2  nathanw 	romcrc = P1212_DIRENT_GET_CRC((ntohl(t[0])));
    767  1.1.2.2  nathanw 	crc = p1212_calc_crc(0, &t[1], crclen, 0);
    768  1.1.2.2  nathanw 	crc1 = p1212_calc_crc(0,&t[1], crclen, 1);
    769  1.1.2.2  nathanw 	if ((crc != romcrc) && (crc1 != romcrc)) {
    770  1.1.2.2  nathanw 		DPRINTF(("Invalid ROM: CRC: 0x%04hx, Calculated CRC: "
    771  1.1.2.2  nathanw 			    "0x%04hx, CRC1: 0x%04hx\n", (unsigned short)romcrc,
    772  1.1.2.2  nathanw 			    (unsigned short)crc, (unsigned short)crc1));
    773  1.1.2.2  nathanw 		return 1;
    774  1.1.2.2  nathanw 	}
    775  1.1.2.2  nathanw 	t++;
    776  1.1.2.2  nathanw 	for (i = 0; i < crclen; i++) {
    777  1.1.2.2  nathanw 		entry = ntohl(t[i]);
    778  1.1.2.2  nathanw 
    779  1.1.2.2  nathanw 		type = P1212_DIRENT_GET_KEYTYPE(entry);
    780  1.1.2.2  nathanw 		val = P1212_DIRENT_GET_KEYVALUE(entry);
    781  1.1.2.2  nathanw 		if ((type != P1212_KEYTYPE_Leaf) ||
    782  1.1.2.2  nathanw 		    (val != P1212_KEYVALUE_Textual_Descriptor)) {
    783  1.1.2.2  nathanw 			DPRINTF(("Text descriptor directories can only "
    784  1.1.2.2  nathanw 			    "contain text descriptors. Type: %s, value: %s "
    785  1.1.2.2  nathanw 			    "isn't valid at offset 0x%0x\n",
    786  1.1.2.2  nathanw 			    p1212_keytype_strings[type],
    787  1.1.2.2  nathanw 			    p1212_keyvalue_strings[val], &t[i]-&addr[0]));
    788  1.1.2.2  nathanw 			return 1;
    789  1.1.2.2  nathanw 		}
    790  1.1.2.2  nathanw 
    791  1.1.2.2  nathanw 		new = P1212_DIRENT_GET_VALUE(entry);
    792  1.1.2.2  nathanw 		com->text = realloc(com->text, size * (com->textcnt + 1),
    793  1.1.2.2  nathanw 		    M_DEVBUF, M_WAITOK);
    794  1.1.2.2  nathanw 		if ((com->text[i] = p1212_parse_text_desc(&t[i+new])) == NULL) {
    795  1.1.2.2  nathanw 			DPRINTF(("Got an error parsing text descriptor.\n"));
    796  1.1.2.2  nathanw 			if (com->textcnt == 0)
    797  1.1.2.2  nathanw 				free(com->text, M_DEVBUF);
    798  1.1.2.2  nathanw 			return 1;
    799  1.1.2.2  nathanw 		}
    800  1.1.2.2  nathanw 		com->textcnt++;
    801  1.1.2.2  nathanw 	}
    802  1.1.2.2  nathanw 	return 0;
    803  1.1.2.2  nathanw }
    804  1.1.2.2  nathanw 
    805  1.1.2.2  nathanw static struct p1212_textdata *
    806  1.1.2.2  nathanw p1212_parse_text_desc(u_int32_t *addr)
    807  1.1.2.2  nathanw {
    808  1.1.2.2  nathanw 	u_int32_t *t;
    809  1.1.2.2  nathanw 	u_int16_t crclen, crc, crc1, romcrc;
    810  1.1.2.2  nathanw 	struct p1212_textdata *text;
    811  1.1.2.2  nathanw 	int size;
    812  1.1.2.2  nathanw 
    813  1.1.2.2  nathanw 	t = addr;
    814  1.1.2.2  nathanw 
    815  1.1.2.2  nathanw 	crclen = P1212_DIRENT_GET_LEN((ntohl(t[0])));
    816  1.1.2.2  nathanw 	romcrc = P1212_DIRENT_GET_CRC((ntohl(t[0])));
    817  1.1.2.2  nathanw 
    818  1.1.2.2  nathanw 	if (crclen < P1212_TEXT_Min_Leaf_Length) {
    819  1.1.2.2  nathanw 		DPRINTF(("Invalid ROM: text descriptor too short\n"));
    820  1.1.2.2  nathanw 		return NULL;
    821  1.1.2.2  nathanw 	}
    822  1.1.2.2  nathanw 
    823  1.1.2.2  nathanw 	crc = p1212_calc_crc(0, &t[1], crclen, 0);
    824  1.1.2.2  nathanw 	if (crc != romcrc) {
    825  1.1.2.2  nathanw 		crc1 = p1212_calc_crc(0, &t[1], crclen, 1);
    826  1.1.2.2  nathanw 		if (crc1 != romcrc) {
    827  1.1.2.2  nathanw 			DPRINTF(("Invalid ROM: CRC: 0x%04hx, Calculated CRC: "
    828  1.1.2.2  nathanw 		            "0x%04hx, CRC1: 0x%04hx\n", (unsigned short)romcrc,
    829  1.1.2.2  nathanw 			    (unsigned short)crc, (unsigned short)crc1));
    830  1.1.2.2  nathanw 			return NULL;
    831  1.1.2.2  nathanw 		}
    832  1.1.2.2  nathanw 	}
    833  1.1.2.2  nathanw 
    834  1.1.2.2  nathanw 	t++;
    835  1.1.2.2  nathanw 	text = malloc(sizeof(struct p1212_textdata), M_DEVBUF, M_WAITOK);
    836  1.1.2.2  nathanw 	text->spec_type = P1212_TEXT_GET_Spec_Type((ntohl(t[0])));
    837  1.1.2.2  nathanw 	text->spec_id = P1212_TEXT_GET_Spec_Id((ntohl(t[0])));
    838  1.1.2.2  nathanw 	text->lang_id = ntohl(t[1]);
    839  1.1.2.2  nathanw 
    840  1.1.2.2  nathanw 	t++;
    841  1.1.2.2  nathanw 	t++;
    842  1.1.2.2  nathanw 	crclen -= 2;
    843  1.1.2.2  nathanw 	size = (crclen * sizeof(u_int32_t)) + 1;
    844  1.1.2.2  nathanw 
    845  1.1.2.2  nathanw 	text->text = malloc(size, M_DEVBUF, M_WAITOK|M_ZERO);
    846  1.1.2.2  nathanw 
    847  1.1.2.2  nathanw 	memcpy(text->text, &t[0], size);
    848  1.1.2.2  nathanw 
    849  1.1.2.2  nathanw 	return text;
    850  1.1.2.2  nathanw }
    851  1.1.2.2  nathanw 
    852  1.1.2.2  nathanw struct p1212_key **
    853  1.1.2.2  nathanw p1212_find(struct p1212_dir *root, int type, int value, int flags)
    854  1.1.2.2  nathanw {
    855  1.1.2.2  nathanw 	struct p1212_key **retkeys;
    856  1.1.2.2  nathanw 	struct p1212_dir *dir, *sdir, *parent;
    857  1.1.2.2  nathanw 	struct p1212_data *data;
    858  1.1.2.2  nathanw 	int numkeys;
    859  1.1.2.2  nathanw 
    860  1.1.2.2  nathanw 	numkeys = 0;
    861  1.1.2.2  nathanw 	retkeys = NULL;
    862  1.1.2.2  nathanw 
    863  1.1.2.2  nathanw 	if ((type < P1212_KEYTYPE_Immediate) ||
    864  1.1.2.2  nathanw 	    (type > P1212_KEYTYPE_Directory)) {
    865  1.1.2.2  nathanw #ifdef DIAGNOSTIC
    866  1.1.2.2  nathanw 		printf("p1212_find: invalid type - %d\n", type);
    867  1.1.2.2  nathanw #endif
    868  1.1.2.2  nathanw 		return NULL;
    869  1.1.2.2  nathanw 	}
    870  1.1.2.2  nathanw 
    871  1.1.2.2  nathanw 	if ((value < -1) ||
    872  1.1.2.2  nathanw 	    (value > (sizeof(p1212_keyvalue_strings) / sizeof(char *)))) {
    873  1.1.2.2  nathanw #ifdef DIAGNOSTIC
    874  1.1.2.2  nathanw 		printf("p1212_find: invalid value - %d\n", value);
    875  1.1.2.2  nathanw #endif
    876  1.1.2.2  nathanw 		return NULL;
    877  1.1.2.2  nathanw 	}
    878  1.1.2.2  nathanw 
    879  1.1.2.2  nathanw 	if (flags & ~(P1212_FIND_SEARCHALL | P1212_FIND_RETURNALL)) {
    880  1.1.2.2  nathanw #ifdef DIAGNOSTIC
    881  1.1.2.2  nathanw 		printf("p1212_find: invalid flags - %d\n", flags);
    882  1.1.2.2  nathanw #endif
    883  1.1.2.2  nathanw 		return NULL;
    884  1.1.2.2  nathanw 	}
    885  1.1.2.2  nathanw 
    886  1.1.2.2  nathanw 	/*
    887  1.1.2.2  nathanw 	 * Part of this is copied from p1212_walk to do depth first traversal
    888  1.1.2.2  nathanw 	 * without using recursion. Using the walk API would have made things
    889  1.1.2.2  nathanw 	 * more complicated in trying to build up the return struct otherwise.
    890  1.1.2.2  nathanw 	 */
    891  1.1.2.2  nathanw 
    892  1.1.2.2  nathanw 	dir = root;
    893  1.1.2.2  nathanw 	sdir = NULL;
    894  1.1.2.2  nathanw 
    895  1.1.2.2  nathanw 	parent = root->parent;
    896  1.1.2.2  nathanw 	root->parent = NULL;
    897  1.1.2.2  nathanw 
    898  1.1.2.2  nathanw 	while (dir) {
    899  1.1.2.2  nathanw 		if (type == P1212_KEYTYPE_Directory) {
    900  1.1.2.2  nathanw 			TAILQ_FOREACH(sdir, &dir->subdir_root, dir) {
    901  1.1.2.2  nathanw 				if ((sdir->com.key.key_value == value) ||
    902  1.1.2.2  nathanw 				    (value == -1)) {
    903  1.1.2.2  nathanw 					numkeys++;
    904  1.1.2.2  nathanw 					retkeys = realloc(retkeys,
    905  1.1.2.2  nathanw 					    sizeof(struct p1212_key *) *
    906  1.1.2.2  nathanw 					    (numkeys + 1), M_WAITOK, M_DEVBUF);
    907  1.1.2.2  nathanw 					retkeys[numkeys - 1] = &sdir->com.key;
    908  1.1.2.2  nathanw 					retkeys[numkeys] = NULL;
    909  1.1.2.2  nathanw 					if ((flags & P1212_FIND_RETURNALL)
    910  1.1.2.2  nathanw 					    == 0) {
    911  1.1.2.2  nathanw 						root->parent = parent;
    912  1.1.2.2  nathanw 						return retkeys;
    913  1.1.2.2  nathanw 					}
    914  1.1.2.2  nathanw 				}
    915  1.1.2.2  nathanw 			}
    916  1.1.2.2  nathanw 		} else {
    917  1.1.2.2  nathanw 			TAILQ_FOREACH(data, &dir->data_root, data) {
    918  1.1.2.2  nathanw 				if (((data->com.key.key_type == type) &&
    919  1.1.2.2  nathanw 				     (data->com.key.key_value == value)) ||
    920  1.1.2.2  nathanw 				    ((data->com.key.key_type == type) &&
    921  1.1.2.2  nathanw 				     (value == -1))) {
    922  1.1.2.2  nathanw 					numkeys++;
    923  1.1.2.2  nathanw 					retkeys = realloc(retkeys,
    924  1.1.2.2  nathanw 					    sizeof(struct p1212_key *) *
    925  1.1.2.2  nathanw 					    (numkeys + 1), M_WAITOK, M_DEVBUF);
    926  1.1.2.2  nathanw 					retkeys[numkeys - 1] = &data->com.key;
    927  1.1.2.2  nathanw 					retkeys[numkeys] = NULL;
    928  1.1.2.2  nathanw 					if ((flags & P1212_FIND_RETURNALL)
    929  1.1.2.2  nathanw 					    == 0) {
    930  1.1.2.2  nathanw 						root->parent = parent;
    931  1.1.2.2  nathanw 						return retkeys;
    932  1.1.2.2  nathanw 					}
    933  1.1.2.2  nathanw 				}
    934  1.1.2.2  nathanw 			}
    935  1.1.2.2  nathanw 		}
    936  1.1.2.2  nathanw 		if (flags & P1212_FIND_SEARCHALL) {
    937  1.1.2.2  nathanw 			do {
    938  1.1.2.2  nathanw 				sdir = TAILQ_NEXT(dir, dir);
    939  1.1.2.2  nathanw 				if (sdir == NULL) {
    940  1.1.2.2  nathanw 					dir = dir->parent;
    941  1.1.2.2  nathanw 				}
    942  1.1.2.2  nathanw 			} while ((sdir == NULL) && (dir != NULL));
    943  1.1.2.2  nathanw 			dir = sdir;
    944  1.1.2.2  nathanw 		} else
    945  1.1.2.2  nathanw 			dir = NULL;
    946  1.1.2.2  nathanw 	}
    947  1.1.2.2  nathanw 	root->parent = parent;
    948  1.1.2.2  nathanw 	return retkeys;
    949  1.1.2.2  nathanw }
    950  1.1.2.2  nathanw 
    951  1.1.2.2  nathanw void
    952  1.1.2.2  nathanw p1212_walk(struct p1212_dir *root, void *arg,
    953  1.1.2.2  nathanw     void (*func)(struct p1212_key *, void *))
    954  1.1.2.2  nathanw {
    955  1.1.2.2  nathanw 	struct p1212_data *data;
    956  1.1.2.2  nathanw 	struct p1212_dir *sdir, *dir, *parent;
    957  1.1.2.2  nathanw 
    958  1.1.2.2  nathanw 	dir = root;
    959  1.1.2.2  nathanw 	sdir = NULL;
    960  1.1.2.2  nathanw 
    961  1.1.2.2  nathanw 	if (func == NULL) {
    962  1.1.2.2  nathanw #ifdef DIAGNOSTIC
    963  1.1.2.2  nathanw 		printf("p1212_walk: Passed in NULL function\n");
    964  1.1.2.2  nathanw #endif
    965  1.1.2.2  nathanw 		return;
    966  1.1.2.2  nathanw 	}
    967  1.1.2.2  nathanw 	if (root == NULL) {
    968  1.1.2.2  nathanw #ifdef DIAGNOSTIC
    969  1.1.2.2  nathanw 		printf("p1212_walk: Called with NULL root\n");
    970  1.1.2.2  nathanw #endif
    971  1.1.2.2  nathanw 		return;
    972  1.1.2.2  nathanw 	}
    973  1.1.2.2  nathanw 
    974  1.1.2.2  nathanw 	/* Allow walking from any point. Just mark the starting point. */
    975  1.1.2.2  nathanw 	parent = root->parent;
    976  1.1.2.2  nathanw 	root->parent = NULL;
    977  1.1.2.2  nathanw 
    978  1.1.2.2  nathanw 	/*
    979  1.1.2.2  nathanw 	 * Depth first traversal that doesn't use recursion.
    980  1.1.2.2  nathanw 	 *
    981  1.1.2.2  nathanw 	 * Call the function first for the directory node and then loop through
    982  1.1.2.2  nathanw 	 * all the data nodes and call the function for them.
    983  1.1.2.2  nathanw 	 *
    984  1.1.2.2  nathanw 	 * Finally, figure out the next possible directory node if one is
    985  1.1.2.2  nathanw 	 * available or bail out.
    986  1.1.2.2  nathanw 	 */
    987  1.1.2.2  nathanw 
    988  1.1.2.2  nathanw 	while (dir) {
    989  1.1.2.2  nathanw 		func((struct p1212_key *) dir, arg);
    990  1.1.2.2  nathanw 		TAILQ_FOREACH(data, &dir->data_root, data)
    991  1.1.2.2  nathanw 			func((struct p1212_key *) data, arg);
    992  1.1.2.2  nathanw 		if (!TAILQ_EMPTY(&dir->subdir_root)) {
    993  1.1.2.2  nathanw 			sdir = TAILQ_FIRST(&dir->subdir_root);
    994  1.1.2.2  nathanw 		} else {
    995  1.1.2.2  nathanw 			do {
    996  1.1.2.2  nathanw 				sdir = TAILQ_NEXT(dir, dir);
    997  1.1.2.2  nathanw 				if (sdir == NULL) {
    998  1.1.2.2  nathanw 					dir = dir->parent;
    999  1.1.2.2  nathanw 				}
   1000  1.1.2.2  nathanw 			} while ((sdir == NULL) && dir);
   1001  1.1.2.2  nathanw 		}
   1002  1.1.2.2  nathanw 		dir = sdir;
   1003  1.1.2.2  nathanw 	}
   1004  1.1.2.2  nathanw 
   1005  1.1.2.2  nathanw 	root->parent = parent;
   1006  1.1.2.2  nathanw }
   1007  1.1.2.2  nathanw 
   1008  1.1.2.2  nathanw void
   1009  1.1.2.2  nathanw p1212_print(struct p1212_dir *dir)
   1010  1.1.2.2  nathanw {
   1011  1.1.2.2  nathanw 	int indent;
   1012  1.1.2.2  nathanw 
   1013  1.1.2.2  nathanw 	indent = 0;
   1014  1.1.2.2  nathanw 
   1015  1.1.2.2  nathanw 	p1212_walk(dir, &indent, p1212_print_node);
   1016  1.1.2.2  nathanw 	printf("\n");
   1017  1.1.2.2  nathanw }
   1018  1.1.2.2  nathanw 
   1019  1.1.2.2  nathanw static void
   1020  1.1.2.2  nathanw p1212_print_node(struct p1212_key *key, void *arg)
   1021  1.1.2.2  nathanw {
   1022  1.1.2.2  nathanw 
   1023  1.1.2.2  nathanw 	struct p1212_data *data;
   1024  1.1.2.2  nathanw 	struct p1212_dir *sdir, *dir;
   1025  1.1.2.2  nathanw 	int i, j, *indent;
   1026  1.1.2.2  nathanw 
   1027  1.1.2.2  nathanw 	indent = arg;
   1028  1.1.2.2  nathanw 
   1029  1.1.2.2  nathanw 	if (key->key_type == P1212_KEYTYPE_Directory) {
   1030  1.1.2.2  nathanw 		dir = (struct p1212_dir *) key;
   1031  1.1.2.2  nathanw 		data = NULL;
   1032  1.1.2.2  nathanw 	} else {
   1033  1.1.2.2  nathanw 		data = (struct p1212_data *) key;
   1034  1.1.2.2  nathanw 		dir = NULL;
   1035  1.1.2.2  nathanw 	}
   1036  1.1.2.2  nathanw 
   1037  1.1.2.2  nathanw 	/* Recompute the indent level on each directory. */
   1038  1.1.2.2  nathanw 	if (dir) {
   1039  1.1.2.2  nathanw 		*indent = 0;
   1040  1.1.2.2  nathanw 		sdir = dir->parent;
   1041  1.1.2.2  nathanw 		while (sdir != NULL) {
   1042  1.1.2.2  nathanw 			(*indent)++;
   1043  1.1.2.2  nathanw 			sdir = sdir->parent;
   1044  1.1.2.2  nathanw 		}
   1045  1.1.2.2  nathanw 	}
   1046  1.1.2.2  nathanw 
   1047  1.1.2.2  nathanw 	if (dir && dir->parent)
   1048  1.1.2.2  nathanw 		printf("\n");
   1049  1.1.2.2  nathanw 
   1050  1.1.2.2  nathanw 	/* Set the indent string up. 4 spaces per level. */
   1051  1.1.2.2  nathanw 	for (i = 0; i < (*indent * 4); i++)
   1052  1.1.2.2  nathanw 		printf(" ");
   1053  1.1.2.2  nathanw 
   1054  1.1.2.2  nathanw 	if (dir) {
   1055  1.1.2.2  nathanw 		printf("Directory: ");
   1056  1.1.2.2  nathanw 		if (dir->print)
   1057  1.1.2.2  nathanw 			dir->print(dir);
   1058  1.1.2.2  nathanw 		else {
   1059  1.1.2.2  nathanw 			if (key->key_value >=
   1060  1.1.2.2  nathanw 			    (sizeof(p1212_keyvalue_strings) / sizeof(char *)))
   1061  1.1.2.2  nathanw 				printf("Unknown type 0x%04hx\n",
   1062  1.1.2.2  nathanw 				    (unsigned short)key->key_value);
   1063  1.1.2.2  nathanw 			else
   1064  1.1.2.2  nathanw 				printf("%s\n",
   1065  1.1.2.2  nathanw 				    p1212_keyvalue_strings[key->key_value]);
   1066  1.1.2.2  nathanw 		}
   1067  1.1.2.2  nathanw 		if (dir->com.textcnt) {
   1068  1.1.2.2  nathanw 			for (i = 0; i < dir->com.textcnt; i++) {
   1069  1.1.2.2  nathanw 				for (j = 0; j < (*indent * 4); j++)
   1070  1.1.2.2  nathanw 					printf(" ");
   1071  1.1.2.2  nathanw 				printf("Text descriptor: %s\n",
   1072  1.1.2.2  nathanw 				    dir->com.text[i]->text);
   1073  1.1.2.2  nathanw 			}
   1074  1.1.2.2  nathanw 		}
   1075  1.1.2.2  nathanw 		printf("\n");
   1076  1.1.2.2  nathanw 	} else {
   1077  1.1.2.2  nathanw 		if (data->print)
   1078  1.1.2.2  nathanw 			data->print(data);
   1079  1.1.2.2  nathanw 		else {
   1080  1.1.2.2  nathanw 			if (key->key_value >=
   1081  1.1.2.2  nathanw 			    (sizeof(p1212_keyvalue_strings) / sizeof(char *)))
   1082  1.1.2.2  nathanw 				printf("Unknown type 0x%04hx: ",
   1083  1.1.2.2  nathanw 				    (unsigned short)key->key_value);
   1084  1.1.2.2  nathanw 			else
   1085  1.1.2.2  nathanw 				printf("%s: ",
   1086  1.1.2.2  nathanw 				    p1212_keyvalue_strings[key->key_value]);
   1087  1.1.2.2  nathanw 
   1088  1.1.2.2  nathanw 			printf("0x%08x\n", key->val);
   1089  1.1.2.2  nathanw #ifdef DIAGNOSTIC
   1090  1.1.2.2  nathanw 			if ((data->com.key.key_type == P1212_KEYTYPE_Leaf) &&
   1091  1.1.2.2  nathanw 			    (data->leafdata == NULL))
   1092  1.1.2.2  nathanw 				panic("Invalid data node in configrom tree");
   1093  1.1.2.2  nathanw #endif
   1094  1.1.2.2  nathanw 
   1095  1.1.2.2  nathanw 			if (data->leafdata) {
   1096  1.1.2.2  nathanw 				for (i = 0; i < data->leafdata->len; i++) {
   1097  1.1.2.2  nathanw 					for (j = 0; j < (*indent * 4); j++)
   1098  1.1.2.2  nathanw 						printf(" ");
   1099  1.1.2.2  nathanw 					printf ("Leaf data: 0x%08x\n",
   1100  1.1.2.2  nathanw 					    data->leafdata->data[i]);
   1101  1.1.2.2  nathanw 				}
   1102  1.1.2.2  nathanw 			}
   1103  1.1.2.2  nathanw 			if (data->com.textcnt)
   1104  1.1.2.2  nathanw 				for (i = 0; i < data->com.textcnt; i++) {
   1105  1.1.2.2  nathanw 					for (j = 0; j < (*indent * 4); j++)
   1106  1.1.2.2  nathanw 						printf(" ");
   1107  1.1.2.2  nathanw 					printf("Text descriptor: %s\n",
   1108  1.1.2.2  nathanw 					    data->com.text[i]->text);
   1109  1.1.2.2  nathanw 				}
   1110  1.1.2.2  nathanw 
   1111  1.1.2.2  nathanw 		}
   1112  1.1.2.2  nathanw 	}
   1113  1.1.2.2  nathanw }
   1114  1.1.2.2  nathanw 
   1115  1.1.2.2  nathanw 
   1116  1.1.2.2  nathanw void
   1117  1.1.2.2  nathanw p1212_free(struct p1212_rom *rom)
   1118  1.1.2.2  nathanw {
   1119  1.1.2.2  nathanw 	struct p1212_dir *sdir, *dir;
   1120  1.1.2.2  nathanw 	struct p1212_data *data;
   1121  1.1.2.2  nathanw 	int i;
   1122  1.1.2.2  nathanw 
   1123  1.1.2.2  nathanw 	dir = rom->root;
   1124  1.1.2.2  nathanw 
   1125  1.1.2.2  nathanw 	/* Avoid recursing. Find the bottom most node and work back. */
   1126  1.1.2.2  nathanw 	while (dir) {
   1127  1.1.2.2  nathanw 		if (!TAILQ_EMPTY(&dir->subdir_root)) {
   1128  1.1.2.2  nathanw 			sdir = TAILQ_FIRST(&dir->subdir_root);
   1129  1.1.2.2  nathanw 			if (TAILQ_EMPTY(&sdir->subdir_root)) {
   1130  1.1.2.2  nathanw 				TAILQ_REMOVE(&dir->subdir_root, sdir, dir);
   1131  1.1.2.2  nathanw 				dir = sdir;
   1132  1.1.2.2  nathanw 			}
   1133  1.1.2.2  nathanw 			else {
   1134  1.1.2.2  nathanw 				dir = sdir;
   1135  1.1.2.2  nathanw 				continue;
   1136  1.1.2.2  nathanw 			}
   1137  1.1.2.2  nathanw 		} else {
   1138  1.1.2.2  nathanw 			if (dir->parent)
   1139  1.1.2.2  nathanw 				TAILQ_REMOVE(&dir->parent->subdir_root, dir,
   1140  1.1.2.2  nathanw 				    dir);
   1141  1.1.2.2  nathanw 		}
   1142  1.1.2.2  nathanw 
   1143  1.1.2.2  nathanw 		while ((data = TAILQ_FIRST(&dir->data_root))) {
   1144  1.1.2.2  nathanw 			if (data->leafdata) {
   1145  1.1.2.2  nathanw 				if (data->leafdata->data)
   1146  1.1.2.2  nathanw 					free(data->leafdata->data, M_DEVBUF);
   1147  1.1.2.2  nathanw 				free(data->leafdata, M_DEVBUF);
   1148  1.1.2.2  nathanw 			}
   1149  1.1.2.2  nathanw 			TAILQ_REMOVE(&dir->data_root, data, data);
   1150  1.1.2.2  nathanw 			if (data->com.textcnt) {
   1151  1.1.2.2  nathanw 				for (i = 0; i < data->com.textcnt; i++)
   1152  1.1.2.2  nathanw 					free(data->com.text[i], M_DEVBUF);
   1153  1.1.2.2  nathanw 				free(data->com.text, M_DEVBUF);
   1154  1.1.2.2  nathanw 			}
   1155  1.1.2.2  nathanw 			free(data, M_DEVBUF);
   1156  1.1.2.2  nathanw 		}
   1157  1.1.2.2  nathanw 		sdir = dir;
   1158  1.1.2.2  nathanw 		if (dir->parent)
   1159  1.1.2.2  nathanw 			dir = dir->parent;
   1160  1.1.2.2  nathanw 		else
   1161  1.1.2.2  nathanw 			dir = NULL;
   1162  1.1.2.2  nathanw 		if (sdir->com.textcnt) {
   1163  1.1.2.2  nathanw 			for (i = 0; i < sdir->com.textcnt; i++)
   1164  1.1.2.2  nathanw 				free(sdir->com.text[i], M_DEVBUF);
   1165  1.1.2.2  nathanw 			free(sdir->com.text, M_DEVBUF);
   1166  1.1.2.2  nathanw 		}
   1167  1.1.2.2  nathanw 		free(sdir, M_DEVBUF);
   1168  1.1.2.2  nathanw 	}
   1169  1.1.2.2  nathanw 	if (rom->len)
   1170  1.1.2.2  nathanw 		free(rom->data, M_DEVBUF);
   1171  1.1.2.2  nathanw 	free(rom, M_DEVBUF);
   1172  1.1.2.2  nathanw }
   1173  1.1.2.2  nathanw 
   1174  1.1.2.2  nathanw /*
   1175  1.1.2.2  nathanw  * A fairly well published reference implementation of the CRC routine had
   1176  1.1.2.2  nathanw  * a typo in it and some devices may be using it rather than the correct one
   1177  1.1.2.2  nathanw  * in calculating their ROM CRC's. To compensate an interface for generating
   1178  1.1.2.2  nathanw  * either is provided.
   1179  1.1.2.2  nathanw  *
   1180  1.1.2.2  nathanw  * len is the number of u_int32_t entries, not bytes.
   1181  1.1.2.2  nathanw  */
   1182  1.1.2.2  nathanw 
   1183  1.1.2.2  nathanw static u_int16_t
   1184  1.1.2.2  nathanw p1212_calc_crc(u_int32_t crc, u_int32_t *data, int len, int broke)
   1185  1.1.2.2  nathanw {
   1186  1.1.2.2  nathanw 	int shift;
   1187  1.1.2.2  nathanw 	u_int32_t sum;
   1188  1.1.2.2  nathanw 	int i;
   1189  1.1.2.2  nathanw 
   1190  1.1.2.2  nathanw 	for (i = 0; i < len; i++) {
   1191  1.1.2.2  nathanw 		for (shift = 28; shift > 0; shift -= 4) {
   1192  1.1.2.2  nathanw 			sum = ((crc >> 12) ^ (ntohl(data[i]) >> shift)) &
   1193  1.1.2.2  nathanw 			    0x0000000f;
   1194  1.1.2.2  nathanw 			crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ sum;
   1195  1.1.2.2  nathanw 		}
   1196  1.1.2.2  nathanw 
   1197  1.1.2.2  nathanw 
   1198  1.1.2.2  nathanw 		/* The broken implementation doesn't do the last shift. */
   1199  1.1.2.2  nathanw 		if (!broke) {
   1200  1.1.2.2  nathanw 			sum = ((crc >> 12) ^ ntohl(data[i])) & 0x0000000f;
   1201  1.1.2.2  nathanw 			crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ sum;
   1202  1.1.2.2  nathanw 		}
   1203  1.1.2.2  nathanw 	}
   1204  1.1.2.2  nathanw 	return (u_int16_t)crc;
   1205  1.1.2.2  nathanw }
   1206  1.1.2.2  nathanw 
   1207  1.1.2.2  nathanw /*
   1208  1.1.2.2  nathanw  * This is almost identical to the standard autoconf *match idea except it
   1209  1.1.2.2  nathanw  * can match and attach multiple children in one pass.
   1210  1.1.2.2  nathanw  */
   1211  1.1.2.2  nathanw 
   1212  1.1.2.2  nathanw struct device **
   1213  1.1.2.2  nathanw p1212_match_units(struct device *sc, struct p1212_dir *dir,
   1214  1.1.2.2  nathanw     int (*print)(void *, const char *))
   1215  1.1.2.2  nathanw {
   1216  1.1.2.2  nathanw 	struct p1212_dir **udirs;
   1217  1.1.2.2  nathanw 	struct device **devret, *dev;
   1218  1.1.2.2  nathanw 	int numdev;
   1219  1.1.2.2  nathanw 
   1220  1.1.2.2  nathanw 	/*
   1221  1.1.2.2  nathanw 	 * Setup typical return val. Always allocate one extra pointer for a
   1222  1.1.2.2  nathanw 	 * NULL guard end pointer.
   1223  1.1.2.2  nathanw 	 */
   1224  1.1.2.2  nathanw 
   1225  1.1.2.2  nathanw 	numdev = 0;
   1226  1.1.2.2  nathanw 	devret = malloc(sizeof(struct device *) * 2, M_DEVBUF, M_WAITOK);
   1227  1.1.2.2  nathanw 	devret[1] = NULL;
   1228  1.1.2.2  nathanw 
   1229  1.1.2.2  nathanw 	udirs = (struct p1212_dir **)p1212_find(dir, P1212_KEYTYPE_Directory,
   1230  1.1.2.2  nathanw 	    P1212_KEYVALUE_Unit_Directory,
   1231  1.1.2.2  nathanw 	    P1212_FIND_SEARCHALL|P1212_FIND_RETURNALL);
   1232  1.1.2.2  nathanw 
   1233  1.1.2.2  nathanw 	if (udirs) {
   1234  1.1.2.2  nathanw 		while (*udirs++) {
   1235  1.1.2.2  nathanw 			dev = config_found_sm(sc, udirs, print, NULL);
   1236  1.1.2.2  nathanw 			if (dev && numdev) {
   1237  1.1.2.2  nathanw 				devret = realloc(devret,
   1238  1.1.2.2  nathanw 				    sizeof(struct device *) *
   1239  1.1.2.2  nathanw 				    (numdev + 2), M_DEVBUF, M_WAITOK);
   1240  1.1.2.2  nathanw 				devret[numdev++] = dev;
   1241  1.1.2.2  nathanw 				devret[numdev] = NULL;
   1242  1.1.2.2  nathanw 			} else if (dev) {
   1243  1.1.2.2  nathanw 				devret[0] = dev;
   1244  1.1.2.2  nathanw 				numdev++;
   1245  1.1.2.2  nathanw 			}
   1246  1.1.2.2  nathanw 		}
   1247  1.1.2.2  nathanw 	}
   1248  1.1.2.2  nathanw 	if (numdev == 0) {
   1249  1.1.2.2  nathanw 		free(devret, M_DEVBUF);
   1250  1.1.2.2  nathanw 		return NULL;
   1251  1.1.2.2  nathanw 	}
   1252  1.1.2.2  nathanw 	return devret;
   1253  1.1.2.2  nathanw }
   1254  1.1.2.2  nathanw 
   1255  1.1.2.2  nathanw /*
   1256  1.1.2.2  nathanw  * Make these their own functions as they have slightly complicated rules.
   1257  1.1.2.2  nathanw  *
   1258  1.1.2.2  nathanw  * For example:
   1259  1.1.2.2  nathanw  *
   1260  1.1.2.2  nathanw  * Under normal circumstances only the 2 extent types can be offset
   1261  1.1.2.2  nathanw  * types. However some spec's which use p1212 like SBP2 for
   1262  1.1.2.2  nathanw  * firewire/1394 will define a dependent info type as an offset value.
   1263  1.1.2.2  nathanw  * Allow the upper level code to flag this and pass it down during
   1264  1.1.2.2  nathanw  * parsing. The same thing applies to immediate types.
   1265  1.1.2.2  nathanw  */
   1266  1.1.2.2  nathanw 
   1267  1.1.2.2  nathanw static int
   1268  1.1.2.2  nathanw p1212_validate_offset(u_int16_t val, u_int32_t mask)
   1269  1.1.2.2  nathanw {
   1270  1.1.2.2  nathanw         if ((val == P1212_KEYVALUE_Node_Units_Extent) ||
   1271  1.1.2.2  nathanw             (val == P1212_KEYVALUE_Node_Memory_Extent) ||
   1272  1.1.2.2  nathanw             ((mask & P1212_ALLOW_DEPENDENT_INFO_OFFSET_TYPE) &&
   1273  1.1.2.2  nathanw              ((val == P1212_KEYVALUE_Unit_Dependent_Info) ||
   1274  1.1.2.2  nathanw               (val == P1212_KEYVALUE_Node_Dependent_Info) ||
   1275  1.1.2.2  nathanw               (val == P1212_KEYVALUE_Module_Dependent_Info))))
   1276  1.1.2.2  nathanw                 return 0;
   1277  1.1.2.2  nathanw         return 1;
   1278  1.1.2.2  nathanw }
   1279  1.1.2.2  nathanw 
   1280  1.1.2.2  nathanw static int
   1281  1.1.2.2  nathanw p1212_validate_immed(u_int16_t val, u_int32_t mask)
   1282  1.1.2.2  nathanw {
   1283  1.1.2.2  nathanw 	switch (val) {
   1284  1.1.2.2  nathanw 	case P1212_KEYVALUE_Textual_Descriptor:
   1285  1.1.2.2  nathanw 	case P1212_KEYVALUE_Bus_Dependent_Info:
   1286  1.1.2.2  nathanw 	case P1212_KEYVALUE_Module_Dependent_Info:
   1287  1.1.2.2  nathanw 	case P1212_KEYVALUE_Node_Unique_Id:
   1288  1.1.2.2  nathanw 	case P1212_KEYVALUE_Node_Dependent_Info:
   1289  1.1.2.2  nathanw 	case P1212_KEYVALUE_Unit_Directory:
   1290  1.1.2.2  nathanw 	case P1212_KEYVALUE_Unit_Dependent_Info:
   1291  1.1.2.2  nathanw 	case P1212_KEYVALUE_Unit_Location:
   1292  1.1.2.2  nathanw 		if ((mask & P1212_ALLOW_DEPENDENT_INFO_IMMED_TYPE) &&
   1293  1.1.2.2  nathanw 		    ((val == P1212_KEYVALUE_Module_Dependent_Info) ||
   1294  1.1.2.2  nathanw 		     (val == P1212_KEYVALUE_Node_Dependent_Info) ||
   1295  1.1.2.2  nathanw 		     (val == P1212_KEYVALUE_Unit_Dependent_Info)))
   1296  1.1.2.2  nathanw 			break;
   1297  1.1.2.2  nathanw 		return 1;
   1298  1.1.2.2  nathanw 		break;
   1299  1.1.2.2  nathanw 	default:
   1300  1.1.2.2  nathanw 		break;
   1301  1.1.2.2  nathanw 	}
   1302  1.1.2.2  nathanw 	return 0;
   1303  1.1.2.2  nathanw }
   1304  1.1.2.2  nathanw 
   1305  1.1.2.2  nathanw static int
   1306  1.1.2.2  nathanw p1212_validate_leaf(u_int16_t val, u_int32_t mask)
   1307  1.1.2.2  nathanw {
   1308  1.1.2.2  nathanw 	switch(val) {
   1309  1.1.2.2  nathanw 	case P1212_KEYVALUE_Textual_Descriptor:
   1310  1.1.2.2  nathanw 	case P1212_KEYVALUE_Bus_Dependent_Info:
   1311  1.1.2.2  nathanw 	case P1212_KEYVALUE_Module_Dependent_Info:
   1312  1.1.2.2  nathanw 	case P1212_KEYVALUE_Node_Unique_Id:
   1313  1.1.2.2  nathanw 	case P1212_KEYVALUE_Node_Dependent_Info:
   1314  1.1.2.2  nathanw 	case P1212_KEYVALUE_Unit_Dependent_Info:
   1315  1.1.2.2  nathanw 	case P1212_KEYVALUE_Unit_Location:
   1316  1.1.2.2  nathanw 		break;
   1317  1.1.2.2  nathanw 	default:
   1318  1.1.2.2  nathanw 		return 1;
   1319  1.1.2.2  nathanw 		break;
   1320  1.1.2.2  nathanw 	}
   1321  1.1.2.2  nathanw 	return 0;
   1322  1.1.2.2  nathanw }
   1323  1.1.2.2  nathanw 
   1324  1.1.2.2  nathanw static int
   1325  1.1.2.2  nathanw p1212_validate_dir(u_int16_t val, u_int32_t mask)
   1326  1.1.2.2  nathanw {
   1327  1.1.2.2  nathanw 	switch(val) {
   1328  1.1.2.2  nathanw 	case P1212_KEYVALUE_Textual_Descriptor:
   1329  1.1.2.2  nathanw 	case P1212_KEYVALUE_Bus_Dependent_Info:
   1330  1.1.2.2  nathanw 	case P1212_KEYVALUE_Module_Dependent_Info:
   1331  1.1.2.2  nathanw 	case P1212_KEYVALUE_Node_Dependent_Info:
   1332  1.1.2.2  nathanw 	case P1212_KEYVALUE_Unit_Directory:
   1333  1.1.2.2  nathanw 	case P1212_KEYVALUE_Unit_Dependent_Info:
   1334  1.1.2.2  nathanw 		break;
   1335  1.1.2.2  nathanw 	default:
   1336  1.1.2.2  nathanw 		return 1;
   1337  1.1.2.2  nathanw 		break;
   1338  1.1.2.2  nathanw 	}
   1339  1.1.2.2  nathanw 	return 0;
   1340  1.1.2.2  nathanw }
   1341