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:
SciresM
2020-05-11 15:04:51 -07:00
committed by GitHub
parent 3a1ccdd919
commit 81f91803ec
118 changed files with 13301 additions and 405 deletions

View File

@@ -16,6 +16,7 @@
#pragma once
#include <stratosphere/fs/fs_common.hpp>
#include <stratosphere/fs/fs_storage_type.hpp>
#include <stratosphere/fs/fsa/fs_ifile.hpp>
#include <stratosphere/fs/fsa/fs_idirectory.hpp>
#include <stratosphere/fs/fsa/fs_ifilesystem.hpp>
@@ -29,6 +30,7 @@
#include <stratosphere/fs/fs_remote_storage.hpp>
#include <stratosphere/fs/fs_file_storage.hpp>
#include <stratosphere/fs/fs_query_range.hpp>
#include <stratosphere/fs/fs_speed_emulation.hpp>
#include <stratosphere/fs/impl/fs_common_mount_name.hpp>
#include <stratosphere/fs/fs_mount.hpp>
#include <stratosphere/fs/fs_path_tool.hpp>

View File

@@ -17,11 +17,14 @@
#include <stratosphere/fs/fs_common.hpp>
#include <stratosphere/fs/fs_istorage.hpp>
#include <stratosphere/fs/fsa/fs_ifile.hpp>
#include <stratosphere/fs/fsa/fs_ifilesystem.hpp>
#include <stratosphere/fs/impl/fs_newable.hpp>
namespace ams::fs {
class FileStorage : public IStorage, public impl::Newable {
NON_COPYABLE(FileStorage);
NON_MOVEABLE(FileStorage);
private:
static constexpr s64 InvalidSize = -1;
private:
@@ -43,8 +46,25 @@ namespace ams::fs {
}
virtual ~FileStorage() { /* ... */ }
protected:
private:
Result UpdateSize();
protected:
constexpr FileStorage() : unique_file(), shared_file(), base_file(nullptr), size(InvalidSize) { /* ... */ }
void SetFile(fs::fsa::IFile *file) {
AMS_ASSERT(file != nullptr);
AMS_ASSERT(this->base_file == nullptr);
this->base_file = file;
}
void SetFile(std::unique_ptr<fs::fsa::IFile> &&file) {
AMS_ASSERT(file != nullptr);
AMS_ASSERT(this->base_file == nullptr);
AMS_ASSERT(this->unique_file == nullptr);
this->unique_file = std::move(file);
this->base_file = this->unique_file.get();
}
public:
virtual Result Read(s64 offset, void *buffer, size_t size) override;
virtual Result Write(s64 offset, const void *buffer, size_t size) override;
@@ -54,6 +74,17 @@ namespace ams::fs {
virtual Result OperateRange(void *dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override;
};
class FileStorageBasedFileSystem : public FileStorage {
NON_COPYABLE(FileStorageBasedFileSystem);
NON_MOVEABLE(FileStorageBasedFileSystem);
private:
std::shared_ptr<fs::fsa::IFileSystem> base_file_system;
public:
constexpr FileStorageBasedFileSystem() : FileStorage(), base_file_system(nullptr) { /* ... */ }
Result Initialize(std::shared_ptr<fs::fsa::IFileSystem> base_file_system, const char *path, fs::OpenMode mode);
};
class FileHandleStorage : public IStorage, public impl::Newable {
private:
static constexpr s64 InvalidSize = -1;

View File

@@ -46,6 +46,17 @@ namespace ams::fs {
return std::unique_ptr<T, Deleter>(static_cast<T *>(::ams::fs::impl::Allocate(sizeof(T))), Deleter(sizeof(T)));
}
template<typename ArrayT>
std::unique_ptr<ArrayT, Deleter> MakeUnique(size_t size) {
using T = typename std::remove_extent<ArrayT>::type;
static_assert(std::is_pod<ArrayT>::value);
static_assert(std::is_array<ArrayT>::value);
const size_t alloc_size = sizeof(T) * size;
return std::unique_ptr<ArrayT, Deleter>(static_cast<T *>(::ams::fs::impl::Allocate(alloc_size)), Deleter(alloc_size));
}
}
}

View File

@@ -47,6 +47,8 @@ namespace ams::fs {
enum class AesCtrKeyTypeFlag : s32 {
InternalKeyForSoftwareAes = (1 << 0),
InternalKeyForHardwareAes = (1 << 1),
ExternalKeyForHardwareAes = (1 << 2),
};
}

View File

@@ -156,4 +156,14 @@ namespace ams::fs {
static_assert(sizeof(SaveDataExtraData) == 0x200);
static_assert(util::is_pod<SaveDataExtraData>::value);
struct HashSalt {
static constexpr size_t Size = 32;
u8 value[Size];
};
static_assert(std::is_pod<HashSalt>::value);
static_assert(sizeof(HashSalt) == HashSalt::Size);
using SaveDataHashSalt = std::optional<HashSalt>;
}

View File

@@ -0,0 +1,32 @@
/*
* 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 <stratosphere/fs/fs_common.hpp>
namespace ams::fs {
enum class SpeedEmulationMode {
None = 0,
Faster = 1,
Slower = 2,
Random = 3,
};
/* TODO */
/* Result SetSpeedEmulationMode(SpeedEmulationMode mode); */
/* Result GetSpeedEmulationMode(SpeedEmulationMode *out); */
}

View File

@@ -0,0 +1,27 @@
/*
* 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::fs {
enum StorageType : s32 {
StorageType_SaveData = 0,
StorageType_RomFs = 1,
StorageType_Authoring = 2,
};
}

View File

@@ -15,5 +15,13 @@
*/
#pragma once
#include "fssrv/fssrv_sf_path.hpp"
#include "fssrv/fssrv_path_normalizer.hpp"
#include <stratosphere/fssrv/fssrv_sf_path.hpp>
#include <stratosphere/fssrv/fssrv_path_normalizer.hpp>
#include <stratosphere/fssrv/fssrv_nca_crypto_configuration.hpp>
#include <stratosphere/fssrv/fssrv_memory_resource_from_standard_allocator.hpp>
#include <stratosphere/fssrv/fssrv_memory_resource_from_exp_heap.hpp>
#include <stratosphere/fssrv/fssrv_i_file_system_creator.hpp>
#include <stratosphere/fssrv/fscreator/fssrv_partition_file_system_creator.hpp>
#include <stratosphere/fssrv/fscreator/fssrv_rom_file_system_creator.hpp>
#include <stratosphere/fssrv/fscreator/fssrv_storage_on_nca_creator.hpp>
#include <stratosphere/fssrv/fssrv_file_system_proxy_api.hpp>

View File

@@ -0,0 +1,31 @@
/*
* 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/fssrv/fssrv_i_file_system_creator.hpp>
namespace ams::fssrv::fscreator {
class PartitionFileSystemCreator : public IPartitionFileSystemCreator {
NON_COPYABLE(PartitionFileSystemCreator);
NON_MOVEABLE(PartitionFileSystemCreator);
public:
PartitionFileSystemCreator() { /* ... */ }
virtual Result Create(std::shared_ptr<fs::fsa::IFileSystem> *out, std::shared_ptr<fs::IStorage> storage) override;
};
}

View File

@@ -0,0 +1,33 @@
/*
* 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/fssrv/fssrv_i_file_system_creator.hpp>
namespace ams::fssrv::fscreator {
class RomFileSystemCreator : public IRomFileSystemCreator {
NON_COPYABLE(RomFileSystemCreator);
NON_MOVEABLE(RomFileSystemCreator);
private:
MemoryResource *allocator;
public:
explicit RomFileSystemCreator(MemoryResource *mr) : allocator(mr) { /* ... */ }
virtual Result Create(std::shared_ptr<fs::fsa::IFileSystem> *out, std::shared_ptr<fs::IStorage> storage) override;
};
}

View File

@@ -0,0 +1,52 @@
/*
* 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/fssrv/fssrv_i_file_system_creator.hpp>
#include <stratosphere/fssystem/buffers/fssystem_i_buffer_manager.hpp>
namespace ams::fssystem {
struct NcaCryptoConfiguration;
}
namespace ams::fssrv::fscreator {
class StorageOnNcaCreator : public IStorageOnNcaCreator {
NON_COPYABLE(StorageOnNcaCreator);
NON_MOVEABLE(StorageOnNcaCreator);
private:
MemoryResource *allocator;
fssystem::IBufferManager * const buffer_manager;
const fssystem::NcaCryptoConfiguration &nca_crypto_cfg;
bool is_prod;
bool is_enabled_program_verification;
private:
Result VerifyNcaHeaderSign2(fssystem::NcaReader *nca_reader, fs::IStorage *storage);
public:
explicit StorageOnNcaCreator(MemoryResource *mr, const fssystem::NcaCryptoConfiguration &cfg, bool prod, fssystem::IBufferManager *bm) : allocator(mr), buffer_manager(bm), nca_crypto_cfg(cfg), is_prod(prod), is_enabled_program_verification(true) {
/* ... */
}
virtual Result Create(std::shared_ptr<fs::IStorage> *out, fssystem::NcaFsHeaderReader *out_header_reader, std::shared_ptr<fssystem::NcaReader> nca_reader, s32 index, bool verify_header_sign_2) override;
virtual Result CreateWithPatch(std::shared_ptr<fs::IStorage> *out, fssystem::NcaFsHeaderReader *out_header_reader, std::shared_ptr<fssystem::NcaReader> original_nca_reader, std::shared_ptr<fssystem::NcaReader> current_nca_reader, s32 index, bool verify_header_sign_2) override;
virtual Result CreateNcaReader(std::shared_ptr<fssystem::NcaReader> *out, std::shared_ptr<fs::IStorage> storage) override;
virtual Result VerifyAcid(fs::fsa::IFileSystem *fs, fssystem::NcaReader *nca_reader) override;
virtual void SetEnabledProgramVerification(bool en) override;
};
}

View File

@@ -0,0 +1,35 @@
/*
* 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::fssrv::fscreator {
struct FileSystemCreatorInterfaces;
}
namespace ams::fssystem {
class IBufferManager;
}
namespace ams::fssrv {
void InitializeForFileSystemProxy(fscreator::FileSystemCreatorInterfaces *fs_creator_interfaces, fssystem::IBufferManager *buffer_manager, bool is_development_function_enabled);
}

View File

@@ -0,0 +1,78 @@
/*
* 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/fs/impl/fs_newable.hpp>
namespace ams::fs {
class IStorage;
enum class BisPartitionId;
namespace fsa {
class IFileSystem;
}
}
namespace ams::fssystem {
class NcaReader;
class NcaFsHeaderReader;
namespace save {
/* TODO */
}
}
namespace ams::fssrv::fscreator {
class IRomFileSystemCreator {
public:
virtual ~IRomFileSystemCreator() { /* ... */ }
virtual Result Create(std::shared_ptr<fs::fsa::IFileSystem> *out, std::shared_ptr<fs::IStorage> storage) = 0;
};
class IPartitionFileSystemCreator {
public:
virtual ~IPartitionFileSystemCreator() { /* ... */ }
virtual Result Create(std::shared_ptr<fs::fsa::IFileSystem> *out, std::shared_ptr<fs::IStorage> storage) = 0;
};
class IStorageOnNcaCreator {
public:
virtual ~IStorageOnNcaCreator() { /* ... */ }
virtual Result Create(std::shared_ptr<fs::IStorage> *out, fssystem::NcaFsHeaderReader *out_header_reader, std::shared_ptr<fssystem::NcaReader> nca_reader, s32 index, bool verify_header_sign_2) = 0;
virtual Result CreateWithPatch(std::shared_ptr<fs::IStorage> *out, fssystem::NcaFsHeaderReader *out_header_reader, std::shared_ptr<fssystem::NcaReader> original_nca_reader, std::shared_ptr<fssystem::NcaReader> current_nca_reader, s32 index, bool verify_header_sign_2) = 0;
virtual Result CreateNcaReader(std::shared_ptr<fssystem::NcaReader> *out, std::shared_ptr<fs::IStorage> storage) = 0;
virtual Result VerifyAcid(fs::fsa::IFileSystem *fs, fssystem::NcaReader *nca_reader) = 0;
virtual void SetEnabledProgramVerification(bool en) = 0;
};
struct FileSystemCreatorInterfaces {
IRomFileSystemCreator *rom_fs_creator;
IPartitionFileSystemCreator *partition_fs_creator;
IStorageOnNcaCreator *storage_on_nca_creator;
/* TODO: More creators. */
};
static_assert(std::is_pod<FileSystemCreatorInterfaces>::value);
}

View File

@@ -0,0 +1,74 @@
/*
* 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/lmem/lmem_exp_heap.hpp>
namespace ams::fssrv {
class MemoryResourceFromExpHeap : public ams::MemoryResource {
private:
lmem::HeapHandle heap_handle;
public:
constexpr explicit MemoryResourceFromExpHeap(lmem::HeapHandle handle) : heap_handle(handle) { /* ... */ }
protected:
virtual void *AllocateImpl(size_t size, size_t align) override {
return lmem::AllocateFromExpHeap(this->heap_handle, size, static_cast<s32>(align));
}
virtual void DeallocateImpl(void *p, size_t size, size_t align) override {
return lmem::FreeToExpHeap(this->heap_handle, p);
}
virtual bool IsEqualImpl(const MemoryResource &rhs) const override {
return false;
}
};
class PeakCheckableMemoryResourceFromExpHeap : public ams::MemoryResource {
private:
lmem::HeapHandle heap_handle;
os::Mutex mutex;
size_t peak_free_size;
size_t current_free_size;
public:
constexpr explicit PeakCheckableMemoryResourceFromExpHeap(size_t heap_size) : heap_handle(nullptr), mutex(false), peak_free_size(heap_size), current_free_size(heap_size) { /* ... */ }
void SetHeapHandle(lmem::HeapHandle handle) {
this->heap_handle = handle;
}
size_t GetPeakFreeSize() const { return this->peak_free_size; }
size_t GetCurrentFreeSize() const { return this->current_free_size; }
void ClearPeak() { this->peak_free_size = this->current_free_size; }
std::scoped_lock<os::Mutex> GetScopedLock() {
return std::scoped_lock(this->mutex);
}
void OnAllocate(void *p, size_t size);
void OnDeallocate(void *p, size_t size);
protected:
virtual void *AllocateImpl(size_t size, size_t align) override;
virtual void DeallocateImpl(void *p, size_t size, size_t align) override;
virtual bool IsEqualImpl(const MemoryResource &rhs) const override {
return false;
}
};
}

View File

