From: Eduard Vintilă Date: Thu, 16 Mar 2023 12:13:06 +0000 (+0200) Subject: plat/drivers/ofw: Fix search in `fdt_node_offset_by_compatible_list` X-Git-Tag: RELEASE-0.13.0~48 X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=195cc32f7e5e65d8929ae65b8fa9203ddf246ba6;p=unikraft%2Funikraft.git plat/drivers/ofw: Fix search in `fdt_node_offset_by_compatible_list` The current implementation returns as soon as a node that matches a compatible string is found. However, we should iterate through all of the compatible list to make sure that we return the node with the least offset value such that future searches will not skip any matching nodes. Signed-off-by: Eduard Vintilă Reviewed-by: Robert Kuban Reviewed-by: Razvan Virtan Approved-by: Michalis Pappas Tested-by: Unikraft CI GitHub-Closes: #805 --- diff --git a/plat/drivers/ofw/fdt.c b/plat/drivers/ofw/fdt.c index 2c6735bc0..2c90ccc0b 100644 --- a/plat/drivers/ofw/fdt.c +++ b/plat/drivers/ofw/fdt.c @@ -300,33 +300,40 @@ int fdt_get_address(const void *fdt, int nodeoffset, uint32_t index, int fdt_node_offset_by_compatible_list(const void *fdt, int startoffset, const char * const compatibles[]) { - int idx, offset; + int idx, min_offset = INT_MAX, offset; for (idx = 0; compatibles[idx] != NULL; idx++) { offset = fdt_node_offset_by_compatible(fdt, startoffset, compatibles[idx]); - if (offset >= 0) - return offset; + if (offset >= 0 && offset < min_offset) + min_offset = offset; } + if (min_offset != INT_MAX) + return min_offset; + return -FDT_ERR_NOTFOUND; } int fdt_node_offset_idx_by_compatible_list(const void *fdt, int startoffset, const char * const compatibles[], int *index) { - int idx, offset; + int idx, min_offset = INT_MAX, offset, compatible_idx; for (idx = 0; compatibles[idx] != NULL; idx++) { offset = fdt_node_offset_by_compatible(fdt, startoffset, compatibles[idx]); - if (offset >= 0) { - *index = idx; - return offset; + if (offset >= 0 && offset < min_offset) { + min_offset = offset; + compatible_idx = idx; } } - *index = idx; + if (min_offset != INT_MAX) { + *index = compatible_idx; + return min_offset; + } + return -FDT_ERR_NOTFOUND; }