blob: c336575434ef9639290e748d8f2b7d88e76339f0 [file] [log] [blame]
Angel Pons118a9c72020-04-02 23:48:34 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Aaron Durbin899d13d2015-05-15 23:39:23 -05003
4
5#include <stdlib.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -05006#include <cbfs.h>
7#include <cbmem.h>
8#include <console/console.h>
9#include <fallback.h>
10#include <halt.h>
11#include <lib.h>
12#include <program_loading.h>
Aaron Durbin8cd723b2016-10-28 17:32:24 -050013#include <reset.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050014#include <romstage_handoff.h>
15#include <rmodule.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050016#include <stage_cache.h>
17#include <symbols.h>
18#include <timestamp.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020019#include <fit_payload.h>
Wim Vervoorn1058dd82019-11-01 10:22:22 +010020#include <security/vboot/vboot_common.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050021
Aaron Durbin6a452ef2015-05-19 16:25:20 -050022/* Only can represent up to 1 byte less than size_t. */
Antonello Dettorie5f48d22016-06-22 21:09:08 +020023const struct mem_region_device addrspace_32bit =
24 MEM_REGION_DEV_RO_INIT(0, ~0UL);
Aaron Durbin6a452ef2015-05-19 16:25:20 -050025
Aaron Durbin6d720f32015-12-08 17:00:23 -060026int prog_locate(struct prog *prog)
27{
28 struct cbfsf file;
29
Frans Hendriksfc580342019-06-14 14:36:37 +020030 if (prog_locate_hook(prog))
31 return -1;
32
Aaron Durbin6d720f32015-12-08 17:00:23 -060033 if (cbfs_boot_locate(&file, prog_name(prog), NULL))
34 return -1;
35
Patrick Rudolph71327fb2018-05-03 10:35:26 +020036 cbfsf_file_type(&file, &prog->cbfs_type);
37
Aaron Durbin6d720f32015-12-08 17:00:23 -060038 cbfs_file_data(prog_rdev(prog), &file);
39
40 return 0;
41}
42
Aaron Durbin899d13d2015-05-15 23:39:23 -050043void run_romstage(void)
44{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050045 struct prog romstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060046 PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050047
Wim Vervoorn1058dd82019-11-01 10:22:22 +010048 vboot_run_logic();
49
Kyösti Mälkkib8d575c2019-12-16 16:00:49 +020050 if (CONFIG(ARCH_X86) && CONFIG(BOOTBLOCK_NORMAL)) {
51 if (legacy_romstage_selector(&romstage))
52 goto fail;
53 } else {
54 if (prog_locate(&romstage))
55 goto fail;
56 }
Aaron Durbin899d13d2015-05-15 23:39:23 -050057
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
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020065 console_time_report();
66
Aaron Durbin899d13d2015-05-15 23:39:23 -050067 prog_run(&romstage);
68
69fail:
Julius Wernercd49cce2019-03-05 16:53:33 -080070 if (CONFIG(BOOTBLOCK_CONSOLE))
Keith Short70064582019-05-06 16:12:57 -060071 die_with_post_code(POST_INVALID_ROM,
72 "Couldn't load romstage.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -050073 halt();
74}
75
Frans Hendriksfc580342019-06-14 14:36:37 +020076int __weak prog_locate_hook(struct prog *prog) { return 0; }
77
Aaron Durbin6c191d82016-11-29 21:22:42 -060078static void run_ramstage_from_resume(struct prog *ramstage)
Aaron Durbin899d13d2015-05-15 23:39:23 -050079{
Aaron Durbin6c191d82016-11-29 21:22:42 -060080 if (!romstage_handoff_is_resume())
81 return;
Aaron Durbin899d13d2015-05-15 23:39:23 -050082
Aaron Durbin6c191d82016-11-29 21:22:42 -060083 /* Load the cached ramstage to runtime location. */
84 stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
85
Arthur Heymans5331a7c2019-10-23 17:07:15 +020086 prog_set_arg(ramstage, cbmem_top());
87
Aaron Durbin6c191d82016-11-29 21:22:42 -060088 if (prog_entry(ramstage) != NULL) {
89 printk(BIOS_DEBUG, "Jumping to image.\n");
90 prog_run(ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -050091 }
Kyösti Mälkki4f14cd82019-12-18 19:40:48 +020092
93 printk(BIOS_ERR, "ramstage cache invalid.\n");
94 board_reset();
Aaron Durbin899d13d2015-05-15 23:39:23 -050095}
96
97static int load_relocatable_ramstage(struct prog *ramstage)
98{
99 struct rmod_stage_load rmod_ram = {
100 .cbmem_id = CBMEM_ID_RAMSTAGE,
101 .prog = ramstage,
102 };
103
104 return rmodule_stage_load(&rmod_ram);
105}
106
107void run_ramstage(void)
108{
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500109 struct prog ramstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600110 PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500111
Subrata Banik4f42eea2019-03-05 16:45:14 +0530112 if (ENV_POSTCAR)
113 timestamp_add_now(TS_END_POSTCAR);
114
Subrata Banik2847e1e2019-02-25 20:31:22 +0530115 /* Call "end of romstage" here if postcar stage doesn't exist */
116 if (ENV_ROMSTAGE)
117 timestamp_add_now(TS_END_ROMSTAGE);
Aaron Durbin9796f602015-09-23 19:54:12 -0500118
Furquan Shaikh1e162bf2016-05-06 09:20:35 -0700119 /*
120 * Only x86 systems using ramstage stage cache currently take the same
121 * firmware path on resume.
122 */
Julius Wernercd49cce2019-03-05 16:53:33 -0800123 if (CONFIG(ARCH_X86) &&
124 !CONFIG(NO_STAGE_CACHE))
Aaron Durbin6c191d82016-11-29 21:22:42 -0600125 run_ramstage_from_resume(&ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500126
Wim Vervoorn1058dd82019-11-01 10:22:22 +0100127 vboot_run_logic();
128
Aaron Durbin899d13d2015-05-15 23:39:23 -0500129 if (prog_locate(&ramstage))
130 goto fail;
131
132 timestamp_add_now(TS_START_COPYRAM);
133
Julius Wernercd49cce2019-03-05 16:53:33 -0800134 if (CONFIG(RELOCATABLE_RAMSTAGE)) {
Aaron Durbin899d13d2015-05-15 23:39:23 -0500135 if (load_relocatable_ramstage(&ramstage))
136 goto fail;
Kyösti Mälkki7cd2c072018-06-03 23:04:28 +0300137 } else if (cbfs_prog_stage_load(&ramstage))
Aaron Durbin899d13d2015-05-15 23:39:23 -0500138 goto fail;
139
Subrata Banik90f750b2019-06-11 17:52:06 +0530140 stage_cache_add(STAGE_RAMSTAGE, &ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500141
142 timestamp_add_now(TS_END_COPYRAM);
143
Kyösti Mälkki45ddb432019-11-02 14:12:18 +0200144 console_time_report();
145
Arthur Heymans5331a7c2019-10-23 17:07:15 +0200146 /* This overrides the arg fetched from the relocatable module */
147 prog_set_arg(&ramstage, cbmem_top());
148
Aaron Durbin899d13d2015-05-15 23:39:23 -0500149 prog_run(&ramstage);
150
151fail:
Keith Short70064582019-05-06 16:12:57 -0600152 die_with_post_code(POST_INVALID_ROM, "Ramstage was not loaded!\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500153}
154
Subrata Banik42c44c22019-05-15 20:27:04 +0530155#if ENV_PAYLOAD_LOADER // gc-sections should take care of this
Stefan Reinauereec2db42015-06-18 01:19:50 -0700156
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500157static struct prog global_payload =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600158 PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500159
Aaron Durbin899d13d2015-05-15 23:39:23 -0500160void payload_load(void)
161{
162 struct prog *payload = &global_payload;
163
164 timestamp_add_now(TS_LOAD_PAYLOAD);
165
166 if (prog_locate(payload))
167 goto out;
168
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200169 switch (prog_cbfs_type(payload)) {
170 case CBFS_TYPE_SELF: /* Simple ELF */
Ting Shen05532262019-01-28 17:22:22 +0800171 selfload_check(payload, BM_MEM_RAM);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200172 break;
173 case CBFS_TYPE_FIT: /* Flattened image tree */
Julius Wernercd49cce2019-03-05 16:53:33 -0800174 if (CONFIG(PAYLOAD_FIT_SUPPORT)) {
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200175 fit_payload(payload);
176 break;
177 } /* else fall-through */
178 default:
Keith Short70064582019-05-06 16:12:57 -0600179 die_with_post_code(POST_INVALID_ROM,
180 "Unsupported payload type.\n");
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200181 break;
182 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500183
184out:
185 if (prog_entry(payload) == NULL)
Keith Short70064582019-05-06 16:12:57 -0600186 die_with_post_code(POST_INVALID_ROM, "Payload not loaded.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500187}
188
189void payload_run(void)
190{
191 struct prog *payload = &global_payload;
192
193 /* Reset to booting from this image as late as possible */
194 boot_successful();
195
196 printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n",
197 prog_entry(payload), prog_entry_arg(payload));
198
199 post_code(POST_ENTER_ELF_BOOT);
200
201 timestamp_add_now(TS_SELFBOOT_JUMP);
202
203 /* Before we go off to run the payload, see if
204 * we stayed within our bounds.
205 */
206 checkstack(_estack, 0);
207
208 prog_run(payload);
209}
Stefan Reinauereec2db42015-06-18 01:19:50 -0700210
211#endif