of_live.c 7.8 KB

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