Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #include "psp_verstage.h" |
| 4 | |
Felix Held | 26935d1 | 2020-12-08 00:40:04 +0100 | [diff] [blame] | 5 | #include <amdblocks/acpimmio.h> |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 6 | #include <bl_uapp/bl_syscall_public.h> |
| 7 | #include <boot_device.h> |
Martin Roth | e21698b | 2020-06-26 08:55:15 -0600 | [diff] [blame] | 8 | #include <cbfs.h> |
Karthikeyan Ramasubramanian | c2f6f35 | 2021-09-10 12:03:30 -0600 | [diff] [blame] | 9 | #include <commonlib/region.h> |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 10 | #include <console/console.h> |
Martin Roth | e21698b | 2020-06-26 08:55:15 -0600 | [diff] [blame] | 11 | #include <fmap.h> |
Martin Roth | 50cca76 | 2020-08-13 11:06:18 -0600 | [diff] [blame] | 12 | #include <pc80/mc146818rtc.h> |
Martin Roth | 0c12abe | 2020-06-26 08:40:56 -0600 | [diff] [blame] | 13 | #include <soc/psp_transfer.h> |
Martin Roth | 50cca76 | 2020-08-13 11:06:18 -0600 | [diff] [blame] | 14 | #include <security/vboot/vbnv.h> |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 15 | #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 Won | 4e2f5fd | 2020-09-17 16:37:13 +1000 | [diff] [blame] | 21 | #include <timestamp.h> |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 22 | |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 23 | extern char _bss_start, _bss_end; |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 24 | |
| 25 | void __weak verstage_mainboard_early_init(void) {} |
| 26 | void __weak verstage_mainboard_init(void) {} |
Kangheui Won | 695732b | 2021-04-25 12:11:17 +1000 | [diff] [blame] | 27 | uint32_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 Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 35 | |
| 36 | static 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 Roth | c9689e0 | 2020-08-20 17:25:37 -0600 | [diff] [blame] | 41 | /* |
| 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 Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 51 | 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 Roth | 50cca76 | 2020-08-13 11:06:18 -0600 | [diff] [blame] | 58 | static 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 Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 74 | /* |
| 75 | * Tell the PSP where to load the rest of the firmware from |
| 76 | */ |
| 77 | static uint32_t update_boot_region(struct vb2_context *ctx) |
| 78 | { |
Kangheui Won | 26bb4aa | 2021-10-18 15:31:45 +1100 | [diff] [blame^] | 79 | struct embedded_firmware *ef_table; |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 80 | uint32_t psp_dir_addr, bios_dir_addr; |
| 81 | uint32_t *psp_dir_in_spi, *bios_dir_in_spi; |
Kangheui Won | ac7ec27 | 2021-01-15 15:04:25 +1100 | [diff] [blame] | 82 | const char *fname; |
| 83 | void *amdfw_location; |
Karthikeyan Ramasubramanian | c2f6f35 | 2021-09-10 12:03:30 -0600 | [diff] [blame] | 84 | void *boot_dev_base = rdev_mmap_full(boot_device_ro()); |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 85 | |
| 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 Roth | e21698b | 2020-06-26 08:55:15 -0600 | [diff] [blame] | 93 | fname = "apu/amdfw_a"; |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 94 | } else { |
Martin Roth | e21698b | 2020-06-26 08:55:15 -0600 | [diff] [blame] | 95 | fname = "apu/amdfw_b"; |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 96 | } |
| 97 | |
Kangheui Won | ac7ec27 | 2021-01-15 15:04:25 +1100 | [diff] [blame] | 98 | amdfw_location = cbfs_map(fname, NULL); |
Martin Roth | e21698b | 2020-06-26 08:55:15 -0600 | [diff] [blame] | 99 | if (!amdfw_location) { |
| 100 | printk(BIOS_ERR, "Error: AMD Firmware table not found.\n"); |
| 101 | return POSTCODE_AMD_FW_MISSING; |
| 102 | } |
Kangheui Won | 26bb4aa | 2021-10-18 15:31:45 +1100 | [diff] [blame^] | 103 | ef_table = (struct embedded_firmware *)amdfw_location; |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 104 | 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 Won | 26bb4aa | 2021-10-18 15:31:45 +1100 | [diff] [blame^] | 109 | psp_dir_addr = ef_table->combo_psp_directory; |
Kangheui Won | 5858fb4 | 2021-05-06 13:30:51 +1000 | [diff] [blame] | 110 | bios_dir_addr = get_bios_dir_addr(ef_table); |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 111 | psp_dir_in_spi = (uint32_t *)((psp_dir_addr & SPI_ADDR_MASK) + |
Karthikeyan Ramasubramanian | c2f6f35 | 2021-09-10 12:03:30 -0600 | [diff] [blame] | 112 | (uint32_t)boot_dev_base); |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 113 | bios_dir_in_spi = (uint32_t *)((bios_dir_addr & SPI_ADDR_MASK) + |
Karthikeyan Ramasubramanian | c2f6f35 | 2021-09-10 12:03:30 -0600 | [diff] [blame] | 114 | (uint32_t)boot_dev_base); |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 115 | 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 Won | 26bb4aa | 2021-10-18 15:31:45 +1100 | [diff] [blame^] | 124 | if (update_psp_bios_dir(&psp_dir_addr, &bios_dir_addr)) { |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 125 | 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 | */ |
| 136 | static uint32_t save_buffers(struct vb2_context **ctx) |
| 137 | { |
| 138 | uint32_t retval; |
Martin Roth | 0c12abe | 2020-06-26 08:40:56 -0600 | [diff] [blame] | 139 | uint32_t buffer_size = MIN_TRANSFER_BUFFER_SIZE; |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 140 | uint32_t max_buffer_size; |
Martin Roth | 0c12abe | 2020-06-26 08:40:56 -0600 | [diff] [blame] | 141 | struct transfer_info_struct buffer_info = {0}; |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 142 | |
| 143 | /* |
Kangheui Won | 695732b | 2021-04-25 12:11:17 +1000 | [diff] [blame] | 144 | * 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 Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 147 | */ |
Kangheui Won | 695732b | 2021-04-25 12:11:17 +1000 | [diff] [blame] | 148 | if (get_max_workbuf_size(&max_buffer_size)) { |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 149 | post_code(POSTCODE_DEFAULT_BUFFER_SIZE_NOTICE); |
| 150 | printk(BIOS_NOTICE, "Notice: using default transfer buffer size.\n"); |
Martin Roth | 0c12abe | 2020-06-26 08:40:56 -0600 | [diff] [blame] | 151 | max_buffer_size = MIN_TRANSFER_BUFFER_SIZE; |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 152 | } |
| 153 | printk(BIOS_DEBUG, "\nMaximum buffer size: %d bytes\n", max_buffer_size); |
| 154 | |
Martin Roth | 0c12abe | 2020-06-26 08:40:56 -0600 | [diff] [blame] | 155 | /* 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 Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | if (buffer_size > max_buffer_size) { |
Martin Roth | 0c12abe | 2020-06-26 08:40:56 -0600 | [diff] [blame] | 177 | printk(BIOS_ERR, "Error: Buffer is larger than max buffer size.\n"); |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 178 | post_code(POSTCODE_WORKBUF_BUFFER_SIZE_ERROR); |
| 179 | return POSTCODE_WORKBUF_BUFFER_SIZE_ERROR; |
| 180 | } |
| 181 | |
Martin Roth | 0c12abe | 2020-06-26 08:40:56 -0600 | [diff] [blame] | 182 | 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 Won | 5f027fa | 2020-08-25 18:12:19 +1000 | [diff] [blame] | 188 | memcpy(_transfer_buffer, &buffer_info, sizeof(buffer_info)); |
| 189 | |
Kangheui Won | a767eb4 | 2021-04-14 09:35:28 +1000 | [diff] [blame] | 190 | retval = save_uapp_data((void *)_transfer_buffer, buffer_size); |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 191 | if (retval) { |
Martin Roth | 0c12abe | 2020-06-26 08:40:56 -0600 | [diff] [blame] | 192 | printk(BIOS_ERR, "Error: Could not save workbuf. Error code 0x%08x\n", retval); |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 193 | return POSTCODE_WORKBUF_SAVE_ERROR; |
| 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | void Main(void) |
| 200 | { |
| 201 | uint32_t retval; |
| 202 | struct vb2_context *ctx = NULL; |
Karthikeyan Ramasubramanian | c2f6f35 | 2021-09-10 12:03:30 -0600 | [diff] [blame] | 203 | void *boot_dev_base; |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 204 | |
| 205 | /* |
| 206 | * Do not use printk() before console_init() |
| 207 | * Do not use post_code() before verstage_mainboard_init() |
| 208 | */ |
Kangheui Won | 4e2f5fd | 2020-09-17 16:37:13 +1000 | [diff] [blame] | 209 | timestamp_init(timestamp_get()); |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 210 | 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 Held | 26935d1 | 2020-12-08 00:40:04 +0100 | [diff] [blame] | 228 | fch_io_enable_legacy_io(); |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 229 | verstage_soc_init(); |
| 230 | verstage_mainboard_init(); |
| 231 | |
| 232 | post_code(POSTCODE_VERSTAGE_MAIN); |
| 233 | |
Kangheui Won | ac7ec27 | 2021-01-15 15:04:25 +1100 | [diff] [blame] | 234 | vboot_run_logic(); |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 235 | |
Martin Roth | c9689e0 | 2020-08-20 17:25:37 -0600 | [diff] [blame] | 236 | ctx = vboot_get_context(); |
Martin Roth | 50cca76 | 2020-08-13 11:06:18 -0600 | [diff] [blame] | 237 | retval = check_cmos_recovery(); |
| 238 | if (retval) |
Martin Roth | c9689e0 | 2020-08-20 17:25:37 -0600 | [diff] [blame] | 239 | reboot_into_recovery(ctx, retval); |
Martin Roth | 0c12abe | 2020-06-26 08:40:56 -0600 | [diff] [blame] | 240 | |
Kangheui Won | ac7ec27 | 2021-01-15 15:04:25 +1100 | [diff] [blame] | 241 | post_code(POSTCODE_UPDATE_BOOT_REGION); |
Kangheui Won | 9752725 | 2021-05-20 10:02:00 +1000 | [diff] [blame] | 242 | |
| 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 Won | ac7ec27 | 2021-01-15 15:04:25 +1100 | [diff] [blame] | 252 | retval = update_boot_region(ctx); |
| 253 | if (retval) |
| 254 | reboot_into_recovery(ctx, retval); |
| 255 | |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 256 | post_code(POSTCODE_SAVE_BUFFERS); |
| 257 | retval = save_buffers(&ctx); |
| 258 | if (retval) |
Martin Roth | c9689e0 | 2020-08-20 17:25:37 -0600 | [diff] [blame] | 259 | reboot_into_recovery(ctx, retval); |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 260 | |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 261 | |
| 262 | post_code(POSTCODE_UNMAP_SPI_ROM); |
Karthikeyan Ramasubramanian | c2f6f35 | 2021-09-10 12:03:30 -0600 | [diff] [blame] | 263 | 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 Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 266 | 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 Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 276 | } |
| 277 | |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 278 | /* |
| 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 | */ |
| 283 | void stage_entry(uintptr_t stage_arg) |
| 284 | { |
| 285 | Main(); |
| 286 | } |