aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArisotura <thetotalworm@gmail.com>2022-09-23 22:53:23 +0200
committerArisotura <thetotalworm@gmail.com>2022-09-23 22:53:23 +0200
commit86786738cc91dbea5f9dcdf0e147bd6615e1ca46 (patch)
tree090abb961c3b990517a8b5d6c5018d7801c71202
parentfc112580710a1a96b38ded2481e54a1ba42a3e0d (diff)
properly make the DSi NAND instance-unique
-rw-r--r--src/DSi.cpp45
-rw-r--r--src/DSi_NAND.cpp49
-rw-r--r--src/DSi_NAND.h4
-rw-r--r--src/DSi_SD.cpp5
-rw-r--r--src/frontend/qt_sdl/TitleManagerDialog.cpp19
-rw-r--r--src/frontend/qt_sdl/TitleManagerDialog.h2
6 files changed, 80 insertions, 44 deletions
diff --git a/src/DSi.cpp b/src/DSi.cpp
index db63440..85c6cb6 100644
--- a/src/DSi.cpp
+++ b/src/DSi.cpp
@@ -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();
@@ -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;
}
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);
diff --git a/src/DSi_NAND.h b/src/DSi_NAND.h
index 6feb2d0..a23e62f 100644
--- a/src/DSi_NAND.h
+++ b/src/DSi_NAND.h
@@ -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);
diff --git a/src/DSi_SD.cpp b/src/DSi_SD.cpp
index c6932d4..e603347 100644
--- a/src/DSi_SD.cpp
+++ b/src/DSi_SD.cpp
@@ -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;
diff --git a/src/frontend/qt_sdl/TitleManagerDialog.cpp b/src/frontend/qt_sdl/TitleManagerDialog.cpp
index a4c9cfd..8087ee6 100644
--- a/src/frontend/qt_sdl/TitleManagerDialog.cpp
+++ b/src/frontend/qt_sdl/TitleManagerDialog.cpp
@@ -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;
}
}
diff --git a/src/frontend/qt_sdl/TitleManagerDialog.h b/src/frontend/qt_sdl/TitleManagerDialog.h
index 201e5e8..cba7047 100644
--- a/src/frontend/qt_sdl/TitleManagerDialog.h
+++ b/src/frontend/qt_sdl/TitleManagerDialog.h
@@ -45,7 +45,7 @@ public:
explicit TitleManagerDialog(QWidget* parent);
~TitleManagerDialog();
- static FILE* curNAND;
+ static bool NANDInited;
static bool openNAND();
static void closeNAND();