of_live.c 7.9 KB

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