Skip to content

Commit d43be42

Browse files
authored
Merge pull request #55 from wolfSSL/sector_size_fixes
Sector size fixes
2 parents 5a692a7 + c1f0164 commit d43be42

5 files changed

Lines changed: 42 additions & 18 deletions

File tree

arch.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ endif
153153

154154
ifeq ($(TARGET),psoc6)
155155
CORTEX_M0=1
156-
PKA_EXTRA_OBJS+= $(CYPRESS_PDL)/drivers/source/cy_flash.o \
156+
OBJS+= $(CYPRESS_PDL)/drivers/source/cy_flash.o \
157157
$(CYPRESS_PDL)/drivers/source/cy_ipc_pipe.o \
158158
$(CYPRESS_PDL)/drivers/source/cy_ipc_sema.o \
159159
$(CYPRESS_PDL)/drivers/source/cy_ipc_drv.o \

config/examples/cypsoc6.config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ SPMATH?=1
2121
RAM_CODE?=0
2222
DUALBANK_SWAP?=0
2323
IMAGE_HEADER_SIZE?=256
24-
PKA?=1
24+
PKA?=0
2525
WOLFTPM?=0
2626
WOLFBOOT_PARTITION_SIZE?=0x80000
27-
WOLFBOOT_SECTOR_SIZE?=4096
27+
WOLFBOOT_SECTOR_SIZE?=512
2828
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x10080000
2929
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x10100000
3030
WOLFBOOT_PARTITION_SWAP_ADDRESS?=10010000

hal/psoc6.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
#include "psoc6_02_config.h"
3636

37-
#define ROW_SIZE (0x1000)
37+
#define ROW_SIZE (WOLFBOOT_SECTOR_SIZE)
3838
#define FLASH_BASE_ADDRESS (0x10000000)
3939
#define CPU_FREQ (100000000)
4040

@@ -89,8 +89,14 @@ static void hal_set_pll(void)
8989
}
9090
}
9191

