Skip to content

Commit afb9c84

Browse files
dgarskedanielinux
authored andcommitted
Fix for uSD read to handle start that is not block aligned
1 parent 3317fba commit afb9c84

File tree

5 files changed

+32
-131
lines changed

5 files changed

+32
-131
lines changed

config/examples/polarfire_mpfs250.config

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,19 @@ NO_ARM_ASM?=0
3939
WOLFBOOT_SECTOR_SIZE?=0x1000
4040

4141
# Load Partition to RAM Address
42-
WOLFBOOT_LOAD_ADDRESS?=0xA0000000
42+
WOLFBOOT_LOAD_ADDRESS?=0x80200000
4343

4444
# Partition layout for PolarFire SoC MPFS250T
45-
# TODO: Update with actual flash layout based on your system design
46-
WOLFBOOT_PARTITION_SIZE?=0x10000
47-
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08080000
48-
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x08090000
49-
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x080FF000
45+
# Using update_disk loader we just need to specify the partition number or A/B
46+
WOLFBOOT_NO_PARTITIONS=1
47+
CFLAGS_EXTRA+=-DBOOT_PART_A=1
48+
CFLAGS_EXTRA+=-DBOOT_PART_B=2
5049

5150
# DTS (Device Tree)
52-
WOLFBOOT_LOAD_DTS_ADDRESS?=0x80000000
53-
WOLFBOOT_DTS_BOOT_ADDRESS?=0x20F00000
54-
WOLFBOOT_DTS_UPDATE_ADDRESS?=0x20F00000
51+
WOLFBOOT_LOAD_DTS_ADDRESS?=0x8a000000
5552

56-
#CFLAGS_EXTRA+=-DDEBUG_EXT_FLASH
53+
#CFLAGS_EXTRA+=-DDEBUG_MMC
54+
55+
# Used by test-application for ELF
56+
WOLFBOOT_PARTITION_BOOT_ADDRESS=0x80200000
57+
WOLFBOOT_PARTITION_SIZE=0x4000000

docs/Targets.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -847,13 +847,9 @@ This section describes how to build the test-application, create a custom uSD wi
847847

848848
To use your own application (Linux FIT Image, ELF, etc) just replace test-app/image.elf with your own filename.
849849

850-
```sh
851-
# make test-app
852-
make test-app/image.elf
853-
```
850+
1) Partition uSD card (replace /dev/sdc with your actual media, find using `lsblk`):
854851

855852
```sh
856-
# Partition uSD card
857853
sudo fdisk /dev/sdc <<EOF
858854
g
859855
n
@@ -906,17 +902,21 @@ Device Start End Sectors Size Type
906902
/dev/sdc4 280576 62332927 62052352 29.6G Linux filesystem
907903
```
908904

905+
2) Build, Sign and copy images
909906
```sh
907+
# make test-app
908+
make test-app/image.elf
910909
# Sign image with version 1
911910
./tools/keytools/sign --ecc384 --sha384 test-app/image.elf wolfboot_signing_private_key.der 1
912911
# Copy signed image to both OFP partitions
913912
sudo dd if=image_v1_signed.bin of=/dev/sdc2 bs=512
914-
sudo dd if=image_v1_signed.bin of=/dev/sdc2 bs=512
913+
sudo dd if=image_v1_signed.bin of=/dev/sdc3 bs=512
915914

916915
# Copy wolfBoot to BIOS boot partition
917916
sudo dd if=wolfboot.bin of=/dev/sdc1 bs=512
918917
```
919918

919+
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.
920920

921921
### Debugging PolarFire Soc
922922

hal/mpfs250.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
#include "disk.h"
4545
#include "gpt.h"
4646

