diff options
author | Zach Hilman <DarkLordZach@users.noreply.github.com> | 2018-07-19 03:07:11 +0200 |
---|---|---|
committer | bunnei <bunneidev@gmail.com> | 2018-07-19 03:07:11 +0200 |
commit | 29aff8d5ab46c8d0199aa4bfa7eeff5d4fa2d7ef (patch) | |
tree | 3202e2ce55ab6387a4ca366a509eccdd963434c3 /src/core/loader | |
parent | Merge pull request #683 from DarkLordZach/touch (diff) | |
download | yuzu-29aff8d5ab46c8d0199aa4bfa7eeff5d4fa2d7ef.tar yuzu-29aff8d5ab46c8d0199aa4bfa7eeff5d4fa2d7ef.tar.gz yuzu-29aff8d5ab46c8d0199aa4bfa7eeff5d4fa2d7ef.tar.bz2 yuzu-29aff8d5ab46c8d0199aa4bfa7eeff5d4fa2d7ef.tar.lz yuzu-29aff8d5ab46c8d0199aa4bfa7eeff5d4fa2d7ef.tar.xz yuzu-29aff8d5ab46c8d0199aa4bfa7eeff5d4fa2d7ef.tar.zst yuzu-29aff8d5ab46c8d0199aa4bfa7eeff5d4fa2d7ef.zip |
Diffstat (limited to 'src/core/loader')
-rw-r--r-- | src/core/loader/deconstructed_rom_directory.cpp | 105 | ||||
-rw-r--r-- | src/core/loader/deconstructed_rom_directory.h | 15 | ||||
-rw-r--r-- | src/core/loader/elf.cpp | 24 | ||||
-rw-r--r-- | src/core/loader/elf.h | 12 | ||||
-rw-r--r-- | src/core/loader/loader.cpp | 63 | ||||
-rw-r--r-- | src/core/loader/loader.h | 30 | ||||
-rw-r--r-- | src/core/loader/nca.cpp | 248 | ||||
-rw-r--r-- | src/core/loader/nca.h | 19 | ||||
-rw-r--r-- | src/core/loader/nro.cpp | 32 | ||||
-rw-r--r-- | src/core/loader/nro.h | 13 | ||||
-rw-r--r-- | src/core/loader/nso.cpp | 93 | ||||
-rw-r--r-- | src/core/loader/nso.h | 17 |
12 files changed, 146 insertions, 525 deletions
diff --git a/src/core/loader/deconstructed_rom_directory.cpp b/src/core/loader/deconstructed_rom_directory.cpp index 0b11bf4f3..19b8667ba 100644 --- a/src/core/loader/deconstructed_rom_directory.cpp +++ b/src/core/loader/deconstructed_rom_directory.cpp @@ -4,11 +4,10 @@ #include <cinttypes> #include "common/common_funcs.h" -#include "common/common_paths.h" #include "common/file_util.h" #include "common/logging/log.h" #include "common/string_util.h" -#include "core/file_sys/romfs_factory.h" +#include "core/file_sys/content_archive.h" #include "core/gdbstub/gdbstub.h" #include "core/hle/kernel/process.h" #include "core/hle/kernel/resource_limit.h" @@ -47,55 +46,11 @@ static std::string FindRomFS(const std::string& directory) { return filepath_romfs; } -AppLoader_DeconstructedRomDirectory::AppLoader_DeconstructedRomDirectory(FileUtil::IOFile&& file, - std::string filepath) - : AppLoader(std::move(file)), filepath(std::move(filepath)) {} - -FileType AppLoader_DeconstructedRomDirectory::IdentifyType(FileUtil::IOFile& file, - const std::string& filepath) { - bool is_main_found{}; - bool is_npdm_found{}; - bool is_rtld_found{}; - bool is_sdk_found{}; - - const auto callback = [&](unsigned* num_entries_out, const std::string& directory, - const std::string& virtual_name) -> bool { - // Skip directories - std::string physical_name = directory + virtual_name; - if (FileUtil::IsDirectory(physical_name)) { - return true; - } - - // Verify filename - if (Common::ToLower(virtual_name) == "main") { - is_main_found = true; - } else if (Common::ToLower(virtual_name) == "main.npdm") { - is_npdm_found = true; - return true; - } else if (Common::ToLower(virtual_name) == "rtld") { - is_rtld_found = true; - } else if (Common::ToLower(virtual_name) == "sdk") { - is_sdk_found = true; - } else { - // Continue searching - return true; - } - - // Verify file is an NSO - FileUtil::IOFile file(physical_name, "rb"); - if (AppLoader_NSO::IdentifyType(file, physical_name) != FileType::NSO) { - return false; - } - - // We are done if we've found and verified all required NSOs - return !(is_main_found && is_npdm_found && is_rtld_found && is_sdk_found); - }; +AppLoader_DeconstructedRomDirectory::AppLoader_DeconstructedRomDirectory(FileSys::VirtualFile file) + : AppLoader(std::move(file)) {} - // Search the directory recursively, looking for the required modules - const std::string directory = filepath.substr(0, filepath.find_last_of("/\\")) + DIR_SEP; - FileUtil::ForeachDirectoryEntry(nullptr, directory, callback); - - if (is_main_found && is_npdm_found && is_rtld_found && is_sdk_found) { +FileType AppLoader_DeconstructedRomDirectory::IdentifyType(const FileSys::VirtualFile& file) { + if (FileSys::IsDirectoryExeFS(file->GetContainingDirectory())) { return FileType::DeconstructedRomDirectory; } @@ -107,14 +62,13 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load( if (is_loaded) { return ResultStatus::ErrorAlreadyLoaded; } - if (!file.IsOpen()) { - return ResultStatus::Error; - } - const std::string directory = filepath.substr(0, filepath.find_last_of("/\\")) + DIR_SEP; - const std::string npdm_path = directory + DIR_SEP + "main.npdm"; + const FileSys::VirtualDir dir = file->GetContainingDirectory(); + const FileSys::VirtualFile npdm = dir->GetFile("main.npdm"); + if (npdm == nullptr) + return ResultStatus::ErrorInvalidFormat; - ResultStatus result = metadata.Load(npdm_path); + ResultStatus result = metadata.Load(npdm); if (result != ResultStatus::Success) { return result; } @@ -129,9 +83,10 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load( VAddr next_load_addr{Memory::PROCESS_IMAGE_VADDR}; for (const auto& module : {"rtld", "main", "subsdk0", "subsdk1", "subsdk2", "subsdk3", "subsdk4", "subsdk5", "subsdk6", "subsdk7", "sdk"}) { - const std::string path = directory + DIR_SEP + module; const VAddr load_addr = next_load_addr; - next_load_addr = AppLoader_NSO::LoadModule(path, load_addr); + const FileSys::VirtualFile module_file = dir->GetFile(module); + if (module_file != nullptr) + next_load_addr = AppLoader_NSO::LoadModule(module_file, load_addr); if (next_load_addr) { LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", module, load_addr); // Register module with GDBStub @@ -150,10 +105,15 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load( metadata.GetMainThreadStackSize()); // Find the RomFS by searching for a ".romfs" file in this directory - filepath_romfs = FindRomFS(directory); + const auto& files = dir->GetFiles(); + const auto romfs_iter = + std::find_if(files.begin(), files.end(), [](const FileSys::VirtualFile& file) { + return file->GetName().find(".romfs") != std::string::npos; + }); // Register the RomFS if a ".romfs" file was found - if (!filepath_romfs.empty()) { + if (romfs_iter != files.end() && *romfs_iter != nullptr) { + romfs = *romfs_iter; Service::FileSystem::RegisterRomFS(std::make_unique<FileSys::RomFSFactory>(*this)); } @@ -161,29 +121,10 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load( return ResultStatus::Success; } -ResultStatus AppLoader_DeconstructedRomDirectory::ReadRomFS( - std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, u64& size) { - - if (filepath_romfs.empty()) { - LOG_DEBUG(Loader, "No RomFS available"); +ResultStatus AppLoader_DeconstructedRomDirectory::ReadRomFS(FileSys::VirtualFile& dir) { + if (romfs == nullptr) return ResultStatus::ErrorNotUsed; - } - - // We reopen the file, to allow its position to be independent - romfs_file = std::make_shared<FileUtil::IOFile>(filepath_romfs, "rb"); - if (!romfs_file->IsOpen()) { - return ResultStatus::Error; - } - - offset = 0; - size = romfs_file->GetSize(); - - LOG_DEBUG(Loader, "RomFS offset: 0x{:016X}", offset); - LOG_DEBUG(Loader, "RomFS size: 0x{:016X}", size); - - // Reset read pointer - file.Seek(0, SEEK_SET); - + dir = romfs; return ResultStatus::Success; } diff --git a/src/core/loader/deconstructed_rom_directory.h b/src/core/loader/deconstructed_rom_directory.h index 23295d911..982a037f7 100644 --- a/src/core/loader/deconstructed_rom_directory.h +++ b/src/core/loader/deconstructed_rom_directory.h @@ -20,29 +20,26 @@ namespace Loader { */ class AppLoader_DeconstructedRomDirectory final : public AppLoader { public: - AppLoader_DeconstructedRomDirectory(FileUtil::IOFile&& file, std::string filepath); + explicit AppLoader_DeconstructedRomDirectory(FileSys::VirtualFile main_file); /** * Returns the type of the file - * @param file FileUtil::IOFile open file - * @param filepath Path of the file that we are opening. + * @param file std::shared_ptr<VfsFile> open file * @return FileType found, or FileType::Error if this loader doesn't know it */ - static FileType IdentifyType(FileUtil::IOFile& file, const std::string& filepath); + static FileType IdentifyType(const FileSys::VirtualFile& file); FileType GetFileType() override { - return IdentifyType(file, filepath); + return IdentifyType(file); } ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; - ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, - u64& size) override; + ResultStatus ReadRomFS(FileSys::VirtualFile& dir) override; private: - std::string filepath_romfs; - std::string filepath; FileSys::ProgramMetadata metadata; + FileSys::VirtualFile romfs; }; } // namespace Loader diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp index b69e5c6ef..4bfd5f536 100644 --- a/src/core/loader/elf.cpp +++ b/src/core/loader/elf.cpp @@ -365,20 +365,17 @@ SectionID ElfReader::GetSectionByName(const char* name, int firstSection) const namespace Loader { -AppLoader_ELF::AppLoader_ELF(FileUtil::IOFile&& file, std::string filename) - : AppLoader(std::move(file)), filename(std::move(filename)) {} +AppLoader_ELF::AppLoader_ELF(FileSys::VirtualFile file) : AppLoader(std::move(file)) {} -FileType AppLoader_ELF::IdentifyType(FileUtil::IOFile& file, const std::string&) { +FileType AppLoader_ELF::IdentifyType(const FileSys::VirtualFile& file) { static constexpr u16 ELF_MACHINE_ARM{0x28}; u32 magic = 0; - file.Seek(0, SEEK_SET); - if (1 != file.ReadArray<u32>(&magic, 1)) + if (4 != file->ReadObject(&magic)) return FileType::Error; u16 machine = 0; - file.Seek(18, SEEK_SET); - if (1 != file.ReadArray<u16>(&machine, 1)) + if (2 != file->ReadObject(&machine, 18)) return FileType::Error; if (Common::MakeMagic('\x7f', 'E', 'L', 'F') == magic && ELF_MACHINE_ARM == machine) @@ -391,20 +388,13 @@ ResultStatus AppLoader_ELF::Load(Kernel::SharedPtr<Kernel::Process>& process) { if (is_loaded) return ResultStatus::ErrorAlreadyLoaded; - if (!file.IsOpen()) - return ResultStatus::Error; - - // Reset read pointer in case this file has been read before. - file.Seek(0, SEEK_SET); - - size_t size = file.GetSize(); - std::unique_ptr<u8[]> buffer(new u8[size]); - if (file.ReadBytes(&buffer[0], size) != size) + std::vector<u8> buffer = file->ReadAllBytes(); + if (buffer.size() != file->GetSize()) return ResultStatus::Error; ElfReader elf_reader(&buffer[0]); SharedPtr<CodeSet> codeset = elf_reader.LoadInto(Memory::PROCESS_IMAGE_VADDR); - codeset->name = filename; + codeset->name = file->GetName(); process->LoadModule(codeset, codeset->entrypoint); process->svc_access_mask.set(); diff --git a/src/core/loader/elf.h b/src/core/loader/elf.h index ee741a789..b8fb982d0 100644 --- a/src/core/loader/elf.h +++ b/src/core/loader/elf.h @@ -16,24 +16,20 @@ namespace Loader { /// Loads an ELF/AXF file class AppLoader_ELF final : public AppLoader { public: - AppLoader_ELF(FileUtil::IOFile&& file, std::string filename); + explicit AppLoader_ELF(FileSys::VirtualFile file); /** * Returns the type of the file - * @param file FileUtil::IOFile open file - * @param filepath Path of the file that we are opening. + * @param file std::shared_ptr<VfsFile> open file * @return FileType found, or FileType::Error if this loader doesn't know it */ - static FileType IdentifyType(FileUtil::IOFile& file, const std::string& filepath); + static FileType IdentifyType(const FileSys::VirtualFile& file); FileType GetFileType() override { - return IdentifyType(file, filename); + return IdentifyType(file); } ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; - -private: - std::string filename; }; } // namespace Loader diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp index 8831d8e83..1574345a1 100644 --- a/src/core/loader/loader.cpp +++ b/src/core/loader/loader.cpp @@ -6,6 +6,7 @@ #include <string> #include "common/logging/log.h" #include "common/string_util.h" +#include "core/file_sys/vfs_real.h" #include "core/hle/kernel/process.h" #include "core/loader/deconstructed_rom_directory.h" #include "core/loader/elf.h" @@ -21,11 +22,11 @@ const std::initializer_list<Kernel::AddressMapping> default_address_mappings = { {0x1F000000, 0x600000, false}, // entire VRAM }; -FileType IdentifyFile(FileUtil::IOFile& file, const std::string& filepath) { +FileType IdentifyFile(FileSys::VirtualFile file) { FileType type; #define CHECK_TYPE(loader) \ - type = AppLoader_##loader::IdentifyType(file, filepath); \ + type = AppLoader_##loader::IdentifyType(file); \ if (FileType::Error != type) \ return type; @@ -41,25 +42,22 @@ FileType IdentifyFile(FileUtil::IOFile& file, const std::string& filepath) { } FileType IdentifyFile(const std::string& file_name) { - FileUtil::IOFile file(file_name, "rb"); - if (!file.IsOpen()) { - LOG_ERROR(Loader, "Failed to load file {}", file_name); - return FileType::Unknown; - } - - return IdentifyFile(file, file_name); + return IdentifyFile(FileSys::VirtualFile(std::make_shared<FileSys::RealVfsFile>(file_name))); } -FileType GuessFromExtension(const std::string& extension_) { - std::string extension = Common::ToLower(extension_); +FileType GuessFromFilename(const std::string& name) { + if (name == "main") + return FileType::DeconstructedRomDirectory; - if (extension == ".elf") + const std::string extension = Common::ToLower(FileUtil::GetExtensionFromFilename(name)); + + if (extension == "elf") return FileType::ELF; - else if (extension == ".nro") + if (extension == "nro") return FileType::NRO; - else if (extension == ".nso") + if (extension == "nso") return FileType::NSO; - else if (extension == ".nca") + if (extension == "nca") return FileType::NCA; return FileType::Unknown; @@ -93,58 +91,47 @@ const char* GetFileTypeString(FileType type) { * @param filepath the file full path (with name) * @return std::unique_ptr<AppLoader> a pointer to a loader object; nullptr for unsupported type */ -static std::unique_ptr<AppLoader> GetFileLoader(FileUtil::IOFile&& file, FileType type, - const std::string& filename, - const std::string& filepath) { +static std::unique_ptr<AppLoader> GetFileLoader(FileSys::VirtualFile file, FileType type) { switch (type) { // Standard ELF file format. case FileType::ELF: - return std::make_unique<AppLoader_ELF>(std::move(file), filename); + return std::make_unique<AppLoader_ELF>(std::move(file)); // NX NSO file format. case FileType::NSO: - return std::make_unique<AppLoader_NSO>(std::move(file), filepath); + return std::make_unique<AppLoader_NSO>(std::move(file)); // NX NRO file format. case FileType::NRO: - return std::make_unique<AppLoader_NRO>(std::move(file), filepath); + return std::make_unique<AppLoader_NRO>(std::move(file)); // NX NCA file format. case FileType::NCA: - return std::make_unique<AppLoader_NCA>(std::move(file), filepath); + return std::make_unique<AppLoader_NCA>(std::move(file)); // NX deconstructed ROM directory. case FileType::DeconstructedRomDirectory: - return std::make_unique<AppLoader_DeconstructedRomDirectory>(std::move(file), filepath); + return std::make_unique<AppLoader_DeconstructedRomDirectory>(std::move(file)); default: return nullptr; } } -std::unique_ptr<AppLoader> GetLoader(const std::string& filename) { - FileUtil::IOFile file(filename, "rb"); - if (!file.IsOpen()) { - LOG_ERROR(Loader, "Failed to load file {}", filename); - return nullptr; - } - - std::string filename_filename, filename_extension; - Common::SplitPath(filename, nullptr, &filename_filename, &filename_extension); - - FileType type = IdentifyFile(file, filename); - FileType filename_type = GuessFromExtension(filename_extension); +std::unique_ptr<AppLoader> GetLoader(FileSys::VirtualFile file) { + FileType type = IdentifyFile(file); + FileType filename_type = GuessFromFilename(file->GetName()); if (type != filename_type) { - LOG_WARNING(Loader, "File {} has a different type than its extension.", filename); + LOG_WARNING(Loader, "File {} has a different type than its extension.", file->GetName()); if (FileType::Unknown == type) type = filename_type; } - LOG_DEBUG(Loader, "Loading file {} as {}...", filename, GetFileTypeString(type)); + LOG_DEBUG(Loader, "Loading file {} as {}...", file->GetName(), GetFileTypeString(type)); - return GetFileLoader(std::move(file), type, filename_filename, filename); + return GetFileLoader(std::move(file), type); } } // namespace Loader diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h index b76f7b13d..1da9e8099 100644 --- a/src/core/loader/loader.h +++ b/src/core/loader/loader.h @@ -13,6 +13,7 @@ #include <boost/optional.hpp> #include "common/common_types.h" #include "common/file_util.h" +#include "core/file_sys/vfs.h" #include "core/hle/kernel/kernel.h" namespace Kernel { @@ -36,10 +37,9 @@ enum class FileType { /** * Identifies the type of a bootable file based on the magic value in its header. * @param file open file - * @param filepath Path of the file that we are opening. * @return FileType of file */ -FileType IdentifyFile(FileUtil::IOFile& file, const std::string& filepath); +FileType IdentifyFile(FileSys::VirtualFile file); /** * Identifies the type of a bootable file based on the magic value in its header. @@ -50,12 +50,12 @@ FileType IdentifyFile(FileUtil::IOFile& file, const std::string& filepath); FileType IdentifyFile(const std::string& file_name); /** - * Guess the type of a bootable file from its extension - * @param extension String extension of bootable file + * Guess the type of a bootable file from its name + * @param name String name of bootable file * @return FileType of file. Note: this will return FileType::Unknown if it is unable to determine * a filetype, and will never return FileType::Error. */ -FileType GuessFromExtension(const std::string& extension); +FileType GuessFromFilename(const std::string& name); /** * Convert a FileType into a string which can be displayed to the user. @@ -79,7 +79,7 @@ enum class ResultStatus { /// Interface for loading an application class AppLoader : NonCopyable { public: - AppLoader(FileUtil::IOFile&& file) : file(std::move(file)) {} + AppLoader(FileSys::VirtualFile file) : file(std::move(file)) {} virtual ~AppLoader() {} /** @@ -154,26 +154,20 @@ public: /** * Get the RomFS of the application * Since the RomFS can be huge, we return a file reference instead of copying to a buffer - * @param romfs_file The file containing the RomFS - * @param offset The offset the romfs begins on - * @param size The size of the romfs + * @param file The file containing the RomFS * @return ResultStatus result of function */ - virtual ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, - u64& size) { + virtual ResultStatus ReadRomFS(FileSys::VirtualFile& dir) { return ResultStatus::ErrorNotImplemented; } /** * Get the update RomFS of the application * Since the RomFS can be huge, we return a file reference instead of copying to a buffer - * @param romfs_file The file containing the RomFS - * @param offset The offset the romfs begins on - * @param size The size of the romfs + * @param file The file containing the RomFS * @return ResultStatus result of function */ - virtual ResultStatus ReadUpdateRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, - u64& size) { + virtual ResultStatus ReadUpdateRomFS(FileSys::VirtualFile& file) { return ResultStatus::ErrorNotImplemented; } @@ -187,7 +181,7 @@ public: } protected: - FileUtil::IOFile file; + FileSys::VirtualFile file; bool is_loaded = false; }; @@ -202,6 +196,6 @@ extern const std::initializer_list<Kernel::AddressMapping> default_address_mappi * @param filename String filename of bootable file * @return best loader for this file */ -std::unique_ptr<AppLoader> GetLoader(const std::string& filename); +std::unique_ptr<AppLoader> GetLoader(FileSys::VirtualFile file); } // namespace Loader diff --git a/src/core/loader/nca.cpp b/src/core/loader/nca.cpp index b463f369c..e73b253b2 100644 --- a/src/core/loader/nca.cpp +++ b/src/core/loader/nca.cpp @@ -4,14 +4,13 @@ #include <vector> -#include "common/common_funcs.h" #include "common/file_util.h" #include "common/logging/log.h" #include "common/string_util.h" #include "common/swap.h" #include "core/core.h" +#include "core/file_sys/content_archive.h" #include "core/file_sys/program_metadata.h" -#include "core/file_sys/romfs_factory.h" #include "core/gdbstub/gdbstub.h" #include "core/hle/kernel/process.h" #include "core/hle/kernel/resource_limit.h" @@ -22,208 +21,15 @@ namespace Loader { -// Media offsets in headers are stored divided by 512. Mult. by this to get real offset. -constexpr u64 MEDIA_OFFSET_MULTIPLIER = 0x200; - -constexpr u64 SECTION_HEADER_SIZE = 0x200; -constexpr u64 SECTION_HEADER_OFFSET = 0x400; - -enum class NcaContentType : u8 { Program = 0, Meta = 1, Control = 2, Manual = 3, Data = 4 }; - -enum class NcaSectionFilesystemType : u8 { PFS0 = 0x2, ROMFS = 0x3 }; - -struct NcaSectionTableEntry { - u32_le media_offset; - u32_le media_end_offset; - INSERT_PADDING_BYTES(0x8); -}; -static_assert(sizeof(NcaSectionTableEntry) == 0x10, "NcaSectionTableEntry has incorrect size."); - -struct NcaHeader { - std::array<u8, 0x100> rsa_signature_1; - std::array<u8, 0x100> rsa_signature_2; - u32_le magic; - u8 is_system; - NcaContentType content_type; - u8 crypto_type; - u8 key_index; - u64_le size; - u64_le title_id; - INSERT_PADDING_BYTES(0x4); - u32_le sdk_version; - u8 crypto_type_2; - INSERT_PADDING_BYTES(15); - std::array<u8, 0x10> rights_id; - std::array<NcaSectionTableEntry, 0x4> section_tables; - std::array<std::array<u8, 0x20>, 0x4> hash_tables; - std::array<std::array<u8, 0x10>, 0x4> key_area; - INSERT_PADDING_BYTES(0xC0); -}; -static_assert(sizeof(NcaHeader) == 0x400, "NcaHeader has incorrect size."); - -struct NcaSectionHeaderBlock { - INSERT_PADDING_BYTES(3); - NcaSectionFilesystemType filesystem_type; - u8 crypto_type; - INSERT_PADDING_BYTES(3); -}; -static_assert(sizeof(NcaSectionHeaderBlock) == 0x8, "NcaSectionHeaderBlock has incorrect size."); - -struct Pfs0Superblock { - NcaSectionHeaderBlock header_block; - std::array<u8, 0x20> hash; - u32_le size; - INSERT_PADDING_BYTES(4); - u64_le hash_table_offset; - u64_le hash_table_size; - u64_le pfs0_header_offset; - u64_le pfs0_size; - INSERT_PADDING_BYTES(432); -}; -static_assert(sizeof(Pfs0Superblock) == 0x200, "Pfs0Superblock has incorrect size."); - -static bool IsValidNca(const NcaHeader& header) { - return header.magic == Common::MakeMagic('N', 'C', 'A', '2') || - header.magic == Common::MakeMagic('N', 'C', 'A', '3'); -} - -// TODO(DarkLordZach): Add support for encrypted. -class Nca final { - std::vector<FileSys::PartitionFilesystem> pfs; - std::vector<u64> pfs_offset; - - u64 romfs_offset = 0; - u64 romfs_size = 0; - - boost::optional<u8> exefs_id = boost::none; - - FileUtil::IOFile file; - std::string path; - - u64 GetExeFsFileOffset(const std::string& file_name) const; - u64 GetExeFsFileSize(const std::string& file_name) const; - -public: - ResultStatus Load(FileUtil::IOFile&& file, std::string path); - - FileSys::PartitionFilesystem GetPfs(u8 id) const; - - u64 GetRomFsOffset() const; - u64 GetRomFsSize() const; - - std::vector<u8> GetExeFsFile(const std::string& file_name); -}; - -static bool IsPfsExeFs(const FileSys::PartitionFilesystem& pfs) { - // According to switchbrew, an exefs must only contain these two files: - return pfs.GetFileSize("main") > 0 && pfs.GetFileSize("main.npdm") > 0; -} - -ResultStatus Nca::Load(FileUtil::IOFile&& in_file, std::string in_path) { - file = std::move(in_file); - path = in_path; - file.Seek(0, SEEK_SET); - std::array<u8, sizeof(NcaHeader)> header_array{}; - if (sizeof(NcaHeader) != file.ReadBytes(header_array.data(), sizeof(NcaHeader))) - LOG_CRITICAL(Loader, "File reader errored out during header read."); - - NcaHeader header{}; - std::memcpy(&header, header_array.data(), sizeof(NcaHeader)); - if (!IsValidNca(header)) - return ResultStatus::ErrorInvalidFormat; - - int number_sections = - std::count_if(std::begin(header.section_tables), std::end(header.section_tables), - [](NcaSectionTableEntry entry) { return entry.media_offset > 0; }); - - for (int i = 0; i < number_sections; ++i) { - // Seek to beginning of this section. - file.Seek(SECTION_HEADER_OFFSET + i * SECTION_HEADER_SIZE, SEEK_SET); - std::array<u8, sizeof(NcaSectionHeaderBlock)> array{}; - if (sizeof(NcaSectionHeaderBlock) != - file.ReadBytes(array.data(), sizeof(NcaSectionHeaderBlock))) - LOG_CRITICAL(Loader, "File reader errored out during header read."); - - NcaSectionHeaderBlock block{}; - std::memcpy(&block, array.data(), sizeof(NcaSectionHeaderBlock)); - - if (block.filesystem_type == NcaSectionFilesystemType::ROMFS) { - romfs_offset = header.section_tables[i].media_offset * MEDIA_OFFSET_MULTIPLIER; - romfs_size = - header.section_tables[i].media_end_offset * MEDIA_OFFSET_MULTIPLIER - romfs_offset; - } else if (block.filesystem_type == NcaSectionFilesystemType::PFS0) { - Pfs0Superblock sb{}; - // Seek back to beginning of this section. - file.Seek(SECTION_HEADER_OFFSET + i * SECTION_HEADER_SIZE, SEEK_SET); - if (sizeof(Pfs0Superblock) != file.ReadBytes(&sb, sizeof(Pfs0Superblock))) - LOG_CRITICAL(Loader, "File reader errored out during header read."); - - u64 offset = (static_cast<u64>(header.section_tables[i].media_offset) * - MEDIA_OFFSET_MULTIPLIER) + - sb.pfs0_header_offset; - FileSys::PartitionFilesystem npfs{}; - ResultStatus status = npfs.Load(path, offset); - - if (status == ResultStatus::Success) { - pfs.emplace_back(std::move(npfs)); - pfs_offset.emplace_back(offset); - } - } - } - - for (size_t i = 0; i < pfs.size(); ++i) { - if (IsPfsExeFs(pfs[i])) - exefs_id = i; - } - - return ResultStatus::Success; -} - -FileSys::PartitionFilesystem Nca::GetPfs(u8 id) const { - return pfs[id]; -} - -u64 Nca::GetExeFsFileOffset(const std::string& file_name) const { - if (exefs_id == boost::none) - return 0; - return pfs[*exefs_id].GetFileOffset(file_name) + pfs_offset[*exefs_id]; -} - -u64 Nca::GetExeFsFileSize(const std::string& file_name) const { - if (exefs_id == boost::none) - return 0; - return pfs[*exefs_id].GetFileSize(file_name); -} - -u64 Nca::GetRomFsOffset() const { - return romfs_offset; -} - -u64 Nca::GetRomFsSize() const { - return romfs_size; -} - -std::vector<u8> Nca::GetExeFsFile(const std::string& file_name) { - std::vector<u8> out(GetExeFsFileSize(file_name)); - file.Seek(GetExeFsFileOffset(file_name), SEEK_SET); - file.ReadBytes(out.data(), GetExeFsFileSize(file_name)); - return out; -} - -AppLoader_NCA::AppLoader_NCA(FileUtil::IOFile&& file, std::string filepath) - : AppLoader(std::move(file)), filepath(std::move(filepath)) {} - -FileType AppLoader_NCA::IdentifyType(FileUtil::IOFile& file, const std::string&) { - file.Seek(0, SEEK_SET); - std::array<u8, 0x400> header_enc_array{}; - if (0x400 != file.ReadBytes(header_enc_array.data(), 0x400)) - return FileType::Error; +AppLoader_NCA::AppLoader_NCA(FileSys::VirtualFile file) : AppLoader(file) {} +FileType AppLoader_NCA::IdentifyType(const FileSys::VirtualFile& file) { // TODO(DarkLordZach): Assuming everything is decrypted. Add crypto support. - NcaHeader header{}; - std::memcpy(&header, header_enc_array.data(), sizeof(NcaHeader)); + FileSys::NCAHeader header{}; + if (sizeof(FileSys::NCAHeader) != file->ReadObject(&header)) + return FileType::Error; - if (IsValidNca(header) && header.content_type == NcaContentType::Program) + if (IsValidNCA(header) && header.content_type == FileSys::NCAContentType::Program) return FileType::NCA; return FileType::Error; @@ -233,17 +39,22 @@ ResultStatus AppLoader_NCA::Load(Kernel::SharedPtr<Kernel::Process>& process) { if (is_loaded) { return ResultStatus::ErrorAlreadyLoaded; } - if (!file.IsOpen()) { - return ResultStatus::Error; - } - nca = std::make_unique<Nca>(); - ResultStatus result = nca->Load(std::move(file), filepath); + nca = std::make_unique<FileSys::NCA>(file); + ResultStatus result = nca->GetStatus(); if (result != ResultStatus::Success) { return result; } - result = metadata.Load(nca->GetExeFsFile("main.npdm")); + if (nca->GetType() != FileSys::NCAContentType::Program) + return ResultStatus::ErrorInvalidFormat; + + auto exefs = nca->GetExeFS(); + + if (exefs == nullptr) + return ResultStatus::ErrorInvalidFormat; + + result = metadata.Load(exefs->GetFile("main.npdm")); if (result != ResultStatus::Success) { return result; } @@ -258,7 +69,8 @@ ResultStatus AppLoader_NCA::Load(Kernel::SharedPtr<Kernel::Process>& process) { for (const auto& module : {"rtld", "main", "subsdk0", "subsdk1", "subsdk2", "subsdk3", "subsdk4", "subsdk5", "subsdk6", "subsdk7", "sdk"}) { const VAddr load_addr = next_load_addr; - next_load_addr = AppLoader_NSO::LoadModule(module, nca->GetExeFsFile(module), load_addr); + + next_load_addr = AppLoader_NSO::LoadModule(exefs->GetFile(module), load_addr); if (next_load_addr) { LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", module, load_addr); // Register module with GDBStub @@ -276,28 +88,18 @@ ResultStatus AppLoader_NCA::Load(Kernel::SharedPtr<Kernel::Process>& process) { process->Run(Memory::PROCESS_IMAGE_VADDR, metadata.GetMainThreadPriority(), metadata.GetMainThreadStackSize()); - if (nca->GetRomFsSize() > 0) + if (nca->GetRomFS() != nullptr && nca->GetRomFS()->GetSize() > 0) Service::FileSystem::RegisterRomFS(std::make_unique<FileSys::RomFSFactory>(*this)); is_loaded = true; + return ResultStatus::Success; } -ResultStatus AppLoader_NCA::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, - u64& size) { - if (nca->GetRomFsSize() == 0) { - LOG_DEBUG(Loader, "No RomFS available"); +ResultStatus AppLoader_NCA::ReadRomFS(FileSys::VirtualFile& dir) { + if (nca == nullptr || nca->GetRomFS() == nullptr || nca->GetRomFS()->GetSize() == 0) return ResultStatus::ErrorNotUsed; - } - - romfs_file = std::make_shared<FileUtil::IOFile>(filepath, "rb"); - - offset = nca->GetRomFsOffset(); - size = nca->GetRomFsSize(); - - LOG_DEBUG(Loader, "RomFS offset: 0x{:016X}", offset); - LOG_DEBUG(Loader, "RomFS size: 0x{:016X}", size); - + dir = nca->GetRomFS(); return ResultStatus::Success; } diff --git a/src/core/loader/nca.h b/src/core/loader/nca.h index 3b6c451d0..52c95953a 100644 --- a/src/core/loader/nca.h +++ b/src/core/loader/nca.h @@ -6,44 +6,39 @@ #include <string> #include "common/common_types.h" -#include "core/file_sys/partition_filesystem.h" +#include "core/file_sys/content_archive.h" #include "core/file_sys/program_metadata.h" #include "core/hle/kernel/kernel.h" #include "core/loader/loader.h" namespace Loader { -class Nca; - /// Loads an NCA file class AppLoader_NCA final : public AppLoader { public: - AppLoader_NCA(FileUtil::IOFile&& file, std::string filepath); + explicit AppLoader_NCA(FileSys::VirtualFile file); /** * Returns the type of the file - * @param file FileUtil::IOFile open file - * @param filepath Path of the file that we are opening. + * @param file std::shared_ptr<VfsFile> open file * @return FileType found, or FileType::Error if this loader doesn't know it */ - static FileType IdentifyType(FileUtil::IOFile& file, const std::string& filepath); + static FileType IdentifyType(const FileSys::VirtualFile& file); FileType GetFileType() override { - return IdentifyType(file, filepath); + return IdentifyType(file); } ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; - ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, - u64& size) override; + ResultStatus ReadRomFS(FileSys::VirtualFile& dir) override; ~AppLoader_NCA(); private: - std::string filepath; FileSys::ProgramMetadata metadata; - std::unique_ptr<Nca> nca; + std::unique_ptr<FileSys::NCA> nca; }; } // namespace Loader diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp index 4d7c69a22..a007d3e6e 100644 --- a/src/core/loader/nro.cpp +++ b/src/core/loader/nro.cpp @@ -48,14 +48,12 @@ struct ModHeader { }; static_assert(sizeof(ModHeader) == 0x1c, "ModHeader has incorrect size."); -AppLoader_NRO::AppLoader_NRO(FileUtil::IOFile&& file, std::string filepath) - : AppLoader(std::move(file)), filepath(std::move(filepath)) {} +AppLoader_NRO::AppLoader_NRO(FileSys::VirtualFile file) : AppLoader(file) {} -FileType AppLoader_NRO::IdentifyType(FileUtil::IOFile& file, const std::string&) { +FileType AppLoader_NRO::IdentifyType(const FileSys::VirtualFile& file) { // Read NSO header NroHeader nro_header{}; - file.Seek(0, SEEK_SET); - if (sizeof(NroHeader) != file.ReadBytes(&nro_header, sizeof(NroHeader))) { + if (sizeof(NroHeader) != file->ReadObject(&nro_header)) { return FileType::Error; } if (nro_header.magic == Common::MakeMagic('N', 'R', 'O', '0')) { @@ -68,16 +66,10 @@ static constexpr u32 PageAlignSize(u32 size) { return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK; } -bool AppLoader_NRO::LoadNro(const std::string& path, VAddr load_base) { - FileUtil::IOFile file(path, "rb"); - if (!file.IsOpen()) { - return {}; - } - +bool AppLoader_NRO::LoadNro(FileSys::VirtualFile file, VAddr load_base) { // Read NSO header NroHeader nro_header{}; - file.Seek(0, SEEK_SET); - if (sizeof(NroHeader) != file.ReadBytes(&nro_header, sizeof(NroHeader))) { + if (sizeof(NroHeader) != file->ReadObject(&nro_header)) { return {}; } if (nro_header.magic != Common::MakeMagic('N', 'R', 'O', '0')) { @@ -86,10 +78,9 @@ bool AppLoader_NRO::LoadNro(const std::string& path, VAddr load_base) { // Build program image Kernel::SharedPtr<Kernel::CodeSet> codeset = Kernel::CodeSet::Create(""); - std::vector<u8> program_image; - program_image.resize(PageAlignSize(nro_header.file_size)); - file.Seek(0, SEEK_SET); - file.ReadBytes(program_image.data(), nro_header.file_size); + std::vector<u8> program_image = file->ReadBytes(PageAlignSize(nro_header.file_size)); + if (program_image.size() != PageAlignSize(nro_header.file_size)) + return {}; for (int i = 0; i < nro_header.segments.size(); ++i) { codeset->segments[i].addr = nro_header.segments[i].offset; @@ -112,7 +103,7 @@ bool AppLoader_NRO::LoadNro(const std::string& path, VAddr load_base) { program_image.resize(static_cast<u32>(program_image.size()) + bss_size); // Load codeset for current process - codeset->name = path; + codeset->name = file->GetName(); codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image)); Core::CurrentProcess()->LoadModule(codeset, load_base); @@ -126,14 +117,11 @@ ResultStatus AppLoader_NRO::Load(Kernel::SharedPtr<Kernel::Process>& process) { if (is_loaded) { return ResultStatus::ErrorAlreadyLoaded; } - if (!file.IsOpen()) { - return ResultStatus::Error; - } // Load NRO static constexpr VAddr base_addr{Memory::PROCESS_IMAGE_VADDR}; - if (!LoadNro(filepath, base_addr)) { + if (!LoadNro(file, base_addr)) { return ResultStatus::ErrorInvalidFormat; } diff --git a/src/core/loader/nro.h b/src/core/loader/nro.h index 599adb253..2c03d06bb 100644 --- a/src/core/loader/nro.h +++ b/src/core/loader/nro.h @@ -15,26 +15,23 @@ namespace Loader { /// Loads an NRO file class AppLoader_NRO final : public AppLoader, Linker { public: - AppLoader_NRO(FileUtil::IOFile&& file, std::string filepath); + AppLoader_NRO(FileSys::VirtualFile file); /** * Returns the type of the file - * @param file FileUtil::IOFile open file - * @param filepath Path of the file that we are opening. + * @param file std::shared_ptr<VfsFile> open file * @return FileType found, or FileType::Error if this loader doesn't know it */ - static FileType IdentifyType(FileUtil::IOFile& file, const std::string& filepath); + static FileType IdentifyType(const FileSys::VirtualFile& file); FileType GetFileType() override { - return IdentifyType(file, filepath); + return IdentifyType(file); } ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; private: - bool LoadNro(const std::string& path, VAddr load_base); - - std::string filepath; + bool LoadNro(FileSys::VirtualFile file, VAddr load_base); }; } // namespace Loader diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp index 7b3d6b837..2beb85fbf 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp @@ -38,6 +38,7 @@ struct NsoHeader { std::array<u32_le, 3> segments_compressed_size; }; static_assert(sizeof(NsoHeader) == 0x6c, "NsoHeader has incorrect size."); +static_assert(std::is_trivially_copyable_v<NsoHeader>, "NsoHeader isn't trivially copyable."); struct ModHeader { u32_le magic; @@ -50,15 +51,11 @@ struct ModHeader { }; static_assert(sizeof(ModHeader) == 0x1c, "ModHeader has incorrect size."); -AppLoader_NSO::AppLoader_NSO(FileUtil::IOFile&& file, std::string filepath) - : AppLoader(std::move(file)), filepath(std::move(filepath)) {} +AppLoader_NSO::AppLoader_NSO(FileSys::VirtualFile file) : AppLoader(std::move(file)) {} -FileType AppLoader_NSO::IdentifyType(FileUtil::IOFile& file, const std::string&) { +FileType AppLoader_NSO::IdentifyType(const FileSys::VirtualFile& file) { u32 magic = 0; - file.Seek(0, SEEK_SET); - if (1 != file.ReadArray<u32>(&magic, 1)) { - return FileType::Error; - } + file->ReadObject(&magic); if (Common::MakeMagic('N', 'S', 'O', '0') == magic) { return FileType::NSO; @@ -99,13 +96,16 @@ static constexpr u32 PageAlignSize(u32 size) { return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK; } -VAddr AppLoader_NSO::LoadModule(const std::string& name, const std::vector<u8>& file_data, - VAddr load_base) { - if (file_data.size() < sizeof(NsoHeader)) +VAddr AppLoader_NSO::LoadModule(FileSys::VirtualFile file, VAddr load_base) { + if (file == nullptr) return {}; - NsoHeader nso_header; - std::memcpy(&nso_header, file_data.data(), sizeof(NsoHeader)); + if (file->GetSize() < sizeof(NsoHeader)) + return {}; + + NsoHeader nso_header{}; + if (sizeof(NsoHeader) != file->ReadObject(&nso_header)) + return {}; if (nso_header.magic != Common::MakeMagic('N', 'S', 'O', '0')) return {}; @@ -114,9 +114,8 @@ VAddr AppLoader_NSO::LoadModule(const std::string& name, const std::vector<u8>& Kernel::SharedPtr<Kernel::CodeSet> codeset = Kernel::CodeSet::Create(""); std::vector<u8> program_image; for (int i = 0; i < nso_header.segments.size(); ++i) { - std::vector<u8> compressed_data(nso_header.segments_compressed_size[i]); - for (auto j = 0; j < nso_header.segments_compressed_size[i]; ++j) - compressed_data[j] = file_data[nso_header.segments[i].offset + j]; + const std::vector<u8> compressed_data = + file->ReadBytes(nso_header.segments_compressed_size[i], nso_header.segments[i].offset); std::vector<u8> data = DecompressSegment(compressed_data, nso_header.segments[i]); program_image.resize(nso_header.segments[i].location); program_image.insert(program_image.end(), data.begin(), data.end()); @@ -144,7 +143,7 @@ VAddr AppLoader_NSO::LoadModule(const std::string& name, const std::vector<u8>& program_image.resize(image_size); // Load codeset for current process - codeset->name = name; + codeset->name = file->GetName(); codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image)); Core::CurrentProcess()->LoadModule(codeset, load_base); @@ -154,72 +153,14 @@ VAddr AppLoader_NSO::LoadModule(const std::string& name, const std::vector<u8>& return load_base + image_size; } -VAddr AppLoader_NSO::LoadModule(const std::string& path, VAddr load_base) { - FileUtil::IOFile file(path, "rb"); - if (!file.IsOpen()) { - return {}; - } - - // Read NSO header - NsoHeader nso_header{}; - file.Seek(0, SEEK_SET); - if (sizeof(NsoHeader) != file.ReadBytes(&nso_header, sizeof(NsoHeader))) { - return {}; - } - if (nso_header.magic != Common::MakeMagic('N', 'S', 'O', '0')) { - return {}; - } - - // Build program image - Kernel::SharedPtr<Kernel::CodeSet> codeset = Kernel::CodeSet::Create(""); - std::vector<u8> program_image; - for (int i = 0; i < nso_header.segments.size(); ++i) { - std::vector<u8> data = - ReadSegment(file, nso_header.segments[i], nso_header.segments_compressed_size[i]); - program_image.resize(nso_header.segments[i].location); - program_image.insert(program_image.end(), data.begin(), data.end()); - codeset->segments[i].addr = nso_header.segments[i].location; - codeset->segments[i].offset = nso_header.segments[i].location; - codeset->segments[i].size = PageAlignSize(static_cast<u32>(data.size())); - } - - // MOD header pointer is at .text offset + 4 - u32 module_offset; - std::memcpy(&module_offset, program_image.data() + 4, sizeof(u32)); - - // Read MOD header - ModHeader mod_header{}; - // Default .bss to size in segment header if MOD0 section doesn't exist - u32 bss_size{PageAlignSize(nso_header.segments[2].bss_size)}; - std::memcpy(&mod_header, program_image.data() + module_offset, sizeof(ModHeader)); - const bool has_mod_header{mod_header.magic == Common::MakeMagic('M', 'O', 'D', '0')}; - if (has_mod_header) { - // Resize program image to include .bss section and page align each section - bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset); - } - codeset->data.size += bss_size; - const u32 image_size{PageAlignSize(static_cast<u32>(program_image.size()) + bss_size)}; - program_image.resize(image_size); - - // Load codeset for current process - codeset->name = path; - codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image)); - Core::CurrentProcess()->LoadModule(codeset, load_base); - - return load_base + image_size; -} - ResultStatus AppLoader_NSO::Load(Kernel::SharedPtr<Kernel::Process>& process) { if (is_loaded) { return ResultStatus::ErrorAlreadyLoaded; } - if (!file.IsOpen()) { - return ResultStatus::Error; - } // Load module - LoadModule(filepath, Memory::PROCESS_IMAGE_VADDR); - LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", filepath, Memory::PROCESS_IMAGE_VADDR); + LoadModule(file, Memory::PROCESS_IMAGE_VADDR); + LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", file->GetName(), Memory::PROCESS_IMAGE_VADDR); process->svc_access_mask.set(); process->address_mappings = default_address_mappings; diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h index 386f4d39a..3f7567500 100644 --- a/src/core/loader/nso.h +++ b/src/core/loader/nso.h @@ -15,29 +15,22 @@ namespace Loader { /// Loads an NSO file class AppLoader_NSO final : public AppLoader, Linker { public: - AppLoader_NSO(FileUtil::IOFile&& file, std::string filepath); + explicit AppLoader_NSO(FileSys::VirtualFile file); /** * Returns the type of the file - * @param file FileUtil::IOFile open file - * @param filepath Path of the file that we are opening. + * @param file std::shared_ptr<VfsFile> open file * @return FileType found, or FileType::Error if this loader doesn't know it */ - static FileType IdentifyType(FileUtil::IOFile& file, const std::string& filepath); + static FileType IdentifyType(const FileSys::VirtualFile& file); FileType GetFileType() override { - return IdentifyType(file, filepath); + return IdentifyType(file); } - static VAddr LoadModule(const std::string& name, const std::vector<u8>& file_data, - VAddr load_base); - - static VAddr LoadModule(const std::string& path, VAddr load_base); + static VAddr LoadModule(FileSys::VirtualFile file, VAddr load_base); ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; - -private: - std::string filepath; }; } // namespace Loader |