2
1
Fork 0
mirror of https://github.com/yuzu-emu/yuzu.git synced 2024-07-04 23:31:19 +01:00

General Filesystem and Save Data Fixes (#670)

This commit is contained in:
Zach Hilman 2018-07-17 15:42:15 -04:00 committed by bunnei
parent 88a3140c9b
commit 69bfe075b5
16 changed files with 260 additions and 216 deletions

View file

@ -12,6 +12,8 @@ namespace ErrCodes {
enum { enum {
NotFound = 1, NotFound = 1,
SaveDataNotFound = 1002, SaveDataNotFound = 1002,
SdCardNotFound = 2001,
RomFSNotFound = 2520,
}; };
} }

View file

@ -167,35 +167,4 @@ public:
virtual ResultVal<EntryType> GetEntryType(const std::string& path) const = 0; virtual ResultVal<EntryType> GetEntryType(const std::string& path) const = 0;
}; };
class FileSystemFactory : NonCopyable {
public:
virtual ~FileSystemFactory() {}
/**
* Get a descriptive name for the archive (e.g. "RomFS", "SaveData", etc.)
*/
virtual std::string GetName() const = 0;
/**
* Tries to open the archive of this type with the specified path
* @param path Path to the archive
* @return An ArchiveBackend corresponding operating specified archive path.
*/
virtual ResultVal<std::unique_ptr<FileSystemBackend>> Open(const Path& path) = 0;
/**
* Deletes the archive contents and then re-creates the base folder
* @param path Path to the archive
* @return ResultCode of the operation, 0 on success
*/
virtual ResultCode Format(const Path& path) = 0;
/**
* Retrieves the format info about the archive with the specified path
* @param path Path to the archive
* @return Format information about the archive or error code
*/
virtual ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const = 0;
};
} // namespace FileSys } // namespace FileSys

View file

