autoboot: Use if() for CONFIG_SILENT_CONSOLE
[platform/kernel/u-boot.git] / common / autoboot.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6
7 #include <common.h>
8 #include <autoboot.h>
9 #include <bootretry.h>
10 #include <cli.h>
11 #include <console.h>
12 #include <fdtdec.h>
13 #include <hash.h>
14 #include <menu.h>
15 #include <post.h>
16 #include <u-boot/sha256.h>
17 #include <bootcount.h>
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 #define MAX_DELAY_STOP_STR 32
22
23 #ifndef DEBUG_BOOTKEYS
24 #define DEBUG_BOOTKEYS 0
25 #endif
26 #define debug_bootkeys(fmt, args...)            \
27         debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
28
29 /* Stored value of bootdelay, used by autoboot_command() */
30 static int stored_bootdelay;
31
32 #ifdef CONFIG_AUTOBOOT_ENCRYPTION
33 #define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256
34 #else
35 #define AUTOBOOT_STOP_STR_SHA256 ""
36 #endif
37
38 #if defined(CONFIG_AUTOBOOT_KEYED)
39
40 /*
41  * Use a "constant-length" time compare function for this
42  * hash compare:
43  *
44  * https://crackstation.net/hashing-security.htm
45  */
46 static int slow_equals(u8 *a, u8 *b, int len)
47 {
48         int diff = 0;
49         int i;
50
51         for (i = 0; i < len; i++)
52                 diff |= a[i] ^ b[i];
53
54         return diff == 0;
55 }
56
57 /**
58  * passwd_abort_sha256() - check for a hashed key sequence to abort booting
59  *
60  * This checks for the user entering a SHA256 hash within a given time.
61  *
62  * @etime: Timeout value ticks (stop when get_ticks() reachs this)
63  * @return 0 if autoboot should continue, 1 if it should stop
64  */
65 static int passwd_abort_sha256(uint64_t etime)
66 {
67         const char *sha_env_str = env_get("bootstopkeysha256");
68         u8 sha_env[SHA256_SUM_LEN];
69         u8 sha[SHA256_SUM_LEN];
70         char presskey[MAX_DELAY_STOP_STR];
71         const char *algo_name = "sha256";
72         u_int presskey_len = 0;
73         int abort = 0;
74         int size = sizeof(sha);
75         int ret;
76
77         if (sha_env_str == NULL)
78                 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
79
80         /*
81          * Generate the binary value from the environment hash value
82          * so that we can compare this value with the computed hash
83          * from the user input
84          */
85         ret = hash_parse_string(algo_name, sha_env_str, sha_env);
86         if (ret) {
87                 printf("Hash %s not supported!\n", algo_name);
88                 return 0;
89         }
90
91         /*
92          * We don't know how long the stop-string is, so we need to
93          * generate the sha256 hash upon each input character and
94          * compare the value with the one saved in the environment
95          */
96         do {
97                 if (tstc()) {
98                         /* Check for input string overflow */
99                         if (presskey_len >= MAX_DELAY_STOP_STR)
100                                 return 0;
101
102                         presskey[presskey_len++] = getc();
103
104                         /* Calculate sha256 upon each new char */
105                         hash_block(algo_name, (const void *)presskey,
106                                    presskey_len, sha, &size);
107
108                         /* And check if sha matches saved value in env */
109                         if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
110                                 abort = 1;
111                 }
112         } while (!abort && get_ticks() <= etime);
113
114         return abort;
115 }
116
117 /**
118  * passwd_abort_key() - check for a key sequence to aborted booting
119  *
120  * This checks for the user entering a string within a given time.
121  *
122  * @etime: Timeout value ticks (stop when get_ticks() reachs this)
123  * @return 0 if autoboot should continue, 1 if it should stop
124  */
125 static int passwd_abort_key(uint64_t etime)
126 {
127         int abort = 0;
128         struct {
129                 char *str;
130                 u_int len;
131                 int retry;
132         }
133         delaykey[] = {
134                 { .str = env_get("bootdelaykey"),  .retry = 1 },
135                 { .str = env_get("bootstopkey"),   .retry = 0 },
136         };
137
138         char presskey[MAX_DELAY_STOP_STR];
139         u_int presskey_len = 0;
140         u_int presskey_max = 0;
141         u_int i;
142
143 #  ifdef CONFIG_AUTOBOOT_DELAY_STR
144         if (delaykey[0].str == NULL)
145                 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
146 #  endif
147 #  ifdef CONFIG_AUTOBOOT_STOP_STR
148         if (delaykey[1].str == NULL)
149                 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
150 #  endif
151
152         for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
153                 delaykey[i].len = delaykey[i].str == NULL ?
154                                     0 : strlen(delaykey[i].str);
155                 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
156                                     MAX_DELAY_STOP_STR : delaykey[i].len;
157
158                 presskey_max = presskey_max > delaykey[i].len ?
159                                     presskey_max : delaykey[i].len;
160
161                 debug_bootkeys("%s key:<%s>\n",
162                                delaykey[i].retry ? "delay" : "stop",
163                                delaykey[i].str ? delaykey[i].str : "NULL");
164         }
165
166         /* In order to keep up with incoming data, check timeout only
167          * when catch up.
168          */
169         do {
170                 if (tstc()) {
171                         if (presskey_len < presskey_max) {
172                                 presskey[presskey_len++] = getc();
173                         } else {
174                                 for (i = 0; i < presskey_max - 1; i++)
175                                         presskey[i] = presskey[i + 1];
176
177                                 presskey[i] = getc();
178                         }
179                 }
180
181                 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
182                         if (delaykey[i].len > 0 &&
183                             presskey_len >= delaykey[i].len &&
184                                 memcmp(presskey + presskey_len -
185                                         delaykey[i].len, delaykey[i].str,
186                                         delaykey[i].len) == 0) {
187                                         debug_bootkeys("got %skey\n",
188                                                 delaykey[i].retry ? "delay" :
189                                                 "stop");
190
191                                 /* don't retry auto boot */
192                                 if (!delaykey[i].retry)
193                                         bootretry_dont_retry();
194                                 abort = 1;
195                         }
196                 }
197         } while (!abort && get_ticks() <= etime);
198
199         return abort;
200 }
201
202 /***************************************************************************
203  * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
204  * returns: 0 -  no key string, allow autoboot 1 - got key string, abort
205  */
206 static int __abortboot(int bootdelay)
207 {
208         int abort;
209         uint64_t etime = endtick(bootdelay);
210
211 #  ifdef CONFIG_AUTOBOOT_PROMPT
212         /*
213          * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
214          * To print the bootdelay value upon bootup.
215          */
216         printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
217 #  endif
218
219         if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION))
220                 abort = passwd_abort_sha256(etime);
221         else
222                 abort = passwd_abort_key(etime);
223         if (!abort)
224                 debug_bootkeys("key timeout\n");
225
226         return abort;
227 }
228
229 # else  /* !defined(CONFIG_AUTOBOOT_KEYED) */
230
231 #ifdef CONFIG_MENUKEY
232 static int menukey;
233 #endif
234
235 static int __abortboot(int bootdelay)
236 {
237         int abort = 0;
238         unsigned long ts;
239
240 #ifdef CONFIG_MENUPROMPT
241         printf(CONFIG_MENUPROMPT);
242 #else
243         printf("Hit any key to stop autoboot: %2d ", bootdelay);
244 #endif
245
246         /*
247          * Check if key already pressed
248          */
249         if (tstc()) {   /* we got a key press   */
250                 (void) getc();  /* consume input        */
251                 puts("\b\b\b 0");
252                 abort = 1;      /* don't auto boot      */
253         }
254
255         while ((bootdelay > 0) && (!abort)) {
256                 --bootdelay;
257                 /* delay 1000 ms */
258                 ts = get_timer(0);
259                 do {
260                         if (tstc()) {   /* we got a key press   */
261                                 abort  = 1;     /* don't auto boot      */
262                                 bootdelay = 0;  /* no more delay        */
263 # ifdef CONFIG_MENUKEY
264                                 menukey = getc();
265 # else
266                                 (void) getc();  /* consume input        */
267 # endif
268                                 break;
269                         }
270                         udelay(10000);
271                 } while (!abort && get_timer(ts) < 1000);
272
273                 printf("\b\b\b%2d ", bootdelay);
274         }
275
276         putc('\n');
277
278         return abort;
279 }
280 # endif /* CONFIG_AUTOBOOT_KEYED */
281
282 static int abortboot(int bootdelay)
283 {
284         int abort = 0;
285
286         if (bootdelay >= 0)
287                 abort = __abortboot(bootdelay);
288
289         if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
290                 gd->flags &= ~GD_FLG_SILENT;
291
292         return abort;
293 }
294
295 static void process_fdt_options(const void *blob)
296 {
297 #if defined(CONFIG_OF_CONTROL) && defined(CONFIG_SYS_TEXT_BASE)
298         ulong addr;
299
300         /* Add an env variable to point to a kernel payload, if available */
301         addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
302         if (addr)
303                 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
304
305         /* Add an env variable to point to a root disk, if available */
306         addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
307         if (addr)
308                 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
309 #endif /* CONFIG_OF_CONTROL && CONFIG_SYS_TEXT_BASE */
310 }
311
312 const char *bootdelay_process(void)
313 {
314         char *s;
315         int bootdelay;
316
317         bootcount_inc();
318
319         s = env_get("bootdelay");
320         bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
321
322 #ifdef CONFIG_OF_CONTROL
323         bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
324                         bootdelay);
325 #endif
326
327         debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
328
329 #if defined(CONFIG_MENU_SHOW)
330         bootdelay = menu_show(bootdelay);
331 #endif
332         bootretry_init_cmd_timeout();
333
334 #ifdef CONFIG_POST
335         if (gd->flags & GD_FLG_POSTFAIL) {
336                 s = env_get("failbootcmd");
337         } else
338 #endif /* CONFIG_POST */
339         if (bootcount_error())
340                 s = env_get("altbootcmd");
341         else
342                 s = env_get("bootcmd");
343
344         process_fdt_options(gd->fdt_blob);
345         stored_bootdelay = bootdelay;
346
347         return s;
348 }
349
350 void autoboot_command(const char *s)
351 {
352         debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
353
354         if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
355 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
356                 int prev = disable_ctrlc(1);    /* disable Control C checking */
357 #endif
358
359                 run_command_list(s, -1, 0);
360
361 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
362                 disable_ctrlc(prev);    /* restore Control C checking */
363 #endif
364         }
365
366 #ifdef CONFIG_MENUKEY
367         if (menukey == CONFIG_MENUKEY) {
368                 s = env_get("menucmd");
369                 if (s)
370                         run_command_list(s, -1, 0);
371         }
372 #endif /* CONFIG_MENUKEY */
373 }