blob: b2abbff57521de201555737b7d0b444d389b653d [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>
Arthur Heymansa2bc2542021-05-29 08:10:49 +020012#include <romstage_common.h>
Elyes Haouasd8cd8352022-10-02 20:31:50 +020013#include <security/vboot/vboot_common.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050014#include <stage_cache.h>
15#include <symbols.h>
16#include <timestamp.h>
17
Aaron Durbin899d13d2015-05-15 23:39:23 -050018void run_romstage(void)
19{
Arthur Heymansa2bc2542021-05-29 08:10:49 +020020 if (!CONFIG(SEPARATE_ROMSTAGE)) {
21 /* Call romstage instead of loading it as a cbfs file. */
22 timestamp_add_now(TS_ROMSTAGE_START);
23 romstage_main();
24 dead_code();
25 }
26
Aaron Durbinac12c66c2015-05-20 12:08:55 -050027 struct prog romstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060028 PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050029
Wim Vervoorn1058dd82019-11-01 10:22:22 +010030 vboot_run_logic();
31
Jakub Czapigaad6157e2022-02-15 11:50:31 +010032 timestamp_add_now(TS_COPYROM_START);
Aaron Durbin899d13d2015-05-15 23:39:23 -050033
Julius Werner1de87082020-12-23 17:38:11 -080034 if (ENV_X86 && CONFIG(BOOTBLOCK_NORMAL)) {
Julius Werner797a1102022-03-07 18:54:47 -080035 if (legacy_romstage_select_and_load(&romstage) != CB_SUCCESS)
Julius Werner1de87082020-12-23 17:38:11 -080036 goto fail;
37 } else {
38 if (cbfs_prog_stage_load(&romstage))
39 goto fail;
40 }
Aaron Durbin899d13d2015-05-15 23:39:23 -050041
Jakub Czapigaad6157e2022-02-15 11:50:31 +010042 timestamp_add_now(TS_COPYROM_END);
Aaron Durbin899d13d2015-05-15 23:39:23 -050043
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020044 console_time_report();
45
Aaron Durbin899d13d2015-05-15 23:39:23 -050046 prog_run(&romstage);
47
48fail:
Julius Wernercd49cce2019-03-05 16:53:33 -080049 if (CONFIG(BOOTBLOCK_CONSOLE))
lilacious40cb3fe2023-06-21 23:24:14 +020050 die_with_post_code(POSTCODE_INVALID_ROM,
Keith Short70064582019-05-06 16:12:57 -060051 "Couldn't load romstage.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -050052 halt();
53}
54
Frans Hendriksfc580342019-06-14 14:36:37 +020055int __weak prog_locate_hook(struct prog *prog) { return 0; }
56
Aaron Durbin6c191d82016-11-29 21:22:42 -060057static void run_ramstage_from_resume(struct prog *ramstage)
Aaron Durbin899d13d2015-05-15 23:39:23 -050058{
Aaron Durbin6c191d82016-11-29 21:22:42 -060059 /* Load the cached ramstage to runtime location. */
60 stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
61
Julius Werner1de87082020-12-23 17:38:11 -080062 ramstage->cbfs_type = CBFS_TYPE_STAGE;
Arthur Heymans5331a7c2019-10-23 17:07:15 +020063 prog_set_arg(ramstage, cbmem_top());
64
Aaron Durbin6c191d82016-11-29 21:22:42 -060065 if (prog_entry(ramstage) != NULL) {
66 printk(BIOS_DEBUG, "Jumping to image.\n");
67 prog_run(ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -050068 }
Kyösti Mälkki4f14cd82019-12-18 19:40:48 +020069
70 printk(BIOS_ERR, "ramstage cache invalid.\n");
71 board_reset();
Aaron Durbin899d13d2015-05-15 23:39:23 -050072}
73
74static int load_relocatable_ramstage(struct prog *ramstage)
75{
76 struct rmod_stage_load rmod_ram = {
77 .cbmem_id = CBMEM_ID_RAMSTAGE,
78 .prog = ramstage,
79 };
80
81 return rmodule_stage_load(&rmod_ram);
82}
Raul E Rangelb25576f2021-11-05 10:29:24 -060083void preload_ramstage(void)
84{
85 if (!CONFIG(CBFS_PRELOAD))
86 return;
Aaron Durbin899d13d2015-05-15 23:39:23 -050087
Raul E Rangelb25576f2021-11-05 10:29:24 -060088 printk(BIOS_DEBUG, "Preloading ramstage\n");
89
90 cbfs_preload(CONFIG_CBFS_PREFIX "/ramstage");
91}
Arthur Heymans84b2f9f2022-06-23 11:53:34 +020092void __noreturn run_ramstage(void)
Aaron Durbin899d13d2015-05-15 23:39:23 -050093{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050094 struct prog ramstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060095 PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050096
Kyösti Mälkkif4b85382022-04-07 07:58:52 +030097 /* Call "end of romstage" here if postcar stage doesn't exist */
Subrata Banik4f42eea2019-03-05 16:45:14 +053098 if (ENV_POSTCAR)
Jakub Czapigaad6157e2022-02-15 11:50:31 +010099 timestamp_add_now(TS_POSTCAR_END);
Kyösti Mälkkif4b85382022-04-07 07:58:52 +0300100 else
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100101 timestamp_add_now(TS_ROMSTAGE_END);
Aaron Durbin9796f602015-09-23 19:54:12 -0500102
Kyösti Mälkki6739a6a2023-07-04 10:09:59 +0300103 vboot_run_logic();
104
Furquan Shaikh1e162bf2016-05-06 09:20:35 -0700105 /*
106 * Only x86 systems using ramstage stage cache currently take the same
107 * firmware path on resume.
108 */
Kyösti Mälkkie0165fb2021-01-09 13:30:57 +0200109 if (ENV_X86 && resume_from_stage_cache())
Aaron Durbin6c191d82016-11-29 21:22:42 -0600110 run_ramstage_from_resume(&ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500111
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100112 timestamp_add_now(TS_COPYRAM_START);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500113
Nico Huber06b68d12020-07-06 11:02:53 +0200114 if (ENV_X86) {
115 if (load_relocatable_ramstage(&ramstage))
116 goto fail;
117 } else {
118 if (cbfs_prog_stage_load(&ramstage))
119 goto fail;
120 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500121
Subrata Banik90f750b2019-06-11 17:52:06 +0530122 stage_cache_add(STAGE_RAMSTAGE, &ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500123
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100124 timestamp_add_now(TS_COPYRAM_END);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500125
Kyösti Mälkki45ddb432019-11-02 14:12:18 +0200126 console_time_report();
127
Arthur Heymans5331a7c2019-10-23 17:07:15 +0200128 /* This overrides the arg fetched from the relocatable module */
129 prog_set_arg(&ramstage, cbmem_top());
130
Aaron Durbin899d13d2015-05-15 23:39:23 -0500131 prog_run(&ramstage);
132
133fail:
lilacious40cb3fe2023-06-21 23:24:14 +0200134 die_with_post_code(POSTCODE_INVALID_ROM, "Ramstage was not loaded!\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500135}
136
Subrata Banik42c44c22019-05-15 20:27:04 +0530137#if ENV_PAYLOAD_LOADER // gc-sections should take care of this
Stefan Reinauereec2db42015-06-18 01:19:50 -0700138
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500139static struct prog global_payload =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600140 PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500141
Raul E Rangel67798cf2021-07-02 17:07:05 -0600142void payload_preload(void)
143{
Raul E Rangel571e7f02021-11-02 11:51:48 -0600144 if (!CONFIG(CBFS_PRELOAD))
Raul E Rangel67798cf2021-07-02 17:07:05 -0600145 return;
146
Raul E Rangel571e7f02021-11-02 11:51:48 -0600147 cbfs_preload(global_payload.name);
Raul E Rangel67798cf2021-07-02 17:07:05 -0600148}
149
Aaron Durbin899d13d2015-05-15 23:39:23 -0500150void payload_load(void)
151{
152 struct prog *payload = &global_payload;
Raul E Rangel571e7f02021-11-02 11:51:48 -0600153 void *mapping;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500154
155 timestamp_add_now(TS_LOAD_PAYLOAD);
156
Julius Werner965846f2021-01-11 16:07:02 -0800157 if (prog_locate_hook(payload))
158 goto out;
159
Raul E Rangel571e7f02021-11-02 11:51:48 -0600160 payload->cbfs_type = CBFS_TYPE_QUERY;
161 mapping = cbfs_type_map(prog_name(payload), NULL, &payload->cbfs_type);
Raul E Rangel67798cf2021-07-02 17:07:05 -0600162
Raul E Rangel571e7f02021-11-02 11:51:48 -0600163 if (!mapping)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500164 goto out;
165
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200166 switch (prog_cbfs_type(payload)) {
167 case CBFS_TYPE_SELF: /* Simple ELF */
Raul E Rangel571e7f02021-11-02 11:51:48 -0600168 selfload_mapped(payload, mapping, BM_MEM_RAM);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200169 break;
Julius Werner00572622022-05-26 20:29:42 -0700170 case CBFS_TYPE_FIT_PAYLOAD: /* Flattened image tree */
Julius Wernercd49cce2019-03-05 16:53:33 -0800171 if (CONFIG(PAYLOAD_FIT_SUPPORT)) {
Raul E Rangel571e7f02021-11-02 11:51:48 -0600172 fit_payload(payload, mapping);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200173 break;
Arthur Heymansfff20212021-03-15 14:56:16 +0100174 }
175 __fallthrough;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200176 default:
lilacious40cb3fe2023-06-21 23:24:14 +0200177 die_with_post_code(POSTCODE_INVALID_ROM,
Julius Werner965846f2021-01-11 16:07:02 -0800178 "Unsupported payload type %d.\n", payload->cbfs_type);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200179 break;
180 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500181
Raul E Rangel571e7f02021-11-02 11:51:48 -0600182 cbfs_unmap(mapping);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500183out:
184 if (prog_entry(payload) == NULL)
lilacious40cb3fe2023-06-21 23:24:14 +0200185 die_with_post_code(POSTCODE_INVALID_ROM, "Payload not loaded.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500186}
187
188void payload_run(void)
189{
190 struct prog *payload = &global_payload;
191
192 /* Reset to booting from this image as late as possible */
193 boot_successful();
194
195 printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n",
196 prog_entry(payload), prog_entry_arg(payload));
197
lilacious40cb3fe2023-06-21 23:24:14 +0200198 post_code(POSTCODE_ENTER_ELF_BOOT);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500199
200 timestamp_add_now(TS_SELFBOOT_JUMP);
201
202 /* Before we go off to run the payload, see if
203 * we stayed within our bounds.
204 */
205 checkstack(_estack, 0);
206
207 prog_run(payload);
208}
Stefan Reinauereec2db42015-06-18 01:19:50 -0700209
210#endif