1 /* $NetBSD: ofctl.c,v 1.10 2009/04/26 04:54:27 lukem Exp $ */
4 * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/cdefs.h>
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 $");
48 #include <sys/types.h>
49 #include <sys/ioctl.h>
51 #include <sys/queue.h>
52 #include <dev/ofw/openfirmio.h>
54 #include <prop/proplib.h>
56 static void oflist(int, const char *, int, void *, size_t);
57 static void ofprop(int);
58 static void ofgetprop(int, const char *);
60 static int isstrprint(const char *, size_t, int);
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
;
78 TAILQ_ENTRY(of_prop
) prop_sibling
;
85 struct of_node of_root
;
86 unsigned long of_node_count
;
87 unsigned long of_prop_count
;
88 prop_dictionary_t of_proplib
;
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 *);
98 struct of_prop
*of_tree_getprop(int, const char *);
101 of_tree_mkprop(struct of_node
*node
, prop_dictionary_t propdict
,
102 prop_dictionary_keysym_t key
)
104 struct of_prop
*prop
;
108 name
= prop_dictionary_keysym_cstring_nocopy(key
);
109 obj
= prop_dictionary_get_keysym(propdict
, key
);
111 prop
= malloc(sizeof(*prop
) + strlen(name
) + 1);
113 err(1, "malloc(%zu)", sizeof(*prop
) + strlen(name
) + 1);
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
);
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"))
130 prop
->prop_length
= prop_data_size(obj
);
131 if (prop
->prop_length
)
132 prop
->prop_data
= prop_data_data(obj
);
135 static struct of_node
*
136 of_tree_mknode(struct of_node
*parent
)
138 struct of_node
*newnode
;
139 newnode
= malloc(sizeof(*newnode
));
141 err(1, "malloc(%zu)", sizeof(*newnode
));
145 memset(newnode
, 0, sizeof(*newnode
));
146 TAILQ_INIT(&newnode
->of_children
);
147 TAILQ_INIT(&newnode
->of_properties
);
148 newnode
->of_parent
= parent
;
150 TAILQ_INSERT_TAIL(&parent
->of_children
, newnode
, of_sibling
);
156 of_tree_fill(prop_dictionary_t dict
, struct of_node
*node
)
158 prop_dictionary_t propdict
;
159 prop_array_t propkeys
;
160 prop_array_t children
;
161 unsigned int i
, count
;
163 node
->of_nodeid
= prop_number_unsigned_integer_value(
164 prop_dictionary_get(dict
, "node"));
166 propdict
= prop_dictionary_get(dict
, "properties");
167 propkeys
= prop_dictionary_all_keys(propdict
);
168 count
= prop_array_count(propkeys
);
170 for (i
= 0; i
< count
; i
++)
171 of_tree_mkprop(node
, propdict
, prop_array_get(propkeys
, i
));
173 children
= prop_dictionary_get(dict
, "children");
175 count
= prop_array_count(children
);
177 for (i
= 0; i
< count
; i
++) {
179 prop_array_get(children
, i
),
180 of_tree_mknode(node
));
186 of_tree_init(prop_dictionary_t dict
)
189 * Initialize the root node of the OFW tree.
191 TAILQ_INIT(&of_root
.of_children
);
192 TAILQ_INIT(&of_root
.of_properties
);
194 of_tree_fill(dict
, &of_root
);
198 of_proplib_mkprop(int fd
, int nodeid
, char *name
)
200 struct ofiocdesc ofio
;
203 ofio
.of_nodeid
= nodeid
;
205 ofio
.of_namelen
= strlen(name
);
210 if (ofio
.of_buf
!= NULL
)
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
) {
220 warn("OFIOCGET(%d, \"%s\")", fd
, name
);
224 obj
= prop_data_create_data(ofio
.of_buf
, ofio
.of_buflen
);
229 static prop_dictionary_t
230 of_proplib_tree_fill(int fd
, int nodeid
)
232 int childid
= nodeid
;
233 struct ofiocdesc ofio
;
236 prop_array_t children
;
237 prop_dictionary_t dict
, propdict
;
240 ofio
.of_nodeid
= nodeid
;
241 ofio
.of_name
= namebuf
;
243 ofio
.of_buf
= newnamebuf
;
247 dict
= prop_dictionary_create();
248 prop_dictionary_set(dict
, "node",
249 prop_number_create_unsigned_integer(nodeid
));
251 propdict
= prop_dictionary_create();
253 ofio
.of_buflen
= sizeof(newnamebuf
);
255 if (ioctl(fd
, OFIOCNEXTPROP
, &ofio
) < 0) {
258 err(1, "OFIOCNEXTPROP(%d, %#x, \"%s\")", fd
,
259 ofio
.of_nodeid
, ofio
.of_name
);
262 ofio
.of_namelen
= ofio
.of_buflen
;
263 if (ofio
.of_namelen
== 0)
265 newnamebuf
[ofio
.of_buflen
] = '\0';
266 strcpy(namebuf
, newnamebuf
);
267 obj
= of_proplib_mkprop(fd
, nodeid
, namebuf
);
269 prop_dictionary_set(propdict
, namebuf
, obj
);
271 prop_dictionary_set(dict
, "properties", propdict
);
273 if (ioctl(fd
, OFIOCGETCHILD
, &childid
) < 0)
274 err(1, "OFIOCGETCHILD(%d, %#x)", fd
, childid
);
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
);
284 if (children
!= NULL
) {
285 prop_array_make_immutable(children
);
286 prop_dictionary_set(dict
, "children", children
);
292 static prop_dictionary_t
293 of_proplib_init(const char *file
)
295 prop_dictionary_t dict
;
299 fd
= open(file
, O_RDONLY
);
303 if (ioctl(fd
, OFIOCGETNEXT
, &rootid
) < 0)
304 err(1, "OFIOCGETNEXT(%d, %#x)", fd
, rootid
);
306 dict
= of_proplib_tree_fill(fd
, rootid
);
311 static struct of_node
*
312 of_tree_walk(struct of_node
*node
,
313 struct of_node
*(*fn
)(struct of_node
*, const void *),
316 struct of_node
*child
, *match
;
318 if ((match
= (*fn
)(node
, ctx
)) != NULL
)
321 TAILQ_FOREACH(child
, &node
->of_children
, of_sibling
) {
322 if ((match
= of_tree_walk(child
, fn
, ctx
)) != NULL
)
328 static struct of_node
*
329 of_match_by_nodeid(struct of_node
*node
, const void *ctx
)
331 return (node
->of_nodeid
== *(const int *) ctx
) ? node
: NULL
;
334 static struct of_node
*
335 of_match_by_parentid(struct of_node
*node
, const void *ctx
)
337 if (node
->of_parent
== NULL
)
339 return (node
->of_parent
->of_nodeid
== *(const int *) ctx
) ? node
: NULL
;
343 OF_parent(int childid
)
345 struct of_node
*child
;
350 child
= of_tree_walk(&of_root
, of_match_by_nodeid
, &childid
);
351 if (child
== NULL
|| child
->of_parent
== NULL
)
353 return child
->of_parent
->of_nodeid
;
357 OF_child(int parentid
)
359 struct of_node
*child
;
361 child
= of_tree_walk(&of_root
, of_match_by_parentid
, &parentid
);
364 return child
->of_nodeid
;
370 struct of_node
*node
, *match
;
373 return of_root
.of_nodeid
;
375 node
= of_tree_walk(&of_root
, of_match_by_nodeid
, &peerid
);
376 if (node
== NULL
|| node
->of_parent
== NULL
)
380 * The peer should be our next sibling (if one exists).
382 match
= TAILQ_NEXT(node
, of_sibling
);
383 return (match
!= NULL
) ? match
->of_nodeid
: 0;
387 OF_finddevice(const char *name
)
390 struct ofiocdesc ofio
;
393 ofio
.of_name
= argv
[optind
++];
394 ofio
.of_namelen
= strlen(ofio
.of_name
);
397 if (ioctl(of_fd
, OFIOCFINDDEVICE
, &ofio
) < 0)
398 err(1, "OFIOCFINDDEVICE(%d, \"%s\")", of_fd
, ofio
.of_name
);
404 of_tree_getprop(int nodeid
, const char *name
)
406 struct of_node
*node
;
407 struct of_prop
*prop
;
412 node
= of_tree_walk(&of_root
, of_match_by_nodeid
, &nodeid
);
417 return TAILQ_FIRST(&node
->of_properties
);
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"))
426 TAILQ_FOREACH(prop
, &node
->of_properties
, prop_sibling
) {
427 if (!strcmp(name
, prop
->prop_name
))
434 OF_getproplen(int nodeid
, const char *name
)
436 struct of_prop
*prop
= of_tree_getprop(nodeid
, name
);
437 return (prop
!= NULL
) ? (int)prop
->prop_length
: -1;
441 OF_getprop(int nodeid
, const char *name
, void *buf
, size_t len
)
443 struct of_prop
*prop
= of_tree_getprop(nodeid
, name
);
446 if (len
> prop
->prop_length
)
447 len
= prop
->prop_length
;
448 memcpy(buf
, prop
->prop_data
, len
);
453 OF_nextprop(int nodeid
, const char *name
, void *nextname
)
455 struct of_prop
*prop
= of_tree_getprop(nodeid
, name
);
458 if (name
[0] != '\0') {
459 prop
= TAILQ_NEXT(prop
, prop_sibling
);
463 strcpy(nextname
, prop
->prop_name
);
464 return strlen(prop
->prop_name
);
468 of_decode_int(const u_int8_t
*p
)
470 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
474 * Now we start the real program
478 main(int argc
, char **argv
)
481 char device_type
[33];
486 #if defined(__sparc__) || defined(__sparc64__)
487 const char *file
= "/dev/openprom";
489 const char *file
= "/dev/openfirm";
491 const char *propfilein
= NULL
;
492 const char *propfileout
= NULL
;
494 while ((c
= getopt(argc
, argv
, "f:lpr:w:")) != EOF
) {
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;
505 errx(1, "usage: ofctl [-pl] [-f file] [-r propfile] [-w propfile] [node...]");
507 if (propfilein
!= NULL
) {
508 of_proplib
= prop_dictionary_internalize_from_file(propfilein
);
510 of_proplib
= of_proplib_init(file
);
514 prop_dictionary_externalize_to_file(of_proplib
, propfileout
);
516 of_tree_init(of_proplib
);
517 printf("[Caching %lu nodes and %lu properties]\n",
518 of_node_count
, of_prop_count
);
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
));
526 len
= OF_getprop(phandle
, "name", device_type
,
527 sizeof(device_type
));
529 device_type
[len
] = '\0';
530 oflist(phandle
, device_type
, 0, of_buf
, sizeof(of_buf
));
533 pandle
= OF_finddevice(argv
[optind
++]);
535 if (argc
== optind
) {
537 oflist(phandle
, 0, of_buf
, sizeof(of_buf
));
541 for (; optind
< argc
; optind
++) {
542 ofgetprop(phandle
, argv
[optind
]);
546 printf("%s: OF_finddevice not yet implemented\n", argv
[optind
]);
553 ofname(int node
, char *buf
, size_t buflen
)
555 u_int8_t address_cells_buf
[4];
556 u_int8_t reg_buf
[4096];
558 char device_type
[33];
560 int parent
= OF_parent(node
);
562 int reg
[sizeof(reg_buf
)/sizeof(int)];
566 len
= OF_getprop(node
, "name", name
, sizeof(name
));
569 off
+= snprintf(buf
+ off
, buflen
- off
, "/%s", name
);
571 reglen
= OF_getprop(node
, "reg", reg_buf
, sizeof(reg_buf
));
575 len
= OF_getprop(parent
, "device_type",
576 device_type
, sizeof(device_type
));
578 len
= OF_getprop(parent
, "name",
579 device_type
, sizeof(device_type
));
580 device_type
[len
] = '\0';
583 len
= OF_getprop(parent
, "#address-cells",
584 address_cells_buf
, sizeof(address_cells_buf
));
589 parent
= OF_parent(parent
);
596 parent
= OF_parent(node
);
599 len
= OF_getprop(parent
, "#size-cells",
600 address_cells_buf
, sizeof(address_cells_buf
));
605 parent
= OF_parent(parent
);
614 /* looks like we're on an OBP2 system */
617 off
+= snprintf(buf
+ off
, buflen
- off
, "@");
618 memcpy(reg
, reg_buf
, 8);
619 off
+= snprintf(buf
+ off
, buflen
- off
, "%x,%x", reg
[0],
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]);
629 if (!strcmp(device_type
,"pci")) {
630 off
+= snprintf(buf
+ off
, buflen
- off
,
631 "%x", (reg
[0] >> 11) & 31);
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
,
641 #if !defined(__sparc__) && !defined(__sparc64__)
642 } else if (!strcmp(device_type
,"isa")) {
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
,
654 offullname2(int node
, char *buf
, size_t len
)
657 int parent
= OF_parent(node
);
661 off
= offullname2(parent
, buf
, len
);
662 off
+= ofname(node
, buf
+ off
, len
- off
);
667 offullname(int node
, char *buf
, size_t len
)
669 if (node
== OF_peer(0)) {
670 size_t off
= snprintf(buf
, len
, "/");
671 off
+= OF_getprop(node
, "name", buf
+ off
, len
- off
);
674 return offullname2(node
, buf
, len
);
678 oflist(int node
, const char *parent_device_type
, int depth
,
679 void *of_buf
, size_t of_buflen
)
685 len
= ofname(node
, of_buf
, of_buflen
-1);
686 printf("%08x: %*s%s", node
, depth
* 2, "",
689 len
= offullname(node
, of_buf
, of_buflen
-1);
690 printf("%08x: %s", node
, (char *) of_buf
);
696 puts("\n----------------------------------------"
697 "----------------------------------------\n\n");
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
));
705 len
= OF_getprop(node
, "name",
706 device_type
, sizeof(device_type
));
708 device_type
[len
] = '\0';
710 oflist(child
, device_type
, depth
, of_buf
, of_buflen
);
715 node
= OF_peer(node
);
720 print_line(const u_int8_t
*buf
, size_t off
, size_t len
)
725 for (; off
< ((len
+ 15) & ~15); off
++) {
728 printf("%12s%04lx:%7s", "",
729 (unsigned long int) off
, "");
730 else if ((off
& 3) == 0)
734 printf("%02x", buf
[off
]);
736 else if (off
>= ((len
+ 3) & ~3))
745 default_format(int node
, const u_int8_t
*buf
, size_t len
)
750 print_line(buf
, off
, len
);
751 printf(" "); /* 24 + 32 + 3 = 59, so +3 makes 62 */
755 for (; off
< end
; off
++) {
758 (isalnum((int)ch
) || ispunct((int)ch
) || ch
== ' '))
768 reg_format(int node
, const u_int8_t
*buf
, size_t len
)
770 /* parent = OF_parent(node); */
771 default_format(node
, buf
, len
);
775 frequency_format(int node
, const u_int8_t
*buf
, size_t len
)
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) {
786 whole
= freq
/ divisor
;
790 frac
= (freq
/ (divisor
/ 1000)) % 1000;
793 case 1000000000: units
= "GHz"; break;
794 case 1000000: units
= "MHz"; break;
795 case 1000: units
= "KHz"; break;
796 case 1: units
= "Hz"; break;
799 printf(" %u.%03u%s\n", whole
, frac
, units
);
801 printf(" %u%s\n", whole
, units
);
803 default_format(node
, buf
, len
);
807 size_format(int node
, const u_int8_t
*buf
, size_t len
)
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) {
818 whole
= freq
/ divisor
;
822 frac
= (freq
/ (divisor
>> 10)) & 1023;
825 case 0x40000000: units
= "G"; break;
826 case 0x100000: units
= "M"; break;
827 case 0x400: units
= "K"; break;
828 case 1: units
= ""; break;
831 printf(" %3u.%03u%s\n", whole
, frac
, units
);
833 printf(" %3u%s\n", whole
, units
);
835 default_format(node
, buf
, len
);
839 string_format(int node
, const u_int8_t
*buf
, size_t len
)
844 size_t string_len
= 0;
846 for (; off
+ string_len
< len
; string_len
++) {
847 if (buf
[off
+string_len
] == '\0') {
852 while (string_len
> 0) {
853 size_t line_len
= string_len
;
857 printf("%12s%04lx:%7s", "",
858 (unsigned long int) off
, "");
859 print_line(buf
+ off
, 0, line_len
);
865 string_len
-= line_len
;
866 for (; line_len
> 0; line_len
--, off
++) {
867 if (buf
[off
] != '\0')
877 static const struct {
878 const char *prop_name
;
879 void (*prop_format
)(int, const u_int8_t
*, size_t);
881 { "reg", reg_format
},
883 { "assigned-addresses", assigned_addresses_format
},
884 { "ranges", ranges_format
},
885 { "interrupt-map", interrup_map_format
},
886 { "interrupt", interrupt_format
},
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
}
901 ofgetprop(int node
, const char *name
)
903 u_int8_t of_buf
[4097];
907 len
= OF_getprop(node
, name
, of_buf
, sizeof(of_buf
) - 1);
911 printf("%-24s", name
);
916 if (strlen(name
) >= 24)
917 printf("\n%24s", "");
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
);
927 if (strcmp(name
, formatters
[i
].prop_name
) == 0) {
928 (*formatters
[i
].prop_format
)(node
, of_buf
, len
);
932 (*formatters
[i
].prop_format
)(node
, of_buf
, len
);
945 len
= OF_nextprop(node
, namebuf
, newnamebuf
);
949 newnamebuf
[len
] = '\0';
950 strcpy(namebuf
, newnamebuf
);
951 ofgetprop(node
, newnamebuf
);
956 isstrprint(const char *str
, size_t len
, int ignorenulls
)
960 for (; len
-- > 0; str
++) {
961 if (*str
== '\0' && len
> 0 && str
[1] == '\0')
963 if (len
== 0 && *str
== '\0')
968 if (isalnum(*str
) || ispunct(*str
) || *str
== ' ')