blob: 3bfa3e99841970eb956618902ade81de5fd28919 [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) {}
27void __weak verstage_mainboard_init(void) {}
Kangheui Won695732b2021-04-25 12:11:17 +100028uint32_t __weak get_max_workbuf_size(uint32_t *size)
29{
30 /* This svc only exists in picasso and deprecated for later platforms.
31 * Provide sane default function here for those platforms.
32 */
33 *size = (uint32_t)((uintptr_t)_etransfer_buffer - (uintptr_t)_transfer_buffer);
34 return 0;
35}
Martin Rothc7acf162020-05-28 00:44:50 -060036
37static void reboot_into_recovery(struct vb2_context *ctx, uint32_t subcode)
38{
39 subcode += PSP_VBOOT_ERROR_SUBCODE;
40 svc_write_postcode(subcode);
41
Martin Rothc9689e02020-08-20 17:25:37 -060042 /*
43 * If there's an error but the PSP_verstage is already booting to RO,
44 * don't reset the system. It may be that the error is fatal, but if
45 * the system is stuck, don't intentionally force it into a reboot loop.
46 */
47 if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) {
48 printk(BIOS_ERR, "Already in recovery mode. Staying in RO.\n");
49 return;
50 }
51
Martin Rothc7acf162020-05-28 00:44:50 -060052 vb2api_fail(ctx, VB2_RECOVERY_RO_UNSPECIFIED, (int)subcode);
53 vboot_save_data(ctx);
54
55 svc_debug_print("Rebooting into recovery\n");
56 vboot_reboot();
57}
58
Martin Roth50cca762020-08-13 11:06:18 -060059static uint32_t check_cmos_recovery(void)
60{
61 /* Only reset if cmos is valid */
62 if (vbnv_cmos_failed())
63 return 0;
64
65 /* If the byte is set, clear it, then return error to reboot */
66 if (cmos_read(CMOS_RECOVERY_BYTE) == CMOS_RECOVERY_MAGIC_VAL) {
67 cmos_write(0x00, CMOS_RECOVERY_BYTE);
68 printk(BIOS_DEBUG, "Reboot into recovery requested by coreboot\n");
69 return POSTCODE_CMOS_RECOVERY;
70 }
71
72 return 0;
73}
74
Martin Rothc7acf162020-05-28 00:44:50 -060075/*
76 * Tell the PSP where to load the rest of the firmware from
77 */
78static uint32_t update_boot_region(struct vb2_context *ctx)
79{
Kangheui Won26bb4aa2021-10-18 15:31:45 +110080 struct embedded_firmware *ef_table;
Martin Rothc7acf162020-05-28 00:44:50 -060081 uint32_t psp_dir_addr, bios_dir_addr;
82 uint32_t *psp_dir_in_spi, *bios_dir_in_spi;
Kangheui Wonac7ec272021-01-15 15:04:25 +110083 const char *fname;
84 void *amdfw_location;
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -060085 void *boot_dev_base = rdev_mmap_full(boot_device_ro());
Martin Rothc7acf162020-05-28 00:44:50 -060086
87 /* Continue booting from RO */
88 if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) {
89 printk(BIOS_ERR, "In recovery mode. Staying in RO.\n");
90 return 0;
91 }
92
93 if (vboot_is_firmware_slot_a(ctx)) {
Martin Rothe21698b2020-06-26 08:55:15 -060094 fname = "apu/amdfw_a";
Martin Rothc7acf162020-05-28 00:44:50 -060095 } else {
Martin Rothe21698b2020-06-26 08:55:15 -060096 fname = "apu/amdfw_b";
Martin Rothc7acf162020-05-28 00:44:50 -060097 }
98
Kangheui Wonac7ec272021-01-15 15:04:25 +110099 amdfw_location = cbfs_map(fname, NULL);
Martin Rothe21698b2020-06-26 08:55:15 -0600100 if (!amdfw_location) {
101 printk(BIOS_ERR, "Error: AMD Firmware table not found.\n");
102 return POSTCODE_AMD_FW_MISSING;
103 }
Kangheui Won26bb4aa2021-10-18 15:31:45 +1100104 ef_table = (struct embedded_firmware *)amdfw_location;
Martin Rothc7acf162020-05-28 00:44:50 -0600105 if (ef_table->signature != EMBEDDED_FW_SIGNATURE) {
106 printk(BIOS_ERR, "Error: ROMSIG address is not correct.\n");
107 return POSTCODE_ROMSIG_MISMATCH_ERROR;
108 }
109
Kangheui Won26bb4aa2021-10-18 15:31:45 +1100110 psp_dir_addr = ef_table->combo_psp_directory;
Kangheui Won5858fb42021-05-06 13:30:51 +1000111 bios_dir_addr = get_bios_dir_addr(ef_table);
Martin Rothc7acf162020-05-28 00:44:50 -0600112 psp_dir_in_spi = (uint32_t *)((psp_dir_addr & SPI_ADDR_MASK) +
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -0600113 (uint32_t)boot_dev_base);
Martin Rothc7acf162020-05-28 00:44:50 -0600114 bios_dir_in_spi = (uint32_t *)((bios_dir_addr & SPI_ADDR_MASK) +
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -0600115 (uint32_t)boot_dev_base);
Martin Rothc7acf162020-05-28 00:44:50 -0600116 if (*psp_dir_in_spi != PSP_COOKIE) {
117 printk(BIOS_ERR, "Error: PSP Directory address is not correct.\n");
118 return POSTCODE_PSP_COOKIE_MISMATCH_ERROR;
119 }
120 if (*bios_dir_in_spi != BDT1_COOKIE) {
121 printk(BIOS_ERR, "Error: BIOS Directory address is not correct.\n");
122 return POSTCODE_BDT1_COOKIE_MISMATCH_ERROR;
123 }
124
Kangheui Wonfab6e442021-10-18 15:35:28 +1100125 /* EFS2 uses relative address and PSP isn't happy with that */
126 if (ef_table->efs_gen.gen == EFS_SECOND_GEN) {
127 psp_dir_addr = FLASH_BASE_ADDR + (psp_dir_addr & SPI_ADDR_MASK);
128 bios_dir_addr = FLASH_BASE_ADDR + (bios_dir_addr & SPI_ADDR_MASK);
129 }
130
Kangheui Won26bb4aa2021-10-18 15:31:45 +1100131 if (update_psp_bios_dir(&psp_dir_addr, &bios_dir_addr)) {
Martin Rothc7acf162020-05-28 00:44:50 -0600132 printk(BIOS_ERR, "Error: Updated BIOS Directory could not be set.\n");
133 return POSTCODE_UPDATE_PSP_BIOS_DIR_ERROR;
134 }
135
136 return 0;
137}
138
139/*
140 * Save workbuf (and soon memory console and timestamps) to the bootloader to pass
141 * back to coreboot.
142 */
143static uint32_t save_buffers(struct vb2_context **ctx)
144{
145 uint32_t retval;
Martin Roth0c12abe2020-06-26 08:40:56 -0600146 uint32_t buffer_size = MIN_TRANSFER_BUFFER_SIZE;
Martin Rothc7acf162020-05-28 00:44:50 -0600147 uint32_t max_buffer_size;
Martin Roth0c12abe2020-06-26 08:40:56 -0600148 struct transfer_info_struct buffer_info = {0};
Martin Rothc7acf162020-05-28 00:44:50 -0600149
150 /*
Kangheui Won695732b2021-04-25 12:11:17 +1000151 * This should never fail on picasso, but if it does, we should still
152 * try to save the buffer. If that fails, then we should go to
153 * recovery mode.
Martin Rothc7acf162020-05-28 00:44:50 -0600154 */
Kangheui Won695732b2021-04-25 12:11:17 +1000155 if (get_max_workbuf_size(&max_buffer_size)) {
Martin Rothc7acf162020-05-28 00:44:50 -0600156 post_code(POSTCODE_DEFAULT_BUFFER_SIZE_NOTICE);
157 printk(BIOS_NOTICE, "Notice: using default transfer buffer size.\n");
Martin Roth0c12abe2020-06-26 08:40:56 -0600158 max_buffer_size = MIN_TRANSFER_BUFFER_SIZE;
Martin Rothc7acf162020-05-28 00:44:50 -0600159 }
160 printk(BIOS_DEBUG, "\nMaximum buffer size: %d bytes\n", max_buffer_size);
161
Martin Roth0c12abe2020-06-26 08:40:56 -0600162 /* Shrink workbuf if MP2 is in use and cannot be used to save buffer */
163 if (max_buffer_size < VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE) {
164 retval = vb2api_relocate(_vboot2_work, _vboot2_work, MIN_WORKBUF_TRANSFER_SIZE,
165 ctx);
166 if (retval != VB2_SUCCESS) {
167 printk(BIOS_ERR, "Error shrinking workbuf. Error code %#x\n", retval);
168 buffer_size = VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE;
169 post_code(POSTCODE_WORKBUF_RESIZE_WARNING);
170 }
171 } else {
172 buffer_size =
173 (uint32_t)((uintptr_t)_etransfer_buffer - (uintptr_t)_transfer_buffer);
174
175 buffer_info.console_offset = (uint32_t)((uintptr_t)_preram_cbmem_console -
176 (uintptr_t)_transfer_buffer);
177 buffer_info.timestamp_offset = (uint32_t)((uintptr_t)_timestamp -
178 (uintptr_t)_transfer_buffer);
179 buffer_info.fmap_offset = (uint32_t)((uintptr_t)_fmap_cache -
180 (uintptr_t)_transfer_buffer);
Martin Rothc7acf162020-05-28 00:44:50 -0600181 }
182
183 if (buffer_size > max_buffer_size) {
Martin Roth0c12abe2020-06-26 08:40:56 -0600184 printk(BIOS_ERR, "Error: Buffer is larger than max buffer size.\n");
Martin Rothc7acf162020-05-28 00:44:50 -0600185 post_code(POSTCODE_WORKBUF_BUFFER_SIZE_ERROR);
186 return POSTCODE_WORKBUF_BUFFER_SIZE_ERROR;
187 }
188
Martin Roth0c12abe2020-06-26 08:40:56 -0600189 buffer_info.magic_val = TRANSFER_MAGIC_VAL;
190 buffer_info.struct_bytes = sizeof(buffer_info);
191 buffer_info.buffer_size = buffer_size;
192 buffer_info.workbuf_offset = (uint32_t)((uintptr_t)_fmap_cache -
193 (uintptr_t)_vboot2_work);
194
Kangheui Won5f027fa2020-08-25 18:12:19 +1000195 memcpy(_transfer_buffer, &buffer_info, sizeof(buffer_info));
196
Kangheui Wona767eb42021-04-14 09:35:28 +1000197 retval = save_uapp_data((void *)_transfer_buffer, buffer_size);
Martin Rothc7acf162020-05-28 00:44:50 -0600198 if (retval) {
Martin Roth0c12abe2020-06-26 08:40:56 -0600199 printk(BIOS_ERR, "Error: Could not save workbuf. Error code 0x%08x\n", retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600200 return POSTCODE_WORKBUF_SAVE_ERROR;
201 }
202
203 return 0;
204}
205
206void Main(void)
207{
208 uint32_t retval;
209 struct vb2_context *ctx = NULL;
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -0600210 void *boot_dev_base;
Martin Rothc7acf162020-05-28 00:44:50 -0600211
212 /*
213 * Do not use printk() before console_init()
214 * Do not use post_code() before verstage_mainboard_init()
215 */
Kangheui Won4e2f5fd2020-09-17 16:37:13 +1000216 timestamp_init(timestamp_get());
Martin Rothc7acf162020-05-28 00:44:50 -0600217 svc_write_postcode(POSTCODE_ENTERED_PSP_VERSTAGE);
218 svc_debug_print("Entering verstage on PSP\n");
219 memset(&_bss_start, '\0', &_bss_end - &_bss_start);
220
221 svc_write_postcode(POSTCODE_CONSOLE_INIT);
222 console_init();
223
224 svc_write_postcode(POSTCODE_EARLY_INIT);
225 retval = verstage_soc_early_init();
226 if (retval) {
Rob Barnesc30a1fa2021-11-08 06:43:07 -0700227 /*
228 * If verstage_soc_early_init fails, cmos is probably not
229 * accessible, so rebooting into recovery is not an option.
230 * Just reboot and hope for the best.
231 */
232 svc_write_postcode(POSTCODE_EARLY_INIT_ERROR);
233 svc_debug_print("verstage_soc_early_init failed! -- rebooting\n");
234 vboot_reboot();
Martin Rothc7acf162020-05-28 00:44:50 -0600235 }
236 svc_debug_print("calling verstage_mainboard_early_init\n");
237
238 verstage_mainboard_early_init();
239
240 svc_write_postcode(POSTCODE_LATE_INIT);
Felix Held26935d12020-12-08 00:40:04 +0100241 fch_io_enable_legacy_io();
Martin Rothc7acf162020-05-28 00:44:50 -0600242 verstage_soc_init();
243 verstage_mainboard_init();
244
245 post_code(POSTCODE_VERSTAGE_MAIN);
246
Kangheui Wonac7ec272021-01-15 15:04:25 +1100247 vboot_run_logic();
Martin Rothc7acf162020-05-28 00:44:50 -0600248
Martin Rothc9689e02020-08-20 17:25:37 -0600249 ctx = vboot_get_context();
Martin Roth50cca762020-08-13 11:06:18 -0600250 retval = check_cmos_recovery();
251 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600252 reboot_into_recovery(ctx, retval);
Martin Roth0c12abe2020-06-26 08:40:56 -0600253
Kangheui Wonac7ec272021-01-15 15:04:25 +1100254 post_code(POSTCODE_UPDATE_BOOT_REGION);
Kangheui Won97527252021-05-20 10:02:00 +1000255
256 /*
257 * Since psp_verstage doesn't load next stage we never call
258 * any cbfs API on RO path. However we still need to initialize
259 * RO CBFS MCACHE manually to pass it in transfer_buffer.
260 * In RW path, MCACHE build will be skipped for RO region since
261 * we already built here.
262 */
263 cbfs_get_boot_device(true);
264
Kangheui Wonac7ec272021-01-15 15:04:25 +1100265 retval = update_boot_region(ctx);
266 if (retval)
267 reboot_into_recovery(ctx, retval);
268
Martin Rothc7acf162020-05-28 00:44:50 -0600269 post_code(POSTCODE_SAVE_BUFFERS);
270 retval = save_buffers(&ctx);
271 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600272 reboot_into_recovery(ctx, retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600273
Martin Rothc7acf162020-05-28 00:44:50 -0600274
275 post_code(POSTCODE_UNMAP_SPI_ROM);
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -0600276 boot_dev_base = rdev_mmap_full(boot_device_ro());
277 if (boot_dev_base) {
278 if (svc_unmap_spi_rom((void *)boot_dev_base))
Martin Rothc7acf162020-05-28 00:44:50 -0600279 printk(BIOS_ERR, "Error unmapping SPI rom\n");
280 }
281
282 post_code(POSTCODE_UNMAP_FCH_DEVICES);
283 unmap_fch_devices();
284
285 post_code(POSTCODE_LEAVING_VERSTAGE);
286
287 printk(BIOS_DEBUG, "Leaving verstage on PSP\n");
288 svc_exit(retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600289}
290
Martin Rothc7acf162020-05-28 00:44:50 -0600291/*
292 * The stage_entry function is not used directly, but stage_entry() is marked as an entry
293 * point in arm/arch/header.h, so if stage_entry() isn't present and calling Main(), all
294 * the verstage code gets dropped by the linker. Slightly hacky, but mostly harmless.
295 */
296void stage_entry(uintptr_t stage_arg)
297{
298 Main();
299}