blob: b31d79341f5509f13b34ed7887138bfba42755fd [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 <rmodule.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050014#include <stage_cache.h>
15#include <symbols.h>
16#include <timestamp.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020017#include <fit_payload.h>
Wim Vervoorn1058dd82019-11-01 10:22:22 +010018#include <security/vboot/vboot_common.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050019
Aaron Durbin6a452ef2015-05-19 16:25:20 -050020/* Only can represent up to 1 byte less than size_t. */
Antonello Dettorie5f48d22016-06-22 21:09:08 +020021const struct mem_region_device addrspace_32bit =
22 MEM_REGION_DEV_RO_INIT(0, ~0UL);
Aaron Durbin6a452ef2015-05-19 16:25:20 -050023
Aaron Durbin6d720f32015-12-08 17:00:23 -060024int prog_locate(struct prog *prog)
25{
26 struct cbfsf file;
27
Frans Hendriksfc580342019-06-14 14:36:37 +020028 if (prog_locate_hook(prog))
29 return -1;
30
Aaron Durbin6d720f32015-12-08 17:00:23 -060031 if (cbfs_boot_locate(&file, prog_name(prog), NULL))
32 return -1;
33
Patrick Rudolph71327fb2018-05-03 10:35:26 +020034 cbfsf_file_type(&file, &prog->cbfs_type);
35
Aaron Durbin6d720f32015-12-08 17:00:23 -060036 cbfs_file_data(prog_rdev(prog), &file);
37
38 return 0;
39}
40
Aaron Durbin899d13d2015-05-15 23:39:23 -050041void run_romstage(void)
42{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050043 struct prog romstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060044 PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050045
Wim Vervoorn1058dd82019-11-01 10:22:22 +010046 vboot_run_logic();
47
Aaron Durbin899d13d2015-05-15 23:39:23 -050048 timestamp_add_now(TS_START_COPYROM);
49
Julius Werner1de87082020-12-23 17:38:11 -080050 if (ENV_X86 && CONFIG(BOOTBLOCK_NORMAL)) {
51 if (legacy_romstage_select_and_load(&romstage))
52 goto fail;
53 } else {
54 if (cbfs_prog_stage_load(&romstage))
55 goto fail;
56 }
Aaron Durbin899d13d2015-05-15 23:39:23 -050057
58 timestamp_add_now(TS_END_COPYROM);
59
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020060 console_time_report();
61
Aaron Durbin899d13d2015-05-15 23:39:23 -050062 prog_run(&romstage);
63
64fail:
Julius Wernercd49cce2019-03-05 16:53:33 -080065 if (CONFIG(BOOTBLOCK_CONSOLE))
Keith Short70064582019-05-06 16:12:57 -060066 die_with_post_code(POST_INVALID_ROM,
67 "Couldn't load romstage.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -050068 halt();
69}
70
Frans Hendriksfc580342019-06-14 14:36:37 +020071int __weak prog_locate_hook(struct prog *prog) { return 0; }
72
Aaron Durbin6c191d82016-11-29 21:22:42 -060073static void run_ramstage_from_resume(struct prog *ramstage)
Aaron Durbin899d13d2015-05-15 23:39:23 -050074{
Aaron Durbin6c191d82016-11-29 21:22:42 -060075 /* Load the cached ramstage to runtime location. */
76 stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
77
Julius Werner1de87082020-12-23 17:38:11 -080078 ramstage->cbfs_type = CBFS_TYPE_STAGE;
Arthur Heymans5331a7c2019-10-23 17:07:15 +020079 prog_set_arg(ramstage, cbmem_top());
80
Aaron Durbin6c191d82016-11-29 21:22:42 -060081 if (prog_entry(ramstage) != NULL) {
82 printk(BIOS_DEBUG, "Jumping to image.\n");
83 prog_run(ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -050084 }
Kyösti Mälkki4f14cd82019-12-18 19:40:48 +020085
86 printk(BIOS_ERR, "ramstage cache invalid.\n");
87 board_reset();
Aaron Durbin899d13d2015-05-15 23:39:23 -050088}
89
90static int load_relocatable_ramstage(struct prog *ramstage)
91{
92 struct rmod_stage_load rmod_ram = {
93 .cbmem_id = CBMEM_ID_RAMSTAGE,
94 .prog = ramstage,
95 };
96
97 return rmodule_stage_load(&rmod_ram);
98}
99
100void run_ramstage(void)
101{
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500102 struct prog ramstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600103 PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500104
Subrata Banik4f42eea2019-03-05 16:45:14 +0530105 if (ENV_POSTCAR)
106 timestamp_add_now(TS_END_POSTCAR);
107
Subrata Banik2847e1e2019-02-25 20:31:22 +0530108 /* Call "end of romstage" here if postcar stage doesn't exist */
109 if (ENV_ROMSTAGE)
110 timestamp_add_now(TS_END_ROMSTAGE);
Aaron Durbin9796f602015-09-23 19:54:12 -0500111
Furquan Shaikh1e162bf2016-05-06 09:20:35 -0700112 /*
113 * Only x86 systems using ramstage stage cache currently take the same
114 * firmware path on resume.
115 */
Kyösti Mälkkie0165fb2021-01-09 13:30:57 +0200116 if (ENV_X86 && resume_from_stage_cache())
Aaron Durbin6c191d82016-11-29 21:22:42 -0600117 run_ramstage_from_resume(&ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500118
Wim Vervoorn1058dd82019-11-01 10:22:22 +0100119 vboot_run_logic();
120
Aaron Durbin899d13d2015-05-15 23:39:23 -0500121 timestamp_add_now(TS_START_COPYRAM);
122
Nico Huber06b68d12020-07-06 11:02:53 +0200123 if (ENV_X86) {
124 if (load_relocatable_ramstage(&ramstage))
125 goto fail;
126 } else {
127 if (cbfs_prog_stage_load(&ramstage))
128 goto fail;
129 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500130
Subrata Banik90f750b2019-06-11 17:52:06 +0530131 stage_cache_add(STAGE_RAMSTAGE, &ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500132
133 timestamp_add_now(TS_END_COPYRAM);
134
Kyösti Mälkki45ddb432019-11-02 14:12:18 +0200135 console_time_report();
136
Arthur Heymans5331a7c2019-10-23 17:07:15 +0200137 /* This overrides the arg fetched from the relocatable module */
138 prog_set_arg(&ramstage, cbmem_top());
139
Aaron Durbin899d13d2015-05-15 23:39:23 -0500140 prog_run(&ramstage);
141
142fail:
Keith Short70064582019-05-06 16:12:57 -0600143 die_with_post_code(POST_INVALID_ROM, "Ramstage was not loaded!\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500144}
145
Subrata Banik42c44c22019-05-15 20:27:04 +0530146#if ENV_PAYLOAD_LOADER // gc-sections should take care of this
Stefan Reinauereec2db42015-06-18 01:19:50 -0700147
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500148static struct prog global_payload =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600149 PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500150
Aaron Durbin899d13d2015-05-15 23:39:23 -0500151void payload_load(void)
152{
153 struct prog *payload = &global_payload;
154
155 timestamp_add_now(TS_LOAD_PAYLOAD);
156
157 if (prog_locate(payload))
158 goto out;
159
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200160 switch (prog_cbfs_type(payload)) {
161 case CBFS_TYPE_SELF: /* Simple ELF */
Ting Shen05532262019-01-28 17:22:22 +0800162 selfload_check(payload, BM_MEM_RAM);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200163 break;
164 case CBFS_TYPE_FIT: /* Flattened image tree */
Julius Wernercd49cce2019-03-05 16:53:33 -0800165 if (CONFIG(PAYLOAD_FIT_SUPPORT)) {
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200166 fit_payload(payload);
167 break;
168 } /* else fall-through */
169 default:
Keith Short70064582019-05-06 16:12:57 -0600170 die_with_post_code(POST_INVALID_ROM,
171 "Unsupported payload type.\n");
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200172 break;
173 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500174
175out:
176 if (prog_entry(payload) == NULL)
Keith Short70064582019-05-06 16:12:57 -0600177 die_with_post_code(POST_INVALID_ROM, "Payload not loaded.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500178}
179
180void payload_run(void)
181{
182 struct prog *payload = &global_payload;
183
184 /* Reset to booting from this image as late as possible */
185 boot_successful();
186
187 printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n",
188 prog_entry(payload), prog_entry_arg(payload));
189
190 post_code(POST_ENTER_ELF_BOOT);
191
192 timestamp_add_now(TS_SELFBOOT_JUMP);
193
194 /* Before we go off to run the payload, see if
195 * we stayed within our bounds.
196 */
197 checkstack(_estack, 0);
198
199 prog_run(payload);
200}
Stefan Reinauereec2db42015-06-18 01:19:50 -0700201
202#endif