2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:09:55 -06:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
#include "DiscIO/DirectoryBlob.h"
|
|
|
|
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <algorithm>
|
2017-06-07 12:32:09 -06:00
|
|
|
#include <array>
|
|
|
|
#include <cinttypes>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
2017-01-24 10:18:26 -07:00
|
|
|
#include <locale>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <map>
|
2014-08-25 21:40:05 -06:00
|
|
|
#include <memory>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <string>
|
2017-06-07 12:32:09 -06:00
|
|
|
#include <utility>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <vector>
|
|
|
|
|
2016-11-27 03:56:22 -07:00
|
|
|
#include "Common/Align.h"
|
2015-09-26 14:39:47 -06:00
|
|
|
#include "Common/Assert.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/CommonPaths.h"
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2017-01-15 13:46:32 -07:00
|
|
|
#include "Common/File.h"
|
2014-02-20 17:47:53 -07:00
|
|
|
#include "Common/FileUtil.h"
|
2015-09-26 14:39:47 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
2017-05-01 06:08:47 -06:00
|
|
|
#include "Common/StringUtil.h"
|
2017-06-07 12:32:09 -06:00
|
|
|
#include "Common/Swap.h"
|
|
|
|
#include "Core/Boot/DolReader.h"
|
2015-09-27 06:01:12 -06:00
|
|
|
#include "DiscIO/Blob.h"
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
static u32 ComputeNameSize(const File::FSTEntry& parent_entry);
|
2017-04-15 11:53:53 -06:00
|
|
|
static std::string ASCIIToUppercase(std::string str);
|
2017-05-01 06:08:47 -06:00
|
|
|
static void ConvertUTF8NamesToSHIFTJIS(File::FSTEntry& parent_entry);
|
2016-12-25 14:57:14 -07:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
constexpr u64 GAME_PARTITION_ADDRESS = 0x50000;
|
|
|
|
constexpr u64 PARTITION_TABLE_ADDRESS = 0x40000;
|
|
|
|
const std::array<u32, 10> PARTITION_TABLE = {
|
|
|
|
{Common::swap32(1), Common::swap32((PARTITION_TABLE_ADDRESS + 0x20) >> 2), 0, 0, 0, 0, 0, 0,
|
|
|
|
Common::swap32(GAME_PARTITION_ADDRESS >> 2), 0}};
|
2015-11-17 02:09:54 -07:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
const size_t DirectoryBlobReader::MAX_NAME_LENGTH;
|
|
|
|
const size_t DirectoryBlobReader::MAX_ID_LENGTH;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
static bool PathCharactersEqual(char a, char b)
|
|
|
|
{
|
|
|
|
return a == b
|
|
|
|
#ifdef _WIN32
|
|
|
|
|| (a == '/' && b == '\\') || (a == '\\' && b == '/')
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
static bool PathEndsWith(const std::string& path, const std::string& suffix)
|
|
|
|
{
|
|
|
|
if (suffix.size() > path.size())
|
|
|
|
return false;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
std::string::const_iterator path_iterator = path.cend() - suffix.size();
|
|
|
|
std::string::const_iterator suffix_iterator = suffix.cbegin();
|
|
|
|
while (path_iterator != path.cend())
|
|
|
|
{
|
|
|
|
if (!PathCharactersEqual(*path_iterator, *suffix_iterator))
|
|
|
|
return false;
|
|
|
|
path_iterator++;
|
|
|
|
suffix_iterator++;
|
|
|
|
}
|
2009-12-10 02:16:10 -07:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
return true;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
bool DirectoryBlobReader::IsValidDirectoryBlob(const std::string& dol_path)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2017-06-07 12:32:09 -06:00
|
|
|
return PathEndsWith(dol_path, "/sys/main.dol");
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
std::unique_ptr<DirectoryBlobReader> DirectoryBlobReader::Create(File::IOFile dol,
|
|
|
|
const std::string& dol_path)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2017-06-07 12:32:09 -06:00
|
|
|
if (!dol || !IsValidDirectoryBlob(dol_path))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const size_t chars_to_remove = std::string("sys/main.dol").size();
|
|
|
|
const std::string root_directory = dol_path.substr(0, dol_path.size() - chars_to_remove);
|
|
|
|
return std::unique_ptr<DirectoryBlobReader>(
|
|
|
|
new DirectoryBlobReader(std::move(dol), root_directory));
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
DirectoryBlobReader::DirectoryBlobReader(File::IOFile dol_file, const std::string& root_directory)
|
|
|
|
: m_root_directory(root_directory), m_data_start_address(UINT64_MAX),
|
|
|
|
m_disk_header(DISKHEADERINFO_ADDRESS),
|
|
|
|
m_disk_header_info(std::make_unique<SDiskHeaderInfo>()), m_fst_address(0), m_dol_address(0)
|
2009-02-22 06:59:06 -07:00
|
|
|
{
|
2017-06-07 12:32:09 -06:00
|
|
|
// create the default disk header
|
|
|
|
SetGameID("AGBJ01");
|
|
|
|
SetName("Default name");
|
2015-06-13 04:51:24 -06:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
// Setting the DOL relies on m_dol_address, which is set by SetApploader
|
|
|
|
if (SetApploader(m_root_directory + "sys/apploader.img"))
|
|
|
|
SetDOLAndDiskType(std::move(dol_file));
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
BuildFST();
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
bool DirectoryBlobReader::ReadPartition(u64 offset, u64 length, u8* buffer)
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
// header
|
2016-12-26 03:45:22 -07:00
|
|
|
if (offset < DISKHEADERINFO_ADDRESS)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
WriteToBuffer(DISKHEADER_ADDRESS, DISKHEADERINFO_ADDRESS, m_disk_header.data(), &offset,
|
|
|
|
&length, &buffer);
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
// header info
|
2016-12-26 03:45:22 -07:00
|
|
|
if (offset >= DISKHEADERINFO_ADDRESS && offset < APPLOADER_ADDRESS)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
WriteToBuffer(DISKHEADERINFO_ADDRESS, sizeof(m_disk_header_info), (u8*)m_disk_header_info.get(),
|
|
|
|
&offset, &length, &buffer);
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
// apploader
|
2016-12-26 03:45:22 -07:00
|
|
|
if (offset >= APPLOADER_ADDRESS && offset < APPLOADER_ADDRESS + m_apploader.size())
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
WriteToBuffer(APPLOADER_ADDRESS, m_apploader.size(), m_apploader.data(), &offset, &length,
|
|
|
|
&buffer);
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
// dol
|
2016-12-26 03:45:22 -07:00
|
|
|
if (offset >= m_dol_address && offset < m_dol_address + m_dol.size())
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
WriteToBuffer(m_dol_address, m_dol.size(), m_dol.data(), &offset, &length, &buffer);
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
// fst
|
2016-12-26 03:45:22 -07:00
|
|
|
if (offset >= m_fst_address && offset < m_data_start_address)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
WriteToBuffer(m_fst_address, m_fst_data.size(), m_fst_data.data(), &offset, &length, &buffer);
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
|
2016-12-26 03:45:22 -07:00
|
|
|
if (m_virtual_disk.empty())
|
2016-06-24 02:43:46 -06:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Determine which file the offset refers to
|
2016-12-26 03:45:22 -07: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 02:43:46 -06:00
|
|
|
--fileIter;
|
|
|
|
|
|
|
|
// zero fill to start of file data
|
2016-12-26 03:45:22 -07:00
|
|
|
PadToAddress(fileIter->first, &offset, &length, &buffer);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2016-12-26 03:45:22 -07:00
|
|
|
while (fileIter != m_virtual_disk.end() && length > 0)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
_dbg_assert_(DVDINTERFACE, fileIter->first <= offset);
|
|
|
|
u64 fileOffset = offset - fileIter->first;
|
2016-06-24 02:43:46 -06: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 03:45:22 -07:00
|
|
|
u64 fileBytes = std::min(fileSize - fileOffset, length);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
if (!file.Seek(fileOffset, SEEK_SET))
|
|
|
|
return false;
|
2016-12-26 03:45:22 -07:00
|
|
|
if (!file.ReadBytes(buffer, fileBytes))
|
2016-06-24 02:43:46 -06:00
|
|
|
return false;
|
|
|
|
|
2016-12-26 03:45:22 -07:00
|
|
|
length -= fileBytes;
|
|
|
|
buffer += fileBytes;
|
|
|
|
offset += fileBytes;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
++fileIter;
|
|
|
|
|
2016-12-26 03:45:22 -07:00
|
|
|
if (fileIter != m_virtual_disk.end())
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
_dbg_assert_(DVDINTERFACE, fileIter->first >= offset);
|
|
|
|
PadToAddress(fileIter->first, &offset, &length, &buffer);
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
bool DirectoryBlobReader::ReadNonPartition(u64 offset, u64 length, u8* buffer)
|
2015-06-13 04:51:24 -06:00
|
|
|
{
|
2017-06-07 12:32:09 -06:00
|
|
|
// header
|
|
|
|
if (offset < DISKHEADERINFO_ADDRESS)
|
|
|
|
{
|
|
|
|
WriteToBuffer(DISKHEADER_ADDRESS, DISKHEADERINFO_ADDRESS, m_disk_header.data(), &offset,
|
|
|
|
&length, &buffer);
|
|
|
|
}
|
|
|
|
if (offset >= 0x40000)
|
|
|
|
{
|
|
|
|
WriteToBuffer(PARTITION_TABLE_ADDRESS, PARTITION_TABLE.size() * sizeof(u32),
|
|
|
|
reinterpret_cast<const u8*>(PARTITION_TABLE.data()), &offset, &length, &buffer);
|
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
// TODO: TMDs, tickets, more headers, the partition contents...
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
if (length > 0)
|
|
|
|
{
|
|
|
|
ERROR_LOG(DISCIO, "Unsupported raw read in DirectoryBlob at 0x%" PRIx64, offset);
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-23 10:41:21 -07:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
return true;
|
2016-12-23 10:41:21 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
bool DirectoryBlobReader::Read(u64 offset, u64 length, u8* buffer)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2017-06-07 12:32:09 -06:00
|
|
|
return m_is_wii ? ReadNonPartition(offset, length, buffer) :
|
|
|
|
ReadPartition(offset, length, buffer);
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
bool DirectoryBlobReader::SupportsReadWiiDecrypted() const
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2017-06-07 12:32:09 -06:00
|
|
|
return m_is_wii;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
bool DirectoryBlobReader::ReadWiiDecrypted(u64 offset, u64 size, u8* buffer, u64 partition_offset)
|
2015-04-10 14:10:49 -06:00
|
|
|
{
|
2017-06-07 12:32:09 -06:00
|
|
|
if (!m_is_wii || partition_offset != GAME_PARTITION_ADDRESS)
|
|
|
|
return false;
|
2015-04-10 14:10:49 -06:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
return ReadPartition(offset, size, buffer);
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
void DirectoryBlobReader::SetGameID(const std::string& id)
|
2015-12-03 09:29:59 -07:00
|
|
|
{
|
2017-06-07 12:32:09 -06:00
|
|
|
memcpy(m_disk_header.data(), id.c_str(), std::min(id.length(), MAX_ID_LENGTH));
|
2015-12-03 09:29:59 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
void DirectoryBlobReader::SetName(const std::string& name)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
size_t length = std::min(name.length(), MAX_NAME_LENGTH);
|
2016-12-26 03:45:22 -07:00
|
|
|
memcpy(&m_disk_header[0x20], name.c_str(), length);
|
|
|
|
m_disk_header[length + 0x20] = 0;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
BlobType DirectoryBlobReader::GetBlobType() const
|
2015-09-26 07:24:29 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
return BlobType::DIRECTORY;
|
2015-09-26 07:24:29 -06:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
u64 DirectoryBlobReader::GetRawSize() const
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
// Not implemented
|
|
|
|
return 0;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
u64 DirectoryBlobReader::GetDataSize() const
|
2013-04-09 11:58:56 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
// Not implemented
|
|
|
|
return 0;
|
2013-04-09 11:58:56 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
void DirectoryBlobReader::SetDiskTypeWii()
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
Write32(0x5d1c9ea3, 0x18, &m_disk_header);
|
|
|
|
memset(&m_disk_header[0x1c], 0, 4);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
m_is_wii = true;
|
2016-12-26 03:45:22 -07:00
|
|
|
m_address_shift = 2;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
void DirectoryBlobReader::SetDiskTypeGC()
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
memset(&m_disk_header[0x18], 0, 4);
|
|
|
|
Write32(0xc2339f3d, 0x1c, &m_disk_header);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
m_is_wii = false;
|
2016-12-26 03:45:22 -07:00
|
|
|
m_address_shift = 0;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
bool DirectoryBlobReader::SetApploader(const std::string& apploader)
|
2009-12-10 02:16:10 -07:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
if (!apploader.empty())
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
|
|
|
std::string data;
|
2016-12-26 03:45:22 -07:00
|
|
|
if (!File::ReadFileToString(apploader, data))
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
|
|
|
PanicAlertT("Apploader unable to load from file");
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-26 03:45:22 -07: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 02:43:46 -06:00
|
|
|
{
|
|
|
|
PanicAlertT("Apploader is the wrong size...is it really an apploader?");
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-26 03:45:22 -07:00
|
|
|
m_apploader.resize(apploader_size);
|
2016-06-24 02:43:46 -06:00
|
|
|
std::copy(data.begin(), data.end(), m_apploader.begin());
|
|
|
|
|
|
|
|
// 32byte aligned (plus 0x20 padding)
|
2016-11-27 03:56:22 -07:00
|
|
|
m_dol_address = Common::AlignUp(APPLOADER_ADDRESS + m_apploader.size() + 0x20, 0x20ull);
|
2016-06-24 02:43:46 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_apploader.resize(0x20);
|
|
|
|
// Make sure BS2 HLE doesn't try to run the apploader
|
2017-06-09 09:57:05 -06:00
|
|
|
Write32(static_cast<u32>(-1), 0x10, &m_apploader);
|
2016-06-24 02:43:46 -06:00
|
|
|
return false;
|
|
|
|
}
|
2009-12-10 02:16:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
void DirectoryBlobReader::SetDOLAndDiskType(File::IOFile dol_file)
|
2009-12-10 02:16:10 -07:00
|
|
|
{
|
2017-06-07 12:32:09 -06:00
|
|
|
m_dol.resize(dol_file.GetSize());
|
|
|
|
dol_file.Seek(0, SEEK_SET);
|
|
|
|
dol_file.ReadBytes(m_dol.data(), m_dol.size());
|
2009-12-10 02:16:10 -07:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
if (DolReader(std::move(dol_file)).IsWii())
|
|
|
|
SetDiskTypeWii();
|
|
|
|
else
|
|
|
|
SetDiskTypeGC();
|
2009-12-10 02:16:10 -07:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
Write32((u32)(m_dol_address >> m_address_shift), 0x0420, &m_disk_header);
|
|
|
|
|
|
|
|
// 32byte aligned (plus 0x20 padding)
|
|
|
|
m_fst_address = Common::AlignUp(m_dol_address + m_dol.size() + 0x20, 0x20ull);
|
2009-12-10 02:16:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
void DirectoryBlobReader::BuildFST()
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
m_fst_data.clear();
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
File::FSTEntry rootEntry = File::ScanDirectoryTree(m_root_directory + "files/", true);
|
2017-05-01 06:08:47 -06:00
|
|
|
|
|
|
|
ConvertUTF8NamesToSHIFTJIS(rootEntry);
|
|
|
|
|
2017-03-13 12:49:31 -06:00
|
|
|
u32 name_table_size = Common::AlignUp(ComputeNameSize(rootEntry), 1ull << m_address_shift);
|
2017-03-13 11:18:51 -06:00
|
|
|
u64 total_entries = rootEntry.size + 1; // The root entry itself isn't counted in rootEntry.size
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-03-13 11:18:51 -06:00
|
|
|
m_fst_name_offset = total_entries * ENTRY_SIZE; // offset of name table in FST
|
2016-12-26 03:45:22 -07:00
|
|
|
m_fst_data.resize(m_fst_name_offset + name_table_size);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2016-06-24 02:43:46 -06: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 01:38:58 -06:00
|
|
|
|
2017-06-09 10:00:25 -06:00
|
|
|
// 32 KiB aligned start of data on disk
|
2016-12-26 03:45:22 -07: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-07 22:30:24 -07:00
|
|
|
|
2016-12-26 03:45:22 -07: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-07 22:30:24 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// write root entry
|
2017-03-27 18:53:42 -06:00
|
|
|
WriteEntryData(&fst_offset, DIRECTORY_ENTRY, 0, 0, total_entries, m_address_shift);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2016-12-26 03:45:22 -07:00
|
|
|
WriteDirectory(rootEntry, &fst_offset, &name_offset, ¤t_data_address, root_offset);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-03-27 18:52:40 -06: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-07 22:30:24 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// write FST size and location
|
2016-12-26 03:45:22 -07: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-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
void DirectoryBlobReader::WriteToBuffer(u64 source_start_address, u64 source_length,
|
|
|
|
const u8* source, u64* address, u64* length,
|
|
|
|
u8** buffer) const
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
if (*length == 0)
|
2016-06-24 02:43:46 -06:00
|
|
|
return;
|
2009-01-11 15:25:57 -07:00
|
|
|
|
2016-12-26 03:45:22 -07:00
|
|
|
_dbg_assert_(DVDINTERFACE, *address >= source_start_address);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2016-12-26 03:45:22 -07:00
|
|
|
u64 source_offset = *address - source_start_address;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2016-12-26 03:45:22 -07:00
|
|
|
if (source_offset < source_length)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
size_t bytes_to_read = std::min(source_length - source_offset, *length);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2016-12-26 03:45:22 -07:00
|
|
|
memcpy(*buffer, source + source_offset, bytes_to_read);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2016-12-26 03:45:22 -07:00
|
|
|
*length -= bytes_to_read;
|
|
|
|
*buffer += bytes_to_read;
|
|
|
|
*address += bytes_to_read;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
void DirectoryBlobReader::PadToAddress(u64 start_address, u64* address, u64* length,
|
|
|
|
u8** buffer) const
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
if (start_address > *address && *length > 0)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
u64 padBytes = std::min(start_address - *address, *length);
|
|
|
|
memset(*buffer, 0, (size_t)padBytes);
|
|
|
|
*length -= padBytes;
|
|
|
|
*buffer += padBytes;
|
|
|
|
*address += padBytes;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
void DirectoryBlobReader::Write32(u32 data, u32 offset, std::vector<u8>* const buffer)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
(*buffer)[offset++] = (data >> 24);
|
|
|
|
(*buffer)[offset++] = (data >> 16) & 0xff;
|
|
|
|
(*buffer)[offset++] = (data >> 8) & 0xff;
|
|
|
|
(*buffer)[offset] = (data)&0xff;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
void DirectoryBlobReader::WriteEntryData(u32* entry_offset, u8 type, u32 name_offset,
|
|
|
|
u64 data_offset, u64 length, u32 address_shift)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
m_fst_data[(*entry_offset)++] = type;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2016-12-26 03:45:22 -07: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-07 22:30:24 -07:00
|
|
|
|
2017-03-27 18:53:42 -06:00
|
|
|
Write32((u32)(data_offset >> address_shift), *entry_offset, &m_fst_data);
|
2016-12-26 03:45:22 -07:00
|
|
|
*entry_offset += 4;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2016-12-26 03:45:22 -07:00
|
|
|
Write32((u32)length, *entry_offset, &m_fst_data);
|
|
|
|
*entry_offset += 4;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
void DirectoryBlobReader::WriteEntryName(u32* name_offset, const std::string& name)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
strncpy((char*)&m_fst_data[*name_offset + m_fst_name_offset], name.c_str(), name.length() + 1);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2016-12-26 03:45:22 -07:00
|
|
|
*name_offset += (u32)(name.length() + 1);
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
void DirectoryBlobReader::WriteDirectory(const File::FSTEntry& parent_entry, u32* fst_offset,
|
|
|
|
u32* name_offset, u64* data_offset, u32 parent_entry_index)
|
2013-10-28 23:23:17 -06:00
|
|
|
{
|
2016-12-25 14:35:38 -07:00
|
|
|
std::vector<File::FSTEntry> sorted_entries = parent_entry.children;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-01-24 10:18:26 -07:00
|
|
|
// Sort for determinism
|
|
|
|
std::sort(sorted_entries.begin(), sorted_entries.end(), [](const File::FSTEntry& one,
|
|
|
|
const File::FSTEntry& two) {
|
2017-04-15 11:53:53 -06: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 10:18:26 -07:00
|
|
|
});
|
2016-12-25 14:35:38 -07:00
|
|
|
|
|
|
|
for (const File::FSTEntry& entry : sorted_entries)
|
|
|
|
{
|
|
|
|
if (entry.isDirectory)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
u32 entry_index = *fst_offset / ENTRY_SIZE;
|
2017-03-27 18:53:42 -06:00
|
|
|
WriteEntryData(fst_offset, DIRECTORY_ENTRY, *name_offset, parent_entry_index,
|
|
|
|
entry_index + entry.size + 1, 0);
|
2016-12-26 03:45:22 -07:00
|
|
|
WriteEntryName(name_offset, entry.virtualName);
|
2016-12-25 14:35:38 -07:00
|
|
|
|
2016-12-26 03:45:22 -07:00
|
|
|
WriteDirectory(entry, fst_offset, name_offset, data_offset, entry_index);
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2016-12-25 14:35:38 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// put entry in FST
|
2017-03-27 18:53:42 -06:00
|
|
|
WriteEntryData(fst_offset, FILE_ENTRY, *name_offset, *data_offset, entry.size,
|
|
|
|
m_address_shift);
|
2016-12-26 03:45:22 -07:00
|
|
|
WriteEntryName(name_offset, entry.virtualName);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2016-12-25 14:35:38 -07:00
|
|
|
// write entry to virtual disk
|
2016-12-26 03:45:22 -07:00
|
|
|
_dbg_assert_(DVDINTERFACE, m_virtual_disk.find(*data_offset) == m_virtual_disk.end());
|
|
|
|
m_virtual_disk.emplace(*data_offset, entry.physicalName);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-09 10:00:25 -06:00
|
|
|
// 32 KiB aligned - many games are fine with less alignment, but not all
|
2016-12-26 03:45:22 -07:00
|
|
|
*data_offset = Common::AlignUp(*data_offset + std::max<u64>(entry.size, 1ull), 0x8000ull);
|
2016-12-25 14:35:38 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2016-12-26 03:45:22 -07:00
|
|
|
static u32 ComputeNameSize(const File::FSTEntry& parent_entry)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-12-26 03:45:22 -07:00
|
|
|
u32 name_size = 0;
|
|
|
|
for (const File::FSTEntry& entry : parent_entry.children)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
|
|
|
if (entry.isDirectory)
|
2016-12-26 03:45:22 -07:00
|
|
|
name_size += ComputeNameSize(entry);
|
2016-12-25 15:01:42 -07:00
|
|
|
|
2016-12-26 03:45:22 -07:00
|
|
|
name_size += (u32)entry.virtualName.length() + 1;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2016-12-26 03:45:22 -07:00
|
|
|
return name_size;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-05-01 06:08:47 -06: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 11:53:53 -06:00
|
|
|
static std::string ASCIIToUppercase(std::string str)
|
2017-01-24 10:18:26 -07:00
|
|
|
{
|
|
|
|
std::transform(str.begin(), str.end(), str.begin(),
|
2017-04-15 11:53:53 -06:00
|
|
|
[](char c) { return std::toupper(c, std::locale::classic()); });
|
2017-01-24 10:18:26 -07:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
} // namespace
|