Add randomized button sequence confirmation

This commit is contained in:
Pk11 2022-01-09 16:02:01 -06:00
parent e3f3358420
commit 9ae5370f11
8 changed files with 99 additions and 4 deletions

View File

@ -2,6 +2,7 @@
#include "menu.h"
#include "storage.h"
#include "message.h"
#include "nand/nandio.h"
#include <dirent.h>
#include <sys/stat.h>
@ -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();
}
}
}

View File

@ -3,6 +3,7 @@
#include "main.h"
#include "message.h"
#include "maketmd.h"
#include "nand/nandio.h"
#include "rom.h"
#include "storage.h"
#include <errno.h>
@ -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;
}

View File

@ -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:

View File

@ -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", "<A>", "<B>", "<X>", "<Y>"};
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<START> 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);

View File

@ -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);

View File

@ -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;
}

View File

@ -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

View File

@ -2,6 +2,7 @@
#include "rom.h"
#include "menu.h"
#include "message.h"
#include "nand/nandio.h"
#include "storage.h"
#include <dirent.h>
@ -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();
}
}
}