diff options
Diffstat (limited to 'src/core/loader')
-rw-r--r-- | src/core/loader/deconstructed_rom_directory.cpp | 113 | ||||
-rw-r--r-- | src/core/loader/deconstructed_rom_directory.h | 14 | ||||
-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 | 252 | ||||
-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 | 91 | ||||
-rw-r--r-- | src/core/loader/nso.h | 17 |
12 files changed, 540 insertions, 140 deletions
diff --git a/src/core/loader/deconstructed_rom_directory.cpp b/src/core/loader/deconstructed_rom_directory.cpp index 0b3b4cd73..eb7feb617 100644 --- a/src/core/loader/deconstructed_rom_directory.cpp +++ b/src/core/loader/deconstructed_rom_directory.cpp @@ -4,10 +4,11 @@ #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/content_archive.h" +#include "core/file_sys/romfs_factory.h" #include "core/hle/kernel/process.h" #include "core/hle/kernel/resource_limit.h" #include "core/hle/service/filesystem/filesystem.h" @@ -45,11 +46,55 @@ static std::string FindRomFS(const std::string& directory) { return filepath_romfs; } -AppLoader_DeconstructedRomDirectory::AppLoader_DeconstructedRomDirectory(FileSys::VirtualFile file) - : AppLoader(std::move(file)) {} +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); + }; + + // 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); -FileType AppLoader_DeconstructedRomDirectory::IdentifyType(const FileSys::VirtualFile& file) { - if (FileSys::IsDirectoryExeFS(file->GetContainingDirectory())) { + if (is_main_found && is_npdm_found && is_rtld_found && is_sdk_found) { return FileType::DeconstructedRomDirectory; } @@ -61,13 +106,14 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load( if (is_loaded) { return ResultStatus::ErrorAlreadyLoaded; } + if (!file.IsOpen()) { + return ResultStatus::Error; + } - const FileSys::VirtualDir dir = file->GetContainingDirectory(); - const FileSys::VirtualFile npdm = dir->GetFile("main.npdm"); - if (npdm == nullptr) - return ResultStatus::ErrorInvalidFormat; + const std::string directory = filepath.substr(0, filepath.find_last_of("/\\")) + DIR_SEP; + const std::string npdm_path = directory + DIR_SEP + "main.npdm"; - ResultStatus result = metadata.Load(npdm); + ResultStatus result = metadata.Load(npdm_path); if (result != ResultStatus::Success) { return result; } @@ -82,10 +128,9 @@ 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; - const FileSys::VirtualFile module_file = dir->GetFile(module); - if (module_file != nullptr) - next_load_addr = AppLoader_NSO::LoadModule(module_file, load_addr); + next_load_addr = AppLoader_NSO::LoadModule(path, load_addr); if (next_load_addr) { LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", module, load_addr); } else { @@ -102,20 +147,42 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load( metadata.GetMainThreadStackSize()); // Find the RomFS by searching for a ".romfs" file in this 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; - }); - - // TODO(DarkLordZach): Identify RomFS if its a subdirectory. - const auto romfs = (romfs_iter == files.end()) ? nullptr : *romfs_iter; + filepath_romfs = FindRomFS(directory); - if (romfs != nullptr) - Service::FileSystem::RegisterRomFS(romfs); + // Register the RomFS if a ".romfs" file was found + if (!filepath_romfs.empty()) { + Service::FileSystem::RegisterFileSystem(std::make_unique<FileSys::RomFS_Factory>(*this), + Service::FileSystem::Type::RomFS); + } is_loaded = true; 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"); + 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); + + return ResultStatus::Success; +} + } // namespace Loader diff --git a/src/core/loader/deconstructed_rom_directory.h b/src/core/loader/deconstructed_rom_directory.h index 494220756..23295d911 100644 --- a/src/core/loader/deconstructed_rom_directory.h +++ b/src/core/loader/deconstructed_rom_directory.h @@ -20,22 +20,28 @@ namespace Loader { */ class AppLoader_DeconstructedRomDirectory final : public AppLoader { public: - explicit AppLoader_DeconstructedRomDirectory(FileSys::VirtualFile main_file); + AppLoader_DeconstructedRomDirectory(FileUtil::IOFile&& file, std::string filepath); /** * Returns the type of the file - * @param file std::shared_ptr<VfsFile> open file + * @param file FileUtil::IOFile open file + * @param filepath Path of the file that we are opening. * @return FileType found, or FileType::Error if this loader doesn't know it */ - static FileType IdentifyType(const FileSys::VirtualFile& file); + static FileType IdentifyType(FileUtil::IOFile& file, const std::string& filepath); FileType GetFileType() override { - return IdentifyType(file); + return IdentifyType(file, filepath); } ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; + ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, + u64& size) override; + private: + std::string filepath_romfs; + std::string filepath; FileSys::ProgramMetadata metadata; }; diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp index 4bfd5f536..b69e5c6ef 100644 --- a/src/core/loader/elf.cpp +++ b/src/core/loader/elf.cpp @@ -365,17 +365,20 @@ SectionID ElfReader::GetSectionByName(const char* name, int firstSection) const namespace Loader { -AppLoader_ELF::AppLoader_ELF(FileSys::VirtualFile file) : AppLoader(std::move(file)) {} +AppLoader_ELF::AppLoader_ELF(FileUtil::IOFile&& file, std::string filename) + : AppLoader(std::move(file)), filename(std::move(filename)) {} -FileType AppLoader_ELF::IdentifyType(const FileSys::VirtualFile& file) { +FileType AppLoader_ELF::IdentifyType(FileUtil::IOFile& file, const std::string&) { static constexpr u16 ELF_MACHINE_ARM{0x28}; u32 magic = 0; - if (4 != file->ReadObject(&magic)) + file.Seek(0, SEEK_SET); + if (1 != file.ReadArray<u32>(&magic, 1)) return FileType::Error; u16 machine = 0; - if (2 != file->ReadObject(&machine, 18)) + file.Seek(18, SEEK_SET); + if (1 != file.ReadArray<u16>(&machine, 1)) return FileType::Error; if (Common::MakeMagic('\x7f', 'E', 'L', 'F') == magic && ELF_MACHINE_ARM == machine) @@ -388,13 +391,20 @@ ResultStatus AppLoader_ELF::Load(Kernel::SharedPtr<Kernel::Process>& process) { if (is_loaded) return ResultStatus::ErrorAlreadyLoaded; - std::vector<u8> buffer = file->ReadAllBytes(); - if (buffer.size() != file->GetSize()) + 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) return ResultStatus::Error; ElfReader elf_reader(&buffer[0]); SharedPtr<CodeSet> codeset = elf_reader.LoadInto(Memory::PROCESS_IMAGE_VADDR); - codeset->name = file->GetName(); + codeset->name = filename; 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 b8fb982d0..ee741a789 100644 --- a/src/core/loader/elf.h +++ b/src/core/loader/elf.h @@ -16,20 +16,24 @@ namespace Loader { /// Loads an ELF/AXF file class AppLoader_ELF final : public AppLoader { public: - explicit AppLoader_ELF(FileSys::VirtualFile file); + AppLoader_ELF(FileUtil::IOFile&& file, std::string filename); /** * Returns the type of the file - * @param file std::shared_ptr<VfsFile> open file + * @param file FileUtil::IOFile open file + * @param filepath Path of the file that we are opening. * @return FileType found, or FileType::Error if this loader doesn't know it */ - static FileType IdentifyType(const FileSys::VirtualFile& file); + static FileType IdentifyType(FileUtil::IOFile& file, const std::string& filepath); FileType GetFileType() override { - return IdentifyType(file); + return IdentifyType(file, filename); } 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 1574345a1..8831d8e83 100644 --- a/src/core/loader/loader.cpp +++ b/src/core/loader/loader.cpp @@ -6,7 +6,6 @@ #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" @@ -22,11 +21,11 @@ const std::initializer_list<Kernel::AddressMapping> default_address_mappings = { {0x1F000000, 0x600000, false}, // entire VRAM }; -FileType IdentifyFile(FileSys::VirtualFile file) { +FileType IdentifyFile(FileUtil::IOFile& file, const std::string& filepath) { FileType type; #define CHECK_TYPE(loader) \ - type = AppLoader_##loader::IdentifyType(file); \ + type = AppLoader_##loader::IdentifyType(file, filepath); \ if (FileType::Error != type) \ return type; @@ -42,22 +41,25 @@ FileType IdentifyFile(FileSys::VirtualFile file) { } FileType IdentifyFile(const std::string& file_name) { - return IdentifyFile(FileSys::VirtualFile(std::make_shared<FileSys::RealVfsFile>(file_name))); -} + FileUtil::IOFile file(file_name, "rb"); + if (!file.IsOpen()) { + LOG_ERROR(Loader, "Failed to load file {}", file_name); + return FileType::Unknown; + } -FileType GuessFromFilename(const std::string& name) { - if (name == "main") - return FileType::DeconstructedRomDirectory; + return IdentifyFile(file, file_name); +} - const std::string extension = Common::ToLower(FileUtil::GetExtensionFromFilename(name)); +FileType GuessFromExtension(const std::string& extension_) { + std::string extension = Common::ToLower(extension_); - if (extension == "elf") + if (extension == ".elf") return FileType::ELF; - if (extension == "nro") + else if (extension == ".nro") return FileType::NRO; - if (extension == "nso") + else if (extension == ".nso") return FileType::NSO; - if (extension == "nca") + else if (extension == ".nca") return FileType::NCA; return FileType::Unknown; @@ -91,47 +93,58 @@ 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(FileSys::VirtualFile file, FileType type) { +static std::unique_ptr<AppLoader> GetFileLoader(FileUtil::IOFile&& file, FileType type, + const std::string& filename, + const std::string& filepath) { switch (type) { // Standard ELF file format. case FileType::ELF: - return std::make_unique<AppLoader_ELF>(std::move(file)); + return std::make_unique<AppLoader_ELF>(std::move(file), filename); // NX NSO file format. case FileType::NSO: - return std::make_unique<AppLoader_NSO>(std::move(file)); + return std::make_unique<AppLoader_NSO>(std::move(file), filepath); // NX NRO file format. case FileType::NRO: - return std::make_unique<AppLoader_NRO>(std::move(file)); + return std::make_unique<AppLoader_NRO>(std::move(file), filepath); // NX NCA file format. case FileType::NCA: - return std::make_unique<AppLoader_NCA>(std::move(file)); + return std::make_unique<AppLoader_NCA>(std::move(file), filepath); // NX deconstructed ROM directory. case FileType::DeconstructedRomDirectory: - return std::make_unique<AppLoader_DeconstructedRomDirectory>(std::move(file)); + return std::make_unique<AppLoader_DeconstructedRomDirectory>(std::move(file), filepath); default: return nullptr; } } -std::unique_ptr<AppLoader> GetLoader(FileSys::VirtualFile file) { - FileType type = IdentifyFile(file); - FileType filename_type = GuessFromFilename(file->GetName()); +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); if (type != filename_type) { - LOG_WARNING(Loader, "File {} has a different type than its extension.", file->GetName()); + LOG_WARNING(Loader, "File {} has a different type than its extension.", filename); if (FileType::Unknown == type) type = filename_type; } - LOG_DEBUG(Loader, "Loading file {} as {}...", file->GetName(), GetFileTypeString(type)); + LOG_DEBUG(Loader, "Loading file {} as {}...", filename, GetFileTypeString(type)); - return GetFileLoader(std::move(file), type); + return GetFileLoader(std::move(file), type, filename_filename, filename); } } // namespace Loader diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h index 1da9e8099..b76f7b13d 100644 --- a/src/core/loader/loader.h +++ b/src/core/loader/loader.h @@ -13,7 +13,6 @@ #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 { @@ -37,9 +36,10 @@ 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(FileSys::VirtualFile file); +FileType IdentifyFile(FileUtil::IOFile& file, const std::string& filepath); /** * Identifies the type of a bootable file based on the magic value in its header. @@ -50,12 +50,12 @@ FileType IdentifyFile(FileSys::VirtualFile file); FileType IdentifyFile(const std::string& file_name); /** - * Guess the type of a bootable file from its name - * @param name String name of bootable file + * Guess the type of a bootable file from its extension + * @param extension String extension 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 GuessFromFilename(const std::string& name); +FileType GuessFromExtension(const std::string& extension); /** * 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(FileSys::VirtualFile file) : file(std::move(file)) {} + AppLoader(FileUtil::IOFile&& file) : file(std::move(file)) {} virtual ~AppLoader() {} /** @@ -154,20 +154,26 @@ 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 file The file containing the RomFS + * @param romfs_file The file containing the RomFS + * @param offset The offset the romfs begins on + * @param size The size of the romfs * @return ResultStatus result of function */ - virtual ResultStatus ReadRomFS(FileSys::VirtualFile& dir) { + virtual ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, + u64& size) { 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 file The file containing the RomFS + * @param romfs_file The file containing the RomFS + * @param offset The offset the romfs begins on + * @param size The size of the romfs * @return ResultStatus result of function */ - virtual ResultStatus ReadUpdateRomFS(FileSys::VirtualFile& file) { + virtual ResultStatus ReadUpdateRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, + u64& size) { return ResultStatus::ErrorNotImplemented; } @@ -181,7 +187,7 @@ public: } protected: - FileSys::VirtualFile file; + FileUtil::IOFile file; bool is_loaded = false; }; @@ -196,6 +202,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(FileSys::VirtualFile file); +std::unique_ptr<AppLoader> GetLoader(const std::string& filename); } // namespace Loader diff --git a/src/core/loader/nca.cpp b/src/core/loader/nca.cpp index 874f42b91..da064f8e3 100644 --- a/src/core/loader/nca.cpp +++ b/src/core/loader/nca.cpp @@ -4,11 +4,13 @@ #include <vector> +#include "common/common_funcs.h" #include "common/file_util.h" #include "common/logging/log.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/hle/kernel/process.h" #include "core/hle/kernel/resource_limit.h" #include "core/hle/service/filesystem/filesystem.h" @@ -18,15 +20,208 @@ namespace Loader { -AppLoader_NCA::AppLoader_NCA(FileSys::VirtualFile file) : AppLoader(file) {} +// Media offsets in headers are stored divided by 512. Mult. by this to get real offset. +constexpr u64 MEDIA_OFFSET_MULTIPLIER = 0x200; -FileType AppLoader_NCA::IdentifyType(const FileSys::VirtualFile& file) { - // TODO(DarkLordZach): Assuming everything is decrypted. Add crypto support. - FileSys::NCAHeader header{}; - if (sizeof(FileSys::NCAHeader) != file->ReadObject(&header)) +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; - if (IsValidNCA(header) && header.content_type == FileSys::NCAContentType::Program) + // TODO(DarkLordZach): Assuming everything is decrypted. Add crypto support. + NcaHeader header{}; + std::memcpy(&header, header_enc_array.data(), sizeof(NcaHeader)); + + if (IsValidNca(header) && header.content_type == NcaContentType::Program) return FileType::NCA; return FileType::Error; @@ -36,22 +231,17 @@ 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<FileSys::NCA>(file); - ResultStatus result = nca->GetStatus(); + nca = std::make_unique<Nca>(); + ResultStatus result = nca->Load(std::move(file), filepath); if (result != ResultStatus::Success) { return result; } - 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")); + result = metadata.Load(nca->GetExeFsFile("main.npdm")); if (result != ResultStatus::Success) { return result; } @@ -66,8 +256,7 @@ 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(exefs->GetFile(module), load_addr); + next_load_addr = AppLoader_NSO::LoadModule(module, nca->GetExeFsFile(module), load_addr); if (next_load_addr) { LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", module, load_addr); } else { @@ -83,11 +272,28 @@ ResultStatus AppLoader_NCA::Load(Kernel::SharedPtr<Kernel::Process>& process) { process->Run(Memory::PROCESS_IMAGE_VADDR, metadata.GetMainThreadPriority(), metadata.GetMainThreadStackSize()); + if (nca->GetRomFsSize() > 0) + Service::FileSystem::RegisterFileSystem(std::make_unique<FileSys::RomFS_Factory>(*this), + Service::FileSystem::Type::RomFS); + 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"); + return ResultStatus::ErrorNotUsed; + } + + romfs_file = std::make_shared<FileUtil::IOFile>(filepath, "rb"); + + offset = nca->GetRomFsOffset(); + size = nca->GetRomFsSize(); - const auto romfs = nca->GetRomFS(); - if (romfs != nullptr) - Service::FileSystem::RegisterRomFS(romfs); + LOG_DEBUG(Loader, "RomFS offset: 0x{:016X}", offset); + LOG_DEBUG(Loader, "RomFS size: 0x{:016X}", size); return ResultStatus::Success; } diff --git a/src/core/loader/nca.h b/src/core/loader/nca.h index a5639f149..3b6c451d0 100644 --- a/src/core/loader/nca.h +++ b/src/core/loader/nca.h @@ -6,37 +6,44 @@ #include <string> #include "common/common_types.h" -#include "core/file_sys/content_archive.h" +#include "core/file_sys/partition_filesystem.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: - explicit AppLoader_NCA(FileSys::VirtualFile file); + AppLoader_NCA(FileUtil::IOFile&& file, std::string filepath); /** * Returns the type of the file - * @param file std::shared_ptr<VfsFile> open file + * @param file FileUtil::IOFile open file + * @param filepath Path of the file that we are opening. * @return FileType found, or FileType::Error if this loader doesn't know it */ - static FileType IdentifyType(const FileSys::VirtualFile& file); + static FileType IdentifyType(FileUtil::IOFile& file, const std::string& filepath); FileType GetFileType() override { - return IdentifyType(file); + return IdentifyType(file, filepath); } ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; + ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, + u64& size) override; + ~AppLoader_NCA(); private: + std::string filepath; FileSys::ProgramMetadata metadata; - std::unique_ptr<FileSys::NCA> nca; + std::unique_ptr<Nca> nca; }; } // namespace Loader diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp index 08b7aad7a..3853cfa1a 100644 --- a/src/core/loader/nro.cpp +++ b/src/core/loader/nro.cpp @@ -47,12 +47,14 @@ struct ModHeader { }; static_assert(sizeof(ModHeader) == 0x1c, "ModHeader has incorrect size."); -AppLoader_NRO::AppLoader_NRO(FileSys::VirtualFile file) : AppLoader(file) {} +AppLoader_NRO::AppLoader_NRO(FileUtil::IOFile&& file, std::string filepath) + : AppLoader(std::move(file)), filepath(std::move(filepath)) {} -FileType AppLoader_NRO::IdentifyType(const FileSys::VirtualFile& file) { +FileType AppLoader_NRO::IdentifyType(FileUtil::IOFile& file, const std::string&) { // Read NSO header NroHeader nro_header{}; - if (sizeof(NroHeader) != file->ReadObject(&nro_header)) { + file.Seek(0, SEEK_SET); + if (sizeof(NroHeader) != file.ReadBytes(&nro_header, sizeof(NroHeader))) { return FileType::Error; } if (nro_header.magic == Common::MakeMagic('N', 'R', 'O', '0')) { @@ -65,10 +67,16 @@ static constexpr u32 PageAlignSize(u32 size) { return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK; } -bool AppLoader_NRO::LoadNro(FileSys::VirtualFile file, VAddr load_base) { +bool AppLoader_NRO::LoadNro(const std::string& path, VAddr load_base) { + FileUtil::IOFile file(path, "rb"); + if (!file.IsOpen()) { + return {}; + } + // Read NSO header NroHeader nro_header{}; - if (sizeof(NroHeader) != file->ReadObject(&nro_header)) { + file.Seek(0, SEEK_SET); + if (sizeof(NroHeader) != file.ReadBytes(&nro_header, sizeof(NroHeader))) { return {}; } if (nro_header.magic != Common::MakeMagic('N', 'R', 'O', '0')) { @@ -77,9 +85,10 @@ bool AppLoader_NRO::LoadNro(FileSys::VirtualFile file, VAddr load_base) { // Build program image Kernel::SharedPtr<Kernel::CodeSet> codeset = Kernel::CodeSet::Create(""); - std::vector<u8> program_image = file->ReadBytes(PageAlignSize(nro_header.file_size)); - if (program_image.size() != PageAlignSize(nro_header.file_size)) - return {}; + 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); for (int i = 0; i < nro_header.segments.size(); ++i) { codeset->segments[i].addr = nro_header.segments[i].offset; @@ -102,7 +111,7 @@ bool AppLoader_NRO::LoadNro(FileSys::VirtualFile file, VAddr load_base) { program_image.resize(static_cast<u32>(program_image.size()) + bss_size); // Load codeset for current process - codeset->name = file->GetName(); + codeset->name = path; codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image)); Core::CurrentProcess()->LoadModule(codeset, load_base); @@ -113,11 +122,14 @@ 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(file, base_addr)) { + if (!LoadNro(filepath, base_addr)) { return ResultStatus::ErrorInvalidFormat; } diff --git a/src/core/loader/nro.h b/src/core/loader/nro.h index 2c03d06bb..599adb253 100644 --- a/src/core/loader/nro.h +++ b/src/core/loader/nro.h @@ -15,23 +15,26 @@ namespace Loader { /// Loads an NRO file class AppLoader_NRO final : public AppLoader, Linker { public: - AppLoader_NRO(FileSys::VirtualFile file); + AppLoader_NRO(FileUtil::IOFile&& file, std::string filepath); /** * Returns the type of the file - * @param file std::shared_ptr<VfsFile> open file + * @param file FileUtil::IOFile open file + * @param filepath Path of the file that we are opening. * @return FileType found, or FileType::Error if this loader doesn't know it */ - static FileType IdentifyType(const FileSys::VirtualFile& file); + static FileType IdentifyType(FileUtil::IOFile& file, const std::string& filepath); FileType GetFileType() override { - return IdentifyType(file); + return IdentifyType(file, filepath); } ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; private: - bool LoadNro(FileSys::VirtualFile file, VAddr load_base); + bool LoadNro(const std::string& path, VAddr load_base); + + std::string filepath; }; } // namespace Loader diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp index 2ed12a5ab..7f84e4b1b 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp @@ -37,7 +37,6 @@ 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,11 +49,15 @@ struct ModHeader { }; static_assert(sizeof(ModHeader) == 0x1c, "ModHeader has incorrect size."); -AppLoader_NSO::AppLoader_NSO(FileSys::VirtualFile file) : AppLoader(std::move(file)) {} +AppLoader_NSO::AppLoader_NSO(FileUtil::IOFile&& file, std::string filepath) + : AppLoader(std::move(file)), filepath(std::move(filepath)) {} -FileType AppLoader_NSO::IdentifyType(const FileSys::VirtualFile& file) { +FileType AppLoader_NSO::IdentifyType(FileUtil::IOFile& file, const std::string&) { u32 magic = 0; - file->ReadObject(&magic); + file.Seek(0, SEEK_SET); + if (1 != file.ReadArray<u32>(&magic, 1)) { + return FileType::Error; + } if (Common::MakeMagic('N', 'S', 'O', '0') == magic) { return FileType::NSO; @@ -95,27 +98,80 @@ static constexpr u32 PageAlignSize(u32 size) { return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK; } -VAddr AppLoader_NSO::LoadModule(FileSys::VirtualFile file, VAddr load_base) { - if (file == nullptr) +VAddr AppLoader_NSO::LoadModule(const std::string& name, const std::vector<u8>& file_data, + VAddr load_base) { + if (file_data.size() < sizeof(NsoHeader)) return {}; - if (file->GetSize() < sizeof(NsoHeader)) + NsoHeader nso_header; + std::memcpy(&nso_header, file_data.data(), sizeof(NsoHeader)); + + if (nso_header.magic != Common::MakeMagic('N', 'S', 'O', '0')) return {}; - NsoHeader nso_header{}; - if (sizeof(NsoHeader) != file->ReadObject(&nso_header)) + // 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> compressed_data(nso_header.segments_compressed_size[i]); + for (int j = 0; j < nso_header.segments_compressed_size[i]; ++j) + compressed_data[j] = file_data[nso_header.segments[i].offset + j]; + 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()); + 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 = name; + codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image)); + Core::CurrentProcess()->LoadModule(codeset, load_base); + + 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 {}; + } - if (nso_header.magic != Common::MakeMagic('N', 'S', 'O', '0')) + // 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) { - 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]); + 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; @@ -142,7 +198,7 @@ VAddr AppLoader_NSO::LoadModule(FileSys::VirtualFile file, VAddr load_base) { program_image.resize(image_size); // Load codeset for current process - codeset->name = file->GetName(); + codeset->name = path; codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image)); Core::CurrentProcess()->LoadModule(codeset, load_base); @@ -153,10 +209,13 @@ ResultStatus AppLoader_NSO::Load(Kernel::SharedPtr<Kernel::Process>& process) { if (is_loaded) { return ResultStatus::ErrorAlreadyLoaded; } + if (!file.IsOpen()) { + return ResultStatus::Error; + } // Load module - LoadModule(file, Memory::PROCESS_IMAGE_VADDR); - LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", file->GetName(), Memory::PROCESS_IMAGE_VADDR); + LoadModule(filepath, Memory::PROCESS_IMAGE_VADDR); + LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", filepath, 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 3f7567500..386f4d39a 100644 --- a/src/core/loader/nso.h +++ b/src/core/loader/nso.h @@ -15,22 +15,29 @@ namespace Loader { /// Loads an NSO file class AppLoader_NSO final : public AppLoader, Linker { public: - explicit AppLoader_NSO(FileSys::VirtualFile file); + AppLoader_NSO(FileUtil::IOFile&& file, std::string filepath); /** * Returns the type of the file - * @param file std::shared_ptr<VfsFile> open file + * @param file FileUtil::IOFile open file + * @param filepath Path of the file that we are opening. * @return FileType found, or FileType::Error if this loader doesn't know it */ - static FileType IdentifyType(const FileSys::VirtualFile& file); + static FileType IdentifyType(FileUtil::IOFile& file, const std::string& filepath); FileType GetFileType() override { - return IdentifyType(file); + return IdentifyType(file, filepath); } - static VAddr LoadModule(FileSys::VirtualFile file, VAddr load_base); + 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); ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; + +private: + std::string filepath; }; } // namespace Loader |