CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Core/Loaders.cpp
Views: 1401
// Copyright (c) 2012- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include <algorithm>18#include <cstdio>1920#include "Common/File/AndroidContentURI.h"21#include "Common/File/FileUtil.h"22#include "Common/File/Path.h"23#include "Common/StringUtils.h"24#include "Core/FileLoaders/CachingFileLoader.h"25#include "Core/FileLoaders/DiskCachingFileLoader.h"26#include "Core/FileLoaders/HTTPFileLoader.h"27#include "Core/FileLoaders/LocalFileLoader.h"28#include "Core/FileLoaders/RetryingFileLoader.h"29#include "Core/FileSystems/MetaFileSystem.h"30#include "Core/PSPLoaders.h"31#include "Core/MemMap.h"32#include "Core/Loaders.h"33#include "Core/System.h"34#include "Core/ELF/PBPReader.h"35#include "Core/ELF/ParamSFO.h"3637FileLoader *ConstructFileLoader(const Path &filename) {38if (filename.Type() == PathType::HTTP) {39FileLoader *baseLoader = new RetryingFileLoader(new HTTPFileLoader(filename));40// For headless, avoid disk caching since it's usually used for tests that might mutate.41if (!PSP_CoreParameter().headLess) {42baseLoader = new DiskCachingFileLoader(baseLoader);43}44return new CachingFileLoader(baseLoader);45}46return new LocalFileLoader(filename);47}4849// TODO : improve, look in the file more50IdentifiedFileType Identify_File(FileLoader *fileLoader, std::string *errorString) {51errorString->clear();52if (fileLoader == nullptr) {53*errorString = "Invalid fileLoader";54return IdentifiedFileType::ERROR_IDENTIFYING;55}56if (fileLoader->GetPath().size() == 0) {57*errorString = "Invalid filename " + fileLoader->GetPath().ToString();58return IdentifiedFileType::ERROR_IDENTIFYING;59}6061if (!fileLoader->Exists()) {62*errorString = "IdentifyFile: File doesn't exist: " + fileLoader->GetPath().ToString();63return IdentifiedFileType::ERROR_IDENTIFYING;64}6566std::string extension = fileLoader->GetPath().GetFileExtension();67if (extension == ".iso") {68// may be a psx iso, they have 2352 byte sectors. You never know what some people try to open69if ((fileLoader->FileSize() % 2352) == 0) {70unsigned char sync[12];71fileLoader->ReadAt(0, 12, sync);7273// each sector in a mode2 image starts with these 12 bytes74if (memcmp(sync,"\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00", 12) == 0) {75*errorString = "ISO in Mode 2: Not a PSP game";76return IdentifiedFileType::ISO_MODE2;77}7879// 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.80}81return IdentifiedFileType::PSP_ISO;82} else if (extension == ".cso" || extension == ".chd") {83return IdentifiedFileType::PSP_ISO;84} else if (extension == ".ppst") {85return IdentifiedFileType::PPSSPP_SAVESTATE;86} else if (extension == ".ppdmp") {87char data[8]{};88fileLoader->ReadAt(0, 8, data);89if (memcmp(data, "PPSSPPGE", 8) == 0) {90return IdentifiedFileType::PPSSPP_GE_DUMP;91}92}9394// First, check if it's a directory with an EBOOT.PBP in it.95if (fileLoader->IsDirectory()) {96Path filename = fileLoader->GetPath();97if (filename.size() > 4) {98// Check for existence of EBOOT.PBP, as required for "Directory games".99if (File::Exists(filename / "EBOOT.PBP")) {100return IdentifiedFileType::PSP_PBP_DIRECTORY;101}102103// check if it's a disc directory104if (File::Exists(filename / "PSP_GAME")) {105return IdentifiedFileType::PSP_DISC_DIRECTORY;106}107108// Not that, okay, let's guess it's a savedata directory if it has a param.sfo...109if (File::Exists(filename / "PARAM.SFO")) {110return IdentifiedFileType::PSP_SAVEDATA_DIRECTORY;111}112}113114return IdentifiedFileType::NORMAL_DIRECTORY;115}116117// OK, quick methods of identification for common types failed. Moving on to more expensive methods,118// starting by reading the first few bytes.119// This can be necessary for weird Android content storage path types, see issue #17462120121u32_le id;122123size_t readSize = fileLoader->ReadAt(0, 4, 1, &id);124if (readSize != 1) {125*errorString = "Failed to read identification bytes";126return IdentifiedFileType::ERROR_IDENTIFYING;127}128129u32_le psar_offset = 0, psar_id = 0;130u32 _id = id;131if (!memcmp(&_id, "PK\x03\x04", 4) || !memcmp(&_id, "PK\x05\x06", 4) || !memcmp(&_id, "PK\x07\x08", 4)) {132return IdentifiedFileType::ARCHIVE_ZIP;133} else if (!memcmp(&_id, "\x00PBP", 4)) {134fileLoader->ReadAt(0x24, 4, 1, &psar_offset);135fileLoader->ReadAt(psar_offset, 4, 1, &psar_id);136// Fall through to the below if chain.137} else if (!memcmp(&_id, "Rar!", 4)) {138return IdentifiedFileType::ARCHIVE_RAR;139} else if (!memcmp(&_id, "\x37\x7A\xBC\xAF", 4)) {140return IdentifiedFileType::ARCHIVE_7Z;141} else if (!memcmp(&_id, "\0\0\0\0", 4)) {142// All zeroes. ISO files start like this but their 16th 2048-byte sector contains metadata.143if (fileLoader->FileSize() > 0x8100) {144char buffer[16];145fileLoader->ReadAt(0x8000, sizeof(buffer), buffer);146if (!memcmp(buffer + 1, "CD001", 5)) {147// It's an ISO file.148if (!memcmp(buffer + 8, "PSP GAME", 8)) {149return IdentifiedFileType::PSP_ISO;150}151return IdentifiedFileType::UNKNOWN_ISO;152}153}154} else if (!memcmp(&_id, "CISO", 4)) {155// CISO are not used for many other kinds of ISO so let's just guess it's a PSP one and let it156// fail later...157return IdentifiedFileType::PSP_ISO;158} else if (!memcmp(&_id, "MCom", 4)) {159size_t readSize = fileLoader->ReadAt(4, 4, 1, &_id);160if (!memcmp(&_id, "prHD", 4)) {161return IdentifiedFileType::PSP_ISO; // CHD file162}163}164165if (id == 'FLE\x7F') {166Path filename = fileLoader->GetPath();167// There are a few elfs misnamed as pbp (like Trig Wars), accept that. Also accept extension-less paths.168if (extension == ".plf" || strstr(filename.GetFilename().c_str(), "BOOT.BIN") ||169extension == ".elf" || extension == ".prx" || extension == ".pbp" || extension == "") {170return IdentifiedFileType::PSP_ELF;171}172return IdentifiedFileType::UNKNOWN_ELF;173} else if (id == 'PBP\x00') {174// Do this PS1 eboot check FIRST before checking other eboot types.175// It seems like some are malformed and slip through the PSAR check below.176PBPReader pbp(fileLoader);177if (pbp.IsValid() && !pbp.IsELF()) {178std::vector<u8> sfoData;179if (pbp.GetSubFile(PBP_PARAM_SFO, &sfoData)) {180ParamSFOData paramSFO;181paramSFO.ReadSFO(sfoData);182// PS1 Eboots are supposed to use "ME" as their PARAM SFO category.183// If they don't, and they're still malformed (e.g. PSISOIMG0000 isn't found), there's nothing we can do.184if (paramSFO.GetValueString("CATEGORY") == "ME")185return IdentifiedFileType::PSP_PS1_PBP;186}187}188189if (psar_id == 'MUPN') {190return IdentifiedFileType::PSP_ISO_NP;191}192// PS1 PSAR begins with "PSISOIMG0000"193if (psar_id == 'SISP') {194return IdentifiedFileType::PSP_PS1_PBP;195}196197// Let's check if we got pointed to a PBP within such a directory.198// If so we just move up and return the directory itself as the game.199// If loading from memstick...200if (fileLoader->GetPath().FilePathContainsNoCase("PSP/GAME/")) {201return IdentifiedFileType::PSP_PBP_DIRECTORY;202}203return IdentifiedFileType::PSP_PBP;204} else if (extension == ".pbp") {205ERROR_LOG(Log::Loader, "A PBP with the wrong magic number?");206return IdentifiedFileType::PSP_PBP;207} else if (extension == ".bin") {208return IdentifiedFileType::UNKNOWN_BIN;209} else if (extension == ".zip") {210return IdentifiedFileType::ARCHIVE_ZIP;211} else if (extension == ".rar") {212return IdentifiedFileType::ARCHIVE_RAR;213} else if (extension == ".r00") {214return IdentifiedFileType::ARCHIVE_RAR;215} else if (extension == ".r01") {216return IdentifiedFileType::ARCHIVE_RAR;217} else if (extension == ".7z") {218return IdentifiedFileType::ARCHIVE_7Z;219}220return IdentifiedFileType::UNKNOWN;221}222223FileLoader *ResolveFileLoaderTarget(FileLoader *fileLoader) {224std::string errorString;225IdentifiedFileType type = Identify_File(fileLoader, &errorString);226if (type == IdentifiedFileType::PSP_PBP_DIRECTORY) {227const Path ebootFilename = ResolvePBPFile(fileLoader->GetPath());228if (ebootFilename != fileLoader->GetPath()) {229// Switch fileLoader to the actual EBOOT.230delete fileLoader;231fileLoader = ConstructFileLoader(ebootFilename);232}233}234return fileLoader;235}236237Path ResolvePBPDirectory(const Path &filename) {238if (filename.GetFilename() == "EBOOT.PBP") {239return filename.NavigateUp();240} else {241return filename;242}243}244245Path ResolvePBPFile(const Path &filename) {246if (filename.GetFilename() != "EBOOT.PBP") {247return filename / "EBOOT.PBP";248} else {249return filename;250}251}252253bool LoadFile(FileLoader **fileLoaderPtr, std::string *error_string) {254FileLoader *&fileLoader = *fileLoaderPtr;255IdentifiedFileType type = Identify_File(fileLoader, error_string);256switch (type) {257case IdentifiedFileType::PSP_PBP_DIRECTORY:258{259fileLoader = ResolveFileLoaderTarget(fileLoader);260if (fileLoader->Exists()) {261INFO_LOG(Log::Loader, "File is a PBP in a directory: %s", fileLoader->GetPath().c_str());262IdentifiedFileType ebootType = Identify_File(fileLoader, error_string);263if (ebootType == IdentifiedFileType::PSP_ISO_NP) {264InitMemoryForGameISO(fileLoader);265pspFileSystem.SetStartingDirectory("disc0:/PSP_GAME/USRDIR");266return Load_PSP_ISO(fileLoader, error_string);267}268else if (ebootType == IdentifiedFileType::PSP_PS1_PBP) {269*error_string = "PS1 EBOOTs are not supported by PPSSPP.";270coreState = CORE_BOOT_ERROR;271return false;272} else if (ebootType == IdentifiedFileType::ERROR_IDENTIFYING) {273// IdentifyFile will have written to errorString.274coreState = CORE_BOOT_ERROR;275return false;276}277278std::string dir = fileLoader->GetPath().GetDirectory();279if (fileLoader->GetPath().Type() == PathType::CONTENT_URI) {280dir = AndroidContentURI(dir).FilePath();281}282size_t pos = dir.find("PSP/GAME/");283if (pos != std::string::npos) {284dir = ResolvePBPDirectory(Path(dir)).ToString();285pspFileSystem.SetStartingDirectory("ms0:/" + dir.substr(pos));286}287return Load_PSP_ELF_PBP(fileLoader, error_string);288} else {289*error_string = "No EBOOT.PBP, misidentified game";290coreState = CORE_BOOT_ERROR;291return false;292}293}294// Looks like a wrong fall through but is not, both paths are handled above.295296case IdentifiedFileType::PSP_PBP:297case IdentifiedFileType::PSP_ELF:298{299INFO_LOG(Log::Loader, "File is an ELF or loose PBP! %s", fileLoader->GetPath().c_str());300return Load_PSP_ELF_PBP(fileLoader, error_string);301}302303case IdentifiedFileType::PSP_ISO:304case IdentifiedFileType::PSP_ISO_NP:305case IdentifiedFileType::PSP_DISC_DIRECTORY: // behaves the same as the mounting is already done by now306pspFileSystem.SetStartingDirectory("disc0:/PSP_GAME/USRDIR");307return Load_PSP_ISO(fileLoader, error_string);308309case IdentifiedFileType::PSP_PS1_PBP:310*error_string = "PS1 EBOOTs are not supported by PPSSPP.";311break;312313case IdentifiedFileType::ARCHIVE_RAR:314#ifdef WIN32315*error_string = "RAR file detected (Require WINRAR)";316#else317*error_string = "RAR file detected (Require UnRAR)";318#endif319break;320321case IdentifiedFileType::ARCHIVE_ZIP:322#ifdef WIN32323*error_string = "ZIP file detected (Require WINRAR)";324#else325*error_string = "ZIP file detected (Require UnRAR)";326#endif327break;328329case IdentifiedFileType::ARCHIVE_7Z:330#ifdef WIN32331*error_string = "7z file detected (Require 7-Zip)";332#else333*error_string = "7z file detected (Require 7-Zip)";334#endif335break;336337case IdentifiedFileType::ISO_MODE2:338*error_string = "PSX game image detected.";339break;340341case IdentifiedFileType::NORMAL_DIRECTORY:342ERROR_LOG(Log::Loader, "Just a directory.");343*error_string = "Just a directory.";344break;345346case IdentifiedFileType::PPSSPP_SAVESTATE:347*error_string = "This is a saved state, not a game."; // Actually, we could make it load it...348break;349350case IdentifiedFileType::PSP_SAVEDATA_DIRECTORY:351*error_string = "This is save data, not a game."; // Actually, we could make it load it...352break;353354case IdentifiedFileType::PPSSPP_GE_DUMP:355return Load_PSP_GE_Dump(fileLoader, error_string);356357case IdentifiedFileType::UNKNOWN_BIN:358case IdentifiedFileType::UNKNOWN_ELF:359case IdentifiedFileType::UNKNOWN_ISO:360case IdentifiedFileType::UNKNOWN:361ERROR_LOG(Log::Loader, "Unknown file type: %s (%s)", fileLoader->GetPath().c_str(), error_string->c_str());362*error_string = "Unknown file type: " + fileLoader->GetPath().ToString();363break;364365case IdentifiedFileType::ERROR_IDENTIFYING:366*error_string = *error_string + ": " + (fileLoader ? fileLoader->LatestError() : "");367ERROR_LOG(Log::Loader, "Error while identifying file: %s", error_string->c_str());368break;369370default:371*error_string = StringFromFormat("Unhandled identified file type %d", (int)type);372ERROR_LOG(Log::Loader, "%s", error_string->c_str());373break;374}375376coreState = CORE_BOOT_ERROR;377return false;378}379380bool UmdReplace(const Path &filepath, FileLoader **fileLoader, std::string &error) {381IFileSystem *currentUMD = pspFileSystem.GetSystem("disc0:");382383if (!currentUMD) {384error = "has no disc";385return false;386}387388FileLoader *loadedFile = ConstructFileLoader(filepath);389390if (!loadedFile->Exists()) {391error = loadedFile->GetPath().ToVisualString() + " doesn't exist";392delete loadedFile;393return false;394}395UpdateLoadedFile(loadedFile);396397loadedFile = ResolveFileLoaderTarget(loadedFile);398399*fileLoader = loadedFile;400401std::string errorString;402IdentifiedFileType type = Identify_File(loadedFile, &errorString);403404switch (type) {405case IdentifiedFileType::PSP_ISO:406case IdentifiedFileType::PSP_ISO_NP:407case IdentifiedFileType::PSP_DISC_DIRECTORY:408if (!ReInitMemoryForGameISO(loadedFile)) {409error = "reinit memory failed";410return false;411}412break;413default:414error = "Unsupported file type: " + std::to_string((int)type) + " " + errorString;415return false;416break;417}418return true;419}420421422