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> |
Aaron Durbin | 6403167 | 2018-04-21 14:45:32 -0600 | [diff] [blame^] | 20 | #include <compiler.h> |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 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> |
| 29 | #include <rules.h> |
| 30 | #include <stage_cache.h> |
| 31 | #include <symbols.h> |
| 32 | #include <timestamp.h> |
| 33 | |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 34 | /* Only can represent up to 1 byte less than size_t. */ |
Antonello Dettori | e5f48d2 | 2016-06-22 21:09:08 +0200 | [diff] [blame] | 35 | const struct mem_region_device addrspace_32bit = |
| 36 | MEM_REGION_DEV_RO_INIT(0, ~0UL); |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 37 | |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 38 | int prog_locate(struct prog *prog) |
| 39 | { |
| 40 | struct cbfsf file; |
| 41 | |
| 42 | cbfs_prepare_program_locate(); |
| 43 | |
| 44 | if (cbfs_boot_locate(&file, prog_name(prog), NULL)) |
| 45 | return -1; |
| 46 | |
| 47 | cbfs_file_data(prog_rdev(prog), &file); |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 52 | void run_romstage(void) |
| 53 | { |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 54 | struct prog romstage = |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 55 | PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage"); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 56 | |
| 57 | if (prog_locate(&romstage)) |
| 58 | goto fail; |
| 59 | |
| 60 | timestamp_add_now(TS_START_COPYROM); |
| 61 | |
| 62 | if (cbfs_prog_stage_load(&romstage)) |
| 63 | goto fail; |
| 64 | |
| 65 | timestamp_add_now(TS_END_COPYROM); |
| 66 | |
| 67 | prog_run(&romstage); |
| 68 | |
| 69 | fail: |
| 70 | if (IS_ENABLED(CONFIG_BOOTBLOCK_CONSOLE)) |
| 71 | die("Couldn't load romstage.\n"); |
| 72 | halt(); |
| 73 | } |
| 74 | |
Aaron Durbin | 6403167 | 2018-04-21 14:45:32 -0600 | [diff] [blame^] | 75 | void __weak stage_cache_add(int stage_id, |
Aaron Durbin | 54546c9 | 2015-08-05 00:52:13 -0500 | [diff] [blame] | 76 | const struct prog *stage) {} |
Aaron Durbin | 6403167 | 2018-04-21 14:45:32 -0600 | [diff] [blame^] | 77 | void __weak stage_cache_load_stage(int stage_id, |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 78 | struct prog *stage) {} |
Aaron Durbin | 8cd723b | 2016-10-28 17:32:24 -0500 | [diff] [blame] | 79 | |
| 80 | static void ramstage_cache_invalid(void) |
| 81 | { |
| 82 | printk(BIOS_ERR, "ramstage cache invalid.\n"); |
| 83 | if (IS_ENABLED(CONFIG_RESET_ON_INVALID_RAMSTAGE_CACHE)) { |
| 84 | hard_reset(); |
| 85 | halt(); |
| 86 | } |
| 87 | } |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 88 | |
Aaron Durbin | 6c191d8 | 2016-11-29 21:22:42 -0600 | [diff] [blame] | 89 | static void run_ramstage_from_resume(struct prog *ramstage) |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 90 | { |
Aaron Durbin | 6c191d8 | 2016-11-29 21:22:42 -0600 | [diff] [blame] | 91 | if (!romstage_handoff_is_resume()) |
| 92 | return; |
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 | /* Load the cached ramstage to runtime location. */ |
| 95 | stage_cache_load_stage(STAGE_RAMSTAGE, ramstage); |
| 96 | |
| 97 | if (prog_entry(ramstage) != NULL) { |
| 98 | printk(BIOS_DEBUG, "Jumping to image.\n"); |
| 99 | prog_run(ramstage); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 100 | } |
Aaron Durbin | 6c191d8 | 2016-11-29 21:22:42 -0600 | [diff] [blame] | 101 | ramstage_cache_invalid(); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static int load_relocatable_ramstage(struct prog *ramstage) |
| 105 | { |
| 106 | struct rmod_stage_load rmod_ram = { |
| 107 | .cbmem_id = CBMEM_ID_RAMSTAGE, |
| 108 | .prog = ramstage, |
| 109 | }; |
| 110 | |
| 111 | return rmodule_stage_load(&rmod_ram); |
| 112 | } |
| 113 | |
Kyösti Mälkki | 9d6f365 | 2016-06-28 07:38:46 +0300 | [diff] [blame] | 114 | static int load_nonrelocatable_ramstage(struct prog *ramstage) |
| 115 | { |
| 116 | if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) { |
| 117 | uintptr_t base = 0; |
| 118 | size_t size = cbfs_prog_stage_section(ramstage, &base); |
| 119 | if (size) |
| 120 | backup_ramstage_section(base, size); |
| 121 | } |
| 122 | |
| 123 | return cbfs_prog_stage_load(ramstage); |
| 124 | } |
| 125 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 126 | void run_ramstage(void) |
| 127 | { |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 128 | struct prog ramstage = |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 129 | PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage"); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 130 | |
Aaron Durbin | 9796f60 | 2015-09-23 19:54:12 -0500 | [diff] [blame] | 131 | timestamp_add_now(TS_END_ROMSTAGE); |
| 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 | */ |
| 137 | if (IS_ENABLED(CONFIG_ARCH_X86) && |
| 138 | !IS_ENABLED(CONFIG_NO_STAGE_CACHE) && |
| 139 | IS_ENABLED(CONFIG_EARLY_CBMEM_INIT)) |
Aaron Durbin | 6c191d8 | 2016-11-29 21:22:42 -0600 | [diff] [blame] | 140 | run_ramstage_from_resume(&ramstage); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 141 | |
| 142 | if (prog_locate(&ramstage)) |
| 143 | goto fail; |
| 144 | |
| 145 | timestamp_add_now(TS_START_COPYRAM); |
| 146 | |
| 147 | if (IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE)) { |
| 148 | if (load_relocatable_ramstage(&ramstage)) |
| 149 | goto fail; |
Kyösti Mälkki | 9d6f365 | 2016-06-28 07:38:46 +0300 | [diff] [blame] | 150 | } else if (load_nonrelocatable_ramstage(&ramstage)) |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 151 | goto fail; |
| 152 | |
| 153 | stage_cache_add(STAGE_RAMSTAGE, &ramstage); |
| 154 | |
| 155 | timestamp_add_now(TS_END_COPYRAM); |
| 156 | |
| 157 | prog_run(&ramstage); |
| 158 | |
| 159 | fail: |
| 160 | die("Ramstage was not loaded!\n"); |
| 161 | } |
| 162 | |
Stefan Reinauer | eec2db4 | 2015-06-18 01:19:50 -0700 | [diff] [blame] | 163 | #ifdef __RAMSTAGE__ // gc-sections should take care of this |
| 164 | |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 165 | static struct prog global_payload = |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 166 | PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload"); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 167 | |
Aaron Durbin | 6403167 | 2018-04-21 14:45:32 -0600 | [diff] [blame^] | 168 | void __weak mirror_payload(struct prog *payload) |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 169 | { |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void payload_load(void) |
| 173 | { |
| 174 | struct prog *payload = &global_payload; |
| 175 | |
| 176 | timestamp_add_now(TS_LOAD_PAYLOAD); |
| 177 | |
| 178 | if (prog_locate(payload)) |
| 179 | goto out; |
| 180 | |
| 181 | mirror_payload(payload); |
| 182 | |
| 183 | /* Pass cbtables to payload if architecture desires it. */ |
Simon Glass | 7ae73fc | 2016-08-27 12:18:38 -0600 | [diff] [blame] | 184 | prog_set_entry(payload, selfload(payload, true), |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 185 | cbmem_find(CBMEM_ID_CBTABLE)); |
| 186 | |
| 187 | out: |
| 188 | if (prog_entry(payload) == NULL) |
| 189 | die("Payload not loaded.\n"); |
| 190 | } |
| 191 | |
| 192 | void payload_run(void) |
| 193 | { |
| 194 | struct prog *payload = &global_payload; |
| 195 | |
| 196 | /* Reset to booting from this image as late as possible */ |
| 197 | boot_successful(); |
| 198 | |
| 199 | printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n", |
| 200 | prog_entry(payload), prog_entry_arg(payload)); |
| 201 | |
| 202 | post_code(POST_ENTER_ELF_BOOT); |
| 203 | |
| 204 | timestamp_add_now(TS_SELFBOOT_JUMP); |
| 205 | |
| 206 | /* Before we go off to run the payload, see if |
| 207 | * we stayed within our bounds. |
| 208 | */ |
| 209 | checkstack(_estack, 0); |
| 210 | |
| 211 | prog_run(payload); |
| 212 | } |
Stefan Reinauer | eec2db4 | 2015-06-18 01:19:50 -0700 | [diff] [blame] | 213 | |
| 214 | #endif |