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