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
|
|
|
|
2013-03-03 22:51:26 +00:00
|
|
|
#include <algorithm>
|
2013-10-26 09:55:41 +00:00
|
|
|
#include <cinttypes>
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
2015-08-09 11:41:41 +00:00
|
|
|
#include <map>
|
2015-08-08 17:59:33 +00:00
|
|
|
#include <memory>
|
2017-06-04 08:33:14 +00:00
|
|
|
#include <optional>
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-05-22 08:39:36 +00:00
|
|
|
#include "Common/CommonFuncs.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"
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2015-09-26 21:13:07 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
#include "DiscIO/FileSystemGCWii.h"
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "DiscIO/Filesystem.h"
|
2014-02-21 00:47:53 +00:00
|
|
|
#include "DiscIO/Volume.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
2015-08-02 15:39:03 +00:00
|
|
|
constexpr u32 FST_ENTRY_SIZE = 4 * 3; // An FST entry consists of three 32-bit integers
|
|
|
|
|
2015-08-08 17:59:33 +00:00
|
|
|
// Set everything manually.
|
2015-08-02 15:39:03 +00:00
|
|
|
FileInfoGCWii::FileInfoGCWii(const u8* fst, u8 offset_shift, u32 index, u32 total_file_infos)
|
|
|
|
: m_fst(fst), m_offset_shift(offset_shift), m_index(index), m_total_file_infos(total_file_infos)
|
2015-07-28 14:56:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-08-08 17:59:33 +00:00
|
|
|
// For the root object only.
|
|
|
|
// m_fst and m_index must be correctly set before GetSize() is called!
|
|
|
|
FileInfoGCWii::FileInfoGCWii(const u8* fst, u8 offset_shift)
|
|
|
|
: m_fst(fst), m_offset_shift(offset_shift), m_index(0), m_total_file_infos(GetSize())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy data that is common to the whole file system.
|
|
|
|
FileInfoGCWii::FileInfoGCWii(const FileInfoGCWii& file_info, u32 index)
|
|
|
|
: FileInfoGCWii(file_info.m_fst, file_info.m_offset_shift, index, file_info.m_total_file_infos)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-06-13 21:05:30 +00:00
|
|
|
FileInfoGCWii::~FileInfoGCWii() = default;
|
2015-07-28 14:56:25 +00:00
|
|
|
|
2015-08-08 17:59:33 +00:00
|
|
|
uintptr_t FileInfoGCWii::GetAddress() const
|
|
|
|
{
|
|
|
|
return reinterpret_cast<uintptr_t>(m_fst + FST_ENTRY_SIZE * m_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 FileInfoGCWii::GetNextIndex() const
|
|
|
|
{
|
|
|
|
return IsDirectory() ? GetSize() : m_index + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileInfo& FileInfoGCWii::operator++()
|
|
|
|
{
|
|
|
|
m_index = GetNextIndex();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<FileInfo> FileInfoGCWii::clone() const
|
|
|
|
{
|
|
|
|
return std::make_unique<FileInfoGCWii>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileInfo::const_iterator FileInfoGCWii::begin() const
|
|
|
|
{
|
|
|
|
return const_iterator(std::make_unique<FileInfoGCWii>(*this, m_index + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
FileInfo::const_iterator FileInfoGCWii::end() const
|
|
|
|
{
|
|
|
|
return const_iterator(std::make_unique<FileInfoGCWii>(*this, GetNextIndex()));
|
|
|
|
}
|
|
|
|
|
2015-07-30 20:18:20 +00:00
|
|
|
u32 FileInfoGCWii::Get(EntryProperty entry_property) const
|
|
|
|
{
|
2015-08-02 15:39:03 +00:00
|
|
|
return Common::swap32(m_fst + FST_ENTRY_SIZE * m_index +
|
|
|
|
sizeof(u32) * static_cast<int>(entry_property));
|
2015-07-30 20:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 FileInfoGCWii::GetSize() const
|
|
|
|
{
|
2015-10-20 12:32:04 +00:00
|
|
|
return Get(EntryProperty::FILE_SIZE);
|
2015-07-30 20:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 FileInfoGCWii::GetOffset() const
|
|
|
|
{
|
2015-08-08 17:59:33 +00:00
|
|
|
return static_cast<u64>(Get(EntryProperty::FILE_OFFSET)) << m_offset_shift;
|
2015-07-30 20:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileInfoGCWii::IsDirectory() const
|
|
|
|
{
|
|
|
|
return (Get(EntryProperty::NAME_OFFSET) & 0xFF000000) != 0;
|
|
|
|
}
|
|
|
|
|
2015-08-08 17:59:33 +00:00
|
|
|
u32 FileInfoGCWii::GetTotalChildren() const
|
|
|
|
{
|
2015-10-20 12:32:04 +00:00
|
|
|
return Get(EntryProperty::FILE_SIZE) - (m_index + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 FileInfoGCWii::GetNameOffset() const
|
|
|
|
{
|
|
|
|
return static_cast<u64>(FST_ENTRY_SIZE) * m_total_file_infos +
|
|
|
|
(Get(EntryProperty::NAME_OFFSET) & 0xFFFFFF);
|
2015-08-08 17:59:33 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 20:18:20 +00:00
|
|
|
std::string FileInfoGCWii::GetName() const
|
|
|
|
{
|
|
|
|
// TODO: Should we really always use SHIFT-JIS?
|
|
|
|
// Some names in Pikmin (NTSC-U) don't make sense without it, but is it correct?
|
2015-10-20 12:32:04 +00:00
|
|
|
return SHIFTJISToUTF8(reinterpret_cast<const char*>(m_fst + GetNameOffset()));
|
2015-07-30 20:18:20 +00:00
|
|
|
}
|
|
|
|
|
2015-08-08 17:59:33 +00:00
|
|
|
std::string FileInfoGCWii::GetPath() const
|
|
|
|
{
|
|
|
|
// The root entry doesn't have a name
|
|
|
|
if (m_index == 0)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
if (IsDirectory())
|
|
|
|
{
|
|
|
|
u32 parent_directory_index = Get(EntryProperty::FILE_OFFSET);
|
|
|
|
return FileInfoGCWii(*this, parent_directory_index).GetPath() + GetName() + "/";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The parent directory can be found by searching backwards
|
2015-10-20 12:32:04 +00:00
|
|
|
// for a directory that contains this file. The search cannot fail,
|
|
|
|
// because the root directory at index 0 contains all files.
|
2015-08-08 17:59:33 +00:00
|
|
|
FileInfoGCWii potential_parent(*this, m_index - 1);
|
2015-10-20 12:32:04 +00:00
|
|
|
while (!(potential_parent.IsDirectory() &&
|
|
|
|
potential_parent.Get(EntryProperty::FILE_SIZE) > m_index))
|
2015-08-08 17:59:33 +00:00
|
|
|
{
|
|
|
|
potential_parent = FileInfoGCWii(*this, potential_parent.m_index - 1);
|
|
|
|
}
|
|
|
|
return potential_parent.GetPath() + GetName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 12:32:04 +00:00
|
|
|
bool FileInfoGCWii::IsValid(u64 fst_size, const FileInfoGCWii& parent_directory) const
|
|
|
|
{
|
|
|
|
if (GetNameOffset() >= fst_size)
|
|
|
|
{
|
|
|
|
ERROR_LOG(DISCIO, "Impossibly large name offset in file system");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsDirectory())
|
|
|
|
{
|
|
|
|
if (Get(EntryProperty::FILE_OFFSET) != parent_directory.m_index)
|
|
|
|
{
|
|
|
|
ERROR_LOG(DISCIO, "Incorrect parent offset in file system");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 size = Get(EntryProperty::FILE_SIZE);
|
|
|
|
|
|
|
|
if (size <= m_index)
|
|
|
|
{
|
|
|
|
ERROR_LOG(DISCIO, "Impossibly small directory size in file system");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size > parent_directory.Get(EntryProperty::FILE_SIZE))
|
|
|
|
{
|
|
|
|
ERROR_LOG(DISCIO, "Impossibly large directory size in file system");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const FileInfo& child : *this)
|
|
|
|
{
|
|
|
|
if (!static_cast<const FileInfoGCWii&>(child).IsValid(fst_size, *this))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-09 18:53:38 +00:00
|
|
|
FileSystemGCWii::FileSystemGCWii(const Volume* volume, const Partition& partition)
|
|
|
|
: FileSystem(volume, partition), m_valid(false), m_offset_shift(0), m_root(nullptr, 0, 0, 0)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2015-07-31 11:36:28 +00:00
|
|
|
// Check if this is a GameCube or Wii disc
|
2015-08-09 18:53:38 +00:00
|
|
|
if (m_volume->ReadSwapped<u32>(0x18, m_partition) == u32(0x5D1C9EA3))
|
2015-07-31 11:36:28 +00:00
|
|
|
m_offset_shift = 2; // Wii file system
|
2015-08-09 18:53:38 +00:00
|
|
|
else if (m_volume->ReadSwapped<u32>(0x1c, m_partition) == u32(0xC2339F3D))
|
2015-07-31 11:36:28 +00:00
|
|
|
m_offset_shift = 0; // GameCube file system
|
2015-07-31 11:56:29 +00:00
|
|
|
else
|
2015-07-31 11:36:28 +00:00
|
|
|
return;
|
|
|
|
|
2015-08-09 18:53:38 +00:00
|
|
|
const std::optional<u32> fst_offset_unshifted = m_volume->ReadSwapped<u32>(0x424, m_partition);
|
|
|
|
const std::optional<u32> fst_size_unshifted = m_volume->ReadSwapped<u32>(0x428, m_partition);
|
2015-07-31 11:36:28 +00:00
|
|
|
if (!fst_offset_unshifted || !fst_size_unshifted)
|
|
|
|
return;
|
|
|
|
const u64 fst_offset = static_cast<u64>(*fst_offset_unshifted) << m_offset_shift;
|
|
|
|
const u64 fst_size = static_cast<u64>(*fst_size_unshifted) << m_offset_shift;
|
2015-08-02 15:39:03 +00:00
|
|
|
if (fst_size < FST_ENTRY_SIZE)
|
2015-08-08 17:59:33 +00:00
|
|
|
{
|
|
|
|
ERROR_LOG(DISCIO, "File system is too small");
|
2015-07-31 11:36:28 +00:00
|
|
|
return;
|
2015-08-08 17:59:33 +00:00
|
|
|
}
|
2015-07-31 11:36:28 +00:00
|
|
|
|
|
|
|
// 128 MiB is more than the total amount of RAM in a Wii.
|
|
|
|
// No file system should use anywhere near that much.
|
|
|
|
static const u32 ARBITRARY_FILE_SYSTEM_SIZE_LIMIT = 128 * 1024 * 1024;
|
|
|
|
if (fst_size > ARBITRARY_FILE_SYSTEM_SIZE_LIMIT)
|
|
|
|
{
|
|
|
|
// Without this check, Dolphin can crash by trying to allocate too much
|
|
|
|
// memory when loading a disc image with an incorrect FST size.
|
|
|
|
|
|
|
|
ERROR_LOG(DISCIO, "File system is abnormally large! Aborting loading");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the whole FST
|
|
|
|
m_file_system_table.resize(fst_size);
|
2015-08-09 18:53:38 +00:00
|
|
|
if (!m_volume->Read(fst_offset, fst_size, m_file_system_table.data(), m_partition))
|
2015-08-08 17:59:33 +00:00
|
|
|
{
|
|
|
|
ERROR_LOG(DISCIO, "Couldn't read file system table");
|
2015-07-31 11:36:28 +00:00
|
|
|
return;
|
2015-08-08 17:59:33 +00:00
|
|
|
}
|
2015-07-31 11:36:28 +00:00
|
|
|
|
2015-08-08 17:59:33 +00:00
|
|
|
// Create the root object
|
|
|
|
m_root = FileInfoGCWii(m_file_system_table.data(), m_offset_shift);
|
|
|
|
if (!m_root.IsDirectory())
|
|
|
|
{
|
|
|
|
ERROR_LOG(DISCIO, "File system root is not a directory");
|
2015-07-31 11:36:28 +00:00
|
|
|
return;
|
2015-08-08 17:59:33 +00:00
|
|
|
}
|
2015-07-31 11:56:29 +00:00
|
|
|
|
2015-10-20 12:32:04 +00:00
|
|
|
if (FST_ENTRY_SIZE * m_root.GetSize() > fst_size)
|
|
|
|
{
|
|
|
|
ERROR_LOG(DISCIO, "File system has too many entries for its size");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the FST's final byte isn't 0, CFileInfoGCWii::GetName() can read past the end
|
|
|
|
if (m_file_system_table[fst_size - 1] != 0)
|
|
|
|
{
|
|
|
|
ERROR_LOG(DISCIO, "File system does not end with a null byte");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-09 18:53:38 +00:00
|
|
|
m_valid = m_root.IsValid(fst_size, m_root);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 21:05:30 +00:00
|
|
|
FileSystemGCWii::~FileSystemGCWii() = default;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2015-08-08 17:59:33 +00:00
|
|
|
const FileInfo& FileSystemGCWii::GetRoot() const
|
2015-07-29 15:27:43 +00:00
|
|
|
{
|
2015-08-08 17:59:33 +00:00
|
|
|
return m_root;
|
2015-07-29 15:27:43 +00:00
|
|
|
}
|
|
|
|
|
2015-08-08 17:59:33 +00:00
|
|
|
std::unique_ptr<FileInfo> FileSystemGCWii::FindFileInfo(const std::string& path) const
|
2015-07-29 15:27:43 +00:00
|
|
|
{
|
2015-08-08 17:59:33 +00:00
|
|
|
if (!IsValid())
|
2015-07-30 20:18:20 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2015-08-08 17:59:33 +00:00
|
|
|
return FindFileInfo(path, m_root);
|
2015-07-30 15:12:44 +00:00
|
|
|
}
|
|
|
|
|
2015-08-08 17:59:33 +00:00
|
|
|
std::unique_ptr<FileInfo> FileSystemGCWii::FindFileInfo(const std::string& path,
|
|
|
|
const FileInfo& file_info) const
|
2015-07-30 15:12:44 +00:00
|
|
|
{
|
|
|
|
// Given a path like "directory1/directory2/fileA.bin", this function will
|
|
|
|
// find directory1 and then call itself to search for "directory2/fileA.bin".
|
|
|
|
|
|
|
|
if (path.empty() || path == "/")
|
2015-08-08 17:59:33 +00:00
|
|
|
return file_info.clone();
|
2015-07-30 15:12:44 +00:00
|
|
|
|
|
|
|
// It's only possible to search in directories. Searching in a file is an error
|
2015-08-08 17:59:33 +00:00
|
|
|
if (!file_info.IsDirectory())
|
2015-07-30 15:12:44 +00:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
size_t first_dir_separator = path.find('/');
|
|
|
|
const std::string searching_for = path.substr(0, first_dir_separator);
|
|
|
|
const std::string rest_of_path =
|
|
|
|
(first_dir_separator != std::string::npos) ? path.substr(first_dir_separator + 1) : "";
|
|
|
|
|
2015-08-08 17:59:33 +00:00
|
|
|
for (const FileInfo& child : file_info)
|
2015-07-29 15:27:43 +00:00
|
|
|
{
|
2017-06-24 12:06:31 +00:00
|
|
|
if (!strcasecmp(child.GetName().c_str(), searching_for.c_str()))
|
2015-07-30 15:12:44 +00:00
|
|
|
{
|
|
|
|
// A match is found. The rest of the path is passed on to finish the search.
|
2015-08-08 17:59:33 +00:00
|
|
|
std::unique_ptr<FileInfo> result = FindFileInfo(rest_of_path, child);
|
2015-07-30 15:12:44 +00:00
|
|
|
|
|
|
|
// If the search wasn't successful, the loop continues, just in case there's a second
|
|
|
|
// file info that matches searching_for (which probably won't happen in practice)
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
}
|
2015-07-29 15:27:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-08-08 17:59:33 +00:00
|
|
|
std::unique_ptr<FileInfo> FileSystemGCWii::FindFileInfo(u64 disc_offset) const
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2015-08-08 17:59:33 +00:00
|
|
|
if (!IsValid())
|
|
|
|
return nullptr;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2015-08-09 11:41:41 +00:00
|
|
|
// Build a cache (unless there already is one)
|
|
|
|
if (m_offset_file_info_cache.empty())
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2015-08-09 11:41:41 +00:00
|
|
|
u32 fst_entries = m_root.GetSize();
|
|
|
|
for (u32 i = 0; i < fst_entries; i++)
|
2015-07-29 14:42:29 +00:00
|
|
|
{
|
2015-08-09 11:41:41 +00:00
|
|
|
FileInfoGCWii file_info(m_root, i);
|
|
|
|
if (!file_info.IsDirectory())
|
|
|
|
m_offset_file_info_cache.emplace(file_info.GetOffset() + file_info.GetSize(), i);
|
2015-07-29 14:42:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-09 11:41:41 +00:00
|
|
|
// Get the first file that ends after disc_offset
|
|
|
|
const auto it = m_offset_file_info_cache.upper_bound(disc_offset);
|
|
|
|
if (it == m_offset_file_info_cache.end())
|
|
|
|
return nullptr;
|
|
|
|
std::unique_ptr<FileInfo> result(std::make_unique<FileInfoGCWii>(m_root, it->second));
|
|
|
|
|
|
|
|
// If the file's start isn't after disc_offset, success
|
|
|
|
if (result->GetOffset() <= disc_offset)
|
|
|
|
return result;
|
|
|
|
|
2015-08-08 17:59:33 +00:00
|
|
|
return nullptr;
|
2015-07-29 14:42:29 +00:00
|
|
|
}
|
|
|
|
|
2015-08-09 18:53:38 +00:00
|
|
|
u64 FileSystemGCWii::ReadFile(const FileInfo* file_info, u8* buffer, u64 max_buffer_size,
|
|
|
|
u64 offset_in_file) const
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2015-07-30 13:06:23 +00:00
|
|
|
if (!file_info || file_info->IsDirectory())
|
2016-06-24 08:43:46 +00:00
|
|
|
return 0;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2015-08-09 18:53:38 +00:00
|
|
|
if (offset_in_file >= file_info->GetSize())
|
2016-06-24 08:43:46 +00:00
|
|
|
return 0;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2015-08-09 18:53:38 +00:00
|
|
|
u64 read_length = std::min(max_buffer_size, file_info->GetSize() - offset_in_file);
|
2010-01-11 05:07:56 +00:00
|
|
|
|
2017-06-07 20:40:51 +00:00
|
|
|
DEBUG_LOG(DISCIO, "Reading %" PRIx64 " bytes at %" PRIx64 " from file %s. Offset: %" PRIx64
|
2015-07-30 20:18:20 +00:00
|
|
|
" Size: %" PRIx32,
|
2015-08-09 18:53:38 +00:00
|
|
|
read_length, offset_in_file, file_info->GetPath().c_str(), file_info->GetOffset(),
|
2015-08-08 17:59:33 +00:00
|
|
|
file_info->GetSize());
|
2015-04-28 14:47:03 +00:00
|
|
|
|
2015-08-09 18:53:38 +00:00
|
|
|
m_volume->Read(file_info->GetOffset() + offset_in_file, read_length, buffer, m_partition);
|
2016-06-24 08:43:46 +00:00
|
|
|
return read_length;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 11:36:28 +00:00
|
|
|
bool FileSystemGCWii::ExportFile(const FileInfo* file_info,
|
2015-08-09 18:53:38 +00:00
|
|
|
const std::string& export_filename) const
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2015-07-30 13:06:23 +00:00
|
|
|
if (!file_info || file_info->IsDirectory())
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2015-08-09 18:53:38 +00:00
|
|
|
u64 remaining_size = file_info->GetSize();
|
|
|
|
u64 file_offset = file_info->GetOffset();
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2015-08-09 18:53:38 +00:00
|
|
|
File::IOFile f(export_filename, "wb");
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!f)
|
|
|
|
return false;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
bool result = true;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2015-08-09 18:53:38 +00:00
|
|
|
while (remaining_size)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
// Limit read size to 128 MB
|
2015-08-09 18:53:38 +00:00
|
|
|
size_t read_size = (size_t)std::min(remaining_size, (u64)0x08000000);
|
2010-05-30 17:57:56 +00:00
|
|
|
|
2015-08-09 18:53:38 +00:00
|
|
|
std::vector<u8> buffer(read_size);
|
2010-05-30 17:57:56 +00:00
|
|
|
|
2015-08-09 18:53:38 +00:00
|
|
|
result = m_volume->Read(file_offset, read_size, &buffer[0], m_partition);
|
2010-05-30 17:57:56 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!result)
|
|
|
|
break;
|
2010-05-30 17:57:56 +00:00
|
|
|
|
2015-08-09 18:53:38 +00:00
|
|
|
f.WriteBytes(&buffer[0], read_size);
|
2010-05-30 17:57:56 +00:00
|
|
|
|
2015-08-09 18:53:38 +00:00
|
|
|
remaining_size -= read_size;
|
|
|
|
file_offset += read_size;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return result;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-09-13 22:03:18 +00:00
|
|
|
|
2015-08-09 18:53:38 +00:00
|
|
|
bool FileSystemGCWii::ExportApploader(const std::string& export_folder) const
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2015-08-09 18:53:38 +00:00
|
|
|
std::optional<u32> apploader_size = m_volume->ReadSwapped<u32>(0x2440 + 0x14, m_partition);
|
|
|
|
const std::optional<u32> trailer_size = m_volume->ReadSwapped<u32>(0x2440 + 0x18, m_partition);
|
2017-06-04 08:33:14 +00:00
|
|
|
constexpr u32 header_size = 0x20;
|
|
|
|
if (!apploader_size || !trailer_size)
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
2017-06-04 08:33:14 +00:00
|
|
|
*apploader_size += *trailer_size + header_size;
|
|
|
|
DEBUG_LOG(DISCIO, "Apploader size -> %x", *apploader_size);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-06-04 08:33:14 +00:00
|
|
|
std::vector<u8> buffer(*apploader_size);
|
2015-08-09 18:53:38 +00:00
|
|
|
if (m_volume->Read(0x2440, *apploader_size, buffer.data(), m_partition))
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2015-08-09 18:53:38 +00:00
|
|
|
std::string export_name(export_folder + "/apploader.img");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-08-09 18:53:38 +00:00
|
|
|
File::IOFile apploader_file(export_name, "wb");
|
|
|
|
if (apploader_file)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2015-08-09 18:53:38 +00:00
|
|
|
apploader_file.WriteBytes(buffer.data(), *apploader_size);
|
2016-06-24 08:43:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2009-10-11 16:06:02 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
std::optional<u64> FileSystemGCWii::GetBootDOLOffset() const
|
2009-10-11 16:06:02 +00:00
|
|
|
{
|
2015-08-09 18:53:38 +00:00
|
|
|
std::optional<u32> offset = m_volume->ReadSwapped<u32>(0x420, m_partition);
|
2017-06-07 20:41:00 +00:00
|
|
|
return offset ? static_cast<u64>(*offset) << m_offset_shift : std::optional<u64>();
|
2015-10-17 18:52:26 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
std::optional<u32> FileSystemGCWii::GetBootDOLSize(u64 dol_offset) const
|
2015-10-17 18:52:26 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
u32 dol_size = 0;
|
|
|
|
|
|
|
|
// Iterate through the 7 code segments
|
|
|
|
for (u8 i = 0; i < 7; i++)
|
|
|
|
{
|
2017-06-04 08:33:14 +00:00
|
|
|
const std::optional<u32> offset =
|
2015-08-09 18:53:38 +00:00
|
|
|
m_volume->ReadSwapped<u32>(dol_offset + 0x00 + i * 4, m_partition);
|
2017-06-04 08:33:14 +00:00
|
|
|
const std::optional<u32> size =
|
2015-08-09 18:53:38 +00:00
|
|
|
m_volume->ReadSwapped<u32>(dol_offset + 0x90 + i * 4, m_partition);
|
2017-06-04 08:33:14 +00:00
|
|
|
if (!offset || !size)
|
|
|
|
return {};
|
|
|
|
dol_size = std::max(*offset + *size, dol_size);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate through the 11 data segments
|
|
|
|
for (u8 i = 0; i < 11; i++)
|
|
|
|
{
|
2017-06-04 08:33:14 +00:00
|
|
|
const std::optional<u32> offset =
|
2015-08-09 18:53:38 +00:00
|
|
|
m_volume->ReadSwapped<u32>(dol_offset + 0x1c + i * 4, m_partition);
|
2017-06-04 08:33:14 +00:00
|
|
|
const std::optional<u32> size =
|
2015-08-09 18:53:38 +00:00
|
|
|
m_volume->ReadSwapped<u32>(dol_offset + 0xac + i * 4, m_partition);
|
2017-06-04 08:33:14 +00:00
|
|
|
if (!offset || !size)
|
|
|
|
return {};
|
|
|
|
dol_size = std::max(*offset + *size, dol_size);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dol_size;
|
2012-05-05 08:38:00 +00:00
|
|
|
}
|
|
|
|
|
2015-08-09 18:53:38 +00:00
|
|
|
bool FileSystemGCWii::ExportDOL(const std::string& export_folder) const
|
2012-05-05 08:38:00 +00:00
|
|
|
{
|
2017-06-04 08:33:14 +00:00
|
|
|
std::optional<u64> dol_offset = GetBootDOLOffset();
|
|
|
|
if (!dol_offset)
|
|
|
|
return false;
|
|
|
|
std::optional<u32> dol_size = GetBootDOLSize(*dol_offset);
|
|
|
|
if (!dol_size)
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
|
|
|
|
2017-06-04 08:33:14 +00:00
|
|
|
std::vector<u8> buffer(*dol_size);
|
2015-08-09 18:53:38 +00:00
|
|
|
if (m_volume->Read(*dol_offset, *dol_size, buffer.data(), m_partition))
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2015-08-09 18:53:38 +00:00
|
|
|
std::string export_name(export_folder + "/boot.dol");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-08-09 18:53:38 +00:00
|
|
|
File::IOFile dol_file(export_name, "wb");
|
|
|
|
if (dol_file)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2015-08-09 18:53:38 +00:00
|
|
|
dol_file.WriteBytes(&buffer[0], *dol_size);
|
2016-06-24 08:43:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|