Skip to content

Commit

Permalink
Merge pull request #1776 from JosJuice/cleanrip-wii-discs
Browse files Browse the repository at this point in the history
Fix CleanRip reading Wii discs
  • Loading branch information
skidau committed Jan 2, 2015
2 parents 598dc03 + bb93336 commit 3738d27
Show file tree
Hide file tree
Showing 19 changed files with 175 additions and 192 deletions.
10 changes: 5 additions & 5 deletions Source/Core/Core/Boot/Boot.cpp
Expand Up @@ -39,7 +39,7 @@ void CBoot::Load_FST(bool _bIsWii)
return;

// copy first 20 bytes of disc to start of Mem 1
VolumeHandler::ReadToPtr(Memory::GetPointer(0x80000000), 0, 0x20);
VolumeHandler::ReadToPtr(Memory::GetPointer(0x80000000), 0, 0x20, false);

// copy of game id
Memory::Write_U32(Memory::Read_U32(0x80000000), 0x80003180);
Expand All @@ -48,15 +48,15 @@ void CBoot::Load_FST(bool _bIsWii)
if (_bIsWii)
shift = 2;

u32 fstOffset = VolumeHandler::Read32(0x0424) << shift;
u32 fstSize = VolumeHandler::Read32(0x0428) << shift;
u32 maxFstSize = VolumeHandler::Read32(0x042c) << shift;
u32 fstOffset = VolumeHandler::Read32(0x0424, _bIsWii) << shift;
u32 fstSize = VolumeHandler::Read32(0x0428, _bIsWii) << shift;
u32 maxFstSize = VolumeHandler::Read32(0x042c, _bIsWii) << shift;

u32 arenaHigh = ROUND_DOWN(0x817FFFFF - maxFstSize, 0x20);
Memory::Write_U32(arenaHigh, 0x00000034);

// load FST
VolumeHandler::ReadToPtr(Memory::GetPointer(arenaHigh), fstOffset, fstSize);
VolumeHandler::ReadToPtr(Memory::GetPointer(arenaHigh), fstOffset, fstSize, _bIsWii);
Memory::Write_U32(arenaHigh, 0x00000038);
Memory::Write_U32(maxFstSize, 0x0000003c);
}
Expand Down
102 changes: 51 additions & 51 deletions Source/Core/Core/Boot/Boot_BS2Emu.cpp
Expand Up @@ -56,7 +56,7 @@ bool CBoot::EmulatedBS2_GC()
// Write necessary values
// Here we write values to memory that the apploader does not take care of. Game info goes
// to 0x80000000 according to YAGCD 4.2.
DVDInterface::DVDRead(0x00000000, 0x80000000, 0x20); // write disc info
DVDInterface::DVDRead(0x00000000, 0x80000000, 0x20, false); // write disc info

Memory::Write_U32(0x0D15EA5E, 0x80000020); // Booted from bootrom. 0xE5207C22 = booted from jtag
Memory::Write_U32(Memory::REALRAM_SIZE, 0x80000028); // Physical Memory Size (24MB on retail)
Expand Down Expand Up @@ -84,14 +84,14 @@ bool CBoot::EmulatedBS2_GC()
// Load Apploader to Memory - The apploader is hardcoded to begin at 0x2440 on the disc,
// but the size can differ between discs. Compare with YAGCD chap 13.
u32 iAppLoaderOffset = 0x2440;
u32 iAppLoaderEntry = VolumeHandler::Read32(iAppLoaderOffset + 0x10);
u32 iAppLoaderSize = VolumeHandler::Read32(iAppLoaderOffset + 0x14) + VolumeHandler::Read32(iAppLoaderOffset + 0x18);
u32 iAppLoaderEntry = VolumeHandler::Read32(iAppLoaderOffset + 0x10, false);
u32 iAppLoaderSize = VolumeHandler::Read32(iAppLoaderOffset + 0x14, false) + VolumeHandler::Read32(iAppLoaderOffset + 0x18, false);
if ((iAppLoaderEntry == (u32)-1) || (iAppLoaderSize == (u32)-1))
{
INFO_LOG(BOOT, "GC BS2: Not running apploader!");
return false;
}
VolumeHandler::ReadToPtr(Memory::GetPointer(0x81200000), iAppLoaderOffset + 0x20, iAppLoaderSize);
VolumeHandler::ReadToPtr(Memory::GetPointer(0x81200000), iAppLoaderOffset + 0x20, iAppLoaderSize, false);

