blob: 43f46899407401c1bedbb9d7747e07cd290cc7ad [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.
Frans Hendriksfc580342019-06-14 14:36:37 +02005 * Copyright (C) 2018-2019 Eltan B.V.
Aaron Durbin899d13d2015-05-15 23:39:23 -05006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Aaron Durbin899d13d2015-05-15 23:39:23 -050015 */
16
17
18#include <stdlib.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>
Aaron Durbin8cd723b2016-10-28 17:32:24 -050026#include <reset.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050027#include <romstage_handoff.h>
28#include <rmodule.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050029#include <stage_cache.h>
30#include <symbols.h>
31#include <timestamp.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020032#include <fit_payload.h>
Wim Vervoorn1058dd82019-11-01 10:22:22 +010033#include <security/vboot/vboot_common.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050034
Aaron Durbin6a452ef2015-05-19 16:25:20 -050035/* Only can represent up to 1 byte less than size_t. */
Antonello Dettorie5f48d22016-06-22 21:09:08 +020036const struct mem_region_device addrspace_32bit =
37 MEM_REGION_DEV_RO_INIT(0, ~0UL);
Aaron Durbin6a452ef2015-05-19 16:25:20 -050038
Aaron Durbin6d720f32015-12-08 17:00:23 -060039int prog_locate(struct prog *prog)
40{
41 struct cbfsf file;
42
Frans Hendriksfc580342019-06-14 14:36:37 +020043 if (prog_locate_hook(prog))
44 return -1;
45
Aaron Durbin6d720f32015-12-08 17:00:23 -060046 cbfs_prepare_program_locate();
47
48 if (cbfs_boot_locate(&file, prog_name(prog), NULL))
49 return -1;
50
Patrick Rudolph71327fb2018-05-03 10:35:26 +020051 cbfsf_file_type(&file, &prog->cbfs_type);
52
Aaron Durbin6d720f32015-12-08 17:00:23 -060053 cbfs_file_data(prog_rdev(prog), &file);
54
55 return 0;
56}
57
Aaron Durbin899d13d2015-05-15 23:39:23 -050058void run_romstage(void)
59{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050060 struct prog romstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060061 PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050062
Wim Vervoorn1058dd82019-11-01 10:22:22 +010063 vboot_run_logic();
64
Aaron Durbin899d13d2015-05-15 23:39:23 -050065 if (prog_locate(&romstage))
66 goto fail;
67
68 timestamp_add_now(TS_START_COPYROM);
69
70 if (cbfs_prog_stage_load(&romstage))
71 goto fail;
72
73 timestamp_add_now(TS_END_COPYROM);
74
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020075 console_time_report();
76
Aaron Durbin899d13d2015-05-15 23:39:23 -050077 prog_run(&romstage);
78
79fail:
Julius Wernercd49cce2019-03-05 16:53:33 -080080 if (CONFIG(BOOTBLOCK_CONSOLE))
Keith Short70064582019-05-06 16:12:57 -060081 die_with_post_code(POST_INVALID_ROM,
82 "Couldn't load romstage.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -050083 halt();
84}
85
Frans Hendriksfc580342019-06-14 14:36:37 +020086int __weak prog_locate_hook(struct prog *prog) { return 0; }
87
Aaron Durbin8cd723b2016-10-28 17:32:24 -050088static void ramstage_cache_invalid(void)
89{
90 printk(BIOS_ERR, "ramstage cache invalid.\n");
Julius Wernercd49cce2019-03-05 16:53:33 -080091 if (CONFIG(RESET_ON_INVALID_RAMSTAGE_CACHE)) {
Nico Huber4f32b642018-10-05 23:40:21 +020092 board_reset();
Aaron Durbin8cd723b2016-10-28 17:32:24 -050093 }
94}
Aaron Durbin899d13d2015-05-15 23:39:23 -050095
Aaron Durbin6c191d82016-11-29 21:22:42 -060096static void run_ramstage_from_resume(struct prog *ramstage)
Aaron Durbin899d13d2015-05-15 23:39:23 -050097{
Aaron Durbin6c191d82016-11-29 21:22:42 -060098 if (!romstage_handoff_is_resume())
99 return;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500100
Aaron Durbin6c191d82016-11-29 21:22:42 -0600101 /* Load the cached ramstage to runtime location. */
102 stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
103
Arthur Heymans5331a7c2019-10-23 17:07:15 +0200104 prog_set_arg(ramstage, cbmem_top());
105
Aaron Durbin6c191d82016-11-29 21:22:42 -0600106 if (prog_entry(ramstage) != NULL) {
107 printk(BIOS_DEBUG, "Jumping to image.\n");
108 prog_run(ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500109 }
Aaron Durbin6c191d82016-11-29 21:22:42 -0600110 ramstage_cache_invalid();
Aaron Durbin899d13d2015-05-15 23:39:23 -0500111}
112
113static int load_relocatable_ramstage(struct prog *ramstage)
114{
115 struct rmod_stage_load rmod_ram = {
116 .cbmem_id = CBMEM_ID_RAMSTAGE,
117 .prog = ramstage,
118 };
119
120 return rmodule_stage_load(&rmod_ram);
121}
122
123void run_ramstage(void)
124{
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500125 struct prog ramstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600126 PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500127
Subrata Banik4f42eea2019-03-05 16:45:14 +0530128 if (ENV_POSTCAR)
129 timestamp_add_now(TS_END_POSTCAR);
130
Subrata Banik2847e1e2019-02-25 20:31:22 +0530131 /* Call "end of romstage" here if postcar stage doesn't exist */
132 if (ENV_ROMSTAGE)
133 timestamp_add_now(TS_END_ROMSTAGE);
Aaron Durbin9796f602015-09-23 19:54:12 -0500134
Furquan Shaikh1e162bf2016-05-06 09:20:35 -0700135 /*
136 * Only x86 systems using ramstage stage cache currently take the same
137 * firmware path on resume.
138 */
Julius Wernercd49cce2019-03-05 16:53:33 -0800139 if (CONFIG(ARCH_X86) &&
140 !CONFIG(NO_STAGE_CACHE))
Aaron Durbin6c191d82016-11-29 21:22:42 -0600141 run_ramstage_from_resume(&ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500142
Wim Vervoorn1058dd82019-11-01 10:22:22 +0100143 vboot_run_logic();
144
Aaron Durbin899d13d2015-05-15 23:39:23 -0500145 if (prog_locate(&ramstage))
146 goto fail;
147
148 timestamp_add_now(TS_START_COPYRAM);
149
Julius Wernercd49cce2019-03-05 16:53:33 -0800150 if (CONFIG(RELOCATABLE_RAMSTAGE)) {
Aaron Durbin899d13d2015-05-15 23:39:23 -0500151 if (load_relocatable_ramstage(&ramstage))
152 goto fail;
Kyösti Mälkki7cd2c072018-06-03 23:04:28 +0300153 } else if (cbfs_prog_stage_load(&ramstage))
Aaron Durbin899d13d2015-05-15 23:39:23 -0500154 goto fail;
155
Subrata Banik90f750b2019-06-11 17:52:06 +0530156 stage_cache_add(STAGE_RAMSTAGE, &ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500157
158 timestamp_add_now(TS_END_COPYRAM);
159
Kyösti Mälkki45ddb432019-11-02 14:12:18 +0200160 console_time_report();
161
Arthur Heymans5331a7c2019-10-23 17:07:15 +0200162 /* This overrides the arg fetched from the relocatable module */
163 prog_set_arg(&ramstage, cbmem_top());
164
Aaron Durbin899d13d2015-05-15 23:39:23 -0500165 prog_run(&ramstage);
166
167fail:
Keith Short70064582019-05-06 16:12:57 -0600168 die_with_post_code(POST_INVALID_ROM, "Ramstage was not loaded!\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500169}
170
Subrata Banik42c44c22019-05-15 20:27:04 +0530171#if ENV_PAYLOAD_LOADER // gc-sections should take care of this
Stefan Reinauereec2db42015-06-18 01:19:50 -0700172
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500173static struct prog global_payload =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600174 PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500175
Aaron Durbin64031672018-04-21 14:45:32 -0600176void __weak mirror_payload(struct prog *payload)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500177{
Aaron Durbin899d13d2015-05-15 23:39:23 -0500178}
179
180void payload_load(void)
181{
182 struct prog *payload = &global_payload;
183
184 timestamp_add_now(TS_LOAD_PAYLOAD);
185
186 if (prog_locate(payload))
187 goto out;
188
189 mirror_payload(payload);
190
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200191 switch (prog_cbfs_type(payload)) {
192 case CBFS_TYPE_SELF: /* Simple ELF */
Ting Shen05532262019-01-28 17:22:22 +0800193 selfload_check(payload, BM_MEM_RAM);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200194 break;
195 case CBFS_TYPE_FIT: /* Flattened image tree */
Julius Wernercd49cce2019-03-05 16:53:33 -0800196 if (CONFIG(PAYLOAD_FIT_SUPPORT)) {
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200197 fit_payload(payload);
198 break;
199 } /* else fall-through */
200 default:
Keith Short70064582019-05-06 16:12:57 -0600201 die_with_post_code(POST_INVALID_ROM,
202 "Unsupported payload type.\n");
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200203 break;
204 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500205
206out:
207 if (prog_entry(payload) == NULL)
Keith Short70064582019-05-06 16:12:57 -0600208 die_with_post_code(POST_INVALID_ROM, "Payload not loaded.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500209}
210
211void payload_run(void)
212{
213 struct prog *payload = &global_payload;
214
215 /* Reset to booting from this image as late as possible */
216 boot_successful();
217
218 printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n",
219 prog_entry(payload), prog_entry_arg(payload));
220
221 post_code(POST_ENTER_ELF_BOOT);
222
223 timestamp_add_now(TS_SELFBOOT_JUMP);
224
225 /* Before we go off to run the payload, see if
226 * we stayed within our bounds.
227 */
228 checkstack(_estack, 0);
229
230 prog_run(payload);
231}
Stefan Reinauereec2db42015-06-18 01:19:50 -0700232
233#endif