fdtget.c revision 318102
111695Sache/*
211695Sache * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
311695Sache *
411695Sache * Portions from U-Boot cmd_fdt.c (C) Copyright 2007
511695Sache * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
611695Sache * Based on code written by:
711695Sache *   Pantelis Antoniou <pantelis.antoniou@gmail.com> and
8227753Stheraven *   Matthew McClintock <msm@freescale.com>
9227753Stheraven *
10227753Stheraven * This program is free software; you can redistribute it and/or
11227753Stheraven * modify it under the terms of the GNU General Public License as
12227753Stheraven * published by the Free Software Foundation; either version 2 of
1311695Sache * the License, or (at your option) any later version.
1411695Sache *
1511695Sache * This program is distributed in the hope that it will be useful,
1611695Sache * but WITHOUT ANY WARRANTY; without even the implied warranty of
1711695Sache * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1811695Sache * GNU General Public License for more details.
1911695Sache *
2011695Sache * You should have received a copy of the GNU General Public License
2111695Sache * along with this program; if not, write to the Free Software
2211695Sache * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
2311695Sache * MA 02111-1307 USA
2411695Sache */
2511695Sache
2611695Sache#include <assert.h>
2711695Sache#include <ctype.h>
2811695Sache#include <getopt.h>
2911695Sache#include <stdio.h>
3011695Sache#include <stdlib.h>
3111695Sache#include <string.h>
3211695Sache
3311695Sache#include <libfdt.h>
3411695Sache
3511695Sache#include "util.h"
3611695Sache
3711695Sacheenum display_mode {
3892986Sobrien	MODE_SHOW_VALUE,	/* show values for node properties */
3992986Sobrien	MODE_LIST_PROPS,	/* list the properties for a node */
4092986Sobrien	MODE_LIST_SUBNODES,	/* list the subnodes of a node */
41232498Stheraven};
42232498Stheraven
43136609Stjr/* Holds information which controls our output and options */
4411695Sachestruct display_info {
4511695Sache	int type;		/* data type (s/i/u/x or 0 for default) */
4611695Sache	int size;		/* data size (1/2/4) */
4711695Sache	enum display_mode mode;	/* display mode that we are using */
4811695Sache	const char *default_val; /* default value if node/property not found */
4924694Sache};
50121845Stjr
51117270Sachestatic void report_error(const char *where, int err)
52129153Stjr{
5322330Sache	fprintf(stderr, "Error at '%s': %s\n", where, fdt_strerror(err));
5411695Sache}
55232498Stheraven
56232498Stheraven/**
57232498Stheraven * Displays data of a given length according to selected options
58232498Stheraven *
59232498Stheraven * If a specific data type is provided in disp, then this is used. Otherwise
60232498Stheraven * we try to guess the data type / size from the contents.
61232498Stheraven *
62232498Stheraven * @param disp		Display information / options
63232498Stheraven * @param data		Data to display
64172619Sache * @param len		Maximum length of buffer
65172619Sache * @return 0 if ok, -1 if data does not match format
66115626Sache */
6711695Sachestatic int show_data(struct display_info *disp, const char *data, int len)
68227753Stheraven{
69117270Sache	int i, size;
70227753Stheraven	const uint8_t *p = (const uint8_t *)data;
71227753Stheraven	const char *s;
72227753Stheraven	int value;
73227753Stheraven	int is_string;
74227753Stheraven	char fmt[3];
75227753Stheraven
76227753Stheraven	/* no data, don't print */
77227753Stheraven	if (len == 0)
78227753Stheraven		return 0;
79227753Stheraven
80227753Stheraven	is_string = (disp->type) == 's' ||
81227753Stheraven		(!disp->type && util_is_printable_string(data, len));
82227753Stheraven	if (is_string) {
83227753Stheraven		if (data[len - 1] != '\0') {
84227753Stheraven			fprintf(stderr, "Unterminated string\n");
85227753Stheraven			return -1;
86232498Stheraven		}
87232498Stheraven		for (s = data; s - data < len; s += strlen(s) + 1) {
88227753Stheraven			if (s != data)
89227753Stheraven				printf(" ");
90227753Stheraven			printf("%s", (const char *)s);
91227753Stheraven		}
92236889Stheraven		return 0;
93236889Stheraven	}
94236889Stheraven	size = disp->size;
95236889Stheraven	if (size == -1) {
96236889Stheraven		size = (len % 4) == 0 ? 4 : 1;
97236889Stheraven	} else if (len % size) {
98236889Stheraven		fprintf(stderr, "Property length must be a multiple of "
99236889Stheraven				"selected data size\n");
100236889Stheraven		return -1;
101236889Stheraven	}
102236889Stheraven	fmt[0] = '%';
103117270Sache	fmt[1] = disp->type ? disp->type : 'd';
104227753Stheraven	fmt[2] = '\0';
105117270Sache	for (i = 0; i < len; i += size, p += size) {
10611695Sache		if (i)
10711695Sache			printf(" ");
10811695Sache		value = size == 4 ? fdt32_to_cpu(*(const uint32_t *)p) :
109101498Sache			size == 2 ? (*p << 8) | p[1] : *p;
110227753Stheraven		printf(fmt, value);
11111695Sache	}
11211695Sache	return 0;
11311695Sache}
11411695Sache
115101498Sache/**
116236889Stheraven * List all properties in a node, one per line.
117227753Stheraven *
118101263Sache * @param blob		FDT blob
11911695Sache * @param node		Node to display
12011695Sache * @return 0 if ok, or FDT_ERR... if not.
121117270Sache */
12220810Sjoergstatic int list_properties(const void *blob, int node)
12320810Sjoerg{
12420810Sjoerg	const struct fdt_property *data;
12520810Sjoerg	const char *name;
12611695Sache	int prop;
12711695Sache
128101566Sache	prop = fdt_first_property_offset(blob, node);
12911695Sache	do {
130101498Sache		/* Stop silently when there are no more properties */
131101566Sache		if (prop < 0)
132101498Sache			return prop == -FDT_ERR_NOTFOUND ? 0 : prop;
133101498Sache		data = fdt_get_property_by_offset(blob, prop, NULL);
13411695Sache		name = fdt_string(blob, fdt32_to_cpu(data->nameoff));
135101498Sache		if (name)
13611695Sache			puts(name);
137227753Stheraven		prop = fdt_next_property_offset(blob, prop);
138227753Stheraven	} while (1);
139227753Stheraven}
140227753Stheraven
141227753Stheraven#define MAX_LEVEL	32		/* how deeply nested we will go */
142175586Sache
143136609Stjr/**
144136609Stjr * List all subnodes in a node, one per line
145175586Sache *
146227753Stheraven * @param blob		FDT blob
147175586Sache * @param node		Node to display
148227753Stheraven * @return 0 if ok, or FDT_ERR... if not.
149175586Sache */
150227753Stheravenstatic int list_subnodes(const void *blob, int node)
151175586Sache{
152227753Stheraven	int nextoffset;		/* next node offset from libfdt */
153175586Sache	uint32_t tag;		/* current tag */
154227753Stheraven	int level = 0;		/* keep track of nesting level */
155175586Sache	const char *pathp;
156227753Stheraven	int depth = 1;		/* the assumed depth of this node */
157175586Sache
158227753Stheraven	while (level >= 0) {
159175586Sache		tag = fdt_next_tag(blob, node, &nextoffset);
160227753Stheraven		switch (tag) {
161175586Sache		case FDT_BEGIN_NODE:
162227753Stheraven			pathp = fdt_get_name(blob, node, NULL);
163175586Sache			if (level <= depth) {
164101498Sache				if (pathp == NULL)
165175585Sache					pathp = "/* NULL pointer error */";
166101498Sache				if (*pathp == '\0')
167227753Stheraven					pathp = "/";	/* root is nameless */
168236889Stheraven				if (level == 1)
169175586Sache					puts(pathp);
170227753Stheraven			}
171227753Stheraven			level++;
172101498Sache			if (level >= MAX_LEVEL) {
173175586Sache				printf("Nested too deep, aborting.\n");
174101498Sache				return 1;
175101498Sache			}
17611695Sache			break;
17711695Sache		case FDT_END_NODE:
178117270Sache			level--;
179117270Sache			if (level == 0)
180117270Sache				level = -1;		/* exit the loop */
181227753Stheraven			break;
182117270Sache		case FDT_END:
183117270Sache			return 1;
184117270Sache		case FDT_PROP:
185117270Sache			break;
186117270Sache		default:
187227753Stheraven			if (level <= depth)
188227753Stheraven				printf("Unknown tag 0x%08X\n", tag);
189232498Stheraven			return 1;
190117270Sache		}
191117270Sache		node = nextoffset;
192232498Stheraven	}
193232498Stheraven	return 0;
194232498Stheraven}
195232498Stheraven
196232498Stheraven/**
197232498Stheraven * Show the data for a given node (and perhaps property) according to the
198232498Stheraven * display option provided.
199232498Stheraven *
200232498Stheraven * @param blob		FDT blob
201232498Stheraven * @param disp		Display information / options
202232498Stheraven * @param node		Node to display
203232498Stheraven * @param property	Name of property to display, or NULL if none
204232498Stheraven * @return 0 if ok, -ve on error
205232498Stheraven */
206232498Stheravenstatic int show_data_for_item(const void *blob, struct display_info *disp,
207227753Stheraven		int node, const char *property)
208227753Stheraven{
209227753Stheraven	const void *value = NULL;
210227753Stheraven	int len, err = 0;
211227753Stheraven
212227753Stheraven	switch (disp->mode) {
213227753Stheraven	case MODE_LIST_PROPS:
214227753Stheraven		err = list_properties(blob, node);
215227753Stheraven		break;
216227753Stheraven
217	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");
229		} else if (disp->default_val) {
230			puts(disp->default_val);
231		} else {
232			report_error(property, len);
233			err = -1;
234		}
235		break;
236	}
237
238	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
248 * @param return 0 if ok, -ve on error
249 */
250static int do_fdtget(struct display_info *disp, const char *filename,
251		     char **arg, int arg_count, int args_per_step)
252{
253	char *blob;
254	const char *prop;
255	int i, node;
256
257	blob = utilfdt_read(filename);
258	if (!blob)
259		return -1;
260
261	for (i = 0; i + args_per_step <= arg_count; i += args_per_step) {
262		node = fdt_path_offset(blob, arg[i]);
263		if (node < 0) {
264			if (disp->default_val) {
265				puts(disp->default_val);
266				continue;
267			} else {
268				report_error(arg[i], node);
269				free(blob);
270				return -1;
271			}
272		}
273		prop = args_per_step == 1 ? NULL : arg[i + 1];
274
275		if (show_data_for_item(blob, disp, node, prop)) {
276			free(blob);
277			return -1;
278		}
279	}
280
281	free(blob);
282
283	return 0;
284}
285
286/* Usage related data. */
287static const char usage_synopsis[] =
288	"read values from device tree\n"
289	"	fdtget <options> <dt file> [<node> <property>]...\n"
290	"	fdtget -p <options> <dt file> [<node> ]...\n"
291	"\n"
292	"Each value is printed on a new line.\n"
293	USAGE_TYPE_MSG;
294static 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};
309
310int main(int argc, char *argv[])
311{
312	int opt;
313	char *filename = NULL;
314	struct display_info disp;
315	int args_per_step = 2;
316
317	/* set defaults */
318	memset(&disp, '\0', sizeof(disp));
319	disp.size = -1;
320	disp.mode = MODE_SHOW_VALUE;
321	while ((opt = util_getopt_long()) != EOF) {
322		switch (opt) {
323		case_USAGE_COMMON_FLAGS
324
325		case 't':
326			if (utilfdt_decode_type(optarg, &disp.type,
327					&disp.size))
328				usage("invalid type string");
329			break;
330
331		case 'p':
332			disp.mode = MODE_LIST_PROPS;
333			args_per_step = 1;
334			break;
335
336		case 'l':
337			disp.mode = MODE_LIST_SUBNODES;
338			args_per_step = 1;
339			break;
340
341		case 'd':
342			disp.default_val = optarg;
343			break;
344		}
345	}
346
347	if (optind < argc)
348		filename = argv[optind++];
349	if (!filename)
350		usage("missing filename");
351
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 */
360	if (args_per_step == 2 && (argc % 2))
361		usage("must have an even number of arguments");
362
363	if (do_fdtget(&disp, filename, argv, argc, args_per_step))
364		return 1;
365	return 0;
366}
367