When a boot script exits with a non-zero status, execute_binfmt()
returns that status as a positive value. bootscript_boot() passes it to
strerror(-ret) and boot_entry() to %pe, both of which expect a negative
errno, resulting in messages like:

  ERROR: Running script '/env/boot/mmc' failed: error -1
  ERROR: Booting entry 'mmc' failed: 0000000000000001

Allow boot entries to return a boot script's exit status and have
boot_entry() report it as such. bootscript_boot() now only reports
errors of its own, so a failing script results in a single

  ERROR: Booting entry 'mmc' failed: exit status 1

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Sascha Hauer <[email protected]>
---
 common/boot.c               | 11 +++++++----
 include/boot.h              |  1 +
 test/py/test_boot_script.py | 13 +++++++++++++
 3 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/common/boot.c b/common/boot.c
index 023eaf5145..b993cabf65 100644
--- a/common/boot.c
+++ b/common/boot.c
@@ -115,10 +115,11 @@ static int bootscript_boot(struct bootentry *entry, int 
verbose, int dryrun)
         * without the shell expanding or splitting it first.
         */
        ret = execute_binfmt(bs->args ? 2 : 1, argv);
-       if (ret) {
-               pr_err("Running script '%s' failed: %s\n", bs->entry.path, 
strerror(-ret));
+       if (ret < 0)
+               pr_err("Running script '%s' failed: %pe\n", bs->entry.path, 
ERR_PTR(ret));
+       /* positive values are the exit status, reported by boot_entry() */
+       if (ret)
                return ret;
-       }
 
        /* No point in repeating bootm if script already called it */
        if (bootm_nattempts != bootm_command_attempts())
@@ -206,7 +207,9 @@ int boot_entry(struct bootentry *be, int verbose, int 
dryrun)
        scope = boot_globalvar_scope_push();
 
        ret = be->boot(be, verbose, dryrun);
-       if (ret && ret != -ENOMEDIUM)
+       if (ret > 0)
+               pr_err("Booting entry '%s' failed: exit status %d\n", 
be->title, ret);
+       else if (ret && ret != -ENOMEDIUM)
                pr_err("Booting entry '%s' failed: %pe\n", be->title, 
ERR_PTR(ret));
 
        globalvar_scope_pop(scope);
diff --git a/include/boot.h b/include/boot.h
index b30cdd314a..bb99dacc85 100644
--- a/include/boot.h
+++ b/include/boot.h
@@ -20,6 +20,7 @@ struct bootentry {
        const char *title;
        char *description;
        const char *path;
+       /* 0 on success, else negative errno or a boot script's exit status */
        int (*boot)(struct bootentry *entry, int verbose, int dryrun);
        void (*release)(struct bootentry *entry);
        struct bootm_overrides overrides;
diff --git a/test/py/test_boot_script.py b/test/py/test_boot_script.py
new file mode 100644
index 0000000000..be08e964d9
--- /dev/null
+++ b/test/py/test_boot_script.py
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+
+def test_boot_script_exit_status(barebox):
+    barebox.run_check("echo -o /env/boot/exit3 '#!/bin/sh'")
+    barebox.run_check("echo -a /env/boot/exit3 'exit 3'")
+
+    stdout, _, ret = barebox.run("boot exit3")
+    assert ret != 0
+    assert "ERROR: Booting entry 'exit3' failed: exit status 3" in stdout
+    assert not any("Running script" in line for line in stdout)
+
+    barebox.run_check("rm /env/boot/exit3")

-- 
2.47.3


Reply via email to