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