// Setup pointers like real BS2 does
if (SConfig::GetInstance().m_LocalCoreStartupParameter.bNTSC)
Expand Down Expand Up @@ -142,7 +142,7 @@ bool CBoot::EmulatedBS2_GC()
u32 iDVDOffset = Memory::ReadUnchecked_U32(0x8130000c);

INFO_LOG(MASTER_LOG, "DVDRead: offset: %08x memOffset: %08x length: %i", iDVDOffset, iRamAddress, iLength);
DVDInterface::DVDRead(iDVDOffset, iRamAddress, iLength);
DVDInterface::DVDRead(iDVDOffset, iRamAddress, iLength, false);

} while (PowerPC::ppcState.gpr[3] != 0x00);

Expand Down Expand Up @@ -241,50 +241,50 @@ bool CBoot::SetupWiiMemory(IVolume::ECountry country)
0x80000060 Copyright code
*/

DVDInterface::DVDRead(0x00000000, 0x00000000, 0x20); // Game Code
Memory::Write_U32(0x0D15EA5E, 0x00000020); // Another magic word
Memory::Write_U32(0x00000001, 0x00000024); // Unknown
Memory::Write_U32(Memory::REALRAM_SIZE, 0x00000028); // MEM1 size 24MB
Memory::Write_U32(0x00000023, 0x0000002c); // Production Board Model
Memory::Write_U32(0x00000000, 0x00000030); // Init
Memory::Write_U32(0x817FEC60, 0x00000034); // Init
DVDInterface::DVDRead(0x00000000, 0x00000000, 0x20, false); // Game Code
Memory::Write_U32(0x0D15EA5E, 0x00000020); // Another magic word
Memory::Write_U32(0x00000001, 0x00000024); // Unknown
Memory::Write_U32(Memory::REALRAM_SIZE, 0x00000028); // MEM1 size 24MB
Memory::Write_U32(0x00000023, 0x0000002c); // Production Board Model
Memory::Write_U32(0x00000000, 0x00000030); // Init
Memory::Write_U32(0x817FEC60, 0x00000034); // Init
// 38, 3C should get start, size of FST through apploader
Memory::Write_U32(0x38a00040, 0x00000060); // Exception init
Memory::Write_U32(0x8008f7b8, 0x000000e4); // Thread Init
Memory::Write_U32(Memory::REALRAM_SIZE, 0x000000f0); // "Simulated memory size" (debug mode?)
Memory::Write_U32(0x8179b500, 0x000000f4); // __start
Memory::Write_U32(0x0e7be2c0, 0x000000f8); // Bus speed
Memory::Write_U32(0x2B73A840, 0x000000fc); // CPU speed
Memory::Write_U16(0x0000, 0x000030e6); // Console type
Memory::Write_U32(0x00000000, 0x000030c0); // EXI
Memory::Write_U32(0x00000000, 0x000030c4); // EXI
Memory::Write_U32(0x00000000, 0x000030dc); // Time
Memory::Write_U32(0x00000000, 0x000030d8); // Time
Memory::Write_U16(0x8201, 0x000030e6); // Dev console / debug capable
Memory::Write_U32(0x00000000, 0x000030f0); // Apploader
Memory::Write_U32(0x01800000, 0x00003100); // BAT
Memory::Write_U32(0x01800000, 0x00003104); // BAT
Memory::Write_U32(0x00000000, 0x0000310c); // Init
Memory::Write_U32(0x8179d500, 0x00003110); // Init
Memory::Write_U32(0x04000000, 0x00003118); // Unknown
Memory::Write_U32(0x04000000, 0x0000311c); // BAT
Memory::Write_U32(0x93400000, 0x00003120); // BAT
Memory::Write_U32(0x90000800, 0x00003124); // Init - MEM2 low
Memory::Write_U32(0x93ae0000, 0x00003128); // Init - MEM2 high
Memory::Write_U32(0x93ae0000, 0x00003130); // IOS MEM2 low
Memory::Write_U32(0x93b00000, 0x00003134); // IOS MEM2 high
Memory::Write_U32(0x00000012, 0x00003138); // Console type
Memory::Write_U32(0x38a00040, 0x00000060); // Exception init
Memory::Write_U32(0x8008f7b8, 0x000000e4); // Thread Init
Memory::Write_U32(Memory::REALRAM_SIZE, 0x000000f0); // "Simulated memory size" (debug mode?)
Memory::Write_U32(0x8179b500, 0x000000f4); // __start
Memory::Write_U32(0x0e7be2c0, 0x000000f8); // Bus speed
Memory::Write_U32(0x2B73A840, 0x000000fc); // CPU speed
Memory::Write_U16(0x0000, 0x000030e6); // Console type
Memory::Write_U32(0x00000000, 0x000030c0); // EXI
Memory::Write_U32(0x00000000, 0x000030c4); // EXI
Memory::Write_U32(0x00000000, 0x000030dc); // Time
Memory::Write_U32(0x00000000, 0x000030d8); // Time
Memory::Write_U16(0x8201, 0x000030e6); // Dev console / debug capable
Memory::Write_U32(0x00000000, 0x000030f0); // Apploader
Memory::Write_U32(0x01800000, 0x00003100); // BAT
Memory::Write_U32(0x01800000, 0x00003104); // BAT
Memory::Write_U32(0x00000000, 0x0000310c); // Init
Memory::Write_U32(0x8179d500, 0x00003110); // Init
Memory::Write_U32(0x04000000, 0x00003118); // Unknown
Memory::Write_U32(0x04000000, 0x0000311c); // BAT
Memory::Write_U32(0x93400000, 0x00003120); // BAT
Memory::Write_U32(0x90000800, 0x00003124); // Init - MEM2 low
Memory::Write_U32(0x93ae0000, 0x00003128); // Init - MEM2 high
Memory::Write_U32(0x93ae0000, 0x00003130); // IOS MEM2 low
Memory::Write_U32(0x93b00000, 0x00003134); // IOS MEM2 high
Memory::Write_U32(0x00000012, 0x00003138); // Console type
// 40 is copied from 88 after running apploader
Memory::Write_U32(0x00090204, 0x00003140); // IOS revision (IOS9, v2.4)
Memory::Write_U32(0x00062507, 0x00003144); // IOS date in USA format (June 25, 2007)
Memory::Write_U16(0x0113, 0x0000315e); // Apploader
Memory::Write_U32(0x0000FF16, 0x00003158); // DDR ram vendor code
Memory::Write_U32(0x00000000, 0x00003160); // Init semaphore (sysmenu waits for this to clear)
Memory::Write_U32(0x00090204, 0x00003188); // Expected IOS revision
Memory::Write_U32(0x00090204, 0x00003140); // IOS revision (IOS9, v2.4)
Memory::Write_U32(0x00062507, 0x00003144); // IOS date in USA format (June 25, 2007)
Memory::Write_U16(0x0113, 0x0000315e); // Apploader
Memory::Write_U32(0x0000FF16, 0x00003158); // DDR ram vendor code
Memory::Write_U32(0x00000000, 0x00003160); // Init semaphore (sysmenu waits for this to clear)
Memory::Write_U32(0x00090204, 0x00003188); // Expected IOS revision

