blob: c03cf9b22e86296b6ebda9401d6fd59c7b265679 [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>
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -06009#include <commonlib/region.h>
Martin Rothc7acf162020-05-28 00:44:50 -060010#include <console/console.h>
Martin Rothe21698b2020-06-26 08:55:15 -060011#include <fmap.h>
Martin Roth50cca762020-08-13 11:06:18 -060012#include <pc80/mc146818rtc.h>
Martin Roth0c12abe2020-06-26 08:40:56 -060013#include <soc/psp_transfer.h>
Martin Roth50cca762020-08-13 11:06:18 -060014#include <security/vboot/vbnv.h>
Martin Rothc7acf162020-05-28 00:44:50 -060015#include <security/vboot/misc.h>
16#include <security/vboot/symbols.h>
17#include <security/vboot/vboot_common.h>
18#include <arch/stages.h>
19#include <stdarg.h>
20#include <stdio.h>
Kangheui Won4e2f5fd2020-09-17 16:37:13 +100021#include <timestamp.h>
Martin Rothc7acf162020-05-28 00:44:50 -060022
Martin Rothc7acf162020-05-28 00:44:50 -060023extern char _bss_start, _bss_end;
Martin Rothc7acf162020-05-28 00:44:50 -060024
25void __weak verstage_mainboard_early_init(void) {}
26void __weak verstage_mainboard_init(void) {}
Kangheui Won695732b2021-04-25 12:11:17 +100027uint32_t __weak get_max_workbuf_size(uint32_t *size)
28{
29 /* This svc only exists in picasso and deprecated for later platforms.
30 * Provide sane default function here for those platforms.
31 */
32 *size = (uint32_t)((uintptr_t)_etransfer_buffer - (uintptr_t)_transfer_buffer);
33 return 0;
34}
Martin Rothc7acf162020-05-28 00:44:50 -060035
36static void reboot_into_recovery(struct vb2_context *ctx, uint32_t subcode)
37{
38 subcode += PSP_VBOOT_ERROR_SUBCODE;
39 svc_write_postcode(subcode);
40
Martin Rothc9689e02020-08-20 17:25:37 -060041 /*
42 * If there's an error but the PSP_verstage is already booting to RO,
43 * don't reset the system. It may be that the error is fatal, but if
44 * the system is stuck, don't intentionally force it into a reboot loop.
45 */
46 if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) {
47 printk(BIOS_ERR, "Already in recovery mode. Staying in RO.\n");
48 return;
49 }
50
Martin Rothc7acf162020-05-28 00:44:50 -060051 vb2api_fail(ctx, VB2_RECOVERY_RO_UNSPECIFIED, (int)subcode);
52 vboot_save_data(ctx);
53
54 svc_debug_print("Rebooting into recovery\n");
55 vboot_reboot();
56}
57
Martin Roth50cca762020-08-13 11:06:18 -060058static uint32_t check_cmos_recovery(void)
59{
60 /* Only reset if cmos is valid */
61 if (vbnv_cmos_failed())
62 return 0;
63
64 /* If the byte is set, clear it, then return error to reboot */
65 if (cmos_read(CMOS_RECOVERY_BYTE) == CMOS_RECOVERY_MAGIC_VAL) {
66 cmos_write(0x00, CMOS_RECOVERY_BYTE);
67 printk(BIOS_DEBUG, "Reboot into recovery requested by coreboot\n");
68 return POSTCODE_CMOS_RECOVERY;
69 }
70
71 return 0;
72}
73
Martin Rothc7acf162020-05-28 00:44:50 -060074/*
75 * Tell the PSP where to load the rest of the firmware from
76 */
77static uint32_t update_boot_region(struct vb2_context *ctx)
78{
Kangheui Won26bb4aa2021-10-18 15:31:45 +110079 struct embedded_firmware *ef_table;
Martin Rothc7acf162020-05-28 00:44:50 -060080 uint32_t psp_dir_addr, bios_dir_addr;
81 uint32_t *psp_dir_in_spi, *bios_dir_in_spi;
Kangheui Wonac7ec272021-01-15 15:04:25 +110082 const char *fname;
83 void *amdfw_location;
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -060084 void *boot_dev_base = rdev_mmap_full(boot_device_ro());
Martin Rothc7acf162020-05-28 00:44:50 -060085
86 /* Continue booting from RO */
87 if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) {
88 printk(BIOS_ERR, "In recovery mode. Staying in RO.\n");
89 return 0;
90 }
91
92 if (vboot_is_firmware_slot_a(ctx)) {
Martin Rothe21698b2020-06-26 08:55:15 -060093 fname = "apu/amdfw_a";
Martin Rothc7acf162020-05-28 00:44:50 -060094 } else {
Martin Rothe21698b2020-06-26 08:55:15 -060095 fname = "apu/amdfw_b";
Martin Rothc7acf162020-05-28 00:44:50 -060096 }
97
Kangheui Wonac7ec272021-01-15 15:04:25 +110098 amdfw_location = cbfs_map(fname, NULL);
Martin Rothe21698b2020-06-26 08:55:15 -060099 if (!amdfw_location) {
100 printk(BIOS_ERR, "Error: AMD Firmware table not found.\n");
101 return POSTCODE_AMD_FW_MISSING;
102 }
Kangheui Won26bb4aa2021-10-18 15:31:45 +1100103 ef_table = (struct embedded_firmware *)amdfw_location;
Martin Rothc7acf162020-05-28 00:44:50 -0600104 if (ef_table->signature != EMBEDDED_FW_SIGNATURE) {
105 printk(BIOS_ERR, "Error: ROMSIG address is not correct.\n");
106 return POSTCODE_ROMSIG_MISMATCH_ERROR;
107 }
108
Kangheui Won26bb4aa2021-10-18 15:31:45 +1100109 psp_dir_addr = ef_table->combo_psp_directory;
Kangheui Won5858fb42021-05-06 13:30:51 +1000110 bios_dir_addr = get_bios_dir_addr(ef_table);
Martin Rothc7acf162020-05-28 00:44:50 -0600111 psp_dir_in_spi = (uint32_t *)((psp_dir_addr & SPI_ADDR_MASK) +
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -0600112 (uint32_t)boot_dev_base);
Martin Rothc7acf162020-05-28 00:44:50 -0600113 bios_dir_in_spi = (uint32_t *)((bios_dir_addr & SPI_ADDR_MASK) +
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -0600114 (uint32_t)boot_dev_base);
Martin Rothc7acf162020-05-28 00:44:50 -0600115 if (*psp_dir_in_spi != PSP_COOKIE) {
116 printk(BIOS_ERR, "Error: PSP Directory address is not correct.\n");
117 return POSTCODE_PSP_COOKIE_MISMATCH_ERROR;
118 }
119 if (*bios_dir_in_spi != BDT1_COOKIE) {
120 printk(BIOS_ERR, "Error: BIOS Directory address is not correct.\n");
121 return POSTCODE_BDT1_COOKIE_MISMATCH_ERROR;
122 }
123
Kangheui Won26bb4aa2021-10-18 15:31:45 +1100124 if (update_psp_bios_dir(&psp_dir_addr, &bios_dir_addr)) {
Martin Rothc7acf162020-05-28 00:44:50 -0600125 printk(BIOS_ERR, "Error: Updated BIOS Directory could not be set.\n");
126 return POSTCODE_UPDATE_PSP_BIOS_DIR_ERROR;
127 }
128
129 return 0;
130}
131
132/*
133 * Save workbuf (and soon memory console and timestamps) to the bootloader to pass
134 * back to coreboot.
135 */
136static uint32_t save_buffers(struct vb2_context **ctx)
137{
138 uint32_t retval;
Martin Roth0c12abe2020-06-26 08:40:56 -0600139 uint32_t buffer_size = MIN_TRANSFER_BUFFER_SIZE;
Martin Rothc7acf162020-05-28 00:44:50 -0600140 uint32_t max_buffer_size;
Martin Roth0c12abe2020-06-26 08:40:56 -0600141 struct transfer_info_struct buffer_info = {0};
Martin Rothc7acf162020-05-28 00:44:50 -0600142
143 /*
Kangheui Won695732b2021-04-25 12:11:17 +1000144 * This should never fail on picasso, but if it does, we should still
145 * try to save the buffer. If that fails, then we should go to
146 * recovery mode.
Martin Rothc7acf162020-05-28 00:44:50 -0600147 */
Kangheui Won695732b2021-04-25 12:11:17 +1000148 if (get_max_workbuf_size(&max_buffer_size)) {
Martin Rothc7acf162020-05-28 00:44:50 -0600149 post_code(POSTCODE_DEFAULT_BUFFER_SIZE_NOTICE);
150 printk(BIOS_NOTICE, "Notice: using default transfer buffer size.\n");
Martin Roth0c12abe2020-06-26 08:40:56 -0600151 max_buffer_size = MIN_TRANSFER_BUFFER_SIZE;
Martin Rothc7acf162020-05-28 00:44:50 -0600152 }
153 printk(BIOS_DEBUG, "\nMaximum buffer size: %d bytes\n", max_buffer_size);
154
Martin Roth0c12abe2020-06-26 08:40:56 -0600155 /* Shrink workbuf if MP2 is in use and cannot be used to save buffer */
156 if (max_buffer_size < VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE) {
157 retval = vb2api_relocate(_vboot2_work, _vboot2_work, MIN_WORKBUF_TRANSFER_SIZE,
158 ctx);
159 if (retval != VB2_SUCCESS) {
160 printk(BIOS_ERR, "Error shrinking workbuf. Error code %#x\n", retval);
161 buffer_size = VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE;
162 post_code(POSTCODE_WORKBUF_RESIZE_WARNING);
163 }
164 } else {
165 buffer_size =
166 (uint32_t)((uintptr_t)_etransfer_buffer - (uintptr_t)_transfer_buffer);
167
168 buffer_info.console_offset = (uint32_t)((uintptr_t)_preram_cbmem_console -
169 (uintptr_t)_transfer_buffer);
170 buffer_info.timestamp_offset = (uint32_t)((uintptr_t)_timestamp -
171 (uintptr_t)_transfer_buffer);
172 buffer_info.fmap_offset = (uint32_t)((uintptr_t)_fmap_cache -
173 (uintptr_t)_transfer_buffer);
Martin Rothc7acf162020-05-28 00:44:50 -0600174 }
175
176 if (buffer_size > max_buffer_size) {
Martin Roth0c12abe2020-06-26 08:40:56 -0600177 printk(BIOS_ERR, "Error: Buffer is larger than max buffer size.\n");
Martin Rothc7acf162020-05-28 00:44:50 -0600178 post_code(POSTCODE_WORKBUF_BUFFER_SIZE_ERROR);
179 return POSTCODE_WORKBUF_BUFFER_SIZE_ERROR;
180 }
181
Martin Roth0c12abe2020-06-26 08:40:56 -0600182 buffer_info.magic_val = TRANSFER_MAGIC_VAL;
183 buffer_info.struct_bytes = sizeof(buffer_info);
184 buffer_info.buffer_size = buffer_size;
185 buffer_info.workbuf_offset = (uint32_t)((uintptr_t)_fmap_cache -
186 (uintptr_t)_vboot2_work);
187
Kangheui Won5f027fa2020-08-25 18:12:19 +1000188 memcpy(_transfer_buffer, &buffer_info, sizeof(buffer_info));
189
Kangheui Wona767eb42021-04-14 09:35:28 +1000190 retval = save_uapp_data((void *)_transfer_buffer, buffer_size);
Martin Rothc7acf162020-05-28 00:44:50 -0600191 if (retval) {
Martin Roth0c12abe2020-06-26 08:40:56 -0600192 printk(BIOS_ERR, "Error: Could not save workbuf. Error code 0x%08x\n", retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600193 return POSTCODE_WORKBUF_SAVE_ERROR;
194 }
195
196 return 0;
197}
198
199void Main(void)
200{
201 uint32_t retval;
202 struct vb2_context *ctx = NULL;
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -0600203 void *boot_dev_base;
Martin Rothc7acf162020-05-28 00:44:50 -0600204
205 /*
206 * Do not use printk() before console_init()
207 * Do not use post_code() before verstage_mainboard_init()
208 */
Kangheui Won4e2f5fd2020-09-17 16:37:13 +1000209 timestamp_init(timestamp_get());
Martin Rothc7acf162020-05-28 00:44:50 -0600210 svc_write_postcode(POSTCODE_ENTERED_PSP_VERSTAGE);
211 svc_debug_print("Entering verstage on PSP\n");
212 memset(&_bss_start, '\0', &_bss_end - &_bss_start);
213
214 svc_write_postcode(POSTCODE_CONSOLE_INIT);
215 console_init();
216
217 svc_write_postcode(POSTCODE_EARLY_INIT);
218 retval = verstage_soc_early_init();
219 if (retval) {
220 svc_debug_print("verstage_soc_early_init failed\n");
221 reboot_into_recovery(NULL, retval);
222 }
223 svc_debug_print("calling verstage_mainboard_early_init\n");
224
225 verstage_mainboard_early_init();
226
227 svc_write_postcode(POSTCODE_LATE_INIT);
Felix Held26935d12020-12-08 00:40:04 +0100228 fch_io_enable_legacy_io();
Martin Rothc7acf162020-05-28 00:44:50 -0600229 verstage_soc_init();
230 verstage_mainboard_init();
231
232 post_code(POSTCODE_VERSTAGE_MAIN);
233
Kangheui Wonac7ec272021-01-15 15:04:25 +1100234 vboot_run_logic();
Martin Rothc7acf162020-05-28 00:44:50 -0600235
Martin Rothc9689e02020-08-20 17:25:37 -0600236 ctx = vboot_get_context();
Martin Roth50cca762020-08-13 11:06:18 -0600237 retval = check_cmos_recovery();
238 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600239 reboot_into_recovery(ctx, retval);
Martin Roth0c12abe2020-06-26 08:40:56 -0600240
Kangheui Wonac7ec272021-01-15 15:04:25 +1100241 post_code(POSTCODE_UPDATE_BOOT_REGION);
Kangheui Won97527252021-05-20 10:02:00 +1000242
243 /*
244 * Since psp_verstage doesn't load next stage we never call
245 * any cbfs API on RO path. However we still need to initialize
246 * RO CBFS MCACHE manually to pass it in transfer_buffer.
247 * In RW path, MCACHE build will be skipped for RO region since
248 * we already built here.
249 */
250 cbfs_get_boot_device(true);
251
Kangheui Wonac7ec272021-01-15 15:04:25 +1100252 retval = update_boot_region(ctx);
253 if (retval)
254 reboot_into_recovery(ctx, retval);
255
Martin Rothc7acf162020-05-28 00:44:50 -0600256 post_code(POSTCODE_SAVE_BUFFERS);
257 retval = save_buffers(&ctx);
258 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600259 reboot_into_recovery(ctx, retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600260
Martin Rothc7acf162020-05-28 00:44:50 -0600261
262 post_code(POSTCODE_UNMAP_SPI_ROM);
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -0600263 boot_dev_base = rdev_mmap_full(boot_device_ro());
264 if (boot_dev_base) {
265 if (svc_unmap_spi_rom((void *)boot_dev_base))
Martin Rothc7acf162020-05-28 00:44:50 -0600266 printk(BIOS_ERR, "Error unmapping SPI rom\n");
267 }
268
269 post_code(POSTCODE_UNMAP_FCH_DEVICES);
270 unmap_fch_devices();
271
272 post_code(POSTCODE_LEAVING_VERSTAGE);
273
274 printk(BIOS_DEBUG, "Leaving verstage on PSP\n");
275 svc_exit(retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600276}
277
Martin Rothc7acf162020-05-28 00:44:50 -0600278/*
279 * The stage_entry function is not used directly, but stage_entry() is marked as an entry
280 * point in arm/arch/header.h, so if stage_entry() isn't present and calling Main(), all
281 * the verstage code gets dropped by the linker. Slightly hacky, but mostly harmless.
282 */
283void stage_entry(uintptr_t stage_arg)
284{
285 Main();
286}