2013-04-17 21:09:55 -06:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2013-03-03 15:51:26 -07:00
|
|
|
#include <algorithm>
|
2013-10-26 03:55:41 -06:00
|
|
|
#include <cinttypes>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "Common/Common.h"
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/StringUtil.h"
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2014-02-20 17:47:53 -07:00
|
|
|
#include "DiscIO/Filesystem.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "DiscIO/FileSystemGCWii.h"
|
2014-02-20 17:47:53 -07:00
|
|
|
#include "DiscIO/Volume.h"
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
|
|
|
CFileSystemGCWii::CFileSystemGCWii(const IVolume *_rVolume)
|
2013-03-03 15:51:26 -07:00
|
|
|
: IFileSystem(_rVolume)
|
|
|
|
, m_Initialized(false)
|
|
|
|
, m_Valid(false)
|
|
|
|
, m_OffsetShift(0)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2010-06-03 14:37:32 -06:00
|
|
|
m_Valid = DetectFileSystem();
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
CFileSystemGCWii::~CFileSystemGCWii()
|
|
|
|
{
|
2010-02-14 07:06:33 -07:00
|
|
|
m_FileInfoVector.clear();
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
u64 CFileSystemGCWii::GetFileSize(const std::string& _rFullPath)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
|
|
|
if (!m_Initialized)
|
2010-06-03 14:37:32 -06:00
|
|
|
InitFileSystem();
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
const SFileInfo* pFileInfo = FindFileInfo(_rFullPath);
|
|
|
|
|
2014-03-09 14:14:26 -06:00
|
|
|
if (pFileInfo != nullptr && !pFileInfo->IsDirectory())
|
2008-12-07 22:30:24 -07:00
|
|
|
return pFileInfo->m_FileSize;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-14 21:38:14 -06:00
|
|
|
const std::string CFileSystemGCWii::GetFileName(u64 _Address)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2010-06-03 14:37:32 -06:00
|
|
|
if (!m_Initialized)
|
|
|
|
InitFileSystem();
|
|
|
|
|
2013-10-28 23:09:01 -06:00
|
|
|
for (auto& fileInfo : m_FileInfoVector)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2013-10-28 23:09:01 -06:00
|
|
|
if ((fileInfo.m_Offset <= _Address) &&
|
|
|
|
((fileInfo.m_Offset + fileInfo.m_FileSize) > _Address))
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2013-10-28 23:09:01 -06:00
|
|
|
return fileInfo.m_FullPath;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-23 21:29:30 -06:00
|
|
|
return "";
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
u64 CFileSystemGCWii::ReadFile(const std::string& _rFullPath, u8* _pBuffer, size_t _MaxBufferSize)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
|
|
|
if (!m_Initialized)
|
2010-06-03 14:37:32 -06:00
|
|
|
InitFileSystem();
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
const SFileInfo* pFileInfo = FindFileInfo(_rFullPath);
|
2014-03-09 14:14:26 -06:00
|
|
|
if (pFileInfo == nullptr)
|
2008-12-07 22:30:24 -07:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (pFileInfo->m_FileSize > _MaxBufferSize)
|
|
|
|
return 0;
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
DEBUG_LOG(DISCIO, "Filename: %s. Offset: %" PRIx64 ". Size: %" PRIx64, _rFullPath.c_str(),
|
2010-12-05 02:04:34 -07:00
|
|
|
pFileInfo->m_Offset, pFileInfo->m_FileSize);
|
2010-01-10 22:07:56 -07:00
|
|
|
|
2008-12-07 22:30:24 -07:00
|
|
|
m_rVolume->Read(pFileInfo->m_Offset, pFileInfo->m_FileSize, _pBuffer);
|
|
|
|
return pFileInfo->m_FileSize;
|
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
bool CFileSystemGCWii::ExportFile(const std::string& _rFullPath, const std::string& _rExportFilename)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2010-06-03 14:37:32 -06:00
|
|
|
if (!m_Initialized)
|
|
|
|
InitFileSystem();
|
|
|
|
|
2010-05-30 11:57:56 -06:00
|
|
|
const SFileInfo* pFileInfo = FindFileInfo(_rFullPath);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2013-01-09 00:16:23 -07:00
|
|
|
if (!pFileInfo)
|
2008-12-07 22:30:24 -07:00
|
|
|
return false;
|
|
|
|
|
2010-05-30 11:57:56 -06:00
|
|
|
u64 remainingSize = pFileInfo->m_FileSize;
|
|
|
|
u64 fileOffset = pFileInfo->m_Offset;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2011-03-11 03:21:46 -07:00
|
|
|
File::IOFile f(_rExportFilename, "wb");
|
2010-05-30 11:57:56 -06:00
|
|
|
if (!f)
|
2008-12-07 22:30:24 -07:00
|
|
|
return false;
|
|
|
|
|
2010-05-30 11:57:56 -06:00
|
|
|
bool result = true;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2010-05-30 11:57:56 -06:00
|
|
|
while (remainingSize)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2010-05-30 11:57:56 -06:00
|
|
|
// Limit read size to 128 MB
|
|
|
|
size_t readSize = (size_t)min(remainingSize, (u64)0x08000000);
|
|
|
|
|
2013-01-09 00:16:23 -07:00
|
|
|
std::vector<u8> buffer(readSize);
|
2010-05-30 11:57:56 -06:00
|
|
|
|
2013-01-09 00:16:23 -07:00
|
|
|
result = m_rVolume->Read(fileOffset, readSize, &buffer[0]);
|
2010-05-30 11:57:56 -06:00
|
|
|
|
|
|
|
if (!result)
|
|
|
|
break;
|
|
|
|
|
2013-01-09 00:16:23 -07:00
|
|
|
f.WriteBytes(&buffer[0], readSize);
|
2010-05-30 11:57:56 -06:00
|
|
|
|
|
|
|
remainingSize -= readSize;
|
|
|
|
fileOffset += readSize;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2010-05-30 11:57:56 -06:00
|
|
|
return result;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
2009-09-13 16:03:18 -06:00
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
bool CFileSystemGCWii::ExportApploader(const std::string& _rExportFolder) const
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2009-12-29 06:59:38 -07:00
|
|
|
u32 AppSize = Read32(0x2440 + 0x14);// apploader size
|
2014-02-16 13:30:18 -07:00
|
|
|
AppSize += Read32(0x2440 + 0x18); // + trailer size
|
|
|
|
AppSize += 0x20; // + header size
|
2009-10-11 10:06:02 -06:00
|
|
|
DEBUG_LOG(DISCIO,"AppSize -> %x", AppSize);
|
|
|
|
|
2013-01-09 00:16:23 -07:00
|
|
|
std::vector<u8> buffer(AppSize);
|
|
|
|
if (m_rVolume->Read(0x2440, AppSize, &buffer[0]))
|
2009-09-13 03:03:38 -06:00
|
|
|
{
|
2013-01-09 00:16:23 -07:00
|
|
|
std::string exportName(_rExportFolder);
|
|
|
|
exportName += "/apploader.img";
|
2011-03-11 03:21:46 -07:00
|
|
|
|
|
|
|
File::IOFile AppFile(exportName, "wb");
|
2009-10-11 10:06:02 -06:00
|
|
|
if (AppFile)
|
|
|
|
{
|
2013-01-09 00:16:23 -07:00
|
|
|
AppFile.WriteBytes(&buffer[0], AppSize);
|
2009-10-11 10:06:02 -06:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-13 03:03:38 -06:00
|
|
|
}
|
2009-09-13 16:03:18 -06:00
|
|
|
|
2009-10-11 10:06:02 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-05 02:38:00 -06:00
|
|
|
u32 CFileSystemGCWii::GetBootDOLSize() const
|
2009-10-11 10:06:02 -06:00
|
|
|
{
|
2009-09-13 16:03:18 -06:00
|
|
|
u32 DolOffset = Read32(0x420) << m_OffsetShift;
|
2009-10-23 19:00:45 -06:00
|
|
|
u32 DolSize = 0, offset = 0, size = 0;
|
2009-09-13 16:03:18 -06:00
|
|
|
|
|
|
|
// Iterate through the 7 code segments
|
|
|
|
for (u8 i = 0; i < 7; i++)
|
2009-09-13 03:03:38 -06:00
|
|
|
{
|
2014-02-16 13:30:18 -07:00
|
|
|
offset = Read32(DolOffset + 0x00 + i * 4);
|
|
|
|
size = Read32(DolOffset + 0x90 + i * 4);
|
2009-09-13 16:03:18 -06:00
|
|
|
if (offset + size > DolSize)
|
|
|
|
DolSize = offset + size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate through the 11 data segments
|
|
|
|
for (u8 i = 0; i < 11; i++)
|
|
|
|
{
|
2014-02-16 13:30:18 -07:00
|
|
|
offset = Read32(DolOffset + 0x1c + i * 4);
|
|
|
|
size = Read32(DolOffset + 0xac + i * 4);
|
2009-09-13 16:03:18 -06:00
|
|
|
if (offset + size > DolSize)
|
|
|
|
DolSize = offset + size;
|
2009-09-13 03:03:38 -06:00
|
|
|
}
|
2012-05-05 02:38:00 -06:00
|
|
|
return DolSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CFileSystemGCWii::GetBootDOL(u8* &buffer, u32 DolSize) const
|
|
|
|
{
|
|
|
|
u32 DolOffset = Read32(0x420) << m_OffsetShift;
|
|
|
|
return m_rVolume->Read(DolOffset, DolSize, buffer);
|
|
|
|
}
|
2009-10-11 10:06:02 -06:00
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
bool CFileSystemGCWii::ExportDOL(const std::string& _rExportFolder) const
|
2012-05-05 02:38:00 -06:00
|
|
|
{
|
|
|
|
u32 DolOffset = Read32(0x420) << m_OffsetShift;
|
|
|
|
u32 DolSize = GetBootDOLSize();
|
2009-10-11 10:06:02 -06:00
|
|
|
|
2013-01-09 00:16:23 -07:00
|
|
|
std::vector<u8> buffer(DolSize);
|
|
|
|
if (m_rVolume->Read(DolOffset, DolSize, &buffer[0]))
|
2009-09-12 03:09:47 -06:00
|
|
|
{
|
2013-01-09 00:16:23 -07:00
|
|
|
std::string exportName(_rExportFolder);
|
|
|
|
exportName += "/boot.dol";
|
|
|
|
|
2011-03-11 03:21:46 -07:00
|
|
|
File::IOFile DolFile(exportName, "wb");
|
2009-10-11 10:06:02 -06:00
|
|
|
if (DolFile)
|
|
|
|
{
|
2013-01-09 00:16:23 -07:00
|
|
|
DolFile.WriteBytes(&buffer[0], DolSize);
|
2009-10-11 10:06:02 -06:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-12 03:09:47 -06:00
|
|
|
}
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2009-10-11 10:06:02 -06:00
|
|
|
return false;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 CFileSystemGCWii::Read32(u64 _Offset) const
|
|
|
|
{
|
|
|
|
u32 Temp = 0;
|
|
|
|
m_rVolume->Read(_Offset, 4, (u8*)&Temp);
|
|
|
|
return Common::swap32(Temp);
|
|
|
|
}
|
|
|
|
|
2013-03-03 15:51:26 -07:00
|
|
|
std::string CFileSystemGCWii::GetStringFromOffset(u64 _Offset) const
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2013-03-03 15:51:26 -07:00
|
|
|
std::string data;
|
|
|
|
data.resize(255);
|
|
|
|
m_rVolume->Read(_Offset, data.size(), (u8*)&data[0]);
|
|
|
|
data.erase(std::find(data.begin(), data.end(), 0x00), data.end());
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2013-03-03 15:51:26 -07: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-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2010-06-03 14:37:32 -06:00
|
|
|
size_t CFileSystemGCWii::GetFileList(std::vector<const SFileInfo *> &_rFilenames)
|
2013-10-28 23:23:17 -06:00
|
|
|
{
|
2010-06-03 14:37:32 -06:00
|
|
|
if (!m_Initialized)
|
|
|
|
InitFileSystem();
|
|
|
|
|
2008-12-07 22:30:24 -07:00
|
|
|
if (_rFilenames.size())
|
|
|
|
PanicAlert("GetFileList : input list has contents?");
|
|
|
|
_rFilenames.clear();
|
|
|
|
_rFilenames.reserve(m_FileInfoVector.size());
|
2013-10-28 23:09:01 -06:00
|
|
|
for (auto& fileInfo : m_FileInfoVector)
|
|
|
|
_rFilenames.push_back(&fileInfo);
|
2008-12-07 22:30:24 -07:00
|
|
|
return m_FileInfoVector.size();
|
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
const SFileInfo* CFileSystemGCWii::FindFileInfo(const std::string& _rFullPath)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2010-06-03 14:37:32 -06:00
|
|
|
if (!m_Initialized)
|
|
|
|
InitFileSystem();
|
|
|
|
|
2013-10-28 23:09:01 -06:00
|
|
|
for (auto& fileInfo : m_FileInfoVector)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2014-03-14 21:38:14 -06:00
|
|
|
if (!strcasecmp(fileInfo.m_FullPath.c_str(), _rFullPath.c_str()))
|
2013-10-28 23:09:01 -06:00
|
|
|
return &fileInfo;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2014-03-09 14:14:26 -06:00
|
|
|
return nullptr;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2010-06-03 14:37:32 -06:00
|
|
|
bool CFileSystemGCWii::DetectFileSystem()
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
|
|
|
if (Read32(0x18) == 0x5D1C9EA3)
|
|
|
|
{
|
|
|
|
m_OffsetShift = 2; // Wii file system
|
2010-06-03 14:59:55 -06:00
|
|
|
return true;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
else if (Read32(0x1c) == 0xC2339F3D)
|
|
|
|
{
|
|
|
|
m_OffsetShift = 0; // GC file system
|
2010-06-03 14:59:55 -06:00
|
|
|
return true;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
2010-06-03 14:59:55 -06:00
|
|
|
|
|
|
|
return false;
|
2010-06-03 14:37:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void CFileSystemGCWii::InitFileSystem()
|
|
|
|
{
|
|
|
|
m_Initialized = true;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
// read the whole FST
|
|
|
|
u64 FSTOffset = (u64)Read32(0x424) << m_OffsetShift;
|
|
|
|
// u32 FSTSize = Read32(0x428);
|
|
|
|
// u32 FSTMaxSize = Read32(0x42C);
|
|
|
|
|
|
|
|
|
|
|
|
// read all fileinfos
|
|
|
|
SFileInfo Root;
|
|
|
|
Root.m_NameOffset = Read32(FSTOffset + 0x0);
|
|
|
|
Root.m_Offset = (u64)Read32(FSTOffset + 0x4) << m_OffsetShift;
|
|
|
|
Root.m_FileSize = Read32(FSTOffset + 0x8);
|
|
|
|
|
|
|
|
if (Root.IsDirectory())
|
|
|
|
{
|
|
|
|
if (m_FileInfoVector.size())
|
|
|
|
PanicAlert("Wtf?");
|
|
|
|
u64 NameTableOffset = FSTOffset;
|
|
|
|
|
2009-01-23 18:50:23 -07:00
|
|
|
m_FileInfoVector.reserve((unsigned int)Root.m_FileSize);
|
2008-12-07 22:30:24 -07:00
|
|
|
for (u32 i = 0; i < Root.m_FileSize; i++)
|
|
|
|
{
|
|
|
|
SFileInfo sfi;
|
|
|
|
u64 Offset = FSTOffset + (i * 0xC);
|
|
|
|
sfi.m_NameOffset = Read32(Offset + 0x0);
|
|
|
|
sfi.m_Offset = (u64)Read32(Offset + 0x4) << m_OffsetShift;
|
|
|
|
sfi.m_FileSize = Read32(Offset + 0x8);
|
|
|
|
|
|
|
|
m_FileInfoVector.push_back(sfi);
|
|
|
|
NameTableOffset += 0xC;
|
|
|
|
}
|
|
|
|
|
2014-03-14 21:38:14 -06:00
|
|
|
BuildFilenames(1, m_FileInfoVector.size(), "", NameTableOffset);
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-14 21:38:14 -06:00
|
|
|
size_t CFileSystemGCWii::BuildFilenames(const size_t _FirstIndex, const size_t _LastIndex, const std::string& _szDirectory, u64 _NameTableOffset)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
|
|
|
size_t CurrentIndex = _FirstIndex;
|
|
|
|
|
|
|
|
while (CurrentIndex < _LastIndex)
|
|
|
|
{
|
|
|
|
SFileInfo *rFileInfo = &m_FileInfoVector[CurrentIndex];
|
|
|
|
u64 uOffset = _NameTableOffset + (rFileInfo->m_NameOffset & 0xFFFFFF);
|
2013-03-03 15:51:26 -07:00
|
|
|
std::string filename = GetStringFromOffset(uOffset);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
// check next index
|
|
|
|
if (rFileInfo->IsDirectory())
|
|
|
|
{
|
2014-03-14 21:38:14 -06:00
|
|
|
if (_szDirectory.empty())
|
|
|
|
rFileInfo->m_FullPath += StringFromFormat("%s/", filename.c_str());
|
2008-12-07 22:30:24 -07:00
|
|
|
else
|
2014-03-14 21:38:14 -06:00
|
|
|
rFileInfo->m_FullPath += StringFromFormat("%s%s/", _szDirectory.c_str(), filename.c_str());
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2008-12-20 11:46:49 -07:00
|
|
|
CurrentIndex = BuildFilenames(CurrentIndex + 1, (size_t) rFileInfo->m_FileSize, rFileInfo->m_FullPath, _NameTableOffset);
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
2014-03-14 21:38:14 -06:00
|
|
|
else // This is a filename
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2014-03-14 21:38:14 -06:00
|
|
|
if (_szDirectory.empty())
|
|
|
|
rFileInfo->m_FullPath += filename;
|
2008-12-07 22:30:24 -07:00
|
|
|
else
|
2014-03-14 21:38:14 -06:00
|
|
|
rFileInfo->m_FullPath += StringFromFormat("%s%s", _szDirectory.c_str(), filename.c_str());
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
CurrentIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CurrentIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|