Skip to content

Commit 64f3a39

Browse files
authored
Merge branch 'master' into swap_tail_type
2 parents 82094c9 + b3f3b53 commit 64f3a39

File tree

4 files changed

+84
-38
lines changed

4 files changed

+84
-38
lines changed

include/printf.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@
4040
#endif
4141
void uart_write(const char* buf, unsigned int sz);
4242

43-
/* turn on small printf support */
43+
/* turn on small printf support in string.c */
4444
#if !defined(PRINTF_ENABLED) && !defined(NO_PRINTF_UART)
4545
#define PRINTF_ENABLED
4646
#endif
4747
#endif
4848

49-
#ifdef PRINTF_ENABLED
49+
/* support for wolfBoot_printf logging */
50+
#if defined(PRINTF_ENABLED) && !defined(WOLFBOOT_NO_PRINTF)
5051
# include <stdio.h>
5152
# if defined(DEBUG_ZYNQ) && !defined(USE_QNX)
5253
# include "xil_printf.h"
@@ -62,8 +63,12 @@
6263
# define wolfBoot_printf(_f_, ...) uart_printf(_f_, ##__VA_ARGS__)
6364
# elif defined(__CCRX__)
6465
# define wolfBoot_printf printf
65-
# else
66+
# elif defined(WOLFBOOT_LOG_PRINTF)
67+
/* allow output to stdout */
6668
# define wolfBoot_printf(_f_, ...) printf(_f_, ##__VA_ARGS__)
69+
# else
70+
/* use stderr by default */
71+
# define wolfBoot_printf(_f_, ...) fprintf(stderr, _f_, ##__VA_ARGS__)
6772
# endif
6873
#else
6974
# define wolfBoot_printf(_f_, ...) do{}while(0)

src/image.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -861,11 +861,12 @@ int wolfBoot_open_image_address(struct wolfBoot_image *img, uint8_t *image)
861861
return -1;
862862
}
863863
img->fw_size = wolfBoot_image_size(image);
864-
wolfBoot_printf("Image size %d\r\n", (unsigned int)img->fw_size);
864+
865865
#ifdef WOLFBOOT_FIXED_PARTITIONS
866866
if (img->fw_size > (WOLFBOOT_PARTITION_SIZE - IMAGE_HEADER_SIZE)) {
867867
wolfBoot_printf("Image size %d > max %d\n",
868-
(unsigned int)img->fw_size, (WOLFBOOT_PARTITION_SIZE - IMAGE_HEADER_SIZE));
868+
(unsigned int)img->fw_size,
869+
(WOLFBOOT_PARTITION_SIZE - IMAGE_HEADER_SIZE));
869870
img->fw_size = 0;
870871
return -1;
871872
}
@@ -881,6 +882,12 @@ int wolfBoot_open_image_address(struct wolfBoot_image *img, uint8_t *image)
881882
img->hdr_ok = 1;
882883
img->fw_base = img->hdr + IMAGE_HEADER_SIZE;
883884

885+
wolfBoot_printf("%s partition: %p (size %d, version 0x%x)\n",
886+
(img->part == PART_BOOT) ? "Boot" : "Update",
887+
img->hdr,
888+
(unsigned int)img->fw_size,
889+
wolfBoot_get_blob_version(image));
890+
884891
return 0;
885892
}
886893

