Boot: Pass volume as argument to functions

Avoids using DVDInterface's global state in the boot process.
This commit is contained in:
JosJuice 2016-09-24 13:24:56 +02:00
parent 807e242d05
commit 5a55957741
4 changed files with 84 additions and 66 deletions

View File

@ -39,24 +39,33 @@
#include "DiscIO/Volume.h"
#include "DiscIO/VolumeCreator.h"
bool CBoot::DVDRead(u64 dvd_offset, u32 output_address, u32 length, bool decrypt)
// Inserts a disc into the emulated disc drive and returns a pointer to it.
// The returned pointer must only be used while we are still booting,
// because DVDThread can do whatever it wants to the disc after that.
static const DiscIO::IVolume* SetDisc(std::unique_ptr<DiscIO::IVolume> volume)
{
const DiscIO::IVolume* pointer = volume.get();
DVDInterface::SetDisc(std::move(volume));
return pointer;
}
bool CBoot::DVDRead(const DiscIO::IVolume& volume, u64 dvd_offset, u32 output_address, u32 length,
bool decrypt)
{
std::vector<u8> buffer(length);
if (!DVDInterface::GetVolume().Read(dvd_offset, length, buffer.data(), decrypt))
if (!volume.Read(dvd_offset, length, buffer.data(), decrypt))
return false;
Memory::CopyToEmu(output_address, buffer.data(), length);
return true;
}
void CBoot::Load_FST(bool is_wii)
void CBoot::Load_FST(bool is_wii, const DiscIO::IVolume* volume)
{
if (!DVDInterface::IsDiscInside())
if (!volume)
return;
const DiscIO::IVolume& volume = DVDInterface::GetVolume();
// copy first 32 bytes of disc to start of Mem 1
DVDRead(/*offset*/ 0, /*address*/ 0, /*length*/ 0x20, false);
DVDRead(*volume, /*offset*/ 0, /*address*/ 0, /*length*/ 0x20, false);
// copy of game id
Memory::Write_U32(Memory::Read_U32(0x0000), 0x3180);
@ -69,15 +78,15 @@ void CBoot::Load_FST(bool is_wii)
u32 fst_size = 0;
u32 max_fst_size = 0;
volume.ReadSwapped(0x0424, &fst_offset, is_wii);
volume.ReadSwapped(0x0428, &fst_size, is_wii);
volume.ReadSwapped(0x042c, &max_fst_size, is_wii);
volume->ReadSwapped(0x0424, &fst_offset, is_wii);
volume->ReadSwapped(0x0428, &fst_size, is_wii);
volume->ReadSwapped(0x042c, &max_fst_size, is_wii);
u32 arena_high = Common::AlignDown(0x817FFFFF - (max_fst_size << shift), 0x20);
Memory::Write_U32(arena_high, 0x00000034);
// load FST
DVDRead(fst_offset << shift, arena_high, fst_size << shift, is_wii);
DVDRead(*volume, fst_offset << shift, arena_high, fst_size << shift, is_wii);
Memory::Write_U32(arena_high, 0x00000038);
Memory::Write_U32(max_fst_size << shift, 0x0000003c);
@ -278,22 +287,22 @@ bool CBoot::BootUp()
{
case SConfig::BOOT_ISO:
{
DVDInterface::SetDisc(DiscIO::CreateVolumeFromFilename(_StartupPara.m_strFilename));
if (!DVDInterface::IsDiscInside())
const DiscIO::IVolume* volume =
SetDisc(DiscIO::CreateVolumeFromFilename(_StartupPara.m_strFilename));
if (!volume)
return false;
const DiscIO::IVolume& pVolume = DVDInterface::GetVolume();
if ((pVolume.GetVolumeType() == DiscIO::Platform::WII_DISC) != _StartupPara.bWii)
if ((volume->GetVolumeType() == DiscIO::Platform::WII_DISC) != _StartupPara.bWii)
{
PanicAlertT("Warning - starting ISO in wrong console mode!");
}
_StartupPara.bWii = pVolume.GetVolumeType() == DiscIO::Platform::WII_DISC;
_StartupPara.bWii = volume->GetVolumeType() == DiscIO::Platform::WII_DISC;
// We HLE the bootrom if requested or if LLEing it fails
if (_StartupPara.bHLE_BS2 || !Load_BS2(_StartupPara.m_strBootROM))
EmulatedBS2(_StartupPara.bWii);
EmulatedBS2(_StartupPara.bWii, volume);
PatchEngine::LoadPatches();
@ -331,29 +340,28 @@ bool CBoot::BootUp()
PanicAlertT("Warning - starting DOL in wrong console mode!");
}
const DiscIO::IVolume* volume = nullptr;
if (!_StartupPara.m_strDVDRoot.empty())
{
NOTICE_LOG(BOOT, "Setting DVDRoot %s", _StartupPara.m_strDVDRoot.c_str());
DVDInterface::SetDisc(DiscIO::CreateVolumeFromDirectory(_StartupPara.m_strDVDRoot, dolWii,
_StartupPara.m_strApploader,
_StartupPara.m_strFilename));
volume = SetDisc(DiscIO::CreateVolumeFromDirectory(_StartupPara.m_strDVDRoot, dolWii,
_StartupPara.m_strApploader,
_StartupPara.m_strFilename));
}
else if (!_StartupPara.m_strDefaultISO.empty())
{
NOTICE_LOG(BOOT, "Loading default ISO %s", _StartupPara.m_strDefaultISO.c_str());
DVDInterface::SetDisc(DiscIO::CreateVolumeFromFilename(_StartupPara.m_strDefaultISO));
volume = SetDisc(DiscIO::CreateVolumeFromFilename(_StartupPara.m_strDefaultISO));
}
if (!EmulatedBS2(dolWii))
if (!EmulatedBS2(dolWii, volume))
{
if (dolWii)
HID4.SBE = 1;
SetupBAT(dolWii);
// Because there is no TMD to get the requested system (IOS) version from,
// we default to IOS58, which is the version used by the Homebrew Channel.
if (dolWii)
SetupWiiMemory(0x000000010000003a);
SetupWiiMemory(volume, 0x000000010000003a);
dolLoader.Load();
PC = dolLoader.GetEntryPoint();
@ -367,17 +375,19 @@ bool CBoot::BootUp()
case SConfig::BOOT_ELF:
{
const DiscIO::IVolume* volume = nullptr;
// load image or create virtual drive from directory
if (!_StartupPara.m_strDVDRoot.empty())
{
NOTICE_LOG(BOOT, "Setting DVDRoot %s", _StartupPara.m_strDVDRoot.c_str());
DVDInterface::SetDisc(
DiscIO::CreateVolumeFromDirectory(_StartupPara.m_strDVDRoot, _StartupPara.bWii));
volume =
SetDisc(DiscIO::CreateVolumeFromDirectory(_StartupPara.m_strDVDRoot, _StartupPara.bWii));
}
else if (!_StartupPara.m_strDefaultISO.empty())
{
NOTICE_LOG(BOOT, "Loading default ISO %s", _StartupPara.m_strDefaultISO.c_str());
DVDInterface::SetDisc(DiscIO::CreateVolumeFromFilename(_StartupPara.m_strDefaultISO));
volume = SetDisc(DiscIO::CreateVolumeFromFilename(_StartupPara.m_strDefaultISO));
}
// Poor man's bootup
@ -385,14 +395,14 @@ bool CBoot::BootUp()
{
// Because there is no TMD to get the requested system (IOS) version from,
// we default to IOS58, which is the version used by the Homebrew Channel.
SetupWiiMemory(0x000000010000003a);
SetupWiiMemory(volume, 0x000000010000003a);
}
else
{
EmulatedBS2_GC(true);
EmulatedBS2_GC(volume, true);
}
Load_FST(_StartupPara.bWii);
Load_FST(_StartupPara.bWii, volume);
if (!Boot_ELF(_StartupPara.m_strFilename))
return false;
@ -412,9 +422,9 @@ bool CBoot::BootUp()
// load default image or create virtual drive from directory
if (!_StartupPara.m_strDVDRoot.empty())
DVDInterface::SetDisc(DiscIO::CreateVolumeFromDirectory(_StartupPara.m_strDVDRoot, true));
SetDisc(DiscIO::CreateVolumeFromDirectory(_StartupPara.m_strDVDRoot, true));
else if (!_StartupPara.m_strDefaultISO.empty())
DVDInterface::SetDisc(DiscIO::CreateVolumeFromFilename(_StartupPara.m_strDefaultISO));
SetDisc(DiscIO::CreateVolumeFromFilename(_StartupPara.m_strDefaultISO));
break;

View File

@ -9,6 +9,11 @@
#include "Common/CommonTypes.h"
namespace DiscIO
{
class IVolume;
}
struct RegionSetting
{
const std::string area;
@ -40,7 +45,8 @@ public:
static bool LoadMapFromFilename();
private:
static bool DVDRead(u64 dvd_offset, u32 output_address, u32 length, bool decrypt);
static bool DVDRead(const DiscIO::IVolume& volume, u64 dvd_offset, u32 output_address, u32 length,
bool decrypt);
static void RunFunction(u32 address);
static void UpdateDebugger_MapLoaded();
@ -49,12 +55,12 @@ private:
static bool Boot_WiiWAD(const std::string& filename);
static void SetupBAT(bool is_wii);
static bool RunApploader(bool is_wii);
static bool EmulatedBS2_GC(bool skip_app_loader = false);
static bool EmulatedBS2_Wii();
static bool EmulatedBS2(bool is_wii);
static bool RunApploader(bool is_wii, const DiscIO::IVolume& volume);
static bool EmulatedBS2_GC(const DiscIO::IVolume* volume, bool skip_app_loader = false);
static bool EmulatedBS2_Wii(const DiscIO::IVolume* volume);
static bool EmulatedBS2(bool is_wii, const DiscIO::IVolume* volume);
static bool Load_BS2(const std::string& boot_rom_filename);
static void Load_FST(bool is_wii);
static void Load_FST(bool is_wii, const DiscIO::IVolume* volume);
static bool SetupWiiMemory(u64 ios_title_id);
static bool SetupWiiMemory(const DiscIO::IVolume* volume, u64 ios_title_id);
};

View File

@ -18,7 +18,6 @@
#include "Core/ConfigManager.h"
#include "Core/Core.h"
#include "Core/HLE/HLE.h"
#include "Core/HW/DVD/DVDInterface.h"
#include "Core/HW/EXI/EXI_DeviceIPL.h"
#include "Core/HW/Memmap.h"
#include "Core/IOS/ES/ES.h"
@ -78,11 +77,10 @@ void CBoot::SetupBAT(bool is_wii)
PowerPC::IBATUpdated();
}
bool CBoot::RunApploader(bool is_wii)
bool CBoot::RunApploader(bool is_wii, const DiscIO::IVolume& volume)
{
// 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.
const DiscIO::IVolume& volume = DVDInterface::GetVolume();
const u32 apploader_offset = 0x2440;
u32 apploader_entry = 0;
u32 apploader_size = 0;
@ -95,7 +93,7 @@ bool CBoot::RunApploader(bool is_wii)
INFO_LOG(BOOT, "Invalid apploader. Your disc image is probably corrupted.");
return false;
}
DVDRead(apploader_offset + 0x20, 0x01200000, apploader_size + apploader_trailer, is_wii);
DVDRead(volume, apploader_offset + 0x20, 0x01200000, apploader_size + apploader_trailer, is_wii);
// TODO - Make Apploader(or just RunFunction()) debuggable!!!
@ -134,7 +132,7 @@ bool CBoot::RunApploader(bool is_wii)
INFO_LOG(MASTER_LOG, "DVDRead: offset: %08x memOffset: %08x length: %i", iDVDOffset,
iRamAddress, iLength);
DVDRead(iDVDOffset, iRamAddress, iLength, is_wii);
DVDRead(volume, iDVDOffset, iRamAddress, iLength, is_wii);
} while (PowerPC::ppcState.gpr[3] != 0x00);
@ -153,7 +151,7 @@ bool CBoot::RunApploader(bool is_wii)
// GameCube Bootstrap 2 HLE:
// copy the apploader to 0x81200000
// execute the apploader, function by function, using the above utility.
bool CBoot::EmulatedBS2_GC(bool skip_app_loader)
bool CBoot::EmulatedBS2_GC(const DiscIO::IVolume* volume, bool skip_app_loader)
{
INFO_LOG(BOOT, "Faking GC BS2...");
@ -164,8 +162,8 @@ bool CBoot::EmulatedBS2_GC(bool skip_app_loader)
// to 0x80000000 according to YAGCD 4.2.
// It's possible to boot DOL and ELF files without a disc inserted
if (DVDInterface::IsDiscInside())
DVDRead(/*offset*/ 0x00000000, /*address*/ 0x00000000, 0x20, false); // write disc info
if (volume)
DVDRead(*volume, /*offset*/ 0x00000000, /*address*/ 0x00000000, 0x20, false);
// Booted from bootrom. 0xE5207C22 = booted from jtag
PowerPC::HostWrite_U32(0x0D15EA5E, 0x80000020);
@ -196,7 +194,7 @@ bool CBoot::EmulatedBS2_GC(bool skip_app_loader)
// HIO checks this
// PowerPC::HostWrite_U16(0x8200, 0x000030e6); // Console type
if (!DVDInterface::IsDiscInside())
if (!volume)
return false;
// Setup pointers like real BS2 does
@ -211,10 +209,10 @@ bool CBoot::EmulatedBS2_GC(bool skip_app_loader)
if (skip_app_loader)
return false;
return RunApploader(/*is_wii*/ false);
return RunApploader(/*is_wii*/ false, *volume);
}
bool CBoot::SetupWiiMemory(u64 ios_title_id)
bool CBoot::SetupWiiMemory(const DiscIO::IVolume* volume, u64 ios_title_id)
{
static const std::map<DiscIO::Region, const RegionSetting> region_settings = {
{DiscIO::Region::NTSC_J, {"JPN", "NTSC", "JP", "LJ"}},
@ -281,8 +279,8 @@ bool CBoot::SetupWiiMemory(u64 ios_title_id)
*/
// When booting a WAD or the system menu, there will probably not be a disc inserted
if (DVDInterface::IsDiscInside())
DVDRead(0x00000000, 0x00000000, 0x20, false); // Game Code
if (volume)
DVDRead(*volume, 0x00000000, 0x00000000, 0x20, false); // Game Code
Memory::Write_U32(0x0D15EA5E, 0x00000020); // Another magic word
Memory::Write_U32(0x00000001, 0x00000024); // Unknown
@ -328,25 +326,25 @@ bool CBoot::SetupWiiMemory(u64 ios_title_id)
// Wii Bootstrap 2 HLE:
// copy the apploader to 0x81200000
// execute the apploader
bool CBoot::EmulatedBS2_Wii()
bool CBoot::EmulatedBS2_Wii(const DiscIO::IVolume* volume)
{
INFO_LOG(BOOT, "Faking Wii BS2...");
if (!DVDInterface::IsDiscInside())
if (!volume)
return false;
if (DVDInterface::GetVolume().GetVolumeType() != DiscIO::Platform::WII_DISC)
if (volume->GetVolumeType() != DiscIO::Platform::WII_DISC)
return false;
const IOS::ES::TMDReader tmd = DVDInterface::GetVolume().GetTMD();
const IOS::ES::TMDReader tmd = volume->GetTMD();
if (!SetupWiiMemory(tmd.GetIOSId()))
if (!SetupWiiMemory(volume, tmd.GetIOSId()))
return false;
// This is some kind of consistency check that is compared to the 0x00
// values as the game boots. This location keeps 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.
DVDRead(0, 0x3180, 4, true);
DVDRead(*volume, 0, 0x3180, 4, true);
SetupBAT(/*is_wii*/ true);
@ -356,16 +354,20 @@ bool CBoot::EmulatedBS2_Wii()
PowerPC::ppcState.gpr[1] = 0x816ffff0; // StackPointer
if (!RunApploader(/*is_wii*/ true))
if (!RunApploader(/*is_wii*/ true, *volume))
return false;
IOS::HLE::Device::ES::DIVerify(tmd, DVDInterface::GetVolume().GetTicket());
// Warning: This call will set incorrect running game metadata if our volume parameter
// doesn't point to the same disc as the one that's inserted in the emulated disc drive!
IOS::HLE::Device::ES::DIVerify(tmd, volume->GetTicket());
return true;
}
// Returns true if apploader has run successfully
bool CBoot::EmulatedBS2(bool is_wii)
// Returns true if apploader has run successfully.
// If is_wii is true and volume is not nullptr, the disc that volume
// point to must currently be inserted into the emulated disc drive.
bool CBoot::EmulatedBS2(bool is_wii, const DiscIO::IVolume* volume)
{
return is_wii ? EmulatedBS2_Wii() : EmulatedBS2_GC();
return is_wii ? EmulatedBS2_Wii(volume) : EmulatedBS2_GC(volume);
}

View File

@ -85,7 +85,7 @@ bool CBoot::Boot_WiiWAD(const std::string& _pFilename)
IOS::HLE::CreateVirtualFATFilesystem();
// setup Wii memory
if (!SetupWiiMemory(ContentLoader.GetTMD().GetIOSId()))
if (!SetupWiiMemory(nullptr, ContentLoader.GetTMD().GetIOSId()))
return false;
IOS::HLE::Device::ES::LoadWAD(_pFilename);