From afb9c84cdf755fb2dcb3636d088ffa1f64fd20cd Mon Sep 17 00:00:00 2001 From: David Garske Date: Mon, 22 Dec 2025 09:38:29 -0800 Subject: [PATCH] Fix for uSD read to handle start that is not block aligned --- config/examples/polarfire_mpfs250.config | 21 ++--- docs/Targets.md | 12 +-- hal/mpfs250.c | 19 ++-- src/libwolfboot.c | 6 +- test-app/app_mpfs250.c | 105 ----------------------- 5 files changed, 32 insertions(+), 131 deletions(-) diff --git a/config/examples/polarfire_mpfs250.config b/config/examples/polarfire_mpfs250.config index 48f26e0b..f7dd8922 100644 --- a/config/examples/polarfire_mpfs250.config +++ b/config/examples/polarfire_mpfs250.config @@ -39,18 +39,19 @@ NO_ARM_ASM?=0 WOLFBOOT_SECTOR_SIZE?=0x1000 # Load Partition to RAM Address -WOLFBOOT_LOAD_ADDRESS?=0xA0000000 +WOLFBOOT_LOAD_ADDRESS?=0x80200000 # Partition layout for PolarFire SoC MPFS250T -# TODO: Update with actual flash layout based on your system design -WOLFBOOT_PARTITION_SIZE?=0x10000 -WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08080000 -WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x08090000 -WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x080FF000 +# Using update_disk loader we just need to specify the partition number or A/B +WOLFBOOT_NO_PARTITIONS=1 +CFLAGS_EXTRA+=-DBOOT_PART_A=1 +CFLAGS_EXTRA+=-DBOOT_PART_B=2 # DTS (Device Tree) -WOLFBOOT_LOAD_DTS_ADDRESS?=0x80000000 -WOLFBOOT_DTS_BOOT_ADDRESS?=0x20F00000 -WOLFBOOT_DTS_UPDATE_ADDRESS?=0x20F00000 +WOLFBOOT_LOAD_DTS_ADDRESS?=0x8a000000 -#CFLAGS_EXTRA+=-DDEBUG_EXT_FLASH +#CFLAGS_EXTRA+=-DDEBUG_MMC + +# Used by test-application for ELF +WOLFBOOT_PARTITION_BOOT_ADDRESS=0x80200000 +WOLFBOOT_PARTITION_SIZE=0x4000000 diff --git a/docs/Targets.md b/docs/Targets.md index c5c8a83c..9b77ed17 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -847,13 +847,9 @@ This section describes how to build the test-application, create a custom uSD wi To use your own application (Linux FIT Image, ELF, etc) just replace test-app/image.elf with your own filename. -```sh -# make test-app -make test-app/image.elf -``` +1) Partition uSD card (replace /dev/sdc with your actual media, find using `lsblk`): ```sh -# Partition uSD card sudo fdisk /dev/sdc < EMMC_SD_BLOCK_SIZE) { read_sz = EMMC_SD_BLOCK_SIZE; } - if (read_sz < EMMC_SD_BLOCK_SIZE || ((uintptr_t)buf % 4) != 0) { - /* partial or unaligned block read */ + if (read_sz < EMMC_SD_BLOCK_SIZE || /* last partial */ + start_offset != 0 || /* start not block aligned */ + ((uintptr_t)buf % 4) != 0) /* buf not 4-byte aligned */ + { + /* block read to temporary buffer */ status = mmc_read(MMC_CMD17_READ_SINGLE, block_addr, tmp_block, EMMC_SD_BLOCK_SIZE); if (status == 0) { - memcpy(buf, tmp_block, read_sz); + uint8_t* tmp_buf = (uint8_t*)tmp_block; + memcpy(buf, tmp_buf + start_offset, read_sz); + start_offset = 0; } } else { - /* full block read */ + /* direct full block read */ status = mmc_read(MMC_CMD17_READ_SINGLE, block_addr, (uint32_t*)buf, read_sz); } diff --git a/src/libwolfboot.c b/src/libwolfboot.c index 409b1785..5647d53a 100644 --- a/src/libwolfboot.c +++ b/src/libwolfboot.c @@ -136,7 +136,7 @@ int wolfBoot_initialize_encryption(void) #undef WOLFBOOT_FIXED_PARTITIONS #endif -#ifdef EXT_FLASH +#if defined(EXT_FLASH) && !defined(WOLFBOOT_NO_PARTITIONS) static uint32_t ext_cache; #endif @@ -154,8 +154,8 @@ static uint32_t wb_reverse_word32(uint32_t x) #endif #endif -#if defined(WOLFBOOT_FIXED_PARTITIONS) || defined(EXT_FLASH) || \ - defined(NVM_FLASH_WRITEONCE) +#if (defined(WOLFBOOT_FIXED_PARTITIONS) || defined(EXT_FLASH) || \ + defined(NVM_FLASH_WRITEONCE)) && !defined(WOLFBOOT_NO_PARTITIONS) static const uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL; #endif diff --git a/test-app/app_mpfs250.c b/test-app/app_mpfs250.c index af05c96f..49513c17 100644 --- a/test-app/app_mpfs250.c +++ b/test-app/app_mpfs250.c @@ -30,125 +30,20 @@ #include "wolfboot/wolfboot.h" #include "target.h" #include "printf.h" -#include "keystore.h" #include "../hal/mpfs250.h" -static uint8_t boot_part_state = IMG_STATE_NEW; -static uint8_t update_part_state = IMG_STATE_NEW; - -const char part_state_names[6][16] = { - "NEW", - "UPDATING", - "FFLAGS", - "TESTING", - "CONFIRMED", - "[Invalid state]" -}; - -static const char *part_state_name(uint8_t state) -{ - switch(state) { - case IMG_STATE_NEW: - return part_state_names[0]; - case IMG_STATE_UPDATING: - return part_state_names[1]; - case IMG_STATE_FINAL_FLAGS: - return part_state_names[2]; - case IMG_STATE_TESTING: - return part_state_names[3]; - case IMG_STATE_SUCCESS: - return part_state_names[4]; - default: - return part_state_names[5]; - } -} - -static int print_info(void) -{ - int i, j; - uint32_t cur_fw_version, update_fw_version; - uint32_t n_keys; - uint16_t hdrSz; - - cur_fw_version = wolfBoot_current_firmware_version(); - update_fw_version = wolfBoot_update_firmware_version(); - - wolfBoot_get_partition_state(PART_BOOT, &boot_part_state); - wolfBoot_get_partition_state(PART_UPDATE, &update_part_state); - - wolfBoot_printf("\r\n"); - wolfBoot_printf("System information\r\n"); - wolfBoot_printf("====================================\r\n"); - wolfBoot_printf("Firmware version : 0x%lx\r\n", wolfBoot_current_firmware_version()); - wolfBoot_printf("Current firmware state: %s\r\n", part_state_name(boot_part_state)); - if (update_fw_version != 0) { - if (update_part_state == IMG_STATE_UPDATING) - wolfBoot_printf("Candidate firmware version : 0x%lx\r\n", update_fw_version); - else - wolfBoot_printf("Backup firmware version : 0x%lx\r\n", update_fw_version); - wolfBoot_printf("Update state: %s\r\n", part_state_name(update_part_state)); - if (update_fw_version > cur_fw_version) { - wolfBoot_printf("'reboot' to initiate update.\r\n"); - } else { - wolfBoot_printf("Update image older than current.\r\n"); - } - } else { - wolfBoot_printf("No image in update partition.\r\n"); - } - - wolfBoot_printf("\r\n"); - wolfBoot_printf("Bootloader OTP keystore information\r\n"); - wolfBoot_printf("====================================\r\n"); - n_keys = keystore_num_pubkeys(); - wolfBoot_printf("Number of public keys: %lu\r\n", n_keys); - for (i = 0; i < n_keys; i++) { - uint32_t size = keystore_get_size(i); - uint32_t type = keystore_get_key_type(i); - uint32_t mask = keystore_get_mask(i); - uint8_t *keybuf = keystore_get_buffer(i); - - wolfBoot_printf("\r\n"); - wolfBoot_printf(" Public Key #%d: size %lu, type %lx, mask %08lx\r\n", i, - size, type, mask); - wolfBoot_printf(" ====================================\r\n "); - for (j = 0; j < size; j++) { - wolfBoot_printf("%02X ", keybuf[j]); - if (j % 16 == 15) { - wolfBoot_printf("\r\n "); - } - } - wolfBoot_printf("\r\n"); - } - return 0; -} void main(void) { - uint32_t app_version; - hal_init(); - app_version = wolfBoot_current_firmware_version(); - wolfBoot_printf("========================\r\n"); wolfBoot_printf("PolarFire SoC MPFS250 wolfBoot demo Application\r\n"); wolfBoot_printf("Copyright 2025 wolfSSL Inc\r\n"); wolfBoot_printf("GPL v3\r\n"); - wolfBoot_printf("Version : 0x%lx\r\n", app_version); wolfBoot_printf("========================\r\n"); - print_info(); - - if (app_version > 1) { - if (boot_part_state == IMG_STATE_TESTING) { - wolfBoot_printf("Booting new firmware, marking successful boot\n"); - - /* Mark successful boot, so update won't be rolled back */ - wolfBoot_success(); - } - } - /* TODO: Add application-specific code here */ while(1) {