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