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