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