blob: 71f07c04172dcc37f24dfef3c4b7ded1938fbb98 [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) {}
Rob Barnes188be6b2021-11-09 13:21:28 -070028void __weak verstage_mainboard_tpm_init(void) {}
Martin Rothc7acf162020-05-28 00:44:50 -060029void __weak verstage_mainboard_init(void) {}
Rob Barnes188be6b2021-11-09 13:21:28 -070030
Kangheui Won695732b2021-04-25 12:11:17 +100031uint32_t __weak get_max_workbuf_size(uint32_t *size)
32{
33 /* This svc only exists in picasso and deprecated for later platforms.
34 * Provide sane default function here for those platforms.
35 */
36 *size = (uint32_t)((uintptr_t)_etransfer_buffer - (uintptr_t)_transfer_buffer);
37 return 0;
38}
Martin Rothc7acf162020-05-28 00:44:50 -060039
40static void reboot_into_recovery(struct vb2_context *ctx, uint32_t subcode)
41{
42 subcode += PSP_VBOOT_ERROR_SUBCODE;
43 svc_write_postcode(subcode);
44
Martin Rothc9689e02020-08-20 17:25:37 -060045 /*
46 * If there's an error but the PSP_verstage is already booting to RO,
47 * don't reset the system. It may be that the error is fatal, but if
48 * the system is stuck, don't intentionally force it into a reboot loop.
49 */
50 if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) {
51 printk(BIOS_ERR, "Already in recovery mode. Staying in RO.\n");
52 return;
53 }
54
Martin Rothc7acf162020-05-28 00:44:50 -060055 vb2api_fail(ctx, VB2_RECOVERY_RO_UNSPECIFIED, (int)subcode);
56 vboot_save_data(ctx);
57
58 svc_debug_print("Rebooting into recovery\n");
59 vboot_reboot();
60}
61
Martin Roth50cca762020-08-13 11:06:18 -060062static uint32_t check_cmos_recovery(void)
63{
64 /* Only reset if cmos is valid */
65 if (vbnv_cmos_failed())
66 return 0;
67
68 /* If the byte is set, clear it, then return error to reboot */
69 if (cmos_read(CMOS_RECOVERY_BYTE) == CMOS_RECOVERY_MAGIC_VAL) {
70 cmos_write(0x00, CMOS_RECOVERY_BYTE);
71 printk(BIOS_DEBUG, "Reboot into recovery requested by coreboot\n");
72 return POSTCODE_CMOS_RECOVERY;
73 }
74
75 return 0;
76}
77
Martin Rothc7acf162020-05-28 00:44:50 -060078/*
79 * Tell the PSP where to load the rest of the firmware from
80 */
81static uint32_t update_boot_region(struct vb2_context *ctx)
82{
Kangheui Won26bb4aa2021-10-18 15:31:45 +110083 struct embedded_firmware *ef_table;
Martin Rothc7acf162020-05-28 00:44:50 -060084 uint32_t psp_dir_addr, bios_dir_addr;
85 uint32_t *psp_dir_in_spi, *bios_dir_in_spi;
Kangheui Wonac7ec272021-01-15 15:04:25 +110086 const char *fname;
87 void *amdfw_location;
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -060088 void *boot_dev_base = rdev_mmap_full(boot_device_ro());
Martin Rothc7acf162020-05-28 00:44:50 -060089
90 /* Continue booting from RO */
91 if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) {
92 printk(BIOS_ERR, "In recovery mode. Staying in RO.\n");
93 return 0;
94 }
95
96 if (vboot_is_firmware_slot_a(ctx)) {
Martin Rothe21698b2020-06-26 08:55:15 -060097 fname = "apu/amdfw_a";
Martin Rothc7acf162020-05-28 00:44:50 -060098 } else {
Martin Rothe21698b2020-06-26 08:55:15 -060099 fname = "apu/amdfw_b";
Martin Rothc7acf162020-05-28 00:44:50 -0600100 }
101
Kangheui Wonac7ec272021-01-15 15:04:25 +1100102 amdfw_location = cbfs_map(fname, NULL);
Martin Rothe21698b2020-06-26 08:55:15 -0600103 if (!amdfw_location) {
104 printk(BIOS_ERR, "Error: AMD Firmware table not found.\n");
105 return POSTCODE_AMD_FW_MISSING;
106 }
Kangheui Won26bb4aa2021-10-18 15:31:45 +1100107 ef_table = (struct embedded_firmware *)amdfw_location;
Martin Rothc7acf162020-05-28 00:44:50 -0600108 if (ef_table->signature != EMBEDDED_FW_SIGNATURE) {
109 printk(BIOS_ERR, "Error: ROMSIG address is not correct.\n");
110 return POSTCODE_ROMSIG_MISMATCH_ERROR;
111 }
112
Kangheui Won26bb4aa2021-10-18 15:31:45 +1100113 psp_dir_addr = ef_table->combo_psp_directory;
Kangheui Won5858fb42021-05-06 13:30:51 +1000114 bios_dir_addr = get_bios_dir_addr(ef_table);
Martin Rothc7acf162020-05-28 00:44:50 -0600115 psp_dir_in_spi = (uint32_t *)((psp_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 bios_dir_in_spi = (uint32_t *)((bios_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 if (*psp_dir_in_spi != PSP_COOKIE) {
120 printk(BIOS_ERR, "Error: PSP Directory address is not correct.\n");
121 return POSTCODE_PSP_COOKIE_MISMATCH_ERROR;
122 }
123 if (*bios_dir_in_spi != BDT1_COOKIE) {
124 printk(BIOS_ERR, "Error: BIOS Directory address is not correct.\n");
125 return POSTCODE_BDT1_COOKIE_MISMATCH_ERROR;
126 }
127
Kangheui Wonfab6e442021-10-18 15:35:28 +1100128 /* EFS2 uses relative address and PSP isn't happy with that */
129 if (ef_table->efs_gen.gen == EFS_SECOND_GEN) {
130 psp_dir_addr = FLASH_BASE_ADDR + (psp_dir_addr & SPI_ADDR_MASK);
131 bios_dir_addr = FLASH_BASE_ADDR + (bios_dir_addr & SPI_ADDR_MASK);
132 }
133
Kangheui Won26bb4aa2021-10-18 15:31:45 +1100134 if (update_psp_bios_dir(&psp_dir_addr, &bios_dir_addr)) {
Martin Rothc7acf162020-05-28 00:44:50 -0600135 printk(BIOS_ERR, "Error: Updated BIOS Directory could not be set.\n");
136 return POSTCODE_UPDATE_PSP_BIOS_DIR_ERROR;
137 }
138
139 return 0;
140}
141
142/*
143 * Save workbuf (and soon memory console and timestamps) to the bootloader to pass
144 * back to coreboot.
145 */
146static uint32_t save_buffers(struct vb2_context **ctx)
147{
148 uint32_t retval;
Martin Roth0c12abe2020-06-26 08:40:56 -0600149 uint32_t buffer_size = MIN_TRANSFER_BUFFER_SIZE;
Martin Rothc7acf162020-05-28 00:44:50 -0600150 uint32_t max_buffer_size;
Martin Roth0c12abe2020-06-26 08:40:56 -0600151 struct transfer_info_struct buffer_info = {0};
Martin Rothc7acf162020-05-28 00:44:50 -0600152
153 /*
Kangheui Won695732b2021-04-25 12:11:17 +1000154 * This should never fail on picasso, but if it does, we should still
155 * try to save the buffer. If that fails, then we should go to
156 * recovery mode.
Martin Rothc7acf162020-05-28 00:44:50 -0600157 */
Kangheui Won695732b2021-04-25 12:11:17 +1000158 if (get_max_workbuf_size(&max_buffer_size)) {
Martin Rothc7acf162020-05-28 00:44:50 -0600159 post_code(POSTCODE_DEFAULT_BUFFER_SIZE_NOTICE);
160 printk(BIOS_NOTICE, "Notice: using default transfer buffer size.\n");
Martin Roth0c12abe2020-06-26 08:40:56 -0600161 max_buffer_size = MIN_TRANSFER_BUFFER_SIZE;
Martin Rothc7acf162020-05-28 00:44:50 -0600162 }
163 printk(BIOS_DEBUG, "\nMaximum buffer size: %d bytes\n", max_buffer_size);
164
Martin Roth0c12abe2020-06-26 08:40:56 -0600165 /* Shrink workbuf if MP2 is in use and cannot be used to save buffer */
166 if (max_buffer_size < VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE) {
167 retval = vb2api_relocate(_vboot2_work, _vboot2_work, MIN_WORKBUF_TRANSFER_SIZE,
168 ctx);
169 if (retval != VB2_SUCCESS) {
170 printk(BIOS_ERR, "Error shrinking workbuf. Error code %#x\n", retval);
171 buffer_size = VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE;
172 post_code(POSTCODE_WORKBUF_RESIZE_WARNING);
173 }
174 } else {
175 buffer_size =
176 (uint32_t)((uintptr_t)_etransfer_buffer - (uintptr_t)_transfer_buffer);
177
178 buffer_info.console_offset = (uint32_t)((uintptr_t)_preram_cbmem_console -
179 (uintptr_t)_transfer_buffer);
180 buffer_info.timestamp_offset = (uint32_t)((uintptr_t)_timestamp -
181 (uintptr_t)_transfer_buffer);
182 buffer_info.fmap_offset = (uint32_t)((uintptr_t)_fmap_cache -
183 (uintptr_t)_transfer_buffer);
Martin Rothc7acf162020-05-28 00:44:50 -0600184 }
185
186 if (buffer_size > max_buffer_size) {
Martin Roth0c12abe2020-06-26 08:40:56 -0600187 printk(BIOS_ERR, "Error: Buffer is larger than max buffer size.\n");
Martin Rothc7acf162020-05-28 00:44:50 -0600188 post_code(POSTCODE_WORKBUF_BUFFER_SIZE_ERROR);
189 return POSTCODE_WORKBUF_BUFFER_SIZE_ERROR;
190 }
191
Martin Roth0c12abe2020-06-26 08:40:56 -0600192 buffer_info.magic_val = TRANSFER_MAGIC_VAL;
193 buffer_info.struct_bytes = sizeof(buffer_info);
194 buffer_info.buffer_size = buffer_size;
195 buffer_info.workbuf_offset = (uint32_t)((uintptr_t)_fmap_cache -
196 (uintptr_t)_vboot2_work);
197
Kangheui Won5f027fa2020-08-25 18:12:19 +1000198 memcpy(_transfer_buffer, &buffer_info, sizeof(buffer_info));
199
Kangheui Wona767eb42021-04-14 09:35:28 +1000200 retval = save_uapp_data((void *)_transfer_buffer, buffer_size);
Martin Rothc7acf162020-05-28 00:44:50 -0600201 if (retval) {
Martin Roth0c12abe2020-06-26 08:40:56 -0600202 printk(BIOS_ERR, "Error: Could not save workbuf. Error code 0x%08x\n", retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600203 return POSTCODE_WORKBUF_SAVE_ERROR;
204 }
205
206 return 0;
207}
208
209void Main(void)
210{
211 uint32_t retval;
212 struct vb2_context *ctx = NULL;
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -0600213 void *boot_dev_base;
Martin Rothc7acf162020-05-28 00:44:50 -0600214
215 /*
216 * Do not use printk() before console_init()
217 * Do not use post_code() before verstage_mainboard_init()
218 */
Kangheui Won4e2f5fd2020-09-17 16:37:13 +1000219 timestamp_init(timestamp_get());
Martin Rothc7acf162020-05-28 00:44:50 -0600220 svc_write_postcode(POSTCODE_ENTERED_PSP_VERSTAGE);
221 svc_debug_print("Entering verstage on PSP\n");
222 memset(&_bss_start, '\0', &_bss_end - &_bss_start);
223
224 svc_write_postcode(POSTCODE_CONSOLE_INIT);
225 console_init();
226
227 svc_write_postcode(POSTCODE_EARLY_INIT);
228 retval = verstage_soc_early_init();
229 if (retval) {
Rob Barnesc30a1fa2021-11-08 06:43:07 -0700230 /*
231 * If verstage_soc_early_init fails, cmos is probably not
232 * accessible, so rebooting into recovery is not an option.
233 * Just reboot and hope for the best.
234 */
235 svc_write_postcode(POSTCODE_EARLY_INIT_ERROR);
236 svc_debug_print("verstage_soc_early_init failed! -- rebooting\n");
237 vboot_reboot();
Martin Rothc7acf162020-05-28 00:44:50 -0600238 }
Martin Rothc7acf162020-05-28 00:44:50 -0600239
Rob Barnesf6e421f2021-11-08 13:04:18 -0700240 printk(BIOS_DEBUG, "calling verstage_mainboard_espi_init\n");
241 verstage_mainboard_espi_init();
242
Rob Barnes188be6b2021-11-09 13:21:28 -0700243 printk(BIOS_DEBUG, "calling verstage_soc_espi_init\n");
244 verstage_soc_espi_init();
245
246 printk(BIOS_DEBUG, "calling verstage_mainboard_tpm_init\n");
247 /* mainboard_tpm_init may check board_id, so make sure espi is ready first */
248 verstage_mainboard_tpm_init();
249
Rob Barnesf6e421f2021-11-08 13:04:18 -0700250 printk(BIOS_DEBUG, "calling verstage_mainboard_early_init\n");
Martin Rothc7acf162020-05-28 00:44:50 -0600251 verstage_mainboard_early_init();
252
253 svc_write_postcode(POSTCODE_LATE_INIT);
Felix Held26935d12020-12-08 00:40:04 +0100254 fch_io_enable_legacy_io();
Rob Barnes847a39f2021-11-15 12:56:34 -0700255
Rob Barnes847a39f2021-11-15 12:56:34 -0700256 printk(BIOS_DEBUG, "calling verstage_soc_aoac_init\n");
257 verstage_soc_aoac_init();
258
259 printk(BIOS_DEBUG, "calling verstage_soc_i2c_init\n");
260 verstage_soc_i2c_init();
261
262 printk(BIOS_DEBUG, "calling verstage_soc_spi_init\n");
263 verstage_soc_spi_init();
264
Martin Rothc7acf162020-05-28 00:44:50 -0600265 verstage_mainboard_init();
266
267 post_code(POSTCODE_VERSTAGE_MAIN);
268
Kangheui Wonac7ec272021-01-15 15:04:25 +1100269 vboot_run_logic();
Martin Rothc7acf162020-05-28 00:44:50 -0600270
Martin Rothc9689e02020-08-20 17:25:37 -0600271 ctx = vboot_get_context();
Martin Roth50cca762020-08-13 11:06:18 -0600272 retval = check_cmos_recovery();
273 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600274 reboot_into_recovery(ctx, retval);
Martin Roth0c12abe2020-06-26 08:40:56 -0600275
Kangheui Wonac7ec272021-01-15 15:04:25 +1100276 post_code(POSTCODE_UPDATE_BOOT_REGION);
Kangheui Won97527252021-05-20 10:02:00 +1000277
278 /*
279 * Since psp_verstage doesn't load next stage we never call
280 * any cbfs API on RO path. However we still need to initialize
281 * RO CBFS MCACHE manually to pass it in transfer_buffer.
282 * In RW path, MCACHE build will be skipped for RO region since
283 * we already built here.
284 */
285 cbfs_get_boot_device(true);
286
Kangheui Wonac7ec272021-01-15 15:04:25 +1100287 retval = update_boot_region(ctx);
288 if (retval)
289 reboot_into_recovery(ctx, retval);
290
Martin Rothc7acf162020-05-28 00:44:50 -0600291 post_code(POSTCODE_SAVE_BUFFERS);
292 retval = save_buffers(&ctx);
293 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600294 reboot_into_recovery(ctx, retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600295
Martin Rothc7acf162020-05-28 00:44:50 -0600296
297 post_code(POSTCODE_UNMAP_SPI_ROM);
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -0600298 boot_dev_base = rdev_mmap_full(boot_device_ro());
299 if (boot_dev_base) {
300 if (svc_unmap_spi_rom((void *)boot_dev_base))
Martin Rothc7acf162020-05-28 00:44:50 -0600301 printk(BIOS_ERR, "Error unmapping SPI rom\n");
302 }
303
304 post_code(POSTCODE_UNMAP_FCH_DEVICES);
305 unmap_fch_devices();
306
307 post_code(POSTCODE_LEAVING_VERSTAGE);
308
309 printk(BIOS_DEBUG, "Leaving verstage on PSP\n");
310 svc_exit(retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600311}
312
Martin Rothc7acf162020-05-28 00:44:50 -0600313/*
314 * The stage_entry function is not used directly, but stage_entry() is marked as an entry
315 * point in arm/arch/header.h, so if stage_entry() isn't present and calling Main(), all
316 * the verstage code gets dropped by the linker. Slightly hacky, but mostly harmless.
317 */
318void stage_entry(uintptr_t stage_arg)
319{
320 Main();
321}