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