blob: 3f5d95ca6ca283d85b289f449a1a105b38ec82fd [file] [log] [blame]
Aaron Durbin899d13d2015-05-15 23:39:23 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2015 Google Inc.
5 *
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.
9 *
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.
Aaron Durbin899d13d2015-05-15 23:39:23 -050014 */
15
16
17#include <stdlib.h>
18#include <arch/stages.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050019#include <cbfs.h>
20#include <cbmem.h>
21#include <console/console.h>
22#include <fallback.h>
23#include <halt.h>
24#include <lib.h>
25#include <program_loading.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
Aaron Durbin6a452ef2015-05-19 16:25:20 -050033/* Only can represent up to 1 byte less than size_t. */
34const struct mem_region_device addrspace_32bit = MEM_REGION_DEV_INIT(0, ~0UL);
35
Aaron Durbin6d720f32015-12-08 17:00:23 -060036int prog_locate(struct prog *prog)
37{
38 struct cbfsf file;
39
40 cbfs_prepare_program_locate();
41
42 if (cbfs_boot_locate(&file, prog_name(prog), NULL))
43 return -1;
44
45 cbfs_file_data(prog_rdev(prog), &file);
46
47 return 0;
48}
49
Aaron Durbin899d13d2015-05-15 23:39:23 -050050void run_romstage(void)
51{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050052 struct prog romstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060053 PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050054
55 if (prog_locate(&romstage))
56 goto fail;
57
58 timestamp_add_now(TS_START_COPYROM);
59
60 if (cbfs_prog_stage_load(&romstage))
61 goto fail;
62
63 timestamp_add_now(TS_END_COPYROM);
64
65 prog_run(&romstage);
66
67fail:
68 if (IS_ENABLED(CONFIG_BOOTBLOCK_CONSOLE))
69 die("Couldn't load romstage.\n");
70 halt();
71}
72
Aaron Durbin54546c92015-08-05 00:52:13 -050073void __attribute__((weak)) stage_cache_add(int stage_id,
74 const struct prog *stage) {}
Aaron Durbin899d13d2015-05-15 23:39:23 -050075void __attribute__((weak)) stage_cache_load_stage(int stage_id,
76 struct prog *stage) {}
77void __attribute__((weak)) ramstage_cache_invalid(void) {}
78
79static void run_ramstage_from_resume(struct romstage_handoff *handoff,
80 struct prog *ramstage)
81{
82 if (handoff != NULL && handoff->s3_resume) {
83 /* Load the cached ramstage to runtime location. */
84 stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
85
86 if (prog_entry(ramstage) != NULL) {
87 printk(BIOS_DEBUG, "Jumping to image.\n");
88 prog_run(ramstage);
89 }
90 ramstage_cache_invalid();
91 }
92}
93
94static int load_relocatable_ramstage(struct prog *ramstage)
95{
96 struct rmod_stage_load rmod_ram = {
97 .cbmem_id = CBMEM_ID_RAMSTAGE,
98 .prog = ramstage,
99 };
100
101 return rmodule_stage_load(&rmod_ram);
102}
103
104void run_ramstage(void)
105{
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500106 struct prog ramstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600107 PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500108
Aaron Durbin9796f602015-09-23 19:54:12 -0500109 timestamp_add_now(TS_END_ROMSTAGE);
110
Aaron Durbin899d13d2015-05-15 23:39:23 -0500111 /* Only x86 systems currently take the same firmware path on resume. */
112 if (IS_ENABLED(CONFIG_ARCH_X86) && IS_ENABLED(CONFIG_EARLY_CBMEM_INIT))
113 run_ramstage_from_resume(romstage_handoff_find_or_add(),
114 &ramstage);
115
116 if (prog_locate(&ramstage))
117 goto fail;
118
119 timestamp_add_now(TS_START_COPYRAM);
120
121 if (IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE)) {
122 if (load_relocatable_ramstage(&ramstage))
123 goto fail;
124 } else if (cbfs_prog_stage_load(&ramstage))
125 goto fail;
126
127 stage_cache_add(STAGE_RAMSTAGE, &ramstage);
128
129 timestamp_add_now(TS_END_COPYRAM);
130
131 prog_run(&ramstage);
132
133fail:
134 die("Ramstage was not loaded!\n");
135}
136
Stefan Reinauereec2db42015-06-18 01:19:50 -0700137#ifdef __RAMSTAGE__ // gc-sections should take care of this
138
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500139static struct prog global_payload =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600140 PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500141
142void __attribute__((weak)) mirror_payload(struct prog *payload)
143{
144 return;
145}
146
147void payload_load(void)
148{
149 struct prog *payload = &global_payload;
150
151 timestamp_add_now(TS_LOAD_PAYLOAD);
152
153 if (prog_locate(payload))
154 goto out;
155
156 mirror_payload(payload);
157
158 /* Pass cbtables to payload if architecture desires it. */
159 prog_set_entry(payload, selfload(payload),
160 cbmem_find(CBMEM_ID_CBTABLE));
161
162out:
163 if (prog_entry(payload) == NULL)
164 die("Payload not loaded.\n");
165}
166
167void payload_run(void)
168{
169 struct prog *payload = &global_payload;
170
171 /* Reset to booting from this image as late as possible */
172 boot_successful();
173
174 printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n",
175 prog_entry(payload), prog_entry_arg(payload));
176
177 post_code(POST_ENTER_ELF_BOOT);
178
179 timestamp_add_now(TS_SELFBOOT_JUMP);
180
181 /* Before we go off to run the payload, see if
182 * we stayed within our bounds.
183 */
184 checkstack(_estack, 0);
185
186 prog_run(payload);
187}
Stefan Reinauereec2db42015-06-18 01:19:50 -0700188
189#endif