Memory::Write_U8(0x80, 0x0000315c); // OSInit
Memory::Write_U16(0x0000, 0x000030e0); // PADInit
Memory::Write_U32(0x80000000, 0x00003184); // GameID Address
Memory::Write_U8(0x80, 0x0000315c); // OSInit
Memory::Write_U16(0x0000, 0x000030e0); // PADInit
Memory::Write_U32(0x80000000, 0x00003184); // GameID Address

// Fake the VI Init of the IPL
Memory::Write_U32(SConfig::GetInstance().m_LocalCoreStartupParameter.bNTSC ? 0 : 1, 0x000000CC);
Expand Down Expand Up @@ -316,7 +316,7 @@ bool CBoot::EmulatedBS2_Wii()
// values as the game boots. This location keep the 4 byte ID for as long
// as the game is running. The 6 byte ID at 0x00 is overwritten sometime
// after this check during booting.
VolumeHandler::ReadToPtr(Memory::GetPointer(0x3180), 0, 4);
VolumeHandler::ReadToPtr(Memory::GetPointer(0x3180), 0, 4, true);

// Execute the apploader
bool apploaderRan = false;
Expand Down Expand Up @@ -349,14 +349,14 @@ bool CBoot::EmulatedBS2_Wii()
u32 iAppLoaderOffset = 0x2440; // 0x1c40;

