1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright 2009 Benjamin Herrenschmidt, IBM Corp
3*4882a593Smuzhiyun * benh@kernel.crashing.org
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Based on parts of drivers/of/fdt.c from Linux v4.9
6*4882a593Smuzhiyun * Modifications for U-Boot
7*4882a593Smuzhiyun * Copyright (c) 2017 Google, Inc
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <common.h>
13*4882a593Smuzhiyun #include <linux/libfdt.h>
14*4882a593Smuzhiyun #include <of_live.h>
15*4882a593Smuzhiyun #include <malloc.h>
16*4882a593Smuzhiyun #include <dm/of_access.h>
17*4882a593Smuzhiyun #include <linux/err.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
20*4882a593Smuzhiyun
unflatten_dt_alloc(void ** mem,unsigned long size,unsigned long align)21*4882a593Smuzhiyun static void *unflatten_dt_alloc(void **mem, unsigned long size,
22*4882a593Smuzhiyun unsigned long align)
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun void *res;
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun *mem = PTR_ALIGN(*mem, align);
27*4882a593Smuzhiyun res = *mem;
28*4882a593Smuzhiyun *mem += size;
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun return res;
31*4882a593Smuzhiyun }
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun /**
34*4882a593Smuzhiyun * unflatten_dt_node() - Alloc and populate a device_node from the flat tree
35*4882a593Smuzhiyun * @blob: The parent device tree blob
36*4882a593Smuzhiyun * @mem: Memory chunk to use for allocating device nodes and properties
37*4882a593Smuzhiyun * @poffset: pointer to node in flat tree
38*4882a593Smuzhiyun * @dad: Parent struct device_node
39*4882a593Smuzhiyun * @nodepp: The device_node tree created by the call
40*4882a593Smuzhiyun * @fpsize: Size of the node path up at t05he current depth.
41*4882a593Smuzhiyun * @dryrun: If true, do not allocate device nodes but still calculate needed
42*4882a593Smuzhiyun * memory size
43*4882a593Smuzhiyun */
unflatten_dt_node(const void * blob,void * mem,int * poffset,struct device_node * dad,struct device_node ** nodepp,unsigned long fpsize,bool dryrun)44*4882a593Smuzhiyun static void *unflatten_dt_node(const void *blob, void *mem, int *poffset,
45*4882a593Smuzhiyun struct device_node *dad,
46*4882a593Smuzhiyun struct device_node **nodepp,
47*4882a593Smuzhiyun unsigned long fpsize, bool dryrun)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun const __be32 *p;
50*4882a593Smuzhiyun struct device_node *np;
51*4882a593Smuzhiyun struct property *pp, **prev_pp = NULL;
52*4882a593Smuzhiyun const char *pathp;
53*4882a593Smuzhiyun int l;
54*4882a593Smuzhiyun unsigned int allocl;
55*4882a593Smuzhiyun static int depth;
56*4882a593Smuzhiyun int old_depth;
57*4882a593Smuzhiyun int offset;
58*4882a593Smuzhiyun int has_name = 0;
59*4882a593Smuzhiyun int new_format = 0;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun pathp = fdt_get_name(blob, *poffset, &l);
62*4882a593Smuzhiyun if (!pathp)
63*4882a593Smuzhiyun return mem;
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun allocl = ++l;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun /*
68*4882a593Smuzhiyun * version 0x10 has a more compact unit name here instead of the full
69*4882a593Smuzhiyun * path. we accumulate the full path size using "fpsize", we'll rebuild
70*4882a593Smuzhiyun * it later. We detect this because the first character of the name is
71*4882a593Smuzhiyun * not '/'.
72*4882a593Smuzhiyun */
73*4882a593Smuzhiyun if ((*pathp) != '/') {
74*4882a593Smuzhiyun new_format = 1;
75*4882a593Smuzhiyun if (fpsize == 0) {
76*4882a593Smuzhiyun /*
77*4882a593Smuzhiyun * root node: special case. fpsize accounts for path
78*4882a593Smuzhiyun * plus terminating zero. root node only has '/', so
79*4882a593Smuzhiyun * fpsize should be 2, but we want to avoid the first
80*4882a593Smuzhiyun * level nodes to have two '/' so we use fpsize 1 here
81*4882a593Smuzhiyun */
82*4882a593Smuzhiyun fpsize = 1;
83*4882a593Smuzhiyun allocl = 2;
84*4882a593Smuzhiyun l = 1;
85*4882a593Smuzhiyun pathp = "";
86*4882a593Smuzhiyun } else {
87*4882a593Smuzhiyun /*
88*4882a593Smuzhiyun * account for '/' and path size minus terminal 0
89*4882a593Smuzhiyun * already in 'l'
90*4882a593Smuzhiyun */
91*4882a593Smuzhiyun fpsize += l;
92*4882a593Smuzhiyun allocl = fpsize;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
97*4882a593Smuzhiyun __alignof__(struct device_node));
98*4882a593Smuzhiyun if (!dryrun) {
99*4882a593Smuzhiyun char *fn;
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun fn = (char *)np + sizeof(*np);
102*4882a593Smuzhiyun np->full_name = fn;
103*4882a593Smuzhiyun if (new_format) {
104*4882a593Smuzhiyun /* rebuild full path for new format */
105*4882a593Smuzhiyun if (dad && dad->parent) {
106*4882a593Smuzhiyun strcpy(fn, dad->full_name);
107*4882a593Smuzhiyun #ifdef DEBUG
108*4882a593Smuzhiyun if ((strlen(fn) + l + 1) != allocl) {
109*4882a593Smuzhiyun debug("%s: p: %d, l: %d, a: %d\n",
110*4882a593Smuzhiyun pathp, (int)strlen(fn), l,
111*4882a593Smuzhiyun allocl);
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun #endif
114*4882a593Smuzhiyun fn += strlen(fn);
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun *(fn++) = '/';
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun memcpy(fn, pathp, l);
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun prev_pp = &np->properties;
121*4882a593Smuzhiyun if (dad != NULL) {
122*4882a593Smuzhiyun np->parent = dad;
123*4882a593Smuzhiyun np->sibling = dad->child;
124*4882a593Smuzhiyun dad->child = np;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun /* process properties */
128*4882a593Smuzhiyun for (offset = fdt_first_property_offset(blob, *poffset);
129*4882a593Smuzhiyun (offset >= 0);
130*4882a593Smuzhiyun (offset = fdt_next_property_offset(blob, offset))) {
131*4882a593Smuzhiyun const char *pname;
132*4882a593Smuzhiyun int sz;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun p = fdt_getprop_by_offset(blob, offset, &pname, &sz);
135*4882a593Smuzhiyun if (!p) {
136*4882a593Smuzhiyun offset = -FDT_ERR_INTERNAL;
137*4882a593Smuzhiyun break;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun if (pname == NULL) {
141*4882a593Smuzhiyun debug("Can't find property name in list !\n");
142*4882a593Smuzhiyun break;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun if (strcmp(pname, "name") == 0)
145*4882a593Smuzhiyun has_name = 1;
146*4882a593Smuzhiyun pp = unflatten_dt_alloc(&mem, sizeof(struct property),
147*4882a593Smuzhiyun __alignof__(struct property));
148*4882a593Smuzhiyun if (!dryrun) {
149*4882a593Smuzhiyun /*
150*4882a593Smuzhiyun * We accept flattened tree phandles either in
151*4882a593Smuzhiyun * ePAPR-style "phandle" properties, or the
152*4882a593Smuzhiyun * legacy "linux,phandle" properties. If both
153*4882a593Smuzhiyun * appear and have different values, things
154*4882a593Smuzhiyun * will get weird. Don't do that. */
155*4882a593Smuzhiyun if ((strcmp(pname, "phandle") == 0) ||
156*4882a593Smuzhiyun (strcmp(pname, "linux,phandle") == 0)) {
157*4882a593Smuzhiyun if (np->phandle == 0)
158*4882a593Smuzhiyun np->phandle = be32_to_cpup(p);
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun /*
161*4882a593Smuzhiyun * And we process the "ibm,phandle" property
162*4882a593Smuzhiyun * used in pSeries dynamic device tree
163*4882a593Smuzhiyun * stuff */
164*4882a593Smuzhiyun if (strcmp(pname, "ibm,phandle") == 0)
165*4882a593Smuzhiyun np->phandle = be32_to_cpup(p);
166*4882a593Smuzhiyun pp->name = (char *)pname;
167*4882a593Smuzhiyun pp->length = sz;
168*4882a593Smuzhiyun pp->value = (__be32 *)p;
169*4882a593Smuzhiyun *prev_pp = pp;
170*4882a593Smuzhiyun prev_pp = &pp->next;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun /*
174*4882a593Smuzhiyun * with version 0x10 we may not have the name property, recreate
175*4882a593Smuzhiyun * it here from the unit name if absent
176*4882a593Smuzhiyun */
177*4882a593Smuzhiyun if (!has_name) {
178*4882a593Smuzhiyun const char *p1 = pathp, *ps = pathp, *pa = NULL;
179*4882a593Smuzhiyun int sz;
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun while (*p1) {
182*4882a593Smuzhiyun if ((*p1) == '@')
183*4882a593Smuzhiyun pa = p1;
184*4882a593Smuzhiyun if ((*p1) == '/')
185*4882a593Smuzhiyun ps = p1 + 1;
186*4882a593Smuzhiyun p1++;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun if (pa < ps)
189*4882a593Smuzhiyun pa = p1;
190*4882a593Smuzhiyun sz = (pa - ps) + 1;
191*4882a593Smuzhiyun pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
192*4882a593Smuzhiyun __alignof__(struct property));
193*4882a593Smuzhiyun if (!dryrun) {
194*4882a593Smuzhiyun pp->name = "name";
195*4882a593Smuzhiyun pp->length = sz;
196*4882a593Smuzhiyun pp->value = pp + 1;
197*4882a593Smuzhiyun *prev_pp = pp;
198*4882a593Smuzhiyun prev_pp = &pp->next;
199*4882a593Smuzhiyun memcpy(pp->value, ps, sz - 1);
200*4882a593Smuzhiyun ((char *)pp->value)[sz - 1] = 0;
201*4882a593Smuzhiyun debug("fixed up name for %s -> %s\n", pathp,
202*4882a593Smuzhiyun (char *)pp->value);
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun if (!dryrun) {
206*4882a593Smuzhiyun *prev_pp = NULL;
207*4882a593Smuzhiyun np->name = of_get_property(np, "name", NULL);
208*4882a593Smuzhiyun np->type = of_get_property(np, "device_type", NULL);
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun if (!np->name)
211*4882a593Smuzhiyun np->name = "<NULL>";
212*4882a593Smuzhiyun if (!np->type)
213*4882a593Smuzhiyun np->type = "<NULL>"; }
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun old_depth = depth;
216*4882a593Smuzhiyun *poffset = fdt_next_node(blob, *poffset, &depth);
217*4882a593Smuzhiyun if (depth < 0)
218*4882a593Smuzhiyun depth = 0;
219*4882a593Smuzhiyun while (*poffset > 0 && depth > old_depth) {
220*4882a593Smuzhiyun mem = unflatten_dt_node(blob, mem, poffset, np, NULL,
221*4882a593Smuzhiyun fpsize, dryrun);
222*4882a593Smuzhiyun if (!mem)
223*4882a593Smuzhiyun return NULL;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND) {
227*4882a593Smuzhiyun debug("unflatten: error %d processing FDT\n", *poffset);
228*4882a593Smuzhiyun return NULL;
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /*
232*4882a593Smuzhiyun * Reverse the child list. Some drivers assumes node order matches .dts
233*4882a593Smuzhiyun * node order
234*4882a593Smuzhiyun */
235*4882a593Smuzhiyun if (!dryrun && np->child) {
236*4882a593Smuzhiyun struct device_node *child = np->child;
237*4882a593Smuzhiyun np->child = NULL;
238*4882a593Smuzhiyun while (child) {
239*4882a593Smuzhiyun struct device_node *next = child->sibling;
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun child->sibling = np->child;
242*4882a593Smuzhiyun np->child = child;
243*4882a593Smuzhiyun child = next;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun if (nodepp)
248*4882a593Smuzhiyun *nodepp = np;
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun return mem;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun /**
254*4882a593Smuzhiyun * unflatten_device_tree() - create tree of device_nodes from flat blob
255*4882a593Smuzhiyun *
256*4882a593Smuzhiyun * unflattens a device-tree, creating the
257*4882a593Smuzhiyun * tree of struct device_node. It also fills the "name" and "type"
258*4882a593Smuzhiyun * pointers of the nodes so the normal device-tree walking functions
259*4882a593Smuzhiyun * can be used.
260*4882a593Smuzhiyun * @blob: The blob to expand
261*4882a593Smuzhiyun * @mynodes: The device_node tree created by the call
262*4882a593Smuzhiyun * @return 0 if OK, -ve on error
263*4882a593Smuzhiyun */
unflatten_device_tree(const void * blob,struct device_node ** mynodes)264*4882a593Smuzhiyun static int unflatten_device_tree(const void *blob,
265*4882a593Smuzhiyun struct device_node **mynodes)
266*4882a593Smuzhiyun {
267*4882a593Smuzhiyun unsigned long size;
268*4882a593Smuzhiyun int start;
269*4882a593Smuzhiyun void *mem;
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun debug(" -> unflatten_device_tree()\n");
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun if (!blob) {
274*4882a593Smuzhiyun debug("No device tree pointer\n");
275*4882a593Smuzhiyun return -EINVAL;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun debug("Unflattening device tree:\n");
279*4882a593Smuzhiyun debug("magic: %08x\n", fdt_magic(blob));
280*4882a593Smuzhiyun debug("size: %08x\n", fdt_totalsize(blob));
281*4882a593Smuzhiyun debug("version: %08x\n", fdt_version(blob));
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun if (fdt_check_header(blob)) {
284*4882a593Smuzhiyun debug("Invalid device tree blob header\n");
285*4882a593Smuzhiyun return -EINVAL;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun /* First pass, scan for size */
289*4882a593Smuzhiyun start = 0;
290*4882a593Smuzhiyun size = (unsigned long)unflatten_dt_node(blob, NULL, &start, NULL, NULL,
291*4882a593Smuzhiyun 0, true);
292*4882a593Smuzhiyun if (!size)
293*4882a593Smuzhiyun return -EFAULT;
294*4882a593Smuzhiyun size = ALIGN(size, 4);
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun debug(" size is %lx, allocating...\n", size);
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun /* Allocate memory for the expanded device tree */
299*4882a593Smuzhiyun mem = malloc(size + 4);
300*4882a593Smuzhiyun memset(mem, '\0', size);
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun debug(" unflattening %p...\n", mem);
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun /* Second pass, do actual unflattening */
307*4882a593Smuzhiyun start = 0;
308*4882a593Smuzhiyun unflatten_dt_node(blob, mem, &start, NULL, mynodes, 0, false);
309*4882a593Smuzhiyun if (be32_to_cpup(mem + size) != 0xdeadbeef) {
310*4882a593Smuzhiyun debug("End of tree marker overwritten: %08x\n",
311*4882a593Smuzhiyun be32_to_cpup(mem + size));
312*4882a593Smuzhiyun return -ENOSPC;
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun debug(" <- unflatten_device_tree()\n");
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun return 0;
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun
of_live_build(const void * fdt_blob,struct device_node ** rootp)320*4882a593Smuzhiyun int of_live_build(const void *fdt_blob, struct device_node **rootp)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun int ret;
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun debug("%s: start\n", __func__);
325*4882a593Smuzhiyun ret = unflatten_device_tree(fdt_blob, rootp);
326*4882a593Smuzhiyun if (ret) {
327*4882a593Smuzhiyun debug("Failed to create live tree: err=%d\n", ret);
328*4882a593Smuzhiyun return ret;
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun ret = of_alias_scan();
331*4882a593Smuzhiyun if (ret) {
332*4882a593Smuzhiyun debug("Failed to scan live tree aliases: err=%d\n", ret);
333*4882a593Smuzhiyun return ret;
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun debug("%s: stop\n", __func__);
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun return ret;
338*4882a593Smuzhiyun }
339