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 | void payload_preload(void) |
| 131 | { |
Raul E Rangel | 571e7f0 | 2021-11-02 11:51:48 -0600 | [diff] [blame^] | 132 | if (!CONFIG(CBFS_PRELOAD)) |
Raul E Rangel | 67798cf | 2021-07-02 17:07:05 -0600 | [diff] [blame] | 133 | return; |
| 134 | |
Raul E Rangel | 571e7f0 | 2021-11-02 11:51:48 -0600 | [diff] [blame^] | 135 | cbfs_preload(global_payload.name); |
Raul E Rangel | 67798cf | 2021-07-02 17:07:05 -0600 | [diff] [blame] | 136 | } |
| 137 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 138 | void payload_load(void) |
| 139 | { |
| 140 | struct prog *payload = &global_payload; |
Raul E Rangel | 571e7f0 | 2021-11-02 11:51:48 -0600 | [diff] [blame^] | 141 | void *mapping; |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 142 | |
| 143 | timestamp_add_now(TS_LOAD_PAYLOAD); |
| 144 | |
Julius Werner | 965846f | 2021-01-11 16:07:02 -0800 | [diff] [blame] | 145 | if (prog_locate_hook(payload)) |
| 146 | goto out; |
| 147 | |
Raul E Rangel | 571e7f0 | 2021-11-02 11:51:48 -0600 | [diff] [blame^] | 148 | payload->cbfs_type = CBFS_TYPE_QUERY; |
| 149 | mapping = cbfs_type_map(prog_name(payload), NULL, &payload->cbfs_type); |
Raul E Rangel | 67798cf | 2021-07-02 17:07:05 -0600 | [diff] [blame] | 150 | |
Raul E Rangel | 571e7f0 | 2021-11-02 11:51:48 -0600 | [diff] [blame^] | 151 | if (!mapping) |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 152 | goto out; |
| 153 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 154 | switch (prog_cbfs_type(payload)) { |
| 155 | case CBFS_TYPE_SELF: /* Simple ELF */ |
Raul E Rangel | 571e7f0 | 2021-11-02 11:51:48 -0600 | [diff] [blame^] | 156 | selfload_mapped(payload, mapping, BM_MEM_RAM); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 157 | break; |
| 158 | case CBFS_TYPE_FIT: /* Flattened image tree */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 159 | if (CONFIG(PAYLOAD_FIT_SUPPORT)) { |
Raul E Rangel | 571e7f0 | 2021-11-02 11:51:48 -0600 | [diff] [blame^] | 160 | fit_payload(payload, mapping); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 161 | break; |
| 162 | } /* else fall-through */ |
| 163 | default: |
Keith Short | 7006458 | 2019-05-06 16:12:57 -0600 | [diff] [blame] | 164 | die_with_post_code(POST_INVALID_ROM, |
Julius Werner | 965846f | 2021-01-11 16:07:02 -0800 | [diff] [blame] | 165 | "Unsupported payload type %d.\n", payload->cbfs_type); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 166 | break; |
| 167 | } |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 168 | |
Raul E Rangel | 571e7f0 | 2021-11-02 11:51:48 -0600 | [diff] [blame^] | 169 | cbfs_unmap(mapping); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 170 | out: |
| 171 | if (prog_entry(payload) == NULL) |
Keith Short | 7006458 | 2019-05-06 16:12:57 -0600 | [diff] [blame] | 172 | die_with_post_code(POST_INVALID_ROM, "Payload not loaded.\n"); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void payload_run(void) |
| 176 | { |
| 177 | struct prog *payload = &global_payload; |
| 178 | |
| 179 | /* Reset to booting from this image as late as possible */ |
| 180 | boot_successful(); |
| 181 | |
| 182 | printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n", |
| 183 | prog_entry(payload), prog_entry_arg(payload)); |
| 184 | |
| 185 | post_code(POST_ENTER_ELF_BOOT); |
| 186 | |
| 187 | timestamp_add_now(TS_SELFBOOT_JUMP); |
| 188 | |
| 189 | /* Before we go off to run the payload, see if |
| 190 | * we stayed within our bounds. |
| 191 | */ |
| 192 | checkstack(_estack, 0); |
| 193 | |
| 194 | prog_run(payload); |
| 195 | } |
Stefan Reinauer | eec2db4 | 2015-06-18 01:19:50 -0700 | [diff] [blame] | 196 | |
| 197 | #endif |