aboutsummaryrefslogtreecommitdiffstats
path: root/meta-ti-extras/recipes/dtc/dtc/0001-fdtdump-Add-live-tree-dump-capability.patch
blob: 372a4304cf8d0acbe72c5bf923057b2181d51714 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
From da5c33dd94949fa27243faf15cd87e98c53ccb29 Mon Sep 17 00:00:00 2001
From: Pantelis Antoniou <pantelis.antoniou@konsulko.com>
Date: Tue, 5 Nov 2013 10:16:14 +0200
Subject: [PATCH] fdtdump: Add live tree dump capability

Adds the capability to dump any point of the kernel's live tree
which resides usually in /proc/device-tree.

For example you can do this:

	# fdtdump /proc/device-tree/ocp/ethernet\@4a100000/
	/* dump of live tree at /proc/device-tree/ocp/ethernet@4a100000 */
	/ {
		name = "ethernet";
		pinctrl-1 = <0x0000000b>;
		pinctrl-0 = <0x0000000a>;
		pinctrl-names = "default", "sleep";
		ranges;
		interrupts = <0x00000028 0x00000000 0x00000000 0x00000000>;
		interrupt-parent = <0x00000001>;
		#size-cells = <0x00000001>;
		#address-cells = <0x00000001>;
		reg = <0x4a100000 0x00000000 0x00000000 0x00000000>;
		cpts_clock_shift = <0x0000001d>;
		cpts_clock_mult = <0x80000000>;
		active_slave = <0x00000000>;
		slaves = <0x00000002>;
		mac_control = <0x00000020>;
		rx_descs = <0x00000040>;
		no_bd_ram = <0x00000000>;
		bd_ram_size = <0x00002000>;
		ale_entries = <0x00000400>;
		cpdma_channels = <0x00000008>;
		ti,hwmods = "cpgmac0";
		compatible = "ti,cpsw";
		slave@4a100300 {
			name = "slave";
			phy-mode = "mii";
			phy_id = <0x0000000e 0x00000000>;
			mac-address = [00 00 00 00 00 00];
		};
		slave@4a100200 {
			name = "slave";
			phy-mode = "mii";
			phy_id = <0x0000000e 0x00000000>;
			mac-address = [00 00 00 00 00 00];
		};
		mdio@4a101000 {
			name = "mdio";
			phandle = <0x0000000e>;
			linux,phandle = <0x0000000e>;
			pinctrl-1 = <0x0000000d>;
			pinctrl-0 = <0x0000000c>;
			pinctrl-names = "default", "sleep";
			reg = <0x4a101000 0x00000000>;
			bus_freq = <0x000f4240>;
			ti,hwmods = "davinci_mdio";
			#size-cells = <0x00000000>;
			#address-cells = <0x00000001>;
			compatible = "ti,davinci_mdio";
		};
	};

This makes it much easier to see the state of the kernel's live tree.

Signed-off-by: Pantelis Antoniou <pantelis.antoniou@konsulko.com>
---
 fdtdump.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 107 insertions(+)

diff --git a/fdtdump.c b/fdtdump.c
index 95a6a20..9183555 100644
--- a/fdtdump.c
+++ b/fdtdump.c
@@ -8,6 +8,14 @@
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <alloca.h>
+#include <dirent.h>
+#include <limits.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
 
 #include <libfdt.h>
 #include <libfdt_env.h>
@@ -143,6 +151,95 @@ static void dump_blob(void *blob, bool debug)
 	}
 }
 
+static void dump_live_internal(const char *path, bool debug, int depth)
+{
+	int maxsz = strlen(path) + 1 + PATH_MAX;
+	char *new_path = alloca(maxsz + 1);
+	struct stat sb;
+	struct dirent *de;
+	char *buf, *p;
+	int buf_alloc, shift, chunk, left, fd, ret;
+	DIR *d;
+
+	shift = 4;
+	buf_alloc = 4 * 1024;	/* 4K (maximum chunk) */
+	buf = alloca(buf_alloc + sizeof(uint32_t));
+	buf[buf_alloc] = '\0';	/* always terminate (just in case) */
+
+	d = opendir(path);
+	if (d == NULL)
+		die("Could not open %s directory\n", path);
+
+	/* first dump the properties (files) */
+	while ((de = readdir(d)) != NULL) {
+		/* properties are files */
+		if (de->d_type != DT_REG)
+			continue;
+		snprintf(new_path, maxsz, "%s/%s", path, de->d_name);
+		new_path[maxsz] = '\0';
+		printf("%*s%s", depth * shift, "", de->d_name);
+
+		if (stat(new_path, &sb) != 0)
+			die("could not open: %s\n", new_path);
+
+		fd = open(new_path, O_RDONLY);
+		if (fd == -1)
+			die("Could not open: %s\n", new_path);
+
+		chunk = sb.st_size > buf_alloc ? buf_alloc : sb.st_size;
+		p = buf;
+		left = chunk;
+		while (left > 0) {
+			do {
+				ret = read(fd, p, left);
+			} while (ret == -1 && (errno == EAGAIN || errno == EINTR));
+			if (ret == -1)
+				die("Read failed on: %s\n", new_path);
+			left -= ret;
+			p += ret;
+		}
+		close(fd);
+
+		if (chunk < sb.st_size)
+			printf(" (trunc)");
+		utilfdt_print_data(buf, chunk);
+		printf(";\n");
+	}
+
+	/* now recurse to the directories */
+	rewinddir(d);
+	while ((de = readdir(d)) != NULL) {
+		/* properties are files */
+		if (de->d_type != DT_DIR)
+			continue;
+		/* skip current and parent directories */
+		if (strcmp(de->d_name, ".") == 0 ||
+				strcmp(de->d_name, "..") == 0)
+			continue;
+		snprintf(new_path, maxsz, "%s/%s", path, de->d_name);
+		new_path[maxsz] = '\0';
+		printf("%*s%s {\n", depth * shift, "", de->d_name);
+		dump_live_internal(new_path, debug, depth + 1);
+		printf("%*s};\n", depth * shift, "");
+	}
+}
+
+static void dump_live(const char *path, bool debug)
+{
+	char *fixed_path = alloca(strlen(path) + 1);
+	char *p;
+
+	/* strip trailing / */
+	strcpy(fixed_path, path);
+	p = fixed_path + strlen(fixed_path) - 1;
+	while (*p == '/' && p > fixed_path)
+		*p-- = '\0';
+	printf("/* dump of live tree at %s */\n", fixed_path);
+	printf("/ {\n");
+	dump_live_internal(fixed_path, debug, 1);
+	printf("};\n");
+}
+
 /* Usage related data. */
 static const char usage_synopsis[] = "fdtdump [options] <file>";
 static const char usage_short_opts[] = "ds" USAGE_COMMON_SHORT_OPTS;
@@ -165,6 +262,7 @@ int main(int argc, char *argv[])
 	bool debug = false;
 	bool scan = false;
 	off_t len;
+	struct stat sb;
 
 	while ((opt = util_getopt_long()) != EOF) {
 		switch (opt) {
@@ -182,6 +280,15 @@ int main(int argc, char *argv[])
 		usage("missing input filename");
 	file = argv[optind];
 
+	if (stat(file, &sb) != 0)
+		die("could not open: %s\n", file);
+
+	/* dump live tree if it's a directory */
+	if (S_ISDIR(sb.st_mode)) {
+		dump_live(file, debug);
+		return 0;
+	}
+
 	buf = utilfdt_read_len(file, &len);
 	if (!buf)
 		die("could not read: %s\n", file);
-- 
1.9.1