18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * libfdt - Flat Device Tree manipulation 48c2ecf20Sopenharmony_ci * Copyright (C) 2016 Free Electrons 58c2ecf20Sopenharmony_ci * Copyright (C) 2016 NextThing Co. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci#include "libfdt_env.h" 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <fdt.h> 108c2ecf20Sopenharmony_ci#include <libfdt.h> 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include "libfdt_internal.h" 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci/** 158c2ecf20Sopenharmony_ci * overlay_get_target_phandle - retrieves the target phandle of a fragment 168c2ecf20Sopenharmony_ci * @fdto: pointer to the device tree overlay blob 178c2ecf20Sopenharmony_ci * @fragment: node offset of the fragment in the overlay 188c2ecf20Sopenharmony_ci * 198c2ecf20Sopenharmony_ci * overlay_get_target_phandle() retrieves the target phandle of an 208c2ecf20Sopenharmony_ci * overlay fragment when that fragment uses a phandle (target 218c2ecf20Sopenharmony_ci * property) instead of a path (target-path property). 228c2ecf20Sopenharmony_ci * 238c2ecf20Sopenharmony_ci * returns: 248c2ecf20Sopenharmony_ci * the phandle pointed by the target property 258c2ecf20Sopenharmony_ci * 0, if the phandle was not found 268c2ecf20Sopenharmony_ci * -1, if the phandle was malformed 278c2ecf20Sopenharmony_ci */ 288c2ecf20Sopenharmony_cistatic uint32_t overlay_get_target_phandle(const void *fdto, int fragment) 298c2ecf20Sopenharmony_ci{ 308c2ecf20Sopenharmony_ci const fdt32_t *val; 318c2ecf20Sopenharmony_ci int len; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci val = fdt_getprop(fdto, fragment, "target", &len); 348c2ecf20Sopenharmony_ci if (!val) 358c2ecf20Sopenharmony_ci return 0; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci if ((len != sizeof(*val)) || (fdt32_to_cpu(*val) == (uint32_t)-1)) 388c2ecf20Sopenharmony_ci return (uint32_t)-1; 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci return fdt32_to_cpu(*val); 418c2ecf20Sopenharmony_ci} 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci/** 448c2ecf20Sopenharmony_ci * overlay_get_target - retrieves the offset of a fragment's target 458c2ecf20Sopenharmony_ci * @fdt: Base device tree blob 468c2ecf20Sopenharmony_ci * @fdto: Device tree overlay blob 478c2ecf20Sopenharmony_ci * @fragment: node offset of the fragment in the overlay 488c2ecf20Sopenharmony_ci * @pathp: pointer which receives the path of the target (or NULL) 498c2ecf20Sopenharmony_ci * 508c2ecf20Sopenharmony_ci * overlay_get_target() retrieves the target offset in the base 518c2ecf20Sopenharmony_ci * device tree of a fragment, no matter how the actual targeting is 528c2ecf20Sopenharmony_ci * done (through a phandle or a path) 538c2ecf20Sopenharmony_ci * 548c2ecf20Sopenharmony_ci * returns: 558c2ecf20Sopenharmony_ci * the targeted node offset in the base device tree 568c2ecf20Sopenharmony_ci * Negative error code on error 578c2ecf20Sopenharmony_ci */ 588c2ecf20Sopenharmony_cistatic int overlay_get_target(const void *fdt, const void *fdto, 598c2ecf20Sopenharmony_ci int fragment, char const **pathp) 608c2ecf20Sopenharmony_ci{ 618c2ecf20Sopenharmony_ci uint32_t phandle; 628c2ecf20Sopenharmony_ci const char *path = NULL; 638c2ecf20Sopenharmony_ci int path_len = 0, ret; 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci /* Try first to do a phandle based lookup */ 668c2ecf20Sopenharmony_ci phandle = overlay_get_target_phandle(fdto, fragment); 678c2ecf20Sopenharmony_ci if (phandle == (uint32_t)-1) 688c2ecf20Sopenharmony_ci return -FDT_ERR_BADPHANDLE; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci /* no phandle, try path */ 718c2ecf20Sopenharmony_ci if (!phandle) { 728c2ecf20Sopenharmony_ci /* And then a path based lookup */ 738c2ecf20Sopenharmony_ci path = fdt_getprop(fdto, fragment, "target-path", &path_len); 748c2ecf20Sopenharmony_ci if (path) 758c2ecf20Sopenharmony_ci ret = fdt_path_offset(fdt, path); 768c2ecf20Sopenharmony_ci else 778c2ecf20Sopenharmony_ci ret = path_len; 788c2ecf20Sopenharmony_ci } else 798c2ecf20Sopenharmony_ci ret = fdt_node_offset_by_phandle(fdt, phandle); 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci /* 828c2ecf20Sopenharmony_ci * If we haven't found either a target or a 838c2ecf20Sopenharmony_ci * target-path property in a node that contains a 848c2ecf20Sopenharmony_ci * __overlay__ subnode (we wouldn't be called 858c2ecf20Sopenharmony_ci * otherwise), consider it a improperly written 868c2ecf20Sopenharmony_ci * overlay 878c2ecf20Sopenharmony_ci */ 888c2ecf20Sopenharmony_ci if (ret < 0 && path_len == -FDT_ERR_NOTFOUND) 898c2ecf20Sopenharmony_ci ret = -FDT_ERR_BADOVERLAY; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci /* return on error */ 928c2ecf20Sopenharmony_ci if (ret < 0) 938c2ecf20Sopenharmony_ci return ret; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci /* return pointer to path (if available) */ 968c2ecf20Sopenharmony_ci if (pathp) 978c2ecf20Sopenharmony_ci *pathp = path ? path : NULL; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci return ret; 1008c2ecf20Sopenharmony_ci} 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci/** 1038c2ecf20Sopenharmony_ci * overlay_phandle_add_offset - Increases a phandle by an offset 1048c2ecf20Sopenharmony_ci * @fdt: Base device tree blob 1058c2ecf20Sopenharmony_ci * @node: Device tree overlay blob 1068c2ecf20Sopenharmony_ci * @name: Name of the property to modify (phandle or linux,phandle) 1078c2ecf20Sopenharmony_ci * @delta: offset to apply 1088c2ecf20Sopenharmony_ci * 1098c2ecf20Sopenharmony_ci * overlay_phandle_add_offset() increments a node phandle by a given 1108c2ecf20Sopenharmony_ci * offset. 1118c2ecf20Sopenharmony_ci * 1128c2ecf20Sopenharmony_ci * returns: 1138c2ecf20Sopenharmony_ci * 0 on success. 1148c2ecf20Sopenharmony_ci * Negative error code on error 1158c2ecf20Sopenharmony_ci */ 1168c2ecf20Sopenharmony_cistatic int overlay_phandle_add_offset(void *fdt, int node, 1178c2ecf20Sopenharmony_ci const char *name, uint32_t delta) 1188c2ecf20Sopenharmony_ci{ 1198c2ecf20Sopenharmony_ci const fdt32_t *val; 1208c2ecf20Sopenharmony_ci uint32_t adj_val; 1218c2ecf20Sopenharmony_ci int len; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci val = fdt_getprop(fdt, node, name, &len); 1248c2ecf20Sopenharmony_ci if (!val) 1258c2ecf20Sopenharmony_ci return len; 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci if (len != sizeof(*val)) 1288c2ecf20Sopenharmony_ci return -FDT_ERR_BADPHANDLE; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci adj_val = fdt32_to_cpu(*val); 1318c2ecf20Sopenharmony_ci if ((adj_val + delta) < adj_val) 1328c2ecf20Sopenharmony_ci return -FDT_ERR_NOPHANDLES; 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci adj_val += delta; 1358c2ecf20Sopenharmony_ci if (adj_val == (uint32_t)-1) 1368c2ecf20Sopenharmony_ci return -FDT_ERR_NOPHANDLES; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci return fdt_setprop_inplace_u32(fdt, node, name, adj_val); 1398c2ecf20Sopenharmony_ci} 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci/** 1428c2ecf20Sopenharmony_ci * overlay_adjust_node_phandles - Offsets the phandles of a node 1438c2ecf20Sopenharmony_ci * @fdto: Device tree overlay blob 1448c2ecf20Sopenharmony_ci * @node: Offset of the node we want to adjust 1458c2ecf20Sopenharmony_ci * @delta: Offset to shift the phandles of 1468c2ecf20Sopenharmony_ci * 1478c2ecf20Sopenharmony_ci * overlay_adjust_node_phandles() adds a constant to all the phandles 1488c2ecf20Sopenharmony_ci * of a given node. This is mainly use as part of the overlay 1498c2ecf20Sopenharmony_ci * application process, when we want to update all the overlay 1508c2ecf20Sopenharmony_ci * phandles to not conflict with the overlays of the base device tree. 1518c2ecf20Sopenharmony_ci * 1528c2ecf20Sopenharmony_ci * returns: 1538c2ecf20Sopenharmony_ci * 0 on success 1548c2ecf20Sopenharmony_ci * Negative error code on failure 1558c2ecf20Sopenharmony_ci */ 1568c2ecf20Sopenharmony_cistatic int overlay_adjust_node_phandles(void *fdto, int node, 1578c2ecf20Sopenharmony_ci uint32_t delta) 1588c2ecf20Sopenharmony_ci{ 1598c2ecf20Sopenharmony_ci int child; 1608c2ecf20Sopenharmony_ci int ret; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci ret = overlay_phandle_add_offset(fdto, node, "phandle", delta); 1638c2ecf20Sopenharmony_ci if (ret && ret != -FDT_ERR_NOTFOUND) 1648c2ecf20Sopenharmony_ci return ret; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci ret = overlay_phandle_add_offset(fdto, node, "linux,phandle", delta); 1678c2ecf20Sopenharmony_ci if (ret && ret != -FDT_ERR_NOTFOUND) 1688c2ecf20Sopenharmony_ci return ret; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci fdt_for_each_subnode(child, fdto, node) { 1718c2ecf20Sopenharmony_ci ret = overlay_adjust_node_phandles(fdto, child, delta); 1728c2ecf20Sopenharmony_ci if (ret) 1738c2ecf20Sopenharmony_ci return ret; 1748c2ecf20Sopenharmony_ci } 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci return 0; 1778c2ecf20Sopenharmony_ci} 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci/** 1808c2ecf20Sopenharmony_ci * overlay_adjust_local_phandles - Adjust the phandles of a whole overlay 1818c2ecf20Sopenharmony_ci * @fdto: Device tree overlay blob 1828c2ecf20Sopenharmony_ci * @delta: Offset to shift the phandles of 1838c2ecf20Sopenharmony_ci * 1848c2ecf20Sopenharmony_ci * overlay_adjust_local_phandles() adds a constant to all the 1858c2ecf20Sopenharmony_ci * phandles of an overlay. This is mainly use as part of the overlay 1868c2ecf20Sopenharmony_ci * application process, when we want to update all the overlay 1878c2ecf20Sopenharmony_ci * phandles to not conflict with the overlays of the base device tree. 1888c2ecf20Sopenharmony_ci * 1898c2ecf20Sopenharmony_ci * returns: 1908c2ecf20Sopenharmony_ci * 0 on success 1918c2ecf20Sopenharmony_ci * Negative error code on failure 1928c2ecf20Sopenharmony_ci */ 1938c2ecf20Sopenharmony_cistatic int overlay_adjust_local_phandles(void *fdto, uint32_t delta) 1948c2ecf20Sopenharmony_ci{ 1958c2ecf20Sopenharmony_ci /* 1968c2ecf20Sopenharmony_ci * Start adjusting the phandles from the overlay root 1978c2ecf20Sopenharmony_ci */ 1988c2ecf20Sopenharmony_ci return overlay_adjust_node_phandles(fdto, 0, delta); 1998c2ecf20Sopenharmony_ci} 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci/** 2028c2ecf20Sopenharmony_ci * overlay_update_local_node_references - Adjust the overlay references 2038c2ecf20Sopenharmony_ci * @fdto: Device tree overlay blob 2048c2ecf20Sopenharmony_ci * @tree_node: Node offset of the node to operate on 2058c2ecf20Sopenharmony_ci * @fixup_node: Node offset of the matching local fixups node 2068c2ecf20Sopenharmony_ci * @delta: Offset to shift the phandles of 2078c2ecf20Sopenharmony_ci * 2088c2ecf20Sopenharmony_ci * overlay_update_local_nodes_references() update the phandles 2098c2ecf20Sopenharmony_ci * pointing to a node within the device tree overlay by adding a 2108c2ecf20Sopenharmony_ci * constant delta. 2118c2ecf20Sopenharmony_ci * 2128c2ecf20Sopenharmony_ci * This is mainly used as part of a device tree application process, 2138c2ecf20Sopenharmony_ci * where you want the device tree overlays phandles to not conflict 2148c2ecf20Sopenharmony_ci * with the ones from the base device tree before merging them. 2158c2ecf20Sopenharmony_ci * 2168c2ecf20Sopenharmony_ci * returns: 2178c2ecf20Sopenharmony_ci * 0 on success 2188c2ecf20Sopenharmony_ci * Negative error code on failure 2198c2ecf20Sopenharmony_ci */ 2208c2ecf20Sopenharmony_cistatic int overlay_update_local_node_references(void *fdto, 2218c2ecf20Sopenharmony_ci int tree_node, 2228c2ecf20Sopenharmony_ci int fixup_node, 2238c2ecf20Sopenharmony_ci uint32_t delta) 2248c2ecf20Sopenharmony_ci{ 2258c2ecf20Sopenharmony_ci int fixup_prop; 2268c2ecf20Sopenharmony_ci int fixup_child; 2278c2ecf20Sopenharmony_ci int ret; 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci fdt_for_each_property_offset(fixup_prop, fdto, fixup_node) { 2308c2ecf20Sopenharmony_ci const fdt32_t *fixup_val; 2318c2ecf20Sopenharmony_ci const char *tree_val; 2328c2ecf20Sopenharmony_ci const char *name; 2338c2ecf20Sopenharmony_ci int fixup_len; 2348c2ecf20Sopenharmony_ci int tree_len; 2358c2ecf20Sopenharmony_ci int i; 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci fixup_val = fdt_getprop_by_offset(fdto, fixup_prop, 2388c2ecf20Sopenharmony_ci &name, &fixup_len); 2398c2ecf20Sopenharmony_ci if (!fixup_val) 2408c2ecf20Sopenharmony_ci return fixup_len; 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci if (fixup_len % sizeof(uint32_t)) 2438c2ecf20Sopenharmony_ci return -FDT_ERR_BADOVERLAY; 2448c2ecf20Sopenharmony_ci fixup_len /= sizeof(uint32_t); 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci tree_val = fdt_getprop(fdto, tree_node, name, &tree_len); 2478c2ecf20Sopenharmony_ci if (!tree_val) { 2488c2ecf20Sopenharmony_ci if (tree_len == -FDT_ERR_NOTFOUND) 2498c2ecf20Sopenharmony_ci return -FDT_ERR_BADOVERLAY; 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci return tree_len; 2528c2ecf20Sopenharmony_ci } 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci for (i = 0; i < fixup_len; i++) { 2558c2ecf20Sopenharmony_ci fdt32_t adj_val; 2568c2ecf20Sopenharmony_ci uint32_t poffset; 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci poffset = fdt32_to_cpu(fixup_val[i]); 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci /* 2618c2ecf20Sopenharmony_ci * phandles to fixup can be unaligned. 2628c2ecf20Sopenharmony_ci * 2638c2ecf20Sopenharmony_ci * Use a memcpy for the architectures that do 2648c2ecf20Sopenharmony_ci * not support unaligned accesses. 2658c2ecf20Sopenharmony_ci */ 2668c2ecf20Sopenharmony_ci memcpy(&adj_val, tree_val + poffset, sizeof(adj_val)); 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci adj_val = cpu_to_fdt32(fdt32_to_cpu(adj_val) + delta); 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci ret = fdt_setprop_inplace_namelen_partial(fdto, 2718c2ecf20Sopenharmony_ci tree_node, 2728c2ecf20Sopenharmony_ci name, 2738c2ecf20Sopenharmony_ci strlen(name), 2748c2ecf20Sopenharmony_ci poffset, 2758c2ecf20Sopenharmony_ci &adj_val, 2768c2ecf20Sopenharmony_ci sizeof(adj_val)); 2778c2ecf20Sopenharmony_ci if (ret == -FDT_ERR_NOSPACE) 2788c2ecf20Sopenharmony_ci return -FDT_ERR_BADOVERLAY; 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci if (ret) 2818c2ecf20Sopenharmony_ci return ret; 2828c2ecf20Sopenharmony_ci } 2838c2ecf20Sopenharmony_ci } 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci fdt_for_each_subnode(fixup_child, fdto, fixup_node) { 2868c2ecf20Sopenharmony_ci const char *fixup_child_name = fdt_get_name(fdto, fixup_child, 2878c2ecf20Sopenharmony_ci NULL); 2888c2ecf20Sopenharmony_ci int tree_child; 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_ci tree_child = fdt_subnode_offset(fdto, tree_node, 2918c2ecf20Sopenharmony_ci fixup_child_name); 2928c2ecf20Sopenharmony_ci if (tree_child == -FDT_ERR_NOTFOUND) 2938c2ecf20Sopenharmony_ci return -FDT_ERR_BADOVERLAY; 2948c2ecf20Sopenharmony_ci if (tree_child < 0) 2958c2ecf20Sopenharmony_ci return tree_child; 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci ret = overlay_update_local_node_references(fdto, 2988c2ecf20Sopenharmony_ci tree_child, 2998c2ecf20Sopenharmony_ci fixup_child, 3008c2ecf20Sopenharmony_ci delta); 3018c2ecf20Sopenharmony_ci if (ret) 3028c2ecf20Sopenharmony_ci return ret; 3038c2ecf20Sopenharmony_ci } 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci return 0; 3068c2ecf20Sopenharmony_ci} 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_ci/** 3098c2ecf20Sopenharmony_ci * overlay_update_local_references - Adjust the overlay references 3108c2ecf20Sopenharmony_ci * @fdto: Device tree overlay blob 3118c2ecf20Sopenharmony_ci * @delta: Offset to shift the phandles of 3128c2ecf20Sopenharmony_ci * 3138c2ecf20Sopenharmony_ci * overlay_update_local_references() update all the phandles pointing 3148c2ecf20Sopenharmony_ci * to a node within the device tree overlay by adding a constant 3158c2ecf20Sopenharmony_ci * delta to not conflict with the base overlay. 3168c2ecf20Sopenharmony_ci * 3178c2ecf20Sopenharmony_ci * This is mainly used as part of a device tree application process, 3188c2ecf20Sopenharmony_ci * where you want the device tree overlays phandles to not conflict 3198c2ecf20Sopenharmony_ci * with the ones from the base device tree before merging them. 3208c2ecf20Sopenharmony_ci * 3218c2ecf20Sopenharmony_ci * returns: 3228c2ecf20Sopenharmony_ci * 0 on success 3238c2ecf20Sopenharmony_ci * Negative error code on failure 3248c2ecf20Sopenharmony_ci */ 3258c2ecf20Sopenharmony_cistatic int overlay_update_local_references(void *fdto, uint32_t delta) 3268c2ecf20Sopenharmony_ci{ 3278c2ecf20Sopenharmony_ci int fixups; 3288c2ecf20Sopenharmony_ci 3298c2ecf20Sopenharmony_ci fixups = fdt_path_offset(fdto, "/__local_fixups__"); 3308c2ecf20Sopenharmony_ci if (fixups < 0) { 3318c2ecf20Sopenharmony_ci /* There's no local phandles to adjust, bail out */ 3328c2ecf20Sopenharmony_ci if (fixups == -FDT_ERR_NOTFOUND) 3338c2ecf20Sopenharmony_ci return 0; 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ci return fixups; 3368c2ecf20Sopenharmony_ci } 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci /* 3398c2ecf20Sopenharmony_ci * Update our local references from the root of the tree 3408c2ecf20Sopenharmony_ci */ 3418c2ecf20Sopenharmony_ci return overlay_update_local_node_references(fdto, 0, fixups, 3428c2ecf20Sopenharmony_ci delta); 3438c2ecf20Sopenharmony_ci} 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci/** 3468c2ecf20Sopenharmony_ci * overlay_fixup_one_phandle - Set an overlay phandle to the base one 3478c2ecf20Sopenharmony_ci * @fdt: Base Device Tree blob 3488c2ecf20Sopenharmony_ci * @fdto: Device tree overlay blob 3498c2ecf20Sopenharmony_ci * @symbols_off: Node offset of the symbols node in the base device tree 3508c2ecf20Sopenharmony_ci * @path: Path to a node holding a phandle in the overlay 3518c2ecf20Sopenharmony_ci * @path_len: number of path characters to consider 3528c2ecf20Sopenharmony_ci * @name: Name of the property holding the phandle reference in the overlay 3538c2ecf20Sopenharmony_ci * @name_len: number of name characters to consider 3548c2ecf20Sopenharmony_ci * @poffset: Offset within the overlay property where the phandle is stored 3558c2ecf20Sopenharmony_ci * @label: Label of the node referenced by the phandle 3568c2ecf20Sopenharmony_ci * 3578c2ecf20Sopenharmony_ci * overlay_fixup_one_phandle() resolves an overlay phandle pointing to 3588c2ecf20Sopenharmony_ci * a node in the base device tree. 3598c2ecf20Sopenharmony_ci * 3608c2ecf20Sopenharmony_ci * This is part of the device tree overlay application process, when 3618c2ecf20Sopenharmony_ci * you want all the phandles in the overlay to point to the actual 3628c2ecf20Sopenharmony_ci * base dt nodes. 3638c2ecf20Sopenharmony_ci * 3648c2ecf20Sopenharmony_ci * returns: 3658c2ecf20Sopenharmony_ci * 0 on success 3668c2ecf20Sopenharmony_ci * Negative error code on failure 3678c2ecf20Sopenharmony_ci */ 3688c2ecf20Sopenharmony_cistatic int overlay_fixup_one_phandle(void *fdt, void *fdto, 3698c2ecf20Sopenharmony_ci int symbols_off, 3708c2ecf20Sopenharmony_ci const char *path, uint32_t path_len, 3718c2ecf20Sopenharmony_ci const char *name, uint32_t name_len, 3728c2ecf20Sopenharmony_ci int poffset, const char *label) 3738c2ecf20Sopenharmony_ci{ 3748c2ecf20Sopenharmony_ci const char *symbol_path; 3758c2ecf20Sopenharmony_ci uint32_t phandle; 3768c2ecf20Sopenharmony_ci fdt32_t phandle_prop; 3778c2ecf20Sopenharmony_ci int symbol_off, fixup_off; 3788c2ecf20Sopenharmony_ci int prop_len; 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_ci if (symbols_off < 0) 3818c2ecf20Sopenharmony_ci return symbols_off; 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ci symbol_path = fdt_getprop(fdt, symbols_off, label, 3848c2ecf20Sopenharmony_ci &prop_len); 3858c2ecf20Sopenharmony_ci if (!symbol_path) 3868c2ecf20Sopenharmony_ci return prop_len; 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci symbol_off = fdt_path_offset(fdt, symbol_path); 3898c2ecf20Sopenharmony_ci if (symbol_off < 0) 3908c2ecf20Sopenharmony_ci return symbol_off; 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_ci phandle = fdt_get_phandle(fdt, symbol_off); 3938c2ecf20Sopenharmony_ci if (!phandle) 3948c2ecf20Sopenharmony_ci return -FDT_ERR_NOTFOUND; 3958c2ecf20Sopenharmony_ci 3968c2ecf20Sopenharmony_ci fixup_off = fdt_path_offset_namelen(fdto, path, path_len); 3978c2ecf20Sopenharmony_ci if (fixup_off == -FDT_ERR_NOTFOUND) 3988c2ecf20Sopenharmony_ci return -FDT_ERR_BADOVERLAY; 3998c2ecf20Sopenharmony_ci if (fixup_off < 0) 4008c2ecf20Sopenharmony_ci return fixup_off; 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci phandle_prop = cpu_to_fdt32(phandle); 4038c2ecf20Sopenharmony_ci return fdt_setprop_inplace_namelen_partial(fdto, fixup_off, 4048c2ecf20Sopenharmony_ci name, name_len, poffset, 4058c2ecf20Sopenharmony_ci &phandle_prop, 4068c2ecf20Sopenharmony_ci sizeof(phandle_prop)); 4078c2ecf20Sopenharmony_ci}; 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci/** 4108c2ecf20Sopenharmony_ci * overlay_fixup_phandle - Set an overlay phandle to the base one 4118c2ecf20Sopenharmony_ci * @fdt: Base Device Tree blob 4128c2ecf20Sopenharmony_ci * @fdto: Device tree overlay blob 4138c2ecf20Sopenharmony_ci * @symbols_off: Node offset of the symbols node in the base device tree 4148c2ecf20Sopenharmony_ci * @property: Property offset in the overlay holding the list of fixups 4158c2ecf20Sopenharmony_ci * 4168c2ecf20Sopenharmony_ci * overlay_fixup_phandle() resolves all the overlay phandles pointed 4178c2ecf20Sopenharmony_ci * to in a __fixups__ property, and updates them to match the phandles 4188c2ecf20Sopenharmony_ci * in use in the base device tree. 4198c2ecf20Sopenharmony_ci * 4208c2ecf20Sopenharmony_ci * This is part of the device tree overlay application process, when 4218c2ecf20Sopenharmony_ci * you want all the phandles in the overlay to point to the actual 4228c2ecf20Sopenharmony_ci * base dt nodes. 4238c2ecf20Sopenharmony_ci * 4248c2ecf20Sopenharmony_ci * returns: 4258c2ecf20Sopenharmony_ci * 0 on success 4268c2ecf20Sopenharmony_ci * Negative error code on failure 4278c2ecf20Sopenharmony_ci */ 4288c2ecf20Sopenharmony_cistatic int overlay_fixup_phandle(void *fdt, void *fdto, int symbols_off, 4298c2ecf20Sopenharmony_ci int property) 4308c2ecf20Sopenharmony_ci{ 4318c2ecf20Sopenharmony_ci const char *value; 4328c2ecf20Sopenharmony_ci const char *label; 4338c2ecf20Sopenharmony_ci int len; 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_ci value = fdt_getprop_by_offset(fdto, property, 4368c2ecf20Sopenharmony_ci &label, &len); 4378c2ecf20Sopenharmony_ci if (!value) { 4388c2ecf20Sopenharmony_ci if (len == -FDT_ERR_NOTFOUND) 4398c2ecf20Sopenharmony_ci return -FDT_ERR_INTERNAL; 4408c2ecf20Sopenharmony_ci 4418c2ecf20Sopenharmony_ci return len; 4428c2ecf20Sopenharmony_ci } 4438c2ecf20Sopenharmony_ci 4448c2ecf20Sopenharmony_ci do { 4458c2ecf20Sopenharmony_ci const char *path, *name, *fixup_end; 4468c2ecf20Sopenharmony_ci const char *fixup_str = value; 4478c2ecf20Sopenharmony_ci uint32_t path_len, name_len; 4488c2ecf20Sopenharmony_ci uint32_t fixup_len; 4498c2ecf20Sopenharmony_ci char *sep, *endptr; 4508c2ecf20Sopenharmony_ci int poffset, ret; 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_ci fixup_end = memchr(value, '\0', len); 4538c2ecf20Sopenharmony_ci if (!fixup_end) 4548c2ecf20Sopenharmony_ci return -FDT_ERR_BADOVERLAY; 4558c2ecf20Sopenharmony_ci fixup_len = fixup_end - fixup_str; 4568c2ecf20Sopenharmony_ci 4578c2ecf20Sopenharmony_ci len -= fixup_len + 1; 4588c2ecf20Sopenharmony_ci value += fixup_len + 1; 4598c2ecf20Sopenharmony_ci 4608c2ecf20Sopenharmony_ci path = fixup_str; 4618c2ecf20Sopenharmony_ci sep = memchr(fixup_str, ':', fixup_len); 4628c2ecf20Sopenharmony_ci if (!sep || *sep != ':') 4638c2ecf20Sopenharmony_ci return -FDT_ERR_BADOVERLAY; 4648c2ecf20Sopenharmony_ci 4658c2ecf20Sopenharmony_ci path_len = sep - path; 4668c2ecf20Sopenharmony_ci if (path_len == (fixup_len - 1)) 4678c2ecf20Sopenharmony_ci return -FDT_ERR_BADOVERLAY; 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ci fixup_len -= path_len + 1; 4708c2ecf20Sopenharmony_ci name = sep + 1; 4718c2ecf20Sopenharmony_ci sep = memchr(name, ':', fixup_len); 4728c2ecf20Sopenharmony_ci if (!sep || *sep != ':') 4738c2ecf20Sopenharmony_ci return -FDT_ERR_BADOVERLAY; 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci name_len = sep - name; 4768c2ecf20Sopenharmony_ci if (!name_len) 4778c2ecf20Sopenharmony_ci return -FDT_ERR_BADOVERLAY; 4788c2ecf20Sopenharmony_ci 4798c2ecf20Sopenharmony_ci poffset = strtoul(sep + 1, &endptr, 10); 4808c2ecf20Sopenharmony_ci if ((*endptr != '\0') || (endptr <= (sep + 1))) 4818c2ecf20Sopenharmony_ci return -FDT_ERR_BADOVERLAY; 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci ret = overlay_fixup_one_phandle(fdt, fdto, symbols_off, 4848c2ecf20Sopenharmony_ci path, path_len, name, name_len, 4858c2ecf20Sopenharmony_ci poffset, label); 4868c2ecf20Sopenharmony_ci if (ret) 4878c2ecf20Sopenharmony_ci return ret; 4888c2ecf20Sopenharmony_ci } while (len > 0); 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ci return 0; 4918c2ecf20Sopenharmony_ci} 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_ci/** 4948c2ecf20Sopenharmony_ci * overlay_fixup_phandles - Resolve the overlay phandles to the base 4958c2ecf20Sopenharmony_ci * device tree 4968c2ecf20Sopenharmony_ci * @fdt: Base Device Tree blob 4978c2ecf20Sopenharmony_ci * @fdto: Device tree overlay blob 4988c2ecf20Sopenharmony_ci * 4998c2ecf20Sopenharmony_ci * overlay_fixup_phandles() resolves all the overlay phandles pointing 5008c2ecf20Sopenharmony_ci * to nodes in the base device tree. 5018c2ecf20Sopenharmony_ci * 5028c2ecf20Sopenharmony_ci * This is one of the steps of the device tree overlay application 5038c2ecf20Sopenharmony_ci * process, when you want all the phandles in the overlay to point to 5048c2ecf20Sopenharmony_ci * the actual base dt nodes. 5058c2ecf20Sopenharmony_ci * 5068c2ecf20Sopenharmony_ci * returns: 5078c2ecf20Sopenharmony_ci * 0 on success 5088c2ecf20Sopenharmony_ci * Negative error code on failure 5098c2ecf20Sopenharmony_ci */ 5108c2ecf20Sopenharmony_cistatic int overlay_fixup_phandles(void *fdt, void *fdto) 5118c2ecf20Sopenharmony_ci{ 5128c2ecf20Sopenharmony_ci int fixups_off, symbols_off; 5138c2ecf20Sopenharmony_ci int property; 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_ci /* We can have overlays without any fixups */ 5168c2ecf20Sopenharmony_ci fixups_off = fdt_path_offset(fdto, "/__fixups__"); 5178c2ecf20Sopenharmony_ci if (fixups_off == -FDT_ERR_NOTFOUND) 5188c2ecf20Sopenharmony_ci return 0; /* nothing to do */ 5198c2ecf20Sopenharmony_ci if (fixups_off < 0) 5208c2ecf20Sopenharmony_ci return fixups_off; 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci /* And base DTs without symbols */ 5238c2ecf20Sopenharmony_ci symbols_off = fdt_path_offset(fdt, "/__symbols__"); 5248c2ecf20Sopenharmony_ci if ((symbols_off < 0 && (symbols_off != -FDT_ERR_NOTFOUND))) 5258c2ecf20Sopenharmony_ci return symbols_off; 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_ci fdt_for_each_property_offset(property, fdto, fixups_off) { 5288c2ecf20Sopenharmony_ci int ret; 5298c2ecf20Sopenharmony_ci 5308c2ecf20Sopenharmony_ci ret = overlay_fixup_phandle(fdt, fdto, symbols_off, property); 5318c2ecf20Sopenharmony_ci if (ret) 5328c2ecf20Sopenharmony_ci return ret; 5338c2ecf20Sopenharmony_ci } 5348c2ecf20Sopenharmony_ci 5358c2ecf20Sopenharmony_ci return 0; 5368c2ecf20Sopenharmony_ci} 5378c2ecf20Sopenharmony_ci 5388c2ecf20Sopenharmony_ci/** 5398c2ecf20Sopenharmony_ci * overlay_apply_node - Merges a node into the base device tree 5408c2ecf20Sopenharmony_ci * @fdt: Base Device Tree blob 5418c2ecf20Sopenharmony_ci * @target: Node offset in the base device tree to apply the fragment to 5428c2ecf20Sopenharmony_ci * @fdto: Device tree overlay blob 5438c2ecf20Sopenharmony_ci * @node: Node offset in the overlay holding the changes to merge 5448c2ecf20Sopenharmony_ci * 5458c2ecf20Sopenharmony_ci * overlay_apply_node() merges a node into a target base device tree 5468c2ecf20Sopenharmony_ci * node pointed. 5478c2ecf20Sopenharmony_ci * 5488c2ecf20Sopenharmony_ci * This is part of the final step in the device tree overlay 5498c2ecf20Sopenharmony_ci * application process, when all the phandles have been adjusted and 5508c2ecf20Sopenharmony_ci * resolved and you just have to merge overlay into the base device 5518c2ecf20Sopenharmony_ci * tree. 5528c2ecf20Sopenharmony_ci * 5538c2ecf20Sopenharmony_ci * returns: 5548c2ecf20Sopenharmony_ci * 0 on success 5558c2ecf20Sopenharmony_ci * Negative error code on failure 5568c2ecf20Sopenharmony_ci */ 5578c2ecf20Sopenharmony_cistatic int overlay_apply_node(void *fdt, int target, 5588c2ecf20Sopenharmony_ci void *fdto, int node) 5598c2ecf20Sopenharmony_ci{ 5608c2ecf20Sopenharmony_ci int property; 5618c2ecf20Sopenharmony_ci int subnode; 5628c2ecf20Sopenharmony_ci 5638c2ecf20Sopenharmony_ci fdt_for_each_property_offset(property, fdto, node) { 5648c2ecf20Sopenharmony_ci const char *name; 5658c2ecf20Sopenharmony_ci const void *prop; 5668c2ecf20Sopenharmony_ci int prop_len; 5678c2ecf20Sopenharmony_ci int ret; 5688c2ecf20Sopenharmony_ci 5698c2ecf20Sopenharmony_ci prop = fdt_getprop_by_offset(fdto, property, &name, 5708c2ecf20Sopenharmony_ci &prop_len); 5718c2ecf20Sopenharmony_ci if (prop_len == -FDT_ERR_NOTFOUND) 5728c2ecf20Sopenharmony_ci return -FDT_ERR_INTERNAL; 5738c2ecf20Sopenharmony_ci if (prop_len < 0) 5748c2ecf20Sopenharmony_ci return prop_len; 5758c2ecf20Sopenharmony_ci 5768c2ecf20Sopenharmony_ci ret = fdt_setprop(fdt, target, name, prop, prop_len); 5778c2ecf20Sopenharmony_ci if (ret) 5788c2ecf20Sopenharmony_ci return ret; 5798c2ecf20Sopenharmony_ci } 5808c2ecf20Sopenharmony_ci 5818c2ecf20Sopenharmony_ci fdt_for_each_subnode(subnode, fdto, node) { 5828c2ecf20Sopenharmony_ci const char *name = fdt_get_name(fdto, subnode, NULL); 5838c2ecf20Sopenharmony_ci int nnode; 5848c2ecf20Sopenharmony_ci int ret; 5858c2ecf20Sopenharmony_ci 5868c2ecf20Sopenharmony_ci nnode = fdt_add_subnode(fdt, target, name); 5878c2ecf20Sopenharmony_ci if (nnode == -FDT_ERR_EXISTS) { 5888c2ecf20Sopenharmony_ci nnode = fdt_subnode_offset(fdt, target, name); 5898c2ecf20Sopenharmony_ci if (nnode == -FDT_ERR_NOTFOUND) 5908c2ecf20Sopenharmony_ci return -FDT_ERR_INTERNAL; 5918c2ecf20Sopenharmony_ci } 5928c2ecf20Sopenharmony_ci 5938c2ecf20Sopenharmony_ci if (nnode < 0) 5948c2ecf20Sopenharmony_ci return nnode; 5958c2ecf20Sopenharmony_ci 5968c2ecf20Sopenharmony_ci ret = overlay_apply_node(fdt, nnode, fdto, subnode); 5978c2ecf20Sopenharmony_ci if (ret) 5988c2ecf20Sopenharmony_ci return ret; 5998c2ecf20Sopenharmony_ci } 6008c2ecf20Sopenharmony_ci 6018c2ecf20Sopenharmony_ci return 0; 6028c2ecf20Sopenharmony_ci} 6038c2ecf20Sopenharmony_ci 6048c2ecf20Sopenharmony_ci/** 6058c2ecf20Sopenharmony_ci * overlay_merge - Merge an overlay into its base device tree 6068c2ecf20Sopenharmony_ci * @fdt: Base Device Tree blob 6078c2ecf20Sopenharmony_ci * @fdto: Device tree overlay blob 6088c2ecf20Sopenharmony_ci * 6098c2ecf20Sopenharmony_ci * overlay_merge() merges an overlay into its base device tree. 6108c2ecf20Sopenharmony_ci * 6118c2ecf20Sopenharmony_ci * This is the next to last step in the device tree overlay application 6128c2ecf20Sopenharmony_ci * process, when all the phandles have been adjusted and resolved and 6138c2ecf20Sopenharmony_ci * you just have to merge overlay into the base device tree. 6148c2ecf20Sopenharmony_ci * 6158c2ecf20Sopenharmony_ci * returns: 6168c2ecf20Sopenharmony_ci * 0 on success 6178c2ecf20Sopenharmony_ci * Negative error code on failure 6188c2ecf20Sopenharmony_ci */ 6198c2ecf20Sopenharmony_cistatic int overlay_merge(void *fdt, void *fdto) 6208c2ecf20Sopenharmony_ci{ 6218c2ecf20Sopenharmony_ci int fragment; 6228c2ecf20Sopenharmony_ci 6238c2ecf20Sopenharmony_ci fdt_for_each_subnode(fragment, fdto, 0) { 6248c2ecf20Sopenharmony_ci int overlay; 6258c2ecf20Sopenharmony_ci int target; 6268c2ecf20Sopenharmony_ci int ret; 6278c2ecf20Sopenharmony_ci 6288c2ecf20Sopenharmony_ci /* 6298c2ecf20Sopenharmony_ci * Each fragments will have an __overlay__ node. If 6308c2ecf20Sopenharmony_ci * they don't, it's not supposed to be merged 6318c2ecf20Sopenharmony_ci */ 6328c2ecf20Sopenharmony_ci overlay = fdt_subnode_offset(fdto, fragment, "__overlay__"); 6338c2ecf20Sopenharmony_ci if (overlay == -FDT_ERR_NOTFOUND) 6348c2ecf20Sopenharmony_ci continue; 6358c2ecf20Sopenharmony_ci 6368c2ecf20Sopenharmony_ci if (overlay < 0) 6378c2ecf20Sopenharmony_ci return overlay; 6388c2ecf20Sopenharmony_ci 6398c2ecf20Sopenharmony_ci target = overlay_get_target(fdt, fdto, fragment, NULL); 6408c2ecf20Sopenharmony_ci if (target < 0) 6418c2ecf20Sopenharmony_ci return target; 6428c2ecf20Sopenharmony_ci 6438c2ecf20Sopenharmony_ci ret = overlay_apply_node(fdt, target, fdto, overlay); 6448c2ecf20Sopenharmony_ci if (ret) 6458c2ecf20Sopenharmony_ci return ret; 6468c2ecf20Sopenharmony_ci } 6478c2ecf20Sopenharmony_ci 6488c2ecf20Sopenharmony_ci return 0; 6498c2ecf20Sopenharmony_ci} 6508c2ecf20Sopenharmony_ci 6518c2ecf20Sopenharmony_cistatic int get_path_len(const void *fdt, int nodeoffset) 6528c2ecf20Sopenharmony_ci{ 6538c2ecf20Sopenharmony_ci int len = 0, namelen; 6548c2ecf20Sopenharmony_ci const char *name; 6558c2ecf20Sopenharmony_ci 6568c2ecf20Sopenharmony_ci FDT_RO_PROBE(fdt); 6578c2ecf20Sopenharmony_ci 6588c2ecf20Sopenharmony_ci for (;;) { 6598c2ecf20Sopenharmony_ci name = fdt_get_name(fdt, nodeoffset, &namelen); 6608c2ecf20Sopenharmony_ci if (!name) 6618c2ecf20Sopenharmony_ci return namelen; 6628c2ecf20Sopenharmony_ci 6638c2ecf20Sopenharmony_ci /* root? we're done */ 6648c2ecf20Sopenharmony_ci if (namelen == 0) 6658c2ecf20Sopenharmony_ci break; 6668c2ecf20Sopenharmony_ci 6678c2ecf20Sopenharmony_ci nodeoffset = fdt_parent_offset(fdt, nodeoffset); 6688c2ecf20Sopenharmony_ci if (nodeoffset < 0) 6698c2ecf20Sopenharmony_ci return nodeoffset; 6708c2ecf20Sopenharmony_ci len += namelen + 1; 6718c2ecf20Sopenharmony_ci } 6728c2ecf20Sopenharmony_ci 6738c2ecf20Sopenharmony_ci /* in case of root pretend it's "/" */ 6748c2ecf20Sopenharmony_ci if (len == 0) 6758c2ecf20Sopenharmony_ci len++; 6768c2ecf20Sopenharmony_ci return len; 6778c2ecf20Sopenharmony_ci} 6788c2ecf20Sopenharmony_ci 6798c2ecf20Sopenharmony_ci/** 6808c2ecf20Sopenharmony_ci * overlay_symbol_update - Update the symbols of base tree after a merge 6818c2ecf20Sopenharmony_ci * @fdt: Base Device Tree blob 6828c2ecf20Sopenharmony_ci * @fdto: Device tree overlay blob 6838c2ecf20Sopenharmony_ci * 6848c2ecf20Sopenharmony_ci * overlay_symbol_update() updates the symbols of the base tree with the 6858c2ecf20Sopenharmony_ci * symbols of the applied overlay 6868c2ecf20Sopenharmony_ci * 6878c2ecf20Sopenharmony_ci * This is the last step in the device tree overlay application 6888c2ecf20Sopenharmony_ci * process, allowing the reference of overlay symbols by subsequent 6898c2ecf20Sopenharmony_ci * overlay operations. 6908c2ecf20Sopenharmony_ci * 6918c2ecf20Sopenharmony_ci * returns: 6928c2ecf20Sopenharmony_ci * 0 on success 6938c2ecf20Sopenharmony_ci * Negative error code on failure 6948c2ecf20Sopenharmony_ci */ 6958c2ecf20Sopenharmony_cistatic int overlay_symbol_update(void *fdt, void *fdto) 6968c2ecf20Sopenharmony_ci{ 6978c2ecf20Sopenharmony_ci int root_sym, ov_sym, prop, path_len, fragment, target; 6988c2ecf20Sopenharmony_ci int len, frag_name_len, ret, rel_path_len; 6998c2ecf20Sopenharmony_ci const char *s, *e; 7008c2ecf20Sopenharmony_ci const char *path; 7018c2ecf20Sopenharmony_ci const char *name; 7028c2ecf20Sopenharmony_ci const char *frag_name; 7038c2ecf20Sopenharmony_ci const char *rel_path; 7048c2ecf20Sopenharmony_ci const char *target_path; 7058c2ecf20Sopenharmony_ci char *buf; 7068c2ecf20Sopenharmony_ci void *p; 7078c2ecf20Sopenharmony_ci 7088c2ecf20Sopenharmony_ci ov_sym = fdt_subnode_offset(fdto, 0, "__symbols__"); 7098c2ecf20Sopenharmony_ci 7108c2ecf20Sopenharmony_ci /* if no overlay symbols exist no problem */ 7118c2ecf20Sopenharmony_ci if (ov_sym < 0) 7128c2ecf20Sopenharmony_ci return 0; 7138c2ecf20Sopenharmony_ci 7148c2ecf20Sopenharmony_ci root_sym = fdt_subnode_offset(fdt, 0, "__symbols__"); 7158c2ecf20Sopenharmony_ci 7168c2ecf20Sopenharmony_ci /* it no root symbols exist we should create them */ 7178c2ecf20Sopenharmony_ci if (root_sym == -FDT_ERR_NOTFOUND) 7188c2ecf20Sopenharmony_ci root_sym = fdt_add_subnode(fdt, 0, "__symbols__"); 7198c2ecf20Sopenharmony_ci 7208c2ecf20Sopenharmony_ci /* any error is fatal now */ 7218c2ecf20Sopenharmony_ci if (root_sym < 0) 7228c2ecf20Sopenharmony_ci return root_sym; 7238c2ecf20Sopenharmony_ci 7248c2ecf20Sopenharmony_ci /* iterate over each overlay symbol */ 7258c2ecf20Sopenharmony_ci fdt_for_each_property_offset(prop, fdto, ov_sym) { 7268c2ecf20Sopenharmony_ci path = fdt_getprop_by_offset(fdto, prop, &name, &path_len); 7278c2ecf20Sopenharmony_ci if (!path) 7288c2ecf20Sopenharmony_ci return path_len; 7298c2ecf20Sopenharmony_ci 7308c2ecf20Sopenharmony_ci /* verify it's a string property (terminated by a single \0) */ 7318c2ecf20Sopenharmony_ci if (path_len < 1 || memchr(path, '\0', path_len) != &path[path_len - 1]) 7328c2ecf20Sopenharmony_ci return -FDT_ERR_BADVALUE; 7338c2ecf20Sopenharmony_ci 7348c2ecf20Sopenharmony_ci /* keep end marker to avoid strlen() */ 7358c2ecf20Sopenharmony_ci e = path + path_len; 7368c2ecf20Sopenharmony_ci 7378c2ecf20Sopenharmony_ci if (*path != '/') 7388c2ecf20Sopenharmony_ci return -FDT_ERR_BADVALUE; 7398c2ecf20Sopenharmony_ci 7408c2ecf20Sopenharmony_ci /* get fragment name first */ 7418c2ecf20Sopenharmony_ci s = strchr(path + 1, '/'); 7428c2ecf20Sopenharmony_ci if (!s) { 7438c2ecf20Sopenharmony_ci /* Symbol refers to something that won't end 7448c2ecf20Sopenharmony_ci * up in the target tree */ 7458c2ecf20Sopenharmony_ci continue; 7468c2ecf20Sopenharmony_ci } 7478c2ecf20Sopenharmony_ci 7488c2ecf20Sopenharmony_ci frag_name = path + 1; 7498c2ecf20Sopenharmony_ci frag_name_len = s - path - 1; 7508c2ecf20Sopenharmony_ci 7518c2ecf20Sopenharmony_ci /* verify format; safe since "s" lies in \0 terminated prop */ 7528c2ecf20Sopenharmony_ci len = sizeof("/__overlay__/") - 1; 7538c2ecf20Sopenharmony_ci if ((e - s) > len && (memcmp(s, "/__overlay__/", len) == 0)) { 7548c2ecf20Sopenharmony_ci /* /<fragment-name>/__overlay__/<relative-subnode-path> */ 7558c2ecf20Sopenharmony_ci rel_path = s + len; 7568c2ecf20Sopenharmony_ci rel_path_len = e - rel_path - 1; 7578c2ecf20Sopenharmony_ci } else if ((e - s) == len 7588c2ecf20Sopenharmony_ci && (memcmp(s, "/__overlay__", len - 1) == 0)) { 7598c2ecf20Sopenharmony_ci /* /<fragment-name>/__overlay__ */ 7608c2ecf20Sopenharmony_ci rel_path = ""; 7618c2ecf20Sopenharmony_ci rel_path_len = 0; 7628c2ecf20Sopenharmony_ci } else { 7638c2ecf20Sopenharmony_ci /* Symbol refers to something that won't end 7648c2ecf20Sopenharmony_ci * up in the target tree */ 7658c2ecf20Sopenharmony_ci continue; 7668c2ecf20Sopenharmony_ci } 7678c2ecf20Sopenharmony_ci 7688c2ecf20Sopenharmony_ci /* find the fragment index in which the symbol lies */ 7698c2ecf20Sopenharmony_ci ret = fdt_subnode_offset_namelen(fdto, 0, frag_name, 7708c2ecf20Sopenharmony_ci frag_name_len); 7718c2ecf20Sopenharmony_ci /* not found? */ 7728c2ecf20Sopenharmony_ci if (ret < 0) 7738c2ecf20Sopenharmony_ci return -FDT_ERR_BADOVERLAY; 7748c2ecf20Sopenharmony_ci fragment = ret; 7758c2ecf20Sopenharmony_ci 7768c2ecf20Sopenharmony_ci /* an __overlay__ subnode must exist */ 7778c2ecf20Sopenharmony_ci ret = fdt_subnode_offset(fdto, fragment, "__overlay__"); 7788c2ecf20Sopenharmony_ci if (ret < 0) 7798c2ecf20Sopenharmony_ci return -FDT_ERR_BADOVERLAY; 7808c2ecf20Sopenharmony_ci 7818c2ecf20Sopenharmony_ci /* get the target of the fragment */ 7828c2ecf20Sopenharmony_ci ret = overlay_get_target(fdt, fdto, fragment, &target_path); 7838c2ecf20Sopenharmony_ci if (ret < 0) 7848c2ecf20Sopenharmony_ci return ret; 7858c2ecf20Sopenharmony_ci target = ret; 7868c2ecf20Sopenharmony_ci 7878c2ecf20Sopenharmony_ci /* if we have a target path use */ 7888c2ecf20Sopenharmony_ci if (!target_path) { 7898c2ecf20Sopenharmony_ci ret = get_path_len(fdt, target); 7908c2ecf20Sopenharmony_ci if (ret < 0) 7918c2ecf20Sopenharmony_ci return ret; 7928c2ecf20Sopenharmony_ci len = ret; 7938c2ecf20Sopenharmony_ci } else { 7948c2ecf20Sopenharmony_ci len = strlen(target_path); 7958c2ecf20Sopenharmony_ci } 7968c2ecf20Sopenharmony_ci 7978c2ecf20Sopenharmony_ci ret = fdt_setprop_placeholder(fdt, root_sym, name, 7988c2ecf20Sopenharmony_ci len + (len > 1) + rel_path_len + 1, &p); 7998c2ecf20Sopenharmony_ci if (ret < 0) 8008c2ecf20Sopenharmony_ci return ret; 8018c2ecf20Sopenharmony_ci 8028c2ecf20Sopenharmony_ci if (!target_path) { 8038c2ecf20Sopenharmony_ci /* again in case setprop_placeholder changed it */ 8048c2ecf20Sopenharmony_ci ret = overlay_get_target(fdt, fdto, fragment, &target_path); 8058c2ecf20Sopenharmony_ci if (ret < 0) 8068c2ecf20Sopenharmony_ci return ret; 8078c2ecf20Sopenharmony_ci target = ret; 8088c2ecf20Sopenharmony_ci } 8098c2ecf20Sopenharmony_ci 8108c2ecf20Sopenharmony_ci buf = p; 8118c2ecf20Sopenharmony_ci if (len > 1) { /* target is not root */ 8128c2ecf20Sopenharmony_ci if (!target_path) { 8138c2ecf20Sopenharmony_ci ret = fdt_get_path(fdt, target, buf, len + 1); 8148c2ecf20Sopenharmony_ci if (ret < 0) 8158c2ecf20Sopenharmony_ci return ret; 8168c2ecf20Sopenharmony_ci } else 8178c2ecf20Sopenharmony_ci memcpy(buf, target_path, len + 1); 8188c2ecf20Sopenharmony_ci 8198c2ecf20Sopenharmony_ci } else 8208c2ecf20Sopenharmony_ci len--; 8218c2ecf20Sopenharmony_ci 8228c2ecf20Sopenharmony_ci buf[len] = '/'; 8238c2ecf20Sopenharmony_ci memcpy(buf + len + 1, rel_path, rel_path_len); 8248c2ecf20Sopenharmony_ci buf[len + 1 + rel_path_len] = '\0'; 8258c2ecf20Sopenharmony_ci } 8268c2ecf20Sopenharmony_ci 8278c2ecf20Sopenharmony_ci return 0; 8288c2ecf20Sopenharmony_ci} 8298c2ecf20Sopenharmony_ci 8308c2ecf20Sopenharmony_ciint fdt_overlay_apply(void *fdt, void *fdto) 8318c2ecf20Sopenharmony_ci{ 8328c2ecf20Sopenharmony_ci uint32_t delta; 8338c2ecf20Sopenharmony_ci int ret; 8348c2ecf20Sopenharmony_ci 8358c2ecf20Sopenharmony_ci FDT_RO_PROBE(fdt); 8368c2ecf20Sopenharmony_ci FDT_RO_PROBE(fdto); 8378c2ecf20Sopenharmony_ci 8388c2ecf20Sopenharmony_ci ret = fdt_find_max_phandle(fdt, &delta); 8398c2ecf20Sopenharmony_ci if (ret) 8408c2ecf20Sopenharmony_ci goto err; 8418c2ecf20Sopenharmony_ci 8428c2ecf20Sopenharmony_ci ret = overlay_adjust_local_phandles(fdto, delta); 8438c2ecf20Sopenharmony_ci if (ret) 8448c2ecf20Sopenharmony_ci goto err; 8458c2ecf20Sopenharmony_ci 8468c2ecf20Sopenharmony_ci ret = overlay_update_local_references(fdto, delta); 8478c2ecf20Sopenharmony_ci if (ret) 8488c2ecf20Sopenharmony_ci goto err; 8498c2ecf20Sopenharmony_ci 8508c2ecf20Sopenharmony_ci ret = overlay_fixup_phandles(fdt, fdto); 8518c2ecf20Sopenharmony_ci if (ret) 8528c2ecf20Sopenharmony_ci goto err; 8538c2ecf20Sopenharmony_ci 8548c2ecf20Sopenharmony_ci ret = overlay_merge(fdt, fdto); 8558c2ecf20Sopenharmony_ci if (ret) 8568c2ecf20Sopenharmony_ci goto err; 8578c2ecf20Sopenharmony_ci 8588c2ecf20Sopenharmony_ci ret = overlay_symbol_update(fdt, fdto); 8598c2ecf20Sopenharmony_ci if (ret) 8608c2ecf20Sopenharmony_ci goto err; 8618c2ecf20Sopenharmony_ci 8628c2ecf20Sopenharmony_ci /* 8638c2ecf20Sopenharmony_ci * The overlay has been damaged, erase its magic. 8648c2ecf20Sopenharmony_ci */ 8658c2ecf20Sopenharmony_ci fdt_set_magic(fdto, ~0); 8668c2ecf20Sopenharmony_ci 8678c2ecf20Sopenharmony_ci return 0; 8688c2ecf20Sopenharmony_ci 8698c2ecf20Sopenharmony_cierr: 8708c2ecf20Sopenharmony_ci /* 8718c2ecf20Sopenharmony_ci * The overlay might have been damaged, erase its magic. 8728c2ecf20Sopenharmony_ci */ 8738c2ecf20Sopenharmony_ci fdt_set_magic(fdto, ~0); 8748c2ecf20Sopenharmony_ci 8758c2ecf20Sopenharmony_ci /* 8768c2ecf20Sopenharmony_ci * The base device tree might have been damaged, erase its 8778c2ecf20Sopenharmony_ci * magic. 8788c2ecf20Sopenharmony_ci */ 8798c2ecf20Sopenharmony_ci fdt_set_magic(fdt, ~0); 8808c2ecf20Sopenharmony_ci 8818c2ecf20Sopenharmony_ci return ret; 8828c2ecf20Sopenharmony_ci} 883