1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
4 *
5 * Portions from U-Boot cmd_fdt.c (C) Copyright 2007
6 * Gerald Van Baren, Custom IDEAS, [email protected]
7 * Based on code written by:
8 * Pantelis Antoniou <[email protected]> and
9 * Matthew McClintock <[email protected]>
10 */
11
12 #include <assert.h>
13 #include <ctype.h>
14 #include <getopt.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include <libfdt.h>
20
21 #include "util.h"
22
23 enum display_mode {
24 MODE_SHOW_VALUE, /* show values for node properties */
25 MODE_LIST_PROPS, /* list the properties for a node */
26 MODE_LIST_SUBNODES, /* list the subnodes of a node */
27 };
28
29 /* Holds information which controls our output and options */
30 struct display_info {
31 int type; /* data type (s/i/u/x or 0 for default) */
32 int size; /* data size (1/2/4) */
33 enum display_mode mode; /* display mode that we are using */
34 const char *default_val; /* default value if node/property not found */
35 };
36
report_error(const char * where,int err)37 static void report_error(const char *where, int err)
38 {
39 fprintf(stderr, "Error at '%s': %s\n", where, fdt_strerror(err));
40 }
41
42 /**
43 * Shows a list of cells in the requested format
44 *
45 * @param disp Display information / options
46 * @param data Data to display
47 * @param len Maximum length of buffer
48 * @param size Data size to use for display (e.g. 4 for 32-bit)
49 * @return 0 if ok, -1 on error
50 */
show_cell_list(struct display_info * disp,const char * data,int len,int size)51 static int show_cell_list(struct display_info *disp, const char *data, int len,
52 int size)
53 {
54 const uint8_t *p = (const uint8_t *)data;
55 char fmt[3];
56 int value;
57 int i;
58
59 fmt[0] = '%';
60 fmt[1] = disp->type ? disp->type : 'd';
61 fmt[2] = '\0';
62 for (i = 0; i < len; i += size, p += size) {
63 if (i)
64 printf(" ");
65 switch (size) {
66 case 4: value = fdt32_ld((const fdt32_t *)p); break;
67 case 2: value = fdt16_ld((const fdt16_t *)p); break;
68 case 1:
69 default:
70 value = *p;
71 break;
72 }
73 printf(fmt, value);
74 }
75
76 return 0;
77 }
78
79 /**
80 * Displays data of a given length according to selected options
81 *
82 * If a specific data type is provided in disp, then this is used. Otherwise
83 * we try to guess the data type / size from the contents.
84 *
85 * @param disp Display information / options
86 * @param data Data to display
87 * @param len Maximum length of buffer
88 * @return 0 if ok, -1 if data does not match format
89 */
show_data(struct display_info * disp,const char * data,int len)90 static int show_data(struct display_info *disp, const char *data, int len)
91 {
92 int size;
93 const char *s;
94 int is_string;
95
96 /* no data, don't print */
97 if (len == 0)
98 return 0;
99
100 if (disp->type == 'r') {
101 fwrite(data, 1, len, stdout);
102 return 0;
103 }
104
105 is_string = (disp->type) == 's' ||
106 (!disp->type && util_is_printable_string(data, len));
107 if (is_string) {
108 if (data[len - 1] != '\0') {
109 fprintf(stderr, "Unterminated string\n");
110 return -1;
111 }
112 for (s = data; s - data < len; s += strlen(s) + 1) {
113 if (s != data)
114 printf(" ");
115 printf("%s", (const char *)s);
116 }
117 return 0;
118 }
119 size = disp->size;
120 if (size == -1) {
121 size = (len % 4) == 0 ? 4 : 1;
122 } else if (len % size) {
123 fprintf(stderr, "Property length must be a multiple of "
124 "selected data size\n");
125 return -1;
126 }
127
128 return show_cell_list(disp, data, len, size);
129 }
130
131 /**
132 * List all properties in a node, one per line.
133 *
134 * @param blob FDT blob
135 * @param node Node to display
136 * @return 0 if ok, or FDT_ERR... if not.
137 */
list_properties(const void * blob,int node)138 static int list_properties(const void *blob, int node)
139 {
140 const char *name;
141 int prop;
142
143 prop = fdt_first_property_offset(blob, node);
144 do {
145 /* Stop silently when there are no more properties */
146 if (prop < 0)
147 return prop == -FDT_ERR_NOTFOUND ? 0 : prop;
148 fdt_getprop_by_offset(blob, prop, &name, NULL);
149 if (name)
150 puts(name);
151 prop = fdt_next_property_offset(blob, prop);
152 } while (1);
153 }
154
155 #define MAX_LEVEL 32 /* how deeply nested we will go */
156
157 /**
158 * List all subnodes in a node, one per line
159 *
160 * @param blob FDT blob
161 * @param node Node to display
162 * @return 0 if ok, or FDT_ERR... if not.
163 */
list_subnodes(const void * blob,int node)164 static int list_subnodes(const void *blob, int node)
165 {
166 int nextoffset; /* next node offset from libfdt */
167 uint32_t tag; /* current tag */
168 int level = 0; /* keep track of nesting level */
169 const char *pathp;
170 int depth = 1; /* the assumed depth of this node */
171
172 while (level >= 0) {
173 tag = fdt_next_tag(blob, node, &nextoffset);
174 switch (tag) {
175 case FDT_BEGIN_NODE:
176 pathp = fdt_get_name(blob, node, NULL);
177 if (level <= depth) {
178 if (pathp == NULL)
179 pathp = "/* NULL pointer error */";
180 if (*pathp == '\0')
181 pathp = "/"; /* root is nameless */
182 if (level == 1)
183 puts(pathp);
184 }
185 level++;
186 if (level >= MAX_LEVEL) {
187 printf("Nested too deep, aborting.\n");
188 return 1;
189 }
190 break;
191 case FDT_END_NODE:
192 level--;
193 if (level == 0)
194 level = -1; /* exit the loop */
195 break;
196 case FDT_END:
197 return 1;
198 case FDT_PROP:
199 break;
200 default:
201 if (level <= depth)
202 printf("Unknown tag 0x%08X\n", tag);
203 return 1;
204 }
205 node = nextoffset;
206 }
207 return 0;
208 }
209
210 /**
211 * Show the data for a given node (and perhaps property) according to the
212 * display option provided.
213 *
214 * @param blob FDT blob
215 * @param disp Display information / options
216 * @param node Node to display
217 * @param property Name of property to display, or NULL if none
218 * @return 0 if ok, -ve on error
219 */
show_data_for_item(const void * blob,struct display_info * disp,int node,const char * property)220 static int show_data_for_item(const void *blob, struct display_info *disp,
221 int node, const char *property)
222 {
223 const void *value = NULL;
224 int len, err = 0;
225
226 switch (disp->mode) {
227 case MODE_LIST_PROPS:
228 err = list_properties(blob, node);
229 break;
230
231 case MODE_LIST_SUBNODES:
232 err = list_subnodes(blob, node);
233 break;
234
235 default:
236 assert(property);
237 value = fdt_getprop(blob, node, property, &len);
238 if (value) {
239 if (show_data(disp, value, len))
240 err = -1;
241 else
242 printf("\n");
243 } else if (disp->default_val) {
244 puts(disp->default_val);
245 } else {
246 report_error(property, len);
247 err = -1;
248 }
249 break;
250 }
251
252 return err;
253 }
254
255 /**
256 * Run the main fdtget operation, given a filename and valid arguments
257 *
258 * @param disp Display information / options
259 * @param filename Filename of blob file
260 * @param arg List of arguments to process
261 * @param arg_count Number of arguments
262 * @return 0 if ok, -ve on error
263 */
do_fdtget(struct display_info * disp,const char * filename,char ** arg,int arg_count,int args_per_step)264 static int do_fdtget(struct display_info *disp, const char *filename,
265 char **arg, int arg_count, int args_per_step)
266 {
267 char *blob;
268 const char *prop;
269 int i, node;
270
271 blob = utilfdt_read(filename, NULL);
272 if (!blob)
273 return -1;
274
275 for (i = 0; i + args_per_step <= arg_count; i += args_per_step) {
276 node = fdt_path_offset(blob, arg[i]);
277 if (node < 0) {
278 if (disp->default_val) {
279 puts(disp->default_val);
280 continue;
281 } else {
282 report_error(arg[i], node);
283 free(blob);
284 return -1;
285 }
286 }
287 prop = args_per_step == 1 ? NULL : arg[i + 1];
288
289 if (show_data_for_item(blob, disp, node, prop)) {
290 free(blob);
291 return -1;
292 }
293 }
294
295 free(blob);
296
297 return 0;
298 }
299
300 /* Usage related data. */
301 static const char usage_synopsis[] =
302 "read values from device tree\n"
303 " fdtget <options> <dt file> [<node> <property>]...\n"
304 " fdtget -p <options> <dt file> [<node> ]...\n"
305 "\n"
306 "Each value is printed on a new line.\n"
307 USAGE_TYPE_MSG;
308 static const char usage_short_opts[] = "t:pld:" USAGE_COMMON_SHORT_OPTS;
309 static struct option const usage_long_opts[] = {
310 {"type", a_argument, NULL, 't'},
311 {"properties", no_argument, NULL, 'p'},
312 {"list", no_argument, NULL, 'l'},
313 {"default", a_argument, NULL, 'd'},
314 USAGE_COMMON_LONG_OPTS,
315 };
316 static const char * const usage_opts_help[] = {
317 "Type of data",
318 "List properties for each node",
319 "List subnodes for each node",
320 "Default value to display when the property is missing",
321 USAGE_COMMON_OPTS_HELP
322 };
323
main(int argc,char * argv[])324 int main(int argc, char *argv[])
325 {
326 int opt;
327 char *filename = NULL;
328 struct display_info disp;
329 int args_per_step = 2;
330
331 /* set defaults */
332 memset(&disp, '\0', sizeof(disp));
333 disp.size = -1;
334 disp.mode = MODE_SHOW_VALUE;
335 while ((opt = util_getopt_long()) != EOF) {
336 switch (opt) {
337 case_USAGE_COMMON_FLAGS
338
339 case 't':
340 if (utilfdt_decode_type(optarg, &disp.type,
341 &disp.size))
342 usage("invalid type string");
343 break;
344
345 case 'p':
346 disp.mode = MODE_LIST_PROPS;
347 args_per_step = 1;
348 break;
349
350 case 'l':
351 disp.mode = MODE_LIST_SUBNODES;
352 args_per_step = 1;
353 break;
354
355 case 'd':
356 disp.default_val = optarg;
357 break;
358 }
359 }
360
361 if (optind < argc)
362 filename = argv[optind++];
363 if (!filename)
364 usage("missing filename");
365
366 argv += optind;
367 argc -= optind;
368
369 /* Allow no arguments, and silently succeed */
370 if (!argc)
371 return 0;
372
373 /* Check for node, property arguments */
374 if (args_per_step == 2 && (argc % 2))
375 usage("must have an even number of arguments");
376
377 if (do_fdtget(&disp, filename, argv, argc, args_per_step))
378 return 1;
379 return 0;
380 }
381