blob: 2e3d09d3a2136403bea428c0a2a014b367fbe879 [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>
Rob Barnesb35acf92021-11-02 17:47:47 -060015#include <security/tpm/tspi.h>
16#include <security/tpm/tss.h>
Martin Roth50cca762020-08-13 11:06:18 -060017#include <security/vboot/vbnv.h>
Martin Rothc7acf162020-05-28 00:44:50 -060018#include <security/vboot/misc.h>
19#include <security/vboot/symbols.h>
20#include <security/vboot/vboot_common.h>
21#include <arch/stages.h>
22#include <stdarg.h>
23#include <stdio.h>
Kangheui Won4e2f5fd2020-09-17 16:37:13 +100024#include <timestamp.h>
Martin Rothc7acf162020-05-28 00:44:50 -060025
Martin Rothc7acf162020-05-28 00:44:50 -060026extern char _bss_start, _bss_end;
Martin Rothc7acf162020-05-28 00:44:50 -060027
28void __weak verstage_mainboard_early_init(void) {}
Rob Barnesf6e421f2021-11-08 13:04:18 -070029void __weak verstage_mainboard_espi_init(void) {}
Rob Barnes188be6b2021-11-09 13:21:28 -070030void __weak verstage_mainboard_tpm_init(void) {}
Martin Rothc7acf162020-05-28 00:44:50 -060031void __weak verstage_mainboard_init(void) {}
Rob Barnes188be6b2021-11-09 13:21:28 -070032
Kangheui Won695732b2021-04-25 12:11:17 +100033uint32_t __weak get_max_workbuf_size(uint32_t *size)
34{
35 /* This svc only exists in picasso and deprecated for later platforms.
36 * Provide sane default function here for those platforms.
37 */
38 *size = (uint32_t)((uintptr_t)_etransfer_buffer - (uintptr_t)_transfer_buffer);
39 return 0;
40}
Martin Rothc7acf162020-05-28 00:44:50 -060041
42static void reboot_into_recovery(struct vb2_context *ctx, uint32_t subcode)
43{
44 subcode += PSP_VBOOT_ERROR_SUBCODE;
45 svc_write_postcode(subcode);
46
Martin Rothc9689e02020-08-20 17:25:37 -060047 /*
48 * If there's an error but the PSP_verstage is already booting to RO,
49 * don't reset the system. It may be that the error is fatal, but if
50 * the system is stuck, don't intentionally force it into a reboot loop.
51 */
52 if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) {
53 printk(BIOS_ERR, "Already in recovery mode. Staying in RO.\n");
54 return;
55 }
56
Martin Rothc7acf162020-05-28 00:44:50 -060057 vb2api_fail(ctx, VB2_RECOVERY_RO_UNSPECIFIED, (int)subcode);
58 vboot_save_data(ctx);
59
60 svc_debug_print("Rebooting into recovery\n");
61 vboot_reboot();
62}
63
Martin Roth50cca762020-08-13 11:06:18 -060064static uint32_t check_cmos_recovery(void)
65{
66 /* Only reset if cmos is valid */
67 if (vbnv_cmos_failed())
68 return 0;
69
70 /* If the byte is set, clear it, then return error to reboot */
71 if (cmos_read(CMOS_RECOVERY_BYTE) == CMOS_RECOVERY_MAGIC_VAL) {
72 cmos_write(0x00, CMOS_RECOVERY_BYTE);
73 printk(BIOS_DEBUG, "Reboot into recovery requested by coreboot\n");
74 return POSTCODE_CMOS_RECOVERY;
75 }
76
77 return 0;
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{
Kangheui Won26bb4aa2021-10-18 15:31:45 +110085 struct embedded_firmware *ef_table;
Martin Rothc7acf162020-05-28 00:44:50 -060086 uint32_t psp_dir_addr, bios_dir_addr;
87 uint32_t *psp_dir_in_spi, *bios_dir_in_spi;
Kangheui Wonac7ec272021-01-15 15:04:25 +110088 const char *fname;
89 void *amdfw_location;
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -060090 void *boot_dev_base = rdev_mmap_full(boot_device_ro());
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 fname = "apu/amdfw_a";
Martin Rothc7acf162020-05-28 00:44:50 -0600100 } else {
Martin Rothe21698b2020-06-26 08:55:15 -0600101 fname = "apu/amdfw_b";
Martin Rothc7acf162020-05-28 00:44:50 -0600102 }
103
Kangheui Wonac7ec272021-01-15 15:04:25 +1100104 amdfw_location = cbfs_map(fname, NULL);
Martin Rothe21698b2020-06-26 08:55:15 -0600105 if (!amdfw_location) {
106 printk(BIOS_ERR, "Error: AMD Firmware table not found.\n");
107 return POSTCODE_AMD_FW_MISSING;
108 }
Kangheui Won26bb4aa2021-10-18 15:31:45 +1100109 ef_table = (struct embedded_firmware *)amdfw_location;
Martin Rothc7acf162020-05-28 00:44:50 -0600110 if (ef_table->signature != EMBEDDED_FW_SIGNATURE) {
111 printk(BIOS_ERR, "Error: ROMSIG address is not correct.\n");
112 return POSTCODE_ROMSIG_MISMATCH_ERROR;
113 }
114
Kangheui Won26bb4aa2021-10-18 15:31:45 +1100115 psp_dir_addr = ef_table->combo_psp_directory;
Kangheui Won5858fb42021-05-06 13:30:51 +1000116 bios_dir_addr = get_bios_dir_addr(ef_table);
Martin Rothc7acf162020-05-28 00:44:50 -0600117 psp_dir_in_spi = (uint32_t *)((psp_dir_addr & SPI_ADDR_MASK) +
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -0600118 (uint32_t)boot_dev_base);
Martin Rothc7acf162020-05-28 00:44:50 -0600119 bios_dir_in_spi = (uint32_t *)((bios_dir_addr & SPI_ADDR_MASK) +
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -0600120 (uint32_t)boot_dev_base);
Martin Rothc7acf162020-05-28 00:44:50 -0600121 if (*psp_dir_in_spi != PSP_COOKIE) {
122 printk(BIOS_ERR, "Error: PSP Directory address is not correct.\n");
123 return POSTCODE_PSP_COOKIE_MISMATCH_ERROR;
124 }
125 if (*bios_dir_in_spi != BDT1_COOKIE) {
126 printk(BIOS_ERR, "Error: BIOS Directory address is not correct.\n");
127 return POSTCODE_BDT1_COOKIE_MISMATCH_ERROR;
128 }
129
Kangheui Wonfab6e442021-10-18 15:35:28 +1100130 /* EFS2 uses relative address and PSP isn't happy with that */
131 if (ef_table->efs_gen.gen == EFS_SECOND_GEN) {
132 psp_dir_addr = FLASH_BASE_ADDR + (psp_dir_addr & SPI_ADDR_MASK);
133 bios_dir_addr = FLASH_BASE_ADDR + (bios_dir_addr & SPI_ADDR_MASK);
134 }
135
Kangheui Won26bb4aa2021-10-18 15:31:45 +1100136 if (update_psp_bios_dir(&psp_dir_addr, &bios_dir_addr)) {
Martin Rothc7acf162020-05-28 00:44:50 -0600137 printk(BIOS_ERR, "Error: Updated BIOS Directory could not be set.\n");
138 return POSTCODE_UPDATE_PSP_BIOS_DIR_ERROR;
139 }
140
141 return 0;
142}
143
144/*
145 * Save workbuf (and soon memory console and timestamps) to the bootloader to pass
146 * back to coreboot.
147 */
148static uint32_t save_buffers(struct vb2_context **ctx)
149{
150 uint32_t retval;
Martin Roth0c12abe2020-06-26 08:40:56 -0600151 uint32_t buffer_size = MIN_TRANSFER_BUFFER_SIZE;
Martin Rothc7acf162020-05-28 00:44:50 -0600152 uint32_t max_buffer_size;
Martin Roth0c12abe2020-06-26 08:40:56 -0600153 struct transfer_info_struct buffer_info = {0};
Martin Rothc7acf162020-05-28 00:44:50 -0600154
155 /*
Kangheui Won695732b2021-04-25 12:11:17 +1000156 * This should never fail on picasso, but if it does, we should still
157 * try to save the buffer. If that fails, then we should go to
158 * recovery mode.
Martin Rothc7acf162020-05-28 00:44:50 -0600159 */
Kangheui Won695732b2021-04-25 12:11:17 +1000160 if (get_max_workbuf_size(&max_buffer_size)) {
Martin Rothc7acf162020-05-28 00:44:50 -0600161 post_code(POSTCODE_DEFAULT_BUFFER_SIZE_NOTICE);
162 printk(BIOS_NOTICE, "Notice: using default transfer buffer size.\n");
Martin Roth0c12abe2020-06-26 08:40:56 -0600163 max_buffer_size = MIN_TRANSFER_BUFFER_SIZE;
Martin Rothc7acf162020-05-28 00:44:50 -0600164 }
165 printk(BIOS_DEBUG, "\nMaximum buffer size: %d bytes\n", max_buffer_size);
166
Martin Roth0c12abe2020-06-26 08:40:56 -0600167 /* Shrink workbuf if MP2 is in use and cannot be used to save buffer */
168 if (max_buffer_size < VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE) {
169 retval = vb2api_relocate(_vboot2_work, _vboot2_work, MIN_WORKBUF_TRANSFER_SIZE,
170 ctx);
171 if (retval != VB2_SUCCESS) {
172 printk(BIOS_ERR, "Error shrinking workbuf. Error code %#x\n", retval);
173 buffer_size = VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE;
174 post_code(POSTCODE_WORKBUF_RESIZE_WARNING);
175 }
176 } else {
177 buffer_size =
178 (uint32_t)((uintptr_t)_etransfer_buffer - (uintptr_t)_transfer_buffer);
179
180 buffer_info.console_offset = (uint32_t)((uintptr_t)_preram_cbmem_console -
181 (uintptr_t)_transfer_buffer);
182 buffer_info.timestamp_offset = (uint32_t)((uintptr_t)_timestamp -
183 (uintptr_t)_transfer_buffer);
184 buffer_info.fmap_offset = (uint32_t)((uintptr_t)_fmap_cache -
185 (uintptr_t)_transfer_buffer);
Martin Rothc7acf162020-05-28 00:44:50 -0600186 }
187
188 if (buffer_size > max_buffer_size) {
Martin Roth0c12abe2020-06-26 08:40:56 -0600189 printk(BIOS_ERR, "Error: Buffer is larger than max buffer size.\n");
Martin Rothc7acf162020-05-28 00:44:50 -0600190 post_code(POSTCODE_WORKBUF_BUFFER_SIZE_ERROR);
191 return POSTCODE_WORKBUF_BUFFER_SIZE_ERROR;
192 }
193
Martin Roth0c12abe2020-06-26 08:40:56 -0600194 buffer_info.magic_val = TRANSFER_MAGIC_VAL;
195 buffer_info.struct_bytes = sizeof(buffer_info);
196 buffer_info.buffer_size = buffer_size;
197 buffer_info.workbuf_offset = (uint32_t)((uintptr_t)_fmap_cache -
198 (uintptr_t)_vboot2_work);
199
Kangheui Won5f027fa2020-08-25 18:12:19 +1000200 memcpy(_transfer_buffer, &buffer_info, sizeof(buffer_info));
201
Kangheui Wona767eb42021-04-14 09:35:28 +1000202 retval = save_uapp_data((void *)_transfer_buffer, buffer_size);
Martin Rothc7acf162020-05-28 00:44:50 -0600203 if (retval) {
Martin Roth0c12abe2020-06-26 08:40:56 -0600204 printk(BIOS_ERR, "Error: Could not save workbuf. Error code 0x%08x\n", retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600205 return POSTCODE_WORKBUF_SAVE_ERROR;
206 }
207
208 return 0;
209}
210
Rob Barnesb35acf92021-11-02 17:47:47 -0600211/*
212 * S0i3 resume in PSP verstage is a special case. The FSDL is restoring mostly
213 * everything, so do the minimum necessary here. Unlike normal boot, subsequent
214 * coreboot stages are not run after s0i3 verstage.
215 * If the TPM is reset in S0i3, it must be re-initialized here.
216 */
217static void psp_verstage_s0i3_resume(void)
218{
219 uint32_t rv;
220
221 post_code(POSTCODE_VERSTAGE_S0I3_RESUME);
222
223 printk(BIOS_DEBUG, "Entering PSP verstage S0i3 resume\n");
224
225 if (!CONFIG(PSP_INIT_TPM_ON_S0I3_RESUME))
226 return;
227
228 rv = tpm_setup(true);
229 if (rv != TPM_SUCCESS) {
230 printk(BIOS_ERR, "tpm_setup failed rv:%d\n", rv);
231 reboot_into_recovery(vboot_get_context(), POSTCODE_INIT_TPM_FAILED);
232 }
233
234 rv = tlcl_disable_platform_hierarchy();
235 if (rv != TPM_SUCCESS) {
236 printk(BIOS_ERR, "tlcl_disable_platform_hierarchy failed rv:%d\n", rv);
237 reboot_into_recovery(vboot_get_context(), POSTCODE_INIT_TPM_FAILED);
238 }
239}
240
Martin Rothc7acf162020-05-28 00:44:50 -0600241void Main(void)
242{
243 uint32_t retval;
244 struct vb2_context *ctx = NULL;
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -0600245 void *boot_dev_base;
Rob Barnesb35acf92021-11-02 17:47:47 -0600246 uint32_t bootmode;
Martin Rothc7acf162020-05-28 00:44:50 -0600247
248 /*
249 * Do not use printk() before console_init()
250 * Do not use post_code() before verstage_mainboard_init()
251 */
Kangheui Won4e2f5fd2020-09-17 16:37:13 +1000252 timestamp_init(timestamp_get());
Martin Rothc7acf162020-05-28 00:44:50 -0600253 svc_write_postcode(POSTCODE_ENTERED_PSP_VERSTAGE);
254 svc_debug_print("Entering verstage on PSP\n");
255 memset(&_bss_start, '\0', &_bss_end - &_bss_start);
256
257 svc_write_postcode(POSTCODE_CONSOLE_INIT);
258 console_init();
259
260 svc_write_postcode(POSTCODE_EARLY_INIT);
261 retval = verstage_soc_early_init();
262 if (retval) {
Rob Barnesc30a1fa2021-11-08 06:43:07 -0700263 /*
264 * If verstage_soc_early_init fails, cmos is probably not
265 * accessible, so rebooting into recovery is not an option.
266 * Just reboot and hope for the best.
267 */
268 svc_write_postcode(POSTCODE_EARLY_INIT_ERROR);
269 svc_debug_print("verstage_soc_early_init failed! -- rebooting\n");
270 vboot_reboot();
Martin Rothc7acf162020-05-28 00:44:50 -0600271 }
Martin Rothc7acf162020-05-28 00:44:50 -0600272
Rob Barnesf6e421f2021-11-08 13:04:18 -0700273 printk(BIOS_DEBUG, "calling verstage_mainboard_espi_init\n");
274 verstage_mainboard_espi_init();
275
Rob Barnes188be6b2021-11-09 13:21:28 -0700276 printk(BIOS_DEBUG, "calling verstage_soc_espi_init\n");
277 verstage_soc_espi_init();
278
279 printk(BIOS_DEBUG, "calling verstage_mainboard_tpm_init\n");
280 /* mainboard_tpm_init may check board_id, so make sure espi is ready first */
281 verstage_mainboard_tpm_init();
282
Rob Barnes847a39f2021-11-15 12:56:34 -0700283 printk(BIOS_DEBUG, "calling verstage_soc_aoac_init\n");
284 verstage_soc_aoac_init();
285
286 printk(BIOS_DEBUG, "calling verstage_soc_i2c_init\n");
287 verstage_soc_i2c_init();
288
Rob Barnesb35acf92021-11-02 17:47:47 -0600289 /*
290 * S0i3 resume in PSP verstage is a special case, handle it separately.
291 * Make sure TPM i2c is ready first.
292 */
293 svc_get_boot_mode(&bootmode);
294 if (bootmode == PSP_BOOT_MODE_S0i3_RESUME) {
295 psp_verstage_s0i3_resume();
296 unmap_fch_devices();
297 svc_exit(0);
298 }
299
300 printk(BIOS_DEBUG, "calling verstage_mainboard_early_init\n");
301 verstage_mainboard_early_init();
302
303 svc_write_postcode(POSTCODE_LATE_INIT);
304 fch_io_enable_legacy_io();
305
Rob Barnes847a39f2021-11-15 12:56:34 -0700306 printk(BIOS_DEBUG, "calling verstage_soc_spi_init\n");
307 verstage_soc_spi_init();
308
Martin Rothc7acf162020-05-28 00:44:50 -0600309 verstage_mainboard_init();
310
311 post_code(POSTCODE_VERSTAGE_MAIN);
312
Kangheui Wonac7ec272021-01-15 15:04:25 +1100313 vboot_run_logic();
Martin Rothc7acf162020-05-28 00:44:50 -0600314
Martin Rothc9689e02020-08-20 17:25:37 -0600315 ctx = vboot_get_context();
Martin Roth50cca762020-08-13 11:06:18 -0600316 retval = check_cmos_recovery();
317 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600318 reboot_into_recovery(ctx, retval);
Martin Roth0c12abe2020-06-26 08:40:56 -0600319
Kangheui Wonac7ec272021-01-15 15:04:25 +1100320 post_code(POSTCODE_UPDATE_BOOT_REGION);
Kangheui Won97527252021-05-20 10:02:00 +1000321
322 /*
323 * Since psp_verstage doesn't load next stage we never call
324 * any cbfs API on RO path. However we still need to initialize
325 * RO CBFS MCACHE manually to pass it in transfer_buffer.
326 * In RW path, MCACHE build will be skipped for RO region since
327 * we already built here.
328 */
329 cbfs_get_boot_device(true);
330
Kangheui Wonac7ec272021-01-15 15:04:25 +1100331 retval = update_boot_region(ctx);
332 if (retval)
333 reboot_into_recovery(ctx, retval);
334
Martin Rothc7acf162020-05-28 00:44:50 -0600335 post_code(POSTCODE_SAVE_BUFFERS);
336 retval = save_buffers(&ctx);
337 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600338 reboot_into_recovery(ctx, retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600339
Martin Rothc7acf162020-05-28 00:44:50 -0600340
341 post_code(POSTCODE_UNMAP_SPI_ROM);
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -0600342 boot_dev_base = rdev_mmap_full(boot_device_ro());
343 if (boot_dev_base) {
344 if (svc_unmap_spi_rom((void *)boot_dev_base))
Martin Rothc7acf162020-05-28 00:44:50 -0600345 printk(BIOS_ERR, "Error unmapping SPI rom\n");
346 }
347
348 post_code(POSTCODE_UNMAP_FCH_DEVICES);
349 unmap_fch_devices();
350
351 post_code(POSTCODE_LEAVING_VERSTAGE);
352
353 printk(BIOS_DEBUG, "Leaving verstage on PSP\n");
354 svc_exit(retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600355}
356
Martin Rothc7acf162020-05-28 00:44:50 -0600357/*
358 * The stage_entry function is not used directly, but stage_entry() is marked as an entry
359 * point in arm/arch/header.h, so if stage_entry() isn't present and calling Main(), all
360 * the verstage code gets dropped by the linker. Slightly hacky, but mostly harmless.
361 */
362void stage_entry(uintptr_t stage_arg)
363{
364 Main();
365}