blob: dce804165296d982a418c6fe7988045a672fdbab [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass5ba8ef62011-10-03 19:26:45 +00002/*
Simon Glass20bf89a2012-02-15 15:51:15 -08003 * Copyright (c) 2011-2012 The Chromium OS Authors.
Simon Glass5ba8ef62011-10-03 19:26:45 +00004 */
5
Tom Riniada64ac2023-12-14 13:16:46 -05006#include <config.h>
Simon Glassfe143ee2022-03-04 08:42:58 -07007#include <cli.h>
Simon Glassadaaa482019-11-14 12:57:43 -07008#include <command.h>
Heinrich Schuchardt43eb8722020-12-02 16:22:11 +01009#include <efi_loader.h>
Simon Glass07a3b232015-05-04 11:31:08 -060010#include <errno.h>
Simon Glassf1c51912022-03-04 08:43:04 -070011#include <event.h>
Simon Glass97589732020-05-10 11:40:02 -060012#include <init.h>
Patrick Delaunay053156d2020-11-27 11:20:55 +010013#include <log.h>
Simon Glass9dd10bf2013-11-10 10:27:03 -070014#include <os.h>
Simon Glass11c92b12020-02-03 07:35:47 -070015#include <sort.h>
Sean Anderson44c925d2023-10-14 16:47:56 -040016#include <spl.h>
Simon Glass8a3e0352012-02-15 15:51:16 -080017#include <asm/getopt.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
Simon Glassc395fdf2014-07-10 22:23:27 -060019#include <asm/io.h>
Simon Glass30f92052020-02-03 07:36:05 -070020#include <asm/malloc.h>
Simon Glass8a3e0352012-02-15 15:51:16 -080021#include <asm/sections.h>
Simon Glass20bf89a2012-02-15 15:51:15 -080022#include <asm/state.h>
Simon Glassfe143ee2022-03-04 08:42:58 -070023#include <dm/root.h>
Simon Glass11c92b12020-02-03 07:35:47 -070024#include <linux/ctype.h>
Simon Glass5ba8ef62011-10-03 19:26:45 +000025
Simon Glass2fd34e62013-11-10 10:26:59 -070026DECLARE_GLOBAL_DATA_PTR;
27
Heinrich Schuchardt1c678442020-10-27 20:29:25 +010028static char **os_argv;
29
Simon Glass11c92b12020-02-03 07:35:47 -070030/* Compare two options so that they can be sorted into alphabetical order */
31static 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
Simon Glass8a3e0352012-02-15 15:51:16 -080060int sandbox_early_getopt_check(void)
61{
62 struct sandbox_state *state = state_get_current();
Marek BehĂșn184c4af2021-05-20 13:24:06 +020063 struct sandbox_cmdline_option **sb_opt =
64 __u_boot_sandbox_option_start();
Simon Glass8a3e0352012-02-15 15:51:16 -080065 size_t num_options = __u_boot_sandbox_option_count();
66 size_t i;
67 int max_arg_len, max_noarg_len;
Simon Glass11c92b12020-02-03 07:35:47 -070068 struct sandbox_cmdline_option **sorted_opt;
69 int size;
Simon Glass8a3e0352012-02-15 15:51:16 -080070
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)
Masahiro Yamadadb204642014-11-07 03:03:31 +090089 max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
Simon Glass8a3e0352012-02-15 15:51:16 -080090 max_noarg_len = max_arg_len + 7;
91
Simon Glass11c92b12020-02-03 07:35:47 -070092 /* Sort the options */
93 size = sizeof(*sorted_opt) * num_options;
Simon Glassedd094e2021-02-06 09:57:33 -070094 sorted_opt = os_malloc(size);
Simon Glass11c92b12020-02-03 07:35:47 -070095 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
Simon Glass8a3e0352012-02-15 15:51:16 -0800102 for (i = 0; i < num_options; ++i) {
Simon Glass11c92b12020-02-03 07:35:47 -0700103 struct sandbox_cmdline_option *opt = sorted_opt[i];
Simon Glass8a3e0352012-02-15 15:51:16 -0800104
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)
Simon Glass8a3e0352012-02-15 15:51:16 -0800113 printf("--%-*s <arg> ", max_arg_len, opt->flag);
Simon Glass5f679f02013-11-10 10:26:58 -0700114 else
115 printf("--%-*s", max_noarg_len, opt->flag);
Simon Glass8a3e0352012-02-15 15:51:16 -0800116
117 /* finally the help text */
118 printf(" %s\n", opt->help);
119 }
120
121 os_exit(0);
122}
Simon Glassb8357c12023-08-21 21:16:56 -0600123EVENT_SPY_SIMPLE(EVT_MISC_INIT_F, sandbox_early_getopt_check);
Simon Glassb419dbb2017-03-28 10:27:28 -0600124
Simon Glass64367c82013-12-03 16:43:23 -0700125static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
Simon Glass8a3e0352012-02-15 15:51:16 -0800126{
127 /* just flag to sandbox_early_getopt_check to show usage */
128 return 1;
129}
Simon Glass64367c82013-12-03 16:43:23 -0700130SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
Simon Glass8a3e0352012-02-15 15:51:16 -0800131
Simon Glass5cf16632016-07-04 11:57:50 -0600132#ifndef CONFIG_SPL_BUILD
Simon Glass0fc3c222012-02-26 17:38:50 -0500133int sandbox_main_loop_init(void)
134{
Simon Glass8a3e0352012-02-15 15:51:16 -0800135 struct sandbox_state *state = state_get_current();
136
137 /* Execute command if required */
Sjoerd Simons335f4702015-04-30 22:16:09 +0200138 if (state->cmd || state->run_distro_boot) {
139 int retval = 0;
Joe Hershberger744d7882015-02-06 15:37:31 -0600140
Rabin Vincent7e0194a2014-10-29 23:21:38 +0100141 cli_init();
142
Simon Glasse0f12802016-03-13 19:07:30 -0600143#ifdef CONFIG_CMDLINE
Sjoerd Simons335f4702015-04-30 22:16:09 +0200144 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);
Simon Glasse0f12802016-03-13 19:07:30 -0600150#endif
Simon Glassf498e432013-11-10 10:27:02 -0700151 if (!state->interactive)
Joe Hershberger744d7882015-02-06 15:37:31 -0600152 os_exit(retval);
Simon Glass8a3e0352012-02-15 15:51:16 -0800153 }
154
Sjoerd Simons335f4702015-04-30 22:16:09 +0200155 return 0;
156}
Simon Glass5cf16632016-07-04 11:57:50 -0600157#endif
Sjoerd Simons335f4702015-04-30 22:16:09 +0200158
159static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
160 const char *arg)
161{
162 state->run_distro_boot = true;
Simon Glass0fc3c222012-02-26 17:38:50 -0500163 return 0;
164}
Sjoerd Simons335f4702015-04-30 22:16:09 +0200165SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
Simon Glass0fc3c222012-02-26 17:38:50 -0500166
Simon Glass64367c82013-12-03 16:43:23 -0700167static int sandbox_cmdline_cb_command(struct sandbox_state *state,
168 const char *arg)
Simon Glass8a3e0352012-02-15 15:51:16 -0800169{
170 state->cmd = arg;
171 return 0;
172}
Simon Glass64367c82013-12-03 16:43:23 -0700173SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
Simon Glass8a3e0352012-02-15 15:51:16 -0800174
Simon Glass64367c82013-12-03 16:43:23 -0700175static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
Simon Glass15393432013-04-20 08:42:41 +0000176{
177 state->fdt_fname = arg;
178 return 0;
179}
Simon Glass64367c82013-12-03 16:43:23 -0700180SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
Simon Glass15393432013-04-20 08:42:41 +0000181
Simon Glass8e170782015-01-19 20:21:34 -0700182static 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;
Simon Glassedd094e2021-02-06 09:57:33 -0700190 fname = os_malloc(len);
Simon Glass8e170782015-01-19 20:21:34 -0700191 if (!fname)
192 return -ENOMEM;
193 snprintf(fname, len, fmt, state->argv[0]);
194 state->fdt_fname = fname;
195
196 return 0;
197}
198SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
199 "Use the default u-boot.dtb control FDT in U-Boot directory");
200
Simon Glass3c3968f2019-09-25 08:56:07 -0600201static int sandbox_cmdline_cb_test_fdt(struct sandbox_state *state,
202 const char *arg)
203{
Simon Glassd74c4612022-09-06 20:27:08 -0600204 char buf[256];
Simon Glass3c3968f2019-09-25 08:56:07 -0600205 char *fname;
Sean Anderson44c925d2023-10-14 16:47:56 -0400206 char *relname;
Simon Glass3c3968f2019-09-25 08:56:07 -0600207 int len;
208
Sean Anderson44c925d2023-10-14 16:47:56 -0400209 if (spl_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));
Simon Glassd74c4612022-09-06 20:27:08 -0600214 if (len < 0)
215 return len;
216
Simon Glassedd094e2021-02-06 09:57:33 -0700217 fname = os_malloc(len);
Simon Glass3c3968f2019-09-25 08:56:07 -0600218 if (!fname)
219 return -ENOMEM;
Simon Glassd74c4612022-09-06 20:27:08 -0600220 strcpy(fname, buf);
Simon Glass3c3968f2019-09-25 08:56:07 -0600221 state->fdt_fname = fname;
222
223 return 0;
224}
225SANDBOX_CMDLINE_OPT_SHORT(test_fdt, 'T', 0,
226 "Use the test.dtb control FDT in U-Boot directory");
227
Simon Glassf498e432013-11-10 10:27:02 -0700228static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
229 const char *arg)
230{
231 state->interactive = true;
232 return 0;
233}
234
235SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
236
Simon Glasse9906532014-02-27 13:26:16 -0700237static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
238 const char *arg)
239{
Simon Glass47acfc62014-02-27 13:26:23 -0700240 /* Remember to delete this U-Boot image later */
241 state->jumped_fname = arg;
Simon Glasse9906532014-02-27 13:26:16 -0700242
243 return 0;
244}
245SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
246
Simon Glassf0b534f2022-10-20 18:23:02 -0600247static 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}
275SANDBOX_CMDLINE_OPT_SHORT(program, 'p', 1, "U-Boot program name");
276
Simon Glass9dd10bf2013-11-10 10:27:03 -0700277static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
278 const char *arg)
279{
Simon Glass9dd10bf2013-11-10 10:27:03 -0700280 /* For now assume we always want to write it */
281 state->write_ram_buf = true;
282 state->ram_buf_fname = arg;
Simon Glass24a284a2018-11-23 21:29:29 -0700283 state->ram_buf_read = true;
Simon Glass9dd10bf2013-11-10 10:27:03 -0700284
285 return 0;
286}
287SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
288 "Read/write ram_buf memory contents from file");
289
Simon Glass47acfc62014-02-27 13:26:23 -0700290static 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}
297SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
298
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700299static int sandbox_cmdline_cb_state(struct sandbox_state *state,
300 const char *arg)
301{
302 state->state_fname = arg;
303 return 0;
304}
305SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
306
307static int sandbox_cmdline_cb_read(struct sandbox_state *state,
308 const char *arg)
309{
310 state->read_state = true;
311 return 0;
312}
313SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
314
315static int sandbox_cmdline_cb_write(struct sandbox_state *state,
316 const char *arg)
317{
318 state->write_state = true;
319 return 0;
320}
321SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
322
323static 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}
329SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
330 "Ignore missing state on read");
331
Simon Glassb9ddbf42014-02-27 13:26:19 -0700332static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
333 const char *arg)
334{
335 state->show_lcd = true;
336 return 0;
337}
338SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
339 "Show the sandbox LCD display");
340
Simon Glassf91de0b2020-02-03 07:36:13 -0700341static 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}
348SANDBOX_CMDLINE_OPT_SHORT(double_lcd, 'K', 0,
349 "Double the LCD display size in each direction");
350
Simon Glass678ef472014-02-27 13:26:22 -0700351static const char *term_args[STATE_TERM_COUNT] = {
352 "raw-with-sigs",
353 "raw",
354 "cooked",
355};
356
357static 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}
376SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
377 "Set terminal to raw/cooked mode");
378
Simon Glassfe6d12a2015-11-08 23:47:50 -0700379static int sandbox_cmdline_cb_verbose(struct sandbox_state *state,
380 const char *arg)
381{
382 state->show_test_output = true;
383 return 0;
384}
385SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output");
386
Simon Glass4e9a64d2018-10-01 11:55:11 -0600387static 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}
394SANDBOX_CMDLINE_OPT_SHORT(log_level, 'L', 1,
395 "Set log level (0=panic, 7=debug)");
396
Simon Glassa4e289b2020-10-25 20:38:28 -0600397static int sandbox_cmdline_cb_unittests(struct sandbox_state *state,
398 const char *arg)
399{
400 state->run_unittests = true;
401
402 return 0;
403}
404SANDBOX_CMDLINE_OPT_SHORT(unittests, 'u', 0, "Run unit tests");
405
Simon Glasseff96582020-10-25 20:38:33 -0600406static 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}
413SANDBOX_CMDLINE_OPT_SHORT(select_unittests, 'k', 1, "Select unit tests to run");
414
Simon Glassb78cc9b2021-03-22 18:21:01 +1300415static int sandbox_cmdline_cb_signals(struct sandbox_state *state,
416 const char *arg)
417{
418 state->handle_signals = true;
419
420 return 0;
421}
422SANDBOX_CMDLINE_OPT_SHORT(signals, 'S', 0,
423 "Handle signals (such as SIGSEGV) in sandbox");
424
Simon Glassd8c60172021-07-24 15:14:39 -0600425static 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}
432SANDBOX_CMDLINE_OPT(autoboot_keyed, 0, "Allow keyed autoboot");
433
Simon Glassb94eed52017-03-28 10:27:16 -0600434static void setup_ram_buf(struct sandbox_state *state)
435{
Simon Glass24a284a2018-11-23 21:29:29 -0700436 /* Zero the RAM buffer if we didn't read it, to keep valgrind happy */
Simon Glasscd3705e2019-04-08 13:20:43 -0600437 if (!state->ram_buf_read)
Simon Glass24a284a2018-11-23 21:29:29 -0700438 memset(state->ram_buf, '\0', state->ram_size);
Simon Glass24a284a2018-11-23 21:29:29 -0700439
Simon Glassb94eed52017-03-28 10:27:16 -0600440 gd->arch.ram_buf = state->ram_buf;
441 gd->ram_size = state->ram_size;
442}
443
Simon Glass46508c92018-11-15 18:44:03 -0700444void state_show(struct sandbox_state *state)
445{
446 char **p;
447
448 printf("Arguments:\n");
449 for (p = state->argv; *p; p++)
450 printf("%s ", *p);
451 printf("\n");
452}
453
Heinrich Schuchardt43eb8722020-12-02 16:22:11 +0100454void __efi_runtime EFIAPI efi_reset_system(
455 enum efi_reset_type reset_type,
456 efi_status_t reset_status,
457 unsigned long data_size, void *reset_data)
458{
Heinrich Schuchardt7fb72542021-11-20 14:49:18 +0100459 if (reset_type == EFI_RESET_SHUTDOWN)
460 sandbox_exit();
461 else
462 sandbox_reset();
Heinrich Schuchardt43eb8722020-12-02 16:22:11 +0100463}
464
Heinrich Schuchardt1c678442020-10-27 20:29:25 +0100465void sandbox_reset(void)
466{
467 /* Do this here while it still has an effect */
468 os_fd_restore();
469 if (state_uninit())
470 os_exit(2);
471
Heinrich Schuchardt1c678442020-10-27 20:29:25 +0100472 /* Restart U-Boot */
473 os_relaunch(os_argv);
474}
475
Andrew Scullca5d1372022-05-30 10:00:10 +0000476int sandbox_main(int argc, char *argv[])
Simon Glass5ba8ef62011-10-03 19:26:45 +0000477{
Simon Glass8a3e0352012-02-15 15:51:16 -0800478 struct sandbox_state *state;
Heinrich Schuchardtb8b92372021-05-15 19:29:13 +0200479 void * text_base;
Simon Glassc395fdf2014-07-10 22:23:27 -0600480 gd_t data;
Simon Glassedd094e2021-02-06 09:57:33 -0700481 int size;
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700482 int ret;
Simon Glass20bf89a2012-02-15 15:51:15 -0800483
Heinrich Schuchardtb8b92372021-05-15 19:29:13 +0200484 text_base = os_find_text_base();
485
Heinrich Schuchardt1c678442020-10-27 20:29:25 +0100486 /*
Heinrich Schuchardte6d24be2021-05-11 21:03:16 +0200487 * This must be the first invocation of os_malloc() to have
488 * state->ram_buf in the low 4 GiB.
489 */
490 ret = state_init();
491 if (ret)
492 goto err;
493
494 /*
Heinrich Schuchardt1c678442020-10-27 20:29:25 +0100495 * Copy argv[] so that we can pass the arguments in the original
496 * sequence when resetting the sandbox.
497 */
Simon Glassedd094e2021-02-06 09:57:33 -0700498 size = sizeof(char *) * (argc + 1);
499 os_argv = os_malloc(size);
Heinrich Schuchardt1c678442020-10-27 20:29:25 +0100500 if (!os_argv)
501 os_exit(1);
Simon Glassedd094e2021-02-06 09:57:33 -0700502 memcpy(os_argv, argv, size);
Heinrich Schuchardt1c678442020-10-27 20:29:25 +0100503
Simon Glass752707a2019-04-08 13:20:41 -0600504 memset(&data, '\0', sizeof(data));
505 gd = &data;
Heinrich Schuchardtb8b92372021-05-15 19:29:13 +0200506 gd->arch.text_base = text_base;
Simon Glass752707a2019-04-08 13:20:41 -0600507
Simon Glass8a3e0352012-02-15 15:51:16 -0800508 state = state_get_current();
509 if (os_parse_args(state, argc, argv))
510 return 1;
511
Simon Glass22c793a2023-09-26 08:14:47 -0600512 if (state->ram_buf_fname) {
513 ret = os_read_ram_buf(state->ram_buf_fname);
514 if (ret) {
515 printf("Failed to read RAM buffer '%s': %d\n",
516 state->ram_buf_fname, ret);
517 } else {
518 state->ram_buf_read = true;
519 log_debug("Read RAM buffer from '%s'\n", state->ram_buf_fname);
520 }
521 }
522
Patrick Delaunay6daf9052020-11-20 09:48:33 +0100523 /* Remove old memory file if required */
524 if (state->ram_buf_rm && state->ram_buf_fname) {
525 os_unlink(state->ram_buf_fname);
526 state->write_ram_buf = false;
527 state->ram_buf_fname = NULL;
528 }
529
Simon Glass27191f12023-09-26 08:14:49 -0600530 if (state->read_state && state->state_fname) {
531 ret = sandbox_read_state(state, state->state_fname);
532 if (ret)
533 goto err;
534 }
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700535
Simon Glassb78cc9b2021-03-22 18:21:01 +1300536 if (state->handle_signals) {
537 ret = os_setup_signal_handlers();
538 if (ret)
539 goto err;
540 }
Heinrich Schuchardt28eb5092020-11-12 00:29:56 +0100541
Simon Glassadad2d02023-09-26 08:14:27 -0600542#if CONFIG_IS_ENABLED(SYS_MALLOC_F)
Tom Rinifb52b942022-12-04 10:04:49 -0500543 gd->malloc_base = CFG_MALLOC_F_ADDR;
Simon Glass0cc6f5c2014-07-10 22:23:31 -0600544#endif
Simon Glass4e9a64d2018-10-01 11:55:11 -0600545#if CONFIG_IS_ENABLED(LOG)
546 gd->default_log_level = state->default_log_level;
547#endif
Simon Glassb94eed52017-03-28 10:27:16 -0600548 setup_ram_buf(state);
Simon Glassc395fdf2014-07-10 22:23:27 -0600549
Simon Glass752707a2019-04-08 13:20:41 -0600550 /*
551 * Set up the relocation offset here, since sandbox symbols are always
552 * relocated by the OS before sandbox is entered.
553 */
554 gd->reloc_off = (ulong)gd->arch.text_base;
555
Patrick Delaunay053156d2020-11-27 11:20:55 +0100556 /* sandbox test: log functions called before log_init in board_init_f */
Patrick Delaunay053156d2020-11-27 11:20:55 +0100557 log_debug("debug: %s\n", __func__);
558
Simon Glass2fd34e62013-11-10 10:26:59 -0700559 /* Do pre- and post-relocation init */
Simon Glass5ba8ef62011-10-03 19:26:45 +0000560 board_init_f(0);
Allen Martin074b4552013-01-22 13:11:21 +0000561
Simon Glass2fd34e62013-11-10 10:26:59 -0700562 board_init_r(gd->new_gd, 0);
563
564 /* NOTREACHED - board_init_r() does not return */
Allen Martin074b4552013-01-22 13:11:21 +0000565 return 0;
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700566
567err:
568 printf("Error %d\n", ret);
569 return 1;
Simon Glass5ba8ef62011-10-03 19:26:45 +0000570}