mirror of
				https://git.suyu.dev/suyu/suyu
				synced 2025-11-04 00:49:02 -06:00 
			
		
		
		
	Merge pull request #216 from Subv/savedata
Implemented the SaveData archive and MountSaveData.
This commit is contained in:
		@@ -20,12 +20,9 @@ namespace Kernel {
 | 
			
		||||
// Lists all processes that exist in the current session.
 | 
			
		||||
static std::vector<SharedPtr<Process>> process_list;
 | 
			
		||||
 | 
			
		||||
SharedPtr<CodeSet> CodeSet::Create(std::string name, u64 program_id) {
 | 
			
		||||
SharedPtr<CodeSet> CodeSet::Create(std::string name) {
 | 
			
		||||
    SharedPtr<CodeSet> codeset(new CodeSet);
 | 
			
		||||
 | 
			
		||||
    codeset->name = std::move(name);
 | 
			
		||||
    codeset->program_id = program_id;
 | 
			
		||||
 | 
			
		||||
    return codeset;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -34,13 +31,14 @@ CodeSet::~CodeSet() {}
 | 
			
		||||
 | 
			
		||||
u32 Process::next_process_id;
 | 
			
		||||
 | 
			
		||||
SharedPtr<Process> Process::Create(std::string&& name) {
 | 
			
		||||
SharedPtr<Process> Process::Create(std::string&& name, u64 program_id) {
 | 
			
		||||
    SharedPtr<Process> process(new Process);
 | 
			
		||||
 | 
			
		||||
    process->name = std::move(name);
 | 
			
		||||
    process->flags.raw = 0;
 | 
			
		||||
    process->flags.memory_region.Assign(MemoryRegion::APPLICATION);
 | 
			
		||||
    process->status = ProcessStatus::Created;
 | 
			
		||||
    process->program_id = program_id;
 | 
			
		||||
 | 
			
		||||
    process_list.push_back(process);
 | 
			
		||||
    return process;
 | 
			
		||||
 
 | 
			
		||||
@@ -56,7 +56,7 @@ class ResourceLimit;
 | 
			
		||||
struct MemoryRegionInfo;
 | 
			
		||||
 | 
			
		||||
struct CodeSet final : public Object {
 | 
			
		||||
    static SharedPtr<CodeSet> Create(std::string name, u64 program_id);
 | 
			
		||||
    static SharedPtr<CodeSet> Create(std::string name);
 | 
			
		||||
 | 
			
		||||
    std::string GetTypeName() const override {
 | 
			
		||||
        return "CodeSet";
 | 
			
		||||
@@ -72,8 +72,6 @@ struct CodeSet final : public Object {
 | 
			
		||||
 | 
			
		||||
    /// Name of the process
 | 
			
		||||
    std::string name;
 | 
			
		||||
    /// Title ID corresponding to the process
 | 
			
		||||
    u64 program_id;
 | 
			
		||||
 | 
			
		||||
    std::shared_ptr<std::vector<u8>> memory;
 | 
			
		||||
 | 
			
		||||
@@ -97,7 +95,7 @@ private:
 | 
			
		||||
 | 
			
		||||
class Process final : public Object {
 | 
			
		||||
public:
 | 
			
		||||
    static SharedPtr<Process> Create(std::string&& name);
 | 
			
		||||
    static SharedPtr<Process> Create(std::string&& name, u64 program_id);
 | 
			
		||||
 | 
			
		||||
    std::string GetTypeName() const override {
 | 
			
		||||
        return "Process";
 | 
			
		||||
@@ -113,6 +111,9 @@ public:
 | 
			
		||||
 | 
			
		||||
    static u32 next_process_id;
 | 
			
		||||
 | 
			
		||||
    /// Title ID corresponding to the process
 | 
			
		||||
    u64 program_id;
 | 
			
		||||
 | 
			
		||||
    /// Resource limit descriptor for this process
 | 
			
		||||
    SharedPtr<ResourceLimit> resource_limit;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -108,11 +108,11 @@ union ResultCode {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    constexpr bool IsSuccess() const {
 | 
			
		||||
        return is_error.ExtractValue(raw) == 0;
 | 
			
		||||
        return raw == 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    constexpr bool IsError() const {
 | 
			
		||||
        return is_error.ExtractValue(raw) == 1;
 | 
			
		||||
        return raw != 0;
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,9 @@
 | 
			
		||||
// Refer to the license.txt file included.
 | 
			
		||||
 | 
			
		||||
#include <boost/container/flat_map.hpp>
 | 
			
		||||
#include "common/file_util.h"
 | 
			
		||||
#include "core/file_sys/filesystem.h"
 | 
			
		||||
#include "core/file_sys/savedata_factory.h"
 | 
			
		||||
#include "core/hle/service/filesystem/filesystem.h"
 | 
			
		||||
#include "core/hle/service/filesystem/fsp_srv.h"
 | 
			
		||||
 | 
			
		||||
@@ -41,12 +43,17 @@ ResultVal<std::unique_ptr<FileSys::FileSystemBackend>> OpenFileSystem(Type type,
 | 
			
		||||
    return itr->second->Open(path);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UnregisterFileSystems() {
 | 
			
		||||
void RegisterFileSystems() {
 | 
			
		||||
    filesystem_map.clear();
 | 
			
		||||
 | 
			
		||||
    std::string nand_directory = FileUtil::GetUserPath(D_NAND_IDX);
 | 
			
		||||
 | 
			
		||||
    auto savedata = std::make_unique<FileSys::SaveData_Factory>(std::move(nand_directory));
 | 
			
		||||
    RegisterFileSystem(std::move(savedata), Type::SaveData);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void InstallInterfaces(SM::ServiceManager& service_manager) {
 | 
			
		||||
    UnregisterFileSystems();
 | 
			
		||||
    RegisterFileSystems();
 | 
			
		||||
    std::make_shared<FSP_SRV>()->InstallAsService(service_manager);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -25,6 +25,7 @@ namespace FileSystem {
 | 
			
		||||
/// Supported FileSystem types
 | 
			
		||||
enum class Type {
 | 
			
		||||
    RomFS = 1,
 | 
			
		||||
    SaveData = 2,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@
 | 
			
		||||
// Licensed under GPLv2 or any later version
 | 
			
		||||
// Refer to the license.txt file included.
 | 
			
		||||
 | 
			
		||||
#include <cinttypes>
 | 
			
		||||
#include "common/logging/log.h"
 | 
			
		||||
#include "core/core.h"
 | 
			
		||||
#include "core/file_sys/filesystem.h"
 | 
			
		||||
@@ -65,10 +66,186 @@ private:
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class IFile final : public ServiceFramework<IFile> {
 | 
			
		||||
public:
 | 
			
		||||
    explicit IFile(std::unique_ptr<FileSys::StorageBackend>&& backend)
 | 
			
		||||
        : ServiceFramework("IFile"), backend(std::move(backend)) {
 | 
			
		||||
        static const FunctionInfo functions[] = {
 | 
			
		||||
            {0, &IFile::Read, "Read"}, {1, &IFile::Write, "Write"}, {2, nullptr, "Flush"},
 | 
			
		||||
            {3, nullptr, "SetSize"},   {4, nullptr, "GetSize"},
 | 
			
		||||
        };
 | 
			
		||||
        RegisterHandlers(functions);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    std::unique_ptr<FileSys::StorageBackend> backend;
 | 
			
		||||
 | 
			
		||||
    void Read(Kernel::HLERequestContext& ctx) {
 | 
			
		||||
        IPC::RequestParser rp{ctx};
 | 
			
		||||
        const u64 unk = rp.Pop<u64>();
 | 
			
		||||
        const s64 offset = rp.Pop<s64>();
 | 
			
		||||
        const s64 length = rp.Pop<s64>();
 | 
			
		||||
 | 
			
		||||
        LOG_DEBUG(Service_FS, "called, offset=0x%llx, length=0x%llx", offset, length);
 | 
			
		||||
 | 
			
		||||
        // Error checking
 | 
			
		||||
        if (length < 0) {
 | 
			
		||||
            IPC::ResponseBuilder rb{ctx, 2};
 | 
			
		||||
            rb.Push(ResultCode(ErrorModule::FS, ErrorDescription::InvalidLength));
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        if (offset < 0) {
 | 
			
		||||
            IPC::ResponseBuilder rb{ctx, 2};
 | 
			
		||||
            rb.Push(ResultCode(ErrorModule::FS, ErrorDescription::InvalidOffset));
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Read the data from the Storage backend
 | 
			
		||||
        std::vector<u8> output(length);
 | 
			
		||||
        ResultVal<size_t> res = backend->Read(offset, length, output.data());
 | 
			
		||||
        if (res.Failed()) {
 | 
			
		||||
            IPC::ResponseBuilder rb{ctx, 2};
 | 
			
		||||
            rb.Push(res.Code());
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Write the data to memory
 | 
			
		||||
        ctx.WriteBuffer(output);
 | 
			
		||||
 | 
			
		||||
        IPC::ResponseBuilder rb{ctx, 4};
 | 
			
		||||
        rb.Push(RESULT_SUCCESS);
 | 
			
		||||
        rb.Push(static_cast<u64>(*res));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void Write(Kernel::HLERequestContext& ctx) {
 | 
			
		||||
        IPC::RequestParser rp{ctx};
 | 
			
		||||
        const u64 unk = rp.Pop<u64>();
 | 
			
		||||
        const s64 offset = rp.Pop<s64>();
 | 
			
		||||
        const s64 length = rp.Pop<s64>();
 | 
			
		||||
 | 
			
		||||
        LOG_DEBUG(Service_FS, "called, offset=0x%llx, length=0x%llx", offset, length);
 | 
			
		||||
 | 
			
		||||
        // Error checking
 | 
			
		||||
        if (length < 0) {
 | 
			
		||||
            IPC::ResponseBuilder rb{ctx, 2};
 | 
			
		||||
            rb.Push(ResultCode(ErrorModule::FS, ErrorDescription::InvalidLength));
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        if (offset < 0) {
 | 
			
		||||
            IPC::ResponseBuilder rb{ctx, 2};
 | 
			
		||||
            rb.Push(ResultCode(ErrorModule::FS, ErrorDescription::InvalidOffset));
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Write the data to the Storage backend
 | 
			
		||||
        std::vector<u8> data = ctx.ReadBuffer();
 | 
			
		||||
        ResultVal<size_t> res = backend->Write(offset, length, true, data.data());
 | 
			
		||||
        if (res.Failed()) {
 | 
			
		||||
            IPC::ResponseBuilder rb{ctx, 2};
 | 
			
		||||
            rb.Push(res.Code());
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        IPC::ResponseBuilder rb{ctx, 2};
 | 
			
		||||
        rb.Push(RESULT_SUCCESS);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class IFileSystem final : public ServiceFramework<IFileSystem> {
 | 
			
		||||
public:
 | 
			
		||||
    explicit IFileSystem(std::unique_ptr<FileSys::FileSystemBackend>&& backend)
 | 
			
		||||
        : ServiceFramework("IFileSystem"), backend(std::move(backend)) {
 | 
			
		||||
        static const FunctionInfo functions[] = {
 | 
			
		||||
            {0, &IFileSystem::CreateFile, "CreateFile"},
 | 
			
		||||
            {7, &IFileSystem::GetEntryType, "GetEntryType"},
 | 
			
		||||
            {8, &IFileSystem::OpenFile, "OpenFile"},
 | 
			
		||||
            {10, &IFileSystem::Commit, "Commit"},
 | 
			
		||||
        };
 | 
			
		||||
        RegisterHandlers(functions);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void CreateFile(Kernel::HLERequestContext& ctx) {
 | 
			
		||||
        IPC::RequestParser rp{ctx};
 | 
			
		||||
 | 
			
		||||
        auto file_buffer = ctx.ReadBuffer();
 | 
			
		||||
        auto end = std::find(file_buffer.begin(), file_buffer.end(), '\0');
 | 
			
		||||
 | 
			
		||||
        std::string name(file_buffer.begin(), end);
 | 
			
		||||
 | 
			
		||||
        u64 mode = rp.Pop<u64>();
 | 
			
		||||
        u32 size = rp.Pop<u32>();
 | 
			
		||||
 | 
			
		||||
        LOG_DEBUG(Service_FS, "called file %s mode 0x%" PRIX64 " size 0x%08X", name.c_str(), mode,
 | 
			
		||||
                  size);
 | 
			
		||||
 | 
			
		||||
        IPC::ResponseBuilder rb{ctx, 2};
 | 
			
		||||
        rb.Push(backend->CreateFile(name, size));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void OpenFile(Kernel::HLERequestContext& ctx) {
 | 
			
		||||
        IPC::RequestParser rp{ctx};
 | 
			
		||||
 | 
			
		||||
        auto file_buffer = ctx.ReadBuffer();
 | 
			
		||||
        auto end = std::find(file_buffer.begin(), file_buffer.end(), '\0');
 | 
			
		||||
 | 
			
		||||
        std::string name(file_buffer.begin(), end);
 | 
			
		||||
 | 
			
		||||
        auto mode = static_cast<FileSys::Mode>(rp.Pop<u32>());
 | 
			
		||||
 | 
			
		||||
        LOG_DEBUG(Service_FS, "called file %s mode %u", name.c_str(), static_cast<u32>(mode));
 | 
			
		||||
 | 
			
		||||
        auto result = backend->OpenFile(name, mode);
 | 
			
		||||
        if (result.Failed()) {
 | 
			
		||||
            IPC::ResponseBuilder rb{ctx, 2};
 | 
			
		||||
            rb.Push(result.Code());
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        auto file = std::move(result.Unwrap());
 | 
			
		||||
 | 
			
		||||
        IPC::ResponseBuilder rb{ctx, 2, 0, 1};
 | 
			
		||||
        rb.Push(RESULT_SUCCESS);
 | 
			
		||||
        rb.PushIpcInterface<IFile>(std::move(file));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void GetEntryType(Kernel::HLERequestContext& ctx) {
 | 
			
		||||
        IPC::RequestParser rp{ctx};
 | 
			
		||||
 | 
			
		||||
        auto file_buffer = ctx.ReadBuffer();
 | 
			
		||||
        auto end = std::find(file_buffer.begin(), file_buffer.end(), '\0');
 | 
			
		||||
 | 
			
		||||
        std::string name(file_buffer.begin(), end);
 | 
			
		||||
 | 
			
		||||
        LOG_DEBUG(Service_FS, "called file %s", name.c_str());
 | 
			
		||||
 | 
			
		||||
        auto result = backend->GetEntryType(name);
 | 
			
		||||
        if (result.Failed()) {
 | 
			
		||||
            IPC::ResponseBuilder rb{ctx, 2};
 | 
			
		||||
            rb.Push(result.Code());
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        IPC::ResponseBuilder rb{ctx, 3};
 | 
			
		||||
        rb.Push(RESULT_SUCCESS);
 | 
			
		||||
        rb.Push<u32>(static_cast<u32>(*result));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void Commit(Kernel::HLERequestContext& ctx) {
 | 
			
		||||
        LOG_WARNING(Service_FS, "(STUBBED) called");
 | 
			
		||||
 | 
			
		||||
        IPC::ResponseBuilder rb{ctx, 2};
 | 
			
		||||
        rb.Push(RESULT_SUCCESS);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    std::unique_ptr<FileSys::FileSystemBackend> backend;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
FSP_SRV::FSP_SRV() : ServiceFramework("fsp-srv") {
 | 
			
		||||
    static const FunctionInfo functions[] = {
 | 
			
		||||
        {1, &FSP_SRV::Initalize, "Initalize"},
 | 
			
		||||
        {18, &FSP_SRV::MountSdCard, "MountSdCard"},
 | 
			
		||||
        {51, &FSP_SRV::MountSaveData, "MountSaveData"},
 | 
			
		||||
        {200, &FSP_SRV::OpenDataStorageByCurrentProcess, "OpenDataStorageByCurrentProcess"},
 | 
			
		||||
        {202, nullptr, "OpenDataStorageByDataId"},
 | 
			
		||||
        {203, &FSP_SRV::OpenRomStorage, "OpenRomStorage"},
 | 
			
		||||
@@ -102,6 +279,17 @@ void FSP_SRV::MountSdCard(Kernel::HLERequestContext& ctx) {
 | 
			
		||||
    rb.Push(RESULT_SUCCESS);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void FSP_SRV::MountSaveData(Kernel::HLERequestContext& ctx) {
 | 
			
		||||
    LOG_WARNING(Service_FS, "(STUBBED) called");
 | 
			
		||||
 | 
			
		||||
    FileSys::Path unused;
 | 
			
		||||
    auto filesystem = OpenFileSystem(Type::SaveData, unused).Unwrap();
 | 
			
		||||
 | 
			
		||||
    IPC::ResponseBuilder rb{ctx, 2, 0, 1};
 | 
			
		||||
    rb.Push(RESULT_SUCCESS);
 | 
			
		||||
    rb.PushIpcInterface<IFileSystem>(std::move(filesystem));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void FSP_SRV::GetGlobalAccessLogMode(Kernel::HLERequestContext& ctx) {
 | 
			
		||||
    LOG_WARNING(Service_FS, "(STUBBED) called");
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -24,6 +24,7 @@ private:
 | 
			
		||||
 | 
			
		||||
    void Initalize(Kernel::HLERequestContext& ctx);
 | 
			
		||||
    void MountSdCard(Kernel::HLERequestContext& ctx);
 | 
			
		||||
    void MountSaveData(Kernel::HLERequestContext& ctx);
 | 
			
		||||
    void GetGlobalAccessLogMode(Kernel::HLERequestContext& ctx);
 | 
			
		||||
    void OpenDataStorageByCurrentProcess(Kernel::HLERequestContext& ctx);
 | 
			
		||||
    void OpenRomStorage(Kernel::HLERequestContext& ctx);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user