blob: a296bd6da5bde35efadb32ce316ee369ec7c16ee [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
Jakub Czapigaad6157e2022-02-15 11:50:31 +010027 timestamp_add_now(TS_COPYROM_START);
Aaron Durbin899d13d2015-05-15 23:39:23 -050028
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
Jakub Czapigaad6157e2022-02-15 11:50:31 +010037 timestamp_add_now(TS_COPYROM_END);
Aaron Durbin899d13d2015-05-15 23:39:23 -050038
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}
Raul E Rangelb25576f2021-11-05 10:29:24 -060078void preload_ramstage(void)
79{
80 if (!CONFIG(CBFS_PRELOAD))
81 return;
Aaron Durbin899d13d2015-05-15 23:39:23 -050082
Raul E Rangelb25576f2021-11-05 10:29:24 -060083 printk(BIOS_DEBUG, "Preloading ramstage\n");
84
85 cbfs_preload(CONFIG_CBFS_PREFIX "/ramstage");
86}
Aaron Durbin899d13d2015-05-15 23:39:23 -050087void run_ramstage(void)
88{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050089 struct prog ramstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060090 PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050091
Subrata Banik4f42eea2019-03-05 16:45:14 +053092 if (ENV_POSTCAR)
Jakub Czapigaad6157e2022-02-15 11:50:31 +010093 timestamp_add_now(TS_POSTCAR_END);
Subrata Banik4f42eea2019-03-05 16:45:14 +053094
Subrata Banik2847e1e2019-02-25 20:31:22 +053095 /* Call "end of romstage" here if postcar stage doesn't exist */
96 if (ENV_ROMSTAGE)
Jakub Czapigaad6157e2022-02-15 11:50:31 +010097 timestamp_add_now(TS_ROMSTAGE_END);
Aaron Durbin9796f602015-09-23 19:54:12 -050098
Furquan Shaikh1e162bf2016-05-06 09:20:35 -070099 /*
100 * Only x86 systems using ramstage stage cache currently take the same
101 * firmware path on resume.
102 */
Kyösti Mälkkie0165fb2021-01-09 13:30:57 +0200103 if (ENV_X86 && resume_from_stage_cache())
Aaron Durbin6c191d82016-11-29 21:22:42 -0600104 run_ramstage_from_resume(&ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500105
Wim Vervoorn1058dd82019-11-01 10:22:22 +0100106 vboot_run_logic();
107
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100108 timestamp_add_now(TS_COPYRAM_START);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500109
Nico Huber06b68d12020-07-06 11:02:53 +0200110 if (ENV_X86) {
111 if (load_relocatable_ramstage(&ramstage))
112 goto fail;
113 } else {
114 if (cbfs_prog_stage_load(&ramstage))
115 goto fail;
116 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500117
Subrata Banik90f750b2019-06-11 17:52:06 +0530118 stage_cache_add(STAGE_RAMSTAGE, &ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500119
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100120 timestamp_add_now(TS_COPYRAM_END);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500121
Kyösti Mälkki45ddb432019-11-02 14:12:18 +0200122 console_time_report();
123
Arthur Heymans5331a7c2019-10-23 17:07:15 +0200124 /* This overrides the arg fetched from the relocatable module */
125 prog_set_arg(&ramstage, cbmem_top());
126
Aaron Durbin899d13d2015-05-15 23:39:23 -0500127 prog_run(&ramstage);
128
129fail:
Keith Short70064582019-05-06 16:12:57 -0600130 die_with_post_code(POST_INVALID_ROM, "Ramstage was not loaded!\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500131}
132
Subrata Banik42c44c22019-05-15 20:27:04 +0530133#if ENV_PAYLOAD_LOADER // gc-sections should take care of this
Stefan Reinauereec2db42015-06-18 01:19:50 -0700134
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500135static struct prog global_payload =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600136 PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500137
Raul E Rangel67798cf2021-07-02 17:07:05 -0600138void payload_preload(void)
139{
Raul E Rangel571e7f02021-11-02 11:51:48 -0600140 if (!CONFIG(CBFS_PRELOAD))
Raul E Rangel67798cf2021-07-02 17:07:05 -0600141 return;
142
Raul E Rangel571e7f02021-11-02 11:51:48 -0600143 cbfs_preload(global_payload.name);
Raul E Rangel67798cf2021-07-02 17:07:05 -0600144}
145
Aaron Durbin899d13d2015-05-15 23:39:23 -0500146void payload_load(void)
147{
148 struct prog *payload = &global_payload;
Raul E Rangel571e7f02021-11-02 11:51:48 -0600149 void *mapping;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500150
151 timestamp_add_now(TS_LOAD_PAYLOAD);
152
Julius Werner965846f2021-01-11 16:07:02 -0800153 if (prog_locate_hook(payload))
154 goto out;
155
Raul E Rangel571e7f02021-11-02 11:51:48 -0600156 payload->cbfs_type = CBFS_TYPE_QUERY;
157 mapping = cbfs_type_map(prog_name(payload), NULL, &payload->cbfs_type);
Raul E Rangel67798cf2021-07-02 17:07:05 -0600158
Raul E Rangel571e7f02021-11-02 11:51:48 -0600159 if (!mapping)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500160 goto out;
161
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200162 switch (prog_cbfs_type(payload)) {
163 case CBFS_TYPE_SELF: /* Simple ELF */
Raul E Rangel571e7f02021-11-02 11:51:48 -0600164 selfload_mapped(payload, mapping, BM_MEM_RAM);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200165 break;
166 case CBFS_TYPE_FIT: /* Flattened image tree */
Julius Wernercd49cce2019-03-05 16:53:33 -0800167 if (CONFIG(PAYLOAD_FIT_SUPPORT)) {
Raul E Rangel571e7f02021-11-02 11:51:48 -0600168 fit_payload(payload, mapping);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200169 break;
Arthur Heymansfff20212021-03-15 14:56:16 +0100170 }
171 __fallthrough;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200172 default:
Keith Short70064582019-05-06 16:12:57 -0600173 die_with_post_code(POST_INVALID_ROM,
Julius Werner965846f2021-01-11 16:07:02 -0800174 "Unsupported payload type %d.\n", payload->cbfs_type);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200175 break;
176 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500177
Raul E Rangel571e7f02021-11-02 11:51:48 -0600178 cbfs_unmap(mapping);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500179out:
180 if (prog_entry(payload) == NULL)
Keith Short70064582019-05-06 16:12:57 -0600181 die_with_post_code(POST_INVALID_ROM, "Payload not loaded.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500182}
183
184void payload_run(void)
185{
186 struct prog *payload = &global_payload;
187
188 /* Reset to booting from this image as late as possible */
189 boot_successful();
190
191 printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n",
192 prog_entry(payload), prog_entry_arg(payload));
193
194 post_code(POST_ENTER_ELF_BOOT);
195
196 timestamp_add_now(TS_SELFBOOT_JUMP);
197
198 /* Before we go off to run the payload, see if
199 * we stayed within our bounds.
200 */
201 checkstack(_estack, 0);
202
203 prog_run(payload);
204}
Stefan Reinauereec2db42015-06-18 01:19:50 -0700205
206#endif