blob: cc46965b73b06226320fe39e17448307c859ddc4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassa7e2d4d2016-07-04 11:57:51 -06002/*
3 * Copyright (c) 2016 Google, Inc
Simon Glassa7e2d4d2016-07-04 11:57:51 -06004 */
5
Simon Glass6056e5a2024-08-07 16:47:38 -06006#define LOG_CATEGORY LOGC_BOOT
7
Simon Glassa7e2d4d2016-07-04 11:57:51 -06008#include <dm.h>
Simon Glassf11478f2019-12-28 10:45:07 -07009#include <hang.h>
Simon Glass9ee7b732022-02-28 15:13:46 -070010#include <handoff.h>
Simon Glass36611152024-08-07 16:47:21 -060011#include <image.h>
Simon Glass97589732020-05-10 11:40:02 -060012#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass6056e5a2024-08-07 16:47:38 -060014#include <mapmem.h>
Simon Glassa7e2d4d2016-07-04 11:57:51 -060015#include <os.h>
Simon Glass4e9c1312016-07-04 11:57:55 -060016#include <spl.h>
Simon Glass6056e5a2024-08-07 16:47:38 -060017#include <upl.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
Simon Glassa7e2d4d2016-07-04 11:57:51 -060019#include <asm/spl.h>
20#include <asm/state.h>
Simon Glass1ef74ab2021-03-07 17:35:12 -070021#include <test/ut.h>
Simon Glassa7e2d4d2016-07-04 11:57:51 -060022
23DECLARE_GLOBAL_DATA_PTR;
24
Simon Glass1cd06002021-07-05 16:32:45 -060025int sandbox_find_next_phase(char *fname, int maxlen, bool use_img)
26{
27 const char *cur_prefix, *next_prefix;
28 int ret;
29
30 cur_prefix = spl_phase_prefix(spl_phase());
31 next_prefix = spl_phase_prefix(spl_next_phase());
32 ret = os_find_u_boot(fname, maxlen, use_img, cur_prefix, next_prefix);
33 if (ret)
34 return log_msg_ret("find", ret);
35
36 return 0;
37}
38
Simon Glassc86e6202022-04-30 00:56:54 -060039/* SPL / TPL / VPL init function */
Simon Glassa7e2d4d2016-07-04 11:57:51 -060040void board_init_f(ulong flag)
41{
42 struct sandbox_state *state = state_get_current();
Simon Glassc86e6202022-04-30 00:56:54 -060043 int ret;
Simon Glassa7e2d4d2016-07-04 11:57:51 -060044
45 gd->arch.ram_buf = state->ram_buf;
46 gd->ram_size = state->ram_size;
Simon Glassc86e6202022-04-30 00:56:54 -060047
48 ret = spl_early_init();
49 if (ret) {
50 debug("spl_early_init() failed: %d\n", ret);
51 hang();
52 }
53 preloader_console_init();
Simon Glassa7e2d4d2016-07-04 11:57:51 -060054}
55
Simon Glass4aa6a9b2022-10-20 18:23:01 -060056void board_boot_order(u32 *spl_boot_list)
Simon Glassa7e2d4d2016-07-04 11:57:51 -060057{
Simon Glassd78aa752022-10-20 18:23:10 -060058 spl_boot_list[0] = BOOT_DEVICE_VBE;
Simon Glass6056e5a2024-08-07 16:47:38 -060059 spl_boot_list[1] = BOOT_DEVICE_UPL;
60 spl_boot_list[2] = BOOT_DEVICE_BOARD;
Simon Glassa7e2d4d2016-07-04 11:57:51 -060061}
62
Simon Glass4aa6a9b2022-10-20 18:23:01 -060063static int spl_board_load_file(struct spl_image_info *spl_image,
64 struct spl_boot_device *bootdev)
Simon Glassa7e2d4d2016-07-04 11:57:51 -060065{
66 char fname[256];
67 int ret;
68
Simon Glass1cd06002021-07-05 16:32:45 -060069 ret = sandbox_find_next_phase(fname, sizeof(fname), false);
Simon Glasse8845d22016-11-30 15:30:56 -070070 if (ret) {
71 printf("(%s not found, error %d)\n", fname, ret);
Simon Glassa7e2d4d2016-07-04 11:57:51 -060072 return ret;
Simon Glasse8845d22016-11-30 15:30:56 -070073 }
Simon Glassa7e2d4d2016-07-04 11:57:51 -060074
Simon Glassedd094e2021-02-06 09:57:33 -070075 /*
76 * Set up spl_image to boot from jump_to_image_no_args(). Allocate this
77 * outsdide the RAM buffer (i.e. don't use strdup()).
78 */
79 spl_image->arg = os_malloc(strlen(fname) + 1);
Simon Glasscca25522018-11-15 18:44:08 -070080 if (!spl_image->arg)
Simon Glassedd094e2021-02-06 09:57:33 -070081 return log_msg_ret("exec", -ENOMEM);
82 strcpy(spl_image->arg, fname);
Simon Glass4aa6a9b2022-10-20 18:23:01 -060083 spl_image->flags = SPL_SANDBOXF_ARG_IS_FNAME;
Simon Glasscca25522018-11-15 18:44:08 -070084
85 return 0;
Simon Glassa7e2d4d2016-07-04 11:57:51 -060086}
Simon Glassa87cd0c2022-10-20 18:23:08 -060087SPL_LOAD_IMAGE_METHOD("sandbox_file", 9, BOOT_DEVICE_BOARD,
88 spl_board_load_file);
89
90static int load_from_image(struct spl_image_info *spl_image,
91 struct spl_boot_device *bootdev)
92{
93 struct sandbox_state *state = state_get_current();
94 enum u_boot_phase next_phase;
95 const char *fname;
96 ulong pos, size;
97 int full_size;
98 void *buf;
99 int ret;
100
101 if (!IS_ENABLED(CONFIG_SANDBOX_VPL))
102 return -ENOENT;
103
104 next_phase = spl_next_phase();
105 pos = spl_get_image_pos();
106 size = spl_get_image_size();
107 if (pos == BINMAN_SYM_MISSING || size == BINMAN_SYM_MISSING) {
108 log_debug("No image found\n");
109 return -ENOENT;
110 }
111 log_info("Reading from pos %lx size %lx\n", pos, size);
112
113 /*
114 * Set up spl_image to boot from jump_to_image_no_args(). Allocate this
115 * outside the RAM buffer (i.e. don't use strdup()).
116 */
117 fname = state->prog_fname ? state->prog_fname : state->argv[0];
118 ret = os_read_file(fname, &buf, &full_size);
119 if (ret)
120 return log_msg_ret("rd", -ENOMEM);
121 spl_image->flags = SPL_SANDBOXF_ARG_IS_BUF;
122 spl_image->arg = buf;
123 spl_image->offset = pos;
124 spl_image->size = size;
125
126 return 0;
127}
128SPL_LOAD_IMAGE_METHOD("sandbox_image", 7, BOOT_DEVICE_BOARD, load_from_image);
Simon Glass4e9c1312016-07-04 11:57:55 -0600129
130void spl_board_init(void)
131{
Simon Glassb5dfea82018-11-15 18:44:01 -0700132 struct sandbox_state *state = state_get_current();
Simon Glassb5dfea82018-11-15 18:44:01 -0700133
Heinrich Schuchardt0b465d62023-10-03 02:59:46 +0200134 if (!CONFIG_IS_ENABLED(UNIT_TEST))
135 return;
136
Sean Andersonc4f86c02023-10-14 16:48:03 -0400137 /* These are necessary so TFTP can use LMBs to check its load address */
138 gd->bd->bi_dram[0].start = gd->ram_base;
139 gd->bd->bi_dram[0].size = get_effective_memsize();
140
Simon Glassa4e289b2020-10-25 20:38:28 -0600141 if (state->run_unittests) {
Simon Glass1ef74ab2021-03-07 17:35:12 -0700142 struct unit_test *tests = UNIT_TEST_ALL_START();
143 const int count = UNIT_TEST_ALL_COUNT();
Simon Glassa4e289b2020-10-25 20:38:28 -0600144 int ret;
145
Simon Glass1ef74ab2021-03-07 17:35:12 -0700146 ret = ut_run_list("spl", NULL, tests, count,
Simon Glass85ba7c32022-10-29 19:47:13 -0600147 state->select_unittests, 1, false, NULL);
Simon Glassa4e289b2020-10-25 20:38:28 -0600148 /* continue execution into U-Boot */
149 }
Simon Glass4e9c1312016-07-04 11:57:55 -0600150}
Simon Glasscca25522018-11-15 18:44:08 -0700151
152void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
153{
Simon Glass4aa6a9b2022-10-20 18:23:01 -0600154 switch (spl_image->flags) {
155 case SPL_SANDBOXF_ARG_IS_FNAME: {
156 const char *fname = spl_image->arg;
Simon Glasscca25522018-11-15 18:44:08 -0700157
Simon Glass4aa6a9b2022-10-20 18:23:01 -0600158 if (fname) {
159 os_fd_restore();
160 os_spl_to_uboot(fname);
161 } else {
162 log_err("No filename provided for U-Boot\n");
163 }
164 break;
165 }
Simon Glassa87cd0c2022-10-20 18:23:08 -0600166 case SPL_SANDBOXF_ARG_IS_BUF: {
167 int ret;
168
169 ret = os_jump_to_image(spl_image->arg + spl_image->offset,
170 spl_image->size);
171 if (ret)
172 log_err("Failed to load image\n");
173 break;
174 }
Simon Glass4aa6a9b2022-10-20 18:23:01 -0600175 default:
176 log_err("Invalid flags\n");
177 break;
Simon Glass38c2eae2018-11-23 21:29:25 -0700178 }
Simon Glasscca25522018-11-15 18:44:08 -0700179 hang();
180}
Simon Glassc5d27202019-09-25 08:11:18 -0600181
182int handoff_arch_save(struct spl_handoff *ho)
183{
184 ho->arch.magic = TEST_HANDOFF_MAGIC;
185
186 return 0;
187}
Simon Glass36611152024-08-07 16:47:21 -0600188
189/* Context used to hold file descriptor */
190struct load_ctx {
191 int fd;
192};
193
194static ulong read_fit_image(struct spl_load_info *load, ulong offset,
195 ulong size, void *buf)
196{
197 struct load_ctx *load_ctx = load->priv;
198 off_t ret;
199 ssize_t res;
200
201 ret = os_lseek(load_ctx->fd, offset, OS_SEEK_SET);
202 if (ret < 0) {
Simon Glass3dbd4ae2024-08-07 16:47:24 -0600203 printf("Failed to seek to %zx, got %zx\n", offset, ret);
204 return log_msg_ret("lse", ret);
Simon Glass36611152024-08-07 16:47:21 -0600205 }
206
207 res = os_read(load_ctx->fd, buf, size);
208 if (res < 0) {
Simon Glass3dbd4ae2024-08-07 16:47:24 -0600209 printf("Failed to read %lx bytes, got %ld\n", size, res);
210 return log_msg_ret("osr", res);
Simon Glass36611152024-08-07 16:47:21 -0600211 }
212
213 return size;
214}
215
216int sandbox_spl_load_fit(char *fname, int maxlen, struct spl_image_info *image)
217{
218 struct legacy_img_hdr *header;
219 struct load_ctx load_ctx;
220 struct spl_load_info load;
221 int ret;
222 int fd;
223
Simon Glasse8066862024-08-22 07:55:02 -0600224 spl_load_init(&load, read_fit_image, &load_ctx,
225 IS_ENABLED(CONFIG_SPL_LOAD_BLOCK) ? 512 : 1);
Simon Glass36611152024-08-07 16:47:21 -0600226
227 ret = sandbox_find_next_phase(fname, maxlen, true);
228 if (ret) {
229 printf("%s not found, error %d\n", fname, ret);
230 return log_msg_ret("nph", ret);
231 }
232
233 header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
234
235 log_debug("reading from %s\n", fname);
236 fd = os_open(fname, OS_O_RDONLY);
237 if (fd < 0) {
238 printf("Failed to open '%s'\n", fname);
239 return log_msg_ret("ope", -errno);
240 }
241 ret = os_read(fd, header, sizeof(*header));
242 if (ret != sizeof(*header)) {
Simon Glass3dbd4ae2024-08-07 16:47:24 -0600243 printf("Failed to read %lx bytes, got %d\n", sizeof(*header),
244 ret);
245 return log_msg_ret("rea", ret);
Simon Glass36611152024-08-07 16:47:21 -0600246 }
247 load_ctx.fd = fd;
248
249 load.priv = &load_ctx;
250
251 ret = spl_load_simple_fit(image, &load, 0, header);
252 if (ret)
253 return log_msg_ret("slf", ret);
254
255 return 0;
256}
Simon Glass6056e5a2024-08-07 16:47:38 -0600257
258static int upl_load_from_image(struct spl_image_info *spl_image,
259 struct spl_boot_device *bootdev)
260{
261 long long size;
262 char *fname;
263 int ret, fd;
264 ulong addr;
265
266 if (!CONFIG_IS_ENABLED(UPL_OUT))
267 return -ENOTSUPP;
268
269 spl_upl_init();
270 fname = os_malloc(256);
271
272 ret = sandbox_spl_load_fit(fname, 256, spl_image);
273 if (ret)
274 return log_msg_ret("fit", ret);
275 spl_image->flags = SPL_SANDBOXF_ARG_IS_BUF;
276 spl_image->arg = map_sysmem(spl_image->load_addr, 0);
277 /* size is set by load_simple_fit(), offset is left as 0 */
278
279 /* now read the whole FIT into memory */
280 fd = os_open(fname, OS_O_RDONLY);
281 if (fd < 0)
282 return log_msg_ret("op2", -ENOENT);
283 if (os_get_filesize(fname, &size))
284 return log_msg_ret("fis", -ENOENT);
285
286 /* place it after the loaded image, allowing plenty of space */
287 addr = ALIGN(spl_image->load_addr + size, 0x1000);
288 log_debug("Loading whole FIT to %lx\n", addr);
289 if (os_read(fd, map_sysmem(addr, 0), size) != size)
290 return log_msg_ret("rea", -EIO);
291 os_close(fd);
292
293 /* tell UPL where it is */
294 upl_set_fit_addr(addr);
295
296 return 0;
297}
298SPL_LOAD_IMAGE_METHOD("upl", 4, BOOT_DEVICE_UPL, upl_load_from_image);