2018-09-07 15:00:13 +00:00
|
|
|
/*
|
2019-04-08 02:00:49 +00:00
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
2018-09-07 15:00:13 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2019-06-20 20:00:32 +00:00
|
|
|
|
2018-06-10 17:11:05 +00:00
|
|
|
#pragma once
|
|
|
|
#include <switch.h>
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
#include "fs_shim.h"
|
|
|
|
|
2019-01-29 10:29:29 +00:00
|
|
|
#include "../debug.hpp"
|
2018-06-12 22:00:09 +00:00
|
|
|
|
2018-06-10 17:11:05 +00:00
|
|
|
class IStorage {
|
|
|
|
public:
|
2018-07-02 16:15:30 +00:00
|
|
|
virtual ~IStorage();
|
2019-03-22 15:49:10 +00:00
|
|
|
|
2018-06-14 23:50:01 +00:00
|
|
|
virtual Result Read(void *buffer, size_t size, u64 offset) = 0;
|
2018-06-10 17:11:05 +00:00
|
|
|
virtual Result Write(void *buffer, size_t size, u64 offset) = 0;
|
|
|
|
virtual Result Flush() = 0;
|
|
|
|
virtual Result SetSize(u64 size) = 0;
|
|
|
|
virtual Result GetSize(u64 *out_size) = 0;
|
2019-06-03 19:15:39 +00:00
|
|
|
virtual Result OperateRange(FsOperationId operation_type, u64 offset, u64 size, FsRangeInfo *out_range_info) = 0;
|
2019-03-22 15:49:10 +00:00
|
|
|
|
|
|
|
static inline bool IsRangeValid(uint64_t offset, uint64_t size, uint64_t total_size) {
|
|
|
|
return size <= total_size && offset <= total_size - size;
|
|
|
|
}
|
2018-06-10 17:11:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class IStorageInterface : public IServiceObject {
|
2019-06-28 06:34:26 +00:00
|
|
|
private:
|
|
|
|
enum class CommandId {
|
|
|
|
Read = 0,
|
|
|
|
Write = 1,
|
|
|
|
Flush = 2,
|
|
|
|
SetSize = 3,
|
|
|
|
GetSize = 4,
|
|
|
|
OperateRange = 5,
|
|
|
|
};
|
2018-06-10 17:11:05 +00:00
|
|
|
private:
|
|
|
|
IStorage *base_storage;
|
|
|
|
public:
|
|
|
|
IStorageInterface(IStorage *s) : base_storage(s) {
|
|
|
|
/* ... */
|
|
|
|
};
|
2019-03-22 15:49:10 +00:00
|
|
|
|
2018-06-10 17:11:05 +00:00
|
|
|
~IStorageInterface() {
|
|
|
|
delete base_storage;
|
|
|
|
};
|
2019-06-20 20:00:32 +00:00
|
|
|
|
2018-06-10 17:11:05 +00:00
|
|
|
private:
|
|
|
|
/* Actual command API. */
|
2018-10-30 00:30:39 +00:00
|
|
|
virtual Result Read(OutBuffer<u8, BufferType_Type1> buffer, u64 offset, u64 size) final {
|
|
|
|
return this->base_storage->Read(buffer.buffer, std::min(buffer.num_elements, size), offset);
|
2018-06-10 17:11:05 +00:00
|
|
|
};
|
2018-10-30 00:30:39 +00:00
|
|
|
virtual Result Write(InBuffer<u8, BufferType_Type1> buffer, u64 offset, u64 size) final {
|
|
|
|
return this->base_storage->Write(buffer.buffer, std::min(buffer.num_elements, size), offset);
|
2018-06-10 17:11:05 +00:00
|
|
|
};
|
2018-10-30 00:30:39 +00:00
|
|
|
virtual Result Flush() final {
|
|
|
|
return this->base_storage->Flush();
|
2018-06-10 17:11:05 +00:00
|
|
|
};
|
2018-10-30 00:30:39 +00:00
|
|
|
virtual Result SetSize(u64 size) final {
|
|
|
|
return this->base_storage->SetSize(size);
|
2018-06-10 17:11:05 +00:00
|
|
|
};
|
2018-10-30 00:30:39 +00:00
|
|
|
virtual Result GetSize(Out<u64> size) final {
|
|
|
|
return this->base_storage->GetSize(size.GetPointer());
|
2018-06-10 17:11:05 +00:00
|
|
|
};
|
2018-10-30 00:30:39 +00:00
|
|
|
virtual Result OperateRange(Out<FsRangeInfo> range_info, u32 operation_type, u64 offset, u64 size) final {
|
2019-06-03 19:15:39 +00:00
|
|
|
return this->base_storage->OperateRange(static_cast<FsOperationId>(operation_type), offset, size, range_info.GetPointer());
|
2018-10-30 00:30:39 +00:00
|
|
|
};
|
|
|
|
public:
|
|
|
|
DEFINE_SERVICE_DISPATCH_TABLE {
|
|
|
|
/* 1.0.0- */
|
2019-06-28 06:34:26 +00:00
|
|
|
MAKE_SERVICE_COMMAND_META(IStorageInterface, Read),
|
|
|
|
MAKE_SERVICE_COMMAND_META(IStorageInterface, Write),
|
|
|
|
MAKE_SERVICE_COMMAND_META(IStorageInterface, Flush),
|
|
|
|
MAKE_SERVICE_COMMAND_META(IStorageInterface, SetSize),
|
|
|
|
MAKE_SERVICE_COMMAND_META(IStorageInterface, GetSize),
|
2019-06-20 20:00:32 +00:00
|
|
|
|
2018-10-30 00:30:39 +00:00
|
|
|
/* 4.0.0- */
|
2019-06-28 06:34:26 +00:00
|
|
|
MAKE_SERVICE_COMMAND_META(IStorageInterface, OperateRange, FirmwareVersion_400),
|
2018-06-10 17:11:05 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
class IROStorage : public IStorage {
|
2018-06-14 23:50:01 +00:00
|
|
|
public:
|
|
|
|
virtual Result Read(void *buffer, size_t size, u64 offset) = 0;
|
2018-10-30 00:30:39 +00:00
|
|
|
virtual Result Write(void *buffer, size_t size, u64 offset) final {
|
2018-06-10 17:11:05 +00:00
|
|
|
(void)(buffer);
|
|
|
|
(void)(offset);
|
|
|
|
(void)(size);
|
2019-03-22 15:49:10 +00:00
|
|
|
return ResultFsUnsupportedOperation;
|
2018-06-10 17:11:05 +00:00
|
|
|
};
|
2018-10-30 00:30:39 +00:00
|
|
|
virtual Result Flush() final {
|
2019-10-24 08:40:44 +00:00
|
|
|
return ResultSuccess();
|
2018-06-10 17:11:05 +00:00
|
|
|
};
|
2018-10-30 00:30:39 +00:00
|
|
|
virtual Result SetSize(u64 size) final {
|
2018-06-10 17:11:05 +00:00
|
|
|
(void)(size);
|
2019-03-22 15:49:10 +00:00
|
|
|
return ResultFsUnsupportedOperation;
|
2018-06-10 17:11:05 +00:00
|
|
|
};
|
|
|
|
virtual Result GetSize(u64 *out_size) = 0;
|
2019-06-03 19:15:39 +00:00
|
|
|
virtual Result OperateRange(FsOperationId operation_type, u64 offset, u64 size, FsRangeInfo *out_range_info) = 0;
|
2018-06-10 17:11:05 +00:00
|
|
|
};
|
2018-11-15 02:39:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProxyStorage : public IStorage {
|
|
|
|
private:
|
|
|
|
FsStorage *base_storage;
|
|
|
|
public:
|
|
|
|
ProxyStorage(FsStorage *s) : base_storage(s) {
|
|
|
|
/* ... */
|
|
|
|
};
|
|
|
|
ProxyStorage(FsStorage s) {
|
|
|
|
this->base_storage = new FsStorage(s);
|
|
|
|
};
|
2018-11-15 03:40:46 +00:00
|
|
|
virtual ~ProxyStorage() {
|
2018-11-15 02:39:48 +00:00
|
|
|
fsStorageClose(base_storage);
|
|
|
|
delete base_storage;
|
|
|
|
};
|
|
|
|
public:
|
|
|
|
virtual Result Read(void *buffer, size_t size, u64 offset) override {
|
|
|
|
return fsStorageRead(this->base_storage, offset, buffer, size);
|
|
|
|
};
|
|
|
|
virtual Result Write(void *buffer, size_t size, u64 offset) override {
|
|
|
|
return fsStorageWrite(this->base_storage, offset, buffer, size);
|
|
|
|
};
|
|
|
|
virtual Result Flush() override {
|
|
|
|
return fsStorageFlush(this->base_storage);
|
|
|
|
};
|
|
|
|
virtual Result GetSize(u64 *out_size) override {
|
|
|
|
return fsStorageGetSize(this->base_storage, out_size);
|
|
|
|
};
|
|
|
|
virtual Result SetSize(u64 size) override {
|
|
|
|
return fsStorageSetSize(this->base_storage, size);
|
|
|
|
};
|
2019-06-03 19:15:39 +00:00
|
|
|
virtual Result OperateRange(FsOperationId operation_type, u64 offset, u64 size, FsRangeInfo *out_range_info) override {
|
2018-11-15 02:39:48 +00:00
|
|
|
return fsStorageOperateRange(this->base_storage, operation_type, offset, size, out_range_info);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-06-03 19:15:39 +00:00
|
|
|
class ReadOnlyStorageAdapter : public IROStorage {
|
2018-11-15 02:39:48 +00:00
|
|
|
private:
|
2019-06-03 19:15:39 +00:00
|
|
|
std::shared_ptr<IStorage> base_storage;
|
|
|
|
IStorage *storage;
|
2018-11-15 02:39:48 +00:00
|
|
|
public:
|
2019-06-03 19:15:39 +00:00
|
|
|
ReadOnlyStorageAdapter(IStorage *s) : base_storage(s) {
|
|
|
|
this->storage = this->base_storage.get();
|
|
|
|
}
|
|
|
|
ReadOnlyStorageAdapter(std::shared_ptr<IStorage> s) : base_storage(s) {
|
|
|
|
this->storage = this->base_storage.get();
|
|
|
|
}
|
|
|
|
virtual ~ReadOnlyStorageAdapter() {
|
2018-11-15 02:39:48 +00:00
|
|
|
/* ... */
|
2019-06-03 19:15:39 +00:00
|
|
|
}
|
2018-11-15 02:39:48 +00:00
|
|
|
public:
|
|
|
|
virtual Result Read(void *buffer, size_t size, u64 offset) override {
|
2019-06-03 19:15:39 +00:00
|
|
|
return this->base_storage->Read(buffer, size, offset);
|
2018-11-15 02:39:48 +00:00
|
|
|
};
|
|
|
|
virtual Result GetSize(u64 *out_size) override {
|
2019-06-03 19:15:39 +00:00
|
|
|
return this->base_storage->GetSize(out_size);
|
2018-11-15 02:39:48 +00:00
|
|
|
};
|
2019-06-03 19:15:39 +00:00
|
|
|
virtual Result OperateRange(FsOperationId operation_type, u64 offset, u64 size, FsRangeInfo *out_range_info) override {
|
|
|
|
return this->base_storage->OperateRange(operation_type, offset, size, out_range_info);
|
2018-11-15 02:39:48 +00:00
|
|
|
};
|
2019-06-03 19:15:39 +00:00
|
|
|
};
|