Skip to content

Commit b82088f

Browse files
committed
moved self-update code to update_flash module
1 parent 9eb5f47 commit b82088f

2 files changed

Lines changed: 69 additions & 188 deletions

File tree

src/loader.c

Lines changed: 0 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -165,196 +165,8 @@ static int wolfBoot_update(int fallback_allowed)
165165
hal_flash_lock();
166166
return 0;
167167
}
168-
169-
#else /* DUALBANK_SWAP */
170-
171-
static inline void boot_panic(void)
172-
{
173-
while(1)
174-
;
175-
}
176-
177-
static void RAMFUNCTION wolfBoot_start(void)
178-
{
179-
int ret;
180-
struct wolfBoot_image fw_image, boot_image;
181-
uint8_t p_state;
182-
uint32_t boot_v, update_v;
183-
int candidate = PART_BOOT;
184-
int fallback_is_possible = 0;
185-
186-
/* Find the candidate */
187-
boot_v = wolfBoot_current_firmware_version();
188-
update_v = wolfBoot_update_firmware_version();
189-
190-
/* panic if no images available */
191-
if ((boot_v == 0) && (update_v == 0))
192-
boot_panic();
193-
194-
else if (boot_v == 0) /* No primary image */
195-
{
196-
candidate = PART_UPDATE;
197-
}
198-
else if ((boot_v > 0) && (update_v > 0)) {
199-
fallback_is_possible = 1;
200-
if (update_v > boot_v)
201-
candidate = PART_UPDATE;
202-
}
203-
204-
/* Check current status for failure (still in TESTING), and fall-back
205-
* if an alternative is available
206-
*/
207-
if (fallback_is_possible &&
208-
(wolfBoot_get_partition_state(candidate, &p_state) == 0) &&
209-
(p_state == IMG_STATE_TESTING))
210-
{
211-
candidate ^= 1; /* switch to other partition if available */
212-
}
213-
214-
for (;;) {
215-
if ((wolfBoot_open_image(&fw_image, candidate) < 0) ||
216-
(wolfBoot_verify_integrity(&fw_image) < 0) ||
217-
(wolfBoot_verify_authenticity(&fw_image) < 0)) {
218-
219-
/* panic if authentication fails and no backup */
220-
if (!fallback_is_possible)
221-
boot_panic();
222-
else {
223-
/* Invalidate failing image and switch to the
224-
* other partition
225-
*/
226-
fallback_is_possible = 0;
227-
wolfBoot_erase_partition(candidate);
228-
candidate ^= 1;
229-
}
230-
} else
231-
break; /* candidate successfully authenticated */
232-
}
233-
234-
/* First time we boot this update, set to TESTING to await
235-
* confirmation from the system
236-
*/
237-
if ((wolfBoot_get_partition_state(candidate, &p_state) == 0) &&
238-
(p_state == IMG_STATE_UPDATING))
239-
{
240-
hal_flash_unlock();
241-
wolfBoot_set_partition_state(candidate, IMG_STATE_TESTING);
242-
hal_flash_lock();
243-
}
244-
245-
/* Booting from update is possible via HW-assisted swap */
246-
if (candidate == PART_UPDATE)
247-
hal_flash_dualbank_swap();
248-
249-
hal_prepare_boot();
250-
do_boot((void *)WOLFBOOT_PARTITION_BOOT_ADDRESS + IMAGE_HEADER_SIZE);
251-
}
252-
#endif
253-
254-
#ifdef RAM_CODE
255-
256-
static void RAMFUNCTION wolfBoot_erase_bootloader(void)
257-
{
258-
uint32_t *start = (uint32_t *)&_start_text;
259-
uint32_t len = WOLFBOOT_PARTITION_BOOT_ADDRESS - (uint32_t)start;
260-
hal_flash_erase((uint32_t)start, len);
261-
262-
}
263-
264-
#include <string.h>
265-
266-
static void RAMFUNCTION wolfBoot_self_update(struct wolfBoot_image *src)
267-
{
268-
uint32_t pos = 0;
269-
uint32_t src_offset = IMAGE_HEADER_SIZE;
270-
271-
hal_flash_unlock();
272-
wolfBoot_erase_bootloader();
273-
#ifdef EXT_FLASH
274-
while (pos < src->fw_size) {
275-
if (PART_IS_EXT(src)) {
276-
uint8_t buffer[FLASHBUFFER_SIZE];
277-
if (src_offset + pos < (src->fw_size + IMAGE_HEADER_SIZE + FLASHBUFFER_SIZE)) {
278-
ext_flash_read((uint32_t)(src->hdr) + src_offset + pos, (void *)buffer, FLASHBUFFER_SIZE);
279-
hal_flash_write(pos + (uint32_t)&_start_text, buffer, FLASHBUFFER_SIZE);
280-
}
281-
pos += FLASHBUFFER_SIZE;
282-
}
283-
goto lock_and_reset;
284-
}
285168
#endif
286-
while (pos < src->fw_size) {
287-
if (src_offset + pos < (src->fw_size + IMAGE_HEADER_SIZE + FLASHBUFFER_SIZE)) {
288-
uint8_t *orig = (uint8_t*)(src->hdr + src_offset + pos);
289-
hal_flash_write(pos + (uint32_t)&_start_text, orig, FLASHBUFFER_SIZE);
290-
}
291-
pos += FLASHBUFFER_SIZE;
292-
}
293-
294-
lock_and_reset:
295-
hal_flash_lock();
296-
arch_reboot();
297-
}
298169

