blob: 28c6bf7978fb8931600011d48dd73e57a9ef6c10 [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>
16#include <timestamp.h>
Wim Vervoorn1058dd82019-11-01 10:22:22 +010017#include <security/vboot/vboot_common.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050018
Aaron Durbin6a452ef2015-05-19 16:25:20 -050019/* Only can represent up to 1 byte less than size_t. */
Antonello Dettorie5f48d22016-06-22 21:09:08 +020020const struct mem_region_device addrspace_32bit =
21 MEM_REGION_DEV_RO_INIT(0, ~0UL);
Aaron Durbin6a452ef2015-05-19 16:25:20 -050022
Aaron Durbin899d13d2015-05-15 23:39:23 -050023void run_romstage(void)
24{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050025 struct prog romstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060026 PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050027
Wim Vervoorn1058dd82019-11-01 10:22:22 +010028 vboot_run_logic();
29
Aaron Durbin899d13d2015-05-15 23:39:23 -050030 timestamp_add_now(TS_START_COPYROM);
31
Julius Werner1de87082020-12-23 17:38:11 -080032 if (ENV_X86 && CONFIG(BOOTBLOCK_NORMAL)) {
33 if (legacy_romstage_select_and_load(&romstage))
34 goto fail;
35 } else {
36 if (cbfs_prog_stage_load(&romstage))
37 goto fail;
38 }
Aaron Durbin899d13d2015-05-15 23:39:23 -050039
40 timestamp_add_now(TS_END_COPYROM);
41
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020042 console_time_report();
43
Aaron Durbin899d13d2015-05-15 23:39:23 -050044 prog_run(&romstage);
45
46fail:
Julius Wernercd49cce2019-03-05 16:53:33 -080047 if (CONFIG(BOOTBLOCK_CONSOLE))
Keith Short70064582019-05-06 16:12:57 -060048 die_with_post_code(POST_INVALID_ROM,
49 "Couldn't load romstage.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -050050 halt();
51}
52
Frans Hendriksfc580342019-06-14 14:36:37 +020053int __weak prog_locate_hook(struct prog *prog) { return 0; }
54
Aaron Durbin6c191d82016-11-29 21:22:42 -060055static void run_ramstage_from_resume(struct prog *ramstage)
Aaron Durbin899d13d2015-05-15 23:39:23 -050056{
Aaron Durbin6c191d82016-11-29 21:22:42 -060057 /* Load the cached ramstage to runtime location. */
58 stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
59
Julius Werner1de87082020-12-23 17:38:11 -080060 ramstage->cbfs_type = CBFS_TYPE_STAGE;
Arthur Heymans5331a7c2019-10-23 17:07:15 +020061 prog_set_arg(ramstage, cbmem_top());
62
Aaron Durbin6c191d82016-11-29 21:22:42 -060063 if (prog_entry(ramstage) != NULL) {
64 printk(BIOS_DEBUG, "Jumping to image.\n");
65 prog_run(ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -050066 }
Kyösti Mälkki4f14cd82019-12-18 19:40:48 +020067
68 printk(BIOS_ERR, "ramstage cache invalid.\n");
69 board_reset();
Aaron Durbin899d13d2015-05-15 23:39:23 -050070}
71
72static int load_relocatable_ramstage(struct prog *ramstage)
73{
74 struct rmod_stage_load rmod_ram = {
75 .cbmem_id = CBMEM_ID_RAMSTAGE,
76 .prog = ramstage,
77 };
78
79 return rmodule_stage_load(&rmod_ram);
80}
81
82void run_ramstage(void)
83{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050084 struct prog ramstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060085 PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050086
Subrata Banik4f42eea2019-03-05 16:45:14 +053087 if (ENV_POSTCAR)
88 timestamp_add_now(TS_END_POSTCAR);
89
Subrata Banik2847e1e2019-02-25 20:31:22 +053090 /* Call "end of romstage" here if postcar stage doesn't exist */
91 if (ENV_ROMSTAGE)
92 timestamp_add_now(TS_END_ROMSTAGE);
Aaron Durbin9796f602015-09-23 19:54:12 -050093
Furquan Shaikh1e162bf2016-05-06 09:20:35 -070094 /*
95 * Only x86 systems using ramstage stage cache currently take the same
96 * firmware path on resume.
97 */
Kyösti Mälkkie0165fb2021-01-09 13:30:57 +020098 if (ENV_X86 && resume_from_stage_cache())
Aaron Durbin6c191d82016-11-29 21:22:42 -060099 run_ramstage_from_resume(&ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500100
Wim Vervoorn1058dd82019-11-01 10:22:22 +0100101 vboot_run_logic();
102
Aaron Durbin899d13d2015-05-15 23:39:23 -0500103 timestamp_add_now(TS_START_COPYRAM);
104
Nico Huber06b68d12020-07-06 11:02:53 +0200105 if (ENV_X86) {
106 if (load_relocatable_ramstage(&ramstage))
107 goto fail;
108 } else {
109 if (cbfs_prog_stage_load(&ramstage))
110 goto fail;
111 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500112
Subrata Banik90f750b2019-06-11 17:52:06 +0530113 stage_cache_add(STAGE_RAMSTAGE, &ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500114
115 timestamp_add_now(TS_END_COPYRAM);
116
Kyösti Mälkki45ddb432019-11-02 14:12:18 +0200117 console_time_report();
118
Arthur Heymans5331a7c2019-10-23 17:07:15 +0200119 /* This overrides the arg fetched from the relocatable module */
120 prog_set_arg(&ramstage, cbmem_top());
121
Aaron Durbin899d13d2015-05-15 23:39:23 -0500122 prog_run(&ramstage);
123
124fail:
Keith Short70064582019-05-06 16:12:57 -0600125 die_with_post_code(POST_INVALID_ROM, "Ramstage was not loaded!\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500126}
127
Subrata Banik42c44c22019-05-15 20:27:04 +0530128#if ENV_PAYLOAD_LOADER // gc-sections should take care of this
Stefan Reinauereec2db42015-06-18 01:19:50 -0700129
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500130static struct prog global_payload =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600131 PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500132
Aaron Durbin899d13d2015-05-15 23:39:23 -0500133void payload_load(void)
134{
135 struct prog *payload = &global_payload;
136
137 timestamp_add_now(TS_LOAD_PAYLOAD);
138
Julius Werner965846f2021-01-11 16:07:02 -0800139 if (prog_locate_hook(payload))
140 goto out;
141
142 payload->cbfs_type = CBFS_TYPE_QUERY;
143 void *mapping = cbfs_type_map(prog_name(payload), NULL, &payload->cbfs_type);
144 if (!mapping)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500145 goto out;
146
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200147 switch (prog_cbfs_type(payload)) {
148 case CBFS_TYPE_SELF: /* Simple ELF */
Julius Werner965846f2021-01-11 16:07:02 -0800149 selfload_mapped(payload, mapping, BM_MEM_RAM);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200150 break;
151 case CBFS_TYPE_FIT: /* Flattened image tree */
Julius Wernercd49cce2019-03-05 16:53:33 -0800152 if (CONFIG(PAYLOAD_FIT_SUPPORT)) {
Julius Werner965846f2021-01-11 16:07:02 -0800153 fit_payload(payload, mapping);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200154 break;
155 } /* else fall-through */
156 default:
Keith Short70064582019-05-06 16:12:57 -0600157 die_with_post_code(POST_INVALID_ROM,
Julius Werner965846f2021-01-11 16:07:02 -0800158 "Unsupported payload type %d.\n", payload->cbfs_type);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200159 break;
160 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500161
Julius Werner965846f2021-01-11 16:07:02 -0800162 cbfs_unmap(mapping);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500163out:
164 if (prog_entry(payload) == NULL)
Keith Short70064582019-05-06 16:12:57 -0600165 die_with_post_code(POST_INVALID_ROM, "Payload not loaded.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500166}
167
168void payload_run(void)
169{
170 struct prog *payload = &global_payload;
171
172 /* Reset to booting from this image as late as possible */
173 boot_successful();
174
175 printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n",
176 prog_entry(payload), prog_entry_arg(payload));
177
178 post_code(POST_ENTER_ELF_BOOT);
179
180 timestamp_add_now(TS_SELFBOOT_JUMP);
181
182 /* Before we go off to run the payload, see if
183 * we stayed within our bounds.
184 */
185 checkstack(_estack, 0);
186
187 prog_run(payload);
188}
Stefan Reinauereec2db42015-06-18 01:19:50 -0700189
190#endif