2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 2002-2003
5 * Hidetoshi Shimokawa. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
18 * This product includes software developed by Hidetoshi Shimokawa.
20 * 4. Neither the name of the author nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
46 #include <bootstrap.h>
48 #if defined(_KERNEL) || defined(TEST)
49 #include <sys/queue.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
55 #include <netinet/in.h>
64 #include <dev/firewire/firewire.h>
65 #include <dev/firewire/iec13213.h>
67 #define MAX_ROM (1024 - sizeof(uint32_t) * 5)
68 #define CROM_END(cc) ((vm_offset_t)(cc)->stack[0].dir + MAX_ROM - 1)
71 crom_init_context(struct crom_context *cc, uint32_t *p)
75 hdr = (struct csrhdr *)p;
76 if (hdr->info_len <= 1) {
77 /* minimum or invalid ROM */
81 p += 1 + hdr->info_len;
83 /* check size of root directory */
84 if (((struct csrdirectory *)p)->crc_len == 0) {
89 cc->stack[0].dir = (struct csrdirectory *)p;
90 cc->stack[0].index = 0;
94 crom_get(struct crom_context *cc)
98 ptr = &cc->stack[cc->depth];
99 return (&ptr->dir->entry[ptr->index]);
103 crom_next(struct crom_context *cc)
105 struct crom_ptr *ptr;
111 if ((reg->key & CSRTYPE_MASK) == CSRTYPE_D) {
112 if (cc->depth >= CROM_MAX_DEPTH) {
113 printf("crom_next: too deep\n");
118 ptr = &cc->stack[cc->depth];
119 ptr->dir = (struct csrdirectory *) (reg + reg->val);
124 ptr = &cc->stack[cc->depth];
127 if (ptr->index < ptr->dir->crc_len &&
128 (vm_offset_t)crom_get(cc) <= CROM_END(cc))
131 if (ptr->index < ptr->dir->crc_len)
132 printf("crom_next: bound check failed\n");
144 crom_search_key(struct crom_context *cc, uint8_t key)
148 while (cc->depth >= 0) {
158 crom_has_specver(uint32_t *p, uint32_t spec, uint32_t ver)
161 struct crom_context c, *cc;
165 crom_init_context(cc, p);
166 while (cc->depth >= 0) {
169 if (reg->key == CSRKEY_SPEC && reg->val == spec)
174 if (reg->key == CSRKEY_VER && reg->val == ver)
185 crom_parse_text(struct crom_context *cc, char *buf, int len)
188 struct csrtext *textleaf;
191 static char *nullstr = "(null)";
197 if (reg->key != CROM_TEXTLEAF ||
198 (vm_offset_t)(reg + reg->val) > CROM_END(cc)) {
199 strncpy(buf, nullstr, len);
202 textleaf = (struct csrtext *)(reg + reg->val);
204 if ((vm_offset_t)textleaf + textleaf->crc_len > CROM_END(cc)) {
205 strncpy(buf, nullstr, len);
209 /* XXX should check spec and type */
211 bp = (uint32_t *)&buf[0];
212 qlen = textleaf->crc_len - 2;
215 for (i = 0; i < qlen; i++)
216 *bp++ = ntohl(textleaf->text[i]);
217 /* make sure to terminate the string */
225 crom_crc(uint32_t *ptr, int len)
228 uint32_t data, sum, crc = 0;
230 for (i = 0; i < len; i++) {
232 for (shift = 28; shift >= 0; shift -= 4) {
233 sum = ((crc >> 12) ^ (data >> shift)) & 0xf;
234 crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ sum;
238 return ((uint16_t) crc);
241 #if !defined(_KERNEL) && !defined(_BOOT)
243 crom_desc_specver(uint32_t spec, uint32_t ver, char *buf, int len)
247 if (spec == CSRVAL_ANSIT10 || spec == 0) {
254 s = "unknown ANSIT10";
257 if (spec == CSRVAL_1394TA || spec == 0) {
281 s = "1394 Direct print";
284 s = "Industrial & Instrument";
288 s = "unknown 1394TA";
292 snprintf(buf, len, "%s", s);
296 crom_desc(struct crom_context *cc, char *buf, int len)
299 struct csrdirectory *dir;
304 switch (reg->key & CSRTYPE_MASK) {
307 len -= snprintf(buf, len, "%d", reg->val);
314 len -= snprintf(buf, len, "offset=0x%04x(%d)",
319 /* XXX fall through */
321 dir = (struct csrdirectory *)(reg + reg->val);
322 crc = crom_crc((uint32_t *)&dir->entry[0], dir->crc_len);
323 len -= snprintf(buf, len, "len=%d crc=0x%04x(%s) ",
324 dir->crc_len, dir->crc,
325 (crc == dir->crc) ? "OK" : "NG");
330 desc = "module_vendor_ID";
333 desc = "hardware_version";
336 desc = "node_capabilities";
339 desc = "unit_spec_ID";
342 desc = "unit_sw_version";
343 crom_desc_specver(0, reg->val, buf, len);
346 desc = "logical_unit_number";
352 desc = "command_set_spec_ID";
355 desc = "command_set";
358 desc = "unit_characteristics";
361 desc = "command_set_revision";
364 desc = "firmware_revision";
367 desc = "reconnect_timeout";
370 desc = "management_agent";
374 crom_parse_text(cc, buf + strlen(buf), len);
377 desc = "unit_directory";
380 desc = "logical_unit_directory";
389 #if defined(_KERNEL) || defined(_BOOT) || defined(TEST)
392 crom_add_quad(struct crom_chunk *chunk, uint32_t entry)
396 index = chunk->data.crc_len;
397 if (index >= CROM_MAX_CHUNK_LEN - 1) {
398 printf("too large chunk %d\n", index);
401 chunk->data.buf[index] = entry;
402 chunk->data.crc_len++;
407 crom_add_entry(struct crom_chunk *chunk, int key, int val)
417 return (crom_add_quad(chunk, foo.i));
421 crom_add_chunk(struct crom_src *src, struct crom_chunk *parent,
422 struct crom_chunk *child, int key)
426 if (parent == NULL) {
427 STAILQ_INSERT_TAIL(&src->chunk_list, child, link);
431 index = crom_add_entry(parent, key, 0);
435 child->ref_chunk = parent;
436 child->ref_index = index;
437 STAILQ_INSERT_TAIL(&src->chunk_list, child, link);
441 #define MAX_TEXT ((CROM_MAX_CHUNK_LEN + 1) * 4 - sizeof(struct csrtext))
443 crom_add_simple_text(struct crom_src *src, struct crom_chunk *parent,
444 struct crom_chunk *chunk, char *buf)
452 if (len > MAX_TEXT) {
453 printf("text(%d) truncated to %td.\n", len, MAX_TEXT);
457 tl = (struct csrtext *) &chunk->data;
458 tl->crc_len = howmany(sizeof(struct csrtext) + len, sizeof(uint32_t));
462 bzero(&t[0], roundup2(len, sizeof(uint32_t)));
463 bcopy(buf, &t[0], len);
464 p = (uint32_t *)&t[0];
465 for (i = 0; i < howmany(len, sizeof(uint32_t)); i++)
466 tl->text[i] = ntohl(*p++);
467 return (crom_add_chunk(src, parent, chunk, CROM_TEXTLEAF));
471 crom_copy(uint32_t *src, uint32_t *dst, int *offset, int len, int maxlen)
473 if (*offset + len > maxlen) {
474 printf("Config. ROM is too large for the buffer\n");
477 bcopy(src, (char *)(dst + *offset), len * sizeof(uint32_t));
483 crom_load(struct crom_src *src, uint32_t *buf, int maxlen)
485 struct crom_chunk *chunk, *parent;
487 #if defined(_KERNEL) || defined(_BOOT)
495 /* Determine offset */
496 STAILQ_FOREACH(chunk, &src->chunk_list, link) {
497 chunk->offset = offset;
498 /* Assume the offset of the parent is already known */
499 parent = chunk->ref_chunk;
500 if (parent != NULL) {
502 reg = (struct csrreg *)
503 &parent->data.buf[chunk->ref_index];
505 (parent->offset + 1 + chunk->ref_index);
507 offset += 1 + chunk->data.crc_len;
510 /* Calculate CRC and dump to the buffer */
511 len = 1 + src->hdr.info_len;
513 if (crom_copy((uint32_t *)&src->hdr, buf, &count, len, maxlen) < 0)
515 STAILQ_FOREACH(chunk, &src->chunk_list, link) {
517 crom_crc(&chunk->data.buf[0], chunk->data.crc_len);
519 len = 1 + chunk->data.crc_len;
520 if (crom_copy((uint32_t *)&chunk->data, buf,
521 &count, len, maxlen) < 0)
524 hdr = (struct csrhdr *)buf;
525 hdr->crc_len = count - 1;
526 hdr->crc = crom_crc(&buf[1], hdr->crc_len);
528 #if defined(_KERNEL) || defined(_BOOT)
531 for (i = 0; i < count; i++) {
546 struct crom_chunk root,unit1,unit2,unit3;
547 struct crom_chunk text1,text2,text3,text4,text5,text6,text7;
548 uint32_t buf[256], *p;
551 bzero(&src, sizeof(src));
552 bzero(&root, sizeof(root));
553 bzero(&unit1, sizeof(unit1));
554 bzero(&unit2, sizeof(unit2));
555 bzero(&unit3, sizeof(unit3));
556 bzero(&text1, sizeof(text1));
557 bzero(&text2, sizeof(text2));
558 bzero(&text3, sizeof(text3));
559 bzero(&text3, sizeof(text4));
560 bzero(&text3, sizeof(text5));
561 bzero(&text3, sizeof(text6));
562 bzero(&text3, sizeof(text7));
563 bzero(buf, sizeof(buf));
565 /* BUS info sample */
566 src.hdr.info_len = 4;
567 src.businfo.bus_name = CSR_BUS_NAME_IEEE1394;
568 src.businfo.eui64.hi = 0x11223344;
569 src.businfo.eui64.lo = 0x55667788;
570 src.businfo.link_spd = FWSPD_S400;
571 src.businfo.generation = 0;
572 src.businfo.max_rom = MAXROM_4;
573 src.businfo.max_rec = 10;
574 src.businfo.cyc_clk_acc = 100;
579 src.businfo.irmc = 1;
580 STAILQ_INIT(&src.chunk_list);
583 crom_add_chunk(&src, NULL, &root, 0);
584 crom_add_entry(&root, CSRKEY_NCAP, 0x123456);
585 /* private company_id */
586 crom_add_entry(&root, CSRKEY_VENDOR, 0xacde48);
588 crom_add_simple_text(&src, &root, &text1, "FreeBSD");
589 crom_add_entry(&root, CSRKEY_HW, __FreeBSD_version);
590 crom_add_simple_text(&src, &root, &text2, "FreeBSD-5");
592 /* SBP unit directory */
593 crom_add_chunk(&src, &root, &unit1, CROM_UDIR);
594 crom_add_entry(&unit1, CSRKEY_SPEC, CSRVAL_ANSIT10);
595 crom_add_entry(&unit1, CSRKEY_VER, CSRVAL_T10SBP2);
596 crom_add_entry(&unit1, CSRKEY_COM_SPEC, CSRVAL_ANSIT10);
597 crom_add_entry(&unit1, CSRKEY_COM_SET, CSRVAL_SCSI);
598 /* management_agent */
599 crom_add_entry(&unit1, CROM_MGM, 0x1000);
600 crom_add_entry(&unit1, CSRKEY_UNIT_CH, (10<<8) | 8);
601 /* Device type and LUN */
602 crom_add_entry(&unit1, CROM_LUN, 0);
603 crom_add_entry(&unit1, CSRKEY_MODEL, 1);
604 crom_add_simple_text(&src, &unit1, &text3, "scsi_target");
606 /* RFC2734 IPv4 over IEEE1394 */
607 crom_add_chunk(&src, &root, &unit2, CROM_UDIR);
608 crom_add_entry(&unit2, CSRKEY_SPEC, CSRVAL_IETF);
609 crom_add_simple_text(&src, &unit2, &text4, "IANA");
610 crom_add_entry(&unit2, CSRKEY_VER, 1);
611 crom_add_simple_text(&src, &unit2, &text5, "IPv4");
613 /* RFC3146 IPv6 over IEEE1394 */
614 crom_add_chunk(&src, &root, &unit3, CROM_UDIR);
615 crom_add_entry(&unit3, CSRKEY_SPEC, CSRVAL_IETF);
616 crom_add_simple_text(&src, &unit3, &text6, "IANA");
617 crom_add_entry(&unit3, CSRKEY_VER, 2);
618 crom_add_simple_text(&src, &unit3, &text7, "IPv6");
620 crom_load(&src, buf, 256);
622 #define DUMP_FORMAT "%08x %08x %08x %08x %08x %08x %08x %08x\n"
623 for (i = 0; i < 256/8; i++) {
625 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);