From 898a938045e65f72438413650dce0f4e52a645bc Mon Sep 17 00:00:00 2001 From: Florian Loitsch Date: Mon, 3 Jun 2024 13:12:52 +0200 Subject: [PATCH] Improve run-image's boot.sh. (#2378) --- system/extensions/host/run-image-boot-sh.toit | 18 ++------ tests/envelope/boot-crash-source.toit | 43 +++++++++++++++++++ tests/envelope/boot-crash-test.toit | 16 +++++++ tests/envelope/boot-deep-sleep-source.toit | 15 +++++++ tests/envelope/boot-deep-sleep-test.toit | 16 +++++++ tests/envelope/boot-flash-test.toit | 2 +- tests/envelope/boot-upgrade-test.toit | 1 - tests/envelope/boot-upgraded-success.toit | 3 +- 8 files changed, 97 insertions(+), 17 deletions(-) create mode 100644 tests/envelope/boot-crash-source.toit create mode 100644 tests/envelope/boot-crash-test.toit create mode 100644 tests/envelope/boot-deep-sleep-source.toit create mode 100644 tests/envelope/boot-deep-sleep-test.toit diff --git a/system/extensions/host/run-image-boot-sh.toit b/system/extensions/host/run-image-boot-sh.toit index b004114ae..1d2dec05b 100644 --- a/system/extensions/host/run-image-boot-sh.toit +++ b/system/extensions/host/run-image-boot-sh.toit @@ -85,12 +85,6 @@ for (( ; ; )); do export TOIT_CONFIG_DATA=\$(realpath \$PREFIX/\$current/config.ubjson) - if [ -e \$PREFIX/\$current/validated ]; then - already_validated=true - else - already_validated=false - fi - # Clear out the scratch directory. rm -rf \$PREFIX/scratch mkdir -p \$PREFIX/scratch @@ -129,18 +123,14 @@ for (( ; ; )); do echo "****************************************" break else - if [ ! \$exit_code -eq 0 -a ! \$exit_code -eq 18 ]; then + if [ ! \$exit_code -eq 0 -a ! \$exit_code -eq $EXIT-CODE-ROLLBACK-REQUESTED ]; then echo "***********************************************" echo "*** Firmware crashed with code=\$exit_code" echo "***********************************************" + # Clear the flash-registry in case it was corrupted. + rm -f \$PREFIX/flash-registry fi - if [ -e \$PREFIX/\$current/validated ]; then - if [ \$already_validated == false ]; then - echo "****************************************" - echo "*** Firmware successfully validated" - echo "****************************************" - fi - else + if [ ! -e \$PREFIX/\$current/validated ]; then if [ \$exit_code -eq $EXIT-CODE-ROLLBACK-REQUESTED ]; then echo "****************************************" echo "*** Rollback requested" diff --git a/tests/envelope/boot-crash-source.toit b/tests/envelope/boot-crash-source.toit new file mode 100644 index 000000000..2e115a7f9 --- /dev/null +++ b/tests/envelope/boot-crash-source.toit @@ -0,0 +1,43 @@ +// Copyright (C) 2024 Toitware ApS. +// Use of this source code is governed by a Zero-Clause BSD license that can +// be found in the tests/LICENSE file. + +import host.file +import host.os +import system.firmware +import system.storage +import .exit-codes + +main: + test-dir := os.env["BOOT_TEST_DIR"] + if file.is-file "$test-dir/mark": + second-call + else: + first-call test-dir + +with-region [block]: + region := storage.Region.open --flash "toitlang.org/envelope-test-region" --capacity=100 + block.call region + region.close + +HELLO ::= "hello world" + +first-call test-dir/string: + print "first call" + file.write-content --path="$test-dir/mark" "first" + + with-region: | region/storage.Region | + region.write --at=0 HELLO + + // Exit with a crash. + exit 1 + +second-call: + with-region: | region/storage.Region | + existing := region.read --from=0 --to=HELLO.size + if existing == HELLO: + print "Test failed" + else: + // The flash was correctly cleared after the crash. + print "Test succeeded" + exit EXIT-CODE-STOP diff --git a/tests/envelope/boot-crash-test.toit b/tests/envelope/boot-crash-test.toit new file mode 100644 index 000000000..36adbfb2f --- /dev/null +++ b/tests/envelope/boot-crash-test.toit @@ -0,0 +1,16 @@ +// Copyright (C) 2024 Toitware ApS. +// Use of this source code is governed by a Zero-Clause BSD license that can +// be found in the tests/LICENSE file. + +import expect show * +import .exit-codes +import .util show EnvelopeTest with-test + +main args: + with-test args: | test/EnvelopeTest | + test.install --name="crash" --source-path="./boot-crash-source.toit" + test.extract-to-dir --dir-path=test.tmp-dir + output := test.boot-backticks test.tmp-dir --env={ + "BOOT_TEST_DIR": test.tmp-dir, + } + expect (output.contains "Test succeeded") diff --git a/tests/envelope/boot-deep-sleep-source.toit b/tests/envelope/boot-deep-sleep-source.toit new file mode 100644 index 000000000..dfaff2cc4 --- /dev/null +++ b/tests/envelope/boot-deep-sleep-source.toit @@ -0,0 +1,15 @@ +// Copyright (C) 2024 Toitware ApS. +// Use of this source code is governed by a Zero-Clause BSD license that can +// be found in the tests/LICENSE file. + +import host.file +import host.os +import .exit-codes + +main: + test-dir := os.env["BOOT_TEST_DIR"] + if file.is-file "$test-dir/mark": + print "Test succeeded" + exit EXIT-CODE_STOP + file.write-content --path="$test-dir/mark" "first" + __deep_sleep__ 20 // Sleep 20 ms. diff --git a/tests/envelope/boot-deep-sleep-test.toit b/tests/envelope/boot-deep-sleep-test.toit new file mode 100644 index 000000000..b0b8d6a65 --- /dev/null +++ b/tests/envelope/boot-deep-sleep-test.toit @@ -0,0 +1,16 @@ +// Copyright (C) 2024 Toitware ApS. +// Use of this source code is governed by a Zero-Clause BSD license that can +// be found in the tests/LICENSE file. + +import expect show * +import .exit-codes +import .util show EnvelopeTest with-test + +main args: + with-test args: | test/EnvelopeTest | + test.install --name="deep-sleep" --source-path="./boot-deep-sleep-source.toit" + test.extract-to-dir --dir-path=test.tmp-dir + output := test.boot-backticks test.tmp-dir --env={ + "BOOT_TEST_DIR": test.tmp-dir, + } + expect (output.contains "Test succeeded") diff --git a/tests/envelope/boot-flash-test.toit b/tests/envelope/boot-flash-test.toit index 20740e861..8aa2651e0 100644 --- a/tests/envelope/boot-flash-test.toit +++ b/tests/envelope/boot-flash-test.toit @@ -8,7 +8,7 @@ import .util show EnvelopeTest with-test main args: with-test args: | test/EnvelopeTest | - test.install --name="hello" --source-path="./boot-flash-source.toit" + test.install --name="flash" --source-path="./boot-flash-source.toit" test.extract-to-dir --dir-path=test.tmp-dir output := test.boot-backticks test.tmp-dir expect (output.contains "Test succeeded") diff --git a/tests/envelope/boot-upgrade-test.toit b/tests/envelope/boot-upgrade-test.toit index 3a9a848b2..917b22285 100644 --- a/tests/envelope/boot-upgrade-test.toit +++ b/tests/envelope/boot-upgrade-test.toit @@ -31,7 +31,6 @@ test-success args/List: "validation is pending", "can rollback", "validating", - "*** Firmware successfully validated", // From the boot script. "hello from updated", "validation is not pending", "can not rollback", diff --git a/tests/envelope/boot-upgraded-success.toit b/tests/envelope/boot-upgraded-success.toit index 23066be2f..fbf1d419e 100644 --- a/tests/envelope/boot-upgraded-success.toit +++ b/tests/envelope/boot-upgraded-success.toit @@ -5,6 +5,7 @@ import system.firmware import host.file import host.os +import .exit-codes main: print "hello from updated firmware!" @@ -14,7 +15,7 @@ main: test-dir := os.env["BOOT_TEST_DIR"] if file.is-file "$test-dir/mark": print "exiting" - exit 19 + exit EXIT-CODE-STOP print "validating" firmware.validate