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