blob: dd709854becc1961bf765cb6f2cb72c7e34bb485 [file] [log] [blame]
Rob Herringacfe84f2019-06-20 15:19:38 -06001// SPDX-License-Identifier: GPL-2.0-or-later
Simon Glass68d057f2012-01-21 10:14:47 -08002/*
3 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
4 *
Simon Glass30eb2012012-03-02 17:12:08 -08005 * 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>
Simon Glass68d057f2012-01-21 10:14:47 -080010 */
11
Simon Glass30eb2012012-03-02 17:12:08 -080012#include <assert.h>
Simon Glass68d057f2012-01-21 10:14:47 -080013#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
Simon Glass30eb2012012-03-02 17:12:08 -080023enum display_mode {
24 MODE_SHOW_VALUE, /* show values for node properties */
25 MODE_LIST_PROPS, /* list the properties for a node */
Simon Glass16c99ee2012-03-06 16:41:46 -080026 MODE_LIST_SUBNODES, /* list the subnodes of a node */
Simon Glass30eb2012012-03-02 17:12:08 -080027};
28
Simon Glass68d057f2012-01-21 10:14:47 -080029/* Holds information which controls our output and options */
30struct display_info {
31 int type; /* data type (s/i/u/x or 0 for default) */
32 int size; /* data size (1/2/4) */
Simon Glass30eb2012012-03-02 17:12:08 -080033 enum display_mode mode; /* display mode that we are using */
Simon Glass7fcbef22012-03-06 16:41:47 -080034 const char *default_val; /* default value if node/property not found */
Simon Glass68d057f2012-01-21 10:14:47 -080035};
36
37static void report_error(const char *where, int err)
38{
39 fprintf(stderr, "Error at '%s': %s\n", where, fdt_strerror(err));
40}
41
42/**
Simon Glassfe50bd12017-08-20 06:28:54 -060043 * 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 */
51static 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(" ");
Pierre-Clément Tosief1978a2022-05-30 20:57:34 +010065 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 }
Simon Glassfe50bd12017-08-20 06:28:54 -060073 printf(fmt, value);
74 }
75
76 return 0;
77}
78
79/**
Simon Glass68d057f2012-01-21 10:14:47 -080080 * 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 */
90static int show_data(struct display_info *disp, const char *data, int len)
91{
Simon Glassfe50bd12017-08-20 06:28:54 -060092 int size;
Simon Glass68d057f2012-01-21 10:14:47 -080093 const char *s;
Simon Glass68d057f2012-01-21 10:14:47 -080094 int is_string;
Simon Glass68d057f2012-01-21 10:14:47 -080095
96 /* no data, don't print */
97 if (len == 0)
98 return 0;
99
Pierre-Clément Tosief1978a2022-05-30 20:57:34 +0100100 if (disp->type == 'r') {
101 fwrite(data, 1, len, stdout);
102 return 0;
103 }
104
Simon Glass68d057f2012-01-21 10:14:47 -0800105 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;
David Gibson53d6ca62012-02-03 16:12:02 +1100120 if (size == -1) {
Simon Glass68d057f2012-01-21 10:14:47 -0800121 size = (len % 4) == 0 ? 4 : 1;
David Gibson53d6ca62012-02-03 16:12:02 +1100122 } else if (len % size) {
Simon Glass68d057f2012-01-21 10:14:47 -0800123 fprintf(stderr, "Property length must be a multiple of "
124 "selected data size\n");
125 return -1;
126 }
Simon Glassfe50bd12017-08-20 06:28:54 -0600127
128 return show_cell_list(disp, data, len, size);
Simon Glass68d057f2012-01-21 10:14:47 -0800129}
130
131/**
Simon Glass30eb2012012-03-02 17:12:08 -0800132 * 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 */
138static int list_properties(const void *blob, int node)
139{
Simon Glass30eb2012012-03-02 17:12:08 -0800140 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;
Nathan Whitehornf1879e12018-01-25 05:13:40 +0000148 fdt_getprop_by_offset(blob, prop, &name, NULL);
Simon Glass30eb2012012-03-02 17:12:08 -0800149 if (name)
150 puts(name);
151 prop = fdt_next_property_offset(blob, prop);
152 } while (1);
153}
154
Simon Glass16c99ee2012-03-06 16:41:46 -0800155#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 */
164static 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
Simon Glass30eb2012012-03-02 17:12:08 -0800210/**
Simon Glass68d057f2012-01-21 10:14:47 -0800211 * 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 */
220static 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
Simon Glass16c99ee2012-03-06 16:41:46 -0800226 switch (disp->mode) {
227 case MODE_LIST_PROPS:
228 err = list_properties(blob, node);
229 break;
Simon Glass30eb2012012-03-02 17:12:08 -0800230
Simon Glass16c99ee2012-03-06 16:41:46 -0800231 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");
Simon Glass7fcbef22012-03-06 16:41:47 -0800243 } else if (disp->default_val) {
244 puts(disp->default_val);
Simon Glass16c99ee2012-03-06 16:41:46 -0800245 } else {
246 report_error(property, len);
Simon Glass68d057f2012-01-21 10:14:47 -0800247 err = -1;
Simon Glass16c99ee2012-03-06 16:41:46 -0800248 }
249 break;
Simon Glass68d057f2012-01-21 10:14:47 -0800250 }
Simon Glass16c99ee2012-03-06 16:41:46 -0800251
Simon Glass68d057f2012-01-21 10:14:47 -0800252 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
Nicolas Iooss194d5ca2017-03-04 14:26:46 +0100262 * @return 0 if ok, -ve on error
Simon Glass68d057f2012-01-21 10:14:47 -0800263 */
264static int do_fdtget(struct display_info *disp, const char *filename,
Simon Glass30eb2012012-03-02 17:12:08 -0800265 char **arg, int arg_count, int args_per_step)
Simon Glass68d057f2012-01-21 10:14:47 -0800266{
267 char *blob;
Simon Glass30eb2012012-03-02 17:12:08 -0800268 const char *prop;
Simon Glass68d057f2012-01-21 10:14:47 -0800269 int i, node;
270
David Gibson6473a212018-03-16 18:27:03 +1100271 blob = utilfdt_read(filename, NULL);
Simon Glass68d057f2012-01-21 10:14:47 -0800272 if (!blob)
273 return -1;
274
Simon Glass30eb2012012-03-02 17:12:08 -0800275 for (i = 0; i + args_per_step <= arg_count; i += args_per_step) {
Simon Glass097ec972012-03-02 17:12:07 -0800276 node = fdt_path_offset(blob, arg[i]);
Simon Glass68d057f2012-01-21 10:14:47 -0800277 if (node < 0) {
Simon Glass7fcbef22012-03-06 16:41:47 -0800278 if (disp->default_val) {
279 puts(disp->default_val);
280 continue;
281 } else {
282 report_error(arg[i], node);
Jean-Christophe Duboisf79ddb82016-07-13 00:36:08 +0200283 free(blob);
Simon Glass7fcbef22012-03-06 16:41:47 -0800284 return -1;
285 }
Simon Glass68d057f2012-01-21 10:14:47 -0800286 }
Simon Glass30eb2012012-03-02 17:12:08 -0800287 prop = args_per_step == 1 ? NULL : arg[i + 1];
Simon Glass68d057f2012-01-21 10:14:47 -0800288
Jean-Christophe Duboisf79ddb82016-07-13 00:36:08 +0200289 if (show_data_for_item(blob, disp, node, prop)) {
290 free(blob);
Simon Glass68d057f2012-01-21 10:14:47 -0800291 return -1;
Jean-Christophe Duboisf79ddb82016-07-13 00:36:08 +0200292 }
Simon Glass68d057f2012-01-21 10:14:47 -0800293 }
Jean-Christophe Duboisf79ddb82016-07-13 00:36:08 +0200294
295 free(blob);
296
Simon Glass68d057f2012-01-21 10:14:47 -0800297 return 0;
298}
299
Mike Frysinger03449b82013-05-24 18:02:35 +1000300/* Usage related data. */
301static const char usage_synopsis[] =
302 "read values from device tree\n"
Simon Glass68d057f2012-01-21 10:14:47 -0800303 " fdtget <options> <dt file> [<node> <property>]...\n"
Simon Glass30eb2012012-03-02 17:12:08 -0800304 " fdtget -p <options> <dt file> [<node> ]...\n"
Mike Frysinger03449b82013-05-24 18:02:35 +1000305 "\n"
306 "Each value is printed on a new line.\n"
Simon Glass68d057f2012-01-21 10:14:47 -0800307 USAGE_TYPE_MSG;
Mike Frysinger03449b82013-05-24 18:02:35 +1000308static const char usage_short_opts[] = "t:pld:" USAGE_COMMON_SHORT_OPTS;
309static 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};
316static 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};
Simon Glass68d057f2012-01-21 10:14:47 -0800323
324int main(int argc, char *argv[])
325{
Mike Frysinger03449b82013-05-24 18:02:35 +1000326 int opt;
Simon Glass68d057f2012-01-21 10:14:47 -0800327 char *filename = NULL;
328 struct display_info disp;
Simon Glass30eb2012012-03-02 17:12:08 -0800329 int args_per_step = 2;
Simon Glass68d057f2012-01-21 10:14:47 -0800330
331 /* set defaults */
332 memset(&disp, '\0', sizeof(disp));
333 disp.size = -1;
Simon Glass30eb2012012-03-02 17:12:08 -0800334 disp.mode = MODE_SHOW_VALUE;
Mike Frysinger03449b82013-05-24 18:02:35 +1000335 while ((opt = util_getopt_long()) != EOF) {
336 switch (opt) {
337 case_USAGE_COMMON_FLAGS
Simon Glass68d057f2012-01-21 10:14:47 -0800338
339 case 't':
340 if (utilfdt_decode_type(optarg, &disp.type,
341 &disp.size))
Mike Frysingerb9e80652013-05-24 18:04:43 +1000342 usage("invalid type string");
Simon Glass68d057f2012-01-21 10:14:47 -0800343 break;
Simon Glass30eb2012012-03-02 17:12:08 -0800344
345 case 'p':
346 disp.mode = MODE_LIST_PROPS;
347 args_per_step = 1;
348 break;
Simon Glass16c99ee2012-03-06 16:41:46 -0800349
350 case 'l':
351 disp.mode = MODE_LIST_SUBNODES;
352 args_per_step = 1;
353 break;
Simon Glass7fcbef22012-03-06 16:41:47 -0800354
355 case 'd':
356 disp.default_val = optarg;
357 break;
Simon Glass68d057f2012-01-21 10:14:47 -0800358 }
359 }
360
361 if (optind < argc)
362 filename = argv[optind++];
363 if (!filename)
Mike Frysingerb9e80652013-05-24 18:04:43 +1000364 usage("missing filename");
Simon Glass68d057f2012-01-21 10:14:47 -0800365
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 */
Simon Glass30eb2012012-03-02 17:12:08 -0800374 if (args_per_step == 2 && (argc % 2))
Mike Frysingerb9e80652013-05-24 18:04:43 +1000375 usage("must have an even number of arguments");
Simon Glass68d057f2012-01-21 10:14:47 -0800376
Simon Glass30eb2012012-03-02 17:12:08 -0800377 if (do_fdtget(&disp, filename, argv, argc, args_per_step))
Simon Glass68d057f2012-01-21 10:14:47 -0800378 return 1;
379 return 0;
380}