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>
|
2017-01-24 17:18:26 +00:00
|
|
|
#include <locale>
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <map>
|
2014-08-26 03:40:05 +00:00
|
|
|
#include <memory>
|
2017-06-04 08:33:14 +00:00
|
|
|
#include <optional>
|
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"
|
2017-01-15 20:46:32 +00:00
|
|
|
#include "Common/File.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"
|
2017-05-01 12:08:47 +00:00
|
|
|
#include "Common/StringUtil.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-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-26 10:45:22 +00:00
|
|
|
static u32 ComputeNameSize(const File::FSTEntry& parent_entry);
|
2017-04-15 17:53:53 +00:00
|
|
|
static std::string ASCIIToUppercase(std::string str);
|
2017-05-01 12:08:47 +00:00
|
|
|
static void ConvertUTF8NamesToSHIFTJIS(File::FSTEntry& parent_entry);
|
2016-12-25 21:57:14 +00:00
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
const size_t VolumeDirectory::MAX_NAME_LENGTH;
|
|
|
|
const size_t VolumeDirectory::MAX_ID_LENGTH;
|
2015-11-17 09:09:54 +00:00
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
VolumeDirectory::VolumeDirectory(const std::string& directory, bool is_wii,
|
|
|
|
const std::string& apploader, const std::string& dol)
|
2017-06-07 11:16:02 +00:00
|
|
|
: m_data_start_address(UINT64_MAX), m_disk_header(DISKHEADERINFO_ADDRESS),
|
2016-12-26 10:45:22 +00:00
|
|
|
m_disk_header_info(std::make_unique<SDiskHeaderInfo>()), m_fst_address(0), m_dol_address(0)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
m_root_directory = ExtractDirectoryName(directory);
|
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-12-26 10:45:22 +00:00
|
|
|
if (is_wii)
|
2016-06-24 08:43:46 +00:00
|
|
|
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
|
2016-12-26 10:45:22 +00:00
|
|
|
if (SetApploader(apploader))
|
|
|
|
SetDOL(dol);
|
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
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
VolumeDirectory::~VolumeDirectory()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
bool VolumeDirectory::IsValidDirectory(const std::string& directory)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
return File::IsDirectory(ExtractDirectoryName(directory));
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
bool VolumeDirectory::Read(u64 offset, u64 length, u8* buffer, const Partition& partition) const
|
2009-02-22 13:59:06 +00:00
|
|
|
{
|
2015-06-13 10:51:24 +00:00
|
|
|
bool decrypt = partition != PARTITION_NONE;
|
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
if (!decrypt && (offset + length >= 0x400) && m_is_wii)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
// 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)
|
2015-06-13 10:51:24 +00:00
|
|
|
return false;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// header
|
2016-12-26 10:45:22 +00:00
|
|
|
if (offset < DISKHEADERINFO_ADDRESS)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
WriteToBuffer(DISKHEADER_ADDRESS, DISKHEADERINFO_ADDRESS, m_disk_header.data(), &offset,
|
|
|
|
&length, &buffer);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
// header info
|
2016-12-26 10:45:22 +00:00
|
|
|
if (offset >= DISKHEADERINFO_ADDRESS && offset < APPLOADER_ADDRESS)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
WriteToBuffer(DISKHEADERINFO_ADDRESS, sizeof(m_disk_header_info), (u8*)m_disk_header_info.get(),
|
|
|
|
&offset, &length, &buffer);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
// apploader
|
2016-12-26 10:45:22 +00:00
|
|
|
if (offset >= APPLOADER_ADDRESS && offset < APPLOADER_ADDRESS + m_apploader.size())
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
WriteToBuffer(APPLOADER_ADDRESS, m_apploader.size(), m_apploader.data(), &offset, &length,
|
|
|
|
&buffer);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
// dol
|
2016-12-26 10:45:22 +00:00
|
|
|
if (offset >= m_dol_address && offset < m_dol_address + m_dol.size())
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
WriteToBuffer(m_dol_address, m_dol.size(), m_dol.data(), &offset, &length, &buffer);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
// fst
|
2016-12-26 10:45:22 +00:00
|
|
|
if (offset >= m_fst_address && offset < m_data_start_address)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
WriteToBuffer(m_fst_address, m_fst_data.size(), m_fst_data.data(), &offset, &length, &buffer);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
if (m_virtual_disk.empty())
|
2016-06-24 08:43:46 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Determine which file the offset refers to
|
2016-12-26 10:45:22 +00:00
|
|
|
std::map<u64, std::string>::const_iterator fileIter = m_virtual_disk.lower_bound(offset);
|
|
|
|
if (fileIter->first > offset && fileIter != m_virtual_disk.begin())
|
2016-06-24 08:43:46 +00:00
|
|
|
--fileIter;
|
|
|
|
|
|
|
|
// zero fill to start of file data
|
2016-12-26 10:45:22 +00:00
|
|
|
PadToAddress(fileIter->first, &offset, &length, &buffer);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
while (fileIter != m_virtual_disk.end() && length > 0)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
_dbg_assert_(DVDINTERFACE, fileIter->first <= offset);
|
|
|
|
u64 fileOffset = offset - fileIter->first;
|
2016-06-24 08:43:46 +00:00
|
|
|
const std::string fileName = fileIter->second;
|
|
|
|
|
|
|
|
File::IOFile file(fileName, "rb");
|
|
|
|
if (!file)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
u64 fileSize = file.GetSize();
|
|
|
|
|
|
|
|
if (fileOffset < fileSize)
|
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
u64 fileBytes = std::min(fileSize - fileOffset, length);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
if (!file.Seek(fileOffset, SEEK_SET))
|
|
|
|
return false;
|
2016-12-26 10:45:22 +00:00
|
|
|
if (!file.ReadBytes(buffer, fileBytes))
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
length -= fileBytes;
|
|
|
|
buffer += fileBytes;
|
|
|
|
offset += fileBytes;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
++fileIter;
|
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
if (fileIter != m_virtual_disk.end())
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
_dbg_assert_(DVDINTERFACE, fileIter->first >= offset);
|
|
|
|
PadToAddress(fileIter->first, &offset, &length, &buffer);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
std::vector<Partition> VolumeDirectory::GetPartitions() const
|
2015-06-13 10:51:24 +00:00
|
|
|
{
|
|
|
|
return m_is_wii ? std::vector<Partition>{GetGamePartition()} : std::vector<Partition>();
|
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
Partition VolumeDirectory::GetGamePartition() const
|
2015-06-13 10:51:24 +00:00
|
|
|
{
|
|
|
|
return m_is_wii ? Partition(0x50000) : PARTITION_NONE;
|
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
std::string VolumeDirectory::GetGameID(const Partition& partition) const
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
return std::string(m_disk_header.begin(), m_disk_header.begin() + MAX_ID_LENGTH);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
void VolumeDirectory::SetGameID(const std::string& id)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
memcpy(m_disk_header.data(), id.c_str(), std::min(id.length(), MAX_ID_LENGTH));
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
Region VolumeDirectory::GetRegion() const
|
2016-12-23 17:41:21 +00:00
|
|
|
{
|
|
|
|
if (m_is_wii)
|
|
|
|
return RegionSwitchWii(m_disk_header[3]);
|
|
|
|
|
|
|
|
return RegionSwitchGC(m_disk_header[3]);
|
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
Country VolumeDirectory::GetCountry(const Partition& partition) const
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
return CountrySwitch(m_disk_header[3]);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
std::string VolumeDirectory::GetMakerID(const Partition& partition) const
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// Not implemented
|
|
|
|
return "00";
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
std::string VolumeDirectory::GetInternalName(const Partition& partition) const
|
2015-04-10 20:10:49 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
char name[0x60];
|
2015-06-13 10:51:24 +00:00
|
|
|
if (Read(0x20, 0x60, (u8*)name, partition))
|
2016-06-24 08:43:46 +00:00
|
|
|
return DecodeString(name);
|
|
|
|
else
|
|
|
|
return "";
|
2015-04-10 20:10:49 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
std::map<Language, std::string> VolumeDirectory::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())
|
2017-06-03 16:14:22 +00:00
|
|
|
return {};
|
2016-07-06 18:33:05 +00:00
|
|
|
return {{Language::LANGUAGE_UNKNOWN, name}};
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
std::vector<u32> VolumeDirectory::GetBanner(int* width, int* height) const
|
2015-12-03 16:29:59 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
void VolumeDirectory::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);
|
2016-12-26 10:45:22 +00:00
|
|
|
memcpy(&m_disk_header[0x20], name.c_str(), length);
|
|
|
|
m_disk_header[length + 0x20] = 0;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
std::string VolumeDirectory::GetApploaderDate(const Partition& partition) const
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// Not implemented
|
|
|
|
return "VOID";
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
Platform VolumeDirectory::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
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
BlobType VolumeDirectory::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
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
u64 VolumeDirectory::GetSize() 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
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
u64 VolumeDirectory::GetRawSize() const
|
2013-04-09 17:58:56 +00:00
|
|
|
{
|
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
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
std::string VolumeDirectory::ExtractDirectoryName(const std::string& directory)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
std::string result = directory;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
size_t last_separator = result.find_last_of(DIR_SEP_CHR);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
if (last_separator != result.size() - 1)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
// 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.
|
2016-12-26 10:45:22 +00:00
|
|
|
size_t extension_start = result.find_last_of('.');
|
|
|
|
if (extension_start != std::string::npos && extension_start > last_separator)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
result.resize(last_separator);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
result.resize(last_separator);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
return result;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
void VolumeDirectory::SetDiskTypeWii()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
Write32(0x5d1c9ea3, 0x18, &m_disk_header);
|
|
|
|
memset(&m_disk_header[0x1c], 0, 4);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_is_wii = true;
|
2016-12-26 10:45:22 +00:00
|
|
|
m_address_shift = 2;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
void VolumeDirectory::SetDiskTypeGC()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
memset(&m_disk_header[0x18], 0, 4);
|
|
|
|
Write32(0xc2339f3d, 0x1c, &m_disk_header);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_is_wii = false;
|
2016-12-26 10:45:22 +00:00
|
|
|
m_address_shift = 0;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
bool VolumeDirectory::SetApploader(const std::string& apploader)
|
2009-12-10 09:16:10 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
if (!apploader.empty())
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
std::string data;
|
2016-12-26 10:45:22 +00:00
|
|
|
if (!File::ReadFileToString(apploader, data))
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
PanicAlertT("Apploader unable to load from file");
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-26 10:45:22 +00:00
|
|
|
size_t apploader_size = 0x20 + Common::swap32(*(u32*)&data.data()[0x14]) +
|
|
|
|
Common::swap32(*(u32*)&data.data()[0x18]);
|
|
|
|
if (apploader_size != data.size())
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
PanicAlertT("Apploader is the wrong size...is it really an apploader?");
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-26 10:45:22 +00:00
|
|
|
m_apploader.resize(apploader_size);
|
2016-06-24 08:43:46 +00:00
|
|
|
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
|
2017-06-09 15:57:05 +00:00
|
|
|
Write32(static_cast<u32>(-1), 0x10, &m_apploader);
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-12-10 09:16:10 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
void VolumeDirectory::SetDOL(const std::string& dol)
|
2009-12-10 09:16:10 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
if (!dol.empty())
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
std::string data;
|
2016-12-26 10:45:22 +00:00
|
|
|
File::ReadFileToString(dol, data);
|
|
|
|
m_dol.resize(data.size());
|
|
|
|
std::copy(data.begin(), data.end(), m_dol.begin());
|
2009-12-10 09:16:10 +00:00
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
Write32((u32)(m_dol_address >> m_address_shift), 0x0420, &m_disk_header);
|
2009-12-10 09:16:10 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// 32byte aligned (plus 0x20 padding)
|
2016-12-26 10:45: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
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
void VolumeDirectory::BuildFST()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
m_fst_data.clear();
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
File::FSTEntry rootEntry = File::ScanDirectoryTree(m_root_directory, true);
|
2017-05-01 12:08:47 +00:00
|
|
|
|
|
|
|
ConvertUTF8NamesToSHIFTJIS(rootEntry);
|
|
|
|
|
2017-03-13 18:49:31 +00:00
|
|
|
u32 name_table_size = Common::AlignUp(ComputeNameSize(rootEntry), 1ull << m_address_shift);
|
2017-03-13 17:18:51 +00:00
|
|
|
u64 total_entries = rootEntry.size + 1; // The root entry itself isn't counted in rootEntry.size
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2017-03-13 17:18:51 +00:00
|
|
|
m_fst_name_offset = total_entries * ENTRY_SIZE; // offset of name table in FST
|
2016-12-26 10:45:22 +00:00
|
|
|
m_fst_data.resize(m_fst_name_offset + 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
|
|
|
|
2017-06-09 16:00:25 +00:00
|
|
|
// 32 KiB aligned start of data on disk
|
2016-12-26 10:45:22 +00:00
|
|
|
m_data_start_address = Common::AlignUp(m_fst_address + m_fst_data.size(), 0x8000ull);
|
|
|
|
u64 current_data_address = m_data_start_address;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
u32 fst_offset = 0; // Offset within FST data
|
|
|
|
u32 name_offset = 0; // Offset within name table
|
|
|
|
u32 root_offset = 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
|
2017-03-28 00:53:42 +00:00
|
|
|
WriteEntryData(&fst_offset, DIRECTORY_ENTRY, 0, 0, total_entries, m_address_shift);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
WriteDirectory(rootEntry, &fst_offset, &name_offset, ¤t_data_address, root_offset);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2017-03-28 00:52:40 +00:00
|
|
|
// overflow check, compare the aligned name offset with the aligned name table size
|
|
|
|
_assert_(Common::AlignUp(name_offset, 1ull << m_address_shift) == name_table_size);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// write FST size and location
|
2016-12-26 10:45:22 +00:00
|
|
|
Write32((u32)(m_fst_address >> m_address_shift), 0x0424, &m_disk_header);
|
|
|
|
Write32((u32)(m_fst_data.size() >> m_address_shift), 0x0428, &m_disk_header);
|
|
|
|
Write32((u32)(m_fst_data.size() >> m_address_shift), 0x042c, &m_disk_header);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
void VolumeDirectory::WriteToBuffer(u64 source_start_address, u64 source_length, const u8* source,
|
|
|
|
u64* address, u64* length, u8** buffer) const
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
if (*length == 0)
|
2016-06-24 08:43:46 +00:00
|
|
|
return;
|
2009-01-11 22:25:57 +00:00
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
_dbg_assert_(DVDINTERFACE, *address >= source_start_address);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
u64 source_offset = *address - source_start_address;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
if (source_offset < source_length)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
size_t bytes_to_read = std::min(source_length - source_offset, *length);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
memcpy(*buffer, source + source_offset, bytes_to_read);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
*length -= bytes_to_read;
|
|
|
|
*buffer += bytes_to_read;
|
|
|
|
*address += bytes_to_read;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
void VolumeDirectory::PadToAddress(u64 start_address, u64* address, u64* length, u8** buffer) const
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
if (start_address > *address && *length > 0)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
u64 padBytes = std::min(start_address - *address, *length);
|
|
|
|
memset(*buffer, 0, (size_t)padBytes);
|
|
|
|
*length -= padBytes;
|
|
|
|
*buffer += padBytes;
|
|
|
|
*address += padBytes;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
void VolumeDirectory::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
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
void VolumeDirectory::WriteEntryData(u32* entry_offset, u8 type, u32 name_offset, u64 data_offset,
|
|
|
|
u64 length, u32 address_shift)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
m_fst_data[(*entry_offset)++] = type;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
m_fst_data[(*entry_offset)++] = (name_offset >> 16) & 0xff;
|
|
|
|
m_fst_data[(*entry_offset)++] = (name_offset >> 8) & 0xff;
|
|
|
|
m_fst_data[(*entry_offset)++] = (name_offset)&0xff;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2017-03-28 00:53:42 +00:00
|
|
|
Write32((u32)(data_offset >> address_shift), *entry_offset, &m_fst_data);
|
2016-12-26 10:45:22 +00:00
|
|
|
*entry_offset += 4;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
Write32((u32)length, *entry_offset, &m_fst_data);
|
|
|
|
*entry_offset += 4;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
void VolumeDirectory::WriteEntryName(u32* name_offset, const std::string& name)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
strncpy((char*)&m_fst_data[*name_offset + m_fst_name_offset], name.c_str(), name.length() + 1);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
*name_offset += (u32)(name.length() + 1);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
void VolumeDirectory::WriteDirectory(const File::FSTEntry& parent_entry, u32* fst_offset,
|
|
|
|
u32* name_offset, u64* data_offset, u32 parent_entry_index)
|
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
|
|
|
|
2017-01-24 17:18:26 +00:00
|
|
|
// Sort for determinism
|
|
|
|
std::sort(sorted_entries.begin(), sorted_entries.end(), [](const File::FSTEntry& one,
|
|
|
|
const File::FSTEntry& two) {
|
2017-04-15 17:53:53 +00:00
|
|
|
const std::string one_upper = ASCIIToUppercase(one.virtualName);
|
|
|
|
const std::string two_upper = ASCIIToUppercase(two.virtualName);
|
|
|
|
return one_upper == two_upper ? one.virtualName < two.virtualName : one_upper < two_upper;
|
2017-01-24 17:18:26 +00:00
|
|
|
});
|
2016-12-25 21:35:38 +00:00
|
|
|
|
|
|
|
for (const File::FSTEntry& entry : sorted_entries)
|
|
|
|
{
|
|
|
|
if (entry.isDirectory)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
u32 entry_index = *fst_offset / ENTRY_SIZE;
|
2017-03-28 00:53:42 +00:00
|
|
|
WriteEntryData(fst_offset, DIRECTORY_ENTRY, *name_offset, parent_entry_index,
|
|
|
|
entry_index + entry.size + 1, 0);
|
2016-12-26 10:45:22 +00:00
|
|
|
WriteEntryName(name_offset, entry.virtualName);
|
2016-12-25 21:35:38 +00:00
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
WriteDirectory(entry, fst_offset, name_offset, data_offset, entry_index);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2016-12-25 21:35:38 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// put entry in FST
|
2017-03-28 00:53:42 +00:00
|
|
|
WriteEntryData(fst_offset, FILE_ENTRY, *name_offset, *data_offset, entry.size,
|
|
|
|
m_address_shift);
|
2016-12-26 10:45:22 +00:00
|
|
|
WriteEntryName(name_offset, entry.virtualName);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-25 21:35:38 +00:00
|
|
|
// write entry to virtual disk
|
2016-12-26 10:45:22 +00:00
|
|
|
_dbg_assert_(DVDINTERFACE, m_virtual_disk.find(*data_offset) == m_virtual_disk.end());
|
|
|
|
m_virtual_disk.emplace(*data_offset, entry.physicalName);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2017-06-09 16:00:25 +00:00
|
|
|
// 32 KiB aligned - many games are fine with less alignment, but not all
|
2016-12-26 10:45:22 +00:00
|
|
|
*data_offset = Common::AlignUp(*data_offset + std::max<u64>(entry.size, 1ull), 0x8000ull);
|
2016-12-25 21:35:38 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
static u32 ComputeNameSize(const File::FSTEntry& parent_entry)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-12-26 10:45:22 +00:00
|
|
|
u32 name_size = 0;
|
|
|
|
for (const File::FSTEntry& entry : parent_entry.children)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
if (entry.isDirectory)
|
2016-12-26 10:45:22 +00:00
|
|
|
name_size += ComputeNameSize(entry);
|
2016-12-25 22:01:42 +00:00
|
|
|
|
2016-12-26 10:45:22 +00:00
|
|
|
name_size += (u32)entry.virtualName.length() + 1;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2016-12-26 10:45:22 +00:00
|
|
|
return name_size;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 12:08:47 +00:00
|
|
|
static void ConvertUTF8NamesToSHIFTJIS(File::FSTEntry& parent_entry)
|
|
|
|
{
|
|
|
|
for (File::FSTEntry& entry : parent_entry.children)
|
|
|
|
{
|
|
|
|
if (entry.isDirectory)
|
|
|
|
ConvertUTF8NamesToSHIFTJIS(entry);
|
|
|
|
|
|
|
|
entry.virtualName = UTF8ToSHIFTJIS(entry.virtualName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-15 17:53:53 +00:00
|
|
|
static std::string ASCIIToUppercase(std::string str)
|
2017-01-24 17:18:26 +00:00
|
|
|
{
|
|
|
|
std::transform(str.begin(), str.end(), str.begin(),
|
2017-04-15 17:53:53 +00:00
|
|
|
[](char c) { return std::toupper(c, std::locale::classic()); });
|
2017-01-24 17:18:26 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
} // namespace
|