]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
66ded17d SG |
2 | /* |
3 | * (C) Copyright 2000 | |
4 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
66ded17d SG |
5 | */ |
6 | ||
7 | #include <common.h> | |
39e1230e | 8 | #include <autoboot.h> |
0098e179 | 9 | #include <bootretry.h> |
66ded17d | 10 | #include <cli.h> |
288b29e4 | 11 | #include <command.h> |
24b852a7 | 12 | #include <console.h> |
c7694dd4 | 13 | #include <env.h> |
66ded17d | 14 | #include <fdtdec.h> |
e8c78056 | 15 | #include <hash.h> |
f7ae49fc | 16 | #include <log.h> |
336d4615 | 17 | #include <malloc.h> |
ecaae801 | 18 | #include <memalign.h> |
66ded17d SG |
19 | #include <menu.h> |
20 | #include <post.h> | |
1045315d | 21 | #include <time.h> |
401d1c4f | 22 | #include <asm/global_data.h> |
c05ed00a | 23 | #include <linux/delay.h> |
8f0b1e24 | 24 | #include <u-boot/sha256.h> |
bc8c440f | 25 | #include <bootcount.h> |
1a4a7786 | 26 | #include <crypt.h> |
66ded17d SG |
27 | |
28 | DECLARE_GLOBAL_DATA_PTR; | |
29 | ||
652b504f | 30 | #define MAX_DELAY_STOP_STR 64 |
66ded17d SG |
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 | ||
affb2156 SG |
38 | /* Stored value of bootdelay, used by autoboot_command() */ |
39 | static int stored_bootdelay; | |
d915ad27 | 40 | static int menukey; |
affb2156 | 41 | |
1a4a7786 SJ |
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 "" | |
0c4bd318 SG |
47 | #endif |
48 | ||
5a87df83 | 49 | #ifdef CONFIG_AUTOBOOT_USE_MENUKEY |
760d2f9e | 50 | #define AUTOBOOT_MENUKEY CONFIG_AUTOBOOT_MENUKEY |
d915ad27 SG |
51 | #else |
52 | #define AUTOBOOT_MENUKEY 0 | |
53 | #endif | |
54 | ||
1a4a7786 SJ |
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 | ||
8f0b1e24 SR |
112 | /* |
113 | * Use a "constant-length" time compare function for this | |
114 | * hash compare: | |
115 | * | |
116 | * https://crackstation.net/hashing-security.htm | |
66ded17d | 117 | */ |
8f0b1e24 SR |
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 | ||
88fa4beb SG |
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 | */ | |
e8c78056 | 137 | static int passwd_abort_sha256(uint64_t etime) |
8f0b1e24 | 138 | { |
00caae6d | 139 | const char *sha_env_str = env_get("bootstopkeysha256"); |
8f0b1e24 | 140 | u8 sha_env[SHA256_SUM_LEN]; |
ecaae801 HS |
141 | u8 *sha; |
142 | char *presskey; | |
652b504f | 143 | char *c; |
8f0b1e24 SR |
144 | const char *algo_name = "sha256"; |
145 | u_int presskey_len = 0; | |
146 | int abort = 0; | |
2d06fd83 | 147 | int size = sizeof(sha); |
8f0b1e24 SR |
148 | int ret; |
149 | ||
150 | if (sha_env_str == NULL) | |
1a4a7786 | 151 | sha_env_str = CONFIG_AUTOBOOT_STOP_STR_SHA256; |
8f0b1e24 | 152 | |
652b504f JP |
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 | } | |
8f0b1e24 SR |
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 | ||
ecaae801 HS |
172 | sha = malloc_cache_aligned(SHA256_SUM_LEN); |
173 | size = SHA256_SUM_LEN; | |
8f0b1e24 SR |
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 */ | |
ecaae801 HS |
182 | if (presskey_len >= MAX_DELAY_STOP_STR) { |
183 | free(presskey); | |
184 | free(sha); | |
8f0b1e24 | 185 | return 0; |
ecaae801 | 186 | } |
8f0b1e24 | 187 | |
c670aeee | 188 | presskey[presskey_len++] = getchar(); |
8f0b1e24 SR |
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 | ||
ecaae801 HS |
200 | free(presskey); |
201 | free(sha); | |
8f0b1e24 SR |
202 | return abort; |
203 | } | |
e8c78056 | 204 | |
88fa4beb SG |
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 | */ | |
e8c78056 | 213 | static int passwd_abort_key(uint64_t etime) |
66ded17d SG |
214 | { |
215 | int abort = 0; | |
66ded17d SG |
216 | struct { |
217 | char *str; | |
218 | u_int len; | |
219 | int retry; | |
220 | } | |
221 | delaykey[] = { | |
00caae6d SG |
222 | { .str = env_get("bootdelaykey"), .retry = 1 }, |
223 | { .str = env_get("bootstopkey"), .retry = 0 }, | |
66ded17d SG |
224 | }; |
225 | ||
226 | char presskey[MAX_DELAY_STOP_STR]; | |
e088f0c3 YM |
227 | int presskey_len = 0; |
228 | int presskey_max = 0; | |
229 | int i; | |
66ded17d | 230 | |
66ded17d SG |
231 | # ifdef CONFIG_AUTOBOOT_DELAY_STR |
232 | if (delaykey[0].str == NULL) | |
233 | delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR; | |
234 | # endif | |
66ded17d | 235 | # ifdef CONFIG_AUTOBOOT_STOP_STR |
2d908fa0 SR |
236 | if (delaykey[1].str == NULL) |
237 | delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR; | |
66ded17d SG |
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) { | |
c670aeee | 260 | presskey[presskey_len++] = getchar(); |
66ded17d SG |
261 | } else { |
262 | for (i = 0; i < presskey_max - 1; i++) | |
263 | presskey[i] = presskey[i + 1]; | |
264 | ||
c670aeee | 265 | presskey[i] = getchar(); |
66ded17d SG |
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 | ||
66ded17d SG |
279 | /* don't retry auto boot */ |
280 | if (!delaykey[i].retry) | |
281 | bootretry_dont_retry(); | |
66ded17d SG |
282 | abort = 1; |
283 | } | |
284 | } | |
285 | } while (!abort && get_ticks() <= etime); | |
286 | ||
8f0b1e24 SR |
287 | return abort; |
288 | } | |
8f0b1e24 SR |
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 | */ | |
e79e4b25 | 294 | static int abortboot_key_sequence(int bootdelay) |
8f0b1e24 SR |
295 | { |
296 | int abort; | |
297 | uint64_t etime = endtick(bootdelay); | |
298 | ||
8f0b1e24 SR |
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 | ||
1a4a7786 SJ |
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 { | |
e8c78056 | 313 | abort = passwd_abort_key(etime); |
1a4a7786 | 314 | } |
66ded17d SG |
315 | if (!abort) |
316 | debug_bootkeys("key timeout\n"); | |
317 | ||
66ded17d SG |
318 | return abort; |
319 | } | |
320 | ||
e79e4b25 | 321 | static int abortboot_single_key(int bootdelay) |
66ded17d SG |
322 | { |
323 | int abort = 0; | |
324 | unsigned long ts; | |
325 | ||
46327392 | 326 | printf("Hit any key to stop autoboot: %2d ", bootdelay); |
66ded17d | 327 | |
66ded17d SG |
328 | /* |
329 | * Check if key already pressed | |
66ded17d | 330 | */ |
46327392 | 331 | if (tstc()) { /* we got a key press */ |
c670aeee | 332 | getchar(); /* consume input */ |
46327392 MY |
333 | puts("\b\b\b 0"); |
334 | abort = 1; /* don't auto boot */ | |
66ded17d | 335 | } |
66ded17d SG |
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 */ | |
d915ad27 SG |
343 | int key; |
344 | ||
66ded17d SG |
345 | abort = 1; /* don't auto boot */ |
346 | bootdelay = 0; /* no more delay */ | |
c670aeee | 347 | key = getchar();/* consume input */ |
5a87df83 | 348 | if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY)) |
d915ad27 | 349 | menukey = key; |
66ded17d SG |
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 | ||
66ded17d SG |
360 | return abort; |
361 | } | |
66ded17d SG |
362 | |
363 | static int abortboot(int bootdelay) | |
364 | { | |
46327392 | 365 | int abort = 0; |
09b9d9e5 | 366 | |
e79e4b25 SG |
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 | } | |
09b9d9e5 | 373 | |
42b4d14e | 374 | if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort) |
09b9d9e5 | 375 | gd->flags &= ~GD_FLG_SILENT; |
09b9d9e5 MY |
376 | |
377 | return abort; | |
66ded17d SG |
378 | } |
379 | ||
66ded17d SG |
380 | static void process_fdt_options(const void *blob) |
381 | { | |
5fa3fd25 | 382 | #ifdef CONFIG_SYS_TEXT_BASE |
66ded17d SG |
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) | |
018f5303 | 388 | env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr)); |
66ded17d SG |
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) | |
018f5303 | 393 | env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr)); |
5fa3fd25 | 394 | #endif /* CONFIG_SYS_TEXT_BASE */ |
affb2156 | 395 | } |
66ded17d | 396 | |
affb2156 | 397 | const char *bootdelay_process(void) |
66ded17d | 398 | { |
66ded17d SG |
399 | char *s; |
400 | int bootdelay; | |
bc8c440f LM |
401 | |
402 | bootcount_inc(); | |
66ded17d | 403 | |
00caae6d | 404 | s = env_get("bootdelay"); |
66ded17d SG |
405 | bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY; |
406 | ||
5fa3fd25 SG |
407 | if (IS_ENABLED(CONFIG_OF_CONTROL)) |
408 | bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay", | |
409 | bootdelay); | |
66ded17d SG |
410 | |
411 | debug("### main_loop entered: bootdelay=%d\n\n", bootdelay); | |
412 | ||
5fa3fd25 SG |
413 | if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW)) |
414 | bootdelay = menu_show(bootdelay); | |
b26440f1 | 415 | bootretry_init_cmd_timeout(); |
66ded17d SG |
416 | |
417 | #ifdef CONFIG_POST | |
418 | if (gd->flags & GD_FLG_POSTFAIL) { | |
00caae6d | 419 | s = env_get("failbootcmd"); |
66ded17d SG |
420 | } else |
421 | #endif /* CONFIG_POST */ | |
bc8c440f | 422 | if (bootcount_error()) |
00caae6d | 423 | s = env_get("altbootcmd"); |
bc8c440f | 424 | else |
00caae6d | 425 | s = env_get("bootcmd"); |
66ded17d | 426 | |
5fa3fd25 SG |
427 | if (IS_ENABLED(CONFIG_OF_CONTROL)) |
428 | process_fdt_options(gd->fdt_blob); | |
affb2156 | 429 | stored_bootdelay = bootdelay; |
66ded17d | 430 | |
affb2156 SG |
431 | return s; |
432 | } | |
66ded17d | 433 | |
affb2156 SG |
434 | void autoboot_command(const char *s) |
435 | { | |
66ded17d SG |
436 | debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>"); |
437 | ||
6ebe6b38 HS |
438 | if (s && (stored_bootdelay == -2 || |
439 | (stored_bootdelay != -1 && !abortboot(stored_bootdelay)))) { | |
5ec35ff3 SG |
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 */ | |
66ded17d SG |
447 | |
448 | run_command_list(s, -1, 0); | |
449 | ||
5ec35ff3 SG |
450 | if (lock) |
451 | disable_ctrlc(prev); /* restore Ctrl-C checking */ | |
66ded17d SG |
452 | } |
453 | ||
5a87df83 | 454 | if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY) && |
d915ad27 | 455 | menukey == AUTOBOOT_MENUKEY) { |
00caae6d | 456 | s = env_get("menucmd"); |
66ded17d SG |
457 | if (s) |
458 | run_command_list(s, -1, 0); | |
459 | } | |
1a4a7786 | 460 | } |