blob: 7d9ef35d5876b6d24cc47d50dc4082918d21d970 [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>
Karthikeyan Ramasubramanian0822ce82022-12-05 14:54:53 -07006#include <bl_uapp/bl_errorcodes_public.h>
Martin Rothc7acf162020-05-28 00:44:50 -06007#include <bl_uapp/bl_syscall_public.h>
8#include <boot_device.h>
Martin Rothe21698b2020-06-26 08:55:15 -06009#include <cbfs.h>
Karthikeyan Ramasubramanianc2f6f352021-09-10 12:03:30 -060010#include <commonlib/region.h>
Martin Rothc7acf162020-05-28 00:44:50 -060011#include <console/console.h>
Martin Rothe21698b2020-06-26 08:55:15 -060012#include <fmap.h>
Karthikeyan Ramasubramanian6f1b03b2023-04-21 14:26:51 -060013#include <fmap_config.h>
Martin Roth50cca762020-08-13 11:06:18 -060014#include <pc80/mc146818rtc.h>
Martin Roth8fc68812023-08-18 16:28:29 -060015#include <psp_verstage/psp_transfer.h>
Kangheui Wonfab6e442021-10-18 15:35:28 +110016#include <soc/iomap.h>
Rob Barnesb35acf92021-11-02 17:47:47 -060017#include <security/tpm/tspi.h>
18#include <security/tpm/tss.h>
Martin Roth50cca762020-08-13 11:06:18 -060019#include <security/vboot/vbnv.h>
Martin Rothc7acf162020-05-28 00:44:50 -060020#include <security/vboot/misc.h>
21#include <security/vboot/symbols.h>
22#include <security/vboot/vboot_common.h>
23#include <arch/stages.h>
24#include <stdarg.h>
25#include <stdio.h>
Kangheui Won4e2f5fd2020-09-17 16:37:13 +100026#include <timestamp.h>
Martin Rothc7acf162020-05-28 00:44:50 -060027
Martin Rothc7acf162020-05-28 00:44:50 -060028extern char _bss_start, _bss_end;
Martin Rothc7acf162020-05-28 00:44:50 -060029
Martin Rothc7acf162020-05-28 00:44:50 -060030void __weak verstage_mainboard_init(void) {}
Rob Barnes188be6b2021-11-09 13:21:28 -070031
Martin Rothc7acf162020-05-28 00:44:50 -060032static void reboot_into_recovery(struct vb2_context *ctx, uint32_t subcode)
33{
34 subcode += PSP_VBOOT_ERROR_SUBCODE;
35 svc_write_postcode(subcode);
36
Martin Rothc9689e02020-08-20 17:25:37 -060037 /*
38 * If there's an error but the PSP_verstage is already booting to RO,
39 * don't reset the system. It may be that the error is fatal, but if
40 * the system is stuck, don't intentionally force it into a reboot loop.
41 */
42 if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) {
43 printk(BIOS_ERR, "Already in recovery mode. Staying in RO.\n");
44 return;
45 }
46
Martin Rothc7acf162020-05-28 00:44:50 -060047 svc_debug_print("Rebooting into recovery\n");
Jakub Czapiga605f7932022-11-04 12:18:04 +000048 vboot_fail_and_reboot(ctx, VB2_RECOVERY_RO_UNSPECIFIED, (int)subcode);
Martin Rothc7acf162020-05-28 00:44:50 -060049}
50
Martin Roth50cca762020-08-13 11:06:18 -060051static 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 Rothc7acf162020-05-28 00:44:50 -060067/*
68 * Tell the PSP where to load the rest of the firmware from
69 */
70static uint32_t update_boot_region(struct vb2_context *ctx)
71{
Kangheui Won26bb4aa2021-10-18 15:31:45 +110072 struct embedded_firmware *ef_table;
Martin Rothc7acf162020-05-28 00:44:50 -060073 uint32_t psp_dir_addr, bios_dir_addr;
74 uint32_t *psp_dir_in_spi, *bios_dir_in_spi;
Kangheui Wonac7ec272021-01-15 15:04:25 +110075 const char *fname;
76 void *amdfw_location;
Karthikeyan Ramasubramanian1da03402023-05-25 15:49:29 -060077 struct region fw_slot;
Karthikeyan Ramasubramanian6f1b03b2023-04-21 14:26:51 -060078 void *map_base = NULL;
Martin Rothc7acf162020-05-28 00:44:50 -060079
80 /* Continue booting from RO */
81 if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) {
82 printk(BIOS_ERR, "In recovery mode. Staying in RO.\n");
83 return 0;
84 }
85
86 if (vboot_is_firmware_slot_a(ctx)) {
Martin Rothe21698b2020-06-26 08:55:15 -060087 fname = "apu/amdfw_a";
Karthikeyan Ramasubramanian1da03402023-05-25 15:49:29 -060088 if (!fmap_locate_area("FW_MAIN_A", &fw_slot))
89 map_base = rdev_mmap(boot_device_ro(), fw_slot.offset, fw_slot.size);
90
Martin Rothc7acf162020-05-28 00:44:50 -060091 } else {
Martin Rothe21698b2020-06-26 08:55:15 -060092 fname = "apu/amdfw_b";
Karthikeyan Ramasubramanian1da03402023-05-25 15:49:29 -060093 if (!fmap_locate_area("FW_MAIN_B", &fw_slot))
94 map_base = rdev_mmap(boot_device_ro(), fw_slot.offset, fw_slot.size);
Karthikeyan Ramasubramanian6f1b03b2023-04-21 14:26:51 -060095 }
96
97 if (!map_base) {
98 printk(BIOS_ERR, "Failed to map RW FW_MAIN section.\n");
99 return POSTCODE_MAP_SPI_ROM_FAILED;
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) {
Julius Wernere9665952022-01-21 17:06:20 -0800104 printk(BIOS_ERR, "AMD Firmware table not found.\n");
Karthikeyan Ramasubramanian6f1b03b2023-04-21 14:26:51 -0600105 rdev_munmap(boot_device_ro(), map_base);
Martin Rothe21698b2020-06-26 08:55:15 -0600106 return POSTCODE_AMD_FW_MISSING;
107 }
Karthikeyan Ramasubramanian6f1b03b2023-04-21 14:26:51 -0600108
Kangheui Won26bb4aa2021-10-18 15:31:45 +1100109 ef_table = (struct embedded_firmware *)amdfw_location;
Martin Rothc7acf162020-05-28 00:44:50 -0600110 if (ef_table->signature != EMBEDDED_FW_SIGNATURE) {
Julius Wernere9665952022-01-21 17:06:20 -0800111 printk(BIOS_ERR, "ROMSIG address is not correct.\n");
Karthikeyan Ramasubramanian6f1b03b2023-04-21 14:26:51 -0600112 cbfs_unmap(amdfw_location);
Karthikeyan Ramasubramanian01c9dfb2023-05-25 15:54:33 -0600113 rdev_munmap(boot_device_ro(), amdfw_location);
Karthikeyan Ramasubramanian6f1b03b2023-04-21 14:26:51 -0600114 rdev_munmap(boot_device_ro(), map_base);
Martin Rothc7acf162020-05-28 00:44:50 -0600115 return POSTCODE_ROMSIG_MISMATCH_ERROR;
116 }
117
Felix Held4bdea412023-02-17 00:31:43 +0100118 psp_dir_addr = ef_table->new_psp_directory;
Kangheui Won5858fb42021-05-06 13:30:51 +1000119 bios_dir_addr = get_bios_dir_addr(ef_table);
Martin Rothc7acf162020-05-28 00:44:50 -0600120 psp_dir_in_spi = (uint32_t *)((psp_dir_addr & SPI_ADDR_MASK) +
Karthikeyan Ramasubramanian1da03402023-05-25 15:49:29 -0600121 (uint32_t)map_base - fw_slot.offset);
Martin Rothc7acf162020-05-28 00:44:50 -0600122 if (*psp_dir_in_spi != PSP_COOKIE) {
Julius Wernere9665952022-01-21 17:06:20 -0800123 printk(BIOS_ERR, "PSP Directory address is not correct.\n");
Karthikeyan Ramasubramanian6f1b03b2023-04-21 14:26:51 -0600124 cbfs_unmap(amdfw_location);
Karthikeyan Ramasubramanian01c9dfb2023-05-25 15:54:33 -0600125 rdev_munmap(boot_device_ro(), amdfw_location);
Karthikeyan Ramasubramanian6f1b03b2023-04-21 14:26:51 -0600126 rdev_munmap(boot_device_ro(), map_base);
Martin Rothc7acf162020-05-28 00:44:50 -0600127 return POSTCODE_PSP_COOKIE_MISMATCH_ERROR;
128 }
Karthikeyan Ramasubramaniane3eedf72022-07-14 15:37:07 -0600129
130 if (bios_dir_addr) {
131 bios_dir_in_spi = (uint32_t *)((bios_dir_addr & SPI_ADDR_MASK) +
Karthikeyan Ramasubramanian1da03402023-05-25 15:49:29 -0600132 (uint32_t)map_base - fw_slot.offset);
Karthikeyan Ramasubramaniane3eedf72022-07-14 15:37:07 -0600133 if (*bios_dir_in_spi != BHD_COOKIE) {
134 printk(BIOS_ERR, "BIOS Directory address is not correct.\n");
Karthikeyan Ramasubramanian6f1b03b2023-04-21 14:26:51 -0600135 cbfs_unmap(amdfw_location);
Karthikeyan Ramasubramanian01c9dfb2023-05-25 15:54:33 -0600136 rdev_munmap(boot_device_ro(), amdfw_location);
Karthikeyan Ramasubramanian6f1b03b2023-04-21 14:26:51 -0600137 rdev_munmap(boot_device_ro(), map_base);
Karthikeyan Ramasubramaniane3eedf72022-07-14 15:37:07 -0600138 return POSTCODE_BHD_COOKIE_MISMATCH_ERROR;
139 }
Martin Rothc7acf162020-05-28 00:44:50 -0600140 }
141
Kangheui Wonfab6e442021-10-18 15:35:28 +1100142 /* EFS2 uses relative address and PSP isn't happy with that */
Karthikeyan Ramasubramaniane3eedf72022-07-14 15:37:07 -0600143 if (ef_table->efs_gen.gen == EFS_SECOND_GEN &&
144 !CONFIG(PSP_SUPPORTS_EFS2_RELATIVE_ADDR)) {
Kangheui Wonfab6e442021-10-18 15:35:28 +1100145 psp_dir_addr = FLASH_BASE_ADDR + (psp_dir_addr & SPI_ADDR_MASK);
146 bios_dir_addr = FLASH_BASE_ADDR + (bios_dir_addr & SPI_ADDR_MASK);
147 }
148
Kangheui Won26bb4aa2021-10-18 15:31:45 +1100149 if (update_psp_bios_dir(&psp_dir_addr, &bios_dir_addr)) {
Julius Wernere9665952022-01-21 17:06:20 -0800150 printk(BIOS_ERR, "Updated BIOS Directory could not be set.\n");
Karthikeyan Ramasubramanian6f1b03b2023-04-21 14:26:51 -0600151 cbfs_unmap(amdfw_location);
Karthikeyan Ramasubramanian01c9dfb2023-05-25 15:54:33 -0600152 rdev_munmap(boot_device_ro(), amdfw_location);
Karthikeyan Ramasubramanian6f1b03b2023-04-21 14:26:51 -0600153 rdev_munmap(boot_device_ro(), map_base);
Martin Rothc7acf162020-05-28 00:44:50 -0600154 return POSTCODE_UPDATE_PSP_BIOS_DIR_ERROR;
155 }
156
Kangheui Won5fb435a2021-12-22 12:24:17 +1100157 if (CONFIG(SEPARATE_SIGNED_PSPFW))
Karthikeyan Ramasubramanian97e57cf2023-07-17 12:43:14 -0600158 update_psp_fw_hash_tables();
Kangheui Won5fb435a2021-12-22 12:24:17 +1100159
Karthikeyan Ramasubramanian6f1b03b2023-04-21 14:26:51 -0600160 cbfs_unmap(amdfw_location);
Karthikeyan Ramasubramanian01c9dfb2023-05-25 15:54:33 -0600161 rdev_munmap(boot_device_ro(), amdfw_location);
Karthikeyan Ramasubramanian6f1b03b2023-04-21 14:26:51 -0600162 rdev_munmap(boot_device_ro(), map_base);
Martin Rothc7acf162020-05-28 00:44:50 -0600163 return 0;
164}
165
166/*
167 * Save workbuf (and soon memory console and timestamps) to the bootloader to pass
168 * back to coreboot.
169 */
Raul E Rangel5e0ed502022-02-24 10:58:29 -0700170static uint32_t save_buffers(void)
Martin Rothc7acf162020-05-28 00:44:50 -0600171{
172 uint32_t retval;
Raul E Rangel5e0ed502022-02-24 10:58:29 -0700173 uint32_t buffer_size;
Martin Roth0c12abe2020-06-26 08:40:56 -0600174 struct transfer_info_struct buffer_info = {0};
Martin Rothc7acf162020-05-28 00:44:50 -0600175
Raul E Rangel5e0ed502022-02-24 10:58:29 -0700176 buffer_size =
177 (uint32_t)((uintptr_t)_etransfer_buffer - (uintptr_t)_transfer_buffer);
Martin Rothc7acf162020-05-28 00:44:50 -0600178
Raul E Rangel5e0ed502022-02-24 10:58:29 -0700179 buffer_info.console_offset = (uint32_t)((uintptr_t)_preram_cbmem_console -
180 (uintptr_t)_transfer_buffer);
181 buffer_info.timestamp_offset = (uint32_t)((uintptr_t)_timestamp -
182 (uintptr_t)_transfer_buffer);
183 buffer_info.fmap_offset = (uint32_t)((uintptr_t)_fmap_cache -
184 (uintptr_t)_transfer_buffer);
Martin Rothc7acf162020-05-28 00:44:50 -0600185
Martin Roth0c12abe2020-06-26 08:40:56 -0600186 buffer_info.magic_val = TRANSFER_MAGIC_VAL;
187 buffer_info.struct_bytes = sizeof(buffer_info);
188 buffer_info.buffer_size = buffer_size;
189 buffer_info.workbuf_offset = (uint32_t)((uintptr_t)_fmap_cache -
190 (uintptr_t)_vboot2_work);
191
Kangheui Won5f027fa2020-08-25 18:12:19 +1000192 memcpy(_transfer_buffer, &buffer_info, sizeof(buffer_info));
193
Kangheui Wona767eb42021-04-14 09:35:28 +1000194 retval = save_uapp_data((void *)_transfer_buffer, buffer_size);
Martin Rothc7acf162020-05-28 00:44:50 -0600195 if (retval) {
Julius Wernere9665952022-01-21 17:06:20 -0800196 printk(BIOS_ERR, "Could not save workbuf. Error code 0x%08x\n", retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600197 return POSTCODE_WORKBUF_SAVE_ERROR;
198 }
199
200 return 0;
201}
202
Rob Barnesb35acf92021-11-02 17:47:47 -0600203/*
204 * S0i3 resume in PSP verstage is a special case. The FSDL is restoring mostly
205 * everything, so do the minimum necessary here. Unlike normal boot, subsequent
206 * coreboot stages are not run after s0i3 verstage.
207 * If the TPM is reset in S0i3, it must be re-initialized here.
208 */
209static void psp_verstage_s0i3_resume(void)
210{
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600211 tpm_result_t rc;
Rob Barnesb35acf92021-11-02 17:47:47 -0600212
213 post_code(POSTCODE_VERSTAGE_S0I3_RESUME);
214
215 printk(BIOS_DEBUG, "Entering PSP verstage S0i3 resume\n");
216
217 if (!CONFIG(PSP_INIT_TPM_ON_S0I3_RESUME))
218 return;
219
Jon Murphy24604812023-09-05 10:37:05 -0600220 rc = tpm_setup(true);
221 if (rc != TPM_SUCCESS) {
222 printk(BIOS_ERR, "tpm_setup failed rc:%d\n", rc);
Rob Barnesb35acf92021-11-02 17:47:47 -0600223 reboot_into_recovery(vboot_get_context(), POSTCODE_INIT_TPM_FAILED);
224 }
225
Jon Murphy24604812023-09-05 10:37:05 -0600226 rc = tlcl_disable_platform_hierarchy();
227 if (rc != TPM_SUCCESS) {
228 printk(BIOS_ERR, "tlcl_disable_platform_hierarchy failed rc:%d\n", rc);
Rob Barnesb35acf92021-11-02 17:47:47 -0600229 reboot_into_recovery(vboot_get_context(), POSTCODE_INIT_TPM_FAILED);
230 }
231}
232
Martin Rothc7acf162020-05-28 00:44:50 -0600233void Main(void)
234{
235 uint32_t retval;
236 struct vb2_context *ctx = NULL;
Rob Barnesb35acf92021-11-02 17:47:47 -0600237 uint32_t bootmode;
Karthikeyan Ramasubramanianb6ab7ba2023-11-20 23:34:22 +0000238 void *boot_dev_base;
Martin Rothc7acf162020-05-28 00:44:50 -0600239
240 /*
241 * Do not use printk() before console_init()
242 * Do not use post_code() before verstage_mainboard_init()
Karthikeyan Ramasubramanian1a24d842022-03-16 16:27:49 -0600243 * Do not use svc_write_postcode before verstage_soc_espi_init() if PSP uses ESPI
244 * to write postcodes.
Martin Rothc7acf162020-05-28 00:44:50 -0600245 */
Kangheui Won4e2f5fd2020-09-17 16:37:13 +1000246 timestamp_init(timestamp_get());
Karthikeyan Ramasubramanian1a24d842022-03-16 16:27:49 -0600247 if (!CONFIG(PSP_POSTCODES_ON_ESPI))
248 svc_write_postcode(POSTCODE_ENTERED_PSP_VERSTAGE);
Martin Rothc7acf162020-05-28 00:44:50 -0600249 svc_debug_print("Entering verstage on PSP\n");
250 memset(&_bss_start, '\0', &_bss_end - &_bss_start);
251
Karthikeyan Ramasubramanian1a24d842022-03-16 16:27:49 -0600252 if (!CONFIG(PSP_POSTCODES_ON_ESPI))
253 svc_write_postcode(POSTCODE_CONSOLE_INIT);
Martin Rothc7acf162020-05-28 00:44:50 -0600254 console_init();
255
Karthikeyan Ramasubramaniane5f627a2022-12-22 13:05:12 -0700256 if (CONFIG(PSP_INCLUDES_HSP))
257 report_hsp_secure_state();
258
Karthikeyan Ramasubramanian1a24d842022-03-16 16:27:49 -0600259 if (!CONFIG(PSP_POSTCODES_ON_ESPI))
260 svc_write_postcode(POSTCODE_EARLY_INIT);
Martin Rothc7acf162020-05-28 00:44:50 -0600261 retval = verstage_soc_early_init();
262 if (retval) {
Rob Barnesc30a1fa2021-11-08 06:43:07 -0700263 /*
264 * If verstage_soc_early_init fails, cmos is probably not
265 * accessible, so rebooting into recovery is not an option.
266 * Just reboot and hope for the best.
267 */
268 svc_write_postcode(POSTCODE_EARLY_INIT_ERROR);
269 svc_debug_print("verstage_soc_early_init failed! -- rebooting\n");
270 vboot_reboot();
Martin Rothc7acf162020-05-28 00:44:50 -0600271 }
Martin Rothc7acf162020-05-28 00:44:50 -0600272
Rob Barnesf6e421f2021-11-08 13:04:18 -0700273 printk(BIOS_DEBUG, "calling verstage_mainboard_espi_init\n");
274 verstage_mainboard_espi_init();
275
Rob Barnes188be6b2021-11-09 13:21:28 -0700276 printk(BIOS_DEBUG, "calling verstage_soc_espi_init\n");
277 verstage_soc_espi_init();
278
279 printk(BIOS_DEBUG, "calling verstage_mainboard_tpm_init\n");
280 /* mainboard_tpm_init may check board_id, so make sure espi is ready first */
281 verstage_mainboard_tpm_init();
282
Rob Barnes847a39f2021-11-15 12:56:34 -0700283 printk(BIOS_DEBUG, "calling verstage_soc_aoac_init\n");
284 verstage_soc_aoac_init();
285
286 printk(BIOS_DEBUG, "calling verstage_soc_i2c_init\n");
287 verstage_soc_i2c_init();
288
Rob Barnesb35acf92021-11-02 17:47:47 -0600289 /*
290 * S0i3 resume in PSP verstage is a special case, handle it separately.
291 * Make sure TPM i2c is ready first.
292 */
293 svc_get_boot_mode(&bootmode);
294 if (bootmode == PSP_BOOT_MODE_S0i3_RESUME) {
295 psp_verstage_s0i3_resume();
Raul E Rangel737ad672022-02-24 11:49:52 -0700296
Raul E Rangel409e5cb2022-02-24 11:54:32 -0700297 post_code(POSTCODE_SAVE_BUFFERS);
298 retval = save_buffers();
299 if (retval)
300 post_code(retval);
301
Raul E Rangel737ad672022-02-24 11:49:52 -0700302 post_code(POSTCODE_UNMAP_FCH_DEVICES);
Rob Barnesb35acf92021-11-02 17:47:47 -0600303 unmap_fch_devices();
Raul E Rangel737ad672022-02-24 11:49:52 -0700304
305 post_code(POSTCODE_LEAVING_VERSTAGE);
Rob Barnesb35acf92021-11-02 17:47:47 -0600306 svc_exit(0);
307 }
308
309 printk(BIOS_DEBUG, "calling verstage_mainboard_early_init\n");
310 verstage_mainboard_early_init();
311
312 svc_write_postcode(POSTCODE_LATE_INIT);
Karthikeyan Ramasubramanian5eb77922023-07-21 18:05:25 -0600313 if (CONFIG(SOC_AMD_COMMON_BLOCK_ACPIMMIO_PM_IO_ACCESS))
314 fch_io_enable_legacy_io();
Rob Barnesb35acf92021-11-02 17:47:47 -0600315
Rob Barnes847a39f2021-11-15 12:56:34 -0700316 printk(BIOS_DEBUG, "calling verstage_soc_spi_init\n");
317 verstage_soc_spi_init();
318
Martin Rothc7acf162020-05-28 00:44:50 -0600319 verstage_mainboard_init();
320
321 post_code(POSTCODE_VERSTAGE_MAIN);
Karthikeyan Ramasubramanian0822ce82022-12-05 14:54:53 -0700322 if (CONFIG(SEPARATE_SIGNED_PSPFW))
323 report_prev_boot_status_to_vboot();
Martin Rothc7acf162020-05-28 00:44:50 -0600324
Kangheui Wonac7ec272021-01-15 15:04:25 +1100325 vboot_run_logic();
Martin Rothc7acf162020-05-28 00:44:50 -0600326
Martin Rothc9689e02020-08-20 17:25:37 -0600327 ctx = vboot_get_context();
Martin Roth50cca762020-08-13 11:06:18 -0600328 retval = check_cmos_recovery();
329 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600330 reboot_into_recovery(ctx, retval);
Martin Roth0c12abe2020-06-26 08:40:56 -0600331
Kangheui Won7e91db72022-01-25 18:55:04 +1100332 platform_report_mode(vboot_developer_mode_enabled());
333
Kangheui Wonac7ec272021-01-15 15:04:25 +1100334 post_code(POSTCODE_UPDATE_BOOT_REGION);
Kangheui Won97527252021-05-20 10:02:00 +1000335
336 /*
337 * Since psp_verstage doesn't load next stage we never call
338 * any cbfs API on RO path. However we still need to initialize
339 * RO CBFS MCACHE manually to pass it in transfer_buffer.
340 * In RW path, MCACHE build will be skipped for RO region since
341 * we already built here.
342 */
343 cbfs_get_boot_device(true);
344
Kangheui Wonac7ec272021-01-15 15:04:25 +1100345 retval = update_boot_region(ctx);
346 if (retval)
347 reboot_into_recovery(ctx, retval);
348
Martin Rothc7acf162020-05-28 00:44:50 -0600349 post_code(POSTCODE_SAVE_BUFFERS);
Raul E Rangel5e0ed502022-02-24 10:58:29 -0700350 retval = save_buffers();
Martin Rothc7acf162020-05-28 00:44:50 -0600351 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600352 reboot_into_recovery(ctx, retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600353
Karthikeyan Ramasubramanianb6ab7ba2023-11-20 23:34:22 +0000354 if (CONFIG(PSP_VERSTAGE_MAP_ENTIRE_SPIROM)) {
355 post_code(POSTCODE_UNMAP_SPI_ROM);
356 boot_dev_base = rdev_mmap_full(boot_device_ro());
357 if (boot_dev_base) {
358 if (svc_unmap_spi_rom((void *)boot_dev_base))
359 printk(BIOS_ERR, "Error unmapping SPI rom\n");
360 }
361 }
Karthikeyan Ramasubramanian6f1b03b2023-04-21 14:26:51 -0600362 assert(!boot_dev_get_active_map_count());
Karthikeyan Ramasubramanianb6ab7ba2023-11-20 23:34:22 +0000363
Martin Rothc7acf162020-05-28 00:44:50 -0600364 post_code(POSTCODE_UNMAP_FCH_DEVICES);
365 unmap_fch_devices();
366
367 post_code(POSTCODE_LEAVING_VERSTAGE);
368
369 printk(BIOS_DEBUG, "Leaving verstage on PSP\n");
370 svc_exit(retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600371}
372
Martin Rothc7acf162020-05-28 00:44:50 -0600373/*
374 * The stage_entry function is not used directly, but stage_entry() is marked as an entry
375 * point in arm/arch/header.h, so if stage_entry() isn't present and calling Main(), all
376 * the verstage code gets dropped by the linker. Slightly hacky, but mostly harmless.
377 */
378void stage_entry(uintptr_t stage_arg)
379{
380 Main();
381}