@ -11,28 +11,17 @@
namespace FileSys { namespace FileSys {
RomFS_Factory::RomFS_Factory(Loader::AppLoader& app_loader) { RomFSFactory::RomFSFactory(Loader::AppLoader& app_loader) {
// Load the RomFS from the app // Load the RomFS from the app
if (Loader::ResultStatus::Success != app_loader.ReadRomFS(romfs_file, data_offset, data_size)) { if (Loader::ResultStatus::Success != app_loader.ReadRomFS(romfs_file, data_offset, data_size)) {
LOG_ERROR(Service_FS, "Unable to read RomFS!"); LOG_ERROR(Service_FS, "Unable to read RomFS!");
} }
} }
ResultVal<std::unique_ptr<FileSystemBackend>> RomFS_Factory::Open(const Path& path) { ResultVal<std::unique_ptr<FileSystemBackend>> RomFSFactory::Open(u64 title_id) {
// TODO(DarkLordZach): Use title id.
auto archive = std::make_unique<RomFS_FileSystem>(romfs_file, data_offset, data_size); auto archive = std::make_unique<RomFS_FileSystem>(romfs_file, data_offset, data_size);
return MakeResult<std::unique_ptr<FileSystemBackend>>(std::move(archive)); return MakeResult<std::unique_ptr<FileSystemBackend>>(std::move(archive));
} }
ResultCode RomFS_Factory::Format(const Path& path) {
LOG_ERROR(Service_FS, "Unimplemented Format archive {}", GetName());
// TODO(bunnei): Find the right error code for this
return ResultCode(-1);
}
ResultVal<ArchiveFormatInfo> RomFS_Factory::GetFormatInfo(const Path& path) const {
LOG_ERROR(Service_FS, "Unimplemented GetFormatInfo archive {}", GetName());
// TODO(bunnei): Find the right error code for this
return ResultCode(-1);
}
} // namespace FileSys } // namespace FileSys

View file

@ -15,16 +15,11 @@
namespace FileSys { namespace FileSys {
/// File system interface to the RomFS archive /// File system interface to the RomFS archive
class RomFS_Factory final : public FileSystemFactory { class RomFSFactory {
public: public:
explicit RomFS_Factory(Loader::AppLoader& app_loader); explicit RomFSFactory(Loader::AppLoader& app_loader);
std::string GetName() const override { ResultVal<std::unique_ptr<FileSystemBackend>> Open(u64 title_id);
return "ArchiveFactory_RomFS";
}
ResultVal<std::unique_ptr<FileSystemBackend>> Open(const Path& path) override;
ResultCode Format(const Path& path) override;
ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override;
private: private:
std::shared_ptr<FileUtil::IOFile> romfs_file; std::shared_ptr<FileUtil::IOFile> romfs_file;

View file

@ -12,11 +12,49 @@
namespace FileSys { namespace FileSys {
SaveData_Factory::SaveData_Factory(std::string nand_directory) std::string SaveDataDescriptor::DebugInfo() {
return fmt::format("[type={:02X}, title_id={:016X}, user_id={:016X}{:016X}, save_id={:016X}]",
static_cast<u8>(type), title_id, user_id[1], user_id[0], save_id);
}
SaveDataFactory::SaveDataFactory(std::string nand_directory)
: nand_directory(std::move(nand_directory)) {} : nand_directory(std::move(nand_directory)) {}
ResultVal<std::unique_ptr<FileSystemBackend>> SaveData_Factory::Open(const Path& path) { ResultVal<std::unique_ptr<FileSystemBackend>> SaveDataFactory::Open(SaveDataSpaceId space,
std::string save_directory = GetFullPath(); SaveDataDescriptor meta) {
if (meta.type == SaveDataType::SystemSaveData || meta.type == SaveDataType::SaveData) {
if (meta.zero_1 != 0) {
LOG_WARNING(Service_FS,
"Possibly incorrect SaveDataDescriptor, type is "
"SystemSaveData||SaveData but offset 0x28 is non-zero ({:016X}).",
meta.zero_1);
}
if (meta.zero_2 != 0) {
LOG_WARNING(Service_FS,
"Possibly incorrect SaveDataDescriptor, type is "
"SystemSaveData||SaveData but offset 0x30 is non-zero ({:016X}).",
meta.zero_2);
}
if (meta.zero_3 != 0) {
LOG_WARNING(Service_FS,
"Possibly incorrect SaveDataDescriptor, type is "
"SystemSaveData||SaveData but offset 0x38 is non-zero ({:016X}).",
meta.zero_3);
}
}
if (meta.type == SaveDataType::SystemSaveData && meta.title_id != 0) {
LOG_WARNING(Service_FS,
"Possibly incorrect SaveDataDescriptor, type is SystemSaveData but title_id is "
"non-zero ({:016X}).",
meta.title_id);
}
std::string save_directory =
GetFullPath(space, meta.type, meta.title_id, meta.user_id, meta.save_id);
// TODO(DarkLordZach): Try to not create when opening, there are dedicated create save methods.
// But, user_ids don't match so this works for now.
if (!FileUtil::Exists(save_directory)) { if (!FileUtil::Exists(save_directory)) {
// TODO(bunnei): This is a work-around to always create a save data directory if it does not // TODO(bunnei): This is a work-around to always create a save data directory if it does not
@ -26,6 +64,12 @@ ResultVal<std::unique_ptr<FileSystemBackend>> SaveData_Factory::Open(const Path&
FileUtil::CreateFullPath(save_directory); FileUtil::CreateFullPath(save_directory);
} }
// TODO(DarkLordZach): For some reason, CreateFullPath doesn't create the last bit. Should be
// fixed with VFS.
if (!FileUtil::IsDirectory(save_directory)) {
FileUtil::CreateDir(save_directory);
}
// Return an error if the save data doesn't actually exist. // Return an error if the save data doesn't actually exist.
if (!FileUtil::IsDirectory(save_directory)) { if (!FileUtil::IsDirectory(save_directory)) {
// TODO(Subv): Find out correct error code. // TODO(Subv): Find out correct error code.
@ -36,28 +80,35 @@ ResultVal<std::unique_ptr<FileSystemBackend>> SaveData_Factory::Open(const Path&
return MakeResult<std::unique_ptr<FileSystemBackend>>(std::move(archive)); return MakeResult<std::unique_ptr<FileSystemBackend>>(std::move(archive));
} }
ResultCode SaveData_Factory::Format(const Path& path) { std::string SaveDataFactory::GetFullPath(SaveDataSpaceId space, SaveDataType type, u64 title_id,
LOG_WARNING(Service_FS, "Format archive {}", GetName()); u128 user_id, u64 save_id) const {
// Create the save data directory. // According to switchbrew, if a save is of type SaveData and the title id field is 0, it should
if (!FileUtil::CreateFullPath(GetFullPath())) { // be interpreted as the title id of the current process.
// TODO(Subv): Find the correct error code. if (type == SaveDataType::SaveData && title_id == 0)
return ResultCode(-1); title_id = Core::CurrentProcess()->program_id;
std::string prefix;
switch (space) {
case SaveDataSpaceId::NandSystem:
prefix = nand_directory + "system/save/";
break;
case SaveDataSpaceId::NandUser:
prefix = nand_directory + "user/save/";
break;
default:
ASSERT_MSG(false, "Unrecognized SaveDataSpaceId: {:02X}", static_cast<u8>(space));
} }
return RESULT_SUCCESS; switch (type) {
} case SaveDataType::SystemSaveData:
return fmt::format("{}{:016X}/{:016X}{:016X}", prefix, save_id, user_id[1], user_id[0]);
ResultVal<ArchiveFormatInfo> SaveData_Factory::GetFormatInfo(const Path& path) const { case SaveDataType::SaveData:
LOG_ERROR(Service_FS, "Unimplemented GetFormatInfo archive {}", GetName()); return fmt::format("{}{:016X}/{:016X}{:016X}/{:016X}", prefix, 0, user_id[1], user_id[0],
// TODO(bunnei): Find the right error code for this title_id);
return ResultCode(-1); default:
} ASSERT_MSG(false, "Unrecognized SaveDataType: {:02X}", static_cast<u8>(type));
}
std::string SaveData_Factory::GetFullPath() const {
u64 title_id = Core::CurrentProcess()->program_id;
// TODO(Subv): Somehow obtain this value.
u32 user = 0;
return fmt::format("{}save/{:016X}/{:08X}/", nand_directory, title_id, user);
} }
} // namespace FileSys } // namespace FileSys

View file

@ -12,22 +12,50 @@
namespace FileSys { namespace FileSys {
/// File system interface to the SaveData archive enum class SaveDataSpaceId : u8 {
class SaveData_Factory final : public FileSystemFactory { NandSystem = 0,
public: NandUser = 1,
explicit SaveData_Factory(std::string nand_directory); SdCard = 2,
TemporaryStorage = 3,
};
std::string GetName() const override { enum class SaveDataType : u8 {
return "SaveData_Factory"; SystemSaveData = 0,
} SaveData = 1,
ResultVal<std::unique_ptr<FileSystemBackend>> Open(const Path& path) override; BcatDeliveryCacheStorage = 2,
ResultCode Format(const Path& path) override; DeviceSaveData = 3,
ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override; TemporaryStorage = 4,
CacheStorage = 5,
};
struct SaveDataDescriptor {
u64_le title_id;
u128 user_id;
u64_le save_id;
SaveDataType type;
INSERT_PADDING_BYTES(7);
u64_le zero_1;
u64_le zero_2;
u64_le zero_3;
std::string DebugInfo();
};
static_assert(sizeof(SaveDataDescriptor) == 0x40, "SaveDataDescriptor has incorrect size.");
/// File system interface to the SaveData archive
class SaveDataFactory {
public:
explicit SaveDataFactory(std::string nand_directory);
ResultVal<std::unique_ptr<FileSystemBackend>> Open(SaveDataSpaceId space,
SaveDataDescriptor meta);
private: private:
std::string nand_directory; std::string nand_directory;
std::string sd_directory;
std::string GetFullPath() const; std::string GetFullPath(SaveDataSpaceId space, SaveDataType type, u64 title_id, u128 user_id,
u64 save_id) const;
}; };
} // namespace FileSys } // namespace FileSys

View file

@ -12,9 +12,9 @@
namespace FileSys { namespace FileSys {
SDMC_Factory::SDMC_Factory(std::string sd_directory) : sd_directory(std::move(sd_directory)) {} SDMCFactory::SDMCFactory(std::string sd_directory) : sd_directory(std::move(sd_directory)) {}
ResultVal<std::unique_ptr<FileSystemBackend>> SDMC_Factory::Open(const Path& path) { ResultVal<std::unique_ptr<FileSystemBackend>> SDMCFactory::Open() {
// Create the SD Card directory if it doesn't already exist. // Create the SD Card directory if it doesn't already exist.
if (!FileUtil::IsDirectory(sd_directory)) { if (!FileUtil::IsDirectory(sd_directory)) {
FileUtil::CreateFullPath(sd_directory); FileUtil::CreateFullPath(sd_directory);
@ -24,16 +24,4 @@ ResultVal<std::unique_ptr<FileSystemBackend>> SDMC_Factory::Open(const Path& pat
return MakeResult<std::unique_ptr<FileSystemBackend>>(std::move(archive)); return MakeResult<std::unique_ptr<FileSystemBackend>>(std::move(archive));
} }
ResultCode SDMC_Factory::Format(const Path& path) {
LOG_ERROR(Service_FS, "Unimplemented Format archive {}", GetName());
// TODO(Subv): Find the right error code for this
return ResultCode(-1);
}
ResultVal<ArchiveFormatInfo> SDMC_Factory::GetFormatInfo(const Path& path) const {
LOG_ERROR(Service_FS, "Unimplemented GetFormatInfo archive {}", GetName());
// TODO(bunnei): Find the right error code for this
return ResultCode(-1);
}
} // namespace FileSys } // namespace FileSys

View file

@ -13,16 +13,11 @@
namespace FileSys { namespace FileSys {
/// File system interface to the SDCard archive /// File system interface to the SDCard archive
class SDMC_Factory final : public FileSystemFactory { class SDMCFactory {
public: public:
explicit SDMC_Factory(std::string sd_directory); explicit SDMCFactory(std::string sd_directory);
std::string GetName() const override { ResultVal<std::unique_ptr<FileSystemBackend>> Open();
return "SDMC_Factory";
}
ResultVal<std::unique_ptr<FileSystemBackend>> Open(const Path& path) override;
ResultCode Format(const Path& path) override;
ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override;
private: private:
std::string sd_directory; std::string sd_directory;

View file

@ -4,9 +4,11 @@
#include <cinttypes> #include <cinttypes>
#include <stack> #include <stack>
#include "core/core.h"
#include "core/file_sys/filesystem.h" #include "core/file_sys/filesystem.h"
#include "core/hle/ipc_helpers.h" #include "core/hle/ipc_helpers.h"
#include "core/hle/kernel/event.h" #include "core/hle/kernel/event.h"
#include "core/hle/kernel/process.h"
#include "core/hle/service/am/am.h" #include "core/hle/service/am/am.h"
#include "core/hle/service/am/applet_ae.h" #include "core/hle/service/am/applet_ae.h"
#include "core/hle/service/am/applet_oe.h" #include "core/hle/service/am/applet_oe.h"
@ -614,25 +616,14 @@ void IApplicationFunctions::CreateApplicationAndRequestToStartForQuest(
void IApplicationFunctions::EnsureSaveData(Kernel::HLERequestContext& ctx) { void IApplicationFunctions::EnsureSaveData(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx}; IPC::RequestParser rp{ctx};
u128 uid = rp.PopRaw<u128>(); u128 uid = rp.PopRaw<u128>(); // What does this do?
LOG_WARNING(Service, "(STUBBED) called uid = {:016X}{:016X}", uid[1], uid[0]); LOG_WARNING(Service, "(STUBBED) called uid = {:016X}{:016X}", uid[1], uid[0]);
IPC::ResponseBuilder rb{ctx, 4}; IPC::ResponseBuilder rb{ctx, 4};
rb.Push(RESULT_SUCCESS);
FileSys::Path unused;
auto savedata = FileSystem::OpenFileSystem(FileSystem::Type::SaveData, unused);
if (savedata.Failed()) {
// Create the save data and return an error indicating that the operation was performed.
FileSystem::FormatFileSystem(FileSystem::Type::SaveData);
// TODO(Subv): Find out the correct error code for this.
rb.Push(ResultCode(ErrorModule::FS, 40));
} else {
rb.Push(RESULT_SUCCESS);
}
rb.Push<u64>(0); rb.Push<u64>(0);
} } // namespace Service::AM
void IApplicationFunctions::SetTerminateResult(Kernel::HLERequestContext& ctx) { void IApplicationFunctions::SetTerminateResult(Kernel::HLERequestContext& ctx) {
// Takes an input u32 Result, no output. // Takes an input u32 Result, no output.

View file

@ -4,6 +4,7 @@
#include <boost/container/flat_map.hpp> #include <boost/container/flat_map.hpp>
#include "common/file_util.h" #include "common/file_util.h"
#include "core/file_sys/errors.h"
#include "core/file_sys/filesystem.h" #include "core/file_sys/filesystem.h"
#include "core/file_sys/savedata_factory.h" #include "core/file_sys/savedata_factory.h"
#include "core/file_sys/sdmc_factory.h" #include "core/file_sys/sdmc_factory.h"
@ -16,57 +17,77 @@ namespace Service::FileSystem {
* Map of registered file systems, identified by type. Once an file system is registered here, it * Map of registered file systems, identified by type. Once an file system is registered here, it
* is never removed until UnregisterFileSystems is called. * is never removed until UnregisterFileSystems is called.
*/ */
static boost::container::flat_map<Type, std::unique_ptr<FileSys::FileSystemFactory>> filesystem_map; static std::unique_ptr<FileSys::RomFSFactory> romfs_factory;
static std::unique_ptr<FileSys::SaveDataFactory> save_data_factory;
static std::unique_ptr<FileSys::SDMCFactory> sdmc_factory;
ResultCode RegisterFileSystem(std::unique_ptr<FileSys::FileSystemFactory>&& factory, Type type) { ResultCode RegisterRomFS(std::unique_ptr<FileSys::RomFSFactory>&& factory) {
auto result = filesystem_map.emplace(type, std::move(factory)); ASSERT_MSG(romfs_factory == nullptr, "Tried to register a second RomFS");
romfs_factory = std::move(factory);
bool inserted = result.second; LOG_DEBUG(Service_FS, "Registered RomFS");
ASSERT_MSG(inserted, "Tried to register more than one system with same id code");
auto& filesystem = result.first->second;
LOG_DEBUG(Service_FS, "Registered file system {} with id code 0x{:08X}", filesystem->GetName(),
static_cast<u32>(type));
return RESULT_SUCCESS; return RESULT_SUCCESS;
} }
ResultVal<std::unique_ptr<FileSys::FileSystemBackend>> OpenFileSystem(Type type, ResultCode RegisterSaveData(std::unique_ptr<FileSys::SaveDataFactory>&& factory) {
FileSys::Path& path) { ASSERT_MSG(romfs_factory == nullptr, "Tried to register a second save data");
LOG_TRACE(Service_FS, "Opening FileSystem with type={}", static_cast<u32>(type)); save_data_factory = std::move(factory);
LOG_DEBUG(Service_FS, "Registered save data");
auto itr = filesystem_map.find(type); return RESULT_SUCCESS;
if (itr == filesystem_map.end()) {
// TODO(bunnei): Find a better error code for this
return ResultCode(-1);
}
return itr->second->Open(path);
} }
ResultCode FormatFileSystem(Type type) { ResultCode RegisterSDMC(std::unique_ptr<FileSys::SDMCFactory>&& factory) {
LOG_TRACE(Service_FS, "Formatting FileSystem with type={}", static_cast<u32>(type)); ASSERT_MSG(sdmc_factory == nullptr, "Tried to register a second SDMC");
sdmc_factory = std::move(factory);
LOG_DEBUG(Service_FS, "Registered SDMC");
return RESULT_SUCCESS;
}
auto itr = filesystem_map.find(type); ResultVal<std::unique_ptr<FileSys::FileSystemBackend>> OpenRomFS(u64 title_id) {
if (itr == filesystem_map.end()) { LOG_TRACE(Service_FS, "Opening RomFS for title_id={:016X}", title_id);
if (romfs_factory == nullptr) {
// TODO(bunnei): Find a better error code for this // TODO(bunnei): Find a better error code for this
return ResultCode(-1); return ResultCode(-1);
} }
FileSys::Path unused; return romfs_factory->Open(title_id);
return itr->second->Format(unused); }
ResultVal<std::unique_ptr<FileSys::FileSystemBackend>> OpenSaveData(
FileSys::SaveDataSpaceId space, FileSys::SaveDataDescriptor save_struct) {
LOG_TRACE(Service_FS, "Opening Save Data for space_id={:01X}, save_struct={}",
static_cast<u8>(space), SaveStructDebugInfo(save_struct));
if (save_data_factory == nullptr) {
return ResultCode(ErrorModule::FS, FileSys::ErrCodes::SaveDataNotFound);
}
return save_data_factory->Open(space, save_struct);
}
ResultVal<std::unique_ptr<FileSys::FileSystemBackend>> OpenSDMC() {
LOG_TRACE(Service_FS, "Opening SDMC");
if (sdmc_factory == nullptr) {
return ResultCode(ErrorModule::FS, FileSys::ErrCodes::SdCardNotFound);
}
return sdmc_factory->Open();
} }
void RegisterFileSystems() { void RegisterFileSystems() {
filesystem_map.clear(); romfs_factory = nullptr;
save_data_factory = nullptr;
sdmc_factory = nullptr;
std::string nand_directory = FileUtil::GetUserPath(D_NAND_IDX); std::string nand_directory = FileUtil::GetUserPath(D_NAND_IDX);
std::string sd_directory = FileUtil::GetUserPath(D_SDMC_IDX); std::string sd_directory = FileUtil::GetUserPath(D_SDMC_IDX);
auto savedata = std::make_unique<FileSys::SaveData_Factory>(std::move(nand_directory)); auto savedata = std::make_unique<FileSys::SaveDataFactory>(std::move(nand_directory));
RegisterFileSystem(std::move(savedata), Type::SaveData); save_data_factory = std::move(savedata);
auto sdcard = std::make_unique<FileSys::SDMC_Factory>(std::move(sd_directory)); auto sdcard = std::make_unique<FileSys::SDMCFactory>(std::move(sd_directory));
RegisterFileSystem(std::move(sdcard), Type::SDMC); sdmc_factory = std::move(sdcard);
} }
void InstallInterfaces(SM::ServiceManager& service_manager) { void InstallInterfaces(SM::ServiceManager& service_manager) {

View file

@ -6,12 +6,13 @@
#include <memory> #include <memory>
#include "common/common_types.h" #include "common/common_types.h"
#include "core/file_sys/romfs_factory.h"
#include "core/file_sys/savedata_factory.h"
#include "core/file_sys/sdmc_factory.h"
#include "core/hle/result.h" #include "core/hle/result.h"
namespace FileSys { namespace FileSys {
class FileSystemBackend; class FileSystemBackend;
class FileSystemFactory;
class Path;
} // namespace FileSys } // namespace FileSys
namespace Service { namespace Service {
@ -22,35 +23,20 @@ class ServiceManager;
namespace FileSystem { namespace FileSystem {
/// Supported FileSystem types ResultCode RegisterRomFS(std::unique_ptr<FileSys::RomFSFactory>&& factory);
enum class Type { ResultCode RegisterSaveData(std::unique_ptr<FileSys::SaveDataFactory>&& factory);
RomFS = 1, ResultCode RegisterSDMC(std::unique_ptr<FileSys::SDMCFactory>&& factory);
SaveData = 2,
SDMC = 3,
};
/** // TODO(DarkLordZach): BIS Filesystem
* Registers a FileSystem, instances of which can later be opened using its IdCode. // ResultCode RegisterBIS(std::unique_ptr<FileSys::BISFactory>&& factory);
* @param factory FileSystem backend interface to use
* @param type Type used to access this type of FileSystem
*/
ResultCode RegisterFileSystem(std::unique_ptr<FileSys::FileSystemFactory>&& factory, Type type);
/** ResultVal<std::unique_ptr<FileSys::FileSystemBackend>> OpenRomFS(u64 title_id);
* Opens a file system ResultVal<std::unique_ptr<FileSys::FileSystemBackend>> OpenSaveData(
* @param type Type of the file system to open FileSys::SaveDataSpaceId space, FileSys::SaveDataDescriptor save_struct);
* @param path Path to the file system, used with Binary paths ResultVal<std::unique_ptr<FileSys::FileSystemBackend>> OpenSDMC();
* @return FileSys::FileSystemBackend interface to the file system
*/
ResultVal<std::unique_ptr<FileSys::FileSystemBackend>> OpenFileSystem(Type type,
FileSys::Path& path);
/** // TODO(DarkLordZach): BIS Filesystem
* Formats a file system // ResultVal<std::unique_ptr<FileSys::FileSystemBackend>> OpenBIS();
* @param type Type of the file system to format
* @return ResultCode of the operation
*/
ResultCode FormatFileSystem(Type type);
/// Registers all Filesystem services with the specified service manager. /// Registers all Filesystem services with the specified service manager.
void InstallInterfaces(SM::ServiceManager& service_manager); void InstallInterfaces(SM::ServiceManager& service_manager);

View file

@ -13,11 +13,21 @@
#include "core/hle/ipc_helpers.h" #include "core/hle/ipc_helpers.h"
#include "core/hle/kernel/client_port.h" #include "core/hle/kernel/client_port.h"
#include "core/hle/kernel/client_session.h" #include "core/hle/kernel/client_session.h"
#include "core/hle/kernel/process.h"
#include "core/hle/service/filesystem/filesystem.h" #include "core/hle/service/filesystem/filesystem.h"
#include "core/hle/service/filesystem/fsp_srv.h" #include "core/hle/service/filesystem/fsp_srv.h"
namespace Service::FileSystem { namespace Service::FileSystem {
enum class StorageId : u8 {
None = 0,
Host = 1,
GameCard = 2,
NandSystem = 3,
NandUser = 4,
SdCard = 5
};
class IStorage final : public ServiceFramework<IStorage> { class IStorage final : public ServiceFramework<IStorage> {
public: public:
IStorage(std::unique_ptr<FileSys::StorageBackend>&& backend) IStorage(std::unique_ptr<FileSys::StorageBackend>&& backend)
@ -487,17 +497,6 @@ FSP_SRV::FSP_SRV() : ServiceFramework("fsp-srv") {
RegisterHandlers(functions); RegisterHandlers(functions);
} }
void FSP_SRV::TryLoadRomFS() {
if (romfs) {
return;
}
FileSys::Path unused;
auto res = OpenFileSystem(Type::RomFS, unused);
if (res.Succeeded()) {
romfs = std::move(res.Unwrap());
}
}
void FSP_SRV::Initialize(Kernel::HLERequestContext& ctx) { void FSP_SRV::Initialize(Kernel::HLERequestContext& ctx) {
LOG_WARNING(Service_FS, "(STUBBED) called"); LOG_WARNING(Service_FS, "(STUBBED) called");
@ -508,8 +507,7 @@ void FSP_SRV::Initialize(Kernel::HLERequestContext& ctx) {
void FSP_SRV::MountSdCard(Kernel::HLERequestContext& ctx) { void FSP_SRV::MountSdCard(Kernel::HLERequestContext& ctx) {
LOG_DEBUG(Service_FS, "called"); LOG_DEBUG(Service_FS, "called");
FileSys::Path unused; IFileSystem filesystem(OpenSDMC().Unwrap());
auto filesystem = OpenFileSystem(Type::SDMC, unused).Unwrap();
IPC::ResponseBuilder rb{ctx, 2, 0, 1}; IPC::ResponseBuilder rb{ctx, 2, 0, 1};
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
@ -519,23 +517,26 @@ void FSP_SRV::MountSdCard(Kernel::HLERequestContext& ctx) {
void FSP_SRV::CreateSaveData(Kernel::HLERequestContext& ctx) { void FSP_SRV::CreateSaveData(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx}; IPC::RequestParser rp{ctx};
auto save_struct = rp.PopRaw<std::array<u8, 0x40>>(); auto save_struct = rp.PopRaw<FileSys::SaveDataDescriptor>();
auto save_create_struct = rp.PopRaw<std::array<u8, 0x40>>(); auto save_create_struct = rp.PopRaw<std::array<u8, 0x40>>();
u128 uid = rp.PopRaw<u128>(); u128 uid = rp.PopRaw<u128>();
LOG_WARNING(Service_FS, "(STUBBED) called uid = {:016X}{:016X}", uid[1], uid[0]); LOG_WARNING(Service_FS, "(STUBBED) called save_struct = {}, uid = {:016X}{:016X}",
save_struct.DebugInfo(), uid[1], uid[0]);
IPC::ResponseBuilder rb{ctx, 2}; IPC::ResponseBuilder rb{ctx, 2};
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
} }
void FSP_SRV::MountSaveData(Kernel::HLERequestContext& ctx) { void FSP_SRV::MountSaveData(Kernel::HLERequestContext& ctx) {
LOG_WARNING(Service_FS, "(STUBBED) called"); IPC::RequestParser rp{ctx};
// TODO(Subv): Read the input parameters and mount the requested savedata instead of always auto space_id = rp.PopRaw<FileSys::SaveDataSpaceId>();
// mounting the current process' savedata. auto unk = rp.Pop<u32>();
FileSys::Path unused; LOG_INFO(Service_FS, "called with unknown={:08X}", unk);
auto filesystem = OpenFileSystem(Type::SaveData, unused); auto save_struct = rp.PopRaw<FileSys::SaveDataDescriptor>();
auto filesystem = OpenSaveData(space_id, save_struct);
if (filesystem.Failed()) { if (filesystem.Failed()) {
IPC::ResponseBuilder rb{ctx, 2, 0, 0}; IPC::ResponseBuilder rb{ctx, 2, 0, 0};
@ -559,8 +560,8 @@ void FSP_SRV::GetGlobalAccessLogMode(Kernel::HLERequestContext& ctx) {
void FSP_SRV::OpenDataStorageByCurrentProcess(Kernel::HLERequestContext& ctx) { void FSP_SRV::OpenDataStorageByCurrentProcess(Kernel::HLERequestContext& ctx) {
LOG_DEBUG(Service_FS, "called"); LOG_DEBUG(Service_FS, "called");
TryLoadRomFS(); auto romfs = OpenRomFS(Core::System::GetInstance().CurrentProcess()->program_id);
if (!romfs) { if (romfs.Failed()) {
// TODO (bunnei): Find the right error code to use here // TODO (bunnei): Find the right error code to use here
LOG_CRITICAL(Service_FS, "no file system interface available!"); LOG_CRITICAL(Service_FS, "no file system interface available!");
IPC::ResponseBuilder rb{ctx, 2}; IPC::ResponseBuilder rb{ctx, 2};
@ -568,8 +569,8 @@ void FSP_SRV::OpenDataStorageByCurrentProcess(Kernel::HLERequestContext& ctx) {
return; return;
} }
// Attempt to open a StorageBackend interface to the RomFS auto storage = romfs.Unwrap()->OpenFile({}, {});
auto storage = romfs->OpenFile({}, {});
if (storage.Failed()) { if (storage.Failed()) {
LOG_CRITICAL(Service_FS, "no storage interface available!"); LOG_CRITICAL(Service_FS, "no storage interface available!");
IPC::ResponseBuilder rb{ctx, 2}; IPC::ResponseBuilder rb{ctx, 2};
@ -583,8 +584,40 @@ void FSP_SRV::OpenDataStorageByCurrentProcess(Kernel::HLERequestContext& ctx) {
} }
void FSP_SRV::OpenRomStorage(Kernel::HLERequestContext& ctx) { void FSP_SRV::OpenRomStorage(Kernel::HLERequestContext& ctx) {
LOG_WARNING(Service_FS, "(STUBBED) called, using OpenDataStorageByCurrentProcess"); IPC::RequestParser rp{ctx};
OpenDataStorageByCurrentProcess(ctx);
auto storage_id = rp.PopRaw<StorageId>();
auto title_id = rp.PopRaw<u64>();
LOG_DEBUG(Service_FS, "called with storage_id={:02X}, title_id={:016X}",
static_cast<u8>(storage_id), title_id);
if (title_id != Core::System::GetInstance().CurrentProcess()->program_id) {
LOG_CRITICAL(
Service_FS,
"Attempting to access RomFS of another title id (current={:016X}, requested={:016X}).",
Core::System::GetInstance().CurrentProcess()->program_id, title_id);
}
auto romfs = OpenRomFS(title_id);
if (romfs.Failed()) {
LOG_CRITICAL(Service_FS, "no file system interface available!");
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ResultCode(ErrorModule::FS, FileSys::ErrCodes::RomFSNotFound));
return;
}
auto storage = romfs.Unwrap()->OpenFile({}, {});
if (storage.Failed()) {
LOG_CRITICAL(Service_FS, "no storage interface available!");
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(storage.Code());
return;
}
IPC::ResponseBuilder rb{ctx, 2, 0, 1};
rb.Push(RESULT_SUCCESS);
rb.PushIpcInterface<IStorage>(std::move(storage.Unwrap()));
} }
} // namespace Service::FileSystem } // namespace Service::FileSystem

View file

@ -19,8 +19,6 @@ public:
~FSP_SRV() = default; ~FSP_SRV() = default;
private: private:
void TryLoadRomFS();
void Initialize(Kernel::HLERequestContext& ctx); void Initialize(Kernel::HLERequestContext& ctx);
void MountSdCard(Kernel::HLERequestContext& ctx); void MountSdCard(Kernel::HLERequestContext& ctx);
void CreateSaveData(Kernel::HLERequestContext& ctx); void CreateSaveData(Kernel::HLERequestContext& ctx);

View file

@ -154,8 +154,7 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load(
// Register the RomFS if a ".romfs" file was found // Register the RomFS if a ".romfs" file was found
if (!filepath_romfs.empty()) { if (!filepath_romfs.empty()) {
Service::FileSystem::RegisterFileSystem(std::make_unique<FileSys::RomFS_Factory>(*this), Service::FileSystem::RegisterRomFS(std::make_unique<FileSys::RomFSFactory>(*this));
Service::FileSystem::Type::RomFS);
} }
is_loaded = true; is_loaded = true;

View file

@ -277,8 +277,7 @@ ResultStatus AppLoader_NCA::Load(Kernel::SharedPtr<Kernel::Process>& process) {
metadata.GetMainThreadStackSize()); metadata.GetMainThreadStackSize());
if (nca->GetRomFsSize() > 0) if (nca->GetRomFsSize() > 0)
Service::FileSystem::RegisterFileSystem(std::make_unique<FileSys::RomFS_Factory>(*this), Service::FileSystem::RegisterRomFS(std::make_unique<FileSys::RomFSFactory>(*this));
Service::FileSystem::Type::RomFS);
is_loaded = true; is_loaded = true;
return ResultStatus::Success; return ResultStatus::Success;

View file

@ -115,7 +115,7 @@ VAddr AppLoader_NSO::LoadModule(const std::string& name, const std::vector<u8>&
std::vector<u8> program_image; std::vector<u8> program_image;
for (int i = 0; i < nso_header.segments.size(); ++i) { for (int i = 0; i < nso_header.segments.size(); ++i) {
std::vector<u8> compressed_data(nso_header.segments_compressed_size[i]); std::vector<u8> compressed_data(nso_header.segments_compressed_size[i]);
for (int j = 0; j < nso_header.segments_compressed_size[i]; ++j) for (auto j = 0; j < nso_header.segments_compressed_size[i]; ++j)
compressed_data[j] = file_data[nso_header.segments[i].offset + j]; compressed_data[j] = file_data[nso_header.segments[i].offset + j];
std::vector<u8> data = DecompressSegment(compressed_data, nso_header.segments[i]); std::vector<u8> data = DecompressSegment(compressed_data, nso_header.segments[i]);
program_image.resize(nso_header.segments[i].location); program_image.resize(nso_header.segments[i].location);