-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdtdump.c
153 lines (123 loc) · 4.04 KB
/
dtdump.c
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
// code adapted from Jonathan Levin's http://www.newosxbook.com/src.jl?tree=listings&file=6-bonus.c
// clean-up a bit to
// 1. make "clang -Wall" happy
// 2. remove img3 stuff: device tree files are in im4p nowadays
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include "dt.h" // for DeviceTree
int verbose = 0;
void copyValue (char *dest, char *src, int length)
{
int i = 0;
for (i = 0; src[i] || i < length; i++);
if (i != length) {
strcpy(dest, "(null)");
return;
}
memcpy(dest, src, length);
}
uint32_t dumpTreeNode(DeviceTreeNode *Node, int indent)
{
char buffer[819200];
char temp[409600];
char h_temp[81920];
char *name;
int prop = 0, child = 0;
int i = 0;
memset(buffer, '\0', 819200);
DeviceTreeNodeProperty *dtp = (DeviceTreeNodeProperty * ) ((char*)Node + sizeof(DeviceTreeNode));
char *offset = 0;
int real_len;
for (prop = 0; prop < Node->nProperties; prop++) {
real_len = dtp->length;
temp[0] = '\0'; // strcat will do the rest
for (i=0; i< indent ; i++) {
strcat(temp,"| ");
}
strcat(temp, "+--");
strncat(buffer, temp, 1024);
if ((real_len & 0x80000000) > 0)
real_len = real_len - 0x80000000;
sprintf(temp, "%s %d bytes: ", dtp->name, real_len);
strncat(buffer, temp, 1024);
if (strcmp(dtp->name,"name") == 0) {
name = (char *) &dtp->length + sizeof(uint32_t);
strncat(buffer, name, dtp->length);
strcat(buffer,"\n");
} else {
copyValue(temp, ((char *) &dtp->length) + sizeof(uint32_t), real_len);
// Yeah, Yeah, Buffer overflows, etc.. :-)
if (verbose) {
char *hex = h_temp;
for (i=0; i < real_len; i++) {
sprintf(hex, " 0x%02x", 0xff & *(((char *) &dtp->length) + sizeof(uint32_t) + i));
hex += 5; // len(" 0x??") = 5
}
}
strncat(buffer, temp, real_len);
if (verbose)
strcat(buffer, h_temp);
strcat(buffer, "\n");
}
dtp = (DeviceTreeNodeProperty *) (((char *) dtp) + sizeof(DeviceTreeNodeProperty) + real_len);
// Align
dtp = (((long) dtp %4) ? (DeviceTreeNodeProperty *) (((char *) dtp) + (4 - ((long)dtp) %4)): dtp);
offset = (char *) dtp;
}
for (i=0; i < indent-1; i++) {
printf(" ");
}
if (indent > 1)
printf ("+--");
printf("%s:\n", name);
printf("%s", buffer);
// Now do children:
for (child = 0; child < Node->nChildren; child++) {
offset+= dumpTreeNode((DeviceTreeNode *) offset, indent+1);
}
return ( (char *) offset - (char*) Node);
}
int
main(int argc, char **argv)
{
struct stat stbuf;
char *filename;
int rc;
int fd;
int filesize;
char *mmapped;
if (argc < 2) {
fprintf (stderr,"Usage: %s [-v] _filename_\n", argv[0]);
exit(0);
}
if (strcmp(argv[1], "-v") == 0)
verbose = 1;
filename = argv[argc -1];
rc = stat(filename, &stbuf);
if (rc == -1) { perror (filename); exit (1); }
filesize = stbuf.st_size;
fd = open (filename, O_RDONLY);
if (fd < 0) { perror (filename); exit(2);}
mmapped = mmap(NULL,
filesize, // size_t len,
PROT_READ, // int prot,
MAP_SHARED | MAP_FILE, // int flags,
fd, // int fd,
0); // off_t offset);
if (!mmapped) {
perror ("mmap");
exit(3);
}
char *data = mmapped;
DeviceTreeNode *dtn = (DeviceTreeNode *) data;
printf ("\tDevice Tree with %d properties and %d children\n", dtn->nProperties, dtn->nChildren);
printf("Properties:\n");
dumpTreeNode (dtn,1);
return 0;
}