autoboot: Drop #ifdef 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 static int passwd_abort_sha256(uint64_t etime)
58 {
59         const char *sha_env_str = env_get("bootstopkeysha256");
60         u8 sha_env[SHA256_SUM_LEN];
61         u8 sha[SHA256_SUM_LEN];
62         char presskey[MAX_DELAY_STOP_STR];
63         const char *algo_name = "sha256";
64         u_int presskey_len = 0;
65         int abort = 0;
66         int size = sizeof(sha);
67         int ret;
68
69         if (sha_env_str == NULL)
70                 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
71
72         /*
73          * Generate the binary value from the environment hash value
74          * so that we can compare this value with the computed hash
75          * from the user input
76          */
77         ret = hash_parse_string(algo_name, sha_env_str, sha_env);
78         if (ret) {
79                 printf("Hash %s not supported!\n", algo_name);
80                 return 0;
81         }
82
83         /*
84          * We don't know how long the stop-string is, so we need to
85          * generate the sha256 hash upon each input character and
86          * compare the value with the one saved in the environment
87          */
88         do {
89                 if (tstc()) {
90                         /* Check for input string overflow */
91                         if (presskey_len >= MAX_DELAY_STOP_STR)
92                                 return 0;
93
94                         presskey[presskey_len++] = getc();
95
96                         /* Calculate sha256 upon each new char */
97                         hash_block(algo_name, (const void *)presskey,
98                                    presskey_len, sha, &size);
99
100                         /* And check if sha matches saved value in env */
101                         if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
102                                 abort = 1;
103                 }
104         } while (!abort && get_ticks() <= etime);
105
106         return abort;
107 }
108
109 static int passwd_abort_key(uint64_t etime)
110 {
111         int abort = 0;
112         struct {
113                 char *str;
114                 u_int len;
115                 int retry;
116         }
117         delaykey[] = {
118                 { .str = env_get("bootdelaykey"),  .retry = 1 },
119                 { .str = env_get("bootstopkey"),   .retry = 0 },
120         };
121
122         char presskey[MAX_DELAY_STOP_STR];
123         u_int presskey_len = 0;
124         u_int presskey_max = 0;
125         u_int i;
126
127 #  ifdef CONFIG_AUTOBOOT_DELAY_STR
128         if (delaykey[0].str == NULL)
129                 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
130 #  endif
131 #  ifdef CONFIG_AUTOBOOT_STOP_STR
132         if (delaykey[1].str == NULL)
133                 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
134 #  endif
135
136         for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
137                 delaykey[i].len = delaykey[i].str == NULL ?
138                                     0 : strlen(delaykey[i].str);
139                 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
140                                     MAX_DELAY_STOP_STR : delaykey[i].len;
141
142                 presskey_max = presskey_max > delaykey[i].len ?
143                                     presskey_max : delaykey[i].len;
144
145                 debug_bootkeys("%s key:<%s>\n",
146                                delaykey[i].retry ? "delay" : "stop",
147                                delaykey[i].str ? delaykey[i].str : "NULL");
148         }
149
150         /* In order to keep up with incoming data, check timeout only
151          * when catch up.
152          */
153         do {
154                 if (tstc()) {
155                         if (presskey_len < presskey_max) {
156                                 presskey[presskey_len++] = getc();
157                         } else {
158                                 for (i = 0; i < presskey_max - 1; i++)
159                                         presskey[i] = presskey[i + 1];
160
161                                 presskey[i] = getc();
162                         }
163                 }
164
165                 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
166                         if (delaykey[i].len > 0 &&
167                             presskey_len >= delaykey[i].len &&
168                                 memcmp(presskey + presskey_len -
169                                         delaykey[i].len, delaykey[i].str,
170                                         delaykey[i].len) == 0) {
171                                         debug_bootkeys("got %skey\n",
172                                                 delaykey[i].retry ? "delay" :
173                                                 "stop");
174
175                                 /* don't retry auto boot */
176                                 if (!delaykey[i].retry)
177                                         bootretry_dont_retry();
178                                 abort = 1;
179                         }
180                 }
181         } while (!abort && get_ticks() <= etime);
182
183         return abort;
184 }
185
186 /***************************************************************************
187  * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
188  * returns: 0 -  no key string, allow autoboot 1 - got key string, abort
189  */
190 static int __abortboot(int bootdelay)
191 {
192         int abort;
193         uint64_t etime = endtick(bootdelay);
194
195 #  ifdef CONFIG_AUTOBOOT_PROMPT
196         /*
197          * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
198          * To print the bootdelay value upon bootup.
199          */
200         printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
201 #  endif
202
203         if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION))
204                 abort = passwd_abort_sha256(etime);
205         else
206                 abort = passwd_abort_key(etime);
207         if (!abort)
208                 debug_bootkeys("key timeout\n");
209
210         return abort;
211 }
212
213 # else  /* !defined(CONFIG_AUTOBOOT_KEYED) */
214
215 #ifdef CONFIG_MENUKEY
216 static int menukey;
217 #endif
218
219 static int __abortboot(int bootdelay)
220 {
221         int abort = 0;
222         unsigned long ts;
223
224 #ifdef CONFIG_MENUPROMPT
225         printf(CONFIG_MENUPROMPT);
226 #else
227         printf("Hit any key to stop autoboot: %2d ", bootdelay);
228 #endif
229
230         /*
231          * Check if key already pressed
232          */
233         if (tstc()) {   /* we got a key press   */
234                 (void) getc();  /* consume input        */
235                 puts("\b\b\b 0");
236                 abort = 1;      /* don't auto boot      */
237         }
238
239         while ((bootdelay > 0) && (!abort)) {
240                 --bootdelay;
241                 /* delay 1000 ms */
242                 ts = get_timer(0);
243                 do {
244                         if (tstc()) {   /* we got a key press   */
245                                 abort  = 1;     /* don't auto boot      */
246                                 bootdelay = 0;  /* no more delay        */
247 # ifdef CONFIG_MENUKEY
248                                 menukey = getc();
249 # else
250                                 (void) getc();  /* consume input        */
251 # endif
252                                 break;
253                         }
254                         udelay(10000);
255                 } while (!abort && get_timer(ts) < 1000);
256
257                 printf("\b\b\b%2d ", bootdelay);
258         }
259
260         putc('\n');
261
262         return abort;
263 }
264 # endif /* CONFIG_AUTOBOOT_KEYED */
265
266 static int abortboot(int bootdelay)
267 {
268         int abort = 0;
269
270         if (bootdelay >= 0)
271                 abort = __abortboot(bootdelay);
272
273 #ifdef CONFIG_SILENT_CONSOLE
274         if (abort)
275                 gd->flags &= ~GD_FLG_SILENT;
276 #endif
277
278         return abort;
279 }
280
281 static void process_fdt_options(const void *blob)
282 {
283 #if defined(CONFIG_OF_CONTROL) && defined(CONFIG_SYS_TEXT_BASE)
284         ulong addr;
285
286         /* Add an env variable to point to a kernel payload, if available */
287         addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
288         if (addr)
289                 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
290
291         /* Add an env variable to point to a root disk, if available */
292         addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
293         if (addr)
294                 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
295 #endif /* CONFIG_OF_CONTROL && CONFIG_SYS_TEXT_BASE */
296 }
297
298 const char *bootdelay_process(void)
299 {
300         char *s;
301         int bootdelay;
302
303         bootcount_inc();
304
305         s = env_get("bootdelay");
306         bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
307
308 #ifdef CONFIG_OF_CONTROL
309         bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
310                         bootdelay);
311 #endif
312
313         debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
314
315 #if defined(CONFIG_MENU_SHOW)
316         bootdelay = menu_show(bootdelay);
317 #endif
318         bootretry_init_cmd_timeout();
319
320 #ifdef CONFIG_POST
321         if (gd->flags & GD_FLG_POSTFAIL) {
322                 s = env_get("failbootcmd");
323         } else
324 #endif /* CONFIG_POST */
325         if (bootcount_error())
326                 s = env_get("altbootcmd");
327         else
328                 s = env_get("bootcmd");
329
330         process_fdt_options(gd->fdt_blob);
331         stored_bootdelay = bootdelay;
332
333         return s;
334 }
335
336 void autoboot_command(const char *s)
337 {
338         debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
339
340         if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
341 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
342                 int prev = disable_ctrlc(1);    /* disable Control C checking */
343 #endif
344
345                 run_command_list(s, -1, 0);
346
347 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
348                 disable_ctrlc(prev);    /* restore Control C checking */
349 #endif
350         }
351
352 #ifdef CONFIG_MENUKEY
353         if (menukey == CONFIG_MENUKEY) {
354                 s = env_get("menucmd");
355                 if (s)
356                         run_command_list(s, -1, 0);
357         }
358 #endif /* CONFIG_MENUKEY */
359 }