blob: 9ad9da686c6ab02d281af87f6402052e92238e64 [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 Glassa7e2d4d2016-07-04 11:57:51 -06006#include <dm.h>
Simon Glassf11478f2019-12-28 10:45:07 -07007#include <hang.h>
Simon Glass9ee7b732022-02-28 15:13:46 -07008#include <handoff.h>
Simon Glass97589732020-05-10 11:40:02 -06009#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glassa7e2d4d2016-07-04 11:57:51 -060011#include <os.h>
Simon Glass4e9c1312016-07-04 11:57:55 -060012#include <spl.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Simon Glassa7e2d4d2016-07-04 11:57:51 -060014#include <asm/spl.h>
15#include <asm/state.h>
Simon Glass1ef74ab2021-03-07 17:35:12 -070016#include <test/ut.h>
Simon Glassa7e2d4d2016-07-04 11:57:51 -060017
18DECLARE_GLOBAL_DATA_PTR;
19
Simon Glass1cd06002021-07-05 16:32:45 -060020int sandbox_find_next_phase(char *fname, int maxlen, bool use_img)
21{
22 const char *cur_prefix, *next_prefix;
23 int ret;
24
25 cur_prefix = spl_phase_prefix(spl_phase());
26 next_prefix = spl_phase_prefix(spl_next_phase());
27 ret = os_find_u_boot(fname, maxlen, use_img, cur_prefix, next_prefix);
28 if (ret)
29 return log_msg_ret("find", ret);
30
31 return 0;
32}
33
Simon Glassc86e6202022-04-30 00:56:54 -060034/* SPL / TPL / VPL init function */
Simon Glassa7e2d4d2016-07-04 11:57:51 -060035void board_init_f(ulong flag)
36{
37 struct sandbox_state *state = state_get_current();
Simon Glassc86e6202022-04-30 00:56:54 -060038 int ret;
Simon Glassa7e2d4d2016-07-04 11:57:51 -060039
40 gd->arch.ram_buf = state->ram_buf;
41 gd->ram_size = state->ram_size;
Simon Glassc86e6202022-04-30 00:56:54 -060042
43 ret = spl_early_init();
44 if (ret) {
45 debug("spl_early_init() failed: %d\n", ret);
46 hang();
47 }
48 preloader_console_init();
Simon Glassa7e2d4d2016-07-04 11:57:51 -060049}
50
Simon Glass4aa6a9b2022-10-20 18:23:01 -060051void board_boot_order(u32 *spl_boot_list)
Simon Glassa7e2d4d2016-07-04 11:57:51 -060052{
Simon Glassd78aa752022-10-20 18:23:10 -060053 spl_boot_list[0] = BOOT_DEVICE_VBE;
54 spl_boot_list[1] = BOOT_DEVICE_BOARD;
Simon Glassa7e2d4d2016-07-04 11:57:51 -060055}
56
Simon Glass4aa6a9b2022-10-20 18:23:01 -060057static int spl_board_load_file(struct spl_image_info *spl_image,
58 struct spl_boot_device *bootdev)
Simon Glassa7e2d4d2016-07-04 11:57:51 -060059{
60 char fname[256];
61 int ret;
62
Simon Glass1cd06002021-07-05 16:32:45 -060063 ret = sandbox_find_next_phase(fname, sizeof(fname), false);
Simon Glasse8845d22016-11-30 15:30:56 -070064 if (ret) {
65 printf("(%s not found, error %d)\n", fname, ret);
Simon Glassa7e2d4d2016-07-04 11:57:51 -060066 return ret;
Simon Glasse8845d22016-11-30 15:30:56 -070067 }
Simon Glassa7e2d4d2016-07-04 11:57:51 -060068
Simon Glassedd094e2021-02-06 09:57:33 -070069 /*
70 * Set up spl_image to boot from jump_to_image_no_args(). Allocate this
71 * outsdide the RAM buffer (i.e. don't use strdup()).
72 */
73 spl_image->arg = os_malloc(strlen(fname) + 1);
Simon Glasscca25522018-11-15 18:44:08 -070074 if (!spl_image->arg)
Simon Glassedd094e2021-02-06 09:57:33 -070075 return log_msg_ret("exec", -ENOMEM);
76 strcpy(spl_image->arg, fname);
Simon Glass4aa6a9b2022-10-20 18:23:01 -060077 spl_image->flags = SPL_SANDBOXF_ARG_IS_FNAME;
Simon Glasscca25522018-11-15 18:44:08 -070078
79 return 0;
Simon Glassa7e2d4d2016-07-04 11:57:51 -060080}
Simon Glassa87cd0c2022-10-20 18:23:08 -060081SPL_LOAD_IMAGE_METHOD("sandbox_file", 9, BOOT_DEVICE_BOARD,
82 spl_board_load_file);
83
84static int load_from_image(struct spl_image_info *spl_image,
85 struct spl_boot_device *bootdev)
86{
87 struct sandbox_state *state = state_get_current();
88 enum u_boot_phase next_phase;
89 const char *fname;
90 ulong pos, size;
91 int full_size;
92 void *buf;
93 int ret;
94
95 if (!IS_ENABLED(CONFIG_SANDBOX_VPL))
96 return -ENOENT;
97
98 next_phase = spl_next_phase();
99 pos = spl_get_image_pos();
100 size = spl_get_image_size();
101 if (pos == BINMAN_SYM_MISSING || size == BINMAN_SYM_MISSING) {
102 log_debug("No image found\n");
103 return -ENOENT;
104 }
105 log_info("Reading from pos %lx size %lx\n", pos, size);
106
107 /*
108 * Set up spl_image to boot from jump_to_image_no_args(). Allocate this
109 * outside the RAM buffer (i.e. don't use strdup()).
110 */
111 fname = state->prog_fname ? state->prog_fname : state->argv[0];
112 ret = os_read_file(fname, &buf, &full_size);
113 if (ret)
114 return log_msg_ret("rd", -ENOMEM);
115 spl_image->flags = SPL_SANDBOXF_ARG_IS_BUF;
116 spl_image->arg = buf;
117 spl_image->offset = pos;
118 spl_image->size = size;
119
120 return 0;
121}
122SPL_LOAD_IMAGE_METHOD("sandbox_image", 7, BOOT_DEVICE_BOARD, load_from_image);
Simon Glass4e9c1312016-07-04 11:57:55 -0600123
124void spl_board_init(void)
125{
Simon Glassb5dfea82018-11-15 18:44:01 -0700126 struct sandbox_state *state = state_get_current();
Simon Glassb5dfea82018-11-15 18:44:01 -0700127
Heinrich Schuchardt0b465d62023-10-03 02:59:46 +0200128 if (!CONFIG_IS_ENABLED(UNIT_TEST))
129 return;
130
Sean Andersonc4f86c02023-10-14 16:48:03 -0400131 /* These are necessary so TFTP can use LMBs to check its load address */
132 gd->bd->bi_dram[0].start = gd->ram_base;
133 gd->bd->bi_dram[0].size = get_effective_memsize();
134
Simon Glassa4e289b2020-10-25 20:38:28 -0600135 if (state->run_unittests) {
Simon Glass1ef74ab2021-03-07 17:35:12 -0700136 struct unit_test *tests = UNIT_TEST_ALL_START();
137 const int count = UNIT_TEST_ALL_COUNT();
Simon Glassa4e289b2020-10-25 20:38:28 -0600138 int ret;
139
Simon Glass1ef74ab2021-03-07 17:35:12 -0700140 ret = ut_run_list("spl", NULL, tests, count,
Simon Glass85ba7c32022-10-29 19:47:13 -0600141 state->select_unittests, 1, false, NULL);
Simon Glassa4e289b2020-10-25 20:38:28 -0600142 /* continue execution into U-Boot */
143 }
Simon Glass4e9c1312016-07-04 11:57:55 -0600144}
Simon Glasscca25522018-11-15 18:44:08 -0700145
146void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
147{
Simon Glass4aa6a9b2022-10-20 18:23:01 -0600148 switch (spl_image->flags) {
149 case SPL_SANDBOXF_ARG_IS_FNAME: {
150 const char *fname = spl_image->arg;
Simon Glasscca25522018-11-15 18:44:08 -0700151
Simon Glass4aa6a9b2022-10-20 18:23:01 -0600152 if (fname) {
153 os_fd_restore();
154 os_spl_to_uboot(fname);
155 } else {
156 log_err("No filename provided for U-Boot\n");
157 }
158 break;
159 }
Simon Glassa87cd0c2022-10-20 18:23:08 -0600160 case SPL_SANDBOXF_ARG_IS_BUF: {
161 int ret;
162
163 ret = os_jump_to_image(spl_image->arg + spl_image->offset,
164 spl_image->size);
165 if (ret)
166 log_err("Failed to load image\n");
167 break;
168 }
Simon Glass4aa6a9b2022-10-20 18:23:01 -0600169 default:
170 log_err("Invalid flags\n");
171 break;
Simon Glass38c2eae2018-11-23 21:29:25 -0700172 }
Simon Glasscca25522018-11-15 18:44:08 -0700173 hang();
174}
Simon Glassc5d27202019-09-25 08:11:18 -0600175
176int handoff_arch_save(struct spl_handoff *ho)
177{
178 ho->arch.magic = TEST_HANDOFF_MAGIC;
179
180 return 0;
181}