summaryrefslogtreecommitdiffstats
path: root/commands/of_dump.c
blob: 86755ff1e44557a52362f94023615f07bd197316 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// SPDX-License-Identifier: GPL-2.0-only
// SPDX-FileCopyrightText: © 2014 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix

/* of_dump.c - dump devicetrees to the console */

#include <common.h>
#include <libfile.h>
#include <fdt.h>
#include <of.h>
#include <command.h>
#include <fs.h>
#include <malloc.h>
#include <complete.h>
#include <linux/ctype.h>
#include <asm/byteorder.h>
#include <errno.h>
#include <getopt.h>
#include <linux/err.h>

static void of_print_nodenames(struct device_node *node)
{
	struct device_node *n;

	printf("%s\n", node->full_name);

	list_for_each_entry(n, &node->children, parent_list) {
		if (ctrlc())
			return;
		of_print_nodenames(n);
	}
}

static int do_of_dump(int argc, char *argv[])
{
	int opt;
	int ret = 0;
	int fix = 0;
	struct device_node *root = NULL, *node, *of_free = NULL;
	char *dtbfile = NULL;
	const char *nodename;
	unsigned maxpropsize = ~0;
	int names_only = 0, properties_only = 0;

	while ((opt = getopt(argc, argv, "Ff:npP:")) > 0) {
		switch (opt) {
		case 'f':
			dtbfile = optarg;
			break;
		case 'F':
			fix = 1;
			break;
		case 'n':
			names_only = 1;
			break;
		case 'p':
			properties_only = 1;
			break;
		case 'P':
			ret = kstrtouint(optarg, 0, &maxpropsize);
			if (ret)
				return ret;
			break;
		default:
			return COMMAND_ERROR_USAGE;
		}
	}

	if (optind == argc)
		nodename = "/";
	else
		nodename = argv[optind];

	if (dtbfile) {
		root = of_read_file(dtbfile);
		if (IS_ERR(root)) {
			printf("Cannot open %s: %pe\n", dtbfile, root);
			ret = PTR_ERR(root);
			goto out;
		}

		of_free = root;
	} else {
		root = of_get_root_node();

		if (fix) {
			/* create a copy of internal devicetree */
			void *fdt;
			fdt = of_flatten_dtb(root);
			root = of_unflatten_dtb(fdt, fdt_totalsize(fdt));

			free(fdt);

			if (IS_ERR(root)) {
				ret = PTR_ERR(root);
				goto out;
			}

			of_free = root;
		}
	}

	if (fix) {
		ret = of_fix_tree(root);
		if (ret)
			goto out;
	}

	node = of_find_node_by_path_or_alias(root, nodename);
	if (!node) {
		printf("Cannot find nodepath %s\n", nodename);
		ret = -ENOENT;
		goto out;
	}

	if (names_only && !properties_only)
		of_print_nodenames(node);
	else if (properties_only && !names_only)
		of_print_properties(node, maxpropsize);
	else
		of_print_nodes(node, 0, maxpropsize);

out:
	if (of_free)
		of_delete_node(of_free);

	return ret;
}

BAREBOX_CMD_HELP_START(of_dump)
BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT  ("-f dtb",  "work on dtb instead of internal devicetree")
BAREBOX_CMD_HELP_OPT  ("-F",  "return fixed devicetree")
BAREBOX_CMD_HELP_OPT  ("-n",  "Print node names only, no properties")
BAREBOX_CMD_HELP_OPT  ("-p",  "Print properties only, no child nodes")
BAREBOX_CMD_HELP_OPT  ("-P len",  "print only len property bytes")
BAREBOX_CMD_HELP_END

BAREBOX_CMD_START(of_dump)
	.cmd		= do_of_dump,
	BAREBOX_CMD_DESC("dump devicetree nodes")
	BAREBOX_CMD_OPTS("[-fFnpP] [NODE]")
	BAREBOX_CMD_GROUP(CMD_GRP_MISC)
	BAREBOX_CMD_COMPLETE(devicetree_file_complete)
	BAREBOX_CMD_HELP(cmd_of_dump_help)
BAREBOX_CMD_END