diff options
-rw-r--r-- | flash.c | 18 | ||||
-rw-r--r-- | globals.h | 2 | ||||
-rw-r--r-- | instr-daemon.c | 6 |
3 files changed, 24 insertions, 2 deletions
@@ -272,7 +272,11 @@ int readUserBlock(FlashStruct *mem) // hopefully we can use the backup.. if (persistence_unfreeze(BACKUPFILE, mem, sizeof(*mem), 0)) { // if the backup was good overwrite the main file - persistence_copyfile(BACKUPFILE, MAINFILE); + if (!globals.Flags.shutdown_started) { + globals.Flags.flash_write_in_progress = TRUE; + persistence_copyfile(BACKUPFILE, MAINFILE); + globals.Flags.flash_write_in_progress = FALSE; + } return sizeof(*mem); } // deadend :( @@ -294,7 +298,16 @@ void writeUserBlock(FlashStruct *mem, int addr, int numbytes) if (!globals.Flags.flash_writes_suspended) { // backup the main copy of the file - if (persistence_copyfile(MAINFILE, BACKUPFILE)) { + if (globals.Flags.shutdown_started) return; + + globals.Flags.flash_write_in_progress = TRUE; + bool backup_ok = persistence_copyfile(MAINFILE, BACKUPFILE); + globals.Flags.flash_write_in_progress = FALSE; + + if (backup_ok) { + if (globals.Flags.shutdown_started) return; + globals.Flags.flash_write_in_progress = TRUE; + if (!persistence_freeze(MAINFILE, mem, addr, numbytes, sizeof(*mem), 0)) { if (errno != PERSIST_ERR_COULDNTWRITE) { printf("Error while trying to write, %d. **Write did not happen!!!**\n", errno); @@ -302,6 +315,7 @@ void writeUserBlock(FlashStruct *mem, int addr, int numbytes) printf("Error while writing data to disk. **File is potentially corrupt!**\n"); } } + globals.Flags.flash_write_in_progress = FALSE; } else { printf("Could not backup current file. **Write did not happen!!!**\n"); } @@ -771,6 +771,8 @@ typedef struct { int do_check_settings; int flash_writes_suspended; int force_output_fully_off; + int shutdown_started; + int flash_write_in_progress; } FlagStruct; diff --git a/instr-daemon.c b/instr-daemon.c index f2b9ca2..021eb62 100644 --- a/instr-daemon.c +++ b/instr-daemon.c @@ -317,6 +317,12 @@ static gboolean periodic_poll (void) { if (bus_getpin (POWER_FAIL)) { + globals.Flags.shutdown_started = TRUE; + + while (globals.Flags.flash_write_in_progress) { + g_usleep(1000); + } + LCD_clear(); LCD_write(0,0,"Power failed. Shutdown."); |