From 0a8cc4f838691dd06b9296881da89891c6595eca Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Thu, 6 Feb 2020 18:10:24 +0200 Subject: [PATCH] Fix self-tests bug. * This is an edge case which happens if a file is written to the flash storage immediately after the first boot and before a reset causing the host to corrupt the selftests.py script. * The selftests.py is now executed before USB MSC is enabled to avoid corrupting the filesystem when selftests.py is removed. * If the selftests.py fails, the USB MSC is enabled to allow the host to read the error log. --- src/omv/main.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/omv/main.c b/src/omv/main.c index 237776ade..7e3731579 100644 --- a/src/omv/main.c +++ b/src/omv/main.c @@ -213,9 +213,12 @@ void NORETURN __fatal_error(const char *msg) { const char *hdr = "FATAL ERROR:\n"; f_write(&fp, hdr, strlen(hdr), &bytes); f_write(&fp, msg, strlen(msg), &bytes); + f_close(&fp); + storage_flush(); + // Initialize the USB device if it's not already initialize to allow + // the host to mount the filesystem and access the error log. + pyb_usb_dev_init(pyb_usb_dev_detect(), USBD_VID, USBD_PID_CDC_MSC, USBD_MODE_CDC_MSC, 0, NULL, NULL); } - f_close(&fp); - storage_flush(); for (uint i = 0;;) { led_toggle(((i++) & 3)); @@ -568,9 +571,12 @@ soft_reset: // Run boot script(s) if (first_soft_reset) { - // Execute the boot.py script before initializing the USB dev - // to override the USB mode if required, otherwise VCP+MSC is used. + // Execute the boot.py script before initializing the USB dev to + // override the USB mode if required, otherwise VCP+MSC is used. exec_boot_script("/boot.py", false, false); + // Execute the selftests.py script before the filesystem is mounted + // to avoid corrupting the filesystem when selftests.py is removed. + exec_boot_script("/selftest.py", true, false); } // Init USB device to default setting if it was not already configured @@ -610,9 +616,8 @@ soft_reset: timer_tim5_init(100); } - // Run boot script(s) + // Run main script if it exists. if (first_soft_reset) { - exec_boot_script("/selftest.py", true, false); exec_boot_script("/main.py", false, true); }