Skip to content

Commit 07db864

Browse files
committed
EXT flash support: image header mapping
1 parent ec0d170 commit 07db864

4 files changed

Lines changed: 158 additions & 56 deletions

File tree

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ VTOR?=1
1515
SWAP?=1
1616
CORTEX_M0?=0
1717
NO_ASM=0
18+
EXT_FLASH=0
1819

1920
LSCRIPT:=hal/$(TARGET).ld
2021

@@ -68,6 +69,10 @@ ifeq ($(FASTMATH),1)
6869
CFLAGS+=-DUSE_FAST_MATH
6970
endif
7071

72+
ifeq ($(EXT_FLASH),1)
73+
CFLAGS+=-DEXT_FLASH -DPART_UPDATE_EXT
74+
endif
75+
7176
CFLAGS+=-mthumb -Wall -Wextra -Wno-main -Wstack-usage=1024 -ffreestanding -Wno-unused \
7277
-Ilib/bootutil/include -Iinclude/ -Ilib/wolfssl -nostartfiles \
7378
-DWOLFSSL_USER_SETTINGS \

include/image.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,33 @@
1111
#define SECT_FLAG_UPDATED 0x00
1212

1313

14+
#ifdef EXT_FLASH
15+
# ifndef PART_UPDATE_DRIVER
16+
# define PART_UPDATE_DRIVER (&internal_flash_driver)
17+
# endif
18+
# define flash_lock(x) (x->flash_driver.lock())
19+
# define flash_unlock(x) (x->flash_driver.unlock())
20+
# define flash_erase(x, addr, len) (x->flash_driver.erase(addr, len)
21+
# define flash_write(x, addr, data, len) (x->flash_driver.write(addr, data, len)
22+
# define flash_read(x, addr, data, len) (x->flash_driver.read(addr, data, len)
23+
#else
24+
# define flash_lock(x) hal_flash_lock()
25+
# define flash_unlock(x) hal_flash_unlock()
26+
# define flash_erase(x, addr, len) (x->flash_driver.erase(addr, len)
27+
# define flash_write(x, addr, data, len) (x->flash_driver.write(addr, data, len)
28+
# define flash_read(x, addr, data, len) (memcpy(data, addr, len) - data + len)
29+
#endif
30+
31+
struct wolfBoot_flash_driver {
32+
int (*write)(uint32_t address, const uint8_t *data, int len);
33+
int (*read)(uint32_t address, uint8_t *data, int len);
34+
int (*erase)(uint32_t address, int len);
35+
void (*lock)(void);
36+
void (*unlock)(void);
37+
};
38+
39+
extern const struct wolfBoot_flash_driver internal_flash_driver;
40+
1441
struct wolfBoot_image {
1542
uint8_t *hdr;
1643
uint8_t *trailer;
@@ -20,6 +47,7 @@ struct wolfBoot_image {
2047
uint8_t *fw_base;
2148
uint32_t fw_size;
2249
uint8_t part;
50+
struct wolfBoot_flash_driver *flash_driver;
2351
};
2452

2553

@@ -30,6 +58,5 @@ int wolfBoot_set_partition_state(uint8_t part, uint8_t newst);
3058
int wolfBoot_set_sector_flag(uint8_t part, uint8_t sector, uint8_t newflag);
3159
int wolfBoot_get_partition_state(uint8_t part, uint8_t *st);
3260
int wolfBoot_get_sector_flag(uint8_t part, uint8_t sector, uint8_t *flag);
33-
int wolfBoot_copy(uint32_t src, uint32_t dst, uint32_t size);
3461

3562
#endif /* IMAGE_H */

src/image.c

Lines changed: 107 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,9 @@ static int wolfBoot_verify_signature(uint8_t *hash, uint8_t *sig)
8282
}
8383
#endif
8484

