Skip to content

Commit 3166b07

Browse files
authored
Merge pull request #409 from danielinux/no_backup_powerfail
DISABLE_BACKUP is now powerfail-safe.
2 parents 11017d8 + a9ba39b commit 3166b07

6 files changed

Lines changed: 184 additions & 11 deletions

File tree

.github/workflows/test-powerfail-simulator.yml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,85 @@ jobs:
416416
- name: Run update-revert test with power failures (AES128 DELTA)
417417
run: |
418418
tools/scripts/sim-update-powerfail-resume.sh
419+
420+
# TEST with backup disabled
421+
- name: make clean
422+
run: |
423+
make distclean
424+
- name: Select config with backup disabled
425+
run: |
426+
cp config/examples/sim-nobackup.config .config
427+
428+
- name: Build tools
429+
run: |
430+
make -C tools/keytools && make -C tools/bin-assemble
431+
432+
- name: Build wolfboot.elf
433+
run: |
434+
make clean && make test-sim-internal-flash-with-update
435+
436+
- name: Run sunny day update test
437+
run: |
438+
tools/scripts/sim-sunnyday-update.sh
439+
440+
- name: Rebuild wolfboot.elf
441+
run: |
442+
make clean && make test-sim-internal-flash-with-update
443+
444+
- name: Run update test with DISABLE_BACKUP and powefail
445+
run: |
446+
tools/scripts/sim-update-powerfail-resume-nobackup.sh
447+
# TEST with backup disabled + NVM_WRITEONCE
448+
- name: make clean
449+
run: |
450+
make distclean
451+
- name: Select config with backup disabled + NVM WRITEONCE
452+
run: |
453+
cp config/examples/sim-nobackup-nvm-writeonce.config .config
454+
455+
- name: Build tools
456+
run: |
457+
make -C tools/keytools && make -C tools/bin-assemble
458+
459+
- name: Build wolfboot.elf
460+
run: |
461+
make clean && make test-sim-internal-flash-with-update
462+
463+
- name: Run sunny day update test
464+
run: |
465+
tools/scripts/sim-sunnyday-update.sh
466+
467+
- name: Rebuild wolfboot.elf
468+
run: |
469+
make clean && make test-sim-internal-flash-with-update
470+
471+
- name: Run update test with DISABLE_BACKUP and powefail
472+
run: |
473+
tools/scripts/sim-update-powerfail-resume-nobackup.sh
474+
# TEST with backup disabled + FLAGS_HOME
475+
- name: make clean
476+
run: |
477+
make distclean
478+
- name: Select config with backup disabled + FLAGS_HOME
479+
run: |
480+
cp config/examples/sim-nobackup-flags-home.config .config
481+
482+
- name: Build tools
483+
run: |
484+
make -C tools/keytools && make -C tools/bin-assemble
485+
486+
- name: Build wolfboot.elf
487+
run: |
488+
make clean && make test-sim-internal-flash-with-update
489+
490+
- name: Run sunny day update test
491+
run: |
492+
tools/scripts/sim-sunnyday-update.sh
493+
494+
- name: Rebuild wolfboot.elf
495+
run: |
496+
make clean && make test-sim-internal-flash-with-update
497+
498+
- name: Run update test with DISABLE_BACKUP and powefail
499+
run: |
500+
tools/scripts/sim-update-powerfail-resume-nobackup.sh
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
ARCH=sim
2+
TARGET=sim
3+
SIGN?=ED25519
4+
HASH?=SHA256
5+
WOLFBOOT_SMALL_STACK?=1
6+
SPI_FLASH=0
7+
DEBUG=1
8+
SPMATH?=0
9+
DISABLE_BACKUP=1
10+
FLAGS_HOME=1
11+
12+
# sizes should be multiple of system page size
13+
WOLFBOOT_PARTITION_SIZE=0x40000
14+
WOLFBOOT_SECTOR_SIZE=0x1000
15+
WOLFBOOT_PARTITION_BOOT_ADDRESS=0x80000
16+
# if on external flash, it should be multiple of system page size
17+
WOLFBOOT_PARTITION_UPDATE_ADDRESS=0x100000
18+
WOLFBOOT_PARTITION_SWAP_ADDRESS=0x180000
19+
20+
# required for keytools
21+
WOLFBOOT_FIXED_PARTITIONS=1
22+
23+
# For debugging XMALLOC/XFREE
24+
#CFLAGS_EXTRA+=-DWOLFBOOT_DEBUG_MALLOC
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
ARCH=sim
2+
TARGET=sim
3+
SIGN?=ED25519
4+
HASH?=SHA256
5+
WOLFBOOT_SMALL_STACK?=1
6+
SPI_FLASH=0
7+
DEBUG=1
8+
SPMATH?=0
9+
DISABLE_BACKUP=1
10+
NVM_WRITEONCE=1
11+
12+
# sizes should be multiple of system page size
13+
WOLFBOOT_PARTITION_SIZE=0x40000
14+
WOLFBOOT_SECTOR_SIZE=0x1000
15+
WOLFBOOT_PARTITION_BOOT_ADDRESS=0x80000
16+
# if on external flash, it should be multiple of system page size
17+
WOLFBOOT_PARTITION_UPDATE_ADDRESS=0x100000
18+
WOLFBOOT_PARTITION_SWAP_ADDRESS=0x180000
19+
20+
# required for keytools
21+
WOLFBOOT_FIXED_PARTITIONS=1
22+
23+
# For debugging XMALLOC/XFREE
24+
#CFLAGS_EXTRA+=-DWOLFBOOT_DEBUG_MALLOC
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
ARCH=sim
2+
TARGET=sim
3+
SIGN?=ED25519
4+
HASH?=SHA256
5+
WOLFBOOT_SMALL_STACK?=1
6+
SPI_FLASH=0
7+
DEBUG=1
8+
SPMATH?=0
9+
DISABLE_BACKUP=1
10+
11+
# sizes should be multiple of system page size
12+
WOLFBOOT_PARTITION_SIZE=0x40000
13+
WOLFBOOT_SECTOR_SIZE=0x1000
14+
WOLFBOOT_PARTITION_BOOT_ADDRESS=0x80000
15+
# if on external flash, it should be multiple of system page size
16+
WOLFBOOT_PARTITION_UPDATE_ADDRESS=0x100000
17+
WOLFBOOT_PARTITION_SWAP_ADDRESS=0x180000
18+
19+
# required for keytools
20+
WOLFBOOT_FIXED_PARTITIONS=1
21+
22+
# For debugging XMALLOC/XFREE
23+
#CFLAGS_EXTRA+=-DWOLFBOOT_DEBUG_MALLOC

