blob: 7c92f3e54d1331e02efdef7870c02574e7218fc8 [file] [log] [blame]
Simon Glass66ded172014-04-10 20:01:28 -06001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
Jeroen Hofstee39e12302014-07-13 22:57:58 +02009#include <autoboot.h>
Simon Glass0098e172014-04-10 20:01:30 -060010#include <bootretry.h>
Simon Glass66ded172014-04-10 20:01:28 -060011#include <cli.h>
12#include <fdtdec.h>
13#include <menu.h>
14#include <post.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
18#define MAX_DELAY_STOP_STR 32
19
20#ifndef DEBUG_BOOTKEYS
21#define DEBUG_BOOTKEYS 0
22#endif
23#define debug_bootkeys(fmt, args...) \
24 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
25
Simon Glassaffb2152014-04-10 20:01:35 -060026/* Stored value of bootdelay, used by autoboot_command() */
27static int stored_bootdelay;
28
Simon Glass66ded172014-04-10 20:01:28 -060029/***************************************************************************
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
32 */
33# if defined(CONFIG_AUTOBOOT_KEYED)
34static int abortboot_keyed(int bootdelay)
35{
36 int abort = 0;
37 uint64_t etime = endtick(bootdelay);
38 struct {
39 char *str;
40 u_int len;
41 int retry;
42 }
43 delaykey[] = {
Jeroen Hofstee9e546ee2014-06-16 00:17:33 +020044 { .str = getenv("bootdelaykey"), .retry = 1 },
Jeroen Hofstee9e546ee2014-06-16 00:17:33 +020045 { .str = getenv("bootstopkey"), .retry = 0 },
Simon Glass66ded172014-04-10 20:01:28 -060046 };
47
48 char presskey[MAX_DELAY_STOP_STR];
49 u_int presskey_len = 0;
50 u_int presskey_max = 0;
51 u_int i;
52
53#ifndef CONFIG_ZERO_BOOTDELAY_CHECK
54 if (bootdelay == 0)
55 return 0;
56#endif
57
58# ifdef CONFIG_AUTOBOOT_PROMPT
59 printf(CONFIG_AUTOBOOT_PROMPT);
60# endif
61
62# ifdef CONFIG_AUTOBOOT_DELAY_STR
63 if (delaykey[0].str == NULL)
64 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
65# endif
Simon Glass66ded172014-04-10 20:01:28 -060066# ifdef CONFIG_AUTOBOOT_STOP_STR
Stefan Roese2d908fa2015-05-18 14:08:22 +020067 if (delaykey[1].str == NULL)
68 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
Simon Glass66ded172014-04-10 20:01:28 -060069# endif
70
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;
76
77 presskey_max = presskey_max > delaykey[i].len ?
78 presskey_max : delaykey[i].len;
79
80 debug_bootkeys("%s key:<%s>\n",
81 delaykey[i].retry ? "delay" : "stop",
82 delaykey[i].str ? delaykey[i].str : "NULL");
83 }
84
85 /* In order to keep up with incoming data, check timeout only
86 * when catch up.
87 */
88 do {
89 if (tstc()) {
90 if (presskey_len < presskey_max) {
91 presskey[presskey_len++] = getc();
92 } else {
93 for (i = 0; i < presskey_max - 1; i++)
94 presskey[i] = presskey[i + 1];
95
96 presskey[i] = getc();
97 }
98 }
99
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" :
108 "stop");
109
Simon Glass66ded172014-04-10 20:01:28 -0600110 /* don't retry auto boot */
111 if (!delaykey[i].retry)
112 bootretry_dont_retry();
Simon Glass66ded172014-04-10 20:01:28 -0600113 abort = 1;
114 }
115 }
116 } while (!abort && get_ticks() <= etime);
117
118 if (!abort)
119 debug_bootkeys("key timeout\n");
120
121#ifdef CONFIG_SILENT_CONSOLE
122 if (abort)
123 gd->flags &= ~GD_FLG_SILENT;
124#endif
125
126 return abort;
127}
128
129# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
130
131#ifdef CONFIG_MENUKEY
132static int menukey;
133#endif
134
135static int abortboot_normal(int bootdelay)
136{
137 int abort = 0;
138 unsigned long ts;
139
140#ifdef CONFIG_MENUPROMPT
141 printf(CONFIG_MENUPROMPT);
142#else
143 if (bootdelay >= 0)
144 printf("Hit any key to stop autoboot: %2d ", bootdelay);
145#endif
146
147#if defined CONFIG_ZERO_BOOTDELAY_CHECK
148 /*
149 * Check if key already pressed
150 * Don't check if bootdelay < 0
151 */
152 if (bootdelay >= 0) {
153 if (tstc()) { /* we got a key press */
154 (void) getc(); /* consume input */
155 puts("\b\b\b 0");
156 abort = 1; /* don't auto boot */
157 }
158 }
159#endif
160
161 while ((bootdelay > 0) && (!abort)) {
162 --bootdelay;
163 /* delay 1000 ms */
164 ts = get_timer(0);
165 do {
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
170 menukey = getc();
171# else
172 (void) getc(); /* consume input */
173# endif
174 break;
175 }
176 udelay(10000);
177 } while (!abort && get_timer(ts) < 1000);
178
179 printf("\b\b\b%2d ", bootdelay);
180 }
181
182 putc('\n');
183
184#ifdef CONFIG_SILENT_CONSOLE
185 if (abort)
186 gd->flags &= ~GD_FLG_SILENT;
187#endif
188
189 return abort;
190}
191# endif /* CONFIG_AUTOBOOT_KEYED */
192
193static int abortboot(int bootdelay)
194{
195#ifdef CONFIG_AUTOBOOT_KEYED
196 return abortboot_keyed(bootdelay);
197#else
198 return abortboot_normal(bootdelay);
199#endif
200}
201
Simon Glass66ded172014-04-10 20:01:28 -0600202static void process_fdt_options(const void *blob)
203{
Simon Glassaffb2152014-04-10 20:01:35 -0600204#if defined(CONFIG_OF_CONTROL)
Simon Glass66ded172014-04-10 20:01:28 -0600205 ulong addr;
206
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);
209 if (addr)
210 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
211
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);
214 if (addr)
215 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glass66ded172014-04-10 20:01:28 -0600216#endif /* CONFIG_OF_CONTROL */
Simon Glassaffb2152014-04-10 20:01:35 -0600217}
Simon Glass66ded172014-04-10 20:01:28 -0600218
Simon Glassaffb2152014-04-10 20:01:35 -0600219const char *bootdelay_process(void)
Simon Glass66ded172014-04-10 20:01:28 -0600220{
Simon Glass66ded172014-04-10 20:01:28 -0600221 char *s;
222 int bootdelay;
223#ifdef CONFIG_BOOTCOUNT_LIMIT
224 unsigned long bootcount = 0;
225 unsigned long bootlimit = 0;
226#endif /* CONFIG_BOOTCOUNT_LIMIT */
227
228#ifdef CONFIG_BOOTCOUNT_LIMIT
229 bootcount = bootcount_load();
230 bootcount++;
231 bootcount_store(bootcount);
232 setenv_ulong("bootcount", bootcount);
233 bootlimit = getenv_ulong("bootlimit", 10, 0);
234#endif /* CONFIG_BOOTCOUNT_LIMIT */
235
236 s = getenv("bootdelay");
237 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
238
239#ifdef CONFIG_OF_CONTROL
240 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
241 bootdelay);
242#endif
243
244 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
245
246#if defined(CONFIG_MENU_SHOW)
247 bootdelay = menu_show(bootdelay);
248#endif
Simon Glassb26440f2014-04-10 20:01:31 -0600249 bootretry_init_cmd_timeout();
Simon Glass66ded172014-04-10 20:01:28 -0600250
251#ifdef CONFIG_POST
252 if (gd->flags & GD_FLG_POSTFAIL) {
253 s = getenv("failbootcmd");
254 } else
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");
261 } else
262#endif /* CONFIG_BOOTCOUNT_LIMIT */
263 s = getenv("bootcmd");
Simon Glass66ded172014-04-10 20:01:28 -0600264
265 process_fdt_options(gd->fdt_blob);
Simon Glassaffb2152014-04-10 20:01:35 -0600266 stored_bootdelay = bootdelay;
Simon Glass66ded172014-04-10 20:01:28 -0600267
Simon Glassaffb2152014-04-10 20:01:35 -0600268 return s;
269}
Simon Glass66ded172014-04-10 20:01:28 -0600270
Simon Glassaffb2152014-04-10 20:01:35 -0600271void autoboot_command(const char *s)
272{
Simon Glass66ded172014-04-10 20:01:28 -0600273 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
274
Simon Glassaffb2152014-04-10 20:01:35 -0600275 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
Simon Glass66ded172014-04-10 20:01:28 -0600276#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
277 int prev = disable_ctrlc(1); /* disable Control C checking */
278#endif
279
280 run_command_list(s, -1, 0);
281
282#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
283 disable_ctrlc(prev); /* restore Control C checking */
284#endif
285 }
286
287#ifdef CONFIG_MENUKEY
288 if (menukey == CONFIG_MENUKEY) {
289 s = getenv("menucmd");
290 if (s)
291 run_command_list(s, -1, 0);
292 }
293#endif /* CONFIG_MENUKEY */
294}