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> |
| 25 | #include <romstage_handoff.h> |
| 26 | #include <rmodule.h> |
| 27 | #include <rules.h> |
| 28 | #include <stage_cache.h> |
| 29 | #include <symbols.h> |
| 30 | #include <timestamp.h> |
| 31 | |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 32 | /* Only can represent up to 1 byte less than size_t. */ |
Antonello Dettori | e5f48d2 | 2016-06-22 21:09:08 +0200 | [diff] [blame] | 33 | const struct mem_region_device addrspace_32bit = |
| 34 | MEM_REGION_DEV_RO_INIT(0, ~0UL); |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 35 | |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 36 | int prog_locate(struct prog *prog) |
| 37 | { |
| 38 | struct cbfsf file; |
| 39 | |
| 40 | cbfs_prepare_program_locate(); |
| 41 | |
| 42 | if (cbfs_boot_locate(&file, prog_name(prog), NULL)) |
| 43 | return -1; |
| 44 | |
| 45 | cbfs_file_data(prog_rdev(prog), &file); |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 50 | void run_romstage(void) |
| 51 | { |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 52 | struct prog romstage = |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 53 | PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage"); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 54 | |
| 55 | if (prog_locate(&romstage)) |
| 56 | goto fail; |
| 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 | |
| 65 | prog_run(&romstage); |
| 66 | |
| 67 | fail: |
| 68 | if (IS_ENABLED(CONFIG_BOOTBLOCK_CONSOLE)) |
| 69 | die("Couldn't load romstage.\n"); |
| 70 | halt(); |
| 71 | } |
| 72 | |
Aaron Durbin | 54546c9 | 2015-08-05 00:52:13 -0500 | [diff] [blame] | 73 | void __attribute__((weak)) stage_cache_add(int stage_id, |
| 74 | const struct prog *stage) {} |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 75 | void __attribute__((weak)) stage_cache_load_stage(int stage_id, |
| 76 | struct prog *stage) {} |
| 77 | void __attribute__((weak)) ramstage_cache_invalid(void) {} |
| 78 | |
| 79 | static void run_ramstage_from_resume(struct romstage_handoff *handoff, |
| 80 | struct prog *ramstage) |
| 81 | { |
| 82 | if (handoff != NULL && handoff->s3_resume) { |
| 83 | /* Load the cached ramstage to runtime location. */ |
| 84 | stage_cache_load_stage(STAGE_RAMSTAGE, ramstage); |
| 85 | |
| 86 | if (prog_entry(ramstage) != NULL) { |
| 87 | printk(BIOS_DEBUG, "Jumping to image.\n"); |
| 88 | prog_run(ramstage); |
| 89 | } |
| 90 | ramstage_cache_invalid(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static int load_relocatable_ramstage(struct prog *ramstage) |
| 95 | { |
| 96 | struct rmod_stage_load rmod_ram = { |
| 97 | .cbmem_id = CBMEM_ID_RAMSTAGE, |
| 98 | .prog = ramstage, |
| 99 | }; |
| 100 | |
| 101 | return rmodule_stage_load(&rmod_ram); |
| 102 | } |
| 103 | |
| 104 | void run_ramstage(void) |
| 105 | { |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 106 | struct prog ramstage = |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 107 | PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage"); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 108 | |
Aaron Durbin | 9796f60 | 2015-09-23 19:54:12 -0500 | [diff] [blame] | 109 | timestamp_add_now(TS_END_ROMSTAGE); |
| 110 | |
Furquan Shaikh | 1e162bf | 2016-05-06 09:20:35 -0700 | [diff] [blame] | 111 | /* |
| 112 | * Only x86 systems using ramstage stage cache currently take the same |
| 113 | * firmware path on resume. |
| 114 | */ |
| 115 | if (IS_ENABLED(CONFIG_ARCH_X86) && |
| 116 | !IS_ENABLED(CONFIG_NO_STAGE_CACHE) && |
| 117 | IS_ENABLED(CONFIG_EARLY_CBMEM_INIT)) |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 118 | run_ramstage_from_resume(romstage_handoff_find_or_add(), |
| 119 | &ramstage); |
| 120 | |
| 121 | if (prog_locate(&ramstage)) |
| 122 | goto fail; |
| 123 | |
| 124 | timestamp_add_now(TS_START_COPYRAM); |
| 125 | |
| 126 | if (IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE)) { |
| 127 | if (load_relocatable_ramstage(&ramstage)) |
| 128 | goto fail; |
| 129 | } else if (cbfs_prog_stage_load(&ramstage)) |
| 130 | goto fail; |
| 131 | |
| 132 | stage_cache_add(STAGE_RAMSTAGE, &ramstage); |
| 133 | |
| 134 | timestamp_add_now(TS_END_COPYRAM); |
| 135 | |
| 136 | prog_run(&ramstage); |
| 137 | |
| 138 | fail: |
| 139 | die("Ramstage was not loaded!\n"); |
| 140 | } |
| 141 | |
Stefan Reinauer | eec2db4 | 2015-06-18 01:19:50 -0700 | [diff] [blame] | 142 | #ifdef __RAMSTAGE__ // gc-sections should take care of this |
| 143 | |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 144 | static struct prog global_payload = |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 145 | PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload"); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 146 | |
| 147 | void __attribute__((weak)) mirror_payload(struct prog *payload) |
| 148 | { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | void payload_load(void) |
| 153 | { |
| 154 | struct prog *payload = &global_payload; |
| 155 | |
| 156 | timestamp_add_now(TS_LOAD_PAYLOAD); |
| 157 | |
| 158 | if (prog_locate(payload)) |
| 159 | goto out; |
| 160 | |
| 161 | mirror_payload(payload); |
| 162 | |
| 163 | /* Pass cbtables to payload if architecture desires it. */ |
| 164 | prog_set_entry(payload, selfload(payload), |
| 165 | cbmem_find(CBMEM_ID_CBTABLE)); |
| 166 | |
| 167 | out: |
| 168 | if (prog_entry(payload) == NULL) |
| 169 | die("Payload not loaded.\n"); |
| 170 | } |
| 171 | |
| 172 | void payload_run(void) |
| 173 | { |
| 174 | struct prog *payload = &global_payload; |
| 175 | |
| 176 | /* Reset to booting from this image as late as possible */ |
| 177 | boot_successful(); |
| 178 | |
| 179 | printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n", |
| 180 | prog_entry(payload), prog_entry_arg(payload)); |
| 181 | |
| 182 | post_code(POST_ENTER_ELF_BOOT); |
| 183 | |
| 184 | timestamp_add_now(TS_SELFBOOT_JUMP); |
| 185 | |
| 186 | /* Before we go off to run the payload, see if |
| 187 | * we stayed within our bounds. |
| 188 | */ |
| 189 | checkstack(_estack, 0); |
| 190 | |
| 191 | prog_run(payload); |
| 192 | } |
Stefan Reinauer | eec2db4 | 2015-06-18 01:19:50 -0700 | [diff] [blame] | 193 | |
| 194 | #endif |