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