blob: 58f17e14f17b1d4e9eea0151c46aee363900777d [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 Rothc7acf162020-05-28 00:44:50 -060067/*
68 * Tell the PSP where to load the rest of the firmware from
69 */
70static uint32_t update_boot_region(struct vb2_context *ctx)
71{
72 struct psp_ef_table *ef_table;
73 uint32_t psp_dir_addr, bios_dir_addr;
74 uint32_t *psp_dir_in_spi, *bios_dir_in_spi;
Kangheui Wonac7ec272021-01-15 15:04:25 +110075 const char *fname;
76 void *amdfw_location;
Martin Rothc7acf162020-05-28 00:44:50 -060077
78 /* Continue booting from RO */
79 if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) {
80 printk(BIOS_ERR, "In recovery mode. Staying in RO.\n");
81 return 0;
82 }
83
84 if (vboot_is_firmware_slot_a(ctx)) {
Martin Rothe21698b2020-06-26 08:55:15 -060085 fname = "apu/amdfw_a";
Martin Rothc7acf162020-05-28 00:44:50 -060086 } else {
Martin Rothe21698b2020-06-26 08:55:15 -060087 fname = "apu/amdfw_b";
Martin Rothc7acf162020-05-28 00:44:50 -060088 }
89
Kangheui Wonac7ec272021-01-15 15:04:25 +110090 amdfw_location = cbfs_map(fname, NULL);
Martin Rothe21698b2020-06-26 08:55:15 -060091 if (!amdfw_location) {
92 printk(BIOS_ERR, "Error: AMD Firmware table not found.\n");
93 return POSTCODE_AMD_FW_MISSING;
94 }
95 ef_table = (struct psp_ef_table *)amdfw_location;
Martin Rothc7acf162020-05-28 00:44:50 -060096 if (ef_table->signature != EMBEDDED_FW_SIGNATURE) {
97 printk(BIOS_ERR, "Error: ROMSIG address is not correct.\n");
98 return POSTCODE_ROMSIG_MISMATCH_ERROR;
99 }
100
101 psp_dir_addr = ef_table->psp_table;
102 bios_dir_addr = ef_table->bios1_entry;
103 psp_dir_in_spi = (uint32_t *)((psp_dir_addr & SPI_ADDR_MASK) +
104 (uint32_t)boot_dev.base);
105 bios_dir_in_spi = (uint32_t *)((bios_dir_addr & SPI_ADDR_MASK) +
106 (uint32_t)boot_dev.base);
107 if (*psp_dir_in_spi != PSP_COOKIE) {
108 printk(BIOS_ERR, "Error: PSP Directory address is not correct.\n");
109 return POSTCODE_PSP_COOKIE_MISMATCH_ERROR;
110 }
111 if (*bios_dir_in_spi != BDT1_COOKIE) {
112 printk(BIOS_ERR, "Error: BIOS Directory address is not correct.\n");
113 return POSTCODE_BDT1_COOKIE_MISMATCH_ERROR;
114 }
115
Kangheui Wona767eb42021-04-14 09:35:28 +1000116 if (update_psp_bios_dir((void *)&psp_dir_addr, (void *)&bios_dir_addr)) {
Martin Rothc7acf162020-05-28 00:44:50 -0600117 printk(BIOS_ERR, "Error: Updated BIOS Directory could not be set.\n");
118 return POSTCODE_UPDATE_PSP_BIOS_DIR_ERROR;
119 }
120
121 return 0;
122}
123
124/*
125 * Save workbuf (and soon memory console and timestamps) to the bootloader to pass
126 * back to coreboot.
127 */
128static uint32_t save_buffers(struct vb2_context **ctx)
129{
130 uint32_t retval;
Martin Roth0c12abe2020-06-26 08:40:56 -0600131 uint32_t buffer_size = MIN_TRANSFER_BUFFER_SIZE;
Martin Rothc7acf162020-05-28 00:44:50 -0600132 uint32_t max_buffer_size;
Martin Roth0c12abe2020-06-26 08:40:56 -0600133 struct transfer_info_struct buffer_info = {0};
Martin Rothc7acf162020-05-28 00:44:50 -0600134
135 /*
136 * This should never fail, but if it does, we should still try to
137 * save the buffer. If that fails, then we should go to recovery mode.
138 */
139 if (svc_get_max_workbuf_size(&max_buffer_size)) {
140 post_code(POSTCODE_DEFAULT_BUFFER_SIZE_NOTICE);
141 printk(BIOS_NOTICE, "Notice: using default transfer buffer size.\n");
Martin Roth0c12abe2020-06-26 08:40:56 -0600142 max_buffer_size = MIN_TRANSFER_BUFFER_SIZE;
Martin Rothc7acf162020-05-28 00:44:50 -0600143 }
144 printk(BIOS_DEBUG, "\nMaximum buffer size: %d bytes\n", max_buffer_size);
145
Martin Roth0c12abe2020-06-26 08:40:56 -0600146 /* Shrink workbuf if MP2 is in use and cannot be used to save buffer */
147 if (max_buffer_size < VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE) {
148 retval = vb2api_relocate(_vboot2_work, _vboot2_work, MIN_WORKBUF_TRANSFER_SIZE,
149 ctx);
150 if (retval != VB2_SUCCESS) {
151 printk(BIOS_ERR, "Error shrinking workbuf. Error code %#x\n", retval);
152 buffer_size = VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE;
153 post_code(POSTCODE_WORKBUF_RESIZE_WARNING);
154 }
155 } else {
156 buffer_size =
157 (uint32_t)((uintptr_t)_etransfer_buffer - (uintptr_t)_transfer_buffer);
158
159 buffer_info.console_offset = (uint32_t)((uintptr_t)_preram_cbmem_console -
160 (uintptr_t)_transfer_buffer);
161 buffer_info.timestamp_offset = (uint32_t)((uintptr_t)_timestamp -
162 (uintptr_t)_transfer_buffer);
163 buffer_info.fmap_offset = (uint32_t)((uintptr_t)_fmap_cache -
164 (uintptr_t)_transfer_buffer);
Martin Rothc7acf162020-05-28 00:44:50 -0600165 }
166
167 if (buffer_size > max_buffer_size) {
Martin Roth0c12abe2020-06-26 08:40:56 -0600168 printk(BIOS_ERR, "Error: Buffer is larger than max buffer size.\n");
Martin Rothc7acf162020-05-28 00:44:50 -0600169 post_code(POSTCODE_WORKBUF_BUFFER_SIZE_ERROR);
170 return POSTCODE_WORKBUF_BUFFER_SIZE_ERROR;
171 }
172
Martin Roth0c12abe2020-06-26 08:40:56 -0600173 buffer_info.magic_val = TRANSFER_MAGIC_VAL;
174 buffer_info.struct_bytes = sizeof(buffer_info);
175 buffer_info.buffer_size = buffer_size;
176 buffer_info.workbuf_offset = (uint32_t)((uintptr_t)_fmap_cache -
177 (uintptr_t)_vboot2_work);
178
Kangheui Won5f027fa2020-08-25 18:12:19 +1000179 memcpy(_transfer_buffer, &buffer_info, sizeof(buffer_info));
180
Kangheui Wona767eb42021-04-14 09:35:28 +1000181 retval = save_uapp_data((void *)_transfer_buffer, buffer_size);
Martin Rothc7acf162020-05-28 00:44:50 -0600182 if (retval) {
Martin Roth0c12abe2020-06-26 08:40:56 -0600183 printk(BIOS_ERR, "Error: Could not save workbuf. Error code 0x%08x\n", retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600184 return POSTCODE_WORKBUF_SAVE_ERROR;
185 }
186
187 return 0;
188}
189
190void Main(void)
191{
192 uint32_t retval;
193 struct vb2_context *ctx = NULL;
194
195 /*
196 * Do not use printk() before console_init()
197 * Do not use post_code() before verstage_mainboard_init()
198 */
Kangheui Won4e2f5fd2020-09-17 16:37:13 +1000199 timestamp_init(timestamp_get());
Martin Rothc7acf162020-05-28 00:44:50 -0600200 svc_write_postcode(POSTCODE_ENTERED_PSP_VERSTAGE);
201 svc_debug_print("Entering verstage on PSP\n");
202 memset(&_bss_start, '\0', &_bss_end - &_bss_start);
203
204 svc_write_postcode(POSTCODE_CONSOLE_INIT);
205 console_init();
206
207 svc_write_postcode(POSTCODE_EARLY_INIT);
208 retval = verstage_soc_early_init();
209 if (retval) {
210 svc_debug_print("verstage_soc_early_init failed\n");
211 reboot_into_recovery(NULL, retval);
212 }
213 svc_debug_print("calling verstage_mainboard_early_init\n");
214
215 verstage_mainboard_early_init();
216
217 svc_write_postcode(POSTCODE_LATE_INIT);
Felix Held26935d12020-12-08 00:40:04 +0100218 fch_io_enable_legacy_io();
Martin Rothc7acf162020-05-28 00:44:50 -0600219 verstage_soc_init();
220 verstage_mainboard_init();
221
222 post_code(POSTCODE_VERSTAGE_MAIN);
223
Kangheui Wonac7ec272021-01-15 15:04:25 +1100224 vboot_run_logic();
Martin Rothc7acf162020-05-28 00:44:50 -0600225
Martin Rothc9689e02020-08-20 17:25:37 -0600226 ctx = vboot_get_context();
Martin Roth50cca762020-08-13 11:06:18 -0600227 retval = check_cmos_recovery();
228 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600229 reboot_into_recovery(ctx, retval);
Martin Roth0c12abe2020-06-26 08:40:56 -0600230
Kangheui Wonac7ec272021-01-15 15:04:25 +1100231 post_code(POSTCODE_UPDATE_BOOT_REGION);
232 retval = update_boot_region(ctx);
233 if (retval)
234 reboot_into_recovery(ctx, retval);
235
Martin Rothc7acf162020-05-28 00:44:50 -0600236 post_code(POSTCODE_SAVE_BUFFERS);
237 retval = save_buffers(&ctx);
238 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600239 reboot_into_recovery(ctx, retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600240
Martin Rothc7acf162020-05-28 00:44:50 -0600241
242 post_code(POSTCODE_UNMAP_SPI_ROM);
243 if (boot_dev.base) {
244 if (svc_unmap_spi_rom((void *)boot_dev.base))
245 printk(BIOS_ERR, "Error unmapping SPI rom\n");
246 }
247
248 post_code(POSTCODE_UNMAP_FCH_DEVICES);
249 unmap_fch_devices();
250
251 post_code(POSTCODE_LEAVING_VERSTAGE);
252
253 printk(BIOS_DEBUG, "Leaving verstage on PSP\n");
254 svc_exit(retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600255}
256
257const struct region_device *boot_device_ro(void)
258{
259 uintptr_t *addr;
260
261 addr = map_spi_rom();
262 mem_region_device_ro_init(&boot_dev, (void *)addr, CONFIG_ROM_SIZE);
263
264 return &boot_dev.rdev;
265}
266
267/*
268 * The stage_entry function is not used directly, but stage_entry() is marked as an entry
269 * point in arm/arch/header.h, so if stage_entry() isn't present and calling Main(), all
270 * the verstage code gets dropped by the linker. Slightly hacky, but mostly harmless.
271 */
272void stage_entry(uintptr_t stage_arg)
273{
274 Main();
275}