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