fwcrom.c revision 1.2 1 /* $NetBSD: fwcrom.c,v 1.2 2005/12/11 12:22:02 christos Exp $ */
2 /*-
3 * Copyright (c) 2002-2003
4 * Hidetoshi Shimokawa. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 *
17 * This product includes software developed by Hidetoshi Shimokawa.
18 *
19 * 4. Neither the name of the author nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifdef __FreeBSD__
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD: src/sys/dev/firewire/fwcrom.c,v 1.12 2004/08/29 13:45:55 simokawa Exp $");
39 #endif
40
41 #if defined(__FreeBSD__)
42 #include <sys/param.h>
43 #if defined(_KERNEL) || defined(TEST)
44 #include <sys/queue.h>
45 #endif
46 #ifdef _KERNEL
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #else
50 #include <netinet/in.h>
51 #include <fcntl.h>
52 #include <stdio.h>
53 #include <err.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #endif
57
58 #ifdef __DragonFly__
59 #include "fw_port.h"
60 #include "firewire.h"
61 #include "iec13213.h"
62 #else
63 #include <dev/firewire/fw_port.h>
64 #include <dev/firewire/firewire.h>
65 #include <dev/firewire/iec13213.h>
66 #endif
67 #elif defined(__NetBSD__)
68 #include <sys/param.h>
69 #ifdef _KERNEL
70 #include <sys/device.h>
71 #include <sys/errno.h>
72 #include <sys/systm.h>
73 #else
74 #include <stdio.h>
75 #include <string.h>
76 #endif
77 #include <dev/ieee1394/fw_port.h>
78 #include <dev/ieee1394/firewire.h>
79 #include <dev/ieee1394/iec13213.h>
80 #endif
81
82 #define MAX_ROM (1024 - sizeof(uint32_t) * 5)
83 #define CROM_END(cc) ((vm_offset_t)(cc)->stack[0].dir + MAX_ROM - 1)
84
85 void
86 crom_init_context(struct crom_context *cc, uint32_t *p)
87 {
88 struct csrhdr *hdr;
89
90 hdr = (struct csrhdr *)p;
91 if (hdr->info_len <= 1) {
92 /* minimum or invalid ROM */
93 cc->depth = -1;
94 return;
95 }
96 p += 1 + hdr->info_len;
97
98 /* check size of root directory */
99 if (((struct csrdirectory *)p)->crc_len == 0) {
100 cc->depth = -1;
101 return;
102 }
103 cc->depth = 0;
104 cc->stack[0].dir = (struct csrdirectory *)p;
105 cc->stack[0].index = 0;
106 }
107
108 struct csrreg *
109 crom_get(struct crom_context *cc)
110 {
111 struct crom_ptr *ptr;
112
113 ptr = &cc->stack[cc->depth];
114 return (&ptr->dir->entry[ptr->index]);
115 }
116
117 void
118 crom_next(struct crom_context *cc)
119 {
120 struct crom_ptr *ptr;
121 struct csrreg *reg;
122
123 if (cc->depth < 0)
124 return;
125 reg = crom_get(cc);
126 if ((reg->key & CSRTYPE_MASK) == CSRTYPE_D) {
127 if (cc->depth >= CROM_MAX_DEPTH) {
128 printf("crom_next: too deep\n");
129 goto again;
130 }
131 cc->depth ++;
132
133 ptr = &cc->stack[cc->depth];
134 ptr->dir = (struct csrdirectory *) (reg + reg->val);
135 ptr->index = 0;
136 goto check;
137 }
138 again:
139 ptr = &cc->stack[cc->depth];
140 ptr->index ++;
141 check:
142 if (ptr->index < ptr->dir->crc_len &&
143 (vm_offset_t)crom_get(cc) <= CROM_END(cc))
144 return;
145
146 if (ptr->index < ptr->dir->crc_len)
147 printf("crom_next: bound check failed\n");
148
149 if (cc->depth > 0) {
150 cc->depth--;
151 goto again;
152 }
153 /* no more data */
154 cc->depth = -1;
155 }
156
157
158 struct csrreg *
159 crom_search_key(struct crom_context *cc, uint8_t key)
160 {
161 struct csrreg *reg;
162
163 while(cc->depth >= 0) {
164 reg = crom_get(cc);
165 if (reg->key == key)
166 return reg;
167 crom_next(cc);
168 }
169 return NULL;
170 }
171
172 int
173 crom_has_specver(uint32_t *p, uint32_t spec, uint32_t ver)
174 {
175 struct csrreg *reg;
176 struct crom_context c, *cc;
177 int state = 0;
178
179 cc = &c;
180 crom_init_context(cc, p);
181 while(cc->depth >= 0) {
182 reg = crom_get(cc);
183 if (state == 0) {
184 if (reg->key == CSRKEY_SPEC && reg->val == spec)
185 state = 1;
186 else
187 state = 0;
188 } else {
189 if (reg->key == CSRKEY_VER && reg->val == ver)
190 return 1;
191 else
192 state = 0;
193 }
194 crom_next(cc);
195 }
196 return 0;
197 }
198
199 void
200 crom_parse_text(struct crom_context *cc, char *buf, int len)
201 {
202 struct csrreg *reg;
203 struct csrtext *textleaf;
204 uint32_t *bp;
205 int i, qlen;
206 static char *nullstr = (char *)&"(null)";
207
208 if (cc->depth < 0)
209 return;
210
211 reg = crom_get(cc);
212 if (reg->key != CROM_TEXTLEAF ||
213 (vm_offset_t)(reg + reg->val) > CROM_END(cc)) {
214 strncpy(buf, nullstr, len);
215 return;
216 }
217 textleaf = (struct csrtext *)(reg + reg->val);
218
219 if ((vm_offset_t)textleaf + textleaf->crc_len > CROM_END(cc)) {
220 strncpy(buf, nullstr, len);
221 return;
222 }
223
224 /* XXX should check spec and type */
225
226 bp = (uint32_t *)&buf[0];
227 qlen = textleaf->crc_len - 2;
228 if (len < qlen * 4)
229 qlen = len/4;
230 for (i = 0; i < qlen; i ++)
231 *bp++ = ntohl(textleaf->text[i]);
232 /* make sure to terminate the string */
233 if (len <= qlen * 4)
234 buf[len - 1] = 0;
235 else
236 buf[qlen * 4] = 0;
237 }
238
239 uint16_t
240 crom_crc(uint32_t *ptr, int len)
241 {
242 int i, shift;
243 uint32_t data, sum, crc = 0;
244
245 for (i = 0; i < len; i++) {
246 data = ptr[i];
247 for (shift = 28; shift >= 0; shift -= 4) {
248 sum = ((crc >> 12) ^ (data >> shift)) & 0xf;
249 crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ sum;
250 }
251 crc &= 0xffff;
252 }
253 return((uint16_t) crc);
254 }
255
256 #ifndef _KERNEL
257 static void
258 crom_desc_specver(uint32_t spec, uint32_t ver, char *buf, int len)
259 {
260 char *s = NULL;
261
262 if (spec == CSRVAL_ANSIT10 || spec == 0) {
263 switch (ver) {
264 case CSRVAL_T10SBP2:
265 s = "SBP-2";
266 break;
267 default:
268 if (spec != 0)
269 s = "unknown ANSIT10";
270 }
271 }
272 if (spec == CSRVAL_1394TA || spec == 0) {
273 switch (ver) {
274 case CSR_PROTAVC:
275 s = "AV/C";
276 break;
277 case CSR_PROTCAL:
278 s = "CAL";
279 break;
280 case CSR_PROTEHS:
281 s = "EHS";
282 break;
283 case CSR_PROTHAVI:
284 s = "HAVi";
285 break;
286 case CSR_PROTCAM104:
287 s = "1394 Cam 1.04";
288 break;
289 case CSR_PROTCAM120:
290 s = "1394 Cam 1.20";
291 break;
292 case CSR_PROTCAM130:
293 s = "1394 Cam 1.30";
294 break;
295 case CSR_PROTDPP:
296 s = "1394 Direct print";
297 break;
298 case CSR_PROTIICP:
299 s = "Industrial & Instrument";
300 break;
301 default:
302 if (spec != 0)
303 s = "unknown 1394TA";
304 }
305 }
306 if (s != NULL)
307 snprintf(buf, len, "%s", s);
308 }
309
310 char *
311 crom_desc(struct crom_context *cc, char *buf, int len)
312 {
313 struct csrreg *reg;
314 struct csrdirectory *dir;
315 char *desc;
316 uint16_t crc;
317
318 reg = crom_get(cc);
319 switch (reg->key & CSRTYPE_MASK) {
320 case CSRTYPE_I:
321 #if 0
322 len -= snprintf(buf, len, "%d", reg->val);
323 buf += strlen(buf);
324 #else
325 *buf = '\0';
326 #endif
327 break;
328 case CSRTYPE_C:
329 len -= snprintf(buf, len, "offset=0x%04x(%d)",
330 reg->val, reg->val);
331 buf += strlen(buf);
332 break;
333 case CSRTYPE_L:
334 /* XXX fall through */
335 case CSRTYPE_D:
336 dir = (struct csrdirectory *) (reg + reg->val);
337 crc = crom_crc((uint32_t *)&dir->entry[0], dir->crc_len);
338 len -= snprintf(buf, len, "len=%d crc=0x%04x(%s) ",
339 dir->crc_len, dir->crc,
340 (crc == dir->crc) ? "OK" : "NG");
341 buf += strlen(buf);
342 }
343 switch (reg->key) {
344 case 0x03:
345 desc = "module_vendor_ID";
346 break;
347 case 0x04:
348 desc = "hardware_version";
349 break;
350 case 0x0c:
351 desc = "node_capabilities";
352 break;
353 case 0x12:
354 desc = "unit_spec_ID";
355 break;
356 case 0x13:
357 desc = "unit_sw_version";
358 crom_desc_specver(0, reg->val, buf, len);
359 break;
360 case 0x14:
361 desc = "logical_unit_number";
362 break;
363 case 0x17:
364 desc = "model_ID";
365 break;
366 case 0x38:
367 desc = "command_set_spec_ID";
368 break;
369 case 0x39:
370 desc = "command_set";
371 break;
372 case 0x3a:
373 desc = "unit_characteristics";
374 break;
375 case 0x3b:
376 desc = "command_set_revision";
377 break;
378 case 0x3c:
379 desc = "firmware_revision";
380 break;
381 case 0x3d:
382 desc = "reconnect_timeout";
383 break;
384 case 0x54:
385 desc = "management_agent";
386 break;
387 case 0x81:
388 desc = "text_leaf";
389 crom_parse_text(cc, buf + strlen(buf), len);
390 break;
391 case 0xd1:
392 desc = "unit_directory";
393 break;
394 case 0xd4:
395 desc = "logical_unit_directory";
396 break;
397 default:
398 desc = "unknown";
399 }
400 return desc;
401 }
402 #endif
403
404 #if defined(_KERNEL) || defined(TEST)
405
406 int
407 crom_add_quad(struct crom_chunk *chunk, uint32_t entry)
408 {
409 int index;
410
411 index = chunk->data.crc_len;
412 if (index >= CROM_MAX_CHUNK_LEN - 1) {
413 printf("too large chunk %d\n", index);
414 return(-1);
415 }
416 chunk->data.buf[index] = entry;
417 chunk->data.crc_len++;
418 return(index);
419 }
420
421 int
422 crom_add_entry(struct crom_chunk *chunk, int key, int val)
423 {
424 struct csrreg *reg;
425 uint32_t i;
426
427 #if defined(__FreeBSD__)
428 reg = (struct csrreg *)&i;
429 #elif defined(__NetBSD__)
430 (uint32_t *)reg = &i;
431 #endif
432 reg->key = key;
433 reg->val = val;
434 return(crom_add_quad(chunk, (uint32_t) i));
435 }
436
437 int
438 crom_add_chunk(struct crom_src *src, struct crom_chunk *parent,
439 struct crom_chunk *child, int key)
440 {
441 int index;
442
443 if (parent == NULL) {
444 STAILQ_INSERT_TAIL(&src->chunk_list, child, link);
445 return(0);
446 }
447
448 index = crom_add_entry(parent, key, 0);
449 if (index < 0) {
450 return(-1);
451 }
452 child->ref_chunk = parent;
453 child->ref_index = index;
454 STAILQ_INSERT_TAIL(&src->chunk_list, child, link);
455 return(index);
456 }
457
458 #if defined(__FreeBSD__)
459 #define MAX_TEXT ((CROM_MAX_CHUNK_LEN + 1) * 4 - sizeof(struct csrtext))
460 #elif defined(__NetBSD__)
461 #define MAX_TEXT (int)((CROM_MAX_CHUNK_LEN + 1) * 4 - sizeof(struct csrtext))
462 #endif
463 int
464 crom_add_simple_text(struct crom_src *src, struct crom_chunk *parent,
465 struct crom_chunk *chunk, const char *buf)
466 {
467 struct csrtext *tl;
468 uint32_t *p;
469 int len, i;
470 char t[MAX_TEXT];
471
472 len = strlen(buf);
473 if (len > MAX_TEXT) {
474 #if defined(__DragonFly__) || __FreeBSD_version < 500000 || defined(__NetBSD__)
475 printf("text(%d) trancated to %d.\n", len, MAX_TEXT);
476 #else
477 printf("text(%d) trancated to %td.\n", len, MAX_TEXT);
478 #endif
479 len = MAX_TEXT;
480 }
481
482 tl = (struct csrtext *) &chunk->data;
483 tl->crc_len = howmany(sizeof(struct csrtext) + len, sizeof(uint32_t));
484 tl->spec_id = 0;
485 tl->spec_type = 0;
486 tl->lang_id = 0;
487 bzero(&t[0], roundup2(len, sizeof(uint32_t)));
488 bcopy(buf, &t[0], len);
489 p = (uint32_t *)&t[0];
490 for (i = 0; i < howmany(len, sizeof(uint32_t)); i ++)
491 tl->text[i] = ntohl(*p++);
492 return (crom_add_chunk(src, parent, chunk, CROM_TEXTLEAF));
493 }
494
495 static int
496 crom_copy(uint32_t *src, uint32_t *dst, int *offset, int len, int maxlen)
497 {
498 if (*offset + len > maxlen) {
499 printf("Config. ROM is too large for the buffer\n");
500 return(-1);
501 }
502 bcopy(src, (char *)(dst + *offset), len * sizeof(uint32_t));
503 *offset += len;
504 return(0);
505 }
506
507 int
508 crom_load(struct crom_src *src, uint32_t *buf, int maxlen)
509 {
510 struct crom_chunk *chunk, *parent;
511 struct csrhdr *hdr;
512 #ifdef _KERNEL
513 uint32_t *ptr;
514 int i;
515 #endif
516 int count, offset;
517 int len;
518
519 offset = 0;
520 /* Determine offset */
521 STAILQ_FOREACH(chunk, &src->chunk_list, link) {
522 chunk->offset = offset;
523 /* Assume the offset of the parent is already known */
524 parent = chunk->ref_chunk;
525 if (parent != NULL) {
526 struct csrreg *reg;
527 reg = (struct csrreg *)
528 &parent->data.buf[chunk->ref_index];
529 reg->val = offset -
530 (parent->offset + 1 + chunk->ref_index);
531 }
532 offset += 1 + chunk->data.crc_len;
533 }
534
535 /* Calculate CRC and dump to the buffer */
536 len = 1 + src->hdr.info_len;
537 count = 0;
538 if (crom_copy((uint32_t *)&src->hdr, buf, &count, len, maxlen) < 0)
539 return(-1);
540 STAILQ_FOREACH(chunk, &src->chunk_list, link) {
541 chunk->data.crc =
542 crom_crc(&chunk->data.buf[0], chunk->data.crc_len);
543
544 len = 1 + chunk->data.crc_len;
545 if (crom_copy((uint32_t *)&chunk->data, buf,
546 &count, len, maxlen) < 0)
547 return(-1);
548 }
549 hdr = (struct csrhdr *)buf;
550 hdr->crc_len = count - 1;
551 hdr->crc = crom_crc(&buf[1], hdr->crc_len);
552
553 #ifdef _KERNEL
554 /* byte swap */
555 ptr = buf;
556 for (i = 0; i < count; i ++) {
557 *ptr = htonl(*ptr);
558 ptr++;
559 }
560 #endif
561
562 return(count);
563 }
564 #endif
565
566 #ifdef TEST
567 int
568 main () {
569 struct crom_src src;
570 struct crom_chunk root,unit1,unit2,unit3;
571 struct crom_chunk text1,text2,text3,text4,text5,text6,text7;
572 uint32_t buf[256], *p;
573 int i;
574
575 bzero(&src, sizeof(src));
576 bzero(&root, sizeof(root));
577 bzero(&unit1, sizeof(unit1));
578 bzero(&unit2, sizeof(unit2));
579 bzero(&unit3, sizeof(unit3));
580 bzero(&text1, sizeof(text1));
581 bzero(&text2, sizeof(text2));
582 bzero(&text3, sizeof(text3));
583 bzero(&text3, sizeof(text4));
584 bzero(&text3, sizeof(text5));
585 bzero(&text3, sizeof(text6));
586 bzero(&text3, sizeof(text7));
587 bzero(buf, sizeof(buf));
588
589 /* BUS info sample */
590 src.hdr.info_len = 4;
591 src.businfo.bus_name = CSR_BUS_NAME_IEEE1394;
592 src.businfo.eui64.hi = 0x11223344;
593 src.businfo.eui64.lo = 0x55667788;
594 src.businfo.link_spd = FWSPD_S400;
595 src.businfo.generation = 0;
596 src.businfo.max_rom = MAXROM_4;
597 src.businfo.max_rec = 10;
598 src.businfo.cyc_clk_acc = 100;
599 src.businfo.pmc = 0;
600 src.businfo.bmc = 1;
601 src.businfo.isc = 1;
602 src.businfo.cmc = 1;
603 src.businfo.irmc = 1;
604 STAILQ_INIT(&src.chunk_list);
605
606 /* Root directory */
607 crom_add_chunk(&src, NULL, &root, 0);
608 crom_add_entry(&root, CSRKEY_NCAP, 0x123456);
609 /* private company_id */
610 crom_add_entry(&root, CSRKEY_VENDOR, 0xacde48);
611
612 crom_add_simple_text(&src, &root, &text1, OS_STR);
613 crom_add_entry(&root, CSRKEY_HW, OS_VER);
614 crom_add_simple_text(&src, &root, &text2, OS_VER_STR);
615
616 /* SBP unit directory */
617 crom_add_chunk(&src, &root, &unit1, CROM_UDIR);
618 crom_add_entry(&unit1, CSRKEY_SPEC, CSRVAL_ANSIT10);
619 crom_add_entry(&unit1, CSRKEY_VER, CSRVAL_T10SBP2);
620 crom_add_entry(&unit1, CSRKEY_COM_SPEC, CSRVAL_ANSIT10);
621 crom_add_entry(&unit1, CSRKEY_COM_SET, CSRVAL_SCSI);
622 /* management_agent */
623 crom_add_entry(&unit1, CROM_MGM, 0x1000);
624 crom_add_entry(&unit1, CSRKEY_UNIT_CH, (10<<8) | 8);
625 /* Device type and LUN */
626 crom_add_entry(&unit1, CROM_LUN, 0);
627 crom_add_entry(&unit1, CSRKEY_MODEL, 1);
628 crom_add_simple_text(&src, &unit1, &text3, "scsi_target");
629
630 /* RFC2734 IPv4 over IEEE1394 */
631 crom_add_chunk(&src, &root, &unit2, CROM_UDIR);
632 crom_add_entry(&unit2, CSRKEY_SPEC, CSRVAL_IETF);
633 crom_add_simple_text(&src, &unit2, &text4, "IANA");
634 crom_add_entry(&unit2, CSRKEY_VER, 1);
635 crom_add_simple_text(&src, &unit2, &text5, "IPv4");
636
637 /* RFC3146 IPv6 over IEEE1394 */
638 crom_add_chunk(&src, &root, &unit3, CROM_UDIR);
639 crom_add_entry(&unit3, CSRKEY_SPEC, CSRVAL_IETF);
640 crom_add_simple_text(&src, &unit3, &text6, "IANA");
641 crom_add_entry(&unit3, CSRKEY_VER, 2);
642 crom_add_simple_text(&src, &unit3, &text7, "IPv6");
643
644 crom_load(&src, buf, 256);
645 p = buf;
646 #define DUMP_FORMAT "%08x %08x %08x %08x %08x %08x %08x %08x\n"
647 for (i = 0; i < 256/8; i ++) {
648 printf(DUMP_FORMAT,
649 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
650 p += 8;
651 }
652 return(0);
653 }
654 #endif
655