Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 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" |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 31 | #include "libufdt.h" |
SzuWei Lin | d62a849 | 2017-04-24 10:17:15 +0800 | [diff] [blame] | 32 | #include "ufdt_node_pool.h" |
Hridya Valsaraju | 5168cab | 2017-12-05 17:31:09 -0800 | [diff] [blame] | 33 | #include "ufdt_overlay_internal.h" |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 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 | */ |
| 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 | */ |
Hridya Valsaraju | 4c7735a | 2018-01-25 14:28:28 -0800 | [diff] [blame] | 70 | uint32_t ufdt_get_max_phandle(struct ufdt *tree) { |
SzuWei Lin | 8a7039c | 2017-04-14 15:38:15 +0800 | [diff] [blame] | 71 | struct ufdt_static_phandle_table sorted_table = tree->phandle_table; |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 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 | */ |
| 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 | */ |
Hridya Valsaraju | 4c7735a | 2018-01-25 14:28:28 -0800 | [diff] [blame] | 99 | void ufdt_try_increase_phandle(struct ufdt *tree, uint32_t offset) { |
SzuWei Lin | 8a7039c | 2017-04-14 15:38:15 +0800 | [diff] [blame] | 100 | struct ufdt_static_phandle_table sorted_table = tree->phandle_table; |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 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 | */ |
Hridya Valsaraju | 5168cab | 2017-12-05 17:31:09 -0800 | [diff] [blame] | 130 | void *ufdt_get_fixup_location(struct ufdt *tree, const char *fixup) { |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 131 | char *path, *prop_ptr, *offset_ptr, *end_ptr; |
| 132 | int prop_offset, prop_len; |
| 133 | const char *prop_data; |
Chris Fries | a3326ad | 2017-04-20 16:23:07 -0500 | [diff] [blame] | 134 | char path_buf[1024]; |
| 135 | char *path_mem = NULL; |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 136 | |
Chris Fries | a3326ad | 2017-04-20 16:23:07 -0500 | [diff] [blame] | 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 | |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 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 valid number\n", offset_ptr); |
| 167 | goto fail; |
| 168 | } |
| 169 | |
| 170 | struct ufdt_node *target_node; |
| 171 | target_node = ufdt_get_node_by_path(tree, path); |
| 172 | if (target_node == NULL) { |
| 173 | dto_error("Path '%s' not found\n", path); |
| 174 | goto fail; |
| 175 | } |
| 176 | |
| 177 | prop_data = |
| 178 | ufdt_node_get_fdt_prop_data_by_name(target_node, prop_ptr, &prop_len); |
| 179 | if (prop_data == NULL) { |
| 180 | dto_error("Property '%s' not found in '%s' node\n", prop_ptr, path); |
| 181 | goto fail; |
| 182 | } |
| 183 | /* |
| 184 | * Note that prop_offset is the offset inside the property data. |
| 185 | */ |
| 186 | if (prop_len < prop_offset + (int)sizeof(uint32_t)) { |
| 187 | dto_error("%s: property length is too small for fixup\n", path); |
| 188 | goto fail; |
| 189 | } |
| 190 | |
Chris Fries | a3326ad | 2017-04-20 16:23:07 -0500 | [diff] [blame] | 191 | if (path_mem) dto_free(path_mem); |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 192 | return (char *)prop_data + prop_offset; |
| 193 | |
| 194 | fail: |
Chris Fries | a3326ad | 2017-04-20 16:23:07 -0500 | [diff] [blame] | 195 | if (path_mem) dto_free(path_mem); |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 196 | return NULL; |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Process one entry in __fixups__ { } node. |
| 201 | * @fixups is property value, array of NUL-terminated strings |
| 202 | * with fixup locations. |
| 203 | * @fixups_len length of the fixups array in bytes. |
| 204 | * @phandle is value for these locations. |
| 205 | */ |
Hridya Valsaraju | 5168cab | 2017-12-05 17:31:09 -0800 | [diff] [blame] | 206 | int ufdt_do_one_fixup(struct ufdt *tree, const char *fixups, int fixups_len, |
| 207 | int phandle) { |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 208 | void *fixup_pos; |
| 209 | uint32_t val; |
| 210 | |
| 211 | val = cpu_to_fdt32(phandle); |
| 212 | |
| 213 | while (fixups_len > 0) { |
| 214 | fixup_pos = ufdt_get_fixup_location(tree, fixups); |
| 215 | if (fixup_pos != NULL) { |
| 216 | dto_memcpy(fixup_pos, &val, sizeof(val)); |
| 217 | } else { |
| 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | fixups_len -= dto_strlen(fixups) + 1; |
| 222 | fixups += dto_strlen(fixups) + 1; |
| 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Handle __fixups__ node in overlay tree. |
| 230 | */ |
| 231 | |
Hridya Valsaraju | 5168cab | 2017-12-05 17:31:09 -0800 | [diff] [blame] | 232 | int ufdt_overlay_do_fixups(struct ufdt *main_tree, struct ufdt *overlay_tree) { |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 233 | int len = 0; |
SzuWei Lin | fba5805 | 2017-03-20 16:11:17 +0800 | [diff] [blame] | 234 | struct ufdt_node *overlay_fixups_node = |
| 235 | ufdt_get_node_by_path(overlay_tree, "/__fixups__"); |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 236 | if (!overlay_fixups_node) { |
SzuWei Lin | fba5805 | 2017-03-20 16:11:17 +0800 | [diff] [blame] | 237 | /* There is no __fixups__. Do nothing. */ |
| 238 | return 0; |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 239 | } |
| 240 | |
SzuWei Lin | fba5805 | 2017-03-20 16:11:17 +0800 | [diff] [blame] | 241 | struct ufdt_node *main_symbols_node = |
| 242 | ufdt_get_node_by_path(main_tree, "/__symbols__"); |
| 243 | |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 244 | struct ufdt_node **it; |
| 245 | for_each_prop(it, overlay_fixups_node) { |
SzuWei Lin | fba5805 | 2017-03-20 16:11:17 +0800 | [diff] [blame] | 246 | /* Find the first property */ |
| 247 | |
| 248 | /* Check __symbols__ is exist when we have any property in __fixups__ */ |
| 249 | if (!main_symbols_node) { |
| 250 | dto_error("No node __symbols__ in main dtb.\n"); |
| 251 | return -1; |
| 252 | } |
| 253 | break; |
| 254 | } |
| 255 | |
| 256 | for_each_prop(it, overlay_fixups_node) { |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 257 | /* |
| 258 | * A property in __fixups__ looks like: |
| 259 | * symbol_name = |
| 260 | * "/path/to/node:prop:offset0\x00/path/to/node:prop:offset1..." |
| 261 | * So we firstly find the node "symbol_name" and obtain its phandle in |
| 262 | * __symbols__ of the main_tree. |
| 263 | */ |
| 264 | |
| 265 | struct ufdt_node *fixups = *it; |
| 266 | char *symbol_path = ufdt_node_get_fdt_prop_data_by_name( |
SzuWei Lin | 8a7039c | 2017-04-14 15:38:15 +0800 | [diff] [blame] | 267 | main_symbols_node, ufdt_node_name(fixups), &len); |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 268 | |
| 269 | if (!symbol_path) { |
SzuWei Lin | 8a7039c | 2017-04-14 15:38:15 +0800 | [diff] [blame] | 270 | dto_error("Couldn't find '%s' symbol in main dtb\n", |
| 271 | ufdt_node_name(fixups)); |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 272 | return -1; |
| 273 | } |
| 274 | |
| 275 | struct ufdt_node *symbol_node; |
| 276 | symbol_node = ufdt_get_node_by_path(main_tree, symbol_path); |
| 277 | |
| 278 | if (!symbol_node) { |
| 279 | dto_error("Couldn't find '%s' path in main dtb\n", symbol_path); |
| 280 | return -1; |
| 281 | } |
| 282 | |
| 283 | uint32_t phandle = ufdt_node_get_phandle(symbol_node); |
| 284 | |
| 285 | const char *fixups_paths = ufdt_node_get_fdt_prop_data(fixups, &len); |
| 286 | |
| 287 | if (ufdt_do_one_fixup(overlay_tree, fixups_paths, len, phandle) < 0) { |
| 288 | dto_error("Failed one fixup in ufdt_do_one_fixup\n"); |
| 289 | return -1; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | /* END of doing fixup in the overlay ufdt. */ |
| 297 | |
| 298 | /* |
| 299 | * Here is to overlay all fragments in the overlay_tree to the main_tree. |
| 300 | * What is "overlay fragment"? The main purpose is to add some subtrees to the |
| 301 | * main_tree in order to complete the entire device tree. |
| 302 | * |
SzuWei Lin | 8a7039c | 2017-04-14 15:38:15 +0800 | [diff] [blame] | 303 | * A fragment consists of two parts: 1. the subtree to be added 2. where it |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 304 | * should be added. |
| 305 | * |
| 306 | * Overlaying a fragment requires: 1. find the node in the main_tree 2. merge |
| 307 | * the subtree into that node in the main_tree. |
| 308 | */ |
| 309 | |
| 310 | /* BEGIN of applying fragments. */ |
| 311 | |
| 312 | /* |
| 313 | * Overlay the overlay_node over target_node. |
| 314 | */ |
| 315 | static int ufdt_overlay_node(struct ufdt_node *target_node, |
SzuWei Lin | d62a849 | 2017-04-24 10:17:15 +0800 | [diff] [blame] | 316 | struct ufdt_node *overlay_node, |
| 317 | struct ufdt_node_pool *pool) { |
| 318 | return ufdt_node_merge_into(target_node, overlay_node, pool); |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 319 | } |
| 320 | |
Hridya Valsaraju | c0957f2 | 2018-01-30 13:01:53 -0800 | [diff] [blame] | 321 | enum overlay_result ufdt_overlay_get_target(struct ufdt *tree, |
| 322 | struct ufdt_node *frag_node, |
| 323 | struct ufdt_node **target_node) { |
| 324 | uint32_t target; |
| 325 | const char *target_path; |
| 326 | const void *val; |
| 327 | *target_node = NULL; |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 328 | |
Hridya Valsaraju | c0957f2 | 2018-01-30 13:01:53 -0800 | [diff] [blame] | 329 | val = ufdt_node_get_fdt_prop_data_by_name(frag_node, "target", NULL); |
| 330 | if (val) { |
| 331 | dto_memcpy(&target, val, sizeof(target)); |
| 332 | target = fdt32_to_cpu(target); |
| 333 | *target_node = ufdt_get_node_by_phandle(tree, target); |
| 334 | if (*target_node == NULL) { |
| 335 | dto_error("failed to find target %04x\n", target); |
| 336 | return OVERLAY_RESULT_TARGET_INVALID; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | if (*target_node == NULL) { |
| 341 | target_path = |
| 342 | ufdt_node_get_fdt_prop_data_by_name(frag_node, "target-path", NULL); |
| 343 | if (target_path == NULL) { |
| 344 | return OVERLAY_RESULT_MISSING_TARGET; |
| 345 | } |
| 346 | |
| 347 | *target_node = ufdt_get_node_by_path(tree, target_path); |
| 348 | if (*target_node == NULL) { |
| 349 | dto_error("failed to find target-path %s\n", target_path); |
| 350 | return OVERLAY_RESULT_TARGET_PATH_INVALID; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | return OVERLAY_RESULT_OK; |
| 355 | } |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 356 | |
| 357 | /* |
| 358 | * Apply one overlay fragment (subtree). |
| 359 | */ |
| 360 | static enum overlay_result ufdt_apply_fragment(struct ufdt *tree, |
SzuWei Lin | d62a849 | 2017-04-24 10:17:15 +0800 | [diff] [blame] | 361 | struct ufdt_node *frag_node, |
| 362 | struct ufdt_node_pool *pool) { |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 363 | struct ufdt_node *target_node = NULL; |
| 364 | struct ufdt_node *overlay_node = NULL; |
| 365 | |
Hridya Valsaraju | f6b572a | 2021-05-04 11:49:43 -0700 | [diff] [blame] | 366 | overlay_node = ufdt_node_get_node_by_path(frag_node, "__overlay__"); |
| 367 | if (overlay_node == NULL) { |
| 368 | return OVERLAY_RESULT_MISSING_OVERLAY; |
| 369 | } |
| 370 | |
Hridya Valsaraju | c0957f2 | 2018-01-30 13:01:53 -0800 | [diff] [blame] | 371 | enum overlay_result result = |
| 372 | ufdt_overlay_get_target(tree, frag_node, &target_node); |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 373 | if (target_node == NULL) { |
Hridya Valsaraju | f6b572a | 2021-05-04 11:49:43 -0700 | [diff] [blame] | 374 | dto_error("Unable to resolve target for %s\n", ufdt_node_name(frag_node)); |
Hridya Valsaraju | c0957f2 | 2018-01-30 13:01:53 -0800 | [diff] [blame] | 375 | return result; |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 376 | } |
| 377 | |
SzuWei Lin | d62a849 | 2017-04-24 10:17:15 +0800 | [diff] [blame] | 378 | int err = ufdt_overlay_node(target_node, overlay_node, pool); |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 379 | |
| 380 | if (err < 0) { |
SzuWei Lin | 8a7039c | 2017-04-14 15:38:15 +0800 | [diff] [blame] | 381 | dto_error("failed to overlay node %s to target %s\n", |
| 382 | ufdt_node_name(overlay_node), ufdt_node_name(target_node)); |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 383 | return OVERLAY_RESULT_MERGE_FAIL; |
| 384 | } |
| 385 | |
| 386 | return OVERLAY_RESULT_OK; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Applies all fragments to the main_tree. |
| 391 | */ |
| 392 | static int ufdt_overlay_apply_fragments(struct ufdt *main_tree, |
SzuWei Lin | d62a849 | 2017-04-24 10:17:15 +0800 | [diff] [blame] | 393 | struct ufdt *overlay_tree, |
| 394 | struct ufdt_node_pool *pool) { |
Hridya Valsaraju | f6b572a | 2021-05-04 11:49:43 -0700 | [diff] [blame] | 395 | enum overlay_result ret; |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 396 | struct ufdt_node **it; |
| 397 | /* |
| 398 | * This loop may iterate to subnodes that's not a fragment node. |
Hridya Valsaraju | f6b572a | 2021-05-04 11:49:43 -0700 | [diff] [blame] | 399 | * We must fail for any other error. |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 400 | */ |
| 401 | for_each_node(it, overlay_tree->root) { |
Hridya Valsaraju | f6b572a | 2021-05-04 11:49:43 -0700 | [diff] [blame] | 402 | ret = ufdt_apply_fragment(main_tree, *it, pool); |
| 403 | if ((ret != OVERLAY_RESULT_OK) && (ret != OVERLAY_RESULT_MISSING_OVERLAY)) { |
| 404 | dto_error("failed to apply overlay fragment %s ret: %d\n", |
| 405 | ufdt_node_name(*it), ret); |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 406 | return -1; |
| 407 | } |
| 408 | } |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | /* END of applying fragments. */ |
| 413 | |
| 414 | /* |
| 415 | * Since the overlay_tree will be "merged" into the main_tree, some |
| 416 | * references (e.g., phandle values that acts as an unique ID) need to be |
| 417 | * updated so it won't lead to collision that different nodes have the same |
| 418 | * phandle value. |
| 419 | * |
| 420 | * Two things need to be done: |
| 421 | * |
| 422 | * 1. ufdt_try_increase_phandle() |
| 423 | * Update phandle (an unique integer ID of a node in the device tree) of each |
| 424 | * node in the overlay_tree. To achieve this, we simply increase each phandle |
| 425 | * values in the overlay_tree by the max phandle value of the main_tree. |
| 426 | * |
| 427 | * 2. ufdt_overlay_do_local_fixups() |
| 428 | * If there are some reference in the overlay_tree that references nodes |
| 429 | * inside the overlay_tree, we have to modify the reference value (address of |
| 430 | * the referenced node: phandle) so that it corresponds to the right node inside |
| 431 | * the overlay_tree. Where the reference exists is kept in __local_fixups__ node |
| 432 | * in the overlay_tree. |
| 433 | */ |
| 434 | |
| 435 | /* BEGIN of updating local references (phandle values) in the overlay ufdt. */ |
| 436 | |
| 437 | /* |
| 438 | * local fixups |
| 439 | */ |
| 440 | static int ufdt_local_fixup_prop(struct ufdt_node *target_prop_node, |
| 441 | struct ufdt_node *local_fixup_prop_node, |
| 442 | uint32_t phandle_offset) { |
| 443 | /* |
| 444 | * prop_offsets_ptr should be a list of fdt32_t. |
| 445 | * <offset0 offset1 offset2 ...> |
| 446 | */ |
| 447 | char *prop_offsets_ptr; |
| 448 | int len = 0; |
| 449 | prop_offsets_ptr = ufdt_node_get_fdt_prop_data(local_fixup_prop_node, &len); |
| 450 | |
Mike McTernan | 46493e9 | 2022-10-31 21:07:09 +0000 | [diff] [blame] | 451 | if (prop_offsets_ptr == NULL || len % sizeof(fdt32_t) != 0) return -1; |
| 452 | |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 453 | char *prop_data; |
| 454 | int target_length = 0; |
| 455 | |
| 456 | prop_data = ufdt_node_get_fdt_prop_data(target_prop_node, &target_length); |
| 457 | |
Mike McTernan | 46493e9 | 2022-10-31 21:07:09 +0000 | [diff] [blame] | 458 | if (prop_data == NULL) return -1; |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 459 | |
| 460 | int i; |
| 461 | for (i = 0; i < len; i += sizeof(fdt32_t)) { |
| 462 | int offset = fdt32_to_cpu(*(fdt32_t *)(prop_offsets_ptr + i)); |
| 463 | if (offset + sizeof(fdt32_t) > (size_t)target_length) return -1; |
| 464 | fdt_increase_u32((prop_data + offset), phandle_offset); |
| 465 | } |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static int ufdt_local_fixup_node(struct ufdt_node *target_node, |
| 470 | struct ufdt_node *local_fixups_node, |
| 471 | uint32_t phandle_offset) { |
| 472 | if (local_fixups_node == NULL) return 0; |
| 473 | |
| 474 | struct ufdt_node **it_local_fixups; |
| 475 | struct ufdt_node *sub_target_node; |
| 476 | |
| 477 | for_each_prop(it_local_fixups, local_fixups_node) { |
SzuWei Lin | 8a7039c | 2017-04-14 15:38:15 +0800 | [diff] [blame] | 478 | sub_target_node = ufdt_node_get_property_by_name( |
| 479 | target_node, ufdt_node_name(*it_local_fixups)); |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 480 | |
| 481 | if (sub_target_node != NULL) { |
| 482 | int err = ufdt_local_fixup_prop(sub_target_node, *it_local_fixups, |
| 483 | phandle_offset); |
| 484 | if (err < 0) return -1; |
| 485 | } else { |
| 486 | return -1; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | for_each_node(it_local_fixups, local_fixups_node) { |
SzuWei Lin | 8a7039c | 2017-04-14 15:38:15 +0800 | [diff] [blame] | 491 | sub_target_node = ufdt_node_get_node_by_path( |
| 492 | target_node, ufdt_node_name(*it_local_fixups)); |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 493 | if (sub_target_node != NULL) { |
| 494 | int err = ufdt_local_fixup_node(sub_target_node, *it_local_fixups, |
| 495 | phandle_offset); |
| 496 | if (err < 0) return -1; |
| 497 | } else { |
| 498 | return -1; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | * Handle __local_fixups__ node in overlay DTB |
| 507 | * The __local_fixups__ format we expect is |
| 508 | * __local_fixups__ { |
| 509 | * path { |
| 510 | * to { |
| 511 | * local_ref1 = <offset>; |
| 512 | * }; |
| 513 | * }; |
| 514 | * path2 { |
| 515 | * to2 { |
| 516 | * local_ref2 = <offset1 offset2 ...>; |
| 517 | * }; |
| 518 | * }; |
| 519 | * }; |
| 520 | * |
| 521 | * which follows the dtc patch from: |
| 522 | * https://marc.info/?l=devicetree&m=144061468601974&w=4 |
| 523 | */ |
Hridya Valsaraju | 4c7735a | 2018-01-25 14:28:28 -0800 | [diff] [blame] | 524 | int ufdt_overlay_do_local_fixups(struct ufdt *tree, uint32_t phandle_offset) { |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 525 | struct ufdt_node *overlay_node = ufdt_get_node_by_path(tree, "/"); |
| 526 | struct ufdt_node *local_fixups_node = |
| 527 | ufdt_get_node_by_path(tree, "/__local_fixups__"); |
| 528 | |
| 529 | int err = |
| 530 | ufdt_local_fixup_node(overlay_node, local_fixups_node, phandle_offset); |
| 531 | |
| 532 | if (err < 0) return -1; |
| 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | static int ufdt_overlay_local_ref_update(struct ufdt *main_tree, |
| 538 | struct ufdt *overlay_tree) { |
| 539 | uint32_t phandle_offset = 0; |
| 540 | |
| 541 | phandle_offset = ufdt_get_max_phandle(main_tree); |
| 542 | if (phandle_offset > 0) { |
| 543 | ufdt_try_increase_phandle(overlay_tree, phandle_offset); |
| 544 | } |
| 545 | |
| 546 | int err = ufdt_overlay_do_local_fixups(overlay_tree, phandle_offset); |
| 547 | if (err < 0) { |
| 548 | dto_error("failed to perform local fixups in overlay\n"); |
| 549 | return -1; |
| 550 | } |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | /* END of updating local references (phandle values) in the overlay ufdt. */ |
| 555 | |
SzuWei Lin | 1be68ae | 2017-03-30 11:44:54 +0800 | [diff] [blame] | 556 | static int _ufdt_overlay_fdtps(struct ufdt *main_tree, |
| 557 | const struct ufdt *overlay_tree) { |
| 558 | for (int i = 0; i < overlay_tree->num_used_fdtps; i++) { |
| 559 | void *fdt = overlay_tree->fdtps[i]; |
| 560 | if (ufdt_add_fdt(main_tree, fdt) < 0) { |
| 561 | return -1; |
| 562 | } |
| 563 | } |
| 564 | return 0; |
| 565 | } |
| 566 | |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 567 | static int ufdt_overlay_apply(struct ufdt *main_tree, struct ufdt *overlay_tree, |
SzuWei Lin | d62a849 | 2017-04-24 10:17:15 +0800 | [diff] [blame] | 568 | size_t overlay_length, |
| 569 | struct ufdt_node_pool *pool) { |
SzuWei Lin | 1be68ae | 2017-03-30 11:44:54 +0800 | [diff] [blame] | 570 | if (_ufdt_overlay_fdtps(main_tree, overlay_tree) < 0) { |
| 571 | dto_error("failed to add more fdt into main ufdt tree.\n"); |
| 572 | return -1; |
| 573 | } |
| 574 | |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 575 | if (overlay_length < sizeof(struct fdt_header)) { |
| 576 | dto_error("Overlay_length %zu smaller than header size %zu\n", |
| 577 | overlay_length, sizeof(struct fdt_header)); |
| 578 | return -1; |
| 579 | } |
| 580 | |
| 581 | if (ufdt_overlay_local_ref_update(main_tree, overlay_tree) < 0) { |
| 582 | dto_error("failed to perform local fixups in overlay\n"); |
| 583 | return -1; |
| 584 | } |
| 585 | |
| 586 | if (ufdt_overlay_do_fixups(main_tree, overlay_tree) < 0) { |
| 587 | dto_error("failed to perform fixups in overlay\n"); |
| 588 | return -1; |
| 589 | } |
SzuWei Lin | d62a849 | 2017-04-24 10:17:15 +0800 | [diff] [blame] | 590 | if (ufdt_overlay_apply_fragments(main_tree, overlay_tree, pool) < 0) { |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 591 | dto_error("failed to apply fragments\n"); |
| 592 | return -1; |
| 593 | } |
| 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | struct fdt_header *ufdt_install_blob(void *blob, size_t blob_size) { |
| 599 | struct fdt_header *pHeader; |
| 600 | int err; |
| 601 | |
| 602 | dto_debug("ufdt_install_blob (0x%08jx)\n", (uintmax_t)blob); |
| 603 | |
| 604 | if (blob_size < sizeof(struct fdt_header)) { |
| 605 | dto_error("Blob_size %zu smaller than the header size %zu\n", blob_size, |
| 606 | sizeof(struct fdt_header)); |
| 607 | return NULL; |
| 608 | } |
| 609 | |
| 610 | pHeader = (struct fdt_header *)blob; |
| 611 | err = fdt_check_header(pHeader); |
| 612 | if (err < 0) { |
| 613 | if (err == -FDT_ERR_BADVERSION) { |
| 614 | dto_error("incompatible blob version: %d, should be: %d", |
| 615 | fdt_version(pHeader), FDT_LAST_SUPPORTED_VERSION); |
| 616 | |
| 617 | } else { |
| 618 | dto_error("error validating blob: %s", fdt_strerror(err)); |
| 619 | } |
| 620 | return NULL; |
| 621 | } |
| 622 | |
| 623 | return pHeader; |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | * From Google, based on dt_overlay_apply() logic |
| 628 | * Will dto_malloc a new fdt blob and return it. Will not dto_free parameters. |
| 629 | */ |
| 630 | struct fdt_header *ufdt_apply_overlay(struct fdt_header *main_fdt_header, |
| 631 | size_t main_fdt_size, |
| 632 | void *overlay_fdtp, |
| 633 | size_t overlay_size) { |
| 634 | size_t out_fdt_size; |
| 635 | |
| 636 | if (main_fdt_header == NULL) { |
| 637 | return NULL; |
| 638 | } |
| 639 | |
| 640 | if (overlay_size < 8 || overlay_size != fdt_totalsize(overlay_fdtp)) { |
| 641 | dto_error("Bad overlay size!\n"); |
| 642 | return NULL; |
| 643 | } |
| 644 | if (main_fdt_size < 8 || main_fdt_size != fdt_totalsize(main_fdt_header)) { |
| 645 | dto_error("Bad fdt size!\n"); |
| 646 | return NULL; |
| 647 | } |
| 648 | |
| 649 | out_fdt_size = fdt_totalsize(main_fdt_header) + overlay_size; |
| 650 | /* It's actually more than enough */ |
| 651 | struct fdt_header *out_fdt_header = dto_malloc(out_fdt_size); |
| 652 | |
| 653 | if (out_fdt_header == NULL) { |
| 654 | dto_error("failed to allocate memory for DTB blob with overlays\n"); |
| 655 | return NULL; |
| 656 | } |
| 657 | |
SzuWei Lin | d62a849 | 2017-04-24 10:17:15 +0800 | [diff] [blame] | 658 | struct ufdt_node_pool pool; |
| 659 | ufdt_node_pool_construct(&pool); |
| 660 | struct ufdt *main_tree = ufdt_from_fdt(main_fdt_header, main_fdt_size, &pool); |
| 661 | struct ufdt *overlay_tree = ufdt_from_fdt(overlay_fdtp, overlay_size, &pool); |
| 662 | int err = ufdt_overlay_apply(main_tree, overlay_tree, overlay_size, &pool); |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 663 | if (err < 0) { |
| 664 | goto fail; |
| 665 | } |
| 666 | |
| 667 | err = ufdt_to_fdt(main_tree, out_fdt_header, out_fdt_size); |
| 668 | if (err < 0) { |
| 669 | dto_error("Failed to dump the device tree to out_fdt_header\n"); |
| 670 | goto fail; |
| 671 | } |
| 672 | |
SzuWei Lin | d62a849 | 2017-04-24 10:17:15 +0800 | [diff] [blame] | 673 | ufdt_destruct(overlay_tree, &pool); |
| 674 | ufdt_destruct(main_tree, &pool); |
| 675 | ufdt_node_pool_destruct(&pool); |
SzuWei Lin | 70107c8 | 2017-04-07 18:56:13 +0800 | [diff] [blame] | 676 | |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 677 | return out_fdt_header; |
| 678 | |
| 679 | fail: |
SzuWei Lin | d62a849 | 2017-04-24 10:17:15 +0800 | [diff] [blame] | 680 | ufdt_destruct(overlay_tree, &pool); |
| 681 | ufdt_destruct(main_tree, &pool); |
| 682 | ufdt_node_pool_destruct(&pool); |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 683 | dto_free(out_fdt_header); |
SzuWei Lin | 70107c8 | 2017-04-07 18:56:13 +0800 | [diff] [blame] | 684 | |
Li Chen | f6c209b | 2016-11-28 12:15:33 +0800 | [diff] [blame] | 685 | return NULL; |
| 686 | } |