@@ -228,6 +228,7 @@ static int RAMFUNCTION nvm_select_fresh_sector(int part)
228228 uint8_t * addrErase = 0 ;
229229 uint32_t word_0 ;
230230 uint32_t word_1 ;
231+ uint32_t * ptr_word_0 , * ptr_word_1 ;
231232
232233#if defined(EXT_FLASH ) && !defined(FLAGS_HOME )
233234 if ((part == PART_UPDATE ) && FLAGS_UPDATE_EXT ()) {
@@ -254,8 +255,28 @@ static int RAMFUNCTION nvm_select_fresh_sector(int part)
254255 }
255256
256257 /* check magic in case the sector is corrupt */
257- word_0 = * ((uint32_t * )((uintptr_t )base - sizeof (uint32_t )));
258- word_1 = * ((uint32_t * )((uintptr_t )base - (WOLFBOOT_SECTOR_SIZE + sizeof (uint32_t ))));
258+ ptr_word_0 = (uint32_t * )((uintptr_t )base - sizeof (uint32_t ));
259+ ptr_word_1 = (uint32_t * )((uintptr_t )base - (WOLFBOOT_SECTOR_SIZE + sizeof (uint32_t )));
260+ #ifdef NO_DIRECT_READ_OF_ERASED_SECTOR
261+ if (hal_flash_is_erased_at ((uintptr_t )ptr_word_0 ))
262+ {
263+ word_0 = FLASH_WORD_ERASED ;
264+ }
265+ else
266+ #endif
267+ {
268+ word_0 = * ptr_word_0 ;
269+ }
270+ #ifdef NO_DIRECT_READ_OF_ERASED_SECTOR
271+ if (hal_flash_is_erased_at ((uintptr_t )ptr_word_1 ))
272+ {
273+ word_1 = FLASH_WORD_ERASED ;
274+ }
275+ else
276+ #endif
277+ {
278+ word_1 = * ptr_word_1 ;
279+ }
259280
260281 if (word_0 == WOLFBOOT_MAGIC_TRAIL && word_1 != WOLFBOOT_MAGIC_TRAIL ) {
261282 sel = 0 ;
@@ -301,8 +322,15 @@ static int RAMFUNCTION nvm_select_fresh_sector(int part)
301322finish :
302323 /* Erase the non-selected partition, requires unlocked flash */
303324 addrErase -= WOLFBOOT_SECTOR_SIZE * (!sel );
304- if (* ((uint32_t * )(addrErase + WOLFBOOT_SECTOR_SIZE - sizeof (uint32_t )))
305- != FLASH_WORD_ERASED ) {
325+ if (
326+ #ifdef NO_DIRECT_READ_OF_ERASED_SECTOR
327+ ! hal_flash_is_erased_at ((uintptr_t )addrErase )
328+ #else
329+ * ((uint32_t * )(addrErase + WOLFBOOT_SECTOR_SIZE - sizeof (uint32_t )))
330+ != FLASH_WORD_ERASED
331+ #endif
332+ )
333+ {
306334 hal_flash_erase ((uintptr_t )addrErase , WOLFBOOT_SECTOR_SIZE );
307335 }
308336 return sel ;
@@ -626,11 +654,20 @@ int RAMFUNCTION wolfBoot_set_partition_state(uint8_t part, uint8_t newst)
626654 if (part == PART_NONE )
627655 return -1 ;
628656 magic = get_partition_magic (part );
629- if (* magic != WOLFBOOT_MAGIC_TRAIL )
657+ if (
658+ #ifdef NO_DIRECT_READ_OF_ERASED_SECTOR
659+ hal_flash_is_erased_at ((uintptr_t )magic ) ||
660+ #endif
661+ * magic != WOLFBOOT_MAGIC_TRAIL
662+ )
663+ {
630664 set_partition_magic (part );
665+ }
631666 state = get_partition_state (part );
632667 if (* state != newst )
668+ {
633669 set_partition_state (part , newst );
670+ }
634671 return 0 ;
635672}
636673
@@ -652,8 +689,15 @@ int RAMFUNCTION wolfBoot_set_update_sector_flag(uint16_t sector,
652689 uint8_t pos = sector >> 1 ;
653690
654691 magic = get_partition_magic (PART_UPDATE );
655- if (* magic != wolfboot_magic_trail )
692+ if (
693+ #ifdef NO_DIRECT_READ_OF_ERASED_SECTOR
694+ hal_flash_is_erased_at ((uintptr_t )magic ) ||
695+ #endif
696+ * magic != wolfboot_magic_trail
697+ )
698+ {
656699 set_partition_magic (PART_UPDATE );
700+ }
657701
658702 flags = get_update_sector_flags (pos );
659703 if (sector == (pos << 1 ))
@@ -681,8 +725,15 @@ int RAMFUNCTION wolfBoot_get_partition_state(uint8_t part, uint8_t *st)
681725 if (part == PART_NONE )
682726 return -1 ;
683727 magic = get_partition_magic (part );
684- if (* magic != WOLFBOOT_MAGIC_TRAIL )
728+ if (
729+ #ifdef NO_DIRECT_READ_OF_ERASED_SECTOR
730+ hal_flash_is_erased_at ((uintptr_t )magic ) ||
731+ #endif
732+ * magic != WOLFBOOT_MAGIC_TRAIL
733+ )
734+ {
685735 return -1 ;
736+ }
686737 state = get_partition_state (part );
687738 * st = * state ;
688739 return 0 ;
@@ -706,8 +757,15 @@ int wolfBoot_get_update_sector_flag(uint16_t sector, uint8_t *flag)
706757 uint8_t * flags ;
707758 uint8_t pos = sector >> 1 ;
708759 magic = get_partition_magic (PART_UPDATE );
709- if (* magic != WOLFBOOT_MAGIC_TRAIL )
760+ if (
761+ #ifdef NO_DIRECT_READ_OF_ERASED_SECTOR
762+ hal_flash_is_erased_at ((uintptr_t )magic ) ||
763+ #endif
764+ * magic != WOLFBOOT_MAGIC_TRAIL
765+ )
766+ {
710767 return -1 ;
768+ }
711769 flags = get_update_sector_flags (pos );
712770 if (sector == (pos << 1 ))
713771 * flag = * flags & 0x0F ;
@@ -1009,8 +1067,15 @@ int wolfBoot_get_delta_info(uint8_t part, int inverse, uint32_t **img_offset,
10091067
10101068 /* Don't check image against NULL to allow using address 0x00000000 */
10111069 magic = (uint32_t * )image ;
1012- if (* magic != WOLFBOOT_MAGIC )
1070+ if (
1071+ #ifdef NO_DIRECT_READ_OF_ERASED_SECTOR
1072+ hal_flash_is_erased_at ((uintptr_t )magic ) ||
1073+ #endif
1074+ * magic != WOLFBOOT_MAGIC
1075+ )
1076+ {
10131077 return -1 ;
1078+ }
10141079 if (inverse ) {
10151080 if (wolfBoot_find_header ((uint8_t * )(image + IMAGE_HEADER_OFFSET ),
10161081 HDR_IMG_DELTA_INVERSE , (uint8_t * * )img_offset )
@@ -1083,8 +1148,15 @@ uint32_t wolfBoot_get_blob_version(uint8_t *blob)
10831148 img_bin = dec_hdr ;
10841149#endif
10851150 magic = (uint32_t * )img_bin ;
1086- if (* magic != WOLFBOOT_MAGIC )
1151+ if (
1152+ #ifdef NO_DIRECT_READ_OF_ERASED_SECTOR
1153+ hal_flash_is_erased_at ((uintptr_t )magic ) ||
1154+ #endif
1155+ * magic != WOLFBOOT_MAGIC
1156+ )
1157+ {
10871158 return 0 ;
1159+ }
10881160 if (wolfBoot_find_header (img_bin + IMAGE_HEADER_OFFSET , HDR_VERSION ,
10891161 (void * )& version_field ) == 0 )
10901162 return 0 ;
@@ -1116,8 +1188,15 @@ uint16_t wolfBoot_get_blob_type(uint8_t *blob)
11161188 img_bin = dec_hdr ;
11171189#endif
11181190 magic = (uint32_t * )img_bin ;
1119- if (* magic != WOLFBOOT_MAGIC )
1191+ if (
1192+ #ifdef NO_DIRECT_READ_OF_ERASED_SECTOR
1193+ hal_flash_is_erased_at ((uintptr_t )magic ) ||
1194+ #endif
1195+ * magic != WOLFBOOT_MAGIC
1196+ )
1197+ {
11201198 return 0 ;
1199+ }
11211200 if (wolfBoot_find_header (img_bin + IMAGE_HEADER_OFFSET , HDR_IMG_TYPE ,
11221201 (void * )& type_field ) == 0 )
11231202 return 0 ;
@@ -1153,8 +1232,15 @@ uint32_t wolfBoot_get_blob_diffbase_version(uint8_t *blob)
11531232 img_bin = dec_hdr ;
11541233#endif
11551234 magic = (uint32_t * )img_bin ;
1156- if (* magic != WOLFBOOT_MAGIC )
1235+ if (
1236+ #ifdef NO_DIRECT_READ_OF_ERASED_SECTOR
1237+ hal_flash_is_erased_at ((uintptr_t )magic ) ||
1238+ #endif
1239+ * magic != WOLFBOOT_MAGIC
1240+ )
1241+ {
11571242 return 0 ;
1243+ }
11581244 if (wolfBoot_find_header (img_bin + IMAGE_HEADER_OFFSET , HDR_IMG_DELTA_BASE ,
11591245 (void * )& delta_base ) == 0 )
11601246 return 0 ;
0 commit comments