1 /* $NetBSD: ofctl.c,v 1.11 2009/05/18 05:51:53 mrg Exp $ */ 2 3 /*- 4 * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Matt Thomas. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 34 #ifndef lint 35 __COPYRIGHT("@(#) Copyright (c) 2006, 2007\ 36 The NetBSD Foundation, Inc. All rights reserved."); 37 __RCSID("$NetBSD: ofctl.c,v 1.11 2009/05/18 05:51:53 mrg Exp $"); 38 #endif /* not lint */ 39 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <unistd.h> 43 #include <errno.h> 44 #include <ctype.h> 45 #include <string.h> 46 #include <assert.h> 47 #include <err.h> 48 #include <sys/types.h> 49 #include <sys/ioctl.h> 50 #include <sys/file.h> 51 #include <sys/queue.h> 52 #include <dev/ofw/openfirmio.h> 53 54 #include <prop/proplib.h> 55 56 static void oflist(int, const char *, int, void *, size_t); 57 static void ofprop(int); 58 static void ofgetprop(int, const char *); 59 #if 0 60 static int isstrprint(const char *, size_t, int); 61 #endif 62 63 static int lflag; 64 static int pflag; 65 66 struct of_node { 67 TAILQ_ENTRY(of_node) of_sibling; 68 TAILQ_HEAD(,of_node) of_children; 69 TAILQ_HEAD(,of_prop) of_properties; 70 struct of_node *of_parent; 71 struct of_prop *of_name; 72 struct of_prop *of_device_type; 73 struct of_prop *of_reg; 74 int of_nodeid; 75 }; 76 77 struct of_prop { 78 TAILQ_ENTRY(of_prop) prop_sibling; 79 char *prop_name; 80 u_int8_t *prop_data; 81 size_t prop_length; 82 size_t prop_namelen; 83 }; 84 85 struct of_node of_root; 86 unsigned long of_node_count; 87 unsigned long of_prop_count; 88 prop_dictionary_t of_proplib; 89 90 int OF_parent(int); 91 int OF_child(int); 92 int OF_peer(int); 93 int OF_finddevice(const char *); 94 int OF_getproplen(int, const char *); 95 int OF_getprop(int, const char *, void *, size_t); 96 int OF_nextprop(int, const char *, void *); 97 98 struct of_prop *of_tree_getprop(int, const char *); 99 100 static void 101 of_tree_mkprop(struct of_node *node, prop_dictionary_t propdict, 102 prop_dictionary_keysym_t key) 103 { 104 struct of_prop *prop; 105 prop_data_t obj; 106 const char *name; 107 108 name = prop_dictionary_keysym_cstring_nocopy(key); 109 obj = prop_dictionary_get_keysym(propdict, key); 110 111 prop = malloc(sizeof(*prop) + strlen(name) + 1); 112 if (prop == NULL) 113 err(1, "malloc(%zu)", sizeof(*prop) + strlen(name) + 1); 114 115 memset(prop, 0, sizeof(*prop)); 116 prop->prop_name = (char *) (prop + 1); 117 prop->prop_namelen = strlen(name); 118 memcpy(prop->prop_name, name, prop->prop_namelen+1); 119 TAILQ_INSERT_TAIL(&node->of_properties, prop, prop_sibling); 120 121 if (!strcmp(name, "name")) 122 node->of_name = prop; 123 else if (!strcmp(name, "device_type")) 124 node->of_device_type = prop; 125 else if (!strcmp(name, "reg")) 126 node->of_reg = prop; 127 128 of_prop_count++; 129 130 prop->prop_length = prop_data_size(obj); 131 if (prop->prop_length) 132 prop->prop_data = prop_data_data(obj); 133 } 134 135 static struct of_node * 136 of_tree_mknode(struct of_node *parent) 137 { 138 struct of_node *newnode; 139 newnode = malloc(sizeof(*newnode)); 140 if (newnode == NULL) 141 err(1, "malloc(%zu)", sizeof(*newnode)); 142 143 of_node_count++; 144 145 memset(newnode, 0, sizeof(*newnode)); 146 TAILQ_INIT(&newnode->of_children); 147 TAILQ_INIT(&newnode->of_properties); 148 newnode->of_parent = parent; 149 150 TAILQ_INSERT_TAIL(&parent->of_children, newnode, of_sibling); 151 152 return newnode; 153 } 154 155 static void 156 of_tree_fill(prop_dictionary_t dict, struct of_node *node) 157 { 158 prop_dictionary_t propdict; 159 prop_array_t propkeys; 160 prop_array_t children; 161 unsigned int i, count; 162 163 node->of_nodeid = prop_number_unsigned_integer_value( 164 prop_dictionary_get(dict, "node")); 165 166 propdict = prop_dictionary_get(dict, "properties"); 167 propkeys = prop_dictionary_all_keys(propdict); 168 count = prop_array_count(propkeys); 169 170 for (i = 0; i < count; i++) 171 of_tree_mkprop(node, propdict, prop_array_get(propkeys, i)); 172 173 children = prop_dictionary_get(dict, "children"); 174 if (children) { 175 count = prop_array_count(children); 176 177 for (i = 0; i < count; i++) { 178 of_tree_fill( 179 prop_array_get(children, i), 180 of_tree_mknode(node)); 181 } 182 } 183 } 184 185 static void 186 of_tree_init(prop_dictionary_t dict) 187 { 188 /* 189 * Initialize the root node of the OFW tree. 190 */ 191 TAILQ_INIT(&of_root.of_children); 192 TAILQ_INIT(&of_root.of_properties); 193 194 of_tree_fill(dict, &of_root); 195 } 196 197 static prop_object_t 198 of_proplib_mkprop(int fd, int nodeid, char *name) 199 { 200 struct ofiocdesc ofio; 201 prop_object_t obj; 202 203 ofio.of_nodeid = nodeid; 204 ofio.of_name = name; 205 ofio.of_namelen = strlen(name); 206 ofio.of_buf = NULL; 207 ofio.of_buflen = 32; 208 209 again: 210 if (ofio.of_buf != NULL) 211 free(ofio.of_buf); 212 ofio.of_buf = malloc(ofio.of_buflen); 213 if (ofio.of_buf == NULL) 214 err(1, "malloc(%d)", ofio.of_buflen); 215 if (ioctl(fd, OFIOCGET, &ofio) < 0) { 216 if (errno == ENOMEM) { 217 ofio.of_buflen *= 2; 218 goto again; 219 } 220 warn("OFIOCGET(%d, \"%s\")", fd, name); 221 free(ofio.of_buf); 222 return NULL; 223 } 224 obj = prop_data_create_data(ofio.of_buf, ofio.of_buflen); 225 free(ofio.of_buf); 226 return obj; 227 } 228 229 static prop_dictionary_t 230 of_proplib_tree_fill(int fd, int nodeid) 231 { 232 int childid = nodeid; 233 struct ofiocdesc ofio; 234 char namebuf[33]; 235 char newnamebuf[33]; 236 prop_array_t children; 237 prop_dictionary_t dict, propdict; 238 prop_object_t obj; 239 240 ofio.of_nodeid = nodeid; 241 ofio.of_name = namebuf; 242 ofio.of_namelen = 1; 243 ofio.of_buf = newnamebuf; 244 245 namebuf[0] = '\0'; 246 247 dict = prop_dictionary_create(); 248 prop_dictionary_set(dict, "node", 249 prop_number_create_unsigned_integer(nodeid)); 250 251 propdict = prop_dictionary_create(); 252 for (;;) { 253 ofio.of_buflen = sizeof(newnamebuf); 254 255 if (ioctl(fd, OFIOCNEXTPROP, &ofio) < 0) { 256 if (errno == ENOENT) 257 break; 258 err(1, "OFIOCNEXTPROP(%d, %#x, \"%s\")", fd, 259 ofio.of_nodeid, ofio.of_name); 260 } 261 262 ofio.of_namelen = ofio.of_buflen; 263 if (ofio.of_namelen == 0) 264 break; 265 newnamebuf[ofio.of_buflen] = '\0'; 266 strcpy(namebuf, newnamebuf); 267 obj = of_proplib_mkprop(fd, nodeid, namebuf); 268 if (obj) 269 prop_dictionary_set(propdict, namebuf, obj); 270 } 271 prop_dictionary_set(dict, "properties", propdict); 272 273 if (ioctl(fd, OFIOCGETCHILD, &childid) < 0) 274 err(1, "OFIOCGETCHILD(%d, %#x)", fd, childid); 275 276 children = NULL; 277 while (childid != 0) { 278 if (children == NULL) 279 children = prop_array_create(); 280 prop_array_add(children, of_proplib_tree_fill(fd, childid)); 281 if (ioctl(fd, OFIOCGETNEXT, &childid) < 0) 282 err(1, "OFIOCGETNEXT(%d, %#x)", fd, childid); 283 } 284 if (children != NULL) { 285 prop_array_make_immutable(children); 286 prop_dictionary_set(dict, "children", children); 287 } 288 289 return dict; 290 } 291 292 static prop_dictionary_t 293 of_proplib_init(const char *file) 294 { 295 prop_dictionary_t dict; 296 int rootid = 0; 297 int fd; 298 299 fd = open(file, O_RDONLY); 300 if (fd < 0) 301 err(1, "%s", file); 302 303 if (ioctl(fd, OFIOCGETNEXT, &rootid) < 0) 304 err(1, "OFIOCGETNEXT(%d, %#x)", fd, rootid); 305 306 dict = of_proplib_tree_fill(fd, rootid); 307 close(fd); 308 return dict; 309 } 310 311 static struct of_node * 312 of_tree_walk(struct of_node *node, 313 struct of_node *(*fn)(struct of_node *, const void *), 314 const void *ctx) 315 { 316 struct of_node *child, *match; 317 318 if ((match = (*fn)(node, ctx)) != NULL) 319 return match; 320 321 TAILQ_FOREACH(child, &node->of_children, of_sibling) { 322 if ((match = of_tree_walk(child, fn, ctx)) != NULL) 323 return match; 324 } 325 return NULL; 326 } 327 328 static struct of_node * 329 of_match_by_nodeid(struct of_node *node, const void *ctx) 330 { 331 return (node->of_nodeid == *(const int *) ctx) ? node : NULL; 332 } 333 334 static struct of_node * 335 of_match_by_parentid(struct of_node *node, const void *ctx) 336 { 337 if (node->of_parent == NULL) 338 return NULL; 339 return (node->of_parent->of_nodeid == *(const int *) ctx) ? node : NULL; 340 } 341 342 int 343 OF_parent(int childid) 344 { 345 struct of_node *child; 346 347 if (childid == 0) 348 return 0; 349 350 child = of_tree_walk(&of_root, of_match_by_nodeid, &childid); 351 if (child == NULL || child->of_parent == NULL) 352 return 0; 353 return child->of_parent->of_nodeid; 354 } 355 356 int 357 OF_child(int parentid) 358 { 359 struct of_node *child; 360 361 child = of_tree_walk(&of_root, of_match_by_parentid, &parentid); 362 if (child == NULL) 363 return 0; 364 return child->of_nodeid; 365 } 366 367 int 368 OF_peer(int peerid) 369 { 370 struct of_node *node, *match; 371 372 if (peerid == 0) 373 return of_root.of_nodeid; 374 375 node = of_tree_walk(&of_root, of_match_by_nodeid, &peerid); 376 if (node == NULL || node->of_parent == NULL) 377 return 0; 378 379 /* 380 * The peer should be our next sibling (if one exists). 381 */ 382 match = TAILQ_NEXT(node, of_sibling); 383 return (match != NULL) ? match->of_nodeid : 0; 384 } 385 386 int 387 OF_finddevice(const char *name) 388 { 389 #if 0 390 struct ofiocdesc ofio; 391 392 ofio.of_nodeid = 0; 393 ofio.of_name = argv[optind++]; 394 ofio.of_namelen = strlen(ofio.of_name); 395 ofio.of_buf = NULL; 396 ofio.of_buflen = 0; 397 if (ioctl(of_fd, OFIOCFINDDEVICE, &ofio) < 0) 398 err(1, "OFIOCFINDDEVICE(%d, \"%s\")", of_fd, ofio.of_name); 399 #endif 400 return 0; 401 } 402 403 struct of_prop * 404 of_tree_getprop(int nodeid, const char *name) 405 { 406 struct of_node *node; 407 struct of_prop *prop; 408 409 if (nodeid == 0) 410 return 0; 411 412 node = of_tree_walk(&of_root, of_match_by_nodeid, &nodeid); 413 if (node == NULL) 414 return NULL; 415 416 if (name[0] == '\0') 417 return TAILQ_FIRST(&node->of_properties); 418 419 if (!strcmp(name, "name")) 420 return node->of_name; 421 if (!strcmp(name, "device_type")) 422 return node->of_device_type; 423 if (!strcmp(name, "reg")) 424 return node->of_reg; 425 426 TAILQ_FOREACH(prop, &node->of_properties, prop_sibling) { 427 if (!strcmp(name, prop->prop_name)) 428 break; 429 } 430 return prop; 431 } 432 433 int 434 OF_getproplen(int nodeid, const char *name) 435 { 436 struct of_prop *prop = of_tree_getprop(nodeid, name); 437 return (prop != NULL) ? (int)prop->prop_length : -1; 438 } 439 440 int 441 OF_getprop(int nodeid, const char *name, void *buf, size_t len) 442 { 443 struct of_prop *prop = of_tree_getprop(nodeid, name); 444 if (prop == NULL) 445 return -1; 446 if (len > prop->prop_length) 447 len = prop->prop_length; 448 memcpy(buf, prop->prop_data, len); 449 return len; 450 } 451 452 int 453 OF_nextprop(int nodeid, const char *name, void *nextname) 454 { 455 struct of_prop *prop = of_tree_getprop(nodeid, name); 456 if (prop == NULL) 457 return -1; 458 if (name[0] != '\0') { 459 prop = TAILQ_NEXT(prop, prop_sibling); 460 if (prop == NULL) 461 return -1; 462 } 463 strcpy(nextname, prop->prop_name); 464 return strlen(prop->prop_name); 465 } 466 467 static u_int32_t 468 of_decode_int(const u_int8_t *p) 469 { 470 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; 471 } 472 473 /* 474 * Now we start the real program 475 */ 476 477 int 478 main(int argc, char **argv) 479 { 480 u_long of_buf[256]; 481 char device_type[33]; 482 int phandle; 483 int errflag = 0; 484 int c; 485 int len; 486 #if defined(__sparc__) || defined(__sparc64__) 487 const char *file = "/dev/openprom"; 488 #else 489 const char *file = "/dev/openfirm"; 490 #endif 491 const char *propfilein = NULL; 492 const char *propfileout = NULL; 493 494 while ((c = getopt(argc, argv, "f:lpr:w:")) != EOF) { 495 switch (c) { 496 case 'l': lflag++; break; 497 case 'p': pflag++; break; 498 case 'f': file = optarg; break; 499 case 'r': propfilein = optarg; break; 500 case 'w': propfileout = optarg; break; 501 default: errflag++; break; 502 } 503 } 504 if (errflag) 505 errx(1, "usage: ofctl [-pl] [-f file] [-r propfile] [-w propfile] [node...]"); 506 507 if (propfilein != NULL) { 508 of_proplib = prop_dictionary_internalize_from_file(propfilein); 509 } else { 510 of_proplib = of_proplib_init(file); 511 } 512 513 if (propfileout) 514 prop_dictionary_externalize_to_file(of_proplib, propfileout); 515 516 of_tree_init(of_proplib); 517 printf("[Caching %lu nodes and %lu properties]\n", 518 of_node_count, of_prop_count); 519 520 if (argc == optind) { 521 phandle = OF_peer(0); 522 device_type[0] = '\0'; 523 len = OF_getprop(phandle, "device_type", device_type, 524 sizeof(device_type)); 525 if (len <= 0) 526 len = OF_getprop(phandle, "name", device_type, 527 sizeof(device_type)); 528 if (len >= 0) 529 device_type[len] = '\0'; 530 oflist(phandle, device_type, 0, of_buf, sizeof(of_buf)); 531 } else { 532 #if 0 533 pandle = OF_finddevice(argv[optind++]); 534 535 if (argc == optind) { 536 if (lflag) 537 oflist(phandle, 0, of_buf, sizeof(of_buf)); 538 else 539 ofprop(phandle); 540 } else { 541 for (; optind < argc; optind++) { 542 ofgetprop(phandle, argv[optind]); 543 } 544 } 545 #else 546 printf("%s: OF_finddevice not yet implemented\n", argv[optind]); 547 #endif 548 } 549 exit(0); 550 } 551 552 static size_t 553 ofname(int node, char *buf, size_t buflen) 554 { 555 u_int8_t address_cells_buf[4]; 556 u_int8_t reg_buf[4096]; 557 char name[33]; 558 char device_type[33]; 559 size_t off = 0; 560 int parent = OF_parent(node); 561 int reglen; 562 int reg[sizeof(reg_buf)/sizeof(int)]; 563 int address_cells; 564 int len; 565 566 len = OF_getprop(node, "name", name, sizeof(name)); 567 if (len <= 0) 568 name[0] = '\0'; 569 off += snprintf(buf + off, buflen - off, "/%s", name); 570 571 reglen = OF_getprop(node, "reg", reg_buf, sizeof(reg_buf)); 572 if (reglen <= 0) 573 return off; 574 575 len = OF_getprop(parent, "device_type", 576 device_type, sizeof(device_type)); 577 if (len <= 0) 578 len = OF_getprop(parent, "name", 579 device_type, sizeof(device_type)); 580 device_type[len] = '\0'; 581 582 for (;;) { 583 len = OF_getprop(parent, "#address-cells", 584 address_cells_buf, sizeof(address_cells_buf)); 585 if (len >= 0) { 586 assert(len == 4); 587 break; 588 } 589 parent = OF_parent(parent); 590 if (parent == 0) 591 break; 592 } 593 594 if (parent == 0) { 595 596 parent = OF_parent(node); 597 598 for (;;) { 599 len = OF_getprop(parent, "#size-cells", 600 address_cells_buf, sizeof(address_cells_buf)); 601 if (len >= 0) { 602 assert(len == 4); 603 break; 604 } 605 parent = OF_parent(parent); 606 if (parent == 0) 607 break; 608 } 609 /* no #size-cells */ 610 len = 0; 611 } 612 613 if (len == 0) { 614 /* looks like we're on an OBP2 system */ 615 if (reglen > 12) 616 return off; 617 off += snprintf(buf + off, buflen - off, "@"); 618 memcpy(reg, reg_buf, 8); 619 off += snprintf(buf + off, buflen - off, "%x,%x", reg[0], 620 reg[1]); 621 return off; 622 } 623 624 off += snprintf(buf + off, buflen - off, "@"); 625 address_cells = of_decode_int(address_cells_buf); 626 for (len = 0; len < address_cells; len ++) 627 reg[len] = of_decode_int(®_buf[len * 4]); 628 629 if (!strcmp(device_type,"pci")) { 630 off += snprintf(buf + off, buflen - off, 631 "%x", (reg[0] >> 11) & 31); 632 if (reg[0] & 0x700) 633 off += snprintf(buf + off, buflen - off, 634 ",%x", (reg[0] >> 8) & 7); 635 } else if (!strcmp(device_type,"upa")) { 636 off += snprintf(buf + off, buflen - off, 637 "%x", (reg[0] >> 4) & 63); 638 for (len = 1; len < address_cells; len++) 639 off += snprintf(buf + off, buflen - off, 640 ",%x", reg[len]); 641 #if !defined(__sparc__) && !defined(__sparc64__) 642 } else if (!strcmp(device_type,"isa")) { 643 #endif 644 } else { 645 off += snprintf(buf + off, buflen - off, "%x", reg[0]); 646 for (len = 1; len < address_cells; len++) 647 off += snprintf(buf + off, buflen - off, 648 ",%x", reg[len]); 649 } 650 return off; 651 } 652 653 static size_t 654 offullname2(int node, char *buf, size_t len) 655 { 656 size_t off; 657 int parent = OF_parent(node); 658 if (parent == 0) 659 return 0; 660 661 off = offullname2(parent, buf, len); 662 off += ofname(node, buf + off, len - off); 663 return off; 664 } 665 666 static size_t 667 offullname(int node, char *buf, size_t len) 668 { 669 if (node == OF_peer(0)) { 670 size_t off = snprintf(buf, len, "/"); 671 off += OF_getprop(node, "name", buf + off, len - off); 672 return off; 673 } 674 return offullname2(node, buf, len); 675 } 676 677 static void 678 oflist(int node, const char *parent_device_type, int depth, 679 void *of_buf, size_t of_buflen) 680 { 681 int len; 682 while (node != 0) { 683 int child; 684 if (pflag == 0) { 685 len = ofname(node, of_buf, of_buflen-1); 686 printf("%08x: %*s%s", node, depth * 2, "", 687 (char *) of_buf); 688 } else { 689 len = offullname(node, of_buf, of_buflen-1); 690 printf("%08x: %s", node, (char *) of_buf); 691 } 692 putchar('\n'); 693 if (pflag) { 694 putchar('\n'); 695 ofprop(node); 696 puts("\n----------------------------------------" 697 "----------------------------------------\n\n"); 698 } 699 child = OF_child(node); 700 if (child != -1 && child != 0) { 701 char device_type[33]; 702 len = OF_getprop(node, "device_type", 703 device_type, sizeof(device_type)); 704 if (len <= 0) 705 len = OF_getprop(node, "name", 706 device_type, sizeof(device_type)); 707 if (len >= 0) 708 device_type[len] = '\0'; 709 depth++; 710 oflist(child, device_type, depth, of_buf, of_buflen); 711 depth--; 712 } 713 if (depth == 0) 714 break; 715 node = OF_peer(node); 716 } 717 } 718 719 static void 720 print_line(const u_int8_t *buf, size_t off, size_t len) 721 { 722 if (len - off > 16) 723 len = off + 16; 724 725 for (; off < ((len + 15) & ~15); off++) { 726 if (off > 0) { 727 if ((off & 15) == 0) 728 printf("%12s%04lx:%7s", "", 729 (unsigned long int) off, ""); 730 else if ((off & 3) == 0) 731 putchar(' '); 732 } 733 if (off < len) 734 printf("%02x", buf[off]); 735 #if 0 736 else if (off >= ((len + 3) & ~3)) 737 printf(" "); 738 #endif 739 else 740 printf(".."); 741 } 742 } 743 744 static void 745 default_format(int node, const u_int8_t *buf, size_t len) 746 { 747 size_t off = 0; 748 while (off < len) { 749 size_t end; 750 print_line(buf, off, len); 751 printf(" "); /* 24 + 32 + 3 = 59, so +3 makes 62 */ 752 end = len; 753 if (end > off + 16) 754 end = off + 16; 755 for (; off < end; off++) { 756 char ch = buf[off]; 757 if (isascii(ch) && 758 (isalnum((int)ch) || ispunct((int)ch) || ch == ' ')) 759 putchar(ch); 760 else 761 putchar('.'); 762 } 763 putchar('\n'); 764 } 765 } 766 767 static void 768 reg_format(int node, const u_int8_t *buf, size_t len) 769 { 770 /* parent = OF_parent(node); */ 771 default_format(node, buf, len); 772 } 773 774 static void 775 frequency_format(int node, const u_int8_t *buf, size_t len) 776 { 777 if (len == 4) { 778 u_int32_t freq = of_decode_int(buf); 779 u_int32_t divisor, whole, frac; 780 const char *units = ""; 781 print_line(buf, 0, len); 782 for (divisor = 1000000000; divisor > 1; divisor /= 1000) { 783 if (freq >= divisor) 784 break; 785 } 786 whole = freq / divisor; 787 if (divisor == 1) 788 frac = 0; 789 else 790 frac = (freq / (divisor / 1000)) % 1000; 791 792 switch (divisor) { 793 case 1000000000: units = "GHz"; break; 794 case 1000000: units = "MHz"; break; 795 case 1000: units = "KHz"; break; 796 case 1: units = "Hz"; break; 797 } 798 if (frac > 0) 799 printf(" %u.%03u%s\n", whole, frac, units); 800 else 801 printf(" %u%s\n", whole, units); 802 } else 803 default_format(node, buf, len); 804 } 805 806 static void 807 size_format(int node, const u_int8_t *buf, size_t len) 808 { 809 if (len == 4) { 810 u_int32_t freq = of_decode_int(buf); 811 u_int32_t divisor, whole, frac; 812 const char *units = ""; 813 print_line(buf, 0, len); 814 for (divisor = 0x40000000; divisor > 1; divisor >>= 10) { 815 if (freq >= divisor) 816 break; 817 } 818 whole = freq / divisor; 819 if (divisor == 1) 820 frac = 0; 821 else 822 frac = (freq / (divisor >> 10)) & 1023; 823 824 switch (divisor) { 825 case 0x40000000: units = "G"; break; 826 case 0x100000: units = "M"; break; 827 case 0x400: units = "K"; break; 828 case 1: units = ""; break; 829 } 830 if (frac > 0) 831 printf(" %3u.%03u%s\n", whole, frac, units); 832 else 833 printf(" %3u%s\n", whole, units); 834 } else 835 default_format(node, buf, len); 836 } 837 838 static void 839 string_format(int node, const u_int8_t *buf, size_t len) 840 { 841 size_t off = 0; 842 int first_line = 1; 843 while (off < len) { 844 size_t string_len = 0; 845 int leading = 1; 846 for (; off + string_len < len; string_len++) { 847 if (buf[off+string_len] == '\0') { 848 string_len++; 849 break; 850 } 851 } 852 while (string_len > 0) { 853 size_t line_len = string_len; 854 if (line_len > 16) 855 line_len = 16; 856 if (!first_line) 857 printf("%12s%04lx:%7s", "", 858 (unsigned long int) off, ""); 859 print_line(buf + off, 0, line_len); 860 printf(" "); 861 if (leading) 862 putchar('"'); 863 first_line = 0; 864 leading = 0; 865 string_len -= line_len; 866 for (; line_len > 0; line_len--, off++) { 867 if (buf[off] != '\0') 868 putchar(buf[off]); 869 } 870 if (string_len == 0) 871 putchar('"'); 872 putchar('\n'); 873 } 874 } 875 } 876 877 static const struct { 878 const char *prop_name; 879 void (*prop_format)(int, const u_int8_t *, size_t); 880 } formatters[] = { 881 { "reg", reg_format }, 882 #if 0 883 { "assigned-addresses", assigned_addresses_format }, 884 { "ranges", ranges_format }, 885 { "interrupt-map", interrup_map_format }, 886 { "interrupt", interrupt_format }, 887 #endif 888 { "model", string_format }, 889 { "name", string_format }, 890 { "device_type", string_format }, 891 { "compatible", string_format }, 892 { "*frequency", frequency_format }, 893 { "*-size", size_format }, 894 { "*-cells", size_format }, 895 { "*-entries", size_format }, 896 { "*-associativity", size_format }, 897 { NULL, default_format } 898 }; 899 900 static void 901 ofgetprop(int node, const char *name) 902 { 903 u_int8_t of_buf[4097]; 904 int len; 905 int i; 906 907 len = OF_getprop(node, name, of_buf, sizeof(of_buf) - 1); 908 if (len < 0) 909 return; 910 of_buf[len] = '\0'; 911 printf("%-24s", name); 912 if (len == 0) { 913 putchar('\n'); 914 return; 915 } 916 if (strlen(name) >= 24) 917 printf("\n%24s", ""); 918 919 for (i = 0; formatters[i].prop_name != NULL; i++) { 920 if (formatters[i].prop_name[0] == '*') { 921 if (strstr(name, &formatters[i].prop_name[1]) != NULL) { 922 (*formatters[i].prop_format)(node, of_buf, len); 923 return; 924 } 925 continue; 926 } 927 if (strcmp(name, formatters[i].prop_name) == 0) { 928 (*formatters[i].prop_format)(node, of_buf, len); 929 return; 930 } 931 } 932 (*formatters[i].prop_format)(node, of_buf, len); 933 } 934 935 static void 936 ofprop(int node) 937 { 938 char namebuf[33]; 939 char newnamebuf[33]; 940 int len; 941 942 namebuf[0] = '\0'; 943 944 for (;;) { 945 len = OF_nextprop(node, namebuf, newnamebuf); 946 if (len <= 0) 947 break; 948 949 newnamebuf[len] = '\0'; 950 strcpy(namebuf, newnamebuf); 951 ofgetprop(node, newnamebuf); 952 } 953 } 954 #if 0 955 static int 956 isstrprint(const char *str, size_t len, int ignorenulls) 957 { 958 if (*str == '\0') 959 return 0; 960 for (; len-- > 0; str++) { 961 if (*str == '\0' && len > 0 && str[1] == '\0') 962 return 0; 963 if (len == 0 && *str == '\0') 964 return 1; 965 if (ignorenulls) { 966 if (*str == '\0') 967 continue; 968 if (isalnum(*str) || ispunct(*str) || *str == ' ') 969 continue; 970 return 0; 971 } 972 if (!isprint(*str)) 973 return 0; 974 } 975 return 1; 976 } 977 #endif 978