@@ -962,11 +969,9 @@ int wolfBoot_open_image(struct wolfBoot_image *img, uint8_t part)
962969
#endif
963970
if (part == PART_BOOT) {
964971
img->hdr = (void*)WOLFBOOT_PARTITION_BOOT_ADDRESS;
965-
wolfBoot_printf("Boot partition: %p\n", img->hdr);
966972
}
967973
else if (part == PART_UPDATE) {
968974
img->hdr = (void*)WOLFBOOT_PARTITION_UPDATE_ADDRESS;
969-
wolfBoot_printf("Update partition: %p\n", img->hdr);
970975
}
971976
else {
972977
return -1;

src/libwolfboot.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,8 @@ uint32_t wolfBoot_get_blob_version(uint8_t *blob)
10181018
uint32_t *volatile version_field = NULL;
10191019
uint32_t *magic = NULL;
10201020
uint8_t *img_bin = blob;
1021+
if (blob == NULL)
1022+
return 0;
10211023
#if defined(EXT_ENCRYPTED) && defined(MMU)
10221024
if (!encrypt_initialized)
10231025
if (crypto_init() < 0)
@@ -1870,15 +1872,16 @@ int wolfBoot_ram_decrypt(uint8_t *src, uint8_t *dst)
18701872
uint32_t dst_offset = 0, iv_counter = 0;
18711873
uint32_t magic, len;
18721874

1873-
18741875
if (!encrypt_initialized) {
18751876
if (crypto_init() < 0) {
1877+
wolfBoot_printf("Error initializing crypto!\n");
18761878
return -1;
18771879
}
18781880
}
18791881

18801882
/* Attempt to decrypt firmware header */
18811883
if (decrypt_header(src) != 0) {
1884+
wolfBoot_printf("Error decrypting header at %p!\n", src);
18821885
return -1;
18831886
}
18841887
len = *((uint32_t*)(dec_hdr + sizeof(uint32_t)));

src/update_flash.c

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ static int RAMFUNCTION wolfBoot_copy_sector(struct wolfBoot_image *src,
138138
if (src == dst)
139139
return 0;
140140

141+
wolfBoot_printf("Copy sector %d (part %d->%d)\n",
142+
sector, src->part, dst->part);
143+
141144
if (src->part == PART_SWAP)
142145
src_sector_offset = 0;
143146
if (dst->part == PART_SWAP)
@@ -219,11 +222,9 @@ static int wolfBoot_swap_and_final_erase(int resume)
219222
WOLFBOOT_SECTOR_SIZE;
220223
uint32_t tmpBuffer[TRAILER_OFFSET_WORDS + 1];
221224

222-
/* open boot */
225+
/* open partitions (ignore failure) */
223226
wolfBoot_open_image(boot, PART_BOOT);
224-
/* open update */
225227
wolfBoot_open_image(update, PART_UPDATE);
226-
/* open swap */
227228
wolfBoot_open_image(swap, PART_SWAP);
228229
wolfBoot_get_partition_state(PART_UPDATE, &st);
229230

@@ -499,6 +500,9 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
499500
uint32_t cur_v;
500501
uint32_t up_v;
501502
#endif
503+
uint32_t cur_ver, upd_ver;
504+
505+
wolfBoot_printf("Staring Update (fallback allowed %d)\n", fallback_allowed);
502506

503507

504508
/* No Safety check on open: we might be in the middle of a broken update */
@@ -508,9 +512,10 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
508512

509513
/* get total size */
510514
total_size = wolfBoot_get_total_size(&boot, &update);
511-
512-
if (total_size <= IMAGE_HEADER_SIZE)
515+
if (total_size <= IMAGE_HEADER_SIZE) {
516+
wolfBoot_printf("Image total size %u too large!\n", total_size);
513517
return -1;
518+
}
514519
/* In case this is a new update, do the required
515520
* checks on the firmware update
516521
* before starting the swap
@@ -522,27 +527,39 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
522527
/* Check the first sector to detect interrupted update */
523528
if (flag == SECT_FLAG_NEW) {
524529
if (((update_type & 0x000F) != HDR_IMG_TYPE_APP) ||
525-
((update_type & 0xFF00) != HDR_IMG_TYPE_AUTH))
530+
((update_type & 0xFF00) != HDR_IMG_TYPE_AUTH)) {
531+
wolfBoot_printf("Invalid update type %d\n", update_type);
526532
return -1;
527-
if (update.fw_size > MAX_UPDATE_SIZE - 1)
533+
}
534+
if (update.fw_size > MAX_UPDATE_SIZE - 1) {
535+
wolfBoot_printf("Invalid update size %u\n", update.fw_size);
528536
return -1;
537+
}
529538
if (!update.hdr_ok || (wolfBoot_verify_integrity(&update) < 0)
530539
|| (wolfBoot_verify_authenticity(&update) < 0)) {
540+
wolfBoot_printf("Update integrity/verification failed!\n");
531541
return -1;
532542
}
533543
PART_SANITY_CHECK(&update);
544+
545+
cur_ver = wolfBoot_current_firmware_version();
546+
upd_ver = wolfBoot_update_firmware_version();
547+
548+
wolfBoot_printf("Versions: Current 0x%x, Update 0x%x\n",
549+
cur_ver, upd_ver);
550+
534551
#ifndef ALLOW_DOWNGRADE
535552
if ( ((fallback_allowed==1) &&
536553
(~(uint32_t)fallback_allowed == 0xFFFFFFFE)) ||
537-
(wolfBoot_current_firmware_version() <
538-
wolfBoot_update_firmware_version()) ) {
554+
(cur_ver < upd_ver) ) {
539555
VERIFY_VERSION_ALLOWED(fallback_allowed);
540-
} else
556+
} else {
557+
wolfBoot_printf("Update version not allowed\n");
541558
return -1;
559+
}
542560
#endif
543561
}
544562

