blob: 128869b2bad8bca4db3b2dd457c52964c1a7d550 [file] [log] [blame]
Aaron Durbin899d13d2015-05-15 23:39:23 -05001/*
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 Durbin899d13d2015-05-15 23:39:23 -050014 */
15
16
17#include <stdlib.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050018#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 Durbin8cd723b2016-10-28 17:32:24 -050025#include <reset.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050026#include <romstage_handoff.h>
27#include <rmodule.h>
28#include <rules.h>
29#include <stage_cache.h>
30#include <symbols.h>
31#include <timestamp.h>
32
Aaron Durbin6a452ef2015-05-19 16:25:20 -050033/* Only can represent up to 1 byte less than size_t. */
Antonello Dettorie5f48d22016-06-22 21:09:08 +020034const struct mem_region_device addrspace_32bit =
35 MEM_REGION_DEV_RO_INIT(0, ~0UL);
Aaron Durbin6a452ef2015-05-19 16:25:20 -050036
Aaron Durbin6d720f32015-12-08 17:00:23 -060037int 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
46 cbfs_file_data(prog_rdev(prog), &file);
47
48 return 0;
49}
50
Aaron Durbin899d13d2015-05-15 23:39:23 -050051void run_romstage(void)
52{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050053 struct prog romstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060054 PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050055
56 if (prog_locate(&romstage))
57 goto fail;
58
59 timestamp_add_now(TS_START_COPYROM);
60
61 if (cbfs_prog_stage_load(&romstage))
62 goto fail;
63
64 timestamp_add_now(TS_END_COPYROM);
65
66 prog_run(&romstage);
67
68fail:
69 if (IS_ENABLED(CONFIG_BOOTBLOCK_CONSOLE))
70 die("Couldn't load romstage.\n");
71 halt();
72}
73
Aaron Durbin54546c92015-08-05 00:52:13 -050074void __attribute__((weak)) stage_cache_add(int stage_id,
75 const struct prog *stage) {}
Aaron Durbin899d13d2015-05-15 23:39:23 -050076void __attribute__((weak)) stage_cache_load_stage(int stage_id,
77 struct prog *stage) {}
Aaron Durbin8cd723b2016-10-28 17:32:24 -050078
79static void ramstage_cache_invalid(void)
80{
81 printk(BIOS_ERR, "ramstage cache invalid.\n");
82 if (IS_ENABLED(CONFIG_RESET_ON_INVALID_RAMSTAGE_CACHE)) {
83 hard_reset();
84 halt();
85 }
86}
Aaron Durbin899d13d2015-05-15 23:39:23 -050087
Aaron Durbin6c191d82016-11-29 21:22:42 -060088static void run_ramstage_from_resume(struct prog *ramstage)
Aaron Durbin899d13d2015-05-15 23:39:23 -050089{
Aaron Durbin6c191d82016-11-29 21:22:42 -060090 if (!romstage_handoff_is_resume())
91 return;
Aaron Durbin899d13d2015-05-15 23:39:23 -050092
Aaron Durbin6c191d82016-11-29 21:22:42 -060093 /* Load the cached ramstage to runtime location. */
94 stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
95
96 if (prog_entry(ramstage) != NULL) {
97 printk(BIOS_DEBUG, "Jumping to image.\n");
98 prog_run(ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -050099 }
Aaron Durbin6c191d82016-11-29 21:22:42 -0600100 ramstage_cache_invalid();
Aaron Durbin899d13d2015-05-15 23:39:23 -0500101}
102
103static int load_relocatable_ramstage(struct prog *ramstage)
104{
105 struct rmod_stage_load rmod_ram = {
106 .cbmem_id = CBMEM_ID_RAMSTAGE,
107 .prog = ramstage,
108 };
109
110 return rmodule_stage_load(&rmod_ram);
111}
112
Kyösti Mälkki9d6f3652016-06-28 07:38:46 +0300113static int load_nonrelocatable_ramstage(struct prog *ramstage)
114{
115 if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) {
116 uintptr_t base = 0;
117 size_t size = cbfs_prog_stage_section(ramstage, &base);
118 if (size)
119 backup_ramstage_section(base, size);
120 }
121
122 return cbfs_prog_stage_load(ramstage);
123}
124
Aaron Durbin899d13d2015-05-15 23:39:23 -0500125void run_ramstage(void)
126{
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500127 struct prog ramstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600128 PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500129
Aaron Durbin9796f602015-09-23 19:54:12 -0500130 timestamp_add_now(TS_END_ROMSTAGE);
131
Furquan Shaikh1e162bf2016-05-06 09:20:35 -0700132 /*
133 * Only x86 systems using ramstage stage cache currently take the same
134 * firmware path on resume.
135 */
136 if (IS_ENABLED(CONFIG_ARCH_X86) &&
137 !IS_ENABLED(CONFIG_NO_STAGE_CACHE) &&
138 IS_ENABLED(CONFIG_EARLY_CBMEM_INIT))
Aaron Durbin6c191d82016-11-29 21:22:42 -0600139 run_ramstage_from_resume(&ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500140
141 if (prog_locate(&ramstage))
142 goto fail;
143
144 timestamp_add_now(TS_START_COPYRAM);
145
146 if (IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE)) {
147 if (load_relocatable_ramstage(&ramstage))
148 goto fail;
Kyösti Mälkki9d6f3652016-06-28 07:38:46 +0300149 } else if (load_nonrelocatable_ramstage(&ramstage))
Aaron Durbin899d13d2015-05-15 23:39:23 -0500150 goto fail;
151
152 stage_cache_add(STAGE_RAMSTAGE, &ramstage);
153
154 timestamp_add_now(TS_END_COPYRAM);
155
156 prog_run(&ramstage);
157
158fail:
159 die("Ramstage was not loaded!\n");
160}
161
Stefan Reinauereec2db42015-06-18 01:19:50 -0700162#ifdef __RAMSTAGE__ // gc-sections should take care of this
163
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500164static struct prog global_payload =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600165 PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500166
167void __attribute__((weak)) mirror_payload(struct prog *payload)
168{
Aaron Durbin899d13d2015-05-15 23:39:23 -0500169}
170
171void payload_load(void)
172{
173 struct prog *payload = &global_payload;
174
175 timestamp_add_now(TS_LOAD_PAYLOAD);
176
177 if (prog_locate(payload))
178 goto out;
179
180 mirror_payload(payload);
181
182 /* Pass cbtables to payload if architecture desires it. */
Simon Glass7ae73fc2016-08-27 12:18:38 -0600183 prog_set_entry(payload, selfload(payload, true),
Aaron Durbin899d13d2015-05-15 23:39:23 -0500184 cbmem_find(CBMEM_ID_CBTABLE));
185
186out:
187 if (prog_entry(payload) == NULL)
188 die("Payload not loaded.\n");
189}
190
191void 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 Reinauereec2db42015-06-18 01:19:50 -0700212
213#endif