resolver.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Functions for dealing with DT resolution
  4. *
  5. * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
  6. * Copyright (C) 2012 Texas Instruments Inc.
  7. */
  8. #define pr_fmt(fmt) "OF: resolver: " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_device.h>
  13. #include <linux/string.h>
  14. #include <linux/ctype.h>
  15. #include <linux/errno.h>
  16. #include <linux/slab.h>
  17. #include "of_private.h"
  18. static phandle live_tree_max_phandle(void)
  19. {
  20. struct device_node *node;
  21. phandle phandle;
  22. unsigned long flags;
  23. raw_spin_lock_irqsave(&devtree_lock, flags);
  24. phandle = 0;
  25. for_each_of_allnodes(node) {
  26. if (node->phandle != OF_PHANDLE_ILLEGAL &&
  27. node->phandle > phandle)
  28. phandle = node->phandle;
  29. }
  30. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  31. return phandle;
  32. }
  33. static void adjust_overlay_phandles(struct device_node *overlay,
  34. int phandle_delta)
  35. {
  36. struct device_node *child;
  37. struct property *prop;
  38. phandle phandle;
  39. /* adjust node's phandle in node */
  40. if (overlay->phandle != 0 && overlay->phandle != OF_PHANDLE_ILLEGAL)
  41. overlay->phandle += phandle_delta;
  42. /* copy adjusted phandle into *phandle properties */
  43. for_each_property_of_node(overlay, prop) {
  44. if (of_prop_cmp(prop->name, "phandle") &&
  45. of_prop_cmp(prop->name, "linux,phandle"))
  46. continue;
  47. if (prop->length < 4)
  48. continue;
  49. phandle = be32_to_cpup(prop->value);
  50. if (phandle == OF_PHANDLE_ILLEGAL)
  51. continue;
  52. *(__be32 *)prop->value = cpu_to_be32(overlay->phandle);
  53. }
  54. for_each_child_of_node(overlay, child)
  55. adjust_overlay_phandles(child, phandle_delta);
  56. }
  57. static int update_usages_of_a_phandle_reference(struct device_node *overlay,
  58. struct property *prop_fixup, phandle phandle)
  59. {
  60. struct device_node *refnode;
  61. struct property *prop;
  62. char *value, *cur, *end, *node_path, *prop_name, *s;
  63. int offset, len;
  64. int err = 0;
  65. value = kmemdup(prop_fixup->value, prop_fixup->length, GFP_KERNEL);
  66. if (!value)
  67. return -ENOMEM;
  68. /* prop_fixup contains a list of tuples of path:property_name:offset */
  69. end = value + prop_fixup->length;
  70. for (cur = value; cur < end; cur += len + 1) {
  71. len = strlen(cur);
  72. node_path = cur;
  73. s = strchr(cur, ':');
  74. if (!s) {
  75. err = -EINVAL;
  76. goto err_fail;
  77. }
  78. *s++ = '\0';
  79. prop_name = s;
  80. s = strchr(s, ':');
  81. if (!s) {
  82. err = -EINVAL;
  83. goto err_fail;
  84. }
  85. *s++ = '\0';
  86. err = kstrtoint(s, 10, &offset);
  87. if (err)
  88. goto err_fail;
  89. refnode = __of_find_node_by_full_path(of_node_get(overlay), node_path);
  90. if (!refnode)
  91. continue;
  92. for_each_property_of_node(refnode, prop) {
  93. if (!of_prop_cmp(prop->name, prop_name))
  94. break;
  95. }
  96. of_node_put(refnode);
  97. if (!prop) {
  98. err = -ENOENT;
  99. goto err_fail;
  100. }
  101. if (offset < 0 || offset + sizeof(__be32) > prop->length) {
  102. err = -EINVAL;
  103. goto err_fail;
  104. }
  105. *(__be32 *)(prop->value + offset) = cpu_to_be32(phandle);
  106. }
  107. err_fail:
  108. kfree(value);
  109. return err;
  110. }
  111. /* compare nodes taking into account that 'name' strips out the @ part */
  112. static int node_name_cmp(const struct device_node *dn1,
  113. const struct device_node *dn2)
  114. {
  115. const char *n1 = kbasename(dn1->full_name);
  116. const char *n2 = kbasename(dn2->full_name);
  117. return of_node_cmp(n1, n2);
  118. }
  119. /*
  120. * Adjust the local phandle references by the given phandle delta.
  121. *
  122. * Subtree @local_fixups, which is overlay node __local_fixups__,
  123. * mirrors the fragment node structure at the root of the overlay.
  124. *
  125. * For each property in the fragments that contains a phandle reference,
  126. * @local_fixups has a property of the same name that contains a list
  127. * of offsets of the phandle reference(s) within the respective property
  128. * value(s). The values at these offsets will be fixed up.
  129. */
  130. static int adjust_local_phandle_references(struct device_node *local_fixups,
  131. struct device_node *overlay, int phandle_delta)
  132. {
  133. struct device_node *child, *overlay_child;
  134. struct property *prop_fix, *prop;
  135. int err, i, count;
  136. unsigned int off;
  137. if (!local_fixups)
  138. return 0;
  139. for_each_property_of_node(local_fixups, prop_fix) {
  140. /* skip properties added automatically */
  141. if (!of_prop_cmp(prop_fix->name, "name") ||
  142. !of_prop_cmp(prop_fix->name, "phandle") ||
  143. !of_prop_cmp(prop_fix->name, "linux,phandle"))
  144. continue;
  145. if ((prop_fix->length % 4) != 0 || prop_fix->length == 0)
  146. return -EINVAL;
  147. count = prop_fix->length / sizeof(__be32);
  148. for_each_property_of_node(overlay, prop) {
  149. if (!of_prop_cmp(prop->name, prop_fix->name))
  150. break;
  151. }
  152. if (!prop)
  153. return -EINVAL;
  154. for (i = 0; i < count; i++) {
  155. off = be32_to_cpu(((__be32 *)prop_fix->value)[i]);
  156. if ((off + 4) > prop->length)
  157. return -EINVAL;
  158. be32_add_cpu(prop->value + off, phandle_delta);
  159. }
  160. }
  161. /*
  162. * These nested loops recurse down two subtrees in parallel, where the
  163. * node names in the two subtrees match.
  164. *
  165. * The roots of the subtrees are the overlay's __local_fixups__ node
  166. * and the overlay's root node.
  167. */
  168. for_each_child_of_node(local_fixups, child) {
  169. for_each_child_of_node(overlay, overlay_child)
  170. if (!node_name_cmp(child, overlay_child))
  171. break;
  172. if (!overlay_child)
  173. return -EINVAL;
  174. err = adjust_local_phandle_references(child, overlay_child,
  175. phandle_delta);
  176. if (err)
  177. return err;
  178. }
  179. return 0;
  180. }
  181. /**
  182. * of_resolve_phandles - Relocate and resolve overlay against live tree
  183. *
  184. * @overlay: Pointer to devicetree overlay to relocate and resolve
  185. *
  186. * Modify (relocate) values of local phandles in @overlay to a range that
  187. * does not conflict with the live expanded devicetree. Update references
  188. * to the local phandles in @overlay. Update (resolve) phandle references
  189. * in @overlay that refer to the live expanded devicetree.
  190. *
  191. * Phandle values in the live tree are in the range of
  192. * 1 .. live_tree_max_phandle(). The range of phandle values in the overlay
  193. * also begin with at 1. Adjust the phandle values in the overlay to begin
  194. * at live_tree_max_phandle() + 1. Update references to the phandles to
  195. * the adjusted phandle values.
  196. *
  197. * The name of each property in the "__fixups__" node in the overlay matches
  198. * the name of a symbol (a label) in the live tree. The values of each
  199. * property in the "__fixups__" node is a list of the property values in the
  200. * overlay that need to be updated to contain the phandle reference
  201. * corresponding to that symbol in the live tree. Update the references in
  202. * the overlay with the phandle values in the live tree.
  203. *
  204. * @overlay must be detached.
  205. *
  206. * Resolving and applying @overlay to the live expanded devicetree must be
  207. * protected by a mechanism to ensure that multiple overlays are processed
  208. * in a single threaded manner so that multiple overlays will not relocate
  209. * phandles to overlapping ranges. The mechanism to enforce this is not
  210. * yet implemented.
  211. *
  212. * Return: %0 on success or a negative error value on error.
  213. */
  214. int of_resolve_phandles(struct device_node *overlay)
  215. {
  216. struct device_node *child, *local_fixups, *refnode;
  217. struct device_node *tree_symbols, *overlay_fixups;
  218. struct property *prop;
  219. const char *refpath;
  220. phandle phandle, phandle_delta;
  221. int err;
  222. tree_symbols = NULL;
  223. if (!overlay) {
  224. pr_err("null overlay\n");
  225. err = -EINVAL;
  226. goto out;
  227. }
  228. if (!of_node_check_flag(overlay, OF_DETACHED)) {
  229. pr_err("overlay not detached\n");
  230. err = -EINVAL;
  231. goto out;
  232. }
  233. phandle_delta = live_tree_max_phandle() + 1;
  234. adjust_overlay_phandles(overlay, phandle_delta);
  235. for_each_child_of_node(overlay, local_fixups)
  236. if (!of_node_cmp(local_fixups->name, "__local_fixups__"))
  237. break;
  238. err = adjust_local_phandle_references(local_fixups, overlay, phandle_delta);
  239. if (err)
  240. goto out;
  241. overlay_fixups = NULL;
  242. for_each_child_of_node(overlay, child) {
  243. if (!of_node_cmp(child->name, "__fixups__"))
  244. overlay_fixups = child;
  245. }
  246. if (!overlay_fixups) {
  247. err = 0;
  248. goto out;
  249. }
  250. tree_symbols = of_find_node_by_path("/__symbols__");
  251. if (!tree_symbols) {
  252. pr_err("no symbols in root of device tree.\n");
  253. err = -EINVAL;
  254. goto out;
  255. }
  256. for_each_property_of_node(overlay_fixups, prop) {
  257. /* skip properties added automatically */
  258. if (!of_prop_cmp(prop->name, "name"))
  259. continue;
  260. err = of_property_read_string(tree_symbols,
  261. prop->name, &refpath);
  262. if (err)
  263. goto out;
  264. refnode = of_find_node_by_path(refpath);
  265. if (!refnode) {
  266. err = -ENOENT;
  267. goto out;
  268. }
  269. phandle = refnode->phandle;
  270. of_node_put(refnode);
  271. err = update_usages_of_a_phandle_reference(overlay, prop, phandle);
  272. if (err)
  273. break;
  274. }
  275. out:
  276. if (err)
  277. pr_err("overlay phandle fixup failed: %d\n", err);
  278. of_node_put(tree_symbols);
  279. return err;
  280. }
  281. EXPORT_SYMBOL_GPL(of_resolve_phandles);