From 32b66f9368efeb36cd91104301da83e9e64af222 Mon Sep 17 00:00:00 2001 From: Marco Oliverio Date: Wed, 13 Sep 2023 15:16:24 +0000 Subject: [PATCH 01/21] ahci: move SATA memory areas in wolfboot .bss --- src/x86/ahci.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/x86/ahci.c b/src/x86/ahci.c index 0efe87b57..31bd74a41 100644 --- a/src/x86/ahci.c +++ b/src/x86/ahci.c @@ -47,15 +47,18 @@ #define SATA_BASE 0x02200000 #endif /* TARGET_qemu_fsp */ -#define HBA_FIS_BASE (SATA_BASE + 0x100) -#define HBA_CLB_BASE (SATA_BASE + 0x1000) -#define HBA_TBL_BASE (SATA_BASE + 0x200000) #define HBA_FIS_SIZE 0x100 #define HBA_CLB_SIZE 0x400 #define HBA_TBL_SIZE 0x800 +#define HBA_TBL_ALIGN 0x80 -#define HBA_FIS_PORT_SIZE 0x80 +static uint8_t ahci_hba_fis[HBA_FIS_SIZE * AHCI_MAX_PORTS] +__attribute__((aligned(HBA_FIS_SIZE))); +static uint8_t ahci_hba_clb[HBA_CLB_SIZE * AHCI_MAX_PORTS] +__attribute__((aligned(HBA_CLB_SIZE))); +static uint8_t ahci_hba_tbl[HBA_TBL_SIZE * AHCI_MAX_PORTS] +__attribute__((aligned(HBA_TBL_ALIGN))); #define PCI_REG_PCS 0x92 #define PCI_REG_CLK 0x94 @@ -193,6 +196,7 @@ void ahci_dump_port(uint32_t base, int i) void sata_enable(uint32_t base) { volatile uint32_t count; uint32_t cap, ports_impl; + uint32_t fis, clb, tbl; uint8_t sata_only; uint8_t cap_sud; uint32_t n_ports; @@ -201,6 +205,7 @@ void sata_enable(uint32_t base) { uint32_t data; uint32_t reg; + mmio_or32(AHCI_HBA_GHC(base), HBA_GHC_AE); /* Wait until enabled. */ @@ -341,19 +346,21 @@ void sata_enable(uint32_t base) { } while ((reg & AHCI_PORT_CMD_FR) != 0); AHCI_DEBUG_PRINTF("AHCI port: FIS disabled.\r\n"); + clb = (uint32_t)(uintptr_t)(ahci_hba_clb + i * HBA_CLB_SIZE); + fis = (uint32_t)(uintptr_t)(ahci_hba_fis + i * HBA_FIS_SIZE); + tbl = (uint32_t)(uintptr_t)(ahci_hba_tbl + i * HBA_TBL_SIZE); + /* Initialize FIS and CLB address */ mmio_write32(AHCI_PxCLB(base, i), - HBA_CLB_BASE + i * HBA_CLB_SIZE); + (uint32_t)(uintptr_t)(clb)); mmio_write32(AHCI_PxCLBH(base, i), 0); mmio_write32(AHCI_PxFB(base, i), - HBA_FIS_BASE + i * HBA_FIS_SIZE); + (uint32_t)(uintptr_t)(fis)); mmio_write32(AHCI_PxFBH(base, i), 0); - memset((void*)(uintptr_t)(HBA_CLB_BASE + i * HBA_CLB_SIZE), - 0, HBA_CLB_SIZE); - memset((void*)(uintptr_t)(HBA_FIS_BASE + i * HBA_FIS_SIZE), - 0, HBA_FIS_SIZE); + memset((uint8_t*)(uintptr_t)clb, 0, HBA_CLB_SIZE); + memset((uint8_t*)(uintptr_t)fis, 0, HBA_FIS_SIZE); /* Wait until CR is cleared */ do { @@ -377,8 +384,7 @@ void sata_enable(uint32_t base) { int drv; wolfBoot_printf("SATA disk drive detected on AHCI port %d\r\n", i); - drv = ata_drive_new(base, i, HBA_CLB_BASE + i * HBA_CLB_SIZE, - HBA_TBL_BASE + i * HBA_TBL_SIZE, HBA_FIS_BASE + i * HBA_FIS_SIZE); + drv = ata_drive_new(base, i, clb, tbl, fis); if (drv < 0) { wolfBoot_printf("Failed to associate ATA drive to disk\r\n"); } else { From ae7221d3213520d395f325d395706ed0d2b29047 Mon Sep 17 00:00:00 2001 From: Marco Oliverio Date: Thu, 10 Aug 2023 14:01:18 +0000 Subject: [PATCH 02/21] fix: remove stale section include in stage1 linker script --- hal/x86_fsp_tgl_stage1.ld.in | 1 - 1 file changed, 1 deletion(-) diff --git a/hal/x86_fsp_tgl_stage1.ld.in b/hal/x86_fsp_tgl_stage1.ld.in index 67d2f47ce..0b3888c30 100644 --- a/hal/x86_fsp_tgl_stage1.ld.in +++ b/hal/x86_fsp_tgl_stage1.ld.in @@ -42,7 +42,6 @@ SECTIONS .bootloader WOLFBOOT_ORIGIN : { - KEEP(./boot_x86_fsp_start.o(.boot*)) KEEP(./tgl_fsp.o(.boot)) *(.boot*) *(.text*) From 478afe33f3d095898d4eac4a80e52d33d8587f68 Mon Sep 17 00:00:00 2001 From: Marco Oliverio Date: Thu, 10 Aug 2023 14:16:31 +0000 Subject: [PATCH 03/21] x86_fsp: move cflags into options.mk --- arch.mk | 2 -- options.mk | 3 ++- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/arch.mk b/arch.mk index 532f63581..4c5eeb62a 100644 --- a/arch.mk +++ b/arch.mk @@ -580,8 +580,6 @@ ifeq ("${FSP}", "1") --defsym wb_end_bss=`grep _end_bss ../wolfboot.map | awk '{print $$1}'` \ --defsym _stage2_params=`grep _stage2_params ../wolfboot.map | awk '{print $$1}'` LDFLAGS += --no-gc-sections --print-gc-sections -T $(LSCRIPT) -m elf_i386 -Map=loader_stage1.map - CFLAGS+=-DFSP_M_LOAD_BASE=$(FSP_M_LOAD_BASE) - CFLAGS+=-DFSP_S_LOAD_BASE=$(FSP_S_LOAD_BASE) OBJS += src/boot_x86_fsp.o OBJS += src/boot_x86_fsp_start.o OBJS += src/fsp_m.o diff --git a/options.mk b/options.mk index a9cdb076d..26a5910a2 100644 --- a/options.mk +++ b/options.mk @@ -649,7 +649,8 @@ ifeq ($(FSP), 1) PCH_PCR_BASE \ PCI_ECAM_BASE \ FSP_S_UPD_DATA_BASE \ - WOLFBOOT_LOAD_BASE + WOLFBOOT_LOAD_BASE \ + FSP_S_LOAD_BASE # set CFLAGS defines for each x86_fsp option $(foreach option,$(X86_FSP_OPTIONS),$(if $($(option)), $(eval CFLAGS += -D$(option)=$($(option))))) From db806ebae76bbb9d4318f44cd84a1b4f5fdc5454 Mon Sep 17 00:00:00 2001 From: Marco Oliverio Date: Thu, 10 Aug 2023 14:01:57 +0000 Subject: [PATCH 04/21] x86_fsp: tgl: improve FPS download script to avoid repo re-cloning --- tools/x86_fsp/tgl/tgl_download_fsp.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tools/x86_fsp/tgl/tgl_download_fsp.sh b/tools/x86_fsp/tgl/tgl_download_fsp.sh index 321dbb137..c4622e7d6 100755 --- a/tools/x86_fsp/tgl/tgl_download_fsp.sh +++ b/tools/x86_fsp/tgl/tgl_download_fsp.sh @@ -89,7 +89,11 @@ download_ucode() { curl -L -o src/x86/ucode0.bin ${UCODE_URL} } -clone_repo +if [ ! -d "$WORK_DIR/FSP" ] +then + clone_repo + patch_tgl_fsp +fi copy_tgl_fsp download_split_tool split_fsp @@ -99,6 +103,5 @@ rebase_fsp_component "S" ${FSP_S_LOAD_BASE} copy_fsp_component "T" ${FSP_T_BASE} copy_fsp_component "M" ${FSP_M_BASE} copy_fsp_component "S" ${FSP_S_LOAD_BASE} -patch_tgl_fsp copy_fsp_headers download_ucode From ab69e27c17a40471460b8e305033e6a10910d781 Mon Sep 17 00:00:00 2001 From: Marco Oliverio Date: Wed, 13 Sep 2023 16:52:27 +0000 Subject: [PATCH 05/21] x86: tgl: flash layout reorder --- hal/x86_fsp_tgl_stage1.ld.in | 84 +++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 39 deletions(-) diff --git a/hal/x86_fsp_tgl_stage1.ld.in b/hal/x86_fsp_tgl_stage1.ld.in index 0b3888c30..21775bab6 100644 --- a/hal/x86_fsp_tgl_stage1.ld.in +++ b/hal/x86_fsp_tgl_stage1.ld.in @@ -1,14 +1,15 @@ FLASH_SIZE = @BOOTLOADER_PARTITION_SIZE@; FLASH_START = 0x100000000 - @BOOTLOADER_PARTITION_SIZE@; -BOOTLOADER_JUMP32_START = 0xfffff000; -RESETVECTOR_START = 0xffffffec; -FSP_T_ORIGIN = @FSP_T_BASE@; /* default base:size 0xFFFFF000:0x3000 [0xfffff000:0x100002000] */ -FSP_M_ORIGIN = @FSP_M_BASE@; /* default base:size 0xfffdd000:0x22000 [0xfffdd000:0xfffff000] */ -FSP_S_ORIGIN = @FSP_S_BASE@; /* default base:size 0xfffc8000:0x15000 [0xfffdd000:0xfffdd000] */ -FIT_TABLE_PTR = 0xffffffc0; -UCODE0_BASE = @UCODE0_BASE@; FSP_S_UPD_DATA_BASE = @FSP_S_UPD_DATA_BASE@; +UCODE0_BASE = @UCODE0_BASE@; +FIT_TABLE = 0xffe00000; +FSP_S_ORIGIN = @FSP_S_BASE@; WOLFBOOT_ORIGIN = @WOLFBOOT_ORIGIN@; +FSP_T_ORIGIN = @FSP_T_BASE@; +FSP_M_ORIGIN = @FSP_M_BASE@; +JMPTO32_ORIGIN = 0xfffffe00; +FIT_TABLE_PTR = 0xffffffc0; +RESETVECTOR_START = 0xffffffec; OUTPUT_FORMAT(elf32-i386) MEMORY @@ -19,25 +20,37 @@ MEMORY SECTIONS { - .jmpto32 BOOTLOADER_JUMP32_START : + .text FLASH_START : { - _off_boot = ABSOLUTE(.) & 0xffff; - KEEP(*(.jmpto32)) + _wolfboot_flash_start = .; + KEEP(*(.sig_wolfboot_raw*)) + *(.wolfboot) + _wolfboot_flash_end = .; } - .fit_table_tr FIT_TABLE_PTR : + .fsps_upd FSP_S_UPD_DATA_BASE : { - QUAD(fit_table); + KEEP(./fsp_tgl_s_upd.o(.fsps_upd)) } - .reset_vector RESETVECTOR_START : + .ucode_update0 UCODE0_BASE : { - KEEP(*(.reset_vector)) + *(.ucode0) } - .ucode_update0 UCODE0_BASE : + .fit_table FIT_TABLE : { - *(.ucode0) + KEEP(*(.fit)); + . = ALIGN(256*1024); + } + + .fsp_s FSP_S_ORIGIN : + { + _fsp_s_hdr = .; + KEEP(*(.sig_fsp_s*)) + _start_fsp_s = .; + *(.fsp_s) + _end_fsp_s = .; } .bootloader WOLFBOOT_ORIGIN : @@ -51,19 +64,6 @@ SECTIONS . = ALIGN(4); } - .fsps_upd FSP_S_UPD_DATA_BASE : - { - KEEP(./fsp_tgl_s_upd.o(.fsps_upd)) - } - - .text FLASH_START : - { - _wolfboot_flash_start = .; - KEEP(*(.sig_wolfboot_raw*)) - *(.wolfboot) - _wolfboot_flash_end = .; - } - .fsp_t FSP_T_ORIGIN : AT(FSP_T_ORIGIN) { @@ -71,21 +71,27 @@ SECTIONS *(.fsp_t) } - .fsp_s FSP_S_ORIGIN : - { - _fsp_s_hdr = .; - KEEP(*(.sig_fsp_s*)) - _start_fsp_s = .; - *(.fsp_s) - _end_fsp_s = .; - } - .fsp_m FSP_M_ORIGIN : { - _fsp_m_hdr = .; _start_fsp_m = .; *(.fsp_m) _end_fsp_m = .; } + .jmpto32 JMPTO32_ORIGIN : + { + _off_boot = ABSOLUTE(.) & 0xffff; + KEEP(*boot_x86_fsp_start.o(.jmpto32*)); + KEEP(*(.jmpto32)); + } + + .fit_table_tr FIT_TABLE_PTR : + { + QUAD(fit_table); + } + + .reset_vector RESETVECTOR_START : + { + KEEP(*(.reset_vector)) + } } From e0d9e6589206be293fa5d2bb7c1c9ea6a4afd8ab Mon Sep 17 00:00:00 2001 From: Marco Oliverio Date: Wed, 13 Sep 2023 16:58:54 +0000 Subject: [PATCH 06/21] x86: fsp: disable hyperthreading --- src/x86/tgl_fsp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/x86/tgl_fsp.c b/src/x86/tgl_fsp.c index d2210a671..d0133633a 100644 --- a/src/x86/tgl_fsp.c +++ b/src/x86/tgl_fsp.c @@ -330,7 +330,7 @@ static int fsp_set_memory_cfg(FSPM_UPD *udp) mem_cfg->DdiPort2Ddc = 1; mem_cfg->DdiPort4Ddc = 1; mem_cfg->EnableC6Dram = 0; - mem_cfg->HyperThreading = 1; + mem_cfg->HyperThreading = 0; mem_cfg->CpuRatio = 0; mem_cfg->FClkFrequency = 1; mem_cfg->VmxEnable = 0; From 0babaae04ac8d50fa107eff6e08fa505710e4ff7 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Mon, 21 Aug 2023 13:23:32 +0200 Subject: [PATCH 07/21] Stage1: allow signing with ecc384/sha384 --- config/examples/x86_fsp_qemu.config | 5 +++-- config/examples/x86_fsp_qemu_tpm.config | 10 ++++++---- stage1/x86_fsp.mk | 2 +- tools/scripts/qemu64/qemu64dbg.sh | 4 ++-- tools/scripts/qemu64/sign_linux.sh | 4 ++-- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/config/examples/x86_fsp_qemu.config b/config/examples/x86_fsp_qemu.config index 1e92c7c8f..46602adde 100644 --- a/config/examples/x86_fsp_qemu.config +++ b/config/examples/x86_fsp_qemu.config @@ -1,8 +1,8 @@ ARCH=x86_64 TARGET=x86_fsp_qemu WOLFBOOT_SMALL_STACK=1 -SIGN?=ECC256 -HASH?=SHA256 +SIGN?=ECC384 +HASH?=SHA384 DEBUG=1 SPMATH=1 FORCE_32BIT=1 @@ -30,6 +30,7 @@ WOLFBOOT_DATA_ADDRESS=0x1000000 FSP_M_BASE=0xffe30000 FSP_S_BASE=0xffed6000 FSP_T_BASE=0xfffe0000 +FSP_S_LOAD_BASE=0x0FED5F00 WOLFBOOT_ORIGIN=0xfffa0000 LINUX_PAYLOAD=1 diff --git a/config/examples/x86_fsp_qemu_tpm.config b/config/examples/x86_fsp_qemu_tpm.config index 869e1af5e..2cb93f0b6 100644 --- a/config/examples/x86_fsp_qemu_tpm.config +++ b/config/examples/x86_fsp_qemu_tpm.config @@ -1,14 +1,14 @@ ARCH=x86_64 TARGET=x86_fsp_qemu WOLFBOOT_SMALL_STACK=1 -SIGN?=ECC256 -HASH?=SHA256 +SIGN?=ECC384 +HASH?=SHA384 DEBUG=1 SPMATH=1 FORCE_32BIT=1 ENCRYPTION=0 WOLFBOOT_FIXED_PARTITIONS=1 -WOLFBOOT_PARTITION_SIZE=0x800000 +WOLFBOOT_PARTITION_SIZE=0x8000000 WOLFTPM=1 # TPM Keystore options @@ -30,7 +30,8 @@ WOLFBOOT_DATA_ADDRESS=0x1000000 FSP_M_BASE=0xffe30000 FSP_S_BASE=0xffed6000 FSP_T_BASE=0xfffe0000 -WOLFBOOT_ORIGIN=0xffff0000 +FSP_S_LOAD_BASE=0x0FED5F00 +WOLFBOOT_ORIGIN=0xfffa0000 LINUX_PAYLOAD=1 BOOTLOADER_PARTITION_SIZE=0xa0000 @@ -39,3 +40,4 @@ MACHINE_OBJ=src/x86/qemu_fsp.o FSP_T_BIN=./src/x86/fsp_t.bin FSP_M_BIN=./src/x86/fsp_m.bin FSP_S_BIN=./src/x86/fsp_s.bin +STAGE1_AUTH=1 diff --git a/stage1/x86_fsp.mk b/stage1/x86_fsp.mk index bfe1f88e0..6ff7a0567 100644 --- a/stage1/x86_fsp.mk +++ b/stage1/x86_fsp.mk @@ -1,5 +1,5 @@ SIGN_TOOL?=../tools/keytools/sign -SIGN_OPTIONS?=--ecc256 --sha256 +SIGN_OPTIONS?=--ecc384 --sha384 SIGN_KEY?=../wolfboot_signing_private_key.der X86FSP_PATH?=../`dirname $(FSP_M_BIN)` diff --git a/tools/scripts/qemu64/qemu64dbg.sh b/tools/scripts/qemu64/qemu64dbg.sh index b44b8628c..43c35b659 100755 --- a/tools/scripts/qemu64/qemu64dbg.sh +++ b/tools/scripts/qemu64/qemu64dbg.sh @@ -1,5 +1,5 @@ #!/bin/bash -qemu-system-x86_64 -m 8G -machine q35 -serial mon:stdio -nographic \ - -pflash loader.bin -drive id=mydisk,format=raw,file=app.bin,if=none \ +qemu-system-x86_64 -m 1G -machine q35 -serial mon:stdio -nographic \ + -pflash wolfboot_stage1.bin -drive id=mydisk,format=raw,file=app.bin,if=none \ -device ide-hd,drive=mydisk -S -s diff --git a/tools/scripts/qemu64/sign_linux.sh b/tools/scripts/qemu64/sign_linux.sh index fa8db8980..476440746 100755 --- a/tools/scripts/qemu64/sign_linux.sh +++ b/tools/scripts/qemu64/sign_linux.sh @@ -1,7 +1,7 @@ cp /tmp/br-linux-wolfboot/output/images/bzImage . -tools/keytools/sign --ecc256 --sha256 bzImage wolfboot_signing_private_key.der 8 -tools/keytools/sign --ecc256 --sha256 bzImage wolfboot_signing_private_key.der 2 +tools/keytools/sign --ecc384 --sha384 bzImage wolfboot_signing_private_key.der 8 +tools/keytools/sign --ecc384 --sha384 bzImage wolfboot_signing_private_key.der 2 cp base-part-image app.bin dd if=bzImage_v8_signed.bin of=app.bin bs=1k seek=1024 conv=notrunc From 6b4f0be37f0fda3f8c6dcce747fecaf6fbfb2b94 Mon Sep 17 00:00:00 2001 From: Marco Oliverio Date: Thu, 14 Sep 2023 13:52:44 +0000 Subject: [PATCH 08/21] x86: fsp: support .data and .bss in stage1 --- hal/x86_fsp_qemu_stage1.ld.in | 35 ++++++++++++++++++++++++++++++----- src/boot_x86_fsp.c | 20 ++++++++++++++++++++ src/image.c | 12 ------------ 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/hal/x86_fsp_qemu_stage1.ld.in b/hal/x86_fsp_qemu_stage1.ld.in index c65268d3e..743f5a505 100644 --- a/hal/x86_fsp_qemu_stage1.ld.in +++ b/hal/x86_fsp_qemu_stage1.ld.in @@ -6,6 +6,8 @@ FSP_T_ORIGIN = @FSP_T_BASE@; /* default base:size 0xFFFFF000:0x3000 [0xfffff000: FSP_M_ORIGIN = @FSP_M_BASE@; /* default base:size 0xfffdd000:0x22000 [0xfffdd000:0xfffff000] */ FSP_S_ORIGIN = @FSP_S_BASE@; /* default base:size 0xfffc8000:0x15000 [0xfffdd000:0xfffdd000] */ WOLFBOOT_ORIGIN = @WOLFBOOT_ORIGIN@; +DATA_MEM_START = 0x800000; /* 8 MB */ + OUTPUT_FORMAT(elf32-i386) MEMORY @@ -16,15 +18,15 @@ MEMORY SECTIONS { - .jmpto32 BOOTLOADER_JUMP32_START : + .jmpto32 BOOTLOADER_JUMP32_START : { _off_boot = ABSOLUTE(.) & 0xffff; KEEP(*(.jmpto32)) - } + } > FLASH .reset_vector RESETVECTOR_START : { KEEP(*(.reset_vector)) - } + } > FLASH .bootloader WOLFBOOT_ORIGIN : { @@ -32,9 +34,30 @@ SECTIONS *(.text*) *(.rodata*) *(.eh_frame*) - *(.data*) . = ALIGN(256); - } + } > FLASH + + _stored_data = .; + .data DATA_MEM_START : AT (_stored_data) + { + _start_data = .; + KEEP(*(.data*)) + . = ALIGN(4); + KEEP(*(.ramcode)) + . = ALIGN(4); + _end_data = .; + } > RAM + + .bss(NOLOAD) : AT (_end_data) + { + _start_bss = .; + __bss_start__ = .; + *(.bss*) + . = ALIGN(4); + _end_bss = .; + __bss_end__ = .; + _end = .; + } > RAM .wolfboot FLASH_START : { @@ -67,4 +90,6 @@ SECTIONS _end_fsp_m = .; } + + } diff --git a/src/boot_x86_fsp.c b/src/boot_x86_fsp.c index ad9f433e1..7ca94f849 100644 --- a/src/boot_x86_fsp.c +++ b/src/boot_x86_fsp.c @@ -99,6 +99,9 @@ extern uint8_t _end_fsp_s[]; extern uint8_t _wolfboot_flash_start[]; extern uint8_t _wolfboot_flash_end[]; extern uint8_t wb_end_bss[], wb_start_bss[]; +extern uint8_t _stored_data[], _start_data[], _end_data[]; +extern uint8_t _start_bss[], _end_bss[]; + extern int main(void); /*! @@ -278,6 +281,8 @@ static void memory_ready_entry(void *ptr) int ret; uint8_t *fsp_s_base; uint8_t *fsp_m_base; + uint32_t *datamem_p; + uint32_t *dataflash_p; fsp_m_base = _start_fsp_m; fsp_s_base = (uint8_t *)(FSP_S_LOAD_BASE); @@ -292,6 +297,21 @@ static void memory_ready_entry(void *ptr) panic(); } + /* Copy data / zero bss */ + datamem_p = (uint32_t *)_start_data; + dataflash_p = (uint32_t *)_stored_data; + while(datamem_p < (uint32_t *)_end_data) { + *(datamem_p++) = *(dataflash_p++); + } + memset(_start_bss, 0, (_end_bss - _start_bss)); + + +#if defined(STAGE1_AUTH) && defined (WOLFBOOT_TPM) + /* TODO: Call TPM Init for STAGE1 */ + wolfBoot_printf("Initializing WOLFBOOT_TPM" ENDLINE); + wolfBoot_tpm2_init(); +#endif + /* Load FSP_S to RAM */ load_fsp_s_to_ram(); diff --git a/src/image.c b/src/image.c index 2cb0d7502..2b81eb6b6 100644 --- a/src/image.c +++ b/src/image.c @@ -947,10 +947,6 @@ int wolfBoot_verify_integrity(struct wolfBoot_image *img) { uint8_t *stored_sha; uint16_t stored_sha_len; -#ifdef STAGE1_AUTH - /* Override global */ - uint8_t digest[WOLFBOOT_SHA_DIGEST_SIZE]; -#endif stored_sha_len = get_header(img, WOLFBOOT_SHA_HDR, &stored_sha); if (stored_sha_len != WOLFBOOT_SHA_DIGEST_SIZE) return -1; @@ -991,10 +987,6 @@ int wolfBoot_verify_authenticity(struct wolfBoot_image *img) uint32_t key_mask = 0U; uint32_t image_part = 1U; int key_slot; -#ifdef STAGE1_AUTH - /* Override global */ - uint8_t digest[WOLFBOOT_SHA_DIGEST_SIZE]; -#endif stored_signature_size = get_header(img, HDR_SIGNATURE, &stored_signature); if (stored_signature_size != IMAGE_SIGNATURE_SIZE) @@ -1095,10 +1087,6 @@ uint8_t* wolfBoot_peek_image(struct wolfBoot_image *img, uint32_t offset, int keyslot_id_by_sha(const uint8_t *hint) { int id; -#ifdef STAGE1_AUTH - /* Override global */ - uint8_t digest[WOLFBOOT_SHA_DIGEST_SIZE]; -#endif for (id = 0; id < keystore_num_pubkeys(); id++) { key_hash(id, digest); From e2a6899de3e6c8192868efdd45301a331c5cbd60 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Tue, 22 Aug 2023 12:55:12 +0200 Subject: [PATCH 09/21] Improved code readability; added comments --- src/boot_x86_fsp.c | 182 ++++++++++++++++++++++++++++----------------- 1 file changed, 113 insertions(+), 69 deletions(-) diff --git a/src/boot_x86_fsp.c b/src/boot_x86_fsp.c index 7ca94f849..a5713b104 100644 --- a/src/boot_x86_fsp.c +++ b/src/boot_x86_fsp.c @@ -258,118 +258,155 @@ static inline int verify_payload(uint8_t *base_addr) } return ret; } + + /*! - * \brief Entry point after memory initialization. + * \brief Initialization of .data and .bss sections after memory initialization. * - * This static function serves as the entry point for further execution after the - * memory initialization is completed. + * This static function copies initial values for .data to the corresponding + * section in the linker script, and initializes the .bss section to zero. + * + * This function is called after memory initialization is completed and the stack + * has been remapped. * - * \param ptr Pointer to a parameter structure. */ -static void memory_ready_entry(void *ptr) +static inline void memory_init_data_bss(void) { - struct stage2_parameter *stage2_params = (struct stage2_parameter *)ptr; - uint8_t silicon_init_parameter[FSP_S_PARAM_SIZE]; - struct fsp_info_header *fsp_info_header; - temp_ram_exit_cb TempRamExit; - silicon_init_cb SiliconInit; - notify_phase_cb notifyPhase; - NOTIFY_PHASE_PARAMS param; - uint32_t info[4]; - uint32_t status; - unsigned int i; - int ret; - uint8_t *fsp_s_base; - uint8_t *fsp_m_base; uint32_t *datamem_p; uint32_t *dataflash_p; - - fsp_m_base = _start_fsp_m; - fsp_s_base = (uint8_t *)(FSP_S_LOAD_BASE); - - fsp_info_header = - (struct fsp_info_header *)(fsp_m_base + FSP_INFO_HEADER_OFFSET); - TempRamExit = (temp_ram_exit_cb)(fsp_m_base + - fsp_info_header->TempRamExitEntryOffset); - status = TempRamExit(NULL); - if (status != EFI_SUCCESS) { - wolfBoot_printf("temp ram exit failed" ENDLINE); - panic(); - } - - /* Copy data / zero bss */ datamem_p = (uint32_t *)_start_data; dataflash_p = (uint32_t *)_stored_data; while(datamem_p < (uint32_t *)_end_data) { *(datamem_p++) = *(dataflash_p++); } memset(_start_bss, 0, (_end_bss - _start_bss)); +} -#if defined(STAGE1_AUTH) && defined (WOLFBOOT_TPM) - /* TODO: Call TPM Init for STAGE1 */ - wolfBoot_printf("Initializing WOLFBOOT_TPM" ENDLINE); - wolfBoot_tpm2_init(); -#endif - - /* Load FSP_S to RAM */ - load_fsp_s_to_ram(); - -#ifdef STAGE1_AUTH - /* Verify FSP_S */ - wolfBoot_printf("Authenticating FSP_S at %x..." ENDLINE, - fsp_s_base - IMAGE_HEADER_SIZE); - - if (verify_payload(fsp_s_base - IMAGE_HEADER_SIZE) == 0) - wolfBoot_printf("FSP_S: verified OK." ENDLINE); - else { - panic(); - } -#endif +/*! + * \brief Staging of FSP_S after verification + * + * Setpu the parameters and call FSP Silicon Initialization. + * + * \param fsp_info FSP information header + * \param fsp_s_base the area in RAM where FSP_S has been loaded and verified + * \return EFI_SUCCESS in case of success, -1 otherwise + */ +static int fsp_silicon_init(struct fsp_info_header *fsp_info, uint8_t *fsp_s_base) +{ + uint8_t silicon_init_parameter[FSP_S_PARAM_SIZE]; + silicon_init_cb SiliconInit; + notify_phase_cb notifyPhase; + NOTIFY_PHASE_PARAMS param; + uint32_t status; + unsigned int i; + int ret; - memcpy(silicon_init_parameter, fsp_s_base + fsp_info_header->CfgRegionOffset, + memcpy(silicon_init_parameter, fsp_s_base + fsp_info->CfgRegionOffset, FSP_S_PARAM_SIZE); status = fsp_machine_update_s_parameters(silicon_init_parameter); - - fsp_info_header = - (struct fsp_info_header *)(fsp_s_base + FSP_INFO_HEADER_OFFSET); - SiliconInit = (silicon_init_cb)(fsp_s_base + - fsp_info_header->FspSiliconInitEntryOffset); + fsp_info = (struct fsp_info_header *)(fsp_s_base + FSP_INFO_HEADER_OFFSET); + SiliconInit = (silicon_init_cb)(fsp_s_base + fsp_info->FspSiliconInitEntryOffset); wolfBoot_printf("call silicon..." ENDLINE); status = SiliconInit(silicon_init_parameter); if (status != EFI_SUCCESS) { wolfBoot_printf("failed %x\n", status); - panic(); + return -1; } wolfBoot_printf("success" ENDLINE); pci_enum_do(); notifyPhase = (notify_phase_cb)(fsp_s_base + - fsp_info_header->NotifyPhaseEntryOffset); + fsp_info->NotifyPhaseEntryOffset); param.Phase = EnumInitPhaseAfterPciEnumeration; status = notifyPhase(¶m); if (status != EFI_SUCCESS) { wolfBoot_printf("failed %d: %x\n", __LINE__, status); - panic(); + return -1; } param.Phase = EnumInitPhaseReadyToBoot; status = notifyPhase(¶m); if (status != EFI_SUCCESS) { wolfBoot_printf("failed %d: %x\n", __LINE__, status); - panic(); + return -1; } param.Phase = EnumInitPhaseEndOfFirmware; status = notifyPhase(¶m); if (status != EFI_SUCCESS) { wolfBoot_printf("failed %d: %x\n", __LINE__, status); + return -1; + } + return EFI_SUCCESS; +} + + +/*! + * \brief Entry point after memory initialization. + * + * This static function serves as the entry point for further execution after the + * memory initialization is completed and the stack has been remapped. + * + * \param ptr Pointer to a parameter structure. + */ +static void memory_ready_entry(void *ptr) +{ + struct stage2_parameter *stage2_params = (struct stage2_parameter *)ptr; + struct fsp_info_header *fsp_info; + temp_ram_exit_cb TempRamExit; + uint8_t *fsp_s_base; + uint8_t *fsp_m_base; + uint32_t cpu_info[4]; + uint32_t status; + /* FSP_M is located in flash */ + fsp_m_base = _start_fsp_m; + /* fsp_s is loaded to RAM for validation */ + fsp_s_base = (uint8_t *)(FSP_S_LOAD_BASE); + fsp_info = + (struct fsp_info_header *)(fsp_m_base + FSP_INFO_HEADER_OFFSET); + TempRamExit = (temp_ram_exit_cb)(fsp_m_base + + fsp_info->TempRamExitEntryOffset); + status = TempRamExit(NULL); + if (status != EFI_SUCCESS) { + wolfBoot_printf("temp ram exit failed" ENDLINE); panic(); } - cpuid(0, &info[0], &info[1], &info[2], NULL); - wolfBoot_printf("CPUID(0):%x %x %x\r\n", info[0], info[1], info[2]); - load_wolfboot(); + /* Confirmed memory initialization complete. + * TempRamExit was successful. + * + * Copy .data section to RAM and initialize .bss + */ + memory_init_data_bss(); + + /* Global variables are accessible after this point */ +#if defined(STAGE1_AUTH) && defined (WOLFBOOT_TPM) + /* TPM initialization */ + wolfBoot_printf("Initializing WOLFBOOT_TPM" ENDLINE); + wolfBoot_tpm2_init(); +#endif + /* Load FSP_S to RAM */ + load_fsp_s_to_ram(); +#ifdef STAGE1_AUTH + /* Verify FSP_S */ + wolfBoot_printf("Authenticating FSP_S at %x..." ENDLINE, + fsp_s_base - IMAGE_HEADER_SIZE); + + if (verify_payload(fsp_s_base - IMAGE_HEADER_SIZE) == 0) + wolfBoot_printf("FSP_S: verified OK." ENDLINE); + else { + panic(); + } +#endif + /* Call FSP_S initialization */ + if (fsp_silicon_init(fsp_info, fsp_s_base) != EFI_SUCCESS) + panic(); + /* Get CPUID */ + cpuid(0, &cpu_info[0], &cpu_info[1], &cpu_info[2], NULL); + wolfBoot_printf("CPUID(0):%x %x %x\r\n", cpu_info[0], cpu_info[1], cpu_info[2]); + /* Load stage2 wolfBoot to RAM */ + load_wolfboot(); #ifdef STAGE1_AUTH - /* Verify wolfBoot */ + /* Verify stage2 wolfBoot */ wolfBoot_printf("Authenticating wolfboot at %x..." ENDLINE, WOLFBOOT_LOAD_BASE); if (verify_payload((uint8_t *)WOLFBOOT_LOAD_BASE - IMAGE_HEADER_SIZE) == 0) @@ -378,6 +415,7 @@ static void memory_ready_entry(void *ptr) panic(); } #endif + /* Finalize staging to stage2 */ set_stage2_parameter(stage2_params); jump_into_wolfboot(); } @@ -440,7 +478,6 @@ void start(uint32_t stack_base, uint32_t stack_top, uint64_t timestamp, (void)bist; fsp_m_base = (uint8_t *)(_start_fsp_m); - status = post_temp_ram_init_cb(); if (status != 0) { wolfBoot_printf("post temp ram init cb failed" ENDLINE); @@ -503,7 +540,6 @@ void start(uint32_t stack_base, uint32_t stack_top, uint64_t timestamp, wolfBoot_printf("hoblist@0x%x" ENDLINE, (uint32_t)hobList); stage2_params->hobList = (uint32_t)hobList; - #ifdef WOLFBOOT_64BIT stage2_params->page_table = ((uint32_t)(stage2_params) - x86_paging_get_page_table_size()); @@ -511,9 +547,17 @@ void start(uint32_t stack_base, uint32_t stack_top, uint64_t timestamp, memset((uint8_t*)stage2_params->page_table, 0, x86_paging_get_page_table_size()); #endif /* WOLFBOOT_64BIT */ + /* change_stack_and_invoke() never returns. + * + * Execution here is eventually transferred to memory_ready_entry + * after the stack has been remapped. + */ change_stack_and_invoke(new_stack, memory_ready_entry, (void*)stage2_params); + /* Returning from change_stack_and_invoke() implies a fatal error + * while attempting to remap the stack. + */ wolfBoot_printf("FAIL" ENDLINE); panic(); } From 5db2714eae64e2a200509d3005bbe4cd657e2c72 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Tue, 22 Aug 2023 18:58:36 +0200 Subject: [PATCH 10/21] Moved keystore section to the end of the flash --- hal/x86_fsp_qemu.ld.in | 1 + hal/x86_fsp_qemu_stage1.ld.in | 6 ++++++ tools/keytools/keygen.c | 2 +- tools/test.mk | 2 +- 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/hal/x86_fsp_qemu.ld.in b/hal/x86_fsp_qemu.ld.in index 5e65b15ee..a900f3e36 100644 --- a/hal/x86_fsp_qemu.ld.in +++ b/hal/x86_fsp_qemu.ld.in @@ -14,6 +14,7 @@ SECTIONS _start_text = .; *(.text*) *(.rodata*) + *(.keystore*) *(.data*) . = ALIGN(4); _end_text = .; diff --git a/hal/x86_fsp_qemu_stage1.ld.in b/hal/x86_fsp_qemu_stage1.ld.in index 743f5a505..9494bcb2b 100644 --- a/hal/x86_fsp_qemu_stage1.ld.in +++ b/hal/x86_fsp_qemu_stage1.ld.in @@ -2,6 +2,7 @@ FLASH_SIZE = @BOOTLOADER_PARTITION_SIZE@; FLASH_START = 0x100000000 - @BOOTLOADER_PARTITION_SIZE@; BOOTLOADER_JUMP32_START = 0xfffff000; RESETVECTOR_START = 0xffffffec; +KEYSTORE_START = 0xffffe000; FSP_T_ORIGIN = @FSP_T_BASE@; /* default base:size 0xFFFFF000:0x3000 [0xfffff000:0x100002000] */ FSP_M_ORIGIN = @FSP_M_BASE@; /* default base:size 0xfffdd000:0x22000 [0xfffdd000:0xfffff000] */ FSP_S_ORIGIN = @FSP_S_BASE@; /* default base:size 0xfffc8000:0x15000 [0xfffdd000:0xfffdd000] */ @@ -28,6 +29,11 @@ SECTIONS KEEP(*(.reset_vector)) } > FLASH + .keystore KEYSTORE_START : + { + *(.keystore*) + } > FLASH + .bootloader WOLFBOOT_ORIGIN : { KEEP(*(.boot*)) diff --git a/tools/keytools/keygen.c b/tools/keytools/keygen.c index 600152843..1de5a5071 100644 --- a/tools/keytools/keygen.c +++ b/tools/keytools/keygen.c @@ -126,7 +126,7 @@ const char Cfile_Banner[]="/* Keystore file for wolfBoot, automatically generate "#error Key algorithm mismatch. Remove old keys via 'make keysclean'\n" "#else\n"; -const char Store_hdr[] = "#define NUM_PUBKEYS %d\nconst struct keystore_slot PubKeys[NUM_PUBKEYS] = {\n\n"; +const char Store_hdr[] = "#define NUM_PUBKEYS %d\nconst __attribute__((section(\".keystore\"))) struct keystore_slot PubKeys[NUM_PUBKEYS] = {\n\n"; const char Slot_hdr[] = "\t /* Key associated to file '%s' */\n" "\t{\n\t\t.slot_id = %d,\n\t\t.key_type = %s,\n" "\t\t.part_id_mask = KEY_VERIFY_ALL,\n\t\t.pubkey_size = %s,\n" diff --git a/tools/test.mk b/tools/test.mk index df84bf521..e9dde6008 100644 --- a/tools/test.mk +++ b/tools/test.mk @@ -961,7 +961,7 @@ test-size-all: make keysclean make test-size SIGN=ECC384 NO_ASM=1 LIMIT=15172 make keysclean - make test-size SIGN=ED448 LIMIT=13414 + make test-size SIGN=ED448 LIMIT=13418 make keysclean make test-size SIGN=RSA3072 LIMIT=11386 make keysclean From 686335bab5986a979d063c0c2b5001c40047080f Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Thu, 31 Aug 2023 10:02:30 +0200 Subject: [PATCH 11/21] Updated linker script files for tgl target - keystore moved to the beginning of the flash space - fixed .bss --- hal/x86_fsp_tgl.ld.in | 1 + hal/x86_fsp_tgl_stage1.ld.in | 53 +++++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/hal/x86_fsp_tgl.ld.in b/hal/x86_fsp_tgl.ld.in index 5e65b15ee..a900f3e36 100644 --- a/hal/x86_fsp_tgl.ld.in +++ b/hal/x86_fsp_tgl.ld.in @@ -14,6 +14,7 @@ SECTIONS _start_text = .; *(.text*) *(.rodata*) + *(.keystore*) *(.data*) . = ALIGN(4); _end_text = .; diff --git a/hal/x86_fsp_tgl_stage1.ld.in b/hal/x86_fsp_tgl_stage1.ld.in index 21775bab6..dfedb5293 100644 --- a/hal/x86_fsp_tgl_stage1.ld.in +++ b/hal/x86_fsp_tgl_stage1.ld.in @@ -10,6 +10,9 @@ FSP_M_ORIGIN = @FSP_M_BASE@; JMPTO32_ORIGIN = 0xfffffe00; FIT_TABLE_PTR = 0xffffffc0; RESETVECTOR_START = 0xffffffec; +DATA_MEM_START = 0x800000; /* 8 MB */ +KEYSTORE_START = 0xffffe000; + OUTPUT_FORMAT(elf32-i386) MEMORY @@ -20,23 +23,23 @@ MEMORY SECTIONS { - .text FLASH_START : + .stage2 FLASH_START : { _wolfboot_flash_start = .; KEEP(*(.sig_wolfboot_raw*)) *(.wolfboot) _wolfboot_flash_end = .; - } + } > FLASH .fsps_upd FSP_S_UPD_DATA_BASE : { KEEP(./fsp_tgl_s_upd.o(.fsps_upd)) - } + } > FLASH .ucode_update0 UCODE0_BASE : { *(.ucode0) - } + } > FLASH .fit_table FIT_TABLE : { @@ -51,47 +54,71 @@ SECTIONS _start_fsp_s = .; *(.fsp_s) _end_fsp_s = .; - } + } > FLASH .bootloader WOLFBOOT_ORIGIN : { KEEP(./tgl_fsp.o(.boot)) *(.boot*) + KEYSTORE_START = .; + *(.keystore*) *(.text*) *(.rodata*) *(.eh_frame*) - *(.data*) - . = ALIGN(4); - } + . = ALIGN(256); + } > FLASH + + + _stored_data = .; + .data DATA_MEM_START : AT (_stored_data) + { + _start_data = .; + KEEP(*(.data*)) + . = ALIGN(4); + KEEP(*(.ramcode)) + . = ALIGN(4); + _end_data = .; + } > RAM + + .bss(NOLOAD) : AT (_end_data) + { + _start_bss = .; + __bss_start__ = .; + *(.bss*) + . = ALIGN(4); + _end_bss = .; + __bss_end__ = .; + _end = .; + } > RAM .fsp_t FSP_T_ORIGIN : AT(FSP_T_ORIGIN) { _start_fsp_t = .; *(.fsp_t) - } + } > FLASH .fsp_m FSP_M_ORIGIN : { _start_fsp_m = .; *(.fsp_m) _end_fsp_m = .; - } + } > FLASH .jmpto32 JMPTO32_ORIGIN : { _off_boot = ABSOLUTE(.) & 0xffff; KEEP(*boot_x86_fsp_start.o(.jmpto32*)); KEEP(*(.jmpto32)); - } + } > FLASH .fit_table_tr FIT_TABLE_PTR : { QUAD(fit_table); - } + } > FLASH .reset_vector RESETVECTOR_START : { KEEP(*(.reset_vector)) - } + } > FLASH } From d79ae03fba25a6e546f482b1c3de6dd4ee52c80f Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Thu, 31 Aug 2023 10:10:23 +0200 Subject: [PATCH 12/21] Updated configuration for tgl target --- config/examples/kontron_vx3060_s2.config | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config/examples/kontron_vx3060_s2.config b/config/examples/kontron_vx3060_s2.config index b846c3b9c..989ec0d0f 100644 --- a/config/examples/kontron_vx3060_s2.config +++ b/config/examples/kontron_vx3060_s2.config @@ -1,8 +1,8 @@ ARCH=x86_64 TARGET=kontron_vx3060_s2 WOLFBOOT_SMALL_STACK=0 -SIGN=ECC256 -HASH?=SHA256 +SIGN=ECC384 +HASH=SHA384 DEBUG=1 SPMATH=1 ENCRYPTION=0 @@ -27,7 +27,7 @@ FSP_S_UPD_DATA_BASE=0xffd00000 FSP_T_BASE=0xfff59000 FSP_M_BASE=0xfff60000 -WOLFBOOT_ORIGIN=0xfff40000 +WOLFBOOT_ORIGIN=0xfff20000 # 4 MB BOOTLOADER_PARTITION_SIZE=0x600000 # 12 MB @@ -51,7 +51,7 @@ PCI_ECAM_BASE=0xC0000000 PCI_USE_ECAM=1 PCH_HAS_PCR=1 -WOLFTPM=1 +WOLFTPM=0 64BIT=1 ELF=1 DEBUG_ELF=0 From d9d1492ec80a7c72f0dc7c6b090c0f9e0a2c5102 Mon Sep 17 00:00:00 2001 From: Marco Oliverio Date: Thu, 31 Aug 2023 09:21:36 +0000 Subject: [PATCH 13/21] makefile: add inlcude/target.h dep to keytools target --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index dd9f8992b..cc2a04b91 100644 --- a/Makefile +++ b/Makefile @@ -162,7 +162,7 @@ $(PRIVATE_KEY): $(Q)(test $(SIGN) = NONE) || ("$(KEYGEN_TOOL)" $(KEYGEN_OPTIONS) -g $(PRIVATE_KEY)) || true $(Q)(test $(SIGN) = NONE) && (echo "// SIGN=NONE" > src/keystore.c) || true -keytools: +keytools: include/target.h @echo "Building key tools" @$(MAKE) -C tools/keytools -s clean @$(MAKE) -C tools/keytools -j From e95dad8dfe540b90af6e3f91b912f9452c230faf Mon Sep 17 00:00:00 2001 From: Marco Oliverio Date: Tue, 12 Sep 2023 15:45:04 +0000 Subject: [PATCH 14/21] image.c: fix newline --- src/image.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/image.c b/src/image.c index 2b81eb6b6..525f8202d 100644 --- a/src/image.c +++ b/src/image.c @@ -795,7 +795,7 @@ int wolfBoot_open_image_address(struct wolfBoot_image *img, uint8_t *image) return -1; } img->fw_size = wolfBoot_image_size(image); - wolfBoot_printf("Image size %d\n", (unsigned int)img->fw_size); + wolfBoot_printf("Image size %d\r\n", (unsigned int)img->fw_size); #ifdef WOLFBOOT_FIXED_PARTITIONS if (img->fw_size > (WOLFBOOT_PARTITION_SIZE - IMAGE_HEADER_SIZE)) { wolfBoot_printf("Image size %d > max %d\n", From ce90b3b7dc19826ebe7b23f8bc33c38fe5bf312a Mon Sep 17 00:00:00 2001 From: Marco Oliverio Date: Tue, 12 Sep 2023 14:55:03 +0000 Subject: [PATCH 15/21] x86: paging: add end of reserved memory check if users call the mapping function without specifying a physical page to use, then the function uses a page from a reserved pool. Check that we don't finish them. --- src/x86/paging.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/x86/paging.c b/src/x86/paging.c index cb64ca4cd..87e1f5166 100644 --- a/src/x86/paging.c +++ b/src/x86/paging.c @@ -197,6 +197,10 @@ static void x86_paging_map_page(uint64_t vaddress, uint64_t paddress) if (paddress == 0) { paddress = (uint64_t)mem; mem += PAGE_TABLE_PAGE_SIZE; + if (mem >= _mem + MEM_SIZE) { + wolfBoot_printf("No more pages to satisfy virtual allocation"); + panic(); + } } x86_paging_setup_entry(pl1e, paddress); } From f4411f2fe44fe51375e4e7a98ca9c7377fc08a02 Mon Sep 17 00:00:00 2001 From: Marco Oliverio Date: Tue, 12 Sep 2023 15:03:04 +0000 Subject: [PATCH 16/21] x86: fsp: add more debugging --- include/x86/hob.h | 3 +++ include/x86/paging.h | 1 + src/boot_x86_fsp.c | 4 ++++ src/boot_x86_fsp_payload.c | 2 ++ src/x86/hob.c | 25 +++++++++++++++++++++++++ src/x86/paging.c | 18 ++++++++++++++++++ 6 files changed, 53 insertions(+) diff --git a/include/x86/hob.h b/include/x86/hob.h index 6725551a4..bf7ef416d 100644 --- a/include/x86/hob.h +++ b/include/x86/hob.h @@ -204,4 +204,7 @@ struct efi_hob_resource_descriptor * hob_find_fsp_reserved(struct efi_hob *hoblist); int hob_iterate_memory_map(struct efi_hob *hobList, hob_mem_map_cb cb, void* ctx); +#ifdef DEBUG +void hob_dump_memory_map(struct efi_hob *hobList); +#endif /* DEBUG */ #endif diff --git a/include/x86/paging.h b/include/x86/paging.h index e51038417..3d9cef454 100644 --- a/include/x86/paging.h +++ b/include/x86/paging.h @@ -33,6 +33,7 @@ int x86_paging_set_page_table(); #if !defined(BUILD_LOADER_STAGE1) int x86_paging_map_memory(uint64_t va, uint64_t pa, uint32_t size); +int x86_paging_dump_info(); #endif /* !BUILD_LOADER_STAGE1 */ #endif /* WOLFBOOT_64BIT */ diff --git a/src/boot_x86_fsp.c b/src/boot_x86_fsp.c index a5713b104..c362cfc30 100644 --- a/src/boot_x86_fsp.c +++ b/src/boot_x86_fsp.c @@ -528,6 +528,10 @@ void start(uint32_t stack_base, uint32_t stack_top, uint64_t timestamp, panic(); } +#ifdef DEBUG + hob_dump_memory_map(hobList); +#endif /* DEBUG */ + if (top_address > MEMORY_4GB) { panic(); } diff --git a/src/boot_x86_fsp_payload.c b/src/boot_x86_fsp_payload.c index 22b715eea..545b847f4 100644 --- a/src/boot_x86_fsp_payload.c +++ b/src/boot_x86_fsp_payload.c @@ -111,10 +111,12 @@ void do_boot(const uint32_t *app) /* TODO: to remove */ mptable_setup(); + x86_paging_dump_info(); r = elf_load_image_mmu((uint8_t *)app, &e, mmu_cb); wolfBoot_printf("Elf loaded (ret %d), entry 0x%x_%x\r\n", 0, (uint32_t)(e >> 32), (uint32_t)(e)); + x86_paging_dump_info(); if (r != 0) panic(); diff --git a/src/x86/hob.c b/src/x86/hob.c index 96f33e7c4..dd7c7f2f7 100644 --- a/src/x86/hob.c +++ b/src/x86/hob.c @@ -176,4 +176,29 @@ int hob_iterate_memory_map(struct efi_hob *hobList, hob_mem_map_cb cb, return 0; } + +#ifdef DEBUG_HOB_LIST +typedef int (*hob_mem_map_cb)(uint64_t start, uint64_t length, uint32_t type, + void *ctx); +int hob_print_entry(uint64_t start, uint64_t length, uint32_t type, void *ctx) +{ + (void)ctx; + wolfBoot_printf("entry type: %x\r\n", type); + wolfBoot_printf("start: %x_%x\r\n", (uint32_t)(start >> 32), + (uint32_t)start); + wolfBoot_printf("end: %x_%x\r\n", (uint32_t)((start+length) >> 32), + (uint32_t)(start+length)); + return 0; +} + +void hob_dump_memory_map(struct efi_hob *hobList) +{ + hob_iterate_memory_map(hobList, hob_print_entry, NULL); +} +#else +void hob_dump_memory_map(struct efi_hob *hb) {} +#endif /* DEBUG_HOB_LIST */ + + + #endif /* HOB_C */ diff --git a/src/x86/paging.c b/src/x86/paging.c index 87e1f5166..bd1563ad9 100644 --- a/src/x86/paging.c +++ b/src/x86/paging.c @@ -219,4 +219,22 @@ int x86_paging_map_memory(uint64_t va, uint64_t pa, uint32_t size) return 0; } + +#ifdef DEBUG_PAGING +void x86_paging_dump_info() +{ + wolfBoot_printf("page_table_pages @ %x %xh\r\n", + (uint32_t)((uintptr_t)page_table_pages >> 32), + (uint32_t)(uintptr_t)page_table_pages); + wolfBoot_printf("page_table_pages used: %d\r\n", page_table_page_used); + wolfBoot_printf("mem start @ %x %xh\r\n", + (uint32_t)((uintptr_t)_mem >> 32), + (uint32_t)(uintptr_t)_mem); + wolfBoot_printf("mem curr @ %x %xh\r\n", + (uint32_t)((uintptr_t)mem >> 32), + (uint32_t)(uintptr_t)mem); +} +#else +void x86_paging_dump_info() {} +#endif /* DEBUG_PAGING */ #endif /* !BUILD_LOADER_STAGE1 */ From 9e9e504e6330c89805cf527820b2515a360ed5d0 Mon Sep 17 00:00:00 2001 From: Marco Oliverio Date: Tue, 12 Sep 2023 15:03:46 +0000 Subject: [PATCH 17/21] fsp: remove unused stage2_params declaration --- hal/kontron_vx3060_s2_loader.c | 1 - hal/x86_fsp_qemu.c | 2 -- hal/x86_fsp_qemu_loader.c | 1 - 3 files changed, 4 deletions(-) diff --git a/hal/kontron_vx3060_s2_loader.c b/hal/kontron_vx3060_s2_loader.c index a5f0f8588..7359cb1e1 100644 --- a/hal/kontron_vx3060_s2_loader.c +++ b/hal/kontron_vx3060_s2_loader.c @@ -27,7 +27,6 @@ #ifdef __WOLFBOOT #include -extern uint8_t* _stage2_params[]; static void panic(void); diff --git a/hal/x86_fsp_qemu.c b/hal/x86_fsp_qemu.c index 6e837409f..e5d9d4189 100644 --- a/hal/x86_fsp_qemu.c +++ b/hal/x86_fsp_qemu.c @@ -32,8 +32,6 @@ #include #include -extern uint8_t* _stage2_params[]; - #define PCI_AHCI_BUS 0 #define PCI_AHCI_DEV 31 #define PCI_AHCI_FUN 2 diff --git a/hal/x86_fsp_qemu_loader.c b/hal/x86_fsp_qemu_loader.c index dc22bf1a3..b685fa57b 100644 --- a/hal/x86_fsp_qemu_loader.c +++ b/hal/x86_fsp_qemu_loader.c @@ -27,7 +27,6 @@ #ifdef __WOLFBOOT #include -extern uint8_t* _stage2_params[]; static void panic(void); From d88315c8019345b77092952ad2ffbb0991399c61 Mon Sep 17 00:00:00 2001 From: Marco Oliverio Date: Tue, 12 Sep 2023 15:05:36 +0000 Subject: [PATCH 18/21] fsp: move _stage2_params symbol in wolfboot .bss including the symbol in the C file will ensure that the linker reserves the necessary space. --- hal/x86_fsp_qemu.ld.in | 3 +-- hal/x86_fsp_tgl.ld.in | 3 +-- include/stage1.h | 3 +++ src/boot_x86_fsp.c | 5 +++-- src/boot_x86_fsp_payload.c | 18 +++++++++++++----- 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/hal/x86_fsp_qemu.ld.in b/hal/x86_fsp_qemu.ld.in index a900f3e36..1ba33161f 100644 --- a/hal/x86_fsp_qemu.ld.in +++ b/hal/x86_fsp_qemu.ld.in @@ -30,7 +30,6 @@ SECTIONS . = ALIGN(4); _end_bss = .; __bss_end__ = .; - _end = .; - _stage2_params = .; + _end_wb = .; } } diff --git a/hal/x86_fsp_tgl.ld.in b/hal/x86_fsp_tgl.ld.in index a900f3e36..1ba33161f 100644 --- a/hal/x86_fsp_tgl.ld.in +++ b/hal/x86_fsp_tgl.ld.in @@ -30,7 +30,6 @@ SECTIONS . = ALIGN(4); _end_bss = .; __bss_end__ = .; - _end = .; - _stage2_params = .; + _end_wb = .; } } diff --git a/include/stage1.h b/include/stage1.h index cf41db7a6..41159760a 100644 --- a/include/stage1.h +++ b/include/stage1.h @@ -30,4 +30,7 @@ struct stage2_parameter { #endif }; +/* implemented in src/boot_x86_fsp_payload.c */ +struct stage2_parameter *stage2_get_parameters(); + #endif /* STAGE1_H */ diff --git a/src/boot_x86_fsp.c b/src/boot_x86_fsp.c index c362cfc30..739a1c294 100644 --- a/src/boot_x86_fsp.c +++ b/src/boot_x86_fsp.c @@ -102,7 +102,10 @@ extern uint8_t wb_end_bss[], wb_start_bss[]; extern uint8_t _stored_data[], _start_data[], _end_data[]; extern uint8_t _start_bss[], _end_bss[]; +/* wolfboot symbols */ extern int main(void); +extern uint8_t _stage2_params[]; + /*! * \brief Get the top address from the EFI HOB (Hand-Off Block) list. @@ -179,8 +182,6 @@ static void load_fsp_s_to_ram(void) _fsp_s_hdr, fsp_s_size); } -extern uint8_t _stage2_params[]; - /*! * \brief Set the stage 2 parameter for the WolfBoot bootloader. * diff --git a/src/boot_x86_fsp_payload.c b/src/boot_x86_fsp_payload.c index 545b847f4..efe00775e 100644 --- a/src/boot_x86_fsp_payload.c +++ b/src/boot_x86_fsp_payload.c @@ -58,8 +58,6 @@ uint8_t mb2_boot_info[MAX_MB2_BOOT_INFO_SIZE]; #include #endif /* WOLFBOOT_64BIT */ -extern uint8_t *_stage2_params[]; - #ifdef TARGET_kontron_vx3060_s2 static char *cmdline = "apic=verbose acpi=no pci=debug console=ttyS0,115200 debug"; #elif TARGET_x86_fsp_qemu @@ -68,6 +66,10 @@ static char *cmdline = "console=ttyS0,115200 pci=earlydump debug"; static char *cmdline = "auto"; #endif /* TARGET_kontron_vx3060_s2 */ +/* must be global so the linker will export the symbol. It's used from loader 1 + * to fill the parameters */ +struct stage2_parameter _stage2_params; + /** * @brief Jump to the specified entry point. * @@ -83,6 +85,11 @@ void jump(uintptr_t entry) : "g"(entry)); } +struct stage2_parameter *stage2_get_parameters() +{ + return &_stage2_params; +} + /** * @brief Perform the boot process for the given application. * @@ -94,11 +101,12 @@ void jump(uintptr_t entry) */ void do_boot(const uint32_t *app) { + struct stage2_parameter *stage2_params; + stage2_params = stage2_get_parameters(); #if defined(WOLFBOOT_LINUX_PAYLOAD) mptable_setup(); - load_linux((uint8_t *)app, (struct stage2_parameter*)_stage2_params, - cmdline); + load_linux((uint8_t *)app, stage2_params, cmdline); #elif defined(WOLFBOOT_ELF) int r; uint64_t e; @@ -129,7 +137,7 @@ void do_boot(const uint32_t *app) wolfBoot_printf("mb2 header found at %x\r\n", (uint32_t)(uintptr_t)mb2_header); r = mb2_build_boot_info_header(mb2_boot_info, mb2_header, - (struct stage2_parameter*)_stage2_params, + stage2_params, MAX_MB2_BOOT_INFO_SIZE); if (r != 0) { wolfBoot_printf("can't build multiboot2 header, panicking\r\n"); From e24c372777476c975a17716c1dfb3c8979388240 Mon Sep 17 00:00:00 2001 From: Marco Oliverio Date: Tue, 12 Sep 2023 15:08:10 +0000 Subject: [PATCH 19/21] fsp: remove WOLFBOOT_FIXED_PARTITIONS (and hardcoded size limit) now the size of the image is limited by the available memory only. The image is loaded in RAM just after wolfboot. --- config/examples/kontron_vx3060_s2.config | 3 +- config/examples/x86_fsp_qemu.config | 3 +- .../examples/x86_fsp_qemu_stage1_auth.config | 3 +- config/examples/x86_fsp_qemu_tpm.config | 3 +- .../examples/x86_fsp_qemu_tpm_keystore.config | 3 +- include/stage1.h | 1 + src/boot_x86_fsp.c | 7 +++- src/update_disk.c | 32 +++++++++++++++---- 8 files changed, 37 insertions(+), 18 deletions(-) diff --git a/config/examples/kontron_vx3060_s2.config b/config/examples/kontron_vx3060_s2.config index 989ec0d0f..053a2a8b4 100644 --- a/config/examples/kontron_vx3060_s2.config +++ b/config/examples/kontron_vx3060_s2.config @@ -6,8 +6,7 @@ HASH=SHA384 DEBUG=1 SPMATH=1 ENCRYPTION=0 -WOLFBOOT_FIXED_PARTITIONS=1 -WOLFBOOT_PARTITION_SIZE=0x4000000 +WOLFBOOT_NO_PARTITIONS=1 FORCE_32BIT=1 # 4gb - 12mb (BIOS Region size) diff --git a/config/examples/x86_fsp_qemu.config b/config/examples/x86_fsp_qemu.config index 46602adde..767340035 100644 --- a/config/examples/x86_fsp_qemu.config +++ b/config/examples/x86_fsp_qemu.config @@ -7,8 +7,7 @@ DEBUG=1 SPMATH=1 FORCE_32BIT=1 ENCRYPTION=0 -WOLFBOOT_FIXED_PARTITIONS=1 -WOLFBOOT_PARTITION_SIZE=0x8000000 +WOLFBOOT_NO_PARTITIONS=1 WOLFTPM=0 # TPM Keystore options diff --git a/config/examples/x86_fsp_qemu_stage1_auth.config b/config/examples/x86_fsp_qemu_stage1_auth.config index fcbcb3409..ec209d2ff 100644 --- a/config/examples/x86_fsp_qemu_stage1_auth.config +++ b/config/examples/x86_fsp_qemu_stage1_auth.config @@ -7,8 +7,7 @@ DEBUG=1 SPMATH=1 FORCE_32BIT=1 ENCRYPTION=0 -WOLFBOOT_FIXED_PARTITIONS=1 -WOLFBOOT_PARTITION_SIZE=0x8000000 +WOLFBOOT_NO_PARTITIONS=1 WOLFTPM=0 # TPM Keystore options diff --git a/config/examples/x86_fsp_qemu_tpm.config b/config/examples/x86_fsp_qemu_tpm.config index 2cb93f0b6..ebaa63c0a 100644 --- a/config/examples/x86_fsp_qemu_tpm.config +++ b/config/examples/x86_fsp_qemu_tpm.config @@ -7,8 +7,7 @@ DEBUG=1 SPMATH=1 FORCE_32BIT=1 ENCRYPTION=0 -WOLFBOOT_FIXED_PARTITIONS=1 -WOLFBOOT_PARTITION_SIZE=0x8000000 +WOLFBOOT_NO_PARTITIONS=1 WOLFTPM=1 # TPM Keystore options diff --git a/config/examples/x86_fsp_qemu_tpm_keystore.config b/config/examples/x86_fsp_qemu_tpm_keystore.config index 52100d91e..9b8c11115 100644 --- a/config/examples/x86_fsp_qemu_tpm_keystore.config +++ b/config/examples/x86_fsp_qemu_tpm_keystore.config @@ -7,8 +7,7 @@ DEBUG=1 SPMATH=1 FORCE_32BIT=1 ENCRYPTION=0 -WOLFBOOT_FIXED_PARTITIONS=1 -WOLFBOOT_PARTITION_SIZE=0x800000 +WOLFBOOT_NO_PARTITIONS=1 WOLFTPM=1 # TPM Keystore options diff --git a/include/stage1.h b/include/stage1.h index 41159760a..6bd5ff8f8 100644 --- a/include/stage1.h +++ b/include/stage1.h @@ -27,6 +27,7 @@ struct stage2_parameter { #if WOLFBOOT_FSP uint32_t hobList; uint32_t page_table; + uint32_t tolum; #endif }; diff --git a/src/boot_x86_fsp.c b/src/boot_x86_fsp.c index 739a1c294..4b5c75b06 100644 --- a/src/boot_x86_fsp.c +++ b/src/boot_x86_fsp.c @@ -546,12 +546,17 @@ void start(uint32_t stack_base, uint32_t stack_top, uint64_t timestamp, stage2_params->hobList = (uint32_t)hobList; #ifdef WOLFBOOT_64BIT - stage2_params->page_table = ((uint32_t)(stage2_params) - + stage2_params->page_table = ((uint32_t)(top_address) - x86_paging_get_page_table_size()); stage2_params->page_table = (((uint32_t)stage2_params->page_table) & ~((1 << 12) - 1)); memset((uint8_t*)stage2_params->page_table, 0, x86_paging_get_page_table_size()); + wolfBoot_printf("page table @ 0x%x [length: %x]" ENDLINE, (uint32_t)stage2_params->page_table, x86_paging_get_page_table_size()); + top_address = stage2_params->page_table; #endif /* WOLFBOOT_64BIT */ + stage2_params->tolum = top_address; + + /* change_stack_and_invoke() never returns. * * Execution here is eventually transferred to memory_ready_entry diff --git a/src/update_disk.c b/src/update_disk.c index 86728a303..091021e39 100644 --- a/src/update_disk.c +++ b/src/update_disk.c @@ -41,7 +41,9 @@ #include "hal.h" #include "spi_flash.h" #include "printf.h" +#include "stage1.h" #include "wolfboot/wolfboot.h" +#include #include #include #include @@ -62,7 +64,9 @@ #define MAX_FAILURES 4 -#define IMAGE_PRELOAD_ADDRESS 0x5000100 +/* from the linker, where wolfBoot ends */ +extern uint8_t _end_wb[]; + /** * @brief function for starting the boot process. * @@ -72,6 +76,7 @@ */ void RAMFUNCTION wolfBoot_start(void) { + struct stage2_parameter *stage2_params; struct wolfBoot_image os_image; uint8_t p_hdr[IMAGE_HEADER_SIZE]; int pA_ver = 0, pB_ver = 0; @@ -110,14 +115,15 @@ void RAMFUNCTION wolfBoot_start(void) wolfBoot_printf("Versions, A:%u B:%u\r\n", pA_ver, pB_ver); - if (pB_ver > pA_ver) selected = 1; else selected = 0; - load_address = (uint32_t *)(IMAGE_PRELOAD_ADDRESS - IMAGE_HEADER_SIZE); - + stage2_params = stage2_get_parameters(); + /* load the image just after wolfboot */ + load_address = (uint32_t *)(_end_wb); + wolfBoot_printf("Load address %x\r\n", load_address); do { failures++; if (selected) @@ -139,7 +145,14 @@ void RAMFUNCTION wolfBoot_start(void) /* Dereference img_size from header */ img_size = *( ((uint32_t *)p_hdr) + 1); + if (img_size > + ((uint32_t)(stage2_params->tolum) - (uint32_t)(uintptr_t)load_address)) { + wolfBoot_printf("Image size %d doesn't fit in low memory\r\n", img_size); + break; + } + /* Read the image into RAM */ + wolfBoot_printf("Loading image from disk..."); load_off = 0; do { ret = disk_read(BOOT_DISK, cur_part, load_off, 512, @@ -155,25 +168,30 @@ void RAMFUNCTION wolfBoot_start(void) selected ^= 1; continue; } - + wolfBoot_printf("done.\r\n"); ret = wolfBoot_open_image_address(&os_image, (void *)load_address); if (ret < 0) { wolfBoot_printf("Error parsing loaded image\r\n"); selected ^= 1; continue; } + + wolfBoot_printf("Checking image integrity..."); if (wolfBoot_verify_integrity(&os_image) != 0) { wolfBoot_printf("Error validating integrity for partition %c\r\n", 'A' + selected); selected ^= 1; continue; } + wolfBoot_printf("done.\r\n"); + wolfBoot_printf("Verifying image signature..."); if (wolfBoot_verify_authenticity(&os_image) != 0) { wolfBoot_printf("Error validating authenticity for partition %c\r\n", 'A' + selected); selected ^= 1; continue; } else { + wolfBoot_printf("done.\r\n"); failures = 0; break; /* Success case */ } @@ -183,8 +201,8 @@ void RAMFUNCTION wolfBoot_start(void) panic(); } - wolfBoot_printf("Firmware Valid\n"); - wolfBoot_printf("Booting at %08lx\n", IMAGE_PRELOAD_ADDRESS); + wolfBoot_printf("Firmware Valid.\r\n"); + wolfBoot_printf("Booting at %08lx\r\n", os_image.fw_base); hal_prepare_boot(); do_boot((uint32_t*)os_image.fw_base); From c428d5ad2342120114e645aa3184e352d14f2dce Mon Sep 17 00:00:00 2001 From: Marco Oliverio Date: Wed, 13 Sep 2023 09:26:52 +0000 Subject: [PATCH 20/21] config: tgl: default wolfboot load base to 64 MB To have more room for app loading --- config/examples/kontron_vx3060_s2.config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/examples/kontron_vx3060_s2.config b/config/examples/kontron_vx3060_s2.config index 053a2a8b4..f80702c45 100644 --- a/config/examples/kontron_vx3060_s2.config +++ b/config/examples/kontron_vx3060_s2.config @@ -13,7 +13,7 @@ FORCE_32BIT=1 WOLFBOOT_PARTITION_BOOT_ADDRESS=0xff400000 WOLFBOOT_PARTITION_SWAP_ADDRESS=0x0 WOLFBOOT_PARTITION_UPDATE_ADDRESS=0x0 -WOLFBOOT_LOAD_BASE=0x2000000 +WOLFBOOT_LOAD_BASE=0x4000000 WOLFBOOT_LOAD_ADDRESS=0x1000000 # required for keytools From f5bd675a3e802c337ac2f4ec90a7c4651a8a377b Mon Sep 17 00:00:00 2001 From: Marco Oliverio Date: Wed, 13 Sep 2023 15:21:12 +0000 Subject: [PATCH 21/21] tools: script: make_hd update to use sha384/ecc384 --- tools/x86_fsp/qemu/make_hd.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/x86_fsp/qemu/make_hd.sh b/tools/x86_fsp/qemu/make_hd.sh index 0a75431e5..6759ae02d 100755 --- a/tools/x86_fsp/qemu/make_hd.sh +++ b/tools/x86_fsp/qemu/make_hd.sh @@ -15,7 +15,7 @@ n w EOF # copy bzImage in the root folder -tools/keytools/sign --ecc256 --sha256 ${IMAGE} wolfboot_signing_private_key.der 1 -tools/keytools/sign --ecc256 --sha256 ${IMAGE} wolfboot_signing_private_key.der 2 +tools/keytools/sign --ecc384 --sha384 ${IMAGE} wolfboot_signing_private_key.der 1 +tools/keytools/sign --ecc384 --sha384 ${IMAGE} wolfboot_signing_private_key.der 2 dd if=${IMAGE}_v1_signed.bin of=app.bin bs=512 seek=2048 conv=notrunc dd if=${IMAGE}_v2_signed.bin of=app.bin bs=512 seek=34816 conv=notrunc