diff options
Diffstat (limited to 'src/core/loader/ncch.cpp')
| -rw-r--r-- | src/core/loader/ncch.cpp | 319 |
1 files changed, 60 insertions, 259 deletions
diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index 79ea50147..bef7fa567 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp | |||
| @@ -13,6 +13,7 @@ | |||
| 13 | #include "common/swap.h" | 13 | #include "common/swap.h" |
| 14 | #include "core/core.h" | 14 | #include "core/core.h" |
| 15 | #include "core/file_sys/archive_selfncch.h" | 15 | #include "core/file_sys/archive_selfncch.h" |
| 16 | #include "core/file_sys/ncch_container.h" | ||
| 16 | #include "core/hle/kernel/process.h" | 17 | #include "core/hle/kernel/process.h" |
| 17 | #include "core/hle/kernel/resource_limit.h" | 18 | #include "core/hle/kernel/resource_limit.h" |
| 18 | #include "core/hle/service/cfg/cfg.h" | 19 | #include "core/hle/service/cfg/cfg.h" |
| @@ -27,87 +28,7 @@ | |||
| 27 | 28 | ||
| 28 | namespace Loader { | 29 | namespace Loader { |
| 29 | 30 | ||
| 30 | static const int kMaxSections = 8; ///< Maximum number of sections (files) in an ExeFs | 31 | static const u64 UPDATE_MASK = 0x0000000e00000000; |
| 31 | static const int kBlockSize = 0x200; ///< Size of ExeFS blocks (in bytes) | ||
| 32 | |||
| 33 | /** | ||
| 34 | * Get the decompressed size of an LZSS compressed ExeFS file | ||
| 35 | * @param buffer Buffer of compressed file | ||
| 36 | * @param size Size of compressed buffer | ||
| 37 | * @return Size of decompressed buffer | ||
| 38 | */ | ||
| 39 | static u32 LZSS_GetDecompressedSize(const u8* buffer, u32 size) { | ||
| 40 | u32 offset_size = *(u32*)(buffer + size - 4); | ||
| 41 | return offset_size + size; | ||
| 42 | } | ||
| 43 | |||
| 44 | /** | ||
| 45 | * Decompress ExeFS file (compressed with LZSS) | ||
| 46 | * @param compressed Compressed buffer | ||
| 47 | * @param compressed_size Size of compressed buffer | ||
| 48 | * @param decompressed Decompressed buffer | ||
| 49 | * @param decompressed_size Size of decompressed buffer | ||
| 50 | * @return True on success, otherwise false | ||
| 51 | */ | ||
| 52 | static bool LZSS_Decompress(const u8* compressed, u32 compressed_size, u8* decompressed, | ||
| 53 | u32 decompressed_size) { | ||
| 54 | const u8* footer = compressed + compressed_size - 8; | ||
| 55 | u32 buffer_top_and_bottom = *reinterpret_cast<const u32*>(footer); | ||
| 56 | u32 out = decompressed_size; | ||
| 57 | u32 index = compressed_size - ((buffer_top_and_bottom >> 24) & 0xFF); | ||
| 58 | u32 stop_index = compressed_size - (buffer_top_and_bottom & 0xFFFFFF); | ||
| 59 | |||
| 60 | memset(decompressed, 0, decompressed_size); | ||
| 61 | memcpy(decompressed, compressed, compressed_size); | ||
| 62 | |||
| 63 | while (index > stop_index) { | ||
| 64 | u8 control = compressed[--index]; | ||
| 65 | |||
| 66 | for (unsigned i = 0; i < 8; i++) { | ||
| 67 | if (index <= stop_index) | ||
| 68 | break; | ||
| 69 | if (index <= 0) | ||
| 70 | break; | ||
| 71 | if (out <= 0) | ||
| 72 | break; | ||
| 73 | |||
| 74 | if (control & 0x80) { | ||
| 75 | // Check if compression is out of bounds | ||
| 76 | if (index < 2) | ||
| 77 | return false; | ||
| 78 | index -= 2; | ||
| 79 | |||
| 80 | u32 segment_offset = compressed[index] | (compressed[index + 1] << 8); | ||
| 81 | u32 segment_size = ((segment_offset >> 12) & 15) + 3; | ||
| 82 | segment_offset &= 0x0FFF; | ||
| 83 | segment_offset += 2; | ||
| 84 | |||
| 85 | // Check if compression is out of bounds | ||
| 86 | if (out < segment_size) | ||
| 87 | return false; | ||
| 88 | |||
| 89 | for (unsigned j = 0; j < segment_size; j++) { | ||
| 90 | // Check if compression is out of bounds | ||
| 91 | if (out + segment_offset >= decompressed_size) | ||
| 92 | return false; | ||
| 93 | |||
| 94 | u8 data = decompressed[out + segment_offset]; | ||
| 95 | decompressed[--out] = data; | ||
| 96 | } | ||
| 97 | } else { | ||
| 98 | // Check if compression is out of bounds | ||
| 99 | if (out < 1) | ||
| 100 | return false; | ||
| 101 | decompressed[--out] = compressed[--index]; | ||
| 102 | } | ||
| 103 | control <<= 1; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | return true; | ||
| 107 | } | ||
| 108 | |||
| 109 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 110 | // AppLoader_NCCH class | ||
| 111 | 32 | ||
| 112 | FileType AppLoader_NCCH::IdentifyType(FileUtil::IOFile& file) { | 33 | FileType AppLoader_NCCH::IdentifyType(FileUtil::IOFile& file) { |
| 113 | u32 magic; | 34 | u32 magic; |
| @@ -124,15 +45,25 @@ FileType AppLoader_NCCH::IdentifyType(FileUtil::IOFile& file) { | |||
| 124 | return FileType::Error; | 45 | return FileType::Error; |
| 125 | } | 46 | } |
| 126 | 47 | ||
| 48 | static std::string GetUpdateNCCHPath(u64_le program_id) { | ||
| 49 | u32 high = static_cast<u32>((program_id | UPDATE_MASK) >> 32); | ||
| 50 | u32 low = static_cast<u32>((program_id | UPDATE_MASK) & 0xFFFFFFFF); | ||
| 51 | |||
| 52 | return Common::StringFromFormat("%sNintendo 3DS/%s/%s/title/%08x/%08x/content/00000000.app", | ||
| 53 | FileUtil::GetUserPath(D_SDMC_IDX).c_str(), SYSTEM_ID, SDCARD_ID, | ||
| 54 | high, low); | ||
| 55 | } | ||
| 56 | |||
| 127 | std::pair<boost::optional<u32>, ResultStatus> AppLoader_NCCH::LoadKernelSystemMode() { | 57 | std::pair<boost::optional<u32>, ResultStatus> AppLoader_NCCH::LoadKernelSystemMode() { |
| 128 | if (!is_loaded) { | 58 | if (!is_loaded) { |
| 129 | ResultStatus res = LoadExeFS(); | 59 | ResultStatus res = base_ncch.Load(); |
| 130 | if (res != ResultStatus::Success) { | 60 | if (res != ResultStatus::Success) { |
| 131 | return std::make_pair(boost::none, res); | 61 | return std::make_pair(boost::none, res); |
| 132 | } | 62 | } |
| 133 | } | 63 | } |
| 64 | |||
| 134 | // Set the system mode as the one from the exheader. | 65 | // Set the system mode as the one from the exheader. |
| 135 | return std::make_pair(exheader_header.arm11_system_local_caps.system_mode.Value(), | 66 | return std::make_pair(overlay_ncch->exheader_header.arm11_system_local_caps.system_mode.Value(), |
| 136 | ResultStatus::Success); | 67 | ResultStatus::Success); |
| 137 | } | 68 | } |
| 138 | 69 | ||
| @@ -144,29 +75,34 @@ ResultStatus AppLoader_NCCH::LoadExec() { | |||
| 144 | return ResultStatus::ErrorNotLoaded; | 75 | return ResultStatus::ErrorNotLoaded; |
| 145 | 76 | ||
| 146 | std::vector<u8> code; | 77 | std::vector<u8> code; |
| 147 | if (ResultStatus::Success == ReadCode(code)) { | 78 | u64_le program_id; |
| 79 | if (ResultStatus::Success == ReadCode(code) && | ||
| 80 | ResultStatus::Success == ReadProgramId(program_id)) { | ||
| 148 | std::string process_name = Common::StringFromFixedZeroTerminatedBuffer( | 81 | std::string process_name = Common::StringFromFixedZeroTerminatedBuffer( |
| 149 | (const char*)exheader_header.codeset_info.name, 8); | 82 | (const char*)overlay_ncch->exheader_header.codeset_info.name, 8); |
| 150 | 83 | ||
| 151 | SharedPtr<CodeSet> codeset = CodeSet::Create(process_name, ncch_header.program_id); | 84 | SharedPtr<CodeSet> codeset = CodeSet::Create(process_name, program_id); |
| 152 | 85 | ||
| 153 | codeset->code.offset = 0; | 86 | codeset->code.offset = 0; |
| 154 | codeset->code.addr = exheader_header.codeset_info.text.address; | 87 | codeset->code.addr = overlay_ncch->exheader_header.codeset_info.text.address; |
| 155 | codeset->code.size = exheader_header.codeset_info.text.num_max_pages * Memory::PAGE_SIZE; | 88 | codeset->code.size = |
| 89 | overlay_ncch->exheader_header.codeset_info.text.num_max_pages * Memory::PAGE_SIZE; | ||
| 156 | 90 | ||
| 157 | codeset->rodata.offset = codeset->code.offset + codeset->code.size; | 91 | codeset->rodata.offset = codeset->code.offset + codeset->code.size; |
| 158 | codeset->rodata.addr = exheader_header.codeset_info.ro.address; | 92 | codeset->rodata.addr = overlay_ncch->exheader_header.codeset_info.ro.address; |
| 159 | codeset->rodata.size = exheader_header.codeset_info.ro.num_max_pages * Memory::PAGE_SIZE; | 93 | codeset->rodata.size = |
| 94 | overlay_ncch->exheader_header.codeset_info.ro.num_max_pages * Memory::PAGE_SIZE; | ||
| 160 | 95 | ||
| 161 | // TODO(yuriks): Not sure if the bss size is added to the page-aligned .data size or just | 96 | // TODO(yuriks): Not sure if the bss size is added to the page-aligned .data size or just |
| 162 | // to the regular size. Playing it safe for now. | 97 | // to the regular size. Playing it safe for now. |
| 163 | u32 bss_page_size = (exheader_header.codeset_info.bss_size + 0xFFF) & ~0xFFF; | 98 | u32 bss_page_size = (overlay_ncch->exheader_header.codeset_info.bss_size + 0xFFF) & ~0xFFF; |
| 164 | code.resize(code.size() + bss_page_size, 0); | 99 | code.resize(code.size() + bss_page_size, 0); |
| 165 | 100 | ||
| 166 | codeset->data.offset = codeset->rodata.offset + codeset->rodata.size; | 101 | codeset->data.offset = codeset->rodata.offset + codeset->rodata.size; |
| 167 | codeset->data.addr = exheader_header.codeset_info.data.address; | 102 | codeset->data.addr = overlay_ncch->exheader_header.codeset_info.data.address; |
| 168 | codeset->data.size = | 103 | codeset->data.size = |
| 169 | exheader_header.codeset_info.data.num_max_pages * Memory::PAGE_SIZE + bss_page_size; | 104 | overlay_ncch->exheader_header.codeset_info.data.num_max_pages * Memory::PAGE_SIZE + |
| 105 | bss_page_size; | ||
| 170 | 106 | ||
| 171 | codeset->entrypoint = codeset->code.addr; | 107 | codeset->entrypoint = codeset->code.addr; |
| 172 | codeset->memory = std::make_shared<std::vector<u8>>(std::move(code)); | 108 | codeset->memory = std::make_shared<std::vector<u8>>(std::move(code)); |
| @@ -177,150 +113,27 @@ ResultStatus AppLoader_NCCH::LoadExec() { | |||
| 177 | // Attach a resource limit to the process based on the resource limit category | 113 | // Attach a resource limit to the process based on the resource limit category |
| 178 | Kernel::g_current_process->resource_limit = | 114 | Kernel::g_current_process->resource_limit = |
| 179 | Kernel::ResourceLimit::GetForCategory(static_cast<Kernel::ResourceLimitCategory>( | 115 | Kernel::ResourceLimit::GetForCategory(static_cast<Kernel::ResourceLimitCategory>( |
| 180 | exheader_header.arm11_system_local_caps.resource_limit_category)); | 116 | overlay_ncch->exheader_header.arm11_system_local_caps.resource_limit_category)); |
| 181 | 117 | ||
| 182 | // Set the default CPU core for this process | 118 | // Set the default CPU core for this process |
| 183 | Kernel::g_current_process->ideal_processor = | 119 | Kernel::g_current_process->ideal_processor = |
| 184 | exheader_header.arm11_system_local_caps.ideal_processor; | 120 | overlay_ncch->exheader_header.arm11_system_local_caps.ideal_processor; |
| 185 | 121 | ||
| 186 | // Copy data while converting endianness | 122 | // Copy data while converting endianness |
| 187 | std::array<u32, ARRAY_SIZE(exheader_header.arm11_kernel_caps.descriptors)> kernel_caps; | 123 | std::array<u32, ARRAY_SIZE(overlay_ncch->exheader_header.arm11_kernel_caps.descriptors)> |
| 188 | std::copy_n(exheader_header.arm11_kernel_caps.descriptors, kernel_caps.size(), | 124 | kernel_caps; |
| 125 | std::copy_n(overlay_ncch->exheader_header.arm11_kernel_caps.descriptors, kernel_caps.size(), | ||
| 189 | begin(kernel_caps)); | 126 | begin(kernel_caps)); |
| 190 | Kernel::g_current_process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size()); | 127 | Kernel::g_current_process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size()); |
| 191 | 128 | ||
| 192 | s32 priority = exheader_header.arm11_system_local_caps.priority; | 129 | s32 priority = overlay_ncch->exheader_header.arm11_system_local_caps.priority; |
| 193 | u32 stack_size = exheader_header.codeset_info.stack_size; | 130 | u32 stack_size = overlay_ncch->exheader_header.codeset_info.stack_size; |
| 194 | Kernel::g_current_process->Run(priority, stack_size); | 131 | Kernel::g_current_process->Run(priority, stack_size); |
| 195 | return ResultStatus::Success; | 132 | return ResultStatus::Success; |
| 196 | } | 133 | } |
| 197 | return ResultStatus::Error; | 134 | return ResultStatus::Error; |
| 198 | } | 135 | } |
| 199 | 136 | ||
| 200 | ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) { | ||
| 201 | if (!file.IsOpen()) | ||
| 202 | return ResultStatus::Error; | ||
| 203 | |||
| 204 | ResultStatus result = LoadExeFS(); | ||
| 205 | if (result != ResultStatus::Success) | ||
| 206 | return result; | ||
| 207 | |||
| 208 | LOG_DEBUG(Loader, "%d sections:", kMaxSections); | ||
| 209 | // Iterate through the ExeFs archive until we find a section with the specified name... | ||
| 210 | for (unsigned section_number = 0; section_number < kMaxSections; section_number++) { | ||
| 211 | const auto& section = exefs_header.section[section_number]; | ||
| 212 | |||
| 213 | // Load the specified section... | ||
| 214 | if (strcmp(section.name, name) == 0) { | ||
| 215 | LOG_DEBUG(Loader, "%d - offset: 0x%08X, size: 0x%08X, name: %s", section_number, | ||
| 216 | section.offset, section.size, section.name); | ||
| 217 | |||
| 218 | s64 section_offset = | ||
| 219 | (section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset); | ||
| 220 | file.Seek(section_offset, SEEK_SET); | ||
| 221 | |||
| 222 | if (strcmp(section.name, ".code") == 0 && is_compressed) { | ||
| 223 | // Section is compressed, read compressed .code section... | ||
| 224 | std::unique_ptr<u8[]> temp_buffer; | ||
| 225 | try { | ||
| 226 | temp_buffer.reset(new u8[section.size]); | ||
| 227 | } catch (std::bad_alloc&) { | ||
| 228 | return ResultStatus::ErrorMemoryAllocationFailed; | ||
| 229 | } | ||
| 230 | |||
| 231 | if (file.ReadBytes(&temp_buffer[0], section.size) != section.size) | ||
| 232 | return ResultStatus::Error; | ||
| 233 | |||
| 234 | // Decompress .code section... | ||
| 235 | u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], section.size); | ||
| 236 | buffer.resize(decompressed_size); | ||
| 237 | if (!LZSS_Decompress(&temp_buffer[0], section.size, &buffer[0], decompressed_size)) | ||
| 238 | return ResultStatus::ErrorInvalidFormat; | ||
| 239 | } else { | ||
| 240 | // Section is uncompressed... | ||
| 241 | buffer.resize(section.size); | ||
| 242 | if (file.ReadBytes(&buffer[0], section.size) != section.size) | ||
| 243 | return ResultStatus::Error; | ||
| 244 | } | ||
| 245 | return ResultStatus::Success; | ||
| 246 | } | ||
| 247 | } | ||
| 248 | return ResultStatus::ErrorNotUsed; | ||
| 249 | } | ||
| 250 | |||
| 251 | ResultStatus AppLoader_NCCH::LoadExeFS() { | ||
| 252 | if (is_exefs_loaded) | ||
| 253 | return ResultStatus::Success; | ||
| 254 | |||
| 255 | if (!file.IsOpen()) | ||
| 256 | return ResultStatus::Error; | ||
| 257 | |||
| 258 | // Reset read pointer in case this file has been read before. | ||
| 259 | file.Seek(0, SEEK_SET); | ||
| 260 | |||
| 261 | if (file.ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header)) | ||
| 262 | return ResultStatus::Error; | ||
| 263 | |||
| 264 | // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)... | ||
| 265 | if (MakeMagic('N', 'C', 'S', 'D') == ncch_header.magic) { | ||
| 266 | LOG_DEBUG(Loader, "Only loading the first (bootable) NCCH within the NCSD file!"); | ||
| 267 | ncch_offset = 0x4000; | ||
| 268 | file.Seek(ncch_offset, SEEK_SET); | ||
| 269 | file.ReadBytes(&ncch_header, sizeof(NCCH_Header)); | ||
| 270 | } | ||
| 271 | |||
| 272 | // Verify we are loading the correct file type... | ||
| 273 | if (MakeMagic('N', 'C', 'C', 'H') != ncch_header.magic) | ||
| 274 | return ResultStatus::ErrorInvalidFormat; | ||
| 275 | |||
| 276 | // Read ExHeader... | ||
| 277 | |||
| 278 | if (file.ReadBytes(&exheader_header, sizeof(ExHeader_Header)) != sizeof(ExHeader_Header)) | ||
| 279 | return ResultStatus::Error; | ||
| 280 | |||
| 281 | is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1; | ||
| 282 | entry_point = exheader_header.codeset_info.text.address; | ||
| 283 | code_size = exheader_header.codeset_info.text.code_size; | ||
| 284 | stack_size = exheader_header.codeset_info.stack_size; | ||
| 285 | bss_size = exheader_header.codeset_info.bss_size; | ||
| 286 | core_version = exheader_header.arm11_system_local_caps.core_version; | ||
| 287 | priority = exheader_header.arm11_system_local_caps.priority; | ||
| 288 | resource_limit_category = exheader_header.arm11_system_local_caps.resource_limit_category; | ||
| 289 | |||
| 290 | LOG_DEBUG(Loader, "Name: %s", exheader_header.codeset_info.name); | ||
| 291 | LOG_DEBUG(Loader, "Program ID: %016" PRIX64, ncch_header.program_id); | ||
| 292 | LOG_DEBUG(Loader, "Code compressed: %s", is_compressed ? "yes" : "no"); | ||
| 293 | LOG_DEBUG(Loader, "Entry point: 0x%08X", entry_point); | ||
| 294 | LOG_DEBUG(Loader, "Code size: 0x%08X", code_size); | ||
| 295 | LOG_DEBUG(Loader, "Stack size: 0x%08X", stack_size); | ||
| 296 | LOG_DEBUG(Loader, "Bss size: 0x%08X", bss_size); | ||
| 297 | LOG_DEBUG(Loader, "Core version: %d", core_version); | ||
| 298 | LOG_DEBUG(Loader, "Thread priority: 0x%X", priority); | ||
| 299 | LOG_DEBUG(Loader, "Resource limit category: %d", resource_limit_category); | ||
| 300 | LOG_DEBUG(Loader, "System Mode: %d", | ||
| 301 | static_cast<int>(exheader_header.arm11_system_local_caps.system_mode)); | ||
| 302 | |||
| 303 | if (exheader_header.arm11_system_local_caps.program_id != ncch_header.program_id) { | ||
| 304 | LOG_ERROR(Loader, "ExHeader Program ID mismatch: the ROM is probably encrypted."); | ||
| 305 | return ResultStatus::ErrorEncrypted; | ||
| 306 | } | ||
| 307 | |||
| 308 | // Read ExeFS... | ||
| 309 | |||
| 310 | exefs_offset = ncch_header.exefs_offset * kBlockSize; | ||
| 311 | u32 exefs_size = ncch_header.exefs_size * kBlockSize; | ||
| 312 | |||
| 313 | LOG_DEBUG(Loader, "ExeFS offset: 0x%08X", exefs_offset); | ||
| 314 | LOG_DEBUG(Loader, "ExeFS size: 0x%08X", exefs_size); | ||
| 315 | |||
| 316 | file.Seek(exefs_offset + ncch_offset, SEEK_SET); | ||
| 317 | if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header)) | ||
| 318 | return ResultStatus::Error; | ||
| 319 | |||
| 320 | is_exefs_loaded = true; | ||
| 321 | return ResultStatus::Success; | ||
| 322 | } | ||
| 323 | |||
| 324 | void AppLoader_NCCH::ParseRegionLockoutInfo() { | 137 | void AppLoader_NCCH::ParseRegionLockoutInfo() { |
| 325 | std::vector<u8> smdh_buffer; | 138 | std::vector<u8> smdh_buffer; |
| 326 | if (ReadIcon(smdh_buffer) == ResultStatus::Success && smdh_buffer.size() >= sizeof(SMDH)) { | 139 | if (ReadIcon(smdh_buffer) == ResultStatus::Success && smdh_buffer.size() >= sizeof(SMDH)) { |
| @@ -339,23 +152,32 @@ void AppLoader_NCCH::ParseRegionLockoutInfo() { | |||
| 339 | } | 152 | } |
| 340 | 153 | ||
| 341 | ResultStatus AppLoader_NCCH::Load() { | 154 | ResultStatus AppLoader_NCCH::Load() { |
| 155 | u64_le ncch_program_id; | ||
| 156 | |||
| 342 | if (is_loaded) | 157 | if (is_loaded) |
| 343 | return ResultStatus::ErrorAlreadyLoaded; | 158 | return ResultStatus::ErrorAlreadyLoaded; |
| 344 | 159 | ||
| 345 | ResultStatus result = LoadExeFS(); | 160 | ResultStatus result = base_ncch.Load(); |
| 346 | if (result != ResultStatus::Success) | 161 | if (result != ResultStatus::Success) |
| 347 | return result; | 162 | return result; |
| 348 | 163 | ||
| 349 | std::string program_id{Common::StringFromFormat("%016" PRIX64, ncch_header.program_id)}; | 164 | ReadProgramId(ncch_program_id); |
| 165 | std::string program_id{Common::StringFromFormat("%016" PRIX64, ncch_program_id)}; | ||
| 350 | 166 | ||
| 351 | LOG_INFO(Loader, "Program ID: %s", program_id.c_str()); | 167 | LOG_INFO(Loader, "Program ID: %s", program_id.c_str()); |
| 352 | 168 | ||
| 169 | update_ncch.OpenFile(GetUpdateNCCHPath(ncch_program_id)); | ||
| 170 | result = update_ncch.Load(); | ||
| 171 | if (result == ResultStatus::Success) { | ||
| 172 | overlay_ncch = &update_ncch; | ||
| 173 | } | ||
| 174 | |||
| 353 | Core::Telemetry().AddField(Telemetry::FieldType::Session, "ProgramId", program_id); | 175 | Core::Telemetry().AddField(Telemetry::FieldType::Session, "ProgramId", program_id); |
| 354 | 176 | ||
| 355 | if (auto room_member = Network::GetRoomMember().lock()) { | 177 | if (auto room_member = Network::GetRoomMember().lock()) { |
| 356 | Network::GameInfo game_info; | 178 | Network::GameInfo game_info; |
| 357 | ReadTitle(game_info.name); | 179 | ReadTitle(game_info.name); |
| 358 | game_info.id = ncch_header.program_id; | 180 | game_info.id = ncch_program_id; |
| 359 | room_member->SendGameInfo(game_info); | 181 | room_member->SendGameInfo(game_info); |
| 360 | } | 182 | } |
| 361 | 183 | ||
| @@ -374,61 +196,40 @@ ResultStatus AppLoader_NCCH::Load() { | |||
| 374 | } | 196 | } |
| 375 | 197 | ||
| 376 | ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) { | 198 | ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) { |
| 377 | return LoadSectionExeFS(".code", buffer); | 199 | return overlay_ncch->LoadSectionExeFS(".code", buffer); |
| 378 | } | 200 | } |
| 379 | 201 | ||
| 380 | ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) { | 202 | ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) { |
| 381 | return LoadSectionExeFS("icon", buffer); | 203 | return overlay_ncch->LoadSectionExeFS("icon", buffer); |
| 382 | } | 204 | } |
| 383 | 205 | ||
| 384 | ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) { | 206 | ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) { |
| 385 | return LoadSectionExeFS("banner", buffer); | 207 | return overlay_ncch->LoadSectionExeFS("banner", buffer); |
| 386 | } | 208 | } |
| 387 | 209 | ||
| 388 | ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) { | 210 | ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) { |
| 389 | return LoadSectionExeFS("logo", buffer); | 211 | return overlay_ncch->LoadSectionExeFS("logo", buffer); |
| 390 | } | 212 | } |
| 391 | 213 | ||
| 392 | ResultStatus AppLoader_NCCH::ReadProgramId(u64& out_program_id) { | 214 | ResultStatus AppLoader_NCCH::ReadProgramId(u64& out_program_id) { |
| 393 | if (!file.IsOpen()) | 215 | ResultStatus result = base_ncch.ReadProgramId(out_program_id); |
| 394 | return ResultStatus::Error; | ||
| 395 | |||
| 396 | ResultStatus result = LoadExeFS(); | ||
| 397 | if (result != ResultStatus::Success) | 216 | if (result != ResultStatus::Success) |
| 398 | return result; | 217 | return result; |
| 399 | 218 | ||
| 400 | out_program_id = ncch_header.program_id; | ||
| 401 | return ResultStatus::Success; | 219 | return ResultStatus::Success; |
| 402 | } | 220 | } |
| 403 | 221 | ||
| 404 | ResultStatus AppLoader_NCCH::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, | 222 | ResultStatus AppLoader_NCCH::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, |
| 405 | u64& size) { | 223 | u64& size) { |
| 406 | if (!file.IsOpen()) | 224 | return base_ncch.ReadRomFS(romfs_file, offset, size); |
| 407 | return ResultStatus::Error; | 225 | } |
| 408 | |||
| 409 | // Check if the NCCH has a RomFS... | ||
| 410 | if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) { | ||
| 411 | u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000; | ||
| 412 | u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000; | ||
| 413 | |||
| 414 | LOG_DEBUG(Loader, "RomFS offset: 0x%08X", romfs_offset); | ||
| 415 | LOG_DEBUG(Loader, "RomFS size: 0x%08X", romfs_size); | ||
| 416 | |||
| 417 | if (file.GetSize() < romfs_offset + romfs_size) | ||
| 418 | return ResultStatus::Error; | ||
| 419 | |||
| 420 | // We reopen the file, to allow its position to be independent from file's | ||
| 421 | romfs_file = std::make_shared<FileUtil::IOFile>(filepath, "rb"); | ||
| 422 | if (!romfs_file->IsOpen()) | ||
| 423 | return ResultStatus::Error; | ||
| 424 | 226 | ||
| 425 | offset = romfs_offset; | 227 | ResultStatus AppLoader_NCCH::ReadUpdateRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, |
| 426 | size = romfs_size; | 228 | u64& offset, u64& size) { |
| 229 | ResultStatus result = update_ncch.ReadRomFS(romfs_file, offset, size); | ||
| 427 | 230 | ||
| 428 | return ResultStatus::Success; | 231 | if (result != ResultStatus::Success) |
| 429 | } | 232 | return base_ncch.ReadRomFS(romfs_file, offset, size); |
| 430 | LOG_DEBUG(Loader, "NCCH has no RomFS"); | ||
| 431 | return ResultStatus::ErrorNotUsed; | ||
| 432 | } | 233 | } |
| 433 | 234 | ||
| 434 | ResultStatus AppLoader_NCCH::ReadTitle(std::string& title) { | 235 | ResultStatus AppLoader_NCCH::ReadTitle(std::string& title) { |