From 9c9299b601e099ad6670b962bf9056a4b82b32b9 Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 27 Oct 2026 17:36:55 -0700 Subject: [PATCH 01/17] nai_68int6: board devices and a per-image Linux or Multiboot2 payload --- docs/Targets.md | 23 +++++++- include/x86/acpi_dsdt.h | 73 +++++++++++++++++++++++-- include/x86/gpio_config.h | 109 +++++++++++++++++++++++++++++++++++++ src/boot_x86_fsp_payload.c | 58 +++++++++++++------- src/x86/acpi_dsdt.asl | 92 ++++++++++++++++++++++++++++++- src/x86/tgl_fsp.c | 2 + 6 files changed, 331 insertions(+), 26 deletions(-) diff --git a/docs/Targets.md b/docs/Targets.md index bd7e9e68e3..a2af70712b 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -9223,7 +9223,7 @@ Notable options in [config/examples/nai_68int6.config](../config/examples/nai_68 | Option | Meaning | |---|---| | `X86_UART_NUMBER=2` | LPSS UART2 for all three FSP phases (Kontron uses 0) | -| `ACPI=1` | Required. Generates ACPI tables (RSDP/XSDT/FADT/MADT/MCFG and a minimal DSDT); the board's PCI interrupt routing is only in the DSDT `_PRT`, since the MP table is the Kontron one | +| `ACPI=1` | Required. Generates ACPI tables (RSDP/XSDT/FADT/MADT/MCFG and a minimal DSDT); the board's PCI interrupt routing is only in the DSDT `_PRT`, since the MP table is the Kontron one. The DSDT also declares the board devices on LPSS I2C0 (EEPROMs, RTC/watchdog) and GSPI0 (the PRC, for NAI's `naigspi` driver) | | `ANTI_ROLLBACK=1` | Persistent version floor and boot audit counters in TPM NV | | `MEASURED_BOOT=1`, `MEASURED_PCR_OS=4` | Firmware into PCR 0, the verified OS image into PCR 4 | | `WOLFBOOT_BOOT_DISK=0` | Boot from GPT partitions; the A/B indices (sda3/sda4) are set in `src/update_disk.c` | @@ -9244,7 +9244,26 @@ The booted OS cannot lower the floor. The index is platform-created and writable ### Boot, programming and recovery -wolfBoot boots the highest-versioned signed 64-bit `bzImage` that verifies from the two GPT boot partitions. It falls back to the other slot only if that is not a downgrade (see Anti-rollback), and halts if neither boots. Program the flash with a Dediprog SF100 on JP4 while the board is off; the BIOS region lock blocks `flashrom` from the booted OS. JP36 selects which bank boots and which one the programmer sees; move it only with the board off. +wolfBoot boots the highest-versioned signed 64-bit `bzImage` or Multiboot2 ELF that verifies from the two GPT boot partitions. It falls back to the other slot only if that is not a downgrade (see Anti-rollback), and halts if neither boots. Program the flash with a Dediprog SF100 on JP4 while the board is off; the BIOS region lock blocks `flashrom` from the booted OS. JP36 selects which bank boots and which one the programmer sees; move it only with the board off. + +### Multiboot2 ELF payloads + +The configuration builds both payload paths (`LINUX_PAYLOAD=1`, `ELF=1`, `MULTIBOOT2=1`), and `do_boot()` picks one per image: an image that starts with the ELF magic takes the Multiboot2 path, and anything else boots as Linux. A Multiboot2 RTOS image is signed and written to an A/B slot the same way as a kernel: + +``` +./tools/keytools/sign --ecc384 --sha384 wolfboot_signing_private_key.der +``` + +The two paths differ only in the handoff: + +| | Linux `bzImage` | Multiboot2 ELF | +|---|---|---| +| Entry | 64-bit Linux boot protocol, `boot_params` and the command line | ELF segments loaded at their physical addresses, virtual addresses mapped in wolfBoot's page tables, jump to `e_entry` in 64-bit mode with EAX=`0x36d76289` and EBX pointing to the Multiboot2 information | +| Memory map | E820 table in `boot_params` | Multiboot2 memory map and basic memory information tags, from the FSP HOBs | +| ACPI tables | Generated (`ACPI=1`) | None; they are only built on the Linux path | +| RTC | The legacy CMOS RTC is ignored under the hardware-reduced ACPI tables; the I2C0 RTC is declared in the DSDT | The CMOS RTC is usable directly; I2C0 is enabled for the board RTC | + +Everything else is shared: FSP silicon setup (VT-d at the stock BIOS addresses, Hyper-Threading off), the halted PCH TCO watchdog, the BIOS region lock, measured boot, anti-rollback and A/B selection. ## Infineon PSOC Control C3 diff --git a/include/x86/acpi_dsdt.h b/include/x86/acpi_dsdt.h index 71b4ebbc20..af6aa92c94 100644 --- a/include/x86/acpi_dsdt.h +++ b/include/x86/acpi_dsdt.h @@ -22,10 +22,10 @@ * Regenerate: iasl src/x86/acpi_dsdt.asl && * xxd -i -n acpi_dsdt_aml src/x86/acpi_dsdt.aml (append below this header). */ static const unsigned char acpi_dsdt_aml[] = { - 0x44, 0x53, 0x44, 0x54, 0x1e, 0x02, 0x00, 0x00, 0x02, 0x48, 0x57, 0x4f, + 0x44, 0x53, 0x44, 0x54, 0x29, 0x05, 0x00, 0x00, 0x02, 0xa7, 0x57, 0x4f, 0x4c, 0x46, 0x42, 0x54, 0x4e, 0x41, 0x49, 0x54, 0x47, 0x4c, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x49, 0x4e, 0x54, 0x4c, 0x28, 0x06, 0x23, 0x20, - 0x10, 0x49, 0x1f, 0x5f, 0x53, 0x42, 0x5f, 0x5b, 0x82, 0x41, 0x1f, 0x50, + 0x10, 0x44, 0x50, 0x5f, 0x53, 0x42, 0x5f, 0x5b, 0x82, 0x4c, 0x4f, 0x50, 0x43, 0x49, 0x30, 0x08, 0x5f, 0x48, 0x49, 0x44, 0x0c, 0x41, 0xd0, 0x0a, 0x08, 0x08, 0x5f, 0x43, 0x49, 0x44, 0x0c, 0x41, 0xd0, 0x0a, 0x03, 0x08, 0x5f, 0x53, 0x45, 0x47, 0x00, 0x08, 0x5f, 0x42, 0x42, 0x4e, 0x00, 0x08, @@ -67,6 +67,71 @@ static const unsigned char acpi_dsdt_aml[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, - 0x79, 0x00 + 0x79, 0x00, 0x5b, 0x82, 0x49, 0x25, 0x49, 0x32, 0x43, 0x30, 0x08, 0x5f, + 0x41, 0x44, 0x52, 0x0c, 0x00, 0x00, 0x15, 0x00, 0x5b, 0x82, 0x43, 0x0c, + 0x45, 0x45, 0x50, 0x32, 0x08, 0x5f, 0x48, 0x49, 0x44, 0x0d, 0x31, 0x35, + 0x41, 0x43, 0x30, 0x30, 0x30, 0x32, 0x00, 0x08, 0x5f, 0x43, 0x49, 0x44, + 0x0d, 0x50, 0x52, 0x50, 0x30, 0x30, 0x30, 0x31, 0x00, 0x08, 0x5f, 0x44, + 0x44, 0x4e, 0x0d, 0x4e, 0x41, 0x49, 0x20, 0x4d, 0x6f, 0x74, 0x68, 0x65, + 0x72, 0x62, 0x6f, 0x61, 0x72, 0x64, 0x20, 0x45, 0x45, 0x50, 0x52, 0x4f, + 0x4d, 0x00, 0x08, 0x5f, 0x43, 0x52, 0x53, 0x11, 0x26, 0x0a, 0x23, 0x8e, + 0x1e, 0x00, 0x02, 0x00, 0x01, 0x02, 0x00, 0x00, 0x01, 0x06, 0x00, 0xa0, + 0x86, 0x01, 0x00, 0x56, 0x00, 0x5c, 0x5f, 0x53, 0x42, 0x2e, 0x50, 0x43, + 0x49, 0x30, 0x2e, 0x49, 0x32, 0x43, 0x30, 0x00, 0x79, 0x00, 0x08, 0x5f, + 0x44, 0x53, 0x44, 0x12, 0x41, 0x05, 0x02, 0x11, 0x13, 0x0a, 0x10, 0x14, + 0xd8, 0xff, 0xda, 0xba, 0x6e, 0x8c, 0x4d, 0x8a, 0x91, 0xbc, 0x9b, 0xbf, + 0x4a, 0xa3, 0x01, 0x12, 0x39, 0x03, 0x12, 0x1b, 0x02, 0x0d, 0x63, 0x6f, + 0x6d, 0x70, 0x61, 0x74, 0x69, 0x62, 0x6c, 0x65, 0x00, 0x0d, 0x61, 0x74, + 0x6d, 0x65, 0x6c, 0x2c, 0x32, 0x34, 0x63, 0x30, 0x32, 0x00, 0x12, 0x0b, + 0x02, 0x0d, 0x73, 0x69, 0x7a, 0x65, 0x00, 0x0b, 0x00, 0x01, 0x12, 0x0e, + 0x02, 0x0d, 0x70, 0x61, 0x67, 0x65, 0x73, 0x69, 0x7a, 0x65, 0x00, 0x0a, + 0x08, 0x5b, 0x82, 0x46, 0x0d, 0x45, 0x45, 0x50, 0x33, 0x08, 0x5f, 0x48, + 0x49, 0x44, 0x0d, 0x31, 0x35, 0x41, 0x43, 0x30, 0x30, 0x30, 0x33, 0x00, + 0x08, 0x5f, 0x43, 0x49, 0x44, 0x0d, 0x50, 0x52, 0x50, 0x30, 0x30, 0x30, + 0x31, 0x00, 0x08, 0x5f, 0x44, 0x44, 0x4e, 0x0d, 0x4e, 0x41, 0x49, 0x20, + 0x4d, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x62, 0x6f, 0x61, 0x72, 0x64, 0x20, + 0x45, 0x45, 0x50, 0x52, 0x4f, 0x4d, 0x20, 0x53, 0x4e, 0x00, 0x08, 0x5f, + 0x43, 0x52, 0x53, 0x11, 0x26, 0x0a, 0x23, 0x8e, 0x1e, 0x00, 0x02, 0x00, + 0x01, 0x02, 0x00, 0x00, 0x01, 0x06, 0x00, 0xa0, 0x86, 0x01, 0x00, 0x5e, + 0x00, 0x5c, 0x5f, 0x53, 0x42, 0x2e, 0x50, 0x43, 0x49, 0x30, 0x2e, 0x49, + 0x32, 0x43, 0x30, 0x00, 0x79, 0x00, 0x08, 0x5f, 0x44, 0x53, 0x44, 0x12, + 0x41, 0x06, 0x02, 0x11, 0x13, 0x0a, 0x10, 0x14, 0xd8, 0xff, 0xda, 0xba, + 0x6e, 0x8c, 0x4d, 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01, 0x12, + 0x49, 0x04, 0x04, 0x12, 0x1c, 0x02, 0x0d, 0x63, 0x6f, 0x6d, 0x70, 0x61, + 0x74, 0x69, 0x62, 0x6c, 0x65, 0x00, 0x0d, 0x61, 0x74, 0x6d, 0x65, 0x6c, + 0x2c, 0x32, 0x34, 0x63, 0x73, 0x30, 0x32, 0x00, 0x12, 0x0a, 0x02, 0x0d, + 0x73, 0x69, 0x7a, 0x65, 0x00, 0x0a, 0x10, 0x12, 0x0e, 0x02, 0x0d, 0x70, + 0x61, 0x67, 0x65, 0x73, 0x69, 0x7a, 0x65, 0x00, 0x0a, 0x08, 0x12, 0x0e, + 0x02, 0x0d, 0x72, 0x65, 0x61, 0x64, 0x2d, 0x6f, 0x6e, 0x6c, 0x79, 0x00, + 0x01, 0x5b, 0x82, 0x4a, 0x0a, 0x52, 0x54, 0x43, 0x31, 0x08, 0x5f, 0x48, + 0x49, 0x44, 0x0d, 0x31, 0x35, 0x41, 0x43, 0x30, 0x30, 0x30, 0x34, 0x00, + 0x08, 0x5f, 0x43, 0x49, 0x44, 0x0d, 0x50, 0x52, 0x50, 0x30, 0x30, 0x30, + 0x31, 0x00, 0x08, 0x5f, 0x44, 0x44, 0x4e, 0x0d, 0x4e, 0x41, 0x49, 0x20, + 0x4d, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x62, 0x6f, 0x61, 0x72, 0x64, 0x20, + 0x57, 0x44, 0x54, 0x20, 0x52, 0x54, 0x43, 0x00, 0x08, 0x5f, 0x43, 0x52, + 0x53, 0x11, 0x26, 0x0a, 0x23, 0x8e, 0x1e, 0x00, 0x02, 0x00, 0x01, 0x02, + 0x00, 0x00, 0x01, 0x06, 0x00, 0xa0, 0x86, 0x01, 0x00, 0x68, 0x00, 0x5c, + 0x5f, 0x53, 0x42, 0x2e, 0x50, 0x43, 0x49, 0x30, 0x2e, 0x49, 0x32, 0x43, + 0x30, 0x00, 0x79, 0x00, 0x08, 0x5f, 0x44, 0x53, 0x44, 0x12, 0x37, 0x02, + 0x11, 0x13, 0x0a, 0x10, 0x14, 0xd8, 0xff, 0xda, 0xba, 0x6e, 0x8c, 0x4d, + 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01, 0x12, 0x20, 0x01, 0x12, + 0x1d, 0x02, 0x0d, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x69, 0x62, 0x6c, + 0x65, 0x00, 0x0d, 0x64, 0x61, 0x6c, 0x6c, 0x61, 0x73, 0x2c, 0x64, 0x73, + 0x31, 0x33, 0x37, 0x34, 0x00, 0x5b, 0x82, 0x4e, 0x0a, 0x53, 0x50, 0x49, + 0x30, 0x08, 0x5f, 0x41, 0x44, 0x52, 0x0c, 0x02, 0x00, 0x1e, 0x00, 0x5b, + 0x82, 0x4c, 0x09, 0x50, 0x52, 0x43, 0x30, 0x08, 0x5f, 0x48, 0x49, 0x44, + 0x0d, 0x31, 0x35, 0x41, 0x43, 0x30, 0x30, 0x30, 0x31, 0x00, 0x08, 0x5f, + 0x53, 0x54, 0x52, 0x11, 0x42, 0x05, 0x0a, 0x4e, 0x4e, 0x00, 0x41, 0x00, + 0x49, 0x00, 0x20, 0x00, 0x47, 0x00, 0x53, 0x00, 0x50, 0x00, 0x49, 0x00, + 0x20, 0x00, 0x50, 0x00, 0x72, 0x00, 0x6f, 0x00, 0x63, 0x00, 0x65, 0x00, + 0x73, 0x00, 0x73, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x20, 0x00, 0x52, 0x00, + 0x65, 0x00, 0x67, 0x00, 0x69, 0x00, 0x73, 0x00, 0x74, 0x00, 0x65, 0x00, + 0x72, 0x00, 0x20, 0x00, 0x43, 0x00, 0x6f, 0x00, 0x6e, 0x00, 0x74, 0x00, + 0x72, 0x00, 0x6f, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x65, 0x00, 0x72, 0x00, + 0x00, 0x00, 0x08, 0x5f, 0x43, 0x52, 0x53, 0x11, 0x29, 0x0a, 0x26, 0x8e, + 0x21, 0x00, 0x02, 0x00, 0x02, 0x02, 0x00, 0x00, 0x01, 0x09, 0x00, 0x20, + 0xa1, 0x07, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x5c, 0x5f, 0x53, 0x42, + 0x2e, 0x50, 0x43, 0x49, 0x30, 0x2e, 0x53, 0x50, 0x49, 0x30, 0x00, 0x79, + 0x00 }; -static const unsigned int acpi_dsdt_aml_len = 542; +static const unsigned int acpi_dsdt_aml_len = 1321; diff --git a/include/x86/gpio_config.h b/include/x86/gpio_config.h index 73bcd468fe..9b60d90c2b 100644 --- a/include/x86/gpio_config.h +++ b/include/x86/gpio_config.h @@ -39,6 +39,10 @@ #define GPIO_GPPC_B9_CFG_OFF (0x790) #define GPIO_GPPC_B10_CFG_OFF (0x7a0) +#define GPIO_GPPC_B15_CFG_OFF (0x7f0) +#define GPIO_GPPC_B16_CFG_OFF (0x800) +#define GPIO_GPPC_B17_CFG_OFF (0x810) +#define GPIO_GPPC_B18_CFG_OFF (0x820) #define GPIO_GPPC_C6_CFG_OFF (0x760) #define GPIO_GPPC_C7_CFG_OFF (0x770) #define GPIO_GPPC_C8_CFG_OFF (0x780) @@ -49,6 +53,8 @@ #define GPIO_GPPC_C13_CFG_OFF (0x7d0) #define GPIO_GPPC_C14_CFG_OFF (0x7e0) #define GPIO_GPPC_C15_CFG_OFF (0x7f0) +#define GPIO_GPPC_C16_CFG_OFF (0x800) +#define GPIO_GPPC_C17_CFG_OFF (0x810) #define GPIO_GPPC_C20_CFG_OFF (0x840) #define GPIO_GPPC_C21_CFG_OFF (0x850) #define GPIO_GPPC_C22_CFG_OFF (0x860) @@ -688,6 +694,109 @@ static const struct tgl_gpio_conf gpio_table_presilicon[] = { .gpio_rxinv = 0x00, .gpio_rxevconf = 0x02, .gpio_reset = GPIO_RESET_PLTRST}, + /* GSPI0 (B15-B18) and I2C0 (C16/C17) in native mode, as stock. */ + /* PAD B15 (DW0=0x44000601 DW1=0x00000027) */ + {.gpio = + { + .comm_port_id = GPIO_COMM_0_PORT_ID, + .cfg_offset = GPIO_GPPC_B15_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF), + .gpio_mode = GPIO_MODE_NATIVE_1, + .gpio_dir = GPIO_DIR_OUTPUT, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_reset = GPIO_RESET_HOSTDEEPRESET}, + /* PAD B16 (DW0=0x84000601 DW1=0x00000028) */ + {.gpio = + { + .comm_port_id = GPIO_COMM_0_PORT_ID, + .cfg_offset = GPIO_GPPC_B16_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF), + .gpio_mode = GPIO_MODE_NATIVE_1, + .gpio_dir = GPIO_DIR_OUTPUT, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_reset = GPIO_RESET_PLTRST}, + /* PAD B17 (DW0=0x44000702 DW1=0x00010029) */ + {.gpio = + { + .comm_port_id = GPIO_COMM_0_PORT_ID, + .cfg_offset = GPIO_GPPC_B17_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF), + .gpio_mode = GPIO_MODE_NATIVE_1, + .gpio_dir = 0x03, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_reset = GPIO_RESET_HOSTDEEPRESET}, + /* PAD B18 (DW0=0x84000600 DW1=0x0000002A) */ + {.gpio = + { + .comm_port_id = GPIO_COMM_0_PORT_ID, + .cfg_offset = GPIO_GPPC_B18_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF), + .gpio_mode = GPIO_MODE_NATIVE_1, + .gpio_dir = GPIO_DIR_OUTPUT, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_reset = GPIO_RESET_PLTRST}, + /* PAD C16 (DW0=0x44000702 DW1=0x0000001E) */ + {.gpio = + { + .comm_port_id = GPIO_COMM_4_PORT_ID, + .cfg_offset = GPIO_GPPC_C16_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF), + .gpio_mode = GPIO_MODE_NATIVE_1, + .gpio_dir = 0x03, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_reset = GPIO_RESET_HOSTDEEPRESET}, + /* PAD C17 (DW0=0x44000702 DW1=0x0000001F) */ + {.gpio = + { + .comm_port_id = GPIO_COMM_4_PORT_ID, + .cfg_offset = GPIO_GPPC_C17_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF), + .gpio_mode = GPIO_MODE_NATIVE_1, + .gpio_dir = 0x03, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_reset = GPIO_RESET_HOSTDEEPRESET}, }; #else /* TARGET_kontron_vx3060_s2 */ diff --git a/src/boot_x86_fsp_payload.c b/src/boot_x86_fsp_payload.c index c976687fc3..ecdbe6c418 100644 --- a/src/boot_x86_fsp_payload.c +++ b/src/boot_x86_fsp_payload.c @@ -94,29 +94,16 @@ void jump(uintptr_t entry) : "g"(entry)); } +#if defined(WOLFBOOT_ELF) /** - * @brief Perform the boot process for the given application. - * - * This function performs the boot process for the specified application. - * It either loads and boots a Linux payload or an ELF binary, depending on the - * configuration. + * @brief Load an ELF image and boot it, through Multiboot2 when enabled. * - * @param app Pointer to the start of the application. + * @param app Pointer to the start of the ELF image. + * @param stage2_params Stage 2 parameters (memory layout and FSP HOBs). */ -void do_boot(const uint32_t *app) +static void boot_elf(const uint32_t *app, + struct stage2_parameter *stage2_params) { - struct stage2_parameter *stage2_params; - -#ifdef WOLFBOOT_TPM - wolfBoot_tpm2_deinit(); -#endif /* WOLFBOOT_TPM */ - - stage2_params = stage2_get_parameters(); -#if defined(WOLFBOOT_LINUX_PAYLOAD) - mptable_setup(); - load_linux((uint8_t *)app, stage2_params->payload_size, stage2_params, - cmdline); -#elif defined(WOLFBOOT_ELF) int r; uint64_t e; uint8_t *mb2_header; @@ -163,7 +150,40 @@ void do_boot(const uint32_t *app) #else jump(e); #endif /* WOLFBOOT_MULTIBOOT2 */ +} +#endif /* WOLFBOOT_ELF */ + +/** + * @brief Perform the boot process for the given application. + * + * This function performs the boot process for the specified application. + * It either loads and boots a Linux payload or an ELF binary, depending on the + * configuration. With both built in, an image starting with the ELF magic + * takes the ELF path and anything else boots as Linux. + * + * @param app Pointer to the start of the application. + */ +void do_boot(const uint32_t *app) +{ + struct stage2_parameter *stage2_params; + +#ifdef WOLFBOOT_TPM + wolfBoot_tpm2_deinit(); +#endif /* WOLFBOOT_TPM */ + stage2_params = stage2_get_parameters(); +#if defined(WOLFBOOT_LINUX_PAYLOAD) && defined(WOLFBOOT_ELF) + if (memcmp(app, ELF_IDENT_STR, 4) == 0) { + boot_elf(app, stage2_params); + return; + } +#endif +#if defined(WOLFBOOT_LINUX_PAYLOAD) + mptable_setup(); + load_linux((uint8_t *)app, stage2_params->payload_size, stage2_params, + cmdline); +#elif defined(WOLFBOOT_ELF) + boot_elf(app, stage2_params); #else #error "No payload compiled in" #endif /* WOLFBOOT_LINUX_PAYLOAD */ diff --git a/src/x86/acpi_dsdt.asl b/src/x86/acpi_dsdt.asl index 0a9b66f121..6242953d9f 100644 --- a/src/x86/acpi_dsdt.asl +++ b/src/x86/acpi_dsdt.asl @@ -1,7 +1,8 @@ /* acpi_dsdt.asl * * Minimal DSDT for the NAI 68INT6: the PCI root bus with its _CRS and the - * _PRT INTx routing. Compiled offline with iasl and committed as a C array. + * _PRT INTx routing, plus the board devices on LPSS I2C0 and GSPI0. + * Compiled offline with iasl and committed as a C array. * * Regenerate after editing: * iasl src/x86/acpi_dsdt.asl @@ -84,6 +85,95 @@ DefinitionBlock ("", "DSDT", 2, "WOLFBT", "NAITGL", 0x00000001) 0x0000000000000000, 0x0000004000000000, 0x0000007FFFFFFFFF, 0x0000000000000000, 0x0000004000000000) }) + + /* LPSS I2C0 and GSPI0 with the board devices the stock BIOS + * declares, so NAI's Linux drivers (naigspi, at24) bind. */ + Device (I2C0) + { + Name (_ADR, 0x00150000) + + Device (EEP2) + { + Name (_HID, "15AC0002") + Name (_CID, "PRP0001") + Name (_DDN, "NAI Motherboard EEPROM") + Name (_CRS, ResourceTemplate () + { + I2cSerialBusV2 (0x0056, ControllerInitiated, 100000, + AddressingMode7Bit, "\\_SB.PCI0.I2C0") + }) + Name (_DSD, Package () + { + ToUUID ("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"), + Package () + { + Package () { "compatible", "atmel,24c02" }, + Package () { "size", 256 }, + Package () { "pagesize", 8 } + } + }) + } + + Device (EEP3) + { + Name (_HID, "15AC0003") + Name (_CID, "PRP0001") + Name (_DDN, "NAI Motherboard EEPROM SN") + Name (_CRS, ResourceTemplate () + { + I2cSerialBusV2 (0x005E, ControllerInitiated, 100000, + AddressingMode7Bit, "\\_SB.PCI0.I2C0") + }) + Name (_DSD, Package () + { + ToUUID ("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"), + Package () + { + Package () { "compatible", "atmel,24cs02" }, + Package () { "size", 16 }, + Package () { "pagesize", 8 }, + Package () { "read-only", One } + } + }) + } + + Device (RTC1) + { + Name (_HID, "15AC0004") + Name (_CID, "PRP0001") + Name (_DDN, "NAI Motherboard WDT RTC") + Name (_CRS, ResourceTemplate () + { + I2cSerialBusV2 (0x0068, ControllerInitiated, 100000, + AddressingMode7Bit, "\\_SB.PCI0.I2C0") + }) + Name (_DSD, Package () + { + ToUUID ("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"), + Package () + { + Package () { "compatible", "dallas,ds1374" } + } + }) + } + } + + Device (SPI0) + { + Name (_ADR, 0x001E0002) + + Device (PRC0) + { + Name (_HID, "15AC0001") + Name (_STR, Unicode ("NAI GSPI Processor Register Controller")) + Name (_CRS, ResourceTemplate () + { + SpiSerialBusV2 (0x0000, PolarityLow, FourWireMode, 8, + ControllerInitiated, 500000, ClockPolarityLow, + ClockPhaseFirst, "\\_SB.PCI0.SPI0") + }) + } + } } } } diff --git a/src/x86/tgl_fsp.c b/src/x86/tgl_fsp.c index 03066e6f82..c9092ba221 100644 --- a/src/x86/tgl_fsp.c +++ b/src/x86/tgl_fsp.c @@ -1035,6 +1035,8 @@ int fsp_machine_update_s_parameters(uint8_t *default_s_params) upd->Cx = FSP_C_STATES; upd->TxtEnable = FSP_TXT; upd->ThermalMonitor = FSP_THERMAL_THROTTLING; + /* I2C0 (PCI mode) carries the board EEPROMs and the watchdog RTC */ + upd->SerialIoI2cMode[0] = 1; #endif return 0; } From 1efd95b2f43cf68427a7f95eb467a9760c11416a Mon Sep 17 00:00:00 2001 From: David Garske Date: Sat, 31 Oct 2026 10:38:32 -0700 Subject: [PATCH 02/17] x86 fsp: cache FSP-M training data in flash, stop the NAI global reset --- .gitignore | 4 + arch.mk | 6 + config/examples/nai_68int6.config | 6 + docs/Targets.md | 1 + hal/x86_fsp_tgl.c | 151 ++++++++++ include/stage2_params.h | 3 + include/x86/common.h | 2 + include/x86/fsp_nvs.h | 48 ++++ include/x86/tgl_fsp.h | 2 + options.mk | 11 + src/boot_x86_fsp.c | 20 ++ src/x86/common.c | 24 ++ src/x86/fsp.c | 6 + src/x86/fsp_nvs.c | 202 ++++++++++++++ src/x86/tgl_fsp.c | 6 + tools/scripts/x86_fsp/tgl/assemble_image.sh | 19 ++ tools/unit-tests/Makefile | 29 ++ tools/unit-tests/unit-fsp-nvs-save.c | 254 +++++++++++++++++ tools/unit-tests/unit-fsp-nvs.c | 247 +++++++++++++++++ tools/unit-tests/unit-tgl-spi-hwseq.c | 289 ++++++++++++++++++++ 20 files changed, 1330 insertions(+) create mode 100644 include/x86/fsp_nvs.h create mode 100644 src/x86/fsp_nvs.c create mode 100644 tools/unit-tests/unit-fsp-nvs-save.c create mode 100644 tools/unit-tests/unit-fsp-nvs.c create mode 100644 tools/unit-tests/unit-tgl-spi-hwseq.c diff --git a/.gitignore b/.gitignore index fa29746e8b..18dc0805ef 100644 --- a/.gitignore +++ b/.gitignore @@ -633,6 +633,9 @@ tools/unit-tests/unit-hifive1-flash-write tools/unit-tests/unit-hifive1-hfrosccfg tools/unit-tests/unit-keygen-keystore tools/unit-tests/unit-kontron-tgl-spi +tools/unit-tests/unit-fsp-nvs +tools/unit-tests/unit-fsp-nvs-save +tools/unit-tests/unit-tgl-spi-hwseq tools/unit-tests/unit-nrf5340-flash-protect tools/unit-tests/unit-nrf5340-uart-crlf tools/unit-tests/unit-nrf54l-uart-write @@ -678,6 +681,7 @@ tools/unit-tests/keystore_gen tools/unit-tests/kinetis_erase_extract.h tools/unit-tests/kontron_spi_extract.h tools/unit-tests/kontron_spi_fn_extract.h +tools/unit-tests/tgl_spi_hwseq_extract.h tools/unit-tests/nrf5340_protect_fn_extract.h tools/unit-tests/nrf54l_uart_write_extract.h tools/unit-tests/p1021_erase_extract.h diff --git a/arch.mk b/arch.mk index b5eccfc0ee..91304f10bc 100644 --- a/arch.mk +++ b/arch.mk @@ -2659,6 +2659,9 @@ ifeq ("${FSP}", "1") OBJS+=src/x86/tgl_fsp.o OBJS+=src/ucode0.o CFLAGS += -DUCODE0_ADDRESS=$(UCODE0_BASE) + ifeq ($(FSP_NVS_CACHE),1) + OBJS+=src/x86/fsp_nvs.o + endif endif else # building wolfBoot @@ -2705,6 +2708,9 @@ ifeq ("${FSP}", "1") endif ifeq ($(FSP_TGL), 1) OBJS+=src/x86/tgl_fsp.o + ifeq ($(FSP_NVS_CACHE),1) + OBJS+=src/x86/fsp_nvs.o + endif endif endif ifeq ($(64BIT),1) diff --git a/config/examples/nai_68int6.config b/config/examples/nai_68int6.config index 286f0301e4..457c59f723 100644 --- a/config/examples/nai_68int6.config +++ b/config/examples/nai_68int6.config @@ -41,6 +41,12 @@ BIOS_REGION_SIZE=0xc00000 UCODE0_BASE=0xffd90000 UCODE0_BIN=src/x86/ucode0.bin +# FSP memory training data cache: 128 KB of the BIOS region just below the +# wolfBoot window, so it survives a reflash of the window. +FSP_NVS_CACHE=1 +FSP_NVS_BASE=0xff9e0000 +FSP_NVS_SIZE=0x20000 + FSP_T_BIN=./src/x86/fsp_t.bin FSP_M_BIN=./src/x86/fsp_m.bin FSP_S_BIN=./src/x86/fsp_s.bin diff --git a/docs/Targets.md b/docs/Targets.md index a2af70712b..1359029b1b 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -9224,6 +9224,7 @@ Notable options in [config/examples/nai_68int6.config](../config/examples/nai_68 |---|---| | `X86_UART_NUMBER=2` | LPSS UART2 for all three FSP phases (Kontron uses 0) | | `ACPI=1` | Required. Generates ACPI tables (RSDP/XSDT/FADT/MADT/MCFG and a minimal DSDT); the board's PCI interrupt routing is only in the DSDT `_PRT`, since the MP table is the Kontron one. The DSDT also declares the board devices on LPSS I2C0 (EEPROMs, RTC/watchdog) and GSPI0 (the PRC, for NAI's `naigspi` driver) | +| `FSP_NVS_CACHE=1`, `FSP_NVS_BASE`, `FSP_NVS_SIZE` | Keeps FSP-M's memory training data (its non-volatile storage HOB) in a 128 KB area of the BIOS region below the wolfBoot window, so later boots skip full memory training. Stage 1 passes a valid copy back through `NvsBufferPtr`; stage 2 rewrites it, before FSP-S and before the BIOS region is locked, only when it changes. The record is keyed on a CRC of the FSP-M image and its UPDs, so a change to either falls back to full training. If FspMemInit fails with a saved record, stage 1 sets a CMOS flag and cold-resets, boots train from scratch until FspMemInit succeeds, and stage 2 replaces the record. `assemble_image.sh -n` blanks the area, so an image built from a board's flash dump does not carry that board's training data | | `ANTI_ROLLBACK=1` | Persistent version floor and boot audit counters in TPM NV | | `MEASURED_BOOT=1`, `MEASURED_PCR_OS=4` | Firmware into PCR 0, the verified OS image into PCR 4 | | `WOLFBOOT_BOOT_DISK=0` | Boot from GPT partitions; the A/B indices (sda3/sda4) are set in `src/update_disk.c` | diff --git a/hal/x86_fsp_tgl.c b/hal/x86_fsp_tgl.c index b0f5889d43..8dae2231e6 100644 --- a/hal/x86_fsp_tgl.c +++ b/hal/x86_fsp_tgl.c @@ -21,6 +21,7 @@ #include "wolfboot/wolfboot.h" #include +#include #ifdef __WOLFBOOT @@ -190,6 +191,156 @@ int hal_flash_protect(haladdr_t address, int len) * hook's address/len are the same range FREG1 describes. */ return tgl_lock_bios_region(); } + +#ifdef WOLFBOOT_FSP_NVS_CACHE +/* Hardware-sequencing flash cycles, used to save the FSP memory training + * data before the BIOS region is locked. Runs from RAM (stage 2) only. */ +#define SPI_BIOS_CNTL (0xdc) +#define SPI_BIOS_CNTL_WPD (1U << 0) +#define SPI_FADDR (0x8) +#define SPI_FDATA0 (0x10) +#define SPI_HSFSTS_FDONE (1U << 0) +#define SPI_HSFSTS_FCERR (1U << 1) +#define SPI_HSFSTS_AEL (1U << 2) +#define SPI_HSFSTS_SCIP (1U << 5) +#define SPI_HSFSTS_FGO (1U << 16) +#define SPI_HSFSTS_FCYCLE_SHIFT (17) +#define SPI_HSFSTS_FDBC_SHIFT (24) +#define SPI_FCYCLE_WRITE (2) +#define SPI_FCYCLE_ERASE_4K (3) +#define SPI_HWSEQ_MAX_DATA (64) +#define SPI_ERASE_SIZE (0x1000) +#define SPI_HWSEQ_TIMEOUT_MS (1000) + +static uint32_t tgl_spi_begin(uint32_t *cmd) +{ + uint32_t spi_bar, cntl; + + spi_bar = pci_config_read32(0, SPI_PCI_DEV, SPI_PCI_FUN, PCI_BAR0_OFFSET); + spi_bar &= PCI_BAR_MASK; + *cmd = pci_config_read32(0, SPI_PCI_DEV, SPI_PCI_FUN, PCI_COMMAND_OFFSET); + pci_config_write32(0, SPI_PCI_DEV, SPI_PCI_FUN, PCI_COMMAND_OFFSET, + *cmd | PCI_COMMAND_MEM_SPACE); + cntl = pci_config_read8(0, SPI_PCI_DEV, SPI_PCI_FUN, SPI_BIOS_CNTL); + pci_config_write8(0, SPI_PCI_DEV, SPI_PCI_FUN, SPI_BIOS_CNTL, + cntl | SPI_BIOS_CNTL_WPD); + if ((pci_config_read8(0, SPI_PCI_DEV, SPI_PCI_FUN, SPI_BIOS_CNTL) & + SPI_BIOS_CNTL_WPD) == 0) + return 0; + return spi_bar; +} + +static void tgl_spi_end(uint32_t cmd) +{ + uint8_t cntl; + + cntl = pci_config_read8(0, SPI_PCI_DEV, SPI_PCI_FUN, SPI_BIOS_CNTL); + pci_config_write8(0, SPI_PCI_DEV, SPI_PCI_FUN, SPI_BIOS_CNTL, + cntl & ~SPI_BIOS_CNTL_WPD); + pci_config_write32(0, SPI_PCI_DEV, SPI_PCI_FUN, PCI_COMMAND_OFFSET, cmd); +} + +/* The BIOS region is mapped so that its last byte sits at 4 GB - 1; + * returns the flash linear address of the memory-mapped address, or -1 + * when the range is outside the BIOS region. */ +static int64_t tgl_spi_linear(uint32_t spi_bar, uint32_t address, + uint32_t len) +{ + uint32_t reg, base, top; + uint64_t lin; + + reg = mmio_read32(spi_bar + SPI_FREG1); + base = (reg & SPI_FREG_BASE_MASK) << SPI_FREG_ADDR_SHIFT; + top = (((reg & SPI_FREG_LIMIT_MASK) >> SPI_FREG_LIMIT_SHIFT) + << SPI_FREG_ADDR_SHIFT) | 0xfffU; + if (top < base || len == 0) + return -1; + lin = (uint64_t)top - (0xffffffffULL - address); + if (lin < base || lin + len - 1 > top) + return -1; + return (int64_t)lin; +} + +static int tgl_spi_cycle(uint32_t spi_bar, uint32_t cycle, uint32_t faddr, + uint32_t len) +{ + uint32_t sts = 0; + int ms; + + for (ms = 0; ms < SPI_HWSEQ_TIMEOUT_MS; ms++) { + if ((mmio_read32(spi_bar + SPI_BIOS_HSFSTS_CTL) & + SPI_HSFSTS_SCIP) == 0) + break; + delay(1); + } + if (ms == SPI_HWSEQ_TIMEOUT_MS) + return -1; + mmio_write32(spi_bar + SPI_BIOS_HSFSTS_CTL, + SPI_HSFSTS_FDONE | SPI_HSFSTS_FCERR | SPI_HSFSTS_AEL); + mmio_write32(spi_bar + SPI_FADDR, faddr); + mmio_write32(spi_bar + SPI_BIOS_HSFSTS_CTL, + ((len - 1) << SPI_HSFSTS_FDBC_SHIFT) | + (cycle << SPI_HSFSTS_FCYCLE_SHIFT) | SPI_HSFSTS_FGO); + for (ms = 0; ms < SPI_HWSEQ_TIMEOUT_MS; ms++) { + sts = mmio_read32(spi_bar + SPI_BIOS_HSFSTS_CTL); + if (sts & (SPI_HSFSTS_FDONE | SPI_HSFSTS_FCERR)) + break; + delay(1); + } + mmio_write32(spi_bar + SPI_BIOS_HSFSTS_CTL, + SPI_HSFSTS_FDONE | SPI_HSFSTS_FCERR | SPI_HSFSTS_AEL); + if (ms == SPI_HWSEQ_TIMEOUT_MS || (sts & SPI_HSFSTS_FCERR)) + return -1; + return 0; +} + +int tgl_spi_flash_erase(uint32_t address, uint32_t len) +{ + uint32_t spi_bar, cmd, off; + int64_t lin; + int ret = 0; + + if ((address | len) & (SPI_ERASE_SIZE - 1)) + return -1; + spi_bar = tgl_spi_begin(&cmd); + lin = (spi_bar != 0) ? tgl_spi_linear(spi_bar, address, len) : -1; + if (lin < 0) + ret = -1; + for (off = 0; ret == 0 && off < len; off += SPI_ERASE_SIZE) + ret = tgl_spi_cycle(spi_bar, SPI_FCYCLE_ERASE_4K, + (uint32_t)lin + off, 1); + tgl_spi_end(cmd); + return ret; +} + +int tgl_spi_flash_write(uint32_t address, const uint8_t *buf, uint32_t len) +{ + uint32_t spi_bar, cmd, off, chunk, word, i; + int64_t lin; + int ret = 0; + + spi_bar = tgl_spi_begin(&cmd); + lin = (spi_bar != 0) ? tgl_spi_linear(spi_bar, address, len) : -1; + if (lin < 0) + ret = -1; + for (off = 0; ret == 0 && off < len; off += chunk) { + /* 64-byte aligned cycles never cross a flash page */ + chunk = SPI_HWSEQ_MAX_DATA - (((uint32_t)lin + off) & + (SPI_HWSEQ_MAX_DATA - 1)); + if (chunk > len - off) + chunk = len - off; + for (i = 0; i < chunk; i += 4) { + word = 0xffffffffU; + memcpy(&word, buf + off + i, (chunk - i < 4) ? chunk - i : 4); + mmio_write32(spi_bar + SPI_FDATA0 + i, word); + } + ret = tgl_spi_cycle(spi_bar, SPI_FCYCLE_WRITE, (uint32_t)lin + off, + chunk); + } + tgl_spi_end(cmd); + return ret; +} +#endif /* WOLFBOOT_FSP_NVS_CACHE */ #endif /* WOLFBOOT_TGL */ #endif diff --git a/include/stage2_params.h b/include/stage2_params.h index ef3c6e2c81..cb172bcc0c 100644 --- a/include/stage2_params.h +++ b/include/stage2_params.h @@ -31,6 +31,9 @@ struct stage2_parameter { /* Verified payload length, set by the disk loader before do_boot so the * Linux loader can bound the signed container header against the image. */ uint32_t payload_size; +#ifdef WOLFBOOT_FSP_NVS_CACHE + uint32_t fsp_nvs_key; +#endif #ifdef WOLFBOOT_TPM_SEAL uint32_t tpm_policy; uint16_t tpm_policy_size; diff --git a/include/x86/common.h b/include/x86/common.h index 954ae07a6f..8df5d87c94 100644 --- a/include/x86/common.h +++ b/include/x86/common.h @@ -60,6 +60,8 @@ uint16_t io_read16(uint16_t port); void io_write32(uint16_t port, uint32_t value); uint32_t io_read32(uint16_t port); void reset(uint8_t warm); +uint8_t cmos_read(uint8_t index); +void cmos_write(uint8_t index, uint8_t value); #ifdef WOLFBOOT_TGL int global_reset(void); #endif diff --git a/include/x86/fsp_nvs.h b/include/x86/fsp_nvs.h new file mode 100644 index 0000000000..920ab607a3 --- /dev/null +++ b/include/x86/fsp_nvs.h @@ -0,0 +1,48 @@ +/* fsp_nvs.h + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ +#ifndef FSP_NVS_H +#define FSP_NVS_H + +#include + +/* Flash record holding the FSP-M non-volatile storage (memory training) + * data. The key covers the FSP-M image and its UPDs, so a change to either + * invalidates the record. */ +#define FSP_NVS_MAGIC 0x5356464BU /* "KFVS" */ + +struct fsp_nvs_record { + uint32_t magic; + uint32_t key; + uint32_t len; + uint32_t crc; + uint32_t reserved[4]; + uint8_t data[]; +} __attribute__((packed)); + +uint32_t fsp_nvs_crc32(uint32_t crc, const uint8_t *buf, uint32_t len); +int fsp_nvs_record_valid(const struct fsp_nvs_record *rec, uint32_t key, + uint32_t area_size); +int fsp_nvs_apply(uint8_t *udp_m, uint32_t udp_size, const uint8_t *fsp_m, + uint32_t fsp_m_size); +void fsp_nvs_skip(int skip); +void fsp_nvs_save(void); + +#endif /* FSP_NVS_H */ diff --git a/include/x86/tgl_fsp.h b/include/x86/tgl_fsp.h index 5145fbc185..2dfcf380ea 100644 --- a/include/x86/tgl_fsp.h +++ b/include/x86/tgl_fsp.h @@ -25,5 +25,7 @@ int x86_fsp_tgl_init_sata(uint32_t *bar); int tgl_lock_bios_region(void); +int tgl_spi_flash_erase(uint32_t address, uint32_t len); +int tgl_spi_flash_write(uint32_t address, const uint8_t *buf, uint32_t len); #endif /* TGL_FSP_H */ diff --git a/options.mk b/options.mk index d409e75d2a..e0965cf8b6 100644 --- a/options.mk +++ b/options.mk @@ -2065,6 +2065,17 @@ ifeq ($(FSP), 1) # set CFLAGS defines for each x86_fsp option $(foreach option,$(X86_FSP_OPTIONS),$(if $($(option)), $(eval CFLAGS += -D$(option)=$($(option))))) + + ifeq ($(FSP_NVS_CACHE),1) + ifeq ($(and $(FSP_NVS_BASE),$(FSP_NVS_SIZE)),) + $(error FSP_NVS_CACHE=1 needs FSP_NVS_BASE and FSP_NVS_SIZE) + endif + ifeq ($(filter $(TARGET),kontron_vx3060_s2 nai_68int6),) + $(error FSP_NVS_CACHE=1 is only supported on the Tiger Lake targets) + endif + CFLAGS+=-DWOLFBOOT_FSP_NVS_CACHE + CFLAGS+=-DFSP_NVS_BASE=$(FSP_NVS_BASE) -DFSP_NVS_SIZE=$(FSP_NVS_SIZE) + endif endif ifeq ($(FLASH_MULTI_SECTOR_ERASE),1) diff --git a/src/boot_x86_fsp.c b/src/boot_x86_fsp.c index 1c13dc0c5e..81239ba233 100644 --- a/src/boot_x86_fsp.c +++ b/src/boot_x86_fsp.c @@ -42,6 +42,9 @@ #include #include #endif +#ifdef WOLFBOOT_FSP_NVS_CACHE +#include +#endif #define WOLFBOOT_X86_STACK_SIZE 0x10000 @@ -542,6 +545,9 @@ void start(uint32_t stack_base, uint32_t stack_top, uint64_t timestamp, uint32_t status; uint16_t type; uint32_t esp; +#if defined(WOLFBOOT_FSP_NVS_CACHE) && defined(BUILD_LOADER_STAGE1) + int nvs_used; +#endif (void)stack_top; (void)timestamp; @@ -586,6 +592,11 @@ void start(uint32_t stack_base, uint32_t stack_top, uint64_t timestamp, if (status != 0) { panic(); } +#if defined(WOLFBOOT_FSP_NVS_CACHE) && defined(BUILD_LOADER_STAGE1) + nvs_used = fsp_nvs_apply(udp_m_parameter, + fsp_m_info_header->CfgRegionSize, fsp_m_base, + fsp_m_info_header->ImageSize); +#endif #if defined(WOLFBOOT_DUMP_FSP_UPD) wolfBoot_printf("Dumping fspm udp (%d bytes)" ENDLINE, (int)fsp_m_info_header->CfgRegionSize); @@ -604,8 +615,17 @@ void start(uint32_t stack_base, uint32_t stack_top, uint64_t timestamp, fsp_handle_reset(status); if (status != EFI_SUCCESS) { wolfBoot_printf("failed: 0x%x" ENDLINE, status); +#if defined(WOLFBOOT_FSP_NVS_CACHE) && defined(BUILD_LOADER_STAGE1) + if (nvs_used) { + fsp_nvs_skip(1); + reset(0); + } +#endif panic(); } +#if defined(WOLFBOOT_FSP_NVS_CACHE) && defined(BUILD_LOADER_STAGE1) + fsp_nvs_skip(0); +#endif wolfBoot_printf("success" ENDLINE); status = get_top_address(&top_address, hobList); diff --git a/src/x86/common.c b/src/x86/common.c index 1da8926c5f..38e07c5522 100644 --- a/src/x86/common.c +++ b/src/x86/common.c @@ -236,6 +236,30 @@ uint8_t io_read8(uint16_t port) return v; } +/* CMOS/RTC scratch RAM (0x70/0x71). Bytes 0x40-0x7F are outside the RTC + * checksum and survive a 0xCF9 reset but not loss of the RTC well. Bit 7 of + * the index port is NMI-disable and must be preserved. */ +#define CMOS_INDEX_PORT 0x70 +#define CMOS_DATA_PORT 0x71 + +uint8_t cmos_read(uint8_t index) +{ + uint8_t nmi; + + nmi = io_read8(CMOS_INDEX_PORT) & 0x80; + io_write8(CMOS_INDEX_PORT, nmi | (index & 0x7F)); + return io_read8(CMOS_DATA_PORT); +} + +void cmos_write(uint8_t index, uint8_t value) +{ + uint8_t nmi; + + nmi = io_read8(CMOS_INDEX_PORT) & 0x80; + io_write8(CMOS_INDEX_PORT, nmi | (index & 0x7F)); + io_write8(CMOS_DATA_PORT, value); +} + /** * @brief Reset the system. * diff --git a/src/x86/fsp.c b/src/x86/fsp.c index 0ed59ec439..4158f32c65 100644 --- a/src/x86/fsp.c +++ b/src/x86/fsp.c @@ -25,6 +25,9 @@ #include #include #include +#ifdef WOLFBOOT_FSP_NVS_CACHE +#include +#endif #define FSP_INFO_HEADER_OFFSET 0x94 #define EFI_SUCCESS 0x0 @@ -162,6 +165,9 @@ void fsp_init_silicon(void) panic(); } wolfBoot_printf("success"); +#if defined(WOLFBOOT_FSP_NVS_CACHE) && !defined(BUILD_LOADER_STAGE1) + fsp_nvs_save(); +#endif memcpy(_fsp_s_base_start, _start_fsp_s, _end_fsp_s - _start_fsp_s); fsp_s_hdr = (struct fsp_info_header*)(_fsp_s_base_start + FSP_INFO_HEADER_OFFSET); silicon_init_off = fsp_s_hdr->FspSiliconInitEntryOffset; diff --git a/src/x86/fsp_nvs.c b/src/x86/fsp_nvs.c new file mode 100644 index 0000000000..24be9cfdee --- /dev/null +++ b/src/x86/fsp_nvs.c @@ -0,0 +1,202 @@ +/* fsp_nvs.c + * + * Cache of the FSP-M memory training data in SPI flash. Stage 1 hands a + * valid saved copy back to FspMemInit through NvsBufferPtr so the memory + * reference code takes its fast path; stage 2 saves the data FSP-M returned + * in its non-volatile storage HOB whenever it differs from the saved copy. + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#if !defined(FSP_NVS_BASE) || !defined(FSP_NVS_SIZE) +#error "FSP_NVS_CACHE needs FSP_NVS_BASE and FSP_NVS_SIZE" +#endif + +#define FSP_NVS_SECTOR_SIZE 0x1000U +#if (FSP_NVS_SIZE) == 0 || ((FSP_NVS_SIZE) % FSP_NVS_SECTOR_SIZE) != 0 +#error "FSP_NVS_SIZE must be a non-zero multiple of the 4 KB erase size" +#endif +#ifdef BOOTLOADER_PARTITION_SIZE +#if (FSP_NVS_BASE) + (FSP_NVS_SIZE) > 0x100000000ULL - (BOOTLOADER_PARTITION_SIZE) +#error "the FSP_NVS area must lie below the wolfBoot flash window" +#endif +#endif + +#ifndef FSP_NVS_CACHE_FLUSH +/* The flash window is cached; drop stale lines before the read-back */ +#define FSP_NVS_CACHE_FLUSH() __asm__ volatile ("wbinvd" ::: "memory") +#endif + +uint32_t fsp_nvs_crc32(uint32_t crc, const uint8_t *buf, uint32_t len) +{ + int i; + + crc = ~crc; + while (len-- > 0) { + crc ^= *buf++; + for (i = 0; i < 8; i++) + crc = (crc >> 1) ^ (0xEDB88320U & (0U - (crc & 1U))); + } + return ~crc; +} + +int fsp_nvs_record_valid(const struct fsp_nvs_record *rec, uint32_t key, + uint32_t area_size) +{ + if (area_size <= sizeof(*rec)) + return 0; + if (rec->magic != FSP_NVS_MAGIC || rec->key != key) + return 0; + if (rec->len == 0 || rec->len > area_size - sizeof(*rec)) + return 0; + return fsp_nvs_crc32(0, rec->data, rec->len) == rec->crc; +} + +#ifdef BUILD_LOADER_STAGE1 +/* CMOS byte set when FspMemInit failed with the saved record. Boots train + * from scratch until FspMemInit succeeds without it, across the resets FSP-M + * requests, and stage 2 then replaces the record. Stage 1 runs from SPI + * flash, so it cannot erase the record itself. */ +#define FSP_NVS_SKIP_CMOS_IDX 0x52 +#define FSP_NVS_SKIP_CMOS_MAGIC 0xA5 + +void fsp_nvs_skip(int skip) +{ + cmos_write(FSP_NVS_SKIP_CMOS_IDX, skip ? FSP_NVS_SKIP_CMOS_MAGIC : 0); +} + +/* Returns 1 when FspMemInit is given the saved record. */ +int fsp_nvs_apply(uint8_t *udp_m, uint32_t udp_size, const uint8_t *fsp_m, + uint32_t fsp_m_size) +{ + const struct fsp_nvs_record *rec = + (const struct fsp_nvs_record *)(uintptr_t)FSP_NVS_BASE; + FSPM_ARCH_UPD *arch = (FSPM_ARCH_UPD *)(udp_m + sizeof(FSP_UPD_HEADER)); + uint32_t key; + + /* The key is taken before NvsBufferPtr and BootMode are changed, so + * stage 2 can match it against the UPDs this boot trained with. It + * covers the whole FSP-M image: different FSP-M builds can report the + * same revision. */ + key = fsp_nvs_crc32(0, fsp_m, fsp_m_size); + key = fsp_nvs_crc32(key, udp_m, udp_size); + stage2_get_parameters()->fsp_nvs_key = key; + + if (cmos_read(FSP_NVS_SKIP_CMOS_IDX) == FSP_NVS_SKIP_CMOS_MAGIC) { + wolfBoot_printf("FSP NVS: saved training data failed, retraining\r\n"); + return 0; + } + if (!fsp_nvs_record_valid(rec, key, FSP_NVS_SIZE)) { + wolfBoot_printf("FSP NVS: no saved training data\r\n"); + return 0; + } + arch->NvsBufferPtr = (uint32_t)(uintptr_t)rec->data; + arch->BootMode = BOOT_ASSUMING_NO_CONFIGURATION_CHANGES; + wolfBoot_printf("FSP NVS: using saved training data (%d bytes)\r\n", + (int)rec->len); + return 1; +} +#else +/* FSP_NON_VOLATILE_STORAGE_HOB_GUID */ +static const struct efi_guid fsp_nvs_hob_guid = { + 0x721acf02, 0x4d77, 0x4c2a, + { 0xb3, 0xdc, 0x27, 0x0b, 0x7b, 0xa9, 0xe4, 0xb0 } +}; + +static struct efi_hob_guid_type *fsp_nvs_find_hob(struct efi_hob *hob) +{ + while (hob != NULL && + hob_get_type(hob) != EFI_HOB_TYPE_END_OF_HOB_LIST) { + if (hob_get_length(hob) < sizeof(struct efi_hob_generic_header)) + return NULL; + if (hob_get_type(hob) == EFI_HOB_TYPE_GUID_EXTENSION && + hob_get_length(hob) > sizeof(struct efi_hob_guid_type) && + hob_guid_equals(&hob->u.guid.name, + (struct efi_guid *)&fsp_nvs_hob_guid)) + return &hob->u.guid; + hob = hob_get_next(hob); + } + return NULL; +} + +static int fsp_nvs_is_current(const struct fsp_nvs_record *rec, uint32_t key, + const uint8_t *data, uint32_t len) +{ + return fsp_nvs_record_valid(rec, key, FSP_NVS_SIZE) && rec->len == len && + memcmp(rec->data, data, len) == 0; +} + +void fsp_nvs_save(void) +{ + const struct fsp_nvs_record *rec = + (const struct fsp_nvs_record *)(uintptr_t)FSP_NVS_BASE; + struct stage2_parameter *params = stage2_get_parameters(); + struct efi_hob_guid_type *hob; + struct fsp_nvs_record hdr; + const uint8_t *data; + uint32_t len, erase_len; + + hob = fsp_nvs_find_hob((struct efi_hob *)(uintptr_t)params->hobList); + if (hob == NULL) { + wolfBoot_printf("FSP NVS: no training data HOB\r\n"); + return; + } + data = (const uint8_t *)(hob + 1); + len = hob->header.hob_length - sizeof(*hob); + if (len > FSP_NVS_SIZE - sizeof(hdr)) { + wolfBoot_printf("FSP NVS: training data (%d bytes) exceeds the cache\r\n", + (int)len); + return; + } + if (fsp_nvs_is_current(rec, params->fsp_nvs_key, data, len)) + return; + + memset(&hdr, 0, sizeof(hdr)); + hdr.magic = FSP_NVS_MAGIC; + hdr.key = params->fsp_nvs_key; + hdr.len = len; + hdr.crc = fsp_nvs_crc32(0, data, len); + erase_len = (sizeof(hdr) + len + FSP_NVS_SECTOR_SIZE - 1) & + ~(FSP_NVS_SECTOR_SIZE - 1); + /* The header goes last, so an interrupted save leaves no valid record */ + if (tgl_spi_flash_erase(FSP_NVS_BASE, erase_len) != 0 || + tgl_spi_flash_write(FSP_NVS_BASE + sizeof(hdr), data, len) != 0 || + tgl_spi_flash_write(FSP_NVS_BASE, (const uint8_t *)&hdr, + sizeof(hdr)) != 0) { + wolfBoot_printf("FSP NVS: saving training data failed\r\n"); + return; + } + FSP_NVS_CACHE_FLUSH(); + if (!fsp_nvs_is_current(rec, hdr.key, data, len)) { + wolfBoot_printf("FSP NVS: training data read-back mismatch\r\n"); + return; + } + wolfBoot_printf("FSP NVS: saved %d bytes of training data\r\n", (int)len); +} +#endif /* BUILD_LOADER_STAGE1 */ diff --git a/src/x86/tgl_fsp.c b/src/x86/tgl_fsp.c index c9092ba221..29cb99f000 100644 --- a/src/x86/tgl_fsp.c +++ b/src/x86/tgl_fsp.c @@ -677,7 +677,13 @@ static void fsp_set_silicon_cfg(FSPS_UPD *fsps) upd->Usb2PhyPehalfbit[15] = 0; upd->PchTsnEnable[0] = 0; upd->PchTsnEnable[1] = 0; +#ifdef TARGET_nai_68int6 + /* No CNVi module is fitted; forcing Disable makes FSP-S request a global + * reset on every cold boot, while Auto finds nothing and does not */ + upd->CnviMode = 1; +#else upd->CnviMode = 0; +#endif upd->CnviBtCore = 0; upd->SataLedEnable = 1; upd->IomTypeCPortPadCfg[0] = 0; diff --git a/tools/scripts/x86_fsp/tgl/assemble_image.sh b/tools/scripts/x86_fsp/tgl/assemble_image.sh index 50cad1ec51..13d06de1fe 100755 --- a/tools/scripts/x86_fsp/tgl/assemble_image.sh +++ b/tools/scripts/x86_fsp/tgl/assemble_image.sh @@ -7,6 +7,24 @@ BIOS_REGION_PATH=/tmp/bios.bin set -e +# The FSP-M training data cache holds one board's memory training. A flash +# dump of a board that has run wolfBoot carries it, so blank it rather than +# copy it to every board flashed with this image. +erase_fsp_nvs_cache() +{ + local base size image_size offset + + grep -q '^FSP_NVS_CACHE=1$' .config || return 0 + base=$(sed -n 's/^FSP_NVS_BASE=//p' .config) + size=$(sed -n 's/^FSP_NVS_SIZE=//p' .config) + image_size=$(stat -c%s "$WOLFBOOT_DIR/final_image.bin") + # the image ends at 4 GB + offset=$((image_size - (0x100000000 - base))) + head -c $((size)) /dev/zero | tr '\0' '\377' | \ + dd of="$WOLFBOOT_DIR/final_image.bin" bs=64K oflag=seek_bytes \ + seek=$offset conv=notrunc status=none +} + build_and_sign_image() { # compute the size differences between $FLASH_DUMP and "$WOLFBOOT_DIR"/wolfboot_stage1.bin and store it in SIZE @@ -16,6 +34,7 @@ build_and_sign_image() cp "$FLASH_DUMP" "$WOLFBOOT_DIR/temp_image.bin" truncate -s $SIZE "$WOLFBOOT_DIR/temp_image.bin" cat "$WOLFBOOT_DIR/temp_image.bin" "$BIOS_REGION_PATH" > "$WOLFBOOT_DIR/final_image.bin" + erase_fsp_nvs_cache if grep -q '^WOLFBOOT_TPM_SEAL=1$' .config; then PCR0=$(python ./tools/scripts/x86_fsp/compute_pcr.py "$WOLFBOOT_DIR"/final_image.bin | tail -n 1) "$WOLFBOOT_DIR"/tools/tpm/policy_sign -ecc256 -key=tpm_seal_key.key -pcr=0 -pcrdigest="$PCR0" diff --git a/tools/unit-tests/Makefile b/tools/unit-tests/Makefile index c58d43262e..abe00e18f8 100644 --- a/tools/unit-tests/Makefile +++ b/tools/unit-tests/Makefile @@ -174,6 +174,9 @@ TESTS+=unit-t10xx-flash-status TESTS+=unit-p1021-erase-advance TESTS+=unit-p1021-read-badblock TESTS+=unit-kontron-tgl-spi +TESTS+=unit-fsp-nvs +TESTS+=unit-fsp-nvs-save +TESTS+=unit-tgl-spi-hwseq TESTS+=unit-nrf5340-flash-protect TESTS+=unit-keygen-keystore TESTS+=unit-samr21-erase-advance @@ -2263,6 +2266,32 @@ unit-kontron-tgl-spi: unit-kontron-tgl-spi.c kontron_spi_extract.h \ kontron_spi_fn_extract.h gcc -o $@ unit-kontron-tgl-spi.c $(CFLAGS) $(LDFLAGS) +# unit-tgl-spi-hwseq runs the real SPI hardware-sequencing erase/write from +# hal/x86_fsp_tgl.c (FSP memory training data cache) against a model of the +# SPI controller registers and a RAM-backed flash window. +tgl_spi_hwseq_extract.h: ../../hal/x86_fsp_tgl.c + sed -n '/^#ifdef WOLFBOOT_FSP_NVS_CACHE/,/^#endif \/\* WOLFBOOT_FSP_NVS_CACHE \*\//p' $< > $@ + +unit-tgl-spi-hwseq:CFLAGS+=-DWOLFBOOT_FSP_NVS_CACHE +unit-tgl-spi-hwseq: unit-tgl-spi-hwseq.c kontron_spi_extract.h \ + tgl_spi_hwseq_extract.h + gcc -o $@ unit-tgl-spi-hwseq.c $(CFLAGS) $(LDFLAGS) + +# unit-fsp-nvs runs the FSP-M memory training data cache record check and +# the stage 1 hand-off (src/x86/fsp_nvs.c) against a RAM-backed area. +unit-fsp-nvs:CFLAGS+=-DWOLFBOOT_FSP -DWOLFBOOT_FSP_NVS_CACHE \ + -DBUILD_LOADER_STAGE1 -DPRINTF_ENABLED +unit-fsp-nvs: unit-fsp-nvs.c ../../src/x86/fsp_nvs.c + gcc -o $@ unit-fsp-nvs.c $(CFLAGS) $(LDFLAGS) + +# unit-fsp-nvs-save runs the stage 2 save path against a synthetic HOB list +# and a RAM-backed flash area; built non-PIE so the HOB list sits below 4 GB +# (stage2_parameter.hobList is 32-bit). +unit-fsp-nvs-save:CFLAGS+=-DWOLFBOOT_FSP -DWOLFBOOT_FSP_NVS_CACHE \ + -DPRINTF_ENABLED -fno-pie +unit-fsp-nvs-save: unit-fsp-nvs-save.c ../../src/x86/fsp_nvs.c ../../src/x86/hob.c + gcc -o $@ unit-fsp-nvs-save.c $(CFLAGS) -no-pie $(LDFLAGS) + # unit-nrf5340-flash-protect runs the real hal_flash_protect() from # hal/nrf5340.c against a mock SPU region-permission array (F-13623: the # region count truncated, so a sub-block or partial-tail len left flash diff --git a/tools/unit-tests/unit-fsp-nvs-save.c b/tools/unit-tests/unit-fsp-nvs-save.c new file mode 100644 index 0000000000..4c02fa5385 --- /dev/null +++ b/tools/unit-tests/unit-fsp-nvs-save.c @@ -0,0 +1,254 @@ +/* unit-fsp-nvs-save.c + * + * Unit tests for the stage 2 half of the FSP-M memory training data cache + * (src/x86/fsp_nvs.c): finding the non-volatile storage HOB, deciding + * whether the saved copy is current, and the erase/write sequence, against + * a RAM-backed flash area and a synthetic HOB list. + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#include +#include +#include + +#define AREA_SIZE 0x2000U +static uint8_t g_area[AREA_SIZE] __attribute__((aligned(4096))); + +#define FSP_NVS_BASE ((uintptr_t)g_area) +#define FSP_NVS_SIZE AREA_SIZE +#define FSP_NVS_CACHE_FLUSH() do { } while (0) + +#include "stage2_params.h" + +static struct stage2_parameter g_params; +static int g_erase_calls, g_write_calls, g_fail_write_at; + +struct stage2_parameter *stage2_get_parameters(void) +{ + return &g_params; +} + +int tgl_spi_flash_erase(uint32_t address, uint32_t len) +{ + uintptr_t off = (uintptr_t)address - (uintptr_t)(uint32_t)FSP_NVS_BASE; + + g_erase_calls++; + ck_assert_uint_eq(address & 0xfff, 0); + ck_assert_uint_eq(len & 0xfff, 0); + ck_assert_uint_le(off + len, AREA_SIZE); + memset(g_area + off, 0xff, len); + return 0; +} + +int tgl_spi_flash_write(uint32_t address, const uint8_t *buf, uint32_t len) +{ + uintptr_t off = (uintptr_t)address - (uintptr_t)(uint32_t)FSP_NVS_BASE; + + g_write_calls++; + if (g_fail_write_at == g_write_calls) + return -1; + ck_assert_uint_le(off + len, AREA_SIZE); + memcpy(g_area + off, buf, len); + return 0; +} + +#include "../../src/x86/hob.c" +/* FSP_NVS_BASE is a RAM buffer here, not a flash address */ +#undef BOOTLOADER_PARTITION_SIZE +#include "../../src/x86/fsp_nvs.c" + +/* HOB list in static storage: the non-PIE build keeps it below 4 GB, as + * stage2_parameter.hobList is 32-bit. */ +static uint8_t g_hobs[0x4000] __attribute__((aligned(8))); + +static uint8_t *add_guid_hob(uint8_t *p, const struct efi_guid *guid, + uint32_t data_len, uint8_t seed) +{ + struct efi_hob_guid_type *h = (struct efi_hob_guid_type *)p; + uint32_t i, total = (uint32_t)sizeof(*h) + data_len; + + total = (total + 7U) & ~7U; + h->header.hob_type = EFI_HOB_TYPE_GUID_EXTENSION; + h->header.hob_length = (uint16_t)total; + h->header.reserved = 0; + memcpy(&h->name, guid, sizeof(*guid)); + for (i = 0; i < data_len; i++) + p[sizeof(*h) + i] = (uint8_t)(seed + i); + return p + total; +} + +static void end_hobs(uint8_t *p) +{ + struct efi_hob_generic_header *h = (struct efi_hob_generic_header *)p; + + h->hob_type = EFI_HOB_TYPE_END_OF_HOB_LIST; + h->hob_length = sizeof(*h); + h->reserved = 0; +} + +static const struct efi_guid other_guid = { + 0x11111111, 0x2222, 0x3333, { 1, 2, 3, 4, 5, 6, 7, 8 } +}; + +/* An unrelated GUID HOB, then the NVS HOB with data_len bytes. */ +static void build_hobs(uint32_t data_len, uint8_t seed) +{ + uint8_t *p = g_hobs; + + memset(g_hobs, 0, sizeof(g_hobs)); + p = add_guid_hob(p, &other_guid, 24, 0x55); + p = add_guid_hob(p, &fsp_nvs_hob_guid, data_len, seed); + end_hobs(p); +} + +static void setup(uint32_t key) +{ + memset(&g_params, 0, sizeof(g_params)); + g_params.hobList = (uint32_t)(uintptr_t)g_hobs; + g_params.fsp_nvs_key = key; + g_erase_calls = g_write_calls = g_fail_write_at = 0; +} + +static const struct fsp_nvs_record *area(void) +{ + return (const struct fsp_nvs_record *)g_area; +} + +START_TEST(test_save_into_empty_area) +{ + ck_assert_uint_lt((uintptr_t)g_hobs, 0x100000000ULL); + memset(g_area, 0xff, sizeof(g_area)); + setup(0xabcd); + build_hobs(1000, 3); + fsp_nvs_save(); + ck_assert_int_eq(g_erase_calls, 1); + ck_assert_int_eq(fsp_nvs_record_valid(area(), 0xabcd, AREA_SIZE), 1); + ck_assert_uint_eq(area()->len, 1000); + /* the unrelated HOB is 48 bytes, then the NVS HOB header */ + ck_assert_int_eq(memcmp(area()->data, + g_hobs + 48 + sizeof(struct efi_hob_guid_type), 1000), 0); +} +END_TEST + +START_TEST(test_current_copy_is_not_rewritten) +{ + memset(g_area, 0xff, sizeof(g_area)); + setup(0xabcd); + build_hobs(1000, 3); + fsp_nvs_save(); + setup(0xabcd); + fsp_nvs_save(); + ck_assert_int_eq(g_erase_calls, 0); + ck_assert_int_eq(g_write_calls, 0); +} +END_TEST + +START_TEST(test_changed_data_or_key_is_rewritten) +{ + memset(g_area, 0xff, sizeof(g_area)); + setup(0xabcd); + build_hobs(1000, 3); + fsp_nvs_save(); + + setup(0xabcd); + build_hobs(1000, 4); + fsp_nvs_save(); + ck_assert_int_eq(g_erase_calls, 1); + ck_assert_uint_eq(area()->data[0], 4); + + setup(0x1234); + fsp_nvs_save(); + ck_assert_int_eq(g_erase_calls, 1); + ck_assert_int_eq(fsp_nvs_record_valid(area(), 0x1234, AREA_SIZE), 1); +} +END_TEST + +START_TEST(test_missing_or_oversized_hob) +{ + uint8_t *p; + + memset(g_area, 0xff, sizeof(g_area)); + setup(1); + memset(g_hobs, 0, sizeof(g_hobs)); + p = add_guid_hob(g_hobs, &other_guid, 24, 0); + end_hobs(p); + fsp_nvs_save(); + ck_assert_int_eq(g_erase_calls, 0); + + setup(1); + build_hobs(AREA_SIZE - sizeof(struct fsp_nvs_record) + 1, 0); + fsp_nvs_save(); + ck_assert_int_eq(g_erase_calls, 0); +} +END_TEST + +START_TEST(test_zero_length_hob_stops_the_walk) +{ + struct efi_hob_generic_header *h = + (struct efi_hob_generic_header *)g_hobs; + + memset(g_area, 0xff, sizeof(g_area)); + setup(1); + memset(g_hobs, 0, sizeof(g_hobs)); + h->hob_type = EFI_HOB_TYPE_GUID_EXTENSION; + h->hob_length = 0; + fsp_nvs_save(); + ck_assert_int_eq(g_erase_calls, 0); +} +END_TEST + +START_TEST(test_failed_header_write_leaves_no_record) +{ + memset(g_area, 0xff, sizeof(g_area)); + setup(7); + build_hobs(1000, 3); + /* the data is written first, then the header; fail the header */ + g_fail_write_at = 2; + fsp_nvs_save(); + ck_assert_int_eq(fsp_nvs_record_valid(area(), 7, AREA_SIZE), 0); +} +END_TEST + +static Suite *fsp_nvs_save_suite(void) +{ + Suite *s = suite_create("fsp-nvs-save"); + TCase *tc = tcase_create("fsp-nvs-save"); + + tcase_add_test(tc, test_save_into_empty_area); + tcase_add_test(tc, test_current_copy_is_not_rewritten); + tcase_add_test(tc, test_changed_data_or_key_is_rewritten); + tcase_add_test(tc, test_missing_or_oversized_hob); + tcase_add_test(tc, test_zero_length_hob_stops_the_walk); + tcase_add_test(tc, test_failed_header_write_leaves_no_record); + suite_add_tcase(s, tc); + return s; +} + +int main(void) +{ + int fails; + Suite *s = fsp_nvs_save_suite(); + SRunner *sr = srunner_create(s); + + srunner_run_all(sr, CK_NORMAL); + fails = srunner_ntests_failed(sr); + srunner_free(sr); + return fails; +} diff --git a/tools/unit-tests/unit-fsp-nvs.c b/tools/unit-tests/unit-fsp-nvs.c new file mode 100644 index 0000000000..84b4c0612b --- /dev/null +++ b/tools/unit-tests/unit-fsp-nvs.c @@ -0,0 +1,247 @@ +/* unit-fsp-nvs.c + * + * Unit tests for the FSP-M memory training data cache (src/x86/fsp_nvs.c): + * the record check that decides whether saved data is handed back to + * FspMemInit, and the stage 1 hand-off itself. + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#include +#include +#include + +#define AREA_SIZE 0x1000U +static uint8_t g_area[AREA_SIZE] __attribute__((aligned(16))); + +#define FSP_NVS_BASE ((uintptr_t)g_area) +#define FSP_NVS_SIZE AREA_SIZE + +#include "stage2_params.h" + +static struct stage2_parameter g_params; + +struct stage2_parameter *stage2_get_parameters(void) +{ + return &g_params; +} + +static uint8_t g_cmos[128]; + +uint8_t cmos_read(uint8_t index) +{ + return g_cmos[index & 0x7F]; +} + +void cmos_write(uint8_t index, uint8_t value) +{ + g_cmos[index & 0x7F] = value; +} + +/* FSP_NVS_BASE is a RAM buffer here, not a flash address */ +#undef BOOTLOADER_PARTITION_SIZE +#include "../../src/x86/fsp_nvs.c" + +#define UDP_SIZE 0x100U +#define FSPM_SIZE 0x200U + +static uint8_t g_udp[UDP_SIZE]; +static uint8_t g_fspm[FSPM_SIZE]; + +static FSPM_ARCH_UPD *arch(void) +{ + return (FSPM_ARCH_UPD *)(g_udp + sizeof(FSP_UPD_HEADER)); +} + +static uint32_t expected_key(void) +{ + uint32_t key = fsp_nvs_crc32(0, g_fspm, FSPM_SIZE); + + return fsp_nvs_crc32(key, g_udp, UDP_SIZE); +} + +static struct fsp_nvs_record *write_record(uint32_t key, uint32_t len) +{ + struct fsp_nvs_record *rec = (struct fsp_nvs_record *)g_area; + uint32_t i; + + memset(g_area, 0xff, sizeof(g_area)); + rec->magic = FSP_NVS_MAGIC; + rec->key = key; + rec->len = len; + for (i = 0; i < len; i++) + rec->data[i] = (uint8_t)(i * 7 + 1); + rec->crc = fsp_nvs_crc32(0, rec->data, len); + return rec; +} + +static void reset_udp(void) +{ + uint32_t i; + + for (i = 0; i < UDP_SIZE; i++) + g_udp[i] = (uint8_t)i; + for (i = 0; i < FSPM_SIZE; i++) + g_fspm[i] = (uint8_t)(i * 3); + arch()->NvsBufferPtr = 0; + arch()->BootMode = BOOT_WITH_FULL_CONFIGURATION; + memset(&g_params, 0, sizeof(g_params)); + memset(g_cmos, 0, sizeof(g_cmos)); +} + +START_TEST(test_crc32_check_value) +{ + ck_assert_uint_eq(fsp_nvs_crc32(0, (const uint8_t *)"123456789", 9), + 0xCBF43926U); + /* chaining two halves equals one pass */ + ck_assert_uint_eq(fsp_nvs_crc32(fsp_nvs_crc32(0, + (const uint8_t *)"1234", 4), + (const uint8_t *)"56789", 5), 0xCBF43926U); +} +END_TEST + +START_TEST(test_record_valid) +{ + struct fsp_nvs_record *rec = write_record(0x1234, 100); + + ck_assert_int_eq(fsp_nvs_record_valid(rec, 0x1234, AREA_SIZE), 1); + ck_assert_int_eq(fsp_nvs_record_valid(rec, 0x1235, AREA_SIZE), 0); +} +END_TEST + +START_TEST(test_record_rejects_bad_fields) +{ + struct fsp_nvs_record *rec; + + rec = write_record(1, 100); + rec->magic ^= 1; + ck_assert_int_eq(fsp_nvs_record_valid(rec, 1, AREA_SIZE), 0); + + rec = write_record(1, 100); + rec->data[50] ^= 0x80; + ck_assert_int_eq(fsp_nvs_record_valid(rec, 1, AREA_SIZE), 0); + + rec = write_record(1, 100); + rec->len = 0; + ck_assert_int_eq(fsp_nvs_record_valid(rec, 1, AREA_SIZE), 0); + + /* data exactly filling the area is accepted, one byte more is not */ + rec = write_record(1, AREA_SIZE - sizeof(*rec)); + ck_assert_int_eq(fsp_nvs_record_valid(rec, 1, AREA_SIZE), 1); + rec->len = AREA_SIZE - sizeof(*rec) + 1; + ck_assert_int_eq(fsp_nvs_record_valid(rec, 1, AREA_SIZE), 0); + rec->len = 0xffffffffU; + ck_assert_int_eq(fsp_nvs_record_valid(rec, 1, AREA_SIZE), 0); + + /* an area too small for the header is never valid */ + rec = write_record(1, 4); + ck_assert_int_eq(fsp_nvs_record_valid(rec, 1, sizeof(*rec)), 0); +} +END_TEST + +START_TEST(test_apply_without_record) +{ + reset_udp(); + memset(g_area, 0xff, sizeof(g_area)); + ck_assert_int_eq(fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE), 0); + ck_assert_uint_eq(arch()->NvsBufferPtr, 0); + ck_assert_uint_eq(arch()->BootMode, BOOT_WITH_FULL_CONFIGURATION); + ck_assert_uint_eq(g_params.fsp_nvs_key, expected_key()); +} +END_TEST + +START_TEST(test_apply_with_matching_record) +{ + struct fsp_nvs_record *rec; + + reset_udp(); + rec = write_record(expected_key(), 200); + ck_assert_int_eq(fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE), 1); + ck_assert_uint_eq(arch()->NvsBufferPtr, + (uint32_t)(uintptr_t)rec->data); + ck_assert_uint_eq(arch()->BootMode, + BOOT_ASSUMING_NO_CONFIGURATION_CHANGES); +} +END_TEST + +START_TEST(test_apply_skips_a_rejected_record_until_cleared) +{ + /* after FspMemInit failed with the record, boots train from scratch, + * across FSP-M's reset requests, until a FspMemInit succeeds */ + reset_udp(); + write_record(expected_key(), 200); + fsp_nvs_skip(1); + ck_assert_int_eq(fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE), 0); + ck_assert_uint_eq(arch()->NvsBufferPtr, 0); + ck_assert_uint_eq(arch()->BootMode, BOOT_WITH_FULL_CONFIGURATION); + ck_assert_uint_eq(g_params.fsp_nvs_key, expected_key()); + ck_assert_int_eq(fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE), 0); + + fsp_nvs_skip(0); + ck_assert_int_eq(fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE), 1); +} +END_TEST + +START_TEST(test_apply_rejects_changed_config) +{ + reset_udp(); + write_record(expected_key(), 200); + + /* a different FSP-M build invalidates the record, even with the same + * revision in its header */ + g_fspm[FSPM_SIZE / 2] ^= 1; + fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE); + ck_assert_uint_eq(arch()->NvsBufferPtr, 0); + + /* so does any change to the UPDs */ + reset_udp(); + g_udp[UDP_SIZE - 1] ^= 1; + fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE); + ck_assert_uint_eq(arch()->NvsBufferPtr, 0); + ck_assert_uint_eq(arch()->BootMode, BOOT_WITH_FULL_CONFIGURATION); +} +END_TEST + +static Suite *fsp_nvs_suite(void) +{ + Suite *s = suite_create("fsp-nvs"); + TCase *tc = tcase_create("fsp-nvs"); + + tcase_add_test(tc, test_crc32_check_value); + tcase_add_test(tc, test_record_valid); + tcase_add_test(tc, test_record_rejects_bad_fields); + tcase_add_test(tc, test_apply_without_record); + tcase_add_test(tc, test_apply_with_matching_record); + tcase_add_test(tc, test_apply_rejects_changed_config); + tcase_add_test(tc, test_apply_skips_a_rejected_record_until_cleared); + suite_add_tcase(s, tc); + return s; +} + +int main(void) +{ + int fails; + Suite *s = fsp_nvs_suite(); + SRunner *sr = srunner_create(s); + + srunner_run_all(sr, CK_NORMAL); + fails = srunner_ntests_failed(sr); + srunner_free(sr); + return fails; +} diff --git a/tools/unit-tests/unit-tgl-spi-hwseq.c b/tools/unit-tests/unit-tgl-spi-hwseq.c new file mode 100644 index 0000000000..f24c2e0896 --- /dev/null +++ b/tools/unit-tests/unit-tgl-spi-hwseq.c @@ -0,0 +1,289 @@ +/* unit-tgl-spi-hwseq.c + * + * Unit tests for the Tiger Lake SPI hardware-sequencing erase/write used to + * save the FSP memory training data (hal/x86_fsp_tgl.c, extracted by the + * Makefile): BIOS-region address translation, 64-byte cycle splitting, the + * BIOS_CNTL write-enable handling and the error paths, against a model of + * the SPI controller registers and a RAM-backed flash window. + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#include +#include +#include +#include + +#define MMIO_BASE 0xFED40000UL +#define SPI_BIOS_CNTL_EXPECTED 0xdc + +#include "kontron_spi_extract.h" + +/* BIOS region: linear 0x01400000-0x01FFFFFF, the top 12 MB of 32 MB */ +#define FREG1_INIT 0x1FFF1400U +#define REG_HSFSTS 0x04 +#define REG_FADDR 0x08 +#define REG_FDATA0 0x10 +#define REG_FREG1 0x58 + +/* Modeled flash window: linear 0x019E0000 + 128 KB */ +#define WIN_LINEAR 0x019E0000U +#define WIN_SIZE 0x20000U +#define WIN_MMIO 0xFF9E0000U + +static uint32_t g_mmio[64]; +static uint32_t g_pci_cmd; +static uint8_t g_bios_cntl; +static int g_wpd_stuck_low; +static int g_fail_cycle; +static int g_never_done; +static uint8_t g_flash[WIN_SIZE]; +static struct { + uint32_t cycle, faddr, len; +} g_log[64]; +static int g_ncycles; + +static void sim_reset(void) +{ + memset(g_mmio, 0, sizeof(g_mmio)); + g_mmio[REG_FREG1 / 4] = FREG1_INIT; + g_pci_cmd = 0x0001; + g_bios_cntl = 0; + g_wpd_stuck_low = g_fail_cycle = g_never_done = 0; + g_ncycles = 0; + memset(g_flash, 0x5a, sizeof(g_flash)); +} + +uint32_t pci_config_read32(uint8_t bus, uint8_t dev, uint8_t fun, uint8_t off) +{ + (void)bus; (void)dev; (void)fun; + if (off == PCI_BAR0_OFFSET) + return (uint32_t)MMIO_BASE; + if (off == PCI_COMMAND_OFFSET) + return g_pci_cmd; + return 0; +} + +void pci_config_write32(uint8_t bus, uint8_t dev, uint8_t fun, uint8_t off, + uint32_t val) +{ + (void)bus; (void)dev; (void)fun; + if (off == PCI_COMMAND_OFFSET) + g_pci_cmd = val; +} + +uint8_t pci_config_read8(uint8_t bus, uint8_t dev, uint8_t fun, uint8_t off) +{ + (void)bus; (void)dev; (void)fun; + ck_assert_uint_eq(off, SPI_BIOS_CNTL_EXPECTED); + return g_bios_cntl; +} + +void pci_config_write8(uint8_t bus, uint8_t dev, uint8_t fun, uint8_t off, + uint8_t val) +{ + (void)bus; (void)dev; (void)fun; + ck_assert_uint_eq(off, SPI_BIOS_CNTL_EXPECTED); + g_bios_cntl = g_wpd_stuck_low ? (uint8_t)(val & ~1U) : val; +} + +void delay(int msec) +{ + (void)msec; +} + +static void run_cycle(uint32_t ctl) +{ + uint32_t cycle = (ctl >> 17) & 0xf; + uint32_t len = ((ctl >> 24) & 0x3f) + 1; + uint32_t faddr = g_mmio[REG_FADDR / 4]; + uint32_t i; + + /* a cycle only runs with flash writes enabled */ + ck_assert_uint_eq(g_bios_cntl & 1U, 1U); + if (g_ncycles < (int)(sizeof(g_log) / sizeof(g_log[0]))) { + g_log[g_ncycles].cycle = cycle; + g_log[g_ncycles].faddr = faddr; + g_log[g_ncycles].len = len; + } + g_ncycles++; + if (faddr >= WIN_LINEAR && faddr < WIN_LINEAR + WIN_SIZE) { + if (cycle == 3) + memset(g_flash + (faddr - WIN_LINEAR), 0xff, 0x1000); + else if (cycle == 2) + for (i = 0; i < len; i++) + g_flash[faddr - WIN_LINEAR + i] = + ((uint8_t *)&g_mmio[REG_FDATA0 / 4])[i]; + } + if (g_never_done) + return; + g_mmio[REG_HSFSTS / 4] |= (g_fail_cycle ? 0x2U : 0x1U); +} + +static void mmio_write32(uintptr_t address, uint32_t value) +{ + uint32_t off = (uint32_t)(address - MMIO_BASE); + + if (off == REG_HSFSTS) { + /* status bits are write-1-to-clear */ + g_mmio[REG_HSFSTS / 4] &= ~(value & 0x7U); + if (value & (1U << 16)) + run_cycle(value); + return; + } + g_mmio[off / 4] = value; +} + +static uint32_t mmio_read32(uintptr_t address) +{ + return g_mmio[(address - MMIO_BASE) / 4]; +} + +#include "tgl_spi_hwseq_extract.h" + +START_TEST(test_linear_mapping) +{ + sim_reset(); + ck_assert_int_eq(tgl_spi_linear(MMIO_BASE, WIN_MMIO, 0x1000), + WIN_LINEAR); + ck_assert_int_eq(tgl_spi_linear(MMIO_BASE, 0xFFFFFFFFU, 1), + 0x01FFFFFF); + ck_assert_int_eq(tgl_spi_linear(MMIO_BASE, 0xFF400000U, 0x1000), + 0x01400000); + /* below the BIOS region, past its top, or empty */ + ck_assert_int_eq(tgl_spi_linear(MMIO_BASE, 0xFF3FF000U, 0x1000), -1); + ck_assert_int_eq(tgl_spi_linear(MMIO_BASE, 0xFFFFF000U, 0x2000), -1); + ck_assert_int_eq(tgl_spi_linear(MMIO_BASE, WIN_MMIO, 0), -1); +} +END_TEST + +START_TEST(test_erase_sectors) +{ + sim_reset(); + ck_assert_int_eq(tgl_spi_flash_erase(WIN_MMIO, 0x2000), 0); + ck_assert_int_eq(g_ncycles, 2); + ck_assert_uint_eq(g_log[0].cycle, 3); + ck_assert_uint_eq(g_log[0].faddr, WIN_LINEAR); + ck_assert_uint_eq(g_log[1].faddr, WIN_LINEAR + 0x1000); + ck_assert_uint_eq(g_flash[0], 0xff); + ck_assert_uint_eq(g_flash[0x1fff], 0xff); + ck_assert_uint_eq(g_flash[0x2000], 0x5a); + /* write enable dropped and PCI command restored afterwards */ + ck_assert_uint_eq(g_bios_cntl & 1U, 0); + ck_assert_uint_eq(g_pci_cmd, 0x0001); +} +END_TEST + +START_TEST(test_erase_rejects_unaligned) +{ + sim_reset(); + ck_assert_int_eq(tgl_spi_flash_erase(WIN_MMIO + 0x10, 0x1000), -1); + ck_assert_int_eq(tgl_spi_flash_erase(WIN_MMIO, 0x800), -1); + ck_assert_int_eq(g_ncycles, 0); +} +END_TEST + +START_TEST(test_write_splits_on_64_byte_boundaries) +{ + uint8_t buf[150]; + uint32_t i; + + sim_reset(); + for (i = 0; i < sizeof(buf); i++) + buf[i] = (uint8_t)(i + 1); + ck_assert_int_eq(tgl_spi_flash_write(WIN_MMIO + 0x10, buf, sizeof(buf)), + 0); + ck_assert_int_eq(g_ncycles, 3); + ck_assert_uint_eq(g_log[0].faddr, WIN_LINEAR + 0x10); + ck_assert_uint_eq(g_log[0].len, 48); + ck_assert_uint_eq(g_log[1].faddr, WIN_LINEAR + 0x40); + ck_assert_uint_eq(g_log[1].len, 64); + ck_assert_uint_eq(g_log[2].faddr, WIN_LINEAR + 0x80); + ck_assert_uint_eq(g_log[2].len, 38); + ck_assert_uint_eq(g_log[0].cycle, 2); + ck_assert_int_eq(memcmp(g_flash + 0x10, buf, sizeof(buf)), 0); + ck_assert_uint_eq(g_flash[0x10 + sizeof(buf)], 0x5a); + ck_assert_uint_eq(g_bios_cntl & 1U, 0); +} +END_TEST + +START_TEST(test_write_outside_bios_region) +{ + uint8_t buf[4] = { 0 }; + + sim_reset(); + ck_assert_int_eq(tgl_spi_flash_write(0xFF000000U, buf, sizeof(buf)), -1); + ck_assert_int_eq(g_ncycles, 0); + ck_assert_uint_eq(g_bios_cntl & 1U, 0); +} +END_TEST + +START_TEST(test_cycle_error_and_timeout) +{ + uint8_t buf[8] = { 0 }; + + sim_reset(); + g_fail_cycle = 1; + ck_assert_int_eq(tgl_spi_flash_write(WIN_MMIO, buf, sizeof(buf)), -1); + + sim_reset(); + g_never_done = 1; + ck_assert_int_eq(tgl_spi_flash_erase(WIN_MMIO, 0x1000), -1); +} +END_TEST + +START_TEST(test_write_enable_refused) +{ + uint8_t buf[8] = { 0 }; + + sim_reset(); + g_wpd_stuck_low = 1; + ck_assert_int_eq(tgl_spi_flash_write(WIN_MMIO, buf, sizeof(buf)), -1); + ck_assert_int_eq(tgl_spi_flash_erase(WIN_MMIO, 0x1000), -1); + ck_assert_int_eq(g_ncycles, 0); +} +END_TEST + +static Suite *hwseq_suite(void) +{ + Suite *s = suite_create("tgl-spi-hwseq"); + TCase *tc = tcase_create("tgl-spi-hwseq"); + + tcase_add_test(tc, test_linear_mapping); + tcase_add_test(tc, test_erase_sectors); + tcase_add_test(tc, test_erase_rejects_unaligned); + tcase_add_test(tc, test_write_splits_on_64_byte_boundaries); + tcase_add_test(tc, test_write_outside_bios_region); + tcase_add_test(tc, test_cycle_error_and_timeout); + tcase_add_test(tc, test_write_enable_refused); + suite_add_tcase(s, tc); + return s; +} + +int main(void) +{ + int fails; + Suite *s = hwseq_suite(); + SRunner *sr = srunner_create(s); + + srunner_run_all(sr, CK_NORMAL); + fails = srunner_ntests_failed(sr); + srunner_free(sr); + return fails; +} From d7f754c7c0d79d5a4765eba00cac96fe0e38e55f Mon Sep 17 00:00:00 2001 From: David Garske Date: Mon, 2 Nov 2026 12:18:20 -0800 Subject: [PATCH 03/17] nai_68int6: stock BIOS PCIe switch sequence, GPIO levels and full reset --- config/examples/nai_68int6.config | 9 + docs/Targets.md | 3 + hal/nai_68int6.c | 8 + include/x86/fsp_config.h | 25 ++ include/x86/gpio_config.h | 394 ++++++++++++++++++++++++++++-- options.mk | 11 + src/x86/fsp.c | 4 + src/x86/qemu_fsp.c | 5 + src/x86/tgl_fsp.c | 118 ++++++++- 9 files changed, 551 insertions(+), 26 deletions(-) diff --git a/config/examples/nai_68int6.config b/config/examples/nai_68int6.config index 457c59f723..b4fc1e4d34 100644 --- a/config/examples/nai_68int6.config +++ b/config/examples/nai_68int6.config @@ -74,6 +74,15 @@ PCH_HAS_PCR=1 # Generate ACPI tables so Linux gets the interrupt model and PCI routing. ACPI=1 +########## PCIe switch and backplane (stock BIOS defaults) ########## +# Lanes: 8x1, 1x4_4x1, 4x1_1x4, 1x4_1x4 or 1x8. Delays are in seconds. +NAI_68INT6_PCIE_LANES=8x1 +NAI_68INT6_PCIE_NT=0 +NAI_68INT6_BACKPLANE_CLK=0 +NAI_68INT6_PCIE_LINK_DELAY_S=0 +NAI_68INT6_BOOT_DELAY_S=0 +NAI_68INT6_FPGA_READY_DELAY_S=0 + ########## Payload: signed 64-bit Linux bzImage ########## 64BIT=1 ELF=1 diff --git a/docs/Targets.md b/docs/Targets.md index 1359029b1b..44732c2c4e 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -9225,6 +9225,9 @@ Notable options in [config/examples/nai_68int6.config](../config/examples/nai_68 | `X86_UART_NUMBER=2` | LPSS UART2 for all three FSP phases (Kontron uses 0) | | `ACPI=1` | Required. Generates ACPI tables (RSDP/XSDT/FADT/MADT/MCFG and a minimal DSDT); the board's PCI interrupt routing is only in the DSDT `_PRT`, since the MP table is the Kontron one. The DSDT also declares the board devices on LPSS I2C0 (EEPROMs, RTC/watchdog) and GSPI0 (the PRC, for NAI's `naigspi` driver) | | `FSP_NVS_CACHE=1`, `FSP_NVS_BASE`, `FSP_NVS_SIZE` | Keeps FSP-M's memory training data (its non-volatile storage HOB) in a 128 KB area of the BIOS region below the wolfBoot window, so later boots skip full memory training. Stage 1 passes a valid copy back through `NvsBufferPtr`; stage 2 rewrites it, before FSP-S and before the BIOS region is locked, only when it changes. The record is keyed on a CRC of the FSP-M image and its UPDs, so a change to either falls back to full training. If FspMemInit fails with a saved record, stage 1 sets a CMOS flag and cold-resets, boots train from scratch until FspMemInit succeeds, and stage 2 replaces the record. `assemble_image.sh -n` blanks the area, so an image built from a board's flash dump does not carry that board's training data | +| `NAI_68INT6_PCIE_LANES` | PCIe switch lane configuration, strapped on GPP_B21/B22/S4/S7/S6 before memory init: `8x1` (default), `1x4_4x1`, `4x1_1x4`, `1x4_1x4` or `1x8` | +| `NAI_68INT6_PCIE_NT`, `NAI_68INT6_BACKPLANE_CLK` | PCIe switch non-transparent mode (GPP_S2) and the backplane PCIe clock (GPP_A0); both off by default | +| `NAI_68INT6_PCIE_LINK_DELAY_S`, `NAI_68INT6_BOOT_DELAY_S`, `NAI_68INT6_FPGA_READY_DELAY_S` | Waits before CPU PCIe link training (0-5 s), before polling PCIE_RDY (0-255 s), and added to the 5 s PCIE_RDY timeout (0, 5 or 10 s); all 0 by default. Before PCI enumeration wolfBoot polls PCIE_RDY (GPP_C23) every 50 ms, continues with a warning on timeout, then drives GPP_B12 high | | `ANTI_ROLLBACK=1` | Persistent version floor and boot audit counters in TPM NV | | `MEASURED_BOOT=1`, `MEASURED_PCR_OS=4` | Firmware into PCR 0, the verified OS image into PCR 4 | | `WOLFBOOT_BOOT_DISK=0` | Boot from GPT partitions; the A/B indices (sda3/sda4) are set in `src/update_disk.c` | diff --git a/hal/nai_68int6.c b/hal/nai_68int6.c index 3b90d90981..0bdf148270 100644 --- a/hal/nai_68int6.c +++ b/hal/nai_68int6.c @@ -40,8 +40,16 @@ void hal_init(void) fsp_init_silicon(); } +/* Reset Control (0xCF9) FULL_RST: written alone it starts no reset, but makes + * any later reset a full one, as the stock BIOS does before the OS loader + * (its OS Reset Select default, Cold Reset). That covers SYS_RESET# from the + * IPMC, which is otherwise a warm reset. */ +#define RST_CNT_PORT 0xcf9 +#define RST_CNT_FULL_RST (1 << 3) + void hal_prepare_boot(void) { + io_write8(RST_CNT_PORT, RST_CNT_FULL_RST); } #endif /* __WOLFBOOT */ diff --git a/include/x86/fsp_config.h b/include/x86/fsp_config.h index 3689c638fd..1994c04d48 100644 --- a/include/x86/fsp_config.h +++ b/include/x86/fsp_config.h @@ -41,4 +41,29 @@ #define FSP_THERMAL_THROTTLING 1 #endif +/* Seconds to wait after the straps, before CPU PCIe link training */ +#ifndef NAI_68INT6_PCIE_LINK_DELAY_S +#define NAI_68INT6_PCIE_LINK_DELAY_S 0 +#endif +#if NAI_68INT6_PCIE_LINK_DELAY_S < 0 || NAI_68INT6_PCIE_LINK_DELAY_S > 5 +#error "NAI_68INT6_PCIE_LINK_DELAY_S must be 0 to 5" +#endif + +/* Seconds to wait before polling PCIE_RDY */ +#ifndef NAI_68INT6_BOOT_DELAY_S +#define NAI_68INT6_BOOT_DELAY_S 0 +#endif +#if NAI_68INT6_BOOT_DELAY_S < 0 || NAI_68INT6_BOOT_DELAY_S > 255 +#error "NAI_68INT6_BOOT_DELAY_S must be 0 to 255" +#endif + +/* Extra seconds added to the 5 s PCIE_RDY timeout */ +#ifndef NAI_68INT6_FPGA_READY_DELAY_S +#define NAI_68INT6_FPGA_READY_DELAY_S 0 +#endif +#if NAI_68INT6_FPGA_READY_DELAY_S != 0 && \ + NAI_68INT6_FPGA_READY_DELAY_S != 5 && NAI_68INT6_FPGA_READY_DELAY_S != 10 +#error "NAI_68INT6_FPGA_READY_DELAY_S must be 0, 5 or 10" +#endif + #endif diff --git a/include/x86/gpio_config.h b/include/x86/gpio_config.h index 9b60d90c2b..d4f9a660a6 100644 --- a/include/x86/gpio_config.h +++ b/include/x86/gpio_config.h @@ -71,13 +71,29 @@ #define GPIO_GPPC_E12_CFG_OFF (0xb30) #define GPIO_GPPC_E15_CFG_OFF (0xb60) #define GPIO_GPPC_E16_CFG_OFF (0xb70) -#define GPIO_GPPC_F2_CFG_OFF (0xba0) -#define GPIO_GPPC_F4_CFG_OFF (0xbc0) -#define GPIO_GPPC_F5_CFG_OFF (0xbd0) +#define GPIO_GPPC_E19_CFG_OFF (0xba0) +#define GPIO_GPPC_E21_CFG_OFF (0xbc0) +#define GPIO_GPPC_E22_CFG_OFF (0xbd0) #define GPIO_GPPC_F9_CFG_OFF (0x910) #define GPIO_GPPC_A9_CFG_OFF (0xa30) #define GPIO_GPPC_T2_CFG_OFF (0x8c0) #define GPIO_GPPC_T3_CFG_OFF (0x8d0) +#define GPIO_GPPC_A0_CFG_OFF (0x9a0) +#define GPIO_GPPC_B12_CFG_OFF (0x7c0) +#define GPIO_GPPC_B21_CFG_OFF (0x850) +#define GPIO_GPPC_B22_CFG_OFF (0x860) +#define GPIO_GPPC_C23_CFG_OFF (0x870) +#define GPIO_GPPC_S0_CFG_OFF (0x700) +#define GPIO_GPPC_S1_CFG_OFF (0x710) +#define GPIO_GPPC_S2_CFG_OFF (0x720) +#define GPIO_GPPC_S3_CFG_OFF (0x730) +#define GPIO_GPPC_S4_CFG_OFF (0x740) +#define GPIO_GPPC_S5_CFG_OFF (0x750) +#define GPIO_GPPC_S6_CFG_OFF (0x760) +#define GPIO_GPPC_S7_CFG_OFF (0x770) +#define GPIO_GPPC_H7_CFG_OFF (0x7f0) +#define GPIO_GPPC_H8_CFG_OFF (0x800) +#define GPIO_GPPC_H9_CFG_OFF (0x810) #define GPIO_OWN_MASK (0x3) /* struct tgl_gpio, enum gpio_config_flags and struct tgl_gpio_conf are defined @@ -86,6 +102,56 @@ #if defined (TARGET_kontron_vx3060_s2) || defined (TARGET_nai_68int6) #if defined (TARGET_nai_68int6) +/* PCIe switch lane configuration, strapped on GPP_B21/B22 (MUX_SEL1/0) and + * GPP_S4/S7/S6 (BOOTCFG2/1/0); the BIOS default is 8x1 */ +#if defined(NAI_68INT6_PCIE_LANES_1x4_4x1) +#define NAI_PCIE_STRAP_MUX_SEL1 1 +#define NAI_PCIE_STRAP_MUX_SEL0 0 +#define NAI_PCIE_STRAP_BOOTCFG2 0 +#define NAI_PCIE_STRAP_BOOTCFG1 0 +#define NAI_PCIE_STRAP_BOOTCFG0 1 +#elif defined(NAI_68INT6_PCIE_LANES_4x1_1x4) +#define NAI_PCIE_STRAP_MUX_SEL1 0 +#define NAI_PCIE_STRAP_MUX_SEL0 1 +#define NAI_PCIE_STRAP_BOOTCFG2 0 +#define NAI_PCIE_STRAP_BOOTCFG1 1 +#define NAI_PCIE_STRAP_BOOTCFG0 0 +#elif defined(NAI_68INT6_PCIE_LANES_1x4_1x4) +#define NAI_PCIE_STRAP_MUX_SEL1 0 +#define NAI_PCIE_STRAP_MUX_SEL0 0 +#define NAI_PCIE_STRAP_BOOTCFG2 0 +#define NAI_PCIE_STRAP_BOOTCFG1 1 +#define NAI_PCIE_STRAP_BOOTCFG0 1 +#elif defined(NAI_68INT6_PCIE_LANES_1x8) +#define NAI_PCIE_STRAP_MUX_SEL1 0 +#define NAI_PCIE_STRAP_MUX_SEL0 0 +#define NAI_PCIE_STRAP_BOOTCFG2 1 +#define NAI_PCIE_STRAP_BOOTCFG1 0 +#define NAI_PCIE_STRAP_BOOTCFG0 0 +#else /* 8x1 */ +#define NAI_PCIE_STRAP_MUX_SEL1 1 +#define NAI_PCIE_STRAP_MUX_SEL0 1 +#define NAI_PCIE_STRAP_BOOTCFG2 0 +#define NAI_PCIE_STRAP_BOOTCFG1 0 +#define NAI_PCIE_STRAP_BOOTCFG0 0 +#endif + +/* PCIe switch non-transparent mode (GPP_S2) */ +#ifndef NAI_68INT6_PCIE_NT +#define NAI_68INT6_PCIE_NT 0 +#endif +#if NAI_68INT6_PCIE_NT != 0 && NAI_68INT6_PCIE_NT != 1 +#error "NAI_68INT6_PCIE_NT must be 0 or 1" +#endif + +/* Backplane PCIe reference clock (GPP_A0) */ +#ifndef NAI_68INT6_BACKPLANE_CLK +#define NAI_68INT6_BACKPLANE_CLK 0 +#endif +#if NAI_68INT6_BACKPLANE_CLK != 0 && NAI_68INT6_BACKPLANE_CLK != 1 +#error "NAI_68INT6_BACKPLANE_CLK must be 0 or 1" +#endif + /* Pad settings snapshotted from a running 68INT6 with the stock BIOS. * HOSTSW_OWN and PADCFGLOCK are not captured. */ @@ -127,8 +193,242 @@ static const struct tgl_gpio_conf gpio_table_tempram[] = { .gpio_reset = GPIO_RESET_HOSTDEEPRESET}, }; -/* Nothing is required between tempram and memory init on this board. */ +/* PCIe switch and backplane straps, in the order the stock BIOS writes + * them, before memory init. */ static const struct tgl_gpio_conf gpio_table_premem[] = { + /* PAD S0: low */ + {.gpio = + { + .comm_port_id = GPIO_COMM_1_PORT_ID, + .cfg_offset = GPIO_GPPC_S0_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF | GPIO_SET_TX), + .gpio_mode = GPIO_MODE_GPIO, + .gpio_dir = GPIO_DIR_OUTPUT, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_tx = 0, + .gpio_reset = GPIO_RESET_PLTRST}, + /* PAD S1: low */ + {.gpio = + { + .comm_port_id = GPIO_COMM_1_PORT_ID, + .cfg_offset = GPIO_GPPC_S1_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF | GPIO_SET_TX), + .gpio_mode = GPIO_MODE_GPIO, + .gpio_dir = GPIO_DIR_OUTPUT, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_tx = 0, + .gpio_reset = GPIO_RESET_PLTRST}, + /* PAD S3: low */ + {.gpio = + { + .comm_port_id = GPIO_COMM_1_PORT_ID, + .cfg_offset = GPIO_GPPC_S3_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF | GPIO_SET_TX), + .gpio_mode = GPIO_MODE_GPIO, + .gpio_dir = GPIO_DIR_OUTPUT, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_tx = 0, + .gpio_reset = GPIO_RESET_PLTRST}, + /* PAD S5: low */ + {.gpio = + { + .comm_port_id = GPIO_COMM_1_PORT_ID, + .cfg_offset = GPIO_GPPC_S5_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF | GPIO_SET_TX), + .gpio_mode = GPIO_MODE_GPIO, + .gpio_dir = GPIO_DIR_OUTPUT, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_tx = 0, + .gpio_reset = GPIO_RESET_PLTRST}, + /* PAD S2: PCIe switch NT (low = transparent) */ + {.gpio = + { + .comm_port_id = GPIO_COMM_1_PORT_ID, + .cfg_offset = GPIO_GPPC_S2_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF | GPIO_SET_TX), + .gpio_mode = GPIO_MODE_GPIO, + .gpio_dir = GPIO_DIR_OUTPUT, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_tx = NAI_68INT6_PCIE_NT, + .gpio_reset = GPIO_RESET_PLTRST}, + /* PAD B12: low until PCIE_RDY, then driven high */ + {.gpio = + { + .comm_port_id = GPIO_COMM_0_PORT_ID, + .cfg_offset = GPIO_GPPC_B12_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF | GPIO_SET_TX), + .gpio_mode = GPIO_MODE_GPIO, + .gpio_dir = GPIO_DIR_OUTPUT, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_tx = 0, + .gpio_reset = GPIO_RESET_PLTRST}, + /* PAD B21: PCIe lane strap MUX_SEL1 */ + {.gpio = + { + .comm_port_id = GPIO_COMM_0_PORT_ID, + .cfg_offset = GPIO_GPPC_B21_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF | GPIO_SET_TX), + .gpio_mode = GPIO_MODE_GPIO, + .gpio_dir = GPIO_DIR_OUTPUT, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_tx = NAI_PCIE_STRAP_MUX_SEL1, + .gpio_reset = GPIO_RESET_PLTRST}, + /* PAD B22: PCIe lane strap MUX_SEL0 */ + {.gpio = + { + .comm_port_id = GPIO_COMM_0_PORT_ID, + .cfg_offset = GPIO_GPPC_B22_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF | GPIO_SET_TX), + .gpio_mode = GPIO_MODE_GPIO, + .gpio_dir = GPIO_DIR_OUTPUT, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_tx = NAI_PCIE_STRAP_MUX_SEL0, + .gpio_reset = GPIO_RESET_PLTRST}, + /* PAD S4: PCIe lane strap BOOTCFG2 */ + {.gpio = + { + .comm_port_id = GPIO_COMM_1_PORT_ID, + .cfg_offset = GPIO_GPPC_S4_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF | GPIO_SET_TX), + .gpio_mode = GPIO_MODE_GPIO, + .gpio_dir = GPIO_DIR_OUTPUT, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_tx = NAI_PCIE_STRAP_BOOTCFG2, + .gpio_reset = GPIO_RESET_PLTRST}, + /* PAD S7: PCIe lane strap BOOTCFG1 */ + {.gpio = + { + .comm_port_id = GPIO_COMM_1_PORT_ID, + .cfg_offset = GPIO_GPPC_S7_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF | GPIO_SET_TX), + .gpio_mode = GPIO_MODE_GPIO, + .gpio_dir = GPIO_DIR_OUTPUT, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_tx = NAI_PCIE_STRAP_BOOTCFG1, + .gpio_reset = GPIO_RESET_PLTRST}, + /* PAD S6: PCIe lane strap BOOTCFG0 */ + {.gpio = + { + .comm_port_id = GPIO_COMM_1_PORT_ID, + .cfg_offset = GPIO_GPPC_S6_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF | GPIO_SET_TX), + .gpio_mode = GPIO_MODE_GPIO, + .gpio_dir = GPIO_DIR_OUTPUT, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_tx = NAI_PCIE_STRAP_BOOTCFG0, + .gpio_reset = GPIO_RESET_PLTRST}, + /* PAD A0: backplane PCIe clock (low = off) */ + {.gpio = + { + .comm_port_id = GPIO_COMM_0_PORT_ID, + .cfg_offset = GPIO_GPPC_A0_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF | GPIO_SET_TX), + .gpio_mode = GPIO_MODE_GPIO, + .gpio_dir = GPIO_DIR_OUTPUT, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_tx = NAI_68INT6_BACKPLANE_CLK, + .gpio_reset = GPIO_RESET_PLTRST}, + /* PAD C23: PCIE_RDY input (high = ready) */ + {.gpio = + { + .comm_port_id = GPIO_COMM_4_PORT_ID, + .cfg_offset = GPIO_GPPC_C23_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF), + .gpio_mode = GPIO_MODE_GPIO, + .gpio_dir = GPIO_DIR_INPUT, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_reset = GPIO_RESET_HOSTDEEPRESET}, }; /* The rest of the stock pad settings, applied after silicon init. */ @@ -261,19 +561,20 @@ static const struct tgl_gpio_conf gpio_table_presilicon[] = { .flags = (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | - GPIO_SET_RXEVCONF), + GPIO_SET_RXEVCONF | GPIO_SET_TX), .gpio_mode = GPIO_MODE_GPIO, .gpio_dir = GPIO_DIR_OUTPUT, .gpio_term = GPIO_TERM_NONE, .gpio_interrupt = 0x00, .gpio_rxinv = 0x00, .gpio_rxevconf = 0x02, + .gpio_tx = 1, .gpio_reset = GPIO_RESET_PLTRST}, - /* PAD F2 (DW0=0x44001600 DW1=0x00003C3F) */ + /* PAD E19 (DW0=0x44001600 DW1=0x00003C3F) */ {.gpio = { .comm_port_id = GPIO_COMM_4_PORT_ID, - .cfg_offset = GPIO_GPPC_F2_CFG_OFF, + .cfg_offset = GPIO_GPPC_E19_CFG_OFF, }, .flags = (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | @@ -312,13 +613,14 @@ static const struct tgl_gpio_conf gpio_table_presilicon[] = { .flags = (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | - GPIO_SET_RXEVCONF), + GPIO_SET_RXEVCONF | GPIO_SET_TX), .gpio_mode = GPIO_MODE_GPIO, .gpio_dir = GPIO_DIR_OUTPUT, .gpio_term = GPIO_TERM_NONE, .gpio_interrupt = 0x00, .gpio_rxinv = 0x00, .gpio_rxevconf = 0x02, + .gpio_tx = 1, .gpio_reset = GPIO_RESET_PLTRST}, /* PAD E15 (DW0=0x44000B00 DW1=0x0000003B) */ {.gpio = @@ -363,13 +665,14 @@ static const struct tgl_gpio_conf gpio_table_presilicon[] = { .flags = (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | - GPIO_SET_RXEVCONF), + GPIO_SET_RXEVCONF | GPIO_SET_TX), .gpio_mode = GPIO_MODE_GPIO, .gpio_dir = GPIO_DIR_OUTPUT, .gpio_term = GPIO_TERM_NONE, .gpio_interrupt = 0x00, .gpio_rxinv = 0x00, .gpio_rxevconf = 0x02, + .gpio_tx = 1, .gpio_reset = GPIO_RESET_PLTRST}, /* PAD A9 (DW0=0x44000B00 DW1=0x00000049) */ {.gpio = @@ -388,11 +691,11 @@ static const struct tgl_gpio_conf gpio_table_presilicon[] = { .gpio_rxinv = 0x00, .gpio_rxevconf = 0x02, .gpio_reset = GPIO_RESET_HOSTDEEPRESET}, - /* PAD F4 (DW0=0x44001600 DW1=0x00003C41) */ + /* PAD E21 (DW0=0x44001600 DW1=0x00003C41) */ {.gpio = { .comm_port_id = GPIO_COMM_4_PORT_ID, - .cfg_offset = GPIO_GPPC_F4_CFG_OFF, + .cfg_offset = GPIO_GPPC_E21_CFG_OFF, }, .flags = (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | @@ -405,11 +708,11 @@ static const struct tgl_gpio_conf gpio_table_presilicon[] = { .gpio_rxinv = 0x00, .gpio_rxevconf = 0x02, .gpio_reset = GPIO_RESET_HOSTDEEPRESET}, - /* PAD F5 (DW0=0x84000200 DW1=0x00000042) */ + /* PAD E22 (DW0=0x84000200 DW1=0x00000042) */ {.gpio = { .comm_port_id = GPIO_COMM_4_PORT_ID, - .cfg_offset = GPIO_GPPC_F5_CFG_OFF, + .cfg_offset = GPIO_GPPC_E22_CFG_OFF, }, .flags = (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | @@ -575,7 +878,7 @@ static const struct tgl_gpio_conf gpio_table_presilicon[] = { .gpio_rxinv = 0x00, .gpio_rxevconf = 0x02, .gpio_reset = GPIO_RESET_PLTRST}, - /* PAD D2 (DW0=0x84000201 DW1=0x0000002E) */ + /* PAD D2: held low; the OS drives it high for the IPMC */ {.gpio = { .comm_port_id = GPIO_COMM_1_PORT_ID, @@ -584,13 +887,14 @@ static const struct tgl_gpio_conf gpio_table_presilicon[] = { .flags = (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | - GPIO_SET_RXEVCONF), + GPIO_SET_RXEVCONF | GPIO_SET_TX), .gpio_mode = GPIO_MODE_GPIO, .gpio_dir = GPIO_DIR_OUTPUT, .gpio_term = GPIO_TERM_NONE, .gpio_interrupt = 0x00, .gpio_rxinv = 0x00, .gpio_rxevconf = 0x02, + .gpio_tx = 0, .gpio_reset = GPIO_RESET_PLTRST}, /* PAD R0 (DW0=0x44000700 DW1=0x0003C058) */ {.gpio = @@ -797,6 +1101,60 @@ static const struct tgl_gpio_conf gpio_table_presilicon[] = { .gpio_rxinv = 0x00, .gpio_rxevconf = 0x02, .gpio_reset = GPIO_RESET_HOSTDEEPRESET}, + /* PAD H7 (DW0=0x84000201 DW1=0x0000001B) USB power enable */ + {.gpio = + { + .comm_port_id = GPIO_COMM_1_PORT_ID, + .cfg_offset = GPIO_GPPC_H7_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF | GPIO_SET_TX), + .gpio_mode = GPIO_MODE_GPIO, + .gpio_dir = GPIO_DIR_OUTPUT, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_tx = 1, + .gpio_reset = GPIO_RESET_PLTRST}, + /* PAD H8 (DW0=0x84000201 DW1=0x0000001C) USB power enable */ + {.gpio = + { + .comm_port_id = GPIO_COMM_1_PORT_ID, + .cfg_offset = GPIO_GPPC_H8_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF | GPIO_SET_TX), + .gpio_mode = GPIO_MODE_GPIO, + .gpio_dir = GPIO_DIR_OUTPUT, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_tx = 1, + .gpio_reset = GPIO_RESET_PLTRST}, + /* PAD H9 (DW0=0x84000201 DW1=0x0000001D) USB power enable */ + {.gpio = + { + .comm_port_id = GPIO_COMM_1_PORT_ID, + .cfg_offset = GPIO_GPPC_H9_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF | GPIO_SET_TX), + .gpio_mode = GPIO_MODE_GPIO, + .gpio_dir = GPIO_DIR_OUTPUT, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_tx = 1, + .gpio_reset = GPIO_RESET_PLTRST}, }; #else /* TARGET_kontron_vx3060_s2 */ @@ -926,7 +1284,7 @@ static const struct tgl_gpio_conf gpio_table_presilicon[] = { {.gpio = { .comm_port_id = GPIO_COMM_4_PORT_ID, - .cfg_offset = GPIO_GPPC_F2_CFG_OFF, + .cfg_offset = GPIO_GPPC_E19_CFG_OFF, }, .flags = (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_INTERRUPT | GPIO_SET_RESET), @@ -1006,7 +1364,7 @@ static const struct tgl_gpio_conf gpio_table_presilicon[] = { {.gpio = { .comm_port_id = GPIO_COMM_4_PORT_ID, - .cfg_offset = GPIO_GPPC_F4_CFG_OFF, + .cfg_offset = GPIO_GPPC_E21_CFG_OFF, }, .flags = (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_INTERRUPT | GPIO_SET_RESET), @@ -1017,7 +1375,7 @@ static const struct tgl_gpio_conf gpio_table_presilicon[] = { {.gpio = { .comm_port_id = GPIO_COMM_4_PORT_ID, - .cfg_offset = GPIO_GPPC_F5_CFG_OFF, + .cfg_offset = GPIO_GPPC_E22_CFG_OFF, }, .flags = (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_INTERRUPT | GPIO_SET_RESET), diff --git a/options.mk b/options.mk index e0965cf8b6..cf16bad110 100644 --- a/options.mk +++ b/options.mk @@ -63,6 +63,17 @@ ifeq ($(TARGET),nai_68int6) ifneq ($(ACPI),1) $(error nai_68int6 needs ACPI=1: its PCI interrupt routing is only in the DSDT) endif + # PCIe switch straps and start-up delays (see docs/Targets.md) + NAI_68INT6_PCIE_LANES?=8x1 + NAI_68INT6_PCIE_LANE_CONFIGS:=8x1 1x4_4x1 4x1_1x4 1x4_1x4 1x8 + ifeq ($(filter $(NAI_68INT6_PCIE_LANES),$(NAI_68INT6_PCIE_LANE_CONFIGS)),) + $(error NAI_68INT6_PCIE_LANES must be one of: $(NAI_68INT6_PCIE_LANE_CONFIGS)) + endif + CFLAGS+=-DNAI_68INT6_PCIE_LANES_$(NAI_68INT6_PCIE_LANES) + $(foreach option,NAI_68INT6_PCIE_NT NAI_68INT6_BACKPLANE_CLK \ + NAI_68INT6_PCIE_LINK_DELAY_S NAI_68INT6_BOOT_DELAY_S \ + NAI_68INT6_FPGA_READY_DELAY_S, \ + $(if $($(option)),$(eval CFLAGS+=-D$(option)=$($(option))))) endif # TPM NV version floor and boot audit counters (see docs/Targets.md). diff --git a/src/x86/fsp.c b/src/x86/fsp.c index 4158f32c65..35540b8ca7 100644 --- a/src/x86/fsp.c +++ b/src/x86/fsp.c @@ -44,6 +44,7 @@ extern uint8_t _fsp_s_base_start[]; int fsp_machine_update_s_parameters(uint8_t *default_s_params); int fsp_pre_silicon_init_cb(void); +int fsp_pre_pci_enum_cb(void); /*! * \brief Check if the FSP info header is valid. * @@ -195,6 +196,9 @@ void fsp_init_silicon(void) panic(); } wolfBoot_printf("success"); + status = fsp_pre_pci_enum_cb(); + if (status != 0) + panic(); pci_enum_do(); pci_dump_config_space(); notify_phase_off = fsp_s_hdr->NotifyPhaseEntryOffset; diff --git a/src/x86/qemu_fsp.c b/src/x86/qemu_fsp.c index 37fc5650f7..ece0977707 100644 --- a/src/x86/qemu_fsp.c +++ b/src/x86/qemu_fsp.c @@ -140,3 +140,8 @@ int fsp_pre_silicon_init_cb(void) { return 0; } + +int fsp_pre_pci_enum_cb(void) +{ + return 0; +} diff --git a/src/x86/tgl_fsp.c b/src/x86/tgl_fsp.c index 29cb99f000..63f5c60d94 100644 --- a/src/x86/tgl_fsp.c +++ b/src/x86/tgl_fsp.c @@ -110,6 +110,8 @@ #define GPIO_TERM_MASK (0xf) << GPIO_TERM_SHIFT #define GPIO_RXEVCONF_SHIFT (25) #define GPIO_RXEVCONF_MASK (0x3) << GPIO_RXEVCONF_SHIFT +#define GPIO_TX_STATE (1U << 0) +#define GPIO_RX_STATE (1U << 1) #define PCR_INTERRUPT_PORT_ID (0xc4) #define GIC_OFFSET (0x31FC) #define GIC_SHUTDOWN_STATUS_BIT (1 << 0) @@ -143,9 +145,9 @@ #define GPIO_GPPC_E12_CFG_OFF (0xb30) #define GPIO_GPPC_E15_CFG_OFF (0xb60) #define GPIO_GPPC_E16_CFG_OFF (0xb70) -#define GPIO_GPPC_F2_CFG_OFF (0xba0) -#define GPIO_GPPC_F4_CFG_OFF (0xbc0) -#define GPIO_GPPC_F5_CFG_OFF (0xbd0) +#define GPIO_GPPC_E19_CFG_OFF (0xba0) +#define GPIO_GPPC_E21_CFG_OFF (0xbc0) +#define GPIO_GPPC_E22_CFG_OFF (0xbd0) #define GPIO_GPPC_F9_CFG_OFF (0x910) #define GPIO_GPPC_A9_CFG_OFF (0xa30) #define GPIO_GPPC_T2_CFG_OFF (0x8c0) @@ -1036,6 +1038,11 @@ int fsp_machine_update_s_parameters(uint8_t *default_s_params) memset(upd->PcieRpHotPlug, 0, sizeof(upd->PcieRpHotPlug)); memset(upd->CpuPcieRpHotPlug, 0, sizeof(upd->CpuPcieRpHotPlug)); #ifdef TARGET_nai_68int6 + /* As the stock BIOS: hot-plug on PCH root ports 5, 9 and 10, so FSP-S + * keeps a port without a link (10 on a bare board) enabled */ + upd->PcieRpHotPlug[4] = 1; + upd->PcieRpHotPlug[8] = 1; + upd->PcieRpHotPlug[9] = 1; upd->Eist = FSP_INTEL_SPEED_STEP; upd->Hwp = FSP_INTEL_SPEED_SHIFT; upd->Cx = FSP_C_STATES; @@ -1097,7 +1104,8 @@ enum gpio_config_flags { GPIO_SET_RXINV = (1 << 4), GPIO_SET_RESET = (1 << 5), GPIO_SET_TERM = (1 << 6), - GPIO_SET_RXEVCONF = (1 << 7) + GPIO_SET_RXEVCONF = (1 << 7), + GPIO_SET_TX = (1 << 8) }; struct tgl_gpio_conf { @@ -1111,6 +1119,7 @@ struct tgl_gpio_conf { uint8_t gpio_term; uint8_t gpio_rxevconf; uint8_t gpio_rxinv; + uint8_t gpio_tx; }; #if defined (TARGET_kontron_vx3060_s2) @@ -1237,7 +1246,7 @@ static const struct tgl_gpio_conf gpio_table_premem[] = { {.gpio = { .comm_port_id = GPIO_COMM_4_PORT_ID, - .cfg_offset = GPIO_GPPC_F2_CFG_OFF, + .cfg_offset = GPIO_GPPC_E19_CFG_OFF, }, .flags = (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_INTERRUPT | GPIO_SET_RESET), @@ -1321,7 +1330,7 @@ static const struct tgl_gpio_conf gpio_table_presilicon[] = { {.gpio = { .comm_port_id = GPIO_COMM_4_PORT_ID, - .cfg_offset = GPIO_GPPC_F4_CFG_OFF, + .cfg_offset = GPIO_GPPC_E21_CFG_OFF, }, .flags = (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_INTERRUPT | GPIO_SET_RESET), @@ -1332,7 +1341,7 @@ static const struct tgl_gpio_conf gpio_table_presilicon[] = { {.gpio = { .comm_port_id = GPIO_COMM_4_PORT_ID, - .cfg_offset = GPIO_GPPC_F5_CFG_OFF, + .cfg_offset = GPIO_GPPC_E22_CFG_OFF, }, .flags = (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_INTERRUPT | GPIO_SET_RESET), @@ -1555,7 +1564,7 @@ static void tgl_gpio_configure(const struct tgl_gpio_conf *gpio) g = gpio->gpio; - GPIO_DEBUG_PRINTF("Gpio port: %d off: 0x%x :\r\n", i, g.comm_port_id, + GPIO_DEBUG_PRINTF("Gpio port: 0x%x off: 0x%x :\r\n", g.comm_port_id, g.cfg_offset); dw0 = _dw0 = pch_read32(g.comm_port_id, g.cfg_offset); @@ -1585,6 +1594,11 @@ static void tgl_gpio_configure(const struct tgl_gpio_conf *gpio) dw0 &= ~(GPIO_RXEVCONF_MASK); dw0 |= gpio->gpio_rxevconf << GPIO_RXEVCONF_SHIFT; } + if (gpio->flags & GPIO_SET_TX) { + dw0 &= ~GPIO_TX_STATE; + if (gpio->gpio_tx) + dw0 |= GPIO_TX_STATE; + } GPIO_DEBUG_PRINTF("dw0: 0x%x -> 0x%x, dw1: 0x%x->0x%x\r\n", _dw0, dw0, _dw1, dw1); @@ -1596,6 +1610,39 @@ static void tgl_gpio_configure(const struct tgl_gpio_conf *gpio) } } +#ifdef TARGET_nai_68int6 +static uint64_t tgl_rdtsc(void) +{ + uint32_t lo, hi; + + __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); + return ((uint64_t)hi << 32) | lo; +} + +/* TSC ticks per millisecond: CPUID 0x15 gives the crystal clock and the + * TSC/crystal ratio, CPUID 0x16 the base frequency in MHz. */ +static uint32_t tgl_tsc_per_ms(void) +{ + uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0; + + cpuid(0x15, &eax, &ebx, &ecx, &edx); + if (eax != 0 && ebx != 0 && ecx != 0) + return (ecx / 1000) * ebx / eax; + cpuid(0x16, &eax, &ebx, &ecx, &edx); + if ((eax & 0xffff) != 0) + return (eax & 0xffff) * 1000; + return 2000000; +} + +static void tgl_delay_ms(uint32_t ms) +{ + uint64_t end = tgl_rdtsc() + (uint64_t)tgl_tsc_per_ms() * ms; + + while (tgl_rdtsc() < end) + ; +} +#endif /* TARGET_nai_68int6 */ + #ifdef TARGET_kontron_vx3060_s2 /** * @brief Set up ECE1200 device on Kontron VX3060-S2 board. @@ -1789,6 +1836,13 @@ int fsp_pre_mem_init_cb(void) for (i = 0; i < n; i++) { tgl_gpio_configure(&gpio_table_premem[i]); } +#ifdef TARGET_nai_68int6 + if (NAI_68INT6_PCIE_LINK_DELAY_S > 0) { + wolfBoot_printf("PCIe link delay %d s\r\n", + NAI_68INT6_PCIE_LINK_DELAY_S); + tgl_delay_ms(NAI_68INT6_PCIE_LINK_DELAY_S * 1000); + } +#endif return 0; } @@ -1804,4 +1858,52 @@ int fsp_pre_silicon_init_cb(void) return 0; } + +#ifdef TARGET_nai_68int6 +#define NAI_PCIE_RDY_POLL_MS 50 +#define NAI_PCIE_RDY_TIMEOUT_MS (5000 + NAI_68INT6_FPGA_READY_DELAY_S * 1000) + +/* As the stock BIOS does before PCI enumeration: wait for the FPGA to raise + * PCIE_RDY (GPP_C23), then release GPP_B12. */ +static void nai_pcie_ready(void) +{ + uint32_t polls = 0, waited_ms = 0, dw0, id; + int ready; + + if (NAI_68INT6_BOOT_DELAY_S > 0) { + wolfBoot_printf("Boot delay %d s\r\n", NAI_68INT6_BOOT_DELAY_S); + tgl_delay_ms(NAI_68INT6_BOOT_DELAY_S * 1000); + } + for (;;) { + polls++; + ready = (pch_read32(GPIO_COMM_4_PORT_ID, GPIO_GPPC_C23_CFG_OFF) & + GPIO_RX_STATE) != 0; + if (ready || waited_ms >= NAI_PCIE_RDY_TIMEOUT_MS) + break; + tgl_delay_ms(NAI_PCIE_RDY_POLL_MS); + waited_ms += NAI_PCIE_RDY_POLL_MS; + } + if (ready) + wolfBoot_printf("PCIE_RDY high after %d polls\r\n", (int)polls); + else + wolfBoot_printf("Warning: PCIE_RDY low after %d ms, continuing\r\n", + (int)waited_ms); + + dw0 = pch_read32(GPIO_COMM_0_PORT_ID, GPIO_GPPC_B12_CFG_OFF); + pch_write32(GPIO_COMM_0_PORT_ID, GPIO_GPPC_B12_CFG_OFF, + dw0 | GPIO_TX_STATE); + + id = pci_config_read32(0, 6, 0, 0); + wolfBoot_printf("PCIe 00:06.0: %s (0x%x)\r\n", + (id & 0xffff) == 0xffff ? "absent" : "present", id); +} +#endif /* TARGET_nai_68int6 */ + +int fsp_pre_pci_enum_cb(void) +{ +#ifdef TARGET_nai_68int6 + nai_pcie_ready(); +#endif + return 0; +} #endif /* TGL_FSP_H */ From 4987d221b23927c62a4766fe279b321e96e55675 Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 3 Nov 2026 08:37:32 -0800 Subject: [PATCH 04/17] x86 fsp: bound the ELF load, re-hash PCR 0, use a signed HDR_CMDLINE --- config/examples/nai_68int6.config | 1 - docs/Targets.md | 16 +++--- include/stage2_params.h | 3 ++ options.mk | 9 ++++ src/boot_x86_fsp.c | 22 +++++++-- src/boot_x86_fsp_payload.c | 54 ++++++++++++++++----- src/update_disk.c | 18 +++++++ tools/scripts/x86_fsp/compute_pcr.py | 10 ++-- tools/scripts/x86_fsp/tgl/assemble_image.sh | 5 +- tools/unit-tests/unit-update-disk-fsp.c | 32 ++++++++++++ 10 files changed, 136 insertions(+), 34 deletions(-) diff --git a/config/examples/nai_68int6.config b/config/examples/nai_68int6.config index b4fc1e4d34..9a3830ba0b 100644 --- a/config/examples/nai_68int6.config +++ b/config/examples/nai_68int6.config @@ -18,7 +18,6 @@ ENCRYPTION=0 WOLFBOOT_NO_PARTITIONS=1 # Disk boot: signed kernels in GPT partitions 3/4, Ubuntu root in partition 2. # The A/B boot-partition indices are set per target in src/update_disk.c. -WOLFBOOT_BOOT_DISK=0 FORCE_32BIT=1 ########## Flash layout (measured from the factory descriptor, 32 MB) ########## diff --git a/docs/Targets.md b/docs/Targets.md index 44732c2c4e..79b20e1af8 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -9185,7 +9185,7 @@ wolfBoot target `nai_68int6` for the North Atlantic Industries 68INT6, a 3U Open | | | |---|---| | CPU | i7-1185GRE (`06-8C-01`), Tiger Lake UP3, PCH-LP | -| Memory | 2 x 32 GB DDR4-3200 SODIMM, non-ECC | +| Memory | DDR4-3200, soldered down, with In-Band ECC (`FSP_IN_BAND_ECC`) | | BIOS flash | Rev C/D5: 2 x W25R256JV (32 MB), selected by JP36. Next revision: one 64 MB device | | TPM | SLB9670 (SHA-1/256), or SLB9672, which adds a SHA-384 bank | | Storage | Onboard SATA SSD, AHCI at `00:17.0` | @@ -9197,12 +9197,11 @@ wolfBoot target `nai_68int6` for the North Atlantic Industries 68INT6, a 3U Open cp config/examples/nai_68int6.config .config ./tools/scripts/x86_fsp/tgl/tgl_download_fsp.sh # FSP UPD headers, first time only ./tools/scripts/x86_fsp/tgl/nai_extract_fsp.sh <68INT6.bin> # board's own FSP blobs -./tools/scripts/x86_fsp/tgl/assemble_image.sh -k # generate signing keys -make +make # first build generates the signing key ./tools/scripts/x86_fsp/tgl/assemble_image.sh -n <68INT6.bin> ``` -`nai_extract_fsp.sh` takes FSP-T/M/S and the microcode from a 68INT6 BIOS image. Without it the build uses the public FSP, which compiles but does not train memory. The `-n` stitch replaces the top 6 MB (`BOOTLOADER_PARTITION_SIZE`) of the 12 MB BIOS region and keeps the descriptor and CSME byte for byte. The board has no recovery region (recovery is the second flash device), so use `-n`, not `-m`. The result is `final_image.bin`. +The host needs `nasm`, `gcc-multilib` and `python-is-python3` besides the usual build tools. `nai_extract_fsp.sh` takes FSP-T/M/S and the microcode from a 68INT6 BIOS image. Without it the build uses the public FSP, which compiles but does not train memory. The `-n` stitch replaces the top 6 MB (`BOOTLOADER_PARTITION_SIZE`) of the 12 MB BIOS region and keeps the descriptor and CSME byte for byte. The board has no recovery region (recovery is the second flash device). The result is `final_image.bin`. ### Integrity algorithms @@ -9214,7 +9213,7 @@ ECC P-384 and SHA-384 match what Intel Boot Guard uses for the Initial Boot Bloc | Image digest | SHA-384 (`HASH=SHA384`) | | Measured boot PCR | SHA-256 (the SLB9670 has only SHA-1 and SHA-256 banks) | -`wolfBoot_image_measure()` re-hashes the authenticated digest with the PCR bank's algorithm, so one image serves either TPM. `MEASURED_PCR_ALG=SHA384` needs an SLB9672 with its SHA-384 bank allocated (`TPM2_PCR_Allocate` under platform auth, a provisioning step). +Stage 1 re-hashes stage 2's authenticated SHA-384 digest with SHA-256 and extends PCR 0 with the result, as stage 2 does for the OS image in PCR 4, so `tools/scripts/x86_fsp/compute_pcr.py` can predict PCR 0. Only the SHA-256 PCR bank is supported. ### Configuration @@ -9230,7 +9229,6 @@ Notable options in [config/examples/nai_68int6.config](../config/examples/nai_68 | `NAI_68INT6_PCIE_LINK_DELAY_S`, `NAI_68INT6_BOOT_DELAY_S`, `NAI_68INT6_FPGA_READY_DELAY_S` | Waits before CPU PCIe link training (0-5 s), before polling PCIE_RDY (0-255 s), and added to the 5 s PCIE_RDY timeout (0, 5 or 10 s); all 0 by default. Before PCI enumeration wolfBoot polls PCIE_RDY (GPP_C23) every 50 ms, continues with a warning on timeout, then drives GPP_B12 high | | `ANTI_ROLLBACK=1` | Persistent version floor and boot audit counters in TPM NV | | `MEASURED_BOOT=1`, `MEASURED_PCR_OS=4` | Firmware into PCR 0, the verified OS image into PCR 4 | -| `WOLFBOOT_BOOT_DISK=0` | Boot from GPT partitions; the A/B indices (sda3/sda4) are set in `src/update_disk.c` | ### BIOS region lock @@ -9240,7 +9238,7 @@ Before handoff, `hal_flash_protect()` in the shared Tiger Lake HAL (`hal/x86_fsp `ANTI_ROLLBACK=1` keeps a version floor and boot audit counters in a TPM NV index; the backend is `src/boot_state_tpm.c` and the policy is `src/boot_state.c`. The floor is the highest version ever booted, set trust-on-first-use. A validly signed older image still boots and records a rollback event. Only an untrustworthy reference halts the boot; `ANTI_ROLLBACK_HALT_ON_FAILURE=1` also halts when the TPM is unreachable, a write fails or the platform hierarchy cannot be disabled. -TPM NV is used instead of the partition state trailer because the floor must survive a full reimage of the disk or flash. `DISK_BOOT_CONFIRM` still handles per-slot confirmation, and the backend can move to a root-of-trust device later without changing the policy. +TPM NV is used instead of the partition state trailer because the floor must survive a full reimage of the disk or flash. Per-slot boot confirmation is `DISK_BOOT_CONFIRM`, which this configuration leaves off, and the backend can move to a root-of-trust device later without changing the policy. `ALLOW_DOWNGRADE` is left unset, the secure default: if the higher-versioned slot fails to verify, wolfBoot halts (`Rollback to lower version not allowed`) rather than boot the lower one. Building with `ALLOW_DOWNGRADE=1` allows that fall-back and leaves the TPM floor as the only rollback guard. @@ -9248,7 +9246,7 @@ The booted OS cannot lower the floor. The index is platform-created and writable ### Boot, programming and recovery -wolfBoot boots the highest-versioned signed 64-bit `bzImage` or Multiboot2 ELF that verifies from the two GPT boot partitions. It falls back to the other slot only if that is not a downgrade (see Anti-rollback), and halts if neither boots. Program the flash with a Dediprog SF100 on JP4 while the board is off; the BIOS region lock blocks `flashrom` from the booted OS. JP36 selects which bank boots and which one the programmer sees; move it only with the board off. +wolfBoot boots the highest-versioned signed 64-bit `bzImage` or Multiboot2 ELF that verifies from the two GPT boot partitions. It falls back to the other slot only if that is not a downgrade (see Anti-rollback), and halts if neither boots. Before handoff it sets FULL_RST in the Reset Control register (0xCF9), as the stock BIOS does, so a later reset such as SYS_RESET# from the IPMC is a full reset. A kernel command line signed into the OS image (`sign --cmdline`, the `HDR_CMDLINE` TLV) replaces the built-in one, so changing a kernel argument does not need a reflash. Program the flash with a Dediprog SF100 on JP4 while the board is off; the BIOS region lock blocks `flashrom` from the booted OS. JP36 selects which bank boots and which one the programmer sees; move it only with the board off. ### Multiboot2 ELF payloads @@ -9267,7 +9265,7 @@ The two paths differ only in the handoff: | ACPI tables | Generated (`ACPI=1`) | None; they are only built on the Linux path | | RTC | The legacy CMOS RTC is ignored under the hardware-reduced ACPI tables; the I2C0 RTC is declared in the DSDT | The CMOS RTC is usable directly; I2C0 is enabled for the board RTC | -Everything else is shared: FSP silicon setup (VT-d at the stock BIOS addresses, Hyper-Threading off), the halted PCH TCO watchdog, the BIOS region lock, measured boot, anti-rollback and A/B selection. +Segments are loaded from the verified image only, and a segment that would overlap wolfBoot's own memory (from the stage 2 load address up to 4 GB) is rejected before anything is copied. Everything else is shared: FSP silicon setup (VT-d at the stock BIOS addresses, Hyper-Threading off), the halted PCH TCO watchdog, the BIOS region lock, measured boot, anti-rollback and A/B selection. ## Infineon PSOC Control C3 diff --git a/include/stage2_params.h b/include/stage2_params.h index cb172bcc0c..2be2f3f169 100644 --- a/include/stage2_params.h +++ b/include/stage2_params.h @@ -31,6 +31,9 @@ struct stage2_parameter { /* Verified payload length, set by the disk loader before do_boot so the * Linux loader can bound the signed container header against the image. */ uint32_t payload_size; + /* Address of the signed HDR_CMDLINE of the verified image, NUL-terminated, + * or 0 when it carries none */ + uint32_t cmdline; #ifdef WOLFBOOT_FSP_NVS_CACHE uint32_t fsp_nvs_key; #endif diff --git a/options.mk b/options.mk index cf16bad110..4d6b152a38 100644 --- a/options.mk +++ b/options.mk @@ -76,6 +76,15 @@ ifeq ($(TARGET),nai_68int6) $(if $($(option)),$(eval CFLAGS+=-D$(option)=$($(option))))) endif +# These options need wolfTPM and turn it on, so an explicit WOLFTPM=0 alongside +# one of them is a conflict rather than something to override silently. +ifeq ($(WOLFTPM),0) + ifneq ($(filter 1,$(ANTI_ROLLBACK) $(MEASURED_BOOT) $(WOLFBOOT_TPM_VERIFY) \ + $(WOLFBOOT_TPM_KEYSTORE) $(WOLFBOOT_TPM_SEAL)),) + $(error WOLFTPM=0 conflicts with ANTI_ROLLBACK, MEASURED_BOOT, WOLFBOOT_TPM_VERIFY, WOLFBOOT_TPM_KEYSTORE or WOLFBOOT_TPM_SEAL; set those to 0 too) + endif +endif + # TPM NV version floor and boot audit counters (see docs/Targets.md). # HALT_ON_FAILURE also halts when the TPM state cannot be read or locked. ifeq ($(ANTI_ROLLBACK),1) diff --git a/src/boot_x86_fsp.c b/src/boot_x86_fsp.c index 81239ba233..8eea8c5098 100644 --- a/src/boot_x86_fsp.c +++ b/src/boot_x86_fsp.c @@ -265,10 +265,17 @@ static void jump_into_wolfboot(void) #endif /* WOLFBOOT_64BIT */ #if defined(WOLFBOOT_MEASURED_BOOT) -/* The image needs to be already verified */ +#if WOLFBOOT_TPM_PCR_DIG_SZ != 32 +#error "measured boot: the stage 1 re-hash only implements a SHA-256 PCR bank" +#endif +/* The image needs to be already verified. Its authenticated digest is + * re-hashed with SHA-256 and that value extended, as the OS measurement in + * update_disk.c is. */ int wolfBoot_image_measure(uint8_t *image) { struct wolfBoot_image img; + uint8_t digest[SHA256_DIGEST_SIZE]; + wc_Sha256 sha; int ret; memset(&img, 0, sizeof(img)); @@ -282,9 +289,16 @@ int wolfBoot_image_measure(uint8_t *image) return -1; } - wolfBoot_print_hexstr(img.sha_hash, WOLFBOOT_SHA_DIGEST_SIZE, 0); - return wolfBoot_tpm2_extend(WOLFBOOT_MEASURED_PCR_A, img.sha_hash, - __LINE__); + ret = wc_InitSha256(&sha); + if (ret == 0) + ret = wc_Sha256Update(&sha, img.sha_hash, WOLFBOOT_SHA_DIGEST_SIZE); + if (ret == 0) + ret = wc_Sha256Final(&sha, digest); + if (ret != 0) + return -1; + + wolfBoot_print_hexstr(digest, sizeof(digest), 0); + return wolfBoot_tpm2_extend(WOLFBOOT_MEASURED_PCR_A, digest, __LINE__); } #endif /* WOLFBOOT_MEASURED_BOOT */ diff --git a/src/boot_x86_fsp_payload.c b/src/boot_x86_fsp_payload.c index ecdbe6c418..de86c2e723 100644 --- a/src/boot_x86_fsp_payload.c +++ b/src/boot_x86_fsp_payload.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -95,6 +96,36 @@ void jump(uintptr_t entry) } #if defined(WOLFBOOT_ELF) +/* wolfBoot's own memory: stage 2, the staged payload after it, and the stack, + * page tables and FSP memory above them, all from the stage 2 load address + * up to 4 GB. */ +#define ELF_RESERVED_START ((uint64_t)WOLFBOOT_LOAD_BASE - IMAGE_HEADER_SIZE) +#define ELF_RESERVED_END (1ULL << 32) + +static int elf_reserved_overlap(uint64_t addr, uint32_t size) +{ + return size != 0 && addr < ELF_RESERVED_END && + addr + size > ELF_RESERVED_START; +} + +/* The loader copies through vaddr, and an address that is already mapped + * keeps its identity mapping, so both ranges are checked. */ +static int boot_elf_map_cb(uint64_t vaddr, uint64_t paddr, uint32_t size) +{ + if (elf_reserved_overlap(paddr, size) || + elf_reserved_overlap(vaddr, size)) { + wolfBoot_printf("ELF: segment at 0x%x_%x overlaps wolfBoot memory\r\n", + (uint32_t)(paddr >> 32), (uint32_t)paddr); + return -1; + } +#ifdef WOLFBOOT_64BIT + return x86_paging_map_memory(vaddr, paddr, size); +#else + (void)vaddr; + return 0; +#endif +} + /** * @brief Load an ELF image and boot it, through Multiboot2 when enabled. * @@ -105,24 +136,19 @@ static void boot_elf(const uint32_t *app, struct stage2_parameter *stage2_params) { int r; - uint64_t e; + uint64_t e = 0; uint8_t *mb2_header; - elf_mmu_map_cb mmu_cb = NULL; - -#ifdef WOLFBOOT_64BIT - mmu_cb = x86_paging_map_memory; -#endif /* WOLFBOOT_64BIT */ /* TODO: to remove */ mptable_setup(); x86_paging_dump_info(); - /* Use available low memory as upper bound for ELF image size */ - if (stage2_params->tolum <= (uint32_t)(uintptr_t)app) { - wolfBoot_printf("ELF: app address beyond TOLUM\r\n"); + /* The disk loader records the verified payload size */ + if (stage2_params->payload_size == 0) { + wolfBoot_printf("ELF: no verified payload size\r\n"); panic(); } - r = elf_load_image_mmu((uint8_t *)app, - stage2_params->tolum - (uint32_t)(uintptr_t)app, &e, mmu_cb); + r = elf_load_image_mmu((uint8_t *)app, stage2_params->payload_size, &e, + boot_elf_map_cb); wolfBoot_printf("Elf loaded (ret %d), entry 0x%x_%x\r\n", r, (uint32_t)(e >> 32), (uint32_t)(e)); @@ -131,7 +157,8 @@ static void boot_elf(const uint32_t *app, panic(); #ifdef WOLFBOOT_MULTIBOOT2 - mb2_header = mb2_find_header((uint8_t*)app, 1024*1024*15); + mb2_header = mb2_find_header((uint8_t*)app, + (int)stage2_params->payload_size); if (mb2_header == NULL) { wolfBoot_printf("No mb2 header found\r\n"); panic(); @@ -181,7 +208,8 @@ void do_boot(const uint32_t *app) #if defined(WOLFBOOT_LINUX_PAYLOAD) mptable_setup(); load_linux((uint8_t *)app, stage2_params->payload_size, stage2_params, - cmdline); + stage2_params->cmdline != 0 ? + (const char *)(uintptr_t)stage2_params->cmdline : cmdline); #elif defined(WOLFBOOT_ELF) boot_elf(app, stage2_params); #else diff --git a/src/update_disk.c b/src/update_disk.c index 50e37f9359..be21989017 100644 --- a/src/update_disk.c +++ b/src/update_disk.c @@ -1590,6 +1590,24 @@ void RAMFUNCTION wolfBoot_start(void) boot_state_on_boot(booted_ver); } #endif +#ifdef WOLFBOOT_FSP + /* The image's signed kernel command line, read before the decrypted + * header is wiped */ + stage2_params->cmdline = 0; + { + static char disk_cmdline[256]; + uint8_t *val = NULL; + uint16_t len; + + len = wolfBoot_find_header((uint8_t *)hdr_ptr + IMAGE_HEADER_OFFSET, + HDR_CMDLINE, &val); + if (len > 0 && val != NULL && len < sizeof(disk_cmdline)) { + memcpy(disk_cmdline, val, len); + disk_cmdline[len] = '\0'; + stage2_params->cmdline = (uint32_t)(uintptr_t)disk_cmdline; + } + } +#endif #ifdef DISK_ENCRYPT disk_decrypted_header_clear(dec_hdr); disk_crypto_clear(); diff --git a/tools/scripts/x86_fsp/compute_pcr.py b/tools/scripts/x86_fsp/compute_pcr.py index 7e3c1048e9..9f19458de3 100644 --- a/tools/scripts/x86_fsp/compute_pcr.py +++ b/tools/scripts/x86_fsp/compute_pcr.py @@ -43,10 +43,11 @@ def get_config_value(config: str, name): def get_sha256_hash_of_wolfboot_image(file_path: str): """ - Get sha256 hash of wolfboot image at file_path + Get the image digest (SHA-256 or SHA-384 TLV) of wolfboot image at file_path """ HDR_OFF = 8 WOLFBOOT_SHA_HDR = 0x03 + WOLFBOOT_SHA384_HDR = 0x14 with open(file_path, 'rb') as f: data = f.read() data = data[HDR_OFF:] @@ -57,7 +58,7 @@ def get_sha256_hash_of_wolfboot_image(file_path: str): t, l = struct.unpack(' bytearray: is_measured_boot = get_config_value(config, 'MEASURED_BOOT') == '1' - # wolfBoot_image_measure() extends PCR with wolfboot image hash + # wolfBoot_image_measure() re-hashes the wolfboot image digest with + # SHA-256 and extends PCR0 with the result if is_measured_boot: wb_hash = get_sha256_hash_of_wolfboot_image('stage1/wolfboot_raw_v1_signed.bin') - pcr0 = pcr_extend(pcr0, wb_hash) + pcr0 = pcr_extend(pcr0, get_sha256_hash(wb_hash)) print(f"PCR0 after wolfboot image measure: {pcr0.hex()}") # the pcrdigest needed by policy_sign tool is the hash of the concatenation of all PCRs involved in the policy. diff --git a/tools/scripts/x86_fsp/tgl/assemble_image.sh b/tools/scripts/x86_fsp/tgl/assemble_image.sh index 13d06de1fe..d1abde1da0 100755 --- a/tools/scripts/x86_fsp/tgl/assemble_image.sh +++ b/tools/scripts/x86_fsp/tgl/assemble_image.sh @@ -49,14 +49,13 @@ assemble() } # Parse command line options -while getopts "s:n:m:" opt; do +while getopts "n:" opt; do case "$opt" in n) FLASH_DUMP="$OPTARG" ;; *) - echo "Usage: $0 [-k] [-s FLASH_DUMP]" - echo "-k: make keys" + echo "Usage: $0 -n FLASH_DUMP" echo "-n FLASH_DUMP: assemble an image for being used without IBG" exit 1 ;; diff --git a/tools/unit-tests/unit-update-disk-fsp.c b/tools/unit-tests/unit-update-disk-fsp.c index e38e4a1a8f..8646804f85 100644 --- a/tools/unit-tests/unit-update-disk-fsp.c +++ b/tools/unit-tests/unit-update-disk-fsp.c @@ -37,6 +37,7 @@ static uint8_t part_b_image[IMAGE_HEADER_SIZE + TEST_PAYLOAD_SIZE]; static int mock_do_boot_called; static const uint32_t *mock_boot_address; static struct stage2_parameter mock_stage2_params; +static const char *mock_cmdline; static void set_u16_le(uint8_t *dst, uint16_t value) { @@ -74,6 +75,8 @@ static void reset_mocks(void) build_image(part_b_image, 7, TEST_PAYLOAD_SIZE, 0xB2); mock_do_boot_called = 0; mock_boot_address = NULL; + mock_cmdline = NULL; + mock_stage2_params.cmdline = 0xffffffffU; /* The image must fit between load_address and tolum: exactly one * TEST_PAYLOAD_SIZE payload. */ mock_stage2_params.tolum = @@ -170,6 +173,17 @@ int wolfBoot_image_part_matches(struct wolfBoot_image* img) return 0; } +uint16_t wolfBoot_find_header(uint8_t *haystack, uint16_t type, uint8_t **ptr) +{ + (void)haystack; + if (type != HDR_CMDLINE || mock_cmdline == NULL) { + *ptr = NULL; + return 0; + } + *ptr = (uint8_t *)mock_cmdline; + return (uint16_t)strlen(mock_cmdline); +} + int wolfBoot_get_dts_size(void *dts_addr, uint32_t capacity) { (void)capacity; @@ -273,6 +287,23 @@ START_TEST(test_fsp_fitting_slot_boots) } END_TEST +START_TEST(test_fsp_signed_cmdline_is_passed_on) +{ + /* A signed HDR_CMDLINE reaches the payload loader; without one the + * loader is told there is none. */ + reset_mocks(); + mock_cmdline = "console=ttyS4,115200 root=/dev/sda2"; + wolfBoot_start(); + ck_assert_int_eq(mock_do_boot_called, 1); + ck_assert_uint_ne(mock_stage2_params.cmdline, 0); + + reset_mocks(); + wolfBoot_start(); + ck_assert_int_eq(mock_do_boot_called, 1); + ck_assert_uint_eq(mock_stage2_params.cmdline, 0); +} +END_TEST + Suite *wolfboot_suite(void) { Suite *s = suite_create("wolfBoot"); @@ -282,6 +313,7 @@ Suite *wolfboot_suite(void) tcase_add_test(tc, test_fsp_both_slots_oversized_panics); tcase_add_test(tc, test_fsp_inverted_tolum_rejects_both_slots); tcase_add_test(tc, test_fsp_fitting_slot_boots); + tcase_add_test(tc, test_fsp_signed_cmdline_is_passed_on); suite_add_tcase(s, tc); return s; From 4a0685c968e9761aba69abd21f156a94300effd6 Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 3 Nov 2026 11:14:29 -0800 Subject: [PATCH 05/17] x86 fsp: CPU list from topology, TPM2/HPET/DMAR, root port routing, _OSC --- .gitignore | 5 + config/examples/nai_68int6.config | 2 + docs/Targets.md | 5 +- include/x86/acpi.h | 4 +- include/x86/acpi_dsdt.h | 205 ++++++++++------- include/x86/common.h | 3 + include/x86/fsp_config.h | 6 +- include/x86/mptable.h | 5 +- options.mk | 6 +- src/boot_x86_fsp.c | 9 +- src/update_disk.c | 50 +++-- src/x86/acpi.c | 209 ++++++++++++++++-- src/x86/acpi_dsdt.asl | 97 ++++++++ src/x86/common.c | 69 ++++++ src/x86/mptable.c | 40 +++- tools/unit-tests/Makefile | 40 ++++ tools/unit-tests/unit-boot-x86-fsp-measure.c | 142 ++++++++++++ tools/unit-tests/unit-boot-x86-fsp-payload.c | 210 ++++++++++++++++++ tools/unit-tests/unit-update-disk-fsp.c | 101 ++++++++- tools/unit-tests/unit-x86-acpi.c | 221 +++++++++++++++++++ tools/unit-tests/unit-x86-apic-ids.c | 112 ++++++++++ 21 files changed, 1397 insertions(+), 144 deletions(-) create mode 100644 tools/unit-tests/unit-boot-x86-fsp-measure.c create mode 100644 tools/unit-tests/unit-boot-x86-fsp-payload.c create mode 100644 tools/unit-tests/unit-x86-acpi.c create mode 100644 tools/unit-tests/unit-x86-apic-ids.c diff --git a/.gitignore b/.gitignore index 18dc0805ef..cc55bff7b2 100644 --- a/.gitignore +++ b/.gitignore @@ -636,6 +636,10 @@ tools/unit-tests/unit-kontron-tgl-spi tools/unit-tests/unit-fsp-nvs tools/unit-tests/unit-fsp-nvs-save tools/unit-tests/unit-tgl-spi-hwseq +tools/unit-tests/unit-x86-apic-ids +tools/unit-tests/unit-x86-acpi +tools/unit-tests/unit-boot-x86-fsp-measure +tools/unit-tests/unit-boot-x86-fsp-payload tools/unit-tests/unit-nrf5340-flash-protect tools/unit-tests/unit-nrf5340-uart-crlf tools/unit-tests/unit-nrf54l-uart-write @@ -682,6 +686,7 @@ tools/unit-tests/kinetis_erase_extract.h tools/unit-tests/kontron_spi_extract.h tools/unit-tests/kontron_spi_fn_extract.h tools/unit-tests/tgl_spi_hwseq_extract.h +tools/unit-tests/x86_apic_ids_extract.h tools/unit-tests/nrf5340_protect_fn_extract.h tools/unit-tests/nrf54l_uart_write_extract.h tools/unit-tests/p1021_erase_extract.h diff --git a/config/examples/nai_68int6.config b/config/examples/nai_68int6.config index 9a3830ba0b..b99f04b809 100644 --- a/config/examples/nai_68int6.config +++ b/config/examples/nai_68int6.config @@ -72,6 +72,8 @@ PCI_USE_ECAM=1 PCH_HAS_PCR=1 # Generate ACPI tables so Linux gets the interrupt model and PCI routing. ACPI=1 +# MP table entries, one per logical processor: 4 cores with Hyper-Threading +MP_CPU_NUM_ENTRY=8 ########## PCIe switch and backplane (stock BIOS defaults) ########## # Lanes: 8x1, 1x4_4x1, 4x1_1x4, 1x4_1x4 or 1x8. Delays are in seconds. diff --git a/docs/Targets.md b/docs/Targets.md index 79b20e1af8..ea691f05fa 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -9222,8 +9222,11 @@ Notable options in [config/examples/nai_68int6.config](../config/examples/nai_68 | Option | Meaning | |---|---| | `X86_UART_NUMBER=2` | LPSS UART2 for all three FSP phases (Kontron uses 0) | -| `ACPI=1` | Required. Generates ACPI tables (RSDP/XSDT/FADT/MADT/MCFG and a minimal DSDT); the board's PCI interrupt routing is only in the DSDT `_PRT`, since the MP table is the Kontron one. The DSDT also declares the board devices on LPSS I2C0 (EEPROMs, RTC/watchdog) and GSPI0 (the PRC, for NAI's `naigspi` driver) | +| `ACPI=1` | Required. Generates ACPI tables (RSDP/XSDT/FADT/MADT/MCFG/TPM2/HPET/DMAR and a minimal DSDT with the TPM device); the board's PCI interrupt routing is only in the DSDT `_PRT`, since the MP table is the Kontron one. The DSDT also declares the board devices on LPSS I2C0 (EEPROMs, RTC/watchdog) and GSPI0 (the PRC, for NAI's `naigspi` driver) | | `FSP_NVS_CACHE=1`, `FSP_NVS_BASE`, `FSP_NVS_SIZE` | Keeps FSP-M's memory training data (its non-volatile storage HOB) in a 128 KB area of the BIOS region below the wolfBoot window, so later boots skip full memory training. Stage 1 passes a valid copy back through `NvsBufferPtr`; stage 2 rewrites it, before FSP-S and before the BIOS region is locked, only when it changes. The record is keyed on a CRC of the FSP-M image and its UPDs, so a change to either falls back to full training. If FspMemInit fails with a saved record, stage 1 sets a CMOS flag and cold-resets, boots train from scratch until FspMemInit succeeds, and stage 2 replaces the record. `assemble_image.sh -n` blanks the area, so an image built from a board's flash dump does not carry that board's training data | +| `FSP_HYPERTHREADING=1` | Hyper-Threading on (off by default). The MADT and the MP table list the enabled logical processors from the CPUID topology, so either setting works | +| `MP_CPU_NUM_ENTRY=8` | MP table processor entries, one per logical processor (default 4) | +| `WOLFBOOT_CMDLINE_MAX` | Buffer for a signed `HDR_CMDLINE`, including the terminating NUL (default 256); a longer command line is ignored and the built-in one is used | | `NAI_68INT6_PCIE_LANES` | PCIe switch lane configuration, strapped on GPP_B21/B22/S4/S7/S6 before memory init: `8x1` (default), `1x4_4x1`, `4x1_1x4`, `1x4_1x4` or `1x8` | | `NAI_68INT6_PCIE_NT`, `NAI_68INT6_BACKPLANE_CLK` | PCIe switch non-transparent mode (GPP_S2) and the backplane PCIe clock (GPP_A0); both off by default | | `NAI_68INT6_PCIE_LINK_DELAY_S`, `NAI_68INT6_BOOT_DELAY_S`, `NAI_68INT6_FPGA_READY_DELAY_S` | Waits before CPU PCIe link training (0-5 s), before polling PCIE_RDY (0-255 s), and added to the 5 s PCIE_RDY timeout (0, 5 or 10 s); all 0 by default. Before PCI enumeration wolfBoot polls PCIE_RDY (GPP_C23) every 50 ms, continues with a warning on timeout, then drives GPP_B12 high | diff --git a/include/x86/acpi.h b/include/x86/acpi.h index 676353df5b..1c92eb6696 100644 --- a/include/x86/acpi.h +++ b/include/x86/acpi.h @@ -30,8 +30,8 @@ #endif #define ACPI_TABLE_SIZE 0x00010000UL /* 64 KiB */ -/* Build RSDP -> XSDT -> { FADT, MADT, MCFG, DSDT } at ACPI_TABLE_BASE and - * return the RSDP physical address for boot_params.acpi_rsdp_addr. */ +/* Build RSDP -> XSDT -> { FADT, MADT, MCFG, TPM2, HPET, DMAR, DSDT } at + * ACPI_TABLE_BASE and return the RSDP physical address for boot_params.acpi_rsdp_addr. */ uint64_t acpi_setup(void); #endif /* WOLFBOOT_ACPI_H */ diff --git a/include/x86/acpi_dsdt.h b/include/x86/acpi_dsdt.h index af6aa92c94..2d84992f75 100644 --- a/include/x86/acpi_dsdt.h +++ b/include/x86/acpi_dsdt.h @@ -22,17 +22,18 @@ * Regenerate: iasl src/x86/acpi_dsdt.asl && * xxd -i -n acpi_dsdt_aml src/x86/acpi_dsdt.aml (append below this header). */ static const unsigned char acpi_dsdt_aml[] = { - 0x44, 0x53, 0x44, 0x54, 0x29, 0x05, 0x00, 0x00, 0x02, 0xa7, 0x57, 0x4f, + 0x44, 0x53, 0x44, 0x54, 0x15, 0x07, 0x00, 0x00, 0x02, 0xfb, 0x57, 0x4f, 0x4c, 0x46, 0x42, 0x54, 0x4e, 0x41, 0x49, 0x54, 0x47, 0x4c, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x49, 0x4e, 0x54, 0x4c, 0x28, 0x06, 0x23, 0x20, - 0x10, 0x44, 0x50, 0x5f, 0x53, 0x42, 0x5f, 0x5b, 0x82, 0x4c, 0x4f, 0x50, + 0x10, 0x40, 0x6f, 0x5f, 0x53, 0x42, 0x5f, 0x5b, 0x82, 0x42, 0x66, 0x50, 0x43, 0x49, 0x30, 0x08, 0x5f, 0x48, 0x49, 0x44, 0x0c, 0x41, 0xd0, 0x0a, 0x08, 0x08, 0x5f, 0x43, 0x49, 0x44, 0x0c, 0x41, 0xd0, 0x0a, 0x03, 0x08, 0x5f, 0x53, 0x45, 0x47, 0x00, 0x08, 0x5f, 0x42, 0x42, 0x4e, 0x00, 0x08, - 0x5f, 0x55, 0x49, 0x44, 0x00, 0x14, 0x46, 0x12, 0x5f, 0x50, 0x52, 0x54, - 0x00, 0xa4, 0x12, 0x4d, 0x11, 0x17, 0x12, 0x0b, 0x04, 0x0c, 0xff, 0xff, + 0x5f, 0x55, 0x49, 0x44, 0x00, 0x14, 0x42, 0x13, 0x5f, 0x50, 0x52, 0x54, + 0x00, 0xa4, 0x12, 0x49, 0x12, 0x18, 0x12, 0x0b, 0x04, 0x0c, 0xff, 0xff, 0x02, 0x00, 0x00, 0x00, 0x0a, 0x10, 0x12, 0x0b, 0x04, 0x0c, 0xff, 0xff, 0x04, 0x00, 0x00, 0x00, 0x0a, 0x10, 0x12, 0x0b, 0x04, 0x0c, 0xff, 0xff, + 0x06, 0x00, 0x00, 0x00, 0x0a, 0x10, 0x12, 0x0b, 0x04, 0x0c, 0xff, 0xff, 0x0d, 0x00, 0x00, 0x00, 0x0a, 0x10, 0x12, 0x0b, 0x04, 0x0c, 0xff, 0xff, 0x14, 0x00, 0x00, 0x00, 0x0a, 0x10, 0x12, 0x0b, 0x04, 0x0c, 0xff, 0xff, 0x14, 0x00, 0x01, 0x00, 0x0a, 0x11, 0x12, 0x0b, 0x04, 0x0c, 0xff, 0xff, @@ -54,84 +55,124 @@ static const unsigned char acpi_dsdt_aml[] = { 0x0b, 0x04, 0x0c, 0xff, 0xff, 0x1e, 0x00, 0x01, 0x00, 0x0a, 0x11, 0x12, 0x0c, 0x04, 0x0c, 0xff, 0xff, 0x1e, 0x00, 0x0a, 0x02, 0x00, 0x0a, 0x24, 0x12, 0x0b, 0x04, 0x0c, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x0a, 0x10, - 0x08, 0x5f, 0x43, 0x52, 0x53, 0x11, 0x48, 0x09, 0x0a, 0x94, 0x88, 0x0d, - 0x00, 0x02, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, - 0x18, 0x00, 0x88, 0x0d, 0x00, 0x01, 0x0c, 0x03, 0x00, 0x00, 0x00, 0x00, - 0xf7, 0x0c, 0x00, 0x00, 0xf8, 0x0c, 0x88, 0x0d, 0x00, 0x01, 0x0c, 0x03, - 0x00, 0x00, 0x00, 0x0d, 0xff, 0xff, 0x00, 0x00, 0x00, 0xf3, 0x87, 0x17, - 0x00, 0x00, 0x0c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, - 0xff, 0xff, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, - 0x87, 0x17, 0x00, 0x00, 0x0c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x80, 0xff, 0xff, 0xff, 0xbf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x40, 0x8a, 0x2b, 0x00, 0x00, 0x0c, 0x01, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, - 0xff, 0xff, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, - 0x79, 0x00, 0x5b, 0x82, 0x49, 0x25, 0x49, 0x32, 0x43, 0x30, 0x08, 0x5f, - 0x41, 0x44, 0x52, 0x0c, 0x00, 0x00, 0x15, 0x00, 0x5b, 0x82, 0x43, 0x0c, - 0x45, 0x45, 0x50, 0x32, 0x08, 0x5f, 0x48, 0x49, 0x44, 0x0d, 0x31, 0x35, - 0x41, 0x43, 0x30, 0x30, 0x30, 0x32, 0x00, 0x08, 0x5f, 0x43, 0x49, 0x44, - 0x0d, 0x50, 0x52, 0x50, 0x30, 0x30, 0x30, 0x31, 0x00, 0x08, 0x5f, 0x44, - 0x44, 0x4e, 0x0d, 0x4e, 0x41, 0x49, 0x20, 0x4d, 0x6f, 0x74, 0x68, 0x65, - 0x72, 0x62, 0x6f, 0x61, 0x72, 0x64, 0x20, 0x45, 0x45, 0x50, 0x52, 0x4f, - 0x4d, 0x00, 0x08, 0x5f, 0x43, 0x52, 0x53, 0x11, 0x26, 0x0a, 0x23, 0x8e, - 0x1e, 0x00, 0x02, 0x00, 0x01, 0x02, 0x00, 0x00, 0x01, 0x06, 0x00, 0xa0, - 0x86, 0x01, 0x00, 0x56, 0x00, 0x5c, 0x5f, 0x53, 0x42, 0x2e, 0x50, 0x43, - 0x49, 0x30, 0x2e, 0x49, 0x32, 0x43, 0x30, 0x00, 0x79, 0x00, 0x08, 0x5f, - 0x44, 0x53, 0x44, 0x12, 0x41, 0x05, 0x02, 0x11, 0x13, 0x0a, 0x10, 0x14, - 0xd8, 0xff, 0xda, 0xba, 0x6e, 0x8c, 0x4d, 0x8a, 0x91, 0xbc, 0x9b, 0xbf, - 0x4a, 0xa3, 0x01, 0x12, 0x39, 0x03, 0x12, 0x1b, 0x02, 0x0d, 0x63, 0x6f, - 0x6d, 0x70, 0x61, 0x74, 0x69, 0x62, 0x6c, 0x65, 0x00, 0x0d, 0x61, 0x74, - 0x6d, 0x65, 0x6c, 0x2c, 0x32, 0x34, 0x63, 0x30, 0x32, 0x00, 0x12, 0x0b, - 0x02, 0x0d, 0x73, 0x69, 0x7a, 0x65, 0x00, 0x0b, 0x00, 0x01, 0x12, 0x0e, - 0x02, 0x0d, 0x70, 0x61, 0x67, 0x65, 0x73, 0x69, 0x7a, 0x65, 0x00, 0x0a, - 0x08, 0x5b, 0x82, 0x46, 0x0d, 0x45, 0x45, 0x50, 0x33, 0x08, 0x5f, 0x48, - 0x49, 0x44, 0x0d, 0x31, 0x35, 0x41, 0x43, 0x30, 0x30, 0x30, 0x33, 0x00, - 0x08, 0x5f, 0x43, 0x49, 0x44, 0x0d, 0x50, 0x52, 0x50, 0x30, 0x30, 0x30, - 0x31, 0x00, 0x08, 0x5f, 0x44, 0x44, 0x4e, 0x0d, 0x4e, 0x41, 0x49, 0x20, - 0x4d, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x62, 0x6f, 0x61, 0x72, 0x64, 0x20, - 0x45, 0x45, 0x50, 0x52, 0x4f, 0x4d, 0x20, 0x53, 0x4e, 0x00, 0x08, 0x5f, - 0x43, 0x52, 0x53, 0x11, 0x26, 0x0a, 0x23, 0x8e, 0x1e, 0x00, 0x02, 0x00, - 0x01, 0x02, 0x00, 0x00, 0x01, 0x06, 0x00, 0xa0, 0x86, 0x01, 0x00, 0x5e, - 0x00, 0x5c, 0x5f, 0x53, 0x42, 0x2e, 0x50, 0x43, 0x49, 0x30, 0x2e, 0x49, - 0x32, 0x43, 0x30, 0x00, 0x79, 0x00, 0x08, 0x5f, 0x44, 0x53, 0x44, 0x12, - 0x41, 0x06, 0x02, 0x11, 0x13, 0x0a, 0x10, 0x14, 0xd8, 0xff, 0xda, 0xba, - 0x6e, 0x8c, 0x4d, 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01, 0x12, - 0x49, 0x04, 0x04, 0x12, 0x1c, 0x02, 0x0d, 0x63, 0x6f, 0x6d, 0x70, 0x61, - 0x74, 0x69, 0x62, 0x6c, 0x65, 0x00, 0x0d, 0x61, 0x74, 0x6d, 0x65, 0x6c, - 0x2c, 0x32, 0x34, 0x63, 0x73, 0x30, 0x32, 0x00, 0x12, 0x0a, 0x02, 0x0d, - 0x73, 0x69, 0x7a, 0x65, 0x00, 0x0a, 0x10, 0x12, 0x0e, 0x02, 0x0d, 0x70, - 0x61, 0x67, 0x65, 0x73, 0x69, 0x7a, 0x65, 0x00, 0x0a, 0x08, 0x12, 0x0e, - 0x02, 0x0d, 0x72, 0x65, 0x61, 0x64, 0x2d, 0x6f, 0x6e, 0x6c, 0x79, 0x00, - 0x01, 0x5b, 0x82, 0x4a, 0x0a, 0x52, 0x54, 0x43, 0x31, 0x08, 0x5f, 0x48, - 0x49, 0x44, 0x0d, 0x31, 0x35, 0x41, 0x43, 0x30, 0x30, 0x30, 0x34, 0x00, - 0x08, 0x5f, 0x43, 0x49, 0x44, 0x0d, 0x50, 0x52, 0x50, 0x30, 0x30, 0x30, - 0x31, 0x00, 0x08, 0x5f, 0x44, 0x44, 0x4e, 0x0d, 0x4e, 0x41, 0x49, 0x20, - 0x4d, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x62, 0x6f, 0x61, 0x72, 0x64, 0x20, - 0x57, 0x44, 0x54, 0x20, 0x52, 0x54, 0x43, 0x00, 0x08, 0x5f, 0x43, 0x52, + 0x14, 0x49, 0x04, 0x5f, 0x4f, 0x53, 0x43, 0x0c, 0x70, 0x6b, 0x60, 0x8a, + 0x60, 0x00, 0x43, 0x44, 0x57, 0x31, 0xa0, 0x28, 0x93, 0x68, 0x11, 0x13, + 0x0a, 0x10, 0x5b, 0x4d, 0xdb, 0x33, 0xf7, 0x1f, 0x1c, 0x40, 0x96, 0x57, + 0x74, 0x41, 0xc0, 0x3d, 0xd7, 0x66, 0xa0, 0x10, 0x92, 0x93, 0x69, 0x01, + 0x7d, 0x43, 0x44, 0x57, 0x31, 0x0a, 0x08, 0x43, 0x44, 0x57, 0x31, 0xa1, + 0x0c, 0x7d, 0x43, 0x44, 0x57, 0x31, 0x0a, 0x04, 0x43, 0x44, 0x57, 0x31, + 0xa4, 0x60, 0x08, 0x5f, 0x43, 0x52, 0x53, 0x11, 0x48, 0x09, 0x0a, 0x94, + 0x88, 0x0d, 0x00, 0x02, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, + 0x00, 0x00, 0x18, 0x00, 0x88, 0x0d, 0x00, 0x01, 0x0c, 0x03, 0x00, 0x00, + 0x00, 0x00, 0xf7, 0x0c, 0x00, 0x00, 0xf8, 0x0c, 0x88, 0x0d, 0x00, 0x01, + 0x0c, 0x03, 0x00, 0x00, 0x00, 0x0d, 0xff, 0xff, 0x00, 0x00, 0x00, 0xf3, + 0x87, 0x17, 0x00, 0x00, 0x0c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x0a, 0x00, 0xff, 0xff, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x87, 0x17, 0x00, 0x00, 0x0c, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x80, 0xff, 0xff, 0xff, 0xbf, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x40, 0x8a, 0x2b, 0x00, 0x00, 0x0c, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, + 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, + 0x00, 0x00, 0x79, 0x00, 0x5b, 0x82, 0x42, 0x04, 0x50, 0x45, 0x47, 0x30, + 0x08, 0x5f, 0x41, 0x44, 0x52, 0x0c, 0x00, 0x00, 0x06, 0x00, 0x08, 0x5f, + 0x50, 0x52, 0x54, 0x12, 0x2c, 0x04, 0x12, 0x09, 0x04, 0x0b, 0xff, 0xff, + 0x00, 0x00, 0x0a, 0x10, 0x12, 0x09, 0x04, 0x0b, 0xff, 0xff, 0x01, 0x00, + 0x0a, 0x11, 0x12, 0x0a, 0x04, 0x0b, 0xff, 0xff, 0x0a, 0x02, 0x00, 0x0a, + 0x12, 0x12, 0x0a, 0x04, 0x0b, 0xff, 0xff, 0x0a, 0x03, 0x00, 0x0a, 0x13, + 0x5b, 0x82, 0x42, 0x04, 0x52, 0x50, 0x30, 0x35, 0x08, 0x5f, 0x41, 0x44, + 0x52, 0x0c, 0x00, 0x00, 0x1c, 0x00, 0x08, 0x5f, 0x50, 0x52, 0x54, 0x12, + 0x2c, 0x04, 0x12, 0x09, 0x04, 0x0b, 0xff, 0xff, 0x00, 0x00, 0x0a, 0x10, + 0x12, 0x09, 0x04, 0x0b, 0xff, 0xff, 0x01, 0x00, 0x0a, 0x11, 0x12, 0x0a, + 0x04, 0x0b, 0xff, 0xff, 0x0a, 0x02, 0x00, 0x0a, 0x12, 0x12, 0x0a, 0x04, + 0x0b, 0xff, 0xff, 0x0a, 0x03, 0x00, 0x0a, 0x13, 0x5b, 0x82, 0x42, 0x04, + 0x52, 0x50, 0x30, 0x39, 0x08, 0x5f, 0x41, 0x44, 0x52, 0x0c, 0x00, 0x00, + 0x1d, 0x00, 0x08, 0x5f, 0x50, 0x52, 0x54, 0x12, 0x2c, 0x04, 0x12, 0x09, + 0x04, 0x0b, 0xff, 0xff, 0x00, 0x00, 0x0a, 0x10, 0x12, 0x09, 0x04, 0x0b, + 0xff, 0xff, 0x01, 0x00, 0x0a, 0x11, 0x12, 0x0a, 0x04, 0x0b, 0xff, 0xff, + 0x0a, 0x02, 0x00, 0x0a, 0x12, 0x12, 0x0a, 0x04, 0x0b, 0xff, 0xff, 0x0a, + 0x03, 0x00, 0x0a, 0x13, 0x5b, 0x82, 0x42, 0x04, 0x52, 0x50, 0x31, 0x30, + 0x08, 0x5f, 0x41, 0x44, 0x52, 0x0c, 0x01, 0x00, 0x1d, 0x00, 0x08, 0x5f, + 0x50, 0x52, 0x54, 0x12, 0x2c, 0x04, 0x12, 0x09, 0x04, 0x0b, 0xff, 0xff, + 0x00, 0x00, 0x0a, 0x11, 0x12, 0x09, 0x04, 0x0b, 0xff, 0xff, 0x01, 0x00, + 0x0a, 0x12, 0x12, 0x0a, 0x04, 0x0b, 0xff, 0xff, 0x0a, 0x02, 0x00, 0x0a, + 0x13, 0x12, 0x0a, 0x04, 0x0b, 0xff, 0xff, 0x0a, 0x03, 0x00, 0x0a, 0x10, + 0x5b, 0x82, 0x49, 0x25, 0x49, 0x32, 0x43, 0x30, 0x08, 0x5f, 0x41, 0x44, + 0x52, 0x0c, 0x00, 0x00, 0x15, 0x00, 0x5b, 0x82, 0x43, 0x0c, 0x45, 0x45, + 0x50, 0x32, 0x08, 0x5f, 0x48, 0x49, 0x44, 0x0d, 0x31, 0x35, 0x41, 0x43, + 0x30, 0x30, 0x30, 0x32, 0x00, 0x08, 0x5f, 0x43, 0x49, 0x44, 0x0d, 0x50, + 0x52, 0x50, 0x30, 0x30, 0x30, 0x31, 0x00, 0x08, 0x5f, 0x44, 0x44, 0x4e, + 0x0d, 0x4e, 0x41, 0x49, 0x20, 0x4d, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x62, + 0x6f, 0x61, 0x72, 0x64, 0x20, 0x45, 0x45, 0x50, 0x52, 0x4f, 0x4d, 0x00, + 0x08, 0x5f, 0x43, 0x52, 0x53, 0x11, 0x26, 0x0a, 0x23, 0x8e, 0x1e, 0x00, + 0x02, 0x00, 0x01, 0x02, 0x00, 0x00, 0x01, 0x06, 0x00, 0xa0, 0x86, 0x01, + 0x00, 0x56, 0x00, 0x5c, 0x5f, 0x53, 0x42, 0x2e, 0x50, 0x43, 0x49, 0x30, + 0x2e, 0x49, 0x32, 0x43, 0x30, 0x00, 0x79, 0x00, 0x08, 0x5f, 0x44, 0x53, + 0x44, 0x12, 0x41, 0x05, 0x02, 0x11, 0x13, 0x0a, 0x10, 0x14, 0xd8, 0xff, + 0xda, 0xba, 0x6e, 0x8c, 0x4d, 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, + 0x01, 0x12, 0x39, 0x03, 0x12, 0x1b, 0x02, 0x0d, 0x63, 0x6f, 0x6d, 0x70, + 0x61, 0x74, 0x69, 0x62, 0x6c, 0x65, 0x00, 0x0d, 0x61, 0x74, 0x6d, 0x65, + 0x6c, 0x2c, 0x32, 0x34, 0x63, 0x30, 0x32, 0x00, 0x12, 0x0b, 0x02, 0x0d, + 0x73, 0x69, 0x7a, 0x65, 0x00, 0x0b, 0x00, 0x01, 0x12, 0x0e, 0x02, 0x0d, + 0x70, 0x61, 0x67, 0x65, 0x73, 0x69, 0x7a, 0x65, 0x00, 0x0a, 0x08, 0x5b, + 0x82, 0x46, 0x0d, 0x45, 0x45, 0x50, 0x33, 0x08, 0x5f, 0x48, 0x49, 0x44, + 0x0d, 0x31, 0x35, 0x41, 0x43, 0x30, 0x30, 0x30, 0x33, 0x00, 0x08, 0x5f, + 0x43, 0x49, 0x44, 0x0d, 0x50, 0x52, 0x50, 0x30, 0x30, 0x30, 0x31, 0x00, + 0x08, 0x5f, 0x44, 0x44, 0x4e, 0x0d, 0x4e, 0x41, 0x49, 0x20, 0x4d, 0x6f, + 0x74, 0x68, 0x65, 0x72, 0x62, 0x6f, 0x61, 0x72, 0x64, 0x20, 0x45, 0x45, + 0x50, 0x52, 0x4f, 0x4d, 0x20, 0x53, 0x4e, 0x00, 0x08, 0x5f, 0x43, 0x52, 0x53, 0x11, 0x26, 0x0a, 0x23, 0x8e, 0x1e, 0x00, 0x02, 0x00, 0x01, 0x02, - 0x00, 0x00, 0x01, 0x06, 0x00, 0xa0, 0x86, 0x01, 0x00, 0x68, 0x00, 0x5c, + 0x00, 0x00, 0x01, 0x06, 0x00, 0xa0, 0x86, 0x01, 0x00, 0x5e, 0x00, 0x5c, 0x5f, 0x53, 0x42, 0x2e, 0x50, 0x43, 0x49, 0x30, 0x2e, 0x49, 0x32, 0x43, - 0x30, 0x00, 0x79, 0x00, 0x08, 0x5f, 0x44, 0x53, 0x44, 0x12, 0x37, 0x02, - 0x11, 0x13, 0x0a, 0x10, 0x14, 0xd8, 0xff, 0xda, 0xba, 0x6e, 0x8c, 0x4d, - 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01, 0x12, 0x20, 0x01, 0x12, - 0x1d, 0x02, 0x0d, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x69, 0x62, 0x6c, - 0x65, 0x00, 0x0d, 0x64, 0x61, 0x6c, 0x6c, 0x61, 0x73, 0x2c, 0x64, 0x73, - 0x31, 0x33, 0x37, 0x34, 0x00, 0x5b, 0x82, 0x4e, 0x0a, 0x53, 0x50, 0x49, - 0x30, 0x08, 0x5f, 0x41, 0x44, 0x52, 0x0c, 0x02, 0x00, 0x1e, 0x00, 0x5b, - 0x82, 0x4c, 0x09, 0x50, 0x52, 0x43, 0x30, 0x08, 0x5f, 0x48, 0x49, 0x44, - 0x0d, 0x31, 0x35, 0x41, 0x43, 0x30, 0x30, 0x30, 0x31, 0x00, 0x08, 0x5f, - 0x53, 0x54, 0x52, 0x11, 0x42, 0x05, 0x0a, 0x4e, 0x4e, 0x00, 0x41, 0x00, - 0x49, 0x00, 0x20, 0x00, 0x47, 0x00, 0x53, 0x00, 0x50, 0x00, 0x49, 0x00, - 0x20, 0x00, 0x50, 0x00, 0x72, 0x00, 0x6f, 0x00, 0x63, 0x00, 0x65, 0x00, - 0x73, 0x00, 0x73, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x20, 0x00, 0x52, 0x00, - 0x65, 0x00, 0x67, 0x00, 0x69, 0x00, 0x73, 0x00, 0x74, 0x00, 0x65, 0x00, - 0x72, 0x00, 0x20, 0x00, 0x43, 0x00, 0x6f, 0x00, 0x6e, 0x00, 0x74, 0x00, - 0x72, 0x00, 0x6f, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x65, 0x00, 0x72, 0x00, - 0x00, 0x00, 0x08, 0x5f, 0x43, 0x52, 0x53, 0x11, 0x29, 0x0a, 0x26, 0x8e, - 0x21, 0x00, 0x02, 0x00, 0x02, 0x02, 0x00, 0x00, 0x01, 0x09, 0x00, 0x20, - 0xa1, 0x07, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x5c, 0x5f, 0x53, 0x42, - 0x2e, 0x50, 0x43, 0x49, 0x30, 0x2e, 0x53, 0x50, 0x49, 0x30, 0x00, 0x79, - 0x00 + 0x30, 0x00, 0x79, 0x00, 0x08, 0x5f, 0x44, 0x53, 0x44, 0x12, 0x41, 0x06, + 0x02, 0x11, 0x13, 0x0a, 0x10, 0x14, 0xd8, 0xff, 0xda, 0xba, 0x6e, 0x8c, + 0x4d, 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01, 0x12, 0x49, 0x04, + 0x04, 0x12, 0x1c, 0x02, 0x0d, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x69, + 0x62, 0x6c, 0x65, 0x00, 0x0d, 0x61, 0x74, 0x6d, 0x65, 0x6c, 0x2c, 0x32, + 0x34, 0x63, 0x73, 0x30, 0x32, 0x00, 0x12, 0x0a, 0x02, 0x0d, 0x73, 0x69, + 0x7a, 0x65, 0x00, 0x0a, 0x10, 0x12, 0x0e, 0x02, 0x0d, 0x70, 0x61, 0x67, + 0x65, 0x73, 0x69, 0x7a, 0x65, 0x00, 0x0a, 0x08, 0x12, 0x0e, 0x02, 0x0d, + 0x72, 0x65, 0x61, 0x64, 0x2d, 0x6f, 0x6e, 0x6c, 0x79, 0x00, 0x01, 0x5b, + 0x82, 0x4a, 0x0a, 0x52, 0x54, 0x43, 0x31, 0x08, 0x5f, 0x48, 0x49, 0x44, + 0x0d, 0x31, 0x35, 0x41, 0x43, 0x30, 0x30, 0x30, 0x34, 0x00, 0x08, 0x5f, + 0x43, 0x49, 0x44, 0x0d, 0x50, 0x52, 0x50, 0x30, 0x30, 0x30, 0x31, 0x00, + 0x08, 0x5f, 0x44, 0x44, 0x4e, 0x0d, 0x4e, 0x41, 0x49, 0x20, 0x4d, 0x6f, + 0x74, 0x68, 0x65, 0x72, 0x62, 0x6f, 0x61, 0x72, 0x64, 0x20, 0x57, 0x44, + 0x54, 0x20, 0x52, 0x54, 0x43, 0x00, 0x08, 0x5f, 0x43, 0x52, 0x53, 0x11, + 0x26, 0x0a, 0x23, 0x8e, 0x1e, 0x00, 0x02, 0x00, 0x01, 0x02, 0x00, 0x00, + 0x01, 0x06, 0x00, 0xa0, 0x86, 0x01, 0x00, 0x68, 0x00, 0x5c, 0x5f, 0x53, + 0x42, 0x2e, 0x50, 0x43, 0x49, 0x30, 0x2e, 0x49, 0x32, 0x43, 0x30, 0x00, + 0x79, 0x00, 0x08, 0x5f, 0x44, 0x53, 0x44, 0x12, 0x37, 0x02, 0x11, 0x13, + 0x0a, 0x10, 0x14, 0xd8, 0xff, 0xda, 0xba, 0x6e, 0x8c, 0x4d, 0x8a, 0x91, + 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01, 0x12, 0x20, 0x01, 0x12, 0x1d, 0x02, + 0x0d, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x69, 0x62, 0x6c, 0x65, 0x00, + 0x0d, 0x64, 0x61, 0x6c, 0x6c, 0x61, 0x73, 0x2c, 0x64, 0x73, 0x31, 0x33, + 0x37, 0x34, 0x00, 0x5b, 0x82, 0x4e, 0x0a, 0x53, 0x50, 0x49, 0x30, 0x08, + 0x5f, 0x41, 0x44, 0x52, 0x0c, 0x02, 0x00, 0x1e, 0x00, 0x5b, 0x82, 0x4c, + 0x09, 0x50, 0x52, 0x43, 0x30, 0x08, 0x5f, 0x48, 0x49, 0x44, 0x0d, 0x31, + 0x35, 0x41, 0x43, 0x30, 0x30, 0x30, 0x31, 0x00, 0x08, 0x5f, 0x53, 0x54, + 0x52, 0x11, 0x42, 0x05, 0x0a, 0x4e, 0x4e, 0x00, 0x41, 0x00, 0x49, 0x00, + 0x20, 0x00, 0x47, 0x00, 0x53, 0x00, 0x50, 0x00, 0x49, 0x00, 0x20, 0x00, + 0x50, 0x00, 0x72, 0x00, 0x6f, 0x00, 0x63, 0x00, 0x65, 0x00, 0x73, 0x00, + 0x73, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x20, 0x00, 0x52, 0x00, 0x65, 0x00, + 0x67, 0x00, 0x69, 0x00, 0x73, 0x00, 0x74, 0x00, 0x65, 0x00, 0x72, 0x00, + 0x20, 0x00, 0x43, 0x00, 0x6f, 0x00, 0x6e, 0x00, 0x74, 0x00, 0x72, 0x00, + 0x6f, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x65, 0x00, 0x72, 0x00, 0x00, 0x00, + 0x08, 0x5f, 0x43, 0x52, 0x53, 0x11, 0x29, 0x0a, 0x26, 0x8e, 0x21, 0x00, + 0x02, 0x00, 0x02, 0x02, 0x00, 0x00, 0x01, 0x09, 0x00, 0x20, 0xa1, 0x07, + 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x5c, 0x5f, 0x53, 0x42, 0x2e, 0x50, + 0x43, 0x49, 0x30, 0x2e, 0x53, 0x50, 0x49, 0x30, 0x00, 0x79, 0x00, 0x5b, + 0x82, 0x44, 0x08, 0x54, 0x50, 0x4d, 0x5f, 0x08, 0x5f, 0x48, 0x49, 0x44, + 0x0d, 0x4d, 0x53, 0x46, 0x54, 0x30, 0x31, 0x30, 0x31, 0x00, 0x08, 0x5f, + 0x53, 0x54, 0x52, 0x11, 0x21, 0x0a, 0x1e, 0x54, 0x00, 0x50, 0x00, 0x4d, + 0x00, 0x20, 0x00, 0x32, 0x00, 0x2e, 0x00, 0x30, 0x00, 0x20, 0x00, 0x44, + 0x00, 0x65, 0x00, 0x76, 0x00, 0x69, 0x00, 0x63, 0x00, 0x65, 0x00, 0x00, + 0x00, 0x5b, 0x80, 0x54, 0x50, 0x4d, 0x52, 0x00, 0x0c, 0x00, 0x00, 0xd4, + 0xfe, 0x0b, 0x00, 0x50, 0x5b, 0x81, 0x0b, 0x54, 0x50, 0x4d, 0x52, 0x00, + 0x41, 0x43, 0x43, 0x30, 0x08, 0x08, 0x5f, 0x43, 0x52, 0x53, 0x11, 0x11, + 0x0a, 0x0e, 0x86, 0x09, 0x00, 0x01, 0x00, 0x00, 0xd4, 0xfe, 0x00, 0x50, + 0x00, 0x00, 0x79, 0x00, 0x14, 0x14, 0x5f, 0x53, 0x54, 0x41, 0x00, 0xa0, + 0x0a, 0x93, 0x41, 0x43, 0x43, 0x30, 0x0a, 0xff, 0xa4, 0x00, 0xa4, 0x0a, + 0x0f }; -static const unsigned int acpi_dsdt_aml_len = 1321; +static const unsigned int acpi_dsdt_aml_len = 1813; diff --git a/include/x86/common.h b/include/x86/common.h index 8df5d87c94..6589df2e61 100644 --- a/include/x86/common.h +++ b/include/x86/common.h @@ -70,6 +70,9 @@ __attribute__((noreturn)) void panic(void); void cpuid(uint32_t eax_param, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx); int cpuid_is_1gb_page_supported(void); +int x86_cpu_apic_ids(uint8_t *ids, int max); +int x86_apic_ids_from_topology(uint32_t core_thread_count, uint32_t shift, + uint8_t *ids, int max); void switch_to_long_mode(uint64_t *entry, uint32_t page_table); void x86_log_memory_load(uint32_t start, uint32_t end, const char *name); void hlt(void); diff --git a/include/x86/fsp_config.h b/include/x86/fsp_config.h index 1994c04d48..a579bb976d 100644 --- a/include/x86/fsp_config.h +++ b/include/x86/fsp_config.h @@ -6,9 +6,9 @@ #ifndef FSP_HYPERTHREADING #define FSP_HYPERTHREADING 0 #endif -#if FSP_HYPERTHREADING -/* The MADT and MP table list four single-thread cores (APIC IDs 0/2/4/6). */ -#error "FSP_HYPERTHREADING=1 needs sibling threads in the MADT and MP table" +#if FSP_HYPERTHREADING && !defined(TARGET_nai_68int6) +/* The Kontron MP table lists four single-thread cores (APIC IDs 0/2/4/6). */ +#error "FSP_HYPERTHREADING=1 needs sibling threads in the MP table" #endif #ifndef FSP_INTEL_SPEED_STEP diff --git a/include/x86/mptable.h b/include/x86/mptable.h index 29ad25e53c..81c236f7bf 100644 --- a/include/x86/mptable.h +++ b/include/x86/mptable.h @@ -155,7 +155,10 @@ enum mp_irq_source_types { #define MP_INTSRC_NUM_ENTRY 17 #define MP_LINTSRC_NUM_ENTRY 0 #define MP_BUS_NUM_ENTRY 2 - #define MP_CPU_NUM_ENTRY 4 + /* one per logical processor; unused entries are left disabled */ + #ifndef MP_CPU_NUM_ENTRY + #define MP_CPU_NUM_ENTRY 4 + #endif #endif struct mptable { diff --git a/options.mk b/options.mk index 4d6b152a38..506a2ab0e9 100644 --- a/options.mk +++ b/options.mk @@ -70,7 +70,7 @@ ifeq ($(TARGET),nai_68int6) $(error NAI_68INT6_PCIE_LANES must be one of: $(NAI_68INT6_PCIE_LANE_CONFIGS)) endif CFLAGS+=-DNAI_68INT6_PCIE_LANES_$(NAI_68INT6_PCIE_LANES) - $(foreach option,NAI_68INT6_PCIE_NT NAI_68INT6_BACKPLANE_CLK \ + $(foreach option,FSP_HYPERTHREADING NAI_68INT6_PCIE_NT NAI_68INT6_BACKPLANE_CLK \ NAI_68INT6_PCIE_LINK_DELAY_S NAI_68INT6_BOOT_DELAY_S \ NAI_68INT6_FPGA_READY_DELAY_S, \ $(if $($(option)),$(eval CFLAGS+=-D$(option)=$($(option))))) @@ -2081,7 +2081,9 @@ ifeq ($(FSP), 1) PCH_PCR_BASE \ PCI_ECAM_BASE \ WOLFBOOT_LOAD_BASE \ - FSP_S_LOAD_BASE + FSP_S_LOAD_BASE \ + MP_CPU_NUM_ENTRY \ + WOLFBOOT_CMDLINE_MAX # set CFLAGS defines for each x86_fsp option $(foreach option,$(X86_FSP_OPTIONS),$(if $($(option)), $(eval CFLAGS += -D$(option)=$($(option))))) diff --git a/src/boot_x86_fsp.c b/src/boot_x86_fsp.c index 8eea8c5098..79c2ec23fb 100644 --- a/src/boot_x86_fsp.c +++ b/src/boot_x86_fsp.c @@ -45,6 +45,9 @@ #ifdef WOLFBOOT_FSP_NVS_CACHE #include #endif +#ifdef WOLFBOOT_MEASURED_BOOT +#include +#endif #define WOLFBOOT_X86_STACK_SIZE 0x10000 @@ -290,10 +293,12 @@ int wolfBoot_image_measure(uint8_t *image) } ret = wc_InitSha256(&sha); - if (ret == 0) - ret = wc_Sha256Update(&sha, img.sha_hash, WOLFBOOT_SHA_DIGEST_SIZE); + if (ret != 0) + return -1; + ret = wc_Sha256Update(&sha, img.sha_hash, WOLFBOOT_SHA_DIGEST_SIZE); if (ret == 0) ret = wc_Sha256Final(&sha, digest); + wc_Sha256Free(&sha); if (ret != 0) return -1; diff --git a/src/update_disk.c b/src/update_disk.c index be21989017..db29f41150 100644 --- a/src/update_disk.c +++ b/src/update_disk.c @@ -766,6 +766,41 @@ int wolfBoot_disk_dts_fixup(fdt_ctx* ctx) } #endif +#ifdef WOLFBOOT_FSP +#ifndef WOLFBOOT_CMDLINE_MAX +#define WOLFBOOT_CMDLINE_MAX 256 +#endif + +/* The image's signed kernel command line, as a stage 2 parameter address, or + * 0 when there is none */ +static uint32_t disk_signed_cmdline(const uint8_t *hdr) +{ + static char disk_cmdline[WOLFBOOT_CMDLINE_MAX]; + uint8_t *val = NULL, *digest = NULL; + uint16_t len; + + len = wolfBoot_find_header((uint8_t *)hdr + IMAGE_HEADER_OFFSET, + HDR_CMDLINE, &val); + if (len == 0 || val == NULL) + return 0; + /* The signature covers the header only up to the digest TLV */ + if (wolfBoot_find_header((uint8_t *)hdr + IMAGE_HEADER_OFFSET, + WOLFBOOT_SHA_HDR, &digest) == 0 || + val + len > digest - 2 * sizeof(uint16_t)) { + wolfBoot_printf("Ignoring a cmdline outside the signed header\r\n"); + return 0; + } + if (len >= sizeof(disk_cmdline)) { + wolfBoot_printf("Signed cmdline too long (%d), using built-in\r\n", + (int)len); + return 0; + } + memcpy(disk_cmdline, val, len); + disk_cmdline[len] = '\0'; + return (uint32_t)(uintptr_t)disk_cmdline; +} +#endif + void RAMFUNCTION wolfBoot_start(void) { uint8_t p_hdr[IMAGE_HEADER_SIZE] XALIGNED_STACK(16); @@ -1593,20 +1628,7 @@ void RAMFUNCTION wolfBoot_start(void) #ifdef WOLFBOOT_FSP /* The image's signed kernel command line, read before the decrypted * header is wiped */ - stage2_params->cmdline = 0; - { - static char disk_cmdline[256]; - uint8_t *val = NULL; - uint16_t len; - - len = wolfBoot_find_header((uint8_t *)hdr_ptr + IMAGE_HEADER_OFFSET, - HDR_CMDLINE, &val); - if (len > 0 && val != NULL && len < sizeof(disk_cmdline)) { - memcpy(disk_cmdline, val, len); - disk_cmdline[len] = '\0'; - stage2_params->cmdline = (uint32_t)(uintptr_t)disk_cmdline; - } - } + stage2_params->cmdline = disk_signed_cmdline(hdr_ptr); #endif #ifdef DISK_ENCRYPT disk_decrypted_header_clear(dec_hdr); diff --git a/src/x86/acpi.c b/src/x86/acpi.c index 8fa4129694..9a2471a72d 100644 --- a/src/x86/acpi.c +++ b/src/x86/acpi.c @@ -21,23 +21,26 @@ * * Minimal ACPI table generation for the x86 FSP (Tiger Lake) target. The FSP * does silicon init only and returns no usable ACPI table set, so wolfBoot - * builds RSDP -> XSDT -> { FADT, MADT, MCFG, DSDT } in low reserved memory and - * points boot_params.acpi_rsdp_addr at the RSDP. The MADT interrupt model and - * the DSDT _PRT routing are the values measured on this board; see docs and - * acpi_dsdt.asl. Only the DSDT needs AML, embedded here as a precompiled blob. + * builds RSDP -> XSDT -> { FADT, MADT, MCFG, TPM2, HPET, DMAR, DSDT } in low + * reserved memory and points boot_params.acpi_rsdp_addr at the RSDP. The MADT + * interrupt model and the DSDT _PRT routing are the values measured on this + * board; see docs and acpi_dsdt.asl. Only the DSDT needs AML, embedded here + * as a precompiled blob. */ #include #include #include +#include +#include #include /* Local APIC and IOAPIC as the MP table already encodes them (mptable.c). */ #define ACPI_LAPIC_ADDR 0xFEE00000UL #define ACPI_IOAPIC_ADDR 0xFEC00000UL #define ACPI_IOAPIC_ID 0x02 -#define ACPI_CPU_COUNT 4 +#define ACPI_CPU_MAX 8 /* ECAM base and bus range, matching MCFG to config PCI_ECAM_BASE. */ #ifndef PCI_ECAM_BASE @@ -154,6 +157,71 @@ struct acpi_mcfg { struct acpi_mcfg_alloc alloc; } __attribute__((packed)); +/* TPM2 table for the TPM's TIS (FIFO) interface at 0xFED40000 */ +#define ACPI_TPM2_START_TIS 6 +struct acpi_tpm2 { + struct acpi_header header; + uint16_t platform_class; + uint16_t reserved; + uint64_t control_address; + uint32_t start_method; + uint8_t start_params[12]; +} __attribute__((packed)); + +#define ACPI_HPET_BASE 0xFED00000UL +struct acpi_hpet { + struct acpi_header header; + uint32_t block_id; + struct acpi_gas base; + uint8_t number; + uint16_t min_tick; + uint8_t page_protection; +} __attribute__((packed)); + +/* DMAR: the two Tiger Lake VT-d units at the addresses FSP-M is given, as + * the stock BIOS describes them */ +#define DMAR_GFX_UNIT 0xFED90000UL +#define DMAR_DEFAULT_UNIT 0xFED91000UL +#define DMAR_HAW 38 /* host address width - 1 */ +#define DMAR_FLAGS 0x05 /* INTR_REMAP | DMA_CTRL_PLATFORM_OPT_IN */ +#define DMAR_DRHD 0 +#define DMAR_RMRR 1 +#define DMAR_INCLUDE_PCI_ALL 0x01 +#define DMAR_SCOPE_ENDPOINT 1 +#define DMAR_SCOPE_IOAPIC 3 +#define DMAR_SCOPE_HPET 4 +struct acpi_dmar { + struct acpi_header header; + uint8_t host_address_width; + uint8_t flags; + uint8_t reserved[10]; +} __attribute__((packed)); +struct dmar_drhd { + uint16_t type; + uint16_t length; + uint8_t flags; + uint8_t reserved; + uint16_t segment; + uint64_t base; +} __attribute__((packed)); +struct dmar_rmrr { + uint16_t type; + uint16_t length; + uint16_t reserved; + uint16_t segment; + uint64_t base; + uint64_t limit; +} __attribute__((packed)); +struct dmar_scope { + uint8_t type; + uint8_t length; + uint16_t reserved; + uint8_t enumeration_id; + uint8_t start_bus; + uint8_t dev; + uint8_t fn; +} __attribute__((packed)); + /* HW-reduced ACPI has no FACS, so the FADT leaves firmware_ctrl zero. */ /* ACPI 6.x FADT (fixed layout to the hypervisor id, 276 bytes). */ @@ -216,12 +284,6 @@ struct acpi_fadt { uint64_t hypervisor_vendor_id; } __attribute__((packed)); -/* acpi_processor_id -> Local APIC id: 4 cores with HT off (APIC ids - * 0/2/4/6). Listing absent threads stalls the kernel per missing AP. */ -static const uint8_t acpi_lapic_ids[ACPI_CPU_COUNT] = { - 0, 2, 4, 6 -}; - static void acpi_set_checksum(struct acpi_header *h) { uint8_t *bytes = (uint8_t *)h; @@ -265,18 +327,27 @@ static uint8_t *acpi_build_madt(uint8_t *p) struct madt_ioapic *ioapic; struct madt_iso *iso; struct madt_lapic_nmi *nmi; - uint32_t i; + uint8_t apic_ids[ACPI_CPU_MAX]; + int cpus; + int i; madt->lapic_address = (uint32_t)ACPI_LAPIC_ADDR; madt->flags = MADT_FLAG_PCAT_COMPAT; - for (i = 0; i < ACPI_CPU_COUNT; i++) { + /* Only the processors that are present: an absent one stalls the kernel + * while it waits for that AP */ + cpus = x86_cpu_apic_ids(apic_ids, ACPI_CPU_MAX); + if (cpus == 0) { + apic_ids[0] = (uint8_t)(mmio_read32(ACPI_LAPIC_ADDR + 0x20) >> 24); + cpus = 1; + } + for (i = 0; i < cpus; i++) { lapic = (struct madt_lapic *)e; memset(lapic, 0, sizeof(*lapic)); lapic->type = MADT_TYPE_LAPIC; lapic->length = sizeof(*lapic); lapic->acpi_processor_id = (uint8_t)i; - lapic->apic_id = acpi_lapic_ids[i]; + lapic->apic_id = apic_ids[i]; lapic->flags = MADT_LAPIC_ENABLED; e += sizeof(*lapic); } @@ -341,6 +412,99 @@ static uint8_t *acpi_build_mcfg(uint8_t *p) return p + sizeof(*mcfg); } +static uint8_t *acpi_build_tpm2(uint8_t *p) +{ + struct acpi_tpm2 *tpm2 = (struct acpi_tpm2 *)p; + + memset(tpm2, 0, sizeof(*tpm2)); + tpm2->start_method = ACPI_TPM2_START_TIS; + acpi_fill_header(&tpm2->header, "TPM2", sizeof(*tpm2), 4); + acpi_set_checksum(&tpm2->header); + return p + sizeof(*tpm2); +} + +static uint8_t *acpi_build_hpet(uint8_t *p) +{ + struct acpi_hpet *hpet = (struct acpi_hpet *)p; + + memset(hpet, 0, sizeof(*hpet)); + /* GCAP_ID low dword: vendor, timer count and revision */ + hpet->block_id = mmio_read32(ACPI_HPET_BASE); + hpet->base.space_id = ACPI_GAS_SYSTEM_MEMORY; + hpet->base.bit_width = 64; + hpet->base.address = ACPI_HPET_BASE; + hpet->min_tick = 0x80; + acpi_fill_header(&hpet->header, "HPET", sizeof(*hpet), 1); + acpi_set_checksum(&hpet->header); + return p + sizeof(*hpet); +} + +static uint8_t *dmar_add_scope(uint8_t *e, uint8_t type, uint8_t id, + uint8_t dev, uint8_t fn) +{ + struct dmar_scope *s = (struct dmar_scope *)e; + + memset(s, 0, sizeof(*s)); + s->type = type; + s->length = sizeof(*s); + s->enumeration_id = id; + s->dev = dev; + s->fn = fn; + return e + sizeof(*s); +} + +static uint8_t *acpi_build_dmar(uint8_t *p) +{ + struct acpi_dmar *dmar = (struct acpi_dmar *)p; + uint8_t *e = p + sizeof(*dmar); + struct dmar_drhd *drhd; + struct dmar_rmrr *rmrr; + uint32_t bdsm, tolud; + int igd; + + memset(dmar, 0, sizeof(*dmar)); + dmar->host_address_width = DMAR_HAW; + dmar->flags = DMAR_FLAGS; + + igd = (pci_config_read32(0, 2, 0, 0) & 0xffff) != 0xffff; + if (igd) { + drhd = (struct dmar_drhd *)e; + memset(drhd, 0, sizeof(*drhd)); + drhd->type = DMAR_DRHD; + drhd->base = DMAR_GFX_UNIT; + e = dmar_add_scope(e + sizeof(*drhd), DMAR_SCOPE_ENDPOINT, 0, 2, 0); + drhd->length = (uint16_t)(e - (uint8_t *)drhd); + } + + drhd = (struct dmar_drhd *)e; + memset(drhd, 0, sizeof(*drhd)); + drhd->type = DMAR_DRHD; + drhd->flags = DMAR_INCLUDE_PCI_ALL; + drhd->base = DMAR_DEFAULT_UNIT; + e = dmar_add_scope(e + sizeof(*drhd), DMAR_SCOPE_IOAPIC, ACPI_IOAPIC_ID, + 0x1e, 7); + e = dmar_add_scope(e, DMAR_SCOPE_HPET, 0, 0x1e, 6); + drhd->length = (uint16_t)(e - (uint8_t *)drhd); + + /* Graphics stolen memory (BDSM, 00:02.0) up to TOLUD (00:00.0), which the + * IGD keeps using through DMA remapping */ + bdsm = pci_config_read32(0, 2, 0, 0x5c) & 0xfff00000U; + tolud = pci_config_read32(0, 0, 0, 0xbc) & 0xfff00000U; + if (igd && bdsm != 0 && tolud > bdsm) { + rmrr = (struct dmar_rmrr *)e; + memset(rmrr, 0, sizeof(*rmrr)); + rmrr->type = DMAR_RMRR; + rmrr->base = bdsm; + rmrr->limit = (uint64_t)tolud - 1; + e = dmar_add_scope(e + sizeof(*rmrr), DMAR_SCOPE_ENDPOINT, 0, 2, 0); + rmrr->length = (uint16_t)(e - (uint8_t *)rmrr); + } + + acpi_fill_header(&dmar->header, "DMAR", (uint32_t)(e - p), 2); + acpi_set_checksum(&dmar->header); + return e; +} + static void acpi_gas_io(struct acpi_gas *g, uint8_t width, uint64_t addr) { g->space_id = ACPI_GAS_SYSTEM_IO; @@ -381,6 +545,9 @@ uint64_t acpi_setup(void) uint8_t *fadt; uint8_t *madt; uint8_t *mcfg; + uint8_t *tpm2; + uint8_t *hpet; + uint8_t *dmar; struct acpi_header *xsdt; struct acpi_rsdp *rsdp; uint64_t *xsdt_entries; @@ -402,14 +569,26 @@ uint64_t acpi_setup(void) mcfg = p; p = acpi_align(acpi_build_mcfg(p)); + tpm2 = p; + p = acpi_align(acpi_build_tpm2(p)); + + hpet = p; + p = acpi_align(acpi_build_hpet(p)); + + dmar = p; + p = acpi_align(acpi_build_dmar(p)); + /* XSDT lists the top-level tables; the DSDT is reached via the FADT. */ xsdt = (struct acpi_header *)p; xsdt_entries = (uint64_t *)((uint8_t *)xsdt + sizeof(*xsdt)); xsdt_entries[0] = (uint64_t)(uintptr_t)fadt; xsdt_entries[1] = (uint64_t)(uintptr_t)madt; xsdt_entries[2] = (uint64_t)(uintptr_t)mcfg; + xsdt_entries[3] = (uint64_t)(uintptr_t)tpm2; + xsdt_entries[4] = (uint64_t)(uintptr_t)hpet; + xsdt_entries[5] = (uint64_t)(uintptr_t)dmar; acpi_fill_header(xsdt, "XSDT", - (uint32_t)(sizeof(*xsdt) + 3 * sizeof(uint64_t)), 1); + (uint32_t)(sizeof(*xsdt) + 6 * sizeof(uint64_t)), 1); acpi_set_checksum(xsdt); p = acpi_align((uint8_t *)xsdt + xsdt->length); diff --git a/src/x86/acpi_dsdt.asl b/src/x86/acpi_dsdt.asl index 6242953d9f..5d85c29b48 100644 --- a/src/x86/acpi_dsdt.asl +++ b/src/x86/acpi_dsdt.asl @@ -30,6 +30,7 @@ DefinitionBlock ("", "DSDT", 2, "WOLFBT", "NAITGL", 0x00000001) { Package () { 0x0002FFFF, 0, Zero, 16 }, /* iGPU INTA */ Package () { 0x0004FFFF, 0, Zero, 16 }, /* DTT INTA */ + Package () { 0x0006FFFF, 0, Zero, 16 }, /* PEG60 INTA */ Package () { 0x000DFFFF, 0, Zero, 16 }, /* TBT INTA */ Package () { 0x0014FFFF, 0, Zero, 16 }, /* xHCI INTA */ Package () { 0x0014FFFF, 1, Zero, 17 }, /* xHCI INTB */ @@ -54,6 +55,27 @@ DefinitionBlock ("", "DSDT", 2, "WOLFBT", "NAITGL", 0x00000001) }) } + /* The OS gets every PCIe feature it asks for (native hot-plug, + * PME, AER, capability structure, LTR): no firmware handler owns + * them after handoff */ + Method (_OSC, 4, Serialized) + { + Local0 = Arg3 + CreateDWordField (Local0, Zero, CDW1) + If ((Arg0 == ToUUID ("33db4d5b-1ff7-401c-9657-7441c03dd766"))) + { + If ((Arg1 != One)) + { + CDW1 |= 0x08 + } + } + Else + { + CDW1 |= 0x04 + } + Return (Local0) + } + Name (_CRS, ResourceTemplate () { /* Bus range covered by the MCFG (bus 0..0x17). */ @@ -86,6 +108,56 @@ DefinitionBlock ("", "DSDT", 2, "WOLFBT", "NAITGL", 0x00000001) 0x0000000000000000, 0x0000004000000000) }) + /* Root ports with the INTx routing of the devices behind them, as + * the stock BIOS declares it; RP10's differs from a plain swizzle. */ + Device (PEG0) /* 00:06.0 */ + { + Name (_ADR, 0x00060000) + Name (_PRT, Package () + { + Package () { 0xFFFF, 0, Zero, 16 }, + Package () { 0xFFFF, 1, Zero, 17 }, + Package () { 0xFFFF, 2, Zero, 18 }, + Package () { 0xFFFF, 3, Zero, 19 }, + }) + } + + Device (RP05) /* 00:1c.0, PCH port 5 */ + { + Name (_ADR, 0x001C0000) + Name (_PRT, Package () + { + Package () { 0xFFFF, 0, Zero, 16 }, + Package () { 0xFFFF, 1, Zero, 17 }, + Package () { 0xFFFF, 2, Zero, 18 }, + Package () { 0xFFFF, 3, Zero, 19 }, + }) + } + + Device (RP09) /* 00:1d.0, PCH port 9 */ + { + Name (_ADR, 0x001D0000) + Name (_PRT, Package () + { + Package () { 0xFFFF, 0, Zero, 16 }, + Package () { 0xFFFF, 1, Zero, 17 }, + Package () { 0xFFFF, 2, Zero, 18 }, + Package () { 0xFFFF, 3, Zero, 19 }, + }) + } + + Device (RP10) /* 00:1d.1, PCH port 10 */ + { + Name (_ADR, 0x001D0001) + Name (_PRT, Package () + { + Package () { 0xFFFF, 0, Zero, 17 }, + Package () { 0xFFFF, 1, Zero, 18 }, + Package () { 0xFFFF, 2, Zero, 19 }, + Package () { 0xFFFF, 3, Zero, 16 }, + }) + } + /* LPSS I2C0 and GSPI0 with the board devices the stock BIOS * declares, so NAI's Linux drivers (naigspi, at24) bind. */ Device (I2C0) @@ -175,5 +247,30 @@ DefinitionBlock ("", "DSDT", 2, "WOLFBT", "NAITGL", 0x00000001) } } } + + /* TPM 2.0 on its TIS (FIFO) interface; wolfBoot leaves its interrupt + * unconfigured, so the OS driver polls */ + Device (TPM) + { + Name (_HID, "MSFT0101") + Name (_STR, Unicode ("TPM 2.0 Device")) + OperationRegion (TPMR, SystemMemory, 0xFED40000, 0x5000) + Field (TPMR, AnyAcc, NoLock, Preserve) + { + ACC0, 8 + } + Name (_CRS, ResourceTemplate () + { + Memory32Fixed (ReadWrite, 0xFED40000, 0x00005000) + }) + Method (_STA, 0, NotSerialized) + { + If ((ACC0 == 0xFF)) + { + Return (Zero) + } + Return (0x0F) + } + } } } diff --git a/src/x86/common.c b/src/x86/common.c index 38e07c5522..f1014799c5 100644 --- a/src/x86/common.c +++ b/src/x86/common.c @@ -392,6 +392,75 @@ void cpuid(uint32_t eax_param, *edx = _edx; } +static void cpuid_subleaf(uint32_t leaf, uint32_t subleaf, uint32_t *eax, + uint32_t *ebx) +{ + uint32_t a, b, c, d; + + __asm__ __volatile__ ("cpuid" + : "=a"(a), "=b"(b), "=c"(c), "=d"(d) + : "a"(leaf), "c"(subleaf)); + (void)c; + (void)d; + *eax = a; + *ebx = b; +} + +#define MSR_CORE_THREAD_COUNT 0x35 + +/** + * @brief List the APIC IDs of the enabled logical processors. + * + * MSR_CORE_THREAD_COUNT gives the enabled cores and threads (CPUID leaf 0xB + * still counts the second thread when Hyper-Threading is off), and leaf 0xB + * the APIC ID layout: thread t of core c has APIC ID (c << SMT shift) | t, + * with enabled cores taken to be numbered from 0. The primary threads come + * first, then their siblings, as a BIOS orders the MADT. + * + * @param ids Output array of APIC IDs. + * @param max Capacity of ids. + * @return The number of IDs written, or 0 if the counts are unusable. + */ +int x86_cpu_apic_ids(uint8_t *ids, int max) +{ + uint32_t eax, ebx, lo, hi; + + __asm__ __volatile__ ("rdmsr" : "=a"(lo), "=d"(hi) + : "c"(MSR_CORE_THREAD_COUNT)); + (void)hi; + cpuid_subleaf(0xb, 0, &eax, &ebx); + (void)ebx; + return x86_apic_ids_from_topology(lo, eax & 0x1f, ids, max); +} + +/** + * @brief APIC IDs from MSR_CORE_THREAD_COUNT and the SMT shift. + * + * @param core_thread_count MSR_CORE_THREAD_COUNT: threads in bits 15:0, + * cores in bits 31:16. + * @param shift APIC ID bits taken by the thread number (CPUID 0xB level 0). + * @param ids Output array of APIC IDs. + * @param max Capacity of ids. + * @return The number of IDs written, or 0 if the counts are unusable. + */ +int x86_apic_ids_from_topology(uint32_t core_thread_count, uint32_t shift, + uint8_t *ids, int max) +{ + uint32_t threads, logical, cores, t, c; + int n = 0; + + logical = core_thread_count & 0xffff; + cores = (core_thread_count >> 16) & 0xffff; + if (cores == 0 || logical < cores) + return 0; + threads = logical / cores; + for (t = 0; t < threads; t++) { + for (c = 0; c < cores && n < max; c++) + ids[n++] = (uint8_t)((c << shift) | t); + } + return n; +} + /** * @brief Check if 1GB page is supported by the CPU. * diff --git a/src/x86/mptable.c b/src/x86/mptable.c index 2d1f294724..7179c4bd86 100644 --- a/src/x86/mptable.c +++ b/src/x86/mptable.c @@ -494,6 +494,12 @@ void mptable_setup(void) uint32_t apic_id, apic_ver; uint32_t cpupid_sign; struct mptable *_mp; +#if defined(TARGET_nai_68int6) + uint8_t apic_ids[MP_CPU_NUM_ENTRY]; + int i, j = 1, cpus; +#elif defined(TARGET_kontron_vx3060_s2) + int i; +#endif _mp = (struct mptable *)MPTABLE_LOAD_BASE; x86_log_memory_load((uint32_t)MPTABLE_LOAD_BASE, @@ -518,19 +524,29 @@ void mptable_setup(void) _mp->mpce_processor[0]._res[0] = _mp->mpce_processor[0]._res[1] = 0; -#if defined(TARGET_kontron_vx3060_s2) || defined(TARGET_nai_68int6) - { - int i; - for (i = 1; i < 4; ++i) { - _mp->mpce_processor[i].apic_id = i * 2; - _mp->mpce_processor[i].apic_ver = (uint8_t)(apic_ver & 0xff); - _mp->mpce_processor[i].cpu_flags = 0x1; /* enabled */ - _mp->mpce_processor[i].cpu_signature = cpupid_sign; - _mp->mpce_processor[i].feature_flags = 0; - _mp->mpce_processor[i]._res[0] = _mp->mpce_processor[i]._res[1] = 0; - } +#if defined(TARGET_nai_68int6) + cpus = x86_cpu_apic_ids(apic_ids, MP_CPU_NUM_ENTRY); + for (i = 0; i < cpus && j < MP_CPU_NUM_ENTRY; i++) { + if (apic_ids[i] == apic_id) + continue; + _mp->mpce_processor[j].apic_id = apic_ids[i]; + _mp->mpce_processor[j].apic_ver = (uint8_t)(apic_ver & 0xff); + _mp->mpce_processor[j].cpu_flags = 0x1; /* enabled */ + _mp->mpce_processor[j].cpu_signature = cpupid_sign; + _mp->mpce_processor[j].feature_flags = 0; + _mp->mpce_processor[j]._res[0] = _mp->mpce_processor[j]._res[1] = 0; + j++; } -#endif /* TARGET_kontron_vx3060_s2 */ +#elif defined(TARGET_kontron_vx3060_s2) + for (i = 1; i < 4 && i < MP_CPU_NUM_ENTRY; ++i) { + _mp->mpce_processor[i].apic_id = i * 2; + _mp->mpce_processor[i].apic_ver = (uint8_t)(apic_ver & 0xff); + _mp->mpce_processor[i].cpu_flags = 0x1; /* enabled */ + _mp->mpce_processor[i].cpu_signature = cpupid_sign; + _mp->mpce_processor[i].feature_flags = 0; + _mp->mpce_processor[i]._res[0] = _mp->mpce_processor[i]._res[1] = 0; + } +#endif calc_checksum(_mp); } diff --git a/tools/unit-tests/Makefile b/tools/unit-tests/Makefile index abe00e18f8..3ac421e2b6 100644 --- a/tools/unit-tests/Makefile +++ b/tools/unit-tests/Makefile @@ -177,6 +177,10 @@ TESTS+=unit-kontron-tgl-spi TESTS+=unit-fsp-nvs TESTS+=unit-fsp-nvs-save TESTS+=unit-tgl-spi-hwseq +TESTS+=unit-x86-apic-ids +TESTS+=unit-x86-acpi +TESTS+=unit-boot-x86-fsp-measure +TESTS+=unit-boot-x86-fsp-payload TESTS+=unit-nrf5340-flash-protect TESTS+=unit-keygen-keystore TESTS+=unit-samr21-erase-advance @@ -2277,6 +2281,42 @@ unit-tgl-spi-hwseq: unit-tgl-spi-hwseq.c kontron_spi_extract.h \ tgl_spi_hwseq_extract.h gcc -o $@ unit-tgl-spi-hwseq.c $(CFLAGS) $(LDFLAGS) +# unit-x86-apic-ids runs the real x86_apic_ids_from_topology() from +# src/x86/common.c (the CPU list behind the MADT and the MP table). +x86_apic_ids_extract.h: ../../src/x86/common.c + sed -n '/^int x86_apic_ids_from_topology/,/^}/p' $< > $@ + +unit-x86-apic-ids: unit-x86-apic-ids.c x86_apic_ids_extract.h + gcc -o $@ unit-x86-apic-ids.c $(CFLAGS) $(LDFLAGS) + +# unit-x86-acpi runs the x86 FSP ACPI table builders (src/x86/acpi.c) +# against mocked PCI, MMIO and CPU topology reads. +unit-x86-acpi: unit-x86-acpi.c ../../src/x86/acpi.c ../../include/x86/acpi_dsdt.h + gcc -o $@ unit-x86-acpi.c $(CFLAGS) $(LDFLAGS) + +# unit-boot-x86-fsp-payload runs the ELF segment check and the per-image +# Multiboot2 ELF or Linux choice (src/boot_x86_fsp_payload.c). +unit-boot-x86-fsp-payload: ../../include/target.h unit-boot-x86-fsp-payload.c \ + ../../src/boot_x86_fsp_payload.c + gcc -o $@ unit-boot-x86-fsp-payload.c $(CFLAGS) \ + -DWOLFBOOT_LOAD_BASE=0x58000200 -DWOLFBOOT_FSP -DTARGET_nai_68int6 \ + -DWOLFBOOT_64BIT \ + -DX86_UART_BASE=0xFE036000 -DWOLFBOOT_LINUX_PAYLOAD -DWOLFBOOT_ELF \ + -DWOLFBOOT_MULTIBOOT2 $(LDFLAGS) + +# unit-boot-x86-fsp-measure runs the stage 1 PCR 0 measurement of stage 2 +# (wolfBoot_image_measure() in src/boot_x86_fsp.c) with real SHA-256. +unit-boot-x86-fsp-measure: ../../include/target.h unit-boot-x86-fsp-measure.c \ + ../../src/boot_x86_fsp.c + gcc -o $@ unit-boot-x86-fsp-measure.c \ + $(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/sha256.c $(CFLAGS) \ + -I$(WOLFBOOT_LIB_WOLFTPM) -DWOLFTPM_USER_SETTINGS \ + -DWOLFBOOT_LOAD_BASE=0x100000 -DWOLFBOOT_FSP -DUCODE0_ADDRESS=0 \ + -D__WOLFBOOT -DWOLFBOOT_TPM -DWOLFBOOT_MEASURED_BOOT \ + -DWOLFBOOT_MEASURED_PCR_A=0 \ + -DWOLFBOOT_HASH_SHA384 -DWOLFBOOT_SIGN_ECC384 \ + -ffunction-sections -fdata-sections $(LDFLAGS) -Wl,--gc-sections + # unit-fsp-nvs runs the FSP-M memory training data cache record check and # the stage 1 hand-off (src/x86/fsp_nvs.c) against a RAM-backed area. unit-fsp-nvs:CFLAGS+=-DWOLFBOOT_FSP -DWOLFBOOT_FSP_NVS_CACHE \ diff --git a/tools/unit-tests/unit-boot-x86-fsp-measure.c b/tools/unit-tests/unit-boot-x86-fsp-measure.c new file mode 100644 index 0000000000..8e3094e8eb --- /dev/null +++ b/tools/unit-tests/unit-boot-x86-fsp-measure.c @@ -0,0 +1,142 @@ +/* unit-boot-x86-fsp-measure.c + * + * Unit tests for the stage 1 measurement of stage 2 (wolfBoot_image_measure() + * in src/boot_x86_fsp.c): the SHA-384 image digest is re-hashed with + * SHA-256 and extended into PCR 0, the value compute_pcr.py predicts. + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#include +#include +#include + +#include "../../src/boot_x86_fsp.c" + +static uint8_t g_image_digest[WOLFBOOT_SHA_DIGEST_SIZE]; +static int g_verify_ret; +static int g_extend_calls; +static uint8_t g_extend_pcr; +static uint8_t g_extended[SHA256_DIGEST_SIZE]; + +uint8_t pci_config_read8(uint8_t bus, uint8_t dev, uint8_t fun, uint8_t off) +{ + (void)bus; (void)dev; (void)fun; (void)off; + return 0xff; +} + +uint16_t pci_config_read16(uint8_t bus, uint8_t dev, uint8_t fun, uint8_t off) +{ + (void)bus; (void)dev; (void)fun; (void)off; + return 0xffff; +} + +int wolfBoot_open_image_address(struct wolfBoot_image *img, uint8_t *image) +{ + (void)image; + img->sha_hash = NULL; + return 0; +} + +int wolfBoot_verify_integrity(struct wolfBoot_image *img) +{ + if (g_verify_ret == 0) + img->sha_hash = g_image_digest; + return g_verify_ret; +} + +int wolfBoot_tpm2_extend(uint8_t pcrIndex, uint8_t *hash, int line) +{ + (void)line; + g_extend_calls++; + g_extend_pcr = pcrIndex; + memcpy(g_extended, hash, sizeof(g_extended)); + return 0; +} + +void wolfBoot_print_hexstr(const unsigned char *bin, unsigned long sz, + unsigned long maxLine) +{ + (void)bin; (void)sz; (void)maxLine; +} + +static void reset_mocks(void) +{ + unsigned int i; + + for (i = 0; i < sizeof(g_image_digest); i++) + g_image_digest[i] = (uint8_t)i; + g_verify_ret = 0; + g_extend_calls = 0; + g_extend_pcr = 0xff; + memset(g_extended, 0, sizeof(g_extended)); +} + +START_TEST(test_measure_extends_sha256_of_the_digest) +{ + /* sha256(bytes(range(48))) */ + static const uint8_t want[SHA256_DIGEST_SIZE] = { + 0x4d, 0xbd, 0xc2, 0xb2, 0xb6, 0x2c, 0xb0, 0x07, + 0x49, 0x78, 0x5b, 0xc8, 0x42, 0x02, 0x23, 0x6d, + 0xbc, 0x37, 0x77, 0xd7, 0x46, 0x60, 0x61, 0x1b, + 0x8e, 0x58, 0x81, 0x2f, 0x0c, 0xfd, 0xe6, 0xc3 + }; + uint8_t image[16]; + + reset_mocks(); + ck_assert_int_eq(wolfBoot_image_measure(image), 0); + ck_assert_int_eq(g_extend_calls, 1); + ck_assert_uint_eq(g_extend_pcr, WOLFBOOT_MEASURED_PCR_A); + ck_assert_int_eq(memcmp(g_extended, want, sizeof(want)), 0); +} +END_TEST + +START_TEST(test_measure_skips_an_unverified_image) +{ + uint8_t image[16]; + + reset_mocks(); + g_verify_ret = -1; + ck_assert_int_ne(wolfBoot_image_measure(image), 0); + ck_assert_int_eq(g_extend_calls, 0); +} +END_TEST + +static Suite *measure_suite(void) +{ + Suite *s = suite_create("boot-x86-fsp-measure"); + TCase *tc = tcase_create("boot-x86-fsp-measure"); + + tcase_add_test(tc, test_measure_extends_sha256_of_the_digest); + tcase_add_test(tc, test_measure_skips_an_unverified_image); + suite_add_tcase(s, tc); + return s; +} + +int main(void) +{ + int fails; + Suite *s = measure_suite(); + SRunner *sr = srunner_create(s); + + srunner_run_all(sr, CK_NORMAL); + fails = srunner_ntests_failed(sr); + srunner_free(sr); + return fails; +} diff --git a/tools/unit-tests/unit-boot-x86-fsp-payload.c b/tools/unit-tests/unit-boot-x86-fsp-payload.c new file mode 100644 index 0000000000..9e9ccd62a9 --- /dev/null +++ b/tools/unit-tests/unit-boot-x86-fsp-payload.c @@ -0,0 +1,210 @@ +/* unit-boot-x86-fsp-payload.c + * + * Unit tests for the x86 FSP payload hand-off (src/boot_x86_fsp_payload.c): + * the ELF segment check against wolfBoot's own memory, and the per-image + * choice between the Multiboot2 ELF and Linux paths, with the signed or + * built-in kernel command line. + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#include +#include +#include +#include + +#include "../../src/boot_x86_fsp_payload.c" + +static struct stage2_parameter g_params; +static jmp_buf g_panic_jmp; +static int g_panics; +static int g_elf_loads; +static int g_mb2_jumps; +static int g_linux_loads; +static const char *g_linux_cmdline; +static uint8_t g_mb2_header[16]; + +void panic(void) +{ + g_panics++; + longjmp(g_panic_jmp, 1); +} + +struct stage2_parameter *stage2_get_parameters(void) +{ + return &g_params; +} + +void mptable_setup(void) +{ +} + +int x86_paging_dump_info(void) +{ + return 0; +} + +int x86_paging_map_memory(uint64_t va, uint64_t pa, uint32_t size) +{ + (void)va; + (void)pa; + (void)size; + return 0; +} + +int elf_load_image_mmu(uint8_t *image, uint32_t image_sz, uintptr_t *pentry, + elf_mmu_map_cb mmu_cb) +{ + (void)image; + (void)image_sz; + (void)mmu_cb; + g_elf_loads++; + *pentry = 0x1000; + return 0; +} + +uint8_t *mb2_find_header(uint8_t *image, int size) +{ + (void)image; + (void)size; + return g_mb2_header; +} + +int mb2_build_boot_info_header(uint8_t *boot_info, uint8_t *mb2_header, + void *params, unsigned max_size) +{ + (void)boot_info; + (void)mb2_header; + (void)params; + (void)max_size; + return 0; +} + +void mb2_jump(uintptr_t entry, uint32_t boot_info) +{ + (void)entry; + (void)boot_info; + g_mb2_jumps++; +} + +void load_linux(uint8_t *linux_image, uint32_t image_size, void *params, + const char *cmd_line) +{ + (void)linux_image; + (void)image_size; + (void)params; + g_linux_loads++; + g_linux_cmdline = cmd_line; +} + +static void reset_mocks(void) +{ + memset(&g_params, 0, sizeof(g_params)); + g_params.payload_size = 4096; + g_panics = 0; + g_elf_loads = 0; + g_mb2_jumps = 0; + g_linux_loads = 0; + g_linux_cmdline = NULL; +} + +START_TEST(test_elf_segments_outside_wolfboot_memory) +{ + /* below the reserved window, ending exactly at its start, and above + * 4 GB are all accepted; an empty segment is never checked */ + ck_assert_int_eq(boot_elf_map_cb(0, 0x200000, 0x1000), 0); + ck_assert_int_eq(boot_elf_map_cb(0, ELF_RESERVED_START - 0x1000, 0x1000), + 0); + ck_assert_int_eq(boot_elf_map_cb(0, ELF_RESERVED_END, 0x1000), 0); + ck_assert_int_eq(boot_elf_map_cb(0, ELF_RESERVED_START, 0), 0); +} +END_TEST + +START_TEST(test_elf_segments_over_wolfboot_memory) +{ + ck_assert_int_eq(boot_elf_map_cb(0, ELF_RESERVED_START - 0x1000, 0x1001), + -1); + ck_assert_int_eq(boot_elf_map_cb(0, ELF_RESERVED_START, 1), -1); + ck_assert_int_eq(boot_elf_map_cb(0, ELF_RESERVED_END - 1, 0x1000), -1); + ck_assert_int_eq(boot_elf_map_cb(0, 0x100000, 0xfff00000U), -1); + /* a safe physical address with a virtual one over stage 2 */ + ck_assert_int_eq(boot_elf_map_cb(WOLFBOOT_LOAD_BASE, 0x200000, 0x1000), + -1); +} +END_TEST + +START_TEST(test_elf_image_takes_the_multiboot2_path) +{ + uint32_t image[4] = { 0 }; + + reset_mocks(); + memcpy(image, ELF_IDENT_STR, 4); + if (setjmp(g_panic_jmp) == 0) + do_boot(image); + ck_assert_int_eq(g_panics, 0); + ck_assert_int_eq(g_elf_loads, 1); + ck_assert_int_eq(g_mb2_jumps, 1); + ck_assert_int_eq(g_linux_loads, 0); +} +END_TEST + +START_TEST(test_other_image_boots_linux) +{ + uint32_t image[4] = { 0 }; + + reset_mocks(); + memcpy(image, "MZ\0\0", 4); + do_boot(image); + ck_assert_int_eq(g_elf_loads, 0); + ck_assert_int_eq(g_linux_loads, 1); + ck_assert_ptr_eq(g_linux_cmdline, cmdline); + + reset_mocks(); + /* stage2_params carries a 32-bit address; only compared, not read */ + g_params.cmdline = 0x5a000000U; + do_boot(image); + ck_assert_int_eq(g_linux_loads, 1); + ck_assert_ptr_eq(g_linux_cmdline, (const char *)(uintptr_t)0x5a000000U); +} +END_TEST + +static Suite *payload_suite(void) +{ + Suite *s = suite_create("boot-x86-fsp-payload"); + TCase *tc = tcase_create("boot-x86-fsp-payload"); + + tcase_add_test(tc, test_elf_segments_outside_wolfboot_memory); + tcase_add_test(tc, test_elf_segments_over_wolfboot_memory); + tcase_add_test(tc, test_elf_image_takes_the_multiboot2_path); + tcase_add_test(tc, test_other_image_boots_linux); + suite_add_tcase(s, tc); + return s; +} + +int main(void) +{ + int fails; + Suite *s = payload_suite(); + SRunner *sr = srunner_create(s); + + srunner_run_all(sr, CK_NORMAL); + fails = srunner_ntests_failed(sr); + srunner_free(sr); + return fails; +} diff --git a/tools/unit-tests/unit-update-disk-fsp.c b/tools/unit-tests/unit-update-disk-fsp.c index 8646804f85..210bdb1e2b 100644 --- a/tools/unit-tests/unit-update-disk-fsp.c +++ b/tools/unit-tests/unit-update-disk-fsp.c @@ -9,7 +9,7 @@ #define WOLFBOOT_SELF_UPDATE_MONOLITHIC #define RAM_CODE #define WOLFBOOT_SELF_HEADER -#define IMAGE_HEADER_SIZE 256 +#define IMAGE_HEADER_SIZE 512 #define BOOT_PART_A 0 #define BOOT_PART_B 1 #define MOCK_ADDRESS_BOOT 0xCD000000 @@ -27,6 +27,12 @@ #include "stage2_params.h" #include "x86/common.h" +/* The test build has no image hash configured; take SHA-256 */ +#ifndef WOLFBOOT_SHA_HDR +#define WOLFBOOT_SHA_HDR HDR_SHA256 +#define WOLFBOOT_SHA_DIGEST_SIZE 32 +#endif + #define TEST_PAYLOAD_SIZE 64 static uint8_t load_buffer[TEST_PAYLOAD_SIZE]; @@ -37,7 +43,6 @@ static uint8_t part_b_image[IMAGE_HEADER_SIZE + TEST_PAYLOAD_SIZE]; static int mock_do_boot_called; static const uint32_t *mock_boot_address; static struct stage2_parameter mock_stage2_params; -static const char *mock_cmdline; static void set_u16_le(uint8_t *dst, uint16_t value) { @@ -75,7 +80,6 @@ static void reset_mocks(void) build_image(part_b_image, 7, TEST_PAYLOAD_SIZE, 0xB2); mock_do_boot_called = 0; mock_boot_address = NULL; - mock_cmdline = NULL; mock_stage2_params.cmdline = 0xffffffffU; /* The image must fit between load_address and tolum: exactly one * TEST_PAYLOAD_SIZE payload. */ @@ -173,15 +177,53 @@ int wolfBoot_image_part_matches(struct wolfBoot_image* img) return 0; } +/* Walks the TLVs of a test header, as the real one does */ uint16_t wolfBoot_find_header(uint8_t *haystack, uint16_t type, uint8_t **ptr) { - (void)haystack; - if (type != HDR_CMDLINE || mock_cmdline == NULL) { - *ptr = NULL; - return 0; + uint8_t *p = haystack; + uint8_t *end = haystack - IMAGE_HEADER_OFFSET + IMAGE_HEADER_SIZE; + uint16_t t, l; + + *ptr = NULL; + while (end - p >= 4) { + t = (uint16_t)(p[0] | (p[1] << 8)); + l = (uint16_t)(p[2] | (p[3] << 8)); + if (t == 0 || l > end - p - 4) + break; + if (t == type) { + *ptr = p + 4; + return l; + } + p += 4 + l; } - *ptr = (uint8_t *)mock_cmdline; - return (uint16_t)strlen(mock_cmdline); + return 0; +} + +/* Appends a TLV after the last one in a test header */ +static void add_tlv(uint8_t *image, uint16_t type, uint16_t len, + const uint8_t *val) +{ + uint8_t *p = image + IMAGE_HEADER_OFFSET; + + while ((p[0] | (p[1] << 8)) != 0) + p += 4 + (p[2] | (p[3] << 8)); + set_u16_le(p, type); + set_u16_le(p + 2, len); + memcpy(p + 4, val, len); +} + +static void add_digest(uint8_t *image) +{ + uint8_t digest[WOLFBOOT_SHA_DIGEST_SIZE]; + + memset(digest, 0x5A, sizeof(digest)); + add_tlv(image, WOLFBOOT_SHA_HDR, sizeof(digest), digest); +} + +static void add_cmdline(uint8_t *image, const char *cmdline) +{ + add_tlv(image, HDR_CMDLINE, (uint16_t)strlen(cmdline), + (const uint8_t *)cmdline); } int wolfBoot_get_dts_size(void *dts_addr, uint32_t capacity) @@ -292,12 +334,49 @@ START_TEST(test_fsp_signed_cmdline_is_passed_on) /* A signed HDR_CMDLINE reaches the payload loader; without one the * loader is told there is none. */ reset_mocks(); - mock_cmdline = "console=ttyS4,115200 root=/dev/sda2"; + add_cmdline(part_a_image, "console=ttyS4,115200 root=/dev/sda2"); + add_digest(part_a_image); wolfBoot_start(); ck_assert_int_eq(mock_do_boot_called, 1); ck_assert_uint_ne(mock_stage2_params.cmdline, 0); reset_mocks(); + add_digest(part_a_image); + wolfBoot_start(); + ck_assert_int_eq(mock_do_boot_called, 1); + ck_assert_uint_eq(mock_stage2_params.cmdline, 0); +} +END_TEST + +START_TEST(test_fsp_signed_cmdline_too_long_is_ignored) +{ + static char long_cmdline[WOLFBOOT_CMDLINE_MAX + 1]; + + reset_mocks(); + memset(long_cmdline, 'a', sizeof(long_cmdline) - 1); + long_cmdline[sizeof(long_cmdline) - 1] = '\0'; + add_cmdline(part_a_image, long_cmdline); + add_digest(part_a_image); + wolfBoot_start(); + ck_assert_int_eq(mock_do_boot_called, 1); + ck_assert_uint_eq(mock_stage2_params.cmdline, 0); +} +END_TEST + +START_TEST(test_fsp_cmdline_after_the_digest_is_ignored) +{ + /* Only the header before the digest TLV is signed, so a command line + * appended after it must not reach the kernel */ + reset_mocks(); + add_digest(part_a_image); + add_cmdline(part_a_image, "init=/bin/sh"); + wolfBoot_start(); + ck_assert_int_eq(mock_do_boot_called, 1); + ck_assert_uint_eq(mock_stage2_params.cmdline, 0); + + /* nor one in a header with no digest TLV at all */ + reset_mocks(); + add_cmdline(part_a_image, "init=/bin/sh"); wolfBoot_start(); ck_assert_int_eq(mock_do_boot_called, 1); ck_assert_uint_eq(mock_stage2_params.cmdline, 0); @@ -314,6 +393,8 @@ Suite *wolfboot_suite(void) tcase_add_test(tc, test_fsp_inverted_tolum_rejects_both_slots); tcase_add_test(tc, test_fsp_fitting_slot_boots); tcase_add_test(tc, test_fsp_signed_cmdline_is_passed_on); + tcase_add_test(tc, test_fsp_signed_cmdline_too_long_is_ignored); + tcase_add_test(tc, test_fsp_cmdline_after_the_digest_is_ignored); suite_add_tcase(s, tc); return s; diff --git a/tools/unit-tests/unit-x86-acpi.c b/tools/unit-tests/unit-x86-acpi.c new file mode 100644 index 0000000000..0853f627d7 --- /dev/null +++ b/tools/unit-tests/unit-x86-acpi.c @@ -0,0 +1,221 @@ +/* unit-x86-acpi.c + * + * Unit tests for the x86 FSP ACPI table builders (src/x86/acpi.c): the MADT + * CPU list, the TPM2, HPET and DMAR tables, their lengths and checksums, + * with the PCI, MMIO and CPU topology reads mocked. + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#include +#include +#include + +#include "../../src/x86/acpi.c" + +static int g_cpus; +static int g_igd; +static uint32_t g_bdsm, g_tolud; +static uint8_t g_buf[4096] __attribute__((aligned(16))); + +int x86_cpu_apic_ids(uint8_t *ids, int max) +{ + const uint8_t order[] = { 0, 2, 4, 6, 1, 3, 5, 7 }; + int i; + + for (i = 0; i < g_cpus && i < max; i++) + ids[i] = order[i]; + return i; +} + +uint32_t mmio_read32(uintptr_t address) +{ + if (address == ACPI_HPET_BASE) + return 0x8086a701U; + if (address == ACPI_LAPIC_ADDR + 0x20) + return 0x05000000U; + return 0; +} + +uint32_t pci_config_read32(uint8_t bus, uint8_t dev, uint8_t fun, uint8_t off) +{ + (void)bus; + if (dev == 2 && fun == 0 && off == 0) + return g_igd ? 0x9a498086U : 0xffffffffU; + if (dev == 2 && fun == 0 && off == 0x5c) + return g_bdsm | 0x1U; + if (dev == 0 && fun == 0 && off == 0xbc) + return g_tolud | 0x1U; + return 0xffffffffU; +} + +static void reset_mocks(void) +{ + memset(g_buf, 0xa5, sizeof(g_buf)); + g_cpus = 4; + g_igd = 1; + g_bdsm = 0x4c000000U; + g_tolud = 0x50400000U; +} + +static int table_ok(const uint8_t *t, const char *sig, uint32_t len) +{ + const struct acpi_header *h = (const struct acpi_header *)t; + uint8_t sum = 0; + uint32_t i; + + if (memcmp(h->signature, sig, 4) != 0 || h->length != len) + return 0; + for (i = 0; i < len; i++) + sum = (uint8_t)(sum + t[i]); + return sum == 0; +} + +START_TEST(test_madt_lists_present_cpus) +{ + uint8_t *end; + const struct madt_lapic *l; + int i, n; + + for (n = 4; n <= 8; n += 4) { + reset_mocks(); + g_cpus = n; + end = acpi_build_madt(g_buf); + ck_assert(table_ok(g_buf, "APIC", (uint32_t)(end - g_buf))); + l = (const struct madt_lapic *)(g_buf + sizeof(struct acpi_madt)); + for (i = 0; i < n; i++) { + ck_assert_uint_eq(l[i].type, MADT_TYPE_LAPIC); + ck_assert_uint_eq(l[i].acpi_processor_id, i); + ck_assert_uint_eq(l[i].flags, MADT_LAPIC_ENABLED); + } + ck_assert_uint_eq(l[3].apic_id, 6); + /* the entry after the last CPU is the IOAPIC */ + ck_assert_uint_eq(l[n].type, MADT_TYPE_IOAPIC); + } + ck_assert_uint_eq(l[4].apic_id, 1); +} +END_TEST + +START_TEST(test_madt_falls_back_to_the_bsp) +{ + const struct madt_lapic *l; + + reset_mocks(); + g_cpus = 0; + acpi_build_madt(g_buf); + l = (const struct madt_lapic *)(g_buf + sizeof(struct acpi_madt)); + ck_assert_uint_eq(l[0].apic_id, 5); + ck_assert_uint_eq(l[1].type, MADT_TYPE_IOAPIC); +} +END_TEST + +START_TEST(test_tpm2_and_hpet) +{ + const struct acpi_tpm2 *tpm2 = (const struct acpi_tpm2 *)g_buf; + const struct acpi_hpet *hpet = (const struct acpi_hpet *)g_buf; + + reset_mocks(); + ck_assert_ptr_eq(acpi_build_tpm2(g_buf), g_buf + sizeof(*tpm2)); + ck_assert(table_ok(g_buf, "TPM2", sizeof(*tpm2))); + ck_assert_uint_eq(tpm2->start_method, ACPI_TPM2_START_TIS); + ck_assert_uint_eq(tpm2->control_address, 0); + + reset_mocks(); + ck_assert_ptr_eq(acpi_build_hpet(g_buf), g_buf + sizeof(*hpet)); + ck_assert(table_ok(g_buf, "HPET", sizeof(*hpet))); + ck_assert_uint_eq(hpet->block_id, 0x8086a701U); + ck_assert_uint_eq(hpet->base.address, ACPI_HPET_BASE); +} +END_TEST + +START_TEST(test_dmar_with_igd) +{ + const struct dmar_drhd *gfx, *def; + const struct dmar_rmrr *rmrr; + const struct dmar_scope *sc; + uint8_t *end; + + reset_mocks(); + end = acpi_build_dmar(g_buf); + ck_assert(table_ok(g_buf, "DMAR", (uint32_t)(end - g_buf))); + + gfx = (const struct dmar_drhd *)(g_buf + sizeof(struct acpi_dmar)); + ck_assert_uint_eq(gfx->base, DMAR_GFX_UNIT); + ck_assert_uint_eq(gfx->flags, 0); + sc = (const struct dmar_scope *)(gfx + 1); + ck_assert_uint_eq(sc->type, DMAR_SCOPE_ENDPOINT); + ck_assert_uint_eq(sc->dev, 2); + + def = (const struct dmar_drhd *)((const uint8_t *)gfx + gfx->length); + ck_assert_uint_eq(def->base, DMAR_DEFAULT_UNIT); + ck_assert_uint_eq(def->flags, DMAR_INCLUDE_PCI_ALL); + sc = (const struct dmar_scope *)(def + 1); + ck_assert_uint_eq(sc[0].type, DMAR_SCOPE_IOAPIC); + ck_assert_uint_eq(sc[0].enumeration_id, ACPI_IOAPIC_ID); + ck_assert_uint_eq(sc[1].type, DMAR_SCOPE_HPET); + + rmrr = (const struct dmar_rmrr *)((const uint8_t *)def + def->length); + ck_assert_uint_eq(rmrr->type, DMAR_RMRR); + ck_assert_uint_eq(rmrr->base, 0x4c000000U); + ck_assert_uint_eq(rmrr->limit, 0x503fffffU); + ck_assert_ptr_eq((const uint8_t *)rmrr + rmrr->length, end); +} +END_TEST + +START_TEST(test_dmar_without_igd) +{ + const struct dmar_drhd *def; + uint8_t *end; + + reset_mocks(); + g_igd = 0; + end = acpi_build_dmar(g_buf); + ck_assert(table_ok(g_buf, "DMAR", (uint32_t)(end - g_buf))); + def = (const struct dmar_drhd *)(g_buf + sizeof(struct acpi_dmar)); + ck_assert_uint_eq(def->base, DMAR_DEFAULT_UNIT); + /* no graphics unit and no reserved region after the default unit */ + ck_assert_ptr_eq((const uint8_t *)def + def->length, end); +} +END_TEST + +static Suite *acpi_suite(void) +{ + Suite *s = suite_create("x86-acpi"); + TCase *tc = tcase_create("x86-acpi"); + + tcase_add_test(tc, test_madt_lists_present_cpus); + tcase_add_test(tc, test_madt_falls_back_to_the_bsp); + tcase_add_test(tc, test_tpm2_and_hpet); + tcase_add_test(tc, test_dmar_with_igd); + tcase_add_test(tc, test_dmar_without_igd); + suite_add_tcase(s, tc); + return s; +} + +int main(void) +{ + int fails; + Suite *s = acpi_suite(); + SRunner *sr = srunner_create(s); + + srunner_run_all(sr, CK_NORMAL); + fails = srunner_ntests_failed(sr); + srunner_free(sr); + return fails; +} diff --git a/tools/unit-tests/unit-x86-apic-ids.c b/tools/unit-tests/unit-x86-apic-ids.c new file mode 100644 index 0000000000..943e678dee --- /dev/null +++ b/tools/unit-tests/unit-x86-apic-ids.c @@ -0,0 +1,112 @@ +/* unit-x86-apic-ids.c + * + * Unit tests for the CPU list the x86 FSP MADT and MP table are built from + * (x86_apic_ids_from_topology() in src/x86/common.c, extracted by the + * Makefile): enabled cores and threads from MSR_CORE_THREAD_COUNT, APIC ID + * layout from the SMT shift, primary threads first. + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#include +#include +#include + +#include "x86_apic_ids_extract.h" + +#define COUNTS(cores, threads) (((uint32_t)(cores) << 16) | (threads)) + +START_TEST(test_ht_off_four_cores) +{ + const uint8_t want[] = { 0, 2, 4, 6 }; + uint8_t ids[8]; + + memset(ids, 0xee, sizeof(ids)); + ck_assert_int_eq(x86_apic_ids_from_topology(COUNTS(4, 4), 1, ids, 8), 4); + ck_assert_int_eq(memcmp(ids, want, sizeof(want)), 0); +} +END_TEST + +START_TEST(test_ht_on_primaries_first) +{ + const uint8_t want[] = { 0, 2, 4, 6, 1, 3, 5, 7 }; + uint8_t ids[8]; + + ck_assert_int_eq(x86_apic_ids_from_topology(COUNTS(4, 8), 1, ids, 8), 8); + ck_assert_int_eq(memcmp(ids, want, sizeof(want)), 0); +} +END_TEST + +START_TEST(test_fewer_cores) +{ + const uint8_t want[] = { 0, 2, 1, 3 }; + uint8_t ids[8]; + + ck_assert_int_eq(x86_apic_ids_from_topology(COUNTS(2, 4), 1, ids, 8), 4); + ck_assert_int_eq(memcmp(ids, want, sizeof(want)), 0); +} +END_TEST + +START_TEST(test_capacity_is_respected) +{ + const uint8_t want[] = { 0, 2, 4 }; + uint8_t ids[8]; + + memset(ids, 0xee, sizeof(ids)); + ck_assert_int_eq(x86_apic_ids_from_topology(COUNTS(4, 8), 1, ids, 3), 3); + ck_assert_int_eq(memcmp(ids, want, sizeof(want)), 0); + ck_assert_uint_eq(ids[3], 0xee); +} +END_TEST + +START_TEST(test_unusable_counts) +{ + uint8_t ids[8]; + + ck_assert_int_eq(x86_apic_ids_from_topology(COUNTS(0, 4), 1, ids, 8), 0); + ck_assert_int_eq(x86_apic_ids_from_topology(COUNTS(4, 2), 1, ids, 8), 0); + ck_assert_int_eq(x86_apic_ids_from_topology(0, 1, ids, 8), 0); +} +END_TEST + +static Suite *apic_ids_suite(void) +{ + Suite *s = suite_create("x86-apic-ids"); + TCase *tc = tcase_create("x86-apic-ids"); + + tcase_add_test(tc, test_ht_off_four_cores); + tcase_add_test(tc, test_ht_on_primaries_first); + tcase_add_test(tc, test_fewer_cores); + tcase_add_test(tc, test_capacity_is_respected); + tcase_add_test(tc, test_unusable_counts); + suite_add_tcase(s, tc); + return s; +} + +int main(void) +{ + int fails; + Suite *s = apic_ids_suite(); + SRunner *sr = srunner_create(s); + + srunner_run_all(sr, CK_NORMAL); + fails = srunner_ntests_failed(sr); + srunner_free(sr); + return fails; +} From 855a67e56fd0ecd4796d7450fce9489fd04cc604 Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 3 Nov 2026 14:16:00 -0800 Subject: [PATCH 06/17] x86 ata: use WRITE DMA EXT for sector writes and return command errors --- .gitignore | 1 + src/x86/ata.c | 28 +++- tools/unit-tests/Makefile | 5 + tools/unit-tests/unit-ata-sector-io.c | 218 ++++++++++++++++++++++++++ 4 files changed, 244 insertions(+), 8 deletions(-) create mode 100644 tools/unit-tests/unit-ata-sector-io.c diff --git a/.gitignore b/.gitignore index cc55bff7b2..c16c46181f 100644 --- a/.gitignore +++ b/.gitignore @@ -250,6 +250,7 @@ tools/unit-tests/unit-fwtpm-nv-oob tools/unit-tests/unit-x86-paging-oob tools/unit-tests/unit-ahci-unlock-panic tools/unit-tests/unit-ata-security-passphrase-zeroize +tools/unit-tests/unit-ata-sector-io tools/unit-tests/unit-arm-tee-psa-ipc tools/unit-tests/unit-flash-write-mcxa tools/unit-tests/unit-rw612-flash diff --git a/src/x86/ata.c b/src/x86/ata.c index 535cca9254..c299d92a26 100644 --- a/src/x86/ata.c +++ b/src/x86/ata.c @@ -774,7 +774,8 @@ static int ata_drive_read_sector(int drv, uint64_t start, uint32_t count, cmdfis->lba5 = (uint8_t)((start >> 40) & 0xFF); cmdfis->device = (1 << 6); /* LBA mode */ cmdfis->count = (uint16_t)(count & 0xFFFF); - exec_cmd_slot(drv, slot); + if (exec_cmd_slot(drv, slot) != 0) + return -1; return count << ata->sector_size_shift; } @@ -794,7 +795,7 @@ static int ata_drive_write_sector(int drv, uint64_t start, uint32_t count, cmdfis = (struct fis_reg_h2d *)(&tbl->cfis); cmdfis->fis_type = FIS_TYPE_REG_H2D; cmdfis->c = 1; - cmdfis->command = ATA_CMD_WRITE_DMA; + cmdfis->command = ATA_CMD_WRITE_DMA_EX; cmdfis->lba0 = (uint8_t)(start & 0xFF); cmdfis->lba1 = (uint8_t)((start >> 8) & 0xFF); cmdfis->lba2 = (uint8_t)((start >> 16) & 0xFF); @@ -804,7 +805,8 @@ static int ata_drive_write_sector(int drv, uint64_t start, uint32_t count, cmdfis->device = (1 << 6); /* LBA mode */ cmdfis->count = (uint16_t)(count & 0xFFFF); - exec_cmd_slot(drv, slot); + if (exec_cmd_slot(drv, slot) != 0) + return -1; return count << ata->sector_size_shift; } @@ -826,12 +828,16 @@ static void ata_cache_pull(int drv, uint64_t sector) ata->cached = sector; } -static void ata_cache_commit(int drv) +static int ata_cache_commit(int drv) { struct ata_drive *ata = &ATA_Drv[drv]; if (ata->cached == CACHE_INVALID) - return; - ata_drive_write_sector(drv, ata->cached, 1, ata->sector_cache); + return 0; + if (ata_drive_write_sector(drv, ata->cached, 1, ata->sector_cache) < 0) { + ata->cached = CACHE_INVALID; + return -1; + } + return 0; } /** @@ -881,6 +887,8 @@ int ata_drive_read(int drv, uint64_t start, uint32_t size, uint8_t *buf) } if (size > 0) { ata_cache_pull(drv, sect_start); + if (ata->cached != sect_start) + return -1; memcpy(buf + buffer_off, ata->sector_cache, size); buffer_off += size; } @@ -920,7 +928,8 @@ int ata_drive_write(int drv, uint64_t start, uint32_t size, if (ata->cached != sect_start) return -1; memcpy(ata->sector_cache + sect_off, buf, len); - ata_cache_commit(drv); + if (ata_cache_commit(drv) < 0) + return -1; size -= len; buffer_off += len; sect_start++; @@ -937,8 +946,11 @@ int ata_drive_write(int drv, uint64_t start, uint32_t size, } if (size > 0) { ata_cache_pull(drv, sect_start); + if (ata->cached != sect_start) + return -1; memcpy(ata->sector_cache, buf + buffer_off, size); - ata_cache_commit(drv); + if (ata_cache_commit(drv) < 0) + return -1; buffer_off += size; } return buffer_off; diff --git a/tools/unit-tests/Makefile b/tools/unit-tests/Makefile index 3ac421e2b6..e6ccccf794 100644 --- a/tools/unit-tests/Makefile +++ b/tools/unit-tests/Makefile @@ -132,6 +132,7 @@ TESTS+=unit-otp-keystore-gen-zeroize TESTS+=unit-x86-paging-oob TESTS+=unit-ahci-unlock-panic TESTS+=unit-ata-security-passphrase-zeroize +TESTS+=unit-ata-sector-io TESTS+=unit-fwtpm-nv-oob TESTS+=unit-elf-bss-guard TESTS+=unit-elf-entry-inplace @@ -1619,6 +1620,10 @@ unit-ata-security-passphrase-zeroize: ../../include/target.h unit-ata-security-p gcc -o $@ unit-ata-security-passphrase-zeroize.c $(CFLAGS) \ -ffunction-sections -fdata-sections $(LDFLAGS) -Wl,--gc-sections +unit-ata-sector-io: ../../include/target.h unit-ata-sector-io.c ../../src/x86/ata.c + gcc -o $@ unit-ata-sector-io.c $(CFLAGS) \ + -ffunction-sections -fdata-sections $(LDFLAGS) -Wl,--gc-sections + # unit-flash-write-cc26x2 includes hal/cc26x2.c directly, with cc26x2_ti_stub/ # standing in for the (not vendored) TI CC26x2 SDK headers it includes. This # is also the only build coverage hal/cc26x2.c has. diff --git a/tools/unit-tests/unit-ata-sector-io.c b/tools/unit-tests/unit-ata-sector-io.c new file mode 100644 index 0000000000..18b28d1304 --- /dev/null +++ b/tools/unit-tests/unit-ata-sector-io.c @@ -0,0 +1,218 @@ +/* unit-ata-sector-io.c + * + * Unit tests for the AHCI sector read and write commands in src/x86/ata.c: + * a write at or past LBA 2^24 uses the 48-bit WRITE DMA EXT command, so its + * upper LBA bits reach the drive, and a command that ends in a task-file error is + * reported to the caller, including the read or write-back of a partial + * sector. + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#include +#include +#include +#include +#include +#include + +#define TEST_AHCI_BASE 0x10000 + +/* Port registers: every command completes at once, except the + * mock_fail_issue-th one (counting from 1), which stays pending in CI while + * IS reports a task-file error. */ +static int mock_fail_issue; +static int mock_issues; +static int mock_port_error; +static uint32_t mock_ci; + +uint32_t mmio_read32(uintptr_t address) +{ + if (address == AHCI_PxCI(TEST_AHCI_BASE, 0)) + return mock_ci; + if (address == AHCI_PxIS(TEST_AHCI_BASE, 0)) + return mock_port_error ? AHCI_PORT_IS_TFES : 0; + return 0; +} + +void mmio_write32(uintptr_t address, uint32_t value) +{ + if (address == AHCI_PxCI(TEST_AHCI_BASE, 0)) { + if (++mock_issues == mock_fail_issue) { + mock_port_error = 1; + mock_ci = value; + } + else { + mock_port_error = 0; + mock_ci = 0; + } + } +} + +void panic(void) +{ + ck_abort_msg("panic!"); +} + +#include "../../src/x86/ata.c" + +/* struct ata_drive keeps 32-bit DMA addresses, so the command list and + * table must sit below 4 GB on a 64-bit test host. */ +static uint8_t *clb_mem; +static uint8_t *ctable_mem; +static uint8_t sector[1024]; + +static void setup(void) +{ + mock_fail_issue = 0; + mock_issues = 0; + mock_port_error = 0; + mock_ci = 0; + clb_mem = mmap(NULL, sizeof(struct hba_cmd_header) * 32, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0); + ck_assert_ptr_ne(clb_mem, MAP_FAILED); + ctable_mem = mmap(NULL, sizeof(struct hba_cmd_table) * 32, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0); + ck_assert_ptr_ne(ctable_mem, MAP_FAILED); + + memset(&ATA_Drv[0], 0, sizeof(ATA_Drv[0])); + ATA_Drv[0].ahci_base = TEST_AHCI_BASE; + ATA_Drv[0].ahci_port = 0; + ATA_Drv[0].clb_port = (uint32_t)(uintptr_t)clb_mem; + ATA_Drv[0].ctable_port = (uint32_t)(uintptr_t)ctable_mem; + ATA_Drv[0].sector_size_shift = 9; + ATA_Drv[0].cached = CACHE_INVALID; + ata_drive_count = 0; +} + +static void teardown(void) +{ + munmap(clb_mem, sizeof(struct hba_cmd_header) * 32); + munmap(ctable_mem, sizeof(struct hba_cmd_table) * 32); +} + +static const struct fis_reg_h2d *issued_fis(void) +{ + const struct hba_cmd_header *cmd = (const struct hba_cmd_header *)clb_mem; + const struct hba_cmd_table *tbl = + (const struct hba_cmd_table *)(uintptr_t)cmd->ctba; + + return (const struct fis_reg_h2d *)(&tbl->cfis); +} + +START_TEST(test_write_past_lba24_uses_48bit_command) +{ + const struct fis_reg_h2d *fis; + + /* LBA 0x10123456: a 28-bit command takes bits 27:24 from the device + * field (0x40) and the write would land at 0x123456 */ + ck_assert_int_eq(ata_drive_write_sector(0, 0x10123456ULL, 1, sector), + 512); + fis = issued_fis(); + ck_assert_uint_eq(fis->command, ATA_CMD_WRITE_DMA_EX); + ck_assert_uint_eq(fis->lba0, 0x56); + ck_assert_uint_eq(fis->lba1, 0x34); + ck_assert_uint_eq(fis->lba2, 0x12); + ck_assert_uint_eq(fis->lba3, 0x10); + ck_assert_uint_eq(fis->count, 1); +} +END_TEST + +START_TEST(test_read_uses_48bit_command) +{ + ck_assert_int_eq(ata_drive_read_sector(0, 0x10123456ULL, 1, sector), 512); + ck_assert_uint_eq(issued_fis()->command, ATA_CMD_READ_DMA_EX); + ck_assert_uint_eq(issued_fis()->lba3, 0x10); +} +END_TEST + +START_TEST(test_port_error_is_reported) +{ + mock_fail_issue = 1; + ck_assert_int_lt(ata_drive_write_sector(0, 100, 1, sector), 0); + + mock_issues = 0; + ck_assert_int_lt(ata_drive_read_sector(0, 100, 1, sector), 0); +} +END_TEST + +START_TEST(test_partial_sector_write_error_is_reported) +{ + /* a partial sector is read into the cache (issue 1) and written back + * (issue 2); a failed write-back fails the call */ + mock_fail_issue = 2; + ck_assert_int_lt(ata_drive_write(0, 512 * 100 + 16, 32, sector), 0); + ck_assert(ATA_Drv[0].cached == CACHE_INVALID); + + /* a trailing partial sector after whole sectors: issue 1 writes the + * whole sector, 2 reads the tail sector, 3 writes it back */ + mock_issues = 0; + mock_fail_issue = 3; + ck_assert_int_lt(ata_drive_write(0, 512 * 100, 512 + 32, sector), 0); + + mock_issues = 0; + mock_fail_issue = 0; + ck_assert_int_eq(ata_drive_write(0, 512 * 100 + 16, 32, sector), 32); +} +END_TEST + +START_TEST(test_partial_sector_read_error_is_reported) +{ + /* a sector is cached, then a read of one whole sector (issue 1) plus a + * trailing partial sector (issue 2) fails on the partial sector */ + ck_assert_int_eq(ata_drive_read(0, 512 * 50 + 16, 32, sector), 32); + mock_issues = 0; + mock_fail_issue = 2; + ck_assert_int_lt(ata_drive_read(0, 512 * 100, 512 + 32, sector), 0); + + /* and a leading partial sector */ + mock_issues = 0; + mock_fail_issue = 1; + ck_assert_int_lt(ata_drive_read(0, 512 * 200 + 16, 32, sector), 0); +} +END_TEST + +static Suite *ata_sector_io_suite(void) +{ + Suite *s = suite_create("ata-sector-io"); + TCase *tc = tcase_create("ata-sector-io"); + + tcase_add_checked_fixture(tc, setup, teardown); + tcase_add_test(tc, test_write_past_lba24_uses_48bit_command); + tcase_add_test(tc, test_read_uses_48bit_command); + tcase_add_test(tc, test_port_error_is_reported); + tcase_add_test(tc, test_partial_sector_write_error_is_reported); + tcase_add_test(tc, test_partial_sector_read_error_is_reported); + suite_add_tcase(s, tc); + return s; +} + +int main(void) +{ + int fails; + Suite *s = ata_sector_io_suite(); + SRunner *sr = srunner_create(s); + + srunner_run_all(sr, CK_NORMAL); + fails = srunner_ntests_failed(sr); + srunner_free(sr); + return fails; +} From c675edcbedb725e31282e05690c642ffb4fa22c7 Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 3 Nov 2026 14:39:38 -0800 Subject: [PATCH 07/17] nai_68int6: configure the D0, C22 and H6 pads as the stock BIOS does --- include/x86/gpio_config.h | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/include/x86/gpio_config.h b/include/x86/gpio_config.h index d4f9a660a6..c656c728ff 100644 --- a/include/x86/gpio_config.h +++ b/include/x86/gpio_config.h @@ -91,6 +91,7 @@ #define GPIO_GPPC_S5_CFG_OFF (0x750) #define GPIO_GPPC_S6_CFG_OFF (0x760) #define GPIO_GPPC_S7_CFG_OFF (0x770) +#define GPIO_GPPC_H6_CFG_OFF (0x7e0) #define GPIO_GPPC_H7_CFG_OFF (0x7f0) #define GPIO_GPPC_H8_CFG_OFF (0x800) #define GPIO_GPPC_H9_CFG_OFF (0x810) @@ -759,7 +760,7 @@ static const struct tgl_gpio_conf gpio_table_presilicon[] = { .gpio_rxinv = 0x00, .gpio_rxevconf = 0x02, .gpio_reset = GPIO_RESET_HOSTDEEPRESET}, - /* PAD C22 (DW0=0x44000102 DW1=0x00000024) */ + /* PAD C22 (DW0=0x42100102 DW1=0x00003024) */ {.gpio = { .comm_port_id = GPIO_COMM_4_PORT_ID, @@ -771,10 +772,10 @@ static const struct tgl_gpio_conf gpio_table_presilicon[] = { GPIO_SET_RXEVCONF), .gpio_mode = GPIO_MODE_GPIO, .gpio_dir = GPIO_DIR_INPUT, - .gpio_term = GPIO_TERM_NONE, - .gpio_interrupt = 0x00, + .gpio_term = 0x0C, + .gpio_interrupt = 0x08, .gpio_rxinv = 0x00, - .gpio_rxevconf = 0x02, + .gpio_rxevconf = 0x01, .gpio_reset = GPIO_RESET_HOSTDEEPRESET}, /* PAD B10 (DW0=0x44000300 DW1=0x00000022) */ {.gpio = @@ -861,7 +862,7 @@ static const struct tgl_gpio_conf gpio_table_presilicon[] = { .gpio_rxinv = 0x00, .gpio_rxevconf = 0x02, .gpio_reset = GPIO_RESET_PLTRST}, - /* PAD D0 (DW0=0x84000102 DW1=0x0000002C) */ + /* PAD D0 (DW0=0x82100102 DW1=0x0000002C) */ {.gpio = { .comm_port_id = GPIO_COMM_1_PORT_ID, @@ -874,9 +875,9 @@ static const struct tgl_gpio_conf gpio_table_presilicon[] = { .gpio_mode = GPIO_MODE_GPIO, .gpio_dir = GPIO_DIR_INPUT, .gpio_term = GPIO_TERM_NONE, - .gpio_interrupt = 0x00, + .gpio_interrupt = 0x08, .gpio_rxinv = 0x00, - .gpio_rxevconf = 0x02, + .gpio_rxevconf = 0x01, .gpio_reset = GPIO_RESET_PLTRST}, /* PAD D2: held low; the OS drives it high for the IPMC */ {.gpio = @@ -1101,6 +1102,24 @@ static const struct tgl_gpio_conf gpio_table_presilicon[] = { .gpio_rxinv = 0x00, .gpio_rxevconf = 0x02, .gpio_reset = GPIO_RESET_HOSTDEEPRESET}, + /* PAD H6 (DW0=0x84000200 DW1=0x0000001A) */ + {.gpio = + { + .comm_port_id = GPIO_COMM_1_PORT_ID, + .cfg_offset = GPIO_GPPC_H6_CFG_OFF, + }, + .flags = + (GPIO_SET_MODE | GPIO_SET_DIRECTION | GPIO_SET_RESET | + GPIO_SET_TERM | GPIO_SET_INTERRUPT | GPIO_SET_RXINV | + GPIO_SET_RXEVCONF | GPIO_SET_TX), + .gpio_mode = GPIO_MODE_GPIO, + .gpio_dir = GPIO_DIR_OUTPUT, + .gpio_term = GPIO_TERM_NONE, + .gpio_interrupt = 0x00, + .gpio_rxinv = 0x00, + .gpio_rxevconf = 0x02, + .gpio_tx = 0, + .gpio_reset = GPIO_RESET_PLTRST}, /* PAD H7 (DW0=0x84000201 DW1=0x0000001B) USB power enable */ {.gpio = { From 6e76bd022c6ea893a0b93dc3f04bd82ebffe5895 Mon Sep 17 00:00:00 2001 From: David Garske Date: Wed, 4 Nov 2026 19:06:47 -0800 Subject: [PATCH 08/17] x86 fsp: time the SPI hardware-sequencing waits from the TSC --- hal/x86_fsp_tgl.c | 24 +++++++++++++----------- include/x86/tgl_fsp.h | 2 ++ src/x86/tgl_fsp.c | 6 +++--- tools/unit-tests/unit-tgl-spi-hwseq.c | 12 ++++++++++-- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/hal/x86_fsp_tgl.c b/hal/x86_fsp_tgl.c index 8dae2231e6..27953d2f41 100644 --- a/hal/x86_fsp_tgl.c +++ b/hal/x86_fsp_tgl.c @@ -265,31 +265,33 @@ static int tgl_spi_cycle(uint32_t spi_bar, uint32_t cycle, uint32_t faddr, uint32_t len) { uint32_t sts = 0; - int ms; + uint64_t deadline; - for (ms = 0; ms < SPI_HWSEQ_TIMEOUT_MS; ms++) { - if ((mmio_read32(spi_bar + SPI_BIOS_HSFSTS_CTL) & - SPI_HSFSTS_SCIP) == 0) - break; - delay(1); + /* Timed from the TSC: a 4 KB erase can take a few hundred ms */ + deadline = tgl_rdtsc() + (uint64_t)tgl_tsc_per_ms() * SPI_HWSEQ_TIMEOUT_MS; + while (mmio_read32(spi_bar + SPI_BIOS_HSFSTS_CTL) & SPI_HSFSTS_SCIP) { + if (tgl_rdtsc() > deadline) + return -1; } - if (ms == SPI_HWSEQ_TIMEOUT_MS) - return -1; mmio_write32(spi_bar + SPI_BIOS_HSFSTS_CTL, SPI_HSFSTS_FDONE | SPI_HSFSTS_FCERR | SPI_HSFSTS_AEL); mmio_write32(spi_bar + SPI_FADDR, faddr); mmio_write32(spi_bar + SPI_BIOS_HSFSTS_CTL, ((len - 1) << SPI_HSFSTS_FDBC_SHIFT) | (cycle << SPI_HSFSTS_FCYCLE_SHIFT) | SPI_HSFSTS_FGO); - for (ms = 0; ms < SPI_HWSEQ_TIMEOUT_MS; ms++) { + deadline = tgl_rdtsc() + (uint64_t)tgl_tsc_per_ms() * SPI_HWSEQ_TIMEOUT_MS; + for (;;) { sts = mmio_read32(spi_bar + SPI_BIOS_HSFSTS_CTL); if (sts & (SPI_HSFSTS_FDONE | SPI_HSFSTS_FCERR)) break; - delay(1); + if (tgl_rdtsc() > deadline) { + sts = SPI_HSFSTS_FCERR; + break; + } } mmio_write32(spi_bar + SPI_BIOS_HSFSTS_CTL, SPI_HSFSTS_FDONE | SPI_HSFSTS_FCERR | SPI_HSFSTS_AEL); - if (ms == SPI_HWSEQ_TIMEOUT_MS || (sts & SPI_HSFSTS_FCERR)) + if (sts & SPI_HSFSTS_FCERR) return -1; return 0; } diff --git a/include/x86/tgl_fsp.h b/include/x86/tgl_fsp.h index 2dfcf380ea..4f95dd1d5f 100644 --- a/include/x86/tgl_fsp.h +++ b/include/x86/tgl_fsp.h @@ -25,6 +25,8 @@ int x86_fsp_tgl_init_sata(uint32_t *bar); int tgl_lock_bios_region(void); +uint64_t tgl_rdtsc(void); +uint32_t tgl_tsc_per_ms(void); int tgl_spi_flash_erase(uint32_t address, uint32_t len); int tgl_spi_flash_write(uint32_t address, const uint8_t *buf, uint32_t len); diff --git a/src/x86/tgl_fsp.c b/src/x86/tgl_fsp.c index 63f5c60d94..55a1da6ae4 100644 --- a/src/x86/tgl_fsp.c +++ b/src/x86/tgl_fsp.c @@ -1610,8 +1610,7 @@ static void tgl_gpio_configure(const struct tgl_gpio_conf *gpio) } } -#ifdef TARGET_nai_68int6 -static uint64_t tgl_rdtsc(void) +uint64_t tgl_rdtsc(void) { uint32_t lo, hi; @@ -1621,7 +1620,7 @@ static uint64_t tgl_rdtsc(void) /* TSC ticks per millisecond: CPUID 0x15 gives the crystal clock and the * TSC/crystal ratio, CPUID 0x16 the base frequency in MHz. */ -static uint32_t tgl_tsc_per_ms(void) +uint32_t tgl_tsc_per_ms(void) { uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0; @@ -1634,6 +1633,7 @@ static uint32_t tgl_tsc_per_ms(void) return 2000000; } +#ifdef TARGET_nai_68int6 static void tgl_delay_ms(uint32_t ms) { uint64_t end = tgl_rdtsc() + (uint64_t)tgl_tsc_per_ms() * ms; diff --git a/tools/unit-tests/unit-tgl-spi-hwseq.c b/tools/unit-tests/unit-tgl-spi-hwseq.c index f24c2e0896..f06687f231 100644 --- a/tools/unit-tests/unit-tgl-spi-hwseq.c +++ b/tools/unit-tests/unit-tgl-spi-hwseq.c @@ -103,9 +103,17 @@ void pci_config_write8(uint8_t bus, uint8_t dev, uint8_t fun, uint8_t off, g_bios_cntl = g_wpd_stuck_low ? (uint8_t)(val & ~1U) : val; } -void delay(int msec) +/* A TSC that advances on every read, one tick per ms */ +static uint64_t g_tsc; + +uint64_t tgl_rdtsc(void) +{ + return ++g_tsc; +} + +uint32_t tgl_tsc_per_ms(void) { - (void)msec; + return 1; } static void run_cycle(uint32_t ctl) From 27323f37e0dd644dedd931c672a09c9bb3c7715d Mon Sep 17 00:00:00 2001 From: David Garske Date: Wed, 4 Nov 2026 19:06:47 -0800 Subject: [PATCH 09/17] nai_68int6: add an FSP-M platform debug consent build option --- config/examples/nai_68int6.config | 4 ++++ docs/Targets.md | 1 + include/x86/fsp_config.h | 9 +++++++++ options.mk | 2 +- src/x86/tgl_fsp.c | 1 + 5 files changed, 16 insertions(+), 1 deletion(-) diff --git a/config/examples/nai_68int6.config b/config/examples/nai_68int6.config index b99f04b809..d0658ad4f3 100644 --- a/config/examples/nai_68int6.config +++ b/config/examples/nai_68int6.config @@ -84,6 +84,10 @@ NAI_68INT6_PCIE_LINK_DELAY_S=0 NAI_68INT6_BOOT_DELAY_S=0 NAI_68INT6_FPGA_READY_DELAY_S=0 +# Debug consent for a JTAG probe: 0 = off (keep 0 in a shipping image), +# 4 = XDP/MIPI-60 +NAI_68INT6_DEBUG_CONSENT=0 + ########## Payload: signed 64-bit Linux bzImage ########## 64BIT=1 ELF=1 diff --git a/docs/Targets.md b/docs/Targets.md index ea691f05fa..ba5596c599 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -9230,6 +9230,7 @@ Notable options in [config/examples/nai_68int6.config](../config/examples/nai_68 | `NAI_68INT6_PCIE_LANES` | PCIe switch lane configuration, strapped on GPP_B21/B22/S4/S7/S6 before memory init: `8x1` (default), `1x4_4x1`, `4x1_1x4`, `1x4_1x4` or `1x8` | | `NAI_68INT6_PCIE_NT`, `NAI_68INT6_BACKPLANE_CLK` | PCIe switch non-transparent mode (GPP_S2) and the backplane PCIe clock (GPP_A0); both off by default | | `NAI_68INT6_PCIE_LINK_DELAY_S`, `NAI_68INT6_BOOT_DELAY_S`, `NAI_68INT6_FPGA_READY_DELAY_S` | Waits before CPU PCIe link training (0-5 s), before polling PCIE_RDY (0-255 s), and added to the 5 s PCIE_RDY timeout (0, 5 or 10 s); all 0 by default. Before PCI enumeration wolfBoot polls PCIE_RDY (GPP_C23) every 50 ms, continues with a warning on timeout, then drives GPP_B12 high | +| `NAI_68INT6_DEBUG_CONSENT` | FSP-M `PlatformDebugConsent`: 0 = debug disabled (default; keep it for a shipping image), 4 = enabled for an XDP/MIPI-60 JTAG probe | | `ANTI_ROLLBACK=1` | Persistent version floor and boot audit counters in TPM NV | | `MEASURED_BOOT=1`, `MEASURED_PCR_OS=4` | Firmware into PCR 0, the verified OS image into PCR 4 | diff --git a/include/x86/fsp_config.h b/include/x86/fsp_config.h index a579bb976d..5f6f8bda99 100644 --- a/include/x86/fsp_config.h +++ b/include/x86/fsp_config.h @@ -66,4 +66,13 @@ #error "NAI_68INT6_FPGA_READY_DELAY_S must be 0, 5 or 10" #endif +/* FSP-M PlatformDebugConsent: 0 = debug disabled (default), 4 = enabled for + * an XDP/MIPI-60 JTAG probe; 1-7 are the FSP's other probe types */ +#ifndef NAI_68INT6_DEBUG_CONSENT +#define NAI_68INT6_DEBUG_CONSENT 0 +#endif +#if NAI_68INT6_DEBUG_CONSENT < 0 || NAI_68INT6_DEBUG_CONSENT > 7 +#error "NAI_68INT6_DEBUG_CONSENT must be 0 to 7" +#endif + #endif diff --git a/options.mk b/options.mk index 506a2ab0e9..a5294bdff7 100644 --- a/options.mk +++ b/options.mk @@ -72,7 +72,7 @@ ifeq ($(TARGET),nai_68int6) CFLAGS+=-DNAI_68INT6_PCIE_LANES_$(NAI_68INT6_PCIE_LANES) $(foreach option,FSP_HYPERTHREADING NAI_68INT6_PCIE_NT NAI_68INT6_BACKPLANE_CLK \ NAI_68INT6_PCIE_LINK_DELAY_S NAI_68INT6_BOOT_DELAY_S \ - NAI_68INT6_FPGA_READY_DELAY_S, \ + NAI_68INT6_FPGA_READY_DELAY_S NAI_68INT6_DEBUG_CONSENT, \ $(if $($(option)),$(eval CFLAGS+=-D$(option)=$($(option))))) endif diff --git a/src/x86/tgl_fsp.c b/src/x86/tgl_fsp.c index 55a1da6ae4..e7eb9d1127 100644 --- a/src/x86/tgl_fsp.c +++ b/src/x86/tgl_fsp.c @@ -421,6 +421,7 @@ static int fsp_set_memory_cfg(FSPM_UPD *udp) #endif /* TARGET_kontron_vx3060_s2 */ #ifdef TARGET_nai_68int6 mem_cfg->VmxEnable = 1; + mem_cfg->PlatformDebugConsent = NAI_68INT6_DEBUG_CONSENT; #else mem_cfg->VmxEnable = 0; #endif From c9a41c5cdfba3e96192b967be7840ac2604984b4 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 5 Nov 2026 15:34:37 -0800 Subject: [PATCH 10/17] elf: reject p_filesz above p_memsz and sizes the map callback truncates --- src/elf.c | 16 ++++++-- tools/unit-tests/unit-elf-mmu-fail.c | 59 ++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/src/elf.c b/src/elf.c index 8e7f196634..4508abad35 100644 --- a/src/elf.c +++ b/src/elf.c @@ -162,9 +162,10 @@ int elf_load_image_mmu(uint8_t *image, uint32_t image_sz, uintptr_t *pentry, continue; } - /* Validate segment data is within image bounds */ - if (file_size > 0 && - (offset >= image_sz || file_size > image_sz - offset)) { + /* Validate segment data is within image bounds and fits the + * segment: the copy below writes file_size bytes at vaddr. */ + if (file_size > mem_size || (file_size > 0 && + (offset >= image_sz || file_size > image_sz - offset))) { return -4; /* segment offset/size out of bounds */ } @@ -187,7 +188,14 @@ int elf_load_image_mmu(uint8_t *image, uint32_t image_sz, uintptr_t *pentry, #ifndef ELF_PARSER if (mmu_cb != NULL) { - if (mmu_cb(vaddr, paddr, mem_size) != 0) { + int map_ret = -1; + /* The callback takes a 32-bit size; a larger segment would be + * mapped and checked shorter than it is written. */ +#if UINTPTR_MAX > 0xFFFFFFFFUL + if (mem_size <= 0xFFFFFFFFUL) +#endif + map_ret = mmu_cb(vaddr, paddr, (uint32_t)mem_size); + if (map_ret != 0) { /* Never silently drop a PT_LOAD segment: a failed mapping * leaves a hole in the image, and publishing the entry * point for a partially loaded ELF is worse than failing diff --git a/tools/unit-tests/unit-elf-mmu-fail.c b/tools/unit-tests/unit-elf-mmu-fail.c index e61c830706..b4d7cc03b0 100644 --- a/tools/unit-tests/unit-elf-mmu-fail.c +++ b/tools/unit-tests/unit-elf-mmu-fail.c @@ -69,6 +69,17 @@ static int mmu_cb_ok(uint64_t vaddr, uint64_t paddr, uint32_t size) return 0; } +static int g_map_calls; + +static int mmu_cb_count(uint64_t vaddr, uint64_t paddr, uint32_t size) +{ + (void)vaddr; + (void)paddr; + (void)size; + g_map_calls++; + return 0; +} + /* Build a minimal ELF64 with two 8-byte PT_LOAD segments, with their * data at image offsets 256 and 264, mapped onto g_target. */ static void build_elf(void) @@ -152,12 +163,60 @@ START_TEST(test_successful_mappings_still_load) } END_TEST +/* p_filesz above p_memsz would copy past the range the callback checked. */ +START_TEST(test_filesz_above_memsz_is_rejected) +{ + elf64_program_header *ph; + uintptr_t entry = 0; + int ret; + + build_elf(); + ph = (elf64_program_header *)(g_image + sizeof(elf64_header)); + ph[1].file_size = 16; + memset(g_image + 272, 0x3C, 8); + + ret = elf_load_image_mmu(g_image, sizeof(g_image), &entry, mmu_cb_ok); + + ck_assert_int_ne(ret, 0); + ck_assert_msg(entry == 0, "entry point published for p_filesz > p_memsz"); + ck_assert_uint_eq(g_target[24], 0); +} +END_TEST + +/* A p_memsz the 32-bit callback size cannot hold must be rejected before + * the callback sees a truncated size and before anything is written. */ +START_TEST(test_memsz_above_callback_size_is_rejected) +{ + elf64_program_header *ph; + uintptr_t entry = 0; + int ret; + + if (sizeof(uintptr_t) <= 4) + return; + + build_elf(); + ph = (elf64_program_header *)(g_image + sizeof(elf64_header)); + ph[0].mem_size = 0x100000000ULL + 8; + g_map_calls = 0; + + ret = elf_load_image_mmu(g_image, sizeof(g_image), &entry, + mmu_cb_count); + + ck_assert_int_ne(ret, 0); + ck_assert_int_eq(g_map_calls, 0); + ck_assert_msg(entry == 0, "entry point published for a 4 GB segment"); + ck_assert_uint_eq(g_target[0], 0); +} +END_TEST + Suite *elf_mmu_fail_suite(void) { Suite *s = suite_create("ELF mmu failure"); TCase *tc = tcase_create("mmu-cb-failure"); tcase_add_test(tc, test_mmu_failure_aborts_load); tcase_add_test(tc, test_successful_mappings_still_load); + tcase_add_test(tc, test_filesz_above_memsz_is_rejected); + tcase_add_test(tc, test_memsz_above_callback_size_is_rejected); tcase_set_timeout(tc, 10); suite_add_tcase(s, tc); return s; From 6c3f439ab31ca093ee90d158d7681247100c88ad Mon Sep 17 00:00:00 2001 From: David Garske Date: Wed, 11 Nov 2026 09:56:32 -0800 Subject: [PATCH 11/17] x86 fsp: mark a boot with cached training data until FSP-S succeeds --- docs/Targets.md | 2 +- include/x86/fsp_nvs.h | 4 +- src/boot_x86_fsp.c | 5 +-- src/x86/fsp.c | 9 +++++ src/x86/fsp_nvs.c | 45 ++++++++++++++++++----- tools/unit-tests/unit-fsp-nvs-save.c | 12 ++++++ tools/unit-tests/unit-fsp-nvs.c | 55 ++++++++++++++++++++++++++-- 7 files changed, 112 insertions(+), 20 deletions(-) diff --git a/docs/Targets.md b/docs/Targets.md index ba5596c599..012d8fa325 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -9223,7 +9223,7 @@ Notable options in [config/examples/nai_68int6.config](../config/examples/nai_68 |---|---| | `X86_UART_NUMBER=2` | LPSS UART2 for all three FSP phases (Kontron uses 0) | | `ACPI=1` | Required. Generates ACPI tables (RSDP/XSDT/FADT/MADT/MCFG/TPM2/HPET/DMAR and a minimal DSDT with the TPM device); the board's PCI interrupt routing is only in the DSDT `_PRT`, since the MP table is the Kontron one. The DSDT also declares the board devices on LPSS I2C0 (EEPROMs, RTC/watchdog) and GSPI0 (the PRC, for NAI's `naigspi` driver) | -| `FSP_NVS_CACHE=1`, `FSP_NVS_BASE`, `FSP_NVS_SIZE` | Keeps FSP-M's memory training data (its non-volatile storage HOB) in a 128 KB area of the BIOS region below the wolfBoot window, so later boots skip full memory training. Stage 1 passes a valid copy back through `NvsBufferPtr`; stage 2 rewrites it, before FSP-S and before the BIOS region is locked, only when it changes. The record is keyed on a CRC of the FSP-M image and its UPDs, so a change to either falls back to full training. If FspMemInit fails with a saved record, stage 1 sets a CMOS flag and cold-resets, boots train from scratch until FspMemInit succeeds, and stage 2 replaces the record. `assemble_image.sh -n` blanks the area, so an image built from a board's flash dump does not carry that board's training data | +| `FSP_NVS_CACHE=1`, `FSP_NVS_BASE`, `FSP_NVS_SIZE` | Keeps FSP-M's memory training data (its non-volatile storage HOB) in a 128 KB area of the BIOS region below the wolfBoot window, so later boots skip full memory training. Stage 1 passes a valid copy back through `NvsBufferPtr`; stage 2 rewrites it, before FSP-S and before the BIOS region is locked, only when it changes. The record is keyed on a CRC of the FSP-M image and its UPDs, so a change to either falls back to full training. Stage 1 sets a CMOS flag before FspMemInit runs with a saved record, an FSP-requested reset clears it, and stage 2 clears it once FspSiliconInit succeeds. If FspMemInit fails with the record, or a boot finds the flag still set because the previous boot crashed or hung with the record, boots train from scratch until one gets through FspSiliconInit, and stage 2 replaces the record. The flag is kept in CMOS, so it survives a reset but needs the RTC battery to survive loss of power. `assemble_image.sh -n` blanks the area, so an image built from a board's flash dump does not carry that board's training data | | `FSP_HYPERTHREADING=1` | Hyper-Threading on (off by default). The MADT and the MP table list the enabled logical processors from the CPUID topology, so either setting works | | `MP_CPU_NUM_ENTRY=8` | MP table processor entries, one per logical processor (default 4) | | `WOLFBOOT_CMDLINE_MAX` | Buffer for a signed `HDR_CMDLINE`, including the terminating NUL (default 256); a longer command line is ignored and the built-in one is used | diff --git a/include/x86/fsp_nvs.h b/include/x86/fsp_nvs.h index 920ab607a3..af5e68d660 100644 --- a/include/x86/fsp_nvs.h +++ b/include/x86/fsp_nvs.h @@ -42,7 +42,9 @@ int fsp_nvs_record_valid(const struct fsp_nvs_record *rec, uint32_t key, uint32_t area_size); int fsp_nvs_apply(uint8_t *udp_m, uint32_t udp_size, const uint8_t *fsp_m, uint32_t fsp_m_size); -void fsp_nvs_skip(int skip); +void fsp_nvs_reject(void); +void fsp_nvs_before_reset(void); +void fsp_nvs_boot_ok(void); void fsp_nvs_save(void); #endif /* FSP_NVS_H */ diff --git a/src/boot_x86_fsp.c b/src/boot_x86_fsp.c index 79c2ec23fb..c69d3435eb 100644 --- a/src/boot_x86_fsp.c +++ b/src/boot_x86_fsp.c @@ -636,15 +636,12 @@ void start(uint32_t stack_base, uint32_t stack_top, uint64_t timestamp, wolfBoot_printf("failed: 0x%x" ENDLINE, status); #if defined(WOLFBOOT_FSP_NVS_CACHE) && defined(BUILD_LOADER_STAGE1) if (nvs_used) { - fsp_nvs_skip(1); + fsp_nvs_reject(); reset(0); } #endif panic(); } -#if defined(WOLFBOOT_FSP_NVS_CACHE) && defined(BUILD_LOADER_STAGE1) - fsp_nvs_skip(0); -#endif wolfBoot_printf("success" ENDLINE); status = get_top_address(&top_address, hobList); diff --git a/src/x86/fsp.c b/src/x86/fsp.c index 35540b8ca7..9c37929b37 100644 --- a/src/x86/fsp.c +++ b/src/x86/fsp.c @@ -111,6 +111,12 @@ void print_fsp_image_revision(struct fsp_info_header *h) * left to the caller, so this returns in those cases. */ void fsp_handle_reset(uint32_t status) { +#ifdef WOLFBOOT_FSP_NVS_CACHE + if (status == FSP_STATUS_RESET_REQUIRED_WARM || + status == FSP_STATUS_RESET_REQUIRED_COLD || + status == FSP_STATUS_RESET_REQUIRED_3) + fsp_nvs_before_reset(); +#endif if (status == FSP_STATUS_RESET_REQUIRED_WARM) { wolfBoot_printf("fsp: warm reset required\n"); reset(1); @@ -196,6 +202,9 @@ void fsp_init_silicon(void) panic(); } wolfBoot_printf("success"); +#if defined(WOLFBOOT_FSP_NVS_CACHE) && !defined(BUILD_LOADER_STAGE1) + fsp_nvs_boot_ok(); +#endif status = fsp_pre_pci_enum_cb(); if (status != 0) panic(); diff --git a/src/x86/fsp_nvs.c b/src/x86/fsp_nvs.c index 24be9cfdee..85bd852ff9 100644 --- a/src/x86/fsp_nvs.c +++ b/src/x86/fsp_nvs.c @@ -78,17 +78,33 @@ int fsp_nvs_record_valid(const struct fsp_nvs_record *rec, uint32_t key, return fsp_nvs_crc32(0, rec->data, rec->len) == rec->crc; } +/* CMOS byte tracking the saved record across boots. Stage 1 sets IN_USE + * before FspMemInit runs with the record, and stage 2 clears it once + * FspSiliconInit has succeeded. A boot that finds IN_USE still set crashed or + * hung with the record, so it sets SKIP: boots then train from scratch, across + * the resets FSP requests, until FspSiliconInit succeeds and stage 2 has + * replaced the record. Stage 1 runs from SPI flash, so it cannot erase the + * record itself. */ +#define FSP_NVS_STATE_CMOS_IDX 0x52 +#define FSP_NVS_STATE_SKIP 0xA5 +#define FSP_NVS_STATE_IN_USE 0x5A + +void fsp_nvs_before_reset(void) +{ + /* An FSP-requested reset is part of a normal boot with the record */ + if (cmos_read(FSP_NVS_STATE_CMOS_IDX) == FSP_NVS_STATE_IN_USE) + cmos_write(FSP_NVS_STATE_CMOS_IDX, 0); +} + +void fsp_nvs_boot_ok(void) +{ + cmos_write(FSP_NVS_STATE_CMOS_IDX, 0); +} + #ifdef BUILD_LOADER_STAGE1 -/* CMOS byte set when FspMemInit failed with the saved record. Boots train - * from scratch until FspMemInit succeeds without it, across the resets FSP-M - * requests, and stage 2 then replaces the record. Stage 1 runs from SPI - * flash, so it cannot erase the record itself. */ -#define FSP_NVS_SKIP_CMOS_IDX 0x52 -#define FSP_NVS_SKIP_CMOS_MAGIC 0xA5 - -void fsp_nvs_skip(int skip) +void fsp_nvs_reject(void) { - cmos_write(FSP_NVS_SKIP_CMOS_IDX, skip ? FSP_NVS_SKIP_CMOS_MAGIC : 0); + cmos_write(FSP_NVS_STATE_CMOS_IDX, FSP_NVS_STATE_SKIP); } /* Returns 1 when FspMemInit is given the saved record. */ @@ -99,6 +115,7 @@ int fsp_nvs_apply(uint8_t *udp_m, uint32_t udp_size, const uint8_t *fsp_m, (const struct fsp_nvs_record *)(uintptr_t)FSP_NVS_BASE; FSPM_ARCH_UPD *arch = (FSPM_ARCH_UPD *)(udp_m + sizeof(FSP_UPD_HEADER)); uint32_t key; + uint8_t state; /* The key is taken before NvsBufferPtr and BootMode are changed, so * stage 2 can match it against the UPDs this boot trained with. It @@ -108,7 +125,14 @@ int fsp_nvs_apply(uint8_t *udp_m, uint32_t udp_size, const uint8_t *fsp_m, key = fsp_nvs_crc32(key, udp_m, udp_size); stage2_get_parameters()->fsp_nvs_key = key; - if (cmos_read(FSP_NVS_SKIP_CMOS_IDX) == FSP_NVS_SKIP_CMOS_MAGIC) { + state = cmos_read(FSP_NVS_STATE_CMOS_IDX); + if (state == FSP_NVS_STATE_IN_USE) { + wolfBoot_printf("FSP NVS: last boot with saved training data did not " + "finish\r\n"); + fsp_nvs_reject(); + state = FSP_NVS_STATE_SKIP; + } + if (state == FSP_NVS_STATE_SKIP) { wolfBoot_printf("FSP NVS: saved training data failed, retraining\r\n"); return 0; } @@ -116,6 +140,7 @@ int fsp_nvs_apply(uint8_t *udp_m, uint32_t udp_size, const uint8_t *fsp_m, wolfBoot_printf("FSP NVS: no saved training data\r\n"); return 0; } + cmos_write(FSP_NVS_STATE_CMOS_IDX, FSP_NVS_STATE_IN_USE); arch->NvsBufferPtr = (uint32_t)(uintptr_t)rec->data; arch->BootMode = BOOT_ASSUMING_NO_CONFIGURATION_CHANGES; wolfBoot_printf("FSP NVS: using saved training data (%d bytes)\r\n", diff --git a/tools/unit-tests/unit-fsp-nvs-save.c b/tools/unit-tests/unit-fsp-nvs-save.c index 4c02fa5385..e2027db906 100644 --- a/tools/unit-tests/unit-fsp-nvs-save.c +++ b/tools/unit-tests/unit-fsp-nvs-save.c @@ -69,6 +69,18 @@ int tgl_spi_flash_write(uint32_t address, const uint8_t *buf, uint32_t len) return 0; } +uint8_t cmos_read(uint8_t index) +{ + (void)index; + return 0; +} + +void cmos_write(uint8_t index, uint8_t value) +{ + (void)index; + (void)value; +} + #include "../../src/x86/hob.c" /* FSP_NVS_BASE is a RAM buffer here, not a flash address */ #undef BOOTLOADER_PARTITION_SIZE diff --git a/tools/unit-tests/unit-fsp-nvs.c b/tools/unit-tests/unit-fsp-nvs.c index 84b4c0612b..5449841041 100644 --- a/tools/unit-tests/unit-fsp-nvs.c +++ b/tools/unit-tests/unit-fsp-nvs.c @@ -91,7 +91,8 @@ static struct fsp_nvs_record *write_record(uint32_t key, uint32_t len) return rec; } -static void reset_udp(void) +/* fresh UPDs, as each boot starts from the FSP-M defaults; CMOS persists */ +static void next_boot(void) { uint32_t i; @@ -102,6 +103,11 @@ static void reset_udp(void) arch()->NvsBufferPtr = 0; arch()->BootMode = BOOT_WITH_FULL_CONFIGURATION; memset(&g_params, 0, sizeof(g_params)); +} + +static void reset_udp(void) +{ + next_boot(); memset(g_cmos, 0, sizeof(g_cmos)); } @@ -183,17 +189,56 @@ END_TEST START_TEST(test_apply_skips_a_rejected_record_until_cleared) { /* after FspMemInit failed with the record, boots train from scratch, - * across FSP-M's reset requests, until a FspMemInit succeeds */ + * across FSP's reset requests, until a boot gets through FspSiliconInit */ reset_udp(); write_record(expected_key(), 200); - fsp_nvs_skip(1); + fsp_nvs_reject(); ck_assert_int_eq(fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE), 0); ck_assert_uint_eq(arch()->NvsBufferPtr, 0); ck_assert_uint_eq(arch()->BootMode, BOOT_WITH_FULL_CONFIGURATION); ck_assert_uint_eq(g_params.fsp_nvs_key, expected_key()); + fsp_nvs_before_reset(); + next_boot(); ck_assert_int_eq(fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE), 0); - fsp_nvs_skip(0); + fsp_nvs_boot_ok(); + next_boot(); + ck_assert_int_eq(fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE), 1); +} +END_TEST + +START_TEST(test_unfinished_boot_with_record_retrains) +{ + /* a boot with the record that crashed or hung never cleared IN_USE */ + reset_udp(); + write_record(expected_key(), 200); + ck_assert_int_eq(fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE), 1); + next_boot(); + ck_assert_int_eq(fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE), 0); + ck_assert_uint_eq(arch()->NvsBufferPtr, 0); + ck_assert_uint_eq(arch()->BootMode, BOOT_WITH_FULL_CONFIGURATION); + + /* and the retraining boots keep skipping it across FSP resets */ + fsp_nvs_before_reset(); + next_boot(); + ck_assert_int_eq(fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE), 0); + fsp_nvs_boot_ok(); + next_boot(); + ck_assert_int_eq(fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE), 1); +} +END_TEST + +START_TEST(test_fsp_reset_keeps_using_the_record) +{ + /* a reset FSP asks for during a boot with the record is not a failure */ + reset_udp(); + write_record(expected_key(), 200); + ck_assert_int_eq(fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE), 1); + fsp_nvs_before_reset(); + next_boot(); + ck_assert_int_eq(fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE), 1); + fsp_nvs_boot_ok(); + next_boot(); ck_assert_int_eq(fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE), 1); } END_TEST @@ -230,6 +275,8 @@ static Suite *fsp_nvs_suite(void) tcase_add_test(tc, test_apply_with_matching_record); tcase_add_test(tc, test_apply_rejects_changed_config); tcase_add_test(tc, test_apply_skips_a_rejected_record_until_cleared); + tcase_add_test(tc, test_unfinished_boot_with_record_retrains); + tcase_add_test(tc, test_fsp_reset_keeps_using_the_record); suite_add_tcase(s, tc); return s; } From c96b3d7f57755be91e0d861f92b91d74f69e78c3 Mon Sep 17 00:00:00 2001 From: David Garske Date: Wed, 11 Nov 2026 15:09:48 -0800 Subject: [PATCH 12/17] x86 fsp: list only RAM and reserved memory in the Linux e820 map --- src/x86/linux_loader.c | 7 +++++ tools/unit-tests/unit-linux-loader-e820.c | 33 +++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/x86/linux_loader.c b/src/x86/linux_loader.c index 1fcf94a400..e1e7c34438 100644 --- a/src/x86/linux_loader.c +++ b/src/x86/linux_loader.c @@ -103,6 +103,13 @@ static int e820_add_entry_cb(uint64_t start, uint64_t length, uint32_t type, struct boot_params *bp = (struct boot_params*)ctx; struct boot_e820_entry *map; + /* Only memory goes in the e820 map. An MMIO range listed as reserved + * keeps the kernel from assigning PCI BARs in it, and an I/O port range + * is not memory at all. */ + if (type != EFI_RESOURCE_SYSTEM_MEMORY && + type != EFI_RESOURCE_MEMORY_RESERVED) + return 0; + if (bp->e820_entries >= E820_MAX_ENTRIES_ZEROPAGE) return -1; diff --git a/tools/unit-tests/unit-linux-loader-e820.c b/tools/unit-tests/unit-linux-loader-e820.c index 8651f01214..b1625a32c9 100644 --- a/tools/unit-tests/unit-linux-loader-e820.c +++ b/tools/unit-tests/unit-linux-loader-e820.c @@ -79,6 +79,39 @@ int main(void) return 1; } + /* Only RAM and reserved memory are listed: MMIO and I/O port ranges + * must not appear as reserved memory. */ + { + static const uint32_t types[] = { + EFI_RESOURCE_SYSTEM_MEMORY, EFI_RESOURCE_MEMORY_MAPPED_IO, + EFI_RESOURCE_IO, EFI_RESOURCE_MEMORY_MAPPED_IO_PORT, + EFI_RESOURCE_MEMORY_RESERVED, EFI_RESOURCE_IO_RESERVED + }; + unsigned int n = sizeof(types) / sizeof(types[0]); + + p = hoblist; + for (i = 0; i < (int)n; i++) { + rd = (struct efi_hob_resource_descriptor *)p; + rd->resource_type = types[i]; + rd->physical_start = 0x1000ULL * (i + 1); + rd->resource_length = 0x1000; + p += sizeof(struct efi_hob_resource_descriptor); + } + end = (struct efi_hob_generic_header *)p; + end->hob_type = EFI_HOB_TYPE_END_OF_HOB_LIST; + end->hob_length = sizeof(struct efi_hob_generic_header); + memset(&obj.bp, 0, sizeof(obj.bp)); + if (memory_map_from_hoblist(&obj.bp, (struct efi_hob *)hoblist) != 0 || + obj.bp.e820_entries != 2 || + obj.bp.e820_table[0].addr != 0x1000 || + obj.bp.e820_table[0].type != E820_TYPE_RAM || + obj.bp.e820_table[1].addr != 0x5000 || + obj.bp.e820_table[1].type != E820_TYPE_RESERVED) { + printf("FAIL: MMIO or I/O ranges listed in the e820 map\n"); + return 1; + } + } + printf("PASS\n"); return 0; } From 9715bf6c187129128a28de81c800acb448c99776 Mon Sep 17 00:00:00 2001 From: David Garske Date: Wed, 11 Nov 2026 15:09:48 -0800 Subject: [PATCH 13/17] nai_68int6: split the PCI BAR window into two 512 MB pools --- options.mk | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/options.mk b/options.mk index a5294bdff7..2dff5c6980 100644 --- a/options.mk +++ b/options.mk @@ -70,6 +70,12 @@ ifeq ($(TARGET),nai_68int6) $(error NAI_68INT6_PCIE_LANES must be one of: $(NAI_68INT6_PCIE_LANE_CONFIGS)) endif CFLAGS+=-DNAI_68INT6_PCIE_LANES_$(NAI_68INT6_PCIE_LANES) + # PCI BAR pools in the 0x80000000-0xBFFFFFFF window the DSDT advertises: + # 512 MB each, so a large BAR behind the PCIe switch leaves room for the + # devices enumerated after it + CFLAGS+=-DPCI_MMIO32_BASE=0x80000000ULL -DPCI_MMIO32_LENGTH=0x20000000U + CFLAGS+=-DPCI_MMIO32_PREFETCH_BASE=0xA0000000ULL + CFLAGS+=-DPCI_MMIO32_PREFETCH_LENGTH=0x20000000U $(foreach option,FSP_HYPERTHREADING NAI_68INT6_PCIE_NT NAI_68INT6_BACKPLANE_CLK \ NAI_68INT6_PCIE_LINK_DELAY_S NAI_68INT6_BOOT_DELAY_S \ NAI_68INT6_FPGA_READY_DELAY_S NAI_68INT6_DEBUG_CONSENT, \ From 2d2414fcf487f4a61616e48c4907f565f91ed0c5 Mon Sep 17 00:00:00 2001 From: David Garske Date: Wed, 11 Nov 2026 15:23:12 -0800 Subject: [PATCH 14/17] x86 fsp: reserve the ECAM range in e820 so Linux uses MMCONFIG --- include/x86/acpi.h | 6 ++++++ src/x86/acpi.c | 6 ------ src/x86/linux_loader.c | 8 ++++++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/include/x86/acpi.h b/include/x86/acpi.h index 1c92eb6696..516e362502 100644 --- a/include/x86/acpi.h +++ b/include/x86/acpi.h @@ -30,6 +30,12 @@ #endif #define ACPI_TABLE_SIZE 0x00010000UL /* 64 KiB */ +/* ECAM base and bus range, matching MCFG to config PCI_ECAM_BASE. */ +#ifndef PCI_ECAM_BASE +#define PCI_ECAM_BASE 0xC0000000UL +#endif +#define ACPI_PCI_END_BUS 0x17 + /* Build RSDP -> XSDT -> { FADT, MADT, MCFG, TPM2, HPET, DMAR, DSDT } at * ACPI_TABLE_BASE and return the RSDP physical address for boot_params.acpi_rsdp_addr. */ uint64_t acpi_setup(void); diff --git a/src/x86/acpi.c b/src/x86/acpi.c index 9a2471a72d..dbe5ecd9ca 100644 --- a/src/x86/acpi.c +++ b/src/x86/acpi.c @@ -42,12 +42,6 @@ #define ACPI_IOAPIC_ID 0x02 #define ACPI_CPU_MAX 8 -/* ECAM base and bus range, matching MCFG to config PCI_ECAM_BASE. */ -#ifndef PCI_ECAM_BASE -#define PCI_ECAM_BASE 0xC0000000UL -#endif -#define ACPI_PCI_END_BUS 0x17 - /* MADT subtable types. */ #define MADT_TYPE_LAPIC 0 #define MADT_TYPE_IOAPIC 1 diff --git a/src/x86/linux_loader.c b/src/x86/linux_loader.c index e1e7c34438..25d42a204e 100644 --- a/src/x86/linux_loader.c +++ b/src/x86/linux_loader.c @@ -340,6 +340,14 @@ void load_linux(uint8_t *linux_image, uint32_t image_size, void *params, param.e820_table[param.e820_entries].size = ACPI_TABLE_SIZE; param.e820_table[param.e820_entries].type = E820_TYPE_ACPI; param.e820_entries++; + /* The kernel uses the MCFG range only if firmware marks it reserved */ + if (param.e820_entries < E820_MAX_ENTRIES_ZEROPAGE) { + param.e820_table[param.e820_entries].addr = PCI_ECAM_BASE; + param.e820_table[param.e820_entries].size = + (uint64_t)(ACPI_PCI_END_BUS + 1) << 20; + param.e820_table[param.e820_entries].type = E820_TYPE_RESERVED; + param.e820_entries++; + } #endif /* WOLFBOOT_ACPI */ if (param.hdr.setup_sects != 0) { From 78843ce13c39633165ac97be3231b29a5007391f Mon Sep 17 00:00:00 2001 From: David Garske Date: Wed, 11 Nov 2026 15:42:56 -0800 Subject: [PATCH 15/17] x86 fsp: check cached training data against a TPM NV hash in stage 1 --- config/examples/nai_68int6.config | 3 + docs/Targets.md | 1 + include/x86/fsp_nvs.h | 1 + options.mk | 8 + src/loader.c | 6 + src/x86/fsp_nvs.c | 265 +++++++++++++++++++++++++++ tools/unit-tests/Makefile | 15 +- tools/unit-tests/unit-fsp-nvs-save.c | 198 +++++++++++++++++++- tools/unit-tests/unit-fsp-nvs.c | 160 ++++++++++++++++ 9 files changed, 651 insertions(+), 6 deletions(-) diff --git a/config/examples/nai_68int6.config b/config/examples/nai_68int6.config index d0658ad4f3..bd8a3a798d 100644 --- a/config/examples/nai_68int6.config +++ b/config/examples/nai_68int6.config @@ -45,6 +45,9 @@ UCODE0_BIN=src/x86/ucode0.bin FSP_NVS_CACHE=1 FSP_NVS_BASE=0xff9e0000 FSP_NVS_SIZE=0x20000 +# Set to 1 to check the saved training data against a hash in TPM NV before +# FSP-M uses it (needs ANTI_ROLLBACK=1) +FSP_NVS_TPM=0 FSP_T_BIN=./src/x86/fsp_t.bin FSP_M_BIN=./src/x86/fsp_m.bin diff --git a/docs/Targets.md b/docs/Targets.md index 012d8fa325..e41ef919a3 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -9224,6 +9224,7 @@ Notable options in [config/examples/nai_68int6.config](../config/examples/nai_68 | `X86_UART_NUMBER=2` | LPSS UART2 for all three FSP phases (Kontron uses 0) | | `ACPI=1` | Required. Generates ACPI tables (RSDP/XSDT/FADT/MADT/MCFG/TPM2/HPET/DMAR and a minimal DSDT with the TPM device); the board's PCI interrupt routing is only in the DSDT `_PRT`, since the MP table is the Kontron one. The DSDT also declares the board devices on LPSS I2C0 (EEPROMs, RTC/watchdog) and GSPI0 (the PRC, for NAI's `naigspi` driver) | | `FSP_NVS_CACHE=1`, `FSP_NVS_BASE`, `FSP_NVS_SIZE` | Keeps FSP-M's memory training data (its non-volatile storage HOB) in a 128 KB area of the BIOS region below the wolfBoot window, so later boots skip full memory training. Stage 1 passes a valid copy back through `NvsBufferPtr`; stage 2 rewrites it, before FSP-S and before the BIOS region is locked, only when it changes. The record is keyed on a CRC of the FSP-M image and its UPDs, so a change to either falls back to full training. Stage 1 sets a CMOS flag before FspMemInit runs with a saved record, an FSP-requested reset clears it, and stage 2 clears it once FspSiliconInit succeeds. If FspMemInit fails with the record, or a boot finds the flag still set because the previous boot crashed or hung with the record, boots train from scratch until one gets through FspSiliconInit, and stage 2 replaces the record. The flag is kept in CMOS, so it survives a reset but needs the RTC battery to survive loss of power. `assemble_image.sh -n` blanks the area, so an image built from a board's flash dump does not carry that board's training data | +| `FSP_NVS_TPM=1` | Authenticates the saved training data, which is otherwise the only unauthenticated input FSP-M consumes. Stage 2 keeps a SHA-256 of the record in a TPM NV index that only the platform hierarchy can write, and stage 1 reads it, before memory is up, and compares it with the record in flash before handing the record to FspMemInit; without a match it trains from scratch. Needs `ANTI_ROLLBACK=1`, which disables the platform hierarchy before handoff so the OS cannot rewrite the hash. The first boot after enabling it trains once. Without this option, prefer `FSP_NVS_CACHE=0` on a Boot Guard image | | `FSP_HYPERTHREADING=1` | Hyper-Threading on (off by default). The MADT and the MP table list the enabled logical processors from the CPUID topology, so either setting works | | `MP_CPU_NUM_ENTRY=8` | MP table processor entries, one per logical processor (default 4) | | `WOLFBOOT_CMDLINE_MAX` | Buffer for a signed `HDR_CMDLINE`, including the terminating NUL (default 256); a longer command line is ignored and the built-in one is used | diff --git a/include/x86/fsp_nvs.h b/include/x86/fsp_nvs.h index af5e68d660..51196b0240 100644 --- a/include/x86/fsp_nvs.h +++ b/include/x86/fsp_nvs.h @@ -46,5 +46,6 @@ void fsp_nvs_reject(void); void fsp_nvs_before_reset(void); void fsp_nvs_boot_ok(void); void fsp_nvs_save(void); +void fsp_nvs_tpm_sync(void); #endif /* FSP_NVS_H */ diff --git a/options.mk b/options.mk index 2dff5c6980..013d4568de 100644 --- a/options.mk +++ b/options.mk @@ -2103,6 +2103,14 @@ ifeq ($(FSP), 1) endif CFLAGS+=-DWOLFBOOT_FSP_NVS_CACHE CFLAGS+=-DFSP_NVS_BASE=$(FSP_NVS_BASE) -DFSP_NVS_SIZE=$(FSP_NVS_SIZE) + ifeq ($(FSP_NVS_TPM),1) + # anti-rollback disables the platform hierarchy before handoff, which + # is what keeps the OS from rewriting the reference hash + ifneq ($(ANTI_ROLLBACK),1) + $(error FSP_NVS_TPM=1 needs ANTI_ROLLBACK=1) + endif + CFLAGS+=-DWOLFBOOT_FSP_NVS_TPM + endif endif endif diff --git a/src/loader.c b/src/loader.c index bf8e5551c3..113a23a3ff 100644 --- a/src/loader.c +++ b/src/loader.c @@ -30,6 +30,9 @@ */ #include "loader.h" +#ifdef WOLFBOOT_FSP_NVS_TPM +#include "x86/fsp_nvs.h" +#endif #include "image.h" #include "hal.h" #include "hooks.h" @@ -167,6 +170,9 @@ int main(void) wolfBoot_panic(); } #endif +#ifdef WOLFBOOT_FSP_NVS_TPM + fsp_nvs_tpm_sync(); +#endif #ifdef WOLFCRYPT_SECURE_MODE wcs_Init(); #endif diff --git a/src/x86/fsp_nvs.c b/src/x86/fsp_nvs.c index 85bd852ff9..3039a3a836 100644 --- a/src/x86/fsp_nvs.c +++ b/src/x86/fsp_nvs.c @@ -101,7 +101,195 @@ void fsp_nvs_boot_ok(void) cmos_write(FSP_NVS_STATE_CMOS_IDX, 0); } +#ifdef WOLFBOOT_FSP_NVS_TPM +#include + +/* TPM NV index holding the SHA-256 of the record stage 2 last saved. It is + * created by the platform hierarchy, which wolfBoot disables before handoff, + * so only wolfBoot can write it. */ +#ifndef FSP_NVS_TPM_NV_INDEX +#define FSP_NVS_TPM_NV_INDEX 0x01400410U +#endif +#define FSP_NVS_HASH_SIZE 32 + +/* Only an index the platform hierarchy created, and that it alone can + * write, holds a hash wolfBoot stored. TPMA_NV bits: PPWRITE, OWNERWRITE, + * AUTHWRITE, POLICYWRITE and PLATFORMCREATE. */ +#define FSP_NVS_NV_ATTR_MASK 0x4000000FU +#define FSP_NVS_NV_ATTR 0x40000001U + +static int fsp_nvs_record_hash(const struct fsp_nvs_record *rec, uint8_t *hash) +{ + wc_Sha256 sha; + int ret; + + ret = wc_InitSha256(&sha); + if (ret != 0) + return ret; + ret = wc_Sha256Update(&sha, (const uint8_t *)rec, sizeof(*rec) + rec->len); + if (ret == 0) + ret = wc_Sha256Final(&sha, hash); + wc_Sha256Free(&sha); + return ret; +} +#endif /* WOLFBOOT_FSP_NVS_TPM */ + #ifdef BUILD_LOADER_STAGE1 +#ifdef WOLFBOOT_FSP_NVS_TPM +/* Stage 1 has no writable data before memory is up, so wolfTPM cannot run + * yet. Read the reference hash with a minimal FIFO exchange that keeps all + * of its state on the stack. */ +#ifndef FSP_NVS_TPM_BASE +#define FSP_NVS_TPM_BASE 0xFED40000U +#endif +#define TIS_ACCESS (FSP_NVS_TPM_BASE + 0x00) +#define TIS_STS (FSP_NVS_TPM_BASE + 0x18) +#define TIS_FIFO (FSP_NVS_TPM_BASE + 0x24) +#define TIS_ACCESS_VALID 0x80 +#define TIS_ACCESS_ACTIVE 0x20 +#define TIS_ACCESS_REQUEST 0x02 +#define TIS_STS_VALID 0x80 +#define TIS_STS_READY 0x40 +#define TIS_STS_GO 0x20 +#define TIS_STS_AVAIL 0x10 +#define TIS_POLL_MAX 2000000U + +static int tis_wait_sts(uint8_t mask) +{ + uint32_t i; + + for (i = 0; i < TIS_POLL_MAX; i++) { + if ((mmio_read8(TIS_STS) & mask) == mask) + return 0; + } + return -1; +} + +static int tis_xfer(const uint8_t *cmd, uint32_t cmd_sz, uint8_t *rsp, + uint32_t rsp_max, uint32_t *rsp_sz) +{ + uint32_t i, n, total; + int ret = -1; + + mmio_write8(TIS_ACCESS, TIS_ACCESS_REQUEST); + for (i = 0; i < TIS_POLL_MAX; i++) { + if ((mmio_read8(TIS_ACCESS) & (TIS_ACCESS_VALID | TIS_ACCESS_ACTIVE)) == + (TIS_ACCESS_VALID | TIS_ACCESS_ACTIVE)) + break; + } + if (i == TIS_POLL_MAX) + return -1; + mmio_write8(TIS_STS, TIS_STS_READY); + if (tis_wait_sts(TIS_STS_READY) != 0) + goto out; + for (i = 0; i < cmd_sz; i++) { + /* burst count, STS bits 8-23 */ + for (n = 0; n < TIS_POLL_MAX; n++) { + if ((mmio_read32(TIS_STS) & 0x00FFFF00U) != 0) + break; + } + if (n == TIS_POLL_MAX) + goto out; + mmio_write8(TIS_FIFO, cmd[i]); + } + mmio_write8(TIS_STS, TIS_STS_GO); + total = 10; + for (i = 0; i < total; i++) { + if (tis_wait_sts(TIS_STS_VALID | TIS_STS_AVAIL) != 0 || i >= rsp_max) + goto out; + rsp[i] = mmio_read8(TIS_FIFO); + if (i == 5) { + total = ((uint32_t)rsp[2] << 24) | ((uint32_t)rsp[3] << 16) | + ((uint32_t)rsp[4] << 8) | rsp[5]; + if (total < 10 || total > rsp_max) + goto out; + } + } + *rsp_sz = total; + ret = 0; +out: + mmio_write8(TIS_STS, TIS_STS_READY); + mmio_write8(TIS_ACCESS, TIS_ACCESS_ACTIVE); + return ret; +} + +static uint32_t tpm_rsp_code(const uint8_t *rsp) +{ + return ((uint32_t)rsp[6] << 24) | ((uint32_t)rsp[7] << 16) | + ((uint32_t)rsp[8] << 8) | rsp[9]; +} + +/* Returns 0 with the reference hash, or -1 when there is none to trust. */ +static int fsp_nvs_tpm_ref(uint8_t *hash) +{ + static const uint8_t startup[] = { + 0x80, 0x01, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x01, 0x44, 0x00, 0x00 + }; + uint8_t nv_read[35] = { + 0x80, 0x02, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x01, 0x4e, + 0, 0, 0, 0, /* authHandle: the index */ + 0, 0, 0, 0, /* nvIndex */ + 0x00, 0x00, 0x00, 0x09, /* authorization size */ + 0x40, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, /* empty PW */ + 0x00, FSP_NVS_HASH_SIZE, 0x00, 0x00 /* size, offset */ + }; + uint8_t nv_public[14] = { + 0x80, 0x01, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x01, 0x69, + 0, 0, 0, 0 /* nvIndex */ + }; + uint8_t rsp[96]; + uint32_t rsp_sz = 0, rc, attr, i; + + for (i = 0; i < 4; i++) { + nv_read[10 + i] = nv_read[14 + i] = nv_public[10 + i] = + (uint8_t)(FSP_NVS_TPM_NV_INDEX >> (24 - 8 * i)); + } + /* TPM_RC_INITIALIZE (0x100): already started */ + if (tis_xfer(startup, sizeof(startup), rsp, sizeof(rsp), &rsp_sz) != 0) + return -1; + rc = tpm_rsp_code(rsp); + if (rc != 0 && rc != 0x100) + return -1; + /* TPMS_NV_PUBLIC: index, name algorithm, then the attributes */ + if (tis_xfer(nv_public, sizeof(nv_public), rsp, sizeof(rsp), + &rsp_sz) != 0 || tpm_rsp_code(rsp) != 0 || rsp_sz < 22) + return -1; + attr = ((uint32_t)rsp[18] << 24) | ((uint32_t)rsp[19] << 16) | + ((uint32_t)rsp[20] << 8) | rsp[21]; + if ((attr & FSP_NVS_NV_ATTR_MASK) != FSP_NVS_NV_ATTR) + return -1; + if (tis_xfer(nv_read, sizeof(nv_read), rsp, sizeof(rsp), &rsp_sz) != 0) + return -1; + if (tpm_rsp_code(rsp) != 0 || rsp_sz < 16 + FSP_NVS_HASH_SIZE || + rsp[14] != 0 || rsp[15] != FSP_NVS_HASH_SIZE) + return -1; + memcpy(hash, rsp + 16, FSP_NVS_HASH_SIZE); + return 0; +} + +static int fsp_nvs_tpm_check(const struct fsp_nvs_record *rec) +{ + uint8_t ref[FSP_NVS_HASH_SIZE], hash[FSP_NVS_HASH_SIZE]; + uint8_t diff = 0; + int i; + + if (fsp_nvs_tpm_ref(ref) != 0) { + wolfBoot_printf("FSP NVS: no TPM reference hash\r\n"); + return -1; + } + if (fsp_nvs_record_hash(rec, hash) != 0) + return -1; + for (i = 0; i < FSP_NVS_HASH_SIZE; i++) + diff |= ref[i] ^ hash[i]; + if (diff != 0) { + wolfBoot_printf("FSP NVS: saved training data does not match the " + "TPM reference hash\r\n"); + return -1; + } + return 0; +} +#endif /* WOLFBOOT_FSP_NVS_TPM */ + void fsp_nvs_reject(void) { cmos_write(FSP_NVS_STATE_CMOS_IDX, FSP_NVS_STATE_SKIP); @@ -140,6 +328,10 @@ int fsp_nvs_apply(uint8_t *udp_m, uint32_t udp_size, const uint8_t *fsp_m, wolfBoot_printf("FSP NVS: no saved training data\r\n"); return 0; } +#ifdef WOLFBOOT_FSP_NVS_TPM + if (fsp_nvs_tpm_check(rec) != 0) + return 0; +#endif cmos_write(FSP_NVS_STATE_CMOS_IDX, FSP_NVS_STATE_IN_USE); arch->NvsBufferPtr = (uint32_t)(uintptr_t)rec->data; arch->BootMode = BOOT_ASSUMING_NO_CONFIGURATION_CHANGES; @@ -224,4 +416,77 @@ void fsp_nvs_save(void) } wolfBoot_printf("FSP NVS: saved %d bytes of training data\r\n", (int)len); } + +#ifdef WOLFBOOT_FSP_NVS_TPM +#include "tpm.h" +#include + +/* Keep the TPM reference hash equal to the record in flash, but only when + * that record holds the data FspMemInit returned on this boot: if the save + * failed, flash may still hold an older record, one stage 1 perhaps rejected. + * Must run before the platform hierarchy is disabled. */ +void fsp_nvs_tpm_sync(void) +{ + const struct fsp_nvs_record *rec = + (const struct fsp_nvs_record *)(uintptr_t)FSP_NVS_BASE; + WOLFTPM2_DEV *dev = &wolftpm_dev; + WOLFTPM2_HANDLE parent; + WOLFTPM2_NV nv; + NV_Write_In in; + struct stage2_parameter *params = stage2_get_parameters(); + struct efi_hob_guid_type *hob; + uint8_t hash[FSP_NVS_HASH_SIZE], ref[FSP_NVS_HASH_SIZE]; + word32 attr = 0, sz = sizeof(ref); + int rc; + + hob = fsp_nvs_find_hob((struct efi_hob *)(uintptr_t)params->hobList); + if (hob == NULL || + !fsp_nvs_is_current(rec, params->fsp_nvs_key, + (const uint8_t *)(hob + 1), + hob->header.hob_length - sizeof(*hob)) || + fsp_nvs_record_hash(rec, hash) != 0) + return; + + memset(&nv, 0, sizeof(nv)); + nv.handle.hndl = FSP_NVS_TPM_NV_INDEX; + if (wolfTPM2_NVOpen(dev, &nv, FSP_NVS_TPM_NV_INDEX, NULL, 0) == 0) { + if ((nv.attributes & FSP_NVS_NV_ATTR_MASK) != FSP_NVS_NV_ATTR) { + wolfBoot_printf("FSP NVS: TPM reference index is not " + "platform-owned\r\n"); + return; + } + wolfTPM2_SetAuthHandle(dev, 0, &nv.handle); + rc = wolfTPM2_NVReadAuth(dev, &nv, FSP_NVS_TPM_NV_INDEX, ref, &sz, 0); + wolfTPM2_UnsetAuth(dev, 0); + if (rc == 0 && sz == sizeof(ref) && + memcmp(ref, hash, sizeof(ref)) == 0) + return; + } + + memset(&parent, 0, sizeof(parent)); + parent.hndl = TPM_RH_PLATFORM; + wolfTPM2_GetNvAttributesTemplate(TPM_RH_PLATFORM, &attr); + attr &= ~TPMA_NV_AUTHWRITE; + rc = wolfTPM2_NVCreateAuth(dev, &parent, &nv, FSP_NVS_TPM_NV_INDEX, attr, + sizeof(hash), NULL, 0); + if (rc == TPM_RC_NV_DEFINED) + rc = 0; + wolfTPM2_UnsetAuth(dev, 1); + if (rc == 0) { + memset(&in, 0, sizeof(in)); + in.authHandle = TPM_RH_PLATFORM; + in.nvIndex = FSP_NVS_TPM_NV_INDEX; + in.data.size = sizeof(hash); + memcpy(in.data.buffer, hash, sizeof(hash)); + wolfTPM2_SetAuthPassword(dev, 0, NULL); + rc = TPM2_NV_Write(&in); + wolfTPM2_UnsetAuth(dev, 0); + } + if (rc != 0) + wolfBoot_printf("FSP NVS: TPM reference hash update failed 0x%x\r\n", + rc); + else + wolfBoot_printf("FSP NVS: TPM reference hash updated\r\n"); +} +#endif /* WOLFBOOT_FSP_NVS_TPM */ #endif /* BUILD_LOADER_STAGE1 */ diff --git a/tools/unit-tests/Makefile b/tools/unit-tests/Makefile index e6ccccf794..d58dca8978 100644 --- a/tools/unit-tests/Makefile +++ b/tools/unit-tests/Makefile @@ -2325,17 +2325,24 @@ unit-boot-x86-fsp-measure: ../../include/target.h unit-boot-x86-fsp-measure.c \ # unit-fsp-nvs runs the FSP-M memory training data cache record check and # the stage 1 hand-off (src/x86/fsp_nvs.c) against a RAM-backed area. unit-fsp-nvs:CFLAGS+=-DWOLFBOOT_FSP -DWOLFBOOT_FSP_NVS_CACHE \ - -DBUILD_LOADER_STAGE1 -DPRINTF_ENABLED + -DWOLFBOOT_FSP_NVS_TPM -DBUILD_LOADER_STAGE1 -DPRINTF_ENABLED unit-fsp-nvs: unit-fsp-nvs.c ../../src/x86/fsp_nvs.c - gcc -o $@ unit-fsp-nvs.c $(CFLAGS) $(LDFLAGS) + gcc -o $@ unit-fsp-nvs.c \ + $(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/sha256.c $(CFLAGS) \ + -ffunction-sections -fdata-sections $(LDFLAGS) -Wl,--gc-sections # unit-fsp-nvs-save runs the stage 2 save path against a synthetic HOB list # and a RAM-backed flash area; built non-PIE so the HOB list sits below 4 GB # (stage2_parameter.hobList is 32-bit). unit-fsp-nvs-save:CFLAGS+=-DWOLFBOOT_FSP -DWOLFBOOT_FSP_NVS_CACHE \ - -DPRINTF_ENABLED -fno-pie + -DWOLFBOOT_FSP_NVS_TPM -DWOLFBOOT_TPM -DWOLFTPM_USER_SETTINGS \ + -I$(WOLFBOOT_LIB_WOLFTPM) -D__WOLFBOOT -DWOLFBOOT_HASH_SHA384 \ + -DWOLFBOOT_SIGN_ECC384 -DPRINTF_ENABLED -fno-pie unit-fsp-nvs-save: unit-fsp-nvs-save.c ../../src/x86/fsp_nvs.c ../../src/x86/hob.c - gcc -o $@ unit-fsp-nvs-save.c $(CFLAGS) -no-pie $(LDFLAGS) + gcc -o $@ unit-fsp-nvs-save.c \ + $(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/sha256.c $(CFLAGS) \ + -ffunction-sections -fdata-sections -no-pie $(LDFLAGS) \ + -Wl,--gc-sections # unit-nrf5340-flash-protect runs the real hal_flash_protect() from # hal/nrf5340.c against a mock SPU region-permission array (F-13623: the diff --git a/tools/unit-tests/unit-fsp-nvs-save.c b/tools/unit-tests/unit-fsp-nvs-save.c index e2027db906..aab904f1be 100644 --- a/tools/unit-tests/unit-fsp-nvs-save.c +++ b/tools/unit-tests/unit-fsp-nvs-save.c @@ -38,7 +38,7 @@ static uint8_t g_area[AREA_SIZE] __attribute__((aligned(4096))); #include "stage2_params.h" static struct stage2_parameter g_params; -static int g_erase_calls, g_write_calls, g_fail_write_at; +static int g_erase_calls, g_write_calls, g_fail_write_at, g_fail_erase; struct stage2_parameter *stage2_get_parameters(void) { @@ -50,6 +50,8 @@ int tgl_spi_flash_erase(uint32_t address, uint32_t len) uintptr_t off = (uintptr_t)address - (uintptr_t)(uint32_t)FSP_NVS_BASE; g_erase_calls++; + if (g_fail_erase) + return -1; ck_assert_uint_eq(address & 0xfff, 0); ck_assert_uint_eq(len & 0xfff, 0); ck_assert_uint_le(off + len, AREA_SIZE); @@ -81,6 +83,89 @@ void cmos_write(uint8_t index, uint8_t value) (void)value; } +/* wolfTPM as fsp_nvs_tpm_sync() uses it, over one NV index */ +#include "tpm.h" +#include + +WOLFTPM2_DEV wolftpm_dev; +static int g_nv_defined, g_nv_written, g_nv_creates, g_nv_writes; +static word32 g_nv_attr; +static uint8_t g_nv_data[32]; + +int wolfTPM2_NVOpen(WOLFTPM2_DEV *dev, WOLFTPM2_NV *nv, word32 nvIndex, + const byte *auth, word32 authSz) +{ + (void)dev; (void)nvIndex; (void)auth; (void)authSz; + if (!g_nv_defined) + return TPM_RC_HANDLE | RC_FMT1; + nv->attributes = g_nv_attr; + return 0; +} + +int wolfTPM2_NVReadAuth(WOLFTPM2_DEV *dev, WOLFTPM2_NV *nv, word32 nvIndex, + byte *dataBuf, word32 *pDataSz, word32 offset) +{ + (void)dev; (void)nv; (void)nvIndex; (void)offset; + if (!g_nv_written) + return TPM_RC_NV_UNINITIALIZED; + memcpy(dataBuf, g_nv_data, sizeof(g_nv_data)); + *pDataSz = sizeof(g_nv_data); + return 0; +} + +int wolfTPM2_NVCreateAuth(WOLFTPM2_DEV *dev, WOLFTPM2_HANDLE *parent, + WOLFTPM2_NV *nv, word32 nvIndex, word32 nvAttributes, + word32 maxSize, const byte *auth, int authSz) +{ + (void)dev; (void)nv; (void)nvIndex; (void)auth; (void)authSz; + ck_assert_uint_eq(parent->hndl, TPM_RH_PLATFORM); + ck_assert_uint_eq(maxSize, sizeof(g_nv_data)); + if (g_nv_defined) + return TPM_RC_NV_DEFINED; + g_nv_creates++; + g_nv_defined = 1; + g_nv_attr = nvAttributes; + return 0; +} + +TPM_RC TPM2_NV_Write(NV_Write_In *in) +{ + ck_assert_uint_eq(in->authHandle, TPM_RH_PLATFORM); + ck_assert_uint_eq(in->data.size, sizeof(g_nv_data)); + g_nv_writes++; + g_nv_written = 1; + memcpy(g_nv_data, in->data.buffer, sizeof(g_nv_data)); + return 0; +} + +int wolfTPM2_GetNvAttributesTemplate(TPM_HANDLE auth, word32 *nvAttributes) +{ + ck_assert_uint_eq(auth, TPM_RH_PLATFORM); + *nvAttributes = TPMA_NV_PPWRITE | TPMA_NV_PPREAD | TPMA_NV_AUTHWRITE | + TPMA_NV_AUTHREAD | TPMA_NV_PLATFORMCREATE | TPMA_NV_NO_DA; + return 0; +} + +int wolfTPM2_SetAuthHandle(WOLFTPM2_DEV *dev, int index, + const WOLFTPM2_HANDLE *handle) +{ + (void)dev; (void)index; (void)handle; + return 0; +} + +int wolfTPM2_SetAuthPassword(WOLFTPM2_DEV *dev, int index, + const TPM2B_AUTH *auth) +{ + (void)dev; (void)index; (void)auth; + return 0; +} + +int wolfTPM2_UnsetAuth(WOLFTPM2_DEV *dev, int index) +{ + (void)dev; (void)index; + return 0; +} + #include "../../src/x86/hob.c" /* FSP_NVS_BASE is a RAM buffer here, not a flash address */ #undef BOOTLOADER_PARTITION_SIZE @@ -135,7 +220,7 @@ static void setup(uint32_t key) memset(&g_params, 0, sizeof(g_params)); g_params.hobList = (uint32_t)(uintptr_t)g_hobs; g_params.fsp_nvs_key = key; - g_erase_calls = g_write_calls = g_fail_write_at = 0; + g_erase_calls = g_write_calls = g_fail_write_at = g_fail_erase = 0; } static const struct fsp_nvs_record *area(void) @@ -238,6 +323,112 @@ START_TEST(test_failed_header_write_leaves_no_record) } END_TEST +static void nv_reset(void) +{ + g_nv_defined = g_nv_written = g_nv_creates = g_nv_writes = 0; + g_nv_attr = 0; + memset(g_nv_data, 0, sizeof(g_nv_data)); +} + +START_TEST(test_tpm_reference_follows_the_record) +{ + uint8_t hash[32]; + + /* no record: nothing to bless */ + nv_reset(); + memset(g_area, 0xff, sizeof(g_area)); + setup(0xabcd); + fsp_nvs_tpm_sync(); + ck_assert_int_eq(g_nv_creates, 0); + ck_assert_int_eq(g_nv_writes, 0); + + /* first record: the index is created for the platform alone */ + build_hobs(300, 0x10); + fsp_nvs_save(); + fsp_nvs_tpm_sync(); + ck_assert_int_eq(g_nv_creates, 1); + ck_assert_int_eq(g_nv_writes, 1); + ck_assert_uint_eq(g_nv_attr & FSP_NVS_NV_ATTR_MASK, FSP_NVS_NV_ATTR); + ck_assert_int_eq(fsp_nvs_record_hash(area(), hash), 0); + ck_assert_mem_eq(g_nv_data, hash, sizeof(hash)); + + /* unchanged record: no TPM write */ + fsp_nvs_tpm_sync(); + ck_assert_int_eq(g_nv_writes, 1); + + /* new record: the hash follows it */ + build_hobs(300, 0x20); + fsp_nvs_save(); + fsp_nvs_tpm_sync(); + ck_assert_int_eq(g_nv_creates, 1); + ck_assert_int_eq(g_nv_writes, 2); + ck_assert_int_eq(fsp_nvs_record_hash(area(), hash), 0); + ck_assert_mem_eq(g_nv_data, hash, sizeof(hash)); + + /* a record for another FSP-M or UPD set is not blessed */ + g_params.fsp_nvs_key = 0x1234; + g_nv_data[0] ^= 1; + fsp_nvs_tpm_sync(); + ck_assert_int_eq(g_nv_writes, 2); +} +END_TEST + +START_TEST(test_tpm_reference_not_updated_after_a_failed_save) +{ + uint8_t first[32]; + + nv_reset(); + memset(g_area, 0xff, sizeof(g_area)); + setup(0xabcd); + build_hobs(300, 0x10); + fsp_nvs_save(); + fsp_nvs_tpm_sync(); + ck_assert_int_eq(g_nv_writes, 1); + memcpy(first, g_nv_data, sizeof(first)); + + /* the erase fails: the older record is still valid in flash, but it is + * not this boot's data, so the TPM hash must not be touched for it */ + build_hobs(300, 0x20); + g_fail_erase = 1; + fsp_nvs_save(); + ck_assert_int_eq(fsp_nvs_record_valid(area(), 0xabcd, AREA_SIZE), 1); + g_nv_data[0] ^= 1; + fsp_nvs_tpm_sync(); + ck_assert_int_eq(g_nv_writes, 1); + g_nv_data[0] ^= 1; + ck_assert_mem_eq(g_nv_data, first, sizeof(first)); + + /* a write failure leaves no valid record: nothing to bless either */ + g_fail_erase = 0; + g_write_calls = 0; + g_fail_write_at = 1; + fsp_nvs_save(); + fsp_nvs_tpm_sync(); + ck_assert_int_eq(g_nv_writes, 1); + + /* no training data HOB at all */ + g_fail_write_at = 0; + end_hobs(g_hobs); + fsp_nvs_tpm_sync(); + ck_assert_int_eq(g_nv_writes, 1); +} +END_TEST + +START_TEST(test_tpm_reference_index_must_be_platform_owned) +{ + nv_reset(); + memset(g_area, 0xff, sizeof(g_area)); + setup(0xabcd); + build_hobs(300, 0x10); + fsp_nvs_save(); + g_nv_defined = g_nv_written = 1; + g_nv_attr = TPMA_NV_OWNERWRITE | TPMA_NV_AUTHREAD; + fsp_nvs_tpm_sync(); + ck_assert_int_eq(g_nv_creates, 0); + ck_assert_int_eq(g_nv_writes, 0); +} +END_TEST + static Suite *fsp_nvs_save_suite(void) { Suite *s = suite_create("fsp-nvs-save"); @@ -249,6 +440,9 @@ static Suite *fsp_nvs_save_suite(void) tcase_add_test(tc, test_missing_or_oversized_hob); tcase_add_test(tc, test_zero_length_hob_stops_the_walk); tcase_add_test(tc, test_failed_header_write_leaves_no_record); + tcase_add_test(tc, test_tpm_reference_follows_the_record); + tcase_add_test(tc, test_tpm_reference_not_updated_after_a_failed_save); + tcase_add_test(tc, test_tpm_reference_index_must_be_platform_owned); suite_add_tcase(s, tc); return s; } diff --git a/tools/unit-tests/unit-fsp-nvs.c b/tools/unit-tests/unit-fsp-nvs.c index 5449841041..d379bbd228 100644 --- a/tools/unit-tests/unit-fsp-nvs.c +++ b/tools/unit-tests/unit-fsp-nvs.c @@ -54,6 +54,100 @@ void cmos_write(uint8_t index, uint8_t value) g_cmos[index & 0x7F] = value; } +/* A TPM behind the FIFO interface stage 1 reads the reference hash through: + * it answers Startup, NV_ReadPublic and NV_Read for one index. */ +#define TPM_BASE 0xFED40000U +static uint8_t g_tpm_cmd[64], g_tpm_rsp[96]; +static uint32_t g_tpm_cmd_len, g_tpm_rsp_len, g_tpm_rsp_pos; +static uint32_t g_tpm_startup_rc, g_tpm_nv_attr; +static int g_tpm_nv_defined, g_tpm_nv_reads; +static uint8_t g_tpm_nv_hash[32]; + +static void tpm_put32(uint8_t *p, uint32_t v) +{ + p[0] = (uint8_t)(v >> 24); + p[1] = (uint8_t)(v >> 16); + p[2] = (uint8_t)(v >> 8); + p[3] = (uint8_t)v; +} + +static void tpm_respond(uint32_t rc, uint32_t len) +{ + g_tpm_rsp[0] = 0x80; + g_tpm_rsp[1] = 0x01; + tpm_put32(g_tpm_rsp + 2, rc != 0 ? 10 : len); + tpm_put32(g_tpm_rsp + 6, rc); + g_tpm_rsp_len = rc != 0 ? 10 : len; + g_tpm_rsp_pos = 0; +} + +static void tpm_execute(void) +{ + uint32_t cc = ((uint32_t)g_tpm_cmd[6] << 24) | + ((uint32_t)g_tpm_cmd[7] << 16) | ((uint32_t)g_tpm_cmd[8] << 8) | + g_tpm_cmd[9]; + + memset(g_tpm_rsp, 0, sizeof(g_tpm_rsp)); + if (cc == 0x144) { /* Startup */ + tpm_respond(g_tpm_startup_rc, 10); + } + else if (!g_tpm_nv_defined) { + tpm_respond(0x18B, 10); /* TPM_RC_HANDLE */ + } + else if (cc == 0x169) { /* NV_ReadPublic */ + g_tpm_rsp[11] = 14; + memcpy(g_tpm_rsp + 12, g_tpm_cmd + 10, 4); + g_tpm_rsp[17] = 0x0b; + tpm_put32(g_tpm_rsp + 18, g_tpm_nv_attr); + g_tpm_rsp[25] = 32; + tpm_respond(0, 62); + } + else if (cc == 0x14E) { /* NV_Read */ + g_tpm_nv_reads++; + g_tpm_rsp[13] = 34; + g_tpm_rsp[15] = 32; + memcpy(g_tpm_rsp + 16, g_tpm_nv_hash, 32); + tpm_respond(0, 53); + } + else { + tpm_respond(0x143, 10); /* TPM_RC_COMMAND_CODE */ + } +} + +uint8_t mmio_read8(uintptr_t address) +{ + if (address == TPM_BASE + 0x00) + return 0x80 | 0x20; + if (address == TPM_BASE + 0x18) + return 0x80 | 0x40 | + (g_tpm_rsp_pos < g_tpm_rsp_len ? 0x10 : 0); + if (address == TPM_BASE + 0x24 && g_tpm_rsp_pos < g_tpm_rsp_len) + return g_tpm_rsp[g_tpm_rsp_pos++]; + return 0xff; +} + +uint32_t mmio_read32(uintptr_t address) +{ + if (address == TPM_BASE + 0x18) + return (8U << 8) | mmio_read8(address); + return 0xffffffffU; +} + +void mmio_write8(uintptr_t address, uint8_t value) +{ + if (address == TPM_BASE + 0x18 && value == 0x40) { + g_tpm_cmd_len = 0; + g_tpm_rsp_len = g_tpm_rsp_pos = 0; + } + else if (address == TPM_BASE + 0x18 && value == 0x20) { + tpm_execute(); + } + else if (address == TPM_BASE + 0x24 && + g_tpm_cmd_len < sizeof(g_tpm_cmd)) { + g_tpm_cmd[g_tpm_cmd_len++] = value; + } +} + /* FSP_NVS_BASE is a RAM buffer here, not a flash address */ #undef BOOTLOADER_PARTITION_SIZE #include "../../src/x86/fsp_nvs.c" @@ -88,6 +182,12 @@ static struct fsp_nvs_record *write_record(uint32_t key, uint32_t len) for (i = 0; i < len; i++) rec->data[i] = (uint8_t)(i * 7 + 1); rec->crc = fsp_nvs_crc32(0, rec->data, len); + /* as stage 2 leaves it: the TPM holds the hash of this record */ + g_tpm_nv_defined = 1; + g_tpm_nv_attr = 0x42040001U; + g_tpm_startup_rc = 0; + g_tpm_nv_reads = 0; + fsp_nvs_record_hash(rec, g_tpm_nv_hash); return rec; } @@ -263,6 +363,64 @@ START_TEST(test_apply_rejects_changed_config) } END_TEST +START_TEST(test_apply_needs_the_tpm_reference_hash) +{ + struct fsp_nvs_record *rec; + + /* a TPM another stage already started is fine */ + reset_udp(); + write_record(expected_key(), 200); + g_tpm_startup_rc = 0x100; + ck_assert_int_eq(fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE), 1); + ck_assert_int_eq(g_tpm_nv_reads, 1); + + /* a record changed in flash, with its CRC fixed up, is not used */ + reset_udp(); + rec = write_record(expected_key(), 200); + rec->data[10] ^= 1; + rec->crc = fsp_nvs_crc32(0, rec->data, rec->len); + ck_assert_int_eq(fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE), 0); + ck_assert_uint_eq(arch()->NvsBufferPtr, 0); + ck_assert_uint_eq(arch()->BootMode, BOOT_WITH_FULL_CONFIGURATION); + ck_assert_uint_eq(cmos_read(FSP_NVS_STATE_CMOS_IDX), 0); + + /* nor is one with no reference in the TPM */ + reset_udp(); + write_record(expected_key(), 200); + g_tpm_nv_defined = 0; + ck_assert_int_eq(fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE), 0); + + /* or with the TPM failing to start */ + reset_udp(); + write_record(expected_key(), 200); + g_tpm_startup_rc = 0x101; + ck_assert_int_eq(fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE), 0); +} +END_TEST + +START_TEST(test_apply_rejects_an_index_the_os_can_write) +{ + static const uint32_t attrs[] = { + 0x02040002U, /* owner-created, owner write */ + 0x42040003U, /* platform-created, but owner write too */ + 0x42040005U, /* auth write */ + 0x42040009U, /* policy write */ + 0x42040000U /* no platform write */ + }; + unsigned int i; + + for (i = 0; i < sizeof(attrs) / sizeof(attrs[0]); i++) { + reset_udp(); + write_record(expected_key(), 200); + g_tpm_nv_attr = attrs[i]; + ck_assert_int_eq(fsp_nvs_apply(g_udp, UDP_SIZE, g_fspm, FSPM_SIZE), + 0); + ck_assert_int_eq(g_tpm_nv_reads, 0); + ck_assert_uint_eq(arch()->NvsBufferPtr, 0); + } +} +END_TEST + static Suite *fsp_nvs_suite(void) { Suite *s = suite_create("fsp-nvs"); @@ -277,6 +435,8 @@ static Suite *fsp_nvs_suite(void) tcase_add_test(tc, test_apply_skips_a_rejected_record_until_cleared); tcase_add_test(tc, test_unfinished_boot_with_record_retrains); tcase_add_test(tc, test_fsp_reset_keeps_using_the_record); + tcase_add_test(tc, test_apply_needs_the_tpm_reference_hash); + tcase_add_test(tc, test_apply_rejects_an_index_the_os_can_write); suite_add_tcase(s, tc); return s; } From 538c134fd005c761381f614dab3d550b5b835940 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 12 Nov 2026 11:18:17 -0800 Subject: [PATCH 16/17] x86 fsp: fix review findings in the training data cache and ELF map --- arch.mk | 5 ++ config/examples/nai_68int6.config | 2 +- docs/Targets.md | 2 +- options.mk | 6 +- src/boot_x86_fsp_payload.c | 6 ++ src/update_disk.c | 15 +++-- src/x86/fsp_nvs.c | 35 ++++++++++-- tools/scripts/x86_fsp/tgl/assemble_image.sh | 6 +- tools/unit-tests/unit-boot-x86-fsp-payload.c | 16 ++++-- tools/unit-tests/unit-fsp-nvs-save.c | 58 +++++++++++++++++++- 10 files changed, 124 insertions(+), 27 deletions(-) diff --git a/arch.mk b/arch.mk index 91304f10bc..c3e68bf2af 100644 --- a/arch.mk +++ b/arch.mk @@ -2661,6 +2661,11 @@ ifeq ("${FSP}", "1") CFLAGS += -DUCODE0_ADDRESS=$(UCODE0_BASE) ifeq ($(FSP_NVS_CACHE),1) OBJS+=src/x86/fsp_nvs.o + ifeq ($(FSP_NVS_TPM),1) + ifneq ($(filter-out $(STAGE1_AUTH),1),) + OBJS+=$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/sha256.o + endif + endif endif endif else diff --git a/config/examples/nai_68int6.config b/config/examples/nai_68int6.config index bd8a3a798d..62a895fe79 100644 --- a/config/examples/nai_68int6.config +++ b/config/examples/nai_68int6.config @@ -46,7 +46,7 @@ FSP_NVS_CACHE=1 FSP_NVS_BASE=0xff9e0000 FSP_NVS_SIZE=0x20000 # Set to 1 to check the saved training data against a hash in TPM NV before -# FSP-M uses it (needs ANTI_ROLLBACK=1) +# FSP-M uses it (needs ANTI_ROLLBACK=1, ANTI_ROLLBACK_HALT_ON_FAILURE=1) FSP_NVS_TPM=0 FSP_T_BIN=./src/x86/fsp_t.bin diff --git a/docs/Targets.md b/docs/Targets.md index e41ef919a3..9f6a2a8565 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -9224,7 +9224,7 @@ Notable options in [config/examples/nai_68int6.config](../config/examples/nai_68 | `X86_UART_NUMBER=2` | LPSS UART2 for all three FSP phases (Kontron uses 0) | | `ACPI=1` | Required. Generates ACPI tables (RSDP/XSDT/FADT/MADT/MCFG/TPM2/HPET/DMAR and a minimal DSDT with the TPM device); the board's PCI interrupt routing is only in the DSDT `_PRT`, since the MP table is the Kontron one. The DSDT also declares the board devices on LPSS I2C0 (EEPROMs, RTC/watchdog) and GSPI0 (the PRC, for NAI's `naigspi` driver) | | `FSP_NVS_CACHE=1`, `FSP_NVS_BASE`, `FSP_NVS_SIZE` | Keeps FSP-M's memory training data (its non-volatile storage HOB) in a 128 KB area of the BIOS region below the wolfBoot window, so later boots skip full memory training. Stage 1 passes a valid copy back through `NvsBufferPtr`; stage 2 rewrites it, before FSP-S and before the BIOS region is locked, only when it changes. The record is keyed on a CRC of the FSP-M image and its UPDs, so a change to either falls back to full training. Stage 1 sets a CMOS flag before FspMemInit runs with a saved record, an FSP-requested reset clears it, and stage 2 clears it once FspSiliconInit succeeds. If FspMemInit fails with the record, or a boot finds the flag still set because the previous boot crashed or hung with the record, boots train from scratch until one gets through FspSiliconInit, and stage 2 replaces the record. The flag is kept in CMOS, so it survives a reset but needs the RTC battery to survive loss of power. `assemble_image.sh -n` blanks the area, so an image built from a board's flash dump does not carry that board's training data | -| `FSP_NVS_TPM=1` | Authenticates the saved training data, which is otherwise the only unauthenticated input FSP-M consumes. Stage 2 keeps a SHA-256 of the record in a TPM NV index that only the platform hierarchy can write, and stage 1 reads it, before memory is up, and compares it with the record in flash before handing the record to FspMemInit; without a match it trains from scratch. Needs `ANTI_ROLLBACK=1`, which disables the platform hierarchy before handoff so the OS cannot rewrite the hash. The first boot after enabling it trains once. Without this option, prefer `FSP_NVS_CACHE=0` on a Boot Guard image | +| `FSP_NVS_TPM=1` | Authenticates the saved training data, which is otherwise the only unauthenticated input FSP-M consumes. Stage 2 keeps a SHA-256 of the record in a TPM NV index that only the platform hierarchy can write, and stage 1 reads it, before memory is up, and compares it with the record in flash before handing the record to FspMemInit; without a match it trains from scratch. Needs `ANTI_ROLLBACK=1`, which disables the platform hierarchy before handoff so the OS cannot rewrite the hash, and `ANTI_ROLLBACK_HALT_ON_FAILURE=1`, so a boot that cannot disable it halts. The first boot after enabling it trains once. Without this option, prefer `FSP_NVS_CACHE=0` on a Boot Guard image | | `FSP_HYPERTHREADING=1` | Hyper-Threading on (off by default). The MADT and the MP table list the enabled logical processors from the CPUID topology, so either setting works | | `MP_CPU_NUM_ENTRY=8` | MP table processor entries, one per logical processor (default 4) | | `WOLFBOOT_CMDLINE_MAX` | Buffer for a signed `HDR_CMDLINE`, including the terminating NUL (default 256); a longer command line is ignored and the built-in one is used | diff --git a/options.mk b/options.mk index 013d4568de..6bb2b4c064 100644 --- a/options.mk +++ b/options.mk @@ -2105,10 +2105,14 @@ ifeq ($(FSP), 1) CFLAGS+=-DFSP_NVS_BASE=$(FSP_NVS_BASE) -DFSP_NVS_SIZE=$(FSP_NVS_SIZE) ifeq ($(FSP_NVS_TPM),1) # anti-rollback disables the platform hierarchy before handoff, which - # is what keeps the OS from rewriting the reference hash + # is what keeps the OS from rewriting the reference hash, so a boot + # that cannot disable it must halt ifneq ($(ANTI_ROLLBACK),1) $(error FSP_NVS_TPM=1 needs ANTI_ROLLBACK=1) endif + ifneq ($(ANTI_ROLLBACK_HALT_ON_FAILURE),1) + $(error FSP_NVS_TPM=1 needs ANTI_ROLLBACK_HALT_ON_FAILURE=1) + endif CFLAGS+=-DWOLFBOOT_FSP_NVS_TPM endif endif diff --git a/src/boot_x86_fsp_payload.c b/src/boot_x86_fsp_payload.c index de86c2e723..1e26c0853e 100644 --- a/src/boot_x86_fsp_payload.c +++ b/src/boot_x86_fsp_payload.c @@ -119,6 +119,12 @@ static int boot_elf_map_cb(uint64_t vaddr, uint64_t paddr, uint32_t size) return -1; } #ifdef WOLFBOOT_64BIT + /* pages are mapped whole, so the checked range is only the one written + * when both addresses sit at the same offset in their page */ + if (((vaddr ^ paddr) & 0xFFF) != 0) { + wolfBoot_printf("ELF: segment addresses differ in page offset\r\n"); + return -1; + } return x86_paging_map_memory(vaddr, paddr, size); #else (void)vaddr; diff --git a/src/update_disk.c b/src/update_disk.c index db29f41150..762fa8b4d1 100644 --- a/src/update_disk.c +++ b/src/update_disk.c @@ -771,9 +771,8 @@ int wolfBoot_disk_dts_fixup(fdt_ctx* ctx) #define WOLFBOOT_CMDLINE_MAX 256 #endif -/* The image's signed kernel command line, as a stage 2 parameter address, or - * 0 when there is none */ -static uint32_t disk_signed_cmdline(const uint8_t *hdr) +/* The image's signed kernel command line, or NULL when there is none */ +static const char *disk_signed_cmdline(const uint8_t *hdr) { static char disk_cmdline[WOLFBOOT_CMDLINE_MAX]; uint8_t *val = NULL, *digest = NULL; @@ -782,22 +781,22 @@ static uint32_t disk_signed_cmdline(const uint8_t *hdr) len = wolfBoot_find_header((uint8_t *)hdr + IMAGE_HEADER_OFFSET, HDR_CMDLINE, &val); if (len == 0 || val == NULL) - return 0; + return NULL; /* The signature covers the header only up to the digest TLV */ if (wolfBoot_find_header((uint8_t *)hdr + IMAGE_HEADER_OFFSET, WOLFBOOT_SHA_HDR, &digest) == 0 || val + len > digest - 2 * sizeof(uint16_t)) { wolfBoot_printf("Ignoring a cmdline outside the signed header\r\n"); - return 0; + return NULL; } if (len >= sizeof(disk_cmdline)) { wolfBoot_printf("Signed cmdline too long (%d), using built-in\r\n", (int)len); - return 0; + return NULL; } memcpy(disk_cmdline, val, len); disk_cmdline[len] = '\0'; - return (uint32_t)(uintptr_t)disk_cmdline; + return disk_cmdline; } #endif @@ -1628,7 +1627,7 @@ void RAMFUNCTION wolfBoot_start(void) #ifdef WOLFBOOT_FSP /* The image's signed kernel command line, read before the decrypted * header is wiped */ - stage2_params->cmdline = disk_signed_cmdline(hdr_ptr); + stage2_params->cmdline = (uint32_t)(uintptr_t)disk_signed_cmdline(hdr_ptr); #endif #ifdef DISK_ENCRYPT disk_decrypted_header_clear(dec_hdr); diff --git a/src/x86/fsp_nvs.c b/src/x86/fsp_nvs.c index 3039a3a836..2f94dacace 100644 --- a/src/x86/fsp_nvs.c +++ b/src/x86/fsp_nvs.c @@ -96,8 +96,19 @@ void fsp_nvs_before_reset(void) cmos_write(FSP_NVS_STATE_CMOS_IDX, 0); } +#ifndef BUILD_LOADER_STAGE1 +/* set when a save left a record in flash that is not this boot's data */ +static int fsp_nvs_stale; +#endif + void fsp_nvs_boot_ok(void) { +#ifndef BUILD_LOADER_STAGE1 + /* the record stage 1 skipped is still there: keep skipping it */ + if (fsp_nvs_stale && + cmos_read(FSP_NVS_STATE_CMOS_IDX) == FSP_NVS_STATE_SKIP) + return; +#endif cmos_write(FSP_NVS_STATE_CMOS_IDX, 0); } @@ -369,7 +380,8 @@ static int fsp_nvs_is_current(const struct fsp_nvs_record *rec, uint32_t key, memcmp(rec->data, data, len) == 0; } -void fsp_nvs_save(void) +/* Returns 1 when flash holds the training data of this boot */ +static int fsp_nvs_store(void) { const struct fsp_nvs_record *rec = (const struct fsp_nvs_record *)(uintptr_t)FSP_NVS_BASE; @@ -382,17 +394,17 @@ void fsp_nvs_save(void) hob = fsp_nvs_find_hob((struct efi_hob *)(uintptr_t)params->hobList); if (hob == NULL) { wolfBoot_printf("FSP NVS: no training data HOB\r\n"); - return; + return 0; } data = (const uint8_t *)(hob + 1); len = hob->header.hob_length - sizeof(*hob); if (len > FSP_NVS_SIZE - sizeof(hdr)) { wolfBoot_printf("FSP NVS: training data (%d bytes) exceeds the cache\r\n", (int)len); - return; + return 0; } if (fsp_nvs_is_current(rec, params->fsp_nvs_key, data, len)) - return; + return 1; memset(&hdr, 0, sizeof(hdr)); hdr.magic = FSP_NVS_MAGIC; @@ -407,14 +419,25 @@ void fsp_nvs_save(void) tgl_spi_flash_write(FSP_NVS_BASE, (const uint8_t *)&hdr, sizeof(hdr)) != 0) { wolfBoot_printf("FSP NVS: saving training data failed\r\n"); - return; + return 0; } FSP_NVS_CACHE_FLUSH(); if (!fsp_nvs_is_current(rec, hdr.key, data, len)) { wolfBoot_printf("FSP NVS: training data read-back mismatch\r\n"); - return; + return 0; } wolfBoot_printf("FSP NVS: saved %d bytes of training data\r\n", (int)len); + return 1; +} + +void fsp_nvs_save(void) +{ + const struct fsp_nvs_record *rec = + (const struct fsp_nvs_record *)(uintptr_t)FSP_NVS_BASE; + + fsp_nvs_stale = !fsp_nvs_store() && + fsp_nvs_record_valid(rec, stage2_get_parameters()->fsp_nvs_key, + FSP_NVS_SIZE); } #ifdef WOLFBOOT_FSP_NVS_TPM diff --git a/tools/scripts/x86_fsp/tgl/assemble_image.sh b/tools/scripts/x86_fsp/tgl/assemble_image.sh index d1abde1da0..4ab16c3912 100755 --- a/tools/scripts/x86_fsp/tgl/assemble_image.sh +++ b/tools/scripts/x86_fsp/tgl/assemble_image.sh @@ -14,9 +14,9 @@ erase_fsp_nvs_cache() { local base size image_size offset - grep -q '^FSP_NVS_CACHE=1$' .config || return 0 - base=$(sed -n 's/^FSP_NVS_BASE=//p' .config) - size=$(sed -n 's/^FSP_NVS_SIZE=//p' .config) + grep -Eq '^FSP_NVS_CACHE[?]?=1$' .config || return 0 + base=$(sed -n 's/^FSP_NVS_BASE?\{0,1\}=//p' .config) + size=$(sed -n 's/^FSP_NVS_SIZE?\{0,1\}=//p' .config) image_size=$(stat -c%s "$WOLFBOOT_DIR/final_image.bin") # the image ends at 4 GB offset=$((image_size - (0x100000000 - base))) diff --git a/tools/unit-tests/unit-boot-x86-fsp-payload.c b/tools/unit-tests/unit-boot-x86-fsp-payload.c index 9e9ccd62a9..b655a12ef6 100644 --- a/tools/unit-tests/unit-boot-x86-fsp-payload.c +++ b/tools/unit-tests/unit-boot-x86-fsp-payload.c @@ -128,11 +128,13 @@ START_TEST(test_elf_segments_outside_wolfboot_memory) { /* below the reserved window, ending exactly at its start, and above * 4 GB are all accepted; an empty segment is never checked */ - ck_assert_int_eq(boot_elf_map_cb(0, 0x200000, 0x1000), 0); - ck_assert_int_eq(boot_elf_map_cb(0, ELF_RESERVED_START - 0x1000, 0x1000), - 0); - ck_assert_int_eq(boot_elf_map_cb(0, ELF_RESERVED_END, 0x1000), 0); - ck_assert_int_eq(boot_elf_map_cb(0, ELF_RESERVED_START, 0), 0); + ck_assert_int_eq(boot_elf_map_cb(0x200000, 0x200000, 0x1000), 0); + ck_assert_int_eq(boot_elf_map_cb(ELF_RESERVED_START - 0x1000, + ELF_RESERVED_START - 0x1000, 0x1000), 0); + ck_assert_int_eq(boot_elf_map_cb(ELF_RESERVED_END, ELF_RESERVED_END, + 0x1000), 0); + ck_assert_int_eq(boot_elf_map_cb(ELF_RESERVED_START, ELF_RESERVED_START, + 0), 0); } END_TEST @@ -146,6 +148,10 @@ START_TEST(test_elf_segments_over_wolfboot_memory) /* a safe physical address with a virtual one over stage 2 */ ck_assert_int_eq(boot_elf_map_cb(WOLFBOOT_LOAD_BASE, 0x200000, 0x1000), -1); + /* different page offsets: the second mapped page would be stage 2's */ + ck_assert_int_eq(boot_elf_map_cb(0x200000fffULL, + ELF_RESERVED_START - 0x1000, 0x1000), + -1); } END_TEST diff --git a/tools/unit-tests/unit-fsp-nvs-save.c b/tools/unit-tests/unit-fsp-nvs-save.c index aab904f1be..a013485d4c 100644 --- a/tools/unit-tests/unit-fsp-nvs-save.c +++ b/tools/unit-tests/unit-fsp-nvs-save.c @@ -71,16 +71,18 @@ int tgl_spi_flash_write(uint32_t address, const uint8_t *buf, uint32_t len) return 0; } +static uint8_t g_cmos; + uint8_t cmos_read(uint8_t index) { (void)index; - return 0; + return g_cmos; } void cmos_write(uint8_t index, uint8_t value) { (void)index; - (void)value; + g_cmos = value; } /* wolfTPM as fsp_nvs_tpm_sync() uses it, over one NV index */ @@ -221,6 +223,7 @@ static void setup(uint32_t key) g_params.hobList = (uint32_t)(uintptr_t)g_hobs; g_params.fsp_nvs_key = key; g_erase_calls = g_write_calls = g_fail_write_at = g_fail_erase = 0; + g_cmos = 0; } static const struct fsp_nvs_record *area(void) @@ -330,6 +333,56 @@ static void nv_reset(void) memset(g_nv_data, 0, sizeof(g_nv_data)); } +START_TEST(test_skip_is_kept_until_the_record_is_replaced) +{ + /* stage 1 skipped the record, and the save of the retrained data fails + * at the erase, so the skipped record is still valid in flash */ + memset(g_area, 0xff, sizeof(g_area)); + setup(0xabcd); + build_hobs(300, 0x10); + fsp_nvs_save(); + build_hobs(300, 0x20); + g_cmos = 0xA5; + g_fail_erase = 1; + fsp_nvs_save(); + ck_assert_int_eq(fsp_nvs_record_valid(area(), 0xabcd, AREA_SIZE), 1); + fsp_nvs_boot_ok(); + ck_assert_uint_eq(g_cmos, 0xA5); + + /* a boot that used the record marks it good whatever the save did */ + g_cmos = 0x5A; + fsp_nvs_save(); + fsp_nvs_boot_ok(); + ck_assert_uint_eq(g_cmos, 0); + + /* a failed write leaves no valid record, so nothing is left to skip */ + g_cmos = 0xA5; + g_fail_erase = 0; + g_write_calls = 0; + g_fail_write_at = 1; + fsp_nvs_save(); + ck_assert_int_eq(fsp_nvs_record_valid(area(), 0xabcd, AREA_SIZE), 0); + fsp_nvs_boot_ok(); + ck_assert_uint_eq(g_cmos, 0); + + /* and a save that replaces the record ends the skipping */ + memset(g_area, 0xff, sizeof(g_area)); + g_fail_write_at = 0; + build_hobs(300, 0x10); + fsp_nvs_save(); + build_hobs(300, 0x20); + g_cmos = 0xA5; + g_fail_erase = 1; + fsp_nvs_save(); + fsp_nvs_boot_ok(); + ck_assert_uint_eq(g_cmos, 0xA5); + g_fail_erase = 0; + fsp_nvs_save(); + fsp_nvs_boot_ok(); + ck_assert_uint_eq(g_cmos, 0); +} +END_TEST + START_TEST(test_tpm_reference_follows_the_record) { uint8_t hash[32]; @@ -440,6 +493,7 @@ static Suite *fsp_nvs_save_suite(void) tcase_add_test(tc, test_missing_or_oversized_hob); tcase_add_test(tc, test_zero_length_hob_stops_the_walk); tcase_add_test(tc, test_failed_header_write_leaves_no_record); + tcase_add_test(tc, test_skip_is_kept_until_the_record_is_replaced); tcase_add_test(tc, test_tpm_reference_follows_the_record); tcase_add_test(tc, test_tpm_reference_not_updated_after_a_failed_save); tcase_add_test(tc, test_tpm_reference_index_must_be_platform_owned); From 1a1efa2075cfb8e65a92d9a4c91333a1208f450a Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 12 Nov 2026 18:20:35 -0800 Subject: [PATCH 17/17] nai_68int6: drive the PCIe switch straps right after cache-as-RAM --- src/x86/tgl_fsp.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/x86/tgl_fsp.c b/src/x86/tgl_fsp.c index e7eb9d1127..8b378165c1 100644 --- a/src/x86/tgl_fsp.c +++ b/src/x86/tgl_fsp.c @@ -1781,6 +1781,12 @@ int post_temp_ram_init_cb(void) for (i = 0; i < n; i++) { tgl_gpio_configure(&gpio_table_tempram[i]); } + /* The PCIe switch latches its straps about a quarter of a second after + * reset, so they are driven before anything slow runs. */ + n = sizeof(gpio_table_premem)/sizeof(gpio_table_premem[0]); + for (i = 0; i < n; i++) { + tgl_gpio_configure(&gpio_table_premem[i]); + } #else (void)tgl_gpio_configure; #endif /* TARGET_kontron_vx3060_s2 */