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> |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 9 | #include <console/console.h> |
Martin Roth | e21698b | 2020-06-26 08:55:15 -0600 | [diff] [blame] | 10 | #include <fmap.h> |
Martin Roth | 50cca76 | 2020-08-13 11:06:18 -0600 | [diff] [blame] | 11 | #include <pc80/mc146818rtc.h> |
Martin Roth | 0c12abe | 2020-06-26 08:40:56 -0600 | [diff] [blame] | 12 | #include <soc/psp_transfer.h> |
Martin Roth | 50cca76 | 2020-08-13 11:06:18 -0600 | [diff] [blame] | 13 | #include <security/vboot/vbnv.h> |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 14 | #include <security/vboot/misc.h> |
| 15 | #include <security/vboot/symbols.h> |
| 16 | #include <security/vboot/vboot_common.h> |
| 17 | #include <arch/stages.h> |
| 18 | #include <stdarg.h> |
| 19 | #include <stdio.h> |
Kangheui Won | 4e2f5fd | 2020-09-17 16:37:13 +1000 | [diff] [blame] | 20 | #include <timestamp.h> |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 21 | |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 22 | extern char _bss_start, _bss_end; |
| 23 | static struct mem_region_device boot_dev = |
| 24 | MEM_REGION_DEV_RO_INIT(NULL, CONFIG_ROM_SIZE); |
| 25 | |
| 26 | void __weak verstage_mainboard_early_init(void) {} |
| 27 | void __weak verstage_mainboard_init(void) {} |
| 28 | |
| 29 | static void reboot_into_recovery(struct vb2_context *ctx, uint32_t subcode) |
| 30 | { |
| 31 | subcode += PSP_VBOOT_ERROR_SUBCODE; |
| 32 | svc_write_postcode(subcode); |
| 33 | |
Martin Roth | c9689e0 | 2020-08-20 17:25:37 -0600 | [diff] [blame] | 34 | /* |
| 35 | * If there's an error but the PSP_verstage is already booting to RO, |
| 36 | * don't reset the system. It may be that the error is fatal, but if |
| 37 | * the system is stuck, don't intentionally force it into a reboot loop. |
| 38 | */ |
| 39 | if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) { |
| 40 | printk(BIOS_ERR, "Already in recovery mode. Staying in RO.\n"); |
| 41 | return; |
| 42 | } |
| 43 | |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 44 | vb2api_fail(ctx, VB2_RECOVERY_RO_UNSPECIFIED, (int)subcode); |
| 45 | vboot_save_data(ctx); |
| 46 | |
| 47 | svc_debug_print("Rebooting into recovery\n"); |
| 48 | vboot_reboot(); |
| 49 | } |
| 50 | |
Martin Roth | 50cca76 | 2020-08-13 11:06:18 -0600 | [diff] [blame] | 51 | static uint32_t check_cmos_recovery(void) |
| 52 | { |
| 53 | /* Only reset if cmos is valid */ |
| 54 | if (vbnv_cmos_failed()) |
| 55 | return 0; |
| 56 | |
| 57 | /* If the byte is set, clear it, then return error to reboot */ |
| 58 | if (cmos_read(CMOS_RECOVERY_BYTE) == CMOS_RECOVERY_MAGIC_VAL) { |
| 59 | cmos_write(0x00, CMOS_RECOVERY_BYTE); |
| 60 | printk(BIOS_DEBUG, "Reboot into recovery requested by coreboot\n"); |
| 61 | return POSTCODE_CMOS_RECOVERY; |
| 62 | } |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 67 | /* |
| 68 | * Tell the PSP where to load the rest of the firmware from |
| 69 | */ |
| 70 | static uint32_t update_boot_region(struct vb2_context *ctx) |
| 71 | { |
| 72 | struct psp_ef_table *ef_table; |
| 73 | uint32_t psp_dir_addr, bios_dir_addr; |
| 74 | uint32_t *psp_dir_in_spi, *bios_dir_in_spi; |
Kangheui Won | ac7ec27 | 2021-01-15 15:04:25 +1100 | [diff] [blame^] | 75 | const char *fname; |
| 76 | void *amdfw_location; |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 77 | |
| 78 | /* Continue booting from RO */ |
| 79 | if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) { |
| 80 | printk(BIOS_ERR, "In recovery mode. Staying in RO.\n"); |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | if (vboot_is_firmware_slot_a(ctx)) { |
Martin Roth | e21698b | 2020-06-26 08:55:15 -0600 | [diff] [blame] | 85 | fname = "apu/amdfw_a"; |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 86 | } else { |
Martin Roth | e21698b | 2020-06-26 08:55:15 -0600 | [diff] [blame] | 87 | fname = "apu/amdfw_b"; |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 88 | } |
| 89 | |
Kangheui Won | ac7ec27 | 2021-01-15 15:04:25 +1100 | [diff] [blame^] | 90 | amdfw_location = cbfs_map(fname, NULL); |
Martin Roth | e21698b | 2020-06-26 08:55:15 -0600 | [diff] [blame] | 91 | if (!amdfw_location) { |
| 92 | printk(BIOS_ERR, "Error: AMD Firmware table not found.\n"); |
| 93 | return POSTCODE_AMD_FW_MISSING; |
| 94 | } |
| 95 | ef_table = (struct psp_ef_table *)amdfw_location; |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 96 | if (ef_table->signature != EMBEDDED_FW_SIGNATURE) { |
| 97 | printk(BIOS_ERR, "Error: ROMSIG address is not correct.\n"); |
| 98 | return POSTCODE_ROMSIG_MISMATCH_ERROR; |
| 99 | } |
| 100 | |
| 101 | psp_dir_addr = ef_table->psp_table; |
| 102 | bios_dir_addr = ef_table->bios1_entry; |
| 103 | psp_dir_in_spi = (uint32_t *)((psp_dir_addr & SPI_ADDR_MASK) + |
| 104 | (uint32_t)boot_dev.base); |
| 105 | bios_dir_in_spi = (uint32_t *)((bios_dir_addr & SPI_ADDR_MASK) + |
| 106 | (uint32_t)boot_dev.base); |
| 107 | if (*psp_dir_in_spi != PSP_COOKIE) { |
| 108 | printk(BIOS_ERR, "Error: PSP Directory address is not correct.\n"); |
| 109 | return POSTCODE_PSP_COOKIE_MISMATCH_ERROR; |
| 110 | } |
| 111 | if (*bios_dir_in_spi != BDT1_COOKIE) { |
| 112 | printk(BIOS_ERR, "Error: BIOS Directory address is not correct.\n"); |
| 113 | return POSTCODE_BDT1_COOKIE_MISMATCH_ERROR; |
| 114 | } |
| 115 | |
| 116 | if (svc_update_psp_bios_dir((void *)&psp_dir_addr, |
| 117 | (void *)&bios_dir_addr, DIR_OFFSET_SET)) { |
| 118 | printk(BIOS_ERR, "Error: Updated BIOS Directory could not be set.\n"); |
| 119 | return POSTCODE_UPDATE_PSP_BIOS_DIR_ERROR; |
| 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Save workbuf (and soon memory console and timestamps) to the bootloader to pass |
| 127 | * back to coreboot. |
| 128 | */ |
| 129 | static uint32_t save_buffers(struct vb2_context **ctx) |
| 130 | { |
| 131 | uint32_t retval; |
Martin Roth | 0c12abe | 2020-06-26 08:40:56 -0600 | [diff] [blame] | 132 | uint32_t buffer_size = MIN_TRANSFER_BUFFER_SIZE; |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 133 | uint32_t max_buffer_size; |
Martin Roth | 0c12abe | 2020-06-26 08:40:56 -0600 | [diff] [blame] | 134 | struct transfer_info_struct buffer_info = {0}; |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 135 | |
| 136 | /* |
| 137 | * This should never fail, but if it does, we should still try to |
| 138 | * save the buffer. If that fails, then we should go to recovery mode. |
| 139 | */ |
| 140 | if (svc_get_max_workbuf_size(&max_buffer_size)) { |
| 141 | post_code(POSTCODE_DEFAULT_BUFFER_SIZE_NOTICE); |
| 142 | printk(BIOS_NOTICE, "Notice: using default transfer buffer size.\n"); |
Martin Roth | 0c12abe | 2020-06-26 08:40:56 -0600 | [diff] [blame] | 143 | max_buffer_size = MIN_TRANSFER_BUFFER_SIZE; |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 144 | } |
| 145 | printk(BIOS_DEBUG, "\nMaximum buffer size: %d bytes\n", max_buffer_size); |
| 146 | |
Martin Roth | 0c12abe | 2020-06-26 08:40:56 -0600 | [diff] [blame] | 147 | /* Shrink workbuf if MP2 is in use and cannot be used to save buffer */ |
| 148 | if (max_buffer_size < VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE) { |
| 149 | retval = vb2api_relocate(_vboot2_work, _vboot2_work, MIN_WORKBUF_TRANSFER_SIZE, |
| 150 | ctx); |
| 151 | if (retval != VB2_SUCCESS) { |
| 152 | printk(BIOS_ERR, "Error shrinking workbuf. Error code %#x\n", retval); |
| 153 | buffer_size = VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE; |
| 154 | post_code(POSTCODE_WORKBUF_RESIZE_WARNING); |
| 155 | } |
| 156 | } else { |
| 157 | buffer_size = |
| 158 | (uint32_t)((uintptr_t)_etransfer_buffer - (uintptr_t)_transfer_buffer); |
| 159 | |
| 160 | buffer_info.console_offset = (uint32_t)((uintptr_t)_preram_cbmem_console - |
| 161 | (uintptr_t)_transfer_buffer); |
| 162 | buffer_info.timestamp_offset = (uint32_t)((uintptr_t)_timestamp - |
| 163 | (uintptr_t)_transfer_buffer); |
| 164 | buffer_info.fmap_offset = (uint32_t)((uintptr_t)_fmap_cache - |
| 165 | (uintptr_t)_transfer_buffer); |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | if (buffer_size > max_buffer_size) { |
Martin Roth | 0c12abe | 2020-06-26 08:40:56 -0600 | [diff] [blame] | 169 | printk(BIOS_ERR, "Error: Buffer is larger than max buffer size.\n"); |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 170 | post_code(POSTCODE_WORKBUF_BUFFER_SIZE_ERROR); |
| 171 | return POSTCODE_WORKBUF_BUFFER_SIZE_ERROR; |
| 172 | } |
| 173 | |
Martin Roth | 0c12abe | 2020-06-26 08:40:56 -0600 | [diff] [blame] | 174 | buffer_info.magic_val = TRANSFER_MAGIC_VAL; |
| 175 | buffer_info.struct_bytes = sizeof(buffer_info); |
| 176 | buffer_info.buffer_size = buffer_size; |
| 177 | buffer_info.workbuf_offset = (uint32_t)((uintptr_t)_fmap_cache - |
| 178 | (uintptr_t)_vboot2_work); |
| 179 | |
Kangheui Won | 5f027fa | 2020-08-25 18:12:19 +1000 | [diff] [blame] | 180 | memcpy(_transfer_buffer, &buffer_info, sizeof(buffer_info)); |
| 181 | |
Martin Roth | 0c12abe | 2020-06-26 08:40:56 -0600 | [diff] [blame] | 182 | retval = svc_save_uapp_data(UAPP_COPYBUF_CHROME_WORKBUF, (void *)_transfer_buffer, |
| 183 | buffer_size); |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 184 | if (retval) { |
Martin Roth | 0c12abe | 2020-06-26 08:40:56 -0600 | [diff] [blame] | 185 | 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] | 186 | return POSTCODE_WORKBUF_SAVE_ERROR; |
| 187 | } |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | void Main(void) |
| 193 | { |
| 194 | uint32_t retval; |
| 195 | struct vb2_context *ctx = NULL; |
| 196 | |
| 197 | /* |
| 198 | * Do not use printk() before console_init() |
| 199 | * Do not use post_code() before verstage_mainboard_init() |
| 200 | */ |
Kangheui Won | 4e2f5fd | 2020-09-17 16:37:13 +1000 | [diff] [blame] | 201 | timestamp_init(timestamp_get()); |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 202 | svc_write_postcode(POSTCODE_ENTERED_PSP_VERSTAGE); |
| 203 | svc_debug_print("Entering verstage on PSP\n"); |
| 204 | memset(&_bss_start, '\0', &_bss_end - &_bss_start); |
| 205 | |
| 206 | svc_write_postcode(POSTCODE_CONSOLE_INIT); |
| 207 | console_init(); |
| 208 | |
| 209 | svc_write_postcode(POSTCODE_EARLY_INIT); |
| 210 | retval = verstage_soc_early_init(); |
| 211 | if (retval) { |
| 212 | svc_debug_print("verstage_soc_early_init failed\n"); |
| 213 | reboot_into_recovery(NULL, retval); |
| 214 | } |
| 215 | svc_debug_print("calling verstage_mainboard_early_init\n"); |
| 216 | |
| 217 | verstage_mainboard_early_init(); |
| 218 | |
| 219 | svc_write_postcode(POSTCODE_LATE_INIT); |
Felix Held | 26935d1 | 2020-12-08 00:40:04 +0100 | [diff] [blame] | 220 | fch_io_enable_legacy_io(); |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 221 | verstage_soc_init(); |
| 222 | verstage_mainboard_init(); |
| 223 | |
| 224 | post_code(POSTCODE_VERSTAGE_MAIN); |
| 225 | |
Kangheui Won | ac7ec27 | 2021-01-15 15:04:25 +1100 | [diff] [blame^] | 226 | vboot_run_logic(); |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 227 | |
Martin Roth | c9689e0 | 2020-08-20 17:25:37 -0600 | [diff] [blame] | 228 | ctx = vboot_get_context(); |
Martin Roth | 50cca76 | 2020-08-13 11:06:18 -0600 | [diff] [blame] | 229 | retval = check_cmos_recovery(); |
| 230 | if (retval) |
Martin Roth | c9689e0 | 2020-08-20 17:25:37 -0600 | [diff] [blame] | 231 | reboot_into_recovery(ctx, retval); |
Martin Roth | 0c12abe | 2020-06-26 08:40:56 -0600 | [diff] [blame] | 232 | |
Kangheui Won | ac7ec27 | 2021-01-15 15:04:25 +1100 | [diff] [blame^] | 233 | post_code(POSTCODE_UPDATE_BOOT_REGION); |
| 234 | retval = update_boot_region(ctx); |
| 235 | if (retval) |
| 236 | reboot_into_recovery(ctx, retval); |
| 237 | |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 238 | post_code(POSTCODE_SAVE_BUFFERS); |
| 239 | retval = save_buffers(&ctx); |
| 240 | if (retval) |
Martin Roth | c9689e0 | 2020-08-20 17:25:37 -0600 | [diff] [blame] | 241 | reboot_into_recovery(ctx, retval); |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 242 | |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 243 | |
| 244 | post_code(POSTCODE_UNMAP_SPI_ROM); |
| 245 | if (boot_dev.base) { |
| 246 | if (svc_unmap_spi_rom((void *)boot_dev.base)) |
| 247 | printk(BIOS_ERR, "Error unmapping SPI rom\n"); |
| 248 | } |
| 249 | |
| 250 | post_code(POSTCODE_UNMAP_FCH_DEVICES); |
| 251 | unmap_fch_devices(); |
| 252 | |
| 253 | post_code(POSTCODE_LEAVING_VERSTAGE); |
| 254 | |
| 255 | printk(BIOS_DEBUG, "Leaving verstage on PSP\n"); |
| 256 | svc_exit(retval); |
Martin Roth | c7acf16 | 2020-05-28 00:44:50 -0600 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | const struct region_device *boot_device_ro(void) |
| 260 | { |
| 261 | uintptr_t *addr; |
| 262 | |
| 263 | addr = map_spi_rom(); |
| 264 | mem_region_device_ro_init(&boot_dev, (void *)addr, CONFIG_ROM_SIZE); |
| 265 | |
| 266 | return &boot_dev.rdev; |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * The stage_entry function is not used directly, but stage_entry() is marked as an entry |
| 271 | * point in arm/arch/header.h, so if stage_entry() isn't present and calling Main(), all |
| 272 | * the verstage code gets dropped by the linker. Slightly hacky, but mostly harmless. |
| 273 | */ |
| 274 | void stage_entry(uintptr_t stage_arg) |
| 275 | { |
| 276 | Main(); |
| 277 | } |