1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2016 Google, Inc
6 #define LOG_CATEGORY LOGC_BOOT
18 #include <asm/global_data.h>
20 #include <asm/state.h>
23 DECLARE_GLOBAL_DATA_PTR;
25 int sandbox_find_next_phase(char *fname, int maxlen, bool use_img)
27 const char *cur_prefix, *next_prefix;
30 cur_prefix = spl_phase_prefix(xpl_phase());
31 next_prefix = spl_phase_prefix(spl_next_phase());
32 ret = os_find_u_boot(fname, maxlen, use_img, cur_prefix, next_prefix);
34 return log_msg_ret("find", ret);
39 /* SPL / TPL / VPL init function */
40 void board_init_f(ulong flag)
42 struct sandbox_state *state = state_get_current();
45 gd->arch.ram_buf = state->ram_buf;
46 gd->ram_size = state->ram_size;
48 ret = spl_early_init();
50 debug("spl_early_init() failed: %d\n", ret);
53 preloader_console_init();
56 void board_boot_order(u32 *spl_boot_list)
58 spl_boot_list[0] = BOOT_DEVICE_VBE;
59 spl_boot_list[1] = BOOT_DEVICE_UPL;
60 spl_boot_list[2] = BOOT_DEVICE_BOARD;
63 static int spl_board_load_file(struct spl_image_info *spl_image,
64 struct spl_boot_device *bootdev)
69 ret = sandbox_find_next_phase(fname, sizeof(fname), false);
71 printf("(%s not found, error %d)\n", fname, ret);
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()).
79 spl_image->arg = os_malloc(strlen(fname) + 1);
81 return log_msg_ret("exec", -ENOMEM);
82 strcpy(spl_image->arg, fname);
83 spl_image->flags = SPL_SANDBOXF_ARG_IS_FNAME;
87 SPL_LOAD_IMAGE_METHOD("sandbox_file", 9, BOOT_DEVICE_BOARD,
90 static int load_from_image(struct spl_image_info *spl_image,
91 struct spl_boot_device *bootdev)
93 struct sandbox_state *state = state_get_current();
94 enum xpl_phase_t next_phase;
101 if (!IS_ENABLED(CONFIG_SANDBOX_VPL))
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");
111 log_info("Reading from pos %lx size %lx\n", pos, size);
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()).
117 fname = state->prog_fname ? state->prog_fname : state->argv[0];
118 ret = os_read_file(fname, &buf, &full_size);
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;
128 SPL_LOAD_IMAGE_METHOD("sandbox_image", 7, BOOT_DEVICE_BOARD, load_from_image);
130 int dram_init_banksize(void)
132 /* These are necessary so TFTP can use LMBs to check its load address */
133 gd->bd->bi_dram[0].start = gd->ram_base;
134 gd->bd->bi_dram[0].size = get_effective_memsize();
139 void spl_board_init(void)
141 struct sandbox_state *state = state_get_current();
143 if (!CONFIG_IS_ENABLED(UNIT_TEST))
146 if (state->run_unittests) {
147 struct unit_test *tests = UNIT_TEST_ALL_START();
148 const int count = UNIT_TEST_ALL_COUNT();
151 ret = ut_run_list("spl", NULL, tests, count,
152 state->select_unittests, 1, false, NULL);
153 /* continue execution into U-Boot */
157 void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
159 switch (spl_image->flags) {
160 case SPL_SANDBOXF_ARG_IS_FNAME: {
161 const char *fname = spl_image->arg;
165 os_spl_to_uboot(fname);
167 log_err("No filename provided for U-Boot\n");
171 case SPL_SANDBOXF_ARG_IS_BUF: {
174 ret = os_jump_to_image(spl_image->arg + spl_image->offset,
177 log_err("Failed to load image\n");
181 log_err("Invalid flags\n");
187 int handoff_arch_save(struct spl_handoff *ho)
189 ho->arch.magic = TEST_HANDOFF_MAGIC;
194 /* Context used to hold file descriptor */
199 static ulong read_fit_image(struct spl_load_info *load, ulong offset,
200 ulong size, void *buf)
202 struct load_ctx *load_ctx = load->priv;
206 ret = os_lseek(load_ctx->fd, offset, OS_SEEK_SET);
208 printf("Failed to seek to %zx, got %zx\n", offset, ret);
209 return log_msg_ret("lse", ret);
212 res = os_read(load_ctx->fd, buf, size);
214 printf("Failed to read %lx bytes, got %ld\n", size, res);
215 return log_msg_ret("osr", res);
221 int sandbox_spl_load_fit(char *fname, int maxlen, struct spl_image_info *image)
223 struct legacy_img_hdr *header;
224 struct load_ctx load_ctx;
225 struct spl_load_info load;
229 spl_load_init(&load, read_fit_image, &load_ctx,
230 IS_ENABLED(CONFIG_SPL_LOAD_BLOCK) ? 512 : 1);
232 ret = sandbox_find_next_phase(fname, maxlen, true);
234 printf("%s not found, error %d\n", fname, ret);
235 return log_msg_ret("nph", ret);
238 header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
240 log_debug("reading from %s\n", fname);
241 fd = os_open(fname, OS_O_RDONLY);
243 printf("Failed to open '%s'\n", fname);
244 return log_msg_ret("ope", -errno);
246 ret = os_read(fd, header, sizeof(*header));
247 if (ret != sizeof(*header)) {
248 printf("Failed to read %lx bytes, got %d\n", sizeof(*header),
250 return log_msg_ret("rea", ret);
254 load.priv = &load_ctx;
256 ret = spl_load_simple_fit(image, &load, 0, header);
258 return log_msg_ret("slf", ret);
263 static int upl_load_from_image(struct spl_image_info *spl_image,
264 struct spl_boot_device *bootdev)
271 if (!CONFIG_IS_ENABLED(UPL_OUT))
275 fname = os_malloc(256);
277 ret = sandbox_spl_load_fit(fname, 256, spl_image);
279 return log_msg_ret("fit", ret);
280 spl_image->flags = SPL_SANDBOXF_ARG_IS_BUF;
281 spl_image->arg = map_sysmem(spl_image->load_addr, 0);
282 /* size is set by load_simple_fit(), offset is left as 0 */
284 /* now read the whole FIT into memory */
285 fd = os_open(fname, OS_O_RDONLY);
287 return log_msg_ret("op2", -ENOENT);
288 if (os_get_filesize(fname, &size))
289 return log_msg_ret("fis", -ENOENT);
291 /* place it after the loaded image, allowing plenty of space */
292 addr = ALIGN(spl_image->load_addr + size, 0x1000);
293 log_debug("Loading whole FIT to %lx\n", addr);
294 if (os_read(fd, map_sysmem(addr, 0), size) != size)
295 return log_msg_ret("rea", -EIO);
298 /* tell UPL where it is */
299 upl_set_fit_addr(addr);
303 SPL_LOAD_IMAGE_METHOD("upl", 4, BOOT_DEVICE_UPL, upl_load_from_image);