47-
#define DEBUG_MMC
48-
4947
void hal_init(void)
5048
{
5149
wolfBoot_printf("wolfBoot Version: %s (%s %s)\n",
@@ -129,7 +127,7 @@ int ext_flash_erase(uintptr_t address, int len)
129127
}
130128
#endif /* EXT_FLASH */
131129

132-
#ifdef MMU
130+
#if defined(MMU) && !defined(WOLFBOOT_NO_PARTITIONS)
133131
void* hal_get_dts_address(void)
134132
{
135133
return (void*)WOLFBOOT_DTS_BOOT_ADDRESS;
@@ -884,11 +882,13 @@ int mmc_init(void)
884882
}
885883

886884
/* returns number of bytes read on success or negative on error */
885+
/* start may not be block aligned and count may not be block multiple */
887886
int disk_read(int drv, uint64_t start, uint32_t count, uint8_t *buf)
888887
{
889888
int status = 0;
890889
uint32_t read_sz, block_addr;
891890
uint32_t tmp_block[EMMC_SD_BLOCK_SIZE/sizeof(uint32_t)];
891+
uint32_t start_offset = (start % EMMC_SD_BLOCK_SIZE);
892892
(void)drv; /* only one drive supported */
893893

894894
#ifdef DEBUG_MMC
@@ -902,16 +902,21 @@ int disk_read(int drv, uint64_t start, uint32_t count, uint8_t *buf)
902902
if (read_sz > EMMC_SD_BLOCK_SIZE) {
903903
read_sz = EMMC_SD_BLOCK_SIZE;
904904
}
905-
if (read_sz < EMMC_SD_BLOCK_SIZE || ((uintptr_t)buf % 4) != 0) {
906-
/* partial or unaligned block read */
905+
if (read_sz < EMMC_SD_BLOCK_SIZE || /* last partial */
906+
start_offset != 0 || /* start not block aligned */
907+
((uintptr_t)buf % 4) != 0) /* buf not 4-byte aligned */
908+
{
909+
/* block read to temporary buffer */
907910
status = mmc_read(MMC_CMD17_READ_SINGLE, block_addr,
908911
tmp_block, EMMC_SD_BLOCK_SIZE);
909912
if (status == 0) {
910-
memcpy(buf, tmp_block, read_sz);
913+
uint8_t* tmp_buf = (uint8_t*)tmp_block;
914+
memcpy(buf, tmp_buf + start_offset, read_sz);
915+
start_offset = 0;
911916
}
912917
}
913918
else {
914-
/* full block read */
919+
/* direct full block read */
915920
status = mmc_read(MMC_CMD17_READ_SINGLE, block_addr,
916921
(uint32_t*)buf, read_sz);
917922
}

src/libwolfboot.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ int wolfBoot_initialize_encryption(void)
136136
#undef WOLFBOOT_FIXED_PARTITIONS
137137
#endif
138138

139-
#ifdef EXT_FLASH
139+
#if defined(EXT_FLASH) && !defined(WOLFBOOT_NO_PARTITIONS)
140140
static uint32_t ext_cache;
141141
#endif
142142

@@ -154,8 +154,8 @@ static uint32_t wb_reverse_word32(uint32_t x)
154154
#endif
155155
#endif
156156

157-
#if defined(WOLFBOOT_FIXED_PARTITIONS) || defined(EXT_FLASH) || \
158-
defined(NVM_FLASH_WRITEONCE)
157+
#if (defined(WOLFBOOT_FIXED_PARTITIONS) || defined(EXT_FLASH) || \
158+
defined(NVM_FLASH_WRITEONCE)) && !defined(WOLFBOOT_NO_PARTITIONS)
159159
static const uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL;
160160
#endif
161161

test-app/app_mpfs250.c

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -30,125 +30,20 @@
3030
#include "wolfboot/wolfboot.h"
3131
#include "target.h"
3232
#include "printf.h"
33-
#include "keystore.h"
3433

3534
#include "../hal/mpfs250.h"
3635

37-
static uint8_t boot_part_state = IMG_STATE_NEW;
38-
static uint8_t update_part_state = IMG_STATE_NEW;
39-
40-
const char part_state_names[6][16] = {
41-
"NEW",
42-
"UPDATING",
43-
"FFLAGS",
44-
"TESTING",
45-
"CONFIRMED",
46-
"[Invalid state]"
47-
};
48-
49-
static const char *part_state_name(uint8_t state)
50-
{
51-
switch(state) {
52-
case IMG_STATE_NEW:
53-
return part_state_names[0];
54-
case IMG_STATE_UPDATING:
55-
return part_state_names[1];
56-
case IMG_STATE_FINAL_FLAGS:
57-
return part_state_names[2];
58-
case IMG_STATE_TESTING:
59-
return part_state_names[3];
60-
case IMG_STATE_SUCCESS:
61-
return part_state_names[4];
62-
default:
63-
return part_state_names[5];
64-
}
65-
}
66-
67-
static int print_info(void)
68-
{
69-
int i, j;
70-
uint32_t cur_fw_version, update_fw_version;
71-
uint32_t n_keys;
72-
uint16_t hdrSz;
73-
74-
cur_fw_version = wolfBoot_current_firmware_version();
75-
update_fw_version = wolfBoot_update_firmware_version();
76-
77-
wolfBoot_get_partition_state(PART_BOOT, &boot_part_state);
78-
wolfBoot_get_partition_state(PART_UPDATE, &update_part_state);
79-
80-
wolfBoot_printf("\r\n");
81-
wolfBoot_printf("System information\r\n");
82-
wolfBoot_printf("====================================\r\n");
83-
wolfBoot_printf("Firmware version : 0x%lx\r\n", wolfBoot_current_firmware_version());
84-
wolfBoot_printf("Current firmware state: %s\r\n", part_state_name(boot_part_state));
85-
if (update_fw_version != 0) {
86-
if (update_part_state == IMG_STATE_UPDATING)
87-
wolfBoot_printf("Candidate firmware version : 0x%lx\r\n", update_fw_version);
88-
else
89-
wolfBoot_printf("Backup firmware version : 0x%lx\r\n", update_fw_version);
90-
wolfBoot_printf("Update state: %s\r\n", part_state_name(update_part_state));
91-
if (update_fw_version > cur_fw_version) {
92-
wolfBoot_printf("'reboot' to initiate update.\r\n");
93-
} else {
94-
wolfBoot_printf("Update image older than current.\r\n");
95-
}
96-
} else {
97-
wolfBoot_printf("No image in update partition.\r\n");
98-
}
99-
100-
wolfBoot_printf("\r\n");
101-
wolfBoot_printf("Bootloader OTP keystore information\r\n");
102-
wolfBoot_printf("====================================\r\n");
103-
n_keys = keystore_num_pubkeys();
104-
wolfBoot_printf("Number of public keys: %lu\r\n", n_keys);
105-
for (i = 0; i < n_keys; i++) {
106-
uint32_t size = keystore_get_size(i);
107-
uint32_t type = keystore_get_key_type(i);
108-
uint32_t mask = keystore_get_mask(i);
109-
uint8_t *keybuf = keystore_get_buffer(i);
110-
111-
wolfBoot_printf("\r\n");
112-
wolfBoot_printf(" Public Key #%d: size %lu, type %lx, mask %08lx\r\n", i,
113-
size, type, mask);
114-
wolfBoot_printf(" ====================================\r\n ");
115-
for (j = 0; j < size; j++) {
116-
wolfBoot_printf("%02X ", keybuf[j]);
117-
if (j % 16 == 15) {
118-
wolfBoot_printf("\r\n ");
119-
}
120-
}
121-
wolfBoot_printf("\r\n");
122-
}
123-
return 0;
124-
}
12536

12637
void main(void)
12738
{
128-
uint32_t app_version;
129-
13039
hal_init();
13140

132-
app_version = wolfBoot_current_firmware_version();
133-
13441
wolfBoot_printf("========================\r\n");
13542
wolfBoot_printf("PolarFire SoC MPFS250 wolfBoot demo Application\r\n");
13643
wolfBoot_printf("Copyright 2025 wolfSSL Inc\r\n");
13744
wolfBoot_printf("GPL v3\r\n");
138-
wolfBoot_printf("Version : 0x%lx\r\n", app_version);
13945
wolfBoot_printf("========================\r\n");
14046

141-
print_info();
142-
143-
if (app_version > 1) {
144-
if (boot_part_state == IMG_STATE_TESTING) {
145-
wolfBoot_printf("Booting new firmware, marking successful boot\n");
146-
147-
/* Mark successful boot, so update won't be rolled back */
148-
wolfBoot_success();
149-
}
150-
}
151-
15247
/* TODO: Add application-specific code here */
15348

15449
while(1) {

0 commit comments

Comments
 (0)