blob: f3b42565b3d844ba7b3ffe715e2358b1cb4da170 [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>
Kangheui Wonfab6e442021-10-18 15:35:28 +110013#include <soc/iomap.h>
Martin Roth0c12abe2020-06-26 08:40:56 -060014#include <soc/psp_transfer.h>
Martin Roth50cca762020-08-13 11:06:18 -060015#include <security/vboot/vbnv.h>
Martin Rothc7acf162020-05-28 00:44:50 -060016#include <security/vboot/misc.h>
17#include <security/vboot/symbols.h>
18#include <security/vboot/vboot_common.h>
19#include <arch/stages.h>
20#include <stdarg.h>
21#include <stdio.h>
Kangheui Won4e2f5fd2020-09-17 16:37:13 +100022#include <timestamp.h>
Martin Rothc7acf162020-05-28 00:44:50 -060023
Martin Rothc7acf162020-05-28 00:44:50 -060024extern char _bss_start, _bss_end;
Martin Rothc7acf162020-05-28 00:44:50 -060025
26void __weak verstage_mainboard_early_init(void) {}
Rob Barnesf6e421f2021-11-08 13:04:18 -070027void __weak verstage_mainboard_espi_init(void) {}
Martin Rothc7acf162020-05-28 00:44:50 -060028void __weak verstage_mainboard_init(void) {}
Kangheui Won695732b2021-04-25 12:11:17 +100029uint32_t __weak get_max_workbuf_size(uint32_t *size)
30{
31 /* This svc only exists in picasso and deprecated for later platforms.
32 * Provide sane default function here for those platforms.
33 */
34 *size = (uint32_t)((uintptr_t)_etransfer_buffer - (uintptr_t)_transfer_buffer);
35 return 0;
36}
Martin Rothc7acf162020-05-28 00:44:50 -060037
38static void reboot_into_recovery(struct vb2_context *ctx, uint32_t subcode)
39{
40 subcode += PSP_VBOOT_ERROR_SUBCODE;
41 svc_write_postcode(subcode);
42
Martin Rothc9689e02020-08-20 17:25:37 -060043 /*
44 * If there's an error but the PSP_verstage is already booting to RO,
45 * don't reset the system. It may be that the error is fatal, but if
46 * the system is stuck, don't intentionally force it into a reboot loop.
47 */
48 if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) {
49 printk(BIOS_ERR, "Already in recovery mode. Staying in RO.\n");
50 return;
51 }
52
Martin Rothc7acf162020-05-28 00:44:50 -060053 vb2api_fail(ctx, VB2_RECOVERY_RO_UNSPECIFIED, (int)subcode);
54 vboot_save_data(ctx);
55
56 svc_debug_print("Rebooting into recovery\n");
57 vboot_reboot();
58}
59
Martin Roth50cca762020-08-13 11:06:18 -060060static uint32_t check_cmos_recovery(void)
61{
62 /* Only reset if cmos is valid */
63 if (vbnv_cmos_failed())
64 return 0;
65
66 /* If the byte is set, clear it, then return error to reboot */
67 if (cmos_read(CMOS_RECOVERY_BYTE) == CMOS_RECOVERY_MAGIC_VAL) {
68 cmos_write(0x00, CMOS_RECOVERY_BYTE);
69 printk(BIOS_DEBUG, "Reboot into recovery requested by coreboot\n");
70 return POSTCODE_CMOS_RECOVERY;
71 }
72
73 return 0;
74}
75
Martin Rothc7acf162020-05-28 00:44:50 -060076/*
77 * Tell the PSP where to load the rest of the firmware from
78 */
79static uint32_t update_boot_region(struct vb2_context *ctx)
80{
Kangheui Won26bb4aa2021-10-18 15:31:45 +110081 struct embedded_firmware *ef_table;
Martin Rothc7acf162020-05-28 00:44:50 -060082 uint32_t psp_dir_addr, bios_dir_addr;
83 uint32_t *psp_dir_in_spi, *bios_dir_in_spi;
Kangheui Wonac7ec272021-01-15 15:04:25 +110084 const char *fname;
85 void *amdfw_location;
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -060086 void *boot_dev_base = rdev_mmap_full(boot_device_ro());
Martin Rothc7acf162020-05-28 00:44:50 -060087
88 /* Continue booting from RO */
89 if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) {
90 printk(BIOS_ERR, "In recovery mode. Staying in RO.\n");
91 return 0;
92 }
93
94 if (vboot_is_firmware_slot_a(ctx)) {
Martin Rothe21698b2020-06-26 08:55:15 -060095 fname = "apu/amdfw_a";
Martin Rothc7acf162020-05-28 00:44:50 -060096 } else {
Martin Rothe21698b2020-06-26 08:55:15 -060097 fname = "apu/amdfw_b";
Martin Rothc7acf162020-05-28 00:44:50 -060098 }
99
Kangheui Wonac7ec272021-01-15 15:04:25 +1100100 amdfw_location = cbfs_map(fname, NULL);
Martin Rothe21698b2020-06-26 08:55:15 -0600101 if (!amdfw_location) {
102 printk(BIOS_ERR, "Error: AMD Firmware table not found.\n");
103 return POSTCODE_AMD_FW_MISSING;
104 }
Kangheui Won26bb4aa2021-10-18 15:31:45 +1100105 ef_table = (struct embedded_firmware *)amdfw_location;
Martin Rothc7acf162020-05-28 00:44:50 -0600106 if (ef_table->signature != EMBEDDED_FW_SIGNATURE) {
107 printk(BIOS_ERR, "Error: ROMSIG address is not correct.\n");
108 return POSTCODE_ROMSIG_MISMATCH_ERROR;
109 }
110
Kangheui Won26bb4aa2021-10-18 15:31:45 +1100111 psp_dir_addr = ef_table->combo_psp_directory;
Kangheui Won5858fb42021-05-06 13:30:51 +1000112 bios_dir_addr = get_bios_dir_addr(ef_table);
Martin Rothc7acf162020-05-28 00:44:50 -0600113 psp_dir_in_spi = (uint32_t *)((psp_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 bios_dir_in_spi = (uint32_t *)((bios_dir_addr & SPI_ADDR_MASK) +
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -0600116 (uint32_t)boot_dev_base);
Martin Rothc7acf162020-05-28 00:44:50 -0600117 if (*psp_dir_in_spi != PSP_COOKIE) {
118 printk(BIOS_ERR, "Error: PSP Directory address is not correct.\n");
119 return POSTCODE_PSP_COOKIE_MISMATCH_ERROR;
120 }
121 if (*bios_dir_in_spi != BDT1_COOKIE) {
122 printk(BIOS_ERR, "Error: BIOS Directory address is not correct.\n");
123 return POSTCODE_BDT1_COOKIE_MISMATCH_ERROR;
124 }
125
Kangheui Wonfab6e442021-10-18 15:35:28 +1100126 /* EFS2 uses relative address and PSP isn't happy with that */
127 if (ef_table->efs_gen.gen == EFS_SECOND_GEN) {
128 psp_dir_addr = FLASH_BASE_ADDR + (psp_dir_addr & SPI_ADDR_MASK);
129 bios_dir_addr = FLASH_BASE_ADDR + (bios_dir_addr & SPI_ADDR_MASK);
130 }
131
Kangheui Won26bb4aa2021-10-18 15:31:45 +1100132 if (update_psp_bios_dir(&psp_dir_addr, &bios_dir_addr)) {
Martin Rothc7acf162020-05-28 00:44:50 -0600133 printk(BIOS_ERR, "Error: Updated BIOS Directory could not be set.\n");
134 return POSTCODE_UPDATE_PSP_BIOS_DIR_ERROR;
135 }
136
137 return 0;
138}
139
140/*
141 * Save workbuf (and soon memory console and timestamps) to the bootloader to pass
142 * back to coreboot.
143 */
144static uint32_t save_buffers(struct vb2_context **ctx)
145{
146 uint32_t retval;
Martin Roth0c12abe2020-06-26 08:40:56 -0600147 uint32_t buffer_size = MIN_TRANSFER_BUFFER_SIZE;
Martin Rothc7acf162020-05-28 00:44:50 -0600148 uint32_t max_buffer_size;
Martin Roth0c12abe2020-06-26 08:40:56 -0600149 struct transfer_info_struct buffer_info = {0};
Martin Rothc7acf162020-05-28 00:44:50 -0600150
151 /*
Kangheui Won695732b2021-04-25 12:11:17 +1000152 * This should never fail on picasso, but if it does, we should still
153 * try to save the buffer. If that fails, then we should go to
154 * recovery mode.
Martin Rothc7acf162020-05-28 00:44:50 -0600155 */
Kangheui Won695732b2021-04-25 12:11:17 +1000156 if (get_max_workbuf_size(&max_buffer_size)) {
Martin Rothc7acf162020-05-28 00:44:50 -0600157 post_code(POSTCODE_DEFAULT_BUFFER_SIZE_NOTICE);
158 printk(BIOS_NOTICE, "Notice: using default transfer buffer size.\n");
Martin Roth0c12abe2020-06-26 08:40:56 -0600159 max_buffer_size = MIN_TRANSFER_BUFFER_SIZE;
Martin Rothc7acf162020-05-28 00:44:50 -0600160 }
161 printk(BIOS_DEBUG, "\nMaximum buffer size: %d bytes\n", max_buffer_size);
162
Martin Roth0c12abe2020-06-26 08:40:56 -0600163 /* Shrink workbuf if MP2 is in use and cannot be used to save buffer */
164 if (max_buffer_size < VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE) {
165 retval = vb2api_relocate(_vboot2_work, _vboot2_work, MIN_WORKBUF_TRANSFER_SIZE,
166 ctx);
167 if (retval != VB2_SUCCESS) {
168 printk(BIOS_ERR, "Error shrinking workbuf. Error code %#x\n", retval);
169 buffer_size = VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE;
170 post_code(POSTCODE_WORKBUF_RESIZE_WARNING);
171 }
172 } else {
173 buffer_size =
174 (uint32_t)((uintptr_t)_etransfer_buffer - (uintptr_t)_transfer_buffer);
175
176 buffer_info.console_offset = (uint32_t)((uintptr_t)_preram_cbmem_console -
177 (uintptr_t)_transfer_buffer);
178 buffer_info.timestamp_offset = (uint32_t)((uintptr_t)_timestamp -
179 (uintptr_t)_transfer_buffer);
180 buffer_info.fmap_offset = (uint32_t)((uintptr_t)_fmap_cache -
181 (uintptr_t)_transfer_buffer);
Martin Rothc7acf162020-05-28 00:44:50 -0600182 }
183
184 if (buffer_size > max_buffer_size) {
Martin Roth0c12abe2020-06-26 08:40:56 -0600185 printk(BIOS_ERR, "Error: Buffer is larger than max buffer size.\n");
Martin Rothc7acf162020-05-28 00:44:50 -0600186 post_code(POSTCODE_WORKBUF_BUFFER_SIZE_ERROR);
187 return POSTCODE_WORKBUF_BUFFER_SIZE_ERROR;
188 }
189
Martin Roth0c12abe2020-06-26 08:40:56 -0600190 buffer_info.magic_val = TRANSFER_MAGIC_VAL;
191 buffer_info.struct_bytes = sizeof(buffer_info);
192 buffer_info.buffer_size = buffer_size;
193 buffer_info.workbuf_offset = (uint32_t)((uintptr_t)_fmap_cache -
194 (uintptr_t)_vboot2_work);
195
Kangheui Won5f027fa2020-08-25 18:12:19 +1000196 memcpy(_transfer_buffer, &buffer_info, sizeof(buffer_info));
197
Kangheui Wona767eb42021-04-14 09:35:28 +1000198 retval = save_uapp_data((void *)_transfer_buffer, buffer_size);
Martin Rothc7acf162020-05-28 00:44:50 -0600199 if (retval) {
Martin Roth0c12abe2020-06-26 08:40:56 -0600200 printk(BIOS_ERR, "Error: Could not save workbuf. Error code 0x%08x\n", retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600201 return POSTCODE_WORKBUF_SAVE_ERROR;
202 }
203
204 return 0;
205}
206
207void Main(void)
208{
209 uint32_t retval;
210 struct vb2_context *ctx = NULL;
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -0600211 void *boot_dev_base;
Martin Rothc7acf162020-05-28 00:44:50 -0600212
213 /*
214 * Do not use printk() before console_init()
215 * Do not use post_code() before verstage_mainboard_init()
216 */
Kangheui Won4e2f5fd2020-09-17 16:37:13 +1000217 timestamp_init(timestamp_get());
Martin Rothc7acf162020-05-28 00:44:50 -0600218 svc_write_postcode(POSTCODE_ENTERED_PSP_VERSTAGE);
219 svc_debug_print("Entering verstage on PSP\n");
220 memset(&_bss_start, '\0', &_bss_end - &_bss_start);
221
222 svc_write_postcode(POSTCODE_CONSOLE_INIT);
223 console_init();
224
225 svc_write_postcode(POSTCODE_EARLY_INIT);
226 retval = verstage_soc_early_init();
227 if (retval) {
Rob Barnesc30a1fa2021-11-08 06:43:07 -0700228 /*
229 * If verstage_soc_early_init fails, cmos is probably not
230 * accessible, so rebooting into recovery is not an option.
231 * Just reboot and hope for the best.
232 */
233 svc_write_postcode(POSTCODE_EARLY_INIT_ERROR);
234 svc_debug_print("verstage_soc_early_init failed! -- rebooting\n");
235 vboot_reboot();
Martin Rothc7acf162020-05-28 00:44:50 -0600236 }
Martin Rothc7acf162020-05-28 00:44:50 -0600237
Rob Barnesf6e421f2021-11-08 13:04:18 -0700238 printk(BIOS_DEBUG, "calling verstage_mainboard_espi_init\n");
239 verstage_mainboard_espi_init();
240
241 printk(BIOS_DEBUG, "calling verstage_mainboard_early_init\n");
Martin Rothc7acf162020-05-28 00:44:50 -0600242 verstage_mainboard_early_init();
243
244 svc_write_postcode(POSTCODE_LATE_INIT);
Felix Held26935d12020-12-08 00:40:04 +0100245 fch_io_enable_legacy_io();
Martin Rothc7acf162020-05-28 00:44:50 -0600246 verstage_soc_init();
247 verstage_mainboard_init();
248
249 post_code(POSTCODE_VERSTAGE_MAIN);
250
Kangheui Wonac7ec272021-01-15 15:04:25 +1100251 vboot_run_logic();
Martin Rothc7acf162020-05-28 00:44:50 -0600252
Martin Rothc9689e02020-08-20 17:25:37 -0600253 ctx = vboot_get_context();
Martin Roth50cca762020-08-13 11:06:18 -0600254 retval = check_cmos_recovery();
255 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600256 reboot_into_recovery(ctx, retval);
Martin Roth0c12abe2020-06-26 08:40:56 -0600257
Kangheui Wonac7ec272021-01-15 15:04:25 +1100258 post_code(POSTCODE_UPDATE_BOOT_REGION);
Kangheui Won97527252021-05-20 10:02:00 +1000259
260 /*
261 * Since psp_verstage doesn't load next stage we never call
262 * any cbfs API on RO path. However we still need to initialize
263 * RO CBFS MCACHE manually to pass it in transfer_buffer.
264 * In RW path, MCACHE build will be skipped for RO region since
265 * we already built here.
266 */
267 cbfs_get_boot_device(true);
268
Kangheui Wonac7ec272021-01-15 15:04:25 +1100269 retval = update_boot_region(ctx);
270 if (retval)
271 reboot_into_recovery(ctx, retval);
272
Martin Rothc7acf162020-05-28 00:44:50 -0600273 post_code(POSTCODE_SAVE_BUFFERS);
274 retval = save_buffers(&ctx);
275 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600276 reboot_into_recovery(ctx, retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600277
Martin Rothc7acf162020-05-28 00:44:50 -0600278
279 post_code(POSTCODE_UNMAP_SPI_ROM);
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -0600280 boot_dev_base = rdev_mmap_full(boot_device_ro());
281 if (boot_dev_base) {
282 if (svc_unmap_spi_rom((void *)boot_dev_base))
Martin Rothc7acf162020-05-28 00:44:50 -0600283 printk(BIOS_ERR, "Error unmapping SPI rom\n");
284 }
285
286 post_code(POSTCODE_UNMAP_FCH_DEVICES);
287 unmap_fch_devices();
288
289 post_code(POSTCODE_LEAVING_VERSTAGE);
290
291 printk(BIOS_DEBUG, "Leaving verstage on PSP\n");
292 svc_exit(retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600293}
294
Martin Rothc7acf162020-05-28 00:44:50 -0600295/*
296 * The stage_entry function is not used directly, but stage_entry() is marked as an entry
297 * point in arm/arch/header.h, so if stage_entry() isn't present and calling Main(), all
298 * the verstage code gets dropped by the linker. Slightly hacky, but mostly harmless.
299 */
300void stage_entry(uintptr_t stage_arg)
301{
302 Main();
303}