of_live.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. if (new_format) {
  91. np->name = pathp;
  92. has_name = 1;
  93. }
  94. np->full_name = fn;
  95. if (new_format) {
  96. /* rebuild full path for new format */
  97. if (dad && dad->parent) {
  98. strcpy(fn, dad->full_name);
  99. #ifdef DEBUG
  100. if ((strlen(fn) + l + 1) != allocl) {
  101. debug("%s: p: %d, l: %d, a: %d\n",
  102. pathp, (int)strlen(fn), l,
  103. allocl);
  104. }
  105. #endif
  106. fn += strlen(fn);
  107. }
  108. *(fn++) = '/';
  109. }
  110. memcpy(fn, pathp, l);
  111. prev_pp = &np->properties;
  112. if (dad != NULL) {
  113. np->parent = dad;
  114. np->sibling = dad->child;
  115. dad->child = np;
  116. }
  117. }
  118. /* process properties */
  119. for (offset = fdt_first_property_offset(blob, *poffset);
  120. (offset >= 0);
  121. (offset = fdt_next_property_offset(blob, offset))) {
  122. const char *pname;
  123. int sz;
  124. p = fdt_getprop_by_offset(blob, offset, &pname, &sz);
  125. if (!p) {
  126. offset = -FDT_ERR_INTERNAL;
  127. break;
  128. }
  129. if (pname == NULL) {
  130. debug("Can't find property name in list !\n");
  131. break;
  132. }
  133. if (strcmp(pname, "name") == 0)
  134. has_name = 1;
  135. pp = unflatten_dt_alloc(&mem, sizeof(struct property),
  136. __alignof__(struct property));
  137. if (!dryrun) {
  138. /*
  139. * We accept flattened tree phandles either in
  140. * ePAPR-style "phandle" properties, or the
  141. * legacy "linux,phandle" properties. If both
  142. * appear and have different values, things
  143. * will get weird. Don't do that. */
  144. if ((strcmp(pname, "phandle") == 0) ||
  145. (strcmp(pname, "linux,phandle") == 0)) {
  146. if (np->phandle == 0)
  147. np->phandle = be32_to_cpup(p);
  148. }
  149. /*
  150. * And we process the "ibm,phandle" property
  151. * used in pSeries dynamic device tree
  152. * stuff */
  153. if (strcmp(pname, "ibm,phandle") == 0)
  154. np->phandle = be32_to_cpup(p);
  155. pp->name = (char *)pname;
  156. pp->length = sz;
  157. pp->value = (__be32 *)p;
  158. *prev_pp = pp;
  159. prev_pp = &pp->next;
  160. }
  161. }
  162. /*
  163. * with version 0x10 we may not have the name property, recreate
  164. * it here from the unit name if absent
  165. */
  166. if (!has_name) {
  167. const char *p1 = pathp, *ps = pathp, *pa = NULL;
  168. int sz;
  169. while (*p1) {
  170. if ((*p1) == '@')
  171. pa = p1;
  172. if ((*p1) == '/')
  173. ps = p1 + 1;
  174. p1++;
  175. }
  176. if (pa < ps)
  177. pa = p1;
  178. sz = (pa - ps) + 1;
  179. pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
  180. __alignof__(struct property));
  181. if (!dryrun) {
  182. pp->name = "name";
  183. pp->length = sz;
  184. pp->value = pp + 1;
  185. *prev_pp = pp;
  186. prev_pp = &pp->next;
  187. memcpy(pp->value, ps, sz - 1);
  188. ((char *)pp->value)[sz - 1] = 0;
  189. debug("fixed up name for %s -> %s\n", pathp,
  190. (char *)pp->value);
  191. }
  192. }
  193. if (!dryrun) {
  194. *prev_pp = NULL;
  195. if (!has_name)
  196. np->name = of_get_property(np, "name", NULL);
  197. np->type = of_get_property(np, "device_type", NULL);
  198. if (!np->name)
  199. np->name = "<NULL>";
  200. if (!np->type)
  201. np->type = "<NULL>"; }
  202. old_depth = depth;
  203. *poffset = fdt_next_node(blob, *poffset, &depth);
  204. if (depth < 0)
  205. depth = 0;
  206. while (*poffset > 0 && depth > old_depth) {
  207. mem = unflatten_dt_node(blob, mem, poffset, np, NULL,
  208. fpsize, dryrun);
  209. if (!mem)
  210. return NULL;
  211. }
  212. if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND) {
  213. debug("unflatten: error %d processing FDT\n", *poffset);
  214. return NULL;
  215. }
  216. /*
  217. * Reverse the child list. Some drivers assumes node order matches .dts
  218. * node order
  219. */
  220. if (!dryrun && np->child) {
  221. struct device_node *child = np->child;
  222. np->child = NULL;
  223. while (child) {
  224. struct device_node *next = child->sibling;
  225. child->sibling = np->child;
  226. np->child = child;
  227. child = next;
  228. }
  229. }
  230. if (nodepp)
  231. *nodepp = np;
  232. return mem;
  233. }
  234. int unflatten_device_tree(const void *blob, struct device_node **mynodes)
  235. {
  236. unsigned long size;
  237. int start;
  238. void *mem;
  239. debug(" -> unflatten_device_tree()\n");
  240. if (!blob) {
  241. debug("No device tree pointer\n");
  242. return -EINVAL;
  243. }
  244. debug("Unflattening device tree:\n");
  245. debug("magic: %08x\n", fdt_magic(blob));
  246. debug("size: %08x\n", fdt_totalsize(blob));
  247. debug("version: %08x\n", fdt_version(blob));
  248. if (fdt_check_header(blob)) {
  249. debug("Invalid device tree blob header\n");
  250. return -EINVAL;
  251. }
  252. /* First pass, scan for size */
  253. start = 0;
  254. size = (unsigned long)unflatten_dt_node(blob, NULL, &start, NULL, NULL,
  255. 0, true);
  256. if (!size)
  257. return -EFAULT;
  258. size = ALIGN(size, 4);
  259. debug(" size is %lx, allocating...\n", size);
  260. /* Allocate memory for the expanded device tree */
  261. mem = memalign(__alignof__(struct device_node), size + 4);
  262. memset(mem, '\0', size);
  263. /* Set up value for dm_test_livetree_align() */
  264. *(u32 *)mem = BAD_OF_ROOT;
  265. *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
  266. debug(" unflattening %p...\n", mem);
  267. /* Second pass, do actual unflattening */
  268. start = 0;
  269. unflatten_dt_node(blob, mem, &start, NULL, mynodes, 0, false);
  270. if (be32_to_cpup(mem + size) != 0xdeadbeef) {
  271. debug("End of tree marker overwritten: %08x\n",
  272. be32_to_cpup(mem + size));
  273. return -ENOSPC;
  274. }
  275. debug(" <- unflatten_device_tree()\n");
  276. return 0;
  277. }
  278. int of_live_build(const void *fdt_blob, struct device_node **rootp)
  279. {
  280. int ret;
  281. debug("%s: start\n", __func__);
  282. ret = unflatten_device_tree(fdt_blob, rootp);
  283. if (ret) {
  284. debug("Failed to create live tree: err=%d\n", ret);
  285. return ret;
  286. }
  287. ret = of_alias_scan();
  288. if (ret) {
  289. debug("Failed to scan live tree aliases: err=%d\n", ret);
  290. return ret;
  291. }
  292. debug("%s: stop\n", __func__);
  293. return ret;
  294. }
  295. void of_live_free(struct device_node *root)
  296. {
  297. /* the tree is stored as a contiguous block of memory */
  298. free(root);
  299. }