blob: 883dbdc7ecaaa616c4dbf5d03417882016a09704 [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>
Patrick Rudolph59b8f272018-04-26 09:53:16 +020032#include <cbfs.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020033#include <fit_payload.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050034
Aaron Durbin6a452ef2015-05-19 16:25:20 -050035/* Only can represent up to 1 byte less than size_t. */
Antonello Dettorie5f48d22016-06-22 21:09:08 +020036const struct mem_region_device addrspace_32bit =
37 MEM_REGION_DEV_RO_INIT(0, ~0UL);
Aaron Durbin6a452ef2015-05-19 16:25:20 -050038
Aaron Durbin6d720f32015-12-08 17:00:23 -060039int prog_locate(struct prog *prog)
40{
41 struct cbfsf file;
42
43 cbfs_prepare_program_locate();
44
45 if (cbfs_boot_locate(&file, prog_name(prog), NULL))
46 return -1;
47
Patrick Rudolph71327fb2018-05-03 10:35:26 +020048 cbfsf_file_type(&file, &prog->cbfs_type);
49
Aaron Durbin6d720f32015-12-08 17:00:23 -060050 cbfs_file_data(prog_rdev(prog), &file);
51
52 return 0;
53}
54
Aaron Durbin899d13d2015-05-15 23:39:23 -050055void run_romstage(void)
56{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050057 struct prog romstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060058 PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050059
60 if (prog_locate(&romstage))
61 goto fail;
62
63 timestamp_add_now(TS_START_COPYROM);
64
65 if (cbfs_prog_stage_load(&romstage))
66 goto fail;
67
68 timestamp_add_now(TS_END_COPYROM);
69
70 prog_run(&romstage);
71
72fail:
73 if (IS_ENABLED(CONFIG_BOOTBLOCK_CONSOLE))
74 die("Couldn't load romstage.\n");
75 halt();
76}
77
Aaron Durbin64031672018-04-21 14:45:32 -060078void __weak stage_cache_add(int stage_id,
Aaron Durbin54546c92015-08-05 00:52:13 -050079 const struct prog *stage) {}
Aaron Durbin64031672018-04-21 14:45:32 -060080void __weak stage_cache_load_stage(int stage_id,
Aaron Durbin899d13d2015-05-15 23:39:23 -050081 struct prog *stage) {}
Aaron Durbin8cd723b2016-10-28 17:32:24 -050082
83static void ramstage_cache_invalid(void)
84{
85 printk(BIOS_ERR, "ramstage cache invalid.\n");
86 if (IS_ENABLED(CONFIG_RESET_ON_INVALID_RAMSTAGE_CACHE)) {
Nico Huber4f32b642018-10-05 23:40:21 +020087 board_reset();
Aaron Durbin8cd723b2016-10-28 17:32:24 -050088 }
89}
Aaron Durbin899d13d2015-05-15 23:39:23 -050090
Aaron Durbin6c191d82016-11-29 21:22:42 -060091static void run_ramstage_from_resume(struct prog *ramstage)
Aaron Durbin899d13d2015-05-15 23:39:23 -050092{
Aaron Durbin6c191d82016-11-29 21:22:42 -060093 if (!romstage_handoff_is_resume())
94 return;
Aaron Durbin899d13d2015-05-15 23:39:23 -050095
Aaron Durbin6c191d82016-11-29 21:22:42 -060096 /* Load the cached ramstage to runtime location. */
97 stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
98
99 if (prog_entry(ramstage) != NULL) {
100 printk(BIOS_DEBUG, "Jumping to image.\n");
101 prog_run(ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500102 }
Aaron Durbin6c191d82016-11-29 21:22:42 -0600103 ramstage_cache_invalid();
Aaron Durbin899d13d2015-05-15 23:39:23 -0500104}
105
106static int load_relocatable_ramstage(struct prog *ramstage)
107{
108 struct rmod_stage_load rmod_ram = {
109 .cbmem_id = CBMEM_ID_RAMSTAGE,
110 .prog = ramstage,
111 };
112
113 return rmodule_stage_load(&rmod_ram);
114}
115
Kyösti Mälkki9d6f3652016-06-28 07:38:46 +0300116static int load_nonrelocatable_ramstage(struct prog *ramstage)
117{
118 if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) {
119 uintptr_t base = 0;
120 size_t size = cbfs_prog_stage_section(ramstage, &base);
121 if (size)
122 backup_ramstage_section(base, size);
123 }
124
125 return cbfs_prog_stage_load(ramstage);
126}
127
Aaron Durbin899d13d2015-05-15 23:39:23 -0500128void run_ramstage(void)
129{
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500130 struct prog ramstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600131 PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500132
Aaron Durbin9796f602015-09-23 19:54:12 -0500133 timestamp_add_now(TS_END_ROMSTAGE);
134
Furquan Shaikh1e162bf2016-05-06 09:20:35 -0700135 /*
136 * Only x86 systems using ramstage stage cache currently take the same
137 * firmware path on resume.
138 */
139 if (IS_ENABLED(CONFIG_ARCH_X86) &&
140 !IS_ENABLED(CONFIG_NO_STAGE_CACHE) &&
141 IS_ENABLED(CONFIG_EARLY_CBMEM_INIT))
Aaron Durbin6c191d82016-11-29 21:22:42 -0600142 run_ramstage_from_resume(&ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500143
144 if (prog_locate(&ramstage))
145 goto fail;
146
147 timestamp_add_now(TS_START_COPYRAM);
148
149 if (IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE)) {
150 if (load_relocatable_ramstage(&ramstage))
151 goto fail;
Kyösti Mälkki9d6f3652016-06-28 07:38:46 +0300152 } else if (load_nonrelocatable_ramstage(&ramstage))
Aaron Durbin899d13d2015-05-15 23:39:23 -0500153 goto fail;
154
Kyösti Mälkkia8c0cb32018-06-25 15:38:45 +0300155 if (!IS_ENABLED(CONFIG_NO_STAGE_CACHE))
156 stage_cache_add(STAGE_RAMSTAGE, &ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500157
158 timestamp_add_now(TS_END_COPYRAM);
159
160 prog_run(&ramstage);
161
162fail:
163 die("Ramstage was not loaded!\n");
164}
165
Stefan Reinauereec2db42015-06-18 01:19:50 -0700166#ifdef __RAMSTAGE__ // gc-sections should take care of this
167
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500168static struct prog global_payload =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600169 PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500170
Aaron Durbin64031672018-04-21 14:45:32 -0600171void __weak mirror_payload(struct prog *payload)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500172{
Aaron Durbin899d13d2015-05-15 23:39:23 -0500173}
174
175void payload_load(void)
176{
177 struct prog *payload = &global_payload;
178
179 timestamp_add_now(TS_LOAD_PAYLOAD);
180
181 if (prog_locate(payload))
182 goto out;
183
184 mirror_payload(payload);
185
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200186 switch (prog_cbfs_type(payload)) {
187 case CBFS_TYPE_SELF: /* Simple ELF */
188 selfload(payload, true);
189 break;
190 case CBFS_TYPE_FIT: /* Flattened image tree */
191 if (IS_ENABLED(CONFIG_PAYLOAD_FIT_SUPPORT)) {
192 fit_payload(payload);
193 break;
194 } /* else fall-through */
195 default:
196 die("Unsupported payload type.\n");
197 break;
198 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500199
200out:
201 if (prog_entry(payload) == NULL)
202 die("Payload not loaded.\n");
203}
204
205void payload_run(void)
206{
207 struct prog *payload = &global_payload;
208
209 /* Reset to booting from this image as late as possible */
210 boot_successful();
211
212 printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n",
213 prog_entry(payload), prog_entry_arg(payload));
214
215 post_code(POST_ENTER_ELF_BOOT);
216
217 timestamp_add_now(TS_SELFBOOT_JUMP);
218
219 /* Before we go off to run the payload, see if
220 * we stayed within our bounds.
221 */
222 checkstack(_estack, 0);
223
224 prog_run(payload);
225}
Stefan Reinauereec2db42015-06-18 01:19:50 -0700226
227#endif