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>
28 #include <stage_cache.h>
30 #include <timestamp.h>
31 #include <fit_payload.h>
33 /* Only can represent up to 1 byte less than size_t. */
34 const struct mem_region_device addrspace_32bit
=
35 MEM_REGION_DEV_RO_INIT(0, ~0UL);
37 int prog_locate(struct prog
*prog
)
41 cbfs_prepare_program_locate();
43 if (cbfs_boot_locate(&file
, prog_name(prog
), NULL
))
46 cbfsf_file_type(&file
, &prog
->cbfs_type
);
48 cbfs_file_data(prog_rdev(prog
), &file
);
53 void run_romstage(void)
55 struct prog romstage
=
56 PROG_INIT(PROG_ROMSTAGE
, CONFIG_CBFS_PREFIX
"/romstage");
58 if (prog_locate(&romstage
))
61 timestamp_add_now(TS_START_COPYROM
);
63 if (cbfs_prog_stage_load(&romstage
))
66 timestamp_add_now(TS_END_COPYROM
);
71 if (CONFIG(BOOTBLOCK_CONSOLE
))
72 die_with_post_code(POST_INVALID_ROM
,
73 "Couldn't load romstage.\n");
77 static void ramstage_cache_invalid(void)
79 printk(BIOS_ERR
, "ramstage cache invalid.\n");
80 if (CONFIG(RESET_ON_INVALID_RAMSTAGE_CACHE
)) {
85 static void run_ramstage_from_resume(struct prog
*ramstage
)
87 if (!romstage_handoff_is_resume())
90 /* Load the cached ramstage to runtime location. */
91 stage_cache_load_stage(STAGE_RAMSTAGE
, ramstage
);
93 if (prog_entry(ramstage
) != NULL
) {
94 printk(BIOS_DEBUG
, "Jumping to image.\n");
97 ramstage_cache_invalid();
100 static int load_relocatable_ramstage(struct prog
*ramstage
)
102 struct rmod_stage_load rmod_ram
= {
103 .cbmem_id
= CBMEM_ID_RAMSTAGE
,
107 return rmodule_stage_load(&rmod_ram
);
110 static int load_nonrelocatable_ramstage(struct prog
*ramstage
)
112 if (CONFIG(HAVE_ACPI_RESUME
)) {
114 size_t size
= cbfs_prog_stage_section(ramstage
, &base
);
116 backup_ramstage_section(base
, size
);
119 return cbfs_prog_stage_load(ramstage
);
122 void run_ramstage(void)
124 struct prog ramstage
=
125 PROG_INIT(PROG_RAMSTAGE
, CONFIG_CBFS_PREFIX
"/ramstage");
128 timestamp_add_now(TS_END_POSTCAR
);
130 /* Call "end of romstage" here if postcar stage doesn't exist */
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 (CONFIG(ARCH_X86
) &&
139 !CONFIG(NO_STAGE_CACHE
))
140 run_ramstage_from_resume(&ramstage
);
142 if (prog_locate(&ramstage
))
145 timestamp_add_now(TS_START_COPYRAM
);
147 if (CONFIG(RELOCATABLE_RAMSTAGE
)) {
148 if (load_relocatable_ramstage(&ramstage
))
150 } else if (load_nonrelocatable_ramstage(&ramstage
))
153 stage_cache_add(STAGE_RAMSTAGE
, &ramstage
);
155 timestamp_add_now(TS_END_COPYRAM
);
160 die_with_post_code(POST_INVALID_ROM
, "Ramstage was not loaded!\n");
163 #if ENV_PAYLOAD_LOADER // gc-sections should take care of this
165 static struct prog global_payload
=
166 PROG_INIT(PROG_PAYLOAD
, CONFIG_CBFS_PREFIX
"/payload");
168 void __weak
mirror_payload(struct prog
*payload
)
172 void payload_load(void)
174 struct prog
*payload
= &global_payload
;
176 timestamp_add_now(TS_LOAD_PAYLOAD
);
178 if (prog_locate(payload
))
181 mirror_payload(payload
);
183 switch (prog_cbfs_type(payload
)) {
184 case CBFS_TYPE_SELF
: /* Simple ELF */
185 selfload_check(payload
, BM_MEM_RAM
);
187 case CBFS_TYPE_FIT
: /* Flattened image tree */
188 if (CONFIG(PAYLOAD_FIT_SUPPORT
)) {
189 fit_payload(payload
);
191 } /* else fall-through */
193 die_with_post_code(POST_INVALID_ROM
,
194 "Unsupported payload type.\n");
199 if (prog_entry(payload
) == NULL
)
200 die_with_post_code(POST_INVALID_ROM
, "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);