]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
plat/drivers/ofw: Fix search in `fdt_node_offset_by_compatible_list`
authorEduard Vintilă <eduard.vintila47@gmail.com>
Thu, 16 Mar 2023 12:13:06 +0000 (14:13 +0200)
committerUnikraft <monkey@unikraft.io>
Mon, 8 May 2023 06:32:28 +0000 (06:32 +0000)
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ă <eduard.vintila47@gmail.com>
Reviewed-by: Robert Kuban <robert.kuban@opensynergy.com>
Reviewed-by: Razvan Virtan <virtanrazvan@gmail.com>
Approved-by: Michalis Pappas <michalis@unikraft.io>
Tested-by: Unikraft CI <monkey@unikraft.io>
GitHub-Closes: #805

plat/drivers/ofw/fdt.c

index 2c6735bc0eba44900129b2d299350e34e833447c..2c90ccc0b93f54bc7e8352a400bd02c941348eea 100644 (file)
@@ -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;
 }