85-
86-
static uint8_t get_header(struct wolfBoot_image *img, uint8_t type, uint8_t **ptr)
85+
static uint8_t find_header(uint8_t *haystack, uint8_t type, uint8_t **ptr)
8786
{
88-
uint8_t *p = img->hdr + IMAGE_HEADER_OFFSET;
87+
uint8_t *p = haystack;
8988
while (*p != 0) {
9089
if (*p == HDR_PADDING) {
9190
p++;
@@ -103,33 +102,35 @@ static uint8_t get_header(struct wolfBoot_image *img, uint8_t type, uint8_t **pt
103102
return 0;
104103
}
105104

106-
int wolfBoot_open_image(struct wolfBoot_image *img, uint8_t part)
105+
106+
107+
static uint8_t get_header_ext(struct wolfBoot_image *img, uint8_t type, uint8_t **ptr);
108+
109+
static uint8_t get_header(struct wolfBoot_image *img, uint8_t type, uint8_t **ptr)
107110
{
108-
uint32_t *magic;
109-
uint32_t *size;
110-
if (!img)
111-
return -1;
112-
memset(img, 0, sizeof(struct wolfBoot_image));
111+
#if defined(PART_UPDATE_EXT) && defined(EXT_FLASH)
112+
if(img->part == PART_UPDATE)
113+
return get_header_ext(img, type, ptr);
114+
#endif
115+
return find_header(img->hdr + IMAGE_HEADER_OFFSET, type, ptr);
116+
}
113117

114-
if (part == PART_BOOT)
115-
img->hdr = (void *)WOLFBOOT_PARTITION_BOOT_ADDRESS;
116-
else if (part == PART_UPDATE)
117-
img->hdr = (void *)WOLFBOOT_PARTITION_UPDATE_ADDRESS;
118-
else
119-
return -1;
120-
magic = (uint32_t *)(img->hdr);
121-
if (*magic != WOLFBOOT_MAGIC)
122-
return -1;
123-
size = (uint32_t *)(img->hdr + sizeof (uint32_t));
124-
125-
if (*size >= WOLFBOOT_PARTITION_SIZE)
126-
return -1;
127-
img->part = part;
128-
img->hdr_ok = 1;
129-
img->fw_size = *size;
130-
img->fw_base = img->hdr + IMAGE_HEADER_SIZE;
131-
img->trailer = img->hdr + WOLFBOOT_PARTITION_SIZE;
132-
return 0;
118+
119+
120+
121+
static uint8_t ext_hash_block[SHA256_BLOCK_SIZE];
122+
123+
static uint8_t *get_sha_block(struct wolfBoot_image *img, uint32_t offset)
124+
{
125+
if (offset > img->fw_size)
126+
return NULL;
127+
#ifdef PART_UPDATE_EXT
128+
if (img->part == PART_UPDATE) {
129+
ext_flash_read(img->fw_base + offset, ext_hash_block, SHA256_BLOCK_SIZE);
130+
return ext_hash_block;
131+
}
132+
#endif
133+
return (uint8_t *)(img->fw_base + offset);
133134
}
134135

135136
static int image_hash(struct wolfBoot_image *img, uint8_t *hash)
@@ -139,10 +140,10 @@ static int image_hash(struct wolfBoot_image *img, uint8_t *hash)
139140
uint8_t *p = img->hdr;
140141
uint8_t *fw_end = img->fw_base + img->fw_size;
141142
int blksz;
143+
uint32_t position = 0;
142144
wc_Sha256 sha256_ctx;
143145
if (!img || !img->hdr)
144146
return -1;
145-
146147
stored_sha_len = get_header(img, HDR_SHA256, &stored_sha);
147148
if (stored_sha_len != SHA256_DIGEST_SIZE)
148149
return -1;
@@ -155,14 +156,17 @@ static int image_hash(struct wolfBoot_image *img, uint8_t *hash)
155156
wc_Sha256Update(&sha256_ctx, p, blksz);
156157
p += blksz;
157158
}
158-
p = img->fw_base;
159-
while(p < fw_end) {
159+
do {
160+
p = get_sha_block(img, position);
161+
if (p == NULL)
162+
break;
160163
blksz = SHA256_BLOCK_SIZE;
161-
if (fw_end - p < blksz)
162-
blksz = fw_end - p;
164+
if (position + blksz > img->fw_size)
165+
blksz = img->fw_size - position;
163166
wc_Sha256Update(&sha256_ctx, p, blksz);
164-
p += blksz;
165-
}
167+
position += blksz;
168+
} while(position < img->fw_size);
169+
166170
wc_Sha256Final(&sha256_ctx, hash);
167171
return 0;
168172
}
@@ -185,6 +189,74 @@ static void key_hash(uint8_t *hash)
185189
}
186190