// Load Apploader to Memory
u32 iAppLoaderEntry = VolumeHandler::Read32(iAppLoaderOffset + 0x10);
u32 iAppLoaderSize = VolumeHandler::Read32(iAppLoaderOffset + 0x14);
u32 iAppLoaderEntry = VolumeHandler::Read32(iAppLoaderOffset + 0x10, true);
u32 iAppLoaderSize = VolumeHandler::Read32(iAppLoaderOffset + 0x14, true);
if ((iAppLoaderEntry == (u32)-1) || (iAppLoaderSize == (u32)-1))
{
ERROR_LOG(BOOT, "Invalid apploader. Probably your image is corrupted.");
return false;
}
VolumeHandler::ReadToPtr(Memory::GetPointer(0x81200000), iAppLoaderOffset + 0x20, iAppLoaderSize);
VolumeHandler::ReadToPtr(Memory::GetPointer(0x81200000), iAppLoaderOffset + 0x20, iAppLoaderSize, true);

//call iAppLoaderEntry
DEBUG_LOG(BOOT, "Call iAppLoaderEntry");
Expand Down Expand Up @@ -394,7 +394,7 @@ bool CBoot::EmulatedBS2_Wii()
u32 iDVDOffset = Memory::ReadUnchecked_U32(0x8130000c) << 2;

INFO_LOG(BOOT, "DVDRead: offset: %08x memOffset: %08x length: %i", iDVDOffset, iRamAddress, iLength);
DVDInterface::DVDRead(iDVDOffset, iRamAddress, iLength);
DVDInterface::DVDRead(iDVDOffset, iRamAddress, iLength, true);
} while (PowerPC::ppcState.gpr[3] != 0x00);

// iAppLoaderClose
Expand Down
27 changes: 12 additions & 15 deletions Source/Core/Core/HW/DVDInterface.cpp
Expand Up @@ -258,7 +258,7 @@ void GenerateDIInterrupt(DIInterruptType _DVDInterrupt);

void WriteImmediate(u32 value, u32 output_address, bool write_to_DIIMMBUF);
DVDCommandResult ExecuteReadCommand(u64 DVD_offset, u32 output_address,
u32 DVD_length, u32 output_length, bool raw = false);
u32 DVD_length, u32 output_length, bool decrypt);

u64 SimulateDiscReadTime(u64 offset, u32 length);
s64 CalculateRawDiscReadTime(u64 offset, s64 length);
Expand Down Expand Up @@ -328,7 +328,7 @@ static u32 ProcessDTKSamples(short *tempPCM, u32 num_samples)

