1*1a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2cd296721SStephen Warren /*
3cd296721SStephen Warren * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
4cd296721SStephen Warren *
5cd296721SStephen Warren * Portions from U-Boot cmd_fdt.c (C) Copyright 2007
6cd296721SStephen Warren * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
7cd296721SStephen Warren * Based on code written by:
8cd296721SStephen Warren * Pantelis Antoniou <pantelis.antoniou@gmail.com> and
9cd296721SStephen Warren * Matthew McClintock <msm@freescale.com>
10cd296721SStephen Warren */
11cd296721SStephen Warren
12cd296721SStephen Warren #include <assert.h>
13cd296721SStephen Warren #include <ctype.h>
14cd296721SStephen Warren #include <getopt.h>
15cd296721SStephen Warren #include <stdio.h>
16cd296721SStephen Warren #include <stdlib.h>
17cd296721SStephen Warren #include <string.h>
18cd296721SStephen Warren
19cd296721SStephen Warren #include <libfdt.h>
20cd296721SStephen Warren
21cd296721SStephen Warren #include "util.h"
22cd296721SStephen Warren
23cd296721SStephen Warren enum display_mode {
24cd296721SStephen Warren MODE_SHOW_VALUE, /* show values for node properties */
25cd296721SStephen Warren MODE_LIST_PROPS, /* list the properties for a node */
26cd296721SStephen Warren MODE_LIST_SUBNODES, /* list the subnodes of a node */
27cd296721SStephen Warren };
28cd296721SStephen Warren
29cd296721SStephen Warren /* Holds information which controls our output and options */
30cd296721SStephen Warren struct display_info {
31cd296721SStephen Warren int type; /* data type (s/i/u/x or 0 for default) */
32cd296721SStephen Warren int size; /* data size (1/2/4) */
33cd296721SStephen Warren enum display_mode mode; /* display mode that we are using */
34cd296721SStephen Warren const char *default_val; /* default value if node/property not found */
35cd296721SStephen Warren };
36cd296721SStephen Warren
report_error(const char * where,int err)37cd296721SStephen Warren static void report_error(const char *where, int err)
38cd296721SStephen Warren {
39cd296721SStephen Warren fprintf(stderr, "Error at '%s': %s\n", where, fdt_strerror(err));
40cd296721SStephen Warren }
41cd296721SStephen Warren
42cd296721SStephen Warren /**
43cd296721SStephen Warren * Displays data of a given length according to selected options
44cd296721SStephen Warren *
45cd296721SStephen Warren * If a specific data type is provided in disp, then this is used. Otherwise
46cd296721SStephen Warren * we try to guess the data type / size from the contents.
47cd296721SStephen Warren *
48cd296721SStephen Warren * @param disp Display information / options
49cd296721SStephen Warren * @param data Data to display
50cd296721SStephen Warren * @param len Maximum length of buffer
51cd296721SStephen Warren * @return 0 if ok, -1 if data does not match format
52cd296721SStephen Warren */
show_data(struct display_info * disp,const char * data,int len)53cd296721SStephen Warren static int show_data(struct display_info *disp, const char *data, int len)
54cd296721SStephen Warren {
55cd296721SStephen Warren int i, size;
56cd296721SStephen Warren const uint8_t *p = (const uint8_t *)data;
57cd296721SStephen Warren const char *s;
58cd296721SStephen Warren int value;
59cd296721SStephen Warren int is_string;
60cd296721SStephen Warren char fmt[3];
61cd296721SStephen Warren
62cd296721SStephen Warren /* no data, don't print */
63cd296721SStephen Warren if (len == 0)
64cd296721SStephen Warren return 0;
65cd296721SStephen Warren
66cd296721SStephen Warren is_string = (disp->type) == 's' ||
67cd296721SStephen Warren (!disp->type && util_is_printable_string(data, len));
68cd296721SStephen Warren if (is_string) {
69cd296721SStephen Warren if (data[len - 1] != '\0') {
70cd296721SStephen Warren fprintf(stderr, "Unterminated string\n");
71cd296721SStephen Warren return -1;
72cd296721SStephen Warren }
73cd296721SStephen Warren for (s = data; s - data < len; s += strlen(s) + 1) {
74cd296721SStephen Warren if (s != data)
75cd296721SStephen Warren printf(" ");
76cd296721SStephen Warren printf("%s", (const char *)s);
77cd296721SStephen Warren }
78cd296721SStephen Warren return 0;
79cd296721SStephen Warren }
80cd296721SStephen Warren size = disp->size;
81cd296721SStephen Warren if (size == -1) {
82cd296721SStephen Warren size = (len % 4) == 0 ? 4 : 1;
83cd296721SStephen Warren } else if (len % size) {
84cd296721SStephen Warren fprintf(stderr, "Property length must be a multiple of "
85cd296721SStephen Warren "selected data size\n");
86cd296721SStephen Warren return -1;
87cd296721SStephen Warren }
88cd296721SStephen Warren fmt[0] = '%';
89cd296721SStephen Warren fmt[1] = disp->type ? disp->type : 'd';
90cd296721SStephen Warren fmt[2] = '\0';
91cd296721SStephen Warren for (i = 0; i < len; i += size, p += size) {
92cd296721SStephen Warren if (i)
93cd296721SStephen Warren printf(" ");
94cd296721SStephen Warren value = size == 4 ? fdt32_to_cpu(*(const uint32_t *)p) :
95cd296721SStephen Warren size == 2 ? (*p << 8) | p[1] : *p;
96cd296721SStephen Warren printf(fmt, value);
97cd296721SStephen Warren }
98cd296721SStephen Warren return 0;
99cd296721SStephen Warren }
100cd296721SStephen Warren
101cd296721SStephen Warren /**
102cd296721SStephen Warren * List all properties in a node, one per line.
103cd296721SStephen Warren *
104cd296721SStephen Warren * @param blob FDT blob
105cd296721SStephen Warren * @param node Node to display
106cd296721SStephen Warren * @return 0 if ok, or FDT_ERR... if not.
107cd296721SStephen Warren */
list_properties(const void * blob,int node)108cd296721SStephen Warren static int list_properties(const void *blob, int node)
109cd296721SStephen Warren {
110cd296721SStephen Warren const struct fdt_property *data;
111cd296721SStephen Warren const char *name;
112cd296721SStephen Warren int prop;
113cd296721SStephen Warren
114cd296721SStephen Warren prop = fdt_first_property_offset(blob, node);
115cd296721SStephen Warren do {
116cd296721SStephen Warren /* Stop silently when there are no more properties */
117cd296721SStephen Warren if (prop < 0)
118cd296721SStephen Warren return prop == -FDT_ERR_NOTFOUND ? 0 : prop;
119cd296721SStephen Warren data = fdt_get_property_by_offset(blob, prop, NULL);
120cd296721SStephen Warren name = fdt_string(blob, fdt32_to_cpu(data->nameoff));
121cd296721SStephen Warren if (name)
122cd296721SStephen Warren puts(name);
123cd296721SStephen Warren prop = fdt_next_property_offset(blob, prop);
124cd296721SStephen Warren } while (1);
125cd296721SStephen Warren }
126cd296721SStephen Warren
127cd296721SStephen Warren #define MAX_LEVEL 32 /* how deeply nested we will go */
128cd296721SStephen Warren
129cd296721SStephen Warren /**
130cd296721SStephen Warren * List all subnodes in a node, one per line
131cd296721SStephen Warren *
132cd296721SStephen Warren * @param blob FDT blob
133cd296721SStephen Warren * @param node Node to display
134cd296721SStephen Warren * @return 0 if ok, or FDT_ERR... if not.
135cd296721SStephen Warren */
list_subnodes(const void * blob,int node)136cd296721SStephen Warren static int list_subnodes(const void *blob, int node)
137cd296721SStephen Warren {
138cd296721SStephen Warren int nextoffset; /* next node offset from libfdt */
139cd296721SStephen Warren uint32_t tag; /* current tag */
140cd296721SStephen Warren int level = 0; /* keep track of nesting level */
141cd296721SStephen Warren const char *pathp;
142cd296721SStephen Warren int depth = 1; /* the assumed depth of this node */
143cd296721SStephen Warren
144cd296721SStephen Warren while (level >= 0) {
145cd296721SStephen Warren tag = fdt_next_tag(blob, node, &nextoffset);
146cd296721SStephen Warren switch (tag) {
147cd296721SStephen Warren case FDT_BEGIN_NODE:
148cd296721SStephen Warren pathp = fdt_get_name(blob, node, NULL);
149cd296721SStephen Warren if (level <= depth) {
150cd296721SStephen Warren if (pathp == NULL)
151cd296721SStephen Warren pathp = "/* NULL pointer error */";
152cd296721SStephen Warren if (*pathp == '\0')
153cd296721SStephen Warren pathp = "/"; /* root is nameless */
154cd296721SStephen Warren if (level == 1)
155cd296721SStephen Warren puts(pathp);
156cd296721SStephen Warren }
157cd296721SStephen Warren level++;
158cd296721SStephen Warren if (level >= MAX_LEVEL) {
159cd296721SStephen Warren printf("Nested too deep, aborting.\n");
160cd296721SStephen Warren return 1;
161cd296721SStephen Warren }
162cd296721SStephen Warren break;
163cd296721SStephen Warren case FDT_END_NODE:
164cd296721SStephen Warren level--;
165cd296721SStephen Warren if (level == 0)
166cd296721SStephen Warren level = -1; /* exit the loop */
167cd296721SStephen Warren break;
168cd296721SStephen Warren case FDT_END:
169cd296721SStephen Warren return 1;
170cd296721SStephen Warren case FDT_PROP:
171cd296721SStephen Warren break;
172cd296721SStephen Warren default:
173cd296721SStephen Warren if (level <= depth)
174cd296721SStephen Warren printf("Unknown tag 0x%08X\n", tag);
175cd296721SStephen Warren return 1;
176cd296721SStephen Warren }
177cd296721SStephen Warren node = nextoffset;
178cd296721SStephen Warren }
179cd296721SStephen Warren return 0;
180cd296721SStephen Warren }
181cd296721SStephen Warren
182cd296721SStephen Warren /**
183cd296721SStephen Warren * Show the data for a given node (and perhaps property) according to the
184cd296721SStephen Warren * display option provided.
185cd296721SStephen Warren *
186cd296721SStephen Warren * @param blob FDT blob
187cd296721SStephen Warren * @param disp Display information / options
188cd296721SStephen Warren * @param node Node to display
189cd296721SStephen Warren * @param property Name of property to display, or NULL if none
190cd296721SStephen Warren * @return 0 if ok, -ve on error
191cd296721SStephen Warren */
show_data_for_item(const void * blob,struct display_info * disp,int node,const char * property)192cd296721SStephen Warren static int show_data_for_item(const void *blob, struct display_info *disp,
193cd296721SStephen Warren int node, const char *property)
194cd296721SStephen Warren {
195cd296721SStephen Warren const void *value = NULL;
196cd296721SStephen Warren int len, err = 0;
197cd296721SStephen Warren
198cd296721SStephen Warren switch (disp->mode) {
199cd296721SStephen Warren case MODE_LIST_PROPS:
200cd296721SStephen Warren err = list_properties(blob, node);
201cd296721SStephen Warren break;
202cd296721SStephen Warren
203cd296721SStephen Warren case MODE_LIST_SUBNODES:
204cd296721SStephen Warren err = list_subnodes(blob, node);
205cd296721SStephen Warren break;
206cd296721SStephen Warren
207cd296721SStephen Warren default:
208cd296721SStephen Warren assert(property);
209cd296721SStephen Warren value = fdt_getprop(blob, node, property, &len);
210cd296721SStephen Warren if (value) {
211cd296721SStephen Warren if (show_data(disp, value, len))
212cd296721SStephen Warren err = -1;
213cd296721SStephen Warren else
214cd296721SStephen Warren printf("\n");
215cd296721SStephen Warren } else if (disp->default_val) {
216cd296721SStephen Warren puts(disp->default_val);
217cd296721SStephen Warren } else {
218cd296721SStephen Warren report_error(property, len);
219cd296721SStephen Warren err = -1;
220cd296721SStephen Warren }
221cd296721SStephen Warren break;
222cd296721SStephen Warren }
223cd296721SStephen Warren
224cd296721SStephen Warren return err;
225cd296721SStephen Warren }
226cd296721SStephen Warren
227cd296721SStephen Warren /**
228cd296721SStephen Warren * Run the main fdtget operation, given a filename and valid arguments
229cd296721SStephen Warren *
230cd296721SStephen Warren * @param disp Display information / options
231cd296721SStephen Warren * @param filename Filename of blob file
232cd296721SStephen Warren * @param arg List of arguments to process
233cd296721SStephen Warren * @param arg_count Number of arguments
234cd296721SStephen Warren * @param return 0 if ok, -ve on error
235cd296721SStephen Warren */
do_fdtget(struct display_info * disp,const char * filename,char ** arg,int arg_count,int args_per_step)236cd296721SStephen Warren static int do_fdtget(struct display_info *disp, const char *filename,
237cd296721SStephen Warren char **arg, int arg_count, int args_per_step)
238cd296721SStephen Warren {
239cd296721SStephen Warren char *blob;
240cd296721SStephen Warren const char *prop;
241cd296721SStephen Warren int i, node;
242cd296721SStephen Warren
243cd296721SStephen Warren blob = utilfdt_read(filename);
244cd296721SStephen Warren if (!blob)
245cd296721SStephen Warren return -1;
246cd296721SStephen Warren
247cd296721SStephen Warren for (i = 0; i + args_per_step <= arg_count; i += args_per_step) {
248cd296721SStephen Warren node = fdt_path_offset(blob, arg[i]);
249cd296721SStephen Warren if (node < 0) {
250cd296721SStephen Warren if (disp->default_val) {
251cd296721SStephen Warren puts(disp->default_val);
252cd296721SStephen Warren continue;
253cd296721SStephen Warren } else {
254cd296721SStephen Warren report_error(arg[i], node);
255cd296721SStephen Warren return -1;
256cd296721SStephen Warren }
257cd296721SStephen Warren }
258cd296721SStephen Warren prop = args_per_step == 1 ? NULL : arg[i + 1];
259cd296721SStephen Warren
260cd296721SStephen Warren if (show_data_for_item(blob, disp, node, prop))
261cd296721SStephen Warren return -1;
262cd296721SStephen Warren }
263cd296721SStephen Warren return 0;
264cd296721SStephen Warren }
265cd296721SStephen Warren
266cd296721SStephen Warren static const char *usage_msg =
267cd296721SStephen Warren "fdtget - read values from device tree\n"
268cd296721SStephen Warren "\n"
269cd296721SStephen Warren "Each value is printed on a new line.\n\n"
270cd296721SStephen Warren "Usage:\n"
271cd296721SStephen Warren " fdtget <options> <dt file> [<node> <property>]...\n"
272cd296721SStephen Warren " fdtget -p <options> <dt file> [<node> ]...\n"
273cd296721SStephen Warren "Options:\n"
274cd296721SStephen Warren "\t-t <type>\tType of data\n"
275cd296721SStephen Warren "\t-p\t\tList properties for each node\n"
276cd296721SStephen Warren "\t-l\t\tList subnodes for each node\n"
277cd296721SStephen Warren "\t-d\t\tDefault value to display when the property is "
278cd296721SStephen Warren "missing\n"
279cd296721SStephen Warren "\t-h\t\tPrint this help\n\n"
280cd296721SStephen Warren USAGE_TYPE_MSG;
281cd296721SStephen Warren
usage(const char * msg)282cd296721SStephen Warren static void usage(const char *msg)
283cd296721SStephen Warren {
284cd296721SStephen Warren if (msg)
285cd296721SStephen Warren fprintf(stderr, "Error: %s\n\n", msg);
286cd296721SStephen Warren
287cd296721SStephen Warren fprintf(stderr, "%s", usage_msg);
288cd296721SStephen Warren exit(2);
289cd296721SStephen Warren }
290cd296721SStephen Warren
main(int argc,char * argv[])291cd296721SStephen Warren int main(int argc, char *argv[])
292cd296721SStephen Warren {
293cd296721SStephen Warren char *filename = NULL;
294cd296721SStephen Warren struct display_info disp;
295cd296721SStephen Warren int args_per_step = 2;
296cd296721SStephen Warren
297cd296721SStephen Warren /* set defaults */
298cd296721SStephen Warren memset(&disp, '\0', sizeof(disp));
299cd296721SStephen Warren disp.size = -1;
300cd296721SStephen Warren disp.mode = MODE_SHOW_VALUE;
301cd296721SStephen Warren for (;;) {
302cd296721SStephen Warren int c = getopt(argc, argv, "d:hlpt:");
303cd296721SStephen Warren if (c == -1)
304cd296721SStephen Warren break;
305cd296721SStephen Warren
306cd296721SStephen Warren switch (c) {
307cd296721SStephen Warren case 'h':
308cd296721SStephen Warren case '?':
309cd296721SStephen Warren usage(NULL);
310cd296721SStephen Warren
311cd296721SStephen Warren case 't':
312cd296721SStephen Warren if (utilfdt_decode_type(optarg, &disp.type,
313cd296721SStephen Warren &disp.size))
314cd296721SStephen Warren usage("Invalid type string");
315cd296721SStephen Warren break;
316cd296721SStephen Warren
317cd296721SStephen Warren case 'p':
318cd296721SStephen Warren disp.mode = MODE_LIST_PROPS;
319cd296721SStephen Warren args_per_step = 1;
320cd296721SStephen Warren break;
321cd296721SStephen Warren
322cd296721SStephen Warren case 'l':
323cd296721SStephen Warren disp.mode = MODE_LIST_SUBNODES;
324cd296721SStephen Warren args_per_step = 1;
325cd296721SStephen Warren break;
326cd296721SStephen Warren
327cd296721SStephen Warren case 'd':
328cd296721SStephen Warren disp.default_val = optarg;
329cd296721SStephen Warren break;
330cd296721SStephen Warren }
331cd296721SStephen Warren }
332cd296721SStephen Warren
333cd296721SStephen Warren if (optind < argc)
334cd296721SStephen Warren filename = argv[optind++];
335cd296721SStephen Warren if (!filename)
336cd296721SStephen Warren usage("Missing filename");
337cd296721SStephen Warren
338cd296721SStephen Warren argv += optind;
339cd296721SStephen Warren argc -= optind;
340cd296721SStephen Warren
341cd296721SStephen Warren /* Allow no arguments, and silently succeed */
342cd296721SStephen Warren if (!argc)
343cd296721SStephen Warren return 0;
344cd296721SStephen Warren
345cd296721SStephen Warren /* Check for node, property arguments */
346cd296721SStephen Warren if (args_per_step == 2 && (argc % 2))
347cd296721SStephen Warren usage("Must have an even number of arguments");
348cd296721SStephen Warren
349cd296721SStephen Warren if (do_fdtget(&disp, filename, argv, argc, args_per_step))
350cd296721SStephen Warren return 1;
351cd296721SStephen Warren return 0;
352cd296721SStephen Warren }
353