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