u8 tempADPCM[NGCADPCM::ONE_BLOCK_SIZE];
// TODO: What if we can't read from AudioPos?
VolumeHandler::ReadToPtr(tempADPCM, AudioPos, sizeof(tempADPCM));
VolumeHandler::ReadToPtr(tempADPCM, AudioPos, sizeof(tempADPCM), false);
AudioPos += sizeof(tempADPCM);
NGCADPCM::DecodeBlock(tempPCM + samples_processed * 2, tempADPCM);
samples_processed += NGCADPCM::SAMPLES_PER_BLOCK;
Expand Down Expand Up @@ -467,12 +467,9 @@ void SetLidOpen(bool _bOpen)
GenerateDIInterrupt(INT_CVRINT);
}

bool DVDRead(u64 _iDVDOffset, u32 _iRamAddress, u32 _iLength, bool raw)
bool DVDRead(u64 _iDVDOffset, u32 _iRamAddress, u32 _iLength, bool decrypt)
{
if (raw)
return VolumeHandler::RAWReadToPtr(Memory::GetPointer(_iRamAddress), _iDVDOffset, _iLength);
else
return VolumeHandler::ReadToPtr(Memory::GetPointer(_iRamAddress), _iDVDOffset, _iLength);
return VolumeHandler::ReadToPtr(Memory::GetPointer(_iRamAddress), _iDVDOffset, _iLength, decrypt);
}

void RegisterMMIO(MMIO::Mapping* mmio, u32 base)
Expand Down Expand Up @@ -615,7 +612,7 @@ void WriteImmediate(u32 value, u32 output_address, bool write_to_DIIMMBUF)
}