299-
static void wolfBoot_check_self_update(void)
300-
{
301-
uint8_t st;
302-
struct wolfBoot_image update;
303-
uint8_t *update_type;
304-
uint32_t update_version;
305-
306-
/* Check for self update in the UPDATE partition */
307-
if ((wolfBoot_get_partition_state(PART_UPDATE, &st) == 0) && (st == IMG_STATE_UPDATING) &&
308-
(wolfBoot_open_image(&update, PART_UPDATE) == 0) &&
309-
wolfBoot_get_image_type(PART_UPDATE) == (HDR_IMG_TYPE_WOLFBOOT | HDR_IMG_TYPE_AUTH)) {
310-
uint32_t update_version = wolfBoot_update_firmware_version();
311-
if (update_version <= wolfboot_version) {
312-
hal_flash_unlock();
313-
wolfBoot_erase_partition(PART_UPDATE);
314-
hal_flash_lock();
315-
return;
316-
}
317-
if (wolfBoot_verify_integrity(&update) < 0)
318-
return;
319-
if (wolfBoot_verify_authenticity(&update) < 0)
320-
return;
321-
wolfBoot_self_update(&update);
322-
}
323-
}
324-
#endif /* RAM_CODE for self_update */
325-
326-
#ifndef DUALBANK_SWAP
327-
static void wolfBoot_start(void)
328-
{
329-
uint8_t st;
330-
struct wolfBoot_image boot, update;
331-
332-
#ifdef RAM_CODE
333-
wolfBoot_check_self_update();
334-
#endif
335-
336-
/* Check if the BOOT partition is still in TESTING,
337-
* to trigger fallback.
338-
*/
339-
if ((wolfBoot_get_partition_state(PART_BOOT, &st) == 0) && (st == IMG_STATE_TESTING)) {
340-
wolfBoot_update_trigger();
341-
wolfBoot_update(1);
342-
/* Check for new updates in the UPDATE partition */
343-
} else if ((wolfBoot_get_partition_state(PART_UPDATE, &st) == 0) && (st == IMG_STATE_UPDATING)) {
344-
wolfBoot_update(0);
345-
}
346-
if ((wolfBoot_open_image(&boot, PART_BOOT) < 0) ||
347-
(wolfBoot_verify_integrity(&boot) < 0) ||
348-
(wolfBoot_verify_authenticity(&boot) < 0)) {
349-
if (wolfBoot_update(1) < 0) {
350-
while(1)
351-
/* panic */;
352-
}
353-
}
354-
hal_prepare_boot();
355-
do_boot((void *)boot.fw_base);
356-
}
357-
#endif /* ifndef DUALBANK_SWAP */
358170