545-
546563
#ifdef DELTA_UPDATES
547564
if ((update_type & 0x00F0) == HDR_IMG_TYPE_DIFF) {
548565
cur_v = wolfBoot_current_firmware_version();
@@ -576,12 +593,14 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
576593
}
577594
#endif
578595

596+
#ifndef DISABLE_BACKUP
597+
/* Interruptible swap */
598+
579599
hal_flash_unlock();
580-
#ifdef EXT_FLASH
600+
#ifdef EXT_FLASH
581601
ext_flash_unlock();
582-
#endif
602+
#endif
583603

584-
#ifndef DISABLE_BACKUP
585604
/* Interruptible swap
586605
* The status is saved in the sector flags of the update partition.
587606
* If something goes wrong, the operation will be resumed upon reboot.
@@ -620,6 +639,7 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
620639
break;
621640
}
622641
sector++;
642+
623643
/* headers that can be in different positions depending on when the
624644
* power fails are now in a known state, re-read and swap fw_size
625645
* because the locations are correct but the metadata is now swapped
@@ -640,9 +660,9 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
640660
/* erase to the last sector, writeonce has 2 sectors */
641661
while((sector * sector_size) < WOLFBOOT_PARTITION_SIZE -
642662
sector_size
643-
#ifdef NVM_FLASH_WRITEONCE
663+
#ifdef NVM_FLASH_WRITEONCE
644664
* 2
645-
#endif
665+
#endif
646666
) {
647667
wb_flash_erase(&boot, sector * sector_size, sector_size);
648668
wb_flash_erase(&update, sector * sector_size, sector_size);
@@ -652,37 +672,45 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
652672
* wolfBoot_start*/
653673
wolfBoot_swap_and_final_erase(0);
654674
/* encryption key was not erased, will be erased by success */
655-
#ifdef EXT_FLASH
675+
#ifdef EXT_FLASH
656676
ext_flash_lock();
657-
#endif
677+
#endif
658678
hal_flash_lock();
679+
659680
#else /* DISABLE_BACKUP */
660-
#ifdef EXT_ENCRYPTED
681+
/* Direct Swap without power fail saftey */
682+
683+
hal_flash_unlock();
684+
#ifdef EXT_FLASH
685+
ext_flash_unlock();
686+
#endif
687+
688+
#ifdef EXT_ENCRYPTED
661689
wolfBoot_get_encrypt_key(key, nonce);
662-
#endif
690+
#endif
663691

664-
/* Directly copy the content of the UPDATE partition into the BOOT partition.
665-
*/
692+
/* Directly copy the content of the UPDATE partition into the BOOT
693+
* partition. */
666694
while ((sector * sector_size) < total_size) {
667695
wolfBoot_copy_sector(&update, &boot, sector);
668696
sector++;
669697
}
670-
while((sector * sector_size) < WOLFBOOT_PARTITION_SIZE) {
698+
while ((sector * sector_size) < WOLFBOOT_PARTITION_SIZE) {
671699
wb_flash_erase(&boot, sector * sector_size, sector_size);
672700
sector++;
673701
}
674702
st = IMG_STATE_SUCCESS;
675703
wolfBoot_set_partition_state(PART_BOOT, st);
676-
#ifdef EXT_FLASH
704+
705+
#ifdef EXT_FLASH
677706
ext_flash_lock();
678-
#endif
707+
#endif
679708
hal_flash_lock();
680709

681-
/* Save the encryption key after swapping */
682-
#ifdef EXT_ENCRYPTED
710+
/* Save the encryption key after swapping */
711+
#ifdef EXT_ENCRYPTED
683712
wolfBoot_set_encrypt_key(key, nonce);
684-
#endif
685-
713+
#endif
686714
#endif /* DISABLE_BACKUP */
687715
return 0;
688716
}
@@ -840,7 +868,12 @@ void RAMFUNCTION wolfBoot_start(void)
840868
wolfBoot_update(0);
841869
}
842870
}
843-
if ((wolfBoot_open_image(&boot, PART_BOOT) < 0)
871+
872+
bootRet = wolfBoot_open_image(&boot, PART_BOOT);
873+
wolfBoot_printf("Booting version: 0x%x\n",
874+
wolfBoot_get_blob_version(boot.hdr));
875+
876+
if (bootRet < 0
844877
|| (wolfBoot_verify_integrity(&boot) < 0)
845878
|| (wolfBoot_verify_authenticity(&boot) < 0)
846879
) {

0 commit comments

Comments
 (0)