Skip to content

Commit 83c392a

Browse files
committed
Implemented safety(sanity) checks in restoreNAND():
*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.
1 parent 699ddb7 commit 83c392a

2 files changed

Lines changed: 82 additions & 44 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ include $(DEVKITARM)/ds_rules
1010
export TARGET := fwTool
1111
export TOPDIR := $(CURDIR)
1212

13-
export VERSION := 1.4.2
13+
export VERSION := 1.6.1
1414

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

arm9/source/main.cpp

Lines changed: 81 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
extern "C" {
1616
bool nand_ReadSectors(sec_t sector, sec_t numSectors,void* buffer);
17-
bool nand_WriteSectors(sec_t sector, sec_t numSectors,void* buffer); //!!!
17+
bool nand_WriteSectors(sec_t sector, sec_t numSectors,const void* buffer); //!!!
1818
}
1919

2020
int menuTop = 5, statusTop = 18;
@@ -228,54 +228,92 @@ void restoreNAND() {
228228

229229
if (!isDSiMode()) {
230230
iprintf("Not a DSi or 3ds!\n");
231-
} else {
231+
return;
232+
}
233+
234+
FILE *f = fopen(nand_type, "rb");
235+
236+
if (f == NULL) {
237+
iprintf("Failure opening %s\n", nand_type);
238+
return;
239+
}
240+
241+
//Sanity checks
242+
// Size check
243+
fseek(f, 0, SEEK_END);
244+
size_t dump_size = ftell(f);
245+
if ( dump_size != (sizMB * 1024 * 1024) ) {
246+
iprintf("%s and NAND sizes\ndo not match.\nOperation aborted.", nand_type);
247+
fclose(f);
248+
return;
249+
}
250+
rewind(f);
251+
// MBR(decrypted image) check
252+
// Taken from TWLtool (https://github.com/WinterMute/twltool)
253+
struct {
254+
u8 code[446];
255+
struct {
256+
u8 status;
257+
u8 start_chs[3];
258+
u8 partition_type;
259+
u8 end_chs[3];
260+
u32 start_sector;
261+
u32 num_sectors;
262+
} __attribute__((__packed__)) partition[4];
263+
u8 signature[2];
264+
} mbr;
265+
fread(&mbr, 1, 0x200, f);
266+
if(mbr.signature[0] == 0x55 || mbr.signature[1] == 0xAA) {
267+
iprintf("Found MBR in %s.\nImage is not encrypted.\nOperation aborted.", nand_type);
268+
fclose(f);
269+
return;
270+
}
271+
rewind(f);
272+
//Sanity checks end
273+
274+
iprintf("Sure? NAND restore is DANGEROUS!");
275+
iprintf("START + SELECT confirm\n");
276+
iprintf("B to cancel\n");
277+
278+
while (1) {
279+
scanKeys();
280+
int keys = keysHeld();
281+
if ((keys & KEY_START) && (keys & KEY_SELECT))break;
282+
if (keys & KEY_B) {
283+
clearStatus();
284+
fclose(f);
285+
return;
286+
}
287+
swiWaitForVBlank();
288+
}
289+
290+
clearStatus();
291+
292+
iprintf("Reading %s/%s\n\n", dirname, nand_type);
293+
size_t i;
294+
size_t sectors = 128;
295+
size_t blocks = (sizMB * 1024 * 1024) / (sectors * 512);
296+
for (i=0; i < blocks; i++) {
232297

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

237-
while(1){
238-
scanKeys();
239-
int keys = keysHeld();
240-
if((keys & KEY_START) && (keys & KEY_SELECT))break;
241-
if(keys & KEY_B){
242-
clearStatus();
243-
return;
244-
}
245-
swiWaitForVBlank();
300+
if(read != 512 * sectors) {
301+
iprintf("\nError reading SD!\n");
302+
break;
246303
}
247304

248-
clearStatus();
249-
250-
FILE *f = fopen(nand_type, "rb");
251-
252-
if (NULL == f) {
253-
iprintf("failure creating %s\n", nand_type);
254-
} else {
255-
iprintf("Reading %s/%s\n\n", dirname, nand_type);
256-
size_t i;
257-
size_t sectors = 128;
258-
size_t blocks = (sizMB * 1024 * 1024) / (sectors * 512);
259-
for (i=0; i < blocks; i++) {
260-
261-
size_t read = fread(firmware_buffer, 1, 512 * sectors, f);
262-
263-
if(read != 512 * sectors) {
264-
iprintf("\nError reading SD!\n");
265-
break;
266-
}
267-
268-
if(!nand_WriteSectors(i * sectors,sectors,firmware_buffer)) {
269-
iprintf("\nError writing NAND!\n");
270-
break;
271-
}
272-
273-
iprintf("%d/%d DON'T poweroff!\r", i+1, blocks);
274-
}
275-
fclose(f);
305+
if(!nand_WriteSectors(i * sectors,sectors,firmware_buffer)) {
306+
iprintf("\nError writing NAND!\n");
307+
break;
276308
}
309+
310+
iprintf("%d/%d DON'T poweroff!\r", i+1, blocks);
277311
}
278-
312+
fclose(f);
313+
314+
clearStatus();
315+
iprintf("Restore %s success.\nYou may now Exit and restart\nyour console.",nand_type);
316+
279317
}
280318

281319
void dumpCID(){

0 commit comments

Comments
 (0)