359171
int main(void)
360172
{

src/update_flash.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,76 @@
3333
#ifdef RAM_CODE
3434
extern unsigned int _start_text;
3535
static volatile const uint32_t __attribute__((used)) wolfboot_version = WOLFBOOT_VERSION;
36+
37+
static void RAMFUNCTION wolfBoot_erase_bootloader(void)
38+
{
39+
uint32_t *start = (uint32_t *)&_start_text;
40+
uint32_t len = WOLFBOOT_PARTITION_BOOT_ADDRESS - (uint32_t)start;
41+
hal_flash_erase((uint32_t)start, len);
42+
43+
}
44+
45+
#include <string.h>
46+
47+
static void RAMFUNCTION wolfBoot_self_update(struct wolfBoot_image *src)
48+
{
49+
uint32_t pos = 0;
50+
uint32_t src_offset = IMAGE_HEADER_SIZE;
51+
52+
hal_flash_unlock();
53+
wolfBoot_erase_bootloader();
54+
#ifdef EXT_FLASH
55+
while (pos < src->fw_size) {
56+
if (PART_IS_EXT(src)) {
57+
uint8_t buffer[FLASHBUFFER_SIZE];
58+
if (src_offset + pos < (src->fw_size + IMAGE_HEADER_SIZE + FLASHBUFFER_SIZE)) {
59+
ext_flash_read((uint32_t)(src->hdr) + src_offset + pos, (void *)buffer, FLASHBUFFER_SIZE);
60+
hal_flash_write(pos + (uint32_t)&_start_text, buffer, FLASHBUFFER_SIZE);
61+
}
62+
pos += FLASHBUFFER_SIZE;
63+
}
64+
goto lock_and_reset;
65+
}
3666
#endif
67+
while (pos < src->fw_size) {
68+
if (src_offset + pos < (src->fw_size + IMAGE_HEADER_SIZE + FLASHBUFFER_SIZE)) {
69+
uint8_t *orig = (uint8_t*)(src->hdr + src_offset + pos);
70+
hal_flash_write(pos + (uint32_t)&_start_text, orig, FLASHBUFFER_SIZE);
71+
}
72+
pos += FLASHBUFFER_SIZE;
73+
}
74+
75+
lock_and_reset:
76+
hal_flash_lock();
77+
arch_reboot();
78+
}
79+
80+
void wolfBoot_check_self_update(void)
81+
{
82+
uint8_t st;
83+
struct wolfBoot_image update;
84+
uint8_t *update_type;
85+
uint32_t update_version;
86+
87+
/* Check for self update in the UPDATE partition */
88+
if ((wolfBoot_get_partition_state(PART_UPDATE, &st) == 0) && (st == IMG_STATE_UPDATING) &&
89+
(wolfBoot_open_image(&update, PART_UPDATE) == 0) &&
90+
wolfBoot_get_image_type(PART_UPDATE) == (HDR_IMG_TYPE_WOLFBOOT | HDR_IMG_TYPE_AUTH)) {
91+
uint32_t update_version = wolfBoot_update_firmware_version();
92+
if (update_version <= wolfboot_version) {
93+
hal_flash_unlock();
94+
wolfBoot_erase_partition(PART_UPDATE);
95+
hal_flash_lock();
96+
return;
97+
}
98+
if (wolfBoot_verify_integrity(&update) < 0)
99+
return;
100+
if (wolfBoot_verify_authenticity(&update) < 0)
101+
return;
102+
wolfBoot_self_update(&update);
103+
}
104+
}
105+
#endif /* RAM_CODE for self_update */
37106

38107
static int wolfBoot_copy_sector(struct wolfBoot_image *src, struct wolfBoot_image *dst, uint32_t sector)
39108
{

0 commit comments

Comments
 (0)