blob: 8ef2dcde3409a99949256d00448fe43388cf766a [file] [log] [blame]
Martin Rothc7acf162020-05-28 00:44:50 -06001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include "psp_verstage.h"
4
Felix Held26935d12020-12-08 00:40:04 +01005#include <amdblocks/acpimmio.h>
Martin Rothc7acf162020-05-28 00:44:50 -06006#include <bl_uapp/bl_syscall_public.h>
7#include <boot_device.h>
Martin Rothe21698b2020-06-26 08:55:15 -06008#include <cbfs.h>
Martin Rothc7acf162020-05-28 00:44:50 -06009#include <console/console.h>
Martin Rothe21698b2020-06-26 08:55:15 -060010#include <fmap.h>
Martin Roth50cca762020-08-13 11:06:18 -060011#include <pc80/mc146818rtc.h>
Martin Roth0c12abe2020-06-26 08:40:56 -060012#include <soc/psp_transfer.h>
Martin Roth50cca762020-08-13 11:06:18 -060013#include <security/vboot/vbnv.h>
Martin Rothc7acf162020-05-28 00:44:50 -060014#include <security/vboot/misc.h>
15#include <security/vboot/symbols.h>
16#include <security/vboot/vboot_common.h>
17#include <arch/stages.h>
18#include <stdarg.h>
19#include <stdio.h>
Kangheui Won4e2f5fd2020-09-17 16:37:13 +100020#include <timestamp.h>
Martin Rothc7acf162020-05-28 00:44:50 -060021
Martin Rothc7acf162020-05-28 00:44:50 -060022extern char _bss_start, _bss_end;
23static struct mem_region_device boot_dev =
24 MEM_REGION_DEV_RO_INIT(NULL, CONFIG_ROM_SIZE);
25
26void __weak verstage_mainboard_early_init(void) {}
27void __weak verstage_mainboard_init(void) {}
28
29static void reboot_into_recovery(struct vb2_context *ctx, uint32_t subcode)
30{
31 subcode += PSP_VBOOT_ERROR_SUBCODE;
32 svc_write_postcode(subcode);
33
Martin Rothc9689e02020-08-20 17:25:37 -060034 /*
35 * If there's an error but the PSP_verstage is already booting to RO,
36 * don't reset the system. It may be that the error is fatal, but if
37 * the system is stuck, don't intentionally force it into a reboot loop.
38 */
39 if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) {
40 printk(BIOS_ERR, "Already in recovery mode. Staying in RO.\n");
41 return;
42 }
43
Martin Rothc7acf162020-05-28 00:44:50 -060044 vb2api_fail(ctx, VB2_RECOVERY_RO_UNSPECIFIED, (int)subcode);
45 vboot_save_data(ctx);
46
47 svc_debug_print("Rebooting into recovery\n");
48 vboot_reboot();
49}
50
Martin Roth50cca762020-08-13 11:06:18 -060051static uint32_t check_cmos_recovery(void)
52{
53 /* Only reset if cmos is valid */
54 if (vbnv_cmos_failed())
55 return 0;
56
57 /* If the byte is set, clear it, then return error to reboot */
58 if (cmos_read(CMOS_RECOVERY_BYTE) == CMOS_RECOVERY_MAGIC_VAL) {
59 cmos_write(0x00, CMOS_RECOVERY_BYTE);
60 printk(BIOS_DEBUG, "Reboot into recovery requested by coreboot\n");
61 return POSTCODE_CMOS_RECOVERY;
62 }
63
64 return 0;
65}
66
Martin Rothe21698b2020-06-26 08:55:15 -060067static uintptr_t locate_amdfw(const char *name, struct region_device *rdev)
68{
69 struct cbfsf fh;
70 uint32_t type = CBFS_TYPE_RAW;
71
72 if (cbfs_locate(&fh, rdev, name, &type))
73 return 0;
74
75 cbfs_file_data(rdev, &fh);
76
77 return (uintptr_t)rdev_mmap_full(rdev);
78}
79
Martin Rothc7acf162020-05-28 00:44:50 -060080/*
81 * Tell the PSP where to load the rest of the firmware from
82 */
83static uint32_t update_boot_region(struct vb2_context *ctx)
84{
85 struct psp_ef_table *ef_table;
86 uint32_t psp_dir_addr, bios_dir_addr;
87 uint32_t *psp_dir_in_spi, *bios_dir_in_spi;
Martin Rothe21698b2020-06-26 08:55:15 -060088 const char *rname, *fname;
89 struct region_device rdev;
90 uintptr_t amdfw_location;
Martin Rothc7acf162020-05-28 00:44:50 -060091
92 /* Continue booting from RO */
93 if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) {
94 printk(BIOS_ERR, "In recovery mode. Staying in RO.\n");
95 return 0;
96 }
97
98 if (vboot_is_firmware_slot_a(ctx)) {
Martin Rothe21698b2020-06-26 08:55:15 -060099 rname = "FW_MAIN_A";
100 fname = "apu/amdfw_a";
Martin Rothc7acf162020-05-28 00:44:50 -0600101 } else {
Martin Rothe21698b2020-06-26 08:55:15 -0600102 rname = "FW_MAIN_B";
103 fname = "apu/amdfw_b";
Martin Rothc7acf162020-05-28 00:44:50 -0600104 }
105
Martin Rothe21698b2020-06-26 08:55:15 -0600106 if (fmap_locate_area_as_rdev(rname, &rdev)) {
107 printk(BIOS_ERR, "Error: Could not locate fmap region %s.\n", rname);
108 return POSTCODE_FMAP_REGION_MISSING;
109 }
110
111 amdfw_location = locate_amdfw(fname, &rdev);
112 if (!amdfw_location) {
113 printk(BIOS_ERR, "Error: AMD Firmware table not found.\n");
114 return POSTCODE_AMD_FW_MISSING;
115 }
116 ef_table = (struct psp_ef_table *)amdfw_location;
Martin Rothc7acf162020-05-28 00:44:50 -0600117 if (ef_table->signature != EMBEDDED_FW_SIGNATURE) {
118 printk(BIOS_ERR, "Error: ROMSIG address is not correct.\n");
119 return POSTCODE_ROMSIG_MISMATCH_ERROR;
120 }
121
122 psp_dir_addr = ef_table->psp_table;
123 bios_dir_addr = ef_table->bios1_entry;
124 psp_dir_in_spi = (uint32_t *)((psp_dir_addr & SPI_ADDR_MASK) +
125 (uint32_t)boot_dev.base);
126 bios_dir_in_spi = (uint32_t *)((bios_dir_addr & SPI_ADDR_MASK) +
127 (uint32_t)boot_dev.base);
128 if (*psp_dir_in_spi != PSP_COOKIE) {
129 printk(BIOS_ERR, "Error: PSP Directory address is not correct.\n");
130 return POSTCODE_PSP_COOKIE_MISMATCH_ERROR;
131 }
132 if (*bios_dir_in_spi != BDT1_COOKIE) {
133 printk(BIOS_ERR, "Error: BIOS Directory address is not correct.\n");
134 return POSTCODE_BDT1_COOKIE_MISMATCH_ERROR;
135 }
136
137 if (svc_update_psp_bios_dir((void *)&psp_dir_addr,
138 (void *)&bios_dir_addr, DIR_OFFSET_SET)) {
139 printk(BIOS_ERR, "Error: Updated BIOS Directory could not be set.\n");
140 return POSTCODE_UPDATE_PSP_BIOS_DIR_ERROR;
141 }
142
143 return 0;
144}
145
146/*
147 * Save workbuf (and soon memory console and timestamps) to the bootloader to pass
148 * back to coreboot.
149 */
150static uint32_t save_buffers(struct vb2_context **ctx)
151{
152 uint32_t retval;
Martin Roth0c12abe2020-06-26 08:40:56 -0600153 uint32_t buffer_size = MIN_TRANSFER_BUFFER_SIZE;
Martin Rothc7acf162020-05-28 00:44:50 -0600154 uint32_t max_buffer_size;
Martin Roth0c12abe2020-06-26 08:40:56 -0600155 struct transfer_info_struct buffer_info = {0};
Martin Rothc7acf162020-05-28 00:44:50 -0600156
157 /*
158 * This should never fail, but if it does, we should still try to
159 * save the buffer. If that fails, then we should go to recovery mode.
160 */
161 if (svc_get_max_workbuf_size(&max_buffer_size)) {
162 post_code(POSTCODE_DEFAULT_BUFFER_SIZE_NOTICE);
163 printk(BIOS_NOTICE, "Notice: using default transfer buffer size.\n");
Martin Roth0c12abe2020-06-26 08:40:56 -0600164 max_buffer_size = MIN_TRANSFER_BUFFER_SIZE;
Martin Rothc7acf162020-05-28 00:44:50 -0600165 }
166 printk(BIOS_DEBUG, "\nMaximum buffer size: %d bytes\n", max_buffer_size);
167
Martin Roth0c12abe2020-06-26 08:40:56 -0600168 /* Shrink workbuf if MP2 is in use and cannot be used to save buffer */
169 if (max_buffer_size < VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE) {
170 retval = vb2api_relocate(_vboot2_work, _vboot2_work, MIN_WORKBUF_TRANSFER_SIZE,
171 ctx);
172 if (retval != VB2_SUCCESS) {
173 printk(BIOS_ERR, "Error shrinking workbuf. Error code %#x\n", retval);
174 buffer_size = VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE;
175 post_code(POSTCODE_WORKBUF_RESIZE_WARNING);
176 }
177 } else {
178 buffer_size =
179 (uint32_t)((uintptr_t)_etransfer_buffer - (uintptr_t)_transfer_buffer);
180
181 buffer_info.console_offset = (uint32_t)((uintptr_t)_preram_cbmem_console -
182 (uintptr_t)_transfer_buffer);
183 buffer_info.timestamp_offset = (uint32_t)((uintptr_t)_timestamp -
184 (uintptr_t)_transfer_buffer);
185 buffer_info.fmap_offset = (uint32_t)((uintptr_t)_fmap_cache -
186 (uintptr_t)_transfer_buffer);
Martin Rothc7acf162020-05-28 00:44:50 -0600187 }
188
189 if (buffer_size > max_buffer_size) {
Martin Roth0c12abe2020-06-26 08:40:56 -0600190 printk(BIOS_ERR, "Error: Buffer is larger than max buffer size.\n");
Martin Rothc7acf162020-05-28 00:44:50 -0600191 post_code(POSTCODE_WORKBUF_BUFFER_SIZE_ERROR);
192 return POSTCODE_WORKBUF_BUFFER_SIZE_ERROR;
193 }
194
Martin Roth0c12abe2020-06-26 08:40:56 -0600195 buffer_info.magic_val = TRANSFER_MAGIC_VAL;
196 buffer_info.struct_bytes = sizeof(buffer_info);
197 buffer_info.buffer_size = buffer_size;
198 buffer_info.workbuf_offset = (uint32_t)((uintptr_t)_fmap_cache -
199 (uintptr_t)_vboot2_work);
200
Kangheui Won5f027fa2020-08-25 18:12:19 +1000201 memcpy(_transfer_buffer, &buffer_info, sizeof(buffer_info));
202
Martin Roth0c12abe2020-06-26 08:40:56 -0600203 retval = svc_save_uapp_data(UAPP_COPYBUF_CHROME_WORKBUF, (void *)_transfer_buffer,
204 buffer_size);
Martin Rothc7acf162020-05-28 00:44:50 -0600205 if (retval) {
Martin Roth0c12abe2020-06-26 08:40:56 -0600206 printk(BIOS_ERR, "Error: Could not save workbuf. Error code 0x%08x\n", retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600207 return POSTCODE_WORKBUF_SAVE_ERROR;
208 }
209
210 return 0;
211}
212
213void Main(void)
214{
215 uint32_t retval;
216 struct vb2_context *ctx = NULL;
217
218 /*
219 * Do not use printk() before console_init()
220 * Do not use post_code() before verstage_mainboard_init()
221 */
Kangheui Won4e2f5fd2020-09-17 16:37:13 +1000222 timestamp_init(timestamp_get());
Martin Rothc7acf162020-05-28 00:44:50 -0600223 svc_write_postcode(POSTCODE_ENTERED_PSP_VERSTAGE);
224 svc_debug_print("Entering verstage on PSP\n");
225 memset(&_bss_start, '\0', &_bss_end - &_bss_start);
226
227 svc_write_postcode(POSTCODE_CONSOLE_INIT);
228 console_init();
229
230 svc_write_postcode(POSTCODE_EARLY_INIT);
231 retval = verstage_soc_early_init();
232 if (retval) {
233 svc_debug_print("verstage_soc_early_init failed\n");
234 reboot_into_recovery(NULL, retval);
235 }
236 svc_debug_print("calling verstage_mainboard_early_init\n");
237
238 verstage_mainboard_early_init();
239
240 svc_write_postcode(POSTCODE_LATE_INIT);
Felix Held26935d12020-12-08 00:40:04 +0100241 fch_io_enable_legacy_io();
Martin Rothc7acf162020-05-28 00:44:50 -0600242 verstage_soc_init();
243 verstage_mainboard_init();
244
245 post_code(POSTCODE_VERSTAGE_MAIN);
246
247 verstage_main();
248
Martin Rothc9689e02020-08-20 17:25:37 -0600249 ctx = vboot_get_context();
Martin Roth50cca762020-08-13 11:06:18 -0600250 retval = check_cmos_recovery();
251 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600252 reboot_into_recovery(ctx, retval);
Martin Roth0c12abe2020-06-26 08:40:56 -0600253
Martin Rothc7acf162020-05-28 00:44:50 -0600254 post_code(POSTCODE_SAVE_BUFFERS);
255 retval = save_buffers(&ctx);
256 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600257 reboot_into_recovery(ctx, retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600258
259 post_code(POSTCODE_UPDATE_BOOT_REGION);
260 retval = update_boot_region(ctx);
261 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600262 reboot_into_recovery(ctx, retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600263
264 post_code(POSTCODE_UNMAP_SPI_ROM);
265 if (boot_dev.base) {
266 if (svc_unmap_spi_rom((void *)boot_dev.base))
267 printk(BIOS_ERR, "Error unmapping SPI rom\n");
268 }
269
270 post_code(POSTCODE_UNMAP_FCH_DEVICES);
271 unmap_fch_devices();
272
273 post_code(POSTCODE_LEAVING_VERSTAGE);
274
275 printk(BIOS_DEBUG, "Leaving verstage on PSP\n");
276 svc_exit(retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600277}
278
279const struct region_device *boot_device_ro(void)
280{
281 uintptr_t *addr;
282
283 addr = map_spi_rom();
284 mem_region_device_ro_init(&boot_dev, (void *)addr, CONFIG_ROM_SIZE);
285
286 return &boot_dev.rdev;
287}
288
289/*
290 * The stage_entry function is not used directly, but stage_entry() is marked as an entry
291 * point in arm/arch/header.h, so if stage_entry() isn't present and calling Main(), all
292 * the verstage code gets dropped by the linker. Slightly hacky, but mostly harmless.
293 */
294void stage_entry(uintptr_t stage_arg)
295{
296 Main();
297}