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
|
|
|
|
2017-06-07 18:32:09 +00:00
|
|
|
#include "DiscIO/DirectoryBlob.h"
|
|
|
|
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <algorithm>
|
2017-06-07 18:32:09 +00:00
|
|
|
#include <array>
|
|
|
|
#include <cinttypes>
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
2017-01-24 17:18:26 +00:00
|
|
|
#include <locale>
|
2014-08-26 03:40:05 +00:00
|
|
|
#include <memory>
|
2017-06-08 14:07:01 +00:00
|
|
|
#include <set>
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <string>
|
2017-06-07 18:32:09 +00:00
|
|
|
#include <utility>
|
2017-06-08 14:37:51 +00:00
|
|
|
#include <variant>
|
2014-02-21 00:47:53 +00:00
|
|
|
#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"
|
2017-06-07 18:32:09 +00:00
|
|
|
#include "Common/Swap.h"
|
|
|
|
#include "Core/Boot/DolReader.h"
|
2015-09-27 12:01:12 +00:00
|
|
|
#include "DiscIO/Blob.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
2017-06-08 20:11:31 +00:00
|
|
|
static const DiscContent& AddFileToContents(std::set<DiscContent>* contents,
|
|
|
|
const std::string& path, u64 offset,
|
|
|
|
u64 max_size = UINT64_MAX);
|
|
|
|
|
2017-06-10 06:46:21 +00:00
|
|
|
// Reads as many bytes as the vector fits (or less, if the file is smaller).
|
|
|
|
// Returns the number of bytes read.
|
|
|
|
static size_t ReadFileToVector(const std::string& path, std::vector<u8>* vector);
|
|
|
|
|
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-07 19:13:24 +00:00
|
|
|
constexpr u8 ENTRY_SIZE = 0x0c;
|
|
|
|
constexpr u8 FILE_ENTRY = 0;
|
|
|
|
constexpr u8 DIRECTORY_ENTRY = 1;
|
|
|
|
constexpr u64 DISKHEADER_ADDRESS = 0;
|
2017-06-09 19:42:26 +00:00
|
|
|
constexpr u64 DISKHEADER_SIZE = 0x440;
|
2017-06-09 10:08:17 +00:00
|
|
|
constexpr u64 NONPARTITION_DISKHEADER_SIZE = 0x100;
|
2017-06-09 19:42:26 +00:00
|
|
|
constexpr u64 BI2_ADDRESS = 0x440;
|
|
|
|
constexpr u64 BI2_SIZE = 0x2000;
|
2017-06-07 19:13:24 +00:00
|
|
|
constexpr u64 APPLOADER_ADDRESS = 0x2440;
|
2017-06-10 07:53:36 +00:00
|
|
|
constexpr u64 WII_REGION_DATA_ADDRESS = 0x4E000;
|
|
|
|
constexpr u64 WII_REGION_DATA_SIZE = 0x20;
|
2017-06-07 18:32:09 +00:00
|
|
|
constexpr u64 GAME_PARTITION_ADDRESS = 0x50000;
|
2015-11-17 09:09:54 +00:00
|
|
|
|
2017-06-08 14:07:01 +00:00
|
|
|
DiscContent::DiscContent(u64 offset, u64 size, const std::string& path)
|
2017-06-08 14:37:51 +00:00
|
|
|
: m_offset(offset), m_size(size), m_content_source(path)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DiscContent::DiscContent(u64 offset, u64 size, const u8* data)
|
|
|
|
: m_offset(offset), m_size(size), m_content_source(data)
|
2017-06-08 14:07:01 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DiscContent::DiscContent(u64 offset) : m_offset(offset)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 DiscContent::GetOffset() const
|
|
|
|
{
|
|
|
|
return m_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 DiscContent::GetSize() const
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DiscContent::Read(u64* offset, u64* length, u8** buffer) const
|
|
|
|
{
|
|
|
|
if (m_size == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
_dbg_assert_(DISCIO, *offset >= m_offset);
|
|
|
|
const u64 offset_in_content = *offset - m_offset;
|
|
|
|
|
|
|
|
if (offset_in_content < m_size)
|
|
|
|
{
|
|
|
|
const u64 bytes_to_read = std::min(m_size - offset_in_content, *length);
|
|
|
|
|
2017-06-08 14:37:51 +00:00
|
|
|
if (std::holds_alternative<std::string>(m_content_source))
|
|
|
|
{
|
|
|
|
File::IOFile file(std::get<std::string>(m_content_source), "rb");
|
|
|
|
file.Seek(offset_in_content, SEEK_SET);
|
|
|
|
if (!file.ReadBytes(*buffer, bytes_to_read))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const u8* const content_pointer = std::get<const u8*>(m_content_source) + offset_in_content;
|
|
|
|
std::copy(content_pointer, content_pointer + bytes_to_read, *buffer);
|
|
|
|
}
|
2017-06-08 14:07:01 +00:00
|
|
|
|
|
|
|
*length -= bytes_to_read;
|
|
|
|
*buffer += bytes_to_read;
|
|
|
|
*offset += bytes_to_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-07 18:32:09 +00:00
|
|
|
static bool PathCharactersEqual(char a, char b)
|
|
|
|
{
|
|
|
|
return a == b
|
|
|
|
#ifdef _WIN32
|
|
|
|
|| (a == '/' && b == '\\') || (a == '\\' && b == '/')
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2017-06-07 18:32:09 +00:00
|
|
|
static bool PathEndsWith(const std::string& path, const std::string& suffix)
|
|
|
|
{
|
|
|
|
if (suffix.size() > path.size())
|
|
|
|
return false;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2017-06-07 18:32:09 +00: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 09:16:10 +00:00
|
|
|
|
2017-06-07 18:32:09 +00:00
|
|
|
return true;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 10:56:36 +00:00
|
|
|
static bool IsValidDirectoryBlob(const std::string& dol_path, std::string* root_directory)
|
2017-06-09 10:08:17 +00:00
|
|
|
{
|
|
|
|
if (!PathEndsWith(dol_path, "/sys/main.dol"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const size_t chars_to_remove = std::string("sys/main.dol").size();
|
|
|
|
*root_directory = dol_path.substr(0, dol_path.size() - chars_to_remove);
|
|
|
|
|
|
|
|
return File::GetSize(*root_directory + "sys/boot.bin") >= 0x20;
|
|
|
|
}
|
|
|
|
|
2017-06-09 10:45:34 +00:00
|
|
|
std::unique_ptr<DirectoryBlobReader> DirectoryBlobReader::Create(const std::string& dol_path)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2017-06-09 10:08:17 +00:00
|
|
|
std::string root_directory;
|
2017-06-09 10:45:34 +00:00
|
|
|
if (!IsValidDirectoryBlob(dol_path, &root_directory))
|
2017-06-07 18:32:09 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2017-06-09 10:45:34 +00:00
|
|
|
return std::unique_ptr<DirectoryBlobReader>(new DirectoryBlobReader(root_directory));
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 10:45:34 +00:00
|
|
|
DirectoryBlobReader::DirectoryBlobReader(const std::string& root_directory)
|
2017-06-07 18:32:09 +00:00
|
|
|
: m_root_directory(root_directory), m_data_start_address(UINT64_MAX),
|
2017-06-09 19:42:26 +00:00
|
|
|
m_disk_header(DISKHEADER_SIZE), m_fst_address(0), m_dol_address(0)
|
2009-02-22 13:59:06 +00:00
|
|
|
{
|
2017-06-09 10:08:17 +00:00
|
|
|
SetDiscHeaderAndDiscType();
|
2015-06-13 10:51:24 +00:00
|
|
|
|
2017-06-10 08:37:06 +00:00
|
|
|
AddFileToContents(&m_virtual_disc, m_root_directory + "sys/bi2.bin", BI2_ADDRESS, BI2_SIZE);
|
|
|
|
|
2017-06-07 18:32:09 +00:00
|
|
|
// Setting the DOL relies on m_dol_address, which is set by SetApploader
|
|
|
|
if (SetApploader(m_root_directory + "sys/apploader.img"))
|
2017-06-09 10:45:34 +00:00
|
|
|
SetDOL();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-06-07 18:32:09 +00:00
|
|
|
BuildFST();
|
2017-06-08 16:30:05 +00:00
|
|
|
|
2017-06-08 16:43:52 +00:00
|
|
|
if (m_is_wii)
|
|
|
|
{
|
2017-06-10 08:41:49 +00:00
|
|
|
SetPartitionTable();
|
2017-06-10 07:53:36 +00:00
|
|
|
SetWiiRegionData();
|
2017-06-10 08:38:59 +00:00
|
|
|
SetTMDAndTicket();
|
2017-06-08 16:43:52 +00:00
|
|
|
}
|
2017-06-07 18:32:09 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-06-08 16:43:52 +00:00
|
|
|
bool DirectoryBlobReader::ReadInternal(u64 offset, u64 length, u8* buffer,
|
|
|
|
const std::set<DiscContent>& contents)
|
2017-06-07 18:32:09 +00:00
|
|
|
{
|
2017-06-08 16:43:52 +00:00
|
|
|
if (contents.empty())
|
2016-06-24 08:43:46 +00:00
|
|
|
return true;
|
|
|
|
|
2017-06-08 14:07:01 +00:00
|
|
|
// Determine which DiscContent the offset refers to
|
2017-06-08 16:43:52 +00:00
|
|
|
std::set<DiscContent>::const_iterator it = contents.lower_bound(DiscContent(offset));
|
|
|
|
if (it->GetOffset() > offset && it != contents.begin())
|
2017-06-08 14:07:01 +00:00
|
|
|
--it;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// zero fill to start of file data
|
2017-06-08 14:07:01 +00:00
|
|
|
PadToAddress(it->GetOffset(), &offset, &length, &buffer);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-06-08 16:43:52 +00:00
|
|
|
while (it != contents.end() && length > 0)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-06-08 16:56:09 +00:00
|
|
|
_dbg_assert_(DISCIO, it->GetOffset() <= offset);
|
2017-06-08 14:07:01 +00:00
|
|
|
if (!it->Read(&offset, &length, &buffer))
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
|
|
|
|
2017-06-08 14:07:01 +00:00
|
|
|
++it;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-06-08 16:43:52 +00:00
|
|
|
if (it != contents.end())
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-06-08 16:56:09 +00:00
|
|
|
_dbg_assert_(DISCIO, it->GetOffset() >= offset);
|
2017-06-08 14:07:01 +00:00
|
|
|
PadToAddress(it->GetOffset(), &offset, &length, &buffer);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 18:32:09 +00:00
|
|
|
bool DirectoryBlobReader::Read(u64 offset, u64 length, u8* buffer)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2017-06-08 20:11:31 +00:00
|
|
|
// TODO: We don't handle raw access to the encrypted area of Wii discs correctly.
|
|
|
|
|
2017-06-08 16:43:52 +00:00
|
|
|
return ReadInternal(offset, length, buffer, m_is_wii ? m_nonpartition_contents : m_virtual_disc);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 18:32:09 +00:00
|
|
|
bool DirectoryBlobReader::SupportsReadWiiDecrypted() const
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2017-06-07 18:32:09 +00:00
|
|
|
return m_is_wii;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 18:32:09 +00:00
|
|
|
bool DirectoryBlobReader::ReadWiiDecrypted(u64 offset, u64 size, u8* buffer, u64 partition_offset)
|
2015-04-10 20:10:49 +00:00
|
|
|
{
|
2017-06-07 18:32:09 +00:00
|
|
|
if (!m_is_wii || partition_offset != GAME_PARTITION_ADDRESS)
|
|
|
|
return false;
|
2015-04-10 20:10:49 +00:00
|
|
|
|
2017-06-08 16:43:52 +00:00
|
|
|
return ReadInternal(offset, size, buffer, m_virtual_disc);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 18:32:09 +00:00
|
|
|
BlobType DirectoryBlobReader::GetBlobType() const
|
2015-09-26 13:24:29 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return BlobType::DIRECTORY;
|
2015-09-26 13:24:29 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 18:32:09 +00:00
|
|
|
u64 DirectoryBlobReader::GetRawSize() 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-07 18:32:09 +00:00
|
|
|
u64 DirectoryBlobReader::GetDataSize() 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-09 10:08:17 +00:00
|
|
|
void DirectoryBlobReader::SetDiscHeaderAndDiscType()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2017-06-09 10:08:17 +00:00
|
|
|
const std::string boot_bin_path = m_root_directory + "sys/boot.bin";
|
2017-06-10 06:46:21 +00:00
|
|
|
if (ReadFileToVector(boot_bin_path, &m_disk_header) < 0x20)
|
|
|
|
ERROR_LOG(DISCIO, "%s doesn't exist or is too small", boot_bin_path.c_str());
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2017-06-10 08:37:06 +00:00
|
|
|
m_virtual_disc.emplace(DISKHEADER_ADDRESS, DISKHEADER_SIZE, m_disk_header.data());
|
|
|
|
|
2017-06-09 10:08:17 +00:00
|
|
|
m_is_wii = Common::swap32(&m_disk_header[0x18]) == 0x5d1c9ea3;
|
|
|
|
const bool is_gc = Common::swap32(&m_disk_header[0x1c]) == 0xc2339f3d;
|
|
|
|
if (m_is_wii == is_gc)
|
|
|
|
ERROR_LOG(DISCIO, "Couldn't detect disc type based on %s", boot_bin_path.c_str());
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2017-06-09 10:08:17 +00:00
|
|
|
m_address_shift = m_is_wii ? 2 : 0;
|
|
|
|
|
|
|
|
if (m_is_wii)
|
|
|
|
{
|
|
|
|
m_disk_header_nonpartition.resize(NONPARTITION_DISKHEADER_SIZE);
|
2017-06-10 06:46:21 +00:00
|
|
|
const size_t header_bin_bytes_read =
|
|
|
|
ReadFileToVector(m_root_directory + "disc/header.bin", &m_disk_header_nonpartition);
|
2017-06-09 10:08:17 +00:00
|
|
|
|
|
|
|
// If header.bin is missing or smaller than expected, use the content of sys/boot.bin instead
|
|
|
|
std::copy(m_disk_header.data() + header_bin_bytes_read,
|
|
|
|
m_disk_header.data() + m_disk_header_nonpartition.size(),
|
|
|
|
m_disk_header_nonpartition.data() + header_bin_bytes_read);
|
|
|
|
|
|
|
|
// 0x60 and 0x61 are the only differences between the partition and non-partition headers
|
|
|
|
if (header_bin_bytes_read < 0x60)
|
|
|
|
m_disk_header_nonpartition[0x60] = 0;
|
|
|
|
if (header_bin_bytes_read < 0x61)
|
|
|
|
m_disk_header_nonpartition[0x61] = 0;
|
2017-06-10 08:37:06 +00:00
|
|
|
|
|
|
|
m_nonpartition_contents.emplace(DISKHEADER_ADDRESS, NONPARTITION_DISKHEADER_SIZE,
|
|
|
|
m_disk_header_nonpartition.data());
|
2017-06-09 10:08:17 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-10 08:41:49 +00:00
|
|
|
void DirectoryBlobReader::SetPartitionTable()
|
|
|
|
{
|
|
|
|
constexpr u64 PARTITION_TABLE_ADDRESS = 0x40000;
|
|
|
|
static 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}};
|
|
|
|
|
|
|
|
m_nonpartition_contents.emplace(PARTITION_TABLE_ADDRESS, PARTITION_TABLE.size() * sizeof(u32),
|
|
|
|
reinterpret_cast<const u8*>(PARTITION_TABLE.data()));
|
|
|
|
}
|
|
|
|
|
2017-06-10 07:53:36 +00:00
|
|
|
void DirectoryBlobReader::SetWiiRegionData()
|
|
|
|
{
|
|
|
|
m_wii_region_data.resize(0x10, 0x00);
|
|
|
|
m_wii_region_data.resize(0x20, 0x80);
|
|
|
|
|
|
|
|
// 0xFF is an arbitrarily picked value. Note that we can't use 0x00, because that means NTSC-J
|
|
|
|
constexpr u32 INVALID_REGION = 0xFF;
|
|
|
|
Write32(INVALID_REGION, 0, &m_wii_region_data);
|
|
|
|
|
|
|
|
const std::string region_bin_path = m_root_directory + "disc/region.bin";
|
|
|
|
const size_t bytes_read = ReadFileToVector(region_bin_path, &m_wii_region_data);
|
|
|
|
if (bytes_read < 0x4)
|
|
|
|
ERROR_LOG(DISCIO, "Couldn't read region from %s", region_bin_path.c_str());
|
|
|
|
else if (bytes_read < 0x20)
|
|
|
|
ERROR_LOG(DISCIO, "Couldn't read age ratings from %s", region_bin_path.c_str());
|
2017-06-10 08:37:06 +00:00
|
|
|
|
|
|
|
m_nonpartition_contents.emplace(WII_REGION_DATA_ADDRESS, WII_REGION_DATA_SIZE,
|
|
|
|
m_wii_region_data.data());
|
2017-06-10 07:53:36 +00:00
|
|
|
}
|
|
|
|
|
2017-06-10 08:38:59 +00:00
|
|
|
void DirectoryBlobReader::SetTMDAndTicket()
|
|
|
|
{
|
|
|
|
constexpr u32 TICKET_OFFSET = 0x0;
|
|
|
|
constexpr u32 TICKET_SIZE = 0x2a4;
|
|
|
|
constexpr u32 TMD_OFFSET = 0x2c0;
|
|
|
|
constexpr u32 MAX_TMD_SIZE = 0x49e4;
|
|
|
|
AddFileToContents(&m_nonpartition_contents, m_root_directory + "ticket.bin",
|
|
|
|
GAME_PARTITION_ADDRESS + TICKET_OFFSET, TICKET_SIZE);
|
|
|
|
const DiscContent& tmd = AddFileToContents(&m_nonpartition_contents, m_root_directory + "tmd.bin",
|
|
|
|
GAME_PARTITION_ADDRESS + TMD_OFFSET, MAX_TMD_SIZE);
|
|
|
|
m_tmd_header = {Common::swap32(static_cast<u32>(tmd.GetSize())),
|
|
|
|
Common::swap32(TMD_OFFSET >> m_address_shift)};
|
|
|
|
m_nonpartition_contents.emplace(GAME_PARTITION_ADDRESS + TICKET_SIZE, sizeof(m_tmd_header),
|
|
|
|
reinterpret_cast<const u8*>(&m_tmd_header));
|
|
|
|
}
|
|
|
|
|
2017-06-07 18:32:09 +00:00
|
|
|
bool DirectoryBlobReader::SetApploader(const std::string& apploader)
|
2009-12-10 09:16:10 +00:00
|
|
|
{
|
2017-06-10 08:27:46 +00:00
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
if (apploader.empty())
|
|
|
|
{
|
|
|
|
m_apploader.resize(0x20);
|
|
|
|
// Make sure BS2 HLE doesn't try to run the apploader
|
|
|
|
Write32(static_cast<u32>(-1), 0x10, &m_apploader);
|
|
|
|
}
|
|
|
|
else
|
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");
|
|
|
|
}
|
2017-06-10 08:27:46 +00:00
|
|
|
else
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-06-10 08:27:46 +00:00
|
|
|
const size_t apploader_size = 0x20 + Common::swap32(*(u32*)&data.data()[0x14]) +
|
|
|
|
Common::swap32(*(u32*)&data.data()[0x18]);
|
|
|
|
if (apploader_size != data.size())
|
|
|
|
{
|
|
|
|
PanicAlertT("Apploader is the wrong size...is it really an apploader?");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_apploader.resize(apploader_size);
|
|
|
|
std::copy(data.begin(), data.end(), m_apploader.begin());
|
|
|
|
|
|
|
|
// 32byte aligned (plus 0x20 padding)
|
|
|
|
m_dol_address = Common::AlignUp(APPLOADER_ADDRESS + m_apploader.size() + 0x20, 0x20ull);
|
|
|
|
success = true;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-10 08:27:46 +00:00
|
|
|
|
2017-06-10 08:37:06 +00:00
|
|
|
m_virtual_disc.emplace(APPLOADER_ADDRESS, m_apploader.size(), m_apploader.data());
|
2017-06-10 08:27:46 +00:00
|
|
|
return success;
|
2009-12-10 09:16:10 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 10:45:34 +00:00
|
|
|
void DirectoryBlobReader::SetDOL()
|
2009-12-10 09:16:10 +00:00
|
|
|
{
|
2017-06-09 10:45:34 +00:00
|
|
|
const DiscContent& dol =
|
|
|
|
AddFileToContents(&m_virtual_disc, m_root_directory + "sys/main.dol", m_dol_address);
|
2009-12-10 09:16:10 +00:00
|
|
|
|
2017-06-07 18:32:09 +00:00
|
|
|
Write32((u32)(m_dol_address >> m_address_shift), 0x0420, &m_disk_header);
|
|
|
|
|
|
|
|
// 32byte aligned (plus 0x20 padding)
|
2017-06-09 10:45:34 +00:00
|
|
|
m_fst_address = Common::AlignUp(m_dol_address + dol.GetSize() + 0x20, 0x20ull);
|
2009-12-10 09:16:10 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 18:32:09 +00:00
|
|
|
void DirectoryBlobReader::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
|
|
|
|
2017-06-07 18:32:09 +00:00
|
|
|
File::FSTEntry rootEntry = File::ScanDirectoryTree(m_root_directory + "files/", 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);
|
2017-06-10 08:37:06 +00:00
|
|
|
|
|
|
|
m_virtual_disc.emplace(m_fst_address, m_fst_data.size(), m_fst_data.data());
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 18:32:09 +00:00
|
|
|
void DirectoryBlobReader::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-07 18:32:09 +00:00
|
|
|
void DirectoryBlobReader::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-07 18:32:09 +00:00
|
|
|
void DirectoryBlobReader::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-07 18:32:09 +00:00
|
|
|
void DirectoryBlobReader::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-07 18:32:09 +00:00
|
|
|
void DirectoryBlobReader::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
|
|
|
|
2017-06-08 14:07:01 +00:00
|
|
|
// write entry to virtual disc
|
|
|
|
auto result = m_virtual_disc.emplace(*data_offset, entry.size, entry.physicalName);
|
|
|
|
_dbg_assert_(DISCIO, result.second); // Check that this offset wasn't already occupied
|
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
|
|
|
}
|
|
|
|
|
2017-06-08 20:11:31 +00:00
|
|
|
static const DiscContent& AddFileToContents(std::set<DiscContent>* contents,
|
|
|
|
const std::string& path, u64 offset, u64 max_size)
|
|
|
|
{
|
|
|
|
return *(contents->emplace(offset, std::min(File::GetSize(path), max_size), path).first);
|
|
|
|
}
|
|
|
|
|
2017-06-10 06:46:21 +00:00
|
|
|
static size_t ReadFileToVector(const std::string& path, std::vector<u8>* vector)
|
|
|
|
{
|
|
|
|
File::IOFile file(path, "rb");
|
|
|
|
size_t bytes_read;
|
|
|
|
file.ReadArray<u8>(vector->data(), std::min<u64>(file.GetSize(), vector->size()), &bytes_read);
|
|
|
|
return bytes_read;
|
|
|
|
}
|
|
|
|
|
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
|