-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfdt.c++
225 lines (198 loc) · 6.05 KB
/
fdt.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "fdt.h++"
#ifdef __MINGW32__
#include <winsock.h>
#else
#include <arpa/inet.h>
#endif
#include <cassert>
#include <cstring>
#include <fcntl.h>
#include <fstream>
#include <map>
#include <string>
#include <sys/stat.h>
#include <unistd.h>
char *dts_read(const char *filename) {
if (strcmp(filename, "-") != 0) {
std::ifstream ifs(filename, std::ios::binary | std::ios::ate);
std::ifstream::pos_type pos = ifs.tellg();
int length = pos;
char *buf = (char *)malloc(length);
ifs.seekg(0, std::ios::beg);
ifs.read(buf, length);
if (!ifs) {
free(buf);
buf = NULL;
}
ifs.close();
return buf;
} else {
/* stdin/cin is not supported yet */
return NULL;
}
}
static uint8_t *read_fdt_from_path(const char *filename) {
return (uint8_t *)dts_read(filename);
}
node node::parent(void) const {
auto parent_offset = fdt_parent_offset(_dts_blob, _offset);
return node(_dts_blob, parent_offset, _depth - 1);
}
int node::num_addr_cells(void) const {
auto f = parent().get_fields<uint32_t>("#address-cells");
if (f.size() == 0)
return 2;
if (f.size() == 1)
return f[0];
std::cerr << "#address-cells has more than one cell\n";
abort();
}
int node::num_size_cells(void) const {
auto f = parent().get_fields<uint32_t>("#size-cells");
if (f.size() == 0)
return 2;
if (f.size() == 1)
return f[0];
std::cerr << "#size-cells has more than one cell\n";
abort();
}
void node::obtain_one(std::vector<node> &v, const uint8_t *buf, int len,
int offset, int *consumed) const {
std::vector<uint32_t> phandles;
obtain_one(phandles, buf, len, offset, consumed);
auto phandle_offset = fdt_node_offset_by_phandle(_dts_blob, phandles[0]);
v.push_back(node(_dts_blob, phandle_offset, -1));
}
void node::obtain_one(std::vector<uint32_t> &v, const uint8_t *buf, int len,
int offset, int *consumed) const {
uint32_t n = *((uint32_t *)(buf + offset));
v.push_back(ntohl(n));
*consumed = 4;
}
void node::obtain_one(std::vector<target_addr> &v, const uint8_t *buf, int len,
int offset, int *consumed) const {
auto n = ((uint32_t *)(buf + offset));
switch (num_addr_cells()) {
case 1:
v.push_back(ntohl(n[0]));
*consumed = 4;
return;
case 2:
v.push_back(ntohl(n[1]) | ((uint64_t)(ntohl(n[0])) << 32));
*consumed = 8;
return;
}
std::cerr << "Unknown number of address cells\n";
abort();
}
void node::obtain_one(std::vector<target_size> &v, const uint8_t *buf, int len,
int offset, int *consumed) const {
auto n = ((uint32_t *)(buf + offset));
switch (num_size_cells()) {
case 1:
v.push_back(ntohl(n[0]));
*consumed = 4;
return;
case 2:
v.push_back(ntohl(n[1]) | ((uint64_t)(ntohl(n[0])) << 32));
*consumed = 8;
return;
}
std::cerr << "Unknown number of address cells\n";
abort();
}
void node::obtain_one(std::vector<std::string> &v, const uint8_t *buf, int len,
int offset, int *consumed) const {
int i = offset;
while (i < len && buf[i] != '\0') {
++i;
}
v.push_back(std::string((const char *)(buf + offset)));
*consumed = i + 1;
}
std::string node::name(void) const {
auto node_name = fdt_get_name(_dts_blob, _offset, NULL);
if (node_name == nullptr)
return std::string();
std::string n(node_name);
std::transform(n.begin(), n.end(), n.begin(),
[](unsigned char c) { return (c == '-') ? '_' : c; });
return n;
}
std::string node::handle(void) const {
auto n = name();
std::transform(n.begin(), n.end(), n.begin(),
[](unsigned char c) { return (c == '@') ? '_' : c; });
return n;
}
std::string node::handle_cap(void) const {
auto n = name();
std::transform(n.begin(), n.end(), n.begin(),
[](unsigned char c) { return (c == '@') ? '_' : toupper(c); });
return n;
}
std::string node::instance(void) const {
auto n = name();
std::size_t p = n.find('@');
return (p != std::string::npos) ? n.substr(p + 1) : "";
}
bool node::field_exists(std::string name) const {
int len;
auto out = fdt_get_property(_dts_blob, _offset, name.c_str(), &len);
return out != nullptr;
}
fdt::fdt(const uint8_t *blob) : _dts_blob(blob), _allocated(false) {
assert(blob != nullptr);
assert(_dts_blob != nullptr);
}
fdt::fdt(std::string path)
: _dts_blob(read_fdt_from_path(path.c_str())), _allocated(true) {
assert(_dts_blob != nullptr);
}
bool fdt::path_exists(std::string path) const {
auto offset = fdt_path_offset(_dts_blob, path.c_str());
return offset > 0;
}
node fdt::node_by_path(std::string path) const {
auto offset = fdt_path_offset(_dts_blob, path.c_str());
return node(_dts_blob, offset, 0);
}
int fdt::match(const std::regex &r, std::function<void(const node &)> f,
bool skip_device_type) const {
int depth, offset;
int matches = 0;
depth = 0;
offset = fdt_path_offset(_dts_blob, "/");
while (offset >= 0 && depth >= 0) {
int compat_len;
auto device_bytes = (const char *)fdt_getprop(_dts_blob, offset,
"device_type", &compat_len);
if (device_bytes != nullptr && !skip_device_type) {
for (int i = 0; i < compat_len; i += strlen(&device_bytes[i]) + 1) {
auto compat = std::string(&device_bytes[i]);
if (i >= compat_len)
break;
if (!std::regex_match(compat, r))
continue;
f(node(_dts_blob, offset, depth));
matches++;
}
} else {
auto compat_bytes = (const char *)fdt_getprop(_dts_blob, offset,
"compatible", &compat_len);
if (compat_bytes != nullptr) {
for (int i = 0; i < compat_len; i += strlen(&compat_bytes[i]) + 1) {
auto compat = std::string(&compat_bytes[i]);
if (i >= compat_len)
break;
if (!std::regex_match(compat, r))
continue;
f(node(_dts_blob, offset, depth));
matches++;
}
}
}
offset = fdt_next_node(_dts_blob, offset, &depth);
}
return matches;
}