3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <bootretry.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 #define MAX_DELAY_STOP_STR 32
20 #ifndef DEBUG_BOOTKEYS
21 #define DEBUG_BOOTKEYS 0
23 #define debug_bootkeys(fmt, args...) \
24 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
26 /* Stored value of bootdelay, used by autoboot_command() */
27 static int stored_bootdelay;
29 /***************************************************************************
30 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
31 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
33 # if defined(CONFIG_AUTOBOOT_KEYED)
34 static int abortboot_keyed(int bootdelay)
37 uint64_t etime = endtick(bootdelay);
44 { .str = getenv("bootdelaykey"), .retry = 1 },
45 { .str = getenv("bootstopkey"), .retry = 0 },
48 char presskey[MAX_DELAY_STOP_STR];
49 u_int presskey_len = 0;
50 u_int presskey_max = 0;
53 #ifndef CONFIG_ZERO_BOOTDELAY_CHECK
58 # ifdef CONFIG_AUTOBOOT_PROMPT
60 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
61 * To print the bootdelay value upon bootup.
63 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
66 # ifdef CONFIG_AUTOBOOT_DELAY_STR
67 if (delaykey[0].str == NULL)
68 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
70 # ifdef CONFIG_AUTOBOOT_STOP_STR
71 if (delaykey[1].str == NULL)
72 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
75 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
76 delaykey[i].len = delaykey[i].str == NULL ?
77 0 : strlen(delaykey[i].str);
78 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
79 MAX_DELAY_STOP_STR : delaykey[i].len;
81 presskey_max = presskey_max > delaykey[i].len ?
82 presskey_max : delaykey[i].len;
84 debug_bootkeys("%s key:<%s>\n",
85 delaykey[i].retry ? "delay" : "stop",
86 delaykey[i].str ? delaykey[i].str : "NULL");
89 /* In order to keep up with incoming data, check timeout only
94 if (presskey_len < presskey_max) {
95 presskey[presskey_len++] = getc();
97 for (i = 0; i < presskey_max - 1; i++)
98 presskey[i] = presskey[i + 1];
100 presskey[i] = getc();
104 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
105 if (delaykey[i].len > 0 &&
106 presskey_len >= delaykey[i].len &&
107 memcmp(presskey + presskey_len -
108 delaykey[i].len, delaykey[i].str,
109 delaykey[i].len) == 0) {
110 debug_bootkeys("got %skey\n",
111 delaykey[i].retry ? "delay" :
114 /* don't retry auto boot */
115 if (!delaykey[i].retry)
116 bootretry_dont_retry();
120 } while (!abort && get_ticks() <= etime);
123 debug_bootkeys("key timeout\n");
125 #ifdef CONFIG_SILENT_CONSOLE
127 gd->flags &= ~GD_FLG_SILENT;
133 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
135 #ifdef CONFIG_MENUKEY
139 static int abortboot_normal(int bootdelay)
144 #ifdef CONFIG_MENUPROMPT
145 printf(CONFIG_MENUPROMPT);
148 printf("Hit any key to stop autoboot: %2d ", bootdelay);
151 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
153 * Check if key already pressed
154 * Don't check if bootdelay < 0
156 if (bootdelay >= 0) {
157 if (tstc()) { /* we got a key press */
158 (void) getc(); /* consume input */
160 abort = 1; /* don't auto boot */
165 while ((bootdelay > 0) && (!abort)) {
170 if (tstc()) { /* we got a key press */
171 abort = 1; /* don't auto boot */
172 bootdelay = 0; /* no more delay */
173 # ifdef CONFIG_MENUKEY
176 (void) getc(); /* consume input */
181 } while (!abort && get_timer(ts) < 1000);
183 printf("\b\b\b%2d ", bootdelay);
188 #ifdef CONFIG_SILENT_CONSOLE
190 gd->flags &= ~GD_FLG_SILENT;
195 # endif /* CONFIG_AUTOBOOT_KEYED */
197 static int abortboot(int bootdelay)
199 #ifdef CONFIG_AUTOBOOT_KEYED
200 return abortboot_keyed(bootdelay);
202 return abortboot_normal(bootdelay);
206 static void process_fdt_options(const void *blob)
208 #if defined(CONFIG_OF_CONTROL)
211 /* Add an env variable to point to a kernel payload, if available */
212 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
214 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
216 /* Add an env variable to point to a root disk, if available */
217 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
219 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
220 #endif /* CONFIG_OF_CONTROL */
223 const char *bootdelay_process(void)
227 #ifdef CONFIG_BOOTCOUNT_LIMIT
228 unsigned long bootcount = 0;
229 unsigned long bootlimit = 0;
230 #endif /* CONFIG_BOOTCOUNT_LIMIT */
232 #ifdef CONFIG_BOOTCOUNT_LIMIT
233 bootcount = bootcount_load();
235 bootcount_store(bootcount);
236 setenv_ulong("bootcount", bootcount);
237 bootlimit = getenv_ulong("bootlimit", 10, 0);
238 #endif /* CONFIG_BOOTCOUNT_LIMIT */
240 s = getenv("bootdelay");
241 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
243 #ifdef CONFIG_OF_CONTROL
244 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
248 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
250 #if defined(CONFIG_MENU_SHOW)
251 bootdelay = menu_show(bootdelay);
253 bootretry_init_cmd_timeout();
256 if (gd->flags & GD_FLG_POSTFAIL) {
257 s = getenv("failbootcmd");
259 #endif /* CONFIG_POST */
260 #ifdef CONFIG_BOOTCOUNT_LIMIT
261 if (bootlimit && (bootcount > bootlimit)) {
262 printf("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
263 (unsigned)bootlimit);
264 s = getenv("altbootcmd");
266 #endif /* CONFIG_BOOTCOUNT_LIMIT */
267 s = getenv("bootcmd");
269 process_fdt_options(gd->fdt_blob);
270 stored_bootdelay = bootdelay;
275 void autoboot_command(const char *s)
277 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
279 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
280 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
281 int prev = disable_ctrlc(1); /* disable Control C checking */
284 run_command_list(s, -1, 0);
286 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
287 disable_ctrlc(prev); /* restore Control C checking */
291 #ifdef CONFIG_MENUKEY
292 if (menukey == CONFIG_MENUKEY) {
293 s = getenv("menucmd");
295 run_command_list(s, -1, 0);
297 #endif /* CONFIG_MENUKEY */