From 5266191670eefbe6a936bf4bd071defbdd8d0613 Mon Sep 17 00:00:00 2001 From: Julius Werner Date: Tue, 14 Jan 2025 13:51:05 -0800 Subject: [PATCH] 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 Reviewed-on: https://review.coreboot.org/c/coreboot/+/85988 Reviewed-by: Doug Anderson Tested-by: build bot (Jenkins) Reviewed-by: Maximilian Brune --- src/commonlib/device_tree.c | 48 ++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/src/commonlib/device_tree.c b/src/commonlib/device_tree.c index c73e258ccb..2ee03162f1 100644 --- a/src/commonlib/device_tree.c +++ b/src/commonlib/device_tree.c @@ -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. *