Skip to content

Commit

Permalink
properly make the DSi NAND instance-unique
Browse files Browse the repository at this point in the history
  • Loading branch information
Arisotura committed Sep 23, 2022
1 parent fc11258 commit 8678673
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 44 deletions.
45 changes: 17 additions & 28 deletions src/DSi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 hwinfoS[0xA4];
u8 hwinfoN[0x9C];
DSi_NAND::ReadHardwareInfo(hwinfoS, hwinfoN);
u8 userdata[0x1B0];
DSi_NAND::ReadUserData(userdata);
for (u32 i = 0; i < 0x128; i+=4)
ARM9Write32(0x02000400+i, *(u32*)&userdata[0x88+i]);

for (u32 i = 0; i < 0x14; i+=4)
ARM9Write32(0x02000600+i, *(u32*)&hwinfoN[0x88+i]);
u8 hwinfoS[0xA4];
u8 hwinfoN[0x9C];
DSi_NAND::ReadHardwareInfo(hwinfoS, hwinfoN);

for (u32 i = 0; i < 0x18; i+=4)
ARM9Write32(0x02FFFD68+i, *(u32*)&hwinfoS[0x88+i]);
for (u32 i = 0; i < 0x14; i+=4)
ARM9Write32(0x02000600+i, *(u32*)&hwinfoN[0x88+i]);

DSi_NAND::DeInit();
}
for (u32 i = 0; i < 0x18; i+=4)
ARM9Write32(0x02FFFD68+i, *(u32*)&hwinfoS[0x88+i]);

fclose(nand);
DSi_NAND::DeInit();
}

u8 nwifiver = SPI_Firmware::GetNWifiVersion();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
49 changes: 48 additions & 1 deletion src/DSi_NAND.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/DSi_NAND.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/DSi_SD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 7 additions & 12 deletions src/frontend/qt_sdl/TitleManagerDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include "ui_TitleImportDialog.h"


FILE* TitleManagerDialog::curNAND = nullptr;
bool TitleManagerDialog::NANDInited = false;
TitleManagerDialog* TitleManagerDialog::currentDlg = nullptr;

extern std::string EmuDirectory;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/frontend/qt_sdl/TitleManagerDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class TitleManagerDialog : public QDialog
explicit TitleManagerDialog(QWidget* parent);
~TitleManagerDialog();

static FILE* curNAND;
static bool NANDInited;
static bool openNAND();
static void closeNAND();

Expand Down

0 comments on commit 8678673

Please sign in to comment.