1 /* $NetBSD: fwcrom.c,v 1.3 2006/05/10 06:24:03 skrll 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 union {
425 struct csrreg reg;
426 uint32_t i;
427 } u;
428
429 u.reg.key = key;
430 u.reg.val = val;
431
432 return(crom_add_quad(chunk, u.i));
433 }
434
435 int
436 crom_add_chunk(struct crom_src *src, struct crom_chunk *parent,
437 struct crom_chunk *child, int key)
438 {
439 int index;
440
441 if (parent == NULL) {
442 STAILQ_INSERT_TAIL(&src->chunk_list, child, link);
443 return(0);
444 }
445
446 index = crom_add_entry(parent, key, 0);
447 if (index < 0) {
448 return(-1);
449 }
450 child->ref_chunk = parent;
451 child->ref_index = index;
452 STAILQ_INSERT_TAIL(&src->chunk_list, child, link);
453 return(index);
454 }
455
456 #if defined(__FreeBSD__)
457 #define MAX_TEXT ((CROM_MAX_CHUNK_LEN + 1) * 4 - sizeof(struct csrtext))
458 #elif defined(__NetBSD__)
459 #define MAX_TEXT (int)((CROM_MAX_CHUNK_LEN + 1) * 4 - sizeof(struct csrtext))
460 #endif
461 int
462 crom_add_simple_text(struct crom_src *src, struct crom_chunk *parent,
463 struct crom_chunk *chunk, const char *buf)
464 {
465 struct csrtext *tl;
466 uint32_t *p;
467 int len, i;
468 char t[MAX_TEXT];
469
470 len = strlen(buf);
471 if (len > MAX_TEXT) {
472 #if defined(__DragonFly__) || __FreeBSD_version < 500000 || defined(__NetBSD__)
473 printf("text(%d) trancated to %d.\n", len, MAX_TEXT);
474 #else
475 printf("text(%d) trancated to %td.\n", len, MAX_TEXT);
476 #endif
477 len = MAX_TEXT;
478 }
479
480 tl = (struct csrtext *) &chunk->data;
481 tl->crc_len = howmany(sizeof(struct csrtext) + len, sizeof(uint32_t));
482 tl->spec_id = 0;
483 tl->spec_type = 0;
484 tl->lang_id = 0;
485 bzero(&t[0], roundup2(len, sizeof(uint32_t)));
486 bcopy(buf, &t[0], len);
487 p = (uint32_t *)&t[0];
488 for (i = 0; i < howmany(len, sizeof(uint32_t)); i ++)
489 tl->text[i] = ntohl(*p++);
490 return (crom_add_chunk(src, parent, chunk, CROM_TEXTLEAF));
491 }
492
493 static int
494 crom_copy(uint32_t *src, uint32_t *dst, int *offset, int len, int maxlen)
495 {
496 if (*offset + len > maxlen) {
497 printf("Config. ROM is too large for the buffer\n");
498 return(-1);
499 }
500 bcopy(src, (char *)(dst + *offset), len * sizeof(uint32_t));
501 *offset += len;
502 return(0);
503 }
504
505 int
506 crom_load(struct crom_src *src, uint32_t *buf, int maxlen)
507 {
508 struct crom_chunk *chunk, *parent;
509 struct csrhdr *hdr;
510 #ifdef _KERNEL
511 uint32_t *ptr;
512 int i;
513 #endif
514 int count, offset;
515 int len;
516
517 offset = 0;
518 /* Determine offset */
519 STAILQ_FOREACH(chunk, &src->chunk_list, link) {
520 chunk->offset = offset;
521 /* Assume the offset of the parent is already known */
522 parent = chunk->ref_chunk;
523 if (parent != NULL) {
524 struct csrreg *reg;
525 reg = (struct csrreg *)
526 &parent->data.buf[chunk->ref_index];
527 reg->val = offset -
528 (parent->offset + 1 + chunk->ref_index);
529 }
530 offset += 1 + chunk->data.crc_len;
531 }
532
533 /* Calculate CRC and dump to the buffer */
534 len = 1 + src->hdr.info_len;
535 count = 0;
536 if (crom_copy((uint32_t *)&src->hdr, buf, &count, len, maxlen) < 0)
537 return(-1);
538 STAILQ_FOREACH(chunk, &src->chunk_list, link) {
539 chunk->data.crc =
540 crom_crc(&chunk->data.buf[0], chunk->data.crc_len);
541
542 len = 1 + chunk->data.crc_len;
543 if (crom_copy((uint32_t *)&chunk->data, buf,
544 &count, len, maxlen) < 0)
545 return(-1);
546 }
547 hdr = (struct csrhdr *)buf;
548 hdr->crc_len = count - 1;
549 hdr->crc = crom_crc(&buf[1], hdr->crc_len);
550
551 #ifdef _KERNEL
552 /* byte swap */
553 ptr = buf;
554 for (i = 0; i < count; i ++) {
555 *ptr = htonl(*ptr);
556 ptr++;
557 }
558 #endif
559
560 return(count);
561 }
562 #endif
563
564 #ifdef TEST
565 int
566 main () {
567 struct crom_src src;
568 struct crom_chunk root,unit1,unit2,unit3;
569 struct crom_chunk text1,text2,text3,text4,text5,text6,text7;
570 uint32_t buf[256], *p;
571 int i;
572
573 bzero(&src, sizeof(src));
574 bzero(&root, sizeof(root));
575 bzero(&unit1, sizeof(unit1));
576 bzero(&unit2, sizeof(unit2));
577 bzero(&unit3, sizeof(unit3));
578 bzero(&text1, sizeof(text1));
579 bzero(&text2, sizeof(text2));
580 bzero(&text3, sizeof(text3));
581 bzero(&text3, sizeof(text4));
582 bzero(&text3, sizeof(text5));
583 bzero(&text3, sizeof(text6));
584 bzero(&text3, sizeof(text7));
585 bzero(buf, sizeof(buf));
586
587 /* BUS info sample */
588 src.hdr.info_len = 4;
589 src.businfo.bus_name = CSR_BUS_NAME_IEEE1394;
590 src.businfo.eui64.hi = 0x11223344;
591 src.businfo.eui64.lo = 0x55667788;
592 src.businfo.link_spd = FWSPD_S400;
593 src.businfo.generation = 0;
594 src.businfo.max_rom = MAXROM_4;
595 src.businfo.max_rec = 10;
596 src.businfo.cyc_clk_acc = 100;
597 src.businfo.pmc = 0;
598 src.businfo.bmc = 1;
599 src.businfo.isc = 1;
600 src.businfo.cmc = 1;
601 src.businfo.irmc = 1;
602 STAILQ_INIT(&src.chunk_list);
603
604 /* Root directory */
605 crom_add_chunk(&src, NULL, &root, 0);
606 crom_add_entry(&root, CSRKEY_NCAP, 0x123456);
607 /* private company_id */
608 crom_add_entry(&root, CSRKEY_VENDOR, 0xacde48);
609
610 crom_add_simple_text(&src, &root, &text1, OS_STR);
611 crom_add_entry(&root, CSRKEY_HW, OS_VER);
612 crom_add_simple_text(&src, &root, &text2, OS_VER_STR);
613
614 /* SBP unit directory */
615 crom_add_chunk(&src, &root, &unit1, CROM_UDIR);
616 crom_add_entry(&unit1, CSRKEY_SPEC, CSRVAL_ANSIT10);
617 crom_add_entry(&unit1, CSRKEY_VER, CSRVAL_T10SBP2);
618 crom_add_entry(&unit1, CSRKEY_COM_SPEC, CSRVAL_ANSIT10);
619 crom_add_entry(&unit1, CSRKEY_COM_SET, CSRVAL_SCSI);
620 /* management_agent */
621 crom_add_entry(&unit1, CROM_MGM, 0x1000);
622 crom_add_entry(&unit1, CSRKEY_UNIT_CH, (10<<8) | 8);
623 /* Device type and LUN */
624 crom_add_entry(&unit1, CROM_LUN, 0);
625 crom_add_entry(&unit1, CSRKEY_MODEL, 1);
626 crom_add_simple_text(&src, &unit1, &text3, "scsi_target");
627
628 /* RFC2734 IPv4 over IEEE1394 */
629 crom_add_chunk(&src, &root, &unit2, CROM_UDIR);
630 crom_add_entry(&unit2, CSRKEY_SPEC, CSRVAL_IETF);
631 crom_add_simple_text(&src, &unit2, &text4, "IANA");
632 crom_add_entry(&unit2, CSRKEY_VER, 1);
633 crom_add_simple_text(&src, &unit2, &text5, "IPv4");
634
635 /* RFC3146 IPv6 over IEEE1394 */
636 crom_add_chunk(&src, &root, &unit3, CROM_UDIR);
637 crom_add_entry(&unit3, CSRKEY_SPEC, CSRVAL_IETF);
638 crom_add_simple_text(&src, &unit3, &text6, "IANA");
639 crom_add_entry(&unit3, CSRKEY_VER, 2);
640 crom_add_simple_text(&src, &unit3, &text7, "IPv6");
641
642 crom_load(&src, buf, 256);
643 p = buf;
644 #define DUMP_FORMAT "%08x %08x %08x %08x %08x %08x %08x %08x\n"
645 for (i = 0; i < 256/8; i ++) {
646 printf(DUMP_FORMAT,
647 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
648 p += 8;
649 }
650 return(0);
651 }
652 #endif
Cache object: 5e18a60a7a645b446c58a2acffb83672
|