]> Git Repo - J-u-boot.git/blob - arch/sandbox/cpu/spl.c
xpl: Rename spl_phase() to xpl_phase()
[J-u-boot.git] / arch / sandbox / cpu / spl.c
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
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 = 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);
33         if (ret)
34                 return log_msg_ret("find", ret);
35
36         return 0;
37 }
38
39 /* SPL / TPL / VPL init function */
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
56 void board_boot_order(u32 *spl_boot_list)
57 {
58         spl_boot_list[0] = BOOT_DEVICE_VBE;
59         spl_boot_list[1] = BOOT_DEVICE_UPL;
60         spl_boot_list[2] = BOOT_DEVICE_BOARD;
61 }
62
63 static int spl_board_load_file(struct spl_image_info *spl_image,
64                                struct spl_boot_device *bootdev)
65 {
66         char fname[256];
67         int ret;
68
69         ret = sandbox_find_next_phase(fname, sizeof(fname), false);
70         if (ret) {
71                 printf("(%s not found, error %d)\n", fname, ret);
72                 return ret;
73         }
74
75         /*
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);
80         if (!spl_image->arg)
81                 return log_msg_ret("exec", -ENOMEM);
82         strcpy(spl_image->arg, fname);
83         spl_image->flags = SPL_SANDBOXF_ARG_IS_FNAME;
84
85         return 0;
86 }
87 SPL_LOAD_IMAGE_METHOD("sandbox_file", 9, BOOT_DEVICE_BOARD,
88                       spl_board_load_file);
89
90 static 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 xpl_phase_t 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 }
128 SPL_LOAD_IMAGE_METHOD("sandbox_image", 7, BOOT_DEVICE_BOARD, load_from_image);
129
130 int dram_init_banksize(void)
131 {
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();
135
136         return 0;
137 }
138
139 void spl_board_init(void)
140 {
141         struct sandbox_state *state = state_get_current();
142
143         if (!CONFIG_IS_ENABLED(UNIT_TEST))
144                 return;
145
146         if (state->run_unittests) {
147                 struct unit_test *tests = UNIT_TEST_ALL_START();
148                 const int count = UNIT_TEST_ALL_COUNT();
149                 int ret;
150
151                 ret = ut_run_list("spl", NULL, tests, count,
152                                   state->select_unittests, 1, false, NULL);
153                 /* continue execution into U-Boot */
154         }
155 }
156
157 void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
158 {
159         switch (spl_image->flags) {
160         case SPL_SANDBOXF_ARG_IS_FNAME: {
161                 const char *fname = spl_image->arg;
162
163                 if (fname) {
164                         os_fd_restore();
165                         os_spl_to_uboot(fname);
166                 } else {
167                         log_err("No filename provided for U-Boot\n");
168                 }
169                 break;
170         }
171         case SPL_SANDBOXF_ARG_IS_BUF: {
172                 int ret;
173
174                 ret = os_jump_to_image(spl_image->arg + spl_image->offset,
175                                        spl_image->size);
176                 if (ret)
177                         log_err("Failed to load image\n");
178                 break;
179         }
180         default:
181                 log_err("Invalid flags\n");
182                 break;
183         }
184         hang();
185 }
186
187 int handoff_arch_save(struct spl_handoff *ho)
188 {
189         ho->arch.magic = TEST_HANDOFF_MAGIC;
190
191         return 0;
192 }
193
194 /* Context used to hold file descriptor */
195 struct load_ctx {
196         int fd;
197 };
198
199 static ulong read_fit_image(struct spl_load_info *load, ulong offset,
200                             ulong size, void *buf)
201 {
202         struct load_ctx *load_ctx = load->priv;
203         off_t ret;
204         ssize_t res;
205
206         ret = os_lseek(load_ctx->fd, offset, OS_SEEK_SET);
207         if (ret < 0) {
208                 printf("Failed to seek to %zx, got %zx\n", offset, ret);
209                 return log_msg_ret("lse", ret);
210         }
211
212         res = os_read(load_ctx->fd, buf, size);
213         if (res < 0) {
214                 printf("Failed to read %lx bytes, got %ld\n", size, res);
215                 return log_msg_ret("osr", res);
216         }
217
218         return size;
219 }
220
221 int sandbox_spl_load_fit(char *fname, int maxlen, struct spl_image_info *image)
222 {
223         struct legacy_img_hdr *header;
224         struct load_ctx load_ctx;
225         struct spl_load_info load;
226         int ret;
227         int fd;
228
229         spl_load_init(&load, read_fit_image, &load_ctx,
230                       IS_ENABLED(CONFIG_SPL_LOAD_BLOCK) ? 512 : 1);
231
232         ret = sandbox_find_next_phase(fname, maxlen, true);
233         if (ret) {
234                 printf("%s not found, error %d\n", fname, ret);
235                 return log_msg_ret("nph", ret);
236         }
237
238         header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
239
240         log_debug("reading from %s\n", fname);
241         fd = os_open(fname, OS_O_RDONLY);
242         if (fd < 0) {
243                 printf("Failed to open '%s'\n", fname);
244                 return log_msg_ret("ope", -errno);
245         }
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),
249                        ret);
250                 return log_msg_ret("rea", ret);
251         }
252         load_ctx.fd = fd;
253
254         load.priv = &load_ctx;
255
256         ret = spl_load_simple_fit(image, &load, 0, header);
257         if (ret)
258                 return log_msg_ret("slf", ret);
259
260         return 0;
261 }
262
263 static int upl_load_from_image(struct spl_image_info *spl_image,
264                                struct spl_boot_device *bootdev)
265 {
266         long long size;
267         char *fname;
268         int ret, fd;
269         ulong addr;
270
271         if (!CONFIG_IS_ENABLED(UPL_OUT))
272                 return -ENOTSUPP;
273
274         spl_upl_init();
275         fname = os_malloc(256);
276
277         ret = sandbox_spl_load_fit(fname, 256, spl_image);
278         if (ret)
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 */
283
284         /* now read the whole FIT into memory */
285         fd = os_open(fname, OS_O_RDONLY);
286         if (fd < 0)
287                 return log_msg_ret("op2", -ENOENT);
288         if (os_get_filesize(fname,  &size))
289                 return log_msg_ret("fis", -ENOENT);
290
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);
296         os_close(fd);
297
298         /* tell UPL where it is */
299         upl_set_fit_addr(addr);
300
301         return 0;
302 }
303 SPL_LOAD_IMAGE_METHOD("upl", 4, BOOT_DEVICE_UPL, upl_load_from_image);
This page took 0.041068 seconds and 4 git commands to generate.