@@ -0,0 +1,51 @@
/*
* 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>
namespace ams::mem {
class StandardAllocator;
}
namespace ams::fssrv {
class MemoryResourceFromStandardAllocator : public ams::MemoryResource {
private:
mem::StandardAllocator *allocator;
os::SdkMutex mutex;
size_t peak_free_size;
size_t current_free_size;
size_t peak_allocated_size;
public:
explicit MemoryResourceFromStandardAllocator(mem::StandardAllocator *allocator);
public:
size_t GetPeakFreeSize() const { return this->peak_free_size; }
size_t GetCurrentFreeSize() const { return this->current_free_size; }
size_t GetPeakAllocatedSize() const { return this->peak_allocated_size; }
void ClearPeak();
protected:
virtual void *AllocateImpl(size_t size, size_t align) override;
virtual void DeallocateImpl(void *p, size_t size, size_t align) override;
virtual bool IsEqualImpl(const MemoryResource &rhs) const override {
return false;
}
};
}

View File

@@ -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 <stratosphere/fssystem/fssystem_nca_file_system_driver.hpp>
namespace ams::fssrv {
const ::ams::fssystem::NcaCryptoConfiguration *GetDefaultNcaCryptoConfiguration(bool prod);
}

View File

@@ -15,9 +15,10 @@
*/
#pragma once
#include <stratosphere/fssystem/fssystem_allocator_utility.hpp>
#include <stratosphere/fssystem/fssystem_utility.hpp>
#include <stratosphere/fssystem/fssystem_speed_emulation_configuration.hpp>
#include <stratosphere/fssystem/fssystem_external_code.hpp>
#include <stratosphere/fssystem/fssystem_acid_sign_key.hpp>
#include <stratosphere/fssystem/fssystem_partition_file_system.hpp>
#include <stratosphere/fssystem/fssystem_partition_file_system_meta.hpp>
#include <stratosphere/fssystem/fssystem_path_tool.hpp>
@@ -28,6 +29,22 @@
#include <stratosphere/fssystem/fssystem_directory_redirection_filesystem.hpp>
#include <stratosphere/fssystem/fssystem_directory_savedata_filesystem.hpp>
#include <stratosphere/fssystem/fssystem_romfs_file_system.hpp>
#include <stratosphere/fssystem/fssystem_bucket_tree.hpp>
#include <stratosphere/fssystem/fssystem_bucket_tree_template_impl.hpp>
#include <stratosphere/fssystem/fssystem_indirect_storage.hpp>
#include <stratosphere/fssystem/fssystem_indirect_storage_template_impl.hpp>
#include <stratosphere/fssystem/fssystem_sparse_storage.hpp>
#include <stratosphere/fssystem/fssystem_nca_header.hpp>
#include <stratosphere/fssystem/fssystem_nca_file_system_driver.hpp>
#include <stratosphere/fssystem/fssystem_nca_file_system_driver_impl.hpp>
#include <stratosphere/fssystem/fssystem_crypto_configuration.hpp>
#include <stratosphere/fssystem/fssystem_aes_ctr_counter_extended_storage.hpp>
#include <stratosphere/fssystem/buffers/fssystem_buffer_manager_utils.hpp>
#include <stratosphere/fssystem/buffers/fssystem_file_system_buddy_heap.hpp>
#include <stratosphere/fssystem/buffers/fssystem_file_system_buffer_manager.hpp>
#include <stratosphere/fssystem/fssystem_pooled_buffer.hpp>
#include <stratosphere/fssystem/fssystem_alignment_matching_storage_impl.hpp>
#include <stratosphere/fssystem/fssystem_alignment_matching_storage.hpp>
#include <stratosphere/fssystem/save/fssystem_buffered_storage.hpp>
#include <stratosphere/fssystem/save/fssystem_hierarchical_integrity_verification_storage.hpp>
#include <stratosphere/fssystem/fssystem_integrity_romfs_storage.hpp>
#include <stratosphere/fssystem/fssystem_file_system_proxy_api.hpp>

View File

@@ -87,4 +87,40 @@ namespace ams::fssystem::buffers {
}
};
template<typename IsValidBufferFunction>
Result AllocateBufferUsingBufferManagerContext(std::pair<uintptr_t, size_t> *out, fssystem::IBufferManager *buffer_manager, size_t size, const IBufferManager::BufferAttribute attribute, IsValidBufferFunction is_valid_buffer, const char *func_name) {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(buffer_manager != nullptr);
AMS_ASSERT(func_name != nullptr);
/* Clear the output. */
*out = std::pair<uintptr_t, size_t>(0, 0);
/* Get the context. */
auto context = GetBufferManagerContext();
auto AllocateBufferImpl = [=]() -> Result {
auto buffer = buffer_manager->AllocateBuffer(size, attribute);
if (!is_valid_buffer(buffer)) {
if (buffer.first != 0) {
buffer_manager->DeallocateBuffer(buffer.first, buffer.second);
}
return fs::ResultBufferAllocationFailed();
}
*out = buffer;
return ResultSuccess();
};
if (context == nullptr || !context->IsNeedBlocking()) {
/* If there's no context (or we don't need to block), just allocate the buffer. */
R_TRY(AllocateBufferImpl());
} else {
/* Otherwise, try to allocate repeatedly. */
R_TRY(DoContinuouslyUntilBufferIsAllocated(AllocateBufferImpl, func_name));
}
AMS_ASSERT(out->first != 0);
return ResultSuccess();
}
}

View File

@@ -0,0 +1,295 @@
/*
* 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_memory_management.hpp>
#include <stratosphere/fssystem/buffers/fssystem_i_buffer_manager.hpp>
#include <stratosphere/fssystem/buffers/fssystem_file_system_buddy_heap.hpp>
namespace ams::fssystem {
class FileSystemBufferManager : public IBufferManager {
NON_COPYABLE(FileSystemBufferManager);
NON_MOVEABLE(FileSystemBufferManager);
public:
using BuddyHeap = FileSystemBuddyHeap;
private:
class CacheHandleTable {
NON_COPYABLE(CacheHandleTable);
NON_MOVEABLE(CacheHandleTable);
private:
class Entry {
private:
CacheHandle handle;
uintptr_t address;
size_t size;
BufferAttribute attr;
public:
constexpr void Initialize(CacheHandle h, uintptr_t a, size_t sz, BufferAttribute t) {
this->handle = h;
this->address = a;
this->size = sz;
this->attr = t;
}
constexpr CacheHandle GetHandle() const {
return this->handle;
}
constexpr uintptr_t GetAddress() const {
return this->address;
}
constexpr size_t GetSize() const {
return this->size;
}
constexpr BufferAttribute GetBufferAttribute() const {
return this->attr;
}
};
class AttrInfo : public util::IntrusiveListBaseNode<AttrInfo>, public ::ams::fs::impl::Newable {
NON_COPYABLE(AttrInfo);
NON_MOVEABLE(AttrInfo);
private:
s32 level;
s32 cache_count;
size_t cache_size;
public:
constexpr AttrInfo(s32 l, s32 cc, size_t cs) : level(l), cache_count(cc), cache_size(cs) {
/* ... */
}
constexpr s32 GetLevel() const {
return this->level;
}
constexpr s32 GetCacheCount() const {
return this->cache_count;
}
constexpr void IncrementCacheCount() {
++this->cache_count;
}
constexpr void DecrementCacheCount() {
--this->cache_count;
}
constexpr size_t GetCacheSize() const {
return this->cache_size;
}
constexpr void AddCacheSize(size_t diff) {
this->cache_size += diff;
}
constexpr void SubtractCacheSize(size_t diff) {
AMS_ASSERT(this->cache_size >= diff);
this->cache_size -= diff;
}
using Newable::operator new;
using Newable::operator delete;
static void *operator new(size_t, void *p) {
return p;
}
static void operator delete(void *, size_t, void*) { /* ... */ }
};
using AttrListTraits = util::IntrusiveListBaseTraits<AttrInfo>;
using AttrList = typename AttrListTraits::ListType;
private:
std::unique_ptr<char[], ::ams::fs::impl::Deleter> internal_entry_buffer;
char *external_entry_buffer;
size_t entry_buffer_size;
Entry *entries;
s32 entry_count;
s32 entry_count_max;
AttrList attr_list;
char *external_attr_info_buffer;
s32 external_attr_info_count;
s32 cache_count_min;
size_t cache_size_min;
size_t total_cache_size;
CacheHandle current_handle;
public:
static constexpr size_t QueryWorkBufferSize(s32 max_cache_count) {
AMS_ASSERT(max_cache_count > 0);
const auto entry_size = sizeof(Entry) * max_cache_count;
const auto attr_list_size = sizeof(AttrInfo) * 0x100;
return util::AlignUp(entry_size + attr_list_size + alignof(Entry) + alignof(AttrInfo), 8);
}
public:
CacheHandleTable() : internal_entry_buffer(), external_entry_buffer(), entry_buffer_size(), entries(), entry_count(), entry_count_max(), attr_list(), external_attr_info_buffer(), external_attr_info_count(), cache_count_min(), cache_size_min(), total_cache_size(), current_handle() {
/* ... */
}
~CacheHandleTable() {
this->Finalize();
}
Result Initialize(s32 max_cache_count);
Result Initialize(s32 max_cache_count, void *work, size_t work_size) {
const auto aligned_entry_buf = util::AlignUp(reinterpret_cast<uintptr_t>(work), alignof(Entry));
this->external_entry_buffer = reinterpret_cast<char *>(aligned_entry_buf);
this->entry_buffer_size = sizeof(Entry) * max_cache_count;
const auto aligned_attr_info_buf = util::AlignUp(reinterpret_cast<uintptr_t>(this->external_entry_buffer + this->entry_buffer_size), alignof(AttrInfo));
const auto work_end = reinterpret_cast<uintptr_t>(work) + work_size;
this->external_attr_info_buffer = reinterpret_cast<char *>(aligned_attr_info_buf);
this->external_attr_info_count = static_cast<s32>((work_end - aligned_attr_info_buf) / sizeof(AttrInfo));
return ResultSuccess();
}
void Finalize();
bool Register(CacheHandle *out, uintptr_t address, size_t size, const BufferAttribute &attr);
bool Unregister(uintptr_t *out_address, size_t *out_size, CacheHandle handle);
bool UnregisterOldest(uintptr_t *out_address, size_t *out_size, const BufferAttribute &attr, size_t required_size = 0);
CacheHandle PublishCacheHandle();
size_t GetTotalCacheSize() const;
private:
void UnregisterCore(uintptr_t *out_address, size_t *out_size, Entry *entry);
Entry *AcquireEntry(uintptr_t address, size_t size, const BufferAttribute &attr);
void ReleaseEntry(Entry *entry);
AttrInfo *FindAttrInfo(const BufferAttribute &attr);
s32 GetCacheCountMin(const BufferAttribute &attr) {
return this->cache_count_min;
}
size_t GetCacheSizeMin(const BufferAttribute &attr) {
return this->cache_size_min;
}
};
private:
BuddyHeap buddy_heap;
CacheHandleTable cache_handle_table;
size_t total_size;
size_t peak_free_size;
size_t peak_total_allocatable_size;
size_t retried_count;
mutable os::Mutex mutex;
public:
static constexpr size_t QueryWorkBufferSize(s32 max_cache_count, s32 max_order) {
const auto buddy_size = FileSystemBuddyHeap::QueryWorkBufferSize(max_order);
const auto table_size = CacheHandleTable::QueryWorkBufferSize(max_cache_count);
return buddy_size + table_size;
}
public:
FileSystemBufferManager() : total_size(), peak_free_size(), peak_total_allocatable_size(), retried_count(), mutex(true) { /* ... */ }
virtual ~FileSystemBufferManager() { /* ... */ }
Result Initialize(s32 max_cache_count, uintptr_t address, size_t buffer_size, size_t block_size) {
AMS_ASSERT(buffer_size > 0);
R_TRY(this->cache_handle_table.Initialize(max_cache_count));
R_TRY(this->buddy_heap.Initialize(address, buffer_size, block_size));
this->total_size = this->buddy_heap.GetTotalFreeSize();
this->peak_free_size = this->total_size;
this->peak_total_allocatable_size = this->total_size;
return ResultSuccess();
}
Result Initialize(s32 max_cache_count, uintptr_t address, size_t buffer_size, size_t block_size, s32 max_order) {
AMS_ASSERT(buffer_size > 0);
R_TRY(this->cache_handle_table.Initialize(max_cache_count));
R_TRY(this->buddy_heap.Initialize(address, buffer_size, block_size, max_order));
this->total_size = this->buddy_heap.GetTotalFreeSize();
this->peak_free_size = this->total_size;
this->peak_total_allocatable_size = this->total_size;
return ResultSuccess();
}
Result Initialize(s32 max_cache_count, uintptr_t address, size_t buffer_size, size_t block_size, void *work, size_t work_size) {
const auto table_size = CacheHandleTable::QueryWorkBufferSize(max_cache_count);
const auto buddy_size = work_size - table_size;
AMS_ASSERT(work_size > table_size);
const auto table_buffer = static_cast<char *>(work);
const auto buddy_buffer = table_buffer + table_size;
R_TRY(this->cache_handle_table.Initialize(max_cache_count, table_buffer, table_size));
R_TRY(this->buddy_heap.Initialize(address, buffer_size, block_size, buddy_buffer, buddy_size));
this->total_size = this->buddy_heap.GetTotalFreeSize();
this->peak_free_size = this->total_size;
this->peak_total_allocatable_size = this->total_size;
return ResultSuccess();
}
Result Initialize(s32 max_cache_count, uintptr_t address, size_t buffer_size, size_t block_size, s32 max_order, void *work, size_t work_size) {
const auto table_size = CacheHandleTable::QueryWorkBufferSize(max_cache_count);
const auto buddy_size = work_size - table_size;
AMS_ASSERT(work_size > table_size);
const auto table_buffer = static_cast<char *>(work);
const auto buddy_buffer = table_buffer + table_size;
R_TRY(this->cache_handle_table.Initialize(max_cache_count, table_buffer, table_size));
R_TRY(this->buddy_heap.Initialize(address, buffer_size, block_size, max_order, buddy_buffer, buddy_size));
this->total_size = this->buddy_heap.GetTotalFreeSize();
this->peak_free_size = this->total_size;
this->peak_total_allocatable_size = this->total_size;
return ResultSuccess();
}
void Finalize() {
this->buddy_heap.Finalize();
this->cache_handle_table.Finalize();
}
private:
virtual const std::pair<uintptr_t, size_t> AllocateBufferImpl(size_t size, const BufferAttribute &attr) override;
virtual void DeallocateBufferImpl(uintptr_t address, size_t size) override;
virtual CacheHandle RegisterCacheImpl(uintptr_t address, size_t size, const BufferAttribute &attr) override;
virtual const std::pair<uintptr_t, size_t> AcquireCacheImpl(CacheHandle handle) override;
virtual size_t GetTotalSizeImpl() const override;
virtual size_t GetFreeSizeImpl() const override;
virtual size_t GetTotalAllocatableSizeImpl() const override;
virtual size_t GetPeakFreeSizeImpl() const override;
virtual size_t GetPeakTotalAllocatableSizeImpl() const override;
virtual size_t GetRetriedCountImpl() const override;
virtual void ClearPeakImpl() override;
};
}

View File

@@ -0,0 +1,110 @@
/*
* 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 {
class IBufferManager {
public:
class BufferAttribute {
private:
s32 level;
public:
constexpr BufferAttribute() : level(0) { /* ... */ }
constexpr explicit BufferAttribute(s32 l) : level(l) { /* ... */ }
constexpr s32 GetLevel() const { return this->level; }
};
using CacheHandle = s64;
static constexpr s32 BufferLevelMin = 0;
public:
virtual ~IBufferManager() { /* ... */ }
const std::pair<uintptr_t, size_t> AllocateBuffer(size_t size, const BufferAttribute &attr) {
return this->AllocateBufferImpl(size, attr);
}
const std::pair<uintptr_t, size_t> AllocateBuffer(size_t size) {
return this->AllocateBufferImpl(size, BufferAttribute());
}
void DeallocateBuffer(uintptr_t address, size_t size) {
return this->DeallocateBufferImpl(address, size);
}
CacheHandle RegisterCache(uintptr_t address, size_t size, const BufferAttribute &attr) {
return this->RegisterCacheImpl(address, size, attr);
}
const std::pair<uintptr_t, size_t> AcquireCache(CacheHandle handle) {
return this->AcquireCacheImpl(handle);
}
size_t GetTotalSize() const {
return this->GetTotalSizeImpl();
}
size_t GetFreeSize() const {
return this->GetFreeSizeImpl();
}
size_t GetTotalAllocatableSize() const {
return this->GetTotalAllocatableSizeImpl();
}
size_t GetPeakFreeSize() const {
return this->GetPeakFreeSizeImpl();
}
size_t GetPeakTotalAllocatableSize() const {
return this->GetPeakTotalAllocatableSizeImpl();
}
size_t GetRetriedCount() const {
return this->GetRetriedCountImpl();
}
void ClearPeak() const {
return this->ClearPeak();
}
protected:
virtual const std::pair<uintptr_t, size_t> AllocateBufferImpl(size_t size, const BufferAttribute &attr) = 0;
virtual void DeallocateBufferImpl(uintptr_t address, size_t size) = 0;
virtual CacheHandle RegisterCacheImpl(uintptr_t address, size_t size, const BufferAttribute &attr) = 0;
virtual const std::pair<uintptr_t, size_t> AcquireCacheImpl(CacheHandle handle) = 0;
virtual size_t GetTotalSizeImpl() const = 0;
virtual size_t GetFreeSizeImpl() const = 0;
virtual size_t GetTotalAllocatableSizeImpl() const = 0;
virtual size_t GetPeakFreeSizeImpl() const = 0;
virtual size_t GetPeakTotalAllocatableSizeImpl() const = 0;
virtual size_t GetRetriedCountImpl() const = 0;
virtual void ClearPeakImpl() = 0;
};
}

