• 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 (len == 0 || !fixups_paths || fixups_paths[len - 1] != 0) {
294       dto_error("Format error for %s: fixups are not null terminated\n",
295                 ufdt_node_name(fixups));
296       return -1;
297     }
298 
299     if (ufdt_do_one_fixup(overlay_tree, fixups_paths, len, phandle) < 0) {
300       dto_error("Failed one fixup in ufdt_do_one_fixup\n");
301       return -1;
302     }
303   }
304 
305   return 0;
306 }
307 
308 /* END of doing fixup in the overlay ufdt. */
309 
310 /*
311  * Here is to overlay all fragments in the overlay_tree to the main_tree.
312  * What is "overlay fragment"? The main purpose is to add some subtrees to the
313  * main_tree in order to complete the entire device tree.
314  *
315  * A fragment consists of two parts: 1. the subtree to be added 2. where it
316  * should be added.
317  *
318  * Overlaying a fragment requires: 1. find the node in the main_tree 2. merge
319  * the subtree into that node in the main_tree.
320  */
321 
322 /* BEGIN of applying fragments. */
323 
324 /*
325  * Overlay the overlay_node over target_node.
326  */
ufdt_overlay_node(struct ufdt_node * target_node,struct ufdt_node * overlay_node,struct ufdt_node_pool * pool)327 static int ufdt_overlay_node(struct ufdt_node *target_node,
328                              struct ufdt_node *overlay_node,
329                              struct ufdt_node_pool *pool) {
330   return ufdt_node_merge_into(target_node, overlay_node, pool);
331 }
332 
ufdt_overlay_get_target(struct ufdt * tree,struct ufdt_node * frag_node,struct ufdt_node ** target_node)333 enum overlay_result ufdt_overlay_get_target(struct ufdt *tree,
334                                             struct ufdt_node *frag_node,
335                                             struct ufdt_node **target_node) {
336   uint32_t target;
337   const char *target_path;
338   const void *val;
339   *target_node = NULL;
340 
341   val = ufdt_node_get_fdt_prop_data_by_name(frag_node, "target", NULL);
342   if (val) {
343     dto_memcpy(&target, val, sizeof(target));
344     target = fdt32_to_cpu(target);
345     *target_node = ufdt_get_node_by_phandle(tree, target);
346     if (*target_node == NULL) {
347       dto_error("failed to find target %04x\n", target);
348       return OVERLAY_RESULT_TARGET_INVALID;
349     }
350   }
351 
352   if (*target_node == NULL) {
353     target_path =
354         ufdt_node_get_fdt_prop_data_by_name(frag_node, "target-path", NULL);
355     if (target_path == NULL) {
356       return OVERLAY_RESULT_MISSING_TARGET;
357     }
358 
359     *target_node = ufdt_get_node_by_path(tree, target_path);
360     if (*target_node == NULL) {
361       dto_error("failed to find target-path %s\n", target_path);
362       return OVERLAY_RESULT_TARGET_PATH_INVALID;
363     }
364   }
365 
366   return OVERLAY_RESULT_OK;
367 }
368 
369 /*
370  * Apply one overlay fragment (subtree).
371  */
ufdt_apply_fragment(struct ufdt * tree,struct ufdt_node * frag_node,struct ufdt_node_pool * pool)372 static enum overlay_result ufdt_apply_fragment(struct ufdt *tree,
373                                                struct ufdt_node *frag_node,
374                                                struct ufdt_node_pool *pool) {
375   struct ufdt_node *target_node = NULL;
376   struct ufdt_node *overlay_node = NULL;
377 
378   overlay_node = ufdt_node_get_node_by_path(frag_node, "__overlay__");
379   if (overlay_node == NULL) {
380     return OVERLAY_RESULT_MISSING_OVERLAY;
381   }
382 
383   enum overlay_result result =
384       ufdt_overlay_get_target(tree, frag_node, &target_node);
385   if (target_node == NULL) {
386     dto_error("Unable to resolve target for %s\n", ufdt_node_name(frag_node));
387     return result;
388   }
389 
390   int err = ufdt_overlay_node(target_node, overlay_node, pool);
391 
392   if (err < 0) {
393     dto_error("failed to overlay node %s to target %s\n",
394               ufdt_node_name(overlay_node), ufdt_node_name(target_node));
395     return OVERLAY_RESULT_MERGE_FAIL;
396   }
397 
398   return OVERLAY_RESULT_OK;
399 }
400 
401 /*
402  * Applies all fragments to the main_tree.
403  */
ufdt_overlay_apply_fragments(struct ufdt * main_tree,struct ufdt * overlay_tree,struct ufdt_node_pool * pool)404 static int ufdt_overlay_apply_fragments(struct ufdt *main_tree,
405                                         struct ufdt *overlay_tree,
406                                         struct ufdt_node_pool *pool) {
407   enum overlay_result ret;
408   struct ufdt_node **it;
409   /*
410    * This loop may iterate to subnodes that's not a fragment node.
411    * We must fail for any other error.
412    */
413   for_each_node(it, overlay_tree->root) {
414     ret = ufdt_apply_fragment(main_tree, *it, pool);
415     if ((ret != OVERLAY_RESULT_OK) && (ret != OVERLAY_RESULT_MISSING_OVERLAY)) {
416       dto_error("failed to apply overlay fragment %s ret: %d\n",
417                 ufdt_node_name(*it), ret);
418       return -1;
419     }
420   }
421   return 0;
422 }
423 
424 /* END of applying fragments. */
425 
426 /*
427  * Since the overlay_tree will be "merged" into the main_tree, some
428  * references (e.g., phandle values that acts as an unique ID) need to be
429  * updated so it won't lead to collision that different nodes have the same
430  * phandle value.
431  *
432  * Two things need to be done:
433  *
434  * 1. ufdt_try_increase_phandle()
435  * Update phandle (an unique integer ID of a node in the device tree) of each
436  * node in the overlay_tree. To achieve this, we simply increase each phandle
437  * values in the overlay_tree by the max phandle value of the main_tree.
438  *
439  * 2. ufdt_overlay_do_local_fixups()
440  * If there are some reference in the overlay_tree that references nodes
441  * inside the overlay_tree, we have to modify the reference value (address of
442  * the referenced node: phandle) so that it corresponds to the right node inside
443  * the overlay_tree. Where the reference exists is kept in __local_fixups__ node
444  * in the overlay_tree.
445  */
446 
447 /* BEGIN of updating local references (phandle values) in the overlay ufdt. */
448 
449 /*
450  * local fixups
451  */
ufdt_local_fixup_prop(struct ufdt_node * target_prop_node,struct ufdt_node * local_fixup_prop_node,uint32_t phandle_offset)452 static int ufdt_local_fixup_prop(struct ufdt_node *target_prop_node,
453                                  struct ufdt_node *local_fixup_prop_node,
454                                  uint32_t phandle_offset) {
455   /*
456    * prop_offsets_ptr should be a list of fdt32_t.
457    * <offset0 offset1 offset2 ...>
458    */
459   char *prop_offsets_ptr;
460   int len = 0;
461   prop_offsets_ptr = ufdt_node_get_fdt_prop_data(local_fixup_prop_node, &len);
462 
463   if (prop_offsets_ptr == NULL || len % sizeof(fdt32_t) != 0) return -1;
464 
465   char *prop_data;
466   int target_length = 0;
467 
468   prop_data = ufdt_node_get_fdt_prop_data(target_prop_node, &target_length);
469 
470   if (prop_data == NULL) return -1;
471 
472   int i;
473   for (i = 0; i < len; i += sizeof(fdt32_t)) {
474     int offset = fdt32_to_cpu(*(fdt32_t *)(prop_offsets_ptr + i));
475     if (offset + sizeof(fdt32_t) > (size_t)target_length) return -1;
476     fdt_increase_u32((prop_data + offset), phandle_offset);
477   }
478   return 0;
479 }
480 
ufdt_local_fixup_node(struct ufdt_node * target_node,struct ufdt_node * local_fixups_node,uint32_t phandle_offset)481 static int ufdt_local_fixup_node(struct ufdt_node *target_node,
482                                  struct ufdt_node *local_fixups_node,
483                                  uint32_t phandle_offset) {
484   if (local_fixups_node == NULL) return 0;
485 
486   struct ufdt_node **it_local_fixups;
487   struct ufdt_node *sub_target_node;
488 
489   for_each_prop(it_local_fixups, local_fixups_node) {
490     sub_target_node = ufdt_node_get_property_by_name(
491         target_node, ufdt_node_name(*it_local_fixups));
492 
493     if (sub_target_node != NULL) {
494       int err = ufdt_local_fixup_prop(sub_target_node, *it_local_fixups,
495                                       phandle_offset);
496       if (err < 0) return -1;
497     } else {
498       return -1;
499     }
500   }
501 
502   for_each_node(it_local_fixups, local_fixups_node) {
503     sub_target_node = ufdt_node_get_node_by_path(
504         target_node, ufdt_node_name(*it_local_fixups));
505     if (sub_target_node != NULL) {
506       int err = ufdt_local_fixup_node(sub_target_node, *it_local_fixups,
507                                       phandle_offset);
508       if (err < 0) return -1;
509     } else {
510       return -1;
511     }
512   }
513 
514   return 0;
515 }
516 
517 /*
518  * Handle __local_fixups__ node in overlay DTB
519  * The __local_fixups__ format we expect is
520  * __local_fixups__ {
521  *   path {
522  *    to {
523  *      local_ref1 = <offset>;
524  *    };
525  *   };
526  *   path2 {
527  *    to2 {
528  *      local_ref2 = <offset1 offset2 ...>;
529  *    };
530  *   };
531  * };
532  *
533  * which follows the dtc patch from:
534  * https://marc.info/?l=devicetree&m=144061468601974&w=4
535  */
ufdt_overlay_do_local_fixups(struct ufdt * tree,uint32_t phandle_offset)536 int ufdt_overlay_do_local_fixups(struct ufdt *tree, uint32_t phandle_offset) {
537   struct ufdt_node *overlay_node = ufdt_get_node_by_path(tree, "/");
538   struct ufdt_node *local_fixups_node =
539       ufdt_get_node_by_path(tree, "/__local_fixups__");
540 
541   int err =
542       ufdt_local_fixup_node(overlay_node, local_fixups_node, phandle_offset);
543 
544   if (err < 0) return -1;
545 
546   return 0;
547 }
548 
ufdt_overlay_local_ref_update(struct ufdt * main_tree,struct ufdt * overlay_tree)549 static int ufdt_overlay_local_ref_update(struct ufdt *main_tree,
550                                          struct ufdt *overlay_tree) {
551   uint32_t phandle_offset = 0;
552 
553   phandle_offset = ufdt_get_max_phandle(main_tree);
554   if (phandle_offset > 0) {
555     ufdt_try_increase_phandle(overlay_tree, phandle_offset);
556   }
557 
558   int err = ufdt_overlay_do_local_fixups(overlay_tree, phandle_offset);
559   if (err < 0) {
560     dto_error("failed to perform local fixups in overlay\n");
561     return -1;
562   }
563   return 0;
564 }
565 
566 /* END of updating local references (phandle values) in the overlay ufdt. */
567 
_ufdt_overlay_fdtps(struct ufdt * main_tree,const struct ufdt * overlay_tree)568 static int _ufdt_overlay_fdtps(struct ufdt *main_tree,
569                                const struct ufdt *overlay_tree) {
570   for (int i = 0; i < overlay_tree->num_used_fdtps; i++) {
571     void *fdt = overlay_tree->fdtps[i];
572     if (ufdt_add_fdt(main_tree, fdt) < 0) {
573       return -1;
574     }
575   }
576   return 0;
577 }
578 
ufdt_overlay_apply(struct ufdt * main_tree,struct ufdt * overlay_tree,size_t overlay_length,struct ufdt_node_pool * pool)579 static int ufdt_overlay_apply(struct ufdt *main_tree, struct ufdt *overlay_tree,
580                               size_t overlay_length,
581                               struct ufdt_node_pool *pool) {
582   if (_ufdt_overlay_fdtps(main_tree, overlay_tree) < 0) {
583     dto_error("failed to add more fdt into main ufdt tree.\n");
584     return -1;
585   }
586 
587   if (overlay_length < sizeof(struct fdt_header)) {
588     dto_error("Overlay_length %zu smaller than header size %zu\n",
589               overlay_length, sizeof(struct fdt_header));
590     return -1;
591   }
592 
593   if (ufdt_overlay_local_ref_update(main_tree, overlay_tree) < 0) {
594     dto_error("failed to perform local fixups in overlay\n");
595     return -1;
596   }
597 
598   if (ufdt_overlay_do_fixups(main_tree, overlay_tree) < 0) {
599     dto_error("failed to perform fixups in overlay\n");
600     return -1;
601   }
602   if (ufdt_overlay_apply_fragments(main_tree, overlay_tree, pool) < 0) {
603     dto_error("failed to apply fragments\n");
604     return -1;
605   }
606 
607   return 0;
608 }
609 
ufdt_install_blob(void * blob,size_t blob_size)610 struct fdt_header *ufdt_install_blob(void *blob, size_t blob_size) {
611   struct fdt_header *pHeader;
612   int err;
613 
614   dto_debug("ufdt_install_blob (0x%08jx)\n", (uintmax_t)blob);
615 
616   if (blob_size < sizeof(struct fdt_header)) {
617     dto_error("Blob_size %zu smaller than the header size %zu\n", blob_size,
618               sizeof(struct fdt_header));
619     return NULL;
620   }
621 
622   pHeader = (struct fdt_header *)blob;
623   err = fdt_check_header(pHeader);
624   if (err < 0) {
625     if (err == -FDT_ERR_BADVERSION) {
626       dto_error("incompatible blob version: %d, should be: %d\n",
627                 fdt_version(pHeader), FDT_LAST_SUPPORTED_VERSION);
628 
629     } else {
630       dto_error("error validating blob: %s\n", fdt_strerror(err));
631     }
632     return NULL;
633   }
634 
635   return pHeader;
636 }
637 
638 /*
639 * From Google, based on dt_overlay_apply() logic
640 * Will dto_malloc a new fdt blob and return it. Will not dto_free parameters.
641 */
ufdt_apply_overlay(struct fdt_header * main_fdt_header,size_t main_fdt_size,void * overlay_fdtp,size_t overlay_size)642 struct fdt_header *ufdt_apply_overlay(struct fdt_header *main_fdt_header,
643                                  size_t main_fdt_size,
644                                  void *overlay_fdtp,
645                                  size_t overlay_size) {
646   size_t out_fdt_size;
647 
648   if (main_fdt_header == NULL) {
649     return NULL;
650   }
651 
652   if (overlay_size < 8 || overlay_size != fdt_totalsize(overlay_fdtp)) {
653     dto_error("Bad overlay size!\n");
654     return NULL;
655   }
656   if (main_fdt_size < 8 || main_fdt_size != fdt_totalsize(main_fdt_header)) {
657     dto_error("Bad fdt size!\n");
658     return NULL;
659   }
660 
661   out_fdt_size = fdt_totalsize(main_fdt_header) + overlay_size;
662   /* It's actually more than enough */
663   struct fdt_header *out_fdt_header = dto_malloc(out_fdt_size);
664 
665   if (out_fdt_header == NULL) {
666     dto_error("failed to allocate memory for DTB blob with overlays\n");
667     return NULL;
668   }
669 
670   struct ufdt_node_pool pool;
671   ufdt_node_pool_construct(&pool);
672   struct ufdt *main_tree = ufdt_from_fdt(main_fdt_header, main_fdt_size, &pool);
673   struct ufdt *overlay_tree = ufdt_from_fdt(overlay_fdtp, overlay_size, &pool);
674   int err = ufdt_overlay_apply(main_tree, overlay_tree, overlay_size, &pool);
675   if (err < 0) {
676     goto fail;
677   }
678 
679   err = ufdt_to_fdt(main_tree, out_fdt_header, out_fdt_size);
680   if (err < 0) {
681     dto_error("Failed to dump the device tree to out_fdt_header\n");
682     goto fail;
683   }
684 
685   ufdt_destruct(overlay_tree, &pool);
686   ufdt_destruct(main_tree, &pool);
687   ufdt_node_pool_destruct(&pool);
688 
689   return out_fdt_header;
690 
691 fail:
692   ufdt_destruct(overlay_tree, &pool);
693   ufdt_destruct(main_tree, &pool);
694   ufdt_node_pool_destruct(&pool);
695   dto_free(out_fdt_header);
696 
697   return NULL;
698 }
699 
700 /*
701  * Apply device tree `overlays` to `main_fdt_header` fdt buffer. (API is unstable)
702  *
703  * `main_fdt_header` is getting overrided by result tree, so it must
704  * have enough space (provided by `main_fdt_buffer_size`) to store it.
705  * `main_fdt_header` and all `overlays` must be 8 bytes aligned.
706  *
707  * `dto_malloc` is used for:
708  * - ufdt structures around main fdt and overlays.
709  * - result tree temporary buffer at most `main_fdt_buffer_size` size.
710  *
711  * TODO(b/362830550): expose a more comprehensive error type.
712  * Returns 0 or -1 in case of error.
713  */
ufdt_apply_multioverlay(struct fdt_header * main_fdt_header,size_t main_fdt_buffer_size,void * const * overlays,size_t overlays_count)714 int ufdt_apply_multioverlay(struct fdt_header *main_fdt_header,
715                             size_t main_fdt_buffer_size, void *const *overlays,
716                             size_t overlays_count) {
717   void *temporary_buffer = NULL;
718 
719   if (main_fdt_header == NULL || fdt_check_header(main_fdt_header) != 0 ||
720       overlays == NULL) {
721     return -1;
722   }
723   if (overlays_count == 0) {
724     return 0;
725   }
726 
727   size_t result_size = fdt_totalsize(main_fdt_header);
728   struct ufdt_node_pool pool;
729   ufdt_node_pool_construct(&pool);
730   struct ufdt *main_tree = ufdt_from_fdt(main_fdt_header, result_size, &pool);
731 
732   for (size_t i = 0; i < overlays_count; i++) {
733     struct fdt_header *current_overlay = overlays[i];
734     if (fdt_check_header(current_overlay) != 0) {
735       dto_error("Failed to parse %zuth overlay header\n", i);
736       goto error;
737     }
738 
739     size_t overlay_size = fdt_totalsize(current_overlay);
740     result_size += overlay_size;
741 
742     // prepare main tree by rebuilding phandle table. don't need to do so
743     // for the first iteration since main_tree hasn't been updated yet
744     if (i != 0) {
745       main_tree->phandle_table = build_phandle_table(main_tree);
746     }
747 
748     struct ufdt *overlay_tree =
749         ufdt_from_fdt(current_overlay, overlay_size, &pool);
750     int err = ufdt_overlay_apply(main_tree, overlay_tree, overlay_size, &pool);
751     ufdt_destruct(overlay_tree, &pool);
752     if (err < 0) {
753       dto_error("Failed to apply overlay number: %d\n", i);
754       goto error;
755     }
756   }
757 
758   if (result_size > main_fdt_buffer_size) {
759     dto_error(
760         "Not enough space in main_fdt to apply the overlays. Required %d, "
761         "available: %d\n",
762         result_size, main_fdt_buffer_size);
763     goto error;
764   }
765 
766   // ufdt tree has references to fdt buffer, so we cannot dump ufdt to
767   // underlying fdt buffer directly. allocate intermediate buffer for that.
768   temporary_buffer = dto_malloc(result_size);
769   if (temporary_buffer == NULL) {
770     dto_error("Failed to allocate memory for temporary buffer: %d\n",
771               result_size);
772     goto error;
773   }
774 
775   int err = ufdt_to_fdt(main_tree, temporary_buffer, result_size);
776   if (err < 0) {
777     dto_error(
778         "Failed to dump the result device tree to the temporary buffer\n");
779     goto error;
780   }
781   ufdt_destruct(main_tree, &pool);
782 
783   dto_memcpy(main_fdt_header, temporary_buffer, result_size);
784   dto_free(temporary_buffer);
785   ufdt_node_pool_destruct(&pool);
786 
787   return 0;
788 
789 error:
790   dto_free(temporary_buffer);
791   ufdt_destruct(main_tree, &pool);
792   ufdt_node_pool_destruct(&pool);
793 
794   return -1;
795 }
796