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
59 printf(CONFIG_AUTOBOOT_PROMPT);
62 # ifdef CONFIG_AUTOBOOT_DELAY_STR
63 if (delaykey[0].str == NULL)
64 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
66 # ifdef CONFIG_AUTOBOOT_STOP_STR
67 if (delaykey[1].str == NULL)
68 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
71 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
72 delaykey[i].len = delaykey[i].str == NULL ?
73 0 : strlen(delaykey[i].str);
74 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
75 MAX_DELAY_STOP_STR : delaykey[i].len;
77 presskey_max = presskey_max > delaykey[i].len ?
78 presskey_max : delaykey[i].len;
80 debug_bootkeys("%s key:<%s>\n",
81 delaykey[i].retry ? "delay" : "stop",
82 delaykey[i].str ? delaykey[i].str : "NULL");
85 /* In order to keep up with incoming data, check timeout only
90 if (presskey_len < presskey_max) {
91 presskey[presskey_len++] = getc();
93 for (i = 0; i < presskey_max - 1; i++)
94 presskey[i] = presskey[i + 1];
100 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
101 if (delaykey[i].len > 0 &&
102 presskey_len >= delaykey[i].len &&
103 memcmp(presskey + presskey_len -
104 delaykey[i].len, delaykey[i].str,
105 delaykey[i].len) == 0) {
106 debug_bootkeys("got %skey\n",
107 delaykey[i].retry ? "delay" :
110 /* don't retry auto boot */
111 if (!delaykey[i].retry)
112 bootretry_dont_retry();
116 } while (!abort && get_ticks() <= etime);
119 debug_bootkeys("key timeout\n");
121 #ifdef CONFIG_SILENT_CONSOLE
123 gd->flags &= ~GD_FLG_SILENT;
129 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
131 #ifdef CONFIG_MENUKEY
135 static int abortboot_normal(int bootdelay)
140 #ifdef CONFIG_MENUPROMPT
141 printf(CONFIG_MENUPROMPT);
144 printf("Hit any key to stop autoboot: %2d ", bootdelay);
147 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
149 * Check if key already pressed
150 * Don't check if bootdelay < 0
152 if (bootdelay >= 0) {
153 if (tstc()) { /* we got a key press */
154 (void) getc(); /* consume input */
156 abort = 1; /* don't auto boot */
161 while ((bootdelay > 0) && (!abort)) {
166 if (tstc()) { /* we got a key press */
167 abort = 1; /* don't auto boot */
168 bootdelay = 0; /* no more delay */
169 # ifdef CONFIG_MENUKEY
172 (void) getc(); /* consume input */
177 } while (!abort && get_timer(ts) < 1000);
179 printf("\b\b\b%2d ", bootdelay);
184 #ifdef CONFIG_SILENT_CONSOLE
186 gd->flags &= ~GD_FLG_SILENT;
191 # endif /* CONFIG_AUTOBOOT_KEYED */
193 static int abortboot(int bootdelay)
195 #ifdef CONFIG_AUTOBOOT_KEYED
196 return abortboot_keyed(bootdelay);
198 return abortboot_normal(bootdelay);
202 static void process_fdt_options(const void *blob)
204 #if defined(CONFIG_OF_CONTROL)
207 /* Add an env variable to point to a kernel payload, if available */
208 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
210 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
212 /* Add an env variable to point to a root disk, if available */
213 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
215 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
216 #endif /* CONFIG_OF_CONTROL */
219 const char *bootdelay_process(void)
223 #ifdef CONFIG_BOOTCOUNT_LIMIT
224 unsigned long bootcount = 0;
225 unsigned long bootlimit = 0;
226 #endif /* CONFIG_BOOTCOUNT_LIMIT */
228 #ifdef CONFIG_BOOTCOUNT_LIMIT
229 bootcount = bootcount_load();
231 bootcount_store(bootcount);
232 setenv_ulong("bootcount", bootcount);
233 bootlimit = getenv_ulong("bootlimit", 10, 0);
234 #endif /* CONFIG_BOOTCOUNT_LIMIT */
236 s = getenv("bootdelay");
237 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
239 #ifdef CONFIG_OF_CONTROL
240 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
244 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
246 #if defined(CONFIG_MENU_SHOW)
247 bootdelay = menu_show(bootdelay);
249 bootretry_init_cmd_timeout();
252 if (gd->flags & GD_FLG_POSTFAIL) {
253 s = getenv("failbootcmd");
255 #endif /* CONFIG_POST */
256 #ifdef CONFIG_BOOTCOUNT_LIMIT
257 if (bootlimit && (bootcount > bootlimit)) {
258 printf("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
259 (unsigned)bootlimit);
260 s = getenv("altbootcmd");
262 #endif /* CONFIG_BOOTCOUNT_LIMIT */
263 s = getenv("bootcmd");
265 process_fdt_options(gd->fdt_blob);
266 stored_bootdelay = bootdelay;
271 void autoboot_command(const char *s)
273 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
275 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
276 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
277 int prev = disable_ctrlc(1); /* disable Control C checking */
280 run_command_list(s, -1, 0);
282 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
283 disable_ctrlc(prev); /* restore Control C checking */
287 #ifdef CONFIG_MENUKEY
288 if (menukey == CONFIG_MENUKEY) {
289 s = getenv("menucmd");
291 run_command_list(s, -1, 0);
293 #endif /* CONFIG_MENUKEY */