Skip to content

Commit

Permalink
Implemented safety(sanity) checks in restoreNAND():
Browse files Browse the repository at this point in the history
    *Check for match of file and NAND sizes
    *Check for precence of MBR in file(this means that file is not encrypted)
Added user-friendly message for successful finish of restoration.
Edited messages a bit in restore function.
Fixed building on last devkitARM(nand_WriteSectors const buffer in declaration).
Changed version to 1.6.1 to differ from base.
  • Loading branch information
Nuck-TH committed Aug 9, 2017
1 parent 699ddb7 commit 83c392a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ include $(DEVKITARM)/ds_rules
export TARGET := fwTool
export TOPDIR := $(CURDIR)

export VERSION := 1.4.2
export VERSION := 1.6.1

.PHONY: arm7/$(TARGET).elf arm9/$(TARGET).elf

Expand Down
124 changes: 81 additions & 43 deletions arm9/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

extern "C" {
bool nand_ReadSectors(sec_t sector, sec_t numSectors,void* buffer);
bool nand_WriteSectors(sec_t sector, sec_t numSectors,void* buffer); //!!!
bool nand_WriteSectors(sec_t sector, sec_t numSectors,const void* buffer); //!!!
}

int menuTop = 5, statusTop = 18;
Expand Down Expand Up @@ -228,54 +228,92 @@ void restoreNAND() {

if (!isDSiMode()) {
iprintf("Not a DSi or 3ds!\n");
} else {
return;
}

FILE *f = fopen(nand_type, "rb");

if (f == NULL) {
iprintf("Failure opening %s\n", nand_type);
return;
}

//Sanity checks
// Size check
fseek(f, 0, SEEK_END);
size_t dump_size = ftell(f);
if ( dump_size != (sizMB * 1024 * 1024) ) {
iprintf("%s and NAND sizes\ndo not match.\nOperation aborted.", nand_type);
fclose(f);
return;
}
rewind(f);
// MBR(decrypted image) check
// Taken from TWLtool (https://github.com/WinterMute/twltool)
struct {
u8 code[446];
struct {
u8 status;
u8 start_chs[3];
u8 partition_type;
u8 end_chs[3];
u32 start_sector;
u32 num_sectors;
} __attribute__((__packed__)) partition[4];
u8 signature[2];
} mbr;
fread(&mbr, 1, 0x200, f);
if(mbr.signature[0] == 0x55 || mbr.signature[1] == 0xAA) {
iprintf("Found MBR in %s.\nImage is not encrypted.\nOperation aborted.", nand_type);
fclose(f);
return;
}
rewind(f);
//Sanity checks end

iprintf("Sure? NAND restore is DANGEROUS!");
iprintf("START + SELECT confirm\n");
iprintf("B to cancel\n");

while (1) {
scanKeys();
int keys = keysHeld();
if ((keys & KEY_START) && (keys & KEY_SELECT))break;
if (keys & KEY_B) {
clearStatus();
fclose(f);
return;
}
swiWaitForVBlank();
}

clearStatus();

iprintf("Reading %s/%s\n\n", dirname, nand_type);
size_t i;
size_t sectors = 128;
size_t blocks = (sizMB * 1024 * 1024) / (sectors * 512);
for (i=0; i < blocks; i++) {

iprintf("Sure? NAND restore is DANGEROUS!");
iprintf("START + SELECT confirm\n");
iprintf("B to exit\n");
size_t read = fread(firmware_buffer, 1, 512 * sectors, f);

while(1){
scanKeys();
int keys = keysHeld();
if((keys & KEY_START) && (keys & KEY_SELECT))break;
if(keys & KEY_B){
clearStatus();
return;
}
swiWaitForVBlank();
if(read != 512 * sectors) {
iprintf("\nError reading SD!\n");
break;
}

clearStatus();

FILE *f = fopen(nand_type, "rb");

if (NULL == f) {
iprintf("failure creating %s\n", nand_type);
} else {
iprintf("Reading %s/%s\n\n", dirname, nand_type);
size_t i;
size_t sectors = 128;
size_t blocks = (sizMB * 1024 * 1024) / (sectors * 512);
for (i=0; i < blocks; i++) {

size_t read = fread(firmware_buffer, 1, 512 * sectors, f);

if(read != 512 * sectors) {
iprintf("\nError reading SD!\n");
break;
}

if(!nand_WriteSectors(i * sectors,sectors,firmware_buffer)) {
iprintf("\nError writing NAND!\n");
break;
}

iprintf("%d/%d DON'T poweroff!\r", i+1, blocks);
}
fclose(f);
if(!nand_WriteSectors(i * sectors,sectors,firmware_buffer)) {
iprintf("\nError writing NAND!\n");
break;
}

iprintf("%d/%d DON'T poweroff!\r", i+1, blocks);
}

fclose(f);

clearStatus();
iprintf("Restore %s success.\nYou may now Exit and restart\nyour console.",nand_type);

}

void dumpCID(){
Expand Down

0 comments on commit 83c392a

Please sign in to comment.