diff --git a/source/nand/sysfiles.h b/source/nand/essentials.h similarity index 62% rename from source/nand/sysfiles.h rename to source/nand/essentials.h index 164502f..b265c3a 100644 --- a/source/nand/sysfiles.h +++ b/source/nand/essentials.h @@ -1,6 +1,9 @@ #pragma once #include "common.h" +#include "exefs.h" + +#define ESSENTIAL_SECTOR 0x1 // start sector of the essential backup in NAND // /rw/sys/LocalFriendCodeSeed_B (/_A) file // see: http://3dbrew.org/wiki/Nandrw/sys/LocalFriendCodeSeed_B @@ -29,3 +32,16 @@ typedef struct { u8 unknown; char serial[0xF]; } __attribute__((packed)) SecureInfo; + +// includes all essential system files +// (this is of our own making) +typedef struct { + ExeFsHeader header; + u8 nand_hdr[0x200]; + SecureInfo secinfo; + u8 padding_secinfo[0x200 - sizeof(SecureInfo)]; + MovableSed movable; + u8 padding_movable[0x200 - sizeof(MovableSed)]; + LocalFriendCodeSeed frndseed; + u8 padding_frndseed[0x200 - sizeof(LocalFriendCodeSeed)]; +} __attribute__((packed)) EssentialBackup; diff --git a/source/nand/nandutil.c b/source/nand/nandutil.c index b8dd813..be5d7f0 100644 --- a/source/nand/nandutil.c +++ b/source/nand/nandutil.c @@ -2,8 +2,7 @@ #include "nand.h" #include "firm.h" #include "fatmbr.h" -#include "sysfiles.h" // for essential backup struct -#include "exefs.h" // for essential backup struct +#include "essentials.h" // for essential backup struct #include "image.h" #include "fsinit.h" #include "fsperm.h" @@ -11,16 +10,6 @@ #include "ui.h" #include "vff.h" -typedef struct { - ExeFsHeader header; - u8 nand_hdr[0x200]; - SecureInfo secinfo; - u8 padding_secinfo[0x200 - sizeof(SecureInfo)]; - MovableSed movable; - u8 padding_movable[0x200 - sizeof(MovableSed)]; - LocalFriendCodeSeed frndseed; - u8 padding_frndseed[0x200 - sizeof(LocalFriendCodeSeed)]; -} __attribute__((packed)) EssentialBackup; u32 ReadNandFile(FIL* file, void* buffer, u32 sector, u32 count, u32 keyslot) { u32 offset = sector * 0x200; @@ -211,6 +200,11 @@ u32 SafeRestoreNandDump(const char* path) { } if (!SetWritePermissions(PERM_SYS_LVL2, true)) return 1; + // inject essential backup to NAND + EssentialBackup* essential = (EssentialBackup*) TEMP_BUFFER; + if (BuildEssentialBackup("1:/nand.bin", essential) == 0) + WriteNandSectors((u8*) essential, ESSENTIAL_SECTOR, (sizeof(EssentialBackup) + 0x1FF) / 0x200, 0xFF, NAND_SYSNAND); + // open file, get size if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK) return 1; @@ -231,7 +225,7 @@ u32 SafeRestoreNandDump(const char* path) { if (WriteNandSectors(MAIN_BUFFER, s, count, 0xFF, NAND_SYSNAND)) ret = 1; if (btr != count * 0x200) ret = 1; if (!ShowProgress(s + count, fsize / 0x200, path)) ret = 1; - } + } } fvx_close(&file);