blob: 93efc0a280655e02622120802169361a0ec887f0 [file] [log] [blame]
Angel Pons118a9c72020-04-02 23:48:34 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin899d13d2015-05-15 23:39:23 -05002
3
4#include <stdlib.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -05005#include <cbfs.h>
6#include <cbmem.h>
7#include <console/console.h>
8#include <fallback.h>
9#include <halt.h>
10#include <lib.h>
11#include <program_loading.h>
Aaron Durbin8cd723b2016-10-28 17:32:24 -050012#include <reset.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050013#include <romstage_handoff.h>
14#include <rmodule.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050015#include <stage_cache.h>
16#include <symbols.h>
17#include <timestamp.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020018#include <fit_payload.h>
Wim Vervoorn1058dd82019-11-01 10:22:22 +010019#include <security/vboot/vboot_common.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050020
Aaron Durbin6a452ef2015-05-19 16:25:20 -050021/* Only can represent up to 1 byte less than size_t. */
Antonello Dettorie5f48d22016-06-22 21:09:08 +020022const struct mem_region_device addrspace_32bit =
23 MEM_REGION_DEV_RO_INIT(0, ~0UL);
Aaron Durbin6a452ef2015-05-19 16:25:20 -050024
Aaron Durbin6d720f32015-12-08 17:00:23 -060025int prog_locate(struct prog *prog)
26{
27 struct cbfsf file;
28
Frans Hendriksfc580342019-06-14 14:36:37 +020029 if (prog_locate_hook(prog))
30 return -1;
31
Aaron Durbin6d720f32015-12-08 17:00:23 -060032 if (cbfs_boot_locate(&file, prog_name(prog), NULL))
33 return -1;
34
Patrick Rudolph71327fb2018-05-03 10:35:26 +020035 cbfsf_file_type(&file, &prog->cbfs_type);
36
Aaron Durbin6d720f32015-12-08 17:00:23 -060037 cbfs_file_data(prog_rdev(prog), &file);
38
39 return 0;
40}
41
Aaron Durbin899d13d2015-05-15 23:39:23 -050042void run_romstage(void)
43{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050044 struct prog romstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060045 PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050046
Wim Vervoorn1058dd82019-11-01 10:22:22 +010047 vboot_run_logic();
48
Kyösti Mälkki7336f972020-06-08 06:05:03 +030049 if (ENV_X86 && CONFIG(BOOTBLOCK_NORMAL)) {
Kyösti Mälkkib8d575c2019-12-16 16:00:49 +020050 if (legacy_romstage_selector(&romstage))
51 goto fail;
52 } else {
53 if (prog_locate(&romstage))
54 goto fail;
55 }
Aaron Durbin899d13d2015-05-15 23:39:23 -050056
57 timestamp_add_now(TS_START_COPYROM);
58
59 if (cbfs_prog_stage_load(&romstage))
60 goto fail;
61
62 timestamp_add_now(TS_END_COPYROM);
63
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020064 console_time_report();
65
Aaron Durbin899d13d2015-05-15 23:39:23 -050066 prog_run(&romstage);
67
68fail:
Julius Wernercd49cce2019-03-05 16:53:33 -080069 if (CONFIG(BOOTBLOCK_CONSOLE))
Keith Short70064582019-05-06 16:12:57 -060070 die_with_post_code(POST_INVALID_ROM,
71 "Couldn't load romstage.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -050072 halt();
73}
74
Frans Hendriksfc580342019-06-14 14:36:37 +020075int __weak prog_locate_hook(struct prog *prog) { return 0; }
76
Aaron Durbin6c191d82016-11-29 21:22:42 -060077static void run_ramstage_from_resume(struct prog *ramstage)
Aaron Durbin899d13d2015-05-15 23:39:23 -050078{
Aaron Durbin6c191d82016-11-29 21:22:42 -060079 if (!romstage_handoff_is_resume())
80 return;
Aaron Durbin899d13d2015-05-15 23:39:23 -050081
Aaron Durbin6c191d82016-11-29 21:22:42 -060082 /* Load the cached ramstage to runtime location. */
83 stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
84
Arthur Heymans5331a7c2019-10-23 17:07:15 +020085 prog_set_arg(ramstage, cbmem_top());
86
Aaron Durbin6c191d82016-11-29 21:22:42 -060087 if (prog_entry(ramstage) != NULL) {
88 printk(BIOS_DEBUG, "Jumping to image.\n");
89 prog_run(ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -050090 }
Kyösti Mälkki4f14cd82019-12-18 19:40:48 +020091
92 printk(BIOS_ERR, "ramstage cache invalid.\n");
93 board_reset();
Aaron Durbin899d13d2015-05-15 23:39:23 -050094}
95
96static int load_relocatable_ramstage(struct prog *ramstage)
97{
98 struct rmod_stage_load rmod_ram = {
99 .cbmem_id = CBMEM_ID_RAMSTAGE,
100 .prog = ramstage,
101 };
102
103 return rmodule_stage_load(&rmod_ram);
104}
105
106void run_ramstage(void)
107{
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500108 struct prog ramstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600109 PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500110
Subrata Banik4f42eea2019-03-05 16:45:14 +0530111 if (ENV_POSTCAR)
112 timestamp_add_now(TS_END_POSTCAR);
113
Subrata Banik2847e1e2019-02-25 20:31:22 +0530114 /* Call "end of romstage" here if postcar stage doesn't exist */
115 if (ENV_ROMSTAGE)
116 timestamp_add_now(TS_END_ROMSTAGE);
Aaron Durbin9796f602015-09-23 19:54:12 -0500117
Furquan Shaikh1e162bf2016-05-06 09:20:35 -0700118 /*
119 * Only x86 systems using ramstage stage cache currently take the same
120 * firmware path on resume.
121 */
Kyösti Mälkki7336f972020-06-08 06:05:03 +0300122 if (ENV_X86 && !CONFIG(NO_STAGE_CACHE))
Aaron Durbin6c191d82016-11-29 21:22:42 -0600123 run_ramstage_from_resume(&ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500124
Wim Vervoorn1058dd82019-11-01 10:22:22 +0100125 vboot_run_logic();
126
Aaron Durbin899d13d2015-05-15 23:39:23 -0500127 if (prog_locate(&ramstage))
128 goto fail;
129
130 timestamp_add_now(TS_START_COPYRAM);
131
Nico Huber06b68d12020-07-06 11:02:53 +0200132 if (ENV_X86) {
133 if (load_relocatable_ramstage(&ramstage))
134 goto fail;
135 } else {
136 if (cbfs_prog_stage_load(&ramstage))
137 goto fail;
138 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500139
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