1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2016 Google, Inc
4  */
5 
6 #define LOG_CATEGORY	LOGC_BOOT
7 
8 #include <dm.h>
9 #include <hang.h>
10 #include <handoff.h>
11 #include <image.h>
12 #include <init.h>
13 #include <log.h>
14 #include <mapmem.h>
15 #include <os.h>
16 #include <spl.h>
17 #include <upl.h>
18 #include <asm/global_data.h>
19 #include <asm/spl.h>
20 #include <asm/state.h>
21 #include <test/ut.h>
22 
23 DECLARE_GLOBAL_DATA_PTR;
24 
sandbox_find_next_phase(char * fname,int maxlen,bool use_img)25 int 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 = xpl_prefix(xpl_phase());
31 	next_prefix = xpl_prefix(xpl_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 
39 /* SPL / TPL / VPL init function */
board_init_f(ulong flag)40 void board_init_f(ulong flag)
41 {
42 	struct sandbox_state *state = state_get_current();
43 	int ret;
44 
45 	gd->arch.ram_buf = state->ram_buf;
46 	gd->ram_size = state->ram_size;
47 
48 	ret = spl_early_init();
49 	if (ret) {
50 		debug("spl_early_init() failed: %d\n", ret);
51 		hang();
52 	}
53 	preloader_console_init();
54 }
55 
board_boot_order(u32 * spl_boot_list)56 void board_boot_order(u32 *spl_boot_list)
57 {
58 	struct sandbox_state *state = state_get_current();
59 
60 	spl_boot_list[0] = BOOT_DEVICE_VBE;
61 	spl_boot_list[1] = state->upl ? BOOT_DEVICE_UPL : BOOT_DEVICE_BOARD;
62 }
63 
spl_board_load_file(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)64 static int spl_board_load_file(struct spl_image_info *spl_image,
65 			       struct spl_boot_device *bootdev)
66 {
67 	char fname[256];
68 	int ret;
69 
70 	ret = sandbox_find_next_phase(fname, sizeof(fname), false);
71 	if (ret) {
72 		printf("(%s not found, error %d)\n", fname, ret);
73 		return ret;
74 	}
75 
76 	/*
77 	 * Set up spl_image to boot from jump_to_image(). Allocate this
78 	 * outsdide the RAM buffer (i.e. don't use strdup()).
79 	 */
80 	spl_image->arg = os_malloc(strlen(fname) + 1);
81 	if (!spl_image->arg)
82 		return log_msg_ret("exec", -ENOMEM);
83 	strcpy(spl_image->arg, fname);
84 	spl_image->flags = SPL_SANDBOXF_ARG_IS_FNAME;
85 
86 	return 0;
87 }
88 SPL_LOAD_IMAGE_METHOD("sandbox_file", 9, BOOT_DEVICE_BOARD,
89 		      spl_board_load_file);
90 
load_from_image(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)91 static int load_from_image(struct spl_image_info *spl_image,
92 			   struct spl_boot_device *bootdev)
93 {
94 	struct sandbox_state *state = state_get_current();
95 	enum xpl_phase_t next_phase;
96 	const char *fname;
97 	ulong pos, size;
98 	int full_size;
99 	void *buf;
100 	int ret;
101 
102 	if (!IS_ENABLED(CONFIG_SANDBOX_VPL))
103 		return -ENOENT;
104 
105 	next_phase = xpl_next_phase();
106 	pos = spl_get_image_pos();
107 	size = spl_get_image_size();
108 	if (pos == BINMAN_SYM_MISSING || size == BINMAN_SYM_MISSING) {
109 		log_debug("No image found\n");
110 		return -ENOENT;
111 	}
112 	log_info("Reading from pos %lx size %lx\n", pos, size);
113 
114 	/*
115 	 * Set up spl_image to boot from jump_to_image(). Allocate this
116 	 * outside the RAM buffer (i.e. don't use strdup()).
117 	 */
118 	fname = state->prog_fname ? state->prog_fname : state->argv[0];
119 	ret = os_read_file(fname, &buf, &full_size);
120 	if (ret)
121 		return log_msg_ret("rd", -ENOMEM);
122 	spl_image->flags = SPL_SANDBOXF_ARG_IS_BUF;
123 	spl_image->arg = buf;
124 	spl_image->offset = pos;
125 	spl_image->size = size;
126 
127 	return 0;
128 }
129 SPL_LOAD_IMAGE_METHOD("sandbox_image", 7, BOOT_DEVICE_BOARD, load_from_image);
130 
dram_init_banksize(void)131 int dram_init_banksize(void)
132 {
133 	/* These are necessary so TFTP can use LMBs to check its load address */
134 	gd->bd->bi_dram[0].start = gd->ram_base;
135 	gd->bd->bi_dram[0].size = get_effective_memsize();
136 
137 	return 0;
138 }
139 
spl_board_init(void)140 void spl_board_init(void)
141 {
142 	struct sandbox_state *state = state_get_current();
143 
144 	if (!CONFIG_IS_ENABLED(UNIT_TEST))
145 		return;
146 
147 	if (state->run_unittests) {
148 		struct unit_test *tests = UNIT_TEST_ALL_START();
149 		const int count = UNIT_TEST_ALL_COUNT();
150 		struct unit_test_state uts;
151 		int ret;
152 
153 		ut_init_state(&uts);
154 		ret = ut_run_list(&uts, "spl", NULL, tests, count,
155 				  state->select_unittests, 1, false, NULL);
156 		ut_report(&uts.cur, 1);
157 		ut_uninit_state(&uts);
158 		/* continue execution into U-Boot */
159 	}
160 }
161 
jump_to_image(struct spl_image_info * spl_image)162 void __noreturn jump_to_image(struct spl_image_info *spl_image)
163 {
164 	switch (spl_image->flags) {
165 	case SPL_SANDBOXF_ARG_IS_FNAME: {
166 		const char *fname = spl_image->arg;
167 
168 		if (fname) {
169 			os_fd_restore();
170 			os_spl_to_uboot(fname);
171 		} else {
172 			log_err("No filename provided for U-Boot\n");
173 		}
174 		break;
175 	}
176 	case SPL_SANDBOXF_ARG_IS_BUF: {
177 		int ret;
178 
179 		ret = os_jump_to_image(spl_image->arg + spl_image->offset,
180 				       spl_image->size);
181 		if (ret)
182 			log_err("Failed to load image\n");
183 		break;
184 	}
185 	default:
186 		log_err("Invalid flags\n");
187 		break;
188 	}
189 	hang();
190 }
191 
handoff_arch_save(struct spl_handoff * ho)192 int handoff_arch_save(struct spl_handoff *ho)
193 {
194 	ho->arch.magic = TEST_HANDOFF_MAGIC;
195 
196 	return 0;
197 }
198 
199 /* Context used to hold file descriptor */
200 struct load_ctx {
201 	int fd;
202 };
203 
read_fit_image(struct spl_load_info * load,ulong offset,ulong size,void * buf)204 static ulong read_fit_image(struct spl_load_info *load, ulong offset,
205 			    ulong size, void *buf)
206 {
207 	struct load_ctx *load_ctx = load->priv;
208 	off_t ret;
209 	ssize_t res;
210 
211 	ret = os_lseek(load_ctx->fd, offset, OS_SEEK_SET);
212 	if (ret < 0) {
213 		printf("Failed to seek to %zx, got %zx\n", offset, ret);
214 		return log_msg_ret("lse", ret);
215 	}
216 
217 	res = os_read(load_ctx->fd, buf, size);
218 	if (res < 0) {
219 		printf("Failed to read %lx bytes, got %ld\n", size, res);
220 		return log_msg_ret("osr", res);
221 	}
222 
223 	return size;
224 }
225 
sandbox_spl_load_fit(char * fname,int maxlen,struct spl_image_info * image)226 int sandbox_spl_load_fit(char *fname, int maxlen, struct spl_image_info *image)
227 {
228 	struct legacy_img_hdr *header;
229 	struct load_ctx load_ctx;
230 	struct spl_load_info load;
231 	int ret;
232 	int fd;
233 
234 	spl_load_init(&load, read_fit_image, &load_ctx,
235 		      IS_ENABLED(CONFIG_SPL_LOAD_BLOCK) ? 512 : 1);
236 
237 	ret = sandbox_find_next_phase(fname, maxlen, true);
238 	if (ret) {
239 		printf("%s not found, error %d\n", fname, ret);
240 		return log_msg_ret("nph", ret);
241 	}
242 
243 	header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
244 
245 	log_debug("reading from %s\n", fname);
246 	fd = os_open(fname, OS_O_RDONLY);
247 	if (fd < 0) {
248 		printf("Failed to open '%s'\n", fname);
249 		return log_msg_ret("ope", -errno);
250 	}
251 	ret = os_read(fd, header, sizeof(*header));
252 	if (ret != sizeof(*header)) {
253 		printf("Failed to read %lx bytes, got %d\n", sizeof(*header),
254 		       ret);
255 		return log_msg_ret("rea", ret);
256 	}
257 	load_ctx.fd = fd;
258 
259 	load.priv = &load_ctx;
260 
261 	ret = spl_load_simple_fit(image, &load, 0, header);
262 	if (ret)
263 		return log_msg_ret("slf", ret);
264 
265 	return 0;
266 }
267 
upl_load_from_image(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)268 static int upl_load_from_image(struct spl_image_info *spl_image,
269 			       struct spl_boot_device *bootdev)
270 {
271 	long long size;
272 	char *fname;
273 	int ret, fd;
274 	ulong addr;
275 
276 	if (!CONFIG_IS_ENABLED(UPL_OUT))
277 		return -ENOTSUPP;
278 
279 	spl_upl_init();
280 	fname = os_malloc(256);
281 
282 	ret = sandbox_spl_load_fit(fname, 256, spl_image);
283 	if (ret)
284 		return log_msg_ret("fit", ret);
285 	spl_image->flags = SPL_SANDBOXF_ARG_IS_BUF;
286 	spl_image->arg = map_sysmem(spl_image->load_addr, 0);
287 	/* size is set by load_simple_fit(), offset is left as 0 */
288 
289 	/* now read the whole FIT into memory */
290 	fd = os_open(fname, OS_O_RDONLY);
291 	if (fd < 0)
292 		return log_msg_ret("op2", -ENOENT);
293 	if (os_get_filesize(fname,  &size))
294 		return log_msg_ret("fis", -ENOENT);
295 
296 	/* place it after the loaded image, allowing plenty of space */
297 	addr = ALIGN(spl_image->load_addr + size, 0x1000);
298 	log_debug("Loading whole FIT to %lx\n", addr);
299 	if (os_read(fd, map_sysmem(addr, 0), size) != size)
300 		return log_msg_ret("rea", -EIO);
301 	os_close(fd);
302 
303 	/* tell UPL where it is */
304 	upl_set_fit_addr(addr);
305 
306 	return 0;
307 }
308 SPL_LOAD_IMAGE_METHOD("upl", 4, BOOT_DEVICE_UPL, upl_load_from_image);
309