Skip to content

Commit

Permalink
disable DSi mode cartridge protection when DSi header is borked
Browse files Browse the repository at this point in the history
  • Loading branch information
RSDuck committed May 11, 2023
1 parent 758db2b commit ca7fb4f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
27 changes: 18 additions & 9 deletions src/NDSCart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
ROMLength = len;
ChipID = chipid;

u8 unitcode = ROM[0x12];
IsDSi = (unitcode & 0x02) != 0;
IsDSi = (unitcode & 0x02) != 0 && !badDSiDump;
DSiBase = *(u16*)&ROM[0x92] << 19;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
{
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -1630,6 +1630,15 @@ bool LoadROM(const u8* romdata, u32 romlen)

u8 unitcode = Header.UnitCode;
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;
if (Header.BannerOffset >= 0x200 && Header.BannerOffset < (CartROMSize - bannersize))
Expand Down Expand Up @@ -1725,11 +1734,11 @@ bool LoadROM(const u8* romdata, u32 romlen)
else if (CartID & 0x08000000)
Cart = new CartRetailNAND(CartROM, CartROMSize, CartID);
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
Cart = new CartRetailBT(CartROM, CartROMSize, CartID);
else
Cart = new CartRetail(CartROM, CartROMSize, CartID);
Cart = new CartRetail(CartROM, CartROMSize, CartID, badDSiDump);

if (Cart)
Cart->Reset();
Expand Down
6 changes: 3 additions & 3 deletions src/NDSCart.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace NDSCart
class CartCommon
{
public:
CartCommon(u8* rom, u32 len, u32 chipid);
CartCommon(u8* rom, u32 len, u32 chipid, bool badDSiDump);
virtual ~CartCommon();

virtual u32 Type() { return 0x001; }
Expand Down Expand Up @@ -75,7 +75,7 @@ class CartCommon
class CartRetail : public CartCommon
{
public:
CartRetail(u8* rom, u32 len, u32 chipid);
CartRetail(u8* rom, u32 len, u32 chipid, bool badDSiDump);
virtual ~CartRetail() override;

virtual u32 Type() override { return 0x101; }
Expand Down Expand Up @@ -145,7 +145,7 @@ class CartRetailNAND : public CartRetail
class CartRetailIR : public CartRetail
{
public:
CartRetailIR(u8* rom, u32 len, u32 chipid, u32 irversion);
CartRetailIR(u8* rom, u32 len, u32 chipid, u32 irversion, bool badDSiDump);
~CartRetailIR() override;

virtual u32 Type() override { return 0x103; }
Expand Down

0 comments on commit ca7fb4f

Please sign in to comment.