2015-05-24 07:55:12 +03:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-18 02:08:10 +03:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 06:09:55 +03:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 07:30:24 +02:00
|
|
|
|
2013-03-04 00:51:26 +02:00
|
|
|
#include <algorithm>
|
2013-10-26 12:55:41 +03:00
|
|
|
#include <cinttypes>
|
2014-02-21 02:47:53 +02:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
2017-06-04 11:33:14 +03:00
|
|
|
#include <optional>
|
2014-02-17 12:18:15 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-05-22 11:39:36 +03:00
|
|
|
#include "Common/CommonFuncs.h"
|
2014-09-08 04:06:58 +03:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 12:18:15 +02:00
|
|
|
#include "Common/FileUtil.h"
|
2016-06-24 11:43:46 +03:00
|
|
|
#include "Common/Logging/Log.h"
|
2015-09-27 00:13:07 +03:00
|
|
|
#include "Common/MsgHandler.h"
|
2014-02-17 12:18:15 +02:00
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
#include "DiscIO/FileSystemGCWii.h"
|
2016-06-24 11:43:46 +03:00
|
|
|
#include "DiscIO/Filesystem.h"
|
2014-02-21 02:47:53 +02:00
|
|
|
#include "DiscIO/Volume.h"
|
2008-12-08 07:30:24 +02:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
2015-07-29 17:42:29 +03:00
|
|
|
FileInfoGCWii::FileInfoGCWii(u64 name_offset, u64 offset, u64 file_size, std::string name)
|
|
|
|
: m_NameOffset(name_offset), m_Offset(offset), m_FileSize(file_size), m_Name(name)
|
2015-07-28 17:56:25 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FileInfoGCWii::~FileInfoGCWii()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-06-06 12:49:01 +03:00
|
|
|
FileSystemGCWii::FileSystemGCWii(const Volume* _rVolume, const Partition& partition)
|
2015-07-28 17:56:25 +03:00
|
|
|
: FileSystem(_rVolume, partition), m_Initialized(false), m_Valid(false), m_offset_shift(0)
|
2008-12-08 07:30:24 +02:00
|
|
|
{
|
2016-06-24 11:43:46 +03:00
|
|
|
m_Valid = DetectFileSystem();
|
2008-12-08 07:30:24 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 12:49:01 +03:00
|
|
|
FileSystemGCWii::~FileSystemGCWii()
|
2008-12-08 07:30:24 +02:00
|
|
|
{
|
2016-06-24 11:43:46 +03:00
|
|
|
m_FileInfoVector.clear();
|
2008-12-08 07:30:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-29 18:27:43 +03:00
|
|
|
const std::vector<FileInfoGCWii>& FileSystemGCWii::GetFileList()
|
|
|
|
{
|
|
|
|
if (!m_Initialized)
|
|
|
|
InitFileSystem();
|
|
|
|
|
|
|
|
return m_FileInfoVector;
|
|
|
|
}
|
|
|
|
|
2015-07-30 16:06:23 +03:00
|
|
|
const FileInfo* FileSystemGCWii::FindFileInfo(const std::string& path)
|
2015-07-29 18:27:43 +03:00
|
|
|
{
|
|
|
|
if (!m_Initialized)
|
|
|
|
InitFileSystem();
|
|
|
|
|
2015-07-30 18:12:44 +03:00
|
|
|
return FindFileInfo(path, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const FileInfo* FileSystemGCWii::FindFileInfo(const std::string& path,
|
|
|
|
size_t search_start_offset) const
|
|
|
|
{
|
|
|
|
// 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 == "/")
|
|
|
|
return &m_FileInfoVector[search_start_offset];
|
|
|
|
|
|
|
|
// It's only possible to search in directories. Searching in a file is an error
|
|
|
|
if (!m_FileInfoVector[search_start_offset].IsDirectory())
|
|
|
|
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) : "";
|
|
|
|
|
|
|
|
size_t search_end_offset = m_FileInfoVector[search_start_offset].GetSize();
|
|
|
|
search_start_offset++;
|
|
|
|
while (search_start_offset < search_end_offset)
|
2015-07-29 18:27:43 +03:00
|
|
|
{
|
2015-07-30 18:12:44 +03:00
|
|
|
const FileInfoGCWii& file_info = m_FileInfoVector[search_start_offset];
|
|
|
|
|
|
|
|
if (file_info.GetName() == searching_for)
|
|
|
|
{
|
|
|
|
// A match is found. The rest of the path is passed on to finish the search.
|
|
|
|
const FileInfo* result = FindFileInfo(rest_of_path, search_start_offset);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_info.IsDirectory())
|
|
|
|
{
|
|
|
|
// Skip a directory and everything that it contains
|
|
|
|
|
|
|
|
if (file_info.GetSize() <= search_start_offset)
|
|
|
|
{
|
|
|
|
// The next offset (obtained by GetSize) is supposed to be larger than
|
|
|
|
// the current offset. If an FST is malformed and breaks that rule,
|
|
|
|
// there's a risk that next offset pointers form a loop.
|
|
|
|
// To avoid infinite loops, this method returns.
|
|
|
|
ERROR_LOG(DISCIO, "Invalid next offset in file system");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
search_start_offset = file_info.GetSize();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Skip a single file
|
|
|
|
search_start_offset++;
|
|
|
|
}
|
2015-07-29 18:27:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-30 16:06:23 +03:00
|
|
|
const FileInfo* FileSystemGCWii::FindFileInfo(u64 disc_offset)
|
2008-12-08 07:30:24 +02:00
|
|
|
{
|
2016-06-24 11:43:46 +03:00
|
|
|
if (!m_Initialized)
|
|
|
|
InitFileSystem();
|
2008-12-08 07:30:24 +02:00
|
|
|
|
2015-07-30 16:06:23 +03:00
|
|
|
for (auto& file_info : m_FileInfoVector)
|
|
|
|
{
|
|
|
|
if ((file_info.GetOffset() <= disc_offset) &&
|
|
|
|
((file_info.GetOffset() + file_info.GetSize()) > disc_offset))
|
|
|
|
{
|
|
|
|
return &file_info;
|
|
|
|
}
|
|
|
|
}
|
2008-12-08 07:30:24 +02:00
|
|
|
|
2015-07-30 16:06:23 +03:00
|
|
|
return nullptr;
|
2008-12-08 07:30:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-29 17:42:29 +03:00
|
|
|
std::string FileSystemGCWii::GetPath(u64 _Address)
|
2008-12-08 07:30:24 +02:00
|
|
|
{
|
2016-06-24 11:43:46 +03:00
|
|
|
if (!m_Initialized)
|
|
|
|
InitFileSystem();
|
|
|
|
|
2015-07-29 17:42:29 +03:00
|
|
|
for (size_t i = 0; i < m_FileInfoVector.size(); ++i)
|
2016-06-24 11:43:46 +03:00
|
|
|
{
|
2015-07-29 17:42:29 +03:00
|
|
|
const FileInfoGCWii& file_info = m_FileInfoVector[i];
|
|
|
|
if ((file_info.GetOffset() <= _Address) &&
|
|
|
|
((file_info.GetOffset() + file_info.GetSize()) > _Address))
|
2016-06-24 11:43:46 +03:00
|
|
|
{
|
2015-07-29 17:42:29 +03:00
|
|
|
return GetPathFromFSTOffset(i);
|
2016-06-24 11:43:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
2008-12-08 07:30:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-29 17:42:29 +03:00
|
|
|
std::string FileSystemGCWii::GetPathFromFSTOffset(size_t file_info_offset)
|
|
|
|
{
|
|
|
|
if (!m_Initialized)
|
|
|
|
InitFileSystem();
|
|
|
|
|
|
|
|
// Root entry doesn't have a name
|
|
|
|
if (file_info_offset == 0)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
const FileInfoGCWii& file_info = m_FileInfoVector[file_info_offset];
|
|
|
|
if (file_info.IsDirectory())
|
|
|
|
{
|
|
|
|
// The offset of the parent directory is stored in the current directory.
|
|
|
|
|
|
|
|
if (file_info.GetOffset() >= file_info_offset)
|
|
|
|
{
|
|
|
|
// The offset of the parent directory is supposed to be smaller than
|
|
|
|
// the current offset. If an FST is malformed and breaks that rule,
|
|
|
|
// there's a risk that parent directory pointers form a loop.
|
|
|
|
// To avoid stack overflows, this method returns.
|
|
|
|
ERROR_LOG(DISCIO, "Invalid parent offset in file system");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return GetPathFromFSTOffset(file_info.GetOffset()) + file_info.GetName() + "/";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The parent directory can be found by searching backwards
|
|
|
|
// for a directory that contains this file.
|
|
|
|
|
|
|
|
size_t parent_offset = file_info_offset - 1;
|
|
|
|
while (!(m_FileInfoVector[parent_offset].IsDirectory() &&
|
|
|
|
m_FileInfoVector[parent_offset].GetSize() > file_info_offset))
|
|
|
|
{
|
|
|
|
parent_offset--;
|
|
|
|
}
|
|
|
|
return GetPathFromFSTOffset(parent_offset) + file_info.GetName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 16:06:23 +03:00
|
|
|
u64 FileSystemGCWii::ReadFile(const FileInfo* file_info, u8* _pBuffer, u64 _MaxBufferSize,
|
2017-06-06 12:49:01 +03:00
|
|
|
u64 _OffsetInFile)
|
2008-12-08 07:30:24 +02:00
|
|
|
{
|
2016-06-24 11:43:46 +03:00
|
|
|
if (!m_Initialized)
|
|
|
|
InitFileSystem();
|
2008-12-08 07:30:24 +02:00
|
|
|
|
2015-07-30 16:06:23 +03:00
|
|
|
if (!file_info || file_info->IsDirectory())
|
2016-06-24 11:43:46 +03:00
|
|
|
return 0;
|
2008-12-08 07:30:24 +02:00
|
|
|
|
2015-07-30 16:06:23 +03:00
|
|
|
if (_OffsetInFile >= file_info->GetSize())
|
2016-06-24 11:43:46 +03:00
|
|
|
return 0;
|
2008-12-08 07:30:24 +02:00
|
|
|
|
2015-07-30 16:06:23 +03:00
|
|
|
u64 read_length = std::min(_MaxBufferSize, file_info->GetSize() - _OffsetInFile);
|
2010-01-11 07:07:56 +02:00
|
|
|
|
2017-06-07 23:40:51 +03:00
|
|
|
DEBUG_LOG(DISCIO, "Reading %" PRIx64 " bytes at %" PRIx64 " from file %s. Offset: %" PRIx64
|
|
|
|
" Size: %" PRIx64,
|
2015-07-30 16:06:23 +03:00
|
|
|
read_length, _OffsetInFile, GetPath(file_info->GetOffset()).c_str(),
|
|
|
|
file_info->GetOffset(), file_info->GetSize());
|
2015-04-28 17:47:03 +03:00
|
|
|
|
2015-07-30 16:06:23 +03:00
|
|
|
m_rVolume->Read(file_info->GetOffset() + _OffsetInFile, read_length, _pBuffer, m_partition);
|
2016-06-24 11:43:46 +03:00
|
|
|
return read_length;
|
2008-12-08 07:30:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-30 16:06:23 +03:00
|
|
|
bool FileSystemGCWii::ExportFile(const FileInfo* file_info, const std::string& _rExportFilename)
|
2008-12-08 07:30:24 +02:00
|
|
|
{
|
2016-06-24 11:43:46 +03:00
|
|
|
if (!m_Initialized)
|
|
|
|
InitFileSystem();
|
2010-06-03 23:37:32 +03:00
|
|
|
|
2015-07-30 16:06:23 +03:00
|
|
|
if (!file_info || file_info->IsDirectory())
|
2016-06-24 11:43:46 +03:00
|
|
|
return false;
|
2008-12-08 07:30:24 +02:00
|
|
|
|
2015-07-30 16:06:23 +03:00
|
|
|
u64 remainingSize = file_info->GetSize();
|
|
|
|
u64 fileOffset = file_info->GetOffset();
|
2008-12-08 07:30:24 +02:00
|
|
|
|
2016-06-24 11:43:46 +03:00
|
|
|
File::IOFile f(_rExportFilename, "wb");
|
|
|
|
if (!f)
|
|
|
|
return false;
|
2008-12-08 07:30:24 +02:00
|
|
|
|
2016-06-24 11:43:46 +03:00
|
|
|
bool result = true;
|
2008-12-08 07:30:24 +02:00
|
|
|
|
2016-06-24 11:43:46 +03:00
|
|
|
while (remainingSize)
|
|
|
|
{
|
|
|
|
// Limit read size to 128 MB
|
|
|
|
size_t readSize = (size_t)std::min(remainingSize, (u64)0x08000000);
|
2010-05-30 20:57:56 +03:00
|
|
|
|
2016-06-24 11:43:46 +03:00
|
|
|
std::vector<u8> buffer(readSize);
|
2010-05-30 20:57:56 +03:00
|
|
|
|
2015-06-13 13:51:24 +03:00
|
|
|
result = m_rVolume->Read(fileOffset, readSize, &buffer[0], m_partition);
|
2010-05-30 20:57:56 +03:00
|
|
|
|
2016-06-24 11:43:46 +03:00
|
|
|
if (!result)
|
|
|
|
break;
|
2010-05-30 20:57:56 +03:00
|
|
|
|
2016-06-24 11:43:46 +03:00
|
|
|
f.WriteBytes(&buffer[0], readSize);
|
2010-05-30 20:57:56 +03:00
|
|
|
|
2016-06-24 11:43:46 +03:00
|
|
|
remainingSize -= readSize;
|
|
|
|
fileOffset += readSize;
|
|
|
|
}
|
2008-12-08 07:30:24 +02:00
|
|
|
|
2016-06-24 11:43:46 +03:00
|
|
|
return result;
|
2008-12-08 07:30:24 +02:00
|
|
|
}
|
2009-09-14 01:03:18 +03:00
|
|
|
|
2017-06-06 12:49:01 +03:00
|
|
|
bool FileSystemGCWii::ExportApploader(const std::string& _rExportFolder) const
|
2008-12-08 07:30:24 +02:00
|
|
|
{
|
2017-06-04 11:33:14 +03:00
|
|
|
std::optional<u32> apploader_size = m_rVolume->ReadSwapped<u32>(0x2440 + 0x14, m_partition);
|
|
|
|
const std::optional<u32> trailer_size = m_rVolume->ReadSwapped<u32>(0x2440 + 0x18, m_partition);
|
|
|
|
constexpr u32 header_size = 0x20;
|
|
|
|
if (!apploader_size || !trailer_size)
|
2016-06-24 11:43:46 +03:00
|
|
|
return false;
|
2017-06-04 11:33:14 +03:00
|
|
|
*apploader_size += *trailer_size + header_size;
|
|
|
|
DEBUG_LOG(DISCIO, "Apploader size -> %x", *apploader_size);
|
2016-06-24 11:43:46 +03:00
|
|
|
|
2017-06-04 11:33:14 +03:00
|
|
|
std::vector<u8> buffer(*apploader_size);
|
|
|
|
if (m_rVolume->Read(0x2440, *apploader_size, buffer.data(), m_partition))
|
2016-06-24 11:43:46 +03:00
|
|
|
{
|
|
|
|
std::string exportName(_rExportFolder + "/apploader.img");
|
|
|
|
|
|
|
|
File::IOFile AppFile(exportName, "wb");
|
|
|
|
if (AppFile)
|
|
|
|
{
|
2017-06-04 11:33:14 +03:00
|
|
|
AppFile.WriteBytes(buffer.data(), *apploader_size);
|
2016-06-24 11:43:46 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2009-10-11 19:06:02 +03:00
|
|
|
}
|
|
|
|
|
2017-06-06 12:49:01 +03:00
|
|
|
std::optional<u64> FileSystemGCWii::GetBootDOLOffset() const
|
2009-10-11 19:06:02 +03:00
|
|
|
{
|
2017-06-04 11:33:14 +03:00
|
|
|
std::optional<u32> offset = m_rVolume->ReadSwapped<u32>(0x420, m_partition);
|
2017-06-07 23:41:00 +03:00
|
|
|
return offset ? static_cast<u64>(*offset) << m_offset_shift : std::optional<u64>();
|
2015-10-17 21:52:26 +03:00
|
|
|
}
|
|
|
|
|
2017-06-06 12:49:01 +03:00
|
|
|
std::optional<u32> FileSystemGCWii::GetBootDOLSize(u64 dol_offset) const
|
2015-10-17 21:52:26 +03:00
|
|
|
{
|
2016-06-24 11:43:46 +03:00
|
|
|
u32 dol_size = 0;
|
|
|
|
|
|
|
|
// Iterate through the 7 code segments
|
|
|
|
for (u8 i = 0; i < 7; i++)
|
|
|
|
{
|
2017-06-04 11:33:14 +03:00
|
|
|
const std::optional<u32> offset =
|
|
|
|
m_rVolume->ReadSwapped<u32>(dol_offset + 0x00 + i * 4, m_partition);
|
|
|
|
const std::optional<u32> size =
|
|
|
|
m_rVolume->ReadSwapped<u32>(dol_offset + 0x90 + i * 4, m_partition);
|
|
|
|
if (!offset || !size)
|
|
|
|
return {};
|
|
|
|
dol_size = std::max(*offset + *size, dol_size);
|
2016-06-24 11:43:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate through the 11 data segments
|
|
|
|
for (u8 i = 0; i < 11; i++)
|
|
|
|
{
|
2017-06-04 11:33:14 +03:00
|
|
|
const std::optional<u32> offset =
|
2017-06-06 12:49:01 +03:00
|
|
|
m_rVolume->ReadSwapped<u32>(dol_offset + 0x1c + i * 4, m_partition);
|
2017-06-04 11:33:14 +03:00
|
|
|
const std::optional<u32> size =
|
2017-06-06 12:49:01 +03:00
|
|
|
m_rVolume->ReadSwapped<u32>(dol_offset + 0xac + i * 4, m_partition);
|
2017-06-04 11:33:14 +03:00
|
|
|
if (!offset || !size)
|
|
|
|
return {};
|
|
|
|
dol_size = std::max(*offset + *size, dol_size);
|
2016-06-24 11:43:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return dol_size;
|
2012-05-05 11:38:00 +03:00
|
|
|
}
|
|
|
|
|
2017-06-06 12:49:01 +03:00
|
|
|
bool FileSystemGCWii::ExportDOL(const std::string& _rExportFolder) const
|
2012-05-05 11:38:00 +03:00
|
|
|
{
|
2017-06-04 11:33:14 +03: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 11:43:46 +03:00
|
|
|
return false;
|
|
|
|
|
2017-06-04 11:33:14 +03:00
|
|
|
std::vector<u8> buffer(*dol_size);
|
|
|
|
if (m_rVolume->Read(*dol_offset, *dol_size, &buffer[0], m_partition))
|
2016-06-24 11:43:46 +03:00
|
|
|
{
|
|
|
|
std::string exportName(_rExportFolder + "/boot.dol");
|
|
|
|
|
|
|
|
File::IOFile DolFile(exportName, "wb");
|
|
|
|
if (DolFile)
|
|
|
|
{
|
2017-06-04 11:33:14 +03:00
|
|
|
DolFile.WriteBytes(&buffer[0], *dol_size);
|
2016-06-24 11:43:46 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2008-12-08 07:30:24 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 12:49:01 +03:00
|
|
|
std::string FileSystemGCWii::GetStringFromOffset(u64 _Offset) const
|
2008-12-08 07:30:24 +02:00
|
|
|
{
|
2016-06-24 11:43:46 +03:00
|
|
|
std::string data(255, 0x00);
|
2015-06-13 13:51:24 +03:00
|
|
|
m_rVolume->Read(_Offset, data.size(), (u8*)&data[0], m_partition);
|
2016-06-24 11:43:46 +03:00
|
|
|
data.erase(std::find(data.begin(), data.end(), 0x00), data.end());
|
2013-10-29 07:23:17 +02:00
|
|
|
|
2016-06-24 11:43:46 +03:00
|
|
|
// TODO: Should we really always use SHIFT-JIS?
|
|
|
|
// It makes some filenames in Pikmin (NTSC-U) sane, but is it correct?
|
|
|
|
return SHIFTJISToUTF8(data);
|
2008-12-08 07:30:24 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 12:49:01 +03:00
|
|
|
bool FileSystemGCWii::DetectFileSystem()
|
2008-12-08 07:30:24 +02:00
|
|
|
{
|
2017-06-04 11:33:14 +03:00
|
|
|
if (m_rVolume->ReadSwapped<u32>(0x18, m_partition) == u32(0x5D1C9EA3))
|
2016-06-24 11:43:46 +03:00
|
|
|
{
|
2015-06-13 21:50:03 +03:00
|
|
|
m_offset_shift = 2; // Wii file system
|
2016-06-24 11:43:46 +03:00
|
|
|
return true;
|
|
|
|
}
|
2017-06-04 11:33:14 +03:00
|
|
|
else if (m_rVolume->ReadSwapped<u32>(0x1c, m_partition) == u32(0xC2339F3D))
|
2016-06-24 11:43:46 +03:00
|
|
|
{
|
2015-06-13 21:50:03 +03:00
|
|
|
m_offset_shift = 0; // GameCube file system
|
2016-06-24 11:43:46 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2010-06-03 23:37:32 +03:00
|
|
|
}
|
|
|
|
|
2017-06-06 12:49:01 +03:00
|
|
|
void FileSystemGCWii::InitFileSystem()
|
2010-06-03 23:37:32 +03:00
|
|
|
{
|
2016-06-24 11:43:46 +03:00
|
|
|
m_Initialized = true;
|
|
|
|
|
|
|
|
// read the whole FST
|
2017-06-04 11:33:14 +03:00
|
|
|
const std::optional<u32> fst_offset_unshifted = m_rVolume->ReadSwapped<u32>(0x424, m_partition);
|
|
|
|
if (!fst_offset_unshifted)
|
2016-06-24 11:43:46 +03:00
|
|
|
return;
|
2017-06-04 11:33:14 +03:00
|
|
|
const u64 FSTOffset = static_cast<u64>(*fst_offset_unshifted) << m_offset_shift;
|
2016-06-24 11:43:46 +03:00
|
|
|
|
|
|
|
// read all fileinfos
|
2017-06-04 11:33:14 +03:00
|
|
|
const std::optional<u32> root_name_offset = m_rVolume->ReadSwapped<u32>(FSTOffset, m_partition);
|
|
|
|
const std::optional<u32> root_offset = m_rVolume->ReadSwapped<u32>(FSTOffset + 0x4, m_partition);
|
|
|
|
const std::optional<u32> root_size = m_rVolume->ReadSwapped<u32>(FSTOffset + 0x8, m_partition);
|
|
|
|
if (!root_name_offset || !root_offset || !root_size)
|
2016-06-24 11:43:46 +03:00
|
|
|
return;
|
2015-07-28 17:56:25 +03:00
|
|
|
FileInfoGCWii root(*root_name_offset, static_cast<u64>(*root_offset) << m_offset_shift,
|
2015-07-29 17:42:29 +03:00
|
|
|
*root_size, "");
|
2016-06-24 11:43:46 +03:00
|
|
|
|
|
|
|
if (!root.IsDirectory())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// 12 bytes (the size of a file entry) times 10 * 1024 * 1024 is 120 MiB,
|
|
|
|
// more than total RAM in a Wii. No file system should use anywhere near that much.
|
|
|
|
static const u32 ARBITRARY_FILE_SYSTEM_SIZE_LIMIT = 10 * 1024 * 1024;
|
2015-07-28 17:56:25 +03:00
|
|
|
if (root.GetSize() > ARBITRARY_FILE_SYSTEM_SIZE_LIMIT)
|
2016-06-24 11:43:46 +03:00
|
|
|
{
|
|
|
|
// Without this check, Dolphin can crash by trying to allocate too much
|
|
|
|
// memory when loading the file systems of certain malformed disc images.
|
|
|
|
|
|
|
|
ERROR_LOG(DISCIO, "File system is abnormally large! Aborting loading");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_FileInfoVector.size())
|
|
|
|
PanicAlert("Wtf?");
|
2015-07-29 17:42:29 +03:00
|
|
|
u64 NameTableOffset = FSTOffset + (root.GetSize() * 0xC);
|
2016-06-24 11:43:46 +03:00
|
|
|
|
2015-07-28 17:56:25 +03:00
|
|
|
m_FileInfoVector.reserve((size_t)root.GetSize());
|
|
|
|
for (u32 i = 0; i < root.GetSize(); i++)
|
2016-06-24 11:43:46 +03:00
|
|
|
{
|
|
|
|
const u64 read_offset = FSTOffset + (i * 0xC);
|
2015-07-29 17:42:29 +03:00
|
|
|
const u32 name_offset = m_rVolume->ReadSwapped<u32>(read_offset, m_partition).value_or(0);
|
|
|
|
const u32 offset = m_rVolume->ReadSwapped<u32>(read_offset + 0x4, m_partition).value_or(0);
|
|
|
|
const u32 size = m_rVolume->ReadSwapped<u32>(read_offset + 0x8, m_partition).value_or(0);
|
|
|
|
const std::string name = GetStringFromOffset(NameTableOffset + (name_offset & 0xFFFFFF));
|
|
|
|
m_FileInfoVector.emplace_back(
|
|
|
|
name_offset, static_cast<u64>(offset) << (m_offset_shift * !(name_offset & 0xFF000000)),
|
|
|
|
size, name);
|
2016-06-24 11:43:46 +03:00
|
|
|
}
|
2008-12-08 07:30:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|