source: mainline/uspace/app/ofw/ofw.c

Last change on this file was 4d58bac, checked in by Jiri Svoboda <jiri@…>, 21 months ago

Library and utility for printing OpenFirmware device tree

  • Property mode set to 100644
File size: 4.1 KB
Line 
1/*
2 * Copyright (c) 2023 Jiri Svoboda
3 * 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 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup ofw
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Tool for printing the OpenFirmware device tree
36 */
37
38#include <errno.h>
39#include <ofw.h>
40#include <stdbool.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <str.h>
44
45#define NAME "ofw"
46
47#define MAX_NAME_LENGTH 1024
48
49static void syntax_print(void);
50static errno_t ofw_print_subtree(const char *, bool);
51static errno_t ofw_print_properties(const char *);
52
53int main(int argc, char **argv)
54{
55 errno_t rc;
56 const char *path = "/";
57 bool verbose = false;
58
59 --argc;
60 ++argv;
61
62 while (argc > 0 && argv[0][0] == '-') {
63 if (str_cmp(argv[0], "-p") == 0) {
64 --argc;
65 ++argv;
66 if (argc < 1) {
67 printf("Option argument missing.\n");
68 return 1;
69 }
70
71 path = argv[0];
72 --argc;
73 ++argv;
74 } else if (str_cmp(argv[0], "-v") == 0) {
75 --argc;
76 ++argv;
77
78 verbose = true;
79 } else {
80 syntax_print();
81 return 1;
82 }
83 }
84
85 if (argc != 0) {
86 syntax_print();
87 return 1;
88 }
89
90 rc = ofw_print_subtree(path, verbose);
91 if (rc != EOK)
92 return 1;
93
94 return 0;
95}
96
97static void syntax_print(void)
98{
99 printf("syntax: %s [<options>]\n", NAME);
100 printf("options:\n"
101 "\t-v Verbose mode (print properties and their values)\n"
102 "\t-p <path> Only print devices under <path>\n");
103}
104
105/** List OpenFirmware device nodes under a specific node.
106 *
107 * @param path Path of node where to start printing
108 * @param verbose If @c true, print properties
109 * @return EOK on success or an error code
110 */
111static errno_t ofw_print_subtree(const char *path, bool verbose)
112{
113 char *subpath;
114 ofw_child_it_t it;
115 errno_t rc;
116
117 printf("%s\n", path);
118
119 if (verbose) {
120 rc = ofw_print_properties(path);
121 if (rc != EOK)
122 return rc;
123 }
124
125 rc = ofw_child_it_first(&it, path);
126 while (!ofw_child_it_end(&it)) {
127 rc = ofw_child_it_get_path(&it, &subpath);
128 if (rc != EOK)
129 goto error;
130
131 rc = ofw_print_subtree(subpath, verbose);
132 if (rc != EOK)
133 goto error;
134
135 free(subpath);
136 subpath = NULL;
137
138 ofw_child_it_next(&it);
139 }
140
141 ofw_child_it_fini(&it);
142 return EOK;
143error:
144 if (subpath != NULL)
145 free(subpath);
146 ofw_child_it_fini(&it);
147 return rc;
148}
149
150static errno_t ofw_print_properties(const char *ofwpath)
151{
152 const char *propname;
153 const uint8_t *propval;
154 size_t val_sz;
155 ofw_prop_it_t it;
156 errno_t rc;
157 size_t i;
158
159 rc = ofw_prop_it_first(&it, ofwpath);
160 if (rc != EOK)
161 return rc;
162
163 while (!ofw_prop_it_end(&it)) {
164 propname = ofw_prop_it_get_name(&it);
165 printf("'%s' =", propname);
166
167 propval = ofw_prop_it_get_data(&it, &val_sz);
168
169 for (i = 0; i < val_sz; i++)
170 printf(" %02x", propval[i]);
171
172 printf(" ('%*s')\n", (int)val_sz - 1, (char *)propval);
173 ofw_prop_it_next(&it);
174 }
175
176 ofw_prop_it_fini(&it);
177 return EOK;
178}
179
180/**
181 * @}
182 */
Note: See TracBrowser for help on using the repository browser.