View File

@@ -0,0 +1,43 @@
/*
* 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::dbm {
namespace {
constexpr inline s32 CountLeadingZeros(u32 val) {
return util::CountLeadingZeros(val);
}
constexpr inline s32 CountLeadingOnes(u32 val) {
return CountLeadingZeros(~val);
}
inline u32 ReadU32(const u8 *buf, size_t index) {
u32 val;
std::memcpy(std::addressof(val), buf + index, sizeof(u32));
return val;
}
inline void WriteU32(u8 *buf, size_t index, u32 val) {
std::memcpy(buf + index, std::addressof(val), sizeof(u32));
}
}
}

View File

@@ -1,111 +0,0 @@
/*
* 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 {
constexpr inline size_t AcidSignatureKeyGenerationMax = 1;
constexpr inline size_t AcidSignatureKeyModulusSize = 0x100;
constexpr inline const u8 AcidSignatureKeyModulusDev[AcidSignatureKeyGenerationMax + 1][AcidSignatureKeyModulusSize] = {
{
0xD6, 0x34, 0xA5, 0x78, 0x6C, 0x68, 0xCE, 0x5A, 0xC2, 0x37, 0x17, 0xF3, 0x82, 0x45, 0xC6, 0x89,
0xE1, 0x2D, 0x06, 0x67, 0xBF, 0xB4, 0x06, 0x19, 0x55, 0x6B, 0x27, 0x66, 0x0C, 0xA4, 0xB5, 0x87,
0x81, 0x25, 0xF4, 0x30, 0xBC, 0x53, 0x08, 0x68, 0xA2, 0x48, 0x49, 0x8C, 0x3F, 0x38, 0x40, 0x9C,
0xC4, 0x26, 0xF4, 0x79, 0xE2, 0xA1, 0x85, 0xF5, 0x5C, 0x7F, 0x58, 0xBA, 0xA6, 0x1C, 0xA0, 0x8B,
0x84, 0x16, 0x14, 0x6F, 0x85, 0xD9, 0x7C, 0xE1, 0x3C, 0x67, 0x22, 0x1E, 0xFB, 0xD8, 0xA7, 0xA5,
0x9A, 0xBF, 0xEC, 0x0E, 0xCF, 0x96, 0x7E, 0x85, 0xC2, 0x1D, 0x49, 0x5D, 0x54, 0x26, 0xCB, 0x32,
0x7C, 0xF6, 0xBB, 0x58, 0x03, 0x80, 0x2B, 0x5D, 0xF7, 0xFB, 0xD1, 0x9D, 0xC7, 0xC6, 0x2E, 0x53,
0xC0, 0x6F, 0x39, 0x2C, 0x1F, 0xA9, 0x92, 0xF2, 0x4D, 0x7D, 0x4E, 0x74, 0xFF, 0xE4, 0xEF, 0xE4,
0x7C, 0x3D, 0x34, 0x2A, 0x71, 0xA4, 0x97, 0x59, 0xFF, 0x4F, 0xA2, 0xF4, 0x66, 0x78, 0xD8, 0xBA,
0x99, 0xE3, 0xE6, 0xDB, 0x54, 0xB9, 0xE9, 0x54, 0xA1, 0x70, 0xFC, 0x05, 0x1F, 0x11, 0x67, 0x4B,
0x26, 0x8C, 0x0C, 0x3E, 0x03, 0xD2, 0xA3, 0x55, 0x5C, 0x7D, 0xC0, 0x5D, 0x9D, 0xFF, 0x13, 0x2F,
0xFD, 0x19, 0xBF, 0xED, 0x44, 0xC3, 0x8C, 0xA7, 0x28, 0xCB, 0xE5, 0xE0, 0xB1, 0xA7, 0x9C, 0x33,
0x8D, 0xB8, 0x6E, 0xDE, 0x87, 0x18, 0x22, 0x60, 0xC4, 0xAE, 0xF2, 0x87, 0x9F, 0xCE, 0x09, 0x5C,
0xB5, 0x99, 0xA5, 0x9F, 0x49, 0xF2, 0xD7, 0x58, 0xFA, 0xF9, 0xC0, 0x25, 0x7D, 0xD6, 0xCB, 0xF3,
0xD8, 0x6C, 0xA2, 0x69, 0x91, 0x68, 0x73, 0xB1, 0x94, 0x6F, 0xA3, 0xF3, 0xB9, 0x7D, 0xF8, 0xE0,
0x72, 0x9E, 0x93, 0x7B, 0x7A, 0xA2, 0x57, 0x60, 0xB7, 0x5B, 0xA9, 0x84, 0xAE, 0x64, 0x88, 0x69
},
{
0xBC, 0xA5, 0x6A, 0x7E, 0xEA, 0x38, 0x34, 0x62, 0xA6, 0x10, 0x18, 0x3C, 0xE1, 0x63, 0x7B, 0xF0,
0xD3, 0x08, 0x8C, 0xF5, 0xC5, 0xC4, 0xC7, 0x93, 0xE9, 0xD9, 0xE6, 0x32, 0xF3, 0xA0, 0xF6, 0x6E,
0x8A, 0x98, 0x76, 0x47, 0x33, 0x47, 0x65, 0x02, 0x70, 0xDC, 0x86, 0x5F, 0x3D, 0x61, 0x5A, 0x70,
0xBC, 0x5A, 0xCA, 0xCA, 0x50, 0xAD, 0x61, 0x7E, 0xC9, 0xEC, 0x27, 0xFF, 0xE8, 0x64, 0x42, 0x9A,
0xEE, 0xBE, 0xC3, 0xD1, 0x0B, 0xC0, 0xE9, 0xBF, 0x83, 0x8D, 0xC0, 0x0C, 0xD8, 0x00, 0x5B, 0x76,
0x90, 0xD2, 0x4B, 0x30, 0x84, 0x35, 0x8B, 0x1E, 0x20, 0xB7, 0xE4, 0xDC, 0x63, 0xE5, 0xDF, 0xCD,
0x00, 0x5F, 0x81, 0x5F, 0x67, 0xC5, 0x8B, 0xDF, 0xFC, 0xE1, 0x37, 0x5F, 0x07, 0xD9, 0xDE, 0x4F,
0xE6, 0x7B, 0xF1, 0xFB, 0xA1, 0x5A, 0x71, 0x40, 0xFE, 0xBA, 0x1E, 0xAE, 0x13, 0x22, 0xD2, 0xFE,
0x37, 0xA2, 0xB6, 0x8B, 0xAB, 0xEB, 0x84, 0x81, 0x4E, 0x7C, 0x1E, 0x02, 0xD1, 0xFB, 0xD7, 0x5D,
0x11, 0x84, 0x64, 0xD2, 0x4D, 0xBB, 0x50, 0x00, 0x67, 0x54, 0xE2, 0x77, 0x89, 0xBA, 0x0B, 0xE7,
0x05, 0x57, 0x9A, 0x22, 0x5A, 0xEC, 0x76, 0x1C, 0xFD, 0xE8, 0xA8, 0x18, 0x16, 0x41, 0x65, 0x03,
0xFA, 0xC4, 0xA6, 0x31, 0x5C, 0x1A, 0x7F, 0xAB, 0x11, 0xC8, 0x4A, 0x99, 0xB9, 0xE6, 0xCF, 0x62,
0x21, 0xA6, 0x72, 0x47, 0xDB, 0xBA, 0x96, 0x26, 0x4E, 0x2E, 0xD4, 0x8C, 0x46, 0xD6, 0xA7, 0x1A,
0x6C, 0x32, 0xA7, 0xDF, 0x85, 0x1C, 0x03, 0xC3, 0x6D, 0xA9, 0xE9, 0x68, 0xF4, 0x17, 0x1E, 0xB2,
0x70, 0x2A, 0xA1, 0xE5, 0xE1, 0xF3, 0x8F, 0x6F, 0x63, 0xAC, 0xEB, 0x72, 0x0B, 0x4C, 0x4A, 0x36,
0x3C, 0x60, 0x91, 0x9F, 0x6E, 0x1C, 0x71, 0xEA, 0xD0, 0x78, 0x78, 0xA0, 0x2E, 0xC6, 0x32, 0x6B
}
};
constexpr inline const u8 AcidSignatureKeyModulusProd[AcidSignatureKeyGenerationMax + 1][AcidSignatureKeyModulusSize] = {
{
0xDD, 0xC8, 0xDD, 0xF2, 0x4E, 0x6D, 0xF0, 0xCA, 0x9E, 0xC7, 0x5D, 0xC7, 0x7B, 0xAD, 0xFE, 0x7D,
0x23, 0x89, 0x69, 0xB6, 0xF2, 0x06, 0xA2, 0x02, 0x88, 0xE1, 0x55, 0x91, 0xAB, 0xCB, 0x4D, 0x50,
0x2E, 0xFC, 0x9D, 0x94, 0x76, 0xD6, 0x4C, 0xD8, 0xFF, 0x10, 0xFA, 0x5E, 0x93, 0x0A, 0xB4, 0x57,
0xAC, 0x51, 0xC7, 0x16, 0x66, 0xF4, 0x1A, 0x54, 0xC2, 0xC5, 0x04, 0x3D, 0x1B, 0xFE, 0x30, 0x20,
0x8A, 0xAC, 0x6F, 0x6F, 0xF5, 0xC7, 0xB6, 0x68, 0xB8, 0xC9, 0x40, 0x6B, 0x42, 0xAD, 0x11, 0x21,
0xE7, 0x8B, 0xE9, 0x75, 0x01, 0x86, 0xE4, 0x48, 0x9B, 0x0A, 0x0A, 0xF8, 0x7F, 0xE8, 0x87, 0xF2,
0x82, 0x01, 0xE6, 0xA3, 0x0F, 0xE4, 0x66, 0xAE, 0x83, 0x3F, 0x4E, 0x9F, 0x5E, 0x01, 0x30, 0xA4,
0x00, 0xB9, 0x9A, 0xAE, 0x5F, 0x03, 0xCC, 0x18, 0x60, 0xE5, 0xEF, 0x3B, 0x5E, 0x15, 0x16, 0xFE,
0x1C, 0x82, 0x78, 0xB5, 0x2F, 0x47, 0x7C, 0x06, 0x66, 0x88, 0x5D, 0x35, 0xA2, 0x67, 0x20, 0x10,
0xE7, 0x6C, 0x43, 0x68, 0xD3, 0xE4, 0x5A, 0x68, 0x2A, 0x5A, 0xE2, 0x6D, 0x73, 0xB0, 0x31, 0x53,
0x1C, 0x20, 0x09, 0x44, 0xF5, 0x1A, 0x9D, 0x22, 0xBE, 0x12, 0xA1, 0x77, 0x11, 0xE2, 0xA1, 0xCD,
0x40, 0x9A, 0xA2, 0x8B, 0x60, 0x9B, 0xEF, 0xA0, 0xD3, 0x48, 0x63, 0xA2, 0xF8, 0xA3, 0x2C, 0x08,
0x56, 0x52, 0x2E, 0x60, 0x19, 0x67, 0x5A, 0xA7, 0x9F, 0xDC, 0x3F, 0x3F, 0x69, 0x2B, 0x31, 0x6A,
0xB7, 0x88, 0x4A, 0x14, 0x84, 0x80, 0x33, 0x3C, 0x9D, 0x44, 0xB7, 0x3F, 0x4C, 0xE1, 0x75, 0xEA,
0x37, 0xEA, 0xE8, 0x1E, 0x7C, 0x77, 0xB7, 0xC6, 0x1A, 0xA2, 0xF0, 0x9F, 0x10, 0x61, 0xCD, 0x7B,
0x5B, 0x32, 0x4C, 0x37, 0xEF, 0xB1, 0x71, 0x68, 0x53, 0x0A, 0xED, 0x51, 0x7D, 0x35, 0x22, 0xFD
},
{
0xE7, 0xAA, 0x25, 0xC8, 0x01, 0xA5, 0x14, 0x6B, 0x01, 0x60, 0x3E, 0xD9, 0x96, 0x5A, 0xBF, 0x90,
0xAC, 0xA7, 0xFD, 0x9B, 0x5B, 0xBD, 0x8A, 0x26, 0xB0, 0xCB, 0x20, 0x28, 0x9A, 0x72, 0x12, 0xF5,
0x20, 0x65, 0xB3, 0xB9, 0x84, 0x58, 0x1F, 0x27, 0xBC, 0x7C, 0xA2, 0xC9, 0x9E, 0x18, 0x95, 0xCF,
0xC2, 0x73, 0x2E, 0x74, 0x8C, 0x66, 0xE5, 0x9E, 0x79, 0x2B, 0xB8, 0x07, 0x0C, 0xB0, 0x4E, 0x8E,
0xAB, 0x85, 0x21, 0x42, 0xC4, 0xC5, 0x6D, 0x88, 0x9C, 0xDB, 0x15, 0x95, 0x3F, 0x80, 0xDB, 0x7A,
0x9A, 0x7D, 0x41, 0x56, 0x25, 0x17, 0x18, 0x42, 0x4D, 0x8C, 0xAC, 0xA5, 0x7B, 0xDB, 0x42, 0x5D,
0x59, 0x35, 0x45, 0x5D, 0x8A, 0x02, 0xB5, 0x70, 0xC0, 0x72, 0x35, 0x46, 0xD0, 0x1D, 0x60, 0x01,
0x4A, 0xCC, 0x1C, 0x46, 0xD3, 0xD6, 0x35, 0x52, 0xD6, 0xE1, 0xF8, 0x3B, 0x5D, 0xEA, 0xDD, 0xB8,
0xFE, 0x7D, 0x50, 0xCB, 0x35, 0x23, 0x67, 0x8B, 0xB6, 0xE4, 0x74, 0xD2, 0x60, 0xFC, 0xFD, 0x43,
0xBF, 0x91, 0x08, 0x81, 0xC5, 0x4F, 0x5D, 0x16, 0x9A, 0xC4, 0x9A, 0xC6, 0xF6, 0xF3, 0xE1, 0xF6,
0x5C, 0x07, 0xAA, 0x71, 0x6C, 0x13, 0xA4, 0xB1, 0xB3, 0x66, 0xBF, 0x90, 0x4C, 0x3D, 0xA2, 0xC4,
0x0B, 0xB8, 0x3D, 0x7A, 0x8C, 0x19, 0xFA, 0xFF, 0x6B, 0xB9, 0x1F, 0x02, 0xCC, 0xB6, 0xD3, 0x0C,
0x7D, 0x19, 0x1F, 0x47, 0xF9, 0xC7, 0x40, 0x01, 0xFA, 0x46, 0xEA, 0x0B, 0xD4, 0x02, 0xE0, 0x3D,
0x30, 0x9A, 0x1A, 0x0F, 0xEA, 0xA7, 0x66, 0x55, 0xF7, 0xCB, 0x28, 0xE2, 0xBB, 0x99, 0xE4, 0x83,
0xC3, 0x43, 0x03, 0xEE, 0xDC, 0x1F, 0x02, 0x23, 0xDD, 0xD1, 0x2D, 0x39, 0xA4, 0x65, 0x75, 0x03,
0xEF, 0x37, 0x9C, 0x06, 0xD6, 0xFA, 0xA1, 0x15, 0xF0, 0xDB, 0x17, 0x47, 0x26, 0x4F, 0x49, 0x03
}
};
static_assert(sizeof(AcidSignatureKeyModulusProd) == sizeof(AcidSignatureKeyModulusDev));
constexpr inline const u8 AcidSignatureKeyExponent[] = {
0x01, 0x00, 0x01
};
constexpr inline size_t AcidSignatureKeyExponentSize = util::size(AcidSignatureKeyExponent);
}

View File

@@ -0,0 +1,116 @@
/*
* 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/fssystem/fssystem_aes_ctr_storage.hpp>
#include <stratosphere/fssystem/fssystem_bucket_tree.hpp>
namespace ams::fssystem {
class AesCtrCounterExtendedStorage : public ::ams::fs::IStorage, public ::ams::fs::impl::Newable {
NON_COPYABLE(AesCtrCounterExtendedStorage);
NON_MOVEABLE(AesCtrCounterExtendedStorage);
public:
static constexpr size_t BlockSize = crypto::Aes128CtrEncryptor::BlockSize;
static constexpr size_t KeySize = crypto::Aes128CtrEncryptor::KeySize;
static constexpr size_t IvSize = crypto::Aes128CtrEncryptor::IvSize;
static constexpr size_t NodeSize = 16_KB;
using IAllocator = BucketTree::IAllocator;
using DecryptFunction = void(*)(void *dst, size_t dst_size, s32 index, const void *enc_key, size_t enc_key_size, const void *iv, size_t iv_size, const void *src, size_t src_size);
class IDecryptor {
public:
virtual ~IDecryptor() { /* ... */ }
virtual void Decrypt(void *buf, size_t buf_size, const void *enc_key, size_t enc_key_size, void *iv, size_t iv_size) = 0;
virtual bool HasExternalDecryptionKey() const = 0;
};
struct Entry {
u8 offset[sizeof(s64)];
s32 reserved;
s32 generation;
void SetOffset(s64 value) {
std::memcpy(this->offset, std::addressof(value), sizeof(s64));
}
s64 GetOffset() const {
s64 value;
std::memcpy(std::addressof(value), this->offset, sizeof(s64));
return value;
}
};
static_assert(sizeof(Entry) == 0x10);
static_assert(alignof(Entry) == 4);
static_assert(std::is_pod<Entry>::value);
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);
}
static Result CreateExternalDecryptor(std::unique_ptr<IDecryptor> *out, DecryptFunction func, s32 key_index);
static Result CreateSoftwareDecryptor(std::unique_ptr<IDecryptor> *out);
private:
BucketTree table;
fs::SubStorage data_storage;
u8 key[KeySize];
u32 secure_value;
s64 counter_offset;
std::unique_ptr<IDecryptor> decryptor;
public:
AesCtrCounterExtendedStorage() : table(), data_storage(), secure_value(), counter_offset(), decryptor() { /* ... */ }
virtual ~AesCtrCounterExtendedStorage() { this->Finalize(); }
Result Initialize(IAllocator *allocator, const void *key, size_t key_size, u32 secure_value, s64 counter_offset, fs::SubStorage data_storage, fs::SubStorage node_storage, fs::SubStorage entry_storage, s32 entry_count, std::unique_ptr<IDecryptor> &&decryptor);
void Finalize();
bool IsInitialized() const { return this->table.IsInitialized(); }
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);
*out = this->table.GetSize();
return ResultSuccess();
}
virtual Result Flush() override {
return ResultSuccess();
}
virtual Result Write(s64 offset, const void *buffer, size_t size) override {
return fs::ResultUnsupportedOperationInAesCtrCounterExtendedStorageA();
}
virtual Result SetSize(s64 size) override {
return fs::ResultUnsupportedOperationInAesCtrCounterExtendedStorageB();
}
private:
Result Initialize(IAllocator *allocator, const void *key, size_t key_size, u32 secure_value, fs::SubStorage data_storage, fs::SubStorage table_storage);
};
}

