blob: 39f1742b0697fbf0e0dd796c47b3ebf6da261d7b [file] [log] [blame]
Simon Glass68d057f2012-01-21 10:14:47 -08001/*
2 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
3 *
Simon Glass30eb2012012-03-02 17:12:08 -08004 * Portions from U-Boot cmd_fdt.c (C) Copyright 2007
5 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
6 * Based on code written by:
7 * Pantelis Antoniou <pantelis.antoniou@gmail.com> and
8 * Matthew McClintock <msm@freescale.com>
9 *
Simon Glass68d057f2012-01-21 10:14:47 -080010 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
25
Simon Glass30eb2012012-03-02 17:12:08 -080026#include <assert.h>
Simon Glass68d057f2012-01-21 10:14:47 -080027#include <ctype.h>
28#include <getopt.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
33#include <libfdt.h>
34
35#include "util.h"
36
Simon Glass30eb2012012-03-02 17:12:08 -080037enum display_mode {
38 MODE_SHOW_VALUE, /* show values for node properties */
39 MODE_LIST_PROPS, /* list the properties for a node */
Simon Glass16c99ee2012-03-06 16:41:46 -080040 MODE_LIST_SUBNODES, /* list the subnodes of a node */
Simon Glass30eb2012012-03-02 17:12:08 -080041};
42
Simon Glass68d057f2012-01-21 10:14:47 -080043/* Holds information which controls our output and options */
44struct display_info {
45 int type; /* data type (s/i/u/x or 0 for default) */
46 int size; /* data size (1/2/4) */
Simon Glass30eb2012012-03-02 17:12:08 -080047 enum display_mode mode; /* display mode that we are using */
Simon Glass7fcbef22012-03-06 16:41:47 -080048 const char *default_val; /* default value if node/property not found */
Simon Glass68d057f2012-01-21 10:14:47 -080049};
50
51static void report_error(const char *where, int err)
52{
53 fprintf(stderr, "Error at '%s': %s\n", where, fdt_strerror(err));
54}
55
56/**
57 * Displays data of a given length according to selected options
58 *
59 * If a specific data type is provided in disp, then this is used. Otherwise
60 * we try to guess the data type / size from the contents.
61 *
62 * @param disp Display information / options
63 * @param data Data to display
64 * @param len Maximum length of buffer
65 * @return 0 if ok, -1 if data does not match format
66 */
67static int show_data(struct display_info *disp, const char *data, int len)
68{
69 int i, size;
70 const uint8_t *p = (const uint8_t *)data;
71 const char *s;
72 int value;
73 int is_string;
74 char fmt[3];
75
76 /* no data, don't print */
77 if (len == 0)
78 return 0;
79
80 is_string = (disp->type) == 's' ||
81 (!disp->type && util_is_printable_string(data, len));
82 if (is_string) {
83 if (data[len - 1] != '\0') {
84 fprintf(stderr, "Unterminated string\n");
85 return -1;
86 }
87 for (s = data; s - data < len; s += strlen(s) + 1) {
88 if (s != data)
89 printf(" ");
90 printf("%s", (const char *)s);
91 }
92 return 0;
93 }
94 size = disp->size;
David Gibson53d6ca62012-02-03 16:12:02 +110095 if (size == -1) {
Simon Glass68d057f2012-01-21 10:14:47 -080096 size = (len % 4) == 0 ? 4 : 1;
David Gibson53d6ca62012-02-03 16:12:02 +110097 } else if (len % size) {
Simon Glass68d057f2012-01-21 10:14:47 -080098 fprintf(stderr, "Property length must be a multiple of "
99 "selected data size\n");
100 return -1;
101 }
102 fmt[0] = '%';
103 fmt[1] = disp->type ? disp->type : 'd';
104 fmt[2] = '\0';
105 for (i = 0; i < len; i += size, p += size) {
106 if (i)
107 printf(" ");
David Gibsonbad5b282017-03-06 12:08:53 +1100108 value = size == 4 ? fdt32_to_cpu(*(const fdt32_t *)p) :
Simon Glass68d057f2012-01-21 10:14:47 -0800109 size == 2 ? (*p << 8) | p[1] : *p;
110 printf(fmt, value);
111 }
112 return 0;
113}
114
115/**
Simon Glass30eb2012012-03-02 17:12:08 -0800116 * List all properties in a node, one per line.
117 *
118 * @param blob FDT blob
119 * @param node Node to display
120 * @return 0 if ok, or FDT_ERR... if not.
121 */
122static int list_properties(const void *blob, int node)
123{
124 const struct fdt_property *data;
125 const char *name;
126 int prop;
127
128 prop = fdt_first_property_offset(blob, node);
129 do {
130 /* Stop silently when there are no more properties */
131 if (prop < 0)
132 return prop == -FDT_ERR_NOTFOUND ? 0 : prop;
133 data = fdt_get_property_by_offset(blob, prop, NULL);
134 name = fdt_string(blob, fdt32_to_cpu(data->nameoff));
135 if (name)
136 puts(name);
137 prop = fdt_next_property_offset(blob, prop);
138 } while (1);
139}
140
Simon Glass16c99ee2012-03-06 16:41:46 -0800141#define MAX_LEVEL 32 /* how deeply nested we will go */
142
143/**
144 * List all subnodes in a node, one per line
145 *
146 * @param blob FDT blob
147 * @param node Node to display
148 * @return 0 if ok, or FDT_ERR... if not.
149 */
150static int list_subnodes(const void *blob, int node)
151{
152 int nextoffset; /* next node offset from libfdt */
153 uint32_t tag; /* current tag */
154 int level = 0; /* keep track of nesting level */
155 const char *pathp;
156 int depth = 1; /* the assumed depth of this node */
157
158 while (level >= 0) {
159 tag = fdt_next_tag(blob, node, &nextoffset);
160 switch (tag) {
161 case FDT_BEGIN_NODE:
162 pathp = fdt_get_name(blob, node, NULL);
163 if (level <= depth) {
164 if (pathp == NULL)
165 pathp = "/* NULL pointer error */";
166 if (*pathp == '\0')
167 pathp = "/"; /* root is nameless */
168 if (level == 1)
169 puts(pathp);
170 }
171 level++;
172 if (level >= MAX_LEVEL) {
173 printf("Nested too deep, aborting.\n");
174 return 1;
175 }
176 break;
177 case FDT_END_NODE:
178 level--;
179 if (level == 0)
180 level = -1; /* exit the loop */
181 break;
182 case FDT_END:
183 return 1;
184 case FDT_PROP:
185 break;
186 default:
187 if (level <= depth)
188 printf("Unknown tag 0x%08X\n", tag);
189 return 1;
190 }
191 node = nextoffset;
192 }
193 return 0;
194}
195
Simon Glass30eb2012012-03-02 17:12:08 -0800196/**
Simon Glass68d057f2012-01-21 10:14:47 -0800197 * Show the data for a given node (and perhaps property) according to the
198 * display option provided.
199 *
200 * @param blob FDT blob
201 * @param disp Display information / options
202 * @param node Node to display
203 * @param property Name of property to display, or NULL if none
204 * @return 0 if ok, -ve on error
205 */
206static int show_data_for_item(const void *blob, struct display_info *disp,
207 int node, const char *property)
208{
209 const void *value = NULL;
210 int len, err = 0;
211
Simon Glass16c99ee2012-03-06 16:41:46 -0800212 switch (disp->mode) {
213 case MODE_LIST_PROPS:
214 err = list_properties(blob, node);
215 break;
Simon Glass30eb2012012-03-02 17:12:08 -0800216
Simon Glass16c99ee2012-03-06 16:41:46 -0800217 case MODE_LIST_SUBNODES:
218 err = list_subnodes(blob, node);
219 break;
220
221 default:
222 assert(property);
223 value = fdt_getprop(blob, node, property, &len);
224 if (value) {
225 if (show_data(disp, value, len))
226 err = -1;
227 else
228 printf("\n");
Simon Glass7fcbef22012-03-06 16:41:47 -0800229 } else if (disp->default_val) {
230 puts(disp->default_val);
Simon Glass16c99ee2012-03-06 16:41:46 -0800231 } else {
232 report_error(property, len);
Simon Glass68d057f2012-01-21 10:14:47 -0800233 err = -1;
Simon Glass16c99ee2012-03-06 16:41:46 -0800234 }
235 break;
Simon Glass68d057f2012-01-21 10:14:47 -0800236 }
Simon Glass16c99ee2012-03-06 16:41:46 -0800237
Simon Glass68d057f2012-01-21 10:14:47 -0800238 return err;
239}
240
241/**
242 * Run the main fdtget operation, given a filename and valid arguments
243 *
244 * @param disp Display information / options
245 * @param filename Filename of blob file
246 * @param arg List of arguments to process
247 * @param arg_count Number of arguments
Nicolas Iooss194d5ca2017-03-04 14:26:46 +0100248 * @return 0 if ok, -ve on error
Simon Glass68d057f2012-01-21 10:14:47 -0800249 */
250static int do_fdtget(struct display_info *disp, const char *filename,
Simon Glass30eb2012012-03-02 17:12:08 -0800251 char **arg, int arg_count, int args_per_step)
Simon Glass68d057f2012-01-21 10:14:47 -0800252{
253 char *blob;
Simon Glass30eb2012012-03-02 17:12:08 -0800254 const char *prop;
Simon Glass68d057f2012-01-21 10:14:47 -0800255 int i, node;
256
Jon Loeliger5543b882013-04-22 15:41:41 -0500257 blob = utilfdt_read(filename);
Simon Glass68d057f2012-01-21 10:14:47 -0800258 if (!blob)
259 return -1;
260
Simon Glass30eb2012012-03-02 17:12:08 -0800261 for (i = 0; i + args_per_step <= arg_count; i += args_per_step) {
Simon Glass097ec972012-03-02 17:12:07 -0800262 node = fdt_path_offset(blob, arg[i]);
Simon Glass68d057f2012-01-21 10:14:47 -0800263 if (node < 0) {
Simon Glass7fcbef22012-03-06 16:41:47 -0800264 if (disp->default_val) {
265 puts(disp->default_val);
266 continue;
267 } else {
268 report_error(arg[i], node);
Jean-Christophe Duboisf79ddb82016-07-13 00:36:08 +0200269 free(blob);
Simon Glass7fcbef22012-03-06 16:41:47 -0800270 return -1;
271 }
Simon Glass68d057f2012-01-21 10:14:47 -0800272 }
Simon Glass30eb2012012-03-02 17:12:08 -0800273 prop = args_per_step == 1 ? NULL : arg[i + 1];
Simon Glass68d057f2012-01-21 10:14:47 -0800274
Jean-Christophe Duboisf79ddb82016-07-13 00:36:08 +0200275 if (show_data_for_item(blob, disp, node, prop)) {
276 free(blob);
Simon Glass68d057f2012-01-21 10:14:47 -0800277 return -1;
Jean-Christophe Duboisf79ddb82016-07-13 00:36:08 +0200278 }
Simon Glass68d057f2012-01-21 10:14:47 -0800279 }
Jean-Christophe Duboisf79ddb82016-07-13 00:36:08 +0200280
281 free(blob);
282
Simon Glass68d057f2012-01-21 10:14:47 -0800283 return 0;
284}
285
Mike Frysinger03449b82013-05-24 18:02:35 +1000286/* Usage related data. */
287static const char usage_synopsis[] =
288 "read values from device tree\n"
Simon Glass68d057f2012-01-21 10:14:47 -0800289 " fdtget <options> <dt file> [<node> <property>]...\n"
Simon Glass30eb2012012-03-02 17:12:08 -0800290 " fdtget -p <options> <dt file> [<node> ]...\n"
Mike Frysinger03449b82013-05-24 18:02:35 +1000291 "\n"
292 "Each value is printed on a new line.\n"
Simon Glass68d057f2012-01-21 10:14:47 -0800293 USAGE_TYPE_MSG;
Mike Frysinger03449b82013-05-24 18:02:35 +1000294static const char usage_short_opts[] = "t:pld:" USAGE_COMMON_SHORT_OPTS;
295static struct option const usage_long_opts[] = {
296 {"type", a_argument, NULL, 't'},
297 {"properties", no_argument, NULL, 'p'},
298 {"list", no_argument, NULL, 'l'},
299 {"default", a_argument, NULL, 'd'},
300 USAGE_COMMON_LONG_OPTS,
301};
302static const char * const usage_opts_help[] = {
303 "Type of data",
304 "List properties for each node",
305 "List subnodes for each node",
306 "Default value to display when the property is missing",
307 USAGE_COMMON_OPTS_HELP
308};
Simon Glass68d057f2012-01-21 10:14:47 -0800309
310int main(int argc, char *argv[])
311{
Mike Frysinger03449b82013-05-24 18:02:35 +1000312 int opt;
Simon Glass68d057f2012-01-21 10:14:47 -0800313 char *filename = NULL;
314 struct display_info disp;
Simon Glass30eb2012012-03-02 17:12:08 -0800315 int args_per_step = 2;
Simon Glass68d057f2012-01-21 10:14:47 -0800316
317 /* set defaults */
318 memset(&disp, '\0', sizeof(disp));
319 disp.size = -1;
Simon Glass30eb2012012-03-02 17:12:08 -0800320 disp.mode = MODE_SHOW_VALUE;
Mike Frysinger03449b82013-05-24 18:02:35 +1000321 while ((opt = util_getopt_long()) != EOF) {
322 switch (opt) {
323 case_USAGE_COMMON_FLAGS
Simon Glass68d057f2012-01-21 10:14:47 -0800324
325 case 't':
326 if (utilfdt_decode_type(optarg, &disp.type,
327 &disp.size))
Mike Frysingerb9e80652013-05-24 18:04:43 +1000328 usage("invalid type string");
Simon Glass68d057f2012-01-21 10:14:47 -0800329 break;
Simon Glass30eb2012012-03-02 17:12:08 -0800330
331 case 'p':
332 disp.mode = MODE_LIST_PROPS;
333 args_per_step = 1;
334 break;
Simon Glass16c99ee2012-03-06 16:41:46 -0800335
336 case 'l':
337 disp.mode = MODE_LIST_SUBNODES;
338 args_per_step = 1;
339 break;
Simon Glass7fcbef22012-03-06 16:41:47 -0800340
341 case 'd':
342 disp.default_val = optarg;
343 break;
Simon Glass68d057f2012-01-21 10:14:47 -0800344 }
345 }
346
347 if (optind < argc)
348 filename = argv[optind++];
349 if (!filename)
Mike Frysingerb9e80652013-05-24 18:04:43 +1000350 usage("missing filename");
Simon Glass68d057f2012-01-21 10:14:47 -0800351
352 argv += optind;
353 argc -= optind;
354
355 /* Allow no arguments, and silently succeed */
356 if (!argc)
357 return 0;
358
359 /* Check for node, property arguments */
Simon Glass30eb2012012-03-02 17:12:08 -0800360 if (args_per_step == 2 && (argc % 2))
Mike Frysingerb9e80652013-05-24 18:04:43 +1000361 usage("must have an even number of arguments");
Simon Glass68d057f2012-01-21 10:14:47 -0800362
Simon Glass30eb2012012-03-02 17:12:08 -0800363 if (do_fdtget(&disp, filename, argv, argc, args_per_step))
Simon Glass68d057f2012-01-21 10:14:47 -0800364 return 1;
365 return 0;
366}