summaryrefslogtreecommitdiff
path: root/src/core/loader/3dsx.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2017-10-12 21:21:49 -0400
committerGravatar bunnei2017-10-12 21:21:49 -0400
commit72b03025ac4ef0d8633c2f3e55b513cd149c59e5 (patch)
treef1fbeb915a0b3df8e4e988a6a562a763e18ea666 /src/core/loader/3dsx.cpp
parenthle: Remove a large amount of 3ds-specific service code. (diff)
downloadyuzu-72b03025ac4ef0d8633c2f3e55b513cd149c59e5.tar.gz
yuzu-72b03025ac4ef0d8633c2f3e55b513cd149c59e5.tar.xz
yuzu-72b03025ac4ef0d8633c2f3e55b513cd149c59e5.zip
Remove lots more 3DS-specific code.
Diffstat (limited to 'src/core/loader/3dsx.cpp')
-rw-r--r--src/core/loader/3dsx.cpp350
1 files changed, 0 insertions, 350 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
15namespace 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
38enum THREEDSX_Error { ERROR_NONE = 0, ERROR_READ = 1, ERROR_FILE = 2, ERROR_ALLOC = 3 };
39
40static const u32 RELOCBUFSIZE = 512;
41static const unsigned int NUM_SEGMENTS = 3;
42
43// File header
44#pragma pack(1)
45struct 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.
61struct 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
75struct THREEDSX_Reloc {
76 u16 skip, patch;
77};
78#pragma pack()
79
80struct THREEloadinfo {
81 u8* seg_ptrs[3]; // code, rodata & data
82 u32 seg_addrs[3];
83 u32 seg_sizes[3];
84};
85
86static 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
94using Kernel::CodeSet;
95using Kernel::SharedPtr;
96
97static 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
246FileType 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
258ResultStatus 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
286ResultStatus 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
323ResultStatus 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