cart: ensure each set of commands can only be run in the correct command mode.

fixes #1083 (there was a chance an encrypted KEY1 command could be interpreted as something else and fuck things up)
This commit is contained in:
Arisotura 2021-04-30 17:29:04 +02:00
parent 5e648a8db3
commit c2f37d44ce
1 changed files with 99 additions and 80 deletions

View File

@ -235,6 +235,8 @@ void CartCommon::FlushSRAMFile()
int CartCommon::ROMCommandStart(u8* cmd, u8* data, u32 len) int CartCommon::ROMCommandStart(u8* cmd, u8* data, u32 len)
{ {
if (CmdEncMode == 0)
{
switch (cmd[0]) switch (cmd[0])
{ {
case 0x9F: case 0x9F:
@ -254,7 +256,6 @@ int CartCommon::ROMCommandStart(u8* cmd, u8* data, u32 len)
return 0; return 0;
case 0x90: case 0x90:
case 0xB8:
for (u32 pos = 0; pos < len; pos += 4) for (u32 pos = 0; pos < len; pos += 4)
*(u32*)&data[pos] = ChipID; *(u32*)&data[pos] = ChipID;
return 0; return 0;
@ -273,7 +274,10 @@ int CartCommon::ROMCommandStart(u8* cmd, u8* data, u32 len)
return 0; return 0;
default: default:
if (CmdEncMode == 1 || CmdEncMode == 11) return 0;
}
}
else if (CmdEncMode == 1 || CmdEncMode == 11)
{ {
// decrypt the KEY1 command as needed // decrypt the KEY1 command as needed
// (KEY2 commands do not need decrypted because KEY2 is handled entirely by hardware, // (KEY2 commands do not need decrypted because KEY2 is handled entirely by hardware,
@ -320,8 +324,8 @@ int CartCommon::ROMCommandStart(u8* cmd, u8* data, u32 len)
return 0; return 0;
} }
} }
return 0; return 0;
}
} }
void CartCommon::ROMCommandFinish(u8* cmd, u8* data, u32 len) void CartCommon::ROMCommandFinish(u8* cmd, u8* data, u32 len)
@ -509,6 +513,8 @@ void CartRetail::FlushSRAMFile()
int CartRetail::ROMCommandStart(u8* cmd, u8* data, u32 len) int CartRetail::ROMCommandStart(u8* cmd, u8* data, u32 len)
{ {
if (CmdEncMode != 2) return CartCommon::ROMCommandStart(cmd, data, len);
switch (cmd[0]) switch (cmd[0])
{ {
case 0xB7: case 0xB7:
@ -527,6 +533,11 @@ int CartRetail::ROMCommandStart(u8* cmd, u8* data, u32 len)
} }
return 0; return 0;
case 0xB8:
for (u32 pos = 0; pos < len; pos += 4)
*(u32*)&data[pos] = ChipID;
return 0;
default: default:
return CartCommon::ROMCommandStart(cmd, data, len); return CartCommon::ROMCommandStart(cmd, data, len);
} }
@ -871,6 +882,8 @@ int CartRetailNAND::ImportSRAM(const u8* data, u32 length)
int CartRetailNAND::ROMCommandStart(u8* cmd, u8* data, u32 len) int CartRetailNAND::ROMCommandStart(u8* cmd, u8* data, u32 len)
{ {
if (CmdEncMode != 2) return CartCommon::ROMCommandStart(cmd, data, len);
switch (cmd[0]) switch (cmd[0])
{ {
case 0x81: // write data case 0x81: // write data
@ -1005,6 +1018,8 @@ int CartRetailNAND::ROMCommandStart(u8* cmd, u8* data, u32 len)
void CartRetailNAND::ROMCommandFinish(u8* cmd, u8* data, u32 len) void CartRetailNAND::ROMCommandFinish(u8* cmd, u8* data, u32 len)
{ {
if (CmdEncMode != 2) return CartCommon::ROMCommandFinish(cmd, data, len);
switch (cmd[0]) switch (cmd[0])
{ {
case 0x81: // write data case 0x81: // write data
@ -1163,6 +1178,8 @@ void CartHomebrew::DoSavestate(Savestate* file)
int CartHomebrew::ROMCommandStart(u8* cmd, u8* data, u32 len) int CartHomebrew::ROMCommandStart(u8* cmd, u8* data, u32 len)
{ {
if (CmdEncMode != 2) return CartCommon::ROMCommandStart(cmd, data, len);
switch (cmd[0]) switch (cmd[0])
{ {
case 0xB7: case 0xB7:
@ -1204,6 +1221,8 @@ int CartHomebrew::ROMCommandStart(u8* cmd, u8* data, u32 len)
void CartHomebrew::ROMCommandFinish(u8* cmd, u8* data, u32 len) void CartHomebrew::ROMCommandFinish(u8* cmd, u8* data, u32 len)
{ {
if (CmdEncMode != 2) return CartCommon::ROMCommandFinish(cmd, data, len);
// TODO: delayed SD writing? like we have for SRAM // TODO: delayed SD writing? like we have for SRAM
switch (cmd[0]) switch (cmd[0])