summaryrefslogtreecommitdiff
path: root/src/core/loader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/loader.cpp')
-rw-r--r--src/core/loader.cpp275
1 files changed, 0 insertions, 275 deletions
diff --git a/src/core/loader.cpp b/src/core/loader.cpp
deleted file mode 100644
index ff1c873bb..000000000
--- a/src/core/loader.cpp
+++ /dev/null
@@ -1,275 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2
3// Refer to the license.txt file included.
4
5#include "common/common_types.h"
6#include "common/file_util.h"
7
8#include "core/loader.h"
9#include "core/system.h"
10#include "core/core.h"
11#include "core/file_sys/directory_file_system.h"
12#include "core/elf/elf_reader.h"
13#include "core/hle/kernel/kernel.h"
14#include "core/mem_map.h"
15
16////////////////////////////////////////////////////////////////////////////////////////////////////
17
18/// Loads an extracted CXI from a directory
19bool LoadDirectory_CXI(std::string &filename) {
20 std::string full_path = filename;
21 std::string path, file, extension;
22 SplitPath(ReplaceAll(full_path, "\\", "/"), &path, &file, &extension);
23#if EMU_PLATFORM == PLATFORM_WINDOWS
24 path = ReplaceAll(path, "/", "\\");
25#endif
26 DirectoryFileSystem *fs = new DirectoryFileSystem(&System::g_ctr_file_system, path);
27 System::g_ctr_file_system.Mount("fs:", fs);
28
29 std::string final_name = "fs:/" + file + extension;
30 File::IOFile f(filename, "rb");
31
32 if (f.IsOpen()) {
33 // TODO(ShizZy): read here to memory....
34 }
35 ERROR_LOG(TIME, "Unimplemented function!");
36 return true;
37}
38
39/// Loads a CTR ELF file
40bool Load_ELF(std::string &filename) {
41 std::string full_path = filename;
42 std::string path, file, extension;
43 SplitPath(ReplaceAll(full_path, "\\", "/"), &path, &file, &extension);
44#if EMU_PLATFORM == PLATFORM_WINDOWS
45 path = ReplaceAll(path, "/", "\\");
46#endif
47 File::IOFile f(filename, "rb");
48
49 if (f.IsOpen()) {
50 u64 size = f.GetSize();
51 u8* buffer = new u8[size];
52 ElfReader* elf_reader = NULL;
53
54 f.ReadBytes(buffer, size);
55
56 elf_reader = new ElfReader(buffer);
57 elf_reader->LoadInto(0x00100000);
58
59 Kernel::LoadExec(elf_reader->GetEntryPoint());
60
61 delete[] buffer;
62 delete elf_reader;
63 } else {
64 return false;
65 }
66 f.Close();
67
68 return true;
69}
70
71/// Loads a Launcher DAT file
72bool Load_DAT(std::string &filename) {
73 std::string full_path = filename;
74 std::string path, file, extension;
75 SplitPath(ReplaceAll(full_path, "\\", "/"), &path, &file, &extension);
76#if EMU_PLATFORM == PLATFORM_WINDOWS
77 path = ReplaceAll(path, "/", "\\");
78#endif
79 File::IOFile f(filename, "rb");
80
81 if (f.IsOpen()) {
82 u64 size = f.GetSize();
83 u8* buffer = new u8[size];
84
85 f.ReadBytes(buffer, size);
86
87 /**
88 * (mattvail) We shouldn't really support this type of file
89 * but for the sake of making it easier... we'll temporarily/hackishly
90 * allow it. No sense in making a proper reader for this.
91 */
92 u32 entry_point = 0x00100000; // write to same entrypoint as elf
93 u32 payload_offset = 0xA150;
94
95 const u8 *src = &buffer[payload_offset];
96 u8 *dst = Memory::GetPointer(entry_point);
97 u32 srcSize = size - payload_offset; //just load everything...
98 u32 *s = (u32*)src;
99 u32 *d = (u32*)dst;
100 for (int j = 0; j < (int)(srcSize + 3) / 4; j++)
101 {
102 *d++ = (*s++);
103 }
104
105 Kernel::LoadExec(entry_point);
106
107
108 delete[] buffer;
109 }
110 else {
111 return false;
112 }
113 f.Close();
114
115 return true;
116}
117
118
119/// Loads a CTR BIN file extracted from an ExeFS
120bool Load_BIN(std::string &filename) {
121 std::string full_path = filename;
122 std::string path, file, extension;
123 SplitPath(ReplaceAll(full_path, "\\", "/"), &path, &file, &extension);
124#if EMU_PLATFORM == PLATFORM_WINDOWS
125 path = ReplaceAll(path, "/", "\\");
126#endif
127 File::IOFile f(filename, "rb");
128
129 if (f.IsOpen()) {
130 u64 size = f.GetSize();
131 u8* buffer = new u8[size];
132
133 f.ReadBytes(buffer, size);
134
135 u32 entry_point = 0x00100000; // Hardcoded, read from exheader
136
137 const u8 *src = buffer;
138 u8 *dst = Memory::GetPointer(entry_point);
139 u32 srcSize = size;
140 u32 *s = (u32*)src;
141 u32 *d = (u32*)dst;
142 for (int j = 0; j < (int)(srcSize + 3) / 4; j++)
143 {
144 *d++ = (*s++);
145 }
146
147 Kernel::LoadExec(entry_point);
148
149 delete[] buffer;
150 }
151 else {
152 return false;
153 }
154 f.Close();
155
156 return true;
157}
158
159namespace Loader {
160
161bool IsBootableDirectory() {
162 ERROR_LOG(TIME, "Unimplemented function!");
163 return true;
164}
165
166/**
167 * Identifies the type of a bootable file
168 * @param filename String filename of bootable file
169 * @todo (ShizZy) this function sucks... make it actually check file contents etc.
170 * @return FileType of file
171 */
172FileType IdentifyFile(std::string &filename) {
173 if (filename.size() == 0) {
174 ERROR_LOG(LOADER, "invalid filename %s", filename.c_str());
175 return FILETYPE_ERROR;
176 }
177 std::string extension = filename.size() >= 5 ? filename.substr(filename.size() - 4) : "";
178
179 if (File::IsDirectory(filename)) {
180 if (IsBootableDirectory()) {
181 return FILETYPE_DIRECTORY_CXI;
182 }
183 else {
184 return FILETYPE_NORMAL_DIRECTORY;
185 }
186 }
187 else if (!strcasecmp(extension.c_str(), ".elf")) {
188 return FILETYPE_CTR_ELF; // TODO(bunnei): Do some filetype checking :p
189 }
190 else if (!strcasecmp(extension.c_str(), ".axf")) {
191 return FILETYPE_CTR_ELF; // TODO(bunnei): Do some filetype checking :p
192 }
193 else if (!strcasecmp(extension.c_str(), ".bin")) {
194 return FILETYPE_CTR_BIN;
195 }
196 else if (!strcasecmp(extension.c_str(), ".dat")) {
197 return FILETYPE_LAUNCHER_DAT;
198 }
199 else if (!strcasecmp(extension.c_str(), ".zip")) {
200 return FILETYPE_ARCHIVE_ZIP;
201 }
202 else if (!strcasecmp(extension.c_str(), ".rar")) {
203 return FILETYPE_ARCHIVE_RAR;
204 }
205 else if (!strcasecmp(extension.c_str(), ".r00")) {
206 return FILETYPE_ARCHIVE_RAR;
207 }
208 else if (!strcasecmp(extension.c_str(), ".r01")) {
209 return FILETYPE_ARCHIVE_RAR;
210 }
211 return FILETYPE_UNKNOWN;
212}
213
214/**
215 * Identifies and loads a bootable file
216 * @param filename String filename of bootable file
217 * @param error_string Point to string to put error message if an error has occurred
218 * @return True on success, otherwise false
219 */
220bool LoadFile(std::string &filename, std::string *error_string) {
221 INFO_LOG(LOADER, "Identifying file...");
222
223 // Note that this can modify filename!
224 switch (IdentifyFile(filename)) {
225
226 case FILETYPE_CTR_ELF:
227 return Load_ELF(filename);
228
229 case FILETYPE_CTR_BIN:
230 return Load_BIN(filename);
231
232 case FILETYPE_LAUNCHER_DAT:
233 return Load_DAT(filename);
234
235 case FILETYPE_DIRECTORY_CXI:
236 return LoadDirectory_CXI(filename);
237
238 case FILETYPE_ERROR:
239 ERROR_LOG(LOADER, "Could not read file");
240 *error_string = "Error reading file";
241 break;
242
243 case FILETYPE_ARCHIVE_RAR:
244#ifdef WIN32
245 *error_string = "RAR file detected (Require WINRAR)";
246#else
247 *error_string = "RAR file detected (Require UnRAR)";
248#endif
249 break;
250
251 case FILETYPE_ARCHIVE_ZIP:
252#ifdef WIN32
253 *error_string = "ZIP file detected (Require WINRAR)";
254#else
255 *error_string = "ZIP file detected (Require UnRAR)";
256#endif
257 break;
258
259 case FILETYPE_NORMAL_DIRECTORY:
260 ERROR_LOG(LOADER, "Just a directory.");
261 *error_string = "Just a directory.";
262 break;
263
264 case FILETYPE_UNKNOWN_BIN:
265 case FILETYPE_UNKNOWN_ELF:
266 case FILETYPE_UNKNOWN:
267 default:
268 ERROR_LOG(LOADER, "Failed to identify file");
269 *error_string = " Failed to identify file";
270 break;
271 }
272 return false;
273}
274
275} // namespace \ No newline at end of file