sb/amd/agesa: Fix some white spaces issues
[coreboot.git] / src / lib / prog_loaders.c
blob01775b92bddda27c96b47489993020e2bba9493d
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 <stage_cache.h>
13 #include <symbols.h>
14 #include <thread.h>
15 #include <timestamp.h>
16 #include <security/vboot/vboot_common.h>
18 void run_romstage(void)
20 struct prog romstage =
21 PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage");
23 vboot_run_logic();
25 timestamp_add_now(TS_COPYROM_START);
27 if (ENV_X86 && CONFIG(BOOTBLOCK_NORMAL)) {
28 if (legacy_romstage_select_and_load(&romstage) != CB_SUCCESS)
29 goto fail;
30 } else {
31 if (cbfs_prog_stage_load(&romstage))
32 goto fail;
35 timestamp_add_now(TS_COPYROM_END);
37 console_time_report();
39 prog_run(&romstage);
41 fail:
42 if (CONFIG(BOOTBLOCK_CONSOLE))
43 die_with_post_code(POST_INVALID_ROM,
44 "Couldn't load romstage.\n");
45 halt();
48 int __weak prog_locate_hook(struct prog *prog) { return 0; }
50 static void run_ramstage_from_resume(struct prog *ramstage)
52 /* Load the cached ramstage to runtime location. */
53 stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
55 ramstage->cbfs_type = CBFS_TYPE_STAGE;
56 prog_set_arg(ramstage, cbmem_top());
58 if (prog_entry(ramstage) != NULL) {
59 printk(BIOS_DEBUG, "Jumping to image.\n");
60 prog_run(ramstage);
63 printk(BIOS_ERR, "ramstage cache invalid.\n");
64 board_reset();
67 static int load_relocatable_ramstage(struct prog *ramstage)
69 struct rmod_stage_load rmod_ram = {
70 .cbmem_id = CBMEM_ID_RAMSTAGE,
71 .prog = ramstage,
74 return rmodule_stage_load(&rmod_ram);
76 void preload_ramstage(void)
78 if (!CONFIG(CBFS_PRELOAD))
79 return;
81 printk(BIOS_DEBUG, "Preloading ramstage\n");
83 cbfs_preload(CONFIG_CBFS_PREFIX "/ramstage");
85 void __noreturn run_ramstage(void)
87 struct prog ramstage =
88 PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage");
90 /* Call "end of romstage" here if postcar stage doesn't exist */
91 if (ENV_POSTCAR)
92 timestamp_add_now(TS_POSTCAR_END);
93 else
94 timestamp_add_now(TS_ROMSTAGE_END);
97 * Only x86 systems using ramstage stage cache currently take the same
98 * firmware path on resume.
100 if (ENV_X86 && resume_from_stage_cache())
101 run_ramstage_from_resume(&ramstage);
103 vboot_run_logic();
105 timestamp_add_now(TS_COPYRAM_START);
107 if (ENV_X86) {
108 if (load_relocatable_ramstage(&ramstage))
109 goto fail;
110 } else {
111 if (cbfs_prog_stage_load(&ramstage))
112 goto fail;
115 stage_cache_add(STAGE_RAMSTAGE, &ramstage);
117 timestamp_add_now(TS_COPYRAM_END);
119 console_time_report();
121 /* This overrides the arg fetched from the relocatable module */
122 prog_set_arg(&ramstage, cbmem_top());
124 prog_run(&ramstage);
126 fail:
127 die_with_post_code(POST_INVALID_ROM, "Ramstage was not loaded!\n");
130 #if ENV_PAYLOAD_LOADER // gc-sections should take care of this
132 static struct prog global_payload =
133 PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload");
135 void payload_preload(void)
137 if (!CONFIG(CBFS_PRELOAD))
138 return;
140 cbfs_preload(global_payload.name);
143 void payload_load(void)
145 struct prog *payload = &global_payload;
146 void *mapping;
148 timestamp_add_now(TS_LOAD_PAYLOAD);
150 if (prog_locate_hook(payload))
151 goto out;
153 payload->cbfs_type = CBFS_TYPE_QUERY;
154 mapping = cbfs_type_map(prog_name(payload), NULL, &payload->cbfs_type);
156 if (!mapping)
157 goto out;
159 switch (prog_cbfs_type(payload)) {
160 case CBFS_TYPE_SELF: /* Simple ELF */
161 selfload_mapped(payload, mapping, BM_MEM_RAM);
162 break;
163 case CBFS_TYPE_FIT_PAYLOAD: /* Flattened image tree */
164 if (CONFIG(PAYLOAD_FIT_SUPPORT)) {
165 fit_payload(payload, mapping);
166 break;
168 __fallthrough;
169 default:
170 die_with_post_code(POST_INVALID_ROM,
171 "Unsupported payload type %d.\n", payload->cbfs_type);
172 break;
175 cbfs_unmap(mapping);
176 out:
177 if (prog_entry(payload) == NULL)
178 die_with_post_code(POST_INVALID_ROM, "Payload not loaded.\n");
181 void payload_run(void)
183 struct prog *payload = &global_payload;
185 /* Reset to booting from this image as late as possible */
186 boot_successful();
188 printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n",
189 prog_entry(payload), prog_entry_arg(payload));
191 post_code(POST_ENTER_ELF_BOOT);
193 timestamp_add_now(TS_SELFBOOT_JUMP);
195 /* Before we go off to run the payload, see if
196 * we stayed within our bounds.
198 checkstack(_estack, 0);
200 prog_run(payload);
203 #endif