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