src/update_flash.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -553,23 +553,14 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
553553
#endif
554554
hal_flash_lock();
555555
#else /* DISABLE_BACKUP */
556-
#warning "Backup mechanism disabled! Update installation will not be interruptible"
557-
/* Read encryption key/IV before starting the update */
558556
#ifdef EXT_ENCRYPTED
559557
wolfBoot_get_encrypt_key(key, nonce);
560558
#endif
561559

562560
/* Directly copy the content of the UPDATE partition into the BOOT partition.
563-
* This mechanism is not fail-safe, and will brick your device if interrupted
564-
* before the copy is finished.
565561
*/
566562
while ((sector * sector_size) < total_size) {
567-
if ((wolfBoot_get_update_sector_flag(sector, &flag) != 0) || (flag == SECT_FLAG_NEW)) {
568-
flag = SECT_FLAG_SWAPPING;
569-
wolfBoot_copy_sector(&update, &boot, sector);
570-
if (((sector + 1) * sector_size) < WOLFBOOT_PARTITION_SIZE)
571-
wolfBoot_set_update_sector_flag(sector, flag);
572-
}
563+
wolfBoot_copy_sector(&update, &boot, sector);
573564
sector++;
574565
}
575566
while((sector * sector_size) < WOLFBOOT_PARTITION_SIZE) {
@@ -578,7 +569,6 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
578569
}
579570
st = IMG_STATE_SUCCESS;
580571
wolfBoot_set_partition_state(PART_BOOT, st);
581-
582572
#ifdef EXT_FLASH
583573
ext_flash_lock();
584574
#endif
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
V=`./wolfboot.elf update_trigger get_version 2>/dev/null`
3+
if [ "x$V" != "x1" ]; then
4+
echo "Failed first boot with update_trigger"
5+
exit 1
6+
fi
7+
8+
./wolfboot.elf powerfail 0 get_version 2>/dev/null
9+
./wolfboot.elf powerfail 15000 get_version 2>/dev/null
10+
./wolfboot.elf powerfail 18000 get_version 2>/dev/null
11+
./wolfboot.elf powerfail 1a000 get_version 2>/dev/null
12+
# fail on the last sector to stop the encrypt key save and state update
13+
./wolfboot.elf powerfail 3e000 get_version 2>/dev/null
14+
# may not trigger on non NVM_FLASH_WRITEONCE
15+
V=`./wolfboot.elf powerfail 3f000 get_version` 2>/dev/null
16+
if [ "x$V" != "x2" ]; then
17+
V=`./wolfboot.elf get_version 2>/dev/null`
18+
# if we failed on the final boot state write we need to double fallback
19+
if [ "x$V" == "x1" ]; then
20+
V=`./wolfboot.elf get_version 2>/dev/null`
21+
fi
22+
fi
23+
24+
if [ "x$V" != "x2" ]; then
25+
echo "Failed update (V: $V)"
26+
exit 1
27+
fi
28+
29+
echo Test successful.
30+
exit 0

0 commit comments

Comments
 (0)