yuzu/src/core/file_sys/partition_filesystem.cpp

137 lines
4.6 KiB
C++
Raw Normal View History

2018-04-16 10:29:15 +00:00
// Copyright 2018 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
2018-04-16 12:51:59 +00:00
#include <utility>
2018-04-16 10:29:15 +00:00
#include "common/file_util.h"
#include "common/logging/log.h"
#include "core/file_sys/partition_filesystem.h"
#include "core/loader/loader.h"
namespace FileSys {
Loader::ResultStatus PartitionFilesystem::Load(const std::string& file_path, size_t offset) {
FileUtil::IOFile file(file_path, "rb");
if (!file.IsOpen())
return Loader::ResultStatus::Error;
2018-04-16 10:29:15 +00:00
// At least be as large as the header
if (file.GetSize() < sizeof(Header))
return Loader::ResultStatus::Error;
2018-04-16 10:29:15 +00:00
file.Seek(offset, SEEK_SET);
2018-04-16 10:29:15 +00:00
// For cartridges, HFSs can get very large, so we need to calculate the size up to
// the actual content itself instead of just blindly reading in the entire file.
Header pfs_header;
if (!file.ReadBytes(&pfs_header, sizeof(Header)))
return Loader::ResultStatus::Error;
2018-04-16 10:29:15 +00:00
if (pfs_header.magic != Common::MakeMagic('H', 'F', 'S', '0') &&
pfs_header.magic != Common::MakeMagic('P', 'F', 'S', '0')) {
return Loader::ResultStatus::ErrorInvalidFormat;
}
bool is_hfs = pfs_header.magic == Common::MakeMagic('H', 'F', 'S', '0');
2018-04-16 10:29:15 +00:00
size_t entry_size = is_hfs ? sizeof(HFSEntry) : sizeof(PFSEntry);
size_t metadata_size =
sizeof(Header) + (pfs_header.num_entries * entry_size) + pfs_header.strtab_size;
// Actually read in now...
file.Seek(offset, SEEK_SET);
std::vector<u8> file_data(metadata_size);
2018-04-16 10:29:15 +00:00
if (!file.ReadBytes(file_data.data(), metadata_size))
return Loader::ResultStatus::Error;
2018-04-16 10:29:15 +00:00
Loader::ResultStatus result = Load(file_data);
if (result != Loader::ResultStatus::Success)
LOG_ERROR(Service_FS, "Failed to load PFS from file {}!", file_path);
return result;
}
2018-04-16 10:29:15 +00:00
Loader::ResultStatus PartitionFilesystem::Load(const std::vector<u8>& file_data, size_t offset) {
size_t total_size = file_data.size() - offset;
if (total_size < sizeof(Header))
return Loader::ResultStatus::Error;
memcpy(&pfs_header, &file_data[offset], sizeof(Header));
if (pfs_header.magic != Common::MakeMagic('H', 'F', 'S', '0') &&
pfs_header.magic != Common::MakeMagic('P', 'F', 'S', '0')) {
return Loader::ResultStatus::ErrorInvalidFormat;
}
is_hfs = pfs_header.magic == Common::MakeMagic('H', 'F', 'S', '0');
2018-04-16 10:29:15 +00:00
size_t entries_offset = offset + sizeof(Header);
size_t entry_size = is_hfs ? sizeof(HFSEntry) : sizeof(PFSEntry);
2018-04-16 10:29:15 +00:00
size_t strtab_offset = entries_offset + (pfs_header.num_entries * entry_size);
for (u16 i = 0; i < pfs_header.num_entries; i++) {
FileEntry entry;
2018-04-16 10:29:15 +00:00
memcpy(&entry.fs_entry, &file_data[entries_offset + (i * entry_size)], sizeof(FSEntry));
entry.name = std::string(reinterpret_cast<const char*>(
&file_data[strtab_offset + entry.fs_entry.strtab_offset]));
pfs_entries.push_back(std::move(entry));
}
2018-04-16 10:29:15 +00:00
content_offset = strtab_offset + pfs_header.strtab_size;
2018-04-16 10:29:15 +00:00
return Loader::ResultStatus::Success;
2018-04-16 10:29:15 +00:00
}
u32 PartitionFilesystem::GetNumEntries() const {
return pfs_header.num_entries;
2018-04-16 10:29:15 +00:00
}
u64 PartitionFilesystem::GetEntryOffset(u32 index) const {
if (index > GetNumEntries())
return 0;
return content_offset + pfs_entries[index].fs_entry.offset;
2018-04-16 10:29:15 +00:00
}
u64 PartitionFilesystem::GetEntrySize(u32 index) const {
if (index > GetNumEntries())
return 0;
return pfs_entries[index].fs_entry.size;
}
2018-04-16 10:29:15 +00:00
std::string PartitionFilesystem::GetEntryName(u32 index) const {
if (index > GetNumEntries())
return "";
return pfs_entries[index].name;
2018-04-16 10:29:15 +00:00
}
u64 PartitionFilesystem::GetFileOffset(const std::string& name) const {
2018-04-16 10:29:15 +00:00
for (u32 i = 0; i < pfs_header.num_entries; i++) {
if (pfs_entries[i].name == name)
return content_offset + pfs_entries[i].fs_entry.offset;
2018-04-16 10:29:15 +00:00
}
return 0;
}
2018-04-16 10:29:15 +00:00
u64 PartitionFilesystem::GetFileSize(const std::string& name) const {
for (u32 i = 0; i < pfs_header.num_entries; i++) {
if (pfs_entries[i].name == name)
return pfs_entries[i].fs_entry.size;
}
2018-04-16 10:29:15 +00:00
return 0;
}
void PartitionFilesystem::Print() const {
LOG_DEBUG(Service_FS, "Magic: {}", pfs_header.magic);
LOG_DEBUG(Service_FS, "Files: {}", pfs_header.num_entries);
for (u32 i = 0; i < pfs_header.num_entries; i++) {
LOG_DEBUG(Service_FS, " > File {}: {} (0x{:X} bytes, at 0x{:X})", i,
pfs_entries[i].name.c_str(), pfs_entries[i].fs_entry.size,
GetFileOffset(pfs_entries[i].name));
}
2018-04-16 10:29:15 +00:00
}
} // namespace FileSys