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("bootdelaykey2"), .retry = 1 },
46 { .str = getenv("bootstopkey"), .retry = 0 },
47 { .str = getenv("bootstopkey2"), .retry = 0 },
50 char presskey[MAX_DELAY_STOP_STR];
51 u_int presskey_len = 0;
52 u_int presskey_max = 0;
55 #ifndef CONFIG_ZERO_BOOTDELAY_CHECK
60 # ifdef CONFIG_AUTOBOOT_PROMPT
61 printf(CONFIG_AUTOBOOT_PROMPT);
64 # ifdef CONFIG_AUTOBOOT_DELAY_STR
65 if (delaykey[0].str == NULL)
66 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
68 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
69 if (delaykey[1].str == NULL)
70 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
72 # ifdef CONFIG_AUTOBOOT_STOP_STR
73 if (delaykey[2].str == NULL)
74 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
76 # ifdef CONFIG_AUTOBOOT_STOP_STR2
77 if (delaykey[3].str == NULL)
78 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
81 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
82 delaykey[i].len = delaykey[i].str == NULL ?
83 0 : strlen(delaykey[i].str);
84 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
85 MAX_DELAY_STOP_STR : delaykey[i].len;
87 presskey_max = presskey_max > delaykey[i].len ?
88 presskey_max : delaykey[i].len;
90 debug_bootkeys("%s key:<%s>\n",
91 delaykey[i].retry ? "delay" : "stop",
92 delaykey[i].str ? delaykey[i].str : "NULL");
95 /* In order to keep up with incoming data, check timeout only
100 if (presskey_len < presskey_max) {
101 presskey[presskey_len++] = getc();
103 for (i = 0; i < presskey_max - 1; i++)
104 presskey[i] = presskey[i + 1];
106 presskey[i] = getc();
110 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
111 if (delaykey[i].len > 0 &&
112 presskey_len >= delaykey[i].len &&
113 memcmp(presskey + presskey_len -
114 delaykey[i].len, delaykey[i].str,
115 delaykey[i].len) == 0) {
116 debug_bootkeys("got %skey\n",
117 delaykey[i].retry ? "delay" :
120 /* don't retry auto boot */
121 if (!delaykey[i].retry)
122 bootretry_dont_retry();
126 } while (!abort && get_ticks() <= etime);
129 debug_bootkeys("key timeout\n");
131 #ifdef CONFIG_SILENT_CONSOLE
133 gd->flags &= ~GD_FLG_SILENT;
139 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
141 #ifdef CONFIG_MENUKEY
145 static int abortboot_normal(int bootdelay)
150 #ifdef CONFIG_MENUPROMPT
151 printf(CONFIG_MENUPROMPT);
154 printf("Hit any key to stop autoboot: %2d ", bootdelay);
157 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
159 * Check if key already pressed
160 * Don't check if bootdelay < 0
162 if (bootdelay >= 0) {
163 if (tstc()) { /* we got a key press */
164 (void) getc(); /* consume input */
166 abort = 1; /* don't auto boot */
171 while ((bootdelay > 0) && (!abort)) {
176 if (tstc()) { /* we got a key press */
177 abort = 1; /* don't auto boot */
178 bootdelay = 0; /* no more delay */
179 # ifdef CONFIG_MENUKEY
182 (void) getc(); /* consume input */
187 } while (!abort && get_timer(ts) < 1000);
189 printf("\b\b\b%2d ", bootdelay);
194 #ifdef CONFIG_SILENT_CONSOLE
196 gd->flags &= ~GD_FLG_SILENT;
201 # endif /* CONFIG_AUTOBOOT_KEYED */
203 static int abortboot(int bootdelay)
205 #ifdef CONFIG_AUTOBOOT_KEYED
206 return abortboot_keyed(bootdelay);
208 return abortboot_normal(bootdelay);
212 static void process_fdt_options(const void *blob)
214 #if defined(CONFIG_OF_CONTROL)
217 /* Add an env variable to point to a kernel payload, if available */
218 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
220 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
222 /* Add an env variable to point to a root disk, if available */
223 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
225 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
226 #endif /* CONFIG_OF_CONTROL */
229 const char *bootdelay_process(void)
233 #ifdef CONFIG_BOOTCOUNT_LIMIT
234 unsigned long bootcount = 0;
235 unsigned long bootlimit = 0;
236 #endif /* CONFIG_BOOTCOUNT_LIMIT */
238 #ifdef CONFIG_BOOTCOUNT_LIMIT
239 bootcount = bootcount_load();
241 bootcount_store(bootcount);
242 setenv_ulong("bootcount", bootcount);
243 bootlimit = getenv_ulong("bootlimit", 10, 0);
244 #endif /* CONFIG_BOOTCOUNT_LIMIT */
246 s = getenv("bootdelay");
247 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
249 #ifdef CONFIG_OF_CONTROL
250 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
254 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
256 #if defined(CONFIG_MENU_SHOW)
257 bootdelay = menu_show(bootdelay);
259 bootretry_init_cmd_timeout();
262 if (gd->flags & GD_FLG_POSTFAIL) {
263 s = getenv("failbootcmd");
265 #endif /* CONFIG_POST */
266 #ifdef CONFIG_BOOTCOUNT_LIMIT
267 if (bootlimit && (bootcount > bootlimit)) {
268 printf("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
269 (unsigned)bootlimit);
270 s = getenv("altbootcmd");
272 #endif /* CONFIG_BOOTCOUNT_LIMIT */
273 s = getenv("bootcmd");
275 process_fdt_options(gd->fdt_blob);
276 stored_bootdelay = bootdelay;
281 void autoboot_command(const char *s)
283 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
285 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
286 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
287 int prev = disable_ctrlc(1); /* disable Control C checking */
290 run_command_list(s, -1, 0);
292 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
293 disable_ctrlc(prev); /* restore Control C checking */
297 #ifdef CONFIG_MENUKEY
298 if (menukey == CONFIG_MENUKEY) {
299 s = getenv("menucmd");
301 run_command_list(s, -1, 0);
303 #endif /* CONFIG_MENUKEY */