CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/Loaders.cpp
Views: 1401
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include <algorithm>
19
#include <cstdio>
20
21
#include "Common/File/AndroidContentURI.h"
22
#include "Common/File/FileUtil.h"
23
#include "Common/File/Path.h"
24
#include "Common/StringUtils.h"
25
#include "Core/FileLoaders/CachingFileLoader.h"
26
#include "Core/FileLoaders/DiskCachingFileLoader.h"
27
#include "Core/FileLoaders/HTTPFileLoader.h"
28
#include "Core/FileLoaders/LocalFileLoader.h"
29
#include "Core/FileLoaders/RetryingFileLoader.h"
30
#include "Core/FileSystems/MetaFileSystem.h"
31
#include "Core/PSPLoaders.h"
32
#include "Core/MemMap.h"
33
#include "Core/Loaders.h"
34
#include "Core/System.h"
35
#include "Core/ELF/PBPReader.h"
36
#include "Core/ELF/ParamSFO.h"
37
38
FileLoader *ConstructFileLoader(const Path &filename) {
39
if (filename.Type() == PathType::HTTP) {
40
FileLoader *baseLoader = new RetryingFileLoader(new HTTPFileLoader(filename));
41
// For headless, avoid disk caching since it's usually used for tests that might mutate.
42
if (!PSP_CoreParameter().headLess) {
43
baseLoader = new DiskCachingFileLoader(baseLoader);
44
}
45
return new CachingFileLoader(baseLoader);
46
}
47
return new LocalFileLoader(filename);
48
}
49
50
// TODO : improve, look in the file more
51
IdentifiedFileType Identify_File(FileLoader *fileLoader, std::string *errorString) {
52
errorString->clear();
53
if (fileLoader == nullptr) {
54
*errorString = "Invalid fileLoader";
55
return IdentifiedFileType::ERROR_IDENTIFYING;
56
}
57
if (fileLoader->GetPath().size() == 0) {
58
*errorString = "Invalid filename " + fileLoader->GetPath().ToString();
59
return IdentifiedFileType::ERROR_IDENTIFYING;
60
}
61
62
if (!fileLoader->Exists()) {
63
*errorString = "IdentifyFile: File doesn't exist: " + fileLoader->GetPath().ToString();
64
return IdentifiedFileType::ERROR_IDENTIFYING;
65
}
66
67
std::string extension = fileLoader->GetPath().GetFileExtension();
68
if (extension == ".iso") {
69
// may be a psx iso, they have 2352 byte sectors. You never know what some people try to open
70
if ((fileLoader->FileSize() % 2352) == 0) {
71
unsigned char sync[12];
72
fileLoader->ReadAt(0, 12, sync);
73
74
// each sector in a mode2 image starts with these 12 bytes
75
if (memcmp(sync,"\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00", 12) == 0) {
76
*errorString = "ISO in Mode 2: Not a PSP game";
77
return IdentifiedFileType::ISO_MODE2;
78
}
79
80
// maybe it also just happened to have that size, let's assume it's a PSP ISO and error out later if it's not.
81
}
82
return IdentifiedFileType::PSP_ISO;
83
} else if (extension == ".cso" || extension == ".chd") {
84
return IdentifiedFileType::PSP_ISO;
85
} else if (extension == ".ppst") {
86
return IdentifiedFileType::PPSSPP_SAVESTATE;
87
} else if (extension == ".ppdmp") {
88
char data[8]{};
89
fileLoader->ReadAt(0, 8, data);
90
if (memcmp(data, "PPSSPPGE", 8) == 0) {
91
return IdentifiedFileType::PPSSPP_GE_DUMP;
92
}
93
}
94
95
// First, check if it's a directory with an EBOOT.PBP in it.
96
if (fileLoader->IsDirectory()) {
97
Path filename = fileLoader->GetPath();
98
if (filename.size() > 4) {
99
// Check for existence of EBOOT.PBP, as required for "Directory games".
100
if (File::Exists(filename / "EBOOT.PBP")) {
101
return IdentifiedFileType::PSP_PBP_DIRECTORY;
102
}
103
104
// check if it's a disc directory
105
if (File::Exists(filename / "PSP_GAME")) {
106
return IdentifiedFileType::PSP_DISC_DIRECTORY;
107
}
108
109
// Not that, okay, let's guess it's a savedata directory if it has a param.sfo...
110
if (File::Exists(filename / "PARAM.SFO")) {
111
return IdentifiedFileType::PSP_SAVEDATA_DIRECTORY;
112
}
113
}
114
115
return IdentifiedFileType::NORMAL_DIRECTORY;
116
}
117
118
// OK, quick methods of identification for common types failed. Moving on to more expensive methods,
119
// starting by reading the first few bytes.
120
// This can be necessary for weird Android content storage path types, see issue #17462
121
122
u32_le id;
123
124
size_t readSize = fileLoader->ReadAt(0, 4, 1, &id);
125
if (readSize != 1) {
126
*errorString = "Failed to read identification bytes";
127
return IdentifiedFileType::ERROR_IDENTIFYING;
128
}
129
130
u32_le psar_offset = 0, psar_id = 0;
131
u32 _id = id;
132
if (!memcmp(&_id, "PK\x03\x04", 4) || !memcmp(&_id, "PK\x05\x06", 4) || !memcmp(&_id, "PK\x07\x08", 4)) {
133
return IdentifiedFileType::ARCHIVE_ZIP;
134
} else if (!memcmp(&_id, "\x00PBP", 4)) {
135
fileLoader->ReadAt(0x24, 4, 1, &psar_offset);
136
fileLoader->ReadAt(psar_offset, 4, 1, &psar_id);
137
// Fall through to the below if chain.
138
} else if (!memcmp(&_id, "Rar!", 4)) {
139
return IdentifiedFileType::ARCHIVE_RAR;
140
} else if (!memcmp(&_id, "\x37\x7A\xBC\xAF", 4)) {
141
return IdentifiedFileType::ARCHIVE_7Z;
142
} else if (!memcmp(&_id, "\0\0\0\0", 4)) {
143
// All zeroes. ISO files start like this but their 16th 2048-byte sector contains metadata.
144
if (fileLoader->FileSize() > 0x8100) {
145
char buffer[16];
146
fileLoader->ReadAt(0x8000, sizeof(buffer), buffer);
147
if (!memcmp(buffer + 1, "CD001", 5)) {
148
// It's an ISO file.
149
if (!memcmp(buffer + 8, "PSP GAME", 8)) {
150
return IdentifiedFileType::PSP_ISO;
151
}
152
return IdentifiedFileType::UNKNOWN_ISO;
153
}
154
}
155
} else if (!memcmp(&_id, "CISO", 4)) {
156
// CISO are not used for many other kinds of ISO so let's just guess it's a PSP one and let it
157
// fail later...
158
return IdentifiedFileType::PSP_ISO;
159
} else if (!memcmp(&_id, "MCom", 4)) {
160
size_t readSize = fileLoader->ReadAt(4, 4, 1, &_id);
161
if (!memcmp(&_id, "prHD", 4)) {
162
return IdentifiedFileType::PSP_ISO; // CHD file
163
}
164
}
165
166
if (id == 'FLE\x7F') {
167
Path filename = fileLoader->GetPath();
168
// There are a few elfs misnamed as pbp (like Trig Wars), accept that. Also accept extension-less paths.
169
if (extension == ".plf" || strstr(filename.GetFilename().c_str(), "BOOT.BIN") ||
170
extension == ".elf" || extension == ".prx" || extension == ".pbp" || extension == "") {
171
return IdentifiedFileType::PSP_ELF;
172
}
173
return IdentifiedFileType::UNKNOWN_ELF;
174
} else if (id == 'PBP\x00') {
175
// Do this PS1 eboot check FIRST before checking other eboot types.
176
// It seems like some are malformed and slip through the PSAR check below.
177
PBPReader pbp(fileLoader);
178
if (pbp.IsValid() && !pbp.IsELF()) {
179
std::vector<u8> sfoData;
180
if (pbp.GetSubFile(PBP_PARAM_SFO, &sfoData)) {
181
ParamSFOData paramSFO;
182
paramSFO.ReadSFO(sfoData);
183
// PS1 Eboots are supposed to use "ME" as their PARAM SFO category.
184
// If they don't, and they're still malformed (e.g. PSISOIMG0000 isn't found), there's nothing we can do.
185
if (paramSFO.GetValueString("CATEGORY") == "ME")
186
return IdentifiedFileType::PSP_PS1_PBP;
187
}
188
}
189
190
if (psar_id == 'MUPN') {
191
return IdentifiedFileType::PSP_ISO_NP;
192
}
193
// PS1 PSAR begins with "PSISOIMG0000"
194
if (psar_id == 'SISP') {
195
return IdentifiedFileType::PSP_PS1_PBP;
196
}
197
198
// Let's check if we got pointed to a PBP within such a directory.
199
// If so we just move up and return the directory itself as the game.
200
// If loading from memstick...
201
if (fileLoader->GetPath().FilePathContainsNoCase("PSP/GAME/")) {
202
return IdentifiedFileType::PSP_PBP_DIRECTORY;
203
}
204
return IdentifiedFileType::PSP_PBP;
205
} else if (extension == ".pbp") {
206
ERROR_LOG(Log::Loader, "A PBP with the wrong magic number?");
207
return IdentifiedFileType::PSP_PBP;
208
} else if (extension == ".bin") {
209
return IdentifiedFileType::UNKNOWN_BIN;
210
} else if (extension == ".zip") {
211
return IdentifiedFileType::ARCHIVE_ZIP;
212
} else if (extension == ".rar") {
213
return IdentifiedFileType::ARCHIVE_RAR;
214
} else if (extension == ".r00") {
215
return IdentifiedFileType::ARCHIVE_RAR;
216
} else if (extension == ".r01") {
217
return IdentifiedFileType::ARCHIVE_RAR;
218
} else if (extension == ".7z") {
219
return IdentifiedFileType::ARCHIVE_7Z;
220
}
221
return IdentifiedFileType::UNKNOWN;
222
}
223
224
FileLoader *ResolveFileLoaderTarget(FileLoader *fileLoader) {
225
std::string errorString;
226
IdentifiedFileType type = Identify_File(fileLoader, &errorString);
227
if (type == IdentifiedFileType::PSP_PBP_DIRECTORY) {
228
const Path ebootFilename = ResolvePBPFile(fileLoader->GetPath());
229
if (ebootFilename != fileLoader->GetPath()) {
230
// Switch fileLoader to the actual EBOOT.
231
delete fileLoader;
232
fileLoader = ConstructFileLoader(ebootFilename);
233
}
234
}
235
return fileLoader;
236
}
237
238
Path ResolvePBPDirectory(const Path &filename) {
239
if (filename.GetFilename() == "EBOOT.PBP") {
240
return filename.NavigateUp();
241
} else {
242
return filename;
243
}
244
}
245
246
Path ResolvePBPFile(const Path &filename) {
247
if (filename.GetFilename() != "EBOOT.PBP") {
248
return filename / "EBOOT.PBP";
249
} else {
250
return filename;
251
}
252
}
253
254
bool LoadFile(FileLoader **fileLoaderPtr, std::string *error_string) {
255
FileLoader *&fileLoader = *fileLoaderPtr;
256
IdentifiedFileType type = Identify_File(fileLoader, error_string);
257
switch (type) {
258
case IdentifiedFileType::PSP_PBP_DIRECTORY:
259
{
260
fileLoader = ResolveFileLoaderTarget(fileLoader);
261
if (fileLoader->Exists()) {
262
INFO_LOG(Log::Loader, "File is a PBP in a directory: %s", fileLoader->GetPath().c_str());
263
IdentifiedFileType ebootType = Identify_File(fileLoader, error_string);
264
if (ebootType == IdentifiedFileType::PSP_ISO_NP) {
265
InitMemoryForGameISO(fileLoader);
266
pspFileSystem.SetStartingDirectory("disc0:/PSP_GAME/USRDIR");
267
return Load_PSP_ISO(fileLoader, error_string);
268
}
269
else if (ebootType == IdentifiedFileType::PSP_PS1_PBP) {
270
*error_string = "PS1 EBOOTs are not supported by PPSSPP.";
271
coreState = CORE_BOOT_ERROR;
272
return false;
273
} else if (ebootType == IdentifiedFileType::ERROR_IDENTIFYING) {
274
// IdentifyFile will have written to errorString.
275
coreState = CORE_BOOT_ERROR;
276
return false;
277
}
278
279
std::string dir = fileLoader->GetPath().GetDirectory();
280
if (fileLoader->GetPath().Type() == PathType::CONTENT_URI) {
281
dir = AndroidContentURI(dir).FilePath();
282
}
283
size_t pos = dir.find("PSP/GAME/");
284
if (pos != std::string::npos) {
285
dir = ResolvePBPDirectory(Path(dir)).ToString();
286
pspFileSystem.SetStartingDirectory("ms0:/" + dir.substr(pos));
287
}
288
return Load_PSP_ELF_PBP(fileLoader, error_string);
289
} else {
290
*error_string = "No EBOOT.PBP, misidentified game";
291
coreState = CORE_BOOT_ERROR;
292
return false;
293
}
294
}
295
// Looks like a wrong fall through but is not, both paths are handled above.
296
297
case IdentifiedFileType::PSP_PBP:
298
case IdentifiedFileType::PSP_ELF:
299
{
300
INFO_LOG(Log::Loader, "File is an ELF or loose PBP! %s", fileLoader->GetPath().c_str());
301
return Load_PSP_ELF_PBP(fileLoader, error_string);
302
}
303
304
case IdentifiedFileType::PSP_ISO:
305
case IdentifiedFileType::PSP_ISO_NP:
306
case IdentifiedFileType::PSP_DISC_DIRECTORY: // behaves the same as the mounting is already done by now
307
pspFileSystem.SetStartingDirectory("disc0:/PSP_GAME/USRDIR");
308
return Load_PSP_ISO(fileLoader, error_string);
309
310
case IdentifiedFileType::PSP_PS1_PBP:
311
*error_string = "PS1 EBOOTs are not supported by PPSSPP.";
312
break;
313
314
case IdentifiedFileType::ARCHIVE_RAR:
315
#ifdef WIN32
316
*error_string = "RAR file detected (Require WINRAR)";
317
#else
318
*error_string = "RAR file detected (Require UnRAR)";
319
#endif
320
break;
321
322
case IdentifiedFileType::ARCHIVE_ZIP:
323
#ifdef WIN32
324
*error_string = "ZIP file detected (Require WINRAR)";
325
#else
326
*error_string = "ZIP file detected (Require UnRAR)";
327
#endif
328
break;
329
330
case IdentifiedFileType::ARCHIVE_7Z:
331
#ifdef WIN32
332
*error_string = "7z file detected (Require 7-Zip)";
333
#else
334
*error_string = "7z file detected (Require 7-Zip)";
335
#endif
336
break;
337
338
case IdentifiedFileType::ISO_MODE2:
339
*error_string = "PSX game image detected.";
340
break;
341
342
case IdentifiedFileType::NORMAL_DIRECTORY:
343
ERROR_LOG(Log::Loader, "Just a directory.");
344
*error_string = "Just a directory.";
345
break;
346
347
case IdentifiedFileType::PPSSPP_SAVESTATE:
348
*error_string = "This is a saved state, not a game."; // Actually, we could make it load it...
349
break;
350
351
case IdentifiedFileType::PSP_SAVEDATA_DIRECTORY:
352
*error_string = "This is save data, not a game."; // Actually, we could make it load it...
353
break;
354
355
case IdentifiedFileType::PPSSPP_GE_DUMP:
356
return Load_PSP_GE_Dump(fileLoader, error_string);
357
358
case IdentifiedFileType::UNKNOWN_BIN:
359
case IdentifiedFileType::UNKNOWN_ELF:
360
case IdentifiedFileType::UNKNOWN_ISO:
361
case IdentifiedFileType::UNKNOWN:
362
ERROR_LOG(Log::Loader, "Unknown file type: %s (%s)", fileLoader->GetPath().c_str(), error_string->c_str());
363
*error_string = "Unknown file type: " + fileLoader->GetPath().ToString();
364
break;
365
366
case IdentifiedFileType::ERROR_IDENTIFYING:
367
*error_string = *error_string + ": " + (fileLoader ? fileLoader->LatestError() : "");
368
ERROR_LOG(Log::Loader, "Error while identifying file: %s", error_string->c_str());
369
break;
370
371
default:
372
*error_string = StringFromFormat("Unhandled identified file type %d", (int)type);
373
ERROR_LOG(Log::Loader, "%s", error_string->c_str());
374
break;
375
}
376
377
coreState = CORE_BOOT_ERROR;
378
return false;
379
}
380
381
bool UmdReplace(const Path &filepath, FileLoader **fileLoader, std::string &error) {
382
IFileSystem *currentUMD = pspFileSystem.GetSystem("disc0:");
383
384
if (!currentUMD) {
385
error = "has no disc";
386
return false;
387
}
388
389
FileLoader *loadedFile = ConstructFileLoader(filepath);
390
391
if (!loadedFile->Exists()) {
392
error = loadedFile->GetPath().ToVisualString() + " doesn't exist";
393
delete loadedFile;
394
return false;
395
}
396
UpdateLoadedFile(loadedFile);
397
398
loadedFile = ResolveFileLoaderTarget(loadedFile);
399
400
*fileLoader = loadedFile;
401
402
std::string errorString;
403
IdentifiedFileType type = Identify_File(loadedFile, &errorString);
404
405
switch (type) {
406
case IdentifiedFileType::PSP_ISO:
407
case IdentifiedFileType::PSP_ISO_NP:
408
case IdentifiedFileType::PSP_DISC_DIRECTORY:
409
if (!ReInitMemoryForGameISO(loadedFile)) {
410
error = "reinit memory failed";
411
return false;
412
}
413
break;
414
default:
415
error = "Unsupported file type: " + std::to_string((int)type) + " " + errorString;
416
return false;
417
break;
418
}
419
return true;
420
}
421
422