diff options
| author | 2017-10-12 21:21:49 -0400 | |
|---|---|---|
| committer | 2017-10-12 21:21:49 -0400 | |
| commit | 72b03025ac4ef0d8633c2f3e55b513cd149c59e5 (patch) | |
| tree | f1fbeb915a0b3df8e4e988a6a562a763e18ea666 /src/core/loader | |
| parent | hle: Remove a large amount of 3ds-specific service code. (diff) | |
| download | yuzu-72b03025ac4ef0d8633c2f3e55b513cd149c59e5.tar.gz yuzu-72b03025ac4ef0d8633c2f3e55b513cd149c59e5.tar.xz yuzu-72b03025ac4ef0d8633c2f3e55b513cd149c59e5.zip | |
Remove lots more 3DS-specific code.
Diffstat (limited to 'src/core/loader')
| -rw-r--r-- | src/core/loader/3dsx.cpp | 350 | ||||
| -rw-r--r-- | src/core/loader/3dsx.h | 46 | ||||
| -rw-r--r-- | src/core/loader/loader.cpp | 31 | ||||
| -rw-r--r-- | src/core/loader/loader.h | 4 | ||||
| -rw-r--r-- | src/core/loader/ncch.cpp | 263 | ||||
| -rw-r--r-- | src/core/loader/ncch.h | 80 |
6 files changed, 0 insertions, 774 deletions
diff --git a/src/core/loader/3dsx.cpp b/src/core/loader/3dsx.cpp deleted file mode 100644 index 7b0342cc9..000000000 --- a/src/core/loader/3dsx.cpp +++ /dev/null | |||
| @@ -1,350 +0,0 @@ | |||
| 1 | // Copyright 2014 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <vector> | ||
| 7 | #include "common/logging/log.h" | ||
| 8 | #include "core/file_sys/archive_selfncch.h" | ||
| 9 | #include "core/hle/kernel/process.h" | ||
| 10 | #include "core/hle/kernel/resource_limit.h" | ||
| 11 | #include "core/hle/service/fs/archive.h" | ||
| 12 | #include "core/loader/3dsx.h" | ||
| 13 | #include "core/memory.h" | ||
| 14 | |||
| 15 | namespace Loader { | ||
| 16 | |||
| 17 | /* | ||
| 18 | * File layout: | ||
| 19 | * - File header | ||
| 20 | * - Code, rodata and data relocation table headers | ||
| 21 | * - Code segment | ||
| 22 | * - Rodata segment | ||
| 23 | * - Loadable (non-BSS) part of the data segment | ||
| 24 | * - Code relocation table | ||
| 25 | * - Rodata relocation table | ||
| 26 | * - Data relocation table | ||
| 27 | * | ||
| 28 | * Memory layout before relocations are applied: | ||
| 29 | * [0..codeSegSize) -> code segment | ||
| 30 | * [codeSegSize..rodataSegSize) -> rodata segment | ||
| 31 | * [rodataSegSize..dataSegSize) -> data segment | ||
| 32 | * | ||
| 33 | * Memory layout after relocations are applied: well, however the loader sets it up :) | ||
| 34 | * The entrypoint is always the start of the code segment. | ||
| 35 | * The BSS section must be cleared manually by the application. | ||
| 36 | */ | ||
| 37 | |||
| 38 | enum THREEDSX_Error { ERROR_NONE = 0, ERROR_READ = 1, ERROR_FILE = 2, ERROR_ALLOC = 3 }; | ||
| 39 | |||
| 40 | static const u32 RELOCBUFSIZE = 512; | ||
| 41 | static const unsigned int NUM_SEGMENTS = 3; | ||
| 42 | |||
| 43 | // File header | ||
| 44 | #pragma pack(1) | ||
| 45 | struct THREEDSX_Header { | ||
| 46 | u32 magic; | ||
| 47 | u16 header_size, reloc_hdr_size; | ||
| 48 | u32 format_ver; | ||
| 49 | u32 flags; | ||
| 50 | |||
| 51 | // Sizes of the code, rodata and data segments + | ||
| 52 | // size of the BSS section (uninitialized latter half of the data segment) | ||
| 53 | u32 code_seg_size, rodata_seg_size, data_seg_size, bss_size; | ||
| 54 | // offset and size of smdh | ||
| 55 | u32 smdh_offset, smdh_size; | ||
| 56 | // offset to filesystem | ||
| 57 | u32 fs_offset; | ||
| 58 | }; | ||
| 59 | |||
| 60 | // Relocation header: all fields (even extra unknown fields) are guaranteed to be relocation counts. | ||
| 61 | struct THREEDSX_RelocHdr { | ||
| 62 | // # of absolute relocations (that is, fix address to post-relocation memory layout) | ||
| 63 | u32 cross_segment_absolute; | ||
| 64 | // # of cross-segment relative relocations (that is, 32bit signed offsets that need to be | ||
| 65 | // patched) | ||
| 66 | u32 cross_segment_relative; | ||
| 67 | // more? | ||
| 68 | |||
| 69 | // Relocations are written in this order: | ||
| 70 | // - Absolute relocations | ||
| 71 | // - Relative relocations | ||
| 72 | }; | ||
| 73 | |||
| 74 | // Relocation entry: from the current pointer, skip X words and patch Y words | ||
| 75 | struct THREEDSX_Reloc { | ||
| 76 | u16 skip, patch; | ||
| 77 | }; | ||
| 78 | #pragma pack() | ||
| 79 | |||
| 80 | struct THREEloadinfo { | ||
| 81 | u8* seg_ptrs[3]; // code, rodata & data | ||
| 82 | u32 seg_addrs[3]; | ||
| 83 | u32 seg_sizes[3]; | ||
| 84 | }; | ||
| 85 | |||
| 86 | static u32 TranslateAddr(u32 addr, const THREEloadinfo* loadinfo, u32* offsets) { | ||
| 87 | if (addr < offsets[0]) | ||
| 88 | return loadinfo->seg_addrs[0] + addr; | ||
| 89 | if (addr < offsets[1]) | ||
| 90 | return loadinfo->seg_addrs[1] + addr - offsets[0]; | ||
| 91 | return loadinfo->seg_addrs[2] + addr - offsets[1]; | ||
| 92 | } | ||
| 93 | |||
| 94 | using Kernel::CodeSet; | ||
| 95 | using Kernel::SharedPtr; | ||
| 96 | |||
| 97 | static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr, | ||
| 98 | SharedPtr<CodeSet>* out_codeset) { | ||
| 99 | if (!file.IsOpen()) | ||
| 100 | return ERROR_FILE; | ||
| 101 | |||
| 102 | // Reset read pointer in case this file has been read before. | ||
| 103 | file.Seek(0, SEEK_SET); | ||
| 104 | |||
| 105 | THREEDSX_Header hdr; | ||
| 106 | if (file.ReadBytes(&hdr, sizeof(hdr)) != sizeof(hdr)) | ||
| 107 | return ERROR_READ; | ||
| 108 | |||
| 109 | THREEloadinfo loadinfo; | ||
| 110 | // loadinfo segments must be a multiple of 0x1000 | ||
| 111 | loadinfo.seg_sizes[0] = (hdr.code_seg_size + 0xFFF) & ~0xFFF; | ||
| 112 | loadinfo.seg_sizes[1] = (hdr.rodata_seg_size + 0xFFF) & ~0xFFF; | ||
| 113 | loadinfo.seg_sizes[2] = (hdr.data_seg_size + 0xFFF) & ~0xFFF; | ||
| 114 | u32 offsets[2] = {loadinfo.seg_sizes[0], loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1]}; | ||
| 115 | u32 n_reloc_tables = hdr.reloc_hdr_size / sizeof(u32); | ||
| 116 | std::vector<u8> program_image(loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] + | ||
| 117 | loadinfo.seg_sizes[2]); | ||
| 118 | |||
| 119 | loadinfo.seg_addrs[0] = base_addr; | ||
| 120 | loadinfo.seg_addrs[1] = loadinfo.seg_addrs[0] + loadinfo.seg_sizes[0]; | ||
| 121 | loadinfo.seg_addrs[2] = loadinfo.seg_addrs[1] + loadinfo.seg_sizes[1]; | ||
| 122 | loadinfo.seg_ptrs[0] = program_image.data(); | ||
| 123 | loadinfo.seg_ptrs[1] = loadinfo.seg_ptrs[0] + loadinfo.seg_sizes[0]; | ||
| 124 | loadinfo.seg_ptrs[2] = loadinfo.seg_ptrs[1] + loadinfo.seg_sizes[1]; | ||
| 125 | |||
| 126 | // Skip header for future compatibility | ||
| 127 | file.Seek(hdr.header_size, SEEK_SET); | ||
| 128 | |||
| 129 | // Read the relocation headers | ||
| 130 | std::vector<u32> relocs(n_reloc_tables * NUM_SEGMENTS); | ||
| 131 | for (unsigned int current_segment = 0; current_segment < NUM_SEGMENTS; ++current_segment) { | ||
| 132 | size_t size = n_reloc_tables * sizeof(u32); | ||
| 133 | if (file.ReadBytes(&relocs[current_segment * n_reloc_tables], size) != size) | ||
| 134 | return ERROR_READ; | ||
| 135 | } | ||
| 136 | |||
| 137 | // Read the segments | ||
| 138 | if (file.ReadBytes(loadinfo.seg_ptrs[0], hdr.code_seg_size) != hdr.code_seg_size) | ||
| 139 | return ERROR_READ; | ||
| 140 | if (file.ReadBytes(loadinfo.seg_ptrs[1], hdr.rodata_seg_size) != hdr.rodata_seg_size) | ||
| 141 | return ERROR_READ; | ||
| 142 | if (file.ReadBytes(loadinfo.seg_ptrs[2], hdr.data_seg_size - hdr.bss_size) != | ||
| 143 | hdr.data_seg_size - hdr.bss_size) | ||
| 144 | return ERROR_READ; | ||
| 145 | |||
| 146 | // BSS clear | ||
| 147 | memset((char*)loadinfo.seg_ptrs[2] + hdr.data_seg_size - hdr.bss_size, 0, hdr.bss_size); | ||
| 148 | |||
| 149 | // Relocate the segments | ||
| 150 | for (unsigned int current_segment = 0; current_segment < NUM_SEGMENTS; ++current_segment) { | ||
| 151 | for (unsigned current_segment_reloc_table = 0; current_segment_reloc_table < n_reloc_tables; | ||
| 152 | current_segment_reloc_table++) { | ||
| 153 | u32 n_relocs = relocs[current_segment * n_reloc_tables + current_segment_reloc_table]; | ||
| 154 | if (current_segment_reloc_table >= 2) { | ||
| 155 | // We are not using this table - ignore it because we don't know what it dose | ||
| 156 | file.Seek(n_relocs * sizeof(THREEDSX_Reloc), SEEK_CUR); | ||
| 157 | continue; | ||
| 158 | } | ||
| 159 | THREEDSX_Reloc reloc_table[RELOCBUFSIZE]; | ||
| 160 | |||
| 161 | u32* pos = (u32*)loadinfo.seg_ptrs[current_segment]; | ||
| 162 | const u32* end_pos = pos + (loadinfo.seg_sizes[current_segment] / 4); | ||
| 163 | |||
| 164 | while (n_relocs) { | ||
| 165 | u32 remaining = std::min(RELOCBUFSIZE, n_relocs); | ||
| 166 | n_relocs -= remaining; | ||
| 167 | |||
| 168 | if (file.ReadBytes(reloc_table, remaining * sizeof(THREEDSX_Reloc)) != | ||
| 169 | remaining * sizeof(THREEDSX_Reloc)) | ||
| 170 | return ERROR_READ; | ||
| 171 | |||
| 172 | for (unsigned current_inprogress = 0; | ||
| 173 | current_inprogress < remaining && pos < end_pos; current_inprogress++) { | ||
| 174 | const auto& table = reloc_table[current_inprogress]; | ||
| 175 | LOG_TRACE(Loader, "(t=%d,skip=%u,patch=%u)", current_segment_reloc_table, | ||
| 176 | static_cast<u32>(table.skip), static_cast<u32>(table.patch)); | ||
| 177 | pos += table.skip; | ||
| 178 | s32 num_patches = table.patch; | ||
| 179 | while (0 < num_patches && pos < end_pos) { | ||
| 180 | u32 in_addr = base_addr + static_cast<u32>(reinterpret_cast<u8*>(pos) - | ||
| 181 | program_image.data()); | ||
| 182 | u32 orig_data = *pos; | ||
| 183 | u32 sub_type = orig_data >> (32 - 4); | ||
| 184 | u32 addr = TranslateAddr(orig_data & ~0xF0000000, &loadinfo, offsets); | ||
| 185 | LOG_TRACE(Loader, "Patching %08X <-- rel(%08X,%d) (%08X)", in_addr, addr, | ||
| 186 | current_segment_reloc_table, *pos); | ||
| 187 | switch (current_segment_reloc_table) { | ||
| 188 | case 0: { | ||
| 189 | if (sub_type != 0) | ||
| 190 | return ERROR_READ; | ||
| 191 | *pos = addr; | ||
| 192 | break; | ||
| 193 | } | ||
| 194 | case 1: { | ||
| 195 | u32 data = addr - in_addr; | ||
| 196 | switch (sub_type) { | ||
| 197 | case 0: // 32-bit signed offset | ||
| 198 | *pos = data; | ||
| 199 | break; | ||
| 200 | case 1: // 31-bit signed offset | ||
| 201 | *pos = data & ~(1U << 31); | ||
| 202 | break; | ||
| 203 | default: | ||
| 204 | return ERROR_READ; | ||
| 205 | } | ||
| 206 | break; | ||
| 207 | } | ||
| 208 | default: | ||
| 209 | break; // this should never happen | ||
| 210 | } | ||
| 211 | pos++; | ||
| 212 | num_patches--; | ||
| 213 | } | ||
| 214 | } | ||
| 215 | } | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | // Create the CodeSet | ||
| 220 | SharedPtr<CodeSet> code_set = CodeSet::Create("", 0); | ||
| 221 | |||
| 222 | code_set->code.offset = loadinfo.seg_ptrs[0] - program_image.data(); | ||
| 223 | code_set->code.addr = loadinfo.seg_addrs[0]; | ||
| 224 | code_set->code.size = loadinfo.seg_sizes[0]; | ||
| 225 | |||
| 226 | code_set->rodata.offset = loadinfo.seg_ptrs[1] - program_image.data(); | ||
| 227 | code_set->rodata.addr = loadinfo.seg_addrs[1]; | ||
| 228 | code_set->rodata.size = loadinfo.seg_sizes[1]; | ||
| 229 | |||
| 230 | code_set->data.offset = loadinfo.seg_ptrs[2] - program_image.data(); | ||
| 231 | code_set->data.addr = loadinfo.seg_addrs[2]; | ||
| 232 | code_set->data.size = loadinfo.seg_sizes[2]; | ||
| 233 | |||
| 234 | code_set->entrypoint = code_set->code.addr; | ||
| 235 | code_set->memory = std::make_shared<std::vector<u8>>(std::move(program_image)); | ||
| 236 | |||
| 237 | LOG_DEBUG(Loader, "code size: 0x%X", loadinfo.seg_sizes[0]); | ||
| 238 | LOG_DEBUG(Loader, "rodata size: 0x%X", loadinfo.seg_sizes[1]); | ||
| 239 | LOG_DEBUG(Loader, "data size: 0x%X (including 0x%X of bss)", loadinfo.seg_sizes[2], | ||
| 240 | hdr.bss_size); | ||
| 241 | |||
| 242 | *out_codeset = code_set; | ||
| 243 | return ERROR_NONE; | ||
| 244 | } | ||
| 245 | |||
| 246 | FileType AppLoader_THREEDSX::IdentifyType(FileUtil::IOFile& file) { | ||
| 247 | u32 magic; | ||
| 248 | file.Seek(0, SEEK_SET); | ||
| 249 | if (1 != file.ReadArray<u32>(&magic, 1)) | ||
| 250 | return FileType::Error; | ||
| 251 | |||
| 252 | if (MakeMagic('3', 'D', 'S', 'X') == magic) | ||
| 253 | return FileType::THREEDSX; | ||
| 254 | |||
| 255 | return FileType::Error; | ||
| 256 | } | ||
| 257 | |||
| 258 | ResultStatus AppLoader_THREEDSX::Load(Kernel::SharedPtr<Kernel::Process>& process) { | ||
| 259 | if (is_loaded) | ||
| 260 | return ResultStatus::ErrorAlreadyLoaded; | ||
| 261 | |||
| 262 | if (!file.IsOpen()) | ||
| 263 | return ResultStatus::Error; | ||
| 264 | |||
| 265 | SharedPtr<CodeSet> codeset; | ||
| 266 | if (Load3DSXFile(file, Memory::PROCESS_IMAGE_VADDR, &codeset) != ERROR_NONE) | ||
| 267 | return ResultStatus::Error; | ||
| 268 | codeset->name = filename; | ||
| 269 | |||
| 270 | process = Kernel::Process::Create("main"); | ||
| 271 | process->LoadModule(codeset, codeset->entrypoint); | ||
| 272 | process->svc_access_mask.set(); | ||
| 273 | process->address_mappings = default_address_mappings; | ||
| 274 | |||
| 275 | // Attach the default resource limit (APPLICATION) to the process | ||
| 276 | process->resource_limit = | ||
| 277 | Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); | ||
| 278 | process->Run(codeset->entrypoint, 48, Kernel::DEFAULT_STACK_SIZE); | ||
| 279 | |||
| 280 | Service::FS::RegisterSelfNCCH(*this); | ||
| 281 | |||
| 282 | is_loaded = true; | ||
| 283 | return ResultStatus::Success; | ||
| 284 | } | ||
| 285 | |||
| 286 | ResultStatus AppLoader_THREEDSX::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, | ||
| 287 | u64& offset, u64& size) { | ||
| 288 | if (!file.IsOpen()) | ||
| 289 | return ResultStatus::Error; | ||
| 290 | |||
| 291 | // Reset read pointer in case this file has been read before. | ||
| 292 | file.Seek(0, SEEK_SET); | ||
| 293 | |||
| 294 | THREEDSX_Header hdr; | ||
| 295 | if (file.ReadBytes(&hdr, sizeof(THREEDSX_Header)) != sizeof(THREEDSX_Header)) | ||
| 296 | return ResultStatus::Error; | ||
| 297 | |||
| 298 | if (hdr.header_size != sizeof(THREEDSX_Header)) | ||
| 299 | return ResultStatus::Error; | ||
| 300 | |||
| 301 | // Check if the 3DSX has a RomFS... | ||
| 302 | if (hdr.fs_offset != 0) { | ||
| 303 | u32 romfs_offset = hdr.fs_offset; | ||
| 304 | u32 romfs_size = static_cast<u32>(file.GetSize()) - hdr.fs_offset; | ||
| 305 | |||
| 306 | LOG_DEBUG(Loader, "RomFS offset: 0x%08X", romfs_offset); | ||
| 307 | LOG_DEBUG(Loader, "RomFS size: 0x%08X", romfs_size); | ||
| 308 | |||
| 309 | // We reopen the file, to allow its position to be independent from file's | ||
| 310 | romfs_file = std::make_shared<FileUtil::IOFile>(filepath, "rb"); | ||
| 311 | if (!romfs_file->IsOpen()) | ||
| 312 | return ResultStatus::Error; | ||
| 313 | |||
| 314 | offset = romfs_offset; | ||
| 315 | size = romfs_size; | ||
| 316 | |||
| 317 | return ResultStatus::Success; | ||
| 318 | } | ||
| 319 | LOG_DEBUG(Loader, "3DSX has no RomFS"); | ||
| 320 | return ResultStatus::ErrorNotUsed; | ||
| 321 | } | ||
| 322 | |||
| 323 | ResultStatus AppLoader_THREEDSX::ReadIcon(std::vector<u8>& buffer) { | ||
| 324 | if (!file.IsOpen()) | ||
| 325 | return ResultStatus::Error; | ||
| 326 | |||
| 327 | // Reset read pointer in case this file has been read before. | ||
| 328 | file.Seek(0, SEEK_SET); | ||
| 329 | |||
| 330 | THREEDSX_Header hdr; | ||
| 331 | if (file.ReadBytes(&hdr, sizeof(THREEDSX_Header)) != sizeof(THREEDSX_Header)) | ||
| 332 | return ResultStatus::Error; | ||
| 333 | |||
| 334 | if (hdr.header_size != sizeof(THREEDSX_Header)) | ||
| 335 | return ResultStatus::Error; | ||
| 336 | |||
| 337 | // Check if the 3DSX has a SMDH... | ||
| 338 | if (hdr.smdh_offset != 0) { | ||
| 339 | file.Seek(hdr.smdh_offset, SEEK_SET); | ||
| 340 | buffer.resize(hdr.smdh_size); | ||
| 341 | |||
| 342 | if (file.ReadBytes(&buffer[0], hdr.smdh_size) != hdr.smdh_size) | ||
| 343 | return ResultStatus::Error; | ||
| 344 | |||
| 345 | return ResultStatus::Success; | ||
| 346 | } | ||
| 347 | return ResultStatus::ErrorNotUsed; | ||
| 348 | } | ||
| 349 | |||
| 350 | } // namespace Loader | ||
diff --git a/src/core/loader/3dsx.h b/src/core/loader/3dsx.h deleted file mode 100644 index 1e59bbb9d..000000000 --- a/src/core/loader/3dsx.h +++ /dev/null | |||
| @@ -1,46 +0,0 @@ | |||
| 1 | // Copyright 2014 Dolphin Emulator Project / 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 <string> | ||
| 8 | #include "common/common_types.h" | ||
| 9 | #include "core/loader/loader.h" | ||
| 10 | |||
| 11 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 12 | // Loader namespace | ||
| 13 | |||
| 14 | namespace Loader { | ||
| 15 | |||
| 16 | /// Loads an 3DSX file | ||
| 17 | class AppLoader_THREEDSX final : public AppLoader { | ||
| 18 | public: | ||
| 19 | AppLoader_THREEDSX(FileUtil::IOFile&& file, const std::string& filename, | ||
| 20 | const std::string& filepath) | ||
| 21 | : AppLoader(std::move(file)), filename(std::move(filename)), filepath(filepath) {} | ||
| 22 | |||
| 23 | /** | ||
| 24 | * Returns the type of the file | ||
| 25 | * @param file FileUtil::IOFile open file | ||
| 26 | * @return FileType found, or FileType::Error if this loader doesn't know it | ||
| 27 | */ | ||
| 28 | static FileType IdentifyType(FileUtil::IOFile& file); | ||
| 29 | |||
| 30 | FileType GetFileType() override { | ||
| 31 | return IdentifyType(file); | ||
| 32 | } | ||
| 33 | |||
| 34 | ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; | ||
| 35 | |||
| 36 | ResultStatus ReadIcon(std::vector<u8>& buffer) override; | ||
| 37 | |||
| 38 | ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, | ||
| 39 | u64& size) override; | ||
| 40 | |||
| 41 | private: | ||
| 42 | std::string filename; | ||
| 43 | std::string filepath; | ||
| 44 | }; | ||
| 45 | |||
| 46 | } // namespace Loader | ||
diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp index 73318c584..46b2c28d2 100644 --- a/src/core/loader/loader.cpp +++ b/src/core/loader/loader.cpp | |||
| @@ -31,9 +31,7 @@ FileType IdentifyFile(FileUtil::IOFile& file) { | |||
| 31 | if (FileType::Error != type) \ | 31 | if (FileType::Error != type) \ |
| 32 | return type; | 32 | return type; |
| 33 | 33 | ||
| 34 | CHECK_TYPE(THREEDSX) | ||
| 35 | CHECK_TYPE(ELF) | 34 | CHECK_TYPE(ELF) |
| 36 | CHECK_TYPE(NCCH) | ||
| 37 | CHECK_TYPE(NSO) | 35 | CHECK_TYPE(NSO) |
| 38 | CHECK_TYPE(NRO) | 36 | CHECK_TYPE(NRO) |
| 39 | 37 | ||
| @@ -58,33 +56,13 @@ FileType GuessFromExtension(const std::string& extension_) { | |||
| 58 | if (extension == ".elf" || extension == ".axf") | 56 | if (extension == ".elf" || extension == ".axf") |
| 59 | return FileType::ELF; | 57 | return FileType::ELF; |
| 60 | 58 | ||
| 61 | if (extension == ".cci" || extension == ".3ds") | ||
| 62 | return FileType::CCI; | ||
| 63 | |||
| 64 | if (extension == ".cxi") | ||
| 65 | return FileType::CXI; | ||
| 66 | |||
| 67 | if (extension == ".3dsx") | ||
| 68 | return FileType::THREEDSX; | ||
| 69 | |||
| 70 | if (extension == ".cia") | ||
| 71 | return FileType::CIA; | ||
| 72 | |||
| 73 | return FileType::Unknown; | 59 | return FileType::Unknown; |
| 74 | } | 60 | } |
| 75 | 61 | ||
| 76 | const char* GetFileTypeString(FileType type) { | 62 | const char* GetFileTypeString(FileType type) { |
| 77 | switch (type) { | 63 | switch (type) { |
| 78 | case FileType::CCI: | ||
| 79 | return "NCSD"; | ||
| 80 | case FileType::CXI: | ||
| 81 | return "NCCH"; | ||
| 82 | case FileType::CIA: | ||
| 83 | return "CIA"; | ||
| 84 | case FileType::ELF: | 64 | case FileType::ELF: |
| 85 | return "ELF"; | 65 | return "ELF"; |
| 86 | case FileType::THREEDSX: | ||
| 87 | return "3DSX"; | ||
| 88 | case FileType::Error: | 66 | case FileType::Error: |
| 89 | case FileType::Unknown: | 67 | case FileType::Unknown: |
| 90 | break; | 68 | break; |
| @@ -106,19 +84,10 @@ static std::unique_ptr<AppLoader> GetFileLoader(FileUtil::IOFile&& file, FileTyp | |||
| 106 | const std::string& filepath) { | 84 | const std::string& filepath) { |
| 107 | switch (type) { | 85 | switch (type) { |
| 108 | 86 | ||
| 109 | // 3DSX file format. | ||
| 110 | case FileType::THREEDSX: | ||
| 111 | return std::make_unique<AppLoader_THREEDSX>(std::move(file), filename, filepath); | ||
| 112 | |||
| 113 | // Standard ELF file format. | 87 | // Standard ELF file format. |
| 114 | case FileType::ELF: | 88 | case FileType::ELF: |
| 115 | return std::make_unique<AppLoader_ELF>(std::move(file), filename); | 89 | return std::make_unique<AppLoader_ELF>(std::move(file), filename); |
| 116 | 90 | ||
| 117 | // NCCH/NCSD container formats. | ||
| 118 | case FileType::CXI: | ||
| 119 | case FileType::CCI: | ||
| 120 | return std::make_unique<AppLoader_NCCH>(std::move(file), filepath); | ||
| 121 | |||
| 122 | // NX NSO file format. | 91 | // NX NSO file format. |
| 123 | case FileType::NSO: | 92 | case FileType::NSO: |
| 124 | return std::make_unique<AppLoader_NSO>(std::move(file), filepath); | 93 | return std::make_unique<AppLoader_NSO>(std::move(file), filepath); |
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h index 311785d05..ac4e7acc2 100644 --- a/src/core/loader/loader.h +++ b/src/core/loader/loader.h | |||
| @@ -29,11 +29,7 @@ namespace Loader { | |||
| 29 | enum class FileType { | 29 | enum class FileType { |
| 30 | Error, | 30 | Error, |
| 31 | Unknown, | 31 | Unknown, |
| 32 | CCI, | ||
| 33 | CXI, | ||
| 34 | CIA, | ||
| 35 | ELF, | 32 | ELF, |
| 36 | THREEDSX, // 3DSX | ||
| 37 | NSO, | 33 | NSO, |
| 38 | NRO, | 34 | NRO, |
| 39 | }; | 35 | }; |
diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp deleted file mode 100644 index e33a37b2e..000000000 --- a/src/core/loader/ncch.cpp +++ /dev/null | |||
| @@ -1,263 +0,0 @@ | |||
| 1 | // Copyright 2014 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cinttypes> | ||
| 7 | #include <codecvt> | ||
| 8 | #include <cstring> | ||
| 9 | #include <locale> | ||
| 10 | #include <memory> | ||
| 11 | #include "common/logging/log.h" | ||
| 12 | #include "common/string_util.h" | ||
| 13 | #include "common/swap.h" | ||
| 14 | #include "core/core.h" | ||
| 15 | #include "core/file_sys/archive_selfncch.h" | ||
| 16 | #include "core/file_sys/ncch_container.h" | ||
| 17 | #include "core/file_sys/title_metadata.h" | ||
| 18 | #include "core/hle/kernel/process.h" | ||
| 19 | #include "core/hle/kernel/resource_limit.h" | ||
| 20 | #include "core/hle/service/cfg/cfg.h" | ||
| 21 | #include "core/hle/service/fs/archive.h" | ||
| 22 | #include "core/loader/ncch.h" | ||
| 23 | #include "core/loader/smdh.h" | ||
| 24 | #include "core/memory.h" | ||
| 25 | #include "network/network.h" | ||
| 26 | |||
| 27 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 28 | // Loader namespace | ||
| 29 | |||
| 30 | namespace Loader { | ||
| 31 | |||
| 32 | static const u64 UPDATE_MASK = 0x0000000e00000000; | ||
| 33 | |||
| 34 | FileType AppLoader_NCCH::IdentifyType(FileUtil::IOFile& file) { | ||
| 35 | u32 magic; | ||
| 36 | file.Seek(0x100, SEEK_SET); | ||
| 37 | if (1 != file.ReadArray<u32>(&magic, 1)) | ||
| 38 | return FileType::Error; | ||
| 39 | |||
| 40 | if (MakeMagic('N', 'C', 'S', 'D') == magic) | ||
| 41 | return FileType::CCI; | ||
| 42 | |||
| 43 | if (MakeMagic('N', 'C', 'C', 'H') == magic) | ||
| 44 | return FileType::CXI; | ||
| 45 | |||
| 46 | return FileType::Error; | ||
| 47 | } | ||
| 48 | |||
| 49 | static std::string GetUpdateNCCHPath(u64_le program_id) { | ||
| 50 | u32 high = static_cast<u32>((program_id | UPDATE_MASK) >> 32); | ||
| 51 | u32 low = static_cast<u32>((program_id | UPDATE_MASK) & 0xFFFFFFFF); | ||
| 52 | |||
| 53 | // TODO(shinyquagsire23): Title database should be doing this path lookup | ||
| 54 | std::string content_path = Common::StringFromFormat( | ||
| 55 | "%sNintendo 3DS/%s/%s/title/%08x/%08x/content/", FileUtil::GetUserPath(D_SDMC_IDX).c_str(), | ||
| 56 | SYSTEM_ID, SDCARD_ID, high, low); | ||
| 57 | std::string tmd_path = content_path + "00000000.tmd"; | ||
| 58 | |||
| 59 | u32 content_id = 0; | ||
| 60 | FileSys::TitleMetadata tmd(tmd_path); | ||
| 61 | if (tmd.Load() == ResultStatus::Success) { | ||
| 62 | content_id = tmd.GetBootContentID(); | ||
| 63 | } | ||
| 64 | |||
| 65 | return Common::StringFromFormat("%s%08x.app", content_path.c_str(), content_id); | ||
| 66 | } | ||
| 67 | |||
| 68 | std::pair<boost::optional<u32>, ResultStatus> AppLoader_NCCH::LoadKernelSystemMode() { | ||
| 69 | if (!is_loaded) { | ||
| 70 | ResultStatus res = base_ncch.Load(); | ||
| 71 | if (res != ResultStatus::Success) { | ||
| 72 | return std::make_pair(boost::none, res); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | // Set the system mode as the one from the exheader. | ||
| 77 | return std::make_pair(overlay_ncch->exheader_header.arm11_system_local_caps.system_mode.Value(), | ||
| 78 | ResultStatus::Success); | ||
| 79 | } | ||
| 80 | |||
| 81 | ResultStatus AppLoader_NCCH::LoadExec(Kernel::SharedPtr<Kernel::Process>& process) { | ||
| 82 | using Kernel::CodeSet; | ||
| 83 | using Kernel::SharedPtr; | ||
| 84 | |||
| 85 | if (!is_loaded) | ||
| 86 | return ResultStatus::ErrorNotLoaded; | ||
| 87 | |||
| 88 | std::vector<u8> code; | ||
| 89 | u64_le program_id; | ||
| 90 | if (ResultStatus::Success == ReadCode(code) && | ||
| 91 | ResultStatus::Success == ReadProgramId(program_id)) { | ||
| 92 | std::string process_name = Common::StringFromFixedZeroTerminatedBuffer( | ||
| 93 | (const char*)overlay_ncch->exheader_header.codeset_info.name, 8); | ||
| 94 | |||
| 95 | SharedPtr<CodeSet> codeset = CodeSet::Create(process_name, program_id); | ||
| 96 | |||
| 97 | codeset->code.offset = 0; | ||
| 98 | codeset->code.addr = overlay_ncch->exheader_header.codeset_info.text.address; | ||
| 99 | codeset->code.size = | ||
| 100 | overlay_ncch->exheader_header.codeset_info.text.num_max_pages * Memory::PAGE_SIZE; | ||
| 101 | |||
| 102 | codeset->rodata.offset = codeset->code.offset + codeset->code.size; | ||
| 103 | codeset->rodata.addr = overlay_ncch->exheader_header.codeset_info.ro.address; | ||
| 104 | codeset->rodata.size = | ||
| 105 | overlay_ncch->exheader_header.codeset_info.ro.num_max_pages * Memory::PAGE_SIZE; | ||
| 106 | |||
| 107 | // TODO(yuriks): Not sure if the bss size is added to the page-aligned .data size or just | ||
| 108 | // to the regular size. Playing it safe for now. | ||
| 109 | u32 bss_page_size = (overlay_ncch->exheader_header.codeset_info.bss_size + 0xFFF) & ~0xFFF; | ||
| 110 | code.resize(code.size() + bss_page_size, 0); | ||
| 111 | |||
| 112 | codeset->data.offset = codeset->rodata.offset + codeset->rodata.size; | ||
| 113 | codeset->data.addr = overlay_ncch->exheader_header.codeset_info.data.address; | ||
| 114 | codeset->data.size = | ||
| 115 | overlay_ncch->exheader_header.codeset_info.data.num_max_pages * Memory::PAGE_SIZE + | ||
| 116 | bss_page_size; | ||
| 117 | |||
| 118 | codeset->entrypoint = codeset->code.addr; | ||
| 119 | codeset->memory = std::make_shared<std::vector<u8>>(std::move(code)); | ||
| 120 | |||
| 121 | process = Kernel::Process::Create("main"); | ||
| 122 | process->LoadModule(codeset, codeset->entrypoint); | ||
| 123 | |||
| 124 | // Attach a resource limit to the process based on the resource limit category | ||
| 125 | process->resource_limit = | ||
| 126 | Kernel::ResourceLimit::GetForCategory(static_cast<Kernel::ResourceLimitCategory>( | ||
| 127 | overlay_ncch->exheader_header.arm11_system_local_caps.resource_limit_category)); | ||
| 128 | |||
| 129 | // Set the default CPU core for this process | ||
| 130 | process->ideal_processor = | ||
| 131 | overlay_ncch->exheader_header.arm11_system_local_caps.ideal_processor; | ||
| 132 | |||
| 133 | // Copy data while converting endianness | ||
| 134 | std::array<u32, ARRAY_SIZE(overlay_ncch->exheader_header.arm11_kernel_caps.descriptors)> | ||
| 135 | kernel_caps; | ||
| 136 | std::copy_n(overlay_ncch->exheader_header.arm11_kernel_caps.descriptors, kernel_caps.size(), | ||
| 137 | begin(kernel_caps)); | ||
| 138 | process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size()); | ||
| 139 | |||
| 140 | s32 priority = overlay_ncch->exheader_header.arm11_system_local_caps.priority; | ||
| 141 | u32 stack_size = overlay_ncch->exheader_header.codeset_info.stack_size; | ||
| 142 | process->Run(codeset->entrypoint, priority, stack_size); | ||
| 143 | return ResultStatus::Success; | ||
| 144 | } | ||
| 145 | return ResultStatus::Error; | ||
| 146 | } | ||
| 147 | |||
| 148 | void AppLoader_NCCH::ParseRegionLockoutInfo() { | ||
| 149 | std::vector<u8> smdh_buffer; | ||
| 150 | if (ReadIcon(smdh_buffer) == ResultStatus::Success && smdh_buffer.size() >= sizeof(SMDH)) { | ||
| 151 | SMDH smdh; | ||
| 152 | memcpy(&smdh, smdh_buffer.data(), sizeof(SMDH)); | ||
| 153 | u32 region_lockout = smdh.region_lockout; | ||
| 154 | constexpr u32 REGION_COUNT = 7; | ||
| 155 | for (u32 region = 0; region < REGION_COUNT; ++region) { | ||
| 156 | if (region_lockout & 1) { | ||
| 157 | Service::CFG::SetPreferredRegionCode(region); | ||
| 158 | break; | ||
| 159 | } | ||
| 160 | region_lockout >>= 1; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | ResultStatus AppLoader_NCCH::Load(Kernel::SharedPtr<Kernel::Process>& process) { | ||
| 166 | u64_le ncch_program_id; | ||
| 167 | |||
| 168 | if (is_loaded) | ||
| 169 | return ResultStatus::ErrorAlreadyLoaded; | ||
| 170 | |||
| 171 | ResultStatus result = base_ncch.Load(); | ||
| 172 | if (result != ResultStatus::Success) | ||
| 173 | return result; | ||
| 174 | |||
| 175 | ReadProgramId(ncch_program_id); | ||
| 176 | std::string program_id{Common::StringFromFormat("%016" PRIX64, ncch_program_id)}; | ||
| 177 | |||
| 178 | LOG_INFO(Loader, "Program ID: %s", program_id.c_str()); | ||
| 179 | |||
| 180 | update_ncch.OpenFile(GetUpdateNCCHPath(ncch_program_id)); | ||
| 181 | result = update_ncch.Load(); | ||
| 182 | if (result == ResultStatus::Success) { | ||
| 183 | overlay_ncch = &update_ncch; | ||
| 184 | } | ||
| 185 | |||
| 186 | Core::Telemetry().AddField(Telemetry::FieldType::Session, "ProgramId", program_id); | ||
| 187 | |||
| 188 | if (auto room_member = Network::GetRoomMember().lock()) { | ||
| 189 | Network::GameInfo game_info; | ||
| 190 | ReadTitle(game_info.name); | ||
| 191 | game_info.id = ncch_program_id; | ||
| 192 | room_member->SendGameInfo(game_info); | ||
| 193 | } | ||
| 194 | |||
| 195 | is_loaded = true; // Set state to loaded | ||
| 196 | |||
| 197 | result = LoadExec(process); // Load the executable into memory for booting | ||
| 198 | if (ResultStatus::Success != result) | ||
| 199 | return result; | ||
| 200 | |||
| 201 | Service::FS::RegisterSelfNCCH(*this); | ||
| 202 | |||
| 203 | ParseRegionLockoutInfo(); | ||
| 204 | |||
| 205 | return ResultStatus::Success; | ||
| 206 | } | ||
| 207 | |||
| 208 | ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) { | ||
| 209 | return overlay_ncch->LoadSectionExeFS(".code", buffer); | ||
| 210 | } | ||
| 211 | |||
| 212 | ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) { | ||
| 213 | return overlay_ncch->LoadSectionExeFS("icon", buffer); | ||
| 214 | } | ||
| 215 | |||
| 216 | ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) { | ||
| 217 | return overlay_ncch->LoadSectionExeFS("banner", buffer); | ||
| 218 | } | ||
| 219 | |||
| 220 | ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) { | ||
| 221 | return overlay_ncch->LoadSectionExeFS("logo", buffer); | ||
| 222 | } | ||
| 223 | |||
| 224 | ResultStatus AppLoader_NCCH::ReadProgramId(u64& out_program_id) { | ||
| 225 | ResultStatus result = base_ncch.ReadProgramId(out_program_id); | ||
| 226 | if (result != ResultStatus::Success) | ||
| 227 | return result; | ||
| 228 | |||
| 229 | return ResultStatus::Success; | ||
| 230 | } | ||
| 231 | |||
| 232 | ResultStatus AppLoader_NCCH::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, | ||
| 233 | u64& size) { | ||
| 234 | return base_ncch.ReadRomFS(romfs_file, offset, size); | ||
| 235 | } | ||
| 236 | |||
| 237 | ResultStatus AppLoader_NCCH::ReadUpdateRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, | ||
| 238 | u64& offset, u64& size) { | ||
| 239 | ResultStatus result = update_ncch.ReadRomFS(romfs_file, offset, size); | ||
| 240 | |||
| 241 | if (result != ResultStatus::Success) | ||
| 242 | return base_ncch.ReadRomFS(romfs_file, offset, size); | ||
| 243 | } | ||
| 244 | |||
| 245 | ResultStatus AppLoader_NCCH::ReadTitle(std::string& title) { | ||
| 246 | std::vector<u8> data; | ||
| 247 | Loader::SMDH smdh; | ||
| 248 | ReadIcon(data); | ||
| 249 | |||
| 250 | if (!Loader::IsValidSMDH(data)) { | ||
| 251 | return ResultStatus::ErrorInvalidFormat; | ||
| 252 | } | ||
| 253 | |||
| 254 | memcpy(&smdh, data.data(), sizeof(Loader::SMDH)); | ||
| 255 | |||
| 256 | const auto& short_title = smdh.GetShortTitle(SMDH::TitleLanguage::English); | ||
| 257 | auto title_end = std::find(short_title.begin(), short_title.end(), u'\0'); | ||
| 258 | title = Common::UTF16ToUTF8(std::u16string{short_title.begin(), title_end}); | ||
| 259 | |||
| 260 | return ResultStatus::Success; | ||
| 261 | } | ||
| 262 | |||
| 263 | } // namespace Loader | ||
diff --git a/src/core/loader/ncch.h b/src/core/loader/ncch.h deleted file mode 100644 index 09230ae33..000000000 --- a/src/core/loader/ncch.h +++ /dev/null | |||
| @@ -1,80 +0,0 @@ | |||
| 1 | // Copyright 2014 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 <memory> | ||
| 8 | #include "common/common_types.h" | ||
| 9 | #include "common/swap.h" | ||
| 10 | #include "core/file_sys/ncch_container.h" | ||
| 11 | #include "core/loader/loader.h" | ||
| 12 | |||
| 13 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 14 | // Loader namespace | ||
| 15 | |||
| 16 | namespace Loader { | ||
| 17 | |||
| 18 | /// Loads an NCCH file (e.g. from a CCI, or the first NCCH in a CXI) | ||
| 19 | class AppLoader_NCCH final : public AppLoader { | ||
| 20 | public: | ||
| 21 | AppLoader_NCCH(FileUtil::IOFile&& file, const std::string& filepath) | ||
| 22 | : AppLoader(std::move(file)), filepath(filepath), base_ncch(filepath), | ||
| 23 | overlay_ncch(&base_ncch) {} | ||
| 24 | |||
| 25 | /** | ||
| 26 | * Returns the type of the file | ||
| 27 | * @param file FileUtil::IOFile open file | ||
| 28 | * @return FileType found, or FileType::Error if this loader doesn't know it | ||
| 29 | */ | ||
| 30 | static FileType IdentifyType(FileUtil::IOFile& file); | ||
| 31 | |||
| 32 | FileType GetFileType() override { | ||
| 33 | return IdentifyType(file); | ||
| 34 | } | ||
| 35 | |||
| 36 | ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; | ||
| 37 | |||
| 38 | /** | ||
| 39 | * Loads the Exheader and returns the system mode for this application. | ||
| 40 | * @returns A pair with the optional system mode, and and the status. | ||
| 41 | */ | ||
| 42 | std::pair<boost::optional<u32>, ResultStatus> LoadKernelSystemMode() override; | ||
| 43 | |||
| 44 | ResultStatus ReadCode(std::vector<u8>& buffer) override; | ||
| 45 | |||
| 46 | ResultStatus ReadIcon(std::vector<u8>& buffer) override; | ||
| 47 | |||
| 48 | ResultStatus ReadBanner(std::vector<u8>& buffer) override; | ||
| 49 | |||
| 50 | ResultStatus ReadLogo(std::vector<u8>& buffer) override; | ||
| 51 | |||
| 52 | ResultStatus ReadProgramId(u64& out_program_id) override; | ||
| 53 | |||
| 54 | ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, | ||
| 55 | u64& size) override; | ||
| 56 | |||
| 57 | ResultStatus ReadUpdateRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, | ||
| 58 | u64& size) override; | ||
| 59 | |||
| 60 | ResultStatus ReadTitle(std::string& title) override; | ||
| 61 | |||
| 62 | private: | ||
| 63 | /** | ||
| 64 | * Loads .code section into memory for booting | ||
| 65 | * @param process The newly created process | ||
| 66 | * @return ResultStatus result of function | ||
| 67 | */ | ||
| 68 | ResultStatus LoadExec(Kernel::SharedPtr<Kernel::Process>& process); | ||
| 69 | |||
| 70 | /// Reads the region lockout info in the SMDH and send it to CFG service | ||
| 71 | void ParseRegionLockoutInfo(); | ||
| 72 | |||
| 73 | FileSys::NCCHContainer base_ncch; | ||
| 74 | FileSys::NCCHContainer update_ncch; | ||
| 75 | FileSys::NCCHContainer* overlay_ncch; | ||
| 76 | |||
| 77 | std::string filepath; | ||
| 78 | }; | ||
| 79 | |||
| 80 | } // namespace Loader | ||