blob: 17d946eac94751524b2406b43d5ddc5fb644a83d [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>
Elyes Haouasd8cd8352022-10-02 20:31:50 +020012#include <security/vboot/vboot_common.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050013#include <stage_cache.h>
14#include <symbols.h>
15#include <timestamp.h>
16
Aaron Durbin899d13d2015-05-15 23:39:23 -050017void run_romstage(void)
18{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050019 struct prog romstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060020 PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050021
Wim Vervoorn1058dd82019-11-01 10:22:22 +010022 vboot_run_logic();
23
Jakub Czapigaad6157e2022-02-15 11:50:31 +010024 timestamp_add_now(TS_COPYROM_START);
Aaron Durbin899d13d2015-05-15 23:39:23 -050025
Julius Werner1de87082020-12-23 17:38:11 -080026 if (ENV_X86 && CONFIG(BOOTBLOCK_NORMAL)) {
Julius Werner797a1102022-03-07 18:54:47 -080027 if (legacy_romstage_select_and_load(&romstage) != CB_SUCCESS)
Julius Werner1de87082020-12-23 17:38:11 -080028 goto fail;
29 } else {
30 if (cbfs_prog_stage_load(&romstage))
31 goto fail;
32 }
Aaron Durbin899d13d2015-05-15 23:39:23 -050033
Jakub Czapigaad6157e2022-02-15 11:50:31 +010034 timestamp_add_now(TS_COPYROM_END);
Aaron Durbin899d13d2015-05-15 23:39:23 -050035
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020036 console_time_report();
37
Aaron Durbin899d13d2015-05-15 23:39:23 -050038 prog_run(&romstage);
39
40fail:
Julius Wernercd49cce2019-03-05 16:53:33 -080041 if (CONFIG(BOOTBLOCK_CONSOLE))
lilacious40cb3fe2023-06-21 23:24:14 +020042 die_with_post_code(POSTCODE_INVALID_ROM,
Keith Short70064582019-05-06 16:12:57 -060043 "Couldn't load romstage.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -050044 halt();
45}
46
Frans Hendriksfc580342019-06-14 14:36:37 +020047int __weak prog_locate_hook(struct prog *prog) { return 0; }
48
Aaron Durbin6c191d82016-11-29 21:22:42 -060049static void run_ramstage_from_resume(struct prog *ramstage)
Aaron Durbin899d13d2015-05-15 23:39:23 -050050{
Aaron Durbin6c191d82016-11-29 21:22:42 -060051 /* Load the cached ramstage to runtime location. */
52 stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
53
Julius Werner1de87082020-12-23 17:38:11 -080054 ramstage->cbfs_type = CBFS_TYPE_STAGE;
Arthur Heymans5331a7c2019-10-23 17:07:15 +020055 prog_set_arg(ramstage, cbmem_top());
56
Aaron Durbin6c191d82016-11-29 21:22:42 -060057 if (prog_entry(ramstage) != NULL) {
58 printk(BIOS_DEBUG, "Jumping to image.\n");
59 prog_run(ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -050060 }
Kyösti Mälkki4f14cd82019-12-18 19:40:48 +020061
62 printk(BIOS_ERR, "ramstage cache invalid.\n");
63 board_reset();
Aaron Durbin899d13d2015-05-15 23:39:23 -050064}
65
66static int load_relocatable_ramstage(struct prog *ramstage)
67{
68 struct rmod_stage_load rmod_ram = {
69 .cbmem_id = CBMEM_ID_RAMSTAGE,
70 .prog = ramstage,
71 };
72
73 return rmodule_stage_load(&rmod_ram);
74}
Raul E Rangelb25576f2021-11-05 10:29:24 -060075void preload_ramstage(void)
76{
77 if (!CONFIG(CBFS_PRELOAD))
78 return;
Aaron Durbin899d13d2015-05-15 23:39:23 -050079
Raul E Rangelb25576f2021-11-05 10:29:24 -060080 printk(BIOS_DEBUG, "Preloading ramstage\n");
81
82 cbfs_preload(CONFIG_CBFS_PREFIX "/ramstage");
83}
Arthur Heymans84b2f9f2022-06-23 11:53:34 +020084void __noreturn run_ramstage(void)
Aaron Durbin899d13d2015-05-15 23:39:23 -050085{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050086 struct prog ramstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060087 PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050088
Kyösti Mälkkif4b85382022-04-07 07:58:52 +030089 /* Call "end of romstage" here if postcar stage doesn't exist */
Subrata Banik4f42eea2019-03-05 16:45:14 +053090 if (ENV_POSTCAR)
Jakub Czapigaad6157e2022-02-15 11:50:31 +010091 timestamp_add_now(TS_POSTCAR_END);
Kyösti Mälkkif4b85382022-04-07 07:58:52 +030092 else
Jakub Czapigaad6157e2022-02-15 11:50:31 +010093 timestamp_add_now(TS_ROMSTAGE_END);
Aaron Durbin9796f602015-09-23 19:54:12 -050094
Furquan Shaikh1e162bf2016-05-06 09:20:35 -070095 /*
96 * Only x86 systems using ramstage stage cache currently take the same
97 * firmware path on resume.
98 */
Kyösti Mälkkie0165fb2021-01-09 13:30:57 +020099 if (ENV_X86 && resume_from_stage_cache())
Aaron Durbin6c191d82016-11-29 21:22:42 -0600100 run_ramstage_from_resume(&ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500101
Wim Vervoorn1058dd82019-11-01 10:22:22 +0100102 vboot_run_logic();
103
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100104 timestamp_add_now(TS_COPYRAM_START);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500105
Nico Huber06b68d12020-07-06 11:02:53 +0200106 if (ENV_X86) {
107 if (load_relocatable_ramstage(&ramstage))
108 goto fail;
109 } else {
110 if (cbfs_prog_stage_load(&ramstage))
111 goto fail;
112 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500113
Subrata Banik90f750b2019-06-11 17:52:06 +0530114 stage_cache_add(STAGE_RAMSTAGE, &ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500115
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100116 timestamp_add_now(TS_COPYRAM_END);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500117
Kyösti Mälkki45ddb432019-11-02 14:12:18 +0200118 console_time_report();
119
Arthur Heymans5331a7c2019-10-23 17:07:15 +0200120 /* This overrides the arg fetched from the relocatable module */
121 prog_set_arg(&ramstage, cbmem_top());
122
Aaron Durbin899d13d2015-05-15 23:39:23 -0500123 prog_run(&ramstage);
124
125fail:
lilacious40cb3fe2023-06-21 23:24:14 +0200126 die_with_post_code(POSTCODE_INVALID_ROM, "Ramstage was not loaded!\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500127}
128
Subrata Banik42c44c22019-05-15 20:27:04 +0530129#if ENV_PAYLOAD_LOADER // gc-sections should take care of this
Stefan Reinauereec2db42015-06-18 01:19:50 -0700130
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500131static struct prog global_payload =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600132 PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500133
Raul E Rangel67798cf2021-07-02 17:07:05 -0600134void payload_preload(void)
135{
Raul E Rangel571e7f02021-11-02 11:51:48 -0600136 if (!CONFIG(CBFS_PRELOAD))
Raul E Rangel67798cf2021-07-02 17:07:05 -0600137 return;
138
Raul E Rangel571e7f02021-11-02 11:51:48 -0600139 cbfs_preload(global_payload.name);
Raul E Rangel67798cf2021-07-02 17:07:05 -0600140}
141
Aaron Durbin899d13d2015-05-15 23:39:23 -0500142void payload_load(void)
143{
144 struct prog *payload = &global_payload;
Raul E Rangel571e7f02021-11-02 11:51:48 -0600145 void *mapping;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500146
147 timestamp_add_now(TS_LOAD_PAYLOAD);
148
Julius Werner965846f2021-01-11 16:07:02 -0800149 if (prog_locate_hook(payload))
150 goto out;
151
Raul E Rangel571e7f02021-11-02 11:51:48 -0600152 payload->cbfs_type = CBFS_TYPE_QUERY;
153 mapping = cbfs_type_map(prog_name(payload), NULL, &payload->cbfs_type);
Raul E Rangel67798cf2021-07-02 17:07:05 -0600154
Raul E Rangel571e7f02021-11-02 11:51:48 -0600155 if (!mapping)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500156 goto out;
157
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200158 switch (prog_cbfs_type(payload)) {
159 case CBFS_TYPE_SELF: /* Simple ELF */
Raul E Rangel571e7f02021-11-02 11:51:48 -0600160 selfload_mapped(payload, mapping, BM_MEM_RAM);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200161 break;
Julius Werner00572622022-05-26 20:29:42 -0700162 case CBFS_TYPE_FIT_PAYLOAD: /* Flattened image tree */
Julius Wernercd49cce2019-03-05 16:53:33 -0800163 if (CONFIG(PAYLOAD_FIT_SUPPORT)) {
Raul E Rangel571e7f02021-11-02 11:51:48 -0600164 fit_payload(payload, mapping);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200165 break;
Arthur Heymansfff20212021-03-15 14:56:16 +0100166 }
167 __fallthrough;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200168 default:
lilacious40cb3fe2023-06-21 23:24:14 +0200169 die_with_post_code(POSTCODE_INVALID_ROM,
Julius Werner965846f2021-01-11 16:07:02 -0800170 "Unsupported payload type %d.\n", payload->cbfs_type);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200171 break;
172 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500173
Raul E Rangel571e7f02021-11-02 11:51:48 -0600174 cbfs_unmap(mapping);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500175out:
176 if (prog_entry(payload) == NULL)
lilacious40cb3fe2023-06-21 23:24:14 +0200177 die_with_post_code(POSTCODE_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
lilacious40cb3fe2023-06-21 23:24:14 +0200190 post_code(POSTCODE_ENTER_ELF_BOOT);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500191
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