DVDCommandResult ExecuteReadCommand(u64 DVD_offset, u32 output_address,
u32 DVD_length, u32 output_length, bool raw)
u32 DVD_length, u32 output_length, bool decrypt)
{
if (DVD_length > output_length)
{
Expand All @@ -633,7 +630,7 @@ DVDCommandResult ExecuteReadCommand(u64 DVD_offset, u32 output_address,
return result;
}

if (!DVDRead(DVD_offset, output_address, DVD_length, raw))
if (!DVDRead(DVD_offset, output_address, DVD_length, decrypt))
PanicAlertT("Can't read from DVD_Plugin - DVD-Interface: Fatal Error");

result.interrupt_type = INT_TCINT;
Expand Down Expand Up @@ -690,13 +687,13 @@ DVDCommandResult ExecuteCommand(u32 command_0, u32 command_1, u32 command_2,
// Only seems to be used from WII_IPC, not through direct access
case DVDLowReadDiskID:
INFO_LOG(DVDINTERFACE, "DVDLowReadDiskID");
result = ExecuteReadCommand(0, output_address, 0x20, output_length, true);
result = ExecuteReadCommand(0, output_address, 0x20, output_length, false);
break;

// Only seems to be used from WII_IPC, not through direct access
// Only used from WII_IPC. This is the only read command that decrypts data
case DVDLowRead:
INFO_LOG(DVDINTERFACE, "DVDLowRead: DVDAddr: 0x%09" PRIx64 ", Size: 0x%x", (u64)command_2 << 2, command_1);
result = ExecuteReadCommand((u64)command_2 << 2, output_address, command_1, output_length);
result = ExecuteReadCommand((u64)command_2 << 2, output_address, command_1, output_length, true);
break;

// Probably only used by Wii
Expand Down Expand Up @@ -780,7 +777,7 @@ DVDCommandResult ExecuteCommand(u32 command_0, u32 command_1, u32 command_2,
(command_2 > 0x7ed40000 && command_2 < 0x7ed40008) ||
(((command_2 + command_1) > 0x7ed40000) && (command_2 + command_1) < 0x7ed40008)))
{
result = ExecuteReadCommand((u64)command_2 << 2, output_address, command_1, output_length, true);
result = ExecuteReadCommand((u64)command_2 << 2, output_address, command_1, output_length, false);
}
else
{
Expand Down Expand Up @@ -876,13 +873,13 @@ DVDCommandResult ExecuteCommand(u32 command_0, u32 command_1, u32 command_2,
}
}

result = ExecuteReadCommand(iDVDOffset, output_address, command_2, output_length);
result = ExecuteReadCommand(iDVDOffset, output_address, command_2, output_length, false);
}
break;

case 0x40: // Read DiscID
INFO_LOG(DVDINTERFACE, "Read DiscID %08x", Memory::Read_U32(output_address));
result = ExecuteReadCommand(0, output_address, 0x20, output_length);
result = ExecuteReadCommand(0, output_address, 0x20, output_length, false);
break;

default:
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/HW/DVDInterface.h
Expand Up @@ -103,7 +103,7 @@ bool IsDiscInside();
void ChangeDisc(const std::string& fileName);

// DVD Access Functions
bool DVDRead(u64 _iDVDOffset, u32 _iRamAddress, u32 _iLength, bool raw = false);
bool DVDRead(u64 _iDVDOffset, u32 _iRamAddress, u32 _iLength, bool decrypt);
extern bool g_bStream;
DVDCommandResult ExecuteCommand(u32 command_0, u32 command_1, u32 command_2,
u32 output_address, u32 output_length, bool write_to_DIIMMBUF);
Expand Down
16 changes: 4 additions & 12 deletions Source/Core/Core/VolumeHandler.cpp
Expand Up @@ -53,29 +53,21 @@ void SetVolumeDirectory(const std::string& _rFullPath, bool _bIsWii, const std::
g_pVolume = DiscIO::CreateVolumeFromDirectory(_rFullPath, _bIsWii, _rApploader, _rDOL);
}

u32 Read32(u64 _Offset)
u32 Read32(u64 _Offset, bool decrypt)
{
if (g_pVolume != nullptr)
{
u32 Temp;
g_pVolume->Read(_Offset, 4, (u8*)&Temp);
g_pVolume->Read(_Offset, 4, (u8*)&Temp, decrypt);
return Common::swap32(Temp);
}
return 0;
}

bool ReadToPtr(u8* ptr, u64 _dwOffset, u64 _dwLength)
bool ReadToPtr(u8* ptr, u64 _dwOffset, u64 _dwLength, bool decrypt)
{
if (g_pVolume != nullptr && ptr)
return g_pVolume->Read(_dwOffset, _dwLength, ptr);

return false;
}

bool RAWReadToPtr(u8* ptr, u64 _dwOffset, u64 _dwLength)
{
if (g_pVolume != nullptr && ptr)
return g_pVolume->RAWRead(_dwOffset, _dwLength, ptr);
return g_pVolume->Read(_dwOffset, _dwLength, ptr, decrypt);

return false;
}
Expand Down
7 changes: 4 additions & 3 deletions Source/Core/Core/VolumeHandler.h
Expand Up @@ -19,9 +19,10 @@ namespace VolumeHandler
bool SetVolumeName(const std::string& _rFullPath);
void SetVolumeDirectory(const std::string& _rFullPath, bool _bIsWii, const std::string& _rApploader = "", const std::string& _rDOL = "");

u32 Read32(u64 _Offset);
bool ReadToPtr(u8* ptr, u64 _dwOffset, u64 _dwLength);
bool RAWReadToPtr(u8* ptr, u64 _dwOffset, u64 _dwLength);
// decrypt parameter must be false if not reading a Wii disc
u32 Read32(u64 _Offset, bool decrypt);
// decrypt parameter must be false if not reading a Wii disc
bool ReadToPtr(u8* ptr, u64 _dwOffset, u64 _dwLength, bool decrypt);

bool IsValid();
bool IsWii();
Expand Down

0 comments on commit 3738d27

Please sign in to comment.