Skip to content

Commit

Permalink
Added more safety checks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Nuck-TH committed Aug 17, 2017
1 parent 1440e2c commit c5c83de
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions arm9/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ void restoreNAND() {
}

//Sanity checks

// Size check
fseek(f, 0, SEEK_END);
size_t dump_size = ftell(f);
Expand All @@ -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 {
Expand All @@ -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!");
Expand All @@ -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);
Expand All @@ -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);

Expand Down

0 comments on commit c5c83de

Please sign in to comment.