file_sys/archive_ncch: Use AM to get title content path, add ExeFS support and support for additional content indexes

This commit is contained in:
shinyquagsire23 2017-10-05 16:24:56 -06:00
parent a4af750759
commit 1ac5137655
3 changed files with 249 additions and 51 deletions

View File

@ -14,7 +14,7 @@
#include "core/file_sys/errors.h" #include "core/file_sys/errors.h"
#include "core/file_sys/ivfc_archive.h" #include "core/file_sys/ivfc_archive.h"
#include "core/file_sys/ncch_container.h" #include "core/file_sys/ncch_container.h"
#include "core/file_sys/title_metadata.h" #include "core/hle/service/am/am.h"
#include "core/hle/service/fs/archive.h" #include "core/hle/service/fs/archive.h"
#include "core/loader/loader.h" #include "core/loader/loader.h"
@ -23,42 +23,72 @@
namespace FileSys { namespace FileSys {
static std::string GetNCCHContainerPath(const std::string& nand_directory) { enum class NCCHFilePathType : u32 {
return Common::StringFromFormat("%s%s/title/", nand_directory.c_str(), SYSTEM_ID); RomFS = 0,
Code = 1,
ExeFS = 2,
};
struct NCCHArchivePath {
u64_le tid;
u32_le media_type;
u32_le unknown;
};
static_assert(sizeof(NCCHArchivePath) == 0x10, "NCCHArchivePath has wrong size!");
struct NCCHFilePath {
u32_le open_type;
u32_le content_index;
u32_le filepath_type;
std::array<char, 8> exefs_filepath;
};
static_assert(sizeof(NCCHFilePath) == 0x14, "NCCHFilePath has wrong size!");
ResultVal<std::unique_ptr<FileBackend>> NCCHArchive::OpenFile(const Path& path,
const Mode& mode) const {
if (path.GetType() != LowPathType::Binary) {
LOG_ERROR(Service_FS, "Path need to be Binary");
return ERROR_INVALID_PATH;
} }
static std::string GetNCCHPath(const std::string& mount_point, u32 high, u32 low) { std::vector<u8> binary = path.AsBinary();
u32 content_id = 0; if (binary.size() != sizeof(NCCHFilePath)) {
LOG_ERROR(Service_FS, "Wrong path size %zu", binary.size());
// TODO(shinyquagsire23): Title database should be doing this path lookup return ERROR_INVALID_PATH;
std::string content_path =
Common::StringFromFormat("%s%08x/%08x/content/", mount_point.c_str(), high, low);
std::string tmd_path = content_path + "00000000.tmd";
TitleMetadata tmd(tmd_path);
if (tmd.Load() == Loader::ResultStatus::Success) {
content_id = tmd.GetBootContentID();
} }
return Common::StringFromFormat("%s%08x.app", content_path.c_str(), content_id); NCCHFilePath openfile_path;
} std::memcpy(&openfile_path, binary.data(), sizeof(NCCHFilePath));
ArchiveFactory_NCCH::ArchiveFactory_NCCH(const std::string& nand_directory) std::string file_path =
: mount_point(GetNCCHContainerPath(nand_directory)) {} Service::AM::GetTitleContentPath(media_type, title_id, openfile_path.content_index);
auto ncch_container = NCCHContainer(file_path);
ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_NCCH::Open(const Path& path) { Loader::ResultStatus result;
auto vec = path.AsBinary(); std::unique_ptr<FileBackend> file;
const u32* data = reinterpret_cast<u32*>(vec.data());
u32 high = data[1];
u32 low = data[0];
std::string file_path = GetNCCHPath(mount_point, high, low);
// NCCH RomFS
NCCHFilePathType filepath_type = static_cast<NCCHFilePathType>(openfile_path.filepath_type);
if (filepath_type == NCCHFilePathType::RomFS) {
std::shared_ptr<FileUtil::IOFile> romfs_file; std::shared_ptr<FileUtil::IOFile> romfs_file;
u64 romfs_offset = 0; u64 romfs_offset = 0;
u64 romfs_size = 0; u64 romfs_size = 0;
auto ncch_container = NCCHContainer(file_path);
if (ncch_container.ReadRomFS(romfs_file, romfs_offset, romfs_size) != result = ncch_container.ReadRomFS(romfs_file, romfs_offset, romfs_size);
Loader::ResultStatus::Success) { file = std::make_unique<IVFCFile>(romfs_file, romfs_offset, romfs_size);
} else if (filepath_type == NCCHFilePathType::Code ||
filepath_type == NCCHFilePathType::ExeFS) {
std::vector<u8> buffer;
// Load NCCH .code or icon/banner/logo
result = ncch_container.LoadSectionExeFS(openfile_path.exefs_filepath.data(), buffer);
file = std::make_unique<NCCHFile>(buffer);
} else {
LOG_ERROR(Service_FS, "Unknown NCCH archive type %u!", openfile_path.filepath_type);
result = Loader::ResultStatus::Error;
}
if (result != Loader::ResultStatus::Success) {
// High Title ID of the archive: The category (https://3dbrew.org/wiki/Title_list). // High Title ID of the archive: The category (https://3dbrew.org/wiki/Title_list).
constexpr u32 shared_data_archive = 0x0004009B; constexpr u32 shared_data_archive = 0x0004009B;
constexpr u32 system_data_archive = 0x000400DB; constexpr u32 system_data_archive = 0x000400DB;
@ -68,32 +98,149 @@ ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_NCCH::Open(const Path&
constexpr u32 region_manifest = 0x00010402; constexpr u32 region_manifest = 0x00010402;
constexpr u32 ng_word_list = 0x00010302; constexpr u32 ng_word_list = 0x00010302;
u32 high = static_cast<u32>(title_id >> 32);
u32 low = static_cast<u32>(title_id & 0xFFFFFFFF);
LOG_DEBUG(Service_FS, "Full Path: %s. Category: 0x%X. Path: 0x%X.", path.DebugStr().c_str(), LOG_DEBUG(Service_FS, "Full Path: %s. Category: 0x%X. Path: 0x%X.", path.DebugStr().c_str(),
high, low); high, low);
std::string archive_name;
if (high == shared_data_archive) { if (high == shared_data_archive) {
if (low == mii_data) { if (low == mii_data)
LOG_ERROR(Service_FS, "Failed to get a handle for shared data archive: Mii data. "); archive_name = "Mii Data";
Core::System::GetInstance().SetStatus(Core::System::ResultStatus::ErrorSystemFiles, else if (low == region_manifest)
"Mii data"); archive_name = "Region manifest";
} else if (low == region_manifest) {
LOG_ERROR(Service_FS,
"Failed to get a handle for shared data archive: region manifest.");
Core::System::GetInstance().SetStatus(Core::System::ResultStatus::ErrorSystemFiles,
"Region manifest");
}
} else if (high == system_data_archive) { } else if (high == system_data_archive) {
if (low == ng_word_list) { if (low == ng_word_list)
LOG_ERROR(Service_FS, archive_name = "NG bad word list";
"Failed to get a handle for system data archive: NG bad word list.");
Core::System::GetInstance().SetStatus(Core::System::ResultStatus::ErrorSystemFiles,
"NG bad word list");
} }
if (!archive_name.empty()) {
LOG_ERROR(Service_FS, "Failed to get a handle for shared data archive: %s. ",
archive_name.c_str());
Core::System::GetInstance().SetStatus(Core::System::ResultStatus::ErrorSystemFiles,
archive_name.c_str());
} }
return ERROR_NOT_FOUND; return ERROR_NOT_FOUND;
} }
auto archive = std::make_unique<IVFCArchive>(romfs_file, romfs_offset, romfs_size); return MakeResult<std::unique_ptr<FileBackend>>(std::move(file));
}
ResultCode NCCHArchive::DeleteFile(const Path& path) const {
LOG_CRITICAL(Service_FS, "Attempted to delete a file from an NCCH archive (%s).",
GetName().c_str());
// TODO(Subv): Verify error code
return ResultCode(ErrorDescription::NoData, ErrorModule::FS, ErrorSummary::Canceled,
ErrorLevel::Status);
}
ResultCode NCCHArchive::RenameFile(const Path& src_path, const Path& dest_path) const {
LOG_CRITICAL(Service_FS, "Attempted to rename a file within an NCCH archive (%s).",
GetName().c_str());
// TODO(wwylele): Use correct error code
return ResultCode(-1);
}
ResultCode NCCHArchive::DeleteDirectory(const Path& path) const {
LOG_CRITICAL(Service_FS, "Attempted to delete a directory from an NCCH archive (%s).",
GetName().c_str());
// TODO(wwylele): Use correct error code
return ResultCode(-1);
}
ResultCode NCCHArchive::DeleteDirectoryRecursively(const Path& path) const {
LOG_CRITICAL(Service_FS, "Attempted to delete a directory from an NCCH archive (%s).",
GetName().c_str());
// TODO(wwylele): Use correct error code
return ResultCode(-1);
}
ResultCode NCCHArchive::CreateFile(const Path& path, u64 size) const {
LOG_CRITICAL(Service_FS, "Attempted to create a file in an NCCH archive (%s).",
GetName().c_str());
// TODO: Verify error code
return ResultCode(ErrorDescription::NotAuthorized, ErrorModule::FS, ErrorSummary::NotSupported,
ErrorLevel::Permanent);
}
ResultCode NCCHArchive::CreateDirectory(const Path& path) const {
LOG_CRITICAL(Service_FS, "Attempted to create a directory in an NCCH archive (%s).",
GetName().c_str());
// TODO(wwylele): Use correct error code
return ResultCode(-1);
}
ResultCode NCCHArchive::RenameDirectory(const Path& src_path, const Path& dest_path) const {
LOG_CRITICAL(Service_FS, "Attempted to rename a file within an NCCH archive (%s).",
GetName().c_str());
// TODO(wwylele): Use correct error code
return ResultCode(-1);
}
ResultVal<std::unique_ptr<DirectoryBackend>> NCCHArchive::OpenDirectory(const Path& path) const {
LOG_CRITICAL(Service_FS, "Attempted to open a directory within an NCCH archive (%s).",
GetName().c_str());
// TODO(shinyquagsire23): Use correct error code
return ResultCode(-1);
}
u64 NCCHArchive::GetFreeBytes() const {
LOG_WARNING(Service_FS, "Attempted to get the free space in an NCCH archive");
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
ResultVal<size_t> NCCHFile::Read(const u64 offset, const size_t length, u8* buffer) const {
LOG_TRACE(Service_FS, "called offset=%" PRIu64 ", length=%zu", offset, length);
size_t length_left = static_cast<size_t>(data_size - offset);
size_t read_length = static_cast<size_t>(std::min(length, length_left));
size_t available_size = static_cast<size_t>(file_buffer.size() - offset);
size_t copy_size = std::min(length, available_size);
memcpy(buffer, file_buffer.data() + offset, copy_size);
return MakeResult<size_t>(copy_size);
}
ResultVal<size_t> NCCHFile::Write(const u64 offset, const size_t length, const bool flush,
const u8* buffer) const {
LOG_ERROR(Service_FS, "Attempted to write to NCCH file");
// TODO(shinyquagsire23): Find error code
return MakeResult<size_t>(0);
}
u64 NCCHFile::GetSize() const {
return file_buffer.size();
}
bool NCCHFile::SetSize(const u64 size) const {
LOG_ERROR(Service_FS, "Attempted to set the size of an NCCH file");
return false;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
ArchiveFactory_NCCH::ArchiveFactory_NCCH() {}
ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_NCCH::Open(const Path& path) {
if (path.GetType() != LowPathType::Binary) {
LOG_ERROR(Service_FS, "Path need to be Binary");
return ERROR_INVALID_PATH;
}
std::vector<u8> binary = path.AsBinary();
if (binary.size() != sizeof(NCCHArchivePath)) {
LOG_ERROR(Service_FS, "Wrong path size %zu", binary.size());
return ERROR_INVALID_PATH;
}
NCCHArchivePath open_path;
std::memcpy(&open_path, binary.data(), sizeof(NCCHArchivePath));
auto archive = std::make_unique<NCCHArchive>(
open_path.tid, static_cast<Service::FS::MediaType>(open_path.media_type & 0xFF));
return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive)); return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
} }

View File

@ -7,17 +7,71 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "core/file_sys/archive_backend.h" #include "core/file_sys/archive_backend.h"
#include "core/file_sys/file_backend.h"
#include "core/hle/result.h" #include "core/hle/result.h"
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
// FileSys namespace // FileSys namespace
namespace Service {
namespace FS {
enum class MediaType : u32;
}
}
namespace FileSys { namespace FileSys {
/// Archive backend for NCCH Archives (RomFS, ExeFS)
class NCCHArchive : public ArchiveBackend {
public:
explicit NCCHArchive(u64 title_id, Service::FS::MediaType media_type)
: title_id(title_id), media_type(media_type) {}
std::string GetName() const override {
return "NCCHArchive";
}
ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path,
const Mode& mode) const override;
ResultCode DeleteFile(const Path& path) const override;
ResultCode RenameFile(const Path& src_path, const Path& dest_path) const override;
ResultCode DeleteDirectory(const Path& path) const override;
ResultCode DeleteDirectoryRecursively(const Path& path) const override;
ResultCode CreateFile(const Path& path, u64 size) const override;
ResultCode CreateDirectory(const Path& path) const override;
ResultCode RenameDirectory(const Path& src_path, const Path& dest_path) const override;
ResultVal<std::unique_ptr<DirectoryBackend>> OpenDirectory(const Path& path) const override;
u64 GetFreeBytes() const override;
protected:
u64 title_id;
Service::FS::MediaType media_type;
};
// File backend for NCCH files
class NCCHFile : public FileBackend {
public:
NCCHFile(std::vector<u8> buffer) : file_buffer(buffer) {}
ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override;
ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) const override;
u64 GetSize() const override;
bool SetSize(u64 size) const override;
bool Close() const override {
return false;
}
void Flush() const override {}
private:
std::vector<u8> file_buffer;
u64 data_offset;
u64 data_size;
};
/// File system interface to the NCCH archive /// File system interface to the NCCH archive
class ArchiveFactory_NCCH final : public ArchiveFactory { class ArchiveFactory_NCCH final : public ArchiveFactory {
public: public:
explicit ArchiveFactory_NCCH(const std::string& mount_point); explicit ArchiveFactory_NCCH();
std::string GetName() const override { std::string GetName() const override {
return "NCCH"; return "NCCH";
@ -26,9 +80,6 @@ public:
ResultVal<std::unique_ptr<ArchiveBackend>> Open(const Path& path) override; ResultVal<std::unique_ptr<ArchiveBackend>> Open(const Path& path) override;
ResultCode Format(const Path& path, const FileSys::ArchiveFormatInfo& format_info) override; ResultCode Format(const Path& path, const FileSys::ArchiveFormatInfo& format_info) override;
ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override; ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override;
private:
std::string mount_point;
}; };
} // namespace FileSys } // namespace FileSys

View File

@ -559,7 +559,7 @@ void RegisterArchiveTypes() {
sharedextsavedata_factory->GetMountPoint().c_str()); sharedextsavedata_factory->GetMountPoint().c_str());
// Create the NCCH archive, basically a small variation of the RomFS archive // Create the NCCH archive, basically a small variation of the RomFS archive
auto savedatacheck_factory = std::make_unique<FileSys::ArchiveFactory_NCCH>(nand_directory); auto savedatacheck_factory = std::make_unique<FileSys::ArchiveFactory_NCCH>();
RegisterArchiveType(std::move(savedatacheck_factory), ArchiveIdCode::NCCH); RegisterArchiveType(std::move(savedatacheck_factory), ArchiveIdCode::NCCH);
auto systemsavedata_factory = auto systemsavedata_factory =