187191
static uint8_t digest[SHA256_DIGEST_SIZE];
192+
static uint8_t verification[IMAGE_SIGNATURE_SIZE];
193+
#ifdef EXT_FLASH
194+
195+
static uint8_t hdr_cpy[IMAGE_HEADER_SIZE];
196+
static int hdr_cpy_done = 0;
197+
198+
static uint8_t *fetch_hdr_cpy(struct wolfBoot_image *img)
199+
{
200+
if (!hdr_cpy_done) {
201+
ext_flash_read(img->hdr, hdr_cpy, IMAGE_HEADER_SIZE);
202+
hdr_cpy_done = 1;
203+
}
204+
return hdr_cpy;
205+
}
206+
207+
static uint8_t get_header_ext(struct wolfBoot_image *img, uint8_t type, uint8_t **ptr)
208+
{
209+
return find_header(fetch_hdr_cpy(img) + IMAGE_HEADER_OFFSET, type, ptr);
210+
}
211+
212+
#endif
213+
214+
215+
static uint8_t *get_img_hdr(struct wolfBoot_image *img)
216+
{
217+
#ifdef PART_UPDATE_EXT
218+
if (img->part == PART_UPDATE) {
219+
return fetch_hdr_cpy(img);
220+
}
221+
#endif
222+
return (uint8_t *)(img->hdr);
223+
}
224+
225+
int wolfBoot_open_image(struct wolfBoot_image *img, uint8_t part)
226+
{
227+
uint32_t *magic;
228+
uint32_t *size;
229+
uint8_t *image;
230+
if (!img)
231+
return -1;
232+
memset(img, 0, sizeof(struct wolfBoot_image));
233+
if (part == PART_BOOT) {
234+
img->hdr = (void *)WOLFBOOT_PARTITION_BOOT_ADDRESS;
235+
image = (uint8_t *)img->hdr;
236+
} else if (part == PART_UPDATE) {
237+
img->hdr = (void *)WOLFBOOT_PARTITION_UPDATE_ADDRESS;
238+
#if defined(PART_UPDATE_EXT)
239+
image = fetch_hdr_cpy(img);
240+
#else
241+
image = (uint8_t *)img->hdr;
242+
#endif
243+
} else
244+
return -1;
245+
magic = (uint32_t *)(image);
246+
if (*magic != WOLFBOOT_MAGIC)
247+
return -1;
248+
size = (uint32_t *)(image + sizeof (uint32_t));
249+
250+
if (*size >= WOLFBOOT_PARTITION_SIZE)
251+
return -1;
252+
img->part = part;
253+
img->hdr_ok = 1;
254+
img->fw_size = *size;
255+
img->fw_base = img->hdr + IMAGE_HEADER_SIZE;
256+
img->trailer = img->hdr + WOLFBOOT_PARTITION_SIZE;
257+
return 0;
258+
}
259+
188260
int wolfBoot_verify_integrity(struct wolfBoot_image *img)
189261
{
190262
uint8_t *stored_sha;
@@ -200,7 +272,6 @@ int wolfBoot_verify_integrity(struct wolfBoot_image *img)
200272
return 0;
201273
}
202274

203-
static uint8_t verification[IMAGE_SIGNATURE_SIZE];
204275
int wolfBoot_verify_authenticity(struct wolfBoot_image *img)
205276
{
206277
uint8_t *stored_signature;
@@ -217,7 +288,6 @@ int wolfBoot_verify_authenticity(struct wolfBoot_image *img)
217288
if (memcmp(digest, pubkey_hint, SHA256_DIGEST_SIZE) != 0)
218289
return -1;
219290
}
220-
221291
if (image_hash(img, digest) != 0)
222292
return -1;
223293
if (wolfBoot_verify_signature(digest, stored_signature) != 0)
@@ -227,20 +297,3 @@ int wolfBoot_verify_authenticity(struct wolfBoot_image *img)
227297
}
228298

229299

230-
int wolfBoot_copy(uint32_t src, uint32_t dst, uint32_t size)
231-
{
232-
uint32_t *orig, *copy;
233-
uint32_t pos = 0;
234-
if (src == dst)
235-
return 0;
236-
if ((src & 0x03) || (dst & 0x03))
237-
return -1;
238-
while (pos < size) {
239-
orig = (uint32_t *)(src + pos);
240-
copy = (uint32_t *)(dst + pos);
241-
while (*orig != *copy)
242-
hal_flash_write(dst + pos, (void *)orig, sizeof(uint32_t));
243-
pos += sizeof(uint32_t);
244-
}
245-
return pos;
246-
}

src/loader.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,26 @@
2222
#include <image.h>
2323
#include <hal.h>
2424

25-
2625
extern void do_boot(const uint32_t *app_offset);
2726

27+
static int wolfBoot_copy(uint32_t src, uint32_t dst, uint32_t size)
28+
{
29+
uint32_t *orig, *copy;
30+
uint32_t pos = 0;
31+
if (src == dst)
32+
return 0;
33+
if ((src & 0x03) || (dst & 0x03))
34+
return -1;
35+
while (pos < size) {
36+
orig = (uint32_t *)(src + pos);
37+
copy = (uint32_t *)(dst + pos);
38+
while (*orig != *copy)
39+
hal_flash_write(dst + pos, (void *)orig, sizeof(uint32_t));
40+
pos += sizeof(uint32_t);
41+
}
42+
return pos;
43+
}
44+
2845
static int wolfBoot_update(void)
2946
{
3047
uint32_t total_size = 0;

0 commit comments

Comments
 (0)