autoboot: Improve docs for CONFIG_AUTOBOOT_ENCRYPTION
[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 <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 #ifdef CONFIG_SILENT_CONSOLE
290         if (abort)
291                 gd->flags &= ~GD_FLG_SILENT;
292 #endif
293
294         return abort;
295 }
296
297 static void process_fdt_options(const void *blob)
298 {
299 #if defined(CONFIG_OF_CONTROL) && defined(CONFIG_SYS_TEXT_BASE)
300         ulong addr;
301
302         /* Add an env variable to point to a kernel payload, if available */
303         addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
304         if (addr)
305                 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
306
307         /* Add an env variable to point to a root disk, if available */
308         addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
309         if (addr)
310                 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
311 #endif /* CONFIG_OF_CONTROL && CONFIG_SYS_TEXT_BASE */
312 }
313
314 const char *bootdelay_process(void)
315 {
316         char *s;
317         int bootdelay;
318
319         bootcount_inc();
320
321         s = env_get("bootdelay");
322         bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
323
324 #ifdef CONFIG_OF_CONTROL
325         bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
326                         bootdelay);
327 #endif
328
329         debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
330
331 #if defined(CONFIG_MENU_SHOW)
332         bootdelay = menu_show(bootdelay);
333 #endif
334         bootretry_init_cmd_timeout();
335
336 #ifdef CONFIG_POST
337         if (gd->flags & GD_FLG_POSTFAIL) {
338                 s = env_get("failbootcmd");
339         } else
340 #endif /* CONFIG_POST */
341         if (bootcount_error())
342                 s = env_get("altbootcmd");
343         else
344                 s = env_get("bootcmd");
345
346         process_fdt_options(gd->fdt_blob);
347         stored_bootdelay = bootdelay;
348
349         return s;
350 }
351
352 void autoboot_command(const char *s)
353 {
354         debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
355
356         if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
357 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
358                 int prev = disable_ctrlc(1);    /* disable Control C checking */
359 #endif
360
361                 run_command_list(s, -1, 0);
362
363 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
364                 disable_ctrlc(prev);    /* restore Control C checking */
365 #endif
366         }
367
368 #ifdef CONFIG_MENUKEY
369         if (menukey == CONFIG_MENUKEY) {
370                 s = env_get("menucmd");
371                 if (s)
372                         run_command_list(s, -1, 0);
373         }
374 #endif /* CONFIG_MENUKEY */
375 }