diff --git a/Source/Core/Common/Src/MathUtil.h b/Source/Core/Common/Src/MathUtil.h index ecace1fb4a..a6290ff602 100644 --- a/Source/Core/Common/Src/MathUtil.h +++ b/Source/Core/Common/Src/MathUtil.h @@ -161,6 +161,7 @@ void LoadDefaultSSEState(); float MathFloatVectorSum(const std::vector&); #define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1)) +#define ROUND_DOWN(x, a) ((x) & ~((a) - 1)) // Tiny matrix/vector library. diff --git a/Source/Core/Core/Src/Boot/Boot.cpp b/Source/Core/Core/Src/Boot/Boot.cpp index 8d000611c8..be82d217dc 100644 --- a/Source/Core/Core/Src/Boot/Boot.cpp +++ b/Source/Core/Core/Src/Boot/Boot.cpp @@ -19,6 +19,7 @@ #include "Common.h" // Common #include "StringUtil.h" #include "FileUtil.h" +#include "MathUtil.h" #include "../HLE/HLE.h" // Core #include "../PowerPC/PowerPC.h" @@ -68,13 +69,13 @@ void CBoot::Load_FST(bool _bIsWii) u32 fstSize = VolumeHandler::Read32(0x0428) << shift; u32 maxFstSize = VolumeHandler::Read32(0x042c) << shift; - u32 arenaHigh = 0x817FFFF4 - maxFstSize; + u32 arenaHigh = ROUND_DOWN(0x817FFFFF - maxFstSize, 0x20); Memory::Write_U32(arenaHigh, 0x00000034); // load FST VolumeHandler::ReadToPtr(Memory::GetPointer(arenaHigh), fstOffset, fstSize); Memory::Write_U32(arenaHigh, 0x00000038); - Memory::Write_U32(maxFstSize, 0x0000003c); + Memory::Write_U32(maxFstSize, 0x0000003c); } void CBoot::UpdateDebugger_MapLoaded(const char *_gameID) diff --git a/Source/Core/DiscIO/Src/VolumeDirectory.cpp b/Source/Core/DiscIO/Src/VolumeDirectory.cpp index 52e9f4389a..dc76e2e641 100644 --- a/Source/Core/DiscIO/Src/VolumeDirectory.cpp +++ b/Source/Core/DiscIO/Src/VolumeDirectory.cpp @@ -16,6 +16,7 @@ // http://code.google.com/p/dolphin-emu/ #include "Common.h" +#include "MathUtil.h" #include "CommonPaths.h" #include "VolumeDirectory.h" #include "FileBlob.h" @@ -23,17 +24,6 @@ namespace DiscIO { -static const u8 ENTRY_SIZE = 0x0c; -static const u8 FILE_ENTRY = 0; -static const u8 DIRECTORY_ENTRY = 1; -static const u64 DISKHEADER_ADDRESS = 0; -static const u64 DISKHEADERINFO_ADDRESS = 0x440; -static const u64 APPLOADER_ADDRESS = 0x2440; -static const u32 MAX_NAME_LENGTH = 0x3df; -// relocatable -static u64 FST_ADDRESS = 0x440; -static u64 DOL_ADDRESS = 0; - CVolumeDirectory::CVolumeDirectory(const std::string& _rDirectory, bool _bIsWii, const std::string& _rApploader, const std::string& _rDOL) : m_totalNameSize(0) @@ -44,6 +34,8 @@ CVolumeDirectory::CVolumeDirectory(const std::string& _rDirectory, bool _bIsWii, , m_apploader(NULL) , m_DOLSize(0) , m_DOL(NULL) + , FST_ADDRESS(0) + , DOL_ADDRESS(0) { m_rootDirectory = ExtractDirectoryName(_rDirectory); @@ -321,7 +313,7 @@ bool CVolumeDirectory::SetApploader(const std::string& _rApploader) copy(data.begin(), data.end(), m_apploader); // 32byte aligned (plus 0x20 padding) - DOL_ADDRESS = (APPLOADER_ADDRESS + m_apploaderSize + 0x20 + 31) & ~31ULL; + DOL_ADDRESS = ROUND_UP(APPLOADER_ADDRESS + m_apploaderSize + 0x20, 0x20ull); return true; } else @@ -347,7 +339,7 @@ void CVolumeDirectory::SetDOL(const std::string& _rDOL) Write32((u32)(DOL_ADDRESS >> m_addressShift), 0x0420, m_diskHeader); // 32byte aligned (plus 0x20 padding) - FST_ADDRESS = (DOL_ADDRESS + m_DOLSize + 0x20 + 31) & ~31ULL; + FST_ADDRESS = ROUND_UP(DOL_ADDRESS + m_DOLSize + 0x20, 0x20ull); } } @@ -367,8 +359,12 @@ void CVolumeDirectory::BuildFST() m_fstSize = m_fstNameOffset + m_totalNameSize; m_FSTData = new u8[(u32)m_fstSize]; + // if FST hasn't been assigned (ie no apploader/dol setup), set to default + if (FST_ADDRESS == 0) + FST_ADDRESS = APPLOADER_ADDRESS + 0x2000; + // 4 byte aligned start of data on disk - m_dataStartAddress = (FST_ADDRESS + m_fstSize + 3) & ~3; + m_dataStartAddress = ROUND_UP(FST_ADDRESS + m_fstSize, 0x8000ull); u64 curDataAddress = m_dataStartAddress; u32 fstOffset = 0; // offset within FST data @@ -490,7 +486,7 @@ void CVolumeDirectory::WriteEntry(const File::FSTEntry& entry, u32& fstOffset, u m_virtualDisk.insert(make_pair(dataOffset, entry.physicalName)); // 4 byte aligned - dataOffset = (dataOffset + entry.size + 3) & ~3ULL; + dataOffset = ROUND_UP(dataOffset + entry.size, 0x8000ull); } } diff --git a/Source/Core/DiscIO/Src/VolumeDirectory.h b/Source/Core/DiscIO/Src/VolumeDirectory.h index 727974a417..0547be3b8a 100644 --- a/Source/Core/DiscIO/Src/VolumeDirectory.h +++ b/Source/Core/DiscIO/Src/VolumeDirectory.h @@ -141,6 +141,16 @@ private: u64 m_DOLSize; u8* m_DOL; + + static const u8 ENTRY_SIZE = 0x0c; + static const u8 FILE_ENTRY = 0; + static const u8 DIRECTORY_ENTRY = 1; + static const u64 DISKHEADER_ADDRESS = 0; + static const u64 DISKHEADERINFO_ADDRESS = 0x440; + static const u64 APPLOADER_ADDRESS = 0x2440; + static const u32 MAX_NAME_LENGTH = 0x3df; + u64 FST_ADDRESS; + u64 DOL_ADDRESS; }; } // namespace