xref: /aosp_15_r20/external/coreboot/src/soc/amd/common/vboot/transfer_buffer.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <amdblocks/reset.h>
4 #include <console/cbmem_console.h>
5 #include <console/console.h>
6 #include <pc80/mc146818rtc.h>
7 #include <psp_verstage/psp_transfer.h>
8 #include <security/vboot/vbnv.h>
9 #include <security/vboot/symbols.h>
10 #include <2struct.h>
11 
DECLARE_REGION(cbmemc_transfer)12 DECLARE_REGION(cbmemc_transfer)
13 
14 int transfer_buffer_valid(const struct transfer_info_struct *ptr)
15 {
16 	if (ptr->magic_val == TRANSFER_MAGIC_VAL && ptr->struct_bytes == sizeof(*ptr))
17 		return 1;
18 	else
19 		return 0;
20 }
21 
verify_psp_transfer_buf(void)22 void verify_psp_transfer_buf(void)
23 {
24 	if (*(uint32_t *)_vboot2_work == VB2_SHARED_DATA_MAGIC) {
25 		cmos_write(0x00, CMOS_RECOVERY_BYTE);
26 		return;
27 	}
28 
29 	/*
30 	 * If CMOS is valid and the system has already been rebooted once, but
31 	 * still returns here, instead of rebooting to verstage again, assume
32 	 * that the system is in a reboot loop and halt.
33 	 */
34 	if ((!vbnv_cmos_failed()) && cmos_read(CMOS_RECOVERY_BYTE) ==
35 			CMOS_RECOVERY_MAGIC_VAL)
36 		die("Error: Reboot into recovery was unsuccessful.  Halting.");
37 
38 	printk(BIOS_ERR, "VBOOT workbuf not valid.\n");
39 	printk(BIOS_DEBUG, "Signature: %#08x\n", *(uint32_t *)_vboot2_work);
40 	cmos_init(0);
41 	cmos_write(CMOS_RECOVERY_MAGIC_VAL, CMOS_RECOVERY_BYTE);
42 	warm_reset();
43 }
44 
show_psp_transfer_info(void)45 void show_psp_transfer_info(void)
46 {
47 	struct transfer_info_struct *info = (struct transfer_info_struct *)
48 			(void *)(uintptr_t)_transfer_buffer;
49 
50 	if (transfer_buffer_valid(info)) {
51 		if ((info->psp_info & PSP_INFO_VALID) == 0) {
52 			printk(BIOS_INFO, "No PSP info found in transfer buffer.\n");
53 			return;
54 		}
55 
56 		printk(BIOS_INFO, "PSP boot mode: %s\n",
57 				info->psp_info & PSP_INFO_PRODUCTION_MODE ?
58 				"Production" : "Development");
59 	}
60 }
61 
replay_transfer_buffer_cbmemc(void)62 void replay_transfer_buffer_cbmemc(void)
63 {
64 	const struct transfer_info_struct *info = (const struct transfer_info_struct *)
65 		(void *)(uintptr_t)_transfer_buffer;
66 
67 	void *cbmemc;
68 	size_t cbmemc_size;
69 
70 	if (!transfer_buffer_valid(info))
71 		return;
72 
73 	if (info->console_offset < sizeof(*info))
74 		return;
75 
76 	if (info->timestamp_offset <= info->console_offset)
77 		return;
78 
79 	cbmemc_size = info->timestamp_offset - info->console_offset;
80 
81 	if (info->console_offset + cbmemc_size > info->buffer_size)
82 		return;
83 
84 	cbmemc = (void *)((uintptr_t)info + info->console_offset);
85 
86 	/* Verify the cbmemc transfer buffer is where we expect it to be. */
87 	if ((void *)_cbmemc_transfer != (void *)cbmemc)
88 		return;
89 
90 	if (REGION_SIZE(cbmemc_transfer) != cbmemc_size)
91 		return;
92 
93 	/* We need to manually initialize cbmemc so we can fill the new buffer. cbmemc_init()
94 	 * will also be called later in console_hw_init(), but it will be a no-op. */
95 	cbmemc_init();
96 	cbmemc_copy_in(cbmemc, cbmemc_size);
97 }
98