ufdt_overlay.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /*-
  2. * Copyright (c) 2015 Oleksandr Tymoshenko <gonzo@FreeBSD.org>
  3. * All rights reserved.
  4. *
  5. * This software was developed by Semihalf under sponsorship from
  6. * the FreeBSD Foundation.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include "ufdt_overlay.h"
  30. #include "libufdt.h"
  31. /*
  32. * The original version of fdt_overlay.c is slow in searching for particular
  33. * nodes and adding subnodes/properties due to the operations on flattened
  34. * device tree (FDT).
  35. *
  36. * Here we introduce `libufdt` which builds a real tree structure (named
  37. * ufdt -- unflattned device tree) from FDT. In the real tree, we can perform
  38. * certain operations (e.g., merge 2 subtrees, search for a node by path) in
  39. * almost optimal time complexity with acceptable additional memory usage.
  40. *
  41. * This file is the improved version of fdt_overlay.c by using the real tree
  42. * structure defined in libufdt.
  43. *
  44. * How the device tree overlay works and some
  45. * special terms (e.g., fixups, local fixups, fragment, etc)
  46. * are described in the document
  47. * external/dtc/Documentation/dt-object-internal.txt.
  48. */
  49. /* BEGIN of operations about phandles in ufdt. */
  50. /*
  51. * Increases u32 value at pos by offset.
  52. */
  53. static void fdt_increase_u32(void *pos, uint32_t offset) {
  54. uint32_t val;
  55. dto_memcpy(&val, pos, sizeof(val));
  56. val = cpu_to_fdt32(fdt32_to_cpu(val) + offset);
  57. dto_memcpy(pos, &val, sizeof(val));
  58. }
  59. /*
  60. * Gets the max phandle of a given ufdt.
  61. */
  62. static uint32_t ufdt_get_max_phandle(struct ufdt *tree) {
  63. struct static_phandle_table sorted_table = tree->phandle_table;
  64. if (sorted_table.len > 0)
  65. return sorted_table.data[sorted_table.len - 1].phandle;
  66. else
  67. return 0;
  68. }
  69. /*
  70. * Tries to increase the phandle value of a node
  71. * if the phandle exists.
  72. */
  73. static void ufdt_node_try_increase_phandle(struct ufdt_node *node,
  74. uint32_t offset) {
  75. int len = 0;
  76. char *prop_data = ufdt_node_get_fdt_prop_data_by_name(node, "phandle", &len);
  77. if (prop_data != NULL && len == sizeof(fdt32_t)) {
  78. fdt_increase_u32(prop_data, offset);
  79. }
  80. prop_data = ufdt_node_get_fdt_prop_data_by_name(node, "linux,phandle", &len);
  81. if (prop_data != NULL && len == sizeof(fdt32_t)) {
  82. fdt_increase_u32(prop_data, offset);
  83. }
  84. }
  85. /*
  86. * Increases all phandles by offset in a ufdt
  87. * in O(n) time.
  88. */
  89. static void ufdt_try_increase_phandle(struct ufdt *tree, uint32_t offset) {
  90. struct static_phandle_table sorted_table = tree->phandle_table;
  91. int i;
  92. for (i = 0; i < sorted_table.len; i++) {
  93. struct ufdt_node *target_node = sorted_table.data[i].node;
  94. ufdt_node_try_increase_phandle(target_node, offset);
  95. }
  96. }
  97. /* END of operations about phandles in ufdt. */
  98. /*
  99. * In the overlay_tree, there are some references (phandle)
  100. * pointing to somewhere in the main_tree.
  101. * Fix-up operations is to resolve the right address
  102. * in the overlay_tree.
  103. */
  104. /* BEGIN of doing fixup in the overlay ufdt. */
  105. /*
  106. * Returns exact memory location specified by fixup in format
  107. * /path/to/node:property:offset.
  108. * A property might contain multiple values and the offset is used to locate a
  109. * reference inside the property.
  110. * e.g.,
  111. * "property"=<1, 2, &ref, 4>, we can use /path/to/node:property:8 to get ref,
  112. * where 8 is sizeof(uint32) + sizeof(unit32).
  113. */
  114. static void *ufdt_get_fixup_location(struct ufdt *tree, const char *fixup) {
  115. char *path, *prop_ptr, *offset_ptr, *end_ptr;
  116. int prop_offset, prop_len;
  117. const char *prop_data;
  118. /*
  119. * TODO(akaineko): Keep track of substring lengths so we don't have to
  120. * dto_malloc a copy and split it up.
  121. */
  122. path = dto_strdup(fixup);
  123. prop_ptr = dto_strchr(path, ':');
  124. if (prop_ptr == NULL) {
  125. dto_error("Missing property part in '%s'\n", path);
  126. goto fail;
  127. }
  128. *prop_ptr = '\0';
  129. prop_ptr++;
  130. offset_ptr = dto_strchr(prop_ptr, ':');
  131. if (offset_ptr == NULL) {
  132. dto_error("Missing offset part in '%s'\n", path);
  133. goto fail;
  134. }
  135. *offset_ptr = '\0';
  136. offset_ptr++;
  137. prop_offset = dto_strtoul(offset_ptr, &end_ptr, 10 /* base */);
  138. if (*end_ptr != '\0') {
  139. dto_error("'%s' is not valid number\n", offset_ptr);
  140. goto fail;
  141. }
  142. struct ufdt_node *target_node;
  143. target_node = ufdt_get_node_by_path(tree, path);
  144. if (target_node == NULL) {
  145. dto_error("Path '%s' not found\n", path);
  146. goto fail;
  147. }
  148. prop_data =
  149. ufdt_node_get_fdt_prop_data_by_name(target_node, prop_ptr, &prop_len);
  150. if (prop_data == NULL) {
  151. dto_error("Property '%s' not found in '%s' node\n", prop_ptr, path);
  152. goto fail;
  153. }
  154. /*
  155. * Note that prop_offset is the offset inside the property data.
  156. */
  157. if (prop_len < prop_offset + (int)sizeof(uint32_t)) {
  158. dto_error("%s: property length is too small for fixup\n", path);
  159. goto fail;
  160. }
  161. dto_free(path);
  162. return (char *)prop_data + prop_offset;
  163. fail:
  164. dto_free(path);
  165. return NULL;
  166. }
  167. /*
  168. * Process one entry in __fixups__ { } node.
  169. * @fixups is property value, array of NUL-terminated strings
  170. * with fixup locations.
  171. * @fixups_len length of the fixups array in bytes.
  172. * @phandle is value for these locations.
  173. */
  174. static int ufdt_do_one_fixup(struct ufdt *tree, const char *fixups,
  175. int fixups_len, int phandle) {
  176. void *fixup_pos;
  177. uint32_t val;
  178. val = cpu_to_fdt32(phandle);
  179. while (fixups_len > 0) {
  180. fixup_pos = ufdt_get_fixup_location(tree, fixups);
  181. if (fixup_pos != NULL) {
  182. dto_memcpy(fixup_pos, &val, sizeof(val));
  183. } else {
  184. return -1;
  185. }
  186. fixups_len -= dto_strlen(fixups) + 1;
  187. fixups += dto_strlen(fixups) + 1;
  188. }
  189. return 0;
  190. }
  191. /*
  192. * Handle __fixups__ node in overlay tree.
  193. */
  194. static int ufdt_overlay_do_fixups(struct ufdt *main_tree,
  195. struct ufdt *overlay_tree) {
  196. int len = 0;
  197. struct ufdt_node *overlay_fixups_node =
  198. ufdt_get_node_by_path(overlay_tree, "/__fixups__");
  199. if (!overlay_fixups_node) {
  200. /* There is no __fixups__. Do nothing. */
  201. return 0;
  202. }
  203. struct ufdt_node *main_symbols_node =
  204. ufdt_get_node_by_path(main_tree, "/__symbols__");
  205. struct ufdt_node **it;
  206. for_each_prop(it, overlay_fixups_node) {
  207. /* Find the first property */
  208. /* Check __symbols__ is exist when we have any property in __fixups__ */
  209. if (!main_symbols_node) {
  210. dto_error("No node __symbols__ in main dtb.\n");
  211. return -1;
  212. }
  213. break;
  214. }
  215. for_each_prop(it, overlay_fixups_node) {
  216. /*
  217. * A property in __fixups__ looks like:
  218. * symbol_name =
  219. * "/path/to/node:prop:offset0\x00/path/to/node:prop:offset1..."
  220. * So we firstly find the node "symbol_name" and obtain its phandle in
  221. * __symbols__ of the main_tree.
  222. */
  223. struct ufdt_node *fixups = *it;
  224. char *symbol_path = ufdt_node_get_fdt_prop_data_by_name(
  225. main_symbols_node, name_of(fixups), &len);
  226. if (!symbol_path) {
  227. dto_error("Couldn't find '%s' symbol in main dtb\n", name_of(fixups));
  228. return -1;
  229. }
  230. struct ufdt_node *symbol_node;
  231. symbol_node = ufdt_get_node_by_path(main_tree, symbol_path);
  232. if (!symbol_node) {
  233. dto_error("Couldn't find '%s' path in main dtb\n", symbol_path);
  234. return -1;
  235. }
  236. uint32_t phandle = ufdt_node_get_phandle(symbol_node);
  237. const char *fixups_paths = ufdt_node_get_fdt_prop_data(fixups, &len);
  238. if (ufdt_do_one_fixup(overlay_tree, fixups_paths, len, phandle) < 0) {
  239. dto_error("Failed one fixup in ufdt_do_one_fixup\n");
  240. return -1;
  241. }
  242. }
  243. return 0;
  244. }
  245. /* END of doing fixup in the overlay ufdt. */
  246. /*
  247. * Here is to overlay all fragments in the overlay_tree to the main_tree.
  248. * What is "overlay fragment"? The main purpose is to add some subtrees to the
  249. * main_tree in order to complete the entire device tree.
  250. *
  251. * A frgament consists of two parts: 1. the subtree to be added 2. where it
  252. * should be added.
  253. *
  254. * Overlaying a fragment requires: 1. find the node in the main_tree 2. merge
  255. * the subtree into that node in the main_tree.
  256. */
  257. /* BEGIN of applying fragments. */
  258. /*
  259. * Overlay the overlay_node over target_node.
  260. */
  261. static int ufdt_overlay_node(struct ufdt_node *target_node,
  262. struct ufdt_node *overlay_node) {
  263. return merge_ufdt_into(target_node, overlay_node);
  264. }
  265. /*
  266. * Return value of ufdt_apply_fragment().
  267. */
  268. enum overlay_result {
  269. OVERLAY_RESULT_OK,
  270. OVERLAY_RESULT_MISSING_TARGET,
  271. OVERLAY_RESULT_MISSING_OVERLAY,
  272. OVERLAY_RESULT_TARGET_PATH_INVALID,
  273. OVERLAY_RESULT_TARGET_INVALID,
  274. OVERLAY_RESULT_MERGE_FAIL,
  275. };
  276. /*
  277. * Apply one overlay fragment (subtree).
  278. */
  279. static enum overlay_result ufdt_apply_fragment(struct ufdt *tree,
  280. struct ufdt_node *frag_node) {
  281. uint32_t target;
  282. const char *target_path;
  283. const void *val;
  284. struct ufdt_node *target_node = NULL;
  285. struct ufdt_node *overlay_node = NULL;
  286. val = ufdt_node_get_fdt_prop_data_by_name(frag_node, "target", NULL);
  287. if (val) {
  288. dto_memcpy(&target, val, sizeof(target));
  289. target = fdt32_to_cpu(target);
  290. target_node = ufdt_get_node_by_phandle(tree, target);
  291. if (target_node == NULL) {
  292. dto_error("failed to find target %04x\n", target);
  293. return OVERLAY_RESULT_TARGET_INVALID;
  294. }
  295. }
  296. if (target_node == NULL) {
  297. target_path =
  298. ufdt_node_get_fdt_prop_data_by_name(frag_node, "target-path", NULL);
  299. if (target_path == NULL) {
  300. return OVERLAY_RESULT_MISSING_TARGET;
  301. }
  302. target_node = ufdt_get_node_by_path(tree, target_path);
  303. if (target_node == NULL) {
  304. dto_error("failed to find target-path %s\n", target_path);
  305. return OVERLAY_RESULT_TARGET_PATH_INVALID;
  306. }
  307. }
  308. overlay_node = ufdt_node_get_node_by_path(frag_node, "__overlay__");
  309. if (overlay_node == NULL) {
  310. dto_error("missing __overlay__ sub-node\n");
  311. return OVERLAY_RESULT_MISSING_OVERLAY;
  312. }
  313. int err = ufdt_overlay_node(target_node, overlay_node);
  314. if (err < 0) {
  315. dto_error("failed to overlay node %s to target %s\n", name_of(overlay_node),
  316. name_of(target_node));
  317. return OVERLAY_RESULT_MERGE_FAIL;
  318. }
  319. return OVERLAY_RESULT_OK;
  320. }
  321. /*
  322. * Applies all fragments to the main_tree.
  323. */
  324. static int ufdt_overlay_apply_fragments(struct ufdt *main_tree,
  325. struct ufdt *overlay_tree) {
  326. enum overlay_result err;
  327. struct ufdt_node **it;
  328. /*
  329. * This loop may iterate to subnodes that's not a fragment node.
  330. * In such case, ufdt_apply_fragment would fail with return value = -1.
  331. */
  332. for_each_node(it, overlay_tree->root) {
  333. err = ufdt_apply_fragment(main_tree, *it);
  334. if (err == OVERLAY_RESULT_MERGE_FAIL) {
  335. return -1;
  336. }
  337. }
  338. return 0;
  339. }
  340. /* END of applying fragments. */
  341. /*
  342. * Since the overlay_tree will be "merged" into the main_tree, some
  343. * references (e.g., phandle values that acts as an unique ID) need to be
  344. * updated so it won't lead to collision that different nodes have the same
  345. * phandle value.
  346. *
  347. * Two things need to be done:
  348. *
  349. * 1. ufdt_try_increase_phandle()
  350. * Update phandle (an unique integer ID of a node in the device tree) of each
  351. * node in the overlay_tree. To achieve this, we simply increase each phandle
  352. * values in the overlay_tree by the max phandle value of the main_tree.
  353. *
  354. * 2. ufdt_overlay_do_local_fixups()
  355. * If there are some reference in the overlay_tree that references nodes
  356. * inside the overlay_tree, we have to modify the reference value (address of
  357. * the referenced node: phandle) so that it corresponds to the right node inside
  358. * the overlay_tree. Where the reference exists is kept in __local_fixups__ node
  359. * in the overlay_tree.
  360. */
  361. /* BEGIN of updating local references (phandle values) in the overlay ufdt. */
  362. /*
  363. * local fixups
  364. */
  365. static int ufdt_local_fixup_prop(struct ufdt_node *target_prop_node,
  366. struct ufdt_node *local_fixup_prop_node,
  367. uint32_t phandle_offset) {
  368. /*
  369. * prop_offsets_ptr should be a list of fdt32_t.
  370. * <offset0 offset1 offset2 ...>
  371. */
  372. char *prop_offsets_ptr;
  373. int len = 0;
  374. prop_offsets_ptr = ufdt_node_get_fdt_prop_data(local_fixup_prop_node, &len);
  375. char *prop_data;
  376. int target_length = 0;
  377. prop_data = ufdt_node_get_fdt_prop_data(target_prop_node, &target_length);
  378. if (prop_offsets_ptr == NULL || prop_data == NULL) return -1;
  379. int i;
  380. for (i = 0; i < len; i += sizeof(fdt32_t)) {
  381. int offset = fdt32_to_cpu(*(fdt32_t *)(prop_offsets_ptr + i));
  382. if (offset + sizeof(fdt32_t) > (size_t)target_length) return -1;
  383. fdt_increase_u32((prop_data + offset), phandle_offset);
  384. }
  385. return 0;
  386. }
  387. static int ufdt_local_fixup_node(struct ufdt_node *target_node,
  388. struct ufdt_node *local_fixups_node,
  389. uint32_t phandle_offset) {
  390. if (local_fixups_node == NULL) return 0;
  391. struct ufdt_node **it_local_fixups;
  392. struct ufdt_node *sub_target_node;
  393. for_each_prop(it_local_fixups, local_fixups_node) {
  394. sub_target_node =
  395. ufdt_node_get_property_by_name(target_node, name_of(*it_local_fixups));
  396. if (sub_target_node != NULL) {
  397. int err = ufdt_local_fixup_prop(sub_target_node, *it_local_fixups,
  398. phandle_offset);
  399. if (err < 0) return -1;
  400. } else {
  401. return -1;
  402. }
  403. }
  404. for_each_node(it_local_fixups, local_fixups_node) {
  405. sub_target_node =
  406. ufdt_node_get_node_by_path(target_node, name_of(*it_local_fixups));
  407. if (sub_target_node != NULL) {
  408. int err = ufdt_local_fixup_node(sub_target_node, *it_local_fixups,
  409. phandle_offset);
  410. if (err < 0) return -1;
  411. } else {
  412. return -1;
  413. }
  414. }
  415. return 0;
  416. }
  417. /*
  418. * Handle __local_fixups__ node in overlay DTB
  419. * The __local_fixups__ format we expect is
  420. * __local_fixups__ {
  421. * path {
  422. * to {
  423. * local_ref1 = <offset>;
  424. * };
  425. * };
  426. * path2 {
  427. * to2 {
  428. * local_ref2 = <offset1 offset2 ...>;
  429. * };
  430. * };
  431. * };
  432. *
  433. * which follows the dtc patch from:
  434. * https://marc.info/?l=devicetree&m=144061468601974&w=4
  435. */
  436. static int ufdt_overlay_do_local_fixups(struct ufdt *tree,
  437. uint32_t phandle_offset) {
  438. struct ufdt_node *overlay_node = ufdt_get_node_by_path(tree, "/");
  439. struct ufdt_node *local_fixups_node =
  440. ufdt_get_node_by_path(tree, "/__local_fixups__");
  441. int err =
  442. ufdt_local_fixup_node(overlay_node, local_fixups_node, phandle_offset);
  443. if (err < 0) return -1;
  444. return 0;
  445. }
  446. static int ufdt_overlay_local_ref_update(struct ufdt *main_tree,
  447. struct ufdt *overlay_tree) {
  448. uint32_t phandle_offset = 0;
  449. phandle_offset = ufdt_get_max_phandle(main_tree);
  450. if (phandle_offset > 0) {
  451. ufdt_try_increase_phandle(overlay_tree, phandle_offset);
  452. }
  453. int err = ufdt_overlay_do_local_fixups(overlay_tree, phandle_offset);
  454. if (err < 0) {
  455. dto_error("failed to perform local fixups in overlay\n");
  456. return -1;
  457. }
  458. return 0;
  459. }
  460. /* END of updating local references (phandle values) in the overlay ufdt. */
  461. static int _ufdt_overlay_fdtps(struct ufdt *main_tree,
  462. const struct ufdt *overlay_tree) {
  463. for (int i = 0; i < overlay_tree->num_used_fdtps; i++) {
  464. void *fdt = overlay_tree->fdtps[i];
  465. if (ufdt_add_fdt(main_tree, fdt) < 0) {
  466. return -1;
  467. }
  468. }
  469. return 0;
  470. }
  471. static int ufdt_overlay_apply(struct ufdt *main_tree, struct ufdt *overlay_tree,
  472. size_t overlay_length) {
  473. if (_ufdt_overlay_fdtps(main_tree, overlay_tree) < 0) {
  474. dto_error("failed to add more fdt into main ufdt tree.\n");
  475. return -1;
  476. }
  477. if (overlay_length < sizeof(struct fdt_header)) {
  478. dto_error("Overlay_length %zu smaller than header size %zu\n",
  479. overlay_length, sizeof(struct fdt_header));
  480. return -1;
  481. }
  482. if (ufdt_overlay_local_ref_update(main_tree, overlay_tree) < 0) {
  483. dto_error("failed to perform local fixups in overlay\n");
  484. return -1;
  485. }
  486. if (ufdt_overlay_do_fixups(main_tree, overlay_tree) < 0) {
  487. dto_error("failed to perform fixups in overlay\n");
  488. return -1;
  489. }
  490. if (ufdt_overlay_apply_fragments(main_tree, overlay_tree) < 0) {
  491. dto_error("failed to apply fragments\n");
  492. return -1;
  493. }
  494. return 0;
  495. }
  496. struct fdt_header *ufdt_install_blob(void *blob, size_t blob_size) {
  497. struct fdt_header *pHeader;
  498. int err;
  499. dto_debug("ufdt_install_blob (0x%08jx)\n", (uintmax_t)blob);
  500. if (blob_size < sizeof(struct fdt_header)) {
  501. dto_error("Blob_size %zu smaller than the header size %zu\n", blob_size,
  502. sizeof(struct fdt_header));
  503. return NULL;
  504. }
  505. pHeader = (struct fdt_header *)blob;
  506. err = fdt_check_header(pHeader);
  507. if (err < 0) {
  508. if (err == -FDT_ERR_BADVERSION) {
  509. dto_error("incompatible blob version: %d, should be: %d",
  510. fdt_version(pHeader), FDT_LAST_SUPPORTED_VERSION);
  511. } else {
  512. dto_error("error validating blob: %s", fdt_strerror(err));
  513. }
  514. return NULL;
  515. }
  516. return pHeader;
  517. }
  518. /*
  519. * From Google, based on dt_overlay_apply() logic
  520. * Will dto_malloc a new fdt blob and return it. Will not dto_free parameters.
  521. */
  522. struct fdt_header *ufdt_apply_overlay(struct fdt_header *main_fdt_header,
  523. size_t main_fdt_size,
  524. void *overlay_fdtp,
  525. size_t overlay_size) {
  526. size_t out_fdt_size;
  527. if (main_fdt_header == NULL) {
  528. return NULL;
  529. }
  530. if (overlay_size < 8 || overlay_size != fdt_totalsize(overlay_fdtp)) {
  531. dto_error("Bad overlay size!\n");
  532. return NULL;
  533. }
  534. if (main_fdt_size < 8 || main_fdt_size != fdt_totalsize(main_fdt_header)) {
  535. dto_error("Bad fdt size!\n");
  536. return NULL;
  537. }
  538. out_fdt_size = fdt_totalsize(main_fdt_header) + overlay_size;
  539. /* It's actually more than enough */
  540. struct fdt_header *out_fdt_header = dto_malloc(out_fdt_size);
  541. if (out_fdt_header == NULL) {
  542. dto_error("failed to allocate memory for DTB blob with overlays\n");
  543. return NULL;
  544. }
  545. struct ufdt *main_tree = NULL;
  546. struct ufdt *overlay_tree = NULL;
  547. main_tree = fdt_to_ufdt(main_fdt_header, main_fdt_size);
  548. overlay_tree = fdt_to_ufdt(overlay_fdtp, overlay_size);
  549. int err = ufdt_overlay_apply(main_tree, overlay_tree, overlay_size);
  550. if (err < 0) {
  551. goto fail;
  552. }
  553. err = ufdt_to_fdt(main_tree, out_fdt_header, out_fdt_size);
  554. if (err < 0) {
  555. dto_error("Failed to dump the device tree to out_fdt_header\n");
  556. goto fail;
  557. }
  558. ufdt_destruct(overlay_tree);
  559. ufdt_destruct(main_tree);
  560. return out_fdt_header;
  561. fail:
  562. ufdt_destruct(overlay_tree);
  563. ufdt_destruct(main_tree);
  564. dto_free(out_fdt_header);
  565. return NULL;
  566. }