@@ -27,21 +27,40 @@ extern void do_boot(const uint32_t *app_offset);
2727
2828static int wolfBoot_update (void )
2929{
30- uint32_t total_size ;
30+ uint32_t total_size = 0 ;
3131 uint32_t sector_size = WOLFBOOT_SECTOR_SIZE ;
3232 uint32_t sector = 0 ;
3333 uint8_t flag , st ;
34- struct wolfBoot_image update ;
34+ struct wolfBoot_image boot , update ;
3535
36- if ((wolfBoot_open_image (& update , PART_UPDATE ) < 0 ) ||
37- (wolfBoot_verify_integrity (& update ) < 0 ) ||
38- (wolfBoot_verify_authenticity (& update ) < 0 )) {
36+ /* Use biggest size for the swap */
37+ if ((wolfBoot_open_image (& update , PART_UPDATE ) == 0 ) && (update .fw_size + IMAGE_HEADER_SIZE ) > total_size )
38+ total_size = update .fw_size + IMAGE_HEADER_SIZE ;
39+ if ((wolfBoot_open_image (& boot , PART_BOOT ) == 0 ) && (boot .fw_size + IMAGE_HEADER_SIZE ) > total_size )
40+ total_size = boot .fw_size + IMAGE_HEADER_SIZE ;
41+
42+ if (total_size < IMAGE_HEADER_SIZE )
3943 return -1 ;
44+
45+ /* Check the first sector to detect interrupted update */
46+ if ((wolfBoot_get_sector_flag (PART_UPDATE , 0 , & flag ) < 0 ) || (flag == SECT_FLAG_NEW ))
47+ {
48+ /* In case this is a new update, check
49+ * integrity/authenticity of the firmware update
50+ * before starting the swap
51+ */
52+ if (!update .hdr_ok || (wolfBoot_verify_integrity (& update ) < 0 )
53+ || (wolfBoot_verify_authenticity (& update ) < 0 )) {
54+ return -1 ;
55+ }
4056 }
4157
42- total_size = update .fw_size + IMAGE_HEADER_SIZE ;
4358 hal_flash_unlock ();
4459
60+ /* Interruptible swap
61+ * The status is saved in the sector flags of the update partition.
62+ * If something goes wrong, the operation will be resumed upon reboot.
63+ */
4564 while ((sector * sector_size ) < total_size ) {
4665 if ((wolfBoot_get_sector_flag (PART_UPDATE , sector , & flag ) != 0 ) || (flag == SECT_FLAG_NEW )) {
4766 flag = SECT_FLAG_SWAPPING ;
@@ -94,12 +113,16 @@ static int wolfBoot_update(void)
94113static void wolfBoot_start (void )
95114{
96115 uint8_t st ;
97- struct wolfBoot_image boot ;
116+ struct wolfBoot_image boot , update ;
98117 if ((wolfBoot_get_partition_state (PART_UPDATE , & st ) == 0 ) && (st == IMG_STATE_UPDATING )) {
99118 wolfBoot_update ();
100119 } else if ((wolfBoot_get_partition_state (PART_BOOT , & st ) == 0 ) && (st == IMG_STATE_TESTING )) {
101120 wolfBoot_update_trigger ();
102- wolfBoot_update ();
121+ if ((wolfBoot_open_image (& update , PART_UPDATE ) < 0 ) ||
122+ (wolfBoot_verify_integrity (& update ) < 0 ) ||
123+ (wolfBoot_verify_authenticity (& update ) < 0 )) {
124+ wolfBoot_update ();
125+ }
103126 }
104127 if ((wolfBoot_open_image (& boot , PART_BOOT ) < 0 ) ||
105128 (wolfBoot_verify_integrity (& boot ) < 0 ) ||
0 commit comments