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