diff --git a/include/arm9/nandimage.h b/include/arm9/nandimage.h index 0e922c3..6dbaed3 100644 --- a/include/arm9/nandimage.h +++ b/include/arm9/nandimage.h @@ -1,9 +1,12 @@ #pragma once #include "types.h" +#include "arm9/ncsd.h" +#include "fatfs/ff.h" #define NANDIMG_ERROR_BADPATH -1 #define NANDIMG_ERROR_NEXISTS -2 #define NANDIMG_ERROR_NCONTS -3 int validateNandImage(const char *filePath); +bool isNandImageCompatible(FIL *file); diff --git a/include/arm9/ncsd.h b/include/arm9/ncsd.h index d35c040..f339635 100644 --- a/include/arm9/ncsd.h +++ b/include/arm9/ncsd.h @@ -3,7 +3,6 @@ #include "types.h" - typedef struct { u8 signature[0x100]; diff --git a/source/arm9/menu_nand.c b/source/arm9/menu_nand.c index 30eaded..d51cac8 100644 --- a/source/arm9/menu_nand.c +++ b/source/arm9/menu_nand.c @@ -12,6 +12,7 @@ #include "arm9/timer.h" #include "arm9/partitions.h" #include "arm9/firmwriter.h" +#include "arm9/nandimage.h" static u64 getFreeSpace(const char *drive) { @@ -193,6 +194,13 @@ goto fail; } + if(!isNandImageCompatible(&file)) + { + uiPrintError("NAND file is not compatible with this console!\n"); + f_close(&file); + goto fail; + } + uiPrintTextAt(0, 4, "Unmounting NAND fs...\n"); unmount_nand_fs(); diff --git a/source/arm9/nandimage.c b/source/arm9/nandimage.c index 7db252d..b313e8b 100644 --- a/source/arm9/nandimage.c +++ b/source/arm9/nandimage.c @@ -1,8 +1,8 @@ #include #include #include +#include "arm9/dev.h" #include "arm9/nandimage.h" -#include "fatfs/ff.h" #include "util.h" int validateNandImage(const char *filePath) @@ -26,3 +26,24 @@ return 0; } + +bool isNandImageCompatible(FIL *file) +{ + NCSD_header imageHeader; + NCSD_header nandHeader; + size_t bytesRead; + + if(f_read(file, &imageHeader, sizeof(NCSD_header), &bytesRead) != FR_OK + || bytesRead != sizeof(NCSD_header)) + { + return false; + } + + if(!dev_rawnand->read_sector(0, 1, &nandHeader)) + { + return false; + } + + /* compare everything except the signature */ + return memcmp(&imageHeader.magic, &nandHeader.magic, 0x100) == 0; +}