autoboot: Use CONFIG_AUTOBOOT_STOP_STR_SHA256 indirectly
[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 <menu.h>
14 #include <post.h>
15 #include <u-boot/sha256.h>
16 #include <bootcount.h>
17
18 DECLARE_GLOBAL_DATA_PTR;
19
20 #define MAX_DELAY_STOP_STR 32
21
22 #ifndef DEBUG_BOOTKEYS
23 #define DEBUG_BOOTKEYS 0
24 #endif
25 #define debug_bootkeys(fmt, args...)            \
26         debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
27
28 /* Stored value of bootdelay, used by autoboot_command() */
29 static int stored_bootdelay;
30
31 #ifdef CONFIG_AUTOBOOT_ENCRYPTION
32 #define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256
33 #else
34 #define AUTOBOOT_STOP_STR_SHA256 ""
35 #endif
36
37 #if defined(CONFIG_AUTOBOOT_KEYED)
38 #if defined(CONFIG_AUTOBOOT_STOP_STR_SHA256)
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(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 #else
109 static int passwd_abort(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 #endif
186
187 /***************************************************************************
188  * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
189  * returns: 0 -  no key string, allow autoboot 1 - got key string, abort
190  */
191 static int __abortboot(int bootdelay)
192 {
193         int abort;
194         uint64_t etime = endtick(bootdelay);
195
196 #  ifdef CONFIG_AUTOBOOT_PROMPT
197         /*
198          * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
199          * To print the bootdelay value upon bootup.
200          */
201         printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
202 #  endif
203
204         abort = passwd_abort(etime);
205         if (!abort)
206                 debug_bootkeys("key timeout\n");
207
208         return abort;
209 }
210
211 # else  /* !defined(CONFIG_AUTOBOOT_KEYED) */
212
213 #ifdef CONFIG_MENUKEY
214 static int menukey;
215 #endif
216
217 static int __abortboot(int bootdelay)
218 {
219         int abort = 0;
220         unsigned long ts;
221
222 #ifdef CONFIG_MENUPROMPT
223         printf(CONFIG_MENUPROMPT);
224 #else
225         printf("Hit any key to stop autoboot: %2d ", bootdelay);
226 #endif
227
228         /*
229          * Check if key already pressed
230          */
231         if (tstc()) {   /* we got a key press   */
232                 (void) getc();  /* consume input        */
233                 puts("\b\b\b 0");
234                 abort = 1;      /* don't auto boot      */
235         }
236
237         while ((bootdelay > 0) && (!abort)) {
238                 --bootdelay;
239                 /* delay 1000 ms */
240                 ts = get_timer(0);
241                 do {
242                         if (tstc()) {   /* we got a key press   */
243                                 abort  = 1;     /* don't auto boot      */
244                                 bootdelay = 0;  /* no more delay        */
245 # ifdef CONFIG_MENUKEY
246                                 menukey = getc();
247 # else
248                                 (void) getc();  /* consume input        */
249 # endif
250                                 break;
251                         }
252                         udelay(10000);
253                 } while (!abort && get_timer(ts) < 1000);
254
255                 printf("\b\b\b%2d ", bootdelay);
256         }
257
258         putc('\n');
259
260         return abort;
261 }
262 # endif /* CONFIG_AUTOBOOT_KEYED */
263
264 static int abortboot(int bootdelay)
265 {
266         int abort = 0;
267
268         if (bootdelay >= 0)
269                 abort = __abortboot(bootdelay);
270
271 #ifdef CONFIG_SILENT_CONSOLE
272         if (abort)
273                 gd->flags &= ~GD_FLG_SILENT;
274 #endif
275
276         return abort;
277 }
278
279 static void process_fdt_options(const void *blob)
280 {
281 #if defined(CONFIG_OF_CONTROL) && defined(CONFIG_SYS_TEXT_BASE)
282         ulong addr;
283
284         /* Add an env variable to point to a kernel payload, if available */
285         addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
286         if (addr)
287                 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
288
289         /* Add an env variable to point to a root disk, if available */
290         addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
291         if (addr)
292                 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
293 #endif /* CONFIG_OF_CONTROL && CONFIG_SYS_TEXT_BASE */
294 }
295
296 const char *bootdelay_process(void)
297 {
298         char *s;
299         int bootdelay;
300
301         bootcount_inc();
302
303         s = env_get("bootdelay");
304         bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
305
306 #ifdef CONFIG_OF_CONTROL
307         bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
308                         bootdelay);
309 #endif
310
311         debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
312
313 #if defined(CONFIG_MENU_SHOW)
314         bootdelay = menu_show(bootdelay);
315 #endif
316         bootretry_init_cmd_timeout();
317
318 #ifdef CONFIG_POST
319         if (gd->flags & GD_FLG_POSTFAIL) {
320                 s = env_get("failbootcmd");
321         } else
322 #endif /* CONFIG_POST */
323         if (bootcount_error())
324                 s = env_get("altbootcmd");
325         else
326                 s = env_get("bootcmd");
327
328         process_fdt_options(gd->fdt_blob);
329         stored_bootdelay = bootdelay;
330
331         return s;
332 }
333
334 void autoboot_command(const char *s)
335 {
336         debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
337
338         if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
339 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
340                 int prev = disable_ctrlc(1);    /* disable Control C checking */
341 #endif
342
343                 run_command_list(s, -1, 0);
344
345 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
346                 disable_ctrlc(prev);    /* restore Control C checking */
347 #endif
348         }
349
350 #ifdef CONFIG_MENUKEY
351         if (menukey == CONFIG_MENUKEY) {
352                 s = env_get("menucmd");
353                 if (s)
354                         run_command_list(s, -1, 0);
355         }
356 #endif /* CONFIG_MENUKEY */
357 }