2 * Copyright (c) 2002-2003
3 * Hidetoshi Shimokawa. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
16 * This product includes software developed by Hidetoshi Shimokawa.
18 * 4. Neither the name of the author nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $FreeBSD: src/sys/dev/firewire/fwcrom.c,v 1.2.2.2 2003/04/28 03:29:18 simokawa Exp $
35 * $DragonFly: src/sys/bus/firewire/fwcrom.c,v 1.3 2003/08/07 21:16:45 dillon Exp $
38 #include <sys/param.h>
39 #if defined(_KERNEL) || defined(TEST)
40 #include <sys/queue.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <netinet/in.h>
57 crom_init_context(struct crom_context *cc, u_int32_t *p)
61 hdr = (struct csrhdr *)p;
62 if (hdr->info_len == 1) {
66 p += 1 + hdr->info_len;
68 /* check size of root directory */
69 if (((struct csrdirectory *)p)->crc_len == 0) {
74 cc->stack[0].dir = (struct csrdirectory *)p;
75 cc->stack[0].index = 0;
79 crom_get(struct crom_context *cc)
83 ptr = &cc->stack[cc->depth];
84 return (&ptr->dir->entry[ptr->index]);
88 crom_next(struct crom_context *cc)
96 if ((reg->key & CSRTYPE_MASK) == CSRTYPE_D) {
98 if (cc->depth > CROM_MAX_DEPTH) {
99 printf("crom_next: too deep\n");
103 ptr = &cc->stack[cc->depth];
104 ptr->dir = (struct csrdirectory *) (reg + reg->val);
109 ptr = &cc->stack[cc->depth];
112 if (ptr->index < ptr->dir->crc_len)
124 crom_search_key(struct crom_context *cc, u_int8_t key)
128 while(cc->depth >= 0) {
138 crom_has_specver(u_int32_t *p, u_int32_t spec, u_int32_t ver)
141 struct crom_context c, *cc;
145 crom_init_context(cc, p);
146 while(cc->depth >= 0) {
149 if (reg->key == CSRKEY_SPEC && reg->val == spec)
154 if (reg->key == CSRKEY_VER && reg->val == ver)
165 crom_parse_text(struct crom_context *cc, char *buf, int len)
168 struct csrtext *textleaf;
171 static char *nullstr = "(null)";
177 if (reg->key != CROM_TEXTLEAF) {
178 strncpy(buf, nullstr, len);
181 textleaf = (struct csrtext *)(reg + reg->val);
183 /* XXX should check spec and type */
185 bp = (u_int32_t *)&buf[0];
186 qlen = textleaf->crc_len - 2;
189 for (i = 0; i < qlen; i ++)
190 *bp++ = ntohl(textleaf->text[i]);
191 /* make sure to terminate the string */
199 crom_crc(u_int32_t *ptr, int len)
202 u_int32_t data, sum, crc = 0;
204 for (i = 0; i < len; i++) {
206 for (shift = 28; shift >= 0; shift -= 4) {
207 sum = ((crc >> 12) ^ (data >> shift)) & 0xf;
208 crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ sum;
212 return((u_int16_t) crc);
217 crom_desc_specver(u_int32_t spec, u_int32_t ver, char *buf, int len)
221 if (spec == CSRVAL_ANSIT10 || spec == 0) {
228 s = "unknown ANSIT10";
231 if (spec == CSRVAL_1394TA || spec == 0) {
255 s = "1394 Direct print";
258 s = "Industrial & Instrument";
262 s = "unknown 1394TA";
266 snprintf(buf, len, "%s", s);
270 crom_desc(struct crom_context *cc, char *buf, int len)
273 struct csrdirectory *dir;
278 switch (reg->key & CSRTYPE_MASK) {
281 len -= snprintf(buf, len, "%d", reg->val);
288 len -= snprintf(buf, len, "offset=0x%04x(%d)",
293 /* XXX fall through */
295 dir = (struct csrdirectory *) (reg + reg->val);
296 crc = crom_crc((u_int32_t *)&dir->entry[0], dir->crc_len);
297 len -= snprintf(buf, len, "len=%d crc=0x%04x(%s) ",
298 dir->crc_len, dir->crc,
299 (crc == dir->crc) ? "OK" : "NG");
304 desc = "module_vendor_ID";
307 desc = "hardware_version";
310 desc = "node_capabilities";
313 desc = "unit_spec_ID";
316 desc = "unit_sw_version";
317 crom_desc_specver(0, reg->val, buf, len);
320 desc = "logical_unit_number";
326 desc = "command_set_spec_ID";
329 desc = "command_set";
332 desc = "unit_characteristics";
335 desc = "command_set_revision";
338 desc = "firmware_revision";
341 desc = "reconnect_timeout";
344 desc = "management_agent";
348 crom_parse_text(cc, buf + strlen(buf), len);
351 desc = "unit_directory";
354 desc = "logical_unit_directory";
363 #if defined(_KERNEL) || defined(TEST)
366 crom_add_quad(struct crom_chunk *chunk, u_int32_t entry)
370 index = chunk->data.crc_len;
371 if (index >= CROM_MAX_CHUNK_LEN - 1) {
372 printf("too large chunk %d\n", index);
375 chunk->data.buf[index] = entry;
376 chunk->data.crc_len++;
381 crom_add_entry(struct crom_chunk *chunk, int key, int val)
386 reg = (struct csrreg *)&i;
389 return(crom_add_quad(chunk, (u_int32_t) i));
393 crom_add_chunk(struct crom_src *src, struct crom_chunk *parent,
394 struct crom_chunk *child, int key)
398 if (parent == NULL) {
399 STAILQ_INSERT_TAIL(&src->chunk_list, child, link);
403 index = crom_add_entry(parent, key, 0);
407 child->ref_chunk = parent;
408 child->ref_index = index;
409 STAILQ_INSERT_TAIL(&src->chunk_list, child, link);
414 crom_add_simple_text(struct crom_src *src, struct crom_chunk *parent,
415 struct crom_chunk *chunk, char *buf)
422 #define MAX_TEXT ((CROM_MAX_CHUNK_LEN + 1) * 4 - sizeof(struct csrtext))
423 if (len > MAX_TEXT) {
424 #if __FreeBSD_version < 500000
425 printf("text(%d) trancated to %d.\n", len, MAX_TEXT);
427 printf("text(%d) trancated to %td.\n", len, MAX_TEXT);
432 tl = (struct csrtext *) &chunk->data;
433 tl->crc_len = howmany(sizeof(struct csrtext) + len, sizeof(u_int32_t));
437 p = (u_int32_t *) buf;
438 for (i = 0; i < howmany(len, sizeof(u_int32_t)) / 4; i ++)
439 tl->text[i] = ntohl(*p++);
440 return (crom_add_chunk(src, parent, chunk, CROM_TEXTLEAF));
444 crom_copy(u_int32_t *src, u_int32_t *dst, int *offset, int len, int maxlen)
446 if (*offset + len > maxlen) {
447 printf("Config. ROM is too large for the buffer\n");
450 bcopy(src, (char *)(dst + *offset), len * sizeof(u_int32_t));
456 crom_load(struct crom_src *src, u_int32_t *buf, int maxlen)
458 struct crom_chunk *chunk, *parent;
467 /* Determine offset */
468 STAILQ_FOREACH(chunk, &src->chunk_list, link) {
469 chunk->offset = offset;
470 /* Assume the offset of the parent is already known */
471 parent = chunk->ref_chunk;
472 if (parent != NULL) {
474 reg = (struct csrreg *)
475 &parent->data.buf[chunk->ref_index];
477 (parent->offset + 1 + chunk->ref_index);
479 offset += 1 + chunk->data.crc_len;
482 /* Calculate CRC and dump to the buffer */
483 len = 1 + src->hdr.info_len;
485 if (crom_copy((u_int32_t *)&src->hdr, buf, &count, len, maxlen) < 0)
487 STAILQ_FOREACH(chunk, &src->chunk_list, link) {
489 crom_crc(&chunk->data.buf[0], chunk->data.crc_len);
491 len = 1 + chunk->data.crc_len;
492 if (crom_copy((u_int32_t *)&chunk->data, buf,
493 &count, len, maxlen) < 0)
496 hdr = (struct csrhdr *)buf;
497 hdr->crc_len = count - 1;
498 hdr->crc = crom_crc(buf + 1, hdr->crc_len);
503 for (i = 0; i < count; i ++) {
517 struct crom_chunk root,unit1,unit2,unit3;
518 struct crom_chunk text1,text2,text3,text4,text5,text6,text7;
519 u_int32_t buf[256], *p;
522 bzero(&src, sizeof(src));
523 bzero(&root, sizeof(root));
524 bzero(&unit1, sizeof(unit1));
525 bzero(&unit2, sizeof(unit2));
526 bzero(&unit3, sizeof(unit3));
527 bzero(&text1, sizeof(text1));
528 bzero(&text2, sizeof(text2));
529 bzero(&text3, sizeof(text3));
530 bzero(&text3, sizeof(text4));
531 bzero(&text3, sizeof(text5));
532 bzero(&text3, sizeof(text6));
533 bzero(&text3, sizeof(text7));
534 bzero(buf, sizeof(buf));
536 /* BUS info sample */
537 src.hdr.info_len = 4;
538 src.businfo.bus_name = CSR_BUS_NAME_IEEE1394;
539 src.businfo.eui64.hi = 0x11223344;
540 src.businfo.eui64.lo = 0x55667788;
541 src.businfo.link_spd = FWSPD_S400;
542 src.businfo.generation = 0;
543 src.businfo.max_rom = MAXROM_4;
544 src.businfo.max_rec = 10;
545 src.businfo.cyc_clk_acc = 100;
550 src.businfo.irmc = 1;
551 STAILQ_INIT(&src.chunk_list);
554 crom_add_chunk(&src, NULL, &root, 0);
555 crom_add_entry(&root, CSRKEY_NCAP, 0x123456);
556 /* private company_id */
557 crom_add_entry(&root, CSRKEY_VENDOR, 0xacde48);
559 crom_add_simple_text(&src, &root, &text1, "FreeBSD");
560 crom_add_entry(&root, CSRKEY_HW, __FreeBSD_version);
561 crom_add_simple_text(&src, &root, &text2, "FreeBSD-5");
563 /* SBP unit directory */
564 crom_add_chunk(&src, &root, &unit1, CROM_UDIR);
565 crom_add_entry(&unit1, CSRKEY_SPEC, CSRVAL_ANSIT10);
566 crom_add_entry(&unit1, CSRKEY_VER, CSRVAL_T10SBP2);
567 crom_add_entry(&unit1, CSRKEY_COM_SPEC, CSRVAL_ANSIT10);
568 crom_add_entry(&unit1, CSRKEY_COM_SET, CSRVAL_SCSI);
569 /* management_agent */
570 crom_add_entry(&unit1, CROM_MGM, 0x1000);
571 crom_add_entry(&unit1, CSRKEY_UNIT_CH, (10<<8) | 8);
572 /* Device type and LUN */
573 crom_add_entry(&unit1, CROM_LUN, 0);
574 crom_add_entry(&unit1, CSRKEY_MODEL, 1);
575 crom_add_simple_text(&src, &unit1, &text3, "scsi_target");
577 /* RFC2734 IPv4 over IEEE1394 */
578 crom_add_chunk(&src, &root, &unit2, CROM_UDIR);
579 crom_add_entry(&unit2, CSRKEY_SPEC, CSRVAL_IETF);
580 crom_add_simple_text(&src, &unit2, &text4, "IANA");
581 crom_add_entry(&unit2, CSRKEY_VER, 1);
582 crom_add_simple_text(&src, &unit2, &text5, "IPv4");
584 /* RFC3146 IPv6 over IEEE1394 */
585 crom_add_chunk(&src, &root, &unit3, CROM_UDIR);
586 crom_add_entry(&unit3, CSRKEY_SPEC, CSRVAL_IETF);
587 crom_add_simple_text(&src, &unit3, &text6, "IANA");
588 crom_add_entry(&unit3, CSRKEY_VER, 2);
589 crom_add_simple_text(&src, &unit3, &text7, "IPv6");
591 crom_load(&src, buf, 256);
593 #define DUMP_FORMAT "%08x %08x %08x %08x %08x %08x %08x %08x\n"
594 for (i = 0; i < 256/8; i ++) {
596 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);