View File

@@ -0,0 +1,309 @@
/*
* 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/fs/fs_istorage.hpp>
#include <stratosphere/fs/impl/fs_newable.hpp>
#include <stratosphere/fssystem/fssystem_alignment_matching_storage_impl.hpp>
#include <stratosphere/fssystem/fssystem_pooled_buffer.hpp>
namespace ams::fssystem {
template<size_t _DataAlign, size_t _BufferAlign>
class AlignmentMatchingStorage : public ::ams::fs::IStorage, public ::ams::fs::impl::Newable {
NON_COPYABLE(AlignmentMatchingStorage);
NON_MOVEABLE(AlignmentMatchingStorage);
public:
static constexpr size_t DataAlign = _DataAlign;
static constexpr size_t BufferAlign = _BufferAlign;
static constexpr size_t DataAlignMax = 0x200;
static_assert(DataAlign <= DataAlignMax);
static_assert(util::IsPowerOfTwo(DataAlign));
static_assert(util::IsPowerOfTwo(BufferAlign));
private:
std::shared_ptr<fs::IStorage> shared_base_storage;
fs::IStorage * const base_storage;
s64 base_storage_size;
bool is_base_storage_size_dirty;
public:
explicit AlignmentMatchingStorage(fs::IStorage *bs) : base_storage(bs), is_base_storage_size_dirty(true) {
/* ... */
}
explicit AlignmentMatchingStorage(std::shared_ptr<fs::IStorage> bs) : shared_base_storage(bs), base_storage(shared_base_storage.get()), is_base_storage_size_dirty(true) {
/* ... */
}
virtual Result Read(s64 offset, void *buffer, size_t size) override {
/* Allocate a work buffer on stack. */
__attribute__((aligned(DataAlignMax))) char work_buf[DataAlign];
static_assert(util::IsAligned(alignof(work_buf), BufferAlign));
/* Succeed if zero size. */
R_SUCCEED_IF(size == 0);
/* Validate arguments. */
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
s64 bs_size = 0;
R_TRY(this->GetSize(std::addressof(bs_size)));
R_UNLESS(fs::IStorage::IsRangeValid(offset, size, bs_size), fs::ResultOutOfRange());
return AlignmentMatchingStorageImpl::Read(this->base_storage, work_buf, sizeof(work_buf), DataAlign, BufferAlign, offset, static_cast<char *>(buffer), size);
}
virtual Result Write(s64 offset, const void *buffer, size_t size) override {
/* Allocate a work buffer on stack. */
__attribute__((aligned(DataAlignMax))) char work_buf[DataAlign];
static_assert(util::IsAligned(alignof(work_buf), BufferAlign));
/* Succeed if zero size. */
R_SUCCEED_IF(size == 0);
/* Validate arguments. */
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
s64 bs_size = 0;
R_TRY(this->GetSize(std::addressof(bs_size)));
R_UNLESS(fs::IStorage::IsRangeValid(offset, size, bs_size), fs::ResultOutOfRange());
return AlignmentMatchingStorageImpl::Write(this->base_storage, work_buf, sizeof(work_buf), DataAlign, BufferAlign, offset, static_cast<const char *>(buffer), size);
}
virtual Result Flush() override {
return this->base_storage->Flush();
}
virtual Result SetSize(s64 size) override {
ON_SCOPE_EXIT { this->is_base_storage_size_dirty = true; };
return this->base_storage->SetSize(util::AlignUp(size, DataAlign));
}
virtual Result GetSize(s64 *out) override {
AMS_ASSERT(out != nullptr);
if (this->is_base_storage_size_dirty) {
s64 size;
R_TRY(this->base_storage->GetSize(std::addressof(size)));
this->base_storage_size = size;
this->is_base_storage_size_dirty = false;
}
*out = this->base_storage_size;
return ResultSuccess();
}
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 {
/* Succeed if zero size. */
R_SUCCEED_IF(size == 0);
/* Get the base storage size. */
s64 bs_size = 0;
R_TRY(this->GetSize(std::addressof(bs_size)));
R_UNLESS(fs::IStorage::IsOffsetAndSizeValid(offset, size), fs::ResultOutOfRange());
/* Operate on the base storage. */
const auto valid_size = std::min(size, bs_size - offset);
const auto aligned_offset = util::AlignDown(offset, DataAlign);
const auto aligned_offset_end = util::AlignUp(offset + valid_size, DataAlign);
const auto aligned_size = aligned_offset_end - aligned_offset;
return this->base_storage->OperateRange(dst, dst_size, op_id, aligned_offset, aligned_size, src, src_size);
}
};
template<size_t _BufferAlign>
class AlignmentMatchingStoragePooledBuffer : public ::ams::fs::IStorage, public ::ams::fs::impl::Newable {
NON_COPYABLE(AlignmentMatchingStoragePooledBuffer);
NON_MOVEABLE(AlignmentMatchingStoragePooledBuffer);
public:
static constexpr size_t BufferAlign = _BufferAlign;
static_assert(util::IsPowerOfTwo(BufferAlign));
private:
fs::IStorage * const base_storage;
s64 base_storage_size;
size_t data_align;
bool is_base_storage_size_dirty;
public:
explicit AlignmentMatchingStoragePooledBuffer(fs::IStorage *bs, size_t da) : base_storage(bs), data_align(da), is_base_storage_size_dirty(true) {
AMS_ASSERT(util::IsPowerOfTwo(da));
}
virtual Result Read(s64 offset, void *buffer, size_t size) override {
/* Succeed if zero size. */
R_SUCCEED_IF(size == 0);
/* Validate arguments. */
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
s64 bs_size = 0;
R_TRY(this->GetSize(std::addressof(bs_size)));
R_UNLESS(fs::IStorage::IsRangeValid(offset, size, bs_size), fs::ResultOutOfRange());
/* Allocate a pooled buffer. */
PooledBuffer pooled_buffer;
pooled_buffer.AllocateParticularlyLarge(this->data_align, this->data_align);
return AlignmentMatchingStorageImpl::Read(this->base_storage, pooled_buffer.GetBuffer(), pooled_buffer.GetSize(), this->data_align, BufferAlign, offset, static_cast<char *>(buffer), size);
}
virtual Result Write(s64 offset, const void *buffer, size_t size) override {
/* Succeed if zero size. */
R_SUCCEED_IF(size == 0);
/* Validate arguments. */
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
s64 bs_size = 0;
R_TRY(this->GetSize(std::addressof(bs_size)));
R_UNLESS(fs::IStorage::IsRangeValid(offset, size, bs_size), fs::ResultOutOfRange());
/* Allocate a pooled buffer. */
PooledBuffer pooled_buffer;
pooled_buffer.AllocateParticularlyLarge(this->data_align, this->data_align);
return AlignmentMatchingStorageImpl::Write(this->base_storage, pooled_buffer.GetBuffer(), pooled_buffer.GetSize(), this->data_align, BufferAlign, offset, static_cast<const char *>(buffer), size);
}
virtual Result Flush() override {
return this->base_storage->Flush();
}
virtual Result SetSize(s64 size) override {
ON_SCOPE_EXIT { this->is_base_storage_size_dirty = true; };
return this->base_storage->SetSize(util::AlignUp(size, this->data_align));
}
virtual Result GetSize(s64 *out) override {
AMS_ASSERT(out != nullptr);
if (this->is_base_storage_size_dirty) {
s64 size;
R_TRY(this->base_storage->GetSize(std::addressof(size)));
this->base_storage_size = size;
this->is_base_storage_size_dirty = false;
}
*out = this->base_storage_size;
return ResultSuccess();
}
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 {
/* Succeed if zero size. */
R_SUCCEED_IF(size == 0);
/* Get the base storage size. */
s64 bs_size = 0;
R_TRY(this->GetSize(std::addressof(bs_size)));
R_UNLESS(fs::IStorage::IsOffsetAndSizeValid(offset, size), fs::ResultOutOfRange());
/* Operate on the base storage. */
const auto valid_size = std::min(size, bs_size - offset);
const auto aligned_offset = util::AlignDown(offset, this->data_align);
const auto aligned_offset_end = util::AlignUp(offset + valid_size, this->data_align);
const auto aligned_size = aligned_offset_end - aligned_offset;
return this->base_storage->OperateRange(dst, dst_size, op_id, aligned_offset, aligned_size, src, src_size);
}
};
template<size_t _BufferAlign>
class AlignmentMatchingStorageInBulkRead : public ::ams::fs::IStorage, public ::ams::fs::impl::Newable {
NON_COPYABLE(AlignmentMatchingStorageInBulkRead);
NON_MOVEABLE(AlignmentMatchingStorageInBulkRead);
public:
static constexpr size_t BufferAlign = _BufferAlign;
static_assert(util::IsPowerOfTwo(BufferAlign));
private:
std::shared_ptr<fs::IStorage> shared_base_storage;
fs::IStorage * const base_storage;
s64 base_storage_size;
size_t data_align;
public:
explicit AlignmentMatchingStorageInBulkRead(fs::IStorage *bs, size_t da) : shared_base_storage(), base_storage(bs), base_storage_size(-1), data_align(da) {
AMS_ASSERT(util::IsPowerOfTwo(this->data_align));
}
explicit AlignmentMatchingStorageInBulkRead(std::shared_ptr<fs::IStorage> bs, size_t da) : shared_base_storage(bs), base_storage(shared_base_storage.get()), base_storage_size(-1), data_align(da) {
AMS_ASSERT(util::IsPowerOfTwo(da));
}
virtual Result Read(s64 offset, void *buffer, size_t size) override;
virtual Result Write(s64 offset, const void *buffer, size_t size) override {
/* Succeed if zero size. */
R_SUCCEED_IF(size == 0);
/* Validate arguments. */
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
s64 bs_size = 0;
R_TRY(this->GetSize(std::addressof(bs_size)));
R_UNLESS(fs::IStorage::IsRangeValid(offset, size, bs_size), fs::ResultOutOfRange());
/* Allocate a pooled buffer. */
PooledBuffer pooled_buffer(this->data_align, this->data_align);
return AlignmentMatchingStorageImpl::Write(this->base_storage, pooled_buffer.GetBuffer(), pooled_buffer.GetSize(), this->data_align, BufferAlign, offset, static_cast<const char *>(buffer), size);
}
virtual Result Flush() override {
return this->base_storage->Flush();
}
virtual Result SetSize(s64 size) override {
ON_SCOPE_EXIT { this->base_storage_size = -1; };
return this->base_storage->SetSize(util::AlignUp(size, this->data_align));
}
virtual Result GetSize(s64 *out) override {
AMS_ASSERT(out != nullptr);
if (this->base_storage_size < 0) {
s64 size;
R_TRY(this->base_storage->GetSize(std::addressof(size)));
this->base_storage_size = size;
}
*out = this->base_storage_size;
return ResultSuccess();
}
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 {
/* Succeed if zero size. */
R_SUCCEED_IF(size == 0);
/* Get the base storage size. */
s64 bs_size = 0;
R_TRY(this->GetSize(std::addressof(bs_size)));
R_UNLESS(fs::IStorage::IsOffsetAndSizeValid(offset, size), fs::ResultOutOfRange());
/* Operate on the base storage. */
const auto valid_size = std::min(size, bs_size - offset);
const auto aligned_offset = util::AlignDown(offset, this->data_align);
const auto aligned_offset_end = util::AlignUp(offset + valid_size, this->data_align);
const auto aligned_size = aligned_offset_end - aligned_offset;
return this->base_storage->OperateRange(dst, dst_size, op_id, aligned_offset, aligned_size, src, src_size);
}
};
}

View File

