650bdb0a7014ecd7c764d0097f036e5b45f8c06f
[platform/kernel/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 #include <common.h>
7 #include <dm.h>
8 #include <hang.h>
9 #include <init.h>
10 #include <log.h>
11 #include <os.h>
12 #include <spl.h>
13 #include <asm/global_data.h>
14 #include <asm/spl.h>
15 #include <asm/state.h>
16 #include <test/ut.h>
17
18 DECLARE_GLOBAL_DATA_PTR;
19
20 int 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
34 /* SPL / TPL init function */
35 void board_init_f(ulong flag)
36 {
37         struct sandbox_state *state = state_get_current();
38
39         gd->arch.ram_buf = state->ram_buf;
40         gd->ram_size = state->ram_size;
41 }
42
43 u32 spl_boot_device(void)
44 {
45         return BOOT_DEVICE_BOARD;
46 }
47
48 static int spl_board_load_image(struct spl_image_info *spl_image,
49                                 struct spl_boot_device *bootdev)
50 {
51         char fname[256];
52         int ret;
53
54         ret = sandbox_find_next_phase(fname, sizeof(fname), false);
55         if (ret) {
56                 printf("(%s not found, error %d)\n", fname, ret);
57                 return ret;
58         }
59
60         /*
61          * Set up spl_image to boot from jump_to_image_no_args(). Allocate this
62          * outsdide the RAM buffer (i.e. don't use strdup()).
63          */
64         spl_image->arg = os_malloc(strlen(fname) + 1);
65         if (!spl_image->arg)
66                 return log_msg_ret("exec", -ENOMEM);
67         strcpy(spl_image->arg, fname);
68
69         return 0;
70 }
71 SPL_LOAD_IMAGE_METHOD("sandbox", 9, BOOT_DEVICE_BOARD, spl_board_load_image);
72
73 void spl_board_init(void)
74 {
75         struct sandbox_state *state = state_get_current();
76
77         preloader_console_init();
78
79         if (state->run_unittests) {
80                 struct unit_test *tests = UNIT_TEST_ALL_START();
81                 const int count = UNIT_TEST_ALL_COUNT();
82                 int ret;
83
84                 ret = ut_run_list("spl", NULL, tests, count,
85                                   state->select_unittests);
86                 /* continue execution into U-Boot */
87         }
88 }
89
90 void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
91 {
92         const char *fname = spl_image->arg;
93
94         if (fname) {
95                 os_fd_restore();
96                 os_spl_to_uboot(fname);
97         } else {
98                 printf("No filename provided for U-Boot\n");
99         }
100         hang();
101 }
102
103 int handoff_arch_save(struct spl_handoff *ho)
104 {
105         ho->arch.magic = TEST_HANDOFF_MAGIC;
106
107         return 0;
108 }