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