disable DSi mode cartridge protection when DSi header is borked

This commit is contained in:
RSDuck
2023-05-11 13:24:01 +02:00
parent 758db2b986
commit ca7fb4f55e
2 changed files with 21 additions and 12 deletions

View File

@ -194,14 +194,14 @@ void Key2_Encrypt(u8* data, u32 len)
} }
CartCommon::CartCommon(u8* rom, u32 len, u32 chipid) CartCommon::CartCommon(u8* rom, u32 len, u32 chipid, bool badDSiDump)
{ {
ROM = rom; ROM = rom;
ROMLength = len; ROMLength = len;
ChipID = chipid; ChipID = chipid;
u8 unitcode = ROM[0x12]; u8 unitcode = ROM[0x12];
IsDSi = (unitcode & 0x02) != 0; IsDSi = (unitcode & 0x02) != 0 && !badDSiDump;
DSiBase = *(u16*)&ROM[0x92] << 19; DSiBase = *(u16*)&ROM[0x92] << 19;
} }
@ -403,7 +403,7 @@ void CartCommon::ReadROM(u32 addr, u32 len, u8* data, u32 offset)
} }
CartRetail::CartRetail(u8* rom, u32 len, u32 chipid) : CartCommon(rom, len, chipid) CartRetail::CartRetail(u8* rom, u32 len, u32 chipid, bool badDSiDump) : CartCommon(rom, len, chipid, badDSiDump)
{ {
SRAM = nullptr; SRAM = nullptr;
} }
@ -865,7 +865,7 @@ u8 CartRetail::SRAMWrite_FLASH(u8 val, u32 pos, bool last)
} }
CartRetailNAND::CartRetailNAND(u8* rom, u32 len, u32 chipid) : CartRetail(rom, len, chipid) CartRetailNAND::CartRetailNAND(u8* rom, u32 len, u32 chipid) : CartRetail(rom, len, chipid, false)
{ {
} }
@ -1092,7 +1092,7 @@ void CartRetailNAND::BuildSRAMID()
} }
CartRetailIR::CartRetailIR(u8* rom, u32 len, u32 chipid, u32 irversion) : CartRetail(rom, len, chipid) CartRetailIR::CartRetailIR(u8* rom, u32 len, u32 chipid, u32 irversion, bool badDSiDump) : CartRetail(rom, len, chipid, badDSiDump)
{ {
IRVersion = irversion; IRVersion = irversion;
} }
@ -1138,7 +1138,7 @@ u8 CartRetailIR::SPIWrite(u8 val, u32 pos, bool last)
} }
CartRetailBT::CartRetailBT(u8* rom, u32 len, u32 chipid) : CartRetail(rom, len, chipid) CartRetailBT::CartRetailBT(u8* rom, u32 len, u32 chipid) : CartRetail(rom, len, chipid, false)
{ {
Log(LogLevel::Info,"POKETYPE CART\n"); Log(LogLevel::Info,"POKETYPE CART\n");
} }
@ -1172,7 +1172,7 @@ u8 CartRetailBT::SPIWrite(u8 val, u32 pos, bool last)
} }
CartHomebrew::CartHomebrew(u8* rom, u32 len, u32 chipid) : CartCommon(rom, len, chipid) CartHomebrew::CartHomebrew(u8* rom, u32 len, u32 chipid) : CartCommon(rom, len, chipid, false)
{ {
SD = nullptr; SD = nullptr;
} }
@ -1630,6 +1630,15 @@ bool LoadROM(const u8* romdata, u32 romlen)
u8 unitcode = Header.UnitCode; u8 unitcode = Header.UnitCode;
bool dsi = (unitcode & 0x02) != 0; bool dsi = (unitcode & 0x02) != 0;
bool badDSiDump = false;
u32 dsiRegion = *(u32*)&CartROM[0x1B0];
if (dsi && dsiRegion == 0)
{
Log(LogLevel::Info, "DS header indicates DSi, but region is zero. Going in bad dump mode.\n");
badDSiDump = true;
dsi = false;
}
size_t bannersize = dsi ? 0x23C0 : 0xA40; size_t bannersize = dsi ? 0x23C0 : 0xA40;
if (Header.BannerOffset >= 0x200 && Header.BannerOffset < (CartROMSize - bannersize)) if (Header.BannerOffset >= 0x200 && Header.BannerOffset < (CartROMSize - bannersize))
@ -1725,11 +1734,11 @@ bool LoadROM(const u8* romdata, u32 romlen)
else if (CartID & 0x08000000) else if (CartID & 0x08000000)
Cart = new CartRetailNAND(CartROM, CartROMSize, CartID); Cart = new CartRetailNAND(CartROM, CartROMSize, CartID);
else if (irversion != 0) else if (irversion != 0)
Cart = new CartRetailIR(CartROM, CartROMSize, CartID, irversion); Cart = new CartRetailIR(CartROM, CartROMSize, CartID, irversion, badDSiDump);
else if ((gamecode & 0xFFFFFF) == 0x505A55) // UZPx else if ((gamecode & 0xFFFFFF) == 0x505A55) // UZPx
Cart = new CartRetailBT(CartROM, CartROMSize, CartID); Cart = new CartRetailBT(CartROM, CartROMSize, CartID);
else else
Cart = new CartRetail(CartROM, CartROMSize, CartID); Cart = new CartRetail(CartROM, CartROMSize, CartID, badDSiDump);
if (Cart) if (Cart)
Cart->Reset(); Cart->Reset();

View File

@ -33,7 +33,7 @@ namespace NDSCart
class CartCommon class CartCommon
{ {
public: public:
CartCommon(u8* rom, u32 len, u32 chipid); CartCommon(u8* rom, u32 len, u32 chipid, bool badDSiDump);
virtual ~CartCommon(); virtual ~CartCommon();
virtual u32 Type() { return 0x001; } virtual u32 Type() { return 0x001; }
@ -75,7 +75,7 @@ protected:
class CartRetail : public CartCommon class CartRetail : public CartCommon
{ {
public: public:
CartRetail(u8* rom, u32 len, u32 chipid); CartRetail(u8* rom, u32 len, u32 chipid, bool badDSiDump);
virtual ~CartRetail() override; virtual ~CartRetail() override;
virtual u32 Type() override { return 0x101; } virtual u32 Type() override { return 0x101; }
@ -145,7 +145,7 @@ private:
class CartRetailIR : public CartRetail class CartRetailIR : public CartRetail
{ {
public: public:
CartRetailIR(u8* rom, u32 len, u32 chipid, u32 irversion); CartRetailIR(u8* rom, u32 len, u32 chipid, u32 irversion, bool badDSiDump);
~CartRetailIR() override; ~CartRetailIR() override;
virtual u32 Type() override { return 0x103; } virtual u32 Type() override { return 0x103; }