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.
20 #include <console/console.h>
24 #include <program_loading.h>
26 #include <romstage_handoff.h>
29 #include <stage_cache.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
)
42 cbfs_prepare_program_locate();
44 if (cbfs_boot_locate(&file
, prog_name(prog
), NULL
))
47 cbfsf_file_type(&file
, &prog
->cbfs_type
);
49 cbfs_file_data(prog_rdev(prog
), &file
);
54 void run_romstage(void)
56 struct prog romstage
=
57 PROG_INIT(PROG_ROMSTAGE
, CONFIG_CBFS_PREFIX
"/romstage");
59 if (prog_locate(&romstage
))
62 timestamp_add_now(TS_START_COPYROM
);
64 if (cbfs_prog_stage_load(&romstage
))
67 timestamp_add_now(TS_END_COPYROM
);
72 if (IS_ENABLED(CONFIG_BOOTBLOCK_CONSOLE
))
73 die("Couldn't load romstage.\n");
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
)) {
90 static void run_ramstage_from_resume(struct prog
*ramstage
)
92 if (!romstage_handoff_is_resume())
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");
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
,
112 return rmodule_stage_load(&rmod_ram
);
115 static int load_nonrelocatable_ramstage(struct prog
*ramstage
)
117 if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME
)) {
119 size_t size
= cbfs_prog_stage_section(ramstage
, &base
);
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
))
145 timestamp_add_now(TS_START_COPYRAM
);
147 if (IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE
)) {
148 if (load_relocatable_ramstage(&ramstage
))
150 } else if (load_nonrelocatable_ramstage(&ramstage
))
153 if (!IS_ENABLED(CONFIG_NO_STAGE_CACHE
))
154 stage_cache_add(STAGE_RAMSTAGE
, &ramstage
);
156 timestamp_add_now(TS_END_COPYRAM
);
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
))
182 mirror_payload(payload
);
184 switch (prog_cbfs_type(payload
)) {
185 case CBFS_TYPE_SELF
: /* Simple ELF */
186 selfload_check(payload
);
188 case CBFS_TYPE_FIT
: /* Flattened image tree */
189 if (IS_ENABLED(CONFIG_PAYLOAD_FIT_SUPPORT
)) {
190 fit_payload(payload
);
192 } /* else fall-through */
194 die("Unsupported payload type.\n");
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 */
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);