fdt_overlay.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
  2. /*
  3. * libfdt - Flat Device Tree manipulation
  4. * Copyright (C) 2016 Free Electrons
  5. * Copyright (C) 2016 NextThing Co.
  6. */
  7. #include "libfdt_env.h"
  8. #include <dragonstub/dragonstub.h>
  9. #include <fdt.h>
  10. #include <libfdt.h>
  11. #include "libfdt_internal.h"
  12. /**
  13. * overlay_get_target_phandle - retrieves the target phandle of a fragment
  14. * @fdto: pointer to the device tree overlay blob
  15. * @fragment: node offset of the fragment in the overlay
  16. *
  17. * overlay_get_target_phandle() retrieves the target phandle of an
  18. * overlay fragment when that fragment uses a phandle (target
  19. * property) instead of a path (target-path property).
  20. *
  21. * returns:
  22. * the phandle pointed by the target property
  23. * 0, if the phandle was not found
  24. * -1, if the phandle was malformed
  25. */
  26. static uint32_t overlay_get_target_phandle(const void *fdto, int fragment)
  27. {
  28. const fdt32_t *val;
  29. int len;
  30. val = fdt_getprop(fdto, fragment, "target", &len);
  31. if (!val)
  32. return 0;
  33. if ((len != sizeof(*val)) || (fdt32_to_cpu(*val) == (uint32_t)-1))
  34. return (uint32_t)-1;
  35. return fdt32_to_cpu(*val);
  36. }
  37. int fdt_overlay_target_offset(const void *fdt, const void *fdto,
  38. int fragment_offset, char const **pathp)
  39. {
  40. uint32_t phandle;
  41. const char *path = NULL;
  42. int path_len = 0, ret;
  43. /* Try first to do a phandle based lookup */
  44. phandle = overlay_get_target_phandle(fdto, fragment_offset);
  45. if (phandle == (uint32_t)-1)
  46. return -FDT_ERR_BADPHANDLE;
  47. /* no phandle, try path */
  48. if (!phandle) {
  49. /* And then a path based lookup */
  50. path = fdt_getprop(fdto, fragment_offset, "target-path", &path_len);
  51. if (path)
  52. ret = fdt_path_offset(fdt, path);
  53. else
  54. ret = path_len;
  55. } else
  56. ret = fdt_node_offset_by_phandle(fdt, phandle);
  57. /*
  58. * If we haven't found either a target or a
  59. * target-path property in a node that contains a
  60. * __overlay__ subnode (we wouldn't be called
  61. * otherwise), consider it a improperly written
  62. * overlay
  63. */
  64. if (ret < 0 && path_len == -FDT_ERR_NOTFOUND)
  65. ret = -FDT_ERR_BADOVERLAY;
  66. /* return on error */
  67. if (ret < 0)
  68. return ret;
  69. /* return pointer to path (if available) */
  70. if (pathp)
  71. *pathp = path ? path : NULL;
  72. return ret;
  73. }
  74. /**
  75. * overlay_phandle_add_offset - Increases a phandle by an offset
  76. * @fdt: Base device tree blob
  77. * @node: Device tree overlay blob
  78. * @name: Name of the property to modify (phandle or linux,phandle)
  79. * @delta: offset to apply
  80. *
  81. * overlay_phandle_add_offset() increments a node phandle by a given
  82. * offset.
  83. *
  84. * returns:
  85. * 0 on success.
  86. * Negative error code on error
  87. */
  88. static int overlay_phandle_add_offset(void *fdt, int node,
  89. const char *name, uint32_t delta)
  90. {
  91. const fdt32_t *val;
  92. uint32_t adj_val;
  93. int len;
  94. val = fdt_getprop(fdt, node, name, &len);
  95. if (!val)
  96. return len;
  97. if (len != sizeof(*val))
  98. return -FDT_ERR_BADPHANDLE;
  99. adj_val = fdt32_to_cpu(*val);
  100. if ((adj_val + delta) < adj_val)
  101. return -FDT_ERR_NOPHANDLES;
  102. adj_val += delta;
  103. if (adj_val == (uint32_t)-1)
  104. return -FDT_ERR_NOPHANDLES;
  105. return fdt_setprop_inplace_u32(fdt, node, name, adj_val);
  106. }
  107. /**
  108. * overlay_adjust_node_phandles - Offsets the phandles of a node
  109. * @fdto: Device tree overlay blob
  110. * @node: Offset of the node we want to adjust
  111. * @delta: Offset to shift the phandles of
  112. *
  113. * overlay_adjust_node_phandles() adds a constant to all the phandles
  114. * of a given node. This is mainly use as part of the overlay
  115. * application process, when we want to update all the overlay
  116. * phandles to not conflict with the overlays of the base device tree.
  117. *
  118. * returns:
  119. * 0 on success
  120. * Negative error code on failure
  121. */
  122. static int overlay_adjust_node_phandles(void *fdto, int node,
  123. uint32_t delta)
  124. {
  125. int child;
  126. int ret;
  127. ret = overlay_phandle_add_offset(fdto, node, "phandle", delta);
  128. if (ret && ret != -FDT_ERR_NOTFOUND)
  129. return ret;
  130. ret = overlay_phandle_add_offset(fdto, node, "linux,phandle", delta);
  131. if (ret && ret != -FDT_ERR_NOTFOUND)
  132. return ret;
  133. fdt_for_each_subnode(child, fdto, node) {
  134. ret = overlay_adjust_node_phandles(fdto, child, delta);
  135. if (ret)
  136. return ret;
  137. }
  138. return 0;
  139. }
  140. /**
  141. * overlay_adjust_local_phandles - Adjust the phandles of a whole overlay
  142. * @fdto: Device tree overlay blob
  143. * @delta: Offset to shift the phandles of
  144. *
  145. * overlay_adjust_local_phandles() adds a constant to all the
  146. * phandles of an overlay. This is mainly use as part of the overlay
  147. * application process, when we want to update all the overlay
  148. * phandles to not conflict with the overlays of the base device tree.
  149. *
  150. * returns:
  151. * 0 on success
  152. * Negative error code on failure
  153. */
  154. static int overlay_adjust_local_phandles(void *fdto, uint32_t delta)
  155. {
  156. /*
  157. * Start adjusting the phandles from the overlay root
  158. */
  159. return overlay_adjust_node_phandles(fdto, 0, delta);
  160. }
  161. /**
  162. * overlay_update_local_node_references - Adjust the overlay references
  163. * @fdto: Device tree overlay blob
  164. * @tree_node: Node offset of the node to operate on
  165. * @fixup_node: Node offset of the matching local fixups node
  166. * @delta: Offset to shift the phandles of
  167. *
  168. * overlay_update_local_nodes_references() update the phandles
  169. * pointing to a node within the device tree overlay by adding a
  170. * constant delta.
  171. *
  172. * This is mainly used as part of a device tree application process,
  173. * where you want the device tree overlays phandles to not conflict
  174. * with the ones from the base device tree before merging them.
  175. *
  176. * returns:
  177. * 0 on success
  178. * Negative error code on failure
  179. */
  180. static int overlay_update_local_node_references(void *fdto,
  181. int tree_node,
  182. int fixup_node,
  183. uint32_t delta)
  184. {
  185. int fixup_prop;
  186. int fixup_child;
  187. int ret;
  188. fdt_for_each_property_offset(fixup_prop, fdto, fixup_node) {
  189. const fdt32_t *fixup_val;
  190. const char *tree_val;
  191. const char *name;
  192. int fixup_len;
  193. int tree_len;
  194. int i;
  195. fixup_val = fdt_getprop_by_offset(fdto, fixup_prop,
  196. &name, &fixup_len);
  197. if (!fixup_val)
  198. return fixup_len;
  199. if (fixup_len % sizeof(uint32_t))
  200. return -FDT_ERR_BADOVERLAY;
  201. fixup_len /= sizeof(uint32_t);
  202. tree_val = fdt_getprop(fdto, tree_node, name, &tree_len);
  203. if (!tree_val) {
  204. if (tree_len == -FDT_ERR_NOTFOUND)
  205. return -FDT_ERR_BADOVERLAY;
  206. return tree_len;
  207. }
  208. for (i = 0; i < fixup_len; i++) {
  209. fdt32_t adj_val;
  210. uint32_t poffset;
  211. poffset = fdt32_to_cpu(fixup_val[i]);
  212. /*
  213. * phandles to fixup can be unaligned.
  214. *
  215. * Use a memcpy for the architectures that do
  216. * not support unaligned accesses.
  217. */
  218. memcpy(&adj_val, tree_val + poffset, sizeof(adj_val));
  219. adj_val = cpu_to_fdt32(fdt32_to_cpu(adj_val) + delta);
  220. ret = fdt_setprop_inplace_namelen_partial(fdto,
  221. tree_node,
  222. name,
  223. strlen(name),
  224. poffset,
  225. &adj_val,
  226. sizeof(adj_val));
  227. if (ret == -FDT_ERR_NOSPACE)
  228. return -FDT_ERR_BADOVERLAY;
  229. if (ret)
  230. return ret;
  231. }
  232. }
  233. fdt_for_each_subnode(fixup_child, fdto, fixup_node) {
  234. const char *fixup_child_name = fdt_get_name(fdto, fixup_child,
  235. NULL);
  236. int tree_child;
  237. tree_child = fdt_subnode_offset(fdto, tree_node,
  238. fixup_child_name);
  239. if (tree_child == -FDT_ERR_NOTFOUND)
  240. return -FDT_ERR_BADOVERLAY;
  241. if (tree_child < 0)
  242. return tree_child;
  243. ret = overlay_update_local_node_references(fdto,
  244. tree_child,
  245. fixup_child,
  246. delta);
  247. if (ret)
  248. return ret;
  249. }
  250. return 0;
  251. }
  252. /**
  253. * overlay_update_local_references - Adjust the overlay references
  254. * @fdto: Device tree overlay blob
  255. * @delta: Offset to shift the phandles of
  256. *
  257. * overlay_update_local_references() update all the phandles pointing
  258. * to a node within the device tree overlay by adding a constant
  259. * delta to not conflict with the base overlay.
  260. *
  261. * This is mainly used as part of a device tree application process,
  262. * where you want the device tree overlays phandles to not conflict
  263. * with the ones from the base device tree before merging them.
  264. *
  265. * returns:
  266. * 0 on success
  267. * Negative error code on failure
  268. */
  269. static int overlay_update_local_references(void *fdto, uint32_t delta)
  270. {
  271. int fixups;
  272. fixups = fdt_path_offset(fdto, "/__local_fixups__");
  273. if (fixups < 0) {
  274. /* There's no local phandles to adjust, bail out */
  275. if (fixups == -FDT_ERR_NOTFOUND)
  276. return 0;
  277. return fixups;
  278. }
  279. /*
  280. * Update our local references from the root of the tree
  281. */
  282. return overlay_update_local_node_references(fdto, 0, fixups,
  283. delta);
  284. }
  285. /**
  286. * overlay_fixup_one_phandle - Set an overlay phandle to the base one
  287. * @fdt: Base Device Tree blob
  288. * @fdto: Device tree overlay blob
  289. * @symbols_off: Node offset of the symbols node in the base device tree
  290. * @path: Path to a node holding a phandle in the overlay
  291. * @path_len: number of path characters to consider
  292. * @name: Name of the property holding the phandle reference in the overlay
  293. * @name_len: number of name characters to consider
  294. * @poffset: Offset within the overlay property where the phandle is stored
  295. * @label: Label of the node referenced by the phandle
  296. *
  297. * overlay_fixup_one_phandle() resolves an overlay phandle pointing to
  298. * a node in the base device tree.
  299. *
  300. * This is part of the device tree overlay application process, when
  301. * you want all the phandles in the overlay to point to the actual
  302. * base dt nodes.
  303. *
  304. * returns:
  305. * 0 on success
  306. * Negative error code on failure
  307. */
  308. static int overlay_fixup_one_phandle(void *fdt, void *fdto,
  309. int symbols_off,
  310. const char *path, uint32_t path_len,
  311. const char *name, uint32_t name_len,
  312. int poffset, const char *label)
  313. {
  314. const char *symbol_path;
  315. uint32_t phandle;
  316. fdt32_t phandle_prop;
  317. int symbol_off, fixup_off;
  318. int prop_len;
  319. if (symbols_off < 0)
  320. return symbols_off;
  321. symbol_path = fdt_getprop(fdt, symbols_off, label,
  322. &prop_len);
  323. if (!symbol_path)
  324. return prop_len;
  325. symbol_off = fdt_path_offset(fdt, symbol_path);
  326. if (symbol_off < 0)
  327. return symbol_off;
  328. phandle = fdt_get_phandle(fdt, symbol_off);
  329. if (!phandle)
  330. return -FDT_ERR_NOTFOUND;
  331. fixup_off = fdt_path_offset_namelen(fdto, path, path_len);
  332. if (fixup_off == -FDT_ERR_NOTFOUND)
  333. return -FDT_ERR_BADOVERLAY;
  334. if (fixup_off < 0)
  335. return fixup_off;
  336. phandle_prop = cpu_to_fdt32(phandle);
  337. return fdt_setprop_inplace_namelen_partial(fdto, fixup_off,
  338. name, name_len, poffset,
  339. &phandle_prop,
  340. sizeof(phandle_prop));
  341. };
  342. /**
  343. * overlay_fixup_phandle - Set an overlay phandle to the base one
  344. * @fdt: Base Device Tree blob
  345. * @fdto: Device tree overlay blob
  346. * @symbols_off: Node offset of the symbols node in the base device tree
  347. * @property: Property offset in the overlay holding the list of fixups
  348. *
  349. * overlay_fixup_phandle() resolves all the overlay phandles pointed
  350. * to in a __fixups__ property, and updates them to match the phandles
  351. * in use in the base device tree.
  352. *
  353. * This is part of the device tree overlay application process, when
  354. * you want all the phandles in the overlay to point to the actual
  355. * base dt nodes.
  356. *
  357. * returns:
  358. * 0 on success
  359. * Negative error code on failure
  360. */
  361. static int overlay_fixup_phandle(void *fdt, void *fdto, int symbols_off,
  362. int property)
  363. {
  364. const char *value;
  365. const char *label;
  366. int len;
  367. value = fdt_getprop_by_offset(fdto, property,
  368. &label, &len);
  369. if (!value) {
  370. if (len == -FDT_ERR_NOTFOUND)
  371. return -FDT_ERR_INTERNAL;
  372. return len;
  373. }
  374. do {
  375. const char *path, *name, *fixup_end;
  376. const char *fixup_str = value;
  377. uint32_t path_len, name_len;
  378. uint32_t fixup_len;
  379. char *sep, *endptr;
  380. int poffset, ret;
  381. fixup_end = memchr(value, '\0', len);
  382. if (!fixup_end)
  383. return -FDT_ERR_BADOVERLAY;
  384. fixup_len = fixup_end - fixup_str;
  385. len -= fixup_len + 1;
  386. value += fixup_len + 1;
  387. path = fixup_str;
  388. sep = memchr(fixup_str, ':', fixup_len);
  389. if (!sep || *sep != ':')
  390. return -FDT_ERR_BADOVERLAY;
  391. path_len = sep - path;
  392. if (path_len == (fixup_len - 1))
  393. return -FDT_ERR_BADOVERLAY;
  394. fixup_len -= path_len + 1;
  395. name = sep + 1;
  396. sep = memchr(name, ':', fixup_len);
  397. if (!sep || *sep != ':')
  398. return -FDT_ERR_BADOVERLAY;
  399. name_len = sep - name;
  400. if (!name_len)
  401. return -FDT_ERR_BADOVERLAY;
  402. poffset = strtoul(sep + 1, &endptr, 10);
  403. if ((*endptr != '\0') || (endptr <= (sep + 1)))
  404. return -FDT_ERR_BADOVERLAY;
  405. ret = overlay_fixup_one_phandle(fdt, fdto, symbols_off,
  406. path, path_len, name, name_len,
  407. poffset, label);
  408. if (ret)
  409. return ret;
  410. } while (len > 0);
  411. return 0;
  412. }
  413. /**
  414. * overlay_fixup_phandles - Resolve the overlay phandles to the base
  415. * device tree
  416. * @fdt: Base Device Tree blob
  417. * @fdto: Device tree overlay blob
  418. *
  419. * overlay_fixup_phandles() resolves all the overlay phandles pointing
  420. * to nodes in the base device tree.
  421. *
  422. * This is one of the steps of the device tree overlay application
  423. * process, when you want all the phandles in the overlay to point to
  424. * the actual base dt nodes.
  425. *
  426. * returns:
  427. * 0 on success
  428. * Negative error code on failure
  429. */
  430. static int overlay_fixup_phandles(void *fdt, void *fdto)
  431. {
  432. int fixups_off, symbols_off;
  433. int property;
  434. /* We can have overlays without any fixups */
  435. fixups_off = fdt_path_offset(fdto, "/__fixups__");
  436. if (fixups_off == -FDT_ERR_NOTFOUND)
  437. return 0; /* nothing to do */
  438. if (fixups_off < 0)
  439. return fixups_off;
  440. /* And base DTs without symbols */
  441. symbols_off = fdt_path_offset(fdt, "/__symbols__");
  442. if ((symbols_off < 0 && (symbols_off != -FDT_ERR_NOTFOUND)))
  443. return symbols_off;
  444. fdt_for_each_property_offset(property, fdto, fixups_off) {
  445. int ret;
  446. ret = overlay_fixup_phandle(fdt, fdto, symbols_off, property);
  447. if (ret)
  448. return ret;
  449. }
  450. return 0;
  451. }
  452. /**
  453. * overlay_apply_node - Merges a node into the base device tree
  454. * @fdt: Base Device Tree blob
  455. * @target: Node offset in the base device tree to apply the fragment to
  456. * @fdto: Device tree overlay blob
  457. * @node: Node offset in the overlay holding the changes to merge
  458. *
  459. * overlay_apply_node() merges a node into a target base device tree
  460. * node pointed.
  461. *
  462. * This is part of the final step in the device tree overlay
  463. * application process, when all the phandles have been adjusted and
  464. * resolved and you just have to merge overlay into the base device
  465. * tree.
  466. *
  467. * returns:
  468. * 0 on success
  469. * Negative error code on failure
  470. */
  471. static int overlay_apply_node(void *fdt, int target,
  472. void *fdto, int node)
  473. {
  474. int property;
  475. int subnode;
  476. fdt_for_each_property_offset(property, fdto, node) {
  477. const char *name;
  478. const void *prop;
  479. int prop_len;
  480. int ret;
  481. prop = fdt_getprop_by_offset(fdto, property, &name,
  482. &prop_len);
  483. if (prop_len == -FDT_ERR_NOTFOUND)
  484. return -FDT_ERR_INTERNAL;
  485. if (prop_len < 0)
  486. return prop_len;
  487. ret = fdt_setprop(fdt, target, name, prop, prop_len);
  488. if (ret)
  489. return ret;
  490. }
  491. fdt_for_each_subnode(subnode, fdto, node) {
  492. const char *name = fdt_get_name(fdto, subnode, NULL);
  493. int nnode;
  494. int ret;
  495. nnode = fdt_add_subnode(fdt, target, name);
  496. if (nnode == -FDT_ERR_EXISTS) {
  497. nnode = fdt_subnode_offset(fdt, target, name);
  498. if (nnode == -FDT_ERR_NOTFOUND)
  499. return -FDT_ERR_INTERNAL;
  500. }
  501. if (nnode < 0)
  502. return nnode;
  503. ret = overlay_apply_node(fdt, nnode, fdto, subnode);
  504. if (ret)
  505. return ret;
  506. }
  507. return 0;
  508. }
  509. /**
  510. * overlay_merge - Merge an overlay into its base device tree
  511. * @fdt: Base Device Tree blob
  512. * @fdto: Device tree overlay blob
  513. *
  514. * overlay_merge() merges an overlay into its base device tree.
  515. *
  516. * This is the next to last step in the device tree overlay application
  517. * process, when all the phandles have been adjusted and resolved and
  518. * you just have to merge overlay into the base device tree.
  519. *
  520. * returns:
  521. * 0 on success
  522. * Negative error code on failure
  523. */
  524. static int overlay_merge(void *fdt, void *fdto)
  525. {
  526. int fragment;
  527. fdt_for_each_subnode(fragment, fdto, 0) {
  528. int overlay;
  529. int target;
  530. int ret;
  531. /*
  532. * Each fragments will have an __overlay__ node. If
  533. * they don't, it's not supposed to be merged
  534. */
  535. overlay = fdt_subnode_offset(fdto, fragment, "__overlay__");
  536. if (overlay == -FDT_ERR_NOTFOUND)
  537. continue;
  538. if (overlay < 0)
  539. return overlay;
  540. target = fdt_overlay_target_offset(fdt, fdto, fragment, NULL);
  541. if (target < 0)
  542. return target;
  543. ret = overlay_apply_node(fdt, target, fdto, overlay);
  544. if (ret)
  545. return ret;
  546. }
  547. return 0;
  548. }
  549. static int get_path_len(const void *fdt, int nodeoffset)
  550. {
  551. int len = 0, namelen;
  552. const char *name;
  553. FDT_RO_PROBE(fdt);
  554. for (;;) {
  555. name = fdt_get_name(fdt, nodeoffset, &namelen);
  556. if (!name)
  557. return namelen;
  558. /* root? we're done */
  559. if (namelen == 0)
  560. break;
  561. nodeoffset = fdt_parent_offset(fdt, nodeoffset);
  562. if (nodeoffset < 0)
  563. return nodeoffset;
  564. len += namelen + 1;
  565. }
  566. /* in case of root pretend it's "/" */
  567. if (len == 0)
  568. len++;
  569. return len;
  570. }
  571. /**
  572. * overlay_symbol_update - Update the symbols of base tree after a merge
  573. * @fdt: Base Device Tree blob
  574. * @fdto: Device tree overlay blob
  575. *
  576. * overlay_symbol_update() updates the symbols of the base tree with the
  577. * symbols of the applied overlay
  578. *
  579. * This is the last step in the device tree overlay application
  580. * process, allowing the reference of overlay symbols by subsequent
  581. * overlay operations.
  582. *
  583. * returns:
  584. * 0 on success
  585. * Negative error code on failure
  586. */
  587. static int overlay_symbol_update(void *fdt, void *fdto)
  588. {
  589. int root_sym, ov_sym, prop, path_len, fragment, target;
  590. int len, frag_name_len, ret, rel_path_len;
  591. const char *s, *e;
  592. const char *path;
  593. const char *name;
  594. const char *frag_name;
  595. const char *rel_path;
  596. const char *target_path;
  597. char *buf;
  598. void *p;
  599. ov_sym = fdt_subnode_offset(fdto, 0, "__symbols__");
  600. /* if no overlay symbols exist no problem */
  601. if (ov_sym < 0)
  602. return 0;
  603. root_sym = fdt_subnode_offset(fdt, 0, "__symbols__");
  604. /* it no root symbols exist we should create them */
  605. if (root_sym == -FDT_ERR_NOTFOUND)
  606. root_sym = fdt_add_subnode(fdt, 0, "__symbols__");
  607. /* any error is fatal now */
  608. if (root_sym < 0)
  609. return root_sym;
  610. /* iterate over each overlay symbol */
  611. fdt_for_each_property_offset(prop, fdto, ov_sym) {
  612. path = fdt_getprop_by_offset(fdto, prop, &name, &path_len);
  613. if (!path)
  614. return path_len;
  615. /* verify it's a string property (terminated by a single \0) */
  616. if (path_len < 1 || memchr(path, '\0', path_len) != &path[path_len - 1])
  617. return -FDT_ERR_BADVALUE;
  618. /* keep end marker to avoid strlen() */
  619. e = path + path_len;
  620. if (*path != '/')
  621. return -FDT_ERR_BADVALUE;
  622. /* get fragment name first */
  623. s = strchr(path + 1, '/');
  624. if (!s) {
  625. /* Symbol refers to something that won't end
  626. * up in the target tree */
  627. continue;
  628. }
  629. frag_name = path + 1;
  630. frag_name_len = s - path - 1;
  631. /* verify format; safe since "s" lies in \0 terminated prop */
  632. len = sizeof("/__overlay__/") - 1;
  633. if ((e - s) > len && (memcmp(s, "/__overlay__/", len) == 0)) {
  634. /* /<fragment-name>/__overlay__/<relative-subnode-path> */
  635. rel_path = s + len;
  636. rel_path_len = e - rel_path - 1;
  637. } else if ((e - s) == len
  638. && (memcmp(s, "/__overlay__", len - 1) == 0)) {
  639. /* /<fragment-name>/__overlay__ */
  640. rel_path = "";
  641. rel_path_len = 0;
  642. } else {
  643. /* Symbol refers to something that won't end
  644. * up in the target tree */
  645. continue;
  646. }
  647. /* find the fragment index in which the symbol lies */
  648. ret = fdt_subnode_offset_namelen(fdto, 0, frag_name,
  649. frag_name_len);
  650. /* not found? */
  651. if (ret < 0)
  652. return -FDT_ERR_BADOVERLAY;
  653. fragment = ret;
  654. /* an __overlay__ subnode must exist */
  655. ret = fdt_subnode_offset(fdto, fragment, "__overlay__");
  656. if (ret < 0)
  657. return -FDT_ERR_BADOVERLAY;
  658. /* get the target of the fragment */
  659. ret = fdt_overlay_target_offset(fdt, fdto, fragment, &target_path);
  660. if (ret < 0)
  661. return ret;
  662. target = ret;
  663. /* if we have a target path use */
  664. if (!target_path) {
  665. ret = get_path_len(fdt, target);
  666. if (ret < 0)
  667. return ret;
  668. len = ret;
  669. } else {
  670. len = strlen(target_path);
  671. }
  672. ret = fdt_setprop_placeholder(fdt, root_sym, name,
  673. len + (len > 1) + rel_path_len + 1, &p);
  674. if (ret < 0)
  675. return ret;
  676. if (!target_path) {
  677. /* again in case setprop_placeholder changed it */
  678. ret = fdt_overlay_target_offset(fdt, fdto, fragment, &target_path);
  679. if (ret < 0)
  680. return ret;
  681. target = ret;
  682. }
  683. buf = p;
  684. if (len > 1) { /* target is not root */
  685. if (!target_path) {
  686. ret = fdt_get_path(fdt, target, buf, len + 1);
  687. if (ret < 0)
  688. return ret;
  689. } else
  690. memcpy(buf, target_path, len + 1);
  691. } else
  692. len--;
  693. buf[len] = '/';
  694. memcpy(buf + len + 1, rel_path, rel_path_len);
  695. buf[len + 1 + rel_path_len] = '\0';
  696. }
  697. return 0;
  698. }
  699. int fdt_overlay_apply(void *fdt, void *fdto)
  700. {
  701. uint32_t delta;
  702. int ret;
  703. FDT_RO_PROBE(fdt);
  704. FDT_RO_PROBE(fdto);
  705. ret = fdt_find_max_phandle(fdt, &delta);
  706. if (ret)
  707. goto err;
  708. ret = overlay_adjust_local_phandles(fdto, delta);
  709. if (ret)
  710. goto err;
  711. ret = overlay_update_local_references(fdto, delta);
  712. if (ret)
  713. goto err;
  714. ret = overlay_fixup_phandles(fdt, fdto);
  715. if (ret)
  716. goto err;
  717. ret = overlay_merge(fdt, fdto);
  718. if (ret)
  719. goto err;
  720. ret = overlay_symbol_update(fdt, fdto);
  721. if (ret)
  722. goto err;
  723. /*
  724. * The overlay has been damaged, erase its magic.
  725. */
  726. fdt_set_magic(fdto, ~0);
  727. return 0;
  728. err:
  729. /*
  730. * The overlay might have been damaged, erase its magic.
  731. */
  732. fdt_set_magic(fdto, ~0);
  733. /*
  734. * The base device tree might have been damaged, erase its
  735. * magic.
  736. */
  737. fdt_set_magic(fdt, ~0);
  738. return ret;
  739. }