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