arch/x86 cbmem: Drop tests for LATE_CBMEM_INIT
[coreboot.git] / src / lib / prog_loaders.c
blobb7634177ff2215d1eb8d8de76aa087f19c6972fc
1 /*
2 * This file is part of the coreboot project.
4 * Copyright 2015 Google Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <stdlib.h>
18 #include <cbfs.h>
19 #include <cbmem.h>
20 #include <console/console.h>
21 #include <fallback.h>
22 #include <halt.h>
23 #include <lib.h>
24 #include <program_loading.h>
25 #include <reset.h>
26 #include <romstage_handoff.h>
27 #include <rmodule.h>
28 #include <rules.h>
29 #include <stage_cache.h>
30 #include <symbols.h>
31 #include <timestamp.h>
32 #include <fit_payload.h>
34 /* Only can represent up to 1 byte less than size_t. */
35 const struct mem_region_device addrspace_32bit =
36 MEM_REGION_DEV_RO_INIT(0, ~0UL);
38 int prog_locate(struct prog *prog)
40 struct cbfsf file;
42 cbfs_prepare_program_locate();
44 if (cbfs_boot_locate(&file, prog_name(prog), NULL))
45 return -1;
47 cbfsf_file_type(&file, &prog->cbfs_type);
49 cbfs_file_data(prog_rdev(prog), &file);
51 return 0;
54 void run_romstage(void)
56 struct prog romstage =
57 PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage");
59 if (prog_locate(&romstage))
60 goto fail;
62 timestamp_add_now(TS_START_COPYROM);
64 if (cbfs_prog_stage_load(&romstage))
65 goto fail;
67 timestamp_add_now(TS_END_COPYROM);
69 prog_run(&romstage);
71 fail:
72 if (IS_ENABLED(CONFIG_BOOTBLOCK_CONSOLE))
73 die("Couldn't load romstage.\n");
74 halt();
77 void __weak stage_cache_add(int stage_id,
78 const struct prog *stage) {}
79 void __weak stage_cache_load_stage(int stage_id,
80 struct prog *stage) {}
82 static void ramstage_cache_invalid(void)
84 printk(BIOS_ERR, "ramstage cache invalid.\n");
85 if (IS_ENABLED(CONFIG_RESET_ON_INVALID_RAMSTAGE_CACHE)) {
86 board_reset();
90 static void run_ramstage_from_resume(struct prog *ramstage)
92 if (!romstage_handoff_is_resume())
93 return;
95 /* Load the cached ramstage to runtime location. */
96 stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
98 if (prog_entry(ramstage) != NULL) {
99 printk(BIOS_DEBUG, "Jumping to image.\n");
100 prog_run(ramstage);
102 ramstage_cache_invalid();
105 static int load_relocatable_ramstage(struct prog *ramstage)
107 struct rmod_stage_load rmod_ram = {
108 .cbmem_id = CBMEM_ID_RAMSTAGE,
109 .prog = ramstage,
112 return rmodule_stage_load(&rmod_ram);
115 static int load_nonrelocatable_ramstage(struct prog *ramstage)
117 if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) {
118 uintptr_t base = 0;
119 size_t size = cbfs_prog_stage_section(ramstage, &base);
120 if (size)
121 backup_ramstage_section(base, size);
124 return cbfs_prog_stage_load(ramstage);
127 void run_ramstage(void)
129 struct prog ramstage =
130 PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage");
132 timestamp_add_now(TS_END_ROMSTAGE);
135 * Only x86 systems using ramstage stage cache currently take the same
136 * firmware path on resume.
138 if (IS_ENABLED(CONFIG_ARCH_X86) &&
139 !IS_ENABLED(CONFIG_NO_STAGE_CACHE))
140 run_ramstage_from_resume(&ramstage);
142 if (prog_locate(&ramstage))
143 goto fail;
145 timestamp_add_now(TS_START_COPYRAM);
147 if (IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE)) {
148 if (load_relocatable_ramstage(&ramstage))
149 goto fail;
150 } else if (load_nonrelocatable_ramstage(&ramstage))
151 goto fail;
153 if (!IS_ENABLED(CONFIG_NO_STAGE_CACHE))
154 stage_cache_add(STAGE_RAMSTAGE, &ramstage);
156 timestamp_add_now(TS_END_COPYRAM);
158 prog_run(&ramstage);
160 fail:
161 die("Ramstage was not loaded!\n");
164 #ifdef __RAMSTAGE__ // gc-sections should take care of this
166 static struct prog global_payload =
167 PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload");
169 void __weak mirror_payload(struct prog *payload)
173 void payload_load(void)
175 struct prog *payload = &global_payload;
177 timestamp_add_now(TS_LOAD_PAYLOAD);
179 if (prog_locate(payload))
180 goto out;
182 mirror_payload(payload);
184 switch (prog_cbfs_type(payload)) {
185 case CBFS_TYPE_SELF: /* Simple ELF */
186 selfload_check(payload);
187 break;
188 case CBFS_TYPE_FIT: /* Flattened image tree */
189 if (IS_ENABLED(CONFIG_PAYLOAD_FIT_SUPPORT)) {
190 fit_payload(payload);
191 break;
192 } /* else fall-through */
193 default:
194 die("Unsupported payload type.\n");
195 break;
198 out:
199 if (prog_entry(payload) == NULL)
200 die("Payload not loaded.\n");
203 void payload_run(void)
205 struct prog *payload = &global_payload;
207 /* Reset to booting from this image as late as possible */
208 boot_successful();
210 printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n",
211 prog_entry(payload), prog_entry_arg(payload));
213 post_code(POST_ENTER_ELF_BOOT);
215 timestamp_add_now(TS_SELFBOOT_JUMP);
217 /* Before we go off to run the payload, see if
218 * we stayed within our bounds.
220 checkstack(_estack, 0);
222 prog_run(payload);
225 #endif