blob: f367dce6fd493ecd0abedbe28b3903f507b03b13 [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
5#include <bl_uapp/bl_syscall_public.h>
6#include <boot_device.h>
Martin Rothe21698b2020-06-26 08:55:15 -06007#include <cbfs.h>
Martin Rothc7acf162020-05-28 00:44:50 -06008#include <console/console.h>
Martin Rothe21698b2020-06-26 08:55:15 -06009#include <fmap.h>
Martin Roth50cca762020-08-13 11:06:18 -060010#include <pc80/mc146818rtc.h>
Martin Roth0c12abe2020-06-26 08:40:56 -060011#include <soc/psp_transfer.h>
Martin Roth50cca762020-08-13 11:06:18 -060012#include <security/vboot/vbnv.h>
Martin Rothc7acf162020-05-28 00:44:50 -060013#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 Won4e2f5fd2020-09-17 16:37:13 +100019#include <timestamp.h>
Martin Rothc7acf162020-05-28 00:44:50 -060020
Martin Rothc7acf162020-05-28 00:44:50 -060021extern char _bss_start, _bss_end;
22static struct mem_region_device boot_dev =
23 MEM_REGION_DEV_RO_INIT(NULL, CONFIG_ROM_SIZE);
24
25void __weak verstage_mainboard_early_init(void) {}
26void __weak verstage_mainboard_init(void) {}
27
28static 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 Rothc9689e02020-08-20 17:25:37 -060033 /*
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 Rothc7acf162020-05-28 00:44:50 -060043 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 Roth50cca762020-08-13 11:06:18 -060050static 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 Rothe21698b2020-06-26 08:55:15 -060066static 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 Rothc7acf162020-05-28 00:44:50 -060079/*
80 * Tell the PSP where to load the rest of the firmware from
81 */
82static 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 Rothe21698b2020-06-26 08:55:15 -060087 const char *rname, *fname;
88 struct region_device rdev;
89 uintptr_t amdfw_location;
Martin Rothc7acf162020-05-28 00:44:50 -060090
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 Rothe21698b2020-06-26 08:55:15 -060098 rname = "FW_MAIN_A";
99 fname = "apu/amdfw_a";
Martin Rothc7acf162020-05-28 00:44:50 -0600100 } else {
Martin Rothe21698b2020-06-26 08:55:15 -0600101 rname = "FW_MAIN_B";
102 fname = "apu/amdfw_b";
Martin Rothc7acf162020-05-28 00:44:50 -0600103 }
104
Martin Rothe21698b2020-06-26 08:55:15 -0600105 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 Rothc7acf162020-05-28 00:44:50 -0600116 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 */
149static uint32_t save_buffers(struct vb2_context **ctx)
150{
151 uint32_t retval;
Martin Roth0c12abe2020-06-26 08:40:56 -0600152 uint32_t buffer_size = MIN_TRANSFER_BUFFER_SIZE;
Martin Rothc7acf162020-05-28 00:44:50 -0600153 uint32_t max_buffer_size;
Martin Roth0c12abe2020-06-26 08:40:56 -0600154 struct transfer_info_struct buffer_info = {0};
Martin Rothc7acf162020-05-28 00:44:50 -0600155
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 Roth0c12abe2020-06-26 08:40:56 -0600163 max_buffer_size = MIN_TRANSFER_BUFFER_SIZE;
Martin Rothc7acf162020-05-28 00:44:50 -0600164 }
165 printk(BIOS_DEBUG, "\nMaximum buffer size: %d bytes\n", max_buffer_size);
166
Martin Roth0c12abe2020-06-26 08:40:56 -0600167 /* 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 Rothc7acf162020-05-28 00:44:50 -0600186 }
187
188 if (buffer_size > max_buffer_size) {
Martin Roth0c12abe2020-06-26 08:40:56 -0600189 printk(BIOS_ERR, "Error: Buffer is larger than max buffer size.\n");
Martin Rothc7acf162020-05-28 00:44:50 -0600190 post_code(POSTCODE_WORKBUF_BUFFER_SIZE_ERROR);
191 return POSTCODE_WORKBUF_BUFFER_SIZE_ERROR;
192 }
193
Martin Roth0c12abe2020-06-26 08:40:56 -0600194 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 Won5f027fa2020-08-25 18:12:19 +1000200 memcpy(_transfer_buffer, &buffer_info, sizeof(buffer_info));
201
Martin Roth0c12abe2020-06-26 08:40:56 -0600202 retval = svc_save_uapp_data(UAPP_COPYBUF_CHROME_WORKBUF, (void *)_transfer_buffer,
203 buffer_size);
Martin Rothc7acf162020-05-28 00:44:50 -0600204 if (retval) {
Martin Roth0c12abe2020-06-26 08:40:56 -0600205 printk(BIOS_ERR, "Error: Could not save workbuf. Error code 0x%08x\n", retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600206 return POSTCODE_WORKBUF_SAVE_ERROR;
207 }
208
209 return 0;
210}
211
212void 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 Won4e2f5fd2020-09-17 16:37:13 +1000221 timestamp_init(timestamp_get());
Martin Rothc7acf162020-05-28 00:44:50 -0600222 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 Rothc9689e02020-08-20 17:25:37 -0600248 ctx = vboot_get_context();
Martin Roth50cca762020-08-13 11:06:18 -0600249 retval = check_cmos_recovery();
250 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600251 reboot_into_recovery(ctx, retval);
Martin Roth0c12abe2020-06-26 08:40:56 -0600252
Martin Rothc7acf162020-05-28 00:44:50 -0600253 post_code(POSTCODE_SAVE_BUFFERS);
254 retval = save_buffers(&ctx);
255 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600256 reboot_into_recovery(ctx, retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600257
258 post_code(POSTCODE_UPDATE_BOOT_REGION);
259 retval = update_boot_region(ctx);
260 if (retval)
Martin Rothc9689e02020-08-20 17:25:37 -0600261 reboot_into_recovery(ctx, retval);
Martin Rothc7acf162020-05-28 00:44:50 -0600262
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 Rothc7acf162020-05-28 00:44:50 -0600276}
277
278const 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 */
293void stage_entry(uintptr_t stage_arg)
294{
295 Main();
296}