If a boot script calls bootm and it returns whether due to error or dry
run, bootscript_boot() will just call bootm again leading to confusing
output.

Let's detect whether bootm was called and skip calling it again in that
case. This will allow boot scripts to do cleanup in future.

Signed-off-by: Ahmad Fatoum <[email protected]>
---
 commands/bootm.c  | 9 +++++++++
 common/boot.c     | 7 +++++++
 include/command.h | 9 +++++++++
 3 files changed, 25 insertions(+)

diff --git a/commands/bootm.c b/commands/bootm.c
index 3936623b5199..24bce5ce6b64 100644
--- a/commands/bootm.c
+++ b/commands/bootm.c
@@ -36,6 +36,13 @@
 #define BOOTM_OPTS BOOTM_OPTS_COMMON
 #endif
 
+static unsigned command_attempts;
+
+unsigned bootm_command_attempts(void)
+{
+       return command_attempts;
+}
+
 static int do_bootm(int argc, char *argv[])
 {
        int opt;
@@ -44,6 +51,8 @@ static int do_bootm(int argc, char *argv[])
 
        bootm_data_init_defaults(&data);
 
+       command_attempts++;
+
        while ((opt = getopt(argc, argv, BOOTM_OPTS)) > 0) {
                switch(opt) {
                case 'c':
diff --git a/common/boot.c b/common/boot.c
index af07d218f059..78d990f1617e 100644
--- a/common/boot.c
+++ b/common/boot.c
@@ -75,6 +75,7 @@ struct bootentry_script {
 static int bootscript_boot(struct bootentry *entry, int verbose, int dryrun)
 {
        struct bootentry_script *bs = container_of(entry, struct 
bootentry_script, entry);
+       int bootm_nattempts;
        int ret;
 
        struct bootm_data backup = {}, data = {};
@@ -89,12 +90,18 @@ static int bootscript_boot(struct bootentry *entry, int 
verbose, int dryrun)
        globalvar_add_simple("linux.bootargs.dyn.ip", NULL);
        globalvar_add_simple("linux.bootargs.dyn.root", NULL);
 
+       bootm_nattempts = bootm_command_attempts();
+
        ret = run_command(bs->scriptpath);
        if (ret) {
                pr_err("Running script '%s' failed: %pe\n", bs->scriptpath, 
ERR_PTR(ret));
                goto out;
        }
 
+       /* No point in repeating bootm if script already called it */
+       if (bootm_nattempts != bootm_command_attempts())
+               goto out;
+
        bootm_data_init_defaults(&data);
 
        if (verbose)
diff --git a/include/command.h b/include/command.h
index 378e1458a589..fb140cb8e250 100644
--- a/include/command.h
+++ b/include/command.h
@@ -122,4 +122,13 @@ static const __maybe_unused char cmd_##_name##_help[] =
 
 int register_command(struct command *);
 
+#ifdef CONFIG_CMD_BOOTM
+unsigned bootm_command_attempts(void);
+#else
+static inline unsigned bootm_command_attempts(void)
+{
+       return 0;
+}
+#endif
+
 #endif /* __COMMAND_H */
-- 
2.47.3


Reply via email to