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