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