blob: 34d819444dfb7da2e10ba1d29325722b1194b11b [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/**
Simon Glassfe50bd12017-08-20 06:28:54 -060057 * Shows a list of cells in the requested format
58 *
59 * @param disp Display information / options
60 * @param data Data to display
61 * @param len Maximum length of buffer
62 * @param size Data size to use for display (e.g. 4 for 32-bit)
63 * @return 0 if ok, -1 on error
64 */
65static int show_cell_list(struct display_info *disp, const char *data, int len,
66 int size)
67{
68 const uint8_t *p = (const uint8_t *)data;
69 char fmt[3];
70 int value;
71 int i;
72
73 fmt[0] = '%';
74 fmt[1] = disp->type ? disp->type : 'd';
75 fmt[2] = '\0';
76 for (i = 0; i < len; i += size, p += size) {
77 if (i)
78 printf(" ");
79 value = size == 4 ? fdt32_to_cpu(*(const fdt32_t *)p) :
80 size == 2 ? (*p << 8) | p[1] : *p;
81 printf(fmt, value);
82 }
83
84 return 0;
85}
86
87/**
Simon Glass68d057f2012-01-21 10:14:47 -080088 * Displays data of a given length according to selected options
89 *
90 * If a specific data type is provided in disp, then this is used. Otherwise
91 * we try to guess the data type / size from the contents.
92 *
93 * @param disp Display information / options
94 * @param data Data to display
95 * @param len Maximum length of buffer
96 * @return 0 if ok, -1 if data does not match format
97 */
98static int show_data(struct display_info *disp, const char *data, int len)
99{
Simon Glassfe50bd12017-08-20 06:28:54 -0600100 int size;
Simon Glass68d057f2012-01-21 10:14:47 -0800101 const char *s;
Simon Glass68d057f2012-01-21 10:14:47 -0800102 int is_string;
Simon Glass68d057f2012-01-21 10:14:47 -0800103
104 /* no data, don't print */
105 if (len == 0)
106 return 0;
107
108 is_string = (disp->type) == 's' ||
109 (!disp->type && util_is_printable_string(data, len));
110 if (is_string) {
111 if (data[len - 1] != '\0') {
112 fprintf(stderr, "Unterminated string\n");
113 return -1;
114 }
115 for (s = data; s - data < len; s += strlen(s) + 1) {
116 if (s != data)
117 printf(" ");
118 printf("%s", (const char *)s);
119 }
120 return 0;
121 }
122 size = disp->size;
David Gibson53d6ca62012-02-03 16:12:02 +1100123 if (size == -1) {
Simon Glass68d057f2012-01-21 10:14:47 -0800124 size = (len % 4) == 0 ? 4 : 1;
David Gibson53d6ca62012-02-03 16:12:02 +1100125 } else if (len % size) {
Simon Glass68d057f2012-01-21 10:14:47 -0800126 fprintf(stderr, "Property length must be a multiple of "
127 "selected data size\n");
128 return -1;
129 }
Simon Glassfe50bd12017-08-20 06:28:54 -0600130
131 return show_cell_list(disp, data, len, size);
Simon Glass68d057f2012-01-21 10:14:47 -0800132}
133
134/**
Simon Glass30eb2012012-03-02 17:12:08 -0800135 * List all properties in a node, one per line.
136 *
137 * @param blob FDT blob
138 * @param node Node to display
139 * @return 0 if ok, or FDT_ERR... if not.
140 */
141static int list_properties(const void *blob, int node)
142{
Simon Glass30eb2012012-03-02 17:12:08 -0800143 const char *name;
144 int prop;
145
146 prop = fdt_first_property_offset(blob, node);
147 do {
148 /* Stop silently when there are no more properties */
149 if (prop < 0)
150 return prop == -FDT_ERR_NOTFOUND ? 0 : prop;
Nathan Whitehornf1879e12018-01-25 05:13:40 +0000151 fdt_getprop_by_offset(blob, prop, &name, NULL);
Simon Glass30eb2012012-03-02 17:12:08 -0800152 if (name)
153 puts(name);
154 prop = fdt_next_property_offset(blob, prop);
155 } while (1);
156}
157
Simon Glass16c99ee2012-03-06 16:41:46 -0800158#define MAX_LEVEL 32 /* how deeply nested we will go */
159
160/**
161 * List all subnodes in a node, one per line
162 *
163 * @param blob FDT blob
164 * @param node Node to display
165 * @return 0 if ok, or FDT_ERR... if not.
166 */
167static int list_subnodes(const void *blob, int node)
168{
169 int nextoffset; /* next node offset from libfdt */
170 uint32_t tag; /* current tag */
171 int level = 0; /* keep track of nesting level */
172 const char *pathp;
173 int depth = 1; /* the assumed depth of this node */
174
175 while (level >= 0) {
176 tag = fdt_next_tag(blob, node, &nextoffset);
177 switch (tag) {
178 case FDT_BEGIN_NODE:
179 pathp = fdt_get_name(blob, node, NULL);
180 if (level <= depth) {
181 if (pathp == NULL)
182 pathp = "/* NULL pointer error */";
183 if (*pathp == '\0')
184 pathp = "/"; /* root is nameless */
185 if (level == 1)
186 puts(pathp);
187 }
188 level++;
189 if (level >= MAX_LEVEL) {
190 printf("Nested too deep, aborting.\n");
191 return 1;
192 }
193 break;
194 case FDT_END_NODE:
195 level--;
196 if (level == 0)
197 level = -1; /* exit the loop */
198 break;
199 case FDT_END:
200 return 1;
201 case FDT_PROP:
202 break;
203 default:
204 if (level <= depth)
205 printf("Unknown tag 0x%08X\n", tag);
206 return 1;
207 }
208 node = nextoffset;
209 }
210 return 0;
211}
212
Simon Glass30eb2012012-03-02 17:12:08 -0800213/**
Simon Glass68d057f2012-01-21 10:14:47 -0800214 * Show the data for a given node (and perhaps property) according to the
215 * display option provided.
216 *
217 * @param blob FDT blob
218 * @param disp Display information / options
219 * @param node Node to display
220 * @param property Name of property to display, or NULL if none
221 * @return 0 if ok, -ve on error
222 */
223static int show_data_for_item(const void *blob, struct display_info *disp,
224 int node, const char *property)
225{
226 const void *value = NULL;
227 int len, err = 0;
228
Simon Glass16c99ee2012-03-06 16:41:46 -0800229 switch (disp->mode) {
230 case MODE_LIST_PROPS:
231 err = list_properties(blob, node);
232 break;
Simon Glass30eb2012012-03-02 17:12:08 -0800233
Simon Glass16c99ee2012-03-06 16:41:46 -0800234 case MODE_LIST_SUBNODES:
235 err = list_subnodes(blob, node);
236 break;
237
238 default:
239 assert(property);
240 value = fdt_getprop(blob, node, property, &len);
241 if (value) {
242 if (show_data(disp, value, len))
243 err = -1;
244 else
245 printf("\n");
Simon Glass7fcbef22012-03-06 16:41:47 -0800246 } else if (disp->default_val) {
247 puts(disp->default_val);
Simon Glass16c99ee2012-03-06 16:41:46 -0800248 } else {
249 report_error(property, len);
Simon Glass68d057f2012-01-21 10:14:47 -0800250 err = -1;
Simon Glass16c99ee2012-03-06 16:41:46 -0800251 }
252 break;
Simon Glass68d057f2012-01-21 10:14:47 -0800253 }
Simon Glass16c99ee2012-03-06 16:41:46 -0800254
Simon Glass68d057f2012-01-21 10:14:47 -0800255 return err;
256}
257
258/**
259 * Run the main fdtget operation, given a filename and valid arguments
260 *
261 * @param disp Display information / options
262 * @param filename Filename of blob file
263 * @param arg List of arguments to process
264 * @param arg_count Number of arguments
Nicolas Iooss194d5ca2017-03-04 14:26:46 +0100265 * @return 0 if ok, -ve on error
Simon Glass68d057f2012-01-21 10:14:47 -0800266 */
267static int do_fdtget(struct display_info *disp, const char *filename,
Simon Glass30eb2012012-03-02 17:12:08 -0800268 char **arg, int arg_count, int args_per_step)
Simon Glass68d057f2012-01-21 10:14:47 -0800269{
270 char *blob;
Simon Glass30eb2012012-03-02 17:12:08 -0800271 const char *prop;
Simon Glass68d057f2012-01-21 10:14:47 -0800272 int i, node;
273
Jon Loeliger5543b882013-04-22 15:41:41 -0500274 blob = utilfdt_read(filename);
Simon Glass68d057f2012-01-21 10:14:47 -0800275 if (!blob)
276 return -1;
277
Simon Glass30eb2012012-03-02 17:12:08 -0800278 for (i = 0; i + args_per_step <= arg_count; i += args_per_step) {
Simon Glass097ec972012-03-02 17:12:07 -0800279 node = fdt_path_offset(blob, arg[i]);
Simon Glass68d057f2012-01-21 10:14:47 -0800280 if (node < 0) {
Simon Glass7fcbef22012-03-06 16:41:47 -0800281 if (disp->default_val) {
282 puts(disp->default_val);
283 continue;
284 } else {
285 report_error(arg[i], node);
Jean-Christophe Duboisf79ddb82016-07-13 00:36:08 +0200286 free(blob);
Simon Glass7fcbef22012-03-06 16:41:47 -0800287 return -1;
288 }
Simon Glass68d057f2012-01-21 10:14:47 -0800289 }
Simon Glass30eb2012012-03-02 17:12:08 -0800290 prop = args_per_step == 1 ? NULL : arg[i + 1];
Simon Glass68d057f2012-01-21 10:14:47 -0800291
Jean-Christophe Duboisf79ddb82016-07-13 00:36:08 +0200292 if (show_data_for_item(blob, disp, node, prop)) {
293 free(blob);
Simon Glass68d057f2012-01-21 10:14:47 -0800294 return -1;
Jean-Christophe Duboisf79ddb82016-07-13 00:36:08 +0200295 }
Simon Glass68d057f2012-01-21 10:14:47 -0800296 }
Jean-Christophe Duboisf79ddb82016-07-13 00:36:08 +0200297
298 free(blob);
299
Simon Glass68d057f2012-01-21 10:14:47 -0800300 return 0;
301}
302
Mike Frysinger03449b82013-05-24 18:02:35 +1000303/* Usage related data. */
304static const char usage_synopsis[] =
305 "read values from device tree\n"
Simon Glass68d057f2012-01-21 10:14:47 -0800306 " fdtget <options> <dt file> [<node> <property>]...\n"
Simon Glass30eb2012012-03-02 17:12:08 -0800307 " fdtget -p <options> <dt file> [<node> ]...\n"
Mike Frysinger03449b82013-05-24 18:02:35 +1000308 "\n"
309 "Each value is printed on a new line.\n"
Simon Glass68d057f2012-01-21 10:14:47 -0800310 USAGE_TYPE_MSG;
Mike Frysinger03449b82013-05-24 18:02:35 +1000311static const char usage_short_opts[] = "t:pld:" USAGE_COMMON_SHORT_OPTS;
312static struct option const usage_long_opts[] = {
313 {"type", a_argument, NULL, 't'},
314 {"properties", no_argument, NULL, 'p'},
315 {"list", no_argument, NULL, 'l'},
316 {"default", a_argument, NULL, 'd'},
317 USAGE_COMMON_LONG_OPTS,
318};
319static const char * const usage_opts_help[] = {
320 "Type of data",
321 "List properties for each node",
322 "List subnodes for each node",
323 "Default value to display when the property is missing",
324 USAGE_COMMON_OPTS_HELP
325};
Simon Glass68d057f2012-01-21 10:14:47 -0800326
327int main(int argc, char *argv[])
328{
Mike Frysinger03449b82013-05-24 18:02:35 +1000329 int opt;
Simon Glass68d057f2012-01-21 10:14:47 -0800330 char *filename = NULL;
331 struct display_info disp;
Simon Glass30eb2012012-03-02 17:12:08 -0800332 int args_per_step = 2;
Simon Glass68d057f2012-01-21 10:14:47 -0800333
334 /* set defaults */
335 memset(&disp, '\0', sizeof(disp));
336 disp.size = -1;
Simon Glass30eb2012012-03-02 17:12:08 -0800337 disp.mode = MODE_SHOW_VALUE;
Mike Frysinger03449b82013-05-24 18:02:35 +1000338 while ((opt = util_getopt_long()) != EOF) {
339 switch (opt) {
340 case_USAGE_COMMON_FLAGS
Simon Glass68d057f2012-01-21 10:14:47 -0800341
342 case 't':
343 if (utilfdt_decode_type(optarg, &disp.type,
344 &disp.size))
Mike Frysingerb9e80652013-05-24 18:04:43 +1000345 usage("invalid type string");
Simon Glass68d057f2012-01-21 10:14:47 -0800346 break;
Simon Glass30eb2012012-03-02 17:12:08 -0800347
348 case 'p':
349 disp.mode = MODE_LIST_PROPS;
350 args_per_step = 1;
351 break;
Simon Glass16c99ee2012-03-06 16:41:46 -0800352
353 case 'l':
354 disp.mode = MODE_LIST_SUBNODES;
355 args_per_step = 1;
356 break;
Simon Glass7fcbef22012-03-06 16:41:47 -0800357
358 case 'd':
359 disp.default_val = optarg;
360 break;
Simon Glass68d057f2012-01-21 10:14:47 -0800361 }
362 }
363
364 if (optind < argc)
365 filename = argv[optind++];
366 if (!filename)
Mike Frysingerb9e80652013-05-24 18:04:43 +1000367 usage("missing filename");
Simon Glass68d057f2012-01-21 10:14:47 -0800368
369 argv += optind;
370 argc -= optind;
371
372 /* Allow no arguments, and silently succeed */
373 if (!argc)
374 return 0;
375
376 /* Check for node, property arguments */
Simon Glass30eb2012012-03-02 17:12:08 -0800377 if (args_per_step == 2 && (argc % 2))
Mike Frysingerb9e80652013-05-24 18:04:43 +1000378 usage("must have an even number of arguments");
Simon Glass68d057f2012-01-21 10:14:47 -0800379
Simon Glass30eb2012012-03-02 17:12:08 -0800380 if (do_fdtget(&disp, filename, argv, argc, args_per_step))
Simon Glass68d057f2012-01-21 10:14:47 -0800381 return 1;
382 return 0;
383}