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