Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | a7e2d4d | 2016-07-04 11:57:51 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016 Google, Inc |
Simon Glass | a7e2d4d | 2016-07-04 11:57:51 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Simon Glass | f11478f | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 8 | #include <hang.h> |
Simon Glass | 9ee7b73 | 2022-02-28 15:13:46 -0700 | [diff] [blame] | 9 | #include <handoff.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 10 | #include <init.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | a7e2d4d | 2016-07-04 11:57:51 -0600 | [diff] [blame] | 12 | #include <os.h> |
Simon Glass | 4e9c131 | 2016-07-04 11:57:55 -0600 | [diff] [blame] | 13 | #include <spl.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Simon Glass | a7e2d4d | 2016-07-04 11:57:51 -0600 | [diff] [blame] | 15 | #include <asm/spl.h> |
| 16 | #include <asm/state.h> |
Simon Glass | 1ef74ab | 2021-03-07 17:35:12 -0700 | [diff] [blame] | 17 | #include <test/ut.h> |
Simon Glass | a7e2d4d | 2016-07-04 11:57:51 -0600 | [diff] [blame] | 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Simon Glass | 1cd0600 | 2021-07-05 16:32:45 -0600 | [diff] [blame] | 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 | |
Simon Glass | c86e620 | 2022-04-30 00:56:54 -0600 | [diff] [blame] | 35 | /* SPL / TPL / VPL init function */ |
Simon Glass | a7e2d4d | 2016-07-04 11:57:51 -0600 | [diff] [blame] | 36 | void board_init_f(ulong flag) |
| 37 | { |
| 38 | struct sandbox_state *state = state_get_current(); |
Simon Glass | c86e620 | 2022-04-30 00:56:54 -0600 | [diff] [blame] | 39 | int ret; |
Simon Glass | a7e2d4d | 2016-07-04 11:57:51 -0600 | [diff] [blame] | 40 | |
| 41 | gd->arch.ram_buf = state->ram_buf; |
| 42 | gd->ram_size = state->ram_size; |
Simon Glass | c86e620 | 2022-04-30 00:56:54 -0600 | [diff] [blame] | 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(); |
Simon Glass | a7e2d4d | 2016-07-04 11:57:51 -0600 | [diff] [blame] | 50 | } |
| 51 | |
Simon Glass | 4aa6a9b | 2022-10-20 18:23:01 -0600 | [diff] [blame] | 52 | void board_boot_order(u32 *spl_boot_list) |
Simon Glass | a7e2d4d | 2016-07-04 11:57:51 -0600 | [diff] [blame] | 53 | { |
Simon Glass | 4aa6a9b | 2022-10-20 18:23:01 -0600 | [diff] [blame] | 54 | spl_boot_list[0] = BOOT_DEVICE_BOARD; |
Simon Glass | a7e2d4d | 2016-07-04 11:57:51 -0600 | [diff] [blame] | 55 | } |
| 56 | |
Simon Glass | 4aa6a9b | 2022-10-20 18:23:01 -0600 | [diff] [blame] | 57 | static int spl_board_load_file(struct spl_image_info *spl_image, |
| 58 | struct spl_boot_device *bootdev) |
Simon Glass | a7e2d4d | 2016-07-04 11:57:51 -0600 | [diff] [blame] | 59 | { |
| 60 | char fname[256]; |
| 61 | int ret; |
| 62 | |
Simon Glass | 1cd0600 | 2021-07-05 16:32:45 -0600 | [diff] [blame] | 63 | ret = sandbox_find_next_phase(fname, sizeof(fname), false); |
Simon Glass | e8845d2 | 2016-11-30 15:30:56 -0700 | [diff] [blame] | 64 | if (ret) { |
| 65 | printf("(%s not found, error %d)\n", fname, ret); |
Simon Glass | a7e2d4d | 2016-07-04 11:57:51 -0600 | [diff] [blame] | 66 | return ret; |
Simon Glass | e8845d2 | 2016-11-30 15:30:56 -0700 | [diff] [blame] | 67 | } |
Simon Glass | a7e2d4d | 2016-07-04 11:57:51 -0600 | [diff] [blame] | 68 | |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 69 | /* |
| 70 | * Set up spl_image to boot from jump_to_image_no_args(). Allocate this |
| 71 | * outsdide the RAM buffer (i.e. don't use strdup()). |
| 72 | */ |
| 73 | spl_image->arg = os_malloc(strlen(fname) + 1); |
Simon Glass | cca2552 | 2018-11-15 18:44:08 -0700 | [diff] [blame] | 74 | if (!spl_image->arg) |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 75 | return log_msg_ret("exec", -ENOMEM); |
| 76 | strcpy(spl_image->arg, fname); |
Simon Glass | 4aa6a9b | 2022-10-20 18:23:01 -0600 | [diff] [blame] | 77 | spl_image->flags = SPL_SANDBOXF_ARG_IS_FNAME; |
Simon Glass | cca2552 | 2018-11-15 18:44:08 -0700 | [diff] [blame] | 78 | |
| 79 | return 0; |
Simon Glass | a7e2d4d | 2016-07-04 11:57:51 -0600 | [diff] [blame] | 80 | } |
Simon Glass | a87cd0c | 2022-10-20 18:23:08 -0600 | [diff] [blame] | 81 | SPL_LOAD_IMAGE_METHOD("sandbox_file", 9, BOOT_DEVICE_BOARD, |
| 82 | spl_board_load_file); |
| 83 | |
| 84 | static int load_from_image(struct spl_image_info *spl_image, |
| 85 | struct spl_boot_device *bootdev) |
| 86 | { |
| 87 | struct sandbox_state *state = state_get_current(); |
| 88 | enum u_boot_phase next_phase; |
| 89 | const char *fname; |
| 90 | ulong pos, size; |
| 91 | int full_size; |
| 92 | void *buf; |
| 93 | int ret; |
| 94 | |
| 95 | if (!IS_ENABLED(CONFIG_SANDBOX_VPL)) |
| 96 | return -ENOENT; |
| 97 | |
| 98 | next_phase = spl_next_phase(); |
| 99 | pos = spl_get_image_pos(); |
| 100 | size = spl_get_image_size(); |
| 101 | if (pos == BINMAN_SYM_MISSING || size == BINMAN_SYM_MISSING) { |
| 102 | log_debug("No image found\n"); |
| 103 | return -ENOENT; |
| 104 | } |
| 105 | log_info("Reading from pos %lx size %lx\n", pos, size); |
| 106 | |
| 107 | /* |
| 108 | * Set up spl_image to boot from jump_to_image_no_args(). Allocate this |
| 109 | * outside the RAM buffer (i.e. don't use strdup()). |
| 110 | */ |
| 111 | fname = state->prog_fname ? state->prog_fname : state->argv[0]; |
| 112 | ret = os_read_file(fname, &buf, &full_size); |
| 113 | if (ret) |
| 114 | return log_msg_ret("rd", -ENOMEM); |
| 115 | spl_image->flags = SPL_SANDBOXF_ARG_IS_BUF; |
| 116 | spl_image->arg = buf; |
| 117 | spl_image->offset = pos; |
| 118 | spl_image->size = size; |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | SPL_LOAD_IMAGE_METHOD("sandbox_image", 7, BOOT_DEVICE_BOARD, load_from_image); |
Simon Glass | 4e9c131 | 2016-07-04 11:57:55 -0600 | [diff] [blame] | 123 | |
| 124 | void spl_board_init(void) |
| 125 | { |
Simon Glass | b5dfea8 | 2018-11-15 18:44:01 -0700 | [diff] [blame] | 126 | struct sandbox_state *state = state_get_current(); |
Simon Glass | b5dfea8 | 2018-11-15 18:44:01 -0700 | [diff] [blame] | 127 | |
Simon Glass | a4e289b | 2020-10-25 20:38:28 -0600 | [diff] [blame] | 128 | if (state->run_unittests) { |
Simon Glass | 1ef74ab | 2021-03-07 17:35:12 -0700 | [diff] [blame] | 129 | struct unit_test *tests = UNIT_TEST_ALL_START(); |
| 130 | const int count = UNIT_TEST_ALL_COUNT(); |
Simon Glass | a4e289b | 2020-10-25 20:38:28 -0600 | [diff] [blame] | 131 | int ret; |
| 132 | |
Simon Glass | 1ef74ab | 2021-03-07 17:35:12 -0700 | [diff] [blame] | 133 | ret = ut_run_list("spl", NULL, tests, count, |
Simon Glass | 1f1614b | 2022-10-20 18:22:50 -0600 | [diff] [blame] | 134 | state->select_unittests, 1, false); |
Simon Glass | a4e289b | 2020-10-25 20:38:28 -0600 | [diff] [blame] | 135 | /* continue execution into U-Boot */ |
| 136 | } |
Simon Glass | 4e9c131 | 2016-07-04 11:57:55 -0600 | [diff] [blame] | 137 | } |
Simon Glass | cca2552 | 2018-11-15 18:44:08 -0700 | [diff] [blame] | 138 | |
| 139 | void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image) |
| 140 | { |
Simon Glass | 4aa6a9b | 2022-10-20 18:23:01 -0600 | [diff] [blame] | 141 | switch (spl_image->flags) { |
| 142 | case SPL_SANDBOXF_ARG_IS_FNAME: { |
| 143 | const char *fname = spl_image->arg; |
Simon Glass | cca2552 | 2018-11-15 18:44:08 -0700 | [diff] [blame] | 144 | |
Simon Glass | 4aa6a9b | 2022-10-20 18:23:01 -0600 | [diff] [blame] | 145 | if (fname) { |
| 146 | os_fd_restore(); |
| 147 | os_spl_to_uboot(fname); |
| 148 | } else { |
| 149 | log_err("No filename provided for U-Boot\n"); |
| 150 | } |
| 151 | break; |
| 152 | } |
Simon Glass | a87cd0c | 2022-10-20 18:23:08 -0600 | [diff] [blame] | 153 | case SPL_SANDBOXF_ARG_IS_BUF: { |
| 154 | int ret; |
| 155 | |
| 156 | ret = os_jump_to_image(spl_image->arg + spl_image->offset, |
| 157 | spl_image->size); |
| 158 | if (ret) |
| 159 | log_err("Failed to load image\n"); |
| 160 | break; |
| 161 | } |
Simon Glass | 4aa6a9b | 2022-10-20 18:23:01 -0600 | [diff] [blame] | 162 | default: |
| 163 | log_err("Invalid flags\n"); |
| 164 | break; |
Simon Glass | 38c2eae | 2018-11-23 21:29:25 -0700 | [diff] [blame] | 165 | } |
Simon Glass | cca2552 | 2018-11-15 18:44:08 -0700 | [diff] [blame] | 166 | hang(); |
| 167 | } |
Simon Glass | c5d2720 | 2019-09-25 08:11:18 -0600 | [diff] [blame] | 168 | |
| 169 | int handoff_arch_save(struct spl_handoff *ho) |
| 170 | { |
| 171 | ho->arch.magic = TEST_HANDOFF_MAGIC; |
| 172 | |
| 173 | return 0; |
| 174 | } |