properly make the DSi NAND instance-unique

This commit is contained in:
Arisotura
2022-09-23 22:53:23 +02:00
parent fc11258071
commit 86786738cc
6 changed files with 80 additions and 44 deletions

View File

@ -511,10 +511,7 @@ void SetupDirectBoot()
ARM9Write32(0x02FFE000+i, tmp); ARM9Write32(0x02FFE000+i, tmp);
} }
FILE* nand = Platform::OpenLocalFile(Platform::GetConfigString(Platform::DSi_NANDPath), "r+b"); if (DSi_NAND::Init(&DSi::ARM7iBIOS[0x8308]))
if (nand)
{
if (DSi_NAND::Init(nand, &DSi::ARM7iBIOS[0x8308]))
{ {
u8 userdata[0x1B0]; u8 userdata[0x1B0];
DSi_NAND::ReadUserData(userdata); DSi_NAND::ReadUserData(userdata);
@ -534,9 +531,6 @@ void SetupDirectBoot()
DSi_NAND::DeInit(); DSi_NAND::DeInit();
} }
fclose(nand);
}
u8 nwifiver = SPI_Firmware::GetNWifiVersion(); u8 nwifiver = SPI_Firmware::GetNWifiVersion();
ARM9Write8(0x020005E0, nwifiver); ARM9Write8(0x020005E0, nwifiver);
@ -707,19 +701,14 @@ bool LoadNAND()
{ {
printf("Loading DSi NAND\n"); printf("Loading DSi NAND\n");
FILE* nand = Platform::OpenLocalFile(Platform::GetConfigString(Platform::DSi_NANDPath), "r+b"); if (!DSi_NAND::Init(&DSi::ARM7iBIOS[0x8308]))
if (!nand)
{
printf("Failed to open DSi NAND\n");
return false;
}
if (!DSi_NAND::Init(nand, &DSi::ARM7iBIOS[0x8308]))
{ {
printf("Failed to load DSi NAND\n"); printf("Failed to load DSi NAND\n");
return false; return false;
} }
FILE* nand = DSi_NAND::GetFile();
// Make sure NWRAM is accessible. // Make sure NWRAM is accessible.
// The Bits are set to the startup values in Reset() and we might // The Bits are set to the startup values in Reset() and we might
// still have them on default (0) or some bits cleared by the previous // still have them on default (0) or some bits cleared by the previous

View File

@ -49,8 +49,48 @@ UINT FF_ReadNAND(BYTE* buf, LBA_t sector, UINT num);
UINT FF_WriteNAND(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) if (!nandfile)
return false; return false;
@ -138,10 +178,17 @@ void DeInit()
f_unmount("0:"); f_unmount("0:");
ff_disk_close(); ff_disk_close();
if (CurFile) fclose(CurFile);
CurFile = nullptr; CurFile = nullptr;
} }
FILE* GetFile()
{
return CurFile;
}
void GetIDs(u8* emmc_cid, u64& consoleid) void GetIDs(u8* emmc_cid, u64& consoleid)
{ {
memcpy(emmc_cid, eMMC_CID, 16); memcpy(emmc_cid, eMMC_CID, 16);

View File

@ -34,9 +34,11 @@ enum
TitleData_BannerSav, TitleData_BannerSav,
}; };
bool Init(FILE* nand, u8* es_keyY); bool Init(u8* es_keyY);
void DeInit(); void DeInit();
FILE* GetFile();
void GetIDs(u8* emmc_cid, u64& consoleid); void GetIDs(u8* emmc_cid, u64& consoleid);
void ReadHardwareInfo(u8* dataS, u8* dataN); void ReadHardwareInfo(u8* dataS, u8* dataN);

View File

@ -136,7 +136,10 @@ void DSi_SDHost::Reset()
else else
sd = nullptr; sd = nullptr;
mmc = new DSi_MMCStorage(this, true, Platform::GetConfigString(Platform::DSi_NANDPath)); std::string nandpath = Platform::GetConfigString(Platform::DSi_NANDPath);
std::string instnand = nandpath + Platform::InstanceFileSuffix();
mmc = new DSi_MMCStorage(this, true, instnand);
mmc->SetCID(DSi::eMMC_CID); mmc->SetCID(DSi::eMMC_CID);
Ports[0] = sd; Ports[0] = sd;

View File

@ -31,7 +31,7 @@
#include "ui_TitleImportDialog.h" #include "ui_TitleImportDialog.h"
FILE* TitleManagerDialog::curNAND = nullptr; bool TitleManagerDialog::NANDInited = false;
TitleManagerDialog* TitleManagerDialog::currentDlg = nullptr; TitleManagerDialog* TitleManagerDialog::currentDlg = nullptr;
extern std::string EmuDirectory; extern std::string EmuDirectory;
@ -136,6 +136,8 @@ void TitleManagerDialog::createTitleItem(u32 category, u32 titleid)
bool TitleManagerDialog::openNAND() bool TitleManagerDialog::openNAND()
{ {
NANDInited = false;
FILE* bios7i = Platform::OpenLocalFile(Config::DSiBIOS7Path, "rb"); FILE* bios7i = Platform::OpenLocalFile(Config::DSiBIOS7Path, "rb");
if (!bios7i) if (!bios7i)
return false; return false;
@ -145,28 +147,21 @@ bool TitleManagerDialog::openNAND()
fread(es_keyY, 16, 1, bios7i); fread(es_keyY, 16, 1, bios7i);
fclose(bios7i); fclose(bios7i);
curNAND = Platform::OpenLocalFile(Config::DSiNANDPath, "r+b"); if (!DSi_NAND::Init(es_keyY))
if (!curNAND)
return false;
if (!DSi_NAND::Init(curNAND, es_keyY))
{ {
fclose(curNAND);
curNAND = nullptr;
return false; return false;
} }
NANDInited = true;
return true; return true;
} }
void TitleManagerDialog::closeNAND() void TitleManagerDialog::closeNAND()
{ {
if (curNAND) if (NANDInited)
{ {
DSi_NAND::DeInit(); DSi_NAND::DeInit();
NANDInited = false;
fclose(curNAND);
curNAND = nullptr;
} }
} }

View File

@ -45,7 +45,7 @@ public:
explicit TitleManagerDialog(QWidget* parent); explicit TitleManagerDialog(QWidget* parent);
~TitleManagerDialog(); ~TitleManagerDialog();
static FILE* curNAND; static bool NANDInited;
static bool openNAND(); static bool openNAND();
static void closeNAND(); static void closeNAND();