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