1 /* SPDX-License-Identifier: GPL-2.0-only */
5 #include <console/console.h>
9 #include <program_loading.h>
12 #include <romstage_common.h>
13 #include <security/vboot/vboot_common.h>
14 #include <stage_cache.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
);
27 struct prog romstage
=
28 PROG_INIT(PROG_ROMSTAGE
, CONFIG_CBFS_PREFIX
"/romstage");
32 timestamp_add_now(TS_COPYROM_START
);
34 if (ENV_X86
&& CONFIG(BOOTBLOCK_NORMAL
)) {
35 if (legacy_romstage_select_and_load(&romstage
) != CB_SUCCESS
)
38 if (cbfs_prog_stage_load(&romstage
))
42 timestamp_add_now(TS_COPYROM_END
);
44 console_time_report();
49 if (CONFIG(BOOTBLOCK_CONSOLE
))
50 die_with_post_code(POSTCODE_INVALID_ROM
,
51 "Couldn't load romstage.\n");
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
, (void *)cbmem_top());
65 if (prog_entry(ramstage
) != NULL
) {
66 printk(BIOS_DEBUG
, "Jumping to image.\n");
70 printk(BIOS_ERR
, "ramstage cache invalid.\n");
74 static int load_relocatable_ramstage(struct prog
*ramstage
)
76 struct rmod_stage_load rmod_ram
= {
77 .cbmem_id
= CBMEM_ID_RAMSTAGE
,
81 return rmodule_stage_load(&rmod_ram
);
83 void preload_ramstage(void)
85 if (!CONFIG(CBFS_PRELOAD
))
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 */
99 timestamp_add_now(TS_POSTCAR_END
);
101 timestamp_add_now(TS_ROMSTAGE_END
);
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
);
115 if (load_relocatable_ramstage(&ramstage
))
118 if (cbfs_prog_stage_load(&ramstage
))
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
, (void *)cbmem_top());
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
))
147 cbfs_preload(global_payload
.name
);
150 void payload_load(void)
152 struct prog
*payload
= &global_payload
;
155 timestamp_add_now(TS_LOAD_PAYLOAD
);
157 if (prog_locate_hook(payload
))
160 payload
->cbfs_type
= CBFS_TYPE_QUERY
;
161 mapping
= cbfs_type_map(prog_name(payload
), NULL
, &payload
->cbfs_type
);
166 switch (prog_cbfs_type(payload
)) {
167 case CBFS_TYPE_SELF
: /* Simple ELF */
168 selfload_mapped(payload
, mapping
, BM_MEM_RAM
);
170 case CBFS_TYPE_FIT_PAYLOAD
: /* Flattened image tree */
171 if (CONFIG(PAYLOAD_FIT_SUPPORT
)) {
172 fit_payload(payload
, mapping
);
177 die_with_post_code(POSTCODE_INVALID_ROM
,
178 "Unsupported payload type %d.\n", payload
->cbfs_type
);
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 */
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);