blob: 09e3d10d6a502f2e6d85a31e9a2182df9d1f4930 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse961a662016-07-04 11:57:51 -06002/*
3 * Copyright (c) 2016 Google, Inc
Simon Glasse961a662016-07-04 11:57:51 -06004 */
5
6#include <common.h>
7#include <dm.h>
Simon Glassdb41d652019-12-28 10:45:07 -07008#include <hang.h>
Simon Glassacc874a2022-02-28 15:13:46 -07009#include <handoff.h>
Simon Glass691d7192020-05-10 11:40:02 -060010#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Simon Glasse961a662016-07-04 11:57:51 -060012#include <os.h>
Simon Glassa091a8f2016-07-04 11:57:55 -060013#include <spl.h>
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <asm/global_data.h>
Simon Glasse961a662016-07-04 11:57:51 -060015#include <asm/spl.h>
16#include <asm/state.h>
Simon Glass84823562021-03-07 17:35:12 -070017#include <test/ut.h>
Simon Glasse961a662016-07-04 11:57:51 -060018
19DECLARE_GLOBAL_DATA_PTR;
20
Simon Glassf178beb2021-07-05 16:32:45 -060021int sandbox_find_next_phase(char *fname, int maxlen, bool use_img)
22{
23 const char *cur_prefix, *next_prefix;
24 int ret;
25
26 cur_prefix = spl_phase_prefix(spl_phase());
27 next_prefix = spl_phase_prefix(spl_next_phase());
28 ret = os_find_u_boot(fname, maxlen, use_img, cur_prefix, next_prefix);
29 if (ret)
30 return log_msg_ret("find", ret);
31
32 return 0;
33}
34
Simon Glass9ae25b92022-04-30 00:56:54 -060035/* SPL / TPL / VPL init function */
Simon Glasse961a662016-07-04 11:57:51 -060036void board_init_f(ulong flag)
37{
38 struct sandbox_state *state = state_get_current();
Simon Glass9ae25b92022-04-30 00:56:54 -060039 int ret;
Simon Glasse961a662016-07-04 11:57:51 -060040
41 gd->arch.ram_buf = state->ram_buf;
42 gd->ram_size = state->ram_size;
Simon Glass9ae25b92022-04-30 00:56:54 -060043
44 ret = spl_early_init();
45 if (ret) {
46 debug("spl_early_init() failed: %d\n", ret);
47 hang();
48 }
49 preloader_console_init();
Simon Glasse961a662016-07-04 11:57:51 -060050}
51
Simon Glass830690d2022-10-20 18:23:01 -060052void board_boot_order(u32 *spl_boot_list)
Simon Glasse961a662016-07-04 11:57:51 -060053{
Simon Glassd2b22ae2022-10-20 18:23:10 -060054 spl_boot_list[0] = BOOT_DEVICE_VBE;
55 spl_boot_list[1] = BOOT_DEVICE_BOARD;
Simon Glasse961a662016-07-04 11:57:51 -060056}
57
Simon Glass830690d2022-10-20 18:23:01 -060058static int spl_board_load_file(struct spl_image_info *spl_image,
59 struct spl_boot_device *bootdev)
Simon Glasse961a662016-07-04 11:57:51 -060060{
61 char fname[256];
62 int ret;
63
Simon Glassf178beb2021-07-05 16:32:45 -060064 ret = sandbox_find_next_phase(fname, sizeof(fname), false);
Simon Glassf831b8e2016-11-30 15:30:56 -070065 if (ret) {
66 printf("(%s not found, error %d)\n", fname, ret);
Simon Glasse961a662016-07-04 11:57:51 -060067 return ret;
Simon Glassf831b8e2016-11-30 15:30:56 -070068 }
Simon Glasse961a662016-07-04 11:57:51 -060069
Simon Glassb308d9f2021-02-06 09:57:33 -070070 /*
71 * Set up spl_image to boot from jump_to_image_no_args(). Allocate this
72 * outsdide the RAM buffer (i.e. don't use strdup()).
73 */
74 spl_image->arg = os_malloc(strlen(fname) + 1);
Simon Glass27028f12018-11-15 18:44:08 -070075 if (!spl_image->arg)
Simon Glassb308d9f2021-02-06 09:57:33 -070076 return log_msg_ret("exec", -ENOMEM);
77 strcpy(spl_image->arg, fname);
Simon Glass830690d2022-10-20 18:23:01 -060078 spl_image->flags = SPL_SANDBOXF_ARG_IS_FNAME;
Simon Glass27028f12018-11-15 18:44:08 -070079
80 return 0;
Simon Glasse961a662016-07-04 11:57:51 -060081}
Simon Glassf1459c32022-10-20 18:23:08 -060082SPL_LOAD_IMAGE_METHOD("sandbox_file", 9, BOOT_DEVICE_BOARD,
83 spl_board_load_file);
84
85static int load_from_image(struct spl_image_info *spl_image,
86 struct spl_boot_device *bootdev)
87{
88 struct sandbox_state *state = state_get_current();
89 enum u_boot_phase next_phase;
90 const char *fname;
91 ulong pos, size;
92 int full_size;
93 void *buf;
94 int ret;
95
96 if (!IS_ENABLED(CONFIG_SANDBOX_VPL))
97 return -ENOENT;
98
99 next_phase = spl_next_phase();
100 pos = spl_get_image_pos();
101 size = spl_get_image_size();
102 if (pos == BINMAN_SYM_MISSING || size == BINMAN_SYM_MISSING) {
103 log_debug("No image found\n");
104 return -ENOENT;
105 }
106 log_info("Reading from pos %lx size %lx\n", pos, size);
107
108 /*
109 * Set up spl_image to boot from jump_to_image_no_args(). Allocate this
110 * outside the RAM buffer (i.e. don't use strdup()).
111 */
112 fname = state->prog_fname ? state->prog_fname : state->argv[0];
113 ret = os_read_file(fname, &buf, &full_size);
114 if (ret)
115 return log_msg_ret("rd", -ENOMEM);
116 spl_image->flags = SPL_SANDBOXF_ARG_IS_BUF;
117 spl_image->arg = buf;
118 spl_image->offset = pos;
119 spl_image->size = size;
120
121 return 0;
122}
123SPL_LOAD_IMAGE_METHOD("sandbox_image", 7, BOOT_DEVICE_BOARD, load_from_image);
Simon Glassa091a8f2016-07-04 11:57:55 -0600124
125void spl_board_init(void)
126{
Simon Glass1ca910b2018-11-15 18:44:01 -0700127 struct sandbox_state *state = state_get_current();
Simon Glass1ca910b2018-11-15 18:44:01 -0700128
Simon Glassb25ff5c2020-10-25 20:38:28 -0600129 if (state->run_unittests) {
Simon Glass84823562021-03-07 17:35:12 -0700130 struct unit_test *tests = UNIT_TEST_ALL_START();
131 const int count = UNIT_TEST_ALL_COUNT();
Simon Glassb25ff5c2020-10-25 20:38:28 -0600132 int ret;
133
Simon Glass84823562021-03-07 17:35:12 -0700134 ret = ut_run_list("spl", NULL, tests, count,
Simon Glassd1b46592022-10-29 19:47:13 -0600135 state->select_unittests, 1, false, NULL);
Simon Glassb25ff5c2020-10-25 20:38:28 -0600136 /* continue execution into U-Boot */
137 }
Simon Glassa091a8f2016-07-04 11:57:55 -0600138}
Simon Glass27028f12018-11-15 18:44:08 -0700139
140void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
141{
Simon Glass830690d2022-10-20 18:23:01 -0600142 switch (spl_image->flags) {
143 case SPL_SANDBOXF_ARG_IS_FNAME: {
144 const char *fname = spl_image->arg;
Simon Glass27028f12018-11-15 18:44:08 -0700145
Simon Glass830690d2022-10-20 18:23:01 -0600146 if (fname) {
147 os_fd_restore();
148 os_spl_to_uboot(fname);
149 } else {
150 log_err("No filename provided for U-Boot\n");
151 }
152 break;
153 }
Simon Glassf1459c32022-10-20 18:23:08 -0600154 case SPL_SANDBOXF_ARG_IS_BUF: {
155 int ret;
156
157 ret = os_jump_to_image(spl_image->arg + spl_image->offset,
158 spl_image->size);
159 if (ret)
160 log_err("Failed to load image\n");
161 break;
162 }
Simon Glass830690d2022-10-20 18:23:01 -0600163 default:
164 log_err("Invalid flags\n");
165 break;
Simon Glass12efc932018-11-23 21:29:25 -0700166 }
Simon Glass27028f12018-11-15 18:44:08 -0700167 hang();
168}
Simon Glass366291a2019-09-25 08:11:18 -0600169
170int handoff_arch_save(struct spl_handoff *ho)
171{
172 ho->arch.magic = TEST_HANDOFF_MAGIC;
173
174 return 0;
175}