Fix for uSD read to handle start that is not block aligned

pull/655/head
David Garske 2025-12-22 09:38:29 -08:00 committed by Daniele Lacamera
parent 3317fba381
commit afb9c84cdf
5 changed files with 32 additions and 131 deletions

View File

@ -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

View File

@ -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 <<EOF
g
n
@ -906,17 +902,21 @@ Device Start End Sectors Size Type
/dev/sdc4 280576 62332927 62052352 29.6G Linux filesystem
```
2) Build, Sign and copy images
```sh
# make test-app
make test-app/image.elf
# Sign image with version 1
./tools/keytools/sign --ecc384 --sha384 test-app/image.elf wolfboot_signing_private_key.der 1
# Copy signed image to both OFP partitions
sudo dd if=image_v1_signed.bin of=/dev/sdc2 bs=512
sudo dd if=image_v1_signed.bin of=/dev/sdc2 bs=512
sudo dd if=image_v1_signed.bin of=/dev/sdc3 bs=512
# Copy wolfBoot to BIOS boot partition
sudo dd if=wolfboot.bin of=/dev/sdc1 bs=512
```
3) Insert SDCARD into PolarFire and let HSS start wolfBoot. You may need to use `boot sdcard` or configure/build HSS to disable MMC / enable SDCARD.
### Debugging PolarFire Soc

View File

@ -44,8 +44,6 @@
#include "disk.h"
#include "gpt.h"
#define DEBUG_MMC
void hal_init(void)
{
wolfBoot_printf("wolfBoot Version: %s (%s %s)\n",
@ -129,7 +127,7 @@ int ext_flash_erase(uintptr_t address, int len)
}
#endif /* EXT_FLASH */
#ifdef MMU
#if defined(MMU) && !defined(WOLFBOOT_NO_PARTITIONS)
void* hal_get_dts_address(void)
{
return (void*)WOLFBOOT_DTS_BOOT_ADDRESS;
@ -884,11 +882,13 @@ int mmc_init(void)
}
/* returns number of bytes read on success or negative on error */
/* start may not be block aligned and count may not be block multiple */
int disk_read(int drv, uint64_t start, uint32_t count, uint8_t *buf)
{
int status = 0;
uint32_t read_sz, block_addr;
uint32_t tmp_block[EMMC_SD_BLOCK_SIZE/sizeof(uint32_t)];
uint32_t start_offset = (start % EMMC_SD_BLOCK_SIZE);
(void)drv; /* only one drive supported */
#ifdef DEBUG_MMC
@ -902,16 +902,21 @@ int disk_read(int drv, uint64_t start, uint32_t count, uint8_t *buf)
if (read_sz > 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);
}

View File

@ -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

View File

@ -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) {