From 12e9ca82d122e70c10906b62ea489e61883241cc Mon Sep 17 00:00:00 2001 From: d0k3 Date: Wed, 22 Feb 2017 17:41:18 +0100 Subject: [PATCH] Include essential.exefs file in virtual NAND (if available) --- source/nand/essentials.h | 7 ++++++- source/virtual/vnand.c | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/source/nand/essentials.h b/source/nand/essentials.h index b265c3a..a61a674 100644 --- a/source/nand/essentials.h +++ b/source/nand/essentials.h @@ -3,7 +3,12 @@ #include "common.h" #include "exefs.h" -#define ESSENTIAL_SECTOR 0x1 // start sector of the essential backup in NAND +// start sector of the essential backup in NAND +// careful with this, essential backup should never reach sector 0x96 +#define ESSENTIAL_SECTOR 0x1 + +// magic number for essential backup +#define ESSENTIAL_MAGIC 'n', 'a', 'n', 'd', '_', 'h', 'd', 'r', 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00 // /rw/sys/LocalFriendCodeSeed_B (/_A) file // see: http://3dbrew.org/wiki/Nandrw/sys/LocalFriendCodeSeed_B diff --git a/source/virtual/vnand.c b/source/virtual/vnand.c index 658baee..bf8b191 100644 --- a/source/virtual/vnand.c +++ b/source/virtual/vnand.c @@ -1,12 +1,14 @@ #include "vnand.h" #include "nand.h" #include "agbsave.h" +#include "essentials.h" #include "platform.h" #define VFLAG_ON_O3DS NAND_TYPE_O3DS #define VFLAG_ON_N3DS NAND_TYPE_N3DS #define VFLAG_ON_NO3DS NAND_TYPE_NO3DS #define VFLAG_ON_NAND (VFLAG_ON_O3DS | VFLAG_ON_N3DS | VFLAG_ON_NO3DS) +#define VFLAG_ESSENTIAL (1UL<<28) #define VFLAG_GBA_VC (1UL<<29) #define VFLAG_NEEDS_OTP (1UL<<30) #define VFLAG_NAND_SIZE (1UL<<31) @@ -32,8 +34,9 @@ static const VirtualFile vNandFileTemplates[] = { { "nand_hdr.bin" , 0x00000000, 0x00000200, 0xFF, VFLAG_ON_NAND }, { "twlmbr.bin" , 0x000001BE, 0x00000042, 0x03, VFLAG_ON_NAND }, { "free0x01.bin" , 0x00000200, 0x00012A00, 0xFF, VFLAG_ON_NAND }, - { "free0x1D7800.bin" , 0x3AF00000, 0x00000000, 0xFF, VFLAG_ON_O3DS | VFLAG_NAND_SIZE }, - { "free0x26C000.bin" , 0x4D800000, 0x00000000, 0xFF, VFLAG_ON_N3DS | VFLAG_ON_NO3DS | VFLAG_NAND_SIZE }, + { "essential.exefs" , 0x00000200, 0x00000000, 0xFF, VFLAG_ON_NAND | VFLAG_ESSENTIAL }, + { "bonus0x1D7800.bin", 0x3AF00000, 0x00000000, 0xFF, VFLAG_ON_O3DS | VFLAG_NAND_SIZE }, + { "bonus0x26C000.bin", 0x4D800000, 0x00000000, 0xFF, VFLAG_ON_N3DS | VFLAG_ON_NO3DS | VFLAG_NAND_SIZE }, { "gbavc.sav" , 0x0B100200, 0x00000000, 0x07, VFLAG_ON_NAND | VFLAG_GBA_VC }, }; @@ -75,6 +78,13 @@ bool ReadVNandDir(VirtualFile* vfile, VirtualDir* vdir) { // uses a generic vdir if (nand_size <= vfile->offset) continue; vfile->size = nand_size - vfile->offset; } + if (vfile->flags & VFLAG_ESSENTIAL) { + const u8 magic[] = { ESSENTIAL_MAGIC }; + u8 data[sizeof(magic)]; + ReadNandBytes(data, vfile->offset, sizeof(magic), vfile->keyslot, nand_src); + if (memcmp(data, magic, sizeof(magic)) != 0) continue; + vfile->size = sizeof(EssentialBackup); + } if (vfile->flags & VFLAG_GBA_VC) { if (CheckAgbSaveCmac(nand_src) != 0) continue; vfile->size = GetAgbSaveSize(nand_src);