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