1
2
3
4
5
6
7
8
9
10
11#include <common.h>
12#include <log.h>
13#include <linux/libfdt.h>
14#include <of_live.h>
15#include <malloc.h>
16#include <dm/of_access.h>
17#include <linux/err.h>
18
19static void *unflatten_dt_alloc(void **mem, unsigned long size,
20 unsigned long align)
21{
22 void *res;
23
24 *mem = PTR_ALIGN(*mem, align);
25 res = *mem;
26 *mem += size;
27
28 return res;
29}
30
31
32
33
34
35
36
37
38
39
40
41
42static void *unflatten_dt_node(const void *blob, void *mem, int *poffset,
43 struct device_node *dad,
44 struct device_node **nodepp,
45 unsigned long fpsize, bool dryrun)
46{
47 const __be32 *p;
48 struct device_node *np;
49 struct property *pp, **prev_pp = NULL;
50 const char *pathp;
51 int l;
52 unsigned int allocl;
53 static int depth;
54 int old_depth;
55 int offset;
56 int has_name = 0;
57 int new_format = 0;
58
59 pathp = fdt_get_name(blob, *poffset, &l);
60 if (!pathp)
61 return mem;
62
63 allocl = ++l;
64
65
66
67
68
69
70
71 if ((*pathp) != '/') {
72 new_format = 1;
73 if (fpsize == 0) {
74
75
76
77
78
79
80 fpsize = 1;
81 allocl = 2;
82 l = 1;
83 pathp = "";
84 } else {
85
86
87
88
89 fpsize += l;
90 allocl = fpsize;
91 }
92 }
93
94 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
95 __alignof__(struct device_node));
96 if (!dryrun) {
97 char *fn;
98
99 fn = (char *)np + sizeof(*np);
100 if (new_format) {
101 np->name = pathp;
102 has_name = 1;
103 }
104 np->full_name = fn;
105 if (new_format) {
106
107 if (dad && dad->parent) {
108 strcpy(fn, dad->full_name);
109#ifdef DEBUG
110 if ((strlen(fn) + l + 1) != allocl) {
111 debug("%s: p: %d, l: %d, a: %d\n",
112 pathp, (int)strlen(fn), l,
113 allocl);
114 }
115#endif
116 fn += strlen(fn);
117 }
118 *(fn++) = '/';
119 }
120 memcpy(fn, pathp, l);
121
122 prev_pp = &np->properties;
123 if (dad != NULL) {
124 np->parent = dad;
125 np->sibling = dad->child;
126 dad->child = np;
127 }
128 }
129
130 for (offset = fdt_first_property_offset(blob, *poffset);
131 (offset >= 0);
132 (offset = fdt_next_property_offset(blob, offset))) {
133 const char *pname;
134 int sz;
135
136 p = fdt_getprop_by_offset(blob, offset, &pname, &sz);
137 if (!p) {
138 offset = -FDT_ERR_INTERNAL;
139 break;
140 }
141
142 if (pname == NULL) {
143 debug("Can't find property name in list !\n");
144 break;
145 }
146 if (strcmp(pname, "name") == 0)
147 has_name = 1;
148 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
149 __alignof__(struct property));
150 if (!dryrun) {
151
152
153
154
155
156
157 if ((strcmp(pname, "phandle") == 0) ||
158 (strcmp(pname, "linux,phandle") == 0)) {
159 if (np->phandle == 0)
160 np->phandle = be32_to_cpup(p);
161 }
162
163
164
165
166 if (strcmp(pname, "ibm,phandle") == 0)
167 np->phandle = be32_to_cpup(p);
168 pp->name = (char *)pname;
169 pp->length = sz;
170 pp->value = (__be32 *)p;
171 *prev_pp = pp;
172 prev_pp = &pp->next;
173 }
174 }
175
176
177
178
179 if (!has_name) {
180 const char *p1 = pathp, *ps = pathp, *pa = NULL;
181 int sz;
182
183 while (*p1) {
184 if ((*p1) == '@')
185 pa = p1;
186 if ((*p1) == '/')
187 ps = p1 + 1;
188 p1++;
189 }
190 if (pa < ps)
191 pa = p1;
192 sz = (pa - ps) + 1;
193 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
194 __alignof__(struct property));
195 if (!dryrun) {
196 pp->name = "name";
197 pp->length = sz;
198 pp->value = pp + 1;
199 *prev_pp = pp;
200 prev_pp = &pp->next;
201 memcpy(pp->value, ps, sz - 1);
202 ((char *)pp->value)[sz - 1] = 0;
203 debug("fixed up name for %s -> %s\n", pathp,
204 (char *)pp->value);
205 }
206 }
207 if (!dryrun) {
208 *prev_pp = NULL;
209 if (!has_name)
210 np->name = of_get_property(np, "name", NULL);
211 np->type = of_get_property(np, "device_type", NULL);
212
213 if (!np->name)
214 np->name = "<NULL>";
215 if (!np->type)
216 np->type = "<NULL>"; }
217
218 old_depth = depth;
219 *poffset = fdt_next_node(blob, *poffset, &depth);
220 if (depth < 0)
221 depth = 0;
222 while (*poffset > 0 && depth > old_depth) {
223 mem = unflatten_dt_node(blob, mem, poffset, np, NULL,
224 fpsize, dryrun);
225 if (!mem)
226 return NULL;
227 }
228
229 if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND) {
230 debug("unflatten: error %d processing FDT\n", *poffset);
231 return NULL;
232 }
233
234
235
236
237
238 if (!dryrun && np->child) {
239 struct device_node *child = np->child;
240 np->child = NULL;
241 while (child) {
242 struct device_node *next = child->sibling;
243
244 child->sibling = np->child;
245 np->child = child;
246 child = next;
247 }
248 }
249
250 if (nodepp)
251 *nodepp = np;
252
253 return mem;
254}
255
256int unflatten_device_tree(const void *blob, struct device_node **mynodes)
257{
258 unsigned long size;
259 int start;
260 void *mem;
261
262 debug(" -> unflatten_device_tree()\n");
263
264 if (!blob) {
265 debug("No device tree pointer\n");
266 return -EINVAL;
267 }
268
269 debug("Unflattening device tree:\n");
270 debug("magic: %08x\n", fdt_magic(blob));
271 debug("size: %08x\n", fdt_totalsize(blob));
272 debug("version: %08x\n", fdt_version(blob));
273
274 if (fdt_check_header(blob)) {
275 debug("Invalid device tree blob header\n");
276 return -EINVAL;
277 }
278
279
280 start = 0;
281 size = (unsigned long)unflatten_dt_node(blob, NULL, &start, NULL, NULL,
282 0, true);
283 if (!size)
284 return -EFAULT;
285 size = ALIGN(size, 4);
286
287 debug(" size is %lx, allocating...\n", size);
288
289
290 mem = malloc(size + 4);
291 memset(mem, '\0', size);
292
293 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
294
295 debug(" unflattening %p...\n", mem);
296
297
298 start = 0;
299 unflatten_dt_node(blob, mem, &start, NULL, mynodes, 0, false);
300 if (be32_to_cpup(mem + size) != 0xdeadbeef) {
301 debug("End of tree marker overwritten: %08x\n",
302 be32_to_cpup(mem + size));
303 return -ENOSPC;
304 }
305
306 debug(" <- unflatten_device_tree()\n");
307
308 return 0;
309}
310
311int of_live_build(const void *fdt_blob, struct device_node **rootp)
312{
313 int ret;
314
315 debug("%s: start\n", __func__);
316 ret = unflatten_device_tree(fdt_blob, rootp);
317 if (ret) {
318 debug("Failed to create live tree: err=%d\n", ret);
319 return ret;
320 }
321 ret = of_alias_scan();
322 if (ret) {
323 debug("Failed to scan live tree aliases: err=%d\n", ret);
324 return ret;
325 }
326 debug("%s: stop\n", __func__);
327
328 return ret;
329}
330