2016-07-05 08:26:44 +00:00
|
|
|
#include "libfdt_env.h"
|
|
|
|
|
|
|
|
#include <fdt.h>
|
|
|
|
#include <libfdt.h>
|
|
|
|
|
|
|
|
#include "libfdt_internal.h"
|
|
|
|
|
2016-07-05 08:26:46 +00:00
|
|
|
/**
|
|
|
|
* overlay_get_target_phandle - retrieves the target phandle of a fragment
|
|
|
|
* @fdto: pointer to the device tree overlay blob
|
|
|
|
* @fragment: node offset of the fragment in the overlay
|
|
|
|
*
|
|
|
|
* overlay_get_target_phandle() retrieves the target phandle of an
|
|
|
|
* overlay fragment when that fragment uses a phandle (target
|
|
|
|
* property) instead of a path (target-path property).
|
|
|
|
*
|
|
|
|
* returns:
|
|
|
|
* the phandle pointed by the target property
|
|
|
|
* 0, if the phandle was not found
|
|
|
|
* -1, if the phandle was malformed
|
|
|
|
*/
|
2016-07-05 08:26:44 +00:00
|
|
|
static uint32_t overlay_get_target_phandle(const void *fdto, int fragment)
|
|
|
|
{
|
2017-03-28 16:23:31 +00:00
|
|
|
const fdt32_t *val;
|
2016-07-05 08:26:44 +00:00
|
|
|
int len;
|
|
|
|
|
|
|
|
val = fdt_getprop(fdto, fragment, "target", &len);
|
|
|
|
if (!val)
|
|
|
|
return 0;
|
|
|
|
|
2017-03-28 16:23:31 +00:00
|
|
|
if ((len != sizeof(*val)) || (fdt32_to_cpu(*val) == (uint32_t)-1))
|
2016-07-05 08:26:46 +00:00
|
|
|
return (uint32_t)-1;
|
2016-07-05 08:26:44 +00:00
|
|
|
|
|
|
|
return fdt32_to_cpu(*val);
|
|
|
|
}
|
|
|
|
|
2016-07-05 08:26:46 +00:00
|
|
|
/**
|
2016-10-17 20:50:18 +00:00
|
|
|
* overlay_get_target - retrieves the offset of a fragment's target
|
2016-07-05 08:26:46 +00:00
|
|
|
* @fdt: Base device tree blob
|
|
|
|
* @fdto: Device tree overlay blob
|
|
|
|
* @fragment: node offset of the fragment in the overlay
|
|
|
|
*
|
2016-10-17 20:50:18 +00:00
|
|
|
* overlay_get_target() retrieves the target offset in the base
|
2016-07-05 08:26:46 +00:00
|
|
|
* device tree of a fragment, no matter how the actual targetting is
|
|
|
|
* done (through a phandle or a path)
|
|
|
|
*
|
|
|
|
* returns:
|
|
|
|
* the targetted node offset in the base device tree
|
|
|
|
* Negative error code on error
|
|
|
|
*/
|
2016-07-05 08:26:44 +00:00
|
|
|
static int overlay_get_target(const void *fdt, const void *fdto,
|
|
|
|
int fragment)
|
|
|
|
{
|
|
|
|
uint32_t phandle;
|
|
|
|
const char *path;
|
2016-10-17 20:50:18 +00:00
|
|
|
int path_len;
|
2016-07-05 08:26:44 +00:00
|
|
|
|
|
|
|
/* Try first to do a phandle based lookup */
|
|
|
|
phandle = overlay_get_target_phandle(fdto, fragment);
|
2016-07-05 08:26:46 +00:00
|
|
|
if (phandle == (uint32_t)-1)
|
2016-07-05 08:26:44 +00:00
|
|
|
return -FDT_ERR_BADPHANDLE;
|
|
|
|
|
|
|
|
if (phandle)
|
|
|
|
return fdt_node_offset_by_phandle(fdt, phandle);
|
|
|
|
|
|
|
|
/* And then a path based lookup */
|
2016-10-17 20:50:18 +00:00
|
|
|
path = fdt_getprop(fdto, fragment, "target-path", &path_len);
|
|
|
|
if (!path) {
|
|
|
|
/*
|
|
|
|
* If we haven't found either a target or a
|
|
|
|
* target-path property in a node that contains a
|
|
|
|
* __overlay__ subnode (we wouldn't be called
|
|
|
|
* otherwise), consider it a improperly written
|
|
|
|
* overlay
|
|
|
|
*/
|
|
|
|
if (path_len == -FDT_ERR_NOTFOUND)
|
|
|
|
return -FDT_ERR_BADOVERLAY;
|
|
|
|
|
|
|
|
return path_len;
|
|
|
|
}
|
2016-07-05 08:26:44 +00:00
|
|
|
|
|
|
|
return fdt_path_offset(fdt, path);
|
|
|
|
}
|
|
|
|
|
2016-07-05 08:26:46 +00:00
|
|
|
/**
|
|
|
|
* overlay_phandle_add_offset - Increases a phandle by an offset
|
|
|
|
* @fdt: Base device tree blob
|
|
|
|
* @node: Device tree overlay blob
|
|
|
|
* @name: Name of the property to modify (phandle or linux,phandle)
|
|
|
|
* @delta: offset to apply
|
|
|
|
*
|
|
|
|
* overlay_phandle_add_offset() increments a node phandle by a given
|
|
|
|
* offset.
|
|
|
|
*
|
|
|
|
* returns:
|
|
|
|
* 0 on success.
|
|
|
|
* Negative error code on error
|
|
|
|
*/
|
2016-07-05 08:26:44 +00:00
|
|
|
static int overlay_phandle_add_offset(void *fdt, int node,
|
|
|
|
const char *name, uint32_t delta)
|
|
|
|
{
|
2017-03-28 16:23:31 +00:00
|
|
|
const fdt32_t *val;
|
2016-07-05 08:26:44 +00:00
|
|
|
uint32_t adj_val;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
val = fdt_getprop(fdt, node, name, &len);
|
|
|
|
if (!val)
|
|
|
|
return len;
|
|
|
|
|
|
|
|
if (len != sizeof(*val))
|
2016-10-17 20:50:18 +00:00
|
|
|
return -FDT_ERR_BADPHANDLE;
|
2016-07-05 08:26:44 +00:00
|
|
|
|
|
|
|
adj_val = fdt32_to_cpu(*val);
|
|
|
|
if ((adj_val + delta) < adj_val)
|
2016-10-17 20:50:18 +00:00
|
|
|
return -FDT_ERR_NOPHANDLES;
|
2016-07-05 08:26:44 +00:00
|
|
|
|
|
|
|
adj_val += delta;
|
2016-10-17 20:50:18 +00:00
|
|
|
if (adj_val == (uint32_t)-1)
|
|
|
|
return -FDT_ERR_NOPHANDLES;
|
|
|
|
|
2016-07-05 08:26:44 +00:00
|
|
|
return fdt_setprop_inplace_u32(fdt, node, name, adj_val);
|
|
|
|
}
|
|
|
|
|
2016-07-05 08:26:46 +00:00
|
|
|
/**
|
|
|
|
* overlay_adjust_node_phandles - Offsets the phandles of a node
|
|
|
|
* @fdto: Device tree overlay blob
|
|
|
|
* @node: Offset of the node we want to adjust
|
|
|
|
* @delta: Offset to shift the phandles of
|
|
|
|
*
|
|
|
|
* overlay_adjust_node_phandles() adds a constant to all the phandles
|
|
|
|
* of a given node. This is mainly use as part of the overlay
|
|
|
|
* application process, when we want to update all the overlay
|
|
|
|
* phandles to not conflict with the overlays of the base device tree.
|
|
|
|
*
|
|
|
|
* returns:
|
|
|
|
* 0 on success
|
|
|
|
* Negative error code on failure
|
|
|
|
*/
|
2016-07-05 08:26:44 +00:00
|
|
|
static int overlay_adjust_node_phandles(void *fdto, int node,
|
|
|
|
uint32_t delta)
|
|
|
|
{
|
|
|
|
int child;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = overlay_phandle_add_offset(fdto, node, "phandle", delta);
|
|
|
|
if (ret && ret != -FDT_ERR_NOTFOUND)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = overlay_phandle_add_offset(fdto, node, "linux,phandle", delta);
|
|
|
|
if (ret && ret != -FDT_ERR_NOTFOUND)
|
|
|
|
return ret;
|
|
|
|
|
2016-10-17 20:50:18 +00:00
|
|
|
fdt_for_each_subnode(child, fdto, node) {
|
|
|
|
ret = overlay_adjust_node_phandles(fdto, child, delta);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2016-07-05 08:26:44 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-05 08:26:46 +00:00
|
|
|
/**
|
|
|
|
* overlay_adjust_local_phandles - Adjust the phandles of a whole overlay
|
|
|
|
* @fdto: Device tree overlay blob
|
|
|
|
* @delta: Offset to shift the phandles of
|
|
|
|
*
|
|
|
|
* overlay_adjust_local_phandles() adds a constant to all the
|
|
|
|
* phandles of an overlay. This is mainly use as part of the overlay
|
|
|
|
* application process, when we want to update all the overlay
|
|
|
|
* phandles to not conflict with the overlays of the base device tree.
|
|
|
|
*
|
|
|
|
* returns:
|
|
|
|
* 0 on success
|
|
|
|
* Negative error code on failure
|
|
|
|
*/
|
2016-07-05 08:26:44 +00:00
|
|
|
static int overlay_adjust_local_phandles(void *fdto, uint32_t delta)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Start adjusting the phandles from the overlay root
|
|
|
|
*/
|
|
|
|
return overlay_adjust_node_phandles(fdto, 0, delta);
|
|
|
|
}
|
|
|
|
|
2016-07-05 08:26:46 +00:00
|
|
|
/**
|
|
|
|
* overlay_update_local_node_references - Adjust the overlay references
|
|
|
|
* @fdto: Device tree overlay blob
|
|
|
|
* @tree_node: Node offset of the node to operate on
|
|
|
|
* @fixup_node: Node offset of the matching local fixups node
|
|
|
|
* @delta: Offset to shift the phandles of
|
|
|
|
*
|
|
|
|
* overlay_update_local_nodes_references() update the phandles
|
|
|
|
* pointing to a node within the device tree overlay by adding a
|
|
|
|
* constant delta.
|
|
|
|
*
|
|
|
|
* This is mainly used as part of a device tree application process,
|
|
|
|
* where you want the device tree overlays phandles to not conflict
|
|
|
|
* with the ones from the base device tree before merging them.
|
|
|
|
*
|
|
|
|
* returns:
|
|
|
|
* 0 on success
|
|
|
|
* Negative error code on failure
|
|
|
|
*/
|
2016-07-05 08:26:44 +00:00
|
|
|
static int overlay_update_local_node_references(void *fdto,
|
|
|
|
int tree_node,
|
|
|
|
int fixup_node,
|
|
|
|
uint32_t delta)
|
|
|
|
{
|
|
|
|
int fixup_prop;
|
|
|
|
int fixup_child;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
fdt_for_each_property_offset(fixup_prop, fdto, fixup_node) {
|
2017-03-28 16:23:31 +00:00
|
|
|
const fdt32_t *fixup_val;
|
2016-10-17 20:50:18 +00:00
|
|
|
const char *tree_val;
|
2016-07-05 08:26:44 +00:00
|
|
|
const char *name;
|
|
|
|
int fixup_len;
|
|
|
|
int tree_len;
|
2016-07-05 08:26:46 +00:00
|
|
|
int i;
|
2016-07-05 08:26:44 +00:00
|
|
|
|
2016-07-05 08:26:46 +00:00
|
|
|
fixup_val = fdt_getprop_by_offset(fdto, fixup_prop,
|
|
|
|
&name, &fixup_len);
|
|
|
|
if (!fixup_val)
|
2016-07-05 08:26:44 +00:00
|
|
|
return fixup_len;
|
|
|
|
|
2016-10-17 20:50:18 +00:00
|
|
|
if (fixup_len % sizeof(uint32_t))
|
|
|
|
return -FDT_ERR_BADOVERLAY;
|
|
|
|
|
2016-07-05 08:26:46 +00:00
|
|
|
tree_val = fdt_getprop(fdto, tree_node, name, &tree_len);
|
2016-10-17 20:50:18 +00:00
|
|
|
if (!tree_val) {
|
|
|
|
if (tree_len == -FDT_ERR_NOTFOUND)
|
|
|
|
return -FDT_ERR_BADOVERLAY;
|
|
|
|
|
2016-07-05 08:26:44 +00:00
|
|
|
return tree_len;
|
2016-10-17 20:50:18 +00:00
|
|
|
}
|
2016-07-05 08:26:44 +00:00
|
|
|
|
2016-10-17 20:50:18 +00:00
|
|
|
for (i = 0; i < (fixup_len / sizeof(uint32_t)); i++) {
|
2017-03-28 16:23:31 +00:00
|
|
|
fdt32_t adj_val;
|
|
|
|
uint32_t poffset;
|
2016-07-05 08:26:46 +00:00
|
|
|
|
2016-10-17 20:50:18 +00:00
|
|
|
poffset = fdt32_to_cpu(fixup_val[i]);
|
2016-07-05 08:26:46 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* phandles to fixup can be unaligned.
|
|
|
|
*
|
|
|
|
* Use a memcpy for the architectures that do
|
|
|
|
* not support unaligned accesses.
|
|
|
|
*/
|
2016-10-17 20:50:18 +00:00
|
|
|
memcpy(&adj_val, tree_val + poffset, sizeof(adj_val));
|
2016-07-05 08:26:46 +00:00
|
|
|
|
2017-03-28 16:23:31 +00:00
|
|
|
adj_val = cpu_to_fdt32(fdt32_to_cpu(adj_val) + delta);
|
2016-07-05 08:26:46 +00:00
|
|
|
|
|
|
|
ret = fdt_setprop_inplace_namelen_partial(fdto,
|
|
|
|
tree_node,
|
|
|
|
name,
|
|
|
|
strlen(name),
|
2016-10-17 20:50:18 +00:00
|
|
|
poffset,
|
2016-07-05 08:26:46 +00:00
|
|
|
&adj_val,
|
|
|
|
sizeof(adj_val));
|
2016-10-17 20:50:18 +00:00
|
|
|
if (ret == -FDT_ERR_NOSPACE)
|
|
|
|
return -FDT_ERR_BADOVERLAY;
|
|
|
|
|
2016-07-05 08:26:46 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2016-07-05 08:26:44 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 23:59:29 +00:00
|
|
|
fdt_for_each_subnode(fixup_child, fdto, fixup_node) {
|
2016-07-05 08:26:44 +00:00
|
|
|
const char *fixup_child_name = fdt_get_name(fdto, fixup_child,
|
|
|
|
NULL);
|
|
|
|
int tree_child;
|
|
|
|
|
|
|
|
tree_child = fdt_subnode_offset(fdto, tree_node,
|
|
|
|
fixup_child_name);
|
2017-03-28 16:23:31 +00:00
|
|
|
if (tree_child == -FDT_ERR_NOTFOUND)
|
2016-10-17 20:50:18 +00:00
|
|
|
return -FDT_ERR_BADOVERLAY;
|
2016-07-05 08:26:44 +00:00
|
|
|
if (tree_child < 0)
|
|
|
|
return tree_child;
|
|
|
|
|
|
|
|
ret = overlay_update_local_node_references(fdto,
|
|
|
|
tree_child,
|
|
|
|
fixup_child,
|
|
|
|
delta);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-05 08:26:46 +00:00
|
|
|
/**
|
|
|
|
* overlay_update_local_references - Adjust the overlay references
|
|
|
|
* @fdto: Device tree overlay blob
|
|
|
|
* @delta: Offset to shift the phandles of
|
|
|
|
*
|
|
|
|
* overlay_update_local_references() update all the phandles pointing
|
|
|
|
* to a node within the device tree overlay by adding a constant
|
|
|
|
* delta to not conflict with the base overlay.
|
|
|
|
*
|
|
|
|
* This is mainly used as part of a device tree application process,
|
|
|
|
* where you want the device tree overlays phandles to not conflict
|
|
|
|
* with the ones from the base device tree before merging them.
|
|
|
|
*
|
|
|
|
* returns:
|
|
|
|
* 0 on success
|
|
|
|
* Negative error code on failure
|
|
|
|
*/
|
|
|
|
static int overlay_update_local_references(void *fdto, uint32_t delta)
|
2016-07-05 08:26:44 +00:00
|
|
|
{
|
|
|
|
int fixups;
|
|
|
|
|
2016-07-05 08:26:46 +00:00
|
|
|
fixups = fdt_path_offset(fdto, "/__local_fixups__");
|
2016-07-05 08:26:44 +00:00
|
|
|
if (fixups < 0) {
|
|
|
|
/* There's no local phandles to adjust, bail out */
|
|
|
|
if (fixups == -FDT_ERR_NOTFOUND)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return fixups;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update our local references from the root of the tree
|
|
|
|
*/
|
2016-07-05 08:26:46 +00:00
|
|
|
return overlay_update_local_node_references(fdto, 0, fixups,
|
2016-07-05 08:26:44 +00:00
|
|
|
delta);
|
|
|
|
}
|
|
|
|
|
2016-07-05 08:26:46 +00:00
|
|
|
/**
|
|
|
|
* overlay_fixup_one_phandle - Set an overlay phandle to the base one
|
|
|
|
* @fdt: Base Device Tree blob
|
|
|
|
* @fdto: Device tree overlay blob
|
|
|
|
* @symbols_off: Node offset of the symbols node in the base device tree
|
|
|
|
* @path: Path to a node holding a phandle in the overlay
|
|
|
|
* @path_len: number of path characters to consider
|
|
|
|
* @name: Name of the property holding the phandle reference in the overlay
|
|
|
|
* @name_len: number of name characters to consider
|
2016-10-17 20:50:18 +00:00
|
|
|
* @poffset: Offset within the overlay property where the phandle is stored
|
2016-07-05 08:26:46 +00:00
|
|
|
* @label: Label of the node referenced by the phandle
|
|
|
|
*
|
|
|
|
* overlay_fixup_one_phandle() resolves an overlay phandle pointing to
|
|
|
|
* a node in the base device tree.
|
|
|
|
*
|
|
|
|
* This is part of the device tree overlay application process, when
|
|
|
|
* you want all the phandles in the overlay to point to the actual
|
|
|
|
* base dt nodes.
|
|
|
|
*
|
|
|
|
* returns:
|
|
|
|
* 0 on success
|
|
|
|
* Negative error code on failure
|
|
|
|
*/
|
2016-07-05 08:26:44 +00:00
|
|
|
static int overlay_fixup_one_phandle(void *fdt, void *fdto,
|
|
|
|
int symbols_off,
|
|
|
|
const char *path, uint32_t path_len,
|
|
|
|
const char *name, uint32_t name_len,
|
2016-10-17 20:50:18 +00:00
|
|
|
int poffset, const char *label)
|
2016-07-05 08:26:44 +00:00
|
|
|
{
|
|
|
|
const char *symbol_path;
|
|
|
|
uint32_t phandle;
|
2017-03-28 16:23:31 +00:00
|
|
|
fdt32_t phandle_prop;
|
2016-07-05 08:26:44 +00:00
|
|
|
int symbol_off, fixup_off;
|
|
|
|
int prop_len;
|
|
|
|
|
2016-12-20 14:58:44 +00:00
|
|
|
if (symbols_off < 0)
|
|
|
|
return symbols_off;
|
|
|
|
|
2016-07-05 08:26:44 +00:00
|
|
|
symbol_path = fdt_getprop(fdt, symbols_off, label,
|
|
|
|
&prop_len);
|
|
|
|
if (!symbol_path)
|
2016-10-17 20:50:18 +00:00
|
|
|
return prop_len;
|
2016-07-05 08:26:44 +00:00
|
|
|
|
|
|
|
symbol_off = fdt_path_offset(fdt, symbol_path);
|
|
|
|
if (symbol_off < 0)
|
|
|
|
return symbol_off;
|
|
|
|
|
|
|
|
phandle = fdt_get_phandle(fdt, symbol_off);
|
|
|
|
if (!phandle)
|
|
|
|
return -FDT_ERR_NOTFOUND;
|
|
|
|
|
|
|
|
fixup_off = fdt_path_offset_namelen(fdto, path, path_len);
|
2016-10-17 20:50:18 +00:00
|
|
|
if (fixup_off == -FDT_ERR_NOTFOUND)
|
|
|
|
return -FDT_ERR_BADOVERLAY;
|
2016-07-05 08:26:44 +00:00
|
|
|
if (fixup_off < 0)
|
|
|
|
return fixup_off;
|
|
|
|
|
2017-03-28 16:23:31 +00:00
|
|
|
phandle_prop = cpu_to_fdt32(phandle);
|
2016-07-05 08:26:45 +00:00
|
|
|
return fdt_setprop_inplace_namelen_partial(fdto, fixup_off,
|
2016-10-17 20:50:18 +00:00
|
|
|
name, name_len, poffset,
|
2017-03-28 16:23:31 +00:00
|
|
|
&phandle_prop,
|
|
|
|
sizeof(phandle_prop));
|
2016-07-05 08:26:44 +00:00
|
|
|
};
|
|
|
|
|
2016-07-05 08:26:46 +00:00
|
|
|
/**
|
|
|
|
* overlay_fixup_phandle - Set an overlay phandle to the base one
|
|
|
|
* @fdt: Base Device Tree blob
|
|
|
|
* @fdto: Device tree overlay blob
|
|
|
|
* @symbols_off: Node offset of the symbols node in the base device tree
|
|
|
|
* @property: Property offset in the overlay holding the list of fixups
|
|
|
|
*
|
|
|
|
* overlay_fixup_phandle() resolves all the overlay phandles pointed
|
2016-10-17 20:50:18 +00:00
|
|
|
* to in a __fixups__ property, and updates them to match the phandles
|
|
|
|
* in use in the base device tree.
|
2016-07-05 08:26:46 +00:00
|
|
|
*
|
|
|
|
* This is part of the device tree overlay application process, when
|
|
|
|
* you want all the phandles in the overlay to point to the actual
|
|
|
|
* base dt nodes.
|
|
|
|
*
|
|
|
|
* returns:
|
|
|
|
* 0 on success
|
|
|
|
* Negative error code on failure
|
|
|
|
*/
|
2016-07-05 08:26:44 +00:00
|
|
|
static int overlay_fixup_phandle(void *fdt, void *fdto, int symbols_off,
|
|
|
|
int property)
|
|
|
|
{
|
|
|
|
const char *value;
|
|
|
|
const char *label;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
value = fdt_getprop_by_offset(fdto, property,
|
|
|
|
&label, &len);
|
2016-10-17 20:50:18 +00:00
|
|
|
if (!value) {
|
|
|
|
if (len == -FDT_ERR_NOTFOUND)
|
|
|
|
return -FDT_ERR_INTERNAL;
|
|
|
|
|
2016-07-05 08:26:44 +00:00
|
|
|
return len;
|
2016-10-17 20:50:18 +00:00
|
|
|
}
|
2016-07-05 08:26:44 +00:00
|
|
|
|
|
|
|
do {
|
2016-10-17 20:50:18 +00:00
|
|
|
const char *path, *name, *fixup_end;
|
|
|
|
const char *fixup_str = value;
|
2016-07-05 08:26:44 +00:00
|
|
|
uint32_t path_len, name_len;
|
2016-10-17 20:50:18 +00:00
|
|
|
uint32_t fixup_len;
|
2016-07-05 08:26:44 +00:00
|
|
|
char *sep, *endptr;
|
2016-10-17 20:50:18 +00:00
|
|
|
int poffset, ret;
|
|
|
|
|
|
|
|
fixup_end = memchr(value, '\0', len);
|
|
|
|
if (!fixup_end)
|
|
|
|
return -FDT_ERR_BADOVERLAY;
|
|
|
|
fixup_len = fixup_end - fixup_str;
|
|
|
|
|
|
|
|
len -= fixup_len + 1;
|
|
|
|
value += fixup_len + 1;
|
|
|
|
|
|
|
|
path = fixup_str;
|
|
|
|
sep = memchr(fixup_str, ':', fixup_len);
|
|
|
|
if (!sep || *sep != ':')
|
|
|
|
return -FDT_ERR_BADOVERLAY;
|
2016-07-05 08:26:44 +00:00
|
|
|
|
|
|
|
path_len = sep - path;
|
2016-10-17 20:50:18 +00:00
|
|
|
if (path_len == (fixup_len - 1))
|
|
|
|
return -FDT_ERR_BADOVERLAY;
|
2016-07-05 08:26:44 +00:00
|
|
|
|
2016-10-17 20:50:18 +00:00
|
|
|
fixup_len -= path_len + 1;
|
2016-07-05 08:26:44 +00:00
|
|
|
name = sep + 1;
|
2016-10-17 20:50:18 +00:00
|
|
|
sep = memchr(name, ':', fixup_len);
|
|
|
|
if (!sep || *sep != ':')
|
|
|
|
return -FDT_ERR_BADOVERLAY;
|
|
|
|
|
2016-07-05 08:26:44 +00:00
|
|
|
name_len = sep - name;
|
2016-10-17 20:50:18 +00:00
|
|
|
if (!name_len)
|
|
|
|
return -FDT_ERR_BADOVERLAY;
|
2016-07-05 08:26:44 +00:00
|
|
|
|
2016-10-17 20:50:18 +00:00
|
|
|
poffset = strtoul(sep + 1, &endptr, 10);
|
2016-07-05 08:26:44 +00:00
|
|
|
if ((*endptr != '\0') || (endptr <= (sep + 1)))
|
2016-10-17 20:50:18 +00:00
|
|
|
return -FDT_ERR_BADOVERLAY;
|
2016-07-05 08:26:44 +00:00
|
|
|
|
|
|
|
ret = overlay_fixup_one_phandle(fdt, fdto, symbols_off,
|
|
|
|
path, path_len, name, name_len,
|
2016-10-17 20:50:18 +00:00
|
|
|
poffset, label);
|
2016-07-05 08:26:44 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
} while (len > 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-05 08:26:46 +00:00
|
|
|
/**
|
|
|
|
* overlay_fixup_phandles - Resolve the overlay phandles to the base
|
|
|
|
* device tree
|
|
|
|
* @fdt: Base Device Tree blob
|
|
|
|
* @fdto: Device tree overlay blob
|
|
|
|
*
|
|
|
|
* overlay_fixup_phandles() resolves all the overlay phandles pointing
|
|
|
|
* to nodes in the base device tree.
|
|
|
|
*
|
|
|
|
* This is one of the steps of the device tree overlay application
|
|
|
|
* process, when you want all the phandles in the overlay to point to
|
|
|
|
* the actual base dt nodes.
|
|
|
|
*
|
|
|
|
* returns:
|
|
|
|
* 0 on success
|
|
|
|
* Negative error code on failure
|
|
|
|
*/
|
|
|
|
static int overlay_fixup_phandles(void *fdt, void *fdto)
|
2016-07-05 08:26:44 +00:00
|
|
|
{
|
|
|
|
int fixups_off, symbols_off;
|
|
|
|
int property;
|
|
|
|
|
2016-10-17 20:50:18 +00:00
|
|
|
/* We can have overlays without any fixups */
|
2016-07-05 08:26:46 +00:00
|
|
|
fixups_off = fdt_path_offset(fdto, "/__fixups__");
|
2016-12-20 14:58:44 +00:00
|
|
|
if (fixups_off == -FDT_ERR_NOTFOUND)
|
|
|
|
return 0; /* nothing to do */
|
|
|
|
if (fixups_off < 0)
|
2016-10-17 20:50:18 +00:00
|
|
|
return fixups_off;
|
|
|
|
|
|
|
|
/* And base DTs without symbols */
|
|
|
|
symbols_off = fdt_path_offset(fdt, "/__symbols__");
|
|
|
|
if ((symbols_off < 0 && (symbols_off != -FDT_ERR_NOTFOUND)))
|
|
|
|
return symbols_off;
|
2016-07-05 08:26:46 +00:00
|
|
|
|
|
|
|
fdt_for_each_property_offset(property, fdto, fixups_off) {
|
|
|
|
int ret;
|
2016-07-05 08:26:44 +00:00
|
|
|
|
2016-07-05 08:26:46 +00:00
|
|
|
ret = overlay_fixup_phandle(fdt, fdto, symbols_off, property);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2016-07-05 08:26:44 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-05 08:26:46 +00:00
|
|
|
/**
|
2016-10-17 20:50:18 +00:00
|
|
|
* overlay_apply_node - Merges a node into the base device tree
|
2016-07-05 08:26:46 +00:00
|
|
|
* @fdt: Base Device Tree blob
|
|
|
|
* @target: Node offset in the base device tree to apply the fragment to
|
|
|
|
* @fdto: Device tree overlay blob
|
2016-10-17 20:50:18 +00:00
|
|
|
* @node: Node offset in the overlay holding the changes to merge
|
2016-07-05 08:26:46 +00:00
|
|
|
*
|
2016-10-17 20:50:18 +00:00
|
|
|
* overlay_apply_node() merges a node into a target base device tree
|
|
|
|
* node pointed.
|
2016-07-05 08:26:46 +00:00
|
|
|
*
|
|
|
|
* This is part of the final step in the device tree overlay
|
|
|
|
* application process, when all the phandles have been adjusted and
|
|
|
|
* resolved and you just have to merge overlay into the base device
|
|
|
|
* tree.
|
|
|
|
*
|
|
|
|
* returns:
|
|
|
|
* 0 on success
|
|
|
|
* Negative error code on failure
|
|
|
|
*/
|
|
|
|
static int overlay_apply_node(void *fdt, int target,
|
2016-10-17 20:50:18 +00:00
|
|
|
void *fdto, int node)
|
2016-07-05 08:26:44 +00:00
|
|
|
{
|
|
|
|
int property;
|
2016-10-17 20:50:18 +00:00
|
|
|
int subnode;
|
2016-07-05 08:26:44 +00:00
|
|
|
|
2016-10-17 20:50:18 +00:00
|
|
|
fdt_for_each_property_offset(property, fdto, node) {
|
2016-07-05 08:26:44 +00:00
|
|
|
const char *name;
|
|
|
|
const void *prop;
|
|
|
|
int prop_len;
|
|
|
|
int ret;
|
|
|
|
|
2016-07-05 08:26:46 +00:00
|
|
|
prop = fdt_getprop_by_offset(fdto, property, &name,
|
2016-07-05 08:26:44 +00:00
|
|
|
&prop_len);
|
|
|
|
if (prop_len == -FDT_ERR_NOTFOUND)
|
|
|
|
return -FDT_ERR_INTERNAL;
|
|
|
|
if (prop_len < 0)
|
|
|
|
return prop_len;
|
|
|
|
|
2016-07-05 08:26:46 +00:00
|
|
|
ret = fdt_setprop(fdt, target, name, prop, prop_len);
|
2016-07-05 08:26:44 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-10-17 20:50:18 +00:00
|
|
|
fdt_for_each_subnode(subnode, fdto, node) {
|
|
|
|
const char *name = fdt_get_name(fdto, subnode, NULL);
|
2016-07-05 08:26:44 +00:00
|
|
|
int nnode;
|
|
|
|
int ret;
|
|
|
|
|
2016-07-05 08:26:46 +00:00
|
|
|
nnode = fdt_add_subnode(fdt, target, name);
|
2016-10-17 20:50:18 +00:00
|
|
|
if (nnode == -FDT_ERR_EXISTS) {
|
2016-07-05 08:26:46 +00:00
|
|
|
nnode = fdt_subnode_offset(fdt, target, name);
|
2016-10-17 20:50:18 +00:00
|
|
|
if (nnode == -FDT_ERR_NOTFOUND)
|
|
|
|
return -FDT_ERR_INTERNAL;
|
|
|
|
}
|
2016-07-05 08:26:44 +00:00
|
|
|
|
|
|
|
if (nnode < 0)
|
|
|
|
return nnode;
|
|
|
|
|
2016-10-17 20:50:18 +00:00
|
|
|
ret = overlay_apply_node(fdt, nnode, fdto, subnode);
|
2016-07-05 08:26:44 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-05 08:26:46 +00:00
|
|
|
/**
|
|
|
|
* overlay_merge - Merge an overlay into its base device tree
|
|
|
|
* @fdt: Base Device Tree blob
|
|
|
|
* @fdto: Device tree overlay blob
|
|
|
|
*
|
|
|
|
* overlay_merge() merges an overlay into its base device tree.
|
|
|
|
*
|
|
|
|
* This is the final step in the device tree overlay application
|
|
|
|
* process, when all the phandles have been adjusted and resolved and
|
|
|
|
* you just have to merge overlay into the base device tree.
|
|
|
|
*
|
|
|
|
* returns:
|
|
|
|
* 0 on success
|
|
|
|
* Negative error code on failure
|
|
|
|
*/
|
2016-10-17 20:50:18 +00:00
|
|
|
static int overlay_merge(void *fdt, void *fdto)
|
2016-07-05 08:26:44 +00:00
|
|
|
{
|
|
|
|
int fragment;
|
|
|
|
|
2016-10-17 20:50:18 +00:00
|
|
|
fdt_for_each_subnode(fragment, fdto, 0) {
|
2016-07-05 08:26:44 +00:00
|
|
|
int overlay;
|
|
|
|
int target;
|
|
|
|
int ret;
|
|
|
|
|
2016-10-17 20:50:18 +00:00
|
|
|
/*
|
|
|
|
* Each fragments will have an __overlay__ node. If
|
|
|
|
* they don't, it's not supposed to be merged
|
|
|
|
*/
|
|
|
|
overlay = fdt_subnode_offset(fdto, fragment, "__overlay__");
|
|
|
|
if (overlay == -FDT_ERR_NOTFOUND)
|
2016-07-05 08:26:44 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (overlay < 0)
|
|
|
|
return overlay;
|
|
|
|
|
2016-10-17 20:50:18 +00:00
|
|
|
target = overlay_get_target(fdt, fdto, fragment);
|
|
|
|
if (target < 0)
|
|
|
|
return target;
|
|
|
|
|
|
|
|
ret = overlay_apply_node(fdt, target, fdto, overlay);
|
2016-07-05 08:26:44 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fdt_overlay_apply(void *fdt, void *fdto)
|
|
|
|
{
|
2016-10-17 20:50:18 +00:00
|
|
|
uint32_t delta = fdt_get_max_phandle(fdt);
|
2016-07-05 08:26:44 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
FDT_CHECK_HEADER(fdt);
|
|
|
|
FDT_CHECK_HEADER(fdto);
|
|
|
|
|
|
|
|
ret = overlay_adjust_local_phandles(fdto, delta);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret = overlay_update_local_references(fdto, delta);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret = overlay_fixup_phandles(fdt, fdto);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret = overlay_merge(fdt, fdto);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The overlay has been damaged, erase its magic.
|
|
|
|
*/
|
|
|
|
fdt_set_magic(fdto, ~0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
/*
|
|
|
|
* The overlay might have been damaged, erase its magic.
|
|
|
|
*/
|
|
|
|
fdt_set_magic(fdto, ~0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The base device tree might have been damaged, erase its
|
|
|
|
* magic.
|
|
|
|
*/
|
|
|
|
fdt_set_magic(fdt, ~0);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|