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