• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
30 #include "ufdt_overlay.h"
31 #include "libufdt.h"
32 #include "ufdt_node_pool.h"
33 #include "ufdt_overlay_internal.h"
34 
35 /*
36  * The original version of fdt_overlay.c is slow in searching for particular
37  * nodes and adding subnodes/properties due to the operations on flattened
38  * device tree (FDT).
39  *
40  * Here we introduce `libufdt` which builds a real tree structure (named
41  * ufdt -- unflattned device tree) from FDT. In the real tree, we can perform
42  * certain operations (e.g., merge 2 subtrees, search for a node by path) in
43  * almost optimal time complexity with acceptable additional memory usage.
44  *
45  * This file is the improved version of fdt_overlay.c by using the real tree
46  * structure defined in libufdt.
47  *
48  * How the device tree overlay works and some
49  * special terms (e.g., fixups, local fixups, fragment, etc)
50  * are described in the document
51  * external/dtc/Documentation/dt-object-internal.txt.
52  */
53 
54 /* BEGIN of operations about phandles in ufdt. */
55 
56 /*
57  * Increases u32 value at pos by offset.
58  */
fdt_increase_u32(void * pos,uint32_t offset)59 static void fdt_increase_u32(void *pos, uint32_t offset) {
60   uint32_t val;
61 
62   dto_memcpy(&val, pos, sizeof(val));
63   val = cpu_to_fdt32(fdt32_to_cpu(val) + offset);
64   dto_memcpy(pos, &val, sizeof(val));
65 }
66 
67 /*
68  * Gets the max phandle of a given ufdt.
69  */
ufdt_get_max_phandle(struct ufdt * tree)70 uint32_t ufdt_get_max_phandle(struct ufdt *tree) {
71   struct ufdt_static_phandle_table sorted_table = tree->phandle_table;
72   if (sorted_table.len > 0)
73     return sorted_table.data[sorted_table.len - 1].phandle;
74   else
75     return 0;
76 }
77 
78 /*
79  * Tries to increase the phandle value of a node
80  * if the phandle exists.
81  */
ufdt_node_try_increase_phandle(struct ufdt_node * node,uint32_t offset)82 static void ufdt_node_try_increase_phandle(struct ufdt_node *node,
83                                            uint32_t offset) {
84   int len = 0;
85   char *prop_data = ufdt_node_get_fdt_prop_data_by_name(node, "phandle", &len);
86   if (prop_data != NULL && len == sizeof(fdt32_t)) {
87     fdt_increase_u32(prop_data, offset);
88   }
89   prop_data = ufdt_node_get_fdt_prop_data_by_name(node, "linux,phandle", &len);
90   if (prop_data != NULL && len == sizeof(fdt32_t)) {
91     fdt_increase_u32(prop_data, offset);
92   }
93 }
94 
95 /*
96  * Increases all phandles by offset in a ufdt
97  * in O(n) time.
98  */
ufdt_try_increase_phandle(struct ufdt * tree,uint32_t offset)99 void ufdt_try_increase_phandle(struct ufdt *tree, uint32_t offset) {
100   struct ufdt_static_phandle_table sorted_table = tree->phandle_table;
101   int i;
102 
103   for (i = 0; i < sorted_table.len; i++) {
104     struct ufdt_node *target_node = sorted_table.data[i].node;
105 
106     ufdt_node_try_increase_phandle(target_node, offset);
107   }
108 }
109 
110 /* END of operations about phandles in ufdt. */
111 
112 /*
113  * In the overlay_tree, there are some references (phandle)
114  * pointing to somewhere in the main_tree.
115  * Fix-up operations is to resolve the right address
116  * in the overlay_tree.
117  */
118 
119 /* BEGIN of doing fixup in the overlay ufdt. */
120 
121 /*
122  * Returns exact memory location specified by fixup in format
123  * /path/to/node:property:offset.
124  * A property might contain multiple values and the offset is used to locate a
125  * reference inside the property.
126  * e.g.,
127  * "property"=<1, 2, &ref, 4>, we can use /path/to/node:property:8 to get ref,
128  * where 8 is sizeof(uint32) + sizeof(unit32).
129  */
ufdt_get_fixup_location(struct ufdt * tree,const char * fixup)130 void *ufdt_get_fixup_location(struct ufdt *tree, const char *fixup) {
131   char *path, *prop_ptr, *offset_ptr, *end_ptr;
132   int prop_offset, prop_len;
133   const char *prop_data;
134   char path_buf[1024];
135   char *path_mem = NULL;
136 
137   size_t fixup_len = strlen(fixup) + 1;
138   if (fixup_len > sizeof(path_buf)) {
139     path_mem = dto_malloc(fixup_len);
140     path = path_mem;
141   } else {
142     path = path_buf;
143   }
144   dto_memcpy(path, fixup, fixup_len);
145 
146   prop_ptr = dto_strchr(path, ':');
147   if (prop_ptr == NULL) {
148     dto_error("Missing property part in '%s'\n", path);
149     goto fail;
150   }
151 
152   *prop_ptr = '\0';
153   prop_ptr++;
154 
155   offset_ptr = dto_strchr(prop_ptr, ':');
156   if (offset_ptr == NULL) {
157     dto_error("Missing offset part in '%s'\n", path);
158     goto fail;
159   }
160 
161   *offset_ptr = '\0';
162   offset_ptr++;
163 
164   prop_offset = dto_strtoul(offset_ptr, &end_ptr, 10 /* base */);
165   if (*end_ptr != '\0') {
166     dto_error("'%s' is not a valid number\n", offset_ptr);
167     goto fail;
168   }
169 
170   if (prop_offset < 0) {
171     dto_error("'%s' is not a valid offset\n", offset_ptr);
172     goto fail;
173   }
174 
175   struct ufdt_node *target_node;
176   target_node = ufdt_get_node_by_path(tree, path);
177   if (target_node == NULL) {
178     dto_error("Path '%s' not found\n", path);
179     goto fail;
180   }
181 
182   prop_data =
183       ufdt_node_get_fdt_prop_data_by_name(target_node, prop_ptr, &prop_len);
184   if (prop_data == NULL) {
185     dto_error("Property '%s' not found in  '%s' node\n", prop_ptr, path);
186     goto fail;
187   }
188   /*
189    * Note that prop_offset is the offset inside the property data.
190    */
191   if (prop_len < (int)sizeof(uint32_t) ||
192       prop_offset > prop_len - (int)sizeof(uint32_t)) {
193     dto_error("%s: property length is too small for fixup\n", path);
194     goto fail;
195   }
196 
197   if (path_mem) dto_free(path_mem);
198   return (char *)prop_data + prop_offset;
199 
200 fail:
201   if (path_mem) dto_free(path_mem);
202   return NULL;
203 }
204 
205 /*
206  * Process one entry in __fixups__ { } node.
207  * @fixups is property value, array of NUL-terminated strings
208  *   with fixup locations.
209  * @fixups_len length of the fixups array in bytes.
210  * @phandle is value for these locations.
211  */
ufdt_do_one_fixup(struct ufdt * tree,const char * fixups,int fixups_len,int phandle)212 int ufdt_do_one_fixup(struct ufdt *tree, const char *fixups, int fixups_len,
213                       int phandle) {
214   void *fixup_pos;
215   uint32_t val;
216 
217   val = cpu_to_fdt32(phandle);
218 
219   while (fixups_len > 0) {
220     fixup_pos = ufdt_get_fixup_location(tree, fixups);
221     if (fixup_pos != NULL) {
222       dto_memcpy(fixup_pos, &val, sizeof(val));
223     } else {
224       return -1;
225     }
226 
227     fixups_len -= dto_strlen(fixups) + 1;
228     fixups += dto_strlen(fixups) + 1;
229   }
230 
231   return 0;
232 }
233 
234 /*
235  * Handle __fixups__ node in overlay tree.
236  */
237 
ufdt_overlay_do_fixups(struct ufdt * main_tree,struct ufdt * overlay_tree)238 int ufdt_overlay_do_fixups(struct ufdt *main_tree, struct ufdt *overlay_tree) {
239   int len = 0;
240   struct ufdt_node *overlay_fixups_node =
241       ufdt_get_node_by_path(overlay_tree, "/__fixups__");
242   if (!overlay_fixups_node) {
243     /* There is no __fixups__. Do nothing. */
244     return 0;
245   }
246 
247   struct ufdt_node *main_symbols_node =
248       ufdt_get_node_by_path(main_tree, "/__symbols__");
249 
250   struct ufdt_node **it;
251   for_each_prop(it, overlay_fixups_node) {
252     /* Find the first property */
253 
254     /* Check __symbols__ is exist when we have any property in __fixups__ */
255     if (!main_symbols_node) {
256       dto_error("No node __symbols__ in main dtb.\n");
257       return -1;
258     }
259     break;
260   }
261 
262   for_each_prop(it, overlay_fixups_node) {
263     /*
264      * A property in __fixups__ looks like:
265      * symbol_name =
266      * "/path/to/node:prop:offset0\x00/path/to/node:prop:offset1..."
267      * So we firstly find the node "symbol_name" and obtain its phandle in
268      * __symbols__ of the main_tree.
269      */
270 
271     struct ufdt_node *fixups = *it;
272     char *symbol_path = ufdt_node_get_fdt_prop_data_by_name(
273         main_symbols_node, ufdt_node_name(fixups), &len);
274 
275     if (!symbol_path) {
276       dto_error("Couldn't find '%s' symbol in main dtb\n",
277                 ufdt_node_name(fixups));
278       return -1;
279     }
280 
281     struct ufdt_node *symbol_node;
282     symbol_node = ufdt_get_node_by_path(main_tree, symbol_path);
283 
284     if (!symbol_node) {
285       dto_error("Couldn't find '%s' path in main dtb\n", symbol_path);
286       return -1;
287     }
288 
289     uint32_t phandle = ufdt_node_get_phandle(symbol_node);
290 
291     const char *fixups_paths = ufdt_node_get_fdt_prop_data(fixups, &len);
292 
293     if (ufdt_do_one_fixup(overlay_tree, fixups_paths, len, phandle) < 0) {
294       dto_error("Failed one fixup in ufdt_do_one_fixup\n");
295       return -1;
296     }
297   }
298 
299   return 0;
300 }
301 
302 /* END of doing fixup in the overlay ufdt. */
303 
304 /*
305  * Here is to overlay all fragments in the overlay_tree to the main_tree.
306  * What is "overlay fragment"? The main purpose is to add some subtrees to the
307  * main_tree in order to complete the entire device tree.
308  *
309  * A fragment consists of two parts: 1. the subtree to be added 2. where it
310  * should be added.
311  *
312  * Overlaying a fragment requires: 1. find the node in the main_tree 2. merge
313  * the subtree into that node in the main_tree.
314  */
315 
316 /* BEGIN of applying fragments. */
317 
318 /*
319  * Overlay the overlay_node over target_node.
320  */
ufdt_overlay_node(struct ufdt_node * target_node,struct ufdt_node * overlay_node,struct ufdt_node_pool * pool)321 static int ufdt_overlay_node(struct ufdt_node *target_node,
322                              struct ufdt_node *overlay_node,
323                              struct ufdt_node_pool *pool) {
324   return ufdt_node_merge_into(target_node, overlay_node, pool);
325 }
326 
ufdt_overlay_get_target(struct ufdt * tree,struct ufdt_node * frag_node,struct ufdt_node ** target_node)327 enum overlay_result ufdt_overlay_get_target(struct ufdt *tree,
328                                             struct ufdt_node *frag_node,
329                                             struct ufdt_node **target_node) {
330   uint32_t target;
331   const char *target_path;
332   const void *val;
333   *target_node = NULL;
334 
335   val = ufdt_node_get_fdt_prop_data_by_name(frag_node, "target", NULL);
336   if (val) {
337     dto_memcpy(&target, val, sizeof(target));
338     target = fdt32_to_cpu(target);
339     *target_node = ufdt_get_node_by_phandle(tree, target);
340     if (*target_node == NULL) {
341       dto_error("failed to find target %04x\n", target);
342       return OVERLAY_RESULT_TARGET_INVALID;
343     }
344   }
345 
346   if (*target_node == NULL) {
347     target_path =
348         ufdt_node_get_fdt_prop_data_by_name(frag_node, "target-path", NULL);
349     if (target_path == NULL) {
350       return OVERLAY_RESULT_MISSING_TARGET;
351     }
352 
353     *target_node = ufdt_get_node_by_path(tree, target_path);
354     if (*target_node == NULL) {
355       dto_error("failed to find target-path %s\n", target_path);
356       return OVERLAY_RESULT_TARGET_PATH_INVALID;
357     }
358   }
359 
360   return OVERLAY_RESULT_OK;
361 }
362 
363 /*
364  * Apply one overlay fragment (subtree).
365  */
ufdt_apply_fragment(struct ufdt * tree,struct ufdt_node * frag_node,struct ufdt_node_pool * pool)366 static enum overlay_result ufdt_apply_fragment(struct ufdt *tree,
367                                                struct ufdt_node *frag_node,
368                                                struct ufdt_node_pool *pool) {
369   struct ufdt_node *target_node = NULL;
370   struct ufdt_node *overlay_node = NULL;
371 
372   overlay_node = ufdt_node_get_node_by_path(frag_node, "__overlay__");
373   if (overlay_node == NULL) {
374     return OVERLAY_RESULT_MISSING_OVERLAY;
375   }
376 
377   enum overlay_result result =
378       ufdt_overlay_get_target(tree, frag_node, &target_node);
379   if (target_node == NULL) {
380     dto_error("Unable to resolve target for %s\n", ufdt_node_name(frag_node));
381     return result;
382   }
383 
384   int err = ufdt_overlay_node(target_node, overlay_node, pool);
385 
386   if (err < 0) {
387     dto_error("failed to overlay node %s to target %s\n",
388               ufdt_node_name(overlay_node), ufdt_node_name(target_node));
389     return OVERLAY_RESULT_MERGE_FAIL;
390   }
391 
392   return OVERLAY_RESULT_OK;
393 }
394 
395 /*
396  * Applies all fragments to the main_tree.
397  */
ufdt_overlay_apply_fragments(struct ufdt * main_tree,struct ufdt * overlay_tree,struct ufdt_node_pool * pool)398 static int ufdt_overlay_apply_fragments(struct ufdt *main_tree,
399                                         struct ufdt *overlay_tree,
400                                         struct ufdt_node_pool *pool) {
401   enum overlay_result ret;
402   struct ufdt_node **it;
403   /*
404    * This loop may iterate to subnodes that's not a fragment node.
405    * We must fail for any other error.
406    */
407   for_each_node(it, overlay_tree->root) {
408     ret = ufdt_apply_fragment(main_tree, *it, pool);
409     if ((ret != OVERLAY_RESULT_OK) && (ret != OVERLAY_RESULT_MISSING_OVERLAY)) {
410       dto_error("failed to apply overlay fragment %s ret: %d\n",
411                 ufdt_node_name(*it), ret);
412       return -1;
413     }
414   }
415   return 0;
416 }
417 
418 /* END of applying fragments. */
419 
420 /*
421  * Since the overlay_tree will be "merged" into the main_tree, some
422  * references (e.g., phandle values that acts as an unique ID) need to be
423  * updated so it won't lead to collision that different nodes have the same
424  * phandle value.
425  *
426  * Two things need to be done:
427  *
428  * 1. ufdt_try_increase_phandle()
429  * Update phandle (an unique integer ID of a node in the device tree) of each
430  * node in the overlay_tree. To achieve this, we simply increase each phandle
431  * values in the overlay_tree by the max phandle value of the main_tree.
432  *
433  * 2. ufdt_overlay_do_local_fixups()
434  * If there are some reference in the overlay_tree that references nodes
435  * inside the overlay_tree, we have to modify the reference value (address of
436  * the referenced node: phandle) so that it corresponds to the right node inside
437  * the overlay_tree. Where the reference exists is kept in __local_fixups__ node
438  * in the overlay_tree.
439  */
440 
441 /* BEGIN of updating local references (phandle values) in the overlay ufdt. */
442 
443 /*
444  * local fixups
445  */
ufdt_local_fixup_prop(struct ufdt_node * target_prop_node,struct ufdt_node * local_fixup_prop_node,uint32_t phandle_offset)446 static int ufdt_local_fixup_prop(struct ufdt_node *target_prop_node,
447                                  struct ufdt_node *local_fixup_prop_node,
448                                  uint32_t phandle_offset) {
449   /*
450    * prop_offsets_ptr should be a list of fdt32_t.
451    * <offset0 offset1 offset2 ...>
452    */
453   char *prop_offsets_ptr;
454   int len = 0;
455   prop_offsets_ptr = ufdt_node_get_fdt_prop_data(local_fixup_prop_node, &len);
456 
457   if (prop_offsets_ptr == NULL || len % sizeof(fdt32_t) != 0) return -1;
458 
459   char *prop_data;
460   int target_length = 0;
461 
462   prop_data = ufdt_node_get_fdt_prop_data(target_prop_node, &target_length);
463 
464   if (prop_data == NULL) return -1;
465 
466   int i;
467   for (i = 0; i < len; i += sizeof(fdt32_t)) {
468     int offset = fdt32_to_cpu(*(fdt32_t *)(prop_offsets_ptr + i));
469     if (offset + sizeof(fdt32_t) > (size_t)target_length) return -1;
470     fdt_increase_u32((prop_data + offset), phandle_offset);
471   }
472   return 0;
473 }
474 
ufdt_local_fixup_node(struct ufdt_node * target_node,struct ufdt_node * local_fixups_node,uint32_t phandle_offset)475 static int ufdt_local_fixup_node(struct ufdt_node *target_node,
476                                  struct ufdt_node *local_fixups_node,
477                                  uint32_t phandle_offset) {
478   if (local_fixups_node == NULL) return 0;
479 
480   struct ufdt_node **it_local_fixups;
481   struct ufdt_node *sub_target_node;
482 
483   for_each_prop(it_local_fixups, local_fixups_node) {
484     sub_target_node = ufdt_node_get_property_by_name(
485         target_node, ufdt_node_name(*it_local_fixups));
486 
487     if (sub_target_node != NULL) {
488       int err = ufdt_local_fixup_prop(sub_target_node, *it_local_fixups,
489                                       phandle_offset);
490       if (err < 0) return -1;
491     } else {
492       return -1;
493     }
494   }
495 
496   for_each_node(it_local_fixups, local_fixups_node) {
497     sub_target_node = ufdt_node_get_node_by_path(
498         target_node, ufdt_node_name(*it_local_fixups));
499     if (sub_target_node != NULL) {
500       int err = ufdt_local_fixup_node(sub_target_node, *it_local_fixups,
501                                       phandle_offset);
502       if (err < 0) return -1;
503     } else {
504       return -1;
505     }
506   }
507 
508   return 0;
509 }
510 
511 /*
512  * Handle __local_fixups__ node in overlay DTB
513  * The __local_fixups__ format we expect is
514  * __local_fixups__ {
515  *   path {
516  *    to {
517  *      local_ref1 = <offset>;
518  *    };
519  *   };
520  *   path2 {
521  *    to2 {
522  *      local_ref2 = <offset1 offset2 ...>;
523  *    };
524  *   };
525  * };
526  *
527  * which follows the dtc patch from:
528  * https://marc.info/?l=devicetree&m=144061468601974&w=4
529  */
ufdt_overlay_do_local_fixups(struct ufdt * tree,uint32_t phandle_offset)530 int ufdt_overlay_do_local_fixups(struct ufdt *tree, uint32_t phandle_offset) {
531   struct ufdt_node *overlay_node = ufdt_get_node_by_path(tree, "/");
532   struct ufdt_node *local_fixups_node =
533       ufdt_get_node_by_path(tree, "/__local_fixups__");
534 
535   int err =
536       ufdt_local_fixup_node(overlay_node, local_fixups_node, phandle_offset);
537 
538   if (err < 0) return -1;
539 
540   return 0;
541 }
542 
ufdt_overlay_local_ref_update(struct ufdt * main_tree,struct ufdt * overlay_tree)543 static int ufdt_overlay_local_ref_update(struct ufdt *main_tree,
544                                          struct ufdt *overlay_tree) {
545   uint32_t phandle_offset = 0;
546 
547   phandle_offset = ufdt_get_max_phandle(main_tree);
548   if (phandle_offset > 0) {
549     ufdt_try_increase_phandle(overlay_tree, phandle_offset);
550   }
551 
552   int err = ufdt_overlay_do_local_fixups(overlay_tree, phandle_offset);
553   if (err < 0) {
554     dto_error("failed to perform local fixups in overlay\n");
555     return -1;
556   }
557   return 0;
558 }
559 
560 /* END of updating local references (phandle values) in the overlay ufdt. */
561 
_ufdt_overlay_fdtps(struct ufdt * main_tree,const struct ufdt * overlay_tree)562 static int _ufdt_overlay_fdtps(struct ufdt *main_tree,
563                                const struct ufdt *overlay_tree) {
564   for (int i = 0; i < overlay_tree->num_used_fdtps; i++) {
565     void *fdt = overlay_tree->fdtps[i];
566     if (ufdt_add_fdt(main_tree, fdt) < 0) {
567       return -1;
568     }
569   }
570   return 0;
571 }
572 
ufdt_overlay_apply(struct ufdt * main_tree,struct ufdt * overlay_tree,size_t overlay_length,struct ufdt_node_pool * pool)573 static int ufdt_overlay_apply(struct ufdt *main_tree, struct ufdt *overlay_tree,
574                               size_t overlay_length,
575                               struct ufdt_node_pool *pool) {
576   if (_ufdt_overlay_fdtps(main_tree, overlay_tree) < 0) {
577     dto_error("failed to add more fdt into main ufdt tree.\n");
578     return -1;
579   }
580 
581   if (overlay_length < sizeof(struct fdt_header)) {
582     dto_error("Overlay_length %zu smaller than header size %zu\n",
583               overlay_length, sizeof(struct fdt_header));
584     return -1;
585   }
586 
587   if (ufdt_overlay_local_ref_update(main_tree, overlay_tree) < 0) {
588     dto_error("failed to perform local fixups in overlay\n");
589     return -1;
590   }
591 
592   if (ufdt_overlay_do_fixups(main_tree, overlay_tree) < 0) {
593     dto_error("failed to perform fixups in overlay\n");
594     return -1;
595   }
596   if (ufdt_overlay_apply_fragments(main_tree, overlay_tree, pool) < 0) {
597     dto_error("failed to apply fragments\n");
598     return -1;
599   }
600 
601   return 0;
602 }
603 
ufdt_install_blob(void * blob,size_t blob_size)604 struct fdt_header *ufdt_install_blob(void *blob, size_t blob_size) {
605   struct fdt_header *pHeader;
606   int err;
607 
608   dto_debug("ufdt_install_blob (0x%08jx)\n", (uintmax_t)blob);
609 
610   if (blob_size < sizeof(struct fdt_header)) {
611     dto_error("Blob_size %zu smaller than the header size %zu\n", blob_size,
612               sizeof(struct fdt_header));
613     return NULL;
614   }
615 
616   pHeader = (struct fdt_header *)blob;
617   err = fdt_check_header(pHeader);
618   if (err < 0) {
619     if (err == -FDT_ERR_BADVERSION) {
620       dto_error("incompatible blob version: %d, should be: %d\n",
621                 fdt_version(pHeader), FDT_LAST_SUPPORTED_VERSION);
622 
623     } else {
624       dto_error("error validating blob: %s\n", fdt_strerror(err));
625     }
626     return NULL;
627   }
628 
629   return pHeader;
630 }
631 
632 /*
633 * From Google, based on dt_overlay_apply() logic
634 * Will dto_malloc a new fdt blob and return it. Will not dto_free parameters.
635 */
ufdt_apply_overlay(struct fdt_header * main_fdt_header,size_t main_fdt_size,void * overlay_fdtp,size_t overlay_size)636 struct fdt_header *ufdt_apply_overlay(struct fdt_header *main_fdt_header,
637                                  size_t main_fdt_size,
638                                  void *overlay_fdtp,
639                                  size_t overlay_size) {
640   size_t out_fdt_size;
641 
642   if (main_fdt_header == NULL) {
643     return NULL;
644   }
645 
646   if (overlay_size < 8 || overlay_size != fdt_totalsize(overlay_fdtp)) {
647     dto_error("Bad overlay size!\n");
648     return NULL;
649   }
650   if (main_fdt_size < 8 || main_fdt_size != fdt_totalsize(main_fdt_header)) {
651     dto_error("Bad fdt size!\n");
652     return NULL;
653   }
654 
655   out_fdt_size = fdt_totalsize(main_fdt_header) + overlay_size;
656   /* It's actually more than enough */
657   struct fdt_header *out_fdt_header = dto_malloc(out_fdt_size);
658 
659   if (out_fdt_header == NULL) {
660     dto_error("failed to allocate memory for DTB blob with overlays\n");
661     return NULL;
662   }
663 
664   struct ufdt_node_pool pool;
665   ufdt_node_pool_construct(&pool);
666   struct ufdt *main_tree = ufdt_from_fdt(main_fdt_header, main_fdt_size, &pool);
667   struct ufdt *overlay_tree = ufdt_from_fdt(overlay_fdtp, overlay_size, &pool);
668   int err = ufdt_overlay_apply(main_tree, overlay_tree, overlay_size, &pool);
669   if (err < 0) {
670     goto fail;
671   }
672 
673   err = ufdt_to_fdt(main_tree, out_fdt_header, out_fdt_size);
674   if (err < 0) {
675     dto_error("Failed to dump the device tree to out_fdt_header\n");
676     goto fail;
677   }
678 
679   ufdt_destruct(overlay_tree, &pool);
680   ufdt_destruct(main_tree, &pool);
681   ufdt_node_pool_destruct(&pool);
682 
683   return out_fdt_header;
684 
685 fail:
686   ufdt_destruct(overlay_tree, &pool);
687   ufdt_destruct(main_tree, &pool);
688   ufdt_node_pool_destruct(&pool);
689   dto_free(out_fdt_header);
690 
691   return NULL;
692 }
693