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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
/*
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2019 Western Digital Corporation or its affiliates.
*
* Authors:
* Anup Patel <anup.patel@wdc.com>
*/
#include <sbi/sbi_string.h>
#include <sbi_utils/tinyfdt.h>
#define FDT_MAGIC 0xd00dfeed
#define FDT_VERSION 17
struct fdt_header {
u32 magic;
u32 totalsize;
u32 off_dt_struct;
u32 off_dt_strings;
u32 off_mem_rsvmap;
u32 version;
u32 last_comp_version; /* <= 17 */
u32 boot_cpuid_phys;
u32 size_dt_strings;
u32 size_dt_struct;
} __attribute__((packed));
#define FDT_BEGIN_NODE 1
#define FDT_END_NODE 2
#define FDT_PROP 3
#define FDT_NOP 4
#define FDT_END 9
u32 fdt_rev32(u32 v)
{
return ((v & 0x000000FF) << 24) | ((v & 0x0000FF00) << 8) |
((v & 0x00FF0000) >> 8) | ((v & 0xFF000000) >> 24);
}
int fdt_prop_string_index(const struct fdt_prop *prop, const char *str)
{
int i;
ulong l = 0;
const char *p, *end;
p = prop->value;
end = p + prop->len;
for (i = 0; p < end; i++, p += l) {
l = sbi_strlen(p) + 1;
if (p + l > end)
return -1;
if (sbi_strcmp(str, p) == 0)
return i; /* Found it; return index */
}
return -1;
}
struct recursive_iter_info {
void (*fn)(const struct fdt_node *node, const struct fdt_prop *prop,
void *priv);
void *fn_priv;
const char *str;
};
#define DATA32(ptr) fdt_rev32(*((u32 *)ptr))
static void recursive_iter(char **data, struct recursive_iter_info *info,
const struct fdt_node *parent)
{
struct fdt_node node;
struct fdt_prop prop;
if (DATA32(*data) != FDT_BEGIN_NODE)
return;
node.data = *data;
(*data) += sizeof(u32);
node.parent = parent;
node.name = *data;
*data += sbi_strlen(*data) + 1;
while ((ulong)(*data) % sizeof(u32) != 0)
(*data)++;
node.depth = (parent) ? (parent->depth + 1) : 1;
/* Default cell counts, as per the FDT spec */
node.address_cells = 2;
node.size_cells = 1;
info->fn(&node, NULL, info->fn_priv);
while (DATA32(*data) != FDT_END_NODE) {
switch (DATA32(*data)) {
case FDT_PROP:
prop.node = &node;
*data += sizeof(u32);
prop.len = DATA32(*data);
*data += sizeof(u32);
prop.name = &info->str[DATA32(*data)];
*data += sizeof(u32);
prop.value = *data;
*data += prop.len;
while ((ulong)(*data) % sizeof(u32) != 0)
(*data)++;
info->fn(&node, &prop, info->fn_priv);
break;
case FDT_NOP:
*data += sizeof(u32);
break;
case FDT_BEGIN_NODE:
recursive_iter(data, info, &node);
break;
default:
return;
};
}
*data += sizeof(u32);
}
struct match_iter_info {
int (*match)(const struct fdt_node *node, const struct fdt_prop *prop,
void *priv);
void *match_priv;
void (*fn)(const struct fdt_node *node, const struct fdt_prop *prop,
void *priv);
void *fn_priv;
const char *str;
};
static void match_iter(const struct fdt_node *node, const struct fdt_prop *prop,
void *priv)
{
char *data;
struct match_iter_info *minfo = priv;
struct fdt_prop nprop;
/* Do nothing if node+prop dont match */
if (!minfo->match(node, prop, minfo->match_priv))
return;
/* Call function for node */
if (minfo->fn)
minfo->fn(node, NULL, minfo->fn_priv);
/* Convert node to character stream */
data = node->data;
data += sizeof(u32);
/* Skip node name */
data += sbi_strlen(data) + 1;
while ((ulong)(data) % sizeof(u32) != 0)
data++;
/* Find node property and its value */
while (DATA32(data) == FDT_PROP) {
nprop.node = node;
data += sizeof(u32);
nprop.len = DATA32(data);
data += sizeof(u32);
nprop.name = &minfo->str[DATA32(data)];
data += sizeof(u32);
nprop.value = data;
data += nprop.len;
while ((ulong)(data) % sizeof(u32) != 0)
(data)++;
/* Call function for every property */
if (minfo->fn)
minfo->fn(node, &nprop, minfo->fn_priv);
}
}
int fdt_match_node_prop(void *fdt,
int (*match)(const struct fdt_node *node,
const struct fdt_prop *prop, void *priv),
void *match_priv,
void (*fn)(const struct fdt_node *node,
const struct fdt_prop *prop, void *priv),
void *fn_priv)
{
char *data;
u32 string_offset, data_offset;
struct fdt_header *header;
struct match_iter_info minfo;
struct recursive_iter_info rinfo;
if (!fdt || !match)
return -1;
header = fdt;
if (fdt_rev32(header->magic) != FDT_MAGIC ||
fdt_rev32(header->last_comp_version) > FDT_VERSION)
return -1;
string_offset = fdt_rev32(header->off_dt_strings);
data_offset = fdt_rev32(header->off_dt_struct);
minfo.match = match;
minfo.match_priv = match_priv;
minfo.fn = fn;
minfo.fn_priv = fn_priv;
minfo.str = (const char *)(fdt + string_offset);
rinfo.fn = match_iter;
rinfo.fn_priv = &minfo;
rinfo.str = minfo.str;
data = (char *)(fdt + data_offset);
recursive_iter(&data, &rinfo, NULL);
return 0;
}
struct match_compat_info {
const char *compat;
};
static int match_compat(const struct fdt_node *node,
const struct fdt_prop *prop, void *priv)
{
struct match_compat_info *cinfo = priv;
if (!prop)
return 0;
if (sbi_strcmp(prop->name, "compatible"))
return 0;
if (fdt_prop_string_index(prop, cinfo->compat) < 0)
return 0;
return 1;
}
int fdt_compat_node_prop(void *fdt, const char *compat,
void (*fn)(const struct fdt_node *node,
const struct fdt_prop *prop, void *priv),
void *fn_priv)
{
struct match_compat_info cinfo = { .compat = compat };
return fdt_match_node_prop(fdt, match_compat, &cinfo, fn, fn_priv);
}
static int match_walk(const struct fdt_node *node, const struct fdt_prop *prop,
void *priv)
{
if (!prop)
return 1;
return 0;
}
int fdt_walk(void *fdt,
void (*fn)(const struct fdt_node *node,
const struct fdt_prop *prop, void *priv),
void *fn_priv)
{
return fdt_match_node_prop(fdt, match_walk, NULL, fn, fn_priv);
}
u32 fdt_size(void *fdt)
{
struct fdt_header *header;
if (!fdt)
return 0;
header = fdt;
if (fdt_rev32(header->magic) != FDT_MAGIC ||
fdt_rev32(header->last_comp_version) > FDT_VERSION)
return 0;
return fdt_rev32(header->totalsize);
}
|