@@ -0,0 +1,28 @@
/*
* 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/fs/fs_istorage.hpp>
namespace ams::fssystem {
class AlignmentMatchingStorageImpl {
public:
static Result Read(fs::IStorage *base_storage, char *work_buf, size_t work_buf_size, size_t data_alignment, size_t buffer_alignment, s64 offset, char *buffer, size_t size);
static Result Write(fs::IStorage *base_storage, char *work_buf, size_t work_buf_size, size_t data_alignment, size_t buffer_alignment, s64 offset, const char *buffer, size_t size);
};
}

View File

@@ -0,0 +1,64 @@
/*
* 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 {
using AllocateFunction = void *(*)(size_t size);
using DeallocateFunction = void (*)(void *ptr, size_t size);
void InitializeAllocator(AllocateFunction allocate_func, DeallocateFunction deallocate_func);
void *Allocate(size_t size);
void Deallocate(void *ptr, size_t size);
template<typename T>
class StdAllocator : public std::allocator<T> {
public:
StdAllocator() { /* ... */ }
StdAllocator(const StdAllocator &) { /* ... */ }
template<class U>
StdAllocator(const StdAllocator<U> &) { /* ... */ }
template<typename U>
struct rebind {
using other = StdAllocator<U>;
};
T *Allocate(size_t size, const T *hint = nullptr) {
return static_cast<T *>(::ams::fssystem::Allocate(sizeof(T) * size));
}
void Deallocate(T *ptr, size_t size) {
return ::ams::fssystem::Deallocate(ptr, sizeof(T) * size);
}
ALWAYS_INLINE T *allocate(size_t size, const T *hint = nullptr) { return this->Allocate(size, hint); }
ALWAYS_INLINE void deallocate(T *ptr, size_t size) { return this->Deallocate(ptr, size); }
};
template<typename T>
std::shared_ptr<T> AllocateShared() {
return std::allocate_shared<T>(StdAllocator<T>{});
}
template<typename T, typename... Args>
std::shared_ptr<T> AllocateShared(Args &&... args) {
return std::allocate_shared<T>(StdAllocator<T>{}, std::forward<Args>(args)...);
}
}

View File

@@ -0,0 +1,330 @@
/*
* 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/fs/fs_substorage.hpp>
namespace ams::fssystem {
class BucketTree {
NON_COPYABLE(BucketTree);
NON_MOVEABLE(BucketTree);
public:
static constexpr u32 Magic = util::FourCC<'B','K','T','R'>::Code;
static constexpr u32 Version = 1;
static constexpr size_t NodeSizeMin = 1_KB;
static constexpr size_t NodeSizeMax = 512_KB;
public:
class Visitor;
struct Header {
u32 magic;
u32 version;
s32 entry_count;
s32 reserved;
void Format(s32 entry_count);
Result Verify() const;
};
static_assert(std::is_pod<Header>::value);
static_assert(sizeof(Header) == 0x10);
struct NodeHeader {
s32 index;
s32 count;
s64 offset;
Result Verify(s32 node_index, size_t node_size, size_t entry_size) const;
};
static_assert(std::is_pod<NodeHeader>::value);
static_assert(sizeof(NodeHeader) == 0x10);
class ContinuousReadingInfo {
private:
size_t read_size;
s32 skip_count;
bool done;
public:
constexpr ContinuousReadingInfo() : read_size(), skip_count(), done() { /* ... */ }
constexpr void Reset() { this->read_size = 0; this->skip_count = 0; this->done = false; }
constexpr void SetSkipCount(s32 count) { AMS_ASSERT(count >= 0); this->skip_count = count; }
constexpr s32 GetSkipCount() const { return this->skip_count; }
constexpr bool CheckNeedScan() { return (--this->skip_count) <= 0; }
constexpr void Done() { this->read_size = 0; this->done = true; }
constexpr bool IsDone() const { return this->done; }
constexpr void SetReadSize(size_t size) { this->read_size = size; }
constexpr size_t GetReadSize() const { return this->read_size; }
constexpr bool CanDo() const { return this->read_size > 0; }
};
using IAllocator = MemoryResource;
private:
class NodeBuffer {
NON_COPYABLE(NodeBuffer);
private:
IAllocator *allocator;
void *header;
public:
NodeBuffer() : allocator(), header() { /* ... */ }
~NodeBuffer() {
AMS_ASSERT(this->header == nullptr);
}
NodeBuffer(NodeBuffer &&rhs) : allocator(rhs.allocator), header(rhs.allocator) {
rhs.allocator = nullptr;
rhs.header = nullptr;
}
NodeBuffer &operator=(NodeBuffer &&rhs) {
if (this != std::addressof(rhs)) {
AMS_ASSERT(this->header == nullptr);
this->allocator = rhs.allocator;
this->header = rhs.header;
rhs.allocator = nullptr;
rhs.header = nullptr;
}
return *this;
}
bool Allocate(IAllocator *allocator, size_t node_size) {
AMS_ASSERT(this->header == nullptr);
this->allocator = allocator;
this->header = allocator->Allocate(node_size, sizeof(s64));
AMS_ASSERT(util::IsAligned(this->header, sizeof(s64)));
return this->header != nullptr;
}
void Free(size_t node_size) {
if (this->header) {
this->allocator->Deallocate(this->header, node_size);
this->header = nullptr;
}
this->allocator = nullptr;
}
void FillSzero(size_t node_size) const {
if (this->header) {
std::memset(this->header, 0, node_size);
}
}
NodeHeader *Get() const {
return reinterpret_cast<NodeHeader *>(this->header);
}
NodeHeader *operator->() const { return this->Get(); }
template<typename T>
T *Get() const {
static_assert(std::is_pod<T>::value);
static_assert(sizeof(T) == sizeof(NodeHeader));
return reinterpret_cast<T *>(this->header);
}
IAllocator *GetAllocator() const {
return this->allocator;
}
};
private:
static constexpr s32 GetEntryCount(size_t node_size, size_t entry_size) {
return static_cast<s32>((node_size - sizeof(NodeHeader)) / entry_size);
}
static constexpr s32 GetOffsetCount(size_t node_size) {
return static_cast<s32>((node_size - sizeof(NodeHeader)) / sizeof(s64));
}
static constexpr s32 GetEntrySetCount(size_t node_size, size_t entry_size, s32 entry_count) {
const s32 entry_count_per_node = GetEntryCount(node_size, entry_size);
return util::DivideUp(entry_count, entry_count_per_node);
}
static constexpr s32 GetNodeL2Count(size_t node_size, size_t entry_size, s32 entry_count) {
const s32 offset_count_per_node = GetOffsetCount(node_size);
const s32 entry_set_count = GetEntrySetCount(node_size, entry_size, entry_count);
if (entry_set_count <= offset_count_per_node) {
return 0;
}
const s32 node_l2_count = util::DivideUp(entry_set_count, offset_count_per_node);
AMS_ABORT_UNLESS(node_l2_count <= offset_count_per_node);
return util::DivideUp(entry_set_count - (offset_count_per_node - (node_l2_count - 1)), offset_count_per_node);
}
public:
static constexpr s64 QueryHeaderStorageSize() { return sizeof(Header); }
static constexpr s64 QueryNodeStorageSize(size_t node_size, size_t entry_size, s32 entry_count) {
AMS_ASSERT(entry_size >= sizeof(s64));
AMS_ASSERT(node_size >= entry_size + sizeof(NodeHeader));
AMS_ASSERT(NodeSizeMin <= node_size && node_size <= NodeSizeMax);
AMS_ASSERT(util::IsPowerOfTwo(node_size));
AMS_ASSERT(entry_count >= 0);
if (entry_count <= 0) {
return 0;
}
return (1 + GetNodeL2Count(node_size, entry_size, entry_count)) * static_cast<s64>(node_size);
}
static constexpr s64 QueryEntryStorageSize(size_t node_size, size_t entry_size, s32 entry_count) {
AMS_ASSERT(entry_size >= sizeof(s64));
AMS_ASSERT(node_size >= entry_size + sizeof(NodeHeader));
AMS_ASSERT(NodeSizeMin <= node_size && node_size <= NodeSizeMax);
AMS_ASSERT(util::IsPowerOfTwo(node_size));
AMS_ASSERT(entry_count >= 0);
if (entry_count <= 0) {
return 0;
}
return GetEntrySetCount(node_size, entry_size, entry_count) * static_cast<s64>(node_size);
}
private:
mutable fs::SubStorage node_storage;
mutable fs::SubStorage entry_storage;
NodeBuffer node_l1;
size_t node_size;
size_t entry_size;
s32 entry_count;
s32 offset_count;
s32 entry_set_count;
s64 start_offset;
s64 end_offset;
public:
BucketTree() : node_storage(), entry_storage(), node_l1(), node_size(), entry_size(), entry_count(), offset_count(), entry_set_count(), start_offset(), end_offset() { /* ... */ }
~BucketTree() { this->Finalize(); }
Result Initialize(IAllocator *allocator, fs::SubStorage node_storage, fs::SubStorage entry_storage, size_t node_size, size_t entry_size, s32 entry_count);
void Initialize(size_t node_size, s64 end_offset);
void Finalize();
bool IsInitialized() const { return this->node_size > 0; }
bool IsEmpty() const { return this->entry_size == 0; }
Result Find(Visitor *visitor, s64 virtual_address) const;
Result InvalidateCache();
s32 GetEntryCount() const { return this->entry_count; }
IAllocator *GetAllocator() const { return this->node_l1.GetAllocator(); }
s64 GetStart() const { return this->start_offset; }
s64 GetEnd() const { return this->end_offset; }
s64 GetSize() const { return this->end_offset - this->start_offset; }
bool Includes(s64 offset) const {
return this->start_offset <= offset && offset < this->end_offset;
}
bool Includes(s64 offset, s64 size) const {
return size > 0 && this->start_offset <= offset && size <= this->end_offset - offset;
}
private:
template<typename EntryType>
struct ContinuousReadingParam {
s64 offset;
size_t size;
NodeHeader entry_set;
s32 entry_index;
EntryType entry;
};
private:
template<typename EntryType>
Result ScanContinuousReading(ContinuousReadingInfo *out_info, const ContinuousReadingParam<EntryType> &param) const;
bool IsExistL2() const { return this->offset_count < this->entry_set_count; }
bool IsExistOffsetL2OnL1() const { return this->IsExistL2() && this->node_l1->count < this->offset_count; }
s64 GetEntrySetIndex(s32 node_index, s32 offset_index) const {
return (this->offset_count - this->node_l1->count) + (this->offset_count * node_index) + offset_index;
}
};
class BucketTree::Visitor {
NON_COPYABLE(Visitor);
NON_MOVEABLE(Visitor);
private:
friend class BucketTree;
union EntrySetHeader {
NodeHeader header;
struct Info {
s32 index;
s32 count;
s64 end;
s64 start;
} info;
static_assert(std::is_pod<Info>::value);
};
static_assert(std::is_pod<EntrySetHeader>::value);
private:
const BucketTree *tree;
void *entry;
s32 entry_index;
s32 entry_set_count;
EntrySetHeader entry_set;
public:
constexpr Visitor() : tree(), entry(), entry_index(-1), entry_set_count(), entry_set{} { /* ... */ }
~Visitor() {
if (this->entry != nullptr) {
this->tree->GetAllocator()->Deallocate(this->entry, this->tree->entry_size);
this->tree = nullptr;
this->entry = nullptr;
}
}
bool IsValid() const { return this->entry_index >= 0; }
bool CanMoveNext() const { return this->IsValid() && (this->entry_index + 1 < this->entry_set.info.count || this->entry_set.info.index + 1 < this->entry_set.info.count); }
bool CanMovePrevious() const { return this->IsValid() && (this->entry_index > 0 || this->entry_set.info.index > 0); }
Result MoveNext();
Result MovePrevious();
template<typename EntryType>
Result ScanContinuousReading(ContinuousReadingInfo *out_info, s64 offset, size_t size) const;
const void *Get() const { AMS_ASSERT(this->IsValid()); return this->entry; }
template<typename T>
const T *Get() const { AMS_ASSERT(this->IsValid()); return reinterpret_cast<const T *>(this->entry); }
const BucketTree *GetTree() const { return this->tree; }
private:
Result Initialize(const BucketTree *tree);
Result Find(s64 virtual_address);
Result FindEntrySet(s32 *out_index, s64 virtual_address, s32 node_index);
Result FindEntrySetWithBuffer(s32 *out_index, s64 virtual_address, s32 node_index, char *buffer);
Result FindEntrySetWithoutBuffer(s32 *out_index, s64 virtual_address, s32 node_index);
Result FindEntry(s64 virtual_address, s32 entry_set_index);
Result FindEntryWithBuffer(s64 virtual_address, s32 entry_set_index, char *buffer);
Result FindEntryWithoutBuffer(s64 virtual_address, s32 entry_set_index);
};
}

View File

@@ -0,0 +1,165 @@
/*
* 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 <stratosphere/fssystem/fssystem_pooled_buffer.hpp>
#include <stratosphere/fssystem/fssystem_bucket_tree.hpp>
#include <stratosphere/fssystem/fssystem_bucket_tree_utils.hpp>
namespace ams::fssystem {
template<typename EntryType>
Result BucketTree::ScanContinuousReading(ContinuousReadingInfo *out_info, const ContinuousReadingParam<EntryType> &param) const {
static_assert(std::is_pod<ContinuousReadingParam<EntryType>>::value);
/* Validate our preconditions. */
AMS_ASSERT(this->IsInitialized());
AMS_ASSERT(out_info != nullptr);
AMS_ASSERT(this->entry_size == sizeof(EntryType));
/* Reset the output. */
out_info->Reset();
/* If there's nothing to read, we're done. */
R_SUCCEED_IF(param.size == 0);
/* If we're reading a fragment, we're done. */
R_SUCCEED_IF(param.entry.IsFragment());
/* Validate the first entry. */
auto entry = param.entry;
auto cur_offset = param.offset;
R_UNLESS(entry.GetVirtualOffset() <= cur_offset, fs::ResultOutOfRange());
/* Create a pooled buffer for our scan. */
PooledBuffer pool(this->node_size, 1);
char *buffer = nullptr;
/* Read the node. */
if (this->node_size <= pool.GetSize()) {
buffer = pool.GetBuffer();
const auto ofs = param.entry_set.index * static_cast<s64>(this->node_size);
R_TRY(this->entry_storage.Read(ofs, buffer, this->node_size));
}
/* Calculate extents. */
const auto end_offset = cur_offset + static_cast<s64>(param.size);
s64 phys_offset = entry.GetPhysicalOffset();
/* Start merge tracking. */
s64 merge_size = 0;
s64 readable_size = 0;
bool merged = false;
/* Iterate. */
auto entry_index = param.entry_index;
for (const auto entry_count = param.entry_set.count; entry_index < entry_count; ++entry_index) {
/* If we're past the end, we're done. */
if (end_offset <= cur_offset) {
break;
}
/* Validate the entry offset. */
const auto entry_offset = entry.GetVirtualOffset();
R_UNLESS(entry_offset <= cur_offset, fs::ResultInvalidIndirectEntryOffset());
/* Get the next entry. */
EntryType next_entry = {};
s64 next_entry_offset;
if (entry_index + 1 < entry_count) {
if (buffer != nullptr) {
const auto ofs = impl::GetBucketTreeEntryOffset(0, this->entry_size, entry_index + 1);
std::memcpy(std::addressof(next_entry), buffer + ofs, this->entry_size);
} else {
const auto ofs = impl::GetBucketTreeEntryOffset(param.entry_set.index, this->node_size, this->entry_size, entry_index + 1);
R_TRY(this->entry_storage.Read(ofs, std::addressof(next_entry), this->entry_size));
}
next_entry_offset = next_entry.GetVirtualOffset();
R_UNLESS(this->Includes(next_entry_offset), fs::ResultInvalidIndirectEntryOffset());
} else {
next_entry_offset = param.entry_set.offset;
}
/* Validate the next entry offset. */
R_UNLESS(cur_offset < next_entry_offset, fs::ResultInvalidIndirectEntryOffset());
/* Determine the much data there is. */
const auto data_size = next_entry_offset - cur_offset;
AMS_ASSERT(data_size > 0);
/* Determine how much data we should read. */
const auto remaining_size = end_offset - cur_offset;
const size_t read_size = static_cast<size_t>(std::min(data_size, remaining_size));
AMS_ASSERT(read_size <= param.size);
/* Update our merge tracking. */
if (entry.IsFragment()) {
/* If we can't merge, stop looping. */
if (EntryType::FragmentSizeMax <= read_size || remaining_size <= data_size) {
break;
}
/* Otherwise, add the current size to the merge size. */
merge_size += read_size;
} else {
/* If we can't merge, stop looping. */
if (phys_offset != entry.GetPhysicalOffset()) {
break;
}
/* Add the size to the readable amount. */
readable_size += merge_size + read_size;
AMS_ASSERT(readable_size <= static_cast<s64>(param.size));
/* Update whether we've merged. */
merged |= merge_size > 0;
merge_size = 0;
}
/* Advance. */
cur_offset += read_size;
AMS_ASSERT(cur_offset <= end_offset);
phys_offset += next_entry_offset - entry_offset;
entry = next_entry;
}
/* If we merged, set our readable size. */
if (merged) {
out_info->SetReadSize(static_cast<size_t>(readable_size));
}
out_info->SetSkipCount(entry_index - param.entry_index);
return ResultSuccess();
}
template<typename EntryType>
Result BucketTree::Visitor::ScanContinuousReading(ContinuousReadingInfo *out_info, s64 offset, size_t size) const {
static_assert(std::is_pod<EntryType>::value);
AMS_ASSERT(this->IsValid());
/* Create our parameters. */
ContinuousReadingParam<EntryType> param = {
offset, size, this->entry_set.header, this->entry_index
};
std::memcpy(std::addressof(param.entry), this->entry, sizeof(EntryType));
/* Scan. */
return this->tree->ScanContinuousReading<EntryType>(out_info, param);
}
}

