2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
|
|
|
#include <map>
|
2014-08-26 03:40:05 +00:00
|
|
|
#include <memory>
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-11-27 10:56:22 +00:00
|
|
|
#include "Common/Align.h"
|
2015-09-26 20:39:47 +00:00
|
|
|
#include "Common/Assert.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/CommonPaths.h"
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-21 00:47:53 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2015-09-26 20:39:47 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2015-09-27 12:01:12 +00:00
|
|
|
#include "DiscIO/Blob.h"
|
2016-07-06 18:33:05 +00:00
|
|
|
#include "DiscIO/Enums.h"
|
2014-12-27 19:13:31 +00:00
|
|
|
#include "DiscIO/FileMonitor.h"
|
2014-02-21 00:47:53 +00:00
|
|
|
#include "DiscIO/Volume.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "DiscIO/VolumeDirectory.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
2016-12-25 21:57:14 +00:00
|
|
|
static u32 ComputeNameSize(const File::FSTEntry& parentEntry);
|
|
|
|
|
2015-11-17 09:09:54 +00:00
|
|
|
const size_t CVolumeDirectory::MAX_NAME_LENGTH;
|
|
|
|
const size_t CVolumeDirectory::MAX_ID_LENGTH;
|
|
|
|
|
2009-12-14 05:27:29 +00:00
|
|
|
CVolumeDirectory::CVolumeDirectory(const std::string& _rDirectory, bool _bIsWii,
|
2016-06-24 08:43:46 +00:00
|
|
|
const std::string& _rApploader, const std::string& _rDOL)
|
2016-12-25 21:57:14 +00:00
|
|
|
: m_dataStartAddress(-1), m_diskHeader(DISKHEADERINFO_ADDRESS),
|
2016-06-24 08:43:46 +00:00
|
|
|
m_diskHeaderInfo(std::make_unique<SDiskHeaderInfo>()), m_fst_address(0), m_dol_address(0)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_rootDirectory = ExtractDirectoryName(_rDirectory);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// create the default disk header
|
2016-10-29 12:42:43 +00:00
|
|
|
SetGameID("AGBJ01");
|
2016-06-24 08:43:46 +00:00
|
|
|
SetName("Default name");
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (_bIsWii)
|
|
|
|
SetDiskTypeWii();
|
|
|
|
else
|
|
|
|
SetDiskTypeGC();
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Don't load the DOL if we don't have an apploader
|
|
|
|
if (SetApploader(_rApploader))
|
|
|
|
SetDOL(_rDOL);
|
2009-12-10 09:16:10 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
BuildFST();
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CVolumeDirectory::~CVolumeDirectory()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CVolumeDirectory::IsValidDirectory(const std::string& _rDirectory)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return File::IsDirectory(ExtractDirectoryName(_rDirectory));
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2014-12-28 09:35:48 +00:00
|
|
|
bool CVolumeDirectory::Read(u64 _Offset, u64 _Length, u8* _pBuffer, bool decrypt) const
|
2009-02-22 13:59:06 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!decrypt && (_Offset + _Length >= 0x400) && m_is_wii)
|
|
|
|
{
|
|
|
|
// Fully supporting this would require re-encrypting every file that's read.
|
2016-06-25 16:07:10 +00:00
|
|
|
// Only supporting the areas that IOS allows software to read could be more feasible.
|
|
|
|
// Currently, only the header (up to 0x400) is supported, though we're cheating a bit
|
|
|
|
// with it by reading the header inside the current partition instead. Supporting the
|
|
|
|
// header is enough for booting games, but not for running things like the Disc Channel.
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (decrypt && !m_is_wii)
|
|
|
|
PanicAlertT("Tried to decrypt data from a non-Wii volume");
|
|
|
|
|
|
|
|
// header
|
|
|
|
if (_Offset < DISKHEADERINFO_ADDRESS)
|
|
|
|
{
|
|
|
|
WriteToBuffer(DISKHEADER_ADDRESS, DISKHEADERINFO_ADDRESS, m_diskHeader.data(), _Offset, _Length,
|
|
|
|
_pBuffer);
|
|
|
|
}
|
|
|
|
// header info
|
|
|
|
if (_Offset >= DISKHEADERINFO_ADDRESS && _Offset < APPLOADER_ADDRESS)
|
|
|
|
{
|
|
|
|
WriteToBuffer(DISKHEADERINFO_ADDRESS, sizeof(m_diskHeaderInfo), (u8*)m_diskHeaderInfo.get(),
|
|
|
|
_Offset, _Length, _pBuffer);
|
|
|
|
}
|
|
|
|
// apploader
|
|
|
|
if (_Offset >= APPLOADER_ADDRESS && _Offset < APPLOADER_ADDRESS + m_apploader.size())
|
|
|
|
{
|
|
|
|
WriteToBuffer(APPLOADER_ADDRESS, m_apploader.size(), m_apploader.data(), _Offset, _Length,
|
|
|
|
_pBuffer);
|
|
|
|
}
|
|
|
|
// dol
|
|
|
|
if (_Offset >= m_dol_address && _Offset < m_dol_address + m_DOL.size())
|
|
|
|
{
|
|
|
|
WriteToBuffer(m_dol_address, m_DOL.size(), m_DOL.data(), _Offset, _Length, _pBuffer);
|
|
|
|
}
|
|
|
|
// fst
|
|
|
|
if (_Offset >= m_fst_address && _Offset < m_dataStartAddress)
|
|
|
|
{
|
|
|
|
WriteToBuffer(m_fst_address, m_FSTData.size(), m_FSTData.data(), _Offset, _Length, _pBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_virtualDisk.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Determine which file the offset refers to
|
|
|
|
std::map<u64, std::string>::const_iterator fileIter = m_virtualDisk.lower_bound(_Offset);
|
|
|
|
if (fileIter->first > _Offset && fileIter != m_virtualDisk.begin())
|
|
|
|
--fileIter;
|
|
|
|
|
|
|
|
// zero fill to start of file data
|
|
|
|
PadToAddress(fileIter->first, _Offset, _Length, _pBuffer);
|
|
|
|
|
|
|
|
while (fileIter != m_virtualDisk.end() && _Length > 0)
|
|
|
|
{
|
|
|
|
_dbg_assert_(DVDINTERFACE, fileIter->first <= _Offset);
|
|
|
|
u64 fileOffset = _Offset - fileIter->first;
|
|
|
|
const std::string fileName = fileIter->second;
|
|
|
|
|
|
|
|
File::IOFile file(fileName, "rb");
|
|
|
|
if (!file)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
u64 fileSize = file.GetSize();
|
|
|
|
|
|
|
|
FileMon::CheckFile(fileName, fileSize);
|
|
|
|
|
|
|
|
if (fileOffset < fileSize)
|
|
|
|
{
|
|
|
|
u64 fileBytes = std::min(fileSize - fileOffset, _Length);
|
|
|
|
|
|
|
|
if (!file.Seek(fileOffset, SEEK_SET))
|
|
|
|
return false;
|
|
|
|
if (!file.ReadBytes(_pBuffer, fileBytes))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
_Length -= fileBytes;
|
|
|
|
_pBuffer += fileBytes;
|
|
|
|
_Offset += fileBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
++fileIter;
|
|
|
|
|
|
|
|
if (fileIter != m_virtualDisk.end())
|
|
|
|
{
|
|
|
|
_dbg_assert_(DVDINTERFACE, fileIter->first >= _Offset);
|
|
|
|
PadToAddress(fileIter->first, _Offset, _Length, _pBuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2016-10-29 12:42:43 +00:00
|
|
|
std::string CVolumeDirectory::GetGameID() const
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return std::string(m_diskHeader.begin(), m_diskHeader.begin() + MAX_ID_LENGTH);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2016-10-29 12:42:43 +00:00
|
|
|
void CVolumeDirectory::SetGameID(const std::string& id)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
memcpy(m_diskHeader.data(), id.c_str(), std::min(id.length(), MAX_ID_LENGTH));
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 18:33:05 +00:00
|
|
|
Country CVolumeDirectory::GetCountry() const
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return CountrySwitch(m_diskHeader[3]);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CVolumeDirectory::GetMakerID() const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// Not implemented
|
|
|
|
return "00";
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 09:19:30 +00:00
|
|
|
std::string CVolumeDirectory::GetInternalName() const
|
2015-04-10 20:10:49 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
char name[0x60];
|
|
|
|
if (Read(0x20, 0x60, (u8*)name, false))
|
|
|
|
return DecodeString(name);
|
|
|
|
else
|
|
|
|
return "";
|
2015-04-10 20:10:49 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 18:33:05 +00:00
|
|
|
std::map<Language, std::string> CVolumeDirectory::GetLongNames() const
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
std::string name = GetInternalName();
|
|
|
|
if (name.empty())
|
|
|
|
return {{}};
|
2016-07-06 18:33:05 +00:00
|
|
|
return {{Language::LANGUAGE_UNKNOWN, name}};
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2015-12-03 16:29:59 +00:00
|
|
|
std::vector<u32> CVolumeDirectory::GetBanner(int* width, int* height) const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// Not implemented
|
|
|
|
*width = 0;
|
|
|
|
*height = 0;
|
|
|
|
return std::vector<u32>();
|
2015-12-03 16:29:59 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 17:52:36 +00:00
|
|
|
void CVolumeDirectory::SetName(const std::string& name)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
size_t length = std::min(name.length(), MAX_NAME_LENGTH);
|
|
|
|
memcpy(&m_diskHeader[0x20], name.c_str(), length);
|
|
|
|
m_diskHeader[length + 0x20] = 0;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 14:35:23 +00:00
|
|
|
u64 CVolumeDirectory::GetFSTSize() const
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// Not implemented
|
|
|
|
return 0;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CVolumeDirectory::GetApploaderDate() const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// Not implemented
|
|
|
|
return "VOID";
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 18:33:05 +00:00
|
|
|
Platform CVolumeDirectory::GetVolumeType() const
|
2015-01-17 12:21:02 +00:00
|
|
|
{
|
2016-07-06 18:33:05 +00:00
|
|
|
return m_is_wii ? Platform::WII_DISC : Platform::GAMECUBE_DISC;
|
2015-01-17 12:21:02 +00:00
|
|
|
}
|
|
|
|
|
2015-09-27 12:01:12 +00:00
|
|
|
BlobType CVolumeDirectory::GetBlobType() const
|
2015-09-26 13:24:29 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// VolumeDirectory isn't actually a blob, but it sort of acts
|
|
|
|
// like one, so it makes sense that it has its own blob type.
|
|
|
|
// It should be made into a proper blob in the future.
|
|
|
|
return BlobType::DIRECTORY;
|
2015-09-26 13:24:29 +00:00
|
|
|
}
|
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
u64 CVolumeDirectory::GetSize() const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// Not implemented
|
|
|
|
return 0;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2013-04-09 17:58:56 +00:00
|
|
|
u64 CVolumeDirectory::GetRawSize() const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// Not implemented
|
|
|
|
return 0;
|
2013-04-09 17:58:56 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
std::string CVolumeDirectory::ExtractDirectoryName(const std::string& _rDirectory)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
std::string directoryName = _rDirectory;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
size_t lastSep = directoryName.find_last_of(DIR_SEP_CHR);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (lastSep != directoryName.size() - 1)
|
|
|
|
{
|
|
|
|
// TODO: This assumes that file names will always have a dot in them
|
|
|
|
// and directory names never will; both assumptions are often
|
|
|
|
// right but in general wrong.
|
|
|
|
size_t extensionStart = directoryName.find_last_of('.');
|
|
|
|
if (extensionStart != std::string::npos && extensionStart > lastSep)
|
|
|
|
{
|
|
|
|
directoryName.resize(lastSep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
directoryName.resize(lastSep);
|
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return directoryName;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CVolumeDirectory::SetDiskTypeWii()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
Write32(0x5d1c9ea3, 0x18, &m_diskHeader);
|
|
|
|
memset(&m_diskHeader[0x1c], 0, 4);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_is_wii = true;
|
|
|
|
m_addressShift = 2;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CVolumeDirectory::SetDiskTypeGC()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
memset(&m_diskHeader[0x18], 0, 4);
|
|
|
|
Write32(0xc2339f3d, 0x1c, &m_diskHeader);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_is_wii = false;
|
|
|
|
m_addressShift = 0;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2009-12-10 09:16:10 +00:00
|
|
|
bool CVolumeDirectory::SetApploader(const std::string& _rApploader)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!_rApploader.empty())
|
|
|
|
{
|
|
|
|
std::string data;
|
|
|
|
if (!File::ReadFileToString(_rApploader, data))
|
|
|
|
{
|
|
|
|
PanicAlertT("Apploader unable to load from file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
size_t apploaderSize = 0x20 + Common::swap32(*(u32*)&data.data()[0x14]) +
|
|
|
|
Common::swap32(*(u32*)&data.data()[0x18]);
|
|
|
|
if (apploaderSize != data.size())
|
|
|
|
{
|
|
|
|
PanicAlertT("Apploader is the wrong size...is it really an apploader?");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_apploader.resize(apploaderSize);
|
|
|
|
std::copy(data.begin(), data.end(), m_apploader.begin());
|
|
|
|
|
|
|
|
// 32byte aligned (plus 0x20 padding)
|
2016-11-27 10:56:22 +00:00
|
|
|
m_dol_address = Common::AlignUp(APPLOADER_ADDRESS + m_apploader.size() + 0x20, 0x20ull);
|
2016-06-24 08:43:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_apploader.resize(0x20);
|
|
|
|
// Make sure BS2 HLE doesn't try to run the apploader
|
|
|
|
*(u32*)&m_apploader[0x10] = (u32)-1;
|
|
|
|
return false;
|
|
|
|
}
|
2009-12-10 09:16:10 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
void CVolumeDirectory::SetDOL(const std::string& rDOL)
|
2009-12-10 09:16:10 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!rDOL.empty())
|
|
|
|
{
|
|
|
|
std::string data;
|
|
|
|
File::ReadFileToString(rDOL, data);
|
|
|
|
m_DOL.resize(data.size());
|
|
|
|
std::copy(data.begin(), data.end(), m_DOL.begin());
|
2009-12-10 09:16:10 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
Write32((u32)(m_dol_address >> m_addressShift), 0x0420, &m_diskHeader);
|
2009-12-10 09:16:10 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// 32byte aligned (plus 0x20 padding)
|
2016-11-27 10:56:22 +00:00
|
|
|
m_fst_address = Common::AlignUp(m_dol_address + m_DOL.size() + 0x20, 0x20ull);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2009-12-10 09:16:10 +00:00
|
|
|
}
|
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
void CVolumeDirectory::BuildFST()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_FSTData.clear();
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-25 21:57:14 +00:00
|
|
|
File::FSTEntry rootEntry = File::ScanDirectoryTree(m_rootDirectory, true);
|
|
|
|
u32 name_table_size = ComputeNameSize(rootEntry);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-25 21:57:14 +00:00
|
|
|
m_fstNameOffset = rootEntry.size * ENTRY_SIZE; // offset of name table in FST
|
|
|
|
m_FSTData.resize(m_fstNameOffset + name_table_size);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// if FST hasn't been assigned (ie no apploader/dol setup), set to default
|
|
|
|
if (m_fst_address == 0)
|
|
|
|
m_fst_address = APPLOADER_ADDRESS + 0x2000;
|
2012-04-23 07:38:58 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// 4 byte aligned start of data on disk
|
2016-11-27 10:56:22 +00:00
|
|
|
m_dataStartAddress = Common::AlignUp(m_fst_address + m_FSTData.size(), 0x8000ull);
|
2016-06-24 08:43:46 +00:00
|
|
|
u64 curDataAddress = m_dataStartAddress;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
u32 fstOffset = 0; // Offset within FST data
|
|
|
|
u32 nameOffset = 0; // Offset within name table
|
|
|
|
u32 rootOffset = 0; // Offset of root of FST
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// write root entry
|
2016-12-25 21:57:14 +00:00
|
|
|
WriteEntryData(fstOffset, DIRECTORY_ENTRY, 0, 0, rootEntry.size);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-25 21:35:38 +00:00
|
|
|
WriteDirectory(rootEntry, fstOffset, nameOffset, curDataAddress, rootOffset);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// overflow check
|
2016-12-25 21:57:14 +00:00
|
|
|
_dbg_assert_(DVDINTERFACE, nameOffset == name_table_size);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// write FST size and location
|
|
|
|
Write32((u32)(m_fst_address >> m_addressShift), 0x0424, &m_diskHeader);
|
|
|
|
Write32((u32)(m_FSTData.size() >> m_addressShift), 0x0428, &m_diskHeader);
|
|
|
|
Write32((u32)(m_FSTData.size() >> m_addressShift), 0x042c, &m_diskHeader);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2014-08-26 03:40:05 +00:00
|
|
|
void CVolumeDirectory::WriteToBuffer(u64 _SrcStartAddress, u64 _SrcLength, const u8* _Src,
|
2016-06-24 08:43:46 +00:00
|
|
|
u64& _Address, u64& _Length, u8*& _pBuffer) const
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (_Length == 0)
|
|
|
|
return;
|
2009-01-11 22:25:57 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
_dbg_assert_(DVDINTERFACE, _Address >= _SrcStartAddress);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
u64 srcOffset = _Address - _SrcStartAddress;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (srcOffset < _SrcLength)
|
|
|
|
{
|
|
|
|
u64 srcBytes = std::min(_SrcLength - srcOffset, _Length);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
memcpy(_pBuffer, _Src + srcOffset, (size_t)srcBytes);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
_Length -= srcBytes;
|
|
|
|
_pBuffer += srcBytes;
|
|
|
|
_Address += srcBytes;
|
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
void CVolumeDirectory::PadToAddress(u64 _StartAddress, u64& _Address, u64& _Length,
|
|
|
|
u8*& _pBuffer) const
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (_StartAddress > _Address && _Length > 0)
|
|
|
|
{
|
|
|
|
u64 padBytes = std::min(_StartAddress - _Address, _Length);
|
|
|
|
memset(_pBuffer, 0, (size_t)padBytes);
|
|
|
|
_Length -= padBytes;
|
|
|
|
_pBuffer += padBytes;
|
|
|
|
_Address += padBytes;
|
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2014-08-26 03:40:05 +00:00
|
|
|
void CVolumeDirectory::Write32(u32 data, u32 offset, std::vector<u8>* const buffer)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
(*buffer)[offset++] = (data >> 24);
|
|
|
|
(*buffer)[offset++] = (data >> 16) & 0xff;
|
|
|
|
(*buffer)[offset++] = (data >> 8) & 0xff;
|
|
|
|
(*buffer)[offset] = (data)&0xff;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
void CVolumeDirectory::WriteEntryData(u32& entryOffset, u8 type, u32 nameOffset, u64 dataOffset,
|
|
|
|
u64 length)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_FSTData[entryOffset++] = type;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_FSTData[entryOffset++] = (nameOffset >> 16) & 0xff;
|
|
|
|
m_FSTData[entryOffset++] = (nameOffset >> 8) & 0xff;
|
|
|
|
m_FSTData[entryOffset++] = (nameOffset)&0xff;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
Write32((u32)(dataOffset >> m_addressShift), entryOffset, &m_FSTData);
|
|
|
|
entryOffset += 4;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
Write32((u32)length, entryOffset, &m_FSTData);
|
|
|
|
entryOffset += 4;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CVolumeDirectory::WriteEntryName(u32& nameOffset, const std::string& name)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
strncpy((char*)&m_FSTData[nameOffset + m_fstNameOffset], name.c_str(), name.length() + 1);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
nameOffset += (u32)(name.length() + 1);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2016-12-25 21:35:38 +00:00
|
|
|
void CVolumeDirectory::WriteDirectory(const File::FSTEntry& parent_entry, u32& fstOffset,
|
|
|
|
u32& nameOffset, u64& dataOffset, u32 parentEntryNum)
|
2013-10-29 05:23:17 +00:00
|
|
|
{
|
2016-12-25 21:35:38 +00:00
|
|
|
std::vector<File::FSTEntry> sorted_entries = parent_entry.children;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-25 21:35:38 +00:00
|
|
|
std::sort(sorted_entries.begin(), sorted_entries.end(),
|
|
|
|
[](const File::FSTEntry& one, const File::FSTEntry& two) {
|
|
|
|
return one.virtualName < two.virtualName;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const File::FSTEntry& entry : sorted_entries)
|
|
|
|
{
|
|
|
|
if (entry.isDirectory)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-12-25 21:35:38 +00:00
|
|
|
u32 myOffset = fstOffset;
|
|
|
|
u32 myEntryNum = myOffset / ENTRY_SIZE;
|
|
|
|
WriteEntryData(fstOffset, DIRECTORY_ENTRY, nameOffset, parentEntryNum,
|
|
|
|
myEntryNum + entry.size + 1);
|
|
|
|
WriteEntryName(nameOffset, entry.virtualName);
|
|
|
|
|
|
|
|
WriteDirectory(entry, fstOffset, nameOffset, dataOffset, myEntryNum);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2016-12-25 21:35:38 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// put entry in FST
|
|
|
|
WriteEntryData(fstOffset, FILE_ENTRY, nameOffset, dataOffset, entry.size);
|
|
|
|
WriteEntryName(nameOffset, entry.virtualName);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-25 21:35:38 +00:00
|
|
|
// write entry to virtual disk
|
|
|
|
_dbg_assert_(DVDINTERFACE, m_virtualDisk.find(dataOffset) == m_virtualDisk.end());
|
|
|
|
m_virtualDisk.emplace(dataOffset, entry.physicalName);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-25 21:35:38 +00:00
|
|
|
// 4 byte aligned
|
|
|
|
dataOffset = Common::AlignUp(dataOffset + std::max<u64>(entry.size, 1ull), 0x8000ull);
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static u32 ComputeNameSize(const File::FSTEntry& parentEntry)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
u32 nameSize = 0;
|
2016-12-25 22:01:42 +00:00
|
|
|
for (const File::FSTEntry& entry : parentEntry.children)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
if (entry.isDirectory)
|
|
|
|
nameSize += ComputeNameSize(entry);
|
2016-12-25 22:01:42 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
nameSize += (u32)entry.virtualName.length() + 1;
|
|
|
|
}
|
|
|
|
return nameSize;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
} // namespace
|