soc/intel/skl: Add C232 chipset and reorder IDs
[coreboot.git] / src / lib / prog_loaders.c
blobdfabd3191017566f7a3d736f02d358fe87bbfb2b
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 <stage_cache.h>
29 #include <symbols.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)
39 struct cbfsf file;
41 cbfs_prepare_program_locate();
43 if (cbfs_boot_locate(&file, prog_name(prog), NULL))
44 return -1;
46 cbfsf_file_type(&file, &prog->cbfs_type);
48 cbfs_file_data(prog_rdev(prog), &file);
50 return 0;
53 void run_romstage(void)
55 struct prog romstage =
56 PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage");
58 if (prog_locate(&romstage))
59 goto fail;
61 timestamp_add_now(TS_START_COPYROM);
63 if (cbfs_prog_stage_load(&romstage))
64 goto fail;
66 timestamp_add_now(TS_END_COPYROM);
68 prog_run(&romstage);
70 fail:
71 if (CONFIG(BOOTBLOCK_CONSOLE))
72 die_with_post_code(POST_INVALID_ROM,
73 "Couldn't load romstage.\n");
74 halt();
77 static void ramstage_cache_invalid(void)
79 printk(BIOS_ERR, "ramstage cache invalid.\n");
80 if (CONFIG(RESET_ON_INVALID_RAMSTAGE_CACHE)) {
81 board_reset();
85 static void run_ramstage_from_resume(struct prog *ramstage)
87 if (!romstage_handoff_is_resume())
88 return;
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");
95 prog_run(ramstage);
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,
104 .prog = ramstage,
107 return rmodule_stage_load(&rmod_ram);
110 static int load_nonrelocatable_ramstage(struct prog *ramstage)
112 if (CONFIG(HAVE_ACPI_RESUME)) {
113 uintptr_t base = 0;
114 size_t size = cbfs_prog_stage_section(ramstage, &base);
115 if (size)
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");
127 if (ENV_POSTCAR)
128 timestamp_add_now(TS_END_POSTCAR);
130 /* Call "end of romstage" here if postcar stage doesn't exist */
131 if (ENV_ROMSTAGE)
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))
143 goto fail;
145 timestamp_add_now(TS_START_COPYRAM);
147 if (CONFIG(RELOCATABLE_RAMSTAGE)) {
148 if (load_relocatable_ramstage(&ramstage))
149 goto fail;
150 } else if (load_nonrelocatable_ramstage(&ramstage))
151 goto fail;
153 stage_cache_add(STAGE_RAMSTAGE, &ramstage);
155 timestamp_add_now(TS_END_COPYRAM);
157 prog_run(&ramstage);
159 fail:
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))
179 goto out;
181 mirror_payload(payload);
183 switch (prog_cbfs_type(payload)) {
184 case CBFS_TYPE_SELF: /* Simple ELF */
185 selfload_check(payload, BM_MEM_RAM);
186 break;
187 case CBFS_TYPE_FIT: /* Flattened image tree */
188 if (CONFIG(PAYLOAD_FIT_SUPPORT)) {
189 fit_payload(payload);
190 break;
191 } /* else fall-through */
192 default:
193 die_with_post_code(POST_INVALID_ROM,
194 "Unsupported payload type.\n");
195 break;
198 out:
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 */
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