blob: 40f51ebf48ed17dacc4d151cc2a50e9071c3fac4 [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 Durbin899d13d2015-05-15 23:39:23 -050019void run_romstage(void)
20{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050021 struct prog romstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060022 PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050023
Wim Vervoorn1058dd82019-11-01 10:22:22 +010024 vboot_run_logic();
25
Aaron Durbin899d13d2015-05-15 23:39:23 -050026 timestamp_add_now(TS_START_COPYROM);
27
Julius Werner1de87082020-12-23 17:38:11 -080028 if (ENV_X86 && CONFIG(BOOTBLOCK_NORMAL)) {
29 if (legacy_romstage_select_and_load(&romstage))
30 goto fail;
31 } else {
32 if (cbfs_prog_stage_load(&romstage))
33 goto fail;
34 }
Aaron Durbin899d13d2015-05-15 23:39:23 -050035
36 timestamp_add_now(TS_END_COPYROM);
37
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020038 console_time_report();
39
Aaron Durbin899d13d2015-05-15 23:39:23 -050040 prog_run(&romstage);
41
42fail:
Julius Wernercd49cce2019-03-05 16:53:33 -080043 if (CONFIG(BOOTBLOCK_CONSOLE))
Keith Short70064582019-05-06 16:12:57 -060044 die_with_post_code(POST_INVALID_ROM,
45 "Couldn't load romstage.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -050046 halt();
47}
48
Frans Hendriksfc580342019-06-14 14:36:37 +020049int __weak prog_locate_hook(struct prog *prog) { return 0; }
50
Aaron Durbin6c191d82016-11-29 21:22:42 -060051static void run_ramstage_from_resume(struct prog *ramstage)
Aaron Durbin899d13d2015-05-15 23:39:23 -050052{
Aaron Durbin6c191d82016-11-29 21:22:42 -060053 /* Load the cached ramstage to runtime location. */
54 stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
55
Julius Werner1de87082020-12-23 17:38:11 -080056 ramstage->cbfs_type = CBFS_TYPE_STAGE;
Arthur Heymans5331a7c2019-10-23 17:07:15 +020057 prog_set_arg(ramstage, cbmem_top());
58
Aaron Durbin6c191d82016-11-29 21:22:42 -060059 if (prog_entry(ramstage) != NULL) {
60 printk(BIOS_DEBUG, "Jumping to image.\n");
61 prog_run(ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -050062 }
Kyösti Mälkki4f14cd82019-12-18 19:40:48 +020063
64 printk(BIOS_ERR, "ramstage cache invalid.\n");
65 board_reset();
Aaron Durbin899d13d2015-05-15 23:39:23 -050066}
67
68static int load_relocatable_ramstage(struct prog *ramstage)
69{
70 struct rmod_stage_load rmod_ram = {
71 .cbmem_id = CBMEM_ID_RAMSTAGE,
72 .prog = ramstage,
73 };
74
75 return rmodule_stage_load(&rmod_ram);
76}
77
78void run_ramstage(void)
79{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050080 struct prog ramstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060081 PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050082
Subrata Banik4f42eea2019-03-05 16:45:14 +053083 if (ENV_POSTCAR)
84 timestamp_add_now(TS_END_POSTCAR);
85
Subrata Banik2847e1e2019-02-25 20:31:22 +053086 /* Call "end of romstage" here if postcar stage doesn't exist */
87 if (ENV_ROMSTAGE)
88 timestamp_add_now(TS_END_ROMSTAGE);
Aaron Durbin9796f602015-09-23 19:54:12 -050089
Furquan Shaikh1e162bf2016-05-06 09:20:35 -070090 /*
91 * Only x86 systems using ramstage stage cache currently take the same
92 * firmware path on resume.
93 */
Kyösti Mälkkie0165fb2021-01-09 13:30:57 +020094 if (ENV_X86 && resume_from_stage_cache())
Aaron Durbin6c191d82016-11-29 21:22:42 -060095 run_ramstage_from_resume(&ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -050096
Wim Vervoorn1058dd82019-11-01 10:22:22 +010097 vboot_run_logic();
98
Aaron Durbin899d13d2015-05-15 23:39:23 -050099 timestamp_add_now(TS_START_COPYRAM);
100
Nico Huber06b68d12020-07-06 11:02:53 +0200101 if (ENV_X86) {
102 if (load_relocatable_ramstage(&ramstage))
103 goto fail;
104 } else {
105 if (cbfs_prog_stage_load(&ramstage))
106 goto fail;
107 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500108
Subrata Banik90f750b2019-06-11 17:52:06 +0530109 stage_cache_add(STAGE_RAMSTAGE, &ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500110
111 timestamp_add_now(TS_END_COPYRAM);
112
Kyösti Mälkki45ddb432019-11-02 14:12:18 +0200113 console_time_report();
114
Arthur Heymans5331a7c2019-10-23 17:07:15 +0200115 /* This overrides the arg fetched from the relocatable module */
116 prog_set_arg(&ramstage, cbmem_top());
117
Aaron Durbin899d13d2015-05-15 23:39:23 -0500118 prog_run(&ramstage);
119
120fail:
Keith Short70064582019-05-06 16:12:57 -0600121 die_with_post_code(POST_INVALID_ROM, "Ramstage was not loaded!\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500122}
123
Subrata Banik42c44c22019-05-15 20:27:04 +0530124#if ENV_PAYLOAD_LOADER // gc-sections should take care of this
Stefan Reinauereec2db42015-06-18 01:19:50 -0700125
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500126static struct prog global_payload =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600127 PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500128
Aaron Durbin899d13d2015-05-15 23:39:23 -0500129void payload_load(void)
130{
131 struct prog *payload = &global_payload;
132
133 timestamp_add_now(TS_LOAD_PAYLOAD);
134
Julius Werner965846f2021-01-11 16:07:02 -0800135 if (prog_locate_hook(payload))
136 goto out;
137
138 payload->cbfs_type = CBFS_TYPE_QUERY;
139 void *mapping = cbfs_type_map(prog_name(payload), NULL, &payload->cbfs_type);
140 if (!mapping)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500141 goto out;
142
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200143 switch (prog_cbfs_type(payload)) {
144 case CBFS_TYPE_SELF: /* Simple ELF */
Julius Werner965846f2021-01-11 16:07:02 -0800145 selfload_mapped(payload, mapping, BM_MEM_RAM);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200146 break;
147 case CBFS_TYPE_FIT: /* Flattened image tree */
Julius Wernercd49cce2019-03-05 16:53:33 -0800148 if (CONFIG(PAYLOAD_FIT_SUPPORT)) {
Julius Werner965846f2021-01-11 16:07:02 -0800149 fit_payload(payload, mapping);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200150 break;
151 } /* else fall-through */
152 default:
Keith Short70064582019-05-06 16:12:57 -0600153 die_with_post_code(POST_INVALID_ROM,
Julius Werner965846f2021-01-11 16:07:02 -0800154 "Unsupported payload type %d.\n", payload->cbfs_type);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200155 break;
156 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500157
Julius Werner965846f2021-01-11 16:07:02 -0800158 cbfs_unmap(mapping);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500159out:
160 if (prog_entry(payload) == NULL)
Keith Short70064582019-05-06 16:12:57 -0600161 die_with_post_code(POST_INVALID_ROM, "Payload not loaded.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500162}
163
164void payload_run(void)
165{
166 struct prog *payload = &global_payload;
167
168 /* Reset to booting from this image as late as possible */
169 boot_successful();
170
171 printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n",
172 prog_entry(payload), prog_entry_arg(payload));
173
174 post_code(POST_ENTER_ELF_BOOT);
175
176 timestamp_add_now(TS_SELFBOOT_JUMP);
177
178 /* Before we go off to run the payload, see if
179 * we stayed within our bounds.
180 */
181 checkstack(_estack, 0);
182
183 prog_run(payload);
184}
Stefan Reinauereec2db42015-06-18 01:19:50 -0700185
186#endif