blob: d4c5d155af86b002a5d3f578f0822b29ee17a62e [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
116 if (svc_update_psp_bios_dir((void *)&psp_dir_addr,
117 (void *)&bios_dir_addr, DIR_OFFSET_SET)) {
118 printk(BIOS_ERR, "Error: Updated BIOS Directory could not be set.\n");
119 return POSTCODE_UPDATE_PSP_BIOS_DIR_ERROR;
120 }
121
122 return 0;
123}
124
125/*
126 * Save workbuf (and soon memory console and timestamps) to the bootloader to pass
127 * back to coreboot.
128 */
129static uint32_t save_buffers(struct vb2_context **ctx)
130{
131 uint32_t retval;
Martin Roth0c12abe2020-06-26 08:40:56 -0600132 uint32_t buffer_size = MIN_TRANSFER_BUFFER_SIZE;
Martin Rothc7acf162020-05-28 00:44:50 -0600133 uint32_t max_buffer_size;
Martin Roth0c12abe2020-06-26 08:40:56 -0600134 struct transfer_info_struct buffer_info = {0};
Martin Rothc7acf162020-05-28 00:44:50 -0600135
136 /*
137 * This should never fail, but if it does, we should still try to
138 * save the buffer. If that fails, then we should go to recovery mode.
139 */
140 if (svc_get_max_workbuf_size(&max_buffer_size)) {
141 post_code(POSTCODE_DEFAULT_BUFFER_SIZE_NOTICE);
142 printk(BIOS_NOTICE, "Notice: using default transfer buffer size.\n");
Martin Roth0c12abe2020-06-26 08:40:56 -0600143 max_buffer_size = MIN_TRANSFER_BUFFER_SIZE;
Martin Rothc7acf162020-05-28 00:44:50 -0600144 }
145 printk(BIOS_DEBUG, "\nMaximum buffer size: %d bytes\n", max_buffer_size);
146
Martin Roth0c12abe2020-06-26 08:40:56 -0600147 /* Shrink workbuf if MP2 is in use and cannot be used to save buffer */
148 if (max_buffer_size < VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE) {
149 retval = vb2api_relocate(_vboot2_work, _vboot2_work, MIN_WORKBUF_TRANSFER_SIZE,
150 ctx);
151 if (retval != VB2_SUCCESS) {
152 printk(BIOS_ERR, "Error shrinking workbuf. Error code %#x\n", retval);
153 buffer_size = VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE;
154 post_code(POSTCODE_WORKBUF_RESIZE_WARNING);
155 }
156 } else {
157 buffer_size =
158 (uint32_t)((uintptr_t)_etransfer_buffer - (uintptr_t)_transfer_buffer);
159
160 buffer_info.console_offset = (uint32_t)((uintptr_t)_preram_cbmem_console -
161 (uintptr_t)_transfer_buffer);
162 buffer_info.timestamp_offset = (uint32_t)((uintptr_t)_timestamp -
163 (uintptr_t)_transfer_buffer);
164 buffer_info.fmap_offset = (uint32_t)((uintptr_t)_fmap_cache -
165 (uintptr_t)_transfer_buffer);
Martin Rothc7acf162020-05-28 00:44:50 -0600166 }
167
168 if (buffer_size > max_buffer_size) {
Martin Roth0c12abe2020-06-26 08:40:56 -0600169 printk(BIOS_ERR, "Error: Buffer is larger than max buffer size.\n");
Martin Rothc7acf162020-05-28 00:44:50 -0600170 post_code(POSTCODE_WORKBUF_BUFFER_SIZE_ERROR);
171 return POSTCODE_WORKBUF_BUFFER_SIZE_ERROR;
172 }
173
Martin Roth0c12abe2020-06-26 08:40:56 -0600174 buffer_info.magic_val = TRANSFER_MAGIC_VAL;
175 buffer_info.struct_bytes = sizeof(buffer_info);
176 buffer_info.buffer_size = buffer_size;
177 buffer_info.workbuf_offset = (uint32_t)((uintptr_t)_fmap_cache -
178 (uintptr_t)_vboot2_work);
179
Kangheui Won5f027fa2020-08-25 18:12:19 +1000180 memcpy(_transfer_buffer, &buffer_info, sizeof(buffer_info));
181
Martin Roth0c12abe2020-06-26 08:40:56 -0600182 retval = svc_save_uapp_data(UAPP_COPYBUF_CHROME_WORKBUF, (void *)_transfer_buffer,
183 buffer_size);
Martin Rothc7acf162020-05-28 00:44:50 -0600184 if (retval) {
Martin Roth0c12abe2020-06-26 08:40:56 -0600185 printk(BIOS_ERR, "Error: Could not save workbuf. Error code 0x%08x\n", retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600186 return POSTCODE_WORKBUF_SAVE_ERROR;
187 }
188
189 return 0;
190}
191
192void Main(void)
193{
194 uint32_t retval;
195 struct vb2_context *ctx = NULL;
196
197 /*
198 * Do not use printk() before console_init()
199 * Do not use post_code() before verstage_mainboard_init()
200 */
Kangheui Won4e2f5fd2020-09-17 16:37:13 +1000201 timestamp_init(timestamp_get());
Martin Rothc7acf162020-05-28 00:44:50 -0600202 svc_write_postcode(POSTCODE_ENTERED_PSP_VERSTAGE);
203 svc_debug_print("Entering verstage on PSP\n");
204 memset(&_bss_start, '\0', &_bss_end - &_bss_start);
205
206 svc_write_postcode(POSTCODE_CONSOLE_INIT);
207 console_init();
208
209 svc_write_postcode(POSTCODE_EARLY_INIT);
210 retval = verstage_soc_early_init();
211 if (retval) {
212 svc_debug_print("verstage_soc_early_init failed\n");
213 reboot_into_recovery(NULL, retval);
214 }
215 svc_debug_print("calling verstage_mainboard_early_init\n");
216
217 verstage_mainboard_early_init();
218
219 svc_write_postcode(POSTCODE_LATE_INIT);
Felix Held26935d12020-12-08 00:40:04 +0100220 fch_io_enable_legacy_io();
Martin Rothc7acf162020-05-28 00:44:50 -0600221 verstage_soc_init();
222 verstage_mainboard_init();
223
224 post_code(POSTCODE_VERSTAGE_MAIN);
225
Kangheui Wonac7ec272021-01-15 15:04:25 +1100226 vboot_run_logic();
Martin Rothc7acf162020-05-28 00:44:50 -0600227
Martin Rothc9689e02020-08-20 17:25:37 -0600228 ctx = vboot_get_context();
Martin Roth50cca762020-08-13 11:06:18 -0600229 retval = check_cmos_recovery();
230 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600231 reboot_into_recovery(ctx, retval);
Martin Roth0c12abe2020-06-26 08:40:56 -0600232
Kangheui Wonac7ec272021-01-15 15:04:25 +1100233 post_code(POSTCODE_UPDATE_BOOT_REGION);
234 retval = update_boot_region(ctx);
235 if (retval)
236 reboot_into_recovery(ctx, retval);
237
Martin Rothc7acf162020-05-28 00:44:50 -0600238 post_code(POSTCODE_SAVE_BUFFERS);
239 retval = save_buffers(&ctx);
240 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600241 reboot_into_recovery(ctx, retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600242
Martin Rothc7acf162020-05-28 00:44:50 -0600243
244 post_code(POSTCODE_UNMAP_SPI_ROM);
245 if (boot_dev.base) {
246 if (svc_unmap_spi_rom((void *)boot_dev.base))
247 printk(BIOS_ERR, "Error unmapping SPI rom\n");
248 }
249
250 post_code(POSTCODE_UNMAP_FCH_DEVICES);
251 unmap_fch_devices();
252
253 post_code(POSTCODE_LEAVING_VERSTAGE);
254
255 printk(BIOS_DEBUG, "Leaving verstage on PSP\n");
256 svc_exit(retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600257}
258
259const struct region_device *boot_device_ro(void)
260{
261 uintptr_t *addr;
262
263 addr = map_spi_rom();
264 mem_region_device_ro_init(&boot_dev, (void *)addr, CONFIG_ROM_SIZE);
265
266 return &boot_dev.rdev;
267}
268
269/*
270 * The stage_entry function is not used directly, but stage_entry() is marked as an entry
271 * point in arm/arch/header.h, so if stage_entry() isn't present and calling Main(), all
272 * the verstage code gets dropped by the linker. Slightly hacky, but mostly harmless.
273 */
274void stage_entry(uintptr_t stage_arg)
275{
276 Main();
277}