diff options
| -rw-r--r-- | src/core/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/core/file_sys/partition_filesystem.cpp | 18 | ||||
| -rw-r--r-- | src/core/file_sys/partition_filesystem.h | 2 | ||||
| -rw-r--r-- | src/core/loader/loader.cpp | 10 | ||||
| -rw-r--r-- | src/core/loader/loader.h | 1 | ||||
| -rw-r--r-- | src/core/loader/nca.cpp | 303 | ||||
| -rw-r--r-- | src/core/loader/nca.h | 49 | ||||
| -rw-r--r-- | src/core/loader/nso.cpp | 79 | ||||
| -rw-r--r-- | src/core/loader/nso.h | 3 | ||||
| -rw-r--r-- | src/yuzu/game_list.cpp | 2 |
10 files changed, 453 insertions, 16 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index ba5b02174..f09edb817 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt | |||
| @@ -257,6 +257,8 @@ add_library(core STATIC | |||
| 257 | loader/linker.h | 257 | loader/linker.h |
| 258 | loader/loader.cpp | 258 | loader/loader.cpp |
| 259 | loader/loader.h | 259 | loader/loader.h |
| 260 | loader/nca.cpp | ||
| 261 | loader/nca.h | ||
| 260 | loader/nro.cpp | 262 | loader/nro.cpp |
| 261 | loader/nro.h | 263 | loader/nro.h |
| 262 | loader/nso.cpp | 264 | loader/nso.cpp |
diff --git a/src/core/file_sys/partition_filesystem.cpp b/src/core/file_sys/partition_filesystem.cpp index 86a01a5eb..874b9e23b 100644 --- a/src/core/file_sys/partition_filesystem.cpp +++ b/src/core/file_sys/partition_filesystem.cpp | |||
| @@ -19,13 +19,20 @@ Loader::ResultStatus PartitionFilesystem::Load(const std::string& file_path, siz | |||
| 19 | if (file.GetSize() < sizeof(Header)) | 19 | if (file.GetSize() < sizeof(Header)) |
| 20 | return Loader::ResultStatus::Error; | 20 | return Loader::ResultStatus::Error; |
| 21 | 21 | ||
| 22 | file.Seek(offset, SEEK_SET); | ||
| 22 | // For cartridges, HFSs can get very large, so we need to calculate the size up to | 23 | // For cartridges, HFSs can get very large, so we need to calculate the size up to |
| 23 | // the actual content itself instead of just blindly reading in the entire file. | 24 | // the actual content itself instead of just blindly reading in the entire file. |
| 24 | Header pfs_header; | 25 | Header pfs_header; |
| 25 | if (!file.ReadBytes(&pfs_header, sizeof(Header))) | 26 | if (!file.ReadBytes(&pfs_header, sizeof(Header))) |
| 26 | return Loader::ResultStatus::Error; | 27 | return Loader::ResultStatus::Error; |
| 27 | 28 | ||
| 28 | bool is_hfs = (memcmp(pfs_header.magic.data(), "HFS", 3) == 0); | 29 | if (pfs_header.magic != Common::MakeMagic('H', 'F', 'S', '0') && |
| 30 | pfs_header.magic != Common::MakeMagic('P', 'F', 'S', '0')) { | ||
| 31 | return Loader::ResultStatus::ErrorInvalidFormat; | ||
| 32 | } | ||
| 33 | |||
| 34 | bool is_hfs = pfs_header.magic == Common::MakeMagic('H', 'F', 'S', '0'); | ||
| 35 | |||
| 29 | size_t entry_size = is_hfs ? sizeof(HFSEntry) : sizeof(PFSEntry); | 36 | size_t entry_size = is_hfs ? sizeof(HFSEntry) : sizeof(PFSEntry); |
| 30 | size_t metadata_size = | 37 | size_t metadata_size = |
| 31 | sizeof(Header) + (pfs_header.num_entries * entry_size) + pfs_header.strtab_size; | 38 | sizeof(Header) + (pfs_header.num_entries * entry_size) + pfs_header.strtab_size; |
| @@ -50,7 +57,12 @@ Loader::ResultStatus PartitionFilesystem::Load(const std::vector<u8>& file_data, | |||
| 50 | return Loader::ResultStatus::Error; | 57 | return Loader::ResultStatus::Error; |
| 51 | 58 | ||
| 52 | memcpy(&pfs_header, &file_data[offset], sizeof(Header)); | 59 | memcpy(&pfs_header, &file_data[offset], sizeof(Header)); |
| 53 | is_hfs = (memcmp(pfs_header.magic.data(), "HFS", 3) == 0); | 60 | if (pfs_header.magic != Common::MakeMagic('H', 'F', 'S', '0') && |
| 61 | pfs_header.magic != Common::MakeMagic('P', 'F', 'S', '0')) { | ||
| 62 | return Loader::ResultStatus::ErrorInvalidFormat; | ||
| 63 | } | ||
| 64 | |||
| 65 | is_hfs = pfs_header.magic == Common::MakeMagic('H', 'F', 'S', '0'); | ||
| 54 | 66 | ||
| 55 | size_t entries_offset = offset + sizeof(Header); | 67 | size_t entries_offset = offset + sizeof(Header); |
| 56 | size_t entry_size = is_hfs ? sizeof(HFSEntry) : sizeof(PFSEntry); | 68 | size_t entry_size = is_hfs ? sizeof(HFSEntry) : sizeof(PFSEntry); |
| @@ -113,7 +125,7 @@ u64 PartitionFilesystem::GetFileSize(const std::string& name) const { | |||
| 113 | } | 125 | } |
| 114 | 126 | ||
| 115 | void PartitionFilesystem::Print() const { | 127 | void PartitionFilesystem::Print() const { |
| 116 | NGLOG_DEBUG(Service_FS, "Magic: {:.4}", pfs_header.magic.data()); | 128 | NGLOG_DEBUG(Service_FS, "Magic: {}", pfs_header.magic); |
| 117 | NGLOG_DEBUG(Service_FS, "Files: {}", pfs_header.num_entries); | 129 | NGLOG_DEBUG(Service_FS, "Files: {}", pfs_header.num_entries); |
| 118 | for (u32 i = 0; i < pfs_header.num_entries; i++) { | 130 | for (u32 i = 0; i < pfs_header.num_entries; i++) { |
| 119 | NGLOG_DEBUG(Service_FS, " > File {}: {} (0x{:X} bytes, at 0x{:X})", i, | 131 | NGLOG_DEBUG(Service_FS, " > File {}: {} (0x{:X} bytes, at 0x{:X})", i, |
diff --git a/src/core/file_sys/partition_filesystem.h b/src/core/file_sys/partition_filesystem.h index 65cf572f4..9c5810cf1 100644 --- a/src/core/file_sys/partition_filesystem.h +++ b/src/core/file_sys/partition_filesystem.h | |||
| @@ -37,7 +37,7 @@ public: | |||
| 37 | 37 | ||
| 38 | private: | 38 | private: |
| 39 | struct Header { | 39 | struct Header { |
| 40 | std::array<char, 4> magic; | 40 | u32_le magic; |
| 41 | u32_le num_entries; | 41 | u32_le num_entries; |
| 42 | u32_le strtab_size; | 42 | u32_le strtab_size; |
| 43 | INSERT_PADDING_BYTES(0x4); | 43 | INSERT_PADDING_BYTES(0x4); |
diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp index 6a4fd38cb..20cc0bac0 100644 --- a/src/core/loader/loader.cpp +++ b/src/core/loader/loader.cpp | |||
| @@ -9,6 +9,7 @@ | |||
| 9 | #include "core/hle/kernel/process.h" | 9 | #include "core/hle/kernel/process.h" |
| 10 | #include "core/loader/deconstructed_rom_directory.h" | 10 | #include "core/loader/deconstructed_rom_directory.h" |
| 11 | #include "core/loader/elf.h" | 11 | #include "core/loader/elf.h" |
| 12 | #include "core/loader/nca.h" | ||
| 12 | #include "core/loader/nro.h" | 13 | #include "core/loader/nro.h" |
| 13 | #include "core/loader/nso.h" | 14 | #include "core/loader/nso.h" |
| 14 | 15 | ||
| @@ -32,6 +33,7 @@ FileType IdentifyFile(FileUtil::IOFile& file, const std::string& filepath) { | |||
| 32 | CHECK_TYPE(ELF) | 33 | CHECK_TYPE(ELF) |
| 33 | CHECK_TYPE(NSO) | 34 | CHECK_TYPE(NSO) |
| 34 | CHECK_TYPE(NRO) | 35 | CHECK_TYPE(NRO) |
| 36 | CHECK_TYPE(NCA) | ||
| 35 | 37 | ||
| 36 | #undef CHECK_TYPE | 38 | #undef CHECK_TYPE |
| 37 | 39 | ||
| @@ -57,6 +59,8 @@ FileType GuessFromExtension(const std::string& extension_) { | |||
| 57 | return FileType::NRO; | 59 | return FileType::NRO; |
| 58 | else if (extension == ".nso") | 60 | else if (extension == ".nso") |
| 59 | return FileType::NSO; | 61 | return FileType::NSO; |
| 62 | else if (extension == ".nca") | ||
| 63 | return FileType::NCA; | ||
| 60 | 64 | ||
| 61 | return FileType::Unknown; | 65 | return FileType::Unknown; |
| 62 | } | 66 | } |
| @@ -69,6 +73,8 @@ const char* GetFileTypeString(FileType type) { | |||
| 69 | return "NRO"; | 73 | return "NRO"; |
| 70 | case FileType::NSO: | 74 | case FileType::NSO: |
| 71 | return "NSO"; | 75 | return "NSO"; |
| 76 | case FileType::NCA: | ||
| 77 | return "NCA"; | ||
| 72 | case FileType::DeconstructedRomDirectory: | 78 | case FileType::DeconstructedRomDirectory: |
| 73 | return "Directory"; | 79 | return "Directory"; |
| 74 | case FileType::Error: | 80 | case FileType::Error: |
| @@ -104,6 +110,10 @@ static std::unique_ptr<AppLoader> GetFileLoader(FileUtil::IOFile&& file, FileTyp | |||
| 104 | case FileType::NRO: | 110 | case FileType::NRO: |
| 105 | return std::make_unique<AppLoader_NRO>(std::move(file), filepath); | 111 | return std::make_unique<AppLoader_NRO>(std::move(file), filepath); |
| 106 | 112 | ||
| 113 | // NX NCA file format. | ||
| 114 | case FileType::NCA: | ||
| 115 | return std::make_unique<AppLoader_NCA>(std::move(file), filepath); | ||
| 116 | |||
| 107 | // NX deconstructed ROM directory. | 117 | // NX deconstructed ROM directory. |
| 108 | case FileType::DeconstructedRomDirectory: | 118 | case FileType::DeconstructedRomDirectory: |
| 109 | return std::make_unique<AppLoader_DeconstructedRomDirectory>(std::move(file), filepath); | 119 | return std::make_unique<AppLoader_DeconstructedRomDirectory>(std::move(file), filepath); |
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h index b1aabb1cb..b76f7b13d 100644 --- a/src/core/loader/loader.h +++ b/src/core/loader/loader.h | |||
| @@ -29,6 +29,7 @@ enum class FileType { | |||
| 29 | ELF, | 29 | ELF, |
| 30 | NSO, | 30 | NSO, |
| 31 | NRO, | 31 | NRO, |
| 32 | NCA, | ||
| 32 | DeconstructedRomDirectory, | 33 | DeconstructedRomDirectory, |
| 33 | }; | 34 | }; |
| 34 | 35 | ||
diff --git a/src/core/loader/nca.cpp b/src/core/loader/nca.cpp new file mode 100644 index 000000000..067945d46 --- /dev/null +++ b/src/core/loader/nca.cpp | |||
| @@ -0,0 +1,303 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "common/common_funcs.h" | ||
| 8 | #include "common/file_util.h" | ||
| 9 | #include "common/logging/log.h" | ||
| 10 | #include "common/swap.h" | ||
| 11 | #include "core/core.h" | ||
| 12 | #include "core/file_sys/program_metadata.h" | ||
| 13 | #include "core/file_sys/romfs_factory.h" | ||
| 14 | #include "core/hle/kernel/process.h" | ||
| 15 | #include "core/hle/kernel/resource_limit.h" | ||
| 16 | #include "core/hle/service/filesystem/filesystem.h" | ||
| 17 | #include "core/loader/nca.h" | ||
| 18 | #include "core/loader/nso.h" | ||
| 19 | #include "core/memory.h" | ||
| 20 | |||
| 21 | namespace Loader { | ||
| 22 | |||
| 23 | // Media offsets in headers are stored divided by 512. Mult. by this to get real offset. | ||
| 24 | constexpr u64 MEDIA_OFFSET_MULTIPLIER = 0x200; | ||
| 25 | |||
| 26 | constexpr u64 SECTION_HEADER_SIZE = 0x200; | ||
| 27 | constexpr u64 SECTION_HEADER_OFFSET = 0x400; | ||
| 28 | |||
| 29 | enum class NcaContentType : u8 { Program = 0, Meta = 1, Control = 2, Manual = 3, Data = 4 }; | ||
| 30 | |||
| 31 | enum class NcaSectionFilesystemType : u8 { PFS0 = 0x2, ROMFS = 0x3 }; | ||
| 32 | |||
| 33 | struct NcaSectionTableEntry { | ||
| 34 | u32_le media_offset; | ||
| 35 | u32_le media_end_offset; | ||
| 36 | INSERT_PADDING_BYTES(0x8); | ||
| 37 | }; | ||
| 38 | static_assert(sizeof(NcaSectionTableEntry) == 0x10, "NcaSectionTableEntry has incorrect size."); | ||
| 39 | |||
| 40 | struct NcaHeader { | ||
| 41 | std::array<u8, 0x100> rsa_signature_1; | ||
| 42 | std::array<u8, 0x100> rsa_signature_2; | ||
| 43 | u32_le magic; | ||
| 44 | u8 is_system; | ||
| 45 | NcaContentType content_type; | ||
| 46 | u8 crypto_type; | ||
| 47 | u8 key_index; | ||
| 48 | u64_le size; | ||
| 49 | u64_le title_id; | ||
| 50 | INSERT_PADDING_BYTES(0x4); | ||
| 51 | u32_le sdk_version; | ||
| 52 | u8 crypto_type_2; | ||
| 53 | INSERT_PADDING_BYTES(15); | ||
| 54 | std::array<u8, 0x10> rights_id; | ||
| 55 | std::array<NcaSectionTableEntry, 0x4> section_tables; | ||
| 56 | std::array<std::array<u8, 0x20>, 0x4> hash_tables; | ||
| 57 | std::array<std::array<u8, 0x10>, 0x4> key_area; | ||
| 58 | INSERT_PADDING_BYTES(0xC0); | ||
| 59 | }; | ||
| 60 | static_assert(sizeof(NcaHeader) == 0x400, "NcaHeader has incorrect size."); | ||
| 61 | |||
| 62 | struct NcaSectionHeaderBlock { | ||
| 63 | INSERT_PADDING_BYTES(3); | ||
| 64 | NcaSectionFilesystemType filesystem_type; | ||
| 65 | u8 crypto_type; | ||
| 66 | INSERT_PADDING_BYTES(3); | ||
| 67 | }; | ||
| 68 | static_assert(sizeof(NcaSectionHeaderBlock) == 0x8, "NcaSectionHeaderBlock has incorrect size."); | ||
| 69 | |||
| 70 | struct Pfs0Superblock { | ||
| 71 | NcaSectionHeaderBlock header_block; | ||
| 72 | std::array<u8, 0x20> hash; | ||
| 73 | u32_le size; | ||
| 74 | INSERT_PADDING_BYTES(4); | ||
| 75 | u64_le hash_table_offset; | ||
| 76 | u64_le hash_table_size; | ||
| 77 | u64_le pfs0_header_offset; | ||
| 78 | u64_le pfs0_size; | ||
| 79 | INSERT_PADDING_BYTES(432); | ||
| 80 | }; | ||
| 81 | static_assert(sizeof(Pfs0Superblock) == 0x200, "Pfs0Superblock has incorrect size."); | ||
| 82 | |||
| 83 | static bool IsValidNca(const NcaHeader& header) { | ||
| 84 | return header.magic == Common::MakeMagic('N', 'C', 'A', '2') || | ||
| 85 | header.magic == Common::MakeMagic('N', 'C', 'A', '3'); | ||
| 86 | } | ||
| 87 | |||
| 88 | // TODO(DarkLordZach): Add support for encrypted. | ||
| 89 | class Nca final { | ||
| 90 | std::vector<FileSys::PartitionFilesystem> pfs; | ||
| 91 | std::vector<u64> pfs_offset; | ||
| 92 | |||
| 93 | u64 romfs_offset = 0; | ||
| 94 | u64 romfs_size = 0; | ||
| 95 | |||
| 96 | boost::optional<u8> exefs_id = boost::none; | ||
| 97 | |||
| 98 | FileUtil::IOFile file; | ||
| 99 | std::string path; | ||
| 100 | |||
| 101 | u64 GetExeFsFileOffset(const std::string& file_name) const; | ||
| 102 | u64 GetExeFsFileSize(const std::string& file_name) const; | ||
| 103 | |||
| 104 | public: | ||
| 105 | ResultStatus Load(FileUtil::IOFile&& file, std::string path); | ||
| 106 | |||
| 107 | FileSys::PartitionFilesystem GetPfs(u8 id) const; | ||
| 108 | |||
| 109 | u64 GetRomFsOffset() const; | ||
| 110 | u64 GetRomFsSize() const; | ||
| 111 | |||
| 112 | std::vector<u8> GetExeFsFile(const std::string& file_name); | ||
| 113 | }; | ||
| 114 | |||
| 115 | static bool IsPfsExeFs(const FileSys::PartitionFilesystem& pfs) { | ||
| 116 | // According to switchbrew, an exefs must only contain these two files: | ||
| 117 | return pfs.GetFileSize("main") > 0 && pfs.GetFileSize("main.npdm") > 0; | ||
| 118 | } | ||
| 119 | |||
| 120 | ResultStatus Nca::Load(FileUtil::IOFile&& in_file, std::string in_path) { | ||
| 121 | file = std::move(in_file); | ||
| 122 | path = in_path; | ||
| 123 | file.Seek(0, SEEK_SET); | ||
| 124 | std::array<u8, sizeof(NcaHeader)> header_array{}; | ||
| 125 | if (sizeof(NcaHeader) != file.ReadBytes(header_array.data(), sizeof(NcaHeader))) | ||
| 126 | NGLOG_CRITICAL(Loader, "File reader errored out during header read."); | ||
| 127 | |||
| 128 | NcaHeader header{}; | ||
| 129 | std::memcpy(&header, header_array.data(), sizeof(NcaHeader)); | ||
| 130 | if (!IsValidNca(header)) | ||
| 131 | return ResultStatus::ErrorInvalidFormat; | ||
| 132 | |||
| 133 | int number_sections = | ||
| 134 | std::count_if(std::begin(header.section_tables), std::end(header.section_tables), | ||
| 135 | [](NcaSectionTableEntry entry) { return entry.media_offset > 0; }); | ||
| 136 | |||
| 137 | for (int i = 0; i < number_sections; ++i) { | ||
| 138 | // Seek to beginning of this section. | ||
| 139 | file.Seek(SECTION_HEADER_OFFSET + i * SECTION_HEADER_SIZE, SEEK_SET); | ||
| 140 | std::array<u8, sizeof(NcaSectionHeaderBlock)> array{}; | ||
| 141 | if (sizeof(NcaSectionHeaderBlock) != | ||
| 142 | file.ReadBytes(array.data(), sizeof(NcaSectionHeaderBlock))) | ||
| 143 | NGLOG_CRITICAL(Loader, "File reader errored out during header read."); | ||
| 144 | |||
| 145 | NcaSectionHeaderBlock block{}; | ||
| 146 | std::memcpy(&block, array.data(), sizeof(NcaSectionHeaderBlock)); | ||
| 147 | |||
| 148 | if (block.filesystem_type == NcaSectionFilesystemType::ROMFS) { | ||
| 149 | romfs_offset = header.section_tables[i].media_offset * MEDIA_OFFSET_MULTIPLIER; | ||
| 150 | romfs_size = | ||
| 151 | header.section_tables[i].media_end_offset * MEDIA_OFFSET_MULTIPLIER - romfs_offset; | ||
| 152 | } else if (block.filesystem_type == NcaSectionFilesystemType::PFS0) { | ||
| 153 | Pfs0Superblock sb{}; | ||
| 154 | // Seek back to beginning of this section. | ||
| 155 | file.Seek(SECTION_HEADER_OFFSET + i * SECTION_HEADER_SIZE, SEEK_SET); | ||
| 156 | if (sizeof(Pfs0Superblock) != file.ReadBytes(&sb, sizeof(Pfs0Superblock))) | ||
| 157 | NGLOG_CRITICAL(Loader, "File reader errored out during header read."); | ||
| 158 | |||
| 159 | u64 offset = (static_cast<u64>(header.section_tables[i].media_offset) * | ||
| 160 | MEDIA_OFFSET_MULTIPLIER) + | ||
| 161 | sb.pfs0_header_offset; | ||
| 162 | FileSys::PartitionFilesystem npfs{}; | ||
| 163 | ResultStatus status = npfs.Load(path, offset); | ||
| 164 | |||
| 165 | if (status == ResultStatus::Success) { | ||
| 166 | pfs.emplace_back(std::move(npfs)); | ||
| 167 | pfs_offset.emplace_back(offset); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | for (size_t i = 0; i < pfs.size(); ++i) { | ||
| 173 | if (IsPfsExeFs(pfs[i])) | ||
| 174 | exefs_id = i; | ||
| 175 | } | ||
| 176 | |||
| 177 | return ResultStatus::Success; | ||
| 178 | } | ||
| 179 | |||
| 180 | FileSys::PartitionFilesystem Nca::GetPfs(u8 id) const { | ||
| 181 | return pfs[id]; | ||
| 182 | } | ||
| 183 | |||
| 184 | u64 Nca::GetExeFsFileOffset(const std::string& file_name) const { | ||
| 185 | if (exefs_id == boost::none) | ||
| 186 | return 0; | ||
| 187 | return pfs[*exefs_id].GetFileOffset(file_name) + pfs_offset[*exefs_id]; | ||
| 188 | } | ||
| 189 | |||
| 190 | u64 Nca::GetExeFsFileSize(const std::string& file_name) const { | ||
| 191 | if (exefs_id == boost::none) | ||
| 192 | return 0; | ||
| 193 | return pfs[*exefs_id].GetFileSize(file_name); | ||
| 194 | } | ||
| 195 | |||
| 196 | u64 Nca::GetRomFsOffset() const { | ||
| 197 | return romfs_offset; | ||
| 198 | } | ||
| 199 | |||
| 200 | u64 Nca::GetRomFsSize() const { | ||
| 201 | return romfs_size; | ||
| 202 | } | ||
| 203 | |||
| 204 | std::vector<u8> Nca::GetExeFsFile(const std::string& file_name) { | ||
| 205 | std::vector<u8> out(GetExeFsFileSize(file_name)); | ||
| 206 | file.Seek(GetExeFsFileOffset(file_name), SEEK_SET); | ||
| 207 | file.ReadBytes(out.data(), GetExeFsFileSize(file_name)); | ||
| 208 | return out; | ||
| 209 | } | ||
| 210 | |||
| 211 | AppLoader_NCA::AppLoader_NCA(FileUtil::IOFile&& file, std::string filepath) | ||
| 212 | : AppLoader(std::move(file)), filepath(std::move(filepath)) {} | ||
| 213 | |||
| 214 | FileType AppLoader_NCA::IdentifyType(FileUtil::IOFile& file, const std::string&) { | ||
| 215 | file.Seek(0, SEEK_SET); | ||
| 216 | std::array<u8, 0x400> header_enc_array{}; | ||
| 217 | if (0x400 != file.ReadBytes(header_enc_array.data(), 0x400)) | ||
| 218 | return FileType::Error; | ||
| 219 | |||
| 220 | // TODO(DarkLordZach): Assuming everything is decrypted. Add crypto support. | ||
| 221 | NcaHeader header{}; | ||
| 222 | std::memcpy(&header, header_enc_array.data(), sizeof(NcaHeader)); | ||
| 223 | |||
| 224 | if (IsValidNca(header) && header.content_type == NcaContentType::Program) | ||
| 225 | return FileType::NCA; | ||
| 226 | |||
| 227 | return FileType::Error; | ||
| 228 | } | ||
| 229 | |||
| 230 | ResultStatus AppLoader_NCA::Load(Kernel::SharedPtr<Kernel::Process>& process) { | ||
| 231 | if (is_loaded) { | ||
| 232 | return ResultStatus::ErrorAlreadyLoaded; | ||
| 233 | } | ||
| 234 | if (!file.IsOpen()) { | ||
| 235 | return ResultStatus::Error; | ||
| 236 | } | ||
| 237 | |||
| 238 | nca = std::make_unique<Nca>(); | ||
| 239 | ResultStatus result = nca->Load(std::move(file), filepath); | ||
| 240 | if (result != ResultStatus::Success) { | ||
| 241 | return result; | ||
| 242 | } | ||
| 243 | |||
| 244 | result = metadata.Load(nca->GetExeFsFile("main.npdm")); | ||
| 245 | if (result != ResultStatus::Success) { | ||
| 246 | return result; | ||
| 247 | } | ||
| 248 | metadata.Print(); | ||
| 249 | |||
| 250 | const FileSys::ProgramAddressSpaceType arch_bits{metadata.GetAddressSpaceType()}; | ||
| 251 | if (arch_bits == FileSys::ProgramAddressSpaceType::Is32Bit) { | ||
| 252 | return ResultStatus::ErrorUnsupportedArch; | ||
| 253 | } | ||
| 254 | |||
| 255 | VAddr next_load_addr{Memory::PROCESS_IMAGE_VADDR}; | ||
| 256 | for (const auto& module : {"rtld", "main", "subsdk0", "subsdk1", "subsdk2", "subsdk3", | ||
| 257 | "subsdk4", "subsdk5", "subsdk6", "subsdk7", "sdk"}) { | ||
| 258 | const VAddr load_addr = next_load_addr; | ||
| 259 | next_load_addr = AppLoader_NSO::LoadModule(module, nca->GetExeFsFile(module), load_addr); | ||
| 260 | if (next_load_addr) { | ||
| 261 | NGLOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", module, load_addr); | ||
| 262 | } else { | ||
| 263 | next_load_addr = load_addr; | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | process->program_id = metadata.GetTitleID(); | ||
| 268 | process->svc_access_mask.set(); | ||
| 269 | process->address_mappings = default_address_mappings; | ||
| 270 | process->resource_limit = | ||
| 271 | Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); | ||
| 272 | process->Run(Memory::PROCESS_IMAGE_VADDR, metadata.GetMainThreadPriority(), | ||
| 273 | metadata.GetMainThreadStackSize()); | ||
| 274 | |||
| 275 | if (nca->GetRomFsSize() > 0) | ||
| 276 | Service::FileSystem::RegisterFileSystem(std::make_unique<FileSys::RomFS_Factory>(*this), | ||
| 277 | Service::FileSystem::Type::RomFS); | ||
| 278 | |||
| 279 | is_loaded = true; | ||
| 280 | return ResultStatus::Success; | ||
| 281 | } | ||
| 282 | |||
| 283 | ResultStatus AppLoader_NCA::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, | ||
| 284 | u64& size) { | ||
| 285 | if (nca->GetRomFsSize() == 0) { | ||
| 286 | NGLOG_DEBUG(Loader, "No RomFS available"); | ||
| 287 | return ResultStatus::ErrorNotUsed; | ||
| 288 | } | ||
| 289 | |||
| 290 | romfs_file = std::make_shared<FileUtil::IOFile>(filepath, "rb"); | ||
| 291 | |||
| 292 | offset = nca->GetRomFsOffset(); | ||
| 293 | size = nca->GetRomFsSize(); | ||
| 294 | |||
| 295 | NGLOG_DEBUG(Loader, "RomFS offset: 0x{:016X}", offset); | ||
| 296 | NGLOG_DEBUG(Loader, "RomFS size: 0x{:016X}", size); | ||
| 297 | |||
| 298 | return ResultStatus::Success; | ||
| 299 | } | ||
| 300 | |||
| 301 | AppLoader_NCA::~AppLoader_NCA() = default; | ||
| 302 | |||
| 303 | } // namespace Loader | ||
diff --git a/src/core/loader/nca.h b/src/core/loader/nca.h new file mode 100644 index 000000000..3b6c451d0 --- /dev/null +++ b/src/core/loader/nca.h | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <string> | ||
| 8 | #include "common/common_types.h" | ||
| 9 | #include "core/file_sys/partition_filesystem.h" | ||
| 10 | #include "core/file_sys/program_metadata.h" | ||
| 11 | #include "core/hle/kernel/kernel.h" | ||
| 12 | #include "core/loader/loader.h" | ||
| 13 | |||
| 14 | namespace Loader { | ||
| 15 | |||
| 16 | class Nca; | ||
| 17 | |||
| 18 | /// Loads an NCA file | ||
| 19 | class AppLoader_NCA final : public AppLoader { | ||
| 20 | public: | ||
| 21 | AppLoader_NCA(FileUtil::IOFile&& file, std::string filepath); | ||
| 22 | |||
| 23 | /** | ||
| 24 | * Returns the type of the file | ||
| 25 | * @param file FileUtil::IOFile open file | ||
| 26 | * @param filepath Path of the file that we are opening. | ||
| 27 | * @return FileType found, or FileType::Error if this loader doesn't know it | ||
| 28 | */ | ||
| 29 | static FileType IdentifyType(FileUtil::IOFile& file, const std::string& filepath); | ||
| 30 | |||
| 31 | FileType GetFileType() override { | ||
| 32 | return IdentifyType(file, filepath); | ||
| 33 | } | ||
| 34 | |||
| 35 | ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; | ||
| 36 | |||
| 37 | ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, | ||
| 38 | u64& size) override; | ||
| 39 | |||
| 40 | ~AppLoader_NCA(); | ||
| 41 | |||
| 42 | private: | ||
| 43 | std::string filepath; | ||
| 44 | FileSys::ProgramMetadata metadata; | ||
| 45 | |||
| 46 | std::unique_ptr<Nca> nca; | ||
| 47 | }; | ||
| 48 | |||
| 49 | } // namespace Loader | ||
diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp index 01be9e217..845ed7e90 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp | |||
| @@ -66,8 +66,22 @@ FileType AppLoader_NSO::IdentifyType(FileUtil::IOFile& file, const std::string&) | |||
| 66 | return FileType::Error; | 66 | return FileType::Error; |
| 67 | } | 67 | } |
| 68 | 68 | ||
| 69 | static std::vector<u8> DecompressSegment(const std::vector<u8>& compressed_data, | ||
| 70 | const NsoSegmentHeader& header) { | ||
| 71 | std::vector<u8> uncompressed_data; | ||
| 72 | uncompressed_data.resize(header.size); | ||
| 73 | const int bytes_uncompressed = LZ4_decompress_safe( | ||
| 74 | reinterpret_cast<const char*>(compressed_data.data()), | ||
| 75 | reinterpret_cast<char*>(uncompressed_data.data()), compressed_data.size(), header.size); | ||
| 76 | |||
| 77 | ASSERT_MSG(bytes_uncompressed == header.size && bytes_uncompressed == uncompressed_data.size(), | ||
| 78 | "{} != {} != {}", bytes_uncompressed, header.size, uncompressed_data.size()); | ||
| 79 | |||
| 80 | return uncompressed_data; | ||
| 81 | } | ||
| 82 | |||
| 69 | static std::vector<u8> ReadSegment(FileUtil::IOFile& file, const NsoSegmentHeader& header, | 83 | static std::vector<u8> ReadSegment(FileUtil::IOFile& file, const NsoSegmentHeader& header, |
| 70 | int compressed_size) { | 84 | size_t compressed_size) { |
| 71 | std::vector<u8> compressed_data; | 85 | std::vector<u8> compressed_data; |
| 72 | compressed_data.resize(compressed_size); | 86 | compressed_data.resize(compressed_size); |
| 73 | 87 | ||
| @@ -77,22 +91,65 @@ static std::vector<u8> ReadSegment(FileUtil::IOFile& file, const NsoSegmentHeade | |||
| 77 | return {}; | 91 | return {}; |
| 78 | } | 92 | } |
| 79 | 93 | ||
| 80 | std::vector<u8> uncompressed_data; | 94 | return DecompressSegment(compressed_data, header); |
| 81 | uncompressed_data.resize(header.size); | ||
| 82 | const int bytes_uncompressed = LZ4_decompress_safe( | ||
| 83 | reinterpret_cast<const char*>(compressed_data.data()), | ||
| 84 | reinterpret_cast<char*>(uncompressed_data.data()), compressed_size, header.size); | ||
| 85 | |||
| 86 | ASSERT_MSG(bytes_uncompressed == header.size && bytes_uncompressed == uncompressed_data.size(), | ||
| 87 | "{} != {} != {}", bytes_uncompressed, header.size, uncompressed_data.size()); | ||
| 88 | |||
| 89 | return uncompressed_data; | ||
| 90 | } | 95 | } |
| 91 | 96 | ||
| 92 | static constexpr u32 PageAlignSize(u32 size) { | 97 | static constexpr u32 PageAlignSize(u32 size) { |
| 93 | return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK; | 98 | return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK; |
| 94 | } | 99 | } |
| 95 | 100 | ||
| 101 | VAddr AppLoader_NSO::LoadModule(const std::string& name, const std::vector<u8>& file_data, | ||
| 102 | VAddr load_base) { | ||
| 103 | if (file_data.size() < sizeof(NsoHeader)) | ||
| 104 | return {}; | ||
| 105 | |||
| 106 | NsoHeader nso_header; | ||
| 107 | std::memcpy(&nso_header, file_data.data(), sizeof(NsoHeader)); | ||
| 108 | |||
| 109 | if (nso_header.magic != Common::MakeMagic('N', 'S', 'O', '0')) | ||
| 110 | return {}; | ||
| 111 | |||
| 112 | // Build program image | ||
| 113 | Kernel::SharedPtr<Kernel::CodeSet> codeset = Kernel::CodeSet::Create(""); | ||
| 114 | std::vector<u8> program_image; | ||
| 115 | for (int i = 0; i < nso_header.segments.size(); ++i) { | ||
| 116 | std::vector<u8> compressed_data(nso_header.segments_compressed_size[i]); | ||
| 117 | for (int j = 0; j < nso_header.segments_compressed_size[i]; ++j) | ||
| 118 | compressed_data[j] = file_data[nso_header.segments[i].offset + j]; | ||
| 119 | std::vector<u8> data = DecompressSegment(compressed_data, nso_header.segments[i]); | ||
| 120 | program_image.resize(nso_header.segments[i].location); | ||
| 121 | program_image.insert(program_image.end(), data.begin(), data.end()); | ||
| 122 | codeset->segments[i].addr = nso_header.segments[i].location; | ||
| 123 | codeset->segments[i].offset = nso_header.segments[i].location; | ||
| 124 | codeset->segments[i].size = PageAlignSize(static_cast<u32>(data.size())); | ||
| 125 | } | ||
| 126 | |||
| 127 | // MOD header pointer is at .text offset + 4 | ||
| 128 | u32 module_offset; | ||
| 129 | std::memcpy(&module_offset, program_image.data() + 4, sizeof(u32)); | ||
| 130 | |||
| 131 | // Read MOD header | ||
| 132 | ModHeader mod_header{}; | ||
| 133 | // Default .bss to size in segment header if MOD0 section doesn't exist | ||
| 134 | u32 bss_size{PageAlignSize(nso_header.segments[2].bss_size)}; | ||
| 135 | std::memcpy(&mod_header, program_image.data() + module_offset, sizeof(ModHeader)); | ||
| 136 | const bool has_mod_header{mod_header.magic == Common::MakeMagic('M', 'O', 'D', '0')}; | ||
| 137 | if (has_mod_header) { | ||
| 138 | // Resize program image to include .bss section and page align each section | ||
| 139 | bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset); | ||
| 140 | } | ||
| 141 | codeset->data.size += bss_size; | ||
| 142 | const u32 image_size{PageAlignSize(static_cast<u32>(program_image.size()) + bss_size)}; | ||
| 143 | program_image.resize(image_size); | ||
| 144 | |||
| 145 | // Load codeset for current process | ||
| 146 | codeset->name = name; | ||
| 147 | codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image)); | ||
| 148 | Core::CurrentProcess()->LoadModule(codeset, load_base); | ||
| 149 | |||
| 150 | return load_base + image_size; | ||
| 151 | } | ||
| 152 | |||
| 96 | VAddr AppLoader_NSO::LoadModule(const std::string& path, VAddr load_base) { | 153 | VAddr AppLoader_NSO::LoadModule(const std::string& path, VAddr load_base) { |
| 97 | FileUtil::IOFile file(path, "rb"); | 154 | FileUtil::IOFile file(path, "rb"); |
| 98 | if (!file.IsOpen()) { | 155 | if (!file.IsOpen()) { |
diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h index 1ae30a824..386f4d39a 100644 --- a/src/core/loader/nso.h +++ b/src/core/loader/nso.h | |||
| @@ -29,6 +29,9 @@ public: | |||
| 29 | return IdentifyType(file, filepath); | 29 | return IdentifyType(file, filepath); |
| 30 | } | 30 | } |
| 31 | 31 | ||
| 32 | static VAddr LoadModule(const std::string& name, const std::vector<u8>& file_data, | ||
| 33 | VAddr load_base); | ||
| 34 | |||
| 32 | static VAddr LoadModule(const std::string& path, VAddr load_base); | 35 | static VAddr LoadModule(const std::string& path, VAddr load_base); |
| 33 | 36 | ||
| 34 | ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; | 37 | ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; |
diff --git a/src/yuzu/game_list.cpp b/src/yuzu/game_list.cpp index 9e585b082..55dce6d47 100644 --- a/src/yuzu/game_list.cpp +++ b/src/yuzu/game_list.cpp | |||
| @@ -366,7 +366,7 @@ void GameList::LoadInterfaceLayout() { | |||
| 366 | item_model->sort(header->sortIndicatorSection(), header->sortIndicatorOrder()); | 366 | item_model->sort(header->sortIndicatorSection(), header->sortIndicatorOrder()); |
| 367 | } | 367 | } |
| 368 | 368 | ||
| 369 | const QStringList GameList::supported_file_extensions = {"nso", "nro"}; | 369 | const QStringList GameList::supported_file_extensions = {"nso", "nro", "nca"}; |
| 370 | 370 | ||
| 371 | static bool HasSupportedFileExtension(const std::string& file_name) { | 371 | static bool HasSupportedFileExtension(const std::string& file_name) { |
| 372 | QFileInfo file = QFileInfo(file_name.c_str()); | 372 | QFileInfo file = QFileInfo(file_name.c_str()); |