Skip to content

Commit d5783fe

Browse files
committed
Fixed alignment in magic trailer write with NVM_WRITEONCE
1 parent 0081139 commit d5783fe

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

src/libwolfboot.c

Lines changed: 20 additions & 7 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 */
@@ -198,7 +212,6 @@ int RAMFUNCTION wolfBoot_set_sector_flag(uint8_t part, uint8_t sector, uint8_t n
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)

0 commit comments

Comments
 (0)