diff options
| author | 2017-09-25 00:17:38 -0600 | |
|---|---|---|
| committer | 2017-09-25 08:17:38 +0200 | |
| commit | c91ccbd0ba4118554d7377bbc3bd4c64f9bccf84 (patch) | |
| tree | d56b08a52a11f229e9ce30aeda2b22bfccd48034 /src/core/file_sys | |
| parent | Services/UDS: Added a function to send EAPoL-Start packets (#2920) (diff) | |
| download | yuzu-c91ccbd0ba4118554d7377bbc3bd4c64f9bccf84.tar.gz yuzu-c91ccbd0ba4118554d7377bbc3bd4c64f9bccf84.tar.xz yuzu-c91ccbd0ba4118554d7377bbc3bd4c64f9bccf84.zip | |
Loader/NCCH: Add support for loading application updates (#2927)
* loader/ncch: split NCCH parsing into its own file
* loader/ncch: add support for loading update NCCHs from the SD card
* loader/ncch: fix formatting
* file_sys/ncch_container: Return a value for OpenFile
* loader/ncch: cleanup, always instantiate overlay_ncch to base_ncch
* file_sys/ncch_container: better encryption checks, allow non-app NCCHs to load properly and for the existence of NCCH structures to be checked
* file_sys/ncch_container: pass filepath as a const reference
Diffstat (limited to 'src/core/file_sys')
| -rw-r--r-- | src/core/file_sys/archive_selfncch.cpp | 28 | ||||
| -rw-r--r-- | src/core/file_sys/archive_selfncch.h | 4 | ||||
| -rw-r--r-- | src/core/file_sys/ncch_container.cpp | 316 | ||||
| -rw-r--r-- | src/core/file_sys/ncch_container.h | 244 |
4 files changed, 587 insertions, 5 deletions
diff --git a/src/core/file_sys/archive_selfncch.cpp b/src/core/file_sys/archive_selfncch.cpp index 298a37a44..7dc91a405 100644 --- a/src/core/file_sys/archive_selfncch.cpp +++ b/src/core/file_sys/archive_selfncch.cpp | |||
| @@ -102,8 +102,7 @@ public: | |||
| 102 | 102 | ||
| 103 | switch (static_cast<SelfNCCHFilePathType>(file_path.type)) { | 103 | switch (static_cast<SelfNCCHFilePathType>(file_path.type)) { |
| 104 | case SelfNCCHFilePathType::UpdateRomFS: | 104 | case SelfNCCHFilePathType::UpdateRomFS: |
| 105 | LOG_WARNING(Service_FS, "(STUBBED) open update RomFS"); | 105 | return OpenUpdateRomFS(); |
| 106 | return OpenRomFS(); | ||
| 107 | 106 | ||
| 108 | case SelfNCCHFilePathType::RomFS: | 107 | case SelfNCCHFilePathType::RomFS: |
| 109 | return OpenRomFS(); | 108 | return OpenRomFS(); |
| @@ -179,6 +178,17 @@ private: | |||
| 179 | } | 178 | } |
| 180 | } | 179 | } |
| 181 | 180 | ||
| 181 | ResultVal<std::unique_ptr<FileBackend>> OpenUpdateRomFS() const { | ||
| 182 | if (ncch_data.update_romfs_file) { | ||
| 183 | return MakeResult<std::unique_ptr<FileBackend>>(std::make_unique<IVFCFile>( | ||
| 184 | ncch_data.update_romfs_file, ncch_data.update_romfs_offset, | ||
| 185 | ncch_data.update_romfs_size)); | ||
| 186 | } else { | ||
| 187 | LOG_INFO(Service_FS, "Unable to read update RomFS"); | ||
| 188 | return ERROR_ROMFS_NOT_FOUND; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 182 | ResultVal<std::unique_ptr<FileBackend>> OpenExeFS(const std::string& filename) const { | 192 | ResultVal<std::unique_ptr<FileBackend>> OpenExeFS(const std::string& filename) const { |
| 183 | if (filename == "icon") { | 193 | if (filename == "icon") { |
| 184 | if (ncch_data.icon) { | 194 | if (ncch_data.icon) { |
| @@ -218,11 +228,19 @@ private: | |||
| 218 | }; | 228 | }; |
| 219 | 229 | ||
| 220 | ArchiveFactory_SelfNCCH::ArchiveFactory_SelfNCCH(Loader::AppLoader& app_loader) { | 230 | ArchiveFactory_SelfNCCH::ArchiveFactory_SelfNCCH(Loader::AppLoader& app_loader) { |
| 221 | std::shared_ptr<FileUtil::IOFile> romfs_file_; | 231 | std::shared_ptr<FileUtil::IOFile> romfs_file; |
| 232 | if (Loader::ResultStatus::Success == | ||
| 233 | app_loader.ReadRomFS(romfs_file, ncch_data.romfs_offset, ncch_data.romfs_size)) { | ||
| 234 | |||
| 235 | ncch_data.romfs_file = std::move(romfs_file); | ||
| 236 | } | ||
| 237 | |||
| 238 | std::shared_ptr<FileUtil::IOFile> update_romfs_file; | ||
| 222 | if (Loader::ResultStatus::Success == | 239 | if (Loader::ResultStatus::Success == |
| 223 | app_loader.ReadRomFS(romfs_file_, ncch_data.romfs_offset, ncch_data.romfs_size)) { | 240 | app_loader.ReadUpdateRomFS(update_romfs_file, ncch_data.update_romfs_offset, |
| 241 | ncch_data.update_romfs_size)) { | ||
| 224 | 242 | ||
| 225 | ncch_data.romfs_file = std::move(romfs_file_); | 243 | ncch_data.update_romfs_file = std::move(update_romfs_file); |
| 226 | } | 244 | } |
| 227 | 245 | ||
| 228 | std::vector<u8> buffer; | 246 | std::vector<u8> buffer; |
diff --git a/src/core/file_sys/archive_selfncch.h b/src/core/file_sys/archive_selfncch.h index f1b971296..f1c659948 100644 --- a/src/core/file_sys/archive_selfncch.h +++ b/src/core/file_sys/archive_selfncch.h | |||
| @@ -24,6 +24,10 @@ struct NCCHData { | |||
| 24 | std::shared_ptr<FileUtil::IOFile> romfs_file; | 24 | std::shared_ptr<FileUtil::IOFile> romfs_file; |
| 25 | u64 romfs_offset = 0; | 25 | u64 romfs_offset = 0; |
| 26 | u64 romfs_size = 0; | 26 | u64 romfs_size = 0; |
| 27 | |||
| 28 | std::shared_ptr<FileUtil::IOFile> update_romfs_file; | ||
| 29 | u64 update_romfs_offset = 0; | ||
| 30 | u64 update_romfs_size = 0; | ||
| 27 | }; | 31 | }; |
| 28 | 32 | ||
| 29 | /// File system interface to the SelfNCCH archive | 33 | /// File system interface to the SelfNCCH archive |
diff --git a/src/core/file_sys/ncch_container.cpp b/src/core/file_sys/ncch_container.cpp new file mode 100644 index 000000000..59c72f3e9 --- /dev/null +++ b/src/core/file_sys/ncch_container.cpp | |||
| @@ -0,0 +1,316 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <cinttypes> | ||
| 6 | #include <cstring> | ||
| 7 | #include <memory> | ||
| 8 | #include "common/common_types.h" | ||
| 9 | #include "common/logging/log.h" | ||
| 10 | #include "core/core.h" | ||
| 11 | #include "core/file_sys/ncch_container.h" | ||
| 12 | #include "core/loader/loader.h" | ||
| 13 | |||
| 14 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 15 | // FileSys namespace | ||
| 16 | |||
| 17 | namespace FileSys { | ||
| 18 | |||
| 19 | static const int kMaxSections = 8; ///< Maximum number of sections (files) in an ExeFs | ||
| 20 | static const int kBlockSize = 0x200; ///< Size of ExeFS blocks (in bytes) | ||
| 21 | |||
| 22 | /** | ||
| 23 | * Get the decompressed size of an LZSS compressed ExeFS file | ||
| 24 | * @param buffer Buffer of compressed file | ||
| 25 | * @param size Size of compressed buffer | ||
| 26 | * @return Size of decompressed buffer | ||
| 27 | */ | ||
| 28 | static u32 LZSS_GetDecompressedSize(const u8* buffer, u32 size) { | ||
| 29 | u32 offset_size = *(u32*)(buffer + size - 4); | ||
| 30 | return offset_size + size; | ||
| 31 | } | ||
| 32 | |||
| 33 | /** | ||
| 34 | * Decompress ExeFS file (compressed with LZSS) | ||
| 35 | * @param compressed Compressed buffer | ||
| 36 | * @param compressed_size Size of compressed buffer | ||
| 37 | * @param decompressed Decompressed buffer | ||
| 38 | * @param decompressed_size Size of decompressed buffer | ||
| 39 | * @return True on success, otherwise false | ||
| 40 | */ | ||
| 41 | static bool LZSS_Decompress(const u8* compressed, u32 compressed_size, u8* decompressed, | ||
| 42 | u32 decompressed_size) { | ||
| 43 | const u8* footer = compressed + compressed_size - 8; | ||
| 44 | u32 buffer_top_and_bottom = *reinterpret_cast<const u32*>(footer); | ||
| 45 | u32 out = decompressed_size; | ||
| 46 | u32 index = compressed_size - ((buffer_top_and_bottom >> 24) & 0xFF); | ||
| 47 | u32 stop_index = compressed_size - (buffer_top_and_bottom & 0xFFFFFF); | ||
| 48 | |||
| 49 | memset(decompressed, 0, decompressed_size); | ||
| 50 | memcpy(decompressed, compressed, compressed_size); | ||
| 51 | |||
| 52 | while (index > stop_index) { | ||
| 53 | u8 control = compressed[--index]; | ||
| 54 | |||
| 55 | for (unsigned i = 0; i < 8; i++) { | ||
| 56 | if (index <= stop_index) | ||
| 57 | break; | ||
| 58 | if (index <= 0) | ||
| 59 | break; | ||
| 60 | if (out <= 0) | ||
| 61 | break; | ||
| 62 | |||
| 63 | if (control & 0x80) { | ||
| 64 | // Check if compression is out of bounds | ||
| 65 | if (index < 2) | ||
| 66 | return false; | ||
| 67 | index -= 2; | ||
| 68 | |||
| 69 | u32 segment_offset = compressed[index] | (compressed[index + 1] << 8); | ||
| 70 | u32 segment_size = ((segment_offset >> 12) & 15) + 3; | ||
| 71 | segment_offset &= 0x0FFF; | ||
| 72 | segment_offset += 2; | ||
| 73 | |||
| 74 | // Check if compression is out of bounds | ||
| 75 | if (out < segment_size) | ||
| 76 | return false; | ||
| 77 | |||
| 78 | for (unsigned j = 0; j < segment_size; j++) { | ||
| 79 | // Check if compression is out of bounds | ||
| 80 | if (out + segment_offset >= decompressed_size) | ||
| 81 | return false; | ||
| 82 | |||
| 83 | u8 data = decompressed[out + segment_offset]; | ||
| 84 | decompressed[--out] = data; | ||
| 85 | } | ||
| 86 | } else { | ||
| 87 | // Check if compression is out of bounds | ||
| 88 | if (out < 1) | ||
| 89 | return false; | ||
| 90 | decompressed[--out] = compressed[--index]; | ||
| 91 | } | ||
| 92 | control <<= 1; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | return true; | ||
| 96 | } | ||
| 97 | |||
| 98 | NCCHContainer::NCCHContainer(const std::string& filepath) : filepath(filepath) { | ||
| 99 | file = FileUtil::IOFile(filepath, "rb"); | ||
| 100 | } | ||
| 101 | |||
| 102 | Loader::ResultStatus NCCHContainer::OpenFile(const std::string& filepath) { | ||
| 103 | this->filepath = filepath; | ||
| 104 | file = FileUtil::IOFile(filepath, "rb"); | ||
| 105 | |||
| 106 | if (!file.IsOpen()) { | ||
| 107 | LOG_WARNING(Service_FS, "Failed to open %s", filepath.c_str()); | ||
| 108 | return Loader::ResultStatus::Error; | ||
| 109 | } | ||
| 110 | |||
| 111 | LOG_DEBUG(Service_FS, "Opened %s", filepath.c_str()); | ||
| 112 | return Loader::ResultStatus::Success; | ||
| 113 | } | ||
| 114 | |||
| 115 | Loader::ResultStatus NCCHContainer::Load() { | ||
| 116 | if (is_loaded) | ||
| 117 | return Loader::ResultStatus::Success; | ||
| 118 | |||
| 119 | // Reset read pointer in case this file has been read before. | ||
| 120 | file.Seek(0, SEEK_SET); | ||
| 121 | |||
| 122 | if (file.ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header)) | ||
| 123 | return Loader::ResultStatus::Error; | ||
| 124 | |||
| 125 | // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)... | ||
| 126 | if (Loader::MakeMagic('N', 'C', 'S', 'D') == ncch_header.magic) { | ||
| 127 | LOG_DEBUG(Service_FS, "Only loading the first (bootable) NCCH within the NCSD file!"); | ||
| 128 | ncch_offset = 0x4000; | ||
| 129 | file.Seek(ncch_offset, SEEK_SET); | ||
| 130 | file.ReadBytes(&ncch_header, sizeof(NCCH_Header)); | ||
| 131 | } | ||
| 132 | |||
| 133 | // Verify we are loading the correct file type... | ||
| 134 | if (Loader::MakeMagic('N', 'C', 'C', 'H') != ncch_header.magic) | ||
| 135 | return Loader::ResultStatus::ErrorInvalidFormat; | ||
| 136 | |||
| 137 | // System archives and DLC don't have an extended header but have RomFS | ||
| 138 | if (ncch_header.extended_header_size) { | ||
| 139 | if (file.ReadBytes(&exheader_header, sizeof(ExHeader_Header)) != sizeof(ExHeader_Header)) | ||
| 140 | return Loader::ResultStatus::Error; | ||
| 141 | |||
| 142 | is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1; | ||
| 143 | u32 entry_point = exheader_header.codeset_info.text.address; | ||
| 144 | u32 code_size = exheader_header.codeset_info.text.code_size; | ||
| 145 | u32 stack_size = exheader_header.codeset_info.stack_size; | ||
| 146 | u32 bss_size = exheader_header.codeset_info.bss_size; | ||
| 147 | u32 core_version = exheader_header.arm11_system_local_caps.core_version; | ||
| 148 | u8 priority = exheader_header.arm11_system_local_caps.priority; | ||
| 149 | u8 resource_limit_category = | ||
| 150 | exheader_header.arm11_system_local_caps.resource_limit_category; | ||
| 151 | |||
| 152 | LOG_DEBUG(Service_FS, "Name: %s", exheader_header.codeset_info.name); | ||
| 153 | LOG_DEBUG(Service_FS, "Program ID: %016" PRIX64, ncch_header.program_id); | ||
| 154 | LOG_DEBUG(Service_FS, "Code compressed: %s", is_compressed ? "yes" : "no"); | ||
| 155 | LOG_DEBUG(Service_FS, "Entry point: 0x%08X", entry_point); | ||
| 156 | LOG_DEBUG(Service_FS, "Code size: 0x%08X", code_size); | ||
| 157 | LOG_DEBUG(Service_FS, "Stack size: 0x%08X", stack_size); | ||
| 158 | LOG_DEBUG(Service_FS, "Bss size: 0x%08X", bss_size); | ||
| 159 | LOG_DEBUG(Service_FS, "Core version: %d", core_version); | ||
| 160 | LOG_DEBUG(Service_FS, "Thread priority: 0x%X", priority); | ||
| 161 | LOG_DEBUG(Service_FS, "Resource limit category: %d", resource_limit_category); | ||
| 162 | LOG_DEBUG(Service_FS, "System Mode: %d", | ||
| 163 | static_cast<int>(exheader_header.arm11_system_local_caps.system_mode)); | ||
| 164 | |||
| 165 | if (exheader_header.system_info.jump_id != ncch_header.program_id) { | ||
| 166 | LOG_ERROR(Service_FS, "ExHeader Program ID mismatch: the ROM is probably encrypted."); | ||
| 167 | return Loader::ResultStatus::ErrorEncrypted; | ||
| 168 | } | ||
| 169 | |||
| 170 | has_exheader = true; | ||
| 171 | } | ||
| 172 | |||
| 173 | // DLC can have an ExeFS and a RomFS but no extended header | ||
| 174 | if (ncch_header.exefs_size) { | ||
| 175 | exefs_offset = ncch_header.exefs_offset * kBlockSize; | ||
| 176 | u32 exefs_size = ncch_header.exefs_size * kBlockSize; | ||
| 177 | |||
| 178 | LOG_DEBUG(Service_FS, "ExeFS offset: 0x%08X", exefs_offset); | ||
| 179 | LOG_DEBUG(Service_FS, "ExeFS size: 0x%08X", exefs_size); | ||
| 180 | |||
| 181 | file.Seek(exefs_offset + ncch_offset, SEEK_SET); | ||
| 182 | if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header)) | ||
| 183 | return Loader::ResultStatus::Error; | ||
| 184 | |||
| 185 | has_exefs = true; | ||
| 186 | } | ||
| 187 | |||
| 188 | if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) | ||
| 189 | has_romfs = true; | ||
| 190 | |||
| 191 | is_loaded = true; | ||
| 192 | return Loader::ResultStatus::Success; | ||
| 193 | } | ||
| 194 | |||
| 195 | Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) { | ||
| 196 | if (!file.IsOpen()) | ||
| 197 | return Loader::ResultStatus::Error; | ||
| 198 | |||
| 199 | Loader::ResultStatus result = Load(); | ||
| 200 | if (result != Loader::ResultStatus::Success) | ||
| 201 | return result; | ||
| 202 | |||
| 203 | if (!has_exefs) | ||
| 204 | return Loader::ResultStatus::ErrorNotUsed; | ||
| 205 | |||
| 206 | LOG_DEBUG(Service_FS, "%d sections:", kMaxSections); | ||
| 207 | // Iterate through the ExeFs archive until we find a section with the specified name... | ||
| 208 | for (unsigned section_number = 0; section_number < kMaxSections; section_number++) { | ||
| 209 | const auto& section = exefs_header.section[section_number]; | ||
| 210 | |||
| 211 | // Load the specified section... | ||
| 212 | if (strcmp(section.name, name) == 0) { | ||
| 213 | LOG_DEBUG(Service_FS, "%d - offset: 0x%08X, size: 0x%08X, name: %s", section_number, | ||
| 214 | section.offset, section.size, section.name); | ||
| 215 | |||
| 216 | s64 section_offset = | ||
| 217 | (section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset); | ||
| 218 | file.Seek(section_offset, SEEK_SET); | ||
| 219 | |||
| 220 | if (strcmp(section.name, ".code") == 0 && is_compressed) { | ||
| 221 | // Section is compressed, read compressed .code section... | ||
| 222 | std::unique_ptr<u8[]> temp_buffer; | ||
| 223 | try { | ||
| 224 | temp_buffer.reset(new u8[section.size]); | ||
| 225 | } catch (std::bad_alloc&) { | ||
| 226 | return Loader::ResultStatus::ErrorMemoryAllocationFailed; | ||
| 227 | } | ||
| 228 | |||
| 229 | if (file.ReadBytes(&temp_buffer[0], section.size) != section.size) | ||
| 230 | return Loader::ResultStatus::Error; | ||
| 231 | |||
| 232 | // Decompress .code section... | ||
| 233 | u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], section.size); | ||
| 234 | buffer.resize(decompressed_size); | ||
| 235 | if (!LZSS_Decompress(&temp_buffer[0], section.size, &buffer[0], decompressed_size)) | ||
| 236 | return Loader::ResultStatus::ErrorInvalidFormat; | ||
| 237 | } else { | ||
| 238 | // Section is uncompressed... | ||
| 239 | buffer.resize(section.size); | ||
| 240 | if (file.ReadBytes(&buffer[0], section.size) != section.size) | ||
| 241 | return Loader::ResultStatus::Error; | ||
| 242 | } | ||
| 243 | return Loader::ResultStatus::Success; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | return Loader::ResultStatus::ErrorNotUsed; | ||
| 247 | } | ||
| 248 | |||
| 249 | Loader::ResultStatus NCCHContainer::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, | ||
| 250 | u64& offset, u64& size) { | ||
| 251 | if (!file.IsOpen()) | ||
| 252 | return Loader::ResultStatus::Error; | ||
| 253 | |||
| 254 | Loader::ResultStatus result = Load(); | ||
| 255 | if (result != Loader::ResultStatus::Success) | ||
| 256 | return result; | ||
| 257 | |||
| 258 | if (!has_romfs) { | ||
| 259 | LOG_DEBUG(Service_FS, "RomFS requested from NCCH which has no RomFS"); | ||
| 260 | return Loader::ResultStatus::ErrorNotUsed; | ||
| 261 | } | ||
| 262 | |||
| 263 | u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000; | ||
| 264 | u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000; | ||
| 265 | |||
| 266 | LOG_DEBUG(Service_FS, "RomFS offset: 0x%08X", romfs_offset); | ||
| 267 | LOG_DEBUG(Service_FS, "RomFS size: 0x%08X", romfs_size); | ||
| 268 | |||
| 269 | if (file.GetSize() < romfs_offset + romfs_size) | ||
| 270 | return Loader::ResultStatus::Error; | ||
| 271 | |||
| 272 | // We reopen the file, to allow its position to be independent from file's | ||
| 273 | romfs_file = std::make_shared<FileUtil::IOFile>(filepath, "rb"); | ||
| 274 | if (!romfs_file->IsOpen()) | ||
| 275 | return Loader::ResultStatus::Error; | ||
| 276 | |||
| 277 | offset = romfs_offset; | ||
| 278 | size = romfs_size; | ||
| 279 | |||
| 280 | return Loader::ResultStatus::Success; | ||
| 281 | } | ||
| 282 | |||
| 283 | Loader::ResultStatus NCCHContainer::ReadProgramId(u64_le& program_id) { | ||
| 284 | Loader::ResultStatus result = Load(); | ||
| 285 | if (result != Loader::ResultStatus::Success) | ||
| 286 | return result; | ||
| 287 | |||
| 288 | program_id = ncch_header.program_id; | ||
| 289 | return Loader::ResultStatus::Success; | ||
| 290 | } | ||
| 291 | |||
| 292 | bool NCCHContainer::HasExeFS() { | ||
| 293 | Loader::ResultStatus result = Load(); | ||
| 294 | if (result != Loader::ResultStatus::Success) | ||
| 295 | return false; | ||
| 296 | |||
| 297 | return has_exefs; | ||
| 298 | } | ||
| 299 | |||
| 300 | bool NCCHContainer::HasRomFS() { | ||
| 301 | Loader::ResultStatus result = Load(); | ||
| 302 | if (result != Loader::ResultStatus::Success) | ||
| 303 | return false; | ||
| 304 | |||
| 305 | return has_romfs; | ||
| 306 | } | ||
| 307 | |||
| 308 | bool NCCHContainer::HasExHeader() { | ||
| 309 | Loader::ResultStatus result = Load(); | ||
| 310 | if (result != Loader::ResultStatus::Success) | ||
| 311 | return false; | ||
| 312 | |||
| 313 | return has_exheader; | ||
| 314 | } | ||
| 315 | |||
| 316 | } // namespace FileSys | ||
diff --git a/src/core/file_sys/ncch_container.h b/src/core/file_sys/ncch_container.h new file mode 100644 index 000000000..8af9032b4 --- /dev/null +++ b/src/core/file_sys/ncch_container.h | |||
| @@ -0,0 +1,244 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <cstddef> | ||
| 8 | #include <memory> | ||
| 9 | #include <string> | ||
| 10 | #include <vector> | ||
| 11 | #include "common/bit_field.h" | ||
| 12 | #include "common/common_types.h" | ||
| 13 | #include "common/file_util.h" | ||
| 14 | #include "common/swap.h" | ||
| 15 | #include "core/core.h" | ||
| 16 | |||
| 17 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 18 | /// NCCH header (Note: "NCCH" appears to be a publicly unknown acronym) | ||
| 19 | |||
| 20 | struct NCCH_Header { | ||
| 21 | u8 signature[0x100]; | ||
| 22 | u32_le magic; | ||
| 23 | u32_le content_size; | ||
| 24 | u8 partition_id[8]; | ||
| 25 | u16_le maker_code; | ||
| 26 | u16_le version; | ||
| 27 | u8 reserved_0[4]; | ||
| 28 | u64_le program_id; | ||
| 29 | u8 reserved_1[0x10]; | ||
| 30 | u8 logo_region_hash[0x20]; | ||
| 31 | u8 product_code[0x10]; | ||
| 32 | u8 extended_header_hash[0x20]; | ||
| 33 | u32_le extended_header_size; | ||
| 34 | u8 reserved_2[4]; | ||
| 35 | u8 flags[8]; | ||
| 36 | u32_le plain_region_offset; | ||
| 37 | u32_le plain_region_size; | ||
| 38 | u32_le logo_region_offset; | ||
| 39 | u32_le logo_region_size; | ||
| 40 | u32_le exefs_offset; | ||
| 41 | u32_le exefs_size; | ||
| 42 | u32_le exefs_hash_region_size; | ||
| 43 | u8 reserved_3[4]; | ||
| 44 | u32_le romfs_offset; | ||
| 45 | u32_le romfs_size; | ||
| 46 | u32_le romfs_hash_region_size; | ||
| 47 | u8 reserved_4[4]; | ||
| 48 | u8 exefs_super_block_hash[0x20]; | ||
| 49 | u8 romfs_super_block_hash[0x20]; | ||
| 50 | }; | ||
| 51 | |||
| 52 | static_assert(sizeof(NCCH_Header) == 0x200, "NCCH header structure size is wrong"); | ||
| 53 | |||
| 54 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 55 | // ExeFS (executable file system) headers | ||
| 56 | |||
| 57 | struct ExeFs_SectionHeader { | ||
| 58 | char name[8]; | ||
| 59 | u32 offset; | ||
| 60 | u32 size; | ||
| 61 | }; | ||
| 62 | |||
| 63 | struct ExeFs_Header { | ||
| 64 | ExeFs_SectionHeader section[8]; | ||
| 65 | u8 reserved[0x80]; | ||
| 66 | u8 hashes[8][0x20]; | ||
| 67 | }; | ||
| 68 | |||
| 69 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 70 | // ExHeader (executable file system header) headers | ||
| 71 | |||
| 72 | struct ExHeader_SystemInfoFlags { | ||
| 73 | u8 reserved[5]; | ||
| 74 | u8 flag; | ||
| 75 | u8 remaster_version[2]; | ||
| 76 | }; | ||
| 77 | |||
| 78 | struct ExHeader_CodeSegmentInfo { | ||
| 79 | u32 address; | ||
| 80 | u32 num_max_pages; | ||
| 81 | u32 code_size; | ||
| 82 | }; | ||
| 83 | |||
| 84 | struct ExHeader_CodeSetInfo { | ||
| 85 | u8 name[8]; | ||
| 86 | ExHeader_SystemInfoFlags flags; | ||
| 87 | ExHeader_CodeSegmentInfo text; | ||
| 88 | u32 stack_size; | ||
| 89 | ExHeader_CodeSegmentInfo ro; | ||
| 90 | u8 reserved[4]; | ||
| 91 | ExHeader_CodeSegmentInfo data; | ||
| 92 | u32 bss_size; | ||
| 93 | }; | ||
| 94 | |||
| 95 | struct ExHeader_DependencyList { | ||
| 96 | u8 program_id[0x30][8]; | ||
| 97 | }; | ||
| 98 | |||
| 99 | struct ExHeader_SystemInfo { | ||
| 100 | u64 save_data_size; | ||
| 101 | u64_le jump_id; | ||
| 102 | u8 reserved_2[0x30]; | ||
| 103 | }; | ||
| 104 | |||
| 105 | struct ExHeader_StorageInfo { | ||
| 106 | u8 ext_save_data_id[8]; | ||
| 107 | u8 system_save_data_id[8]; | ||
| 108 | u8 reserved[8]; | ||
| 109 | u8 access_info[7]; | ||
| 110 | u8 other_attributes; | ||
| 111 | }; | ||
| 112 | |||
| 113 | struct ExHeader_ARM11_SystemLocalCaps { | ||
| 114 | u64_le program_id; | ||
| 115 | u32_le core_version; | ||
| 116 | u8 reserved_flags[2]; | ||
| 117 | union { | ||
| 118 | u8 flags0; | ||
| 119 | BitField<0, 2, u8> ideal_processor; | ||
| 120 | BitField<2, 2, u8> affinity_mask; | ||
| 121 | BitField<4, 4, u8> system_mode; | ||
| 122 | }; | ||
| 123 | u8 priority; | ||
| 124 | u8 resource_limit_descriptor[0x10][2]; | ||
| 125 | ExHeader_StorageInfo storage_info; | ||
| 126 | u8 service_access_control[0x20][8]; | ||
| 127 | u8 ex_service_access_control[0x2][8]; | ||
| 128 | u8 reserved[0xf]; | ||
| 129 | u8 resource_limit_category; | ||
| 130 | }; | ||
| 131 | |||
| 132 | struct ExHeader_ARM11_KernelCaps { | ||
| 133 | u32_le descriptors[28]; | ||
| 134 | u8 reserved[0x10]; | ||
| 135 | }; | ||
| 136 | |||
| 137 | struct ExHeader_ARM9_AccessControl { | ||
| 138 | u8 descriptors[15]; | ||
| 139 | u8 descversion; | ||
| 140 | }; | ||
| 141 | |||
| 142 | struct ExHeader_Header { | ||
| 143 | ExHeader_CodeSetInfo codeset_info; | ||
| 144 | ExHeader_DependencyList dependency_list; | ||
| 145 | ExHeader_SystemInfo system_info; | ||
| 146 | ExHeader_ARM11_SystemLocalCaps arm11_system_local_caps; | ||
| 147 | ExHeader_ARM11_KernelCaps arm11_kernel_caps; | ||
| 148 | ExHeader_ARM9_AccessControl arm9_access_control; | ||
| 149 | struct { | ||
| 150 | u8 signature[0x100]; | ||
| 151 | u8 ncch_public_key_modulus[0x100]; | ||
| 152 | ExHeader_ARM11_SystemLocalCaps arm11_system_local_caps; | ||
| 153 | ExHeader_ARM11_KernelCaps arm11_kernel_caps; | ||
| 154 | ExHeader_ARM9_AccessControl arm9_access_control; | ||
| 155 | } access_desc; | ||
| 156 | }; | ||
| 157 | |||
| 158 | static_assert(sizeof(ExHeader_Header) == 0x800, "ExHeader structure size is wrong"); | ||
| 159 | |||
| 160 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 161 | // FileSys namespace | ||
| 162 | |||
| 163 | namespace FileSys { | ||
| 164 | |||
| 165 | /** | ||
| 166 | * Helper which implements an interface to deal with NCCH containers which can | ||
| 167 | * contain ExeFS archives or RomFS archives for games or other applications. | ||
| 168 | */ | ||
| 169 | class NCCHContainer { | ||
| 170 | public: | ||
| 171 | NCCHContainer(const std::string& filepath); | ||
| 172 | NCCHContainer() {} | ||
| 173 | |||
| 174 | Loader::ResultStatus OpenFile(const std::string& filepath); | ||
| 175 | |||
| 176 | /** | ||
| 177 | * Ensure ExeFS and exheader is loaded and ready for reading sections | ||
| 178 | * @return ResultStatus result of function | ||
| 179 | */ | ||
| 180 | Loader::ResultStatus Load(); | ||
| 181 | |||
| 182 | /** | ||
| 183 | * Reads an application ExeFS section of an NCCH file (e.g. .code, .logo, etc.) | ||
| 184 | * @param name Name of section to read out of NCCH file | ||
| 185 | * @param buffer Vector to read data into | ||
| 186 | * @return ResultStatus result of function | ||
| 187 | */ | ||
| 188 | Loader::ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer); | ||
| 189 | |||
| 190 | /** | ||
| 191 | * Get the RomFS of the NCCH container | ||
| 192 | * Since the RomFS can be huge, we return a file reference instead of copying to a buffer | ||
| 193 | * @param romfs_file The file containing the RomFS | ||
| 194 | * @param offset The offset the romfs begins on | ||
| 195 | * @param size The size of the romfs | ||
| 196 | * @return ResultStatus result of function | ||
| 197 | */ | ||
| 198 | Loader::ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, | ||
| 199 | u64& size); | ||
| 200 | |||
| 201 | /** | ||
| 202 | * Get the Program ID of the NCCH container | ||
| 203 | * @return ResultStatus result of function | ||
| 204 | */ | ||
| 205 | Loader::ResultStatus ReadProgramId(u64_le& program_id); | ||
| 206 | |||
| 207 | /** | ||
| 208 | * Checks whether the NCCH container contains an ExeFS | ||
| 209 | * @return bool check result | ||
| 210 | */ | ||
| 211 | bool HasExeFS(); | ||
| 212 | |||
| 213 | /** | ||
| 214 | * Checks whether the NCCH container contains a RomFS | ||
| 215 | * @return bool check result | ||
| 216 | */ | ||
| 217 | bool HasRomFS(); | ||
| 218 | |||
| 219 | /** | ||
| 220 | * Checks whether the NCCH container contains an ExHeader | ||
| 221 | * @return bool check result | ||
| 222 | */ | ||
| 223 | bool HasExHeader(); | ||
| 224 | |||
| 225 | NCCH_Header ncch_header; | ||
| 226 | ExeFs_Header exefs_header; | ||
| 227 | ExHeader_Header exheader_header; | ||
| 228 | |||
| 229 | private: | ||
| 230 | bool has_exheader = false; | ||
| 231 | bool has_exefs = false; | ||
| 232 | bool has_romfs = false; | ||
| 233 | |||
| 234 | bool is_loaded = false; | ||
| 235 | bool is_compressed = false; | ||
| 236 | |||
| 237 | u32 ncch_offset = 0; // Offset to NCCH header, can be 0 or after NCSD header | ||
| 238 | u32 exefs_offset = 0; | ||
| 239 | |||
| 240 | std::string filepath; | ||
| 241 | FileUtil::IOFile file; | ||
| 242 | }; | ||
| 243 | |||
| 244 | } // namespace FileSys | ||