soc/intel: Remove blank lines before '}' and after '{'
[coreboot2.git] / src / lib / prog_loaders.c
blobb2abbff57521de201555737b7d0b444d389b653d
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #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>
10 #include <reset.h>
11 #include <rmodule.h>
12 #include <romstage_common.h>
13 #include <security/vboot/vboot_common.h>
14 #include <stage_cache.h>
15 #include <symbols.h>
16 #include <timestamp.h>
18 void run_romstage(void)
20 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();
27 struct prog romstage =
28 PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage");
30 vboot_run_logic();
32 timestamp_add_now(TS_COPYROM_START);
34 if (ENV_X86 && CONFIG(BOOTBLOCK_NORMAL)) {
35 if (legacy_romstage_select_and_load(&romstage) != CB_SUCCESS)
36 goto fail;
37 } else {
38 if (cbfs_prog_stage_load(&romstage))
39 goto fail;
42 timestamp_add_now(TS_COPYROM_END);
44 console_time_report();
46 prog_run(&romstage);
48 fail:
49 if (CONFIG(BOOTBLOCK_CONSOLE))
50 die_with_post_code(POSTCODE_INVALID_ROM,
51 "Couldn't load romstage.\n");
52 halt();
55 int __weak prog_locate_hook(struct prog *prog) { return 0; }
57 static void run_ramstage_from_resume(struct prog *ramstage)
59 /* Load the cached ramstage to runtime location. */
60 stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
62 ramstage->cbfs_type = CBFS_TYPE_STAGE;
63 prog_set_arg(ramstage, cbmem_top());
65 if (prog_entry(ramstage) != NULL) {
66 printk(BIOS_DEBUG, "Jumping to image.\n");
67 prog_run(ramstage);
70 printk(BIOS_ERR, "ramstage cache invalid.\n");
71 board_reset();
74 static int load_relocatable_ramstage(struct prog *ramstage)
76 struct rmod_stage_load rmod_ram = {
77 .cbmem_id = CBMEM_ID_RAMSTAGE,
78 .prog = ramstage,
81 return rmodule_stage_load(&rmod_ram);
83 void preload_ramstage(void)
85 if (!CONFIG(CBFS_PRELOAD))
86 return;
88 printk(BIOS_DEBUG, "Preloading ramstage\n");
90 cbfs_preload(CONFIG_CBFS_PREFIX "/ramstage");
92 void __noreturn run_ramstage(void)
94 struct prog ramstage =
95 PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage");
97 /* Call "end of romstage" here if postcar stage doesn't exist */
98 if (ENV_POSTCAR)
99 timestamp_add_now(TS_POSTCAR_END);
100 else
101 timestamp_add_now(TS_ROMSTAGE_END);
103 vboot_run_logic();
106 * Only x86 systems using ramstage stage cache currently take the same
107 * firmware path on resume.
109 if (ENV_X86 && resume_from_stage_cache())
110 run_ramstage_from_resume(&ramstage);
112 timestamp_add_now(TS_COPYRAM_START);
114 if (ENV_X86) {
115 if (load_relocatable_ramstage(&ramstage))
116 goto fail;
117 } else {
118 if (cbfs_prog_stage_load(&ramstage))
119 goto fail;
122 stage_cache_add(STAGE_RAMSTAGE, &ramstage);
124 timestamp_add_now(TS_COPYRAM_END);
126 console_time_report();
128 /* This overrides the arg fetched from the relocatable module */
129 prog_set_arg(&ramstage, cbmem_top());
131 prog_run(&ramstage);
133 fail:
134 die_with_post_code(POSTCODE_INVALID_ROM, "Ramstage was not loaded!\n");
137 #if ENV_PAYLOAD_LOADER // gc-sections should take care of this
139 static struct prog global_payload =
140 PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload");
142 void payload_preload(void)
144 if (!CONFIG(CBFS_PRELOAD))
145 return;
147 cbfs_preload(global_payload.name);
150 void payload_load(void)
152 struct prog *payload = &global_payload;
153 void *mapping;
155 timestamp_add_now(TS_LOAD_PAYLOAD);
157 if (prog_locate_hook(payload))
158 goto out;
160 payload->cbfs_type = CBFS_TYPE_QUERY;
161 mapping = cbfs_type_map(prog_name(payload), NULL, &payload->cbfs_type);
163 if (!mapping)
164 goto out;
166 switch (prog_cbfs_type(payload)) {
167 case CBFS_TYPE_SELF: /* Simple ELF */
168 selfload_mapped(payload, mapping, BM_MEM_RAM);
169 break;
170 case CBFS_TYPE_FIT_PAYLOAD: /* Flattened image tree */
171 if (CONFIG(PAYLOAD_FIT_SUPPORT)) {
172 fit_payload(payload, mapping);
173 break;
175 __fallthrough;
176 default:
177 die_with_post_code(POSTCODE_INVALID_ROM,
178 "Unsupported payload type %d.\n", payload->cbfs_type);
179 break;
182 cbfs_unmap(mapping);
183 out:
184 if (prog_entry(payload) == NULL)
185 die_with_post_code(POSTCODE_INVALID_ROM, "Payload not loaded.\n");
188 void payload_run(void)
190 struct prog *payload = &global_payload;
192 /* Reset to booting from this image as late as possible */
193 boot_successful();
195 printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n",
196 prog_entry(payload), prog_entry_arg(payload));
198 post_code(POSTCODE_ENTER_ELF_BOOT);
200 timestamp_add_now(TS_SELFBOOT_JUMP);
202 /* Before we go off to run the payload, see if
203 * we stayed within our bounds.
205 checkstack(_estack, 0);
207 prog_run(payload);
210 #endif