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