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 | } |
Raul E Rangel | b25576f | 2021-11-05 10:29:24 -0600 | [diff] [blame] | 78 | void preload_ramstage(void) |
| 79 | { |
| 80 | if (!CONFIG(CBFS_PRELOAD)) |
| 81 | return; |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 82 | |
Raul E Rangel | b25576f | 2021-11-05 10:29:24 -0600 | [diff] [blame] | 83 | printk(BIOS_DEBUG, "Preloading ramstage\n"); |
| 84 | |
| 85 | cbfs_preload(CONFIG_CBFS_PREFIX "/ramstage"); |
| 86 | } |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 87 | void run_ramstage(void) |
| 88 | { |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 89 | struct prog ramstage = |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 90 | PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage"); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 91 | |
Subrata Banik | 4f42eea | 2019-03-05 16:45:14 +0530 | [diff] [blame] | 92 | if (ENV_POSTCAR) |
| 93 | timestamp_add_now(TS_END_POSTCAR); |
| 94 | |
Subrata Banik | 2847e1e | 2019-02-25 20:31:22 +0530 | [diff] [blame] | 95 | /* Call "end of romstage" here if postcar stage doesn't exist */ |
| 96 | if (ENV_ROMSTAGE) |
| 97 | timestamp_add_now(TS_END_ROMSTAGE); |
Aaron Durbin | 9796f60 | 2015-09-23 19:54:12 -0500 | [diff] [blame] | 98 | |
Furquan Shaikh | 1e162bf | 2016-05-06 09:20:35 -0700 | [diff] [blame] | 99 | /* |
| 100 | * Only x86 systems using ramstage stage cache currently take the same |
| 101 | * firmware path on resume. |
| 102 | */ |
Kyösti Mälkki | e0165fb | 2021-01-09 13:30:57 +0200 | [diff] [blame] | 103 | if (ENV_X86 && resume_from_stage_cache()) |
Aaron Durbin | 6c191d8 | 2016-11-29 21:22:42 -0600 | [diff] [blame] | 104 | run_ramstage_from_resume(&ramstage); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 105 | |
Wim Vervoorn | 1058dd8 | 2019-11-01 10:22:22 +0100 | [diff] [blame] | 106 | vboot_run_logic(); |
| 107 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 108 | timestamp_add_now(TS_START_COPYRAM); |
| 109 | |
Nico Huber | 06b68d1 | 2020-07-06 11:02:53 +0200 | [diff] [blame] | 110 | if (ENV_X86) { |
| 111 | if (load_relocatable_ramstage(&ramstage)) |
| 112 | goto fail; |
| 113 | } else { |
| 114 | if (cbfs_prog_stage_load(&ramstage)) |
| 115 | goto fail; |
| 116 | } |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 117 | |
Subrata Banik | 90f750b | 2019-06-11 17:52:06 +0530 | [diff] [blame] | 118 | stage_cache_add(STAGE_RAMSTAGE, &ramstage); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 119 | |
| 120 | timestamp_add_now(TS_END_COPYRAM); |
| 121 | |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 122 | console_time_report(); |
| 123 | |
Arthur Heymans | 5331a7c | 2019-10-23 17:07:15 +0200 | [diff] [blame] | 124 | /* This overrides the arg fetched from the relocatable module */ |
| 125 | prog_set_arg(&ramstage, cbmem_top()); |
| 126 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 127 | prog_run(&ramstage); |
| 128 | |
| 129 | fail: |
Keith Short | 7006458 | 2019-05-06 16:12:57 -0600 | [diff] [blame] | 130 | die_with_post_code(POST_INVALID_ROM, "Ramstage was not loaded!\n"); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 131 | } |
| 132 | |
Subrata Banik | 42c44c2 | 2019-05-15 20:27:04 +0530 | [diff] [blame] | 133 | #if ENV_PAYLOAD_LOADER // gc-sections should take care of this |
Stefan Reinauer | eec2db4 | 2015-06-18 01:19:50 -0700 | [diff] [blame] | 134 | |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 135 | static struct prog global_payload = |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 136 | PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload"); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 137 | |
Raul E Rangel | 67798cf | 2021-07-02 17:07:05 -0600 | [diff] [blame] | 138 | void payload_preload(void) |
| 139 | { |
Raul E Rangel | 571e7f0 | 2021-11-02 11:51:48 -0600 | [diff] [blame] | 140 | if (!CONFIG(CBFS_PRELOAD)) |
Raul E Rangel | 67798cf | 2021-07-02 17:07:05 -0600 | [diff] [blame] | 141 | return; |
| 142 | |
Raul E Rangel | 571e7f0 | 2021-11-02 11:51:48 -0600 | [diff] [blame] | 143 | cbfs_preload(global_payload.name); |
Raul E Rangel | 67798cf | 2021-07-02 17:07:05 -0600 | [diff] [blame] | 144 | } |
| 145 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 146 | void payload_load(void) |
| 147 | { |
| 148 | struct prog *payload = &global_payload; |
Raul E Rangel | 571e7f0 | 2021-11-02 11:51:48 -0600 | [diff] [blame] | 149 | void *mapping; |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 150 | |
| 151 | timestamp_add_now(TS_LOAD_PAYLOAD); |
| 152 | |
Julius Werner | 965846f | 2021-01-11 16:07:02 -0800 | [diff] [blame] | 153 | if (prog_locate_hook(payload)) |
| 154 | goto out; |
| 155 | |
Raul E Rangel | 571e7f0 | 2021-11-02 11:51:48 -0600 | [diff] [blame] | 156 | payload->cbfs_type = CBFS_TYPE_QUERY; |
| 157 | mapping = cbfs_type_map(prog_name(payload), NULL, &payload->cbfs_type); |
Raul E Rangel | 67798cf | 2021-07-02 17:07:05 -0600 | [diff] [blame] | 158 | |
Raul E Rangel | 571e7f0 | 2021-11-02 11:51:48 -0600 | [diff] [blame] | 159 | if (!mapping) |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 160 | goto out; |
| 161 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 162 | switch (prog_cbfs_type(payload)) { |
| 163 | case CBFS_TYPE_SELF: /* Simple ELF */ |
Raul E Rangel | 571e7f0 | 2021-11-02 11:51:48 -0600 | [diff] [blame] | 164 | selfload_mapped(payload, mapping, BM_MEM_RAM); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 165 | break; |
| 166 | case CBFS_TYPE_FIT: /* Flattened image tree */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 167 | if (CONFIG(PAYLOAD_FIT_SUPPORT)) { |
Raul E Rangel | 571e7f0 | 2021-11-02 11:51:48 -0600 | [diff] [blame] | 168 | fit_payload(payload, mapping); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 169 | break; |
| 170 | } /* else fall-through */ |
| 171 | default: |
Keith Short | 7006458 | 2019-05-06 16:12:57 -0600 | [diff] [blame] | 172 | die_with_post_code(POST_INVALID_ROM, |
Julius Werner | 965846f | 2021-01-11 16:07:02 -0800 | [diff] [blame] | 173 | "Unsupported payload type %d.\n", payload->cbfs_type); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 174 | break; |
| 175 | } |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 176 | |
Raul E Rangel | 571e7f0 | 2021-11-02 11:51:48 -0600 | [diff] [blame] | 177 | cbfs_unmap(mapping); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 178 | out: |
| 179 | if (prog_entry(payload) == NULL) |
Keith Short | 7006458 | 2019-05-06 16:12:57 -0600 | [diff] [blame] | 180 | die_with_post_code(POST_INVALID_ROM, "Payload not loaded.\n"); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void payload_run(void) |
| 184 | { |
| 185 | struct prog *payload = &global_payload; |
| 186 | |
| 187 | /* Reset to booting from this image as late as possible */ |
| 188 | boot_successful(); |
| 189 | |
| 190 | printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n", |
| 191 | prog_entry(payload), prog_entry_arg(payload)); |
| 192 | |
| 193 | post_code(POST_ENTER_ELF_BOOT); |
| 194 | |
| 195 | timestamp_add_now(TS_SELFBOOT_JUMP); |
| 196 | |
| 197 | /* Before we go off to run the payload, see if |
| 198 | * we stayed within our bounds. |
| 199 | */ |
| 200 | checkstack(_estack, 0); |
| 201 | |
| 202 | prog_run(payload); |
| 203 | } |
Stefan Reinauer | eec2db4 | 2015-06-18 01:19:50 -0700 | [diff] [blame] | 204 | |
| 205 | #endif |