View File

@@ -0,0 +1,85 @@
/*
* 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 <stratosphere/fssystem/fssystem_bucket_tree.hpp>
namespace ams::fssystem::impl {
class SafeValue {
public:
static ALWAYS_INLINE s64 GetInt64(const void *ptr) {
s64 value;
std::memcpy(std::addressof(value), ptr, sizeof(s64));
return value;
}
static ALWAYS_INLINE s64 GetInt64(const s64 *ptr) {
return GetInt64(static_cast<const void *>(ptr));
}
static ALWAYS_INLINE s64 GetInt64(const s64 &v) {
return GetInt64(std::addressof(v));
}
static ALWAYS_INLINE void SetInt64(void *dst, const void *src) {
std::memcpy(dst, src, sizeof(s64));
}
static ALWAYS_INLINE void SetInt64(void *dst, const s64 *src) {
return SetInt64(dst, static_cast<const void *>(src));
}
static ALWAYS_INLINE void SetInt64(void *dst, const s64 &v) {
return SetInt64(dst, std::addressof(v));
}
};
template<typename IteratorType>
struct BucketTreeNode {
using Header = BucketTree::NodeHeader;
Header header;
s32 GetCount() const { return this->header.count; }
void *GetArray() { return std::addressof(this->header) + 1; }
template<typename T> T *GetArray() { return reinterpret_cast<T *>(this->GetArray()); }
const void *GetArray() const { return std::addressof(this->header) + 1; }
template<typename T> const T *GetArray() const { return reinterpret_cast<const T *>(this->GetArray()); }
s64 GetBeginOffset() const { return *this->GetArray<s64>(); }
s64 GetEndOffset() const { return this->header.offset; }
IteratorType GetBegin() { return IteratorType(this->GetArray<s64>()); }
IteratorType GetEnd() { return IteratorType(this->GetArray<s64>()) + this->header.count; }
IteratorType GetBegin() const { return IteratorType(this->GetArray<s64>()); }
IteratorType GetEnd() const { return IteratorType(this->GetArray<s64>()) + this->header.count; }
IteratorType GetBegin(size_t entry_size) { return IteratorType(this->GetArray(), entry_size); }
IteratorType GetEnd(size_t entry_size) { return IteratorType(this->GetArray(), entry_size) + this->header.count; }
IteratorType GetBegin(size_t entry_size) const { return IteratorType(this->GetArray(), entry_size); }
IteratorType GetEnd(size_t entry_size) const { return IteratorType(this->GetArray(), entry_size) + this->header.count; }
};
constexpr inline s64 GetBucketTreeEntryOffset(s64 entry_set_offset, size_t entry_size, s32 entry_index) {
return entry_set_offset + sizeof(BucketTree::NodeHeader) + entry_index * static_cast<s64>(entry_size);
}
constexpr inline s64 GetBucketTreeEntryOffset(s32 entry_set_index, size_t node_size, size_t entry_size, s32 entry_index) {
return GetBucketTreeEntryOffset(entry_set_index * static_cast<s64>(node_size), entry_size, entry_index);
}
}

View File

@@ -0,0 +1,34 @@
/*
* 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/fssystem/fssystem_nca_file_system_driver.hpp>
namespace ams::fssystem {
const ::ams::fssystem::NcaCryptoConfiguration *GetNcaCryptoConfiguration(bool prod);
void SetUpKekAccessKeys(bool prod);
void InvalidateHardwareAesKey();
const u8 *GetAcidSignatureKeyModulus(bool prod, size_t key_generation);
const u8 *GetAcidSignatureKeyPublicExponent();
constexpr inline size_t AcidSignatureKeyModulusSize = NcaCryptoConfiguration::Rsa2048KeyModulusSize;
constexpr inline size_t AcidSignatureKeyPublicExponentSize = NcaCryptoConfiguration::Rsa2048KeyPublicExponentSize;
}

View File

@@ -0,0 +1,33 @@
/*
* 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::fssrv::fscreator {
struct FileSystemCreatorInterfaces;
}
namespace ams::fssystem {
/* TODO: This is kind of really a fs process function/tied into fs main. */
/* This should be re-examined when FS is reimplemented. */
void InitializeForFileSystemProxy();
const ::ams::fssrv::fscreator::FileSystemCreatorInterfaces *GetFileSystemCreatorInterfaces();
}

View File

@@ -0,0 +1,163 @@
/*
* 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/fssystem/fssystem_bucket_tree.hpp>
namespace ams::fssystem {
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;
}
};
static_assert(std::is_pod<Entry>::value);
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;
}
};
static_assert(std::is_pod<EntryData>::value);
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;
}
};
static_assert(std::is_pod<ContinuousReadingEntry>::value);
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:
BucketTree table;
fs::SubStorage data_storage[StorageCount];
public:
IndirectStorage() : table(), data_storage() { /* ... */ }
virtual ~IndirectStorage() { this->Finalize(); }
Result Initialize(IAllocator *allocator, fs::SubStorage table_storage);
void Finalize();
bool IsInitialized() const { return this->table.IsInitialized(); }
Result Initialize(IAllocator *allocator, fs::SubStorage node_storage, fs::SubStorage entry_storage, s32 entry_count) {
return this->table.Initialize(allocator, node_storage, entry_storage, NodeSize, sizeof(Entry), entry_count);
}
void SetStorage(s32 idx, fs::SubStorage storage) {
AMS_ASSERT(0 <= idx && idx < StorageCount);
this->data_storage[idx] = storage;
}
template<typename T>
void SetStorage(s32 idx, T storage, s64 offset, s64 size) {
AMS_ASSERT(0 <= idx && idx < StorageCount);
this->data_storage[idx] = fs::SubStorage(storage, offset, size);
}
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);
*out = this->table.GetSize();
return ResultSuccess();
}
virtual Result Flush() override {
return ResultSuccess();
}
virtual Result Write(s64 offset, const void *buffer, size_t size) override {
return fs::ResultUnsupportedOperationInIndirectStorageA();
}
virtual Result SetSize(s64 size) override {
return fs::ResultUnsupportedOperationInIndirectStorageB();
}
protected:
BucketTree &GetEntryTable() { return this->table; }
fs::SubStorage &GetDataStorage(s32 index) {
AMS_ASSERT(0 <= index && index < StorageCount);
return this->data_storage[index];
}
template<bool ContinuousCheck, typename F>
Result OperatePerEntry(s64 offset, s64 size, F func);
};
}

View File

@@ -0,0 +1,113 @@
/*
* 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 <stratosphere/fssystem/fssystem_indirect_storage.hpp>
namespace ams::fssystem {
template<bool ContinuousCheck, typename F>
Result IndirectStorage::OperatePerEntry(s64 offset, s64 size, F func) {
/* Validate preconditions. */
AMS_ASSERT(offset >= 0);
AMS_ASSERT(size >= 0);
AMS_ASSERT(this->IsInitialized());
/* Succeed if there's nothing to operate on. */
R_SUCCEED_IF(size == 0);
/* Validate arguments. */
R_UNLESS(this->table.Includes(offset, size), fs::ResultOutOfRange());
/* Find the offset in our tree. */
BucketTree::Visitor visitor;
R_TRY(this->table.Find(std::addressof(visitor), offset));
{
const auto entry_offset = visitor.Get<Entry>()->GetVirtualOffset();
R_UNLESS(0 <= entry_offset && this->table.Includes(entry_offset), fs::ResultInvalidIndirectEntryOffset());
}
/* Prepare to operate in chunks. */
auto cur_offset = offset;
const auto end_offset = offset + static_cast<s64>(size);
BucketTree::ContinuousReadingInfo cr_info;
while (cur_offset < end_offset) {
/* Get the current entry. */
const auto cur_entry = *visitor.Get<Entry>();
/* Get and validate the entry's offset. */
const auto cur_entry_offset = cur_entry.GetVirtualOffset();
R_UNLESS(cur_entry_offset <= cur_offset, fs::ResultInvalidIndirectEntryOffset());
/* Validate the storage index. */
R_UNLESS(0 <= cur_entry.storage_index && cur_entry.storage_index < StorageCount, fs::ResultInvalidIndirectEntryStorageIndex());
/* If we need to check the continuous info, do so. */
if constexpr (ContinuousCheck) {
/* Scan, if we need to. */
if (cr_info.CheckNeedScan()) {
R_TRY(visitor.ScanContinuousReading<ContinuousReadingEntry>(std::addressof(cr_info), cur_offset, static_cast<size_t>(end_offset - cur_offset)));
}
/* Process a base storage entry. */
if (cr_info.CanDo()) {
R_UNLESS(cur_entry.storage_index == 0, fs::ResultInvalidIndirectEntryStorageIndex());
const auto data_offset = cur_offset - cur_entry_offset;
R_TRY(func(std::addressof(this->data_storage[0]), cur_entry.GetPhysicalOffset() + data_offset, cur_offset, static_cast<s64>(cr_info.GetReadSize())));
}
}
/* Get and validate the next entry offset. */
s64 next_entry_offset;
if (visitor.CanMoveNext()) {
R_TRY(visitor.MoveNext());
next_entry_offset = visitor.Get<Entry>()->GetVirtualOffset();
R_UNLESS(this->table.Includes(next_entry_offset), fs::ResultInvalidIndirectEntryOffset());
} else {
next_entry_offset = this->table.GetEnd();
}
R_UNLESS(cur_offset < next_entry_offset, fs::ResultInvalidIndirectEntryOffset());
/* Get the offset of the entry in the data we read. */
const auto data_offset = cur_offset - cur_entry_offset;
const auto data_size = (next_entry_offset - cur_entry_offset) - data_offset;
AMS_ASSERT(data_size > 0);
/* Determine how much is left. */
const auto remaining_size = end_offset - cur_offset;
const auto cur_size = std::min(remaining_size, data_size);
AMS_ASSERT(cur_size <= size);
/* Operate, if we need to. */
bool needs_operate;
if constexpr (!ContinuousCheck) {
needs_operate = true;
} else {
needs_operate = !cr_info.IsDone() || cur_entry.storage_index != 0;
}
if (needs_operate) {
R_TRY(func(std::addressof(this->data_storage[cur_entry.storage_index]), cur_entry.GetPhysicalOffset() + data_offset, cur_offset, cur_size));
}
cur_offset += cur_size;
}
return ResultSuccess();
}
}

View File

@@ -0,0 +1,74 @@
/*
* 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/fs/fs_istorage.hpp>
#include <stratosphere/fs/fs_memory_storage.hpp>
#include <stratosphere/fs/impl/fs_newable.hpp>
#include <stratosphere/fssystem/fssystem_nca_header.hpp>
#include <stratosphere/fssystem/save/fssystem_hierarchical_integrity_verification_storage.hpp>
namespace ams::fssystem {
constexpr inline size_t IntegrityLayerCountRomFs = 7;
constexpr inline size_t IntegrityHashLayerBlockSize = 16_KB;
class IntegrityRomFsStorage : public ::ams::fs::IStorage, public ::ams::fs::impl::Newable {
private:
save::HierarchicalIntegrityVerificationStorage integrity_storage;
save::FileSystemBufferManagerSet buffers;
os::Mutex mutex;
Hash master_hash;
std::unique_ptr<fs::MemoryStorage> master_hash_storage;
public:
IntegrityRomFsStorage() : mutex(true) { /* ... */ }
virtual ~IntegrityRomFsStorage() override { this->Finalize(); }
Result Initialize(save::HierarchicalIntegrityVerificationInformation level_hash_info, Hash master_hash, save::HierarchicalIntegrityVerificationStorage::HierarchicalStorageInformation storage_info, IBufferManager *bm);
void Finalize();
virtual Result Read(s64 offset, void *buffer, size_t size) override {
return this->integrity_storage.Read(offset, buffer, size);
}
virtual Result Write(s64 offset, const void *buffer, size_t size) override {
return this->integrity_storage.Write(offset, buffer, size);
}
virtual Result SetSize(s64 size) override { return fs::ResultUnsupportedOperationInIntegrityRomFsStorageA(); }
virtual Result GetSize(s64 *out) override {
return this->integrity_storage.GetSize(out);
}
virtual Result Flush() override {
return this->integrity_storage.Flush();
}
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 {
return this->integrity_storage.OperateRange(dst, dst_size, op_id, offset, size, src, src_size);
}
Result Commit() {
return this->integrity_storage.Commit();
}
save::FileSystemBufferManagerSet *GetBuffers() {
return this->integrity_storage.GetBuffers();
}
};
}

View File

