From c5c83de9b1b4f46a828c87c8f313735070956564 Mon Sep 17 00:00:00 2001 From: Nuck-TH Date: Thu, 17 Aug 2017 22:20:35 +0700 Subject: [PATCH] Added more safety checks Implemented more safety/sanity checks: *Check for NO$GBA footer when size is normal(i.e. footer replaces end of image, not appended to it). Yes, such image works in NO$GBA! *Check if battery level is below 50%(2 bars), allow to proceed if charger connected. Changed order of messages while writing nand to properly ask to not disconnect charger if it is connected. In same place altered block counter to actually state that it is operation progress. --- arm9/source/main.cpp | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/arm9/source/main.cpp b/arm9/source/main.cpp index d572ddf..e8a8145 100644 --- a/arm9/source/main.cpp +++ b/arm9/source/main.cpp @@ -247,6 +247,7 @@ void restoreNAND() { } //Sanity checks + // Size check fseek(f, 0, SEEK_END); size_t dump_size = ftell(f); @@ -255,7 +256,20 @@ void restoreNAND() { fclose(f); return; } + // NO$GBA footer check in image of normal size + // yes, that works in emulator + fseek(f, -64, SEEK_CUR); + char ngsign[] = "DSi eMMC CID/CPU\0"; + char fsign[17]; + fsign[16] = '\0'; + fread(fsign, 1, 16, f); + if ( !strcmp(ngsign, fsign) ) { + iprintf("%s has NO$GBA footer\nthat replaces end of image!\nOperation aborted.", nand_type); + fclose(f); + return; + } rewind(f); + // MBR(decrypted image) check // Taken from TWLtool (https://github.com/WinterMute/twltool) struct { @@ -277,6 +291,17 @@ void restoreNAND() { return; } rewind(f); + + // Battery level/charger check + u32 pwrReg = getBatteryLevel(); + bool isCharging = (pwrReg >> 7) & 1; + int batLevel = pwrReg & 0b1111; + + if ( !isCharging && (batLevel < 7) ) { + iprintf("Battery level below 50%%(2 bars)\nPlease connect charger.\nOperation aborted."); + fclose(f); + return; + } //Sanity checks end iprintf("Sure? NAND restore is DANGEROUS!"); @@ -297,7 +322,13 @@ void restoreNAND() { clearStatus(); - iprintf("Reading %s/%s\n\n", dirname, nand_type); + if (isCharging) { + iprintf("DON'T poweroff console\nor disconnect charger!\n"); + } else { + iprintf("DON'T poweroff console!\n"); + } + + iprintf("Reading:\n%s/%s\n", dirname, nand_type); size_t i; size_t sectors = 128; size_t blocks = (sizMB * 1024 * 1024) / (sectors * 512); @@ -315,7 +346,7 @@ void restoreNAND() { break; } - iprintf("%d/%d DON'T poweroff!\r", i+1, blocks); + iprintf("Progress: %d/%d blocks\r", i+1, blocks); } fclose(f);