blob: 777582e2d45fc5c6c79350fa1016da5618884dc3 [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(" ");
David Gibson6dcb8ba2018-06-21 15:32:40 +100065 value = size == 4 ? fdt32_ld((const fdt32_t *)p) :
Simon Glassfe50bd12017-08-20 06:28:54 -060066 size == 2 ? (*p << 8) | p[1] : *p;
67 printf(fmt, value);
68 }
69
70 return 0;
71}
72
73/**
Simon Glass68d057f2012-01-21 10:14:47 -080074 * 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 */
84static int show_data(struct display_info *disp, const char *data, int len)
85{
Simon Glassfe50bd12017-08-20 06:28:54 -060086 int size;
Simon Glass68d057f2012-01-21 10:14:47 -080087 const char *s;
Simon Glass68d057f2012-01-21 10:14:47 -080088 int is_string;
Simon Glass68d057f2012-01-21 10:14:47 -080089
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;
David Gibson53d6ca62012-02-03 16:12:02 +1100109 if (size == -1) {
Simon Glass68d057f2012-01-21 10:14:47 -0800110 size = (len % 4) == 0 ? 4 : 1;
David Gibson53d6ca62012-02-03 16:12:02 +1100111 } else if (len % size) {
Simon Glass68d057f2012-01-21 10:14:47 -0800112 fprintf(stderr, "Property length must be a multiple of "
113 "selected data size\n");
114 return -1;
115 }
Simon Glassfe50bd12017-08-20 06:28:54 -0600116
117 return show_cell_list(disp, data, len, size);
Simon Glass68d057f2012-01-21 10:14:47 -0800118}
119
120/**
Simon Glass30eb2012012-03-02 17:12:08 -0800121 * 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 */
127static int list_properties(const void *blob, int node)
128{
Simon Glass30eb2012012-03-02 17:12:08 -0800129 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;
Nathan Whitehornf1879e12018-01-25 05:13:40 +0000137 fdt_getprop_by_offset(blob, prop, &name, NULL);
Simon Glass30eb2012012-03-02 17:12:08 -0800138 if (name)
139 puts(name);
140 prop = fdt_next_property_offset(blob, prop);
141 } while (1);
142}
143
Simon Glass16c99ee2012-03-06 16:41:46 -0800144#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 */
153static 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
Simon Glass30eb2012012-03-02 17:12:08 -0800199/**
Simon Glass68d057f2012-01-21 10:14:47 -0800200 * 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 */
209static 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
Simon Glass16c99ee2012-03-06 16:41:46 -0800215 switch (disp->mode) {
216 case MODE_LIST_PROPS:
217 err = list_properties(blob, node);
218 break;
Simon Glass30eb2012012-03-02 17:12:08 -0800219
Simon Glass16c99ee2012-03-06 16:41:46 -0800220 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");
Simon Glass7fcbef22012-03-06 16:41:47 -0800232 } else if (disp->default_val) {
233 puts(disp->default_val);
Simon Glass16c99ee2012-03-06 16:41:46 -0800234 } else {
235 report_error(property, len);
Simon Glass68d057f2012-01-21 10:14:47 -0800236 err = -1;
Simon Glass16c99ee2012-03-06 16:41:46 -0800237 }
238 break;
Simon Glass68d057f2012-01-21 10:14:47 -0800239 }
Simon Glass16c99ee2012-03-06 16:41:46 -0800240
Simon Glass68d057f2012-01-21 10:14:47 -0800241 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
Nicolas Iooss194d5ca2017-03-04 14:26:46 +0100251 * @return 0 if ok, -ve on error
Simon Glass68d057f2012-01-21 10:14:47 -0800252 */
253static int do_fdtget(struct display_info *disp, const char *filename,
Simon Glass30eb2012012-03-02 17:12:08 -0800254 char **arg, int arg_count, int args_per_step)
Simon Glass68d057f2012-01-21 10:14:47 -0800255{
256 char *blob;
Simon Glass30eb2012012-03-02 17:12:08 -0800257 const char *prop;
Simon Glass68d057f2012-01-21 10:14:47 -0800258 int i, node;
259
David Gibson6473a212018-03-16 18:27:03 +1100260 blob = utilfdt_read(filename, NULL);
Simon Glass68d057f2012-01-21 10:14:47 -0800261 if (!blob)
262 return -1;
263
Simon Glass30eb2012012-03-02 17:12:08 -0800264 for (i = 0; i + args_per_step <= arg_count; i += args_per_step) {
Simon Glass097ec972012-03-02 17:12:07 -0800265 node = fdt_path_offset(blob, arg[i]);
Simon Glass68d057f2012-01-21 10:14:47 -0800266 if (node < 0) {
Simon Glass7fcbef22012-03-06 16:41:47 -0800267 if (disp->default_val) {
268 puts(disp->default_val);
269 continue;
270 } else {
271 report_error(arg[i], node);
Jean-Christophe Duboisf79ddb82016-07-13 00:36:08 +0200272 free(blob);
Simon Glass7fcbef22012-03-06 16:41:47 -0800273 return -1;
274 }
Simon Glass68d057f2012-01-21 10:14:47 -0800275 }
Simon Glass30eb2012012-03-02 17:12:08 -0800276 prop = args_per_step == 1 ? NULL : arg[i + 1];
Simon Glass68d057f2012-01-21 10:14:47 -0800277
Jean-Christophe Duboisf79ddb82016-07-13 00:36:08 +0200278 if (show_data_for_item(blob, disp, node, prop)) {
279 free(blob);
Simon Glass68d057f2012-01-21 10:14:47 -0800280 return -1;
Jean-Christophe Duboisf79ddb82016-07-13 00:36:08 +0200281 }
Simon Glass68d057f2012-01-21 10:14:47 -0800282 }
Jean-Christophe Duboisf79ddb82016-07-13 00:36:08 +0200283
284 free(blob);
285
Simon Glass68d057f2012-01-21 10:14:47 -0800286 return 0;
287}
288
Mike Frysinger03449b82013-05-24 18:02:35 +1000289/* Usage related data. */
290static const char usage_synopsis[] =
291 "read values from device tree\n"
Simon Glass68d057f2012-01-21 10:14:47 -0800292 " fdtget <options> <dt file> [<node> <property>]...\n"
Simon Glass30eb2012012-03-02 17:12:08 -0800293 " fdtget -p <options> <dt file> [<node> ]...\n"
Mike Frysinger03449b82013-05-24 18:02:35 +1000294 "\n"
295 "Each value is printed on a new line.\n"
Simon Glass68d057f2012-01-21 10:14:47 -0800296 USAGE_TYPE_MSG;
Mike Frysinger03449b82013-05-24 18:02:35 +1000297static const char usage_short_opts[] = "t:pld:" USAGE_COMMON_SHORT_OPTS;
298static 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};
305static 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};
Simon Glass68d057f2012-01-21 10:14:47 -0800312
313int main(int argc, char *argv[])
314{
Mike Frysinger03449b82013-05-24 18:02:35 +1000315 int opt;
Simon Glass68d057f2012-01-21 10:14:47 -0800316 char *filename = NULL;
317 struct display_info disp;
Simon Glass30eb2012012-03-02 17:12:08 -0800318 int args_per_step = 2;
Simon Glass68d057f2012-01-21 10:14:47 -0800319
320 /* set defaults */
321 memset(&disp, '\0', sizeof(disp));
322 disp.size = -1;
Simon Glass30eb2012012-03-02 17:12:08 -0800323 disp.mode = MODE_SHOW_VALUE;
Mike Frysinger03449b82013-05-24 18:02:35 +1000324 while ((opt = util_getopt_long()) != EOF) {
325 switch (opt) {
326 case_USAGE_COMMON_FLAGS
Simon Glass68d057f2012-01-21 10:14:47 -0800327
328 case 't':
329 if (utilfdt_decode_type(optarg, &disp.type,
330 &disp.size))
Mike Frysingerb9e80652013-05-24 18:04:43 +1000331 usage("invalid type string");
Simon Glass68d057f2012-01-21 10:14:47 -0800332 break;
Simon Glass30eb2012012-03-02 17:12:08 -0800333
334 case 'p':
335 disp.mode = MODE_LIST_PROPS;
336 args_per_step = 1;
337 break;
Simon Glass16c99ee2012-03-06 16:41:46 -0800338
339 case 'l':
340 disp.mode = MODE_LIST_SUBNODES;
341 args_per_step = 1;
342 break;
Simon Glass7fcbef22012-03-06 16:41:47 -0800343
344 case 'd':
345 disp.default_val = optarg;
346 break;
Simon Glass68d057f2012-01-21 10:14:47 -0800347 }
348 }
349
350 if (optind < argc)
351 filename = argv[optind++];
352 if (!filename)
Mike Frysingerb9e80652013-05-24 18:04:43 +1000353 usage("missing filename");
Simon Glass68d057f2012-01-21 10:14:47 -0800354
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 */
Simon Glass30eb2012012-03-02 17:12:08 -0800363 if (args_per_step == 2 && (argc % 2))
Mike Frysingerb9e80652013-05-24 18:04:43 +1000364 usage("must have an even number of arguments");
Simon Glass68d057f2012-01-21 10:14:47 -0800365
Simon Glass30eb2012012-03-02 17:12:08 -0800366 if (do_fdtget(&disp, filename, argv, argc, args_per_step))
Simon Glass68d057f2012-01-21 10:14:47 -0800367 return 1;
368 return 0;
369}