blob: 0139ca4099e3f2a9397c310c84d52351009d356d [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
Aaron Durbin899d13d2015-05-15 23:39:23 -05003#include <cbfs.h>
4#include <cbmem.h>
5#include <console/console.h>
6#include <fallback.h>
7#include <halt.h>
8#include <lib.h>
9#include <program_loading.h>
Aaron Durbin8cd723b2016-10-28 17:32:24 -050010#include <reset.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050011#include <rmodule.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050012#include <stage_cache.h>
13#include <symbols.h>
Raul E Rangel67798cf2021-07-02 17:07:05 -060014#include <thread.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050015#include <timestamp.h>
Wim Vervoorn1058dd82019-11-01 10:22:22 +010016#include <security/vboot/vboot_common.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050017
Aaron Durbin899d13d2015-05-15 23:39:23 -050018void run_romstage(void)
19{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050020 struct prog romstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060021 PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050022
Wim Vervoorn1058dd82019-11-01 10:22:22 +010023 vboot_run_logic();
24
Jakub Czapigaad6157e2022-02-15 11:50:31 +010025 timestamp_add_now(TS_COPYROM_START);
Aaron Durbin899d13d2015-05-15 23:39:23 -050026
Julius Werner1de87082020-12-23 17:38:11 -080027 if (ENV_X86 && CONFIG(BOOTBLOCK_NORMAL)) {
Julius Werner797a1102022-03-07 18:54:47 -080028 if (legacy_romstage_select_and_load(&romstage) != CB_SUCCESS)
Julius Werner1de87082020-12-23 17:38:11 -080029 goto fail;
30 } else {
31 if (cbfs_prog_stage_load(&romstage))
32 goto fail;
33 }
Aaron Durbin899d13d2015-05-15 23:39:23 -050034
Jakub Czapigaad6157e2022-02-15 11:50:31 +010035 timestamp_add_now(TS_COPYROM_END);
Aaron Durbin899d13d2015-05-15 23:39:23 -050036
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020037 console_time_report();
38
Aaron Durbin899d13d2015-05-15 23:39:23 -050039 prog_run(&romstage);
40
41fail:
Julius Wernercd49cce2019-03-05 16:53:33 -080042 if (CONFIG(BOOTBLOCK_CONSOLE))
Keith Short70064582019-05-06 16:12:57 -060043 die_with_post_code(POST_INVALID_ROM,
44 "Couldn't load romstage.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -050045 halt();
46}
47
Frans Hendriksfc580342019-06-14 14:36:37 +020048int __weak prog_locate_hook(struct prog *prog) { return 0; }
49
Aaron Durbin6c191d82016-11-29 21:22:42 -060050static void run_ramstage_from_resume(struct prog *ramstage)
Aaron Durbin899d13d2015-05-15 23:39:23 -050051{
Aaron Durbin6c191d82016-11-29 21:22:42 -060052 /* Load the cached ramstage to runtime location. */
53 stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
54
Julius Werner1de87082020-12-23 17:38:11 -080055 ramstage->cbfs_type = CBFS_TYPE_STAGE;
Arthur Heymans5331a7c2019-10-23 17:07:15 +020056 prog_set_arg(ramstage, cbmem_top());
57
Aaron Durbin6c191d82016-11-29 21:22:42 -060058 if (prog_entry(ramstage) != NULL) {
59 printk(BIOS_DEBUG, "Jumping to image.\n");
60 prog_run(ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -050061 }
Kyösti Mälkki4f14cd82019-12-18 19:40:48 +020062
63 printk(BIOS_ERR, "ramstage cache invalid.\n");
64 board_reset();
Aaron Durbin899d13d2015-05-15 23:39:23 -050065}
66
67static int load_relocatable_ramstage(struct prog *ramstage)
68{
69 struct rmod_stage_load rmod_ram = {
70 .cbmem_id = CBMEM_ID_RAMSTAGE,
71 .prog = ramstage,
72 };
73
74 return rmodule_stage_load(&rmod_ram);
75}
Raul E Rangelb25576f2021-11-05 10:29:24 -060076void preload_ramstage(void)
77{
78 if (!CONFIG(CBFS_PRELOAD))
79 return;
Aaron Durbin899d13d2015-05-15 23:39:23 -050080
Raul E Rangelb25576f2021-11-05 10:29:24 -060081 printk(BIOS_DEBUG, "Preloading ramstage\n");
82
83 cbfs_preload(CONFIG_CBFS_PREFIX "/ramstage");
84}
Aaron Durbin899d13d2015-05-15 23:39:23 -050085void run_ramstage(void)
86{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050087 struct prog ramstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060088 PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050089
Kyösti Mälkkif4b85382022-04-07 07:58:52 +030090 /* Call "end of romstage" here if postcar stage doesn't exist */
Subrata Banik4f42eea2019-03-05 16:45:14 +053091 if (ENV_POSTCAR)
Jakub Czapigaad6157e2022-02-15 11:50:31 +010092 timestamp_add_now(TS_POSTCAR_END);
Kyösti Mälkkif4b85382022-04-07 07:58:52 +030093 else
Jakub Czapigaad6157e2022-02-15 11:50:31 +010094 timestamp_add_now(TS_ROMSTAGE_END);
Aaron Durbin9796f602015-09-23 19:54:12 -050095
Furquan Shaikh1e162bf2016-05-06 09:20:35 -070096 /*
97 * Only x86 systems using ramstage stage cache currently take the same
98 * firmware path on resume.
99 */
Kyösti Mälkkie0165fb2021-01-09 13:30:57 +0200100 if (ENV_X86 && resume_from_stage_cache())
Aaron Durbin6c191d82016-11-29 21:22:42 -0600101 run_ramstage_from_resume(&ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500102
Wim Vervoorn1058dd82019-11-01 10:22:22 +0100103 vboot_run_logic();
104
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100105 timestamp_add_now(TS_COPYRAM_START);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500106
Nico Huber06b68d12020-07-06 11:02:53 +0200107 if (ENV_X86) {
108 if (load_relocatable_ramstage(&ramstage))
109 goto fail;
110 } else {
111 if (cbfs_prog_stage_load(&ramstage))
112 goto fail;
113 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500114
Subrata Banik90f750b2019-06-11 17:52:06 +0530115 stage_cache_add(STAGE_RAMSTAGE, &ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500116
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100117 timestamp_add_now(TS_COPYRAM_END);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500118
Kyösti Mälkki45ddb432019-11-02 14:12:18 +0200119 console_time_report();
120
Arthur Heymans5331a7c2019-10-23 17:07:15 +0200121 /* This overrides the arg fetched from the relocatable module */
122 prog_set_arg(&ramstage, cbmem_top());
123
Aaron Durbin899d13d2015-05-15 23:39:23 -0500124 prog_run(&ramstage);
125
126fail:
Keith Short70064582019-05-06 16:12:57 -0600127 die_with_post_code(POST_INVALID_ROM, "Ramstage was not loaded!\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500128}
129
Subrata Banik42c44c22019-05-15 20:27:04 +0530130#if ENV_PAYLOAD_LOADER // gc-sections should take care of this
Stefan Reinauereec2db42015-06-18 01:19:50 -0700131
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500132static struct prog global_payload =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600133 PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500134
Raul E Rangel67798cf2021-07-02 17:07:05 -0600135void payload_preload(void)
136{
Raul E Rangel571e7f02021-11-02 11:51:48 -0600137 if (!CONFIG(CBFS_PRELOAD))
Raul E Rangel67798cf2021-07-02 17:07:05 -0600138 return;
139
Raul E Rangel571e7f02021-11-02 11:51:48 -0600140 cbfs_preload(global_payload.name);
Raul E Rangel67798cf2021-07-02 17:07:05 -0600141}
142
Aaron Durbin899d13d2015-05-15 23:39:23 -0500143void payload_load(void)
144{
145 struct prog *payload = &global_payload;
Raul E Rangel571e7f02021-11-02 11:51:48 -0600146 void *mapping;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500147
148 timestamp_add_now(TS_LOAD_PAYLOAD);
149
Julius Werner965846f2021-01-11 16:07:02 -0800150 if (prog_locate_hook(payload))
151 goto out;
152
Raul E Rangel571e7f02021-11-02 11:51:48 -0600153 payload->cbfs_type = CBFS_TYPE_QUERY;
154 mapping = cbfs_type_map(prog_name(payload), NULL, &payload->cbfs_type);
Raul E Rangel67798cf2021-07-02 17:07:05 -0600155
Raul E Rangel571e7f02021-11-02 11:51:48 -0600156 if (!mapping)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500157 goto out;
158
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200159 switch (prog_cbfs_type(payload)) {
160 case CBFS_TYPE_SELF: /* Simple ELF */
Raul E Rangel571e7f02021-11-02 11:51:48 -0600161 selfload_mapped(payload, mapping, BM_MEM_RAM);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200162 break;
Julius Werner00572622022-05-26 20:29:42 -0700163 case CBFS_TYPE_FIT_PAYLOAD: /* Flattened image tree */
Julius Wernercd49cce2019-03-05 16:53:33 -0800164 if (CONFIG(PAYLOAD_FIT_SUPPORT)) {
Raul E Rangel571e7f02021-11-02 11:51:48 -0600165 fit_payload(payload, mapping);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200166 break;
Arthur Heymansfff20212021-03-15 14:56:16 +0100167 }
168 __fallthrough;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200169 default:
Keith Short70064582019-05-06 16:12:57 -0600170 die_with_post_code(POST_INVALID_ROM,
Julius Werner965846f2021-01-11 16:07:02 -0800171 "Unsupported payload type %d.\n", payload->cbfs_type);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200172 break;
173 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500174
Raul E Rangel571e7f02021-11-02 11:51:48 -0600175 cbfs_unmap(mapping);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500176out:
177 if (prog_entry(payload) == NULL)
Keith Short70064582019-05-06 16:12:57 -0600178 die_with_post_code(POST_INVALID_ROM, "Payload not loaded.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500179}
180
181void payload_run(void)
182{
183 struct prog *payload = &global_payload;
184
185 /* Reset to booting from this image as late as possible */
186 boot_successful();
187
188 printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n",
189 prog_entry(payload), prog_entry_arg(payload));
190
191 post_code(POST_ENTER_ELF_BOOT);
192
193 timestamp_add_now(TS_SELFBOOT_JUMP);
194
195 /* Before we go off to run the payload, see if
196 * we stayed within our bounds.
197 */
198 checkstack(_estack, 0);
199
200 prog_run(payload);
201}
Stefan Reinauereec2db42015-06-18 01:19:50 -0700202
203#endif