From 86786738cc91dbea5f9dcdf0e147bd6615e1ca46 Mon Sep 17 00:00:00 2001 From: Arisotura Date: Fri, 23 Sep 2022 22:53:23 +0200 Subject: properly make the DSi NAND instance-unique --- src/DSi_NAND.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) (limited to 'src/DSi_NAND.cpp') diff --git a/src/DSi_NAND.cpp b/src/DSi_NAND.cpp index e24ed31..912fee4 100644 --- a/src/DSi_NAND.cpp +++ b/src/DSi_NAND.cpp @@ -49,8 +49,48 @@ UINT FF_ReadNAND(BYTE* buf, LBA_t sector, UINT num); UINT FF_WriteNAND(BYTE* buf, LBA_t sector, UINT num); -bool Init(FILE* nandfile, u8* es_keyY) +bool Init(u8* es_keyY) { + CurFile = nullptr; + + std::string nandpath = Platform::GetConfigString(Platform::DSi_NANDPath); + std::string instnand = nandpath + Platform::InstanceFileSuffix(); + + FILE* nandfile = Platform::OpenLocalFile(instnand, "r+b"); + if ((!nandfile) && (Platform::InstanceID() > 0)) + { + FILE* orig = Platform::OpenLocalFile(nandpath, "rb"); + if (!orig) + { + printf("Failed to open DSi NAND\n"); + return false; + } + + fseek(orig, 0, SEEK_END); + long len = ftell(orig); + fseek(orig, 0, SEEK_SET); + + nandfile = Platform::OpenLocalFile(instnand, "w+b"); + if (nandfile) + { + u8* tmpbuf = new u8[0x10000]; + for (long i = 0; i < len; i+=0x10000) + { + long blklen = 0x10000; + if ((i+blklen) > len) blklen = len-i; + + fread(tmpbuf, blklen, 1, orig); + fwrite(tmpbuf, blklen, 1, nandfile); + } + delete[] tmpbuf; + } + + fclose(orig); + fclose(nandfile); + + nandfile = Platform::OpenLocalFile(instnand, "r+b"); + } + if (!nandfile) return false; @@ -138,10 +178,17 @@ void DeInit() f_unmount("0:"); ff_disk_close(); + if (CurFile) fclose(CurFile); CurFile = nullptr; } +FILE* GetFile() +{ + return CurFile; +} + + void GetIDs(u8* emmc_cid, u64& consoleid) { memcpy(emmc_cid, eMMC_CID, 16); -- cgit v1.2.3