of_live.c 7.9 KB

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