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,30 +511,24 @@ void SetupDirectBoot()
ARM9Write32(0x02FFE000+i, tmp);
}
FILE* nand = Platform::OpenLocalFile(Platform::GetConfigString(Platform::DSi_NANDPath), "r+b");
if (nand)
if (DSi_NAND::Init(&DSi::ARM7iBIOS[0x8308]))
{
if (DSi_NAND::Init(nand, &DSi::ARM7iBIOS[0x8308]))
{
u8 userdata[0x1B0];
DSi_NAND::ReadUserData(userdata);
for (u32 i = 0; i < 0x128; i+=4)
ARM9Write32(0x02000400+i, *(u32*)&userdata[0x88+i]);
u8 userdata[0x1B0];
DSi_NAND::ReadUserData(userdata);
for (u32 i = 0; i < 0x128; i+=4)
ARM9Write32(0x02000400+i, *(u32*)&userdata[0x88+i]);
u8 hwinfoS[0xA4];
u8 hwinfoN[0x9C];
DSi_NAND::ReadHardwareInfo(hwinfoS, hwinfoN);
u8 hwinfoS[0xA4];
u8 hwinfoN[0x9C];
DSi_NAND::ReadHardwareInfo(hwinfoS, hwinfoN);
for (u32 i = 0; i < 0x14; i+=4)
ARM9Write32(0x02000600+i, *(u32*)&hwinfoN[0x88+i]);
for (u32 i = 0; i < 0x14; i+=4)
ARM9Write32(0x02000600+i, *(u32*)&hwinfoN[0x88+i]);
for (u32 i = 0; i < 0x18; i+=4)
ARM9Write32(0x02FFFD68+i, *(u32*)&hwinfoS[0x88+i]);
for (u32 i = 0; i < 0x18; i+=4)
ARM9Write32(0x02FFFD68+i, *(u32*)&hwinfoS[0x88+i]);
DSi_NAND::DeInit();
}
fclose(nand);
DSi_NAND::DeInit();
}
u8 nwifiver = SPI_Firmware::GetNWifiVersion();
@ -707,19 +701,14 @@ bool LoadNAND()
{
printf("Loading DSi NAND\n");
FILE* nand = Platform::OpenLocalFile(Platform::GetConfigString(Platform::DSi_NANDPath), "r+b");
if (!nand)
{
printf("Failed to open DSi NAND\n");
return false;
}
if (!DSi_NAND::Init(nand, &DSi::ARM7iBIOS[0x8308]))
if (!DSi_NAND::Init(&DSi::ARM7iBIOS[0x8308]))
{
printf("Failed to load DSi NAND\n");
return false;
}
FILE* nand = DSi_NAND::GetFile();
// Make sure NWRAM is accessible.
// 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
@ -2681,7 +2670,7 @@ u16 ARM7IORead16(u32 addr)
case 0x04004D04: if (SCFG_BIOS & (1<<10)) return 0; return (ConsoleID >> 32) & 0xFFFF;
case 0x04004D06: if (SCFG_BIOS & (1<<10)) return 0; return ConsoleID >> 48;
case 0x04004D08: return 0;
case 0x4004700: return DSi_DSP::SNDExCnt;
}

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);
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);

View File

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

View File

@ -136,7 +136,10 @@ void DSi_SDHost::Reset()
else
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);
Ports[0] = sd;

View File

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

View File

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