Home | History | Annotate | Line # | Download | only in ofctl
ofctl.c revision 1.10
      1 /*	$NetBSD: ofctl.c,v 1.10 2009/04/26 04:54:27 lukem 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.10 2009/04/26 04:54:27 lukem 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 	assert(len > 0);
    568 	off += snprintf(buf + off, buflen - off, "/%s", name);
    569 
    570 	reglen = OF_getprop(node, "reg", reg_buf, sizeof(reg_buf));
    571 	if (reglen <= 0)
    572 		return off;
    573 
    574 	len = OF_getprop(parent, "device_type",
    575 	    device_type, sizeof(device_type));
    576 	if (len <= 0)
    577 		len = OF_getprop(parent, "name",
    578 		    device_type, sizeof(device_type));
    579 	device_type[len] = '\0';
    580 
    581 	for (;;) {
    582 		len = OF_getprop(parent, "#address-cells",
    583 		    address_cells_buf, sizeof(address_cells_buf));
    584 		if (len >= 0) {
    585 			assert(len == 4);
    586 			break;
    587 		}
    588 		parent = OF_parent(parent);
    589 		if (parent == 0)
    590 			break;
    591 	}
    592 
    593 	if (parent == 0) {
    594 
    595 		parent = OF_parent(node);
    596 
    597 		for (;;) {
    598 			len = OF_getprop(parent, "#size-cells",
    599 			    address_cells_buf, sizeof(address_cells_buf));
    600 			if (len >= 0) {
    601 				assert(len == 4);
    602 				break;
    603 			}
    604 			parent = OF_parent(parent);
    605 			if (parent == 0)
    606 				break;
    607 		}
    608 		/* no #size-cells */
    609 		len = 0;
    610 	}
    611 
    612 	if (len == 0) {
    613 		/* looks like we're on an OBP2 system */
    614 		if (reglen > 12)
    615 			return off;
    616 		off += snprintf(buf + off, buflen - off, "@");
    617 		memcpy(reg, reg_buf, 8);
    618 		off += snprintf(buf + off, buflen - off, "%x,%x", reg[0],
    619 		    reg[1]);
    620 		return off;
    621 	}
    622 
    623 	off += snprintf(buf + off, buflen - off, "@");
    624 	address_cells = of_decode_int(address_cells_buf);
    625 	for (len = 0; len < address_cells; len ++)
    626 		reg[len] = of_decode_int(&reg_buf[len * 4]);
    627 
    628 	if (!strcmp(device_type,"pci")) {
    629 		off += snprintf(buf + off, buflen - off,
    630 		    "%x", (reg[0] >> 11) & 31);
    631 		if (reg[0] & 0x700)
    632 			off += snprintf(buf + off, buflen - off,
    633 			    ",%x", (reg[0] >> 8) & 7);
    634 	} else if (!strcmp(device_type,"upa")) {
    635 		off += snprintf(buf + off, buflen - off,
    636 		    "%x", (reg[0] >> 4) & 63);
    637 		for (len = 1; len < address_cells; len++)
    638 			off += snprintf(buf + off, buflen - off,
    639 			    ",%x", reg[len]);
    640 #if !defined(__sparc__) && !defined(__sparc64__)
    641 	} else if (!strcmp(device_type,"isa")) {
    642 #endif
    643 	} else {
    644 		off += snprintf(buf + off, buflen - off, "%x", reg[0]);
    645 		for (len = 1; len < address_cells; len++)
    646 			off += snprintf(buf + off, buflen - off,
    647 			    ",%x", reg[len]);
    648 	}
    649 	return off;
    650 }
    651 
    652 static size_t
    653 offullname2(int node, char *buf, size_t len)
    654 {
    655 	size_t off;
    656 	int parent = OF_parent(node);
    657 	if (parent == 0)
    658 		return 0;
    659 
    660 	off = offullname2(parent, buf, len);
    661 	off += ofname(node, buf + off, len - off);
    662 	return off;
    663 }
    664 
    665 static size_t
    666 offullname(int node, char *buf, size_t len)
    667 {
    668 	if (node == OF_peer(0)) {
    669 		size_t off = snprintf(buf, len, "/");
    670 		off += OF_getprop(node, "name", buf + off, len - off);
    671 		return off;
    672 	}
    673 	return offullname2(node, buf, len);
    674 }
    675 
    676 static void
    677 oflist(int node, const char *parent_device_type, int depth,
    678 	void *of_buf, size_t of_buflen)
    679 {
    680 	int len;
    681 	while (node != 0) {
    682 		int child;
    683 		if (pflag == 0) {
    684 			len = ofname(node, of_buf, of_buflen-1);
    685 			printf("%08x: %*s%s", node, depth * 2, "",
    686 			    (char *) of_buf);
    687 		} else {
    688 			len = offullname(node, of_buf, of_buflen-1);
    689 			printf("%08x: %s", node, (char *) of_buf);
    690 		}
    691 		putchar('\n');
    692 		if (pflag) {
    693 			putchar('\n');
    694 			ofprop(node);
    695 			puts("\n----------------------------------------"
    696 			    "----------------------------------------\n\n");
    697 		}
    698 		child = OF_child(node);
    699 		if (child != -1 && child != 0) {
    700 			char device_type[33];
    701 			len = OF_getprop(node, "device_type",
    702 			    device_type, sizeof(device_type));
    703 			if (len <= 0)
    704 				len = OF_getprop(node, "name",
    705 				    device_type, sizeof(device_type));
    706 			if (len >= 0)
    707 				device_type[len] = '\0';
    708 			depth++;
    709 			oflist(child, device_type, depth, of_buf, of_buflen);
    710 			depth--;
    711 		}
    712 		if (depth == 0)
    713 			break;
    714 		node = OF_peer(node);
    715 	}
    716 }
    717 
    718 static void
    719 print_line(const u_int8_t *buf, size_t off, size_t len)
    720 {
    721 	if (len - off > 16)
    722 		len = off + 16;
    723 
    724 	for (; off < ((len + 15) & ~15); off++) {
    725 		if (off > 0) {
    726 			if ((off & 15) == 0)
    727 				printf("%12s%04lx:%7s", "",
    728 				    (unsigned long int) off, "");
    729 			else if ((off & 3) == 0)
    730 				putchar(' ');
    731 		}
    732 		if (off < len)
    733 			printf("%02x", buf[off]);
    734 #if 0
    735 		else if (off >= ((len + 3) & ~3))
    736 			printf("  ");
    737 #endif
    738 		else
    739 			printf("..");
    740 	}
    741 }
    742 
    743 static void
    744 default_format(int node, const u_int8_t *buf, size_t len)
    745 {
    746 	size_t off = 0;
    747 	while (off < len) {
    748 		size_t end;
    749 		print_line(buf, off, len);
    750 		printf("   ");	/* 24 + 32 + 3 = 59, so +3 makes 62 */
    751 		end = len;
    752 		if (end > off + 16)
    753 			end = off + 16;
    754 		for (; off < end; off++) {
    755 			char ch = buf[off];
    756 			if (isascii(ch) &&
    757 			    (isalnum((int)ch) || ispunct((int)ch) || ch == ' '))
    758 				putchar(ch);
    759 			else
    760 				putchar('.');
    761 		}
    762 		putchar('\n');
    763 	}
    764 }
    765 
    766 static void
    767 reg_format(int node, const u_int8_t *buf, size_t len)
    768 {
    769 	/* parent = OF_parent(node); */
    770 	default_format(node, buf, len);
    771 }
    772 
    773 static void
    774 frequency_format(int node, const u_int8_t *buf, size_t len)
    775 {
    776 	if (len == 4) {
    777 		u_int32_t freq = of_decode_int(buf);
    778 		u_int32_t divisor, whole, frac;
    779 		const char *units = "";
    780 		print_line(buf, 0, len);
    781 		for (divisor = 1000000000; divisor > 1; divisor /= 1000) {
    782 			if (freq >= divisor)
    783 				break;
    784 		}
    785 		whole = freq / divisor;
    786 		if (divisor == 1)
    787 			frac = 0;
    788 		else
    789 			frac = (freq / (divisor / 1000)) % 1000;
    790 
    791 		switch (divisor) {
    792 		case 1000000000: units = "GHz"; break;
    793 		case    1000000: units = "MHz"; break;
    794 		case       1000: units = "KHz"; break;
    795 		case          1: units =  "Hz"; break;
    796 		}
    797 		if (frac > 0)
    798 			printf("   %u.%03u%s\n", whole, frac, units);
    799 		else
    800 			printf("   %u%s\n", whole, units);
    801 	} else
    802 		default_format(node, buf, len);
    803 }
    804 
    805 static void
    806 size_format(int node, const u_int8_t *buf, size_t len)
    807 {
    808 	if (len == 4) {
    809 		u_int32_t freq = of_decode_int(buf);
    810 		u_int32_t divisor, whole, frac;
    811 		const char *units = "";
    812 		print_line(buf, 0, len);
    813 		for (divisor = 0x40000000; divisor > 1; divisor >>= 10) {
    814 			if (freq >= divisor)
    815 				break;
    816 		}
    817 		whole = freq / divisor;
    818 		if (divisor == 1)
    819 			frac = 0;
    820 		else
    821 			frac = (freq / (divisor >> 10)) & 1023;
    822 
    823 		switch (divisor) {
    824 		case 0x40000000: units = "G"; break;
    825 		case   0x100000: units = "M"; break;
    826 		case      0x400: units = "K"; break;
    827 		case          1: units =  ""; break;
    828 		}
    829 		if (frac > 0)
    830 			printf("   %3u.%03u%s\n", whole, frac, units);
    831 		else
    832 			printf("   %3u%s\n", whole, units);
    833 	} else
    834 		default_format(node, buf, len);
    835 }
    836 
    837 static void
    838 string_format(int node, const u_int8_t *buf, size_t len)
    839 {
    840 	size_t off = 0;
    841 	int first_line = 1;
    842 	while (off < len) {
    843 		size_t string_len = 0;
    844 		int leading = 1;
    845 		for (; off + string_len < len; string_len++) {
    846 			if (buf[off+string_len] == '\0') {
    847 				string_len++;
    848 				break;
    849 			}
    850 		}
    851 		while (string_len > 0) {
    852 			size_t line_len = string_len;
    853 			if (line_len > 16)
    854 				line_len = 16;
    855 			if (!first_line)
    856 				printf("%12s%04lx:%7s", "",
    857 				    (unsigned long int) off, "");
    858 			print_line(buf + off, 0, line_len);
    859 			printf("   ");
    860 			if (leading)
    861 				putchar('"');
    862 			first_line = 0;
    863 			leading = 0;
    864 			string_len -= line_len;
    865 			for (; line_len > 0; line_len--, off++) {
    866 				if (buf[off] != '\0')
    867 					putchar(buf[off]);
    868 			}
    869 			if (string_len == 0)
    870 				putchar('"');
    871 			putchar('\n');
    872 		}
    873 	}
    874 }
    875 
    876 static const struct {
    877 	const char *prop_name;
    878 	void (*prop_format)(int, const u_int8_t *, size_t);
    879 } formatters[] = {
    880 	{ "reg", reg_format },
    881 #if 0
    882 	{ "assigned-addresses", assigned_addresses_format },
    883 	{ "ranges", ranges_format },
    884 	{ "interrupt-map", interrup_map_format },
    885 	{ "interrupt", interrupt_format },
    886 #endif
    887 	{ "model", string_format },
    888 	{ "name", string_format },
    889 	{ "device_type", string_format },
    890 	{ "compatible", string_format },
    891 	{ "*frequency", frequency_format },
    892 	{ "*-size", size_format },
    893 	{ "*-cells", size_format },
    894 	{ "*-entries", size_format },
    895 	{ "*-associativity", size_format },
    896 	{ NULL, default_format }
    897 };
    898 
    899 static void
    900 ofgetprop(int node, const char *name)
    901 {
    902 	u_int8_t of_buf[4097];
    903 	int len;
    904 	int i;
    905 
    906 	len = OF_getprop(node, name, of_buf, sizeof(of_buf) - 1);
    907 	if (len < 0)
    908 		return;
    909 	of_buf[len] = '\0';
    910 	printf("%-24s", name);
    911 	if (len == 0) {
    912 		putchar('\n');
    913 		return;
    914 	}
    915 	if (strlen(name) >= 24)
    916 		printf("\n%24s", "");
    917 
    918 	for (i = 0; formatters[i].prop_name != NULL; i++) {
    919 		if (formatters[i].prop_name[0] == '*') {
    920 			if (strstr(name, &formatters[i].prop_name[1]) != NULL) {
    921 				(*formatters[i].prop_format)(node, of_buf, len);
    922 				return;
    923 			}
    924 			continue;
    925 		}
    926 		if (strcmp(name, formatters[i].prop_name) == 0) {
    927 			(*formatters[i].prop_format)(node, of_buf, len);
    928 			return;
    929 		}
    930 	}
    931 	(*formatters[i].prop_format)(node, of_buf, len);
    932 }
    933 
    934 static void
    935 ofprop(int node)
    936 {
    937 	char namebuf[33];
    938 	char newnamebuf[33];
    939 	int len;
    940 
    941 	namebuf[0] = '\0';
    942 
    943 	for (;;) {
    944 		len = OF_nextprop(node, namebuf, newnamebuf);
    945 		if (len <= 0)
    946 			break;
    947 
    948 		newnamebuf[len] = '\0';
    949 		strcpy(namebuf, newnamebuf);
    950 		ofgetprop(node, newnamebuf);
    951 	}
    952 }
    953 #if 0
    954 static int
    955 isstrprint(const char *str, size_t len, int ignorenulls)
    956 {
    957 	if (*str == '\0')
    958 		return 0;
    959 	for (; len-- > 0; str++) {
    960 		if (*str == '\0' && len > 0 && str[1] == '\0')
    961 			return 0;
    962 		if (len == 0 && *str == '\0')
    963 			return 1;
    964 		if (ignorenulls) {
    965 			if (*str == '\0')
    966 				continue;
    967 			if (isalnum(*str) || ispunct(*str) || *str == ' ')
    968 				continue;
    969 			return 0;
    970 		}
    971 		if (!isprint(*str))
    972 			return 0;
    973 	}
    974 	return 1;
    975 }
    976 #endif
    977