Angel Pons | 118a9c7 | 2020-04-02 23:48:34 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 2 | |
| 3 | |
| 4 | #include <stdlib.h> |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 5 | #include <cbfs.h> |
| 6 | #include <cbmem.h> |
| 7 | #include <console/console.h> |
| 8 | #include <fallback.h> |
| 9 | #include <halt.h> |
| 10 | #include <lib.h> |
| 11 | #include <program_loading.h> |
Aaron Durbin | 8cd723b | 2016-10-28 17:32:24 -0500 | [diff] [blame] | 12 | #include <reset.h> |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 13 | #include <rmodule.h> |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 14 | #include <stage_cache.h> |
| 15 | #include <symbols.h> |
Raul E Rangel | 67798cf | 2021-07-02 17:07:05 -0600 | [diff] [blame] | 16 | #include <thread.h> |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 17 | #include <timestamp.h> |
Wim Vervoorn | 1058dd8 | 2019-11-01 10:22:22 +0100 | [diff] [blame] | 18 | #include <security/vboot/vboot_common.h> |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 19 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 20 | void run_romstage(void) |
| 21 | { |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 22 | struct prog romstage = |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 23 | PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage"); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 24 | |
Wim Vervoorn | 1058dd8 | 2019-11-01 10:22:22 +0100 | [diff] [blame] | 25 | vboot_run_logic(); |
| 26 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 27 | timestamp_add_now(TS_START_COPYROM); |
| 28 | |
Julius Werner | 1de8708 | 2020-12-23 17:38:11 -0800 | [diff] [blame] | 29 | if (ENV_X86 && CONFIG(BOOTBLOCK_NORMAL)) { |
| 30 | if (legacy_romstage_select_and_load(&romstage)) |
| 31 | goto fail; |
| 32 | } else { |
| 33 | if (cbfs_prog_stage_load(&romstage)) |
| 34 | goto fail; |
| 35 | } |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 36 | |
| 37 | timestamp_add_now(TS_END_COPYROM); |
| 38 | |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 39 | console_time_report(); |
| 40 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 41 | prog_run(&romstage); |
| 42 | |
| 43 | fail: |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 44 | if (CONFIG(BOOTBLOCK_CONSOLE)) |
Keith Short | 7006458 | 2019-05-06 16:12:57 -0600 | [diff] [blame] | 45 | die_with_post_code(POST_INVALID_ROM, |
| 46 | "Couldn't load romstage.\n"); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 47 | halt(); |
| 48 | } |
| 49 | |
Frans Hendriks | fc58034 | 2019-06-14 14:36:37 +0200 | [diff] [blame] | 50 | int __weak prog_locate_hook(struct prog *prog) { return 0; } |
| 51 | |
Aaron Durbin | 6c191d8 | 2016-11-29 21:22:42 -0600 | [diff] [blame] | 52 | static void run_ramstage_from_resume(struct prog *ramstage) |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 53 | { |
Aaron Durbin | 6c191d8 | 2016-11-29 21:22:42 -0600 | [diff] [blame] | 54 | /* Load the cached ramstage to runtime location. */ |
| 55 | stage_cache_load_stage(STAGE_RAMSTAGE, ramstage); |
| 56 | |
Julius Werner | 1de8708 | 2020-12-23 17:38:11 -0800 | [diff] [blame] | 57 | ramstage->cbfs_type = CBFS_TYPE_STAGE; |
Arthur Heymans | 5331a7c | 2019-10-23 17:07:15 +0200 | [diff] [blame] | 58 | prog_set_arg(ramstage, cbmem_top()); |
| 59 | |
Aaron Durbin | 6c191d8 | 2016-11-29 21:22:42 -0600 | [diff] [blame] | 60 | if (prog_entry(ramstage) != NULL) { |
| 61 | printk(BIOS_DEBUG, "Jumping to image.\n"); |
| 62 | prog_run(ramstage); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 63 | } |
Kyösti Mälkki | 4f14cd8 | 2019-12-18 19:40:48 +0200 | [diff] [blame] | 64 | |
| 65 | printk(BIOS_ERR, "ramstage cache invalid.\n"); |
| 66 | board_reset(); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static int load_relocatable_ramstage(struct prog *ramstage) |
| 70 | { |
| 71 | struct rmod_stage_load rmod_ram = { |
| 72 | .cbmem_id = CBMEM_ID_RAMSTAGE, |
| 73 | .prog = ramstage, |
| 74 | }; |
| 75 | |
| 76 | return rmodule_stage_load(&rmod_ram); |
| 77 | } |
| 78 | |
| 79 | void run_ramstage(void) |
| 80 | { |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 81 | struct prog ramstage = |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 82 | PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage"); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 83 | |
Subrata Banik | 4f42eea | 2019-03-05 16:45:14 +0530 | [diff] [blame] | 84 | if (ENV_POSTCAR) |
| 85 | timestamp_add_now(TS_END_POSTCAR); |
| 86 | |
Subrata Banik | 2847e1e | 2019-02-25 20:31:22 +0530 | [diff] [blame] | 87 | /* Call "end of romstage" here if postcar stage doesn't exist */ |
| 88 | if (ENV_ROMSTAGE) |
| 89 | timestamp_add_now(TS_END_ROMSTAGE); |
Aaron Durbin | 9796f60 | 2015-09-23 19:54:12 -0500 | [diff] [blame] | 90 | |
Furquan Shaikh | 1e162bf | 2016-05-06 09:20:35 -0700 | [diff] [blame] | 91 | /* |
| 92 | * Only x86 systems using ramstage stage cache currently take the same |
| 93 | * firmware path on resume. |
| 94 | */ |
Kyösti Mälkki | e0165fb | 2021-01-09 13:30:57 +0200 | [diff] [blame] | 95 | if (ENV_X86 && resume_from_stage_cache()) |
Aaron Durbin | 6c191d8 | 2016-11-29 21:22:42 -0600 | [diff] [blame] | 96 | run_ramstage_from_resume(&ramstage); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 97 | |
Wim Vervoorn | 1058dd8 | 2019-11-01 10:22:22 +0100 | [diff] [blame] | 98 | vboot_run_logic(); |
| 99 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 100 | timestamp_add_now(TS_START_COPYRAM); |
| 101 | |
Nico Huber | 06b68d1 | 2020-07-06 11:02:53 +0200 | [diff] [blame] | 102 | if (ENV_X86) { |
| 103 | if (load_relocatable_ramstage(&ramstage)) |
| 104 | goto fail; |
| 105 | } else { |
| 106 | if (cbfs_prog_stage_load(&ramstage)) |
| 107 | goto fail; |
| 108 | } |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 109 | |
Subrata Banik | 90f750b | 2019-06-11 17:52:06 +0530 | [diff] [blame] | 110 | stage_cache_add(STAGE_RAMSTAGE, &ramstage); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 111 | |
| 112 | timestamp_add_now(TS_END_COPYRAM); |
| 113 | |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 114 | console_time_report(); |
| 115 | |
Arthur Heymans | 5331a7c | 2019-10-23 17:07:15 +0200 | [diff] [blame] | 116 | /* This overrides the arg fetched from the relocatable module */ |
| 117 | prog_set_arg(&ramstage, cbmem_top()); |
| 118 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 119 | prog_run(&ramstage); |
| 120 | |
| 121 | fail: |
Keith Short | 7006458 | 2019-05-06 16:12:57 -0600 | [diff] [blame] | 122 | die_with_post_code(POST_INVALID_ROM, "Ramstage was not loaded!\n"); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 123 | } |
| 124 | |
Subrata Banik | 42c44c2 | 2019-05-15 20:27:04 +0530 | [diff] [blame] | 125 | #if ENV_PAYLOAD_LOADER // gc-sections should take care of this |
Stefan Reinauer | eec2db4 | 2015-06-18 01:19:50 -0700 | [diff] [blame] | 126 | |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 127 | static struct prog global_payload = |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 128 | PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload"); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 129 | |
Raul E Rangel | 67798cf | 2021-07-02 17:07:05 -0600 | [diff] [blame] | 130 | static struct thread_handle payload_preload_handle; |
| 131 | |
| 132 | static enum cb_err payload_preload_thread_entry(void *arg) |
| 133 | { |
| 134 | size_t size; |
| 135 | struct prog *payload = &global_payload; |
| 136 | |
| 137 | printk(BIOS_DEBUG, "Preloading payload\n"); |
| 138 | |
| 139 | payload->cbfs_type = CBFS_TYPE_QUERY; |
| 140 | |
| 141 | size = cbfs_type_load(prog_name(payload), _payload_preload_cache, |
| 142 | REGION_SIZE(payload_preload_cache), &payload->cbfs_type); |
| 143 | |
| 144 | if (!size) { |
| 145 | printk(BIOS_ERR, "ERROR: Preloading payload failed\n"); |
| 146 | return CB_ERR; |
| 147 | } |
| 148 | |
| 149 | printk(BIOS_DEBUG, "Preloading payload complete\n"); |
| 150 | |
| 151 | return CB_SUCCESS; |
| 152 | } |
| 153 | |
| 154 | void payload_preload(void) |
| 155 | { |
| 156 | struct thread_handle *handle = &payload_preload_handle; |
| 157 | |
| 158 | if (!CONFIG(PAYLOAD_PRELOAD)) |
| 159 | return; |
| 160 | |
| 161 | if (thread_run(handle, payload_preload_thread_entry, NULL)) |
| 162 | printk(BIOS_ERR, "ERROR: Failed to start payload preload thread\n"); |
| 163 | } |
| 164 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 165 | void payload_load(void) |
| 166 | { |
| 167 | struct prog *payload = &global_payload; |
Raul E Rangel | 67798cf | 2021-07-02 17:07:05 -0600 | [diff] [blame] | 168 | struct thread_handle *handle = &payload_preload_handle; |
| 169 | void *mapping = NULL; |
| 170 | void *buffer; |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 171 | |
| 172 | timestamp_add_now(TS_LOAD_PAYLOAD); |
| 173 | |
Julius Werner | 965846f | 2021-01-11 16:07:02 -0800 | [diff] [blame] | 174 | if (prog_locate_hook(payload)) |
| 175 | goto out; |
| 176 | |
Raul E Rangel | 67798cf | 2021-07-02 17:07:05 -0600 | [diff] [blame] | 177 | if (CONFIG(PAYLOAD_PRELOAD) && thread_join(handle) == CB_SUCCESS) { |
| 178 | buffer = _payload_preload_cache; |
| 179 | } else { |
| 180 | payload->cbfs_type = CBFS_TYPE_QUERY; |
| 181 | mapping = cbfs_type_map(prog_name(payload), NULL, &payload->cbfs_type); |
| 182 | buffer = mapping; |
| 183 | } |
| 184 | |
| 185 | if (!buffer) |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 186 | goto out; |
| 187 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 188 | switch (prog_cbfs_type(payload)) { |
| 189 | case CBFS_TYPE_SELF: /* Simple ELF */ |
Raul E Rangel | 67798cf | 2021-07-02 17:07:05 -0600 | [diff] [blame] | 190 | selfload_mapped(payload, buffer, BM_MEM_RAM); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 191 | break; |
| 192 | case CBFS_TYPE_FIT: /* Flattened image tree */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 193 | if (CONFIG(PAYLOAD_FIT_SUPPORT)) { |
Raul E Rangel | 67798cf | 2021-07-02 17:07:05 -0600 | [diff] [blame] | 194 | fit_payload(payload, buffer); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 195 | break; |
| 196 | } /* else fall-through */ |
| 197 | default: |
Keith Short | 7006458 | 2019-05-06 16:12:57 -0600 | [diff] [blame] | 198 | die_with_post_code(POST_INVALID_ROM, |
Julius Werner | 965846f | 2021-01-11 16:07:02 -0800 | [diff] [blame] | 199 | "Unsupported payload type %d.\n", payload->cbfs_type); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 200 | break; |
| 201 | } |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 202 | |
Raul E Rangel | 67798cf | 2021-07-02 17:07:05 -0600 | [diff] [blame] | 203 | if (mapping) |
| 204 | cbfs_unmap(mapping); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 205 | out: |
| 206 | if (prog_entry(payload) == NULL) |
Keith Short | 7006458 | 2019-05-06 16:12:57 -0600 | [diff] [blame] | 207 | die_with_post_code(POST_INVALID_ROM, "Payload not loaded.\n"); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void payload_run(void) |
| 211 | { |
| 212 | struct prog *payload = &global_payload; |
| 213 | |
| 214 | /* Reset to booting from this image as late as possible */ |
| 215 | boot_successful(); |
| 216 | |
| 217 | printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n", |
| 218 | prog_entry(payload), prog_entry_arg(payload)); |
| 219 | |
| 220 | post_code(POST_ENTER_ELF_BOOT); |
| 221 | |
| 222 | timestamp_add_now(TS_SELFBOOT_JUMP); |
| 223 | |
| 224 | /* Before we go off to run the payload, see if |
| 225 | * we stayed within our bounds. |
| 226 | */ |
| 227 | checkstack(_estack, 0); |
| 228 | |
| 229 | prog_run(payload); |
| 230 | } |
Stefan Reinauer | eec2db4 | 2015-06-18 01:19:50 -0700 | [diff] [blame] | 231 | |
| 232 | #endif |