1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2011-2012 The Chromium OS Authors.
4  */
5 
6 #include <config.h>
7 #include <cli.h>
8 #include <command.h>
9 #include <efi_loader.h>
10 #include <errno.h>
11 #include <event.h>
12 #include <init.h>
13 #include <log.h>
14 #include <os.h>
15 #include <sort.h>
16 #include <spl.h>
17 #include <asm/getopt.h>
18 #include <asm/global_data.h>
19 #include <asm/io.h>
20 #include <asm/malloc.h>
21 #include <asm/sections.h>
22 #include <asm/state.h>
23 #include <dm/root.h>
24 #include <linux/ctype.h>
25 
26 DECLARE_GLOBAL_DATA_PTR;
27 
28 static char **os_argv;
29 
30 /* Compare two options so that they can be sorted into alphabetical order */
h_compare_opt(const void * p1,const void * p2)31 static int h_compare_opt(const void *p1, const void *p2)
32 {
33 	const struct sandbox_cmdline_option *opt1 = p1;
34 	const struct sandbox_cmdline_option *opt2 = p2;
35 	const char *str1, *str2;
36 	char flag1[2], flag2[2];
37 
38 	opt1 = *(struct sandbox_cmdline_option **)p1;
39 	opt2 = *(struct sandbox_cmdline_option **)p2;
40 	flag1[1] = '\0';
41 	flag2[1] = '\0';
42 
43 	*flag1 = opt1->flag_short < 0x100 ? opt1->flag_short : '\0';
44 	*flag2 = opt2->flag_short < 0x100 ? opt2->flag_short : '\0';
45 
46 	str1 = *flag1 ? flag1 : opt1->flag;
47 	str2 = *flag2 ? flag2 : opt2->flag;
48 
49 	/*
50 	 * Force lower-case flags to come before upper-case ones. We only
51 	 * support upper-case for short flags.
52 	 */
53 	if (isalpha(*str1) && isalpha(*str2) &&
54 	    tolower(*str1) == tolower(*str2))
55 		return isupper(*str1) - isupper(*str2);
56 
57 	return strcasecmp(str1, str2);
58 }
59 
sandbox_early_getopt_check(void)60 int sandbox_early_getopt_check(void)
61 {
62 	struct sandbox_state *state = state_get_current();
63 	struct sandbox_cmdline_option **sb_opt =
64 		__u_boot_sandbox_option_start();
65 	size_t num_options = __u_boot_sandbox_option_count();
66 	size_t i;
67 	int max_arg_len, max_noarg_len;
68 	struct sandbox_cmdline_option **sorted_opt;
69 	int size;
70 
71 	/* parse_err will be a string of the faulting option */
72 	if (!state->parse_err)
73 		return 0;
74 
75 	if (strcmp(state->parse_err, "help")) {
76 		printf("u-boot: error: failed while parsing option: %s\n"
77 			"\ttry running with --help for more information.\n",
78 			state->parse_err);
79 		os_exit(1);
80 	}
81 
82 	printf(
83 		"u-boot, a command line test interface to U-Boot\n\n"
84 		"Usage: u-boot [options]\n"
85 		"Options:\n");
86 
87 	max_arg_len = 0;
88 	for (i = 0; i < num_options; ++i)
89 		max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
90 	max_noarg_len = max_arg_len + 7;
91 
92 	/* Sort the options */
93 	size = sizeof(*sorted_opt) * num_options;
94 	sorted_opt = os_malloc(size);
95 	if (!sorted_opt) {
96 		printf("No memory to sort options\n");
97 		os_exit(1);
98 	}
99 	memcpy(sorted_opt, sb_opt, size);
100 	qsort(sorted_opt, num_options, sizeof(*sorted_opt), h_compare_opt);
101 
102 	for (i = 0; i < num_options; ++i) {
103 		struct sandbox_cmdline_option *opt = sorted_opt[i];
104 
105 		/* first output the short flag if it has one */
106 		if (opt->flag_short >= 0x100)
107 			printf("      ");
108 		else
109 			printf("  -%c, ", opt->flag_short);
110 
111 		/* then the long flag */
112 		if (opt->has_arg)
113 			printf("--%-*s <arg> ", max_arg_len, opt->flag);
114 		else
115 			printf("--%-*s", max_noarg_len, opt->flag);
116 
117 		/* finally the help text */
118 		printf("  %s\n", opt->help);
119 	}
120 
121 	os_exit(0);
122 }
123 EVENT_SPY_SIMPLE(EVT_MISC_INIT_F, sandbox_early_getopt_check);
124 
sandbox_cmdline_cb_help(struct sandbox_state * state,const char * arg)125 static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
126 {
127 	/* just flag to sandbox_early_getopt_check to show usage */
128 	return 1;
129 }
130 SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
131 
132 #ifndef CONFIG_XPL_BUILD
sandbox_main_loop_init(void)133 int sandbox_main_loop_init(void)
134 {
135 	struct sandbox_state *state = state_get_current();
136 
137 	/* Execute command if required */
138 	if (state->cmd || state->run_distro_boot) {
139 		int retval = 0;
140 
141 		cli_init();
142 
143 #ifdef CONFIG_CMDLINE
144 		if (state->cmd)
145 			retval = run_command_list(state->cmd, -1, 0);
146 
147 		if (state->run_distro_boot)
148 			retval = cli_simple_run_command("run distro_bootcmd",
149 							0);
150 #endif
151 		if (!state->interactive)
152 			os_exit(retval);
153 	}
154 
155 	return 0;
156 }
157 #endif
158 
sandbox_cmdline_cb_boot(struct sandbox_state * state,const char * arg)159 static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
160 				      const char *arg)
161 {
162 	state->run_distro_boot = true;
163 	return 0;
164 }
165 SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
166 
sandbox_cmdline_cb_command(struct sandbox_state * state,const char * arg)167 static int sandbox_cmdline_cb_command(struct sandbox_state *state,
168 				      const char *arg)
169 {
170 	state->cmd = arg;
171 	return 0;
172 }
173 SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
174 
sandbox_cmdline_cb_fdt(struct sandbox_state * state,const char * arg)175 static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
176 {
177 	state->fdt_fname = arg;
178 	return 0;
179 }
180 SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
181 
sandbox_cmdline_cb_default_fdt(struct sandbox_state * state,const char * arg)182 static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state,
183 					  const char *arg)
184 {
185 	const char *fmt = "%s.dtb";
186 	char *fname;
187 	int len;
188 
189 	len = strlen(state->argv[0]) + strlen(fmt) + 1;
190 	fname = os_malloc(len);
191 	if (!fname)
192 		return -ENOMEM;
193 	snprintf(fname, len, fmt, state->argv[0]);
194 	state->fdt_fname = fname;
195 
196 	return 0;
197 }
198 SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
199 		"Use the default u-boot.dtb control FDT in U-Boot directory");
200 
sandbox_cmdline_cb_test_fdt(struct sandbox_state * state,const char * arg)201 static int sandbox_cmdline_cb_test_fdt(struct sandbox_state *state,
202 				       const char *arg)
203 {
204 	char buf[256];
205 	char *fname;
206 	char *relname;
207 	int len;
208 
209 	if (xpl_phase() <= PHASE_SPL)
210 		relname = "../arch/sandbox/dts/test.dtb";
211 	else
212 		relname = "arch/sandbox/dts/test.dtb";
213 	len = state_get_rel_filename(relname, buf, sizeof(buf));
214 	if (len < 0)
215 		return len;
216 
217 	fname = os_malloc(len);
218 	if (!fname)
219 		return -ENOMEM;
220 	strcpy(fname, buf);
221 	state->fdt_fname = fname;
222 
223 	return 0;
224 }
225 SANDBOX_CMDLINE_OPT_SHORT(test_fdt, 'T', 0,
226 			  "Use the test.dtb control FDT in U-Boot directory");
227 
sandbox_cmdline_cb_interactive(struct sandbox_state * state,const char * arg)228 static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
229 					  const char *arg)
230 {
231 	state->interactive = true;
232 	return 0;
233 }
234 
235 SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
236 
sandbox_cmdline_cb_jump(struct sandbox_state * state,const char * arg)237 static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
238 				   const char *arg)
239 {
240 	/* Remember to delete this U-Boot image later */
241 	state->jumped_fname = arg;
242 
243 	return 0;
244 }
245 SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
246 
sandbox_cmdline_cb_program(struct sandbox_state * state,const char * arg)247 static int sandbox_cmdline_cb_program(struct sandbox_state *state,
248 				      const char *arg)
249 {
250 	/*
251 	 * Record the program name to use when jumping to future phases. This
252 	 * is the original executable which holds all the phases. We need to
253 	 * use this instead of argv[0] since each phase is started by
254 	 * extracting a particular binary from the full program, then running
255 	 * it. Therefore in that binary, argv[0] contains only the
256 	 * current-phase executable.
257 	 *
258 	 * For example, sandbox TPL may be started using image file:
259 	 *
260 	 *     ./image.bin
261 	 *
262 	 * but then TPL needs to run VPL, which it does by extracting the VPL
263 	 * image from the image.bin file.
264 	 *
265 	 *    ./temp-vpl
266 	 *
267 	 * When VPL runs it needs access to the original image.bin so it can
268 	 * extract the next phase (SPL). This works if we use '-f image.bin'
269 	 * when starting the original image.bin file.
270 	 */
271 	state->prog_fname = arg;
272 
273 	return 0;
274 }
275 SANDBOX_CMDLINE_OPT_SHORT(program, 'p', 1, "U-Boot program name");
276 
sandbox_cmdline_cb_memory(struct sandbox_state * state,const char * arg)277 static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
278 				     const char *arg)
279 {
280 	/* For now assume we always want to write it */
281 	state->write_ram_buf = true;
282 	state->ram_buf_fname = arg;
283 	state->ram_buf_read = true;
284 
285 	return 0;
286 }
287 SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
288 			  "Read/write ram_buf memory contents from file");
289 
sandbox_cmdline_cb_rm_memory(struct sandbox_state * state,const char * arg)290 static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
291 					const char *arg)
292 {
293 	state->ram_buf_rm = true;
294 
295 	return 0;
296 }
297 SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
298 
sandbox_cmdline_cb_state(struct sandbox_state * state,const char * arg)299 static int sandbox_cmdline_cb_state(struct sandbox_state *state,
300 				    const char *arg)
301 {
302 	state->state_fname = arg;
303 	return 0;
304 }
305 SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
306 
sandbox_cmdline_cb_read(struct sandbox_state * state,const char * arg)307 static int sandbox_cmdline_cb_read(struct sandbox_state *state,
308 				   const char *arg)
309 {
310 	state->read_state = true;
311 	return 0;
312 }
313 SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
314 
sandbox_cmdline_cb_write(struct sandbox_state * state,const char * arg)315 static int sandbox_cmdline_cb_write(struct sandbox_state *state,
316 				    const char *arg)
317 {
318 	state->write_state = true;
319 	return 0;
320 }
321 SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
322 
sandbox_cmdline_cb_ignore_missing(struct sandbox_state * state,const char * arg)323 static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
324 					     const char *arg)
325 {
326 	state->ignore_missing_state_on_read = true;
327 	return 0;
328 }
329 SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
330 			  "Ignore missing state on read");
331 
sandbox_cmdline_cb_show_lcd(struct sandbox_state * state,const char * arg)332 static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
333 				       const char *arg)
334 {
335 	state->show_lcd = true;
336 	return 0;
337 }
338 SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
339 			  "Show the sandbox LCD display");
340 
sandbox_cmdline_cb_double_lcd(struct sandbox_state * state,const char * arg)341 static int sandbox_cmdline_cb_double_lcd(struct sandbox_state *state,
342 					 const char *arg)
343 {
344 	state->double_lcd = true;
345 
346 	return 0;
347 }
348 SANDBOX_CMDLINE_OPT_SHORT(double_lcd, 'K', 0,
349 			  "Double the LCD display size in each direction");
350 
351 static const char *term_args[STATE_TERM_COUNT] = {
352 	"raw-with-sigs",
353 	"raw",
354 	"cooked",
355 };
356 
sandbox_cmdline_cb_terminal(struct sandbox_state * state,const char * arg)357 static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
358 				       const char *arg)
359 {
360 	int i;
361 
362 	for (i = 0; i < STATE_TERM_COUNT; i++) {
363 		if (!strcmp(arg, term_args[i])) {
364 			state->term_raw = i;
365 			return 0;
366 		}
367 	}
368 
369 	printf("Unknown terminal setting '%s' (", arg);
370 	for (i = 0; i < STATE_TERM_COUNT; i++)
371 		printf("%s%s", i ? ", " : "", term_args[i]);
372 	puts(")\n");
373 
374 	return 1;
375 }
376 SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
377 			  "Set terminal to raw/cooked mode");
378 
sandbox_cmdline_cb_verbose(struct sandbox_state * state,const char * arg)379 static int sandbox_cmdline_cb_verbose(struct sandbox_state *state,
380 				      const char *arg)
381 {
382 	state->show_test_output = true;
383 	return 0;
384 }
385 SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output");
386 
sandbox_cmdline_cb_log_level(struct sandbox_state * state,const char * arg)387 static int sandbox_cmdline_cb_log_level(struct sandbox_state *state,
388 					const char *arg)
389 {
390 	state->default_log_level = simple_strtol(arg, NULL, 10);
391 
392 	return 0;
393 }
394 SANDBOX_CMDLINE_OPT_SHORT(log_level, 'L', 1,
395 			  "Set log level (0=panic, 7=debug)");
396 
sandbox_cmdline_cb_unittests(struct sandbox_state * state,const char * arg)397 static int sandbox_cmdline_cb_unittests(struct sandbox_state *state,
398 					const char *arg)
399 {
400 	state->run_unittests = true;
401 
402 	return 0;
403 }
404 SANDBOX_CMDLINE_OPT_SHORT(unittests, 'u', 0, "Run unit tests");
405 
sandbox_cmdline_cb_select_unittests(struct sandbox_state * state,const char * arg)406 static int sandbox_cmdline_cb_select_unittests(struct sandbox_state *state,
407 					       const char *arg)
408 {
409 	state->select_unittests = arg;
410 
411 	return 0;
412 }
413 SANDBOX_CMDLINE_OPT_SHORT(select_unittests, 'k', 1, "Select unit tests to run");
414 
sandbox_cmdline_cb_signals(struct sandbox_state * state,const char * arg)415 static int sandbox_cmdline_cb_signals(struct sandbox_state *state,
416 				      const char *arg)
417 {
418 	state->handle_signals = true;
419 
420 	return 0;
421 }
422 SANDBOX_CMDLINE_OPT_SHORT(signals, 'S', 0,
423 			  "Handle signals (such as SIGSEGV) in sandbox");
424 
sandbox_cmdline_cb_autoboot_keyed(struct sandbox_state * state,const char * arg)425 static int sandbox_cmdline_cb_autoboot_keyed(struct sandbox_state *state,
426 					     const char *arg)
427 {
428 	state->autoboot_keyed = true;
429 
430 	return 0;
431 }
432 SANDBOX_CMDLINE_OPT(autoboot_keyed, 0, "Allow keyed autoboot");
433 
sandbox_cmdline_cb_upl(struct sandbox_state * state,const char * arg)434 static int sandbox_cmdline_cb_upl(struct sandbox_state *state, const char *arg)
435 {
436 	state->upl = true;
437 
438 	return 0;
439 }
440 SANDBOX_CMDLINE_OPT(upl, 0, "Enable Universal Payload (UPL)");
441 
setup_ram_buf(struct sandbox_state * state)442 static void setup_ram_buf(struct sandbox_state *state)
443 {
444 	/* Zero the RAM buffer if we didn't read it, to keep valgrind happy */
445 	if (!state->ram_buf_read)
446 		memset(state->ram_buf, '\0', state->ram_size);
447 
448 	gd->arch.ram_buf = state->ram_buf;
449 	gd->ram_size = state->ram_size;
450 }
451 
sandbox_cmdline_cb_native(struct sandbox_state * state,const char * arg)452 static int sandbox_cmdline_cb_native(struct sandbox_state *state,
453 				     const char *arg)
454 {
455 	state->native = true;
456 
457 	return 0;
458 }
459 SANDBOX_CMDLINE_OPT_SHORT(native, 'N', 0,
460 			  "Use native mode (host-based EFI boot filename)");
461 
state_show(struct sandbox_state * state)462 void state_show(struct sandbox_state *state)
463 {
464 	char **p;
465 
466 	printf("Arguments:\n");
467 	for (p = state->argv; *p; p++)
468 		printf("%s ", *p);
469 	printf("\n");
470 }
471 
efi_reset_system(enum efi_reset_type reset_type,efi_status_t reset_status,unsigned long data_size,void * reset_data)472 void __efi_runtime EFIAPI efi_reset_system(
473 		enum efi_reset_type reset_type,
474 		efi_status_t reset_status,
475 		unsigned long data_size, void *reset_data)
476 {
477 	if (reset_type == EFI_RESET_SHUTDOWN)
478 		sandbox_exit();
479 	else
480 		sandbox_reset();
481 }
482 
sandbox_reset(void)483 void sandbox_reset(void)
484 {
485 	/* Do this here while it still has an effect */
486 	os_fd_restore();
487 	if (state_uninit())
488 		os_exit(2);
489 
490 	/* Restart U-Boot */
491 	os_relaunch(os_argv);
492 }
493 
sandbox_main(int argc,char * argv[])494 int sandbox_main(int argc, char *argv[])
495 {
496 	struct sandbox_state *state;
497 	void * text_base;
498 	gd_t data;
499 	int size;
500 	int ret;
501 
502 	text_base = os_find_text_base();
503 
504 	memset(&data, '\0', sizeof(data));
505 	gd = &data;
506 
507 	/*
508 	 * This must be the first invocation of os_malloc() to have
509 	 * state->ram_buf in the low 4 GiB.
510 	 */
511 	ret = state_init();
512 	if (ret)
513 		goto err;
514 
515 	/*
516 	 * Copy argv[] so that we can pass the arguments in the original
517 	 * sequence when resetting the sandbox.
518 	 */
519 	size = sizeof(char *) * (argc + 1);
520 	os_argv = os_malloc(size);
521 	if (!os_argv)
522 		os_exit(1);
523 	memcpy(os_argv, argv, size);
524 
525 	gd->arch.text_base = text_base;
526 
527 	state = state_get_current();
528 	if (os_parse_args(state, argc, argv))
529 		return 1;
530 
531 	if (state->ram_buf_fname) {
532 		ret = os_read_ram_buf(state->ram_buf_fname);
533 		if (ret) {
534 			printf("Failed to read RAM buffer '%s': %d\n",
535 			       state->ram_buf_fname, ret);
536 		} else {
537 			state->ram_buf_read = true;
538 			log_debug("Read RAM buffer from '%s'\n", state->ram_buf_fname);
539 		}
540 	}
541 
542 	/* Remove old memory file if required */
543 	if (state->ram_buf_rm && state->ram_buf_fname) {
544 		os_unlink(state->ram_buf_fname);
545 		state->write_ram_buf = false;
546 		state->ram_buf_fname = NULL;
547 	}
548 
549 	if (state->read_state && state->state_fname) {
550 		ret = sandbox_read_state(state, state->state_fname);
551 		if (ret)
552 			goto err;
553 	}
554 
555 	if (state->handle_signals) {
556 		ret = os_setup_signal_handlers();
557 		if (ret)
558 			goto err;
559 	}
560 
561 	if (state->upl)
562 		gd->flags |= GD_FLG_UPL;
563 
564 #if CONFIG_IS_ENABLED(SYS_MALLOC_F)
565 	gd->malloc_base = CFG_MALLOC_F_ADDR;
566 #endif
567 #if CONFIG_IS_ENABLED(LOG)
568 	gd->default_log_level = state->default_log_level;
569 #endif
570 	setup_ram_buf(state);
571 
572 	/*
573 	 * Set up the relocation offset here, since sandbox symbols are always
574 	 * relocated by the OS before sandbox is entered.
575 	 */
576 	gd->reloc_off = (ulong)gd->arch.text_base;
577 
578 	/* sandbox test: log functions called before log_init in board_init_f */
579 	log_debug("debug: %s\n", __func__);
580 
581 	/* Do pre- and post-relocation init */
582 	board_init_f(gd->flags);
583 
584 	board_init_r(gd->new_gd, 0);
585 
586 	/* NOTREACHED - board_init_r() does not return */
587 	return 0;
588 
589 err:
590 	printf("Error %d\n", ret);
591 	return 1;
592 }
593