2020-05-11 22:04:51 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-05-11 22:04:51 +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/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <vapours.hpp>
|
|
|
|
#include <stratosphere/fssystem/fssystem_bucket_tree.hpp>
|
|
|
|
|
|
|
|
namespace ams::fssystem {
|
|
|
|
|
2022-03-24 15:43:40 +00:00
|
|
|
/* ACCURATE_TO_VERSION: Unknown */
|
2020-05-11 22:04:51 +00:00
|
|
|
class IndirectStorage : public ::ams::fs::IStorage, public ::ams::fs::impl::Newable {
|
|
|
|
NON_COPYABLE(IndirectStorage);
|
|
|
|
NON_MOVEABLE(IndirectStorage);
|
|
|
|
public:
|
|
|
|
static constexpr s32 StorageCount = 2;
|
|
|
|
static constexpr size_t NodeSize = 16_KB;
|
|
|
|
|
|
|
|
using IAllocator = MemoryResource;
|
|
|
|
|
|
|
|
struct Entry {
|
|
|
|
u8 virt_offset[sizeof(s64)];
|
|
|
|
u8 phys_offset[sizeof(s64)];
|
|
|
|
s32 storage_index;
|
|
|
|
|
|
|
|
void SetVirtualOffset(const s64 &ofs) {
|
|
|
|
std::memcpy(this->virt_offset, std::addressof(ofs), sizeof(s64));
|
|
|
|
}
|
|
|
|
|
|
|
|
s64 GetVirtualOffset() const {
|
|
|
|
s64 offset;
|
|
|
|
std::memcpy(std::addressof(offset), this->virt_offset, sizeof(s64));
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetPhysicalOffset(const s64 &ofs) {
|
|
|
|
std::memcpy(this->phys_offset, std::addressof(ofs), sizeof(s64));
|
|
|
|
}
|
|
|
|
|
|
|
|
s64 GetPhysicalOffset() const {
|
|
|
|
s64 offset;
|
|
|
|
std::memcpy(std::addressof(offset), this->phys_offset, sizeof(s64));
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
};
|
2020-05-14 09:22:24 +00:00
|
|
|
static_assert(util::is_pod<Entry>::value);
|
2020-05-11 22:04:51 +00:00
|
|
|
static_assert(sizeof(Entry) == 0x14);
|
|
|
|
|
|
|
|
struct EntryData {
|
|
|
|
s64 virt_offset;
|
|
|
|
s64 phys_offset;
|
|
|
|
s32 storage_index;
|
|
|
|
|
|
|
|
void Set(const Entry &entry) {
|
|
|
|
this->virt_offset = entry.GetVirtualOffset();
|
|
|
|
this->phys_offset = entry.GetPhysicalOffset();
|
|
|
|
this->storage_index = entry.storage_index;
|
|
|
|
}
|
|
|
|
};
|
2020-05-14 09:22:24 +00:00
|
|
|
static_assert(util::is_pod<EntryData>::value);
|
2020-05-11 22:04:51 +00:00
|
|
|
private:
|
|
|
|
struct ContinuousReadingEntry {
|
|
|
|
static constexpr size_t FragmentSizeMax = 4_KB;
|
|
|
|
|
|
|
|
IndirectStorage::Entry entry;
|
|
|
|
|
|
|
|
s64 GetVirtualOffset() const {
|
|
|
|
return this->entry.GetVirtualOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
s64 GetPhysicalOffset() const {
|
|
|
|
return this->entry.GetPhysicalOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsFragment() const {
|
|
|
|
return this->entry.storage_index != 0;
|
|
|
|
}
|
|
|
|
};
|
2020-05-14 09:22:24 +00:00
|
|
|
static_assert(util::is_pod<ContinuousReadingEntry>::value);
|
2020-05-11 22:04:51 +00:00
|
|
|
public:
|
|
|
|
static constexpr s64 QueryHeaderStorageSize() {
|
|
|
|
return BucketTree::QueryHeaderStorageSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr s64 QueryNodeStorageSize(s32 entry_count) {
|
|
|
|
return BucketTree::QueryNodeStorageSize(NodeSize, sizeof(Entry), entry_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr s64 QueryEntryStorageSize(s32 entry_count) {
|
|
|
|
return BucketTree::QueryEntryStorageSize(NodeSize, sizeof(Entry), entry_count);
|
|
|
|
}
|
|
|
|
private:
|
2021-10-10 07:14:06 +00:00
|
|
|
BucketTree m_table;
|
|
|
|
fs::SubStorage m_data_storage[StorageCount];
|
2020-05-11 22:04:51 +00:00
|
|
|
public:
|
2021-10-10 07:14:06 +00:00
|
|
|
IndirectStorage() : m_table(), m_data_storage() { /* ... */ }
|
2020-05-11 22:04:51 +00:00
|
|
|
virtual ~IndirectStorage() { this->Finalize(); }
|
|
|
|
|
|
|
|
Result Initialize(IAllocator *allocator, fs::SubStorage table_storage);
|
|
|
|
void Finalize();
|
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
bool IsInitialized() const { return m_table.IsInitialized(); }
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
Result Initialize(IAllocator *allocator, fs::SubStorage node_storage, fs::SubStorage entry_storage, s32 entry_count) {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_table.Initialize(allocator, node_storage, entry_storage, NodeSize, sizeof(Entry), entry_count);
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetStorage(s32 idx, fs::SubStorage storage) {
|
|
|
|
AMS_ASSERT(0 <= idx && idx < StorageCount);
|
2021-10-10 07:14:06 +00:00
|
|
|
m_data_storage[idx] = storage;
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void SetStorage(s32 idx, T storage, s64 offset, s64 size) {
|
|
|
|
AMS_ASSERT(0 <= idx && idx < StorageCount);
|
2021-10-10 07:14:06 +00:00
|
|
|
m_data_storage[idx] = fs::SubStorage(storage, offset, size);
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetEntryList(Entry *out_entries, s32 *out_entry_count, s32 entry_count, s64 offset, s64 size);
|
|
|
|
|
|
|
|
virtual Result Read(s64 offset, void *buffer, size_t size) override;
|
|
|
|
virtual Result OperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override;
|
|
|
|
|
|
|
|
virtual Result GetSize(s64 *out) override {
|
|
|
|
AMS_ASSERT(out != nullptr);
|
2022-03-12 21:03:17 +00:00
|
|
|
|
|
|
|
BucketTree::Offsets offsets;
|
|
|
|
R_TRY(m_table.GetOffsets(std::addressof(offsets)));
|
|
|
|
|
|
|
|
*out = offsets.end_offset;
|
|
|
|
R_SUCCEED();
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result Flush() override {
|
2022-03-12 21:03:17 +00:00
|
|
|
R_SUCCEED();
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result Write(s64 offset, const void *buffer, size_t size) override {
|
2021-10-07 00:58:42 +00:00
|
|
|
AMS_UNUSED(offset, buffer, size);
|
2022-03-13 09:32:34 +00:00
|
|
|
R_THROW(fs::ResultUnsupportedWriteForIndirectStorage());
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result SetSize(s64 size) override {
|
2021-10-07 00:58:42 +00:00
|
|
|
AMS_UNUSED(size);
|
2022-03-13 09:32:34 +00:00
|
|
|
R_THROW(fs::ResultUnsupportedSetSizeForIndirectStorage());
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
protected:
|
2021-10-10 07:14:06 +00:00
|
|
|
BucketTree &GetEntryTable() { return m_table; }
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
fs::SubStorage &GetDataStorage(s32 index) {
|
|
|
|
AMS_ASSERT(0 <= index && index < StorageCount);
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_data_storage[index];
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-12 21:03:17 +00:00
|
|
|
template<bool ContinuousCheck, bool RangeCheck, typename F>
|
2020-05-11 22:04:51 +00:00
|
|
|
Result OperatePerEntry(s64 offset, s64 size, F func);
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|