blob: a21663fc0fb32d72b660e1e1531f60a041f69567 [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>
Aaron Durbin899d13d2015-05-15 23:39:23 -050028#include <stage_cache.h>
29#include <symbols.h>
30#include <timestamp.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020031#include <fit_payload.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050032
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
Patrick Rudolph71327fb2018-05-03 10:35:26 +020046 cbfsf_file_type(&file, &prog->cbfs_type);
47
Aaron Durbin6d720f32015-12-08 17:00:23 -060048 cbfs_file_data(prog_rdev(prog), &file);
49
50 return 0;
51}
52
Aaron Durbin899d13d2015-05-15 23:39:23 -050053void run_romstage(void)
54{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050055 struct prog romstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060056 PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050057
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
70fail:
Julius Wernercd49cce2019-03-05 16:53:33 -080071 if (CONFIG(BOOTBLOCK_CONSOLE))
Keith Short70064582019-05-06 16:12:57 -060072 die_with_post_code(POST_INVALID_ROM,
73 "Couldn't load romstage.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -050074 halt();
75}
76
Aaron Durbin64031672018-04-21 14:45:32 -060077void __weak stage_cache_add(int stage_id,
Aaron Durbin54546c92015-08-05 00:52:13 -050078 const struct prog *stage) {}
Aaron Durbin64031672018-04-21 14:45:32 -060079void __weak stage_cache_load_stage(int stage_id,
Aaron Durbin899d13d2015-05-15 23:39:23 -050080 struct prog *stage) {}
Aaron Durbin8cd723b2016-10-28 17:32:24 -050081
82static void ramstage_cache_invalid(void)
83{
84 printk(BIOS_ERR, "ramstage cache invalid.\n");
Julius Wernercd49cce2019-03-05 16:53:33 -080085 if (CONFIG(RESET_ON_INVALID_RAMSTAGE_CACHE)) {
Nico Huber4f32b642018-10-05 23:40:21 +020086 board_reset();
Aaron Durbin8cd723b2016-10-28 17:32:24 -050087 }
88}
Aaron Durbin899d13d2015-05-15 23:39:23 -050089
Aaron Durbin6c191d82016-11-29 21:22:42 -060090static void run_ramstage_from_resume(struct prog *ramstage)
Aaron Durbin899d13d2015-05-15 23:39:23 -050091{
Aaron Durbin6c191d82016-11-29 21:22:42 -060092 if (!romstage_handoff_is_resume())
93 return;
Aaron Durbin899d13d2015-05-15 23:39:23 -050094
Aaron Durbin6c191d82016-11-29 21:22:42 -060095 /* Load the cached ramstage to runtime location. */
96 stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
97
98 if (prog_entry(ramstage) != NULL) {
99 printk(BIOS_DEBUG, "Jumping to image.\n");
100 prog_run(ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500101 }
Aaron Durbin6c191d82016-11-29 21:22:42 -0600102 ramstage_cache_invalid();
Aaron Durbin899d13d2015-05-15 23:39:23 -0500103}
104
105static int load_relocatable_ramstage(struct prog *ramstage)
106{
107 struct rmod_stage_load rmod_ram = {
108 .cbmem_id = CBMEM_ID_RAMSTAGE,
109 .prog = ramstage,
110 };
111
112 return rmodule_stage_load(&rmod_ram);
113}
114
Kyösti Mälkki9d6f3652016-06-28 07:38:46 +0300115static int load_nonrelocatable_ramstage(struct prog *ramstage)
116{
Julius Wernercd49cce2019-03-05 16:53:33 -0800117 if (CONFIG(HAVE_ACPI_RESUME)) {
Kyösti Mälkki9d6f3652016-06-28 07:38:46 +0300118 uintptr_t base = 0;
119 size_t size = cbfs_prog_stage_section(ramstage, &base);
120 if (size)
121 backup_ramstage_section(base, size);
122 }
123
124 return cbfs_prog_stage_load(ramstage);
125}
126
Aaron Durbin899d13d2015-05-15 23:39:23 -0500127void run_ramstage(void)
128{
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500129 struct prog ramstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600130 PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500131
Subrata Banik4f42eea2019-03-05 16:45:14 +0530132 if (ENV_POSTCAR)
133 timestamp_add_now(TS_END_POSTCAR);
134
Subrata Banik2847e1e2019-02-25 20:31:22 +0530135 /* Call "end of romstage" here if postcar stage doesn't exist */
136 if (ENV_ROMSTAGE)
137 timestamp_add_now(TS_END_ROMSTAGE);
Aaron Durbin9796f602015-09-23 19:54:12 -0500138
Furquan Shaikh1e162bf2016-05-06 09:20:35 -0700139 /*
140 * Only x86 systems using ramstage stage cache currently take the same
141 * firmware path on resume.
142 */
Julius Wernercd49cce2019-03-05 16:53:33 -0800143 if (CONFIG(ARCH_X86) &&
144 !CONFIG(NO_STAGE_CACHE))
Aaron Durbin6c191d82016-11-29 21:22:42 -0600145 run_ramstage_from_resume(&ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500146
147 if (prog_locate(&ramstage))
148 goto fail;
149
150 timestamp_add_now(TS_START_COPYRAM);
151
Julius Wernercd49cce2019-03-05 16:53:33 -0800152 if (CONFIG(RELOCATABLE_RAMSTAGE)) {
Aaron Durbin899d13d2015-05-15 23:39:23 -0500153 if (load_relocatable_ramstage(&ramstage))
154 goto fail;
Kyösti Mälkki9d6f3652016-06-28 07:38:46 +0300155 } else if (load_nonrelocatable_ramstage(&ramstage))
Aaron Durbin899d13d2015-05-15 23:39:23 -0500156 goto fail;
157
Julius Wernercd49cce2019-03-05 16:53:33 -0800158 if (!CONFIG(NO_STAGE_CACHE))
Kyösti Mälkkia8c0cb32018-06-25 15:38:45 +0300159 stage_cache_add(STAGE_RAMSTAGE, &ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500160
161 timestamp_add_now(TS_END_COPYRAM);
162
163 prog_run(&ramstage);
164
165fail:
Keith Short70064582019-05-06 16:12:57 -0600166 die_with_post_code(POST_INVALID_ROM, "Ramstage was not loaded!\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500167}
168
Stefan Reinauereec2db42015-06-18 01:19:50 -0700169#ifdef __RAMSTAGE__ // gc-sections should take care of this
170
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500171static struct prog global_payload =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600172 PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500173
Aaron Durbin64031672018-04-21 14:45:32 -0600174void __weak mirror_payload(struct prog *payload)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500175{
Aaron Durbin899d13d2015-05-15 23:39:23 -0500176}
177
178void payload_load(void)
179{
180 struct prog *payload = &global_payload;
181
182 timestamp_add_now(TS_LOAD_PAYLOAD);
183
184 if (prog_locate(payload))
185 goto out;
186
187 mirror_payload(payload);
188
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200189 switch (prog_cbfs_type(payload)) {
190 case CBFS_TYPE_SELF: /* Simple ELF */
Ting Shen05532262019-01-28 17:22:22 +0800191 selfload_check(payload, BM_MEM_RAM);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200192 break;
193 case CBFS_TYPE_FIT: /* Flattened image tree */
Julius Wernercd49cce2019-03-05 16:53:33 -0800194 if (CONFIG(PAYLOAD_FIT_SUPPORT)) {
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200195 fit_payload(payload);
196 break;
197 } /* else fall-through */
198 default:
Keith Short70064582019-05-06 16:12:57 -0600199 die_with_post_code(POST_INVALID_ROM,
200 "Unsupported payload type.\n");
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200201 break;
202 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500203
204out:
205 if (prog_entry(payload) == NULL)
Keith Short70064582019-05-06 16:12:57 -0600206 die_with_post_code(POST_INVALID_ROM, "Payload not loaded.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500207}
208
209void payload_run(void)
210{
211 struct prog *payload = &global_payload;
212
213 /* Reset to booting from this image as late as possible */
214 boot_successful();
215
216 printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n",
217 prog_entry(payload), prog_entry_arg(payload));
218
219 post_code(POST_ENTER_ELF_BOOT);
220
221 timestamp_add_now(TS_SELFBOOT_JUMP);
222
223 /* Before we go off to run the payload, see if
224 * we stayed within our bounds.
225 */
226 checkstack(_estack, 0);
227
228 prog_run(payload);
229}
Stefan Reinauereec2db42015-06-18 01:19:50 -0700230
231#endif