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