commonlib/device_tree: Initialize cells to default values on find()

This patch wraps `dt_find_node()` in a function that initializes the
addr_cells and size_cells values to the defaults provided in the FDT
specification before potentially updating them from found values, so
that we always return the correct result and remove the burden of
correctly initializing them from the caller.

Change-Id: I39ba2c82d3a0d0b39a2ed5eba2420a04fbccb2f7
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/85988
Reviewed-by: Doug Anderson <dianders@chromium.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Maximilian Brune <maximilian.brune@9elements.com>
This commit is contained in:
Julius Werner 2025-01-14 13:51:05 -08:00
commit 5266191670

View file

@ -1039,23 +1039,9 @@ void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
}
}
/*
* Find a node from a device tree path, relative to a parent node.
*
* @param parent The node from which to start the relative path lookup.
* @param path An array of path component strings that will be looked
* up in order to find the node. Must be terminated with
* a NULL pointer. Example: {'firmware', 'coreboot', NULL}
* @param addrcp Pointer that will be updated with any #address-cells
* value found in the path. May be NULL to ignore.
* @param sizecp Pointer that will be updated with any #size-cells
* value found in the path. May be NULL to ignore.
* @param create 1: Create node(s) if not found. 0: Return NULL instead.
* @return The found/created node, or NULL.
*/
struct device_tree_node *dt_find_node(struct device_tree_node *parent,
const char **path, u32 *addrcp,
u32 *sizecp, int create)
static struct device_tree_node *_dt_find_node(struct device_tree_node *parent,
const char **path, u32 *addrcp,
u32 *sizecp, int create)
{
struct device_tree_node *node, *found = NULL;
@ -1090,6 +1076,34 @@ struct device_tree_node *dt_find_node(struct device_tree_node *parent,
return dt_find_node(found, path + 1, addrcp, sizecp, create);
}
/*
* Find a node from a device tree path, relative to a parent node.
*
* @param parent The node from which to start the relative path lookup.
* @param path An array of path component strings that will be looked
* up in order to find the node. Must be terminated with
* a NULL pointer. Example: {'firmware', 'coreboot', NULL}
* @param addrcp Pointer that will be updated with any #address-cells
* value found in the path. May be NULL to ignore.
* @param sizecp Pointer that will be updated with any #size-cells
* value found in the path. May be NULL to ignore.
* @param create 1: Create node(s) if not found. 0: Return NULL instead.
* @return The found/created node, or NULL.
*/
struct device_tree_node *dt_find_node(struct device_tree_node *parent,
const char **path, u32 *addrcp,
u32 *sizecp, int create)
{
/* Initialize cells to default values according to FDT spec. */
if (addrcp)
*addrcp = 2;
if (sizecp)
*sizecp = 1;
return _dt_find_node(parent, path, addrcp, sizecp, create);
}
/*
* Find a node in the tree from a string device tree path.
*