Implement support for parsing/interacting with NCAs. (#942)
* fs: implement support for interacting with ncas. * spl: extend to use virtual keyslots
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* 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/os.hpp>
|
||||
#include <stratosphere/fs/fs_storage_type.hpp>
|
||||
#include <stratosphere/fs/fs_istorage.hpp>
|
||||
#include <stratosphere/fs/fs_memory_management.hpp>
|
||||
#include <stratosphere/fssystem/save/fssystem_i_save_file_system_driver.hpp>
|
||||
#include <stratosphere/fssystem/buffers/fssystem_file_system_buffer_manager.hpp>
|
||||
|
||||
namespace ams::fssystem::save {
|
||||
|
||||
constexpr inline size_t IntegrityMinLayerCount = 2;
|
||||
constexpr inline size_t IntegrityMaxLayerCount = 7;
|
||||
constexpr inline size_t IntegrityLayerCountSave = 5;
|
||||
constexpr inline size_t IntegrityLayerCountSaveDataMeta = 4;
|
||||
|
||||
struct FileSystemBufferManagerSet {
|
||||
IBufferManager *buffers[IntegrityMaxLayerCount];
|
||||
};
|
||||
static_assert(std::is_pod<FileSystemBufferManagerSet>::value);
|
||||
|
||||
class BlockCacheBufferedStorage : public ::ams::fs::IStorage {
|
||||
NON_COPYABLE(BlockCacheBufferedStorage);
|
||||
NON_MOVEABLE(BlockCacheBufferedStorage);
|
||||
public:
|
||||
static constexpr size_t DefaultMaxCacheEntryCount = 24;
|
||||
private:
|
||||
using MemoryRange = std::pair<uintptr_t, size_t>;
|
||||
using CacheIndex = s32;
|
||||
|
||||
struct CacheEntry {
|
||||
size_t size;
|
||||
bool is_valid;
|
||||
bool is_write_back;
|
||||
bool is_cached;
|
||||
bool is_flushing;
|
||||
s64 offset;
|
||||
IBufferManager::CacheHandle handle;
|
||||
uintptr_t memory_address;
|
||||
size_t memory_size;
|
||||
};
|
||||
static_assert(std::is_pod<CacheEntry>::value);
|
||||
|
||||
enum Flag : s32 {
|
||||
Flag_KeepBurstMode = (1 << 8),
|
||||
Flag_RealData = (1 << 10),
|
||||
};
|
||||
private:
|
||||
IBufferManager *buffer_manager;
|
||||
os::Mutex *mutex;
|
||||
std::unique_ptr<CacheEntry[], ::ams::fs::impl::Deleter> entries;
|
||||
IStorage *data_storage;
|
||||
Result last_result;
|
||||
s64 data_size;
|
||||
size_t verification_block_size;
|
||||
size_t verification_block_shift;
|
||||
CacheIndex invalidate_index;
|
||||
s32 max_cache_entry_count;
|
||||
s32 flags;
|
||||
s32 buffer_level;
|
||||
fs::StorageType storage_type;
|
||||
public:
|
||||
BlockCacheBufferedStorage();
|
||||
virtual ~BlockCacheBufferedStorage() override;
|
||||
|
||||
Result Initialize(IBufferManager *bm, os::Mutex *mtx, IStorage *data, s64 data_size, size_t verif_block_size, s32 max_cache_entries, bool is_real_data, s8 buffer_level, bool is_keep_burst_mode, fs::StorageType storage_type);
|
||||
void Finalize();
|
||||
|
||||
virtual Result Read(s64 offset, void *buffer, size_t size) override;
|
||||
virtual Result Write(s64 offset, const void *buffer, size_t size) override;
|
||||
|
||||
virtual Result SetSize(s64 size) override { return fs::ResultUnsupportedOperationInBlockCacheBufferedStorageA(); }
|
||||
virtual Result GetSize(s64 *out) override;
|
||||
|
||||
virtual Result Flush() 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;
|
||||
using IStorage::OperateRange;
|
||||
|
||||
Result Commit();
|
||||
Result OnRollback();
|
||||
|
||||
bool IsEnabledKeepBurstMode() const {
|
||||
return (this->flags & Flag_KeepBurstMode) != 0;
|
||||
}
|
||||
|
||||
bool IsRealDataCache() const {
|
||||
return (this->flags & Flag_RealData) != 0;
|
||||
}
|
||||
|
||||
void SetKeepBurstMode(bool en) {
|
||||
if (en) {
|
||||
this->flags |= Flag_KeepBurstMode;
|
||||
} else {
|
||||
this->flags &= ~Flag_KeepBurstMode;
|
||||
}
|
||||
}
|
||||
|
||||
void SetRealDataCache(bool en) {
|
||||
if (en) {
|
||||
this->flags |= Flag_RealData;
|
||||
} else {
|
||||
this->flags &= ~Flag_RealData;
|
||||
}
|
||||
}
|
||||
private:
|
||||
s32 GetMaxCacheEntryCount() const {
|
||||
return this->max_cache_entry_count;
|
||||
}
|
||||
|
||||
Result ClearImpl(s64 offset, s64 size);
|
||||
Result ClearSignatureImpl(s64 offset, s64 size);
|
||||
Result InvalidateCacheImpl(s64 offset, s64 size);
|
||||
Result QueryRangeImpl(void *dst, size_t dst_size, s64 offset, s64 size);
|
||||
|
||||
bool ExistsRedundantCacheEntry(const CacheEntry &entry) const;
|
||||
|
||||
Result GetAssociateBuffer(MemoryRange *out_range, CacheEntry *out_entry, s64 offset, size_t ideal_size, bool is_allocate_for_write);
|
||||
|
||||
void DestroyBuffer(CacheEntry *entry, const MemoryRange &range);
|
||||
|
||||
Result StoreAssociateBuffer(CacheIndex *out, const MemoryRange &range, const CacheEntry &entry);
|
||||
Result StoreAssociateBuffer(const MemoryRange &range, const CacheEntry &entry) {
|
||||
CacheIndex dummy;
|
||||
return this->StoreAssociateBuffer(std::addressof(dummy), range, entry);
|
||||
}
|
||||
|
||||
Result StoreOrDestroyBuffer(const MemoryRange &range, CacheEntry *entry) {
|
||||
AMS_ASSERT(entry != nullptr);
|
||||
|
||||
auto buf_guard = SCOPE_GUARD { this->DestroyBuffer(entry, range); };
|
||||
|
||||
R_TRY(this->StoreAssociateBuffer(range, *entry));
|
||||
|
||||
buf_guard.Cancel();
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result FlushCacheEntry(CacheIndex index, bool invalidate);
|
||||
Result FlushRangeCacheEntries(s64 offset, s64 size, bool invalidate);
|
||||
void InvalidateRangeCacheEntries(s64 offset, s64 size);
|
||||
|
||||
Result FlushAllCacheEntries();
|
||||
Result InvalidateAllCacheEntries();
|
||||
Result ControlDirtiness();
|
||||
|
||||
Result UpdateLastResult(Result result);
|
||||
|
||||
Result ReadHeadCache(MemoryRange *out_range, CacheEntry *out_entry, bool *out_cache_needed, s64 *offset, s64 *aligned_offset, s64 aligned_offset_end, char **buffer, size_t *size);
|
||||
Result ReadTailCache(MemoryRange *out_range, CacheEntry *out_entry, bool *out_cache_needed, s64 offset, s64 aligned_offset, s64 *aligned_offset_end, char *buffer, size_t *size);
|
||||
|
||||
Result BulkRead(s64 offset, void *buffer, size_t size, MemoryRange *range_head, MemoryRange *range_tail, CacheEntry *entry_head, CacheEntry *entry_tail, bool head_cache_needed, bool tail_cache_needed);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* 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/os.hpp>
|
||||
#include <stratosphere/fs/fs_istorage.hpp>
|
||||
#include <stratosphere/fs/fs_substorage.hpp>
|
||||
#include <stratosphere/fssystem/buffers/fssystem_i_buffer_manager.hpp>
|
||||
|
||||
namespace ams::fssystem::save {
|
||||
|
||||
class BufferedStorage : public ::ams::fs::IStorage {
|
||||
NON_COPYABLE(BufferedStorage);
|
||||
NON_MOVEABLE(BufferedStorage);
|
||||
private:
|
||||
class Cache;
|
||||
class UniqueCache;
|
||||
class SharedCache;
|
||||
private:
|
||||
fs::SubStorage base_storage;
|
||||
IBufferManager *buffer_manager;
|
||||
size_t block_size;
|
||||
s64 base_storage_size;
|
||||
std::unique_ptr<Cache[]> caches;
|
||||
s32 cache_count;
|
||||
Cache *next_acquire_cache;
|
||||
Cache *next_fetch_cache;
|
||||
os::Mutex mutex;
|
||||
bool bulk_read_enabled;
|
||||
public:
|
||||
BufferedStorage();
|
||||
virtual ~BufferedStorage();
|
||||
|
||||
Result Initialize(fs::SubStorage base_storage, IBufferManager *buffer_manager, size_t block_size, s32 buffer_count);
|
||||
void Finalize();
|
||||
|
||||
bool IsInitialized() const { return this->caches != nullptr; }
|
||||
|
||||
virtual Result Read(s64 offset, void *buffer, size_t size) override;
|
||||
virtual Result Write(s64 offset, const void *buffer, size_t size) override;
|
||||
|
||||
virtual Result GetSize(s64 *out) override;
|
||||
virtual Result SetSize(s64 size) override;
|
||||
|
||||
virtual Result Flush() 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;
|
||||
using IStorage::OperateRange;
|
||||
|
||||
void InvalidateCaches();
|
||||
|
||||
IBufferManager *GetBufferManager() const { return this->buffer_manager; }
|
||||
|
||||
void EnableBulkRead() { this->bulk_read_enabled = true; }
|
||||
private:
|
||||
Result PrepareAllocation();
|
||||
Result ControlDirtiness();
|
||||
Result ReadCore(s64 offset, void *buffer, size_t size);
|
||||
|
||||
bool ReadHeadCache(s64 *offset, void *buffer, size_t *size, s64 *buffer_offset);
|
||||
bool ReadTailCache(s64 offset, void *buffer, size_t *size, s64 buffer_offset);
|
||||
|
||||
Result BulkRead(s64 offset, void *buffer, size_t size, bool head_cache_needed, bool tail_cache_needed);
|
||||
|
||||
Result WriteCore(s64 offset, const void *buffer, size_t size);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* 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/os.hpp>
|
||||
#include <stratosphere/fs/fs_istorage.hpp>
|
||||
#include <stratosphere/fs/fs_substorage.hpp>
|
||||
#include <stratosphere/fs/fs_storage_type.hpp>
|
||||
#include <stratosphere/fssystem/save/fssystem_i_save_file.hpp>
|
||||
#include <stratosphere/fssystem/save/fssystem_integrity_verification_storage.hpp>
|
||||
#include <stratosphere/fssystem/save/fssystem_block_cache_buffered_storage.hpp>
|
||||
|
||||
namespace ams::fssystem::save {
|
||||
|
||||
struct HierarchicalIntegrityVerificationLevelInformation {
|
||||
fs::Int64 offset;
|
||||
fs::Int64 size;
|
||||
s32 block_order;
|
||||
u8 reserved[4];
|
||||
};
|
||||
static_assert(std::is_pod<HierarchicalIntegrityVerificationLevelInformation>::value);
|
||||
static_assert(sizeof(HierarchicalIntegrityVerificationLevelInformation) == 0x18);
|
||||
static_assert(alignof(HierarchicalIntegrityVerificationLevelInformation) == 0x4);
|
||||
|
||||
struct HierarchicalIntegrityVerificationInformation {
|
||||
u32 max_layers;
|
||||
HierarchicalIntegrityVerificationLevelInformation info[IntegrityMaxLayerCount - 1];
|
||||
fs::HashSalt seed;
|
||||
|
||||
s64 GetLayeredHashSize() const {
|
||||
return this->info[this->max_layers - 2].offset;
|
||||
}
|
||||
|
||||
s64 GetDataOffset() const {
|
||||
return this->info[this->max_layers - 2].offset;
|
||||
}
|
||||
|
||||
s64 GetDataSize() const {
|
||||
return this->info[this->max_layers - 2].size;
|
||||
}
|
||||
};
|
||||
static_assert(std::is_pod<HierarchicalIntegrityVerificationInformation>::value);
|
||||
|
||||
struct HierarchicalIntegrityVerificationMetaInformation {
|
||||
u32 magic;
|
||||
u32 version;
|
||||
u32 master_hash_size;
|
||||
HierarchicalIntegrityVerificationInformation level_hash_info;
|
||||
|
||||
/* TODO: Format */
|
||||
};
|
||||
static_assert(std::is_pod<HierarchicalIntegrityVerificationMetaInformation>::value);
|
||||
|
||||
struct HierarchicalIntegrityVerificationSizeSet {
|
||||
s64 control_size;
|
||||
s64 master_hash_size;
|
||||
s64 layered_hash_sizes[IntegrityMaxLayerCount - 1];
|
||||
};
|
||||
static_assert(std::is_pod<HierarchicalIntegrityVerificationSizeSet>::value);
|
||||
|
||||
class HierarchicalIntegrityVerificationStorageControlArea {
|
||||
NON_COPYABLE(HierarchicalIntegrityVerificationStorageControlArea);
|
||||
NON_MOVEABLE(HierarchicalIntegrityVerificationStorageControlArea);
|
||||
public:
|
||||
static constexpr size_t HashSize = crypto::Sha256Generator::HashSize;
|
||||
|
||||
struct InputParam {
|
||||
size_t level_block_size[IntegrityMaxLayerCount - 1];
|
||||
};
|
||||
static_assert(std::is_pod<InputParam>::value);
|
||||
private:
|
||||
fs::SubStorage storage;
|
||||
HierarchicalIntegrityVerificationMetaInformation meta;
|
||||
public:
|
||||
static Result QuerySize(HierarchicalIntegrityVerificationSizeSet *out, const InputParam &input_param, s32 layer_count, s64 data_size);
|
||||
/* TODO Format */
|
||||
static Result Expand(fs::SubStorage meta_storage, const HierarchicalIntegrityVerificationMetaInformation &meta);
|
||||
public:
|
||||
HierarchicalIntegrityVerificationStorageControlArea() { /* ... */ }
|
||||
|
||||
Result Initialize(fs::SubStorage meta_storage);
|
||||
void Finalize();
|
||||
|
||||
u32 GetMasterHashSize() const { return this->meta.master_hash_size; }
|
||||
void GetLevelHashInfo(HierarchicalIntegrityVerificationInformation *out) {
|
||||
AMS_ASSERT(out != nullptr);
|
||||
*out = this->meta.level_hash_info;
|
||||
}
|
||||
};
|
||||
|
||||
class HierarchicalIntegrityVerificationStorage : public ::ams::fs::IStorage {
|
||||
NON_COPYABLE(HierarchicalIntegrityVerificationStorage);
|
||||
NON_MOVEABLE(HierarchicalIntegrityVerificationStorage);
|
||||
private:
|
||||
friend class HierarchicalIntegrityVerificationMetaInformation;
|
||||
protected:
|
||||
static constexpr s64 HashSize = crypto::Sha256Generator::HashSize;
|
||||
static constexpr size_t MaxLayers = IntegrityMaxLayerCount;
|
||||
public:
|
||||
using GenerateRandomFunction = void (*)(void *dst, size_t size);
|
||||
|
||||
class HierarchicalStorageInformation {
|
||||
public:
|
||||
enum {
|
||||
MasterStorage = 0,
|
||||
Layer1Storage = 1,
|
||||
Layer2Storage = 2,
|
||||
Layer3Storage = 3,
|
||||
Layer4Storage = 4,
|
||||
Layer5Storage = 5,
|
||||
DataStorage = 6,
|
||||
};
|
||||
private:
|
||||
fs::SubStorage storages[DataStorage + 1];
|
||||
public:
|
||||
void SetMasterHashStorage(fs::SubStorage s) { this->storages[MasterStorage] = s; }
|
||||
void SetLayer1HashStorage(fs::SubStorage s) { this->storages[Layer1Storage] = s; }
|
||||
void SetLayer2HashStorage(fs::SubStorage s) { this->storages[Layer2Storage] = s; }
|
||||
void SetLayer3HashStorage(fs::SubStorage s) { this->storages[Layer3Storage] = s; }
|
||||
void SetLayer4HashStorage(fs::SubStorage s) { this->storages[Layer4Storage] = s; }
|
||||
void SetLayer5HashStorage(fs::SubStorage s) { this->storages[Layer5Storage] = s; }
|
||||
void SetDataStorage(fs::SubStorage s) { this->storages[DataStorage] = s; }
|
||||
|
||||
fs::SubStorage &operator[](s32 index) {
|
||||
AMS_ASSERT(MasterStorage <= index && index <= DataStorage);
|
||||
return this->storages[index];
|
||||
}
|
||||
};
|
||||
private:
|
||||
static GenerateRandomFunction s_generate_random;
|
||||
|
||||
static void SetGenerateRandomFunction(GenerateRandomFunction func) {
|
||||
s_generate_random = func;
|
||||
}
|
||||
private:
|
||||
FileSystemBufferManagerSet *buffers;
|
||||
os::Mutex *mutex;
|
||||
IntegrityVerificationStorage verify_storages[MaxLayers - 1];
|
||||
BlockCacheBufferedStorage buffer_storages[MaxLayers - 1];
|
||||
s64 data_size;
|
||||
s32 max_layers;
|
||||
bool is_written_for_rollback;
|
||||
public:
|
||||
HierarchicalIntegrityVerificationStorage() : buffers(nullptr), mutex(nullptr), data_size(-1), is_written_for_rollback(false) { /* ... */ }
|
||||
virtual ~HierarchicalIntegrityVerificationStorage() override { this->Finalize(); }
|
||||
|
||||
Result Initialize(const HierarchicalIntegrityVerificationInformation &info, HierarchicalStorageInformation storage, FileSystemBufferManagerSet *bufs, os::Mutex *mtx, fs::StorageType storage_type);
|
||||
void Finalize();
|
||||
|
||||
virtual Result Read(s64 offset, void *buffer, size_t size) override;
|
||||
virtual Result Write(s64 offset, const void *buffer, size_t size) override;
|
||||
|
||||
virtual Result SetSize(s64 size) override { return fs::ResultUnsupportedOperationInHierarchicalIntegrityVerificationStorageA(); }
|
||||
virtual Result GetSize(s64 *out) override;
|
||||
|
||||
virtual Result Flush() 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;
|
||||
using IStorage::OperateRange;
|
||||
|
||||
Result Commit();
|
||||
Result OnRollback();
|
||||
|
||||
bool IsInitialized() const {
|
||||
return this->data_size >= 0;
|
||||
}
|
||||
|
||||
bool IsWrittenForRollback() const {
|
||||
return this->is_written_for_rollback;
|
||||
}
|
||||
|
||||
FileSystemBufferManagerSet *GetBuffers() {
|
||||
return this->buffers;
|
||||
}
|
||||
|
||||
void GetParameters(HierarchicalIntegrityVerificationStorageControlArea::InputParam *out) const {
|
||||
AMS_ASSERT(out != nullptr);
|
||||
for (auto level = 0; level <= this->max_layers - 2; ++level) {
|
||||
out->level_block_size[level] = static_cast<size_t>(this->verify_storages[level].GetBlockSize());
|
||||
}
|
||||
}
|
||||
|
||||
s64 GetL1HashVerificationBlockSize() const {
|
||||
return this->verify_storages[this->max_layers - 2].GetBlockSize();
|
||||
}
|
||||
|
||||
fs::SubStorage GetL1HashStorage() {
|
||||
return fs::SubStorage(std::addressof(this->buffer_storages[this->max_layers - 3]), 0, util::DivideUp(this->data_size, this->GetL1HashVerificationBlockSize()));
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* 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>
|
||||
|
||||
namespace ams::fssystem::save {
|
||||
|
||||
/* TODO */
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* 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>
|
||||
|
||||
namespace ams::fssystem::save {
|
||||
|
||||
/* TODO */
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* 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/os.hpp>
|
||||
#include <stratosphere/fs/fs_istorage.hpp>
|
||||
#include <stratosphere/fs/fs_substorage.hpp>
|
||||
#include <stratosphere/fs/fs_storage_type.hpp>
|
||||
#include <stratosphere/fs/fs_save_data_types.hpp>
|
||||
#include <stratosphere/fssystem/save/fssystem_save_types.hpp>
|
||||
#include <stratosphere/fssystem/save/fssystem_i_save_file_system_driver.hpp>
|
||||
#include <stratosphere/fssystem/save/fssystem_block_cache_buffered_storage.hpp>
|
||||
|
||||
namespace ams::fssystem::save {
|
||||
|
||||
class IntegrityVerificationStorage : public ::ams::fs::IStorage {
|
||||
NON_COPYABLE(IntegrityVerificationStorage);
|
||||
NON_MOVEABLE(IntegrityVerificationStorage);
|
||||
public:
|
||||
static constexpr s64 HashSize = crypto::Sha256Generator::HashSize;
|
||||
|
||||
struct BlockHash {
|
||||
u8 hash[HashSize];
|
||||
};
|
||||
static_assert(std::is_pod<BlockHash>::value);
|
||||
private:
|
||||
fs::SubStorage hash_storage;
|
||||
fs::SubStorage data_storage;
|
||||
s64 verification_block_size;
|
||||
s64 verification_block_order;
|
||||
s64 upper_layer_verification_block_size;
|
||||
s64 upper_layer_verification_block_order;
|
||||
IBufferManager *buffer_manager;
|
||||
fs::HashSalt salt;
|
||||
bool is_real_data;
|
||||
fs::StorageType storage_type;
|
||||
public:
|
||||
IntegrityVerificationStorage() : verification_block_size(0), verification_block_order(0), upper_layer_verification_block_size(0), upper_layer_verification_block_order(0), buffer_manager(nullptr) { /* ... */ }
|
||||
virtual ~IntegrityVerificationStorage() override { this->Finalize(); }
|
||||
|
||||
Result Initialize(fs::SubStorage hs, fs::SubStorage ds, s64 verif_block_size, s64 upper_layer_verif_block_size, IBufferManager *bm, const fs::HashSalt &salt, bool is_real_data, fs::StorageType storage_type);
|
||||
void Finalize();
|
||||
|
||||
virtual Result Read(s64 offset, void *buffer, size_t size) override;
|
||||
virtual Result Write(s64 offset, const void *buffer, size_t size) override;
|
||||
|
||||
virtual Result SetSize(s64 size) override { return fs::ResultUnsupportedOperationInIntegrityVerificationStorageA(); }
|
||||
virtual Result GetSize(s64 *out) override;
|
||||
|
||||
virtual Result Flush() 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;
|
||||
using IStorage::OperateRange;
|
||||
|
||||
void CalcBlockHash(BlockHash *out, const void *buffer, size_t block_size) const;
|
||||
|
||||
s64 GetBlockSize() const {
|
||||
return this->verification_block_size;
|
||||
}
|
||||
private:
|
||||
Result ReadBlockSignature(void *dst, size_t dst_size, s64 offset, size_t size);
|
||||
Result WriteBlockSignature(const void *src, size_t src_size, s64 offset, size_t size);
|
||||
Result VerifyHash(const void *buf, BlockHash *hash);
|
||||
|
||||
void CalcBlockHash(BlockHash *out, const void *buffer) const {
|
||||
return this->CalcBlockHash(out, buffer, static_cast<size_t>(this->verification_block_size));
|
||||
}
|
||||
|
||||
Result IsCleared(bool *is_cleared, const BlockHash &hash);
|
||||
private:
|
||||
static void SetValidationBit(BlockHash *hash) {
|
||||
AMS_ASSERT(hash != nullptr);
|
||||
hash->hash[HashSize - 1] |= 0x80;
|
||||
}
|
||||
|
||||
static bool IsValidationBit(const BlockHash *hash) {
|
||||
AMS_ASSERT(hash != nullptr);
|
||||
return (hash->hash[HashSize - 1] & 0x80) != 0;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* 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/lmem.hpp>
|
||||
#include <stratosphere/fs/fs_directory.hpp>
|
||||
#include <stratosphere/fs/fs_filesystem.hpp>
|
||||
#include <stratosphere/fssystem/dbm/fssystem_dbm_utils.hpp>
|
||||
|
||||
namespace ams::fssystem::save {
|
||||
|
||||
constexpr inline bool IsPowerOfTwo(s32 val) {
|
||||
return util::IsPowerOfTwo(val);
|
||||
}
|
||||
|
||||
constexpr inline u32 ILog2(u32 val) {
|
||||
AMS_ASSERT(val > 0);
|
||||
return (BITSIZEOF(u32) - 1 - dbm::CountLeadingZeros(val));
|
||||
}
|
||||
|
||||
constexpr inline u32 CeilPowerOfTwo(u32 val) {
|
||||
if (val == 0) {
|
||||
return 1;
|
||||
}
|
||||
return ((1u << (BITSIZEOF(u32) - 1)) >> (dbm::CountLeadingZeros(val - 1) - 1));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user