blob: 1a361ea3b9600eba8078656b4fc91416644ba084 [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>
Raul E Rangel67798cf2021-07-02 17:07:05 -060016#include <thread.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050017#include <timestamp.h>
Wim Vervoorn1058dd82019-11-01 10:22:22 +010018#include <security/vboot/vboot_common.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050019
Aaron Durbin899d13d2015-05-15 23:39:23 -050020void run_romstage(void)
21{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050022 struct prog romstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060023 PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050024
Wim Vervoorn1058dd82019-11-01 10:22:22 +010025 vboot_run_logic();
26
Aaron Durbin899d13d2015-05-15 23:39:23 -050027 timestamp_add_now(TS_START_COPYROM);
28
Julius Werner1de87082020-12-23 17:38:11 -080029 if (ENV_X86 && CONFIG(BOOTBLOCK_NORMAL)) {
30 if (legacy_romstage_select_and_load(&romstage))
31 goto fail;
32 } else {
33 if (cbfs_prog_stage_load(&romstage))
34 goto fail;
35 }
Aaron Durbin899d13d2015-05-15 23:39:23 -050036
37 timestamp_add_now(TS_END_COPYROM);
38
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020039 console_time_report();
40
Aaron Durbin899d13d2015-05-15 23:39:23 -050041 prog_run(&romstage);
42
43fail:
Julius Wernercd49cce2019-03-05 16:53:33 -080044 if (CONFIG(BOOTBLOCK_CONSOLE))
Keith Short70064582019-05-06 16:12:57 -060045 die_with_post_code(POST_INVALID_ROM,
46 "Couldn't load romstage.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -050047 halt();
48}
49
Frans Hendriksfc580342019-06-14 14:36:37 +020050int __weak prog_locate_hook(struct prog *prog) { return 0; }
51
Aaron Durbin6c191d82016-11-29 21:22:42 -060052static void run_ramstage_from_resume(struct prog *ramstage)
Aaron Durbin899d13d2015-05-15 23:39:23 -050053{
Aaron Durbin6c191d82016-11-29 21:22:42 -060054 /* Load the cached ramstage to runtime location. */
55 stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
56
Julius Werner1de87082020-12-23 17:38:11 -080057 ramstage->cbfs_type = CBFS_TYPE_STAGE;
Arthur Heymans5331a7c2019-10-23 17:07:15 +020058 prog_set_arg(ramstage, cbmem_top());
59
Aaron Durbin6c191d82016-11-29 21:22:42 -060060 if (prog_entry(ramstage) != NULL) {
61 printk(BIOS_DEBUG, "Jumping to image.\n");
62 prog_run(ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -050063 }
Kyösti Mälkki4f14cd82019-12-18 19:40:48 +020064
65 printk(BIOS_ERR, "ramstage cache invalid.\n");
66 board_reset();
Aaron Durbin899d13d2015-05-15 23:39:23 -050067}
68
69static int load_relocatable_ramstage(struct prog *ramstage)
70{
71 struct rmod_stage_load rmod_ram = {
72 .cbmem_id = CBMEM_ID_RAMSTAGE,
73 .prog = ramstage,
74 };
75
76 return rmodule_stage_load(&rmod_ram);
77}
78
79void run_ramstage(void)
80{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050081 struct prog ramstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060082 PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050083
Subrata Banik4f42eea2019-03-05 16:45:14 +053084 if (ENV_POSTCAR)
85 timestamp_add_now(TS_END_POSTCAR);
86
Subrata Banik2847e1e2019-02-25 20:31:22 +053087 /* Call "end of romstage" here if postcar stage doesn't exist */
88 if (ENV_ROMSTAGE)
89 timestamp_add_now(TS_END_ROMSTAGE);
Aaron Durbin9796f602015-09-23 19:54:12 -050090
Furquan Shaikh1e162bf2016-05-06 09:20:35 -070091 /*
92 * Only x86 systems using ramstage stage cache currently take the same
93 * firmware path on resume.
94 */
Kyösti Mälkkie0165fb2021-01-09 13:30:57 +020095 if (ENV_X86 && resume_from_stage_cache())
Aaron Durbin6c191d82016-11-29 21:22:42 -060096 run_ramstage_from_resume(&ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -050097
Wim Vervoorn1058dd82019-11-01 10:22:22 +010098 vboot_run_logic();
99
Aaron Durbin899d13d2015-05-15 23:39:23 -0500100 timestamp_add_now(TS_START_COPYRAM);
101
Nico Huber06b68d12020-07-06 11:02:53 +0200102 if (ENV_X86) {
103 if (load_relocatable_ramstage(&ramstage))
104 goto fail;
105 } else {
106 if (cbfs_prog_stage_load(&ramstage))
107 goto fail;
108 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500109
Subrata Banik90f750b2019-06-11 17:52:06 +0530110 stage_cache_add(STAGE_RAMSTAGE, &ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500111
112 timestamp_add_now(TS_END_COPYRAM);
113
Kyösti Mälkki45ddb432019-11-02 14:12:18 +0200114 console_time_report();
115
Arthur Heymans5331a7c2019-10-23 17:07:15 +0200116 /* This overrides the arg fetched from the relocatable module */
117 prog_set_arg(&ramstage, cbmem_top());
118
Aaron Durbin899d13d2015-05-15 23:39:23 -0500119 prog_run(&ramstage);
120
121fail:
Keith Short70064582019-05-06 16:12:57 -0600122 die_with_post_code(POST_INVALID_ROM, "Ramstage was not loaded!\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500123}
124
Subrata Banik42c44c22019-05-15 20:27:04 +0530125#if ENV_PAYLOAD_LOADER // gc-sections should take care of this
Stefan Reinauereec2db42015-06-18 01:19:50 -0700126
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500127static struct prog global_payload =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600128 PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500129
Raul E Rangel67798cf2021-07-02 17:07:05 -0600130static struct thread_handle payload_preload_handle;
131
132static enum cb_err payload_preload_thread_entry(void *arg)
133{
134 size_t size;
135 struct prog *payload = &global_payload;
136
137 printk(BIOS_DEBUG, "Preloading payload\n");
138
139 payload->cbfs_type = CBFS_TYPE_QUERY;
140
141 size = cbfs_type_load(prog_name(payload), _payload_preload_cache,
142 REGION_SIZE(payload_preload_cache), &payload->cbfs_type);
143
144 if (!size) {
145 printk(BIOS_ERR, "ERROR: Preloading payload failed\n");
146 return CB_ERR;
147 }
148
149 printk(BIOS_DEBUG, "Preloading payload complete\n");
150
151 return CB_SUCCESS;
152}
153
154void payload_preload(void)
155{
156 struct thread_handle *handle = &payload_preload_handle;
157
158 if (!CONFIG(PAYLOAD_PRELOAD))
159 return;
160
161 if (thread_run(handle, payload_preload_thread_entry, NULL))
162 printk(BIOS_ERR, "ERROR: Failed to start payload preload thread\n");
163}
164
Aaron Durbin899d13d2015-05-15 23:39:23 -0500165void payload_load(void)
166{
167 struct prog *payload = &global_payload;
Raul E Rangel67798cf2021-07-02 17:07:05 -0600168 struct thread_handle *handle = &payload_preload_handle;
169 void *mapping = NULL;
170 void *buffer;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500171
172 timestamp_add_now(TS_LOAD_PAYLOAD);
173
Julius Werner965846f2021-01-11 16:07:02 -0800174 if (prog_locate_hook(payload))
175 goto out;
176
Raul E Rangel67798cf2021-07-02 17:07:05 -0600177 if (CONFIG(PAYLOAD_PRELOAD) && thread_join(handle) == CB_SUCCESS) {
178 buffer = _payload_preload_cache;
179 } else {
180 payload->cbfs_type = CBFS_TYPE_QUERY;
181 mapping = cbfs_type_map(prog_name(payload), NULL, &payload->cbfs_type);
182 buffer = mapping;
183 }
184
185 if (!buffer)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500186 goto out;
187
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200188 switch (prog_cbfs_type(payload)) {
189 case CBFS_TYPE_SELF: /* Simple ELF */
Raul E Rangel67798cf2021-07-02 17:07:05 -0600190 selfload_mapped(payload, buffer, BM_MEM_RAM);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200191 break;
192 case CBFS_TYPE_FIT: /* Flattened image tree */
Julius Wernercd49cce2019-03-05 16:53:33 -0800193 if (CONFIG(PAYLOAD_FIT_SUPPORT)) {
Raul E Rangel67798cf2021-07-02 17:07:05 -0600194 fit_payload(payload, buffer);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200195 break;
196 } /* else fall-through */
197 default:
Keith Short70064582019-05-06 16:12:57 -0600198 die_with_post_code(POST_INVALID_ROM,
Julius Werner965846f2021-01-11 16:07:02 -0800199 "Unsupported payload type %d.\n", payload->cbfs_type);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200200 break;
201 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500202
Raul E Rangel67798cf2021-07-02 17:07:05 -0600203 if (mapping)
204 cbfs_unmap(mapping);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500205out:
206 if (prog_entry(payload) == NULL)
Keith Short70064582019-05-06 16:12:57 -0600207 die_with_post_code(POST_INVALID_ROM, "Payload not loaded.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500208}
209
210void payload_run(void)
211{
212 struct prog *payload = &global_payload;
213
214 /* Reset to booting from this image as late as possible */
215 boot_successful();
216
217 printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n",
218 prog_entry(payload), prog_entry_arg(payload));
219
220 post_code(POST_ENTER_ELF_BOOT);
221
222 timestamp_add_now(TS_SELFBOOT_JUMP);
223
224 /* Before we go off to run the payload, see if
225 * we stayed within our bounds.
226 */
227 checkstack(_estack, 0);
228
229 prog_run(payload);
230}
Stefan Reinauereec2db42015-06-18 01:19:50 -0700231
232#endif