1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2016 Google, Inc
4 */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <hang.h>
9 #include <handoff.h>
10 #include <init.h>
11 #include <log.h>
12 #include <os.h>
13 #include <spl.h>
14 #include <asm/global_data.h>
15 #include <asm/spl.h>
16 #include <asm/state.h>
17 #include <test/ut.h>
18
19 DECLARE_GLOBAL_DATA_PTR;
20
sandbox_find_next_phase(char * fname,int maxlen,bool use_img)21 int 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
35 /* SPL / TPL / VPL init function */
board_init_f(ulong flag)36 void board_init_f(ulong flag)
37 {
38 struct sandbox_state *state = state_get_current();
39 int ret;
40
41 gd->arch.ram_buf = state->ram_buf;
42 gd->ram_size = state->ram_size;
43
44 ret = spl_early_init();
45 if (ret) {
46 debug("spl_early_init() failed: %d\n", ret);
47 hang();
48 }
49 preloader_console_init();
50 }
51
board_boot_order(u32 * spl_boot_list)52 void board_boot_order(u32 *spl_boot_list)
53 {
54 spl_boot_list[0] = BOOT_DEVICE_VBE;
55 spl_boot_list[1] = BOOT_DEVICE_BOARD;
56 }
57
spl_board_load_file(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)58 static int spl_board_load_file(struct spl_image_info *spl_image,
59 struct spl_boot_device *bootdev)
60 {
61 char fname[256];
62 int ret;
63
64 ret = sandbox_find_next_phase(fname, sizeof(fname), false);
65 if (ret) {
66 printf("(%s not found, error %d)\n", fname, ret);
67 return ret;
68 }
69
70 /*
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);
75 if (!spl_image->arg)
76 return log_msg_ret("exec", -ENOMEM);
77 strcpy(spl_image->arg, fname);
78 spl_image->flags = SPL_SANDBOXF_ARG_IS_FNAME;
79
80 return 0;
81 }
82 SPL_LOAD_IMAGE_METHOD("sandbox_file", 9, BOOT_DEVICE_BOARD,
83 spl_board_load_file);
84
load_from_image(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)85 static 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 }
123 SPL_LOAD_IMAGE_METHOD("sandbox_image", 7, BOOT_DEVICE_BOARD, load_from_image);
124
spl_board_init(void)125 void spl_board_init(void)
126 {
127 struct sandbox_state *state = state_get_current();
128
129 if (state->run_unittests) {
130 struct unit_test *tests = UNIT_TEST_ALL_START();
131 const int count = UNIT_TEST_ALL_COUNT();
132 int ret;
133
134 ret = ut_run_list("spl", NULL, tests, count,
135 state->select_unittests, 1, false, NULL);
136 /* continue execution into U-Boot */
137 }
138 }
139
jump_to_image_no_args(struct spl_image_info * spl_image)140 void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
141 {
142 switch (spl_image->flags) {
143 case SPL_SANDBOXF_ARG_IS_FNAME: {
144 const char *fname = spl_image->arg;
145
146 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 }
154 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 }
163 default:
164 log_err("Invalid flags\n");
165 break;
166 }
167 hang();
168 }
169
handoff_arch_save(struct spl_handoff * ho)170 int handoff_arch_save(struct spl_handoff *ho)
171 {
172 ho->arch.magic = TEST_HANDOFF_MAGIC;
173
174 return 0;
175 }
176