blob: 183a22bff092a990963c3dc59cbdfb47ca99c3e4 [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>
Aaron Durbin899d13d2015-05-15 23:39:23 -050033
Aaron Durbin6a452ef2015-05-19 16:25:20 -050034/* Only can represent up to 1 byte less than size_t. */
Antonello Dettorie5f48d22016-06-22 21:09:08 +020035const struct mem_region_device addrspace_32bit =
36 MEM_REGION_DEV_RO_INIT(0, ~0UL);
Aaron Durbin6a452ef2015-05-19 16:25:20 -050037
Aaron Durbin6d720f32015-12-08 17:00:23 -060038int prog_locate(struct prog *prog)
39{
40 struct cbfsf file;
41
Frans Hendriksfc580342019-06-14 14:36:37 +020042 if (prog_locate_hook(prog))
43 return -1;
44
Aaron Durbin6d720f32015-12-08 17:00:23 -060045 cbfs_prepare_program_locate();
46
47 if (cbfs_boot_locate(&file, prog_name(prog), NULL))
48 return -1;
49
Patrick Rudolph71327fb2018-05-03 10:35:26 +020050 cbfsf_file_type(&file, &prog->cbfs_type);
51
Aaron Durbin6d720f32015-12-08 17:00:23 -060052 cbfs_file_data(prog_rdev(prog), &file);
53
54 return 0;
55}
56
Aaron Durbin899d13d2015-05-15 23:39:23 -050057void run_romstage(void)
58{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050059 struct prog romstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060060 PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -050061
62 if (prog_locate(&romstage))
63 goto fail;
64
65 timestamp_add_now(TS_START_COPYROM);
66
67 if (cbfs_prog_stage_load(&romstage))
68 goto fail;
69
70 timestamp_add_now(TS_END_COPYROM);
71
72 prog_run(&romstage);
73
74fail:
Julius Wernercd49cce2019-03-05 16:53:33 -080075 if (CONFIG(BOOTBLOCK_CONSOLE))
Keith Short70064582019-05-06 16:12:57 -060076 die_with_post_code(POST_INVALID_ROM,
77 "Couldn't load romstage.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -050078 halt();
79}
80
Frans Hendriksfc580342019-06-14 14:36:37 +020081int __weak prog_locate_hook(struct prog *prog) { return 0; }
82
Aaron Durbin8cd723b2016-10-28 17:32:24 -050083static void ramstage_cache_invalid(void)
84{
85 printk(BIOS_ERR, "ramstage cache invalid.\n");
Julius Wernercd49cce2019-03-05 16:53:33 -080086 if (CONFIG(RESET_ON_INVALID_RAMSTAGE_CACHE)) {
Nico Huber4f32b642018-10-05 23:40:21 +020087 board_reset();
Aaron Durbin8cd723b2016-10-28 17:32:24 -050088 }
89}
Aaron Durbin899d13d2015-05-15 23:39:23 -050090
Aaron Durbin6c191d82016-11-29 21:22:42 -060091static void run_ramstage_from_resume(struct prog *ramstage)
Aaron Durbin899d13d2015-05-15 23:39:23 -050092{
Aaron Durbin6c191d82016-11-29 21:22:42 -060093 if (!romstage_handoff_is_resume())
94 return;
Aaron Durbin899d13d2015-05-15 23:39:23 -050095
Aaron Durbin6c191d82016-11-29 21:22:42 -060096 /* Load the cached ramstage to runtime location. */
97 stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
98
Arthur Heymans5331a7c2019-10-23 17:07:15 +020099 prog_set_arg(ramstage, cbmem_top());
100
Aaron Durbin6c191d82016-11-29 21:22:42 -0600101 if (prog_entry(ramstage) != NULL) {
102 printk(BIOS_DEBUG, "Jumping to image.\n");
103 prog_run(ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500104 }
Aaron Durbin6c191d82016-11-29 21:22:42 -0600105 ramstage_cache_invalid();
Aaron Durbin899d13d2015-05-15 23:39:23 -0500106}
107
108static int load_relocatable_ramstage(struct prog *ramstage)
109{
110 struct rmod_stage_load rmod_ram = {
111 .cbmem_id = CBMEM_ID_RAMSTAGE,
112 .prog = ramstage,
113 };
114
115 return rmodule_stage_load(&rmod_ram);
116}
117
118void run_ramstage(void)
119{
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500120 struct prog ramstage =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600121 PROG_INIT(PROG_RAMSTAGE, CONFIG_CBFS_PREFIX "/ramstage");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500122
Subrata Banik4f42eea2019-03-05 16:45:14 +0530123 if (ENV_POSTCAR)
124 timestamp_add_now(TS_END_POSTCAR);
125
Subrata Banik2847e1e2019-02-25 20:31:22 +0530126 /* Call "end of romstage" here if postcar stage doesn't exist */
127 if (ENV_ROMSTAGE)
128 timestamp_add_now(TS_END_ROMSTAGE);
Aaron Durbin9796f602015-09-23 19:54:12 -0500129
Furquan Shaikh1e162bf2016-05-06 09:20:35 -0700130 /*
131 * Only x86 systems using ramstage stage cache currently take the same
132 * firmware path on resume.
133 */
Julius Wernercd49cce2019-03-05 16:53:33 -0800134 if (CONFIG(ARCH_X86) &&
135 !CONFIG(NO_STAGE_CACHE))
Aaron Durbin6c191d82016-11-29 21:22:42 -0600136 run_ramstage_from_resume(&ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500137
138 if (prog_locate(&ramstage))
139 goto fail;
140
141 timestamp_add_now(TS_START_COPYRAM);
142
Julius Wernercd49cce2019-03-05 16:53:33 -0800143 if (CONFIG(RELOCATABLE_RAMSTAGE)) {
Aaron Durbin899d13d2015-05-15 23:39:23 -0500144 if (load_relocatable_ramstage(&ramstage))
145 goto fail;
Kyösti Mälkki7cd2c072018-06-03 23:04:28 +0300146 } else if (cbfs_prog_stage_load(&ramstage))
Aaron Durbin899d13d2015-05-15 23:39:23 -0500147 goto fail;
148
Subrata Banik90f750b2019-06-11 17:52:06 +0530149 stage_cache_add(STAGE_RAMSTAGE, &ramstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500150
151 timestamp_add_now(TS_END_COPYRAM);
152
Arthur Heymans5331a7c2019-10-23 17:07:15 +0200153 /* This overrides the arg fetched from the relocatable module */
154 prog_set_arg(&ramstage, cbmem_top());
155
Aaron Durbin899d13d2015-05-15 23:39:23 -0500156 prog_run(&ramstage);
157
158fail:
Keith Short70064582019-05-06 16:12:57 -0600159 die_with_post_code(POST_INVALID_ROM, "Ramstage was not loaded!\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500160}
161
Subrata Banik42c44c22019-05-15 20:27:04 +0530162#if ENV_PAYLOAD_LOADER // gc-sections should take care of this
Stefan Reinauereec2db42015-06-18 01:19:50 -0700163
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500164static struct prog global_payload =
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600165 PROG_INIT(PROG_PAYLOAD, CONFIG_CBFS_PREFIX "/payload");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500166
Aaron Durbin64031672018-04-21 14:45:32 -0600167void __weak mirror_payload(struct prog *payload)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500168{
Aaron Durbin899d13d2015-05-15 23:39:23 -0500169}
170
171void payload_load(void)
172{
173 struct prog *payload = &global_payload;
174
175 timestamp_add_now(TS_LOAD_PAYLOAD);
176
177 if (prog_locate(payload))
178 goto out;
179
180 mirror_payload(payload);
181
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200182 switch (prog_cbfs_type(payload)) {
183 case CBFS_TYPE_SELF: /* Simple ELF */
Ting Shen05532262019-01-28 17:22:22 +0800184 selfload_check(payload, BM_MEM_RAM);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200185 break;
186 case CBFS_TYPE_FIT: /* Flattened image tree */
Julius Wernercd49cce2019-03-05 16:53:33 -0800187 if (CONFIG(PAYLOAD_FIT_SUPPORT)) {
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200188 fit_payload(payload);
189 break;
190 } /* else fall-through */
191 default:
Keith Short70064582019-05-06 16:12:57 -0600192 die_with_post_code(POST_INVALID_ROM,
193 "Unsupported payload type.\n");
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200194 break;
195 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500196
197out:
198 if (prog_entry(payload) == NULL)
Keith Short70064582019-05-06 16:12:57 -0600199 die_with_post_code(POST_INVALID_ROM, "Payload not loaded.\n");
Aaron Durbin899d13d2015-05-15 23:39:23 -0500200}
201
202void payload_run(void)
203{
204 struct prog *payload = &global_payload;
205
206 /* Reset to booting from this image as late as possible */
207 boot_successful();
208
209 printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n",
210 prog_entry(payload), prog_entry_arg(payload));
211
212 post_code(POST_ENTER_ELF_BOOT);
213
214 timestamp_add_now(TS_SELFBOOT_JUMP);
215
216 /* Before we go off to run the payload, see if
217 * we stayed within our bounds.
218 */
219 checkstack(_estack, 0);
220
221 prog_run(payload);
222}
Stefan Reinauereec2db42015-06-18 01:19:50 -0700223
224#endif