Skip to content

Commit 1f196bd

Browse files
committed
Major bug fixes in new update code
1 parent 24f8c09 commit 1f196bd

3 files changed

Lines changed: 14 additions & 14 deletions

File tree

hal/nrf52.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ int hal_flash_write(uint32_t address, const uint8_t *data, int len)
6868
*((uint32_t *)(address - off)) = first;
6969
flash_wait_complete();
7070
address += 4 - off;
71-
data += off;
71+
data += 4 - off;
72+
len -= 4 - off;
7273
}
7374
if (len > 3) {
7475
uint32_t *src = (uint32_t *)data;
7576
uint32_t *dst = (uint32_t *)address;
76-
len -= off;
7777
words = len / 4;
7878
align = words * 4;
7979
for (i = 0; i < words; i ++) {

src/libwolfboot.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ int wolfBoot_set_sector_flag(uint8_t part, uint8_t sector, uint8_t newflag)
6363
magic = (uint32_t *)(trailer_end - sizeof(uint32_t));
6464
if (*magic != WOLFBOOT_MAGIC_TRAIL)
6565
hal_flash_write((uint32_t)magic, (void *)&wolfboot_magic_trail, sizeof(uint32_t));
66-
flags = (trailer_end - sizeof(uint32_t)) - pos;
66+
flags = (trailer_end - sizeof(uint32_t)) - (2 + pos);
6767
if (sector == (pos << 1))
6868
fl_value = (*flags & 0xF0) | (newflag & 0x0F);
6969
else
@@ -78,7 +78,7 @@ int wolfBoot_get_partition_state(uint8_t part, uint8_t *st)
7878
uint8_t *trailer_end = get_trailer(part);
7979
uint32_t *magic;
8080
uint8_t *state;
81-
if (trailer_end)
81+
if (!trailer_end)
8282
return -1;
8383
magic = (uint32_t *)(trailer_end - sizeof(uint32_t));
8484
if (*magic != WOLFBOOT_MAGIC_TRAIL)
@@ -99,7 +99,7 @@ int wolfBoot_get_sector_flag(uint8_t part, uint8_t sector, uint8_t *flag)
9999
magic = (uint32_t *)(trailer_end - sizeof(uint32_t));
100100
if (*magic != WOLFBOOT_MAGIC_TRAIL)
101101
return -1;
102-
flags = (trailer_end - sizeof(uint32_t)) - pos;
102+
flags = (trailer_end - sizeof(uint32_t)) - (2 + pos);
103103
if (sector == (pos << 1))
104104
*flag = *flags & 0x0F;
105105
else

src/loader.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ static int wolfBoot_update(void)
3232
uint32_t sector = 0;
3333
uint8_t flag, st;
3434
struct wolfBoot_image update;
35-
36-
if ((wolfBoot_open_image(&update, PART_BOOT) < 0) ||
35+
36+
if ((wolfBoot_open_image(&update, PART_UPDATE) < 0) ||
3737
(wolfBoot_verify_integrity(&update) < 0) ||
3838
(wolfBoot_verify_authenticity(&update) < 0)) {
3939
return -1;
@@ -50,7 +50,7 @@ static int wolfBoot_update(void)
5050
WOLFBOOT_PARTITION_SWAP_ADDRESS,
5151
WOLFBOOT_SECTOR_SIZE);
5252
wolfBoot_set_sector_flag(PART_UPDATE, sector, flag);
53-
}
53+
}
5454
if (flag == SECT_FLAG_SWAPPING) {
5555
uint32_t size = total_size - (sector * sector_size);
5656
if (size > sector_size)
@@ -95,6 +95,12 @@ static void wolfBoot_start(void)
9595
{
9696
uint8_t st;
9797
struct wolfBoot_image boot;
98+
if ((wolfBoot_get_partition_state(PART_UPDATE, &st) == 0) && (st == IMG_STATE_UPDATING)) {
99+
wolfBoot_update();
100+
} else if ((wolfBoot_get_partition_state(PART_BOOT, &st) == 0) && (st == IMG_STATE_TESTING)) {
101+
wolfBoot_update_trigger();
102+
wolfBoot_update();
103+
}
98104
if ((wolfBoot_open_image(&boot, PART_BOOT) < 0) ||
99105
(wolfBoot_verify_integrity(&boot) < 0) ||
100106
(wolfBoot_verify_authenticity(&boot) < 0)) {
@@ -103,12 +109,6 @@ static void wolfBoot_start(void)
103109
/* panic */;
104110
}
105111
}
106-
if ((wolfBoot_get_partition_state(PART_UPDATE, &st) == 0) && (st == IMG_STATE_UPDATING)) {
107-
wolfBoot_update();
108-
} else if ((wolfBoot_get_partition_state(PART_BOOT, &st) == 0) && (st == IMG_STATE_TESTING)) {
109-
wolfBoot_update_trigger();
110-
wolfBoot_update();
111-
}
112112
do_boot((void *)boot.fw_base);
113113
}
114114

0 commit comments

Comments
 (0)