From: Frieder Schrempf <[email protected]>

bootflow_rauc() points std->bootdev_order at a static array and only
restores the original order at its end. If an assertion fails, the
test returns early with the static array still in place, and removing
bootstd then hands it to free(). This trips the dlmalloc checks and
aborts sandbox, so the failure of the flat-tree run is never reported
and no further test runs.

Move the setup into bootflow_rauc_run(), which restores the order
after running the checks, whether they pass or not.

Fixes: 36add050eea4 ("test: boot: Add test for bootmeth_rauc")
Assisted-by: LLM
Signed-off-by: Frieder Schrempf <[email protected]>
---
 test/boot/bootflow.c | 48 +++++++++++++++++++++++++++++++++++-------------
 1 file changed, 35 insertions(+), 13 deletions(-)

diff --git a/test/boot/bootflow.c b/test/boot/bootflow.c
index d478fd575be..d2d4edb0add 100644
--- a/test/boot/bootflow.c
+++ b/test/boot/bootflow.c
@@ -1619,23 +1619,31 @@ static int bootflow_efi(struct unit_test_state *uts)
 }
 BOOTSTD_TEST(bootflow_efi, UTF_CONSOLE);
 
-/* Test RAUC bootmeth */
-static int bootflow_rauc(struct unit_test_state *uts)
+/**
+ * bootflow_rauc_run() - Run RAUC bootmeth checks on an mmc bootdev
+ *
+ * This binds @mmc_dev and the RAUC bootmeth and makes them the only bootdev
+ * and bootmeth used, then runs @check. The bootdev order is restored even if
+ * @check fails, since bootstd frees it when it is removed.
+ *
+ * @uts: Unit test state
+ * @mmc_dev: MMC device to use, e.g. "mmc10"
+ * @check: Checks to run with the RAUC bootmeth
+ * Returns 0 on success, -ve on failure
+ */
+static int bootflow_rauc_run(struct unit_test_state *uts, const char *mmc_dev,
+                            int (*check)(struct unit_test_state *uts))
 {
-       const char *mmc_dev = "mmc10";
+       static const char *order[] = {NULL, NULL};
        struct bootstd_priv *std;
        struct udevice *bootstd;
-       static const char *order[] = {NULL, NULL};
        const char **old_order;
-       ulong mem_start;
        ofnode root;
        ofnode node;
+       int ret;
 
        order[0] = mmc_dev;
 
-       if (!CONFIG_IS_ENABLED(BOOTMETH_RAUC))
-               return -EAGAIN;
-
        /* Enable the requested mmc node since we need a different bootflow */
        root = oftree_root(oftree_default());
        node = ofnode_find_subnode(root, mmc_dev);
@@ -1646,13 +1654,21 @@ static int bootflow_rauc(struct unit_test_state *uts)
        ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
        ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_rauc),
                                "rauc", 0, ofnode_null(), NULL));
+       ut_assertok(bootmeth_set_order("rauc"));
 
-       /* Change the device and bootmeth order */
+       /* Change the device order, and restore it also if a check fails */
        std = dev_get_priv(bootstd);
        old_order = std->bootdev_order;
        std->bootdev_order = order;
+       ret = check(uts);
+       std->bootdev_order = old_order;
 
-       ut_assertok(bootmeth_set_order("rauc"));
+       return ret;
+}
+
+static int bootflow_rauc_check(struct unit_test_state *uts)
+{
+       ulong mem_start;
 
        /* Run scan and list */
        ut_assertok(run_command("bootflow scan", 0));
@@ -1705,11 +1721,17 @@ static int bootflow_rauc(struct unit_test_state *uts)
 
        ut_assertok(env_set("BOOT_ORDER", "A B"));
 
-       /* Restore the order used by the device tree */
-       std->bootdev_order = old_order;
-
        return 0;
 }
+
+/* Test RAUC bootmeth */
+static int bootflow_rauc(struct unit_test_state *uts)
+{
+       if (!CONFIG_IS_ENABLED(BOOTMETH_RAUC))
+               return -EAGAIN;
+
+       return bootflow_rauc_run(uts, "mmc10", bootflow_rauc_check);
+}
 BOOTSTD_TEST(bootflow_rauc, UTF_CONSOLE | UTF_DM | UTF_SCAN_FDT);
 
 /*

-- 
2.56.0

Reply via email to