92+
93+
9294
void hal_init(void)
9395
{
96+
#define VTOR (*(volatile uint32_t *)(0xE000ED08))
97+
VTOR = FLASH_BASE_ADDRESS;
98+
#undef VTOR
99+
94100
Cy_PDL_Init(CY_DEVICE_CFG);
95101
Cy_Flash_Init();
96102
hal_set_pll();
@@ -109,6 +115,7 @@ void hal_prepare_boot(void)
109115
int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
110116
{
111117
const uint8_t *src = data;
118+
int ret;
112119
if (len < ROW_SIZE)
113120
return -1;
114121
if ((((uint32_t)data) & FLASH_BASE_ADDRESS) == FLASH_BASE_ADDRESS) {
@@ -119,7 +126,9 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
119126
src = psoc6_write_buffer;
120127
}
121128
while (len) {
122-
Cy_Flash_ProgramRow(address, (const uint32_t *) src);
129+
ret = Cy_Flash_ProgramRow(address, (const uint32_t *) src);
130+
if (ret)
131+
return ret;
123132
len -= ROW_SIZE;
124133
if ((len > 0) && (len < ROW_SIZE))
125134
return -1;
@@ -137,14 +146,16 @@ void RAMFUNCTION hal_flash_lock(void)
137146

138147
int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
139148
{
140-
int start = -1, end = -1;
141149
uint32_t end_address;
150+
int ret;
142151
uint32_t p = (uint32_t)address;
143152
if (len == 0)
144153
return -1;
145154
end_address = address + len;
146155
while ((end_address - p) >= ROW_SIZE) {
147-
Cy_Flash_EraseRow(p);
156+
ret = Cy_Flash_EraseRow(p);
157+
if (ret)
158+
return ret;
148159
p += ROW_SIZE;
149160
}
150161
return 0;

include/image.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ int wolfBoot_open_image(struct wolfBoot_image *img, uint8_t part);
6666
int wolfBoot_verify_integrity(struct wolfBoot_image *img);
6767
int wolfBoot_verify_authenticity(struct wolfBoot_image *img);
6868
int wolfBoot_set_partition_state(uint8_t part, uint8_t newst);
69-
int wolfBoot_set_sector_flag(uint8_t part, uint8_t sector, uint8_t newflag);
69+
int wolfBoot_set_sector_flag(uint8_t part, uint16_t sector, uint8_t newflag);
7070
int wolfBoot_get_partition_state(uint8_t part, uint8_t *st);
71-
int wolfBoot_get_sector_flag(uint8_t part, uint8_t sector, uint8_t *flag);
71+
int wolfBoot_get_sector_flag(uint8_t part, uint16_t sector, uint8_t *flag);
7272

7373
uint8_t* wolfBoot_peek_image(struct wolfBoot_image *img, uint32_t offset, uint32_t* sz);
7474

src/libwolfboot.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#endif
3232

3333
uint32_t ext_cache;
34+
static const uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL;
3435

3536
#ifndef TRAILER_SKIP
3637
# define TRAILER_SKIP 0
@@ -56,8 +57,23 @@ int RAMFUNCTION hal_trailer_write(uint32_t addr, uint8_t val) {
5657
ret = hal_flash_write(addr_align, NVM_CACHE, WOLFBOOT_SECTOR_SIZE);
5758
return ret;
5859
}
60+
61+
int RAMFUNCTION hal_set_partition_magic(uint32_t addr)
62+
{
63+
uint32_t off = addr % NVM_CACHE_SIZE;
64+
uint32_t base = addr - off;
65+
int ret;
66+
memcpy(NVM_CACHE, (void *)base, NVM_CACHE_SIZE);
67+
ret = hal_flash_erase(base, WOLFBOOT_SECTOR_SIZE);
68+
if (ret != 0)
69+
return ret;
70+
memcpy(NVM_CACHE + off, &wolfboot_magic_trail, sizeof(uint32_t));
71+
ret = hal_flash_write(base, NVM_CACHE, WOLFBOOT_SECTOR_SIZE);
72+
return ret;
73+
}
5974
#else
6075
# define hal_trailer_write(addr, val) hal_flash_write(addr, (void *)&val, 1)
76+
# define hal_set_partition_magic(addr) hal_flash_write(addr, (void*)&wolfboot_magic_trail, sizeof(uint32_t));
6177
#endif
6278

6379
#if defined EXT_FLASH
@@ -103,19 +119,18 @@ static void RAMFUNCTION set_trailer_at(uint8_t part, uint32_t at, uint8_t val)
103119

104120
static void RAMFUNCTION set_partition_magic(uint8_t part)
105121
{
106-
uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL;
107122
if (part == PART_BOOT) {
108123
if (PARTN_IS_EXT(PART_BOOT)) {
109124
ext_flash_write(PART_BOOT_ENDFLAGS - sizeof(uint32_t), (void *)&wolfboot_magic_trail, sizeof(uint32_t));
110125
} else {
111-
hal_flash_write(PART_BOOT_ENDFLAGS - sizeof(uint32_t), (void *)&wolfboot_magic_trail, sizeof(uint32_t));
126+
hal_set_partition_magic(PART_BOOT_ENDFLAGS - sizeof(uint32_t));
112127
}
113128
}
114129
else if (part == PART_UPDATE) {
115130
if (PARTN_IS_EXT(PART_UPDATE)) {
116131
ext_flash_write(PART_UPDATE_ENDFLAGS - sizeof(uint32_t), (void *)&wolfboot_magic_trail, sizeof(uint32_t));
117132
} else {
118-
hal_flash_write(PART_UPDATE_ENDFLAGS - sizeof(uint32_t), (void *)&wolfboot_magic_trail, sizeof(uint32_t));
133+
hal_set_partition_magic(PART_UPDATE_ENDFLAGS - sizeof(uint32_t));
119134
}
120135
}
121136
}
@@ -143,12 +158,11 @@ static void RAMFUNCTION set_trailer_at(uint8_t part, uint32_t at, uint8_t val)
143158

144159
static void RAMFUNCTION set_partition_magic(uint8_t part)
145160
{
146-
uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL;
147161
if (part == PART_BOOT) {
148-
hal_flash_write(PART_BOOT_ENDFLAGS - sizeof(uint32_t), (void *)&wolfboot_magic_trail, sizeof(uint32_t));
162+
hal_set_partition_magic(PART_BOOT_ENDFLAGS - sizeof(uint32_t));
149163
}
150164
else if (part == PART_UPDATE) {
151-
hal_flash_write(PART_UPDATE_ENDFLAGS - sizeof(uint32_t), (void *)&wolfboot_magic_trail, sizeof(uint32_t));
165+
hal_set_partition_magic(PART_UPDATE_ENDFLAGS - sizeof(uint32_t));
152166
}
153167
}
154168
#endif /* EXT_FLASH */
@@ -193,12 +207,11 @@ int RAMFUNCTION wolfBoot_set_partition_state(uint8_t part, uint8_t newst)
193207
return 0;
194208
}
195209

196-
int RAMFUNCTION wolfBoot_set_sector_flag(uint8_t part, uint8_t sector, uint8_t newflag)
210+
int RAMFUNCTION wolfBoot_set_sector_flag(uint8_t part, uint16_t sector, uint8_t newflag)
197211
{
198212
uint32_t *magic;
199213
uint8_t *flags;
200214
uint8_t fl_value;
201-
const uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL;
202215
uint8_t pos = sector >> 1;
203216
magic = get_partition_magic(part);
204217
if (*magic != wolfboot_magic_trail)
@@ -225,7 +238,7 @@ int RAMFUNCTION wolfBoot_get_partition_state(uint8_t part, uint8_t *st)
225238
return 0;
226239
}
227240

228-
int wolfBoot_get_sector_flag(uint8_t part, uint8_t sector, uint8_t *flag)
241+
int wolfBoot_get_sector_flag(uint8_t part, uint16_t sector, uint8_t *flag)
229242
{
230243
uint32_t *magic;
231244
uint8_t *flags;

0 commit comments

Comments
 (0)