summaryrefslogtreecommitdiffstats
path: root/common/startup.c
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2019-07-03 13:48:25 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2019-07-03 16:42:18 +0200
commit35266d7e583f1bcc519f710f9f455127a14f31e8 (patch)
tree651608c918b8efbcca1ff3110133a55851696a02 /common/startup.c
parent815189dd4c8be39b9caae6c06a75a994f1956140 (diff)
downloadbarebox-35266d7e583f1bcc519f710f9f455127a14f31e8.tar.gz
startup: Factor out the autoboot counter to separate function
The autoboot countdown is part of the init function. This patch factors out this code to a separate function to allow boards to call it at a different time. Also boards can set the autoboot state manually in case they have its own idea how to interrupt the autoboot process. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'common/startup.c')
-rw-r--r--common/startup.c134
1 files changed, 88 insertions, 46 deletions
diff --git a/common/startup.c b/common/startup.c
index b88c4a0..92bf94f 100644
--- a/common/startup.c
+++ b/common/startup.c
@@ -182,20 +182,95 @@ static bool test_abort(void)
}
#define INITFILE "/env/bin/init"
+#define MENUFILE "/env/menu/mainmenu"
+
+static enum autoboot_state autoboot_state = AUTOBOOT_UNKNOWN;
+
+/**
+ * set_autoboot_state - set the autoboot state
+ * @autoboot: the state to set
+ *
+ * This functions sets the autoboot state to the given state. This can be called
+ * by boards which have its own idea when and how the autoboot shall be aborted.
+ */
+void set_autoboot_state(enum autoboot_state autoboot)
+{
+ autoboot_state = autoboot;
+}
+
+/**
+ * do_autoboot_countdown - print autoboot countdown to console
+ *
+ * This prints the autoboot countdown to the console and waits for input. This
+ * evaluates the global.autoboot_about_key to determine which keys are allowed
+ * to interrupt booting and also global.autoboot_timeout to determine the timeout
+ * for the counter. This function can be called multiple times, it is executed
+ * only the first time.
+ *
+ * Returns an enum autoboot_state value containing the user input.
+ */
+enum autoboot_state do_autoboot_countdown(void)
+{
+ unsigned flags = CONSOLE_COUNTDOWN_EXTERN;
+ int ret;
+ struct stat s;
+ bool menu_exists;
+ char *abortkeys = NULL;
+ unsigned char outkey;
+
+ if (autoboot_state != AUTOBOOT_UNKNOWN)
+ return autoboot_state;
+
+ globalvar_add_simple_enum("autoboot_abort_key",
+ &global_autoboot_abort_key,
+ global_autoboot_abort_keys,
+ ARRAY_SIZE(global_autoboot_abort_keys));
+ globalvar_add_simple_int("autoboot_timeout",
+ &global_autoboot_timeout, "%u");
+
+ menu_exists = stat(MENUFILE, &s) == 0;
+
+ if (menu_exists) {
+ printf("\nHit m for menu or %s to stop autoboot: ",
+ global_autoboot_abort_keys[global_autoboot_abort_key]);
+ abortkeys = "m";
+ } else {
+ printf("\nHit %s to stop autoboot: ",
+ global_autoboot_abort_keys[global_autoboot_abort_key]);
+ }
+
+ switch (global_autoboot_abort_key) {
+ case 0:
+ flags |= CONSOLE_COUNTDOWN_ANYKEY;
+ break;
+ case 1:
+ flags |= CONSOLE_COUNTDOWN_CTRLC;
+ break;
+ default:
+ break;
+ }
+
+ ret = console_countdown(global_autoboot_timeout, flags, abortkeys,
+ &outkey);
+
+ if (ret == 0)
+ autoboot_state = AUTOBOOT_BOOT;
+ else if (menu_exists && outkey == 'm')
+ autoboot_state = AUTOBOOT_MENU;
+ else
+ autoboot_state = AUTOBOOT_ABORT;
+
+ return autoboot_state;
+}
static int run_init(void)
{
DIR *dir;
struct dirent *d;
const char *initdir = "/env/init";
- const char *menufile = "/env/menu/mainmenu";
- struct stat s;
- unsigned flags = CONSOLE_COUNTDOWN_EXTERN;
- unsigned char outkey;
- int ret;
- bool menu_exists;
bool env_bin_init_exists;
- char *abortkeys = NULL;
+ enum autoboot_state autoboot;
+ struct stat s;
setenv("PATH", "/env/bin");
@@ -207,13 +282,6 @@ static int run_init(void)
return 0;
}
- globalvar_add_simple_enum("autoboot_abort_key",
- &global_autoboot_abort_key,
- global_autoboot_abort_keys,
- ARRAY_SIZE(global_autoboot_abort_keys));
- globalvar_add_simple_int("autoboot_timeout",
- &global_autoboot_timeout, "%u");
-
/* Unblank console cursor */
printf("\e[?25h");
@@ -240,44 +308,18 @@ static int run_init(void)
closedir(dir);
}
- menu_exists = stat(menufile, &s) == 0;
-
- if (menu_exists) {
- printf("\nHit m for menu or %s to stop autoboot: ",
- global_autoboot_abort_keys[global_autoboot_abort_key]);
- abortkeys = "m";
- } else {
- printf("\nHit %s to stop autoboot: ",
- global_autoboot_abort_keys[global_autoboot_abort_key]);
- }
-
- switch (global_autoboot_abort_key) {
- case 0:
- flags |= CONSOLE_COUNTDOWN_ANYKEY;
- break;
- case 1:
- flags |= CONSOLE_COUNTDOWN_CTRLC;
- break;
- default:
- break;
- }
-
- ret = console_countdown(global_autoboot_timeout, flags, abortkeys,
- &outkey);
+ autoboot = do_autoboot_countdown();
- if (ret == 0)
+ if (autoboot == AUTOBOOT_BOOT)
run_command("boot");
console_ctrlc_allow();
- if (menu_exists) {
- if (outkey == 'm')
- run_command(menufile);
+ if (autoboot == AUTOBOOT_MENU)
+ run_command(MENUFILE);
- printf("Enter 'exit' to get back to the menu\n");
- run_shell();
- run_command(menufile);
- }
+ run_shell();
+ run_command(MENUFILE);
return 0;
}