@@ -0,0 +1,226 @@
/*
* 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/fs/impl/fs_newable.hpp>
#include <stratosphere/fs/fs_istorage.hpp>
#include <stratosphere/fssystem/fssystem_nca_header.hpp>
#include <stratosphere/fssystem/buffers/fssystem_i_buffer_manager.hpp>
namespace ams::fssystem {
class AesCtrCounterExtendedStorage;
class IndirectStorage;
class SparseStorage;
struct NcaCryptoConfiguration;
using KeyGenerationFunction = void (*)(void *dst_key, size_t dst_key_size, const void *src_key, size_t src_key_size, s32 key_type, const NcaCryptoConfiguration &cfg);
using DecryptAesCtrFunction = void (*)(void *dst, size_t dst_size, s32 key_type, const void *src_key, size_t src_key_size, const void *iv, size_t iv_size, const void *src, size_t src_size);
struct NcaCryptoConfiguration {
static constexpr size_t Rsa2048KeyModulusSize = crypto::Rsa2048PssSha256Verifier::ModulusSize;
static constexpr size_t Rsa2048KeyPublicExponentSize = crypto::Rsa2048PssSha256Verifier::MaximumExponentSize;
static constexpr size_t Rsa2048KeyPrivateExponentSize = Rsa2048KeyModulusSize;
static constexpr size_t Aes128KeySize = crypto::AesEncryptor128::KeySize;
static constexpr size_t Header1SignatureKeyGenerationMax = 1;
static constexpr s32 KeyAreaEncryptionKeyIndexCount = 3;
static constexpr s32 HeaderEncryptionKeyCount = 2;
static constexpr size_t KeyGenerationMax = 32;
const u8 *header_1_sign_key_moduli[Header1SignatureKeyGenerationMax + 1];
u8 header_1_sign_key_public_exponent[Rsa2048KeyPublicExponentSize];
u8 key_area_encryption_key_source[KeyAreaEncryptionKeyIndexCount][Aes128KeySize];
u8 header_encryption_key_source[Aes128KeySize];
u8 header_encrypted_encryption_keys[HeaderEncryptionKeyCount][Aes128KeySize];
KeyGenerationFunction generate_key;
DecryptAesCtrFunction decrypt_aes_ctr;
DecryptAesCtrFunction decrypt_aes_ctr_external;
bool is_plaintext_header_available;
};
static_assert(std::is_pod<NcaCryptoConfiguration>::value);
constexpr inline bool IsInvalidKeyTypeValue(s32 key_type) {
return key_type < 0;
}
constexpr inline s32 GetKeyTypeValue(u8 key_index, u8 key_generation) {
constexpr s32 InvalidKeyTypeValue = -1;
static_assert(IsInvalidKeyTypeValue(InvalidKeyTypeValue));
if (key_index >= NcaCryptoConfiguration::KeyAreaEncryptionKeyIndexCount) {
return InvalidKeyTypeValue;
}
return NcaCryptoConfiguration::KeyAreaEncryptionKeyIndexCount * key_generation + key_index;
}
constexpr inline s32 KeyAreaEncryptionKeyCount = NcaCryptoConfiguration::KeyAreaEncryptionKeyIndexCount * NcaCryptoConfiguration::KeyGenerationMax;
enum class KeyType : s32 {
NcaHeaderKey = KeyAreaEncryptionKeyCount + 0,
NcaExternalKey = KeyAreaEncryptionKeyCount + 1,
SaveDataDeviceUniqueMac = KeyAreaEncryptionKeyCount + 2,
SaveDataSeedUniqueMac = KeyAreaEncryptionKeyCount + 3,
};
class NcaReader : public ::ams::fs::impl::Newable {
NON_COPYABLE(NcaReader);
NON_MOVEABLE(NcaReader);
private:
NcaHeader header;
u8 decryption_keys[NcaHeader::DecryptionKey_Count][NcaCryptoConfiguration::Aes128KeySize];
std::shared_ptr<fs::IStorage> shared_base_storage;
std::unique_ptr<fs::IStorage> header_storage;
fs::IStorage *body_storage;
u8 external_decryption_key[NcaCryptoConfiguration::Aes128KeySize];
DecryptAesCtrFunction decrypt_aes_ctr;
DecryptAesCtrFunction decrypt_aes_ctr_external;
bool is_software_aes_prioritized;
NcaHeader::EncryptionType header_encryption_type;
public:
NcaReader();
~NcaReader();
Result Initialize(fs::IStorage *base_storage, const NcaCryptoConfiguration &crypto_cfg);
Result Initialize(std::shared_ptr<fs::IStorage> base_storage, const NcaCryptoConfiguration &crypto_cfg);
fs::IStorage *GetBodyStorage();
u32 GetMagic() const;
NcaHeader::DistributionType GetDistributionType() const;
NcaHeader::ContentType GetContentType() const;
u8 GetKeyGeneration() const;
u8 GetKeyIndex() const;
u64 GetContentSize() const;
u64 GetProgramId() const;
u32 GetContentIndex() const;
u32 GetSdkAddonVersion() const;
void GetRightsId(u8 *dst, size_t dst_size) const;
bool HasFsInfo(s32 index) const;
s32 GetFsCount() const;
const Hash &GetFsHeaderHash(s32 index) const;
void GetFsHeaderHash(Hash *dst, s32 index) const;
void GetFsInfo(NcaHeader::FsInfo *dst, s32 index) const;
u64 GetFsOffset(s32 index) const;
u64 GetFsEndOffset(s32 index) const;
u64 GetFsSize(s32 index) const;
void GetEncryptedKey(void *dst, size_t size) const;
const void *GetDecryptionKey(s32 index) const;
bool HasValidInternalKey() const;
bool HasInternalDecryptionKeyForAesHardwareSpeedEmulation() const;
bool IsSoftwareAesPrioritized() const;
void PrioritizeSoftwareAes();
bool HasExternalDecryptionKey() const;
const void *GetExternalDecryptionKey() const;
void SetExternalDecryptionKey(const void *src, size_t size);
void GetRawData(void *dst, size_t dst_size) const;
DecryptAesCtrFunction GetExternalDecryptAesCtrFunction() const;
DecryptAesCtrFunction GetExternalDecryptAesCtrFunctionForExternalKey() const;
NcaHeader::EncryptionType GetEncryptionType() const;
Result ReadHeader(NcaFsHeader *dst, s32 index) const;
Result VerifyHeaderSign2(const void *key, size_t key_size);
};
class NcaFsHeaderReader : public ::ams::fs::impl::Newable {
NON_COPYABLE(NcaFsHeaderReader);
NON_MOVEABLE(NcaFsHeaderReader);
private:
NcaFsHeader data;
s32 fs_index;
public:
NcaFsHeaderReader() : fs_index(-1) {
std::memset(std::addressof(this->data), 0, sizeof(this->data));
}
Result Initialize(const NcaReader &reader, s32 index);
bool IsInitialized() const { return this->fs_index >= 0; }
NcaFsHeader &GetData() { return this->data; }
const NcaFsHeader &GetData() const { return this->data; }
void GetRawData(void *dst, size_t dst_size) const;
NcaFsHeader::HashData &GetHashData();
const NcaFsHeader::HashData &GetHashData() const;
u16 GetVersion() const;
s32 GetFsIndex() const;
NcaFsHeader::FsType GetFsType() const;
NcaFsHeader::HashType GetHashType() const;
NcaFsHeader::EncryptionType GetEncryptionType() const;
NcaPatchInfo &GetPatchInfo();
const NcaPatchInfo &GetPatchInfo() const;
const NcaAesCtrUpperIv GetAesCtrUpperIv() const;
bool ExistsSparseLayer() const;
NcaSparseInfo &GetSparseInfo();
const NcaSparseInfo &GetSparseInfo() const;
};
class NcaFileSystemDriver : public ::ams::fs::impl::Newable {
NON_COPYABLE(NcaFileSystemDriver);
NON_MOVEABLE(NcaFileSystemDriver);
public:
class StorageOption;
class StorageOptionWithHeaderReader;
private:
std::shared_ptr<NcaReader> original_reader;
std::shared_ptr<NcaReader> reader;
MemoryResource * const allocator;
fssystem::IBufferManager * const buffer_manager;
public:
static Result SetupFsHeaderReader(NcaFsHeaderReader *out, const NcaReader &reader, s32 fs_index);
public:
NcaFileSystemDriver(std::shared_ptr<NcaReader> reader, MemoryResource *allocator, IBufferManager *buffer_manager) : original_reader(), reader(reader), allocator(allocator), buffer_manager(buffer_manager) {
AMS_ASSERT(this->reader != nullptr);
}
NcaFileSystemDriver(std::shared_ptr<NcaReader> original_reader, std::shared_ptr<NcaReader> reader, MemoryResource *allocator, IBufferManager *buffer_manager) : original_reader(original_reader), reader(reader), allocator(allocator), buffer_manager(buffer_manager) {
AMS_ASSERT(this->reader != nullptr);
}
Result OpenRawStorage(std::shared_ptr<fs::IStorage> *out, s32 fs_index);
Result OpenStorage(std::shared_ptr<fs::IStorage> *out, NcaFsHeaderReader *out_header_reader, s32 fs_index);
Result OpenStorage(std::shared_ptr<fs::IStorage> *out, StorageOption *option);
Result OpenStorage(std::shared_ptr<fs::IStorage> *out, s32 fs_index) {
NcaFsHeaderReader dummy;
return this->OpenStorage(out, std::addressof(dummy), fs_index);
}
Result OpenDecryptableStorage(std::shared_ptr<fs::IStorage> *out, StorageOption *option, bool indirect_needed);
private:
class BaseStorage;
Result CreateBaseStorage(BaseStorage *out, StorageOption *option);
Result CreateDecryptableStorage(std::unique_ptr<fs::IStorage> *out, StorageOption *option, BaseStorage *base_storage);
Result CreateAesXtsStorage(std::unique_ptr<fs::IStorage> *out, BaseStorage *base_storage);
Result CreateAesCtrStorage(std::unique_ptr<fs::IStorage> *out, BaseStorage *base_storage);
Result CreateAesCtrExStorage(std::unique_ptr<fs::IStorage> *out, StorageOption *option, BaseStorage *base_storage);
Result CreateIndirectStorage(std::unique_ptr<fs::IStorage> *out, StorageOption *option, std::unique_ptr<fs::IStorage> base_storage);
Result CreateVerificationStorage(std::unique_ptr<fs::IStorage> *out, std::unique_ptr<fs::IStorage> base_storage, NcaFsHeaderReader *header_reader);
Result CreateSha256Storage(std::unique_ptr<fs::IStorage> *out, std::unique_ptr<fs::IStorage> base_storage, NcaFsHeaderReader *header_reader);
Result CreateIntegrityVerificationStorage(std::unique_ptr<fs::IStorage> *out, std::unique_ptr<fs::IStorage> base_storage, NcaFsHeaderReader *header_reader);
};
}

View File

@@ -0,0 +1,150 @@
/*
* 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/fs/fs_substorage.hpp>
#include <stratosphere/fssystem/fssystem_nca_file_system_driver.hpp>
namespace ams::fssystem {
class NcaFileSystemDriver::StorageOption {
private:
friend class NcaFileSystemDriver;
private:
const s32 fs_index;
NcaFsHeaderReader * const header_reader;
fs::IStorage *data_storage;
s64 data_storage_size;
fs::IStorage *aes_ctr_ex_table_storage;
AesCtrCounterExtendedStorage *aes_ctr_ex_storage_raw;
fs::IStorage *aes_ctr_ex_storage;
IndirectStorage *indirect_storage;
SparseStorage *sparse_storage;
public:
explicit StorageOption(NcaFsHeaderReader *reader) : fs_index(reader->GetFsIndex()), header_reader(reader), data_storage(), data_storage_size(), aes_ctr_ex_table_storage(), aes_ctr_ex_storage_raw(), aes_ctr_ex_storage(), indirect_storage(), sparse_storage() {
AMS_ASSERT(this->header_reader != nullptr);
}
StorageOption(NcaFsHeaderReader *reader, s32 index) : fs_index(index), header_reader(reader), data_storage(), data_storage_size(), aes_ctr_ex_table_storage(), aes_ctr_ex_storage_raw(), aes_ctr_ex_storage(), indirect_storage(), sparse_storage() {
AMS_ASSERT(this->header_reader != nullptr);
AMS_ASSERT(0 <= index && index < NcaHeader::FsCountMax);
}
s32 GetFsIndex() const { return this->fs_index; }
NcaFsHeaderReader &GetHeaderReader() { return *this->header_reader; }
const NcaFsHeaderReader &GetHeaderReader() const { return *this->header_reader; }
fs::SubStorage GetDataStorage() const { return fs::SubStorage(this->data_storage, 0, this->data_storage_size); }
fs::IStorage *GetAesCtrExTableStorage() const { return this->aes_ctr_ex_table_storage; }
fs::IStorage *GetAesCtrExStorage() const { return this->aes_ctr_ex_storage; }
AesCtrCounterExtendedStorage *GetAesCtrExStorageRaw() const { return this->aes_ctr_ex_storage_raw; }
IndirectStorage *GetIndirectStorage() const { return this->indirect_storage; }
SparseStorage *GetSparseStorage() const { return this->sparse_storage; }
private:
void SetDataStorage(fs::IStorage *storage, s64 size) {
AMS_ASSERT(storage != nullptr);
AMS_ASSERT(size >= 0);
this->data_storage = storage;
this->data_storage_size = size;
}
void SetAesCtrExTableStorage(fs::IStorage *storage) { AMS_ASSERT(storage != nullptr); this->aes_ctr_ex_table_storage = storage; }
void SetAesCtrExStorage(fs::IStorage *storage) { AMS_ASSERT(storage != nullptr); this->aes_ctr_ex_storage = storage; }
void SetAesCtrExStorageRaw(AesCtrCounterExtendedStorage *storage) { AMS_ASSERT(storage != nullptr); this->aes_ctr_ex_storage_raw = storage; }
void SetIndirectStorage(IndirectStorage *storage) { AMS_ASSERT(storage != nullptr); this->indirect_storage = storage; }
void SetSparseStorage(SparseStorage *storage) { AMS_ASSERT(storage != nullptr); this->sparse_storage = storage; }
};
class NcaFileSystemDriver::StorageOptionWithHeaderReader : public NcaFileSystemDriver::StorageOption {
private:
NcaFsHeaderReader header_reader_data;
public:
explicit StorageOptionWithHeaderReader(s32 index) : StorageOption(std::addressof(header_reader_data), index) { /* ... */ }
};
class NcaFileSystemDriver::BaseStorage {
private:
std::unique_ptr<fs::IStorage> storage;
fs::SubStorage sub_storage;
s64 storage_offset;
NcaAesCtrUpperIv aes_ctr_upper_iv;
public:
BaseStorage() : storage(), sub_storage(), storage_offset(0) {
this->aes_ctr_upper_iv.value = 0;
}
explicit BaseStorage(const fs::SubStorage &ss) : storage(), sub_storage(ss), storage_offset(0) {
this->aes_ctr_upper_iv.value = 0;
}
template<typename T>
BaseStorage(T s, s64 offset, s64 size) : storage(), sub_storage(s, offset, size), storage_offset(0) {
this->aes_ctr_upper_iv.value = 0;
}
void SetStorage(std::unique_ptr<fs::IStorage> &&storage) {
this->storage = std::move(storage);
}
template<typename T>
void SetStorage(T storage, s64 offset, s64 size) {
this->sub_storage = fs::SubStorage(storage, offset, size);
}
std::unique_ptr<fs::IStorage> MakeStorage() {
if (this->storage != nullptr) {
return std::move(this->storage);
}
return std::make_unique<fs::SubStorage>(this->sub_storage);
}
std::unique_ptr<fs::IStorage> GetStorage() {
return std::move(this->storage);
}
Result GetSubStorage(fs::SubStorage *out, s64 offset, s64 size) {
s64 storage_size = 0;
if (this->storage != nullptr) {
R_TRY(this->storage->GetSize(std::addressof(storage_size)));
R_UNLESS(offset + size <= storage_size, fs::ResultNcaBaseStorageOutOfRangeA());
*out = fs::SubStorage(this->storage.get(), offset, size);
} else {
R_TRY(this->sub_storage.GetSize(std::addressof(storage_size)));
R_UNLESS(offset + size <= storage_size, fs::ResultNcaBaseStorageOutOfRangeA());
*out = fs::SubStorage(std::addressof(this->sub_storage), offset, size);
}
return ResultSuccess();
}
void SetStorageOffset(s64 offset) {
this->storage_offset = offset;
}
s64 GetStorageOffset() const {
return this->storage_offset;
}
void SetAesCtrUpperIv(NcaAesCtrUpperIv v) {
this->aes_ctr_upper_iv = v;
}
const NcaAesCtrUpperIv GetAesCtrUpperIv() const {
return this->aes_ctr_upper_iv;
}
};
}

View File

@@ -0,0 +1,275 @@
/*
* 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 {
struct Hash {
static constexpr size_t Size = crypto::Sha256Generator::HashSize;
u8 value[Size];
};
static_assert(sizeof(Hash) == Hash::Size);
static_assert(std::is_pod<Hash>::value);
using NcaDigest = Hash;
struct NcaHeader {
enum class ContentType : u8 {
Program = 0,
Meta = 1,
Control = 2,
Manual = 3,
Data = 4,
PublicData = 5,
Start = Program,
End = PublicData,
};
enum class DistributionType : u8 {
Download = 0,
GameCard = 1,
Start = Download,
End = GameCard,
};
enum class EncryptionType : u8 {
Auto = 0,
None = 1,
};
enum DecryptionKey {
DecryptionKey_AesXts = 0,
DecryptionKey_AesXts1 = DecryptionKey_AesXts,
DecryptionKey_AesXts2 = 1,
DecryptionKey_AesCtr = 2,
DecryptionKey_AesCtrEx = 3,
DecryptionKey_AesCtrHw = 4,
DecryptionKey_Count,
};
struct FsInfo {
u32 start_sector;
u32 end_sector;
u32 hash_sectors;
u32 reserved;
};
static_assert(sizeof(FsInfo) == 0x10);
static_assert(std::is_pod<FsInfo>::value);
static constexpr u32 Magic0 = util::FourCC<'N','C','A','0'>::Code;
static constexpr u32 Magic1 = util::FourCC<'N','C','A','1'>::Code;
static constexpr u32 Magic2 = util::FourCC<'N','C','A','2'>::Code;
static constexpr u32 Magic3 = util::FourCC<'N','C','A','3'>::Code;
static constexpr u32 Magic = Magic3;
static constexpr size_t Size = 1_KB;
static constexpr s32 FsCountMax = 4;
static constexpr size_t HeaderSignCount = 2;
static constexpr size_t HeaderSignSize = 0x100;
static constexpr size_t EncryptedKeyAreaSize = 0x100;
static constexpr size_t SectorSize = 0x200;
static constexpr size_t SectorShift = 9;
static constexpr size_t RightsIdSize = 0x10;
static constexpr size_t XtsBlockSize = 0x200;
static constexpr size_t CtrBlockSize = 0x10;
static_assert(SectorSize == (1 << SectorShift));
/* Data members. */
u8 header_sign_1[HeaderSignSize];
u8 header_sign_2[HeaderSignSize];
u32 magic;
DistributionType distribution_type;
ContentType content_type;
u8 key_generation;
u8 key_index;
u64 content_size;
u64 program_id;
u32 content_index;
u32 sdk_addon_version;
u8 key_generation_2;
u8 header1_signature_key_generation;
u8 reserved_222[2];
u32 reserved_224[3];
u8 rights_id[RightsIdSize];
FsInfo fs_info[FsCountMax];
Hash fs_header_hash[FsCountMax];
u8 encrypted_key_area[EncryptedKeyAreaSize];
static constexpr u64 SectorToByte(u32 sector) {
return static_cast<u64>(sector) << SectorShift;
}
static constexpr u32 ByteToSector(u64 byte) {
return static_cast<u32>(byte >> SectorShift);
}
u8 GetProperKeyGeneration() const;
};
static_assert(sizeof(NcaHeader) == NcaHeader::Size);
static_assert(std::is_pod<NcaHeader>::value);
struct NcaBucketInfo {
static constexpr size_t HeaderSize = 0x10;
s64 offset;
s64 size;
u8 header[HeaderSize];
};
static_assert(std::is_pod<NcaBucketInfo>::value);
struct NcaPatchInfo {
static constexpr size_t Size = 0x40;
static constexpr size_t Offset = 0x100;
s64 indirect_offset;
s64 indirect_size;
u8 indirect_header[NcaBucketInfo::HeaderSize];
s64 aes_ctr_ex_offset;
s64 aes_ctr_ex_size;
u8 aes_ctr_ex_header[NcaBucketInfo::HeaderSize];
bool HasIndirectTable() const;
bool HasAesCtrExTable() const;
};
static_assert(std::is_pod<NcaPatchInfo>::value);
union NcaAesCtrUpperIv {
u64 value;
struct {
u32 generation;
u32 secure_value;
} part;
};
static_assert(std::is_pod<NcaAesCtrUpperIv>::value);
struct NcaSparseInfo {
NcaBucketInfo bucket;
s64 physical_offset;
u16 generation;
u8 reserved[6];
s64 GetPhysicalSize() const {
return this->bucket.offset + this->bucket.size;
}
u32 GetGeneration() const {
return static_cast<u32>(this->generation) << 16;
}
const NcaAesCtrUpperIv MakeAesCtrUpperIv(NcaAesCtrUpperIv upper_iv) const {
NcaAesCtrUpperIv sparse_upper_iv = upper_iv;
sparse_upper_iv.part.generation = this->GetGeneration();
return sparse_upper_iv;
}
};
static_assert(std::is_pod<NcaSparseInfo>::value);
struct NcaFsHeader {
static constexpr size_t Size = 0x200;
static constexpr size_t HashDataOffset = 0x8;
struct Region {
s64 offset;
s64 size;
};
static_assert(std::is_pod<Region>::value);
enum class FsType : u8 {
RomFs = 0,
PartitionFs = 1,
};
enum class EncryptionType : u8 {
Auto = 0,
None = 1,
AesXts = 2,
AesCtr = 3,
AesCtrEx = 4,
};
enum class HashType : u8 {
Auto = 0,
None = 1,
HierarchicalSha256Hash = 2,
HierarchicalIntegrityHash = 3,
};
union HashData {
struct HierarchicalSha256Data {
static constexpr size_t HashLayerCountMax = 5;
static const size_t MasterHashOffset;
Hash fs_data_master_hash;
s32 hash_block_size;
s32 hash_layer_count;
Region hash_layer_region[HashLayerCountMax];
} hierarchical_sha256_data;
static_assert(std::is_pod<HierarchicalSha256Data>::value);
struct IntegrityMetaInfo {
static const size_t MasterHashOffset;
u32 magic;
u32 version;
u32 master_hash_size;
struct LevelHashInfo {
u32 max_layers;
struct HierarchicalIntegrityVerificationLevelInformation {
static constexpr size_t IntegrityMaxLayerCount = 7;
s64 offset;
s64 size;
s32 block_order;
u8 reserved[4];
} info[HierarchicalIntegrityVerificationLevelInformation::IntegrityMaxLayerCount - 1];
struct SignatureSalt {
static constexpr size_t Size = 0x20;
u8 value[Size];
} seed;
} level_hash_info;
Hash master_hash;
} integrity_meta_info;
static_assert(std::is_pod<IntegrityMetaInfo>::value);
u8 padding[NcaPatchInfo::Offset - HashDataOffset];
};
u16 version;
FsType fs_type;
HashType hash_type;
EncryptionType encryption_type;
u8 reserved[3];
HashData hash_data;
NcaPatchInfo patch_info;
NcaAesCtrUpperIv aes_ctr_upper_iv;
NcaSparseInfo sparse_info;
u8 pad[0x88];
};
static_assert(sizeof(NcaFsHeader) == NcaFsHeader::Size);
static_assert(std::is_pod<NcaFsHeader>::value);
static_assert(offsetof(NcaFsHeader, patch_info) == NcaPatchInfo::Offset);
inline constexpr const size_t NcaFsHeader::HashData::HierarchicalSha256Data::MasterHashOffset = offsetof(NcaFsHeader, hash_data.hierarchical_sha256_data.fs_data_master_hash);
inline constexpr const size_t NcaFsHeader::HashData::IntegrityMetaInfo::MasterHashOffset = offsetof(NcaFsHeader, hash_data.integrity_meta_info.master_hash);
}

View File

@@ -0,0 +1,97 @@
/*
* 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/fssystem/fssystem_indirect_storage.hpp>
namespace ams::fssystem {
class SparseStorage : public IndirectStorage {
NON_COPYABLE(SparseStorage);
NON_MOVEABLE(SparseStorage);
private:
class ZeroStorage : public ::ams::fs::IStorage, public ::ams::fs::impl::Newable {
public:
ZeroStorage() { /* ... */ }
virtual ~ZeroStorage() { /* ... */ }
virtual Result Read(s64 offset, void *buffer, size_t size) override {
AMS_ASSERT(offset >= 0);
AMS_ASSERT(buffer != nullptr || size == 0);
if (size > 0) {
std::memset(buffer, 0, size);
}
return ResultSuccess();
}
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 {
return ResultSuccess();
}
virtual Result GetSize(s64 *out) override {
AMS_ASSERT(out != nullptr);
*out = std::numeric_limits<s64>::max();
return ResultSuccess();
}
virtual Result Flush() override {
return ResultSuccess();
}
virtual Result Write(s64 offset, const void *buffer, size_t size) override {
return fs::ResultUnsupportedOperationInZeroStorageA();
}
virtual Result SetSize(s64 size) override {
return fs::ResultUnsupportedOperationInZeroStorageB();
}
};
private:
ZeroStorage zero_storage;
public:
SparseStorage() : IndirectStorage(), zero_storage() { /* ... */ }
virtual ~SparseStorage() { /* ... */ }
using IndirectStorage::Initialize;
void Initialize(s64 end_offset) {
this->GetEntryTable().Initialize(NodeSize, end_offset);
this->SetZeroStorage();
}
void SetDataStorage(fs::SubStorage storage) {
AMS_ASSERT(this->IsInitialized());
this->SetStorage(0, storage);
this->SetZeroStorage();
}
template<typename T>
void SetDataStorage(T storage, s64 offset, s64 size) {
AMS_ASSERT(this->IsInitialized());
this->SetStorage(0, storage, offset, size);
this->SetZeroStorage();
}
virtual Result Read(s64 offset, void *buffer, size_t size) override;
private:
void SetZeroStorage() {
return this->SetStorage(1, std::addressof(this->zero_storage), 0, std::numeric_limits<s64>::max());
}
};
}

View File

@@ -0,0 +1,28 @@
/*
* 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/fs/fs_speed_emulation.hpp>
namespace ams::fssystem {
class SpeedEmulationConfiguration {
public:
static void SetSpeedEmulationMode(::ams::fs::SpeedEmulationMode mode);
static ::ams::fs::SpeedEmulationMode GetSpeedEmulationMode();
};
}

View File

@@ -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);
};
}

View File

@@ -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);
};
}

View File

@@ -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()));
}
};
}

View File

@@ -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 */
}

View File

@@ -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 */
}

View File

@@ -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;
}
};
}

View File

@@ -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));
}
}

View File

@@ -20,5 +20,6 @@
namespace ams::hos {
void InitializeForStratosphere();
void InitializeForStratosphereDebug(hos::Version debug_version);
}

View File

@@ -15,19 +15,83 @@
*/
#pragma once
#include "spl_types.hpp"
#include <stratosphere/spl/spl_types.hpp>
namespace ams::spl {
HardwareType GetHardwareType();
MemoryArrangement GetMemoryArrangement();
bool IsDisabledProgramVerification();
bool IsDevelopmentHardware();
bool IsDevelopmentFunctionEnabled();
bool IsMariko();
bool IsRecoveryBoot();
void Initialize();
void InitializeForCrypto();
void InitializeForSsl();
void InitializeForEs();
void InitializeForFs();
void InitializeForManu();
Result GenerateAesKek(AccessKey *access_key, const void *key_source, size_t key_source_size, u32 generation, u32 option);
void Finalize();
Result AllocateAesKeySlot(s32 *out_slot);
Result DeallocateAesKeySlot(s32 slot);
Result GenerateAesKek(AccessKey *access_key, const void *key_source, size_t key_source_size, s32 generation, u32 option);
Result LoadAesKey(s32 slot, const AccessKey &access_key, const void *key_source, size_t key_source_size);
Result GenerateAesKey(void *dst, size_t dst_size, const AccessKey &access_key, const void *key_source, size_t key_source_size);
Result GenerateSpecificAesKey(void *dst, size_t dst_size, const void *key_source, size_t key_source_size, s32 generation, u32 option);
Result ComputeCtr(void *dst, size_t dst_size, s32 slot, const void *src, size_t src_size, const void *iv, size_t iv_size);
Result DecryptAesKey(void *dst, size_t dst_size, const void *src, size_t src_size, s32 generation, u32 option);
Result GetConfig(u64 *out, ConfigItem item);
bool IsDevelopment();
MemoryArrangement GetMemoryArrangement();
inline bool GetConfigBool(ConfigItem item) {
u64 v;
R_ABORT_UNLESS(::ams::spl::GetConfig(std::addressof(v), item));
return v != 0;
}
inline HardwareType GetHardwareType() {
u64 v;
R_ABORT_UNLESS(::ams::spl::GetConfig(std::addressof(v), ::ams::spl::ConfigItem::HardwareType));
return static_cast<HardwareType>(v);
}
inline HardwareState GetHardwareState() {
u64 v;
R_ABORT_UNLESS(::ams::spl::GetConfig(std::addressof(v), ::ams::spl::ConfigItem::HardwareState));
return static_cast<HardwareState>(v);
}
inline u64 GetDeviceIdLow() {
u64 v;
R_ABORT_UNLESS(::ams::spl::GetConfig(std::addressof(v), ::ams::spl::ConfigItem::DeviceId));
return v;
}
inline bool IsRecoveryBoot() {
return ::ams::spl::GetConfigBool(::ams::spl::ConfigItem::IsRecoveryBoot);
}
inline bool IsDevelopmentFunctionEnabled() {
return ::ams::spl::GetConfigBool(::ams::spl::ConfigItem::IsDevelopmentFunctionEnabled);
}
inline bool IsDisabledProgramVerification() {
return ::ams::spl::GetConfigBool(::ams::spl::ConfigItem::DisableProgramVerification);
}
Result SetBootReason(BootReasonValue boot_reason);
Result GetBootReason(BootReasonValue *out);
inline BootReasonValue GetBootReason() {
BootReasonValue br;
R_ABORT_UNLESS(::ams::spl::GetBootReason(std::addressof(br)));
return br;
}
SocType GetSocType();
Result GetPackage2Hash(void *dst, size_t dst_size);
Result GenerateRandomBytes(void *out, size_t buffer_size);
Result LoadPreparedAesKey(s32 slot, const AccessKey &access_key);
}

View File

@@ -112,6 +112,17 @@ namespace ams::spl {
Hoag = 2,
Iowa = 3,
Calcio = 4,
_Five_ = 5,
};
enum SocType {
SocType_Erista = 0,
SocType_Mariko = 1,
};
enum HardwareState {
HardwareState_Development = 0,
HardwareState_Production = 1,
};
enum MemoryArrangement {
@@ -185,23 +196,23 @@ namespace ams::spl {
enum class ConfigItem : u32 {
/* Standard config items. */
DisableProgramVerification = 1,
DramId = 2,
SecurityEngineIrqNumber = 3,
Version = 4,
HardwareType = 5,
IsRetail = 6,
IsRecoveryBoot = 7,
DeviceId = 8,
BootReason = 9,
MemoryMode = 10,
IsDebugMode = 11,
KernelConfiguration = 12,
IsChargerHiZModeEnabled = 13,
IsQuest = 14,
RegulatorType = 15,
DeviceUniqueKeyGeneration = 16,
Package2Hash = 17,
DisableProgramVerification = 1,
DramId = 2,
SecurityEngineIrqNumber = 3,
FuseVersion = 4,
HardwareType = 5,
HardwareState = 6,
IsRecoveryBoot = 7,
DeviceId = 8,
BootReason = 9,
MemoryMode = 10,
IsDevelopmentFunctionEnabled = 11,
KernelConfiguration = 12,
IsChargerHiZModeEnabled = 13,
IsQuest = 14,
RegulatorType = 15,
DeviceUniqueKeyGeneration = 16,
Package2Hash = 17,
/* Extension config items for exosphere. */
ExosphereApiVersion = 65000,