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