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