diff --git a/arm9/src/backupmenu.c b/arm9/src/backupmenu.c index 70da9f5..a960df2 100644 --- a/arm9/src/backupmenu.c +++ b/arm9/src/backupmenu.c @@ -2,6 +2,7 @@ #include "menu.h" #include "storage.h" #include "message.h" +#include "nand/nandio.h" #include #include @@ -190,6 +191,9 @@ static void restore(Menu* m) } else { + if(!nandio_unlock_writing()) + return; + clearScreen(&bottomScreen); if (!copyDir(fpath, "nand:/title")) @@ -200,6 +204,8 @@ static void restore(Menu* m) { messagePrint("\x1B[42m\nBackup restored.\n\x1B[47m"); } + + nandio_lock_writing(); } } } diff --git a/arm9/src/install.c b/arm9/src/install.c index a0d6169..0aa7136 100644 --- a/arm9/src/install.c +++ b/arm9/src/install.c @@ -3,6 +3,7 @@ #include "main.h" #include "message.h" #include "maketmd.h" +#include "nand/nandio.h" #include "rom.h" #include "storage.h" #include @@ -297,6 +298,9 @@ bool install(char* fpath, bool systemTitle) return false; } + if(!nandio_unlock_writing()) + return false; + //start installation clearScreen(&bottomScreen); iprintf("Installing %s\n\n", fpath); swiWaitForVBlank(); @@ -553,5 +557,7 @@ error: complete: free(h); + nandio_lock_writing(); + return result; } \ No newline at end of file diff --git a/arm9/src/main.c b/arm9/src/main.c index 7c10c7a..f30e394 100644 --- a/arm9/src/main.c +++ b/arm9/src/main.c @@ -149,8 +149,11 @@ int main(int argc, char **argv) break; case MAIN_MENU_FIX: - nandio_force_fat_fix(); - messageBox("Mismatch in FAT copies will be\nfixed on close.\n"); + if(nandio_unlock_writing()) { + nandio_force_fat_fix(); + nandio_lock_writing(); + messageBox("Mismatch in FAT copies will be\nfixed on close.\n"); + } break; case MAIN_MENU_EXIT: diff --git a/arm9/src/message.c b/arm9/src/message.c index 3613c10..78b962b 100644 --- a/arm9/src/message.c +++ b/arm9/src/message.c @@ -84,6 +84,55 @@ bool choicePrint(char* message) return choice; } +const static u16 keys[] = {KEY_UP, KEY_DOWN, KEY_RIGHT, KEY_LEFT, KEY_A, KEY_B, KEY_X, KEY_Y}; +const static char *keysLabels[] = {"\x18", "\x19", "\x1A", "\x1B", "", "", "", ""}; + +bool randomConfirmBox(char* message) +{ + const int choiceRow = 10; + int sequencePosition = 0; + + u8 sequence[8]; + for(int i = 0; i < sizeof(sequence); i++) { + sequence[i] = rand() % (sizeof(keys) / sizeof(keys[0])); + } + + clearScreen(&bottomScreen); + + iprintf("\x1B[43m"); //yellow + iprintf("%s\n", message); + iprintf("\x1B[47m"); //white + iprintf("\n cancel\n"); + + while (!programEnd && sequencePosition < sizeof(sequence)) + { + swiWaitForVBlank(); + scanKeys(); + + //Print sequence + iprintf("\x1b[%d;0H", choiceRow); + for(int i = 0; i < sizeof(sequence); i++) { + iprintf("\x1B[%0om", i < sequencePosition ? 032 : 047); + iprintf("%s ", keysLabels[sequence[i]]); + } + + if (keysDown() & (KEY_UP | KEY_DOWN | KEY_RIGHT | KEY_LEFT | KEY_A | KEY_B | KEY_X | KEY_Y)) { + if(keysDown() & keys[sequence[sequencePosition]]) + sequencePosition++; + else + sequencePosition = 0; + } + + if (keysDown() & KEY_START) { + sequencePosition = 0; + break; + } + } + + scanKeys(); + return sequencePosition == sizeof(sequence); +} + void messageBox(char* message) { clearScreen(&bottomScreen); diff --git a/arm9/src/message.h b/arm9/src/message.h index 38c6acd..56d47af 100644 --- a/arm9/src/message.h +++ b/arm9/src/message.h @@ -11,6 +11,7 @@ enum { void keyWait(u32 key); bool choiceBox(char* message); bool choicePrint(char* message); +bool randomConfirmBox(char* message); void messageBox(char* message); void messagePrint(char* message); diff --git a/arm9/src/nand/nandio.c b/arm9/src/nand/nandio.c index 0f0a2ea..76eb6df 100644 --- a/arm9/src/nand/nandio.c +++ b/arm9/src/nand/nandio.c @@ -6,6 +6,7 @@ #include "crypto.h" #include "sector0.h" #include "f_xy.h" +#include "../message.h" #include "nandio.h" #include "u128_math.h" @@ -33,6 +34,7 @@ const DISC_INTERFACE io_dsi_nand = { bool is3DS; +static bool writingLocked = true; static bool nandWritten = false; extern bool nand_Startup(); @@ -169,6 +171,9 @@ bool nandio_read_sectors(sec_t offset, sec_t len, void *buffer) bool nandio_write_sectors(sec_t offset, sec_t len, const void *buffer) { + if(writingLocked) + return false; + nandWritten = true; while (len >= CRYPT_BUF_LEN) @@ -198,7 +203,6 @@ bool nandio_clear_status() bool nandio_shutdown() { if(nandWritten) { - // at cleanup we synchronize the FAT statgings // A FatFS might have multiple copies of the FAT. // we will get them back synchonized as we just worked on the first copy @@ -221,10 +225,12 @@ bool nandio_shutdown() // read fat sector nandio_read_sectors(fat_sig_fix_offset + reservedSectors + sector, 1, sector_buf) ; // write to each copy, except the source copy + writingLocked = false; for (int stage = 1;stage < stagingLevels;stage++) { nandio_write_sectors(fat_sig_fix_offset + reservedSectors + sector + (stage *sectorsPerFatCopy), 1, sector_buf) ; } + writingLocked = true; } } nandWritten = false; @@ -234,9 +240,25 @@ bool nandio_shutdown() return true; } +bool nandio_lock_writing() +{ + writingLocked = true; + + return writingLocked; +} + +bool nandio_unlock_writing() +{ + if(writingLocked && randomConfirmBox("Writing to NAND is locked!\nIf you're sure you understand\nthe risk, input the sequence\nbelow.")) + writingLocked = false; + + return !writingLocked; +} + bool nandio_force_fat_fix() { - nandWritten = true; + if(!writingLocked) + nandWritten = true; return true; } diff --git a/arm9/src/nand/nandio.h b/arm9/src/nand/nandio.h index 62c2f90..bc11f0c 100644 --- a/arm9/src/nand/nandio.h +++ b/arm9/src/nand/nandio.h @@ -22,6 +22,8 @@ void getConsoleID(uint8_t *consoleID) ; extern bool nandio_shutdown() ; +extern bool nandio_lock_writing() ; +extern bool nandio_unlock_writing() ; extern bool nandio_force_fat_fix() ; #ifdef __cplusplus diff --git a/arm9/src/titlemenu.c b/arm9/src/titlemenu.c index 4914a42..ad4437f 100644 --- a/arm9/src/titlemenu.c +++ b/arm9/src/titlemenu.c @@ -2,6 +2,7 @@ #include "rom.h" #include "menu.h" #include "message.h" +#include "nand/nandio.h" #include "storage.h" #include @@ -355,6 +356,9 @@ static bool delete(Menu* m) } else { + if(!nandio_unlock_writing()) + return false; + clearScreen(&bottomScreen); if (deleteDir(dirPath)) @@ -366,6 +370,8 @@ static bool delete(Menu* m) { messagePrint("\nTitle could not be deleted.\n"); } + + nandio_lock_writing(); } } }