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

@@ -0,0 +1,356 @@
/*
* 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/>.
*/
#include <stratosphere.hpp>
namespace ams::fssystem {
Result FileSystemBufferManager::CacheHandleTable::Initialize(s32 max_cache_count) {
/* Validate pre-conditions. */
AMS_ASSERT(this->entries == nullptr);
AMS_ASSERT(this->internal_entry_buffer == nullptr);
/* If we don't have an external buffer, try to allocate an internal one. */
if (this->external_entry_buffer == nullptr) {
this->entry_buffer_size = sizeof(Entry) * max_cache_count;
this->internal_entry_buffer = fs::impl::MakeUnique<char[]>(this->entry_buffer_size);
}
/* We need to have at least one entry buffer. */
R_UNLESS(this->internal_entry_buffer != nullptr || this->external_entry_buffer != nullptr, fs::ResultAllocationFailureInFileSystemBufferManagerA());
/* Set entries. */
this->entries = reinterpret_cast<Entry *>(this->external_entry_buffer != nullptr ? this->external_entry_buffer : this->internal_entry_buffer.get());
this->entry_count = 0;
this->entry_count_max = max_cache_count;
AMS_ASSERT(this->entries != nullptr);
this->cache_count_min = max_cache_count / 16;
this->cache_size_min = this->cache_count_min * 0x100;
return ResultSuccess();
}
void FileSystemBufferManager::CacheHandleTable::Finalize() {
if (this->entries != nullptr) {
AMS_ASSERT(this->entry_count == 0);
if (this->external_attr_info_buffer == nullptr) {
auto it = this->attr_list.begin();
while (it != this->attr_list.end()) {
const auto attr_info = std::addressof(*it);
it = this->attr_list.erase(it);
delete attr_info;
}
}
this->internal_entry_buffer.reset();
this->external_entry_buffer = nullptr;
this->entry_buffer_size = 0;
this->entries = nullptr;
this->total_cache_size = 0;
}
}
bool FileSystemBufferManager::CacheHandleTable::Register(CacheHandle *out, uintptr_t address, size_t size, const BufferAttribute &attr) {
/* Validate pre-conditions. */
AMS_ASSERT(this->entries != nullptr);
AMS_ASSERT(out != nullptr);
/* Get the entry. */
auto entry = this->AcquireEntry(address, size, attr);
/* If we don't have an entry, we can't register. */
if (entry == nullptr) {
return false;
}
/* Get the attr info. If we have one, increment. */
if (const auto attr_info = this->FindAttrInfo(attr); attr_info != nullptr) {
attr_info->IncrementCacheCount();
attr_info->AddCacheSize(size);
} else {
/* Make a new attr info and add it to the list. */
AttrInfo *new_info = nullptr;
if (this->external_attr_info_buffer == nullptr) {
new_info = new AttrInfo(attr.GetLevel(), 1, size);
} else if (0 <= attr.GetLevel() && attr.GetLevel() < this->external_attr_info_count) {
const auto buffer = this->external_attr_info_buffer + attr.GetLevel() * sizeof(AttrInfo);
new_info = new (buffer) AttrInfo(attr.GetLevel(), 1, size);
}
/* If we failed to make a new attr info, we can't register. */
if (new_info == nullptr) {
this->ReleaseEntry(entry);
return false;
}
this->attr_list.push_back(*new_info);
}
this->total_cache_size += size;
*out = entry->GetHandle();
return true;
}
bool FileSystemBufferManager::CacheHandleTable::Unregister(uintptr_t *out_address, size_t *out_size, CacheHandle handle) {
/* Validate pre-conditions. */
AMS_ASSERT(this->entries != nullptr);
AMS_ASSERT(out_address != nullptr);
AMS_ASSERT(out_size != nullptr);
/* Find the lower bound for the entry. */
const auto entry = std::lower_bound(this->entries, this->entries + this->entry_count, handle, [](const Entry &entry, CacheHandle handle) {
return entry.GetHandle() < handle;
});
/* If the entry is a match, unregister it. */
if (entry != this->entries + this->entry_count && entry->GetHandle() == handle) {
this->UnregisterCore(out_address, out_size, entry);
return true;
} else {
return false;
}
}
bool FileSystemBufferManager::CacheHandleTable::UnregisterOldest(uintptr_t *out_address, size_t *out_size, const BufferAttribute &attr, size_t required_size) {
/* Validate pre-conditions. */
AMS_ASSERT(this->entries != nullptr);
AMS_ASSERT(out_address != nullptr);
AMS_ASSERT(out_size != nullptr);
/* If we have no entries, we can't unregister any. */
if (this->entry_count == 0) {
return false;
}
const auto CanUnregister = [this](const Entry &entry) {
const auto attr_info = this->FindAttrInfo(entry.GetBufferAttribute());
AMS_ASSERT(attr_info != nullptr);
const auto ccm = this->GetCacheCountMin(entry.GetBufferAttribute());
const auto csm = this->GetCacheSizeMin(entry.GetBufferAttribute());
return ccm < attr_info->GetCacheCount() && csm + entry.GetSize() <= attr_info->GetCacheSize();
};
/* Find an entry, falling back to the first entry. */
auto entry = std::find_if(this->entries, this->entries + this->entry_count, CanUnregister);
if (entry == this->entries + this->entry_count) {
entry = this->entries;
}
AMS_ASSERT(entry != this->entries + this->entry_count);
this->UnregisterCore(out_address, out_size, entry);
return true;
}
void FileSystemBufferManager::CacheHandleTable::UnregisterCore(uintptr_t *out_address, size_t *out_size, Entry *entry) {
/* Validate pre-conditions. */
AMS_ASSERT(this->entries != nullptr);
AMS_ASSERT(out_address != nullptr);
AMS_ASSERT(out_size != nullptr);
AMS_ASSERT(entry != nullptr);
/* Get the attribute info. */
const auto attr_info = this->FindAttrInfo(entry->GetBufferAttribute());
AMS_ASSERT(attr_info != nullptr);
AMS_ASSERT(attr_info->GetCacheCount() > 0);
AMS_ASSERT(attr_info->GetCacheSize() >= entry->GetSize());
/* Release from the attr info. */
attr_info->DecrementCacheCount();
attr_info->SubtractCacheSize(entry->GetSize());
/* Release from cached size. */
AMS_ASSERT(this->total_cache_size >= entry->GetSize());
this->total_cache_size -= entry->GetSize();
/* Release the entry. */
*out_address = entry->GetAddress();
*out_size = entry->GetSize();
this->ReleaseEntry(entry);
}
FileSystemBufferManager::CacheHandle FileSystemBufferManager::CacheHandleTable::PublishCacheHandle() {
AMS_ASSERT(this->entries != nullptr);
return (++this->current_handle);
}
size_t FileSystemBufferManager::CacheHandleTable::GetTotalCacheSize() const {
return this->total_cache_size;
}
FileSystemBufferManager::CacheHandleTable::Entry *FileSystemBufferManager::CacheHandleTable::AcquireEntry(uintptr_t address, size_t size, const BufferAttribute &attr) {
/* Validate pre-conditions. */
AMS_ASSERT(this->entries != nullptr);
Entry *entry = nullptr;
if (this->entry_count < this->entry_count_max) {
entry = this->entries + this->entry_count;
entry->Initialize(this->PublishCacheHandle(), address, size, attr);
++this->entry_count;
AMS_ASSERT(this->entry_count == 1 || (entry-1)->GetHandle() < entry->GetHandle());
}
return entry;
}
void FileSystemBufferManager::CacheHandleTable::ReleaseEntry(Entry *entry) {
/* Validate pre-conditions. */
AMS_ASSERT(this->entries != nullptr);
AMS_ASSERT(entry != nullptr);
/* Ensure the entry is valid. */
const auto entry_buffer = this->external_entry_buffer != nullptr ? this->external_entry_buffer : this->internal_entry_buffer.get();
AMS_ASSERT(static_cast<void *>(entry_buffer) <= static_cast<void *>(entry));
AMS_ASSERT(static_cast<void *>(entry) < static_cast<void *>(entry_buffer + this->entry_buffer_size));
/* Copy the entries back by one. */
std::memmove(entry, entry + 1, sizeof(Entry) * (this->entry_count - ((entry + 1) - this->entries)));
/* Decrement our entry count. */
--this->entry_count;
}
FileSystemBufferManager::CacheHandleTable::AttrInfo *FileSystemBufferManager::CacheHandleTable::FindAttrInfo(const BufferAttribute &attr) {
const auto it = std::find_if(this->attr_list.begin(), this->attr_list.end(), [&attr](const AttrInfo &info) {
return attr.GetLevel() == attr.GetLevel();
});
return it != this->attr_list.end() ? std::addressof(*it) : nullptr;
}
const std::pair<uintptr_t, size_t> FileSystemBufferManager::AllocateBufferImpl(size_t size, const BufferAttribute &attr) {
std::scoped_lock lk(this->mutex);
std::pair<uintptr_t, size_t> range = {};
const auto order = this->buddy_heap.GetOrderFromBytes(size);
AMS_ASSERT(order >= 0);
while (true) {
if (auto address = this->buddy_heap.AllocateByOrder(order); address != 0) {
const auto allocated_size = this->buddy_heap.GetBytesFromOrder(order);
AMS_ASSERT(size <= allocated_size);
range.first = reinterpret_cast<uintptr_t>(address);
range.second = allocated_size;
const size_t free_size = this->buddy_heap.GetTotalFreeSize();
this->peak_free_size = std::min(this->peak_free_size, free_size);
const size_t total_allocatable_size = free_size + this->cache_handle_table.GetTotalCacheSize();
this->peak_total_allocatable_size = std::min(this->peak_total_allocatable_size, total_allocatable_size);
break;
}
/* Deallocate a buffer. */
uintptr_t deallocate_address = 0;
size_t deallocate_size = 0;
++this->retried_count;
if (this->cache_handle_table.UnregisterOldest(std::addressof(deallocate_address), std::addressof(deallocate_size), attr, size)) {
this->DeallocateBuffer(deallocate_address, deallocate_size);
} else {
break;
}
}
return range;
}
void FileSystemBufferManager::DeallocateBufferImpl(uintptr_t address, size_t size) {
AMS_ASSERT(util::IsPowerOfTwo(size));
std::scoped_lock lk(this->mutex);
this->buddy_heap.Free(reinterpret_cast<void *>(address), this->buddy_heap.GetOrderFromBytes(size));
}
FileSystemBufferManager::CacheHandle FileSystemBufferManager::RegisterCacheImpl(uintptr_t address, size_t size, const BufferAttribute &attr) {
std::scoped_lock lk(this->mutex);
CacheHandle handle = 0;
while (true) {
/* Try to register the handle. */
if (this->cache_handle_table.Register(std::addressof(handle), address, size, attr)) {
break;
}
/* Deallocate a buffer. */
uintptr_t deallocate_address = 0;
size_t deallocate_size = 0;
++this->retried_count;
if (this->cache_handle_table.UnregisterOldest(std::addressof(deallocate_address), std::addressof(deallocate_size), attr)) {
this->DeallocateBuffer(deallocate_address, deallocate_size);
} else {
this->DeallocateBuffer(address, size);
handle = this->cache_handle_table.PublishCacheHandle();
break;
}
}
return handle;
}
const std::pair<uintptr_t, size_t> FileSystemBufferManager::AcquireCacheImpl(CacheHandle handle) {
std::scoped_lock lk(this->mutex);
std::pair<uintptr_t, size_t> range = {};
if (this->cache_handle_table.Unregister(std::addressof(range.first), std::addressof(range.second), handle)) {
const size_t total_allocatable_size = this->buddy_heap.GetTotalFreeSize() + this->cache_handle_table.GetTotalCacheSize();
this->peak_total_allocatable_size = std::min(this->peak_total_allocatable_size, total_allocatable_size);
} else {
range.first = 0;
range.second = 0;
}
return range;
}
size_t FileSystemBufferManager::GetTotalSizeImpl() const {
return this->total_size;
}
size_t FileSystemBufferManager::GetFreeSizeImpl() const {
std::scoped_lock lk(this->mutex);
return this->buddy_heap.GetTotalFreeSize();
}
size_t FileSystemBufferManager::GetTotalAllocatableSizeImpl() const {
return this->GetFreeSize() + this->cache_handle_table.GetTotalCacheSize();
}
size_t FileSystemBufferManager::GetPeakFreeSizeImpl() const {
return this->peak_free_size;
}
size_t FileSystemBufferManager::GetPeakTotalAllocatableSizeImpl() const {
return this->peak_total_allocatable_size;
}
size_t FileSystemBufferManager::GetRetriedCountImpl() const {
return this->retried_count;
}
void FileSystemBufferManager::ClearPeakImpl() {
this->peak_free_size = this->GetFreeSize();
this->retried_count = 0;
}
}

View File

@@ -0,0 +1,296 @@
/*
* 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/>.
*/
#include <stratosphere.hpp>
namespace ams::fssystem {
namespace {
class SoftwareDecryptor final : public AesCtrCounterExtendedStorage::IDecryptor {
public:
virtual void Decrypt(void *buf, size_t buf_size, const void *enc_key, size_t enc_key_size, void *iv, size_t iv_size) override final;
virtual bool HasExternalDecryptionKey() const override final { return false; }
};
class ExternalDecryptor final : public AesCtrCounterExtendedStorage::IDecryptor {
public:
static constexpr size_t BlockSize = AesCtrCounterExtendedStorage::BlockSize;
static constexpr size_t KeySize = AesCtrCounterExtendedStorage::KeySize;
static constexpr size_t IvSize = AesCtrCounterExtendedStorage::IvSize;
private:
AesCtrCounterExtendedStorage::DecryptFunction decrypt_function;
s32 key_index;
public:
ExternalDecryptor(AesCtrCounterExtendedStorage::DecryptFunction df, s32 key_idx) : decrypt_function(df), key_index(key_idx) {
AMS_ASSERT(this->decrypt_function != nullptr);
}
public:
virtual void Decrypt(void *buf, size_t buf_size, const void *enc_key, size_t enc_key_size, void *iv, size_t iv_size) override final;
virtual bool HasExternalDecryptionKey() const override final { return this->key_index < 0; }
};
}
Result AesCtrCounterExtendedStorage::CreateExternalDecryptor(std::unique_ptr<IDecryptor> *out, DecryptFunction func, s32 key_index) {
std::unique_ptr<IDecryptor> decryptor = std::make_unique<ExternalDecryptor>(func, key_index);
R_UNLESS(decryptor != nullptr, fs::ResultAllocationFailureInAesCtrCounterExtendedStorageA());
*out = std::move(decryptor);
return ResultSuccess();
}
Result AesCtrCounterExtendedStorage::CreateSoftwareDecryptor(std::unique_ptr<IDecryptor> *out) {
std::unique_ptr<IDecryptor> decryptor = std::make_unique<SoftwareDecryptor>();
R_UNLESS(decryptor != nullptr, fs::ResultAllocationFailureInAesCtrCounterExtendedStorageA());
*out = std::move(decryptor);
return ResultSuccess();
}
Result AesCtrCounterExtendedStorage::Initialize(IAllocator *allocator, const void *key, size_t key_size, u32 secure_value, fs::SubStorage data_storage, fs::SubStorage table_storage) {
/* Read and verify the bucket tree header. */
BucketTree::Header header;
R_TRY(table_storage.Read(0, std::addressof(header), sizeof(header)));
R_TRY(header.Verify());
/* Determine extents. */
const auto node_storage_size = QueryNodeStorageSize(header.entry_count);
const auto entry_storage_size = QueryEntryStorageSize(header.entry_count);
const auto node_storage_offset = QueryHeaderStorageSize();
const auto entry_storage_offset = node_storage_offset + node_storage_size;
/* Create a software decryptor. */
std::unique_ptr<IDecryptor> sw_decryptor;
R_TRY(CreateSoftwareDecryptor(std::addressof(sw_decryptor)));
/* Initialize. */
return this->Initialize(allocator, key, key_size, secure_value, 0, data_storage, fs::SubStorage(std::addressof(table_storage), node_storage_offset, node_storage_size), fs::SubStorage(std::addressof(table_storage), entry_storage_offset, entry_storage_size), header.entry_count, std::move(sw_decryptor));
}
Result AesCtrCounterExtendedStorage::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) {
/* Validate preconditions. */
AMS_ASSERT(key != nullptr);
AMS_ASSERT(key_size == KeySize);
AMS_ASSERT(counter_offset >= 0);
AMS_ASSERT(decryptor != nullptr);
/* Initialize the bucket tree table. */
R_TRY(this->table.Initialize(allocator, node_storage, entry_storage, NodeSize, sizeof(Entry), entry_count));
/* Set members. */
this->data_storage = data_storage;
std::memcpy(this->key, key, key_size);
this->secure_value = secure_value;
this->counter_offset = counter_offset;
this->decryptor = std::move(decryptor);
return ResultSuccess();
}
void AesCtrCounterExtendedStorage::Finalize() {
if (this->IsInitialized()) {
this->table.Finalize();
this->data_storage = fs::SubStorage();
}
}
Result AesCtrCounterExtendedStorage::Read(s64 offset, void *buffer, size_t size) {
/* Validate preconditions. */
AMS_ASSERT(offset >= 0);
AMS_ASSERT(this->IsInitialized());
/* Allow zero size. */
R_SUCCEED_IF(size == 0);
/* Validate arguments. */
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
R_UNLESS(util::IsAligned(offset, BlockSize), fs::ResultInvalidOffset());
R_UNLESS(util::IsAligned(size, BlockSize), fs::ResultInvalidSize());
R_UNLESS(this->table.Includes(offset, size), fs::ResultOutOfRange());
/* Read the data. */
R_TRY(this->data_storage.Read(offset, buffer, size));
/* Temporarily increase our thread priority. */
ScopedThreadPriorityChanger cp(+1, ScopedThreadPriorityChanger::Mode::Relative);
/* 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>()->GetOffset();
R_UNLESS(util::IsAligned(entry_offset, BlockSize), fs::ResultInvalidAesCtrCounterExtendedEntryOffset());
R_UNLESS(0 <= entry_offset && this->table.Includes(entry_offset), fs::ResultInvalidAesCtrCounterExtendedEntryOffset());
}
/* Prepare to read in chunks. */
u8 *cur_data = static_cast<u8 *>(buffer);
auto cur_offset = offset;
const auto end_offset = offset + static_cast<s64>(size);
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.GetOffset();
R_UNLESS(cur_entry_offset <= cur_offset, fs::ResultInvalidAesCtrCounterExtendedEntryOffset());
/* Get and validate the next entry offset. */
s64 next_entry_offset;
if (visitor.CanMoveNext()) {
R_TRY(visitor.MoveNext());
next_entry_offset = visitor.Get<Entry>()->GetOffset();
R_UNLESS(this->table.Includes(next_entry_offset), fs::ResultInvalidAesCtrCounterExtendedEntryOffset());
} else {
next_entry_offset = this->table.GetEnd();
}
R_UNLESS(util::IsAligned(next_entry_offset, BlockSize), fs::ResultInvalidAesCtrCounterExtendedEntryOffset());
R_UNLESS(cur_offset < next_entry_offset, fs::ResultInvalidAesCtrCounterExtendedEntryOffset());
/* 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 = static_cast<size_t>(std::min(remaining_size, data_size));
AMS_ASSERT(cur_size <= size);
/* Make the CTR for the data we're decrypting. */
const auto counter_offset = this->counter_offset + cur_entry_offset + data_offset;
NcaAesCtrUpperIv upper_iv = { .part = { .generation = static_cast<u32>(cur_entry.generation), .secure_value = this->secure_value } };
u8 iv[IvSize];
AesCtrStorage::MakeIv(iv, IvSize, upper_iv.value, counter_offset);
/* Decrypt. */
this->decryptor->Decrypt(cur_data, cur_size, this->key, KeySize, iv, IvSize);
/* Advance. */
cur_data += cur_size;
cur_offset += cur_size;
}
return ResultSuccess();
}
Result AesCtrCounterExtendedStorage::OperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) {
switch (op_id) {
case fs::OperationId::InvalidateCache:
{
/* Validate preconditions. */
AMS_ASSERT(offset >= 0);
AMS_ASSERT(this->IsInitialized());
/* Succeed if there's nothing to operate on. */
R_SUCCEED_IF(size == 0);
/* Validate arguments. */
R_UNLESS(util::IsAligned(offset, BlockSize), fs::ResultInvalidOffset());
R_UNLESS(util::IsAligned(size, BlockSize), fs::ResultInvalidSize());
R_UNLESS(this->table.Includes(offset, size), fs::ResultOutOfRange());
/* Invalidate our table's cache. */
R_TRY(this->table.InvalidateCache());
/* Operate on our data storage. */
R_TRY(this->data_storage.OperateRange(dst, dst_size, op_id, offset, size, src, src_size));
return ResultSuccess();
}
case fs::OperationId::QueryRange:
{
/* Validate preconditions. */
AMS_ASSERT(offset >= 0);
AMS_ASSERT(this->IsInitialized());
/* Validate that we have an output range info. */
R_UNLESS(dst != nullptr, fs::ResultNullptrArgument());
R_UNLESS(dst_size == sizeof(fs::QueryRangeInfo), fs::ResultInvalidSize());
/* Succeed if there's nothing to operate on. */
if (size == 0) {
reinterpret_cast<fs::QueryRangeInfo *>(dst)->Clear();
return ResultSuccess();
}
/* Validate arguments. */
R_UNLESS(util::IsAligned(offset, BlockSize), fs::ResultInvalidOffset());
R_UNLESS(util::IsAligned(size, BlockSize), fs::ResultInvalidSize());
R_UNLESS(this->table.Includes(offset, size), fs::ResultOutOfRange());
/* Operate on our data storage. */
R_TRY(this->data_storage.OperateRange(dst, dst_size, op_id, offset, size, src, src_size));
/* Add in new flags. */
fs::QueryRangeInfo new_info;
new_info.Clear();
new_info.aes_ctr_key_type = static_cast<s32>(this->decryptor->HasExternalDecryptionKey() ? fs::AesCtrKeyTypeFlag::ExternalKeyForHardwareAes : fs::AesCtrKeyTypeFlag::InternalKeyForHardwareAes);
/* Merge in the new info. */
reinterpret_cast<fs::QueryRangeInfo *>(dst)->Merge(new_info);
return ResultSuccess();
}
default:
return fs::ResultUnsupportedOperationInAesCtrCounterExtendedStorageC();
}
}
void SoftwareDecryptor::Decrypt(void *buf, size_t buf_size, const void *enc_key, size_t enc_key_size, void *iv, size_t iv_size) {
crypto::DecryptAes128Ctr(buf, buf_size, enc_key, enc_key_size, iv, iv_size, buf, buf_size);
}
void ExternalDecryptor::Decrypt(void *buf, size_t buf_size, const void *enc_key, size_t enc_key_size, void *iv, size_t iv_size) {
/* Validate preconditions. */
AMS_ASSERT(buf != nullptr);
AMS_ASSERT(enc_key != nullptr);
AMS_ASSERT(enc_key_size == KeySize);
AMS_ASSERT(iv != nullptr);
AMS_ASSERT(iv_size == IvSize);
/* Copy the ctr. */
u8 ctr[IvSize];
std::memcpy(ctr, iv, IvSize);
/* Setup tracking. */
size_t remaining_size = buf_size;
s64 cur_offset = 0;
/* Allocate a pooled buffer for decryption. */
PooledBuffer pooled_buffer;
pooled_buffer.AllocateParticularlyLarge(buf_size, BlockSize);
AMS_ASSERT(pooled_buffer.GetSize() > 0 && util::IsAligned(pooled_buffer.GetSize(), BlockSize));
/* Read and decrypt in chunks. */
while (remaining_size > 0) {
size_t cur_size = std::min(pooled_buffer.GetSize(), remaining_size);
u8 *dst = static_cast<u8 *>(buf) + cur_offset;
this->decrypt_function(pooled_buffer.GetBuffer(), cur_size, this->key_index, enc_key, enc_key_size, ctr, IvSize, dst, cur_size);
std::memcpy(dst, pooled_buffer.GetBuffer(), cur_size);
cur_offset += cur_size;
remaining_size -= cur_size;
if (remaining_size > 0) {
AddCounter(ctr, IvSize, cur_size / BlockSize);
}
}
}
}

View File

@@ -0,0 +1,256 @@
/*
* 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/>.
*/
#include <stratosphere.hpp>
namespace ams::fssystem {
namespace {
template<typename T>
constexpr ALWAYS_INLINE size_t GetRoundDownDifference(T x, size_t align) {
return static_cast<size_t>(x - util::AlignDown(x, align));
}
template<typename T>
constexpr ALWAYS_INLINE size_t GetRoundUpDifference(T x, size_t align) {
return static_cast<size_t>(util::AlignUp(x, align) - x);
}
template<typename T>
ALWAYS_INLINE size_t GetRoundUpDifference(T *x, size_t align) {
return GetRoundUpDifference(reinterpret_cast<uintptr_t>(x), align);
}
}
Result AlignmentMatchingStorageImpl::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) {
/* Check preconditions. */
AMS_ASSERT(work_buf_size >= data_alignment);
/* Succeed if zero size. */
R_SUCCEED_IF(size == 0);
/* Validate arguments. */
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
/* Determine extents. */
char *aligned_core_buffer;
s64 core_offset;
size_t core_size;
size_t buffer_gap;
size_t offset_gap;
s64 covered_offset;
const size_t offset_round_up_difference = GetRoundUpDifference(offset, data_alignment);
if (util::IsAligned(reinterpret_cast<uintptr_t>(buffer) + offset_round_up_difference, buffer_alignment)) {
aligned_core_buffer = buffer + offset_round_up_difference;
core_offset = util::AlignUp(offset, data_alignment);
core_size = (size < offset_round_up_difference) ? 0 : util::AlignDown(size - offset_round_up_difference, data_alignment);
buffer_gap = 0;
offset_gap = 0;
covered_offset = core_size > 0 ? core_offset : offset;
} else {
const size_t buffer_round_up_difference = GetRoundUpDifference(buffer, buffer_alignment);
aligned_core_buffer = buffer + buffer_round_up_difference;
core_offset = util::AlignDown(offset, data_alignment);
core_size = (size < buffer_round_up_difference) ? 0 : util::AlignDown(size - buffer_round_up_difference, data_alignment);
buffer_gap = buffer_round_up_difference;
offset_gap = GetRoundDownDifference(offset, data_alignment);
}
/* Read the core portion. */
if (core_size > 0) {
R_TRY(base_storage->Read(core_offset, aligned_core_buffer, core_size));
if (offset_gap != 0 || buffer_gap != 0) {
std::memmove(aligned_core_buffer - buffer_gap, aligned_core_buffer + offset_gap, core_size - offset_gap);
core_size -= offset_gap;
}
}
/* Handle the head portion. */
if (offset < covered_offset) {
const s64 head_offset = util::AlignDown(offset, data_alignment);
const size_t head_size = static_cast<size_t>(covered_offset - offset);
AMS_ASSERT(GetRoundDownDifference(offset, data_alignment) + head_size <= work_buf_size);
R_TRY(base_storage->Read(head_offset, work_buf, data_alignment));
std::memcpy(buffer, work_buf + GetRoundDownDifference(offset, data_alignment), head_size);
}
/* Handle the tail portion. */
s64 tail_offset = covered_offset + core_size;
size_t remaining_tail_size = static_cast<size_t>((offset + size) - tail_offset);
while (remaining_tail_size > 0) {
const auto aligned_tail_offset = util::AlignDown(tail_offset, data_alignment);
const auto cur_size = std::min(static_cast<size_t>(aligned_tail_offset + data_alignment - tail_offset), remaining_tail_size);
R_TRY(base_storage->Read(aligned_tail_offset, work_buf, data_alignment));
AMS_ASSERT((tail_offset - offset) + cur_size <= size);
AMS_ASSERT((tail_offset - aligned_tail_offset) + cur_size <= data_alignment);
std::memcpy(static_cast<char *>(buffer) + (tail_offset - offset), work_buf + (tail_offset - aligned_tail_offset), cur_size);
remaining_tail_size -= cur_size;
tail_offset += cur_size;
}
return ResultSuccess();
}
Result AlignmentMatchingStorageImpl::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) {
/* Check preconditions. */
AMS_ASSERT(work_buf_size >= data_alignment);
/* Succeed if zero size. */
R_SUCCEED_IF(size == 0);
/* Validate arguments. */
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
/* Determine extents. */
const char *aligned_core_buffer;
s64 core_offset;
size_t core_size;
s64 covered_offset;
const size_t offset_round_up_difference = GetRoundUpDifference(offset, data_alignment);
if (util::IsAligned(reinterpret_cast<uintptr_t>(buffer) + offset_round_up_difference, buffer_alignment)) {
aligned_core_buffer = buffer + offset_round_up_difference;
core_offset = util::AlignUp(offset, data_alignment);
core_size = (size < offset_round_up_difference) ? 0 : util::AlignDown(size - offset_round_up_difference, data_alignment);
covered_offset = core_size > 0 ? core_offset : offset;
} else {
aligned_core_buffer = nullptr;
core_offset = util::AlignDown(offset, data_alignment);
core_size = 0;
covered_offset = offset;
}
/* Write the core portion. */
if (core_size > 0) {
R_TRY(base_storage->Write(core_offset, aligned_core_buffer, core_size));
}
/* Handle the head portion. */
if (offset < covered_offset) {
const s64 head_offset = util::AlignDown(offset, data_alignment);
const size_t head_size = static_cast<size_t>(covered_offset - offset);
AMS_ASSERT((offset - head_offset) + head_size <= data_alignment);
R_TRY(base_storage->Read(head_offset, work_buf, data_alignment));
std::memcpy(work_buf + (offset - head_offset), buffer, head_size);
R_TRY(base_storage->Write(head_offset, work_buf, data_alignment));
}
/* Handle the tail portion. */
s64 tail_offset = covered_offset + core_size;
size_t remaining_tail_size = static_cast<size_t>((offset + size) - tail_offset);
while (remaining_tail_size > 0) {
AMS_ASSERT(static_cast<size_t>(tail_offset - offset) < size);
const auto aligned_tail_offset = util::AlignDown(tail_offset, data_alignment);
const auto cur_size = std::min(static_cast<size_t>(aligned_tail_offset + data_alignment - tail_offset), remaining_tail_size);
R_TRY(base_storage->Read(aligned_tail_offset, work_buf, data_alignment));
std::memcpy(work_buf + GetRoundDownDifference(tail_offset, data_alignment), buffer + (tail_offset - offset), cur_size);
R_TRY(base_storage->Write(aligned_tail_offset, work_buf, data_alignment));
remaining_tail_size -= cur_size;
tail_offset += cur_size;
}
return ResultSuccess();
}
template<>
Result AlignmentMatchingStorageInBulkRead<1>::Read(s64 offset, void *buffer, size_t size) {
/* 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());
/* Determine extents. */
const auto offset_end = offset + static_cast<s64>(size);
const auto aligned_offset = util::AlignDown(offset, this->data_align);
const auto aligned_offset_end = util::AlignUp(offset_end, this->data_align);
const auto aligned_size = static_cast<size_t>(aligned_offset_end - aligned_offset);
/* If we aren't aligned, we need to allocate a buffer. */
PooledBuffer pooled_buffer;
if (aligned_offset != offset || aligned_size != size) {
if (aligned_size <= pooled_buffer.GetAllocatableSizeMax()) {
pooled_buffer.Allocate(aligned_size, this->data_align);
if (aligned_size <= pooled_buffer.GetSize()) {
R_TRY(this->base_storage->Read(aligned_offset, pooled_buffer.GetBuffer(), aligned_size));
std::memcpy(buffer, pooled_buffer.GetBuffer() + (offset - aligned_offset), size);
return ResultSuccess();
} else {
pooled_buffer.Shrink(this->data_align);
}
} else {
pooled_buffer.Allocate(this->data_align, this->data_align);
}
AMS_ASSERT(pooled_buffer.GetSize() >= static_cast<size_t>(this->data_align));
}
/* Determine read extents for the aligned portion. */
const auto core_offset = util::AlignUp(offset, this->data_align);
const auto core_offset_end = util::AlignDown(offset_end, this->data_align);
/* Handle any data before the aligned portion. */
if (offset < core_offset) {
const auto head_size = static_cast<size_t>(core_offset - offset);
AMS_ASSERT(head_size < size);
R_TRY(this->base_storage->Read(aligned_offset, pooled_buffer.GetBuffer(), this->data_align));
std::memcpy(buffer, pooled_buffer.GetBuffer() + (offset - aligned_offset), head_size);
}
/* Handle the aligned portion. */
if (core_offset < core_offset_end) {
const auto core_buffer = static_cast<char *>(buffer) + (core_offset - offset);
const auto core_size = static_cast<size_t>(core_offset_end - core_offset);
R_TRY(this->base_storage->Read(core_offset, core_buffer, core_size));
}
/* Handle any data after the aligned portion. */
if (core_offset_end < offset_end) {
const auto tail_size = static_cast<size_t>(offset_end - core_offset_end);
R_TRY(this->base_storage->Read(core_offset_end, pooled_buffer.GetBuffer(), this->data_align));
std::memcpy(buffer, pooled_buffer.GetBuffer(), tail_size);
}
return ResultSuccess();
}
}

View File

@@ -0,0 +1,65 @@
/*
* 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/>.
*/
#include <stratosphere.hpp>
namespace ams::fssystem {
namespace {
constexpr bool UseDefaultAllocators = false;
bool g_used_default_allocator;
void *DefaultAllocate(size_t size) {
g_used_default_allocator = true;
return std::malloc(size);
}
void DefaultDeallocate(void *ptr, size_t size) {
std::free(ptr);
}
AllocateFunction g_allocate_func = UseDefaultAllocators ? DefaultAllocate : nullptr;
DeallocateFunction g_deallocate_func = UseDefaultAllocators ? DefaultDeallocate : nullptr;
}
void *Allocate(size_t size) {
AMS_ASSERT(g_allocate_func != nullptr);
return g_allocate_func(size);
}
void Deallocate(void *ptr, size_t size) {
AMS_ASSERT(g_deallocate_func != nullptr);
return g_deallocate_func(ptr, size);
}
void InitializeAllocator(AllocateFunction allocate_func, DeallocateFunction deallocate_func) {
AMS_ASSERT(allocate_func != nullptr);
AMS_ASSERT(deallocate_func != nullptr);
if constexpr (UseDefaultAllocators) {
AMS_ASSERT(g_used_default_allocator == false);
} else {
AMS_ASSERT(g_allocate_func == nullptr);
AMS_ASSERT(g_deallocate_func == nullptr);
}
g_allocate_func = allocate_func;
g_deallocate_func = deallocate_func;
}
}

View File

@@ -0,0 +1,544 @@
/*
* 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/>.
*/
#include <stratosphere.hpp>
namespace ams::fssystem {
namespace {
using Node = impl::BucketTreeNode<const s64 *>;
static_assert(sizeof(Node) == sizeof(BucketTree::NodeHeader));
static_assert(std::is_pod<Node>::value);
constexpr inline s32 NodeHeaderSize = sizeof(BucketTree::NodeHeader);
class StorageNode {
private:
class Offset {
public:
using difference_type = s64;
private:
s64 offset;
s32 stride;
public:
constexpr Offset(s64 offset, s32 stride) : offset(offset), stride(stride) { /* ... */ }
constexpr Offset &operator++() { this->offset += this->stride; return *this; }
constexpr Offset operator++(int) { Offset ret(*this); this->offset += this->stride; return ret; }
constexpr Offset &operator--() { this->offset -= this->stride; return *this; }
constexpr Offset operator--(int) { Offset ret(*this); this->offset -= this->stride; return ret; }
constexpr difference_type operator-(const Offset &rhs) const { return (this->offset - rhs.offset) / this->stride; }
constexpr Offset operator+(difference_type ofs) const { return Offset(this->offset + ofs * this->stride, this->stride); }
constexpr Offset operator-(difference_type ofs) const { return Offset(this->offset - ofs * this->stride, this->stride); }
constexpr Offset &operator+=(difference_type ofs) { this->offset += ofs * this->stride; return *this; }
constexpr Offset &operator-=(difference_type ofs) { this->offset -= ofs * this->stride; return *this; }
constexpr bool operator==(const Offset &rhs) const { return this->offset == rhs.offset; }
constexpr bool operator!=(const Offset &rhs) const { return this->offset != rhs.offset; }
constexpr s64 Get() const { return this->offset; }
};
private:
const Offset start;
const s32 count;
s32 index;
public:
StorageNode(size_t size, s32 count) : start(NodeHeaderSize, static_cast<s32>(size)), count(count), index(-1) { /* ... */ }
StorageNode(s64 ofs, size_t size, s32 count) : start(NodeHeaderSize + ofs, static_cast<s32>(size)), count(count), index(-1) { /* ... */ }
s32 GetIndex() const { return this->index; }
void Find(const char *buffer, s64 virtual_address) {
s32 end = this->count;
auto pos = this->start;
while (end > 0) {
auto half = end / 2;
auto mid = pos + half;
s64 offset = 0;
std::memcpy(std::addressof(offset), buffer + mid.Get(), sizeof(s64));
if (offset <= virtual_address) {
pos = mid + 1;
end -= half + 1;
} else {
end = half;
}
}
this->index = static_cast<s32>(pos - this->start) - 1;
}
Result Find(fs::SubStorage &storage, s64 virtual_address) {
s32 end = this->count;
auto pos = this->start;
while (end > 0) {
auto half = end / 2;
auto mid = pos + half;
s64 offset = 0;
R_TRY(storage.Read(mid.Get(), std::addressof(offset), sizeof(s64)));
if (offset <= virtual_address) {
pos = mid + 1;
end -= half + 1;
} else {
end = half;
}
}
this->index = static_cast<s32>(pos - this->start) - 1;
return ResultSuccess();
}
};
}
void BucketTree::Header::Format(s32 entry_count) {
AMS_ASSERT(entry_count >= 0);
this->magic = Magic;
this->version = Version;
this->entry_count = entry_count;
this->reserved = 0;
}
Result BucketTree::Header::Verify() const {
R_UNLESS(this->magic == Magic, fs::ResultInvalidBucketTreeSignature());
R_UNLESS(this->entry_count >= 0, fs::ResultInvalidBucketTreeEntryCount());
R_UNLESS(this->version <= Version, fs::ResultUnsupportedVersion());
return ResultSuccess();
}
Result BucketTree::NodeHeader::Verify(s32 node_index, size_t node_size, size_t entry_size) const {
R_UNLESS(this->index == node_index, fs::ResultInvalidArgument());
R_UNLESS(entry_size == 0 || node_size < entry_size + NodeHeaderSize, fs::ResultInvalidSize());
const size_t max_entry_count = (node_size - NodeHeaderSize) / entry_size;
R_UNLESS(this->count > 0 && static_cast<size_t>(this->count) <= max_entry_count, fs::ResultInvalidBucketTreeNodeEntryCount());
R_UNLESS(this->offset > 0, fs::ResultInvalidBucketTreeNodeOffset());
return ResultSuccess();
}
Result BucketTree::Initialize(IAllocator *allocator, fs::SubStorage node_storage, fs::SubStorage entry_storage, size_t node_size, size_t entry_size, s32 entry_count) {
/* Validate preconditions. */
AMS_ASSERT(allocator != nullptr);
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(!this->IsInitialized());
/* Ensure valid entry count. */
R_UNLESS(entry_count > 0, fs::ResultInvalidArgument());
/* Allocate node. */
R_UNLESS(this->node_l1.Allocate(allocator, node_size), fs::ResultBufferAllocationFailed());
auto node_guard = SCOPE_GUARD { this->node_l1.Free(node_size); };
/* Read node. */
R_TRY(node_storage.Read(0, this->node_l1.Get(), node_size));
/* Verify node. */
R_TRY(this->node_l1->Verify(0, node_size, sizeof(s64)));
/* Validate offsets. */
const auto offset_count = GetOffsetCount(node_size);
const auto entry_set_count = GetEntrySetCount(node_size, entry_size, entry_count);
const auto * const node = this->node_l1.Get<Node>();
s64 start_offset;
if (offset_count < entry_set_count && node->GetCount() < offset_count) {
start_offset = *node->GetEnd();
} else {
start_offset = *node->GetBegin();
}
const auto end_offset = node->GetEndOffset();
R_UNLESS(0 <= start_offset && start_offset <= node->GetBeginOffset(), fs::ResultInvalidBucketTreeEntryOffset());
R_UNLESS(start_offset < end_offset, fs::ResultInvalidBucketTreeEntryOffset());
/* Set member variables. */
this->node_storage = node_storage;
this->entry_storage = entry_storage;
this->node_size = node_size;
this->entry_size = entry_size;
this->entry_count = entry_count;
this->offset_count = offset_count;
this->entry_set_count = entry_set_count;
this->start_offset = start_offset;
this->end_offset = end_offset;
/* Cancel guard. */
node_guard.Cancel();
return ResultSuccess();
}
void BucketTree::Initialize(size_t node_size, s64 end_offset) {
AMS_ASSERT(NodeSizeMin <= node_size && node_size <= NodeSizeMax);
AMS_ASSERT(util::IsPowerOfTwo(node_size));
AMS_ASSERT(end_offset > 0);
AMS_ASSERT(!this->IsInitialized());
this->node_size = node_size;
this->end_offset = end_offset;
}
void BucketTree::Finalize() {
if (this->IsInitialized()) {
this->node_storage = fs::SubStorage();
this->entry_storage = fs::SubStorage();
this->node_l1.Free(this->node_size);
this->node_size = 0;
this->entry_size = 0;
this->entry_count = 0;
this->offset_count = 0;
this->entry_set_count = 0;
this->start_offset = 0;
this->end_offset = 0;
}
}
Result BucketTree::Find(Visitor *visitor, s64 virtual_address) const {
AMS_ASSERT(visitor != nullptr);
AMS_ASSERT(this->IsInitialized());
R_UNLESS(virtual_address >= 0, fs::ResultInvalidOffset());
R_UNLESS(!this->IsEmpty(), fs::ResultOutOfRange());
R_TRY(visitor->Initialize(this));
return visitor->Find(virtual_address);
}
Result BucketTree::InvalidateCache() {
/* Invalidate the node storage cache. */
{
s64 storage_size;
R_TRY(this->node_storage.GetSize(std::addressof(storage_size)));
R_TRY(this->node_storage.OperateRange(fs::OperationId::InvalidateCache, 0, storage_size));
}
/* Refresh start/end offsets. */
{
/* Read node. */
R_TRY(node_storage.Read(0, this->node_l1.Get(), this->node_size));
/* Verify node. */
R_TRY(this->node_l1->Verify(0, this->node_size, sizeof(s64)));
/* Validate offsets. */
const auto * const node = this->node_l1.Get<Node>();
s64 start_offset;
if (offset_count < this->entry_set_count && node->GetCount() < this->offset_count) {
start_offset = *node->GetEnd();
} else {
start_offset = *node->GetBegin();
}
const auto end_offset = node->GetEndOffset();
R_UNLESS(0 <= start_offset && start_offset <= node->GetBeginOffset(), fs::ResultInvalidBucketTreeEntryOffset());
R_UNLESS(start_offset < end_offset, fs::ResultInvalidBucketTreeEntryOffset());
/* Set refreshed offsets. */
this->start_offset = start_offset;
this->end_offset = end_offset;
}
/* Invalidate the entry storage cache. */
{
s64 storage_size;
R_TRY(this->entry_storage.GetSize(std::addressof(storage_size)));
R_TRY(this->entry_storage.OperateRange(fs::OperationId::InvalidateCache, 0, storage_size));
}
return ResultSuccess();
}
Result BucketTree::Visitor::Initialize(const BucketTree *tree) {
AMS_ASSERT(tree != nullptr);
AMS_ASSERT(this->tree == nullptr || this->tree == tree);
if (this->entry == nullptr) {
this->entry = tree->GetAllocator()->Allocate(tree->entry_size);
R_UNLESS(this->entry != nullptr, fs::ResultBufferAllocationFailed());
this->tree = tree;
}
return ResultSuccess();
}
Result BucketTree::Visitor::MoveNext() {
R_UNLESS(this->IsValid(), fs::ResultOutOfRange());
/* Invalidate our index, and read the header for the next index. */
auto entry_index = this->entry_index + 1;
if (entry_index == this->entry_set.info.count) {
const auto entry_set_index = this->entry_set.info.index + 1;
R_UNLESS(entry_set_index < this->entry_set_count, fs::ResultOutOfRange());
this->entry_index = -1;
const auto end = this->entry_set.info.end;
const auto entry_set_size = this->tree->node_size;
const auto entry_set_offset = entry_set_index * static_cast<s64>(entry_set_size);
R_TRY(this->tree->entry_storage.Read(entry_set_offset, std::addressof(this->entry_set), sizeof(EntrySetHeader)));
R_TRY(this->entry_set.header.Verify(entry_set_index, entry_set_size, this->tree->entry_size));
R_UNLESS(this->entry_set.info.start == end && this->entry_set.info.start < this->entry_set.info.end, fs::ResultInvalidBucketTreeEntrySetOffset());
entry_index = 0;
} else {
this->entry_index = 1;
}
/* Read the new entry. */
const auto entry_size = this->tree->entry_size;
const auto entry_offset = impl::GetBucketTreeEntryOffset(this->entry_set.info.index, this->tree->node_size, entry_size, entry_index);
R_TRY(this->tree->entry_storage.Read(entry_offset, std::addressof(this->entry), entry_size));
/* Note that we changed index. */
this->entry_index = entry_index;
return ResultSuccess();
}
Result BucketTree::Visitor::MovePrevious() {
R_UNLESS(this->IsValid(), fs::ResultOutOfRange());
/* Invalidate our index, and read the heasder for the previous index. */
auto entry_index = this->entry_index;
if (entry_index == 0) {
R_UNLESS(this->entry_set.info.index > 0, fs::ResultOutOfRange());
this->entry_index = -1;
const auto start = this->entry_set.info.start;
const auto entry_set_size = this->tree->node_size;
const auto entry_set_index = this->entry_set.info.index - 1;
const auto entry_set_offset = entry_set_index * static_cast<s64>(entry_set_size);
R_TRY(this->tree->entry_storage.Read(entry_set_offset, std::addressof(this->entry_set), sizeof(EntrySetHeader)));
R_TRY(this->entry_set.header.Verify(entry_set_index, entry_set_size, this->tree->entry_size));
R_UNLESS(this->entry_set.info.end == start && this->entry_set.info.start < this->entry_set.info.end, fs::ResultInvalidBucketTreeEntrySetOffset());
entry_index = this->entry_set.info.count;
} else {
this->entry_index = -1;
}
--entry_index;
/* Read the new entry. */
const auto entry_size = this->tree->entry_size;
const auto entry_offset = impl::GetBucketTreeEntryOffset(this->entry_set.info.index, this->tree->node_size, entry_size, entry_index);
R_TRY(this->tree->entry_storage.Read(entry_offset, std::addressof(this->entry), entry_size));
/* Note that we changed index. */
this->entry_index = entry_index;
return ResultSuccess();
}
Result BucketTree::Visitor::Find(s64 virtual_address) {
AMS_ASSERT(this->tree != nullptr);
/* Get the node. */
const auto * const node = this->tree->node_l1.Get<Node>();
R_UNLESS(virtual_address < node->GetEndOffset(), fs::ResultOutOfRange());
/* Get the entry set index. */
s32 entry_set_index = -1;
if (this->tree->IsExistOffsetL2OnL1() && virtual_address < node->GetBeginOffset()) {
const auto start = node->GetEnd();
const auto end = node->GetBegin() + tree->offset_count;
auto pos = std::upper_bound(start, end, virtual_address);
R_UNLESS(start < pos, fs::ResultOutOfRange());
--pos;
entry_set_index = static_cast<s32>(pos - start);
} else {
const auto start = node->GetBegin();
const auto end = node->GetEnd();
auto pos = std::upper_bound(start, end, virtual_address);
R_UNLESS(start < pos, fs::ResultOutOfRange());
--pos;
if (this->tree->IsExistL2()) {
const auto node_index = static_cast<s32>(pos - start);
R_UNLESS(0 <= node_index && node_index < this->tree->offset_count, fs::ResultInvalidBucketTreeNodeOffset());
R_TRY(this->FindEntrySet(std::addressof(entry_set_index), virtual_address, node_index));
} else {
entry_set_index = static_cast<s32>(pos - start);
}
}
/* Validate the entry set index. */
R_UNLESS(0 <= entry_set_index && entry_set_index < this->tree->entry_set_count, fs::ResultInvalidBucketTreeNodeOffset());
/* Find the entry. */
R_TRY(this->FindEntry(virtual_address, entry_set_index));
/* Set count. */
this->entry_set_count = this->tree->entry_set_count;
return ResultSuccess();
}
Result BucketTree::Visitor::FindEntrySet(s32 *out_index, s64 virtual_address, s32 node_index) {
const auto node_size = this->tree->node_size;
PooledBuffer pool(node_size, 1);
if (node_size <= pool.GetSize()) {
return this->FindEntrySetWithBuffer(out_index, virtual_address, node_index, pool.GetBuffer());
} else {
pool.Deallocate();
return this->FindEntrySetWithoutBuffer(out_index, virtual_address, node_index);
}
}
Result BucketTree::Visitor::FindEntrySetWithBuffer(s32 *out_index, s64 virtual_address, s32 node_index, char *buffer) {
/* Calculate node extents. */
const auto node_size = this->tree->node_size;
const auto node_offset = (node_index + 1) * static_cast<s64>(node_size);
fs::SubStorage &storage = tree->node_storage;
/* Read the node. */
R_TRY(storage.Read(node_offset, buffer, node_size));
/* Validate the header. */
NodeHeader header;
std::memcpy(std::addressof(header), buffer, NodeHeaderSize);
R_TRY(header.Verify(node_index, node_size, sizeof(s64)));
/* Create the node, and find. */
StorageNode node(sizeof(s64), header.count);
node.Find(buffer, virtual_address);
R_UNLESS(node.GetIndex() >= 0, fs::ResultOutOfRange());
/* Return the index. */
*out_index = this->tree->GetEntrySetIndex(header.index, node.GetIndex());
return ResultSuccess();
}
Result BucketTree::Visitor::FindEntrySetWithoutBuffer(s32 *out_index, s64 virtual_address, s32 node_index) {
/* Calculate node extents. */
const auto node_size = this->tree->node_size;
const auto node_offset = (node_index + 1) * static_cast<s64>(node_size);
fs::SubStorage &storage = tree->node_storage;
/* Read and validate the header. */
NodeHeader header;
R_TRY(storage.Read(node_offset, std::addressof(header), NodeHeaderSize));
R_TRY(header.Verify(node_index, node_size, sizeof(s64)));
/* Create the node, and find. */
StorageNode node(node_offset, sizeof(s64), header.count);
R_TRY(node.Find(storage, virtual_address));
R_UNLESS(node.GetIndex() >= 0, fs::ResultOutOfRange());
/* Return the index. */
*out_index = this->tree->GetEntrySetIndex(header.index, node.GetIndex());
return ResultSuccess();
}
Result BucketTree::Visitor::FindEntry(s64 virtual_address, s32 entry_set_index) {
const auto entry_set_size = this->tree->node_size;
PooledBuffer pool(entry_set_size, 1);
if (entry_set_size <= pool.GetSize()) {
return this->FindEntryWithBuffer(virtual_address, entry_set_index, pool.GetBuffer());
} else {
pool.Deallocate();
return this->FindEntryWithoutBuffer(virtual_address, entry_set_index);
}
}
Result BucketTree::Visitor::FindEntryWithBuffer(s64 virtual_address, s32 entry_set_index, char *buffer) {
/* Calculate entry set extents. */
const auto entry_size = this->tree->entry_size;
const auto entry_set_size = this->tree->node_size;
const auto entry_set_offset = entry_set_index * static_cast<s64>(entry_set_size);
fs::SubStorage &storage = tree->node_storage;
/* Read the entry set. */
R_TRY(storage.Read(entry_set_offset, buffer, entry_set_size));
/* Validate the entry_set. */
EntrySetHeader entry_set;
std::memcpy(std::addressof(entry_set), buffer, sizeof(EntrySetHeader));
R_TRY(entry_set.header.Verify(entry_set_index, entry_set_size, entry_size));
/* Create the node, and find. */
StorageNode node(entry_size, entry_set.info.count);
node.Find(buffer, virtual_address);
R_UNLESS(node.GetIndex() >= 0, fs::ResultOutOfRange());
/* Copy the data into entry. */
const auto entry_index = node.GetIndex();
const auto entry_offset = impl::GetBucketTreeEntryOffset(0, entry_size, entry_index);
std::memcpy(this->entry, buffer + entry_offset, entry_size);
/* Set our entry set/index. */
this->entry_set = entry_set;
this->entry_index = entry_index;
return ResultSuccess();
}
Result BucketTree::Visitor::FindEntryWithoutBuffer(s64 virtual_address, s32 entry_set_index) {
/* Calculate entry set extents. */
const auto entry_size = this->tree->entry_size;
const auto entry_set_size = this->tree->node_size;
const auto entry_set_offset = entry_set_index * static_cast<s64>(entry_set_size);
fs::SubStorage &storage = tree->node_storage;
/* Read and validate the entry_set. */
EntrySetHeader entry_set;
R_TRY(storage.Read(entry_set_offset, std::addressof(entry_set), sizeof(EntrySetHeader)));
R_TRY(entry_set.header.Verify(entry_set_index, entry_set_size, entry_size));
/* Create the node, and find. */
StorageNode node(entry_set_offset, entry_size, entry_set.info.count);
R_TRY(node.Find(storage, virtual_address));
R_UNLESS(node.GetIndex() >= 0, fs::ResultOutOfRange());
/* Copy the data into entry. */
const auto entry_index = node.GetIndex();
const auto entry_offset = impl::GetBucketTreeEntryOffset(entry_set_offset, entry_size, entry_index);
R_TRY(storage.Read(entry_offset, this->entry, entry_size));
/* Set our entry set/index. */
this->entry_set = entry_set;
this->entry_index = entry_index;
return ResultSuccess();
}
}

View File

@@ -0,0 +1,232 @@
/*
* 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/>.
*/
#include <stratosphere.hpp>
#include "fssystem_key_slot_cache.hpp"
namespace ams::fssystem {
namespace {
constexpr inline const size_t KeySize = crypto::AesDecryptor128::KeySize;
constexpr inline const size_t AcidSignatureKeyGenerationMax = 1;
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 AcidSignatureKeyPublicExponent[] = {
0x01, 0x00, 0x01
};
NcaCryptoConfiguration g_nca_crypto_configuration_dev;
NcaCryptoConfiguration g_nca_crypto_configuration_prod;
constexpr inline s32 KeySlotCacheEntryCount = 3;
KeySlotCache g_key_slot_cache;
std::optional<KeySlotCacheEntry> g_key_slot_cache_entry[KeySlotCacheEntryCount];
spl::AccessKey &GetNcaKekAccessKey(s32 key_type) {
static spl::AccessKey s_nca_kek_access_key_array[KeyAreaEncryptionKeyCount] = {};
static spl::AccessKey s_nca_header_kek_access_key = {};
static spl::AccessKey s_invalid_nca_kek_access_key = {};
if (key_type > static_cast<s32>(KeyType::NcaHeaderKey) || IsInvalidKeyTypeValue(key_type)) {
return s_invalid_nca_kek_access_key;
} else if (key_type == static_cast<s32>(KeyType::NcaHeaderKey)) {
return s_nca_header_kek_access_key;
} else {
return s_nca_kek_access_key_array[key_type];
}
}
void GenerateNcaKey(void *dst, size_t dst_size, const void *src, size_t src_size, s32 key_type, const NcaCryptoConfiguration &cfg) {
R_ABORT_UNLESS(spl::GenerateAesKey(dst, dst_size, GetNcaKekAccessKey(key_type), src, src_size));
}
void DecryptAesCtr(void *dst, size_t dst_size, s32 key_type, const void *enc_key, size_t enc_key_size, const void *iv, size_t iv_size, const void *src, size_t src_size) {
std::unique_ptr<KeySlotCacheAccessor> accessor;
R_TRY_CATCH(g_key_slot_cache.Find(std::addressof(accessor), enc_key, enc_key_size, key_type)) {
R_CATCH(fs::ResultTargetNotFound) {
R_ABORT_UNLESS(g_key_slot_cache.AllocateHighPriority(std::addressof(accessor), enc_key, enc_key_size, key_type));
R_ABORT_UNLESS(spl::LoadAesKey(accessor->GetKeySlotIndex(), GetNcaKekAccessKey(key_type), enc_key, enc_key_size));
}
} R_END_TRY_CATCH_WITH_ABORT_UNLESS;
R_ABORT_UNLESS(spl::ComputeCtr(dst, dst_size, accessor->GetKeySlotIndex(), src, src_size, iv, iv_size));
}
void DecryptAesCtrForPreparedKey(void *dst, size_t dst_size, s32 key_type, const void *enc_key, size_t enc_key_size, const void *iv, size_t iv_size, const void *src, size_t src_size) {
std::unique_ptr<KeySlotCacheAccessor> accessor;
key_type = static_cast<s32>(KeyType::NcaExternalKey);
R_TRY_CATCH(g_key_slot_cache.Find(std::addressof(accessor), enc_key, enc_key_size, key_type)) {
R_CATCH(fs::ResultTargetNotFound) {
R_ABORT_UNLESS(g_key_slot_cache.AllocateHighPriority(std::addressof(accessor), enc_key, enc_key_size, key_type));
spl::AccessKey access_key;
AMS_ABORT_UNLESS(enc_key_size == sizeof(access_key));
std::memcpy(std::addressof(access_key), enc_key, sizeof(access_key));
R_ABORT_UNLESS(spl::LoadPreparedAesKey(accessor->GetKeySlotIndex(), access_key));
}
} R_END_TRY_CATCH_WITH_ABORT_UNLESS;
R_ABORT_UNLESS(spl::ComputeCtr(dst, dst_size, accessor->GetKeySlotIndex(), src, src_size, iv, iv_size));
}
}
const ::ams::fssystem::NcaCryptoConfiguration *GetNcaCryptoConfiguration(bool prod) {
/* Decide which configuration to use. */
NcaCryptoConfiguration *cfg = prod ? std::addressof(g_nca_crypto_configuration_prod) : std::addressof(g_nca_crypto_configuration_dev);
std::memcpy(cfg, fssrv::GetDefaultNcaCryptoConfiguration(prod), sizeof(NcaCryptoConfiguration));
/* Set the key generation functions. */
cfg->generate_key = GenerateNcaKey;
cfg->decrypt_aes_ctr = DecryptAesCtr;
cfg->decrypt_aes_ctr_external = DecryptAesCtrForPreparedKey;
cfg->is_plaintext_header_available = !prod;
return cfg;
}
void SetUpKekAccessKeys(bool prod) {
/* Get the crypto configuration. */
const NcaCryptoConfiguration *nca_crypto_cfg = GetNcaCryptoConfiguration(prod);
/* Setup the nca keys. */
{
constexpr s32 Option = 0;
/* Setup the key area encryption keys. */
for (u8 i = 0; i < NcaCryptoConfiguration::KeyGenerationMax; ++i) {
spl::GenerateAesKek(std::addressof(GetNcaKekAccessKey(GetKeyTypeValue(0, i))), nca_crypto_cfg->key_area_encryption_key_source[0], KeySize, i, Option);
spl::GenerateAesKek(std::addressof(GetNcaKekAccessKey(GetKeyTypeValue(1, i))), nca_crypto_cfg->key_area_encryption_key_source[1], KeySize, i, Option);
spl::GenerateAesKek(std::addressof(GetNcaKekAccessKey(GetKeyTypeValue(2, i))), nca_crypto_cfg->key_area_encryption_key_source[2], KeySize, i, Option);
}
/* Setup the header encryption key. */
R_ABORT_UNLESS(spl::GenerateAesKek(std::addressof(GetNcaKekAccessKey(static_cast<s32>(KeyType::NcaHeaderKey))), nca_crypto_cfg->header_encryption_key_source, KeySize, 0, Option));
}
/* TODO FS-REIMPL: Save stuff. */
/* Setup the keyslot cache. */
for (s32 i = 0; i < KeySlotCacheEntryCount; i++) {
s32 slot_index;
R_ABORT_UNLESS(spl::AllocateAesKeySlot(std::addressof(slot_index)));
g_key_slot_cache_entry[i].emplace(slot_index);
g_key_slot_cache.AddEntry(std::addressof(g_key_slot_cache_entry[i].value()));
}
}
void InvalidateHardwareAesKey() {
constexpr u8 InvalidKey[KeySize] = {};
for (s32 i = 0; i < KeySlotCacheEntryCount; ++i) {
std::unique_ptr<KeySlotCacheAccessor> accessor;
R_ABORT_UNLESS(g_key_slot_cache.AllocateHighPriority(std::addressof(accessor), InvalidKey, KeySize, -1 - i));
}
}
const u8 *GetAcidSignatureKeyModulus(bool prod, size_t key_generation) {
AMS_ASSERT(key_generation <= AcidSignatureKeyGenerationMax);
const size_t used_keygen = (key_generation % (AcidSignatureKeyGenerationMax + 1));
return prod ? AcidSignatureKeyModulusProd[used_keygen] : AcidSignatureKeyModulusDev[used_keygen];
}
const u8 *GetAcidSignatureKeyPublicExponent() {
return AcidSignatureKeyPublicExponent;
}
}

View File

@@ -0,0 +1,152 @@
/*
* 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/>.
*/
#include <stratosphere.hpp>
namespace ams::fssystem {
/* TODO: All of this should really be inside fs process, but ams.mitm wants it to. */
/* How should we handle this? */
namespace {
/* Official FS has a 4.5 MB exp heap, a 6 MB buffer pool, an 8 MB device buffer manager heap, and a 14 MB buffer manager heap. */
/* We don't need so much memory for ams.mitm (as we're servicing a much more limited context). */
/* We'll give ourselves a 2.5 MB exp heap, a 2 MB buffer pool, a 2 MB device buffer manager heap, and a 2 MB buffer manager heap. */
/* These numbers match signed-system-partition safe FS. */
constexpr size_t ExpHeapSize = 2_MB + 512_KB;
constexpr size_t BufferPoolSize = 2_MB;
constexpr size_t DeviceBufferSize = 2_MB;
constexpr size_t BufferManagerHeapSize = 2_MB;
constexpr size_t MaxCacheCount = 1024;
constexpr size_t BlockSize = 16_KB;
alignas(os::MemoryPageSize) u8 g_exp_heap_buffer[ExpHeapSize];
lmem::HeapHandle g_exp_heap_handle = nullptr;
fssrv::PeakCheckableMemoryResourceFromExpHeap g_exp_allocator(ExpHeapSize);
void InitializeExpHeap() {
if (g_exp_heap_handle == nullptr) {
g_exp_heap_handle = lmem::CreateExpHeap(g_exp_heap_buffer, ExpHeapSize, lmem::CreateOption_ThreadSafe);
AMS_ABORT_UNLESS(g_exp_heap_handle != nullptr);
g_exp_allocator.SetHeapHandle(g_exp_heap_handle);
}
}
void *AllocateForFileSystemProxy(size_t size) {
AMS_ABORT_UNLESS(g_exp_heap_handle != nullptr);
auto scoped_lock = g_exp_allocator.GetScopedLock();
void *p = lmem::AllocateFromExpHeap(g_exp_heap_handle, size);
g_exp_allocator.OnAllocate(p, size);
return p;
}
void DeallocateForFileSystemProxy(void *p, size_t size) {
AMS_ABORT_UNLESS(g_exp_heap_handle != nullptr);
auto scoped_lock = g_exp_allocator.GetScopedLock();
g_exp_allocator.OnDeallocate(p, size);
lmem::FreeToExpHeap(g_exp_heap_handle, p);
}
alignas(os::MemoryPageSize) u8 g_device_buffer[DeviceBufferSize];
alignas(os::MemoryPageSize) u8 g_buffer_pool[BufferPoolSize];
TYPED_STORAGE(mem::StandardAllocator) g_buffer_allocator;
TYPED_STORAGE(fssrv::MemoryResourceFromStandardAllocator) g_allocator;
/* TODO: Nintendo uses os::SetMemoryHeapSize (svc::SetHeapSize) and os::AllocateMemoryBlock for the BufferManager heap. */
/* It's unclear how we should handle this in ams.mitm (especially hoping to reuse some logic for fs reimpl). */
/* Should we be doing the same(?) */
TYPED_STORAGE(fssystem::FileSystemBufferManager) g_buffer_manager;
alignas(os::MemoryPageSize) u8 g_buffer_manager_heap[BufferManagerHeapSize];
/* FileSystem creators. */
TYPED_STORAGE(fssrv::fscreator::RomFileSystemCreator) g_rom_fs_creator;
TYPED_STORAGE(fssrv::fscreator::PartitionFileSystemCreator) g_partition_fs_creator;
TYPED_STORAGE(fssrv::fscreator::StorageOnNcaCreator) g_storage_on_nca_creator;
fssrv::fscreator::FileSystemCreatorInterfaces g_fs_creator_interfaces = {};
}
void InitializeForFileSystemProxy() {
/* TODO FS-REIMPL: fssystem::RegisterServiceContext */
/* TODO FS-REIMPL: spl::InitializeForFs(); */
/* Determine whether we're prod or dev. */
bool is_prod = !spl::IsDevelopment();
bool is_development_function_enabled = spl::IsDevelopmentFunctionEnabled();
/* Setup our crypto configuration. */
SetUpKekAccessKeys(is_prod);
/* Setup our heap. */
InitializeExpHeap();
/* Initialize buffer allocator. */
new (GetPointer(g_buffer_allocator)) mem::StandardAllocator(g_buffer_pool, BufferPoolSize);
new (GetPointer(g_allocator)) fssrv::MemoryResourceFromStandardAllocator(GetPointer(g_buffer_allocator));
/* Set allocators. */
fs::SetAllocator(AllocateForFileSystemProxy, DeallocateForFileSystemProxy);
fssystem::InitializeAllocator(AllocateForFileSystemProxy, DeallocateForFileSystemProxy);
/* Initialize the buffer manager. */
/* TODO FS-REIMPL: os::AllocateMemoryBlock(...); */
new (GetPointer(g_buffer_manager)) fssystem::FileSystemBufferManager;
GetReference(g_buffer_manager).Initialize(MaxCacheCount, reinterpret_cast<uintptr_t>(g_buffer_manager_heap), BufferManagerHeapSize, BlockSize);
/* TODO FS-REIMPL: Memory Report Creators, fssrv::SetMemoryReportCreator */
/* TODO FS-REIMPL: Create Pooled Threads, fssystem::RegisterThreadPool. */
/* Initialize fs creators. */
new (GetPointer(g_rom_fs_creator)) fssrv::fscreator::RomFileSystemCreator(GetPointer(g_allocator));
new (GetPointer(g_partition_fs_creator)) fssrv::fscreator::PartitionFileSystemCreator;
new (GetPointer(g_storage_on_nca_creator)) fssrv::fscreator::StorageOnNcaCreator(GetPointer(g_allocator), *GetNcaCryptoConfiguration(is_prod), is_prod, GetPointer(g_buffer_manager));
/* TODO FS-REIMPL: Initialize other creators. */
g_fs_creator_interfaces = {
.rom_fs_creator = GetPointer(g_rom_fs_creator),
.partition_fs_creator = GetPointer(g_partition_fs_creator),
.storage_on_nca_creator = GetPointer(g_storage_on_nca_creator),
};
/* TODO FS-REIMPL: Sd Card detection, speed emulation. */
/* Initialize fssrv. TODO FS-REIMPL: More arguments, more actions taken. */
fssrv::InitializeForFileSystemProxy(std::addressof(g_fs_creator_interfaces), GetPointer(g_buffer_manager), is_development_function_enabled);
/* Disable auto-abort in fs library code. */
/* TODO: fs::SetEnabledAutoAbort(false); */
/* TODO FS-REIMPL: Initialize fsp server. */
/* NOTE: This is done in fsp server init, normally. */
fssystem::InitializeBufferPool(reinterpret_cast<char *>(g_device_buffer), DeviceBufferSize);
}
const ::ams::fssrv::fscreator::FileSystemCreatorInterfaces *GetFileSystemCreatorInterfaces() {
return std::addressof(g_fs_creator_interfaces);
}
}

View File

@@ -0,0 +1,184 @@
/*
* 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/>.
*/
#include <stratosphere.hpp>
#include "fssystem_hierarchical_sha256_storage.hpp"
namespace ams::fssystem {
namespace {
s32 Log2(s32 value) {
AMS_ASSERT(value > 0);
AMS_ASSERT(util::IsPowerOfTwo(value));
s32 log = 0;
while ((value >>= 1) > 0) {
++log;
}
return log;
}
}
Result HierarchicalSha256Storage::Initialize(IStorage **base_storages, s32 layer_count, size_t htbs, void *hash_buf, size_t hash_buf_size) {
/* Validate preconditions. */
AMS_ASSERT(layer_count == LayerCount);
AMS_ASSERT(util::IsPowerOfTwo(htbs));
AMS_ASSERT(hash_buf != nullptr);
/* Set size tracking members. */
this->hash_target_block_size = htbs;
this->log_size_ratio = Log2(this->hash_target_block_size / HashSize);
/* Get the base storage size. */
R_TRY(base_storages[2]->GetSize(std::addressof(this->base_storage_size)));
{
auto size_guard = SCOPE_GUARD { this->base_storage_size = 0; };
R_UNLESS(this->base_storage_size <= static_cast<s64>(HashSize) << log_size_ratio << log_size_ratio, fs::ResultHierarchicalSha256BaseStorageTooLarge());
size_guard.Cancel();
}
/* Set hash buffer tracking members. */
this->base_storage = base_storages[2];
this->hash_buffer = static_cast<char *>(hash_buf);
this->hash_buffer_size = hash_buf_size;
/* Read the master hash. */
u8 master_hash[HashSize];
R_TRY(base_storages[0]->Read(0, master_hash, HashSize));
/* Read and validate the data being hashed. */
s64 hash_storage_size;
R_TRY(base_storages[1]->GetSize(std::addressof(hash_storage_size)));
AMS_ASSERT(util::IsAligned(hash_storage_size, HashSize));
AMS_ASSERT(hash_storage_size <= this->hash_target_block_size);
AMS_ASSERT(hash_storage_size <= static_cast<s64>(this->hash_buffer_size));
R_TRY(base_storages[1]->Read(0, this->hash_buffer, static_cast<size_t>(hash_storage_size)));
/* Calculate and verify the master hash. */
u8 calc_hash[HashSize];
crypto::GenerateSha256Hash(calc_hash, sizeof(calc_hash), this->hash_buffer, static_cast<size_t>(hash_storage_size));
R_UNLESS(crypto::IsSameBytes(master_hash, calc_hash, HashSize), fs::ResultHierarchicalSha256HashVerificationFailed());
return ResultSuccess();
}
Result HierarchicalSha256Storage::Read(s64 offset, void *buffer, size_t size) {
/* Succeed if zero-size. */
R_SUCCEED_IF(size == 0);
/* Validate that we have a buffer to read into. */
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
/* Validate preconditions. */
R_UNLESS(util::IsAligned(offset, this->hash_target_block_size), fs::ResultInvalidArgument());
R_UNLESS(util::IsAligned(size, this->hash_target_block_size), fs::ResultInvalidArgument());
/* Read the data. */
const size_t reduced_size = static_cast<size_t>(std::min<s64>(this->base_storage_size, util::AlignUp(offset + size, this->hash_target_block_size) - offset));
R_TRY(this->base_storage->Read(offset, buffer, reduced_size));
/* Temporarily increase our thread priority. */
ScopedThreadPriorityChanger cp(+1, ScopedThreadPriorityChanger::Mode::Relative);
/* Setup tracking variables. */
auto cur_offset = offset;
auto remaining_size = reduced_size;
while (remaining_size > 0) {
/* Generate the hash of the region we're validating. */
u8 hash[HashSize];
const auto cur_size = static_cast<size_t>(std::min<s64>(this->hash_target_block_size, remaining_size));
crypto::GenerateSha256Hash(hash, sizeof(hash), static_cast<u8 *>(buffer) + (cur_offset - offset), cur_size);
AMS_ASSERT(static_cast<size_t>(cur_offset >> this->log_size_ratio) < this->hash_buffer_size);
/* Check the hash. */
{
std::scoped_lock lk(this->mutex);
auto clear_guard = SCOPE_GUARD { std::memset(buffer, 0, size); };
R_UNLESS(crypto::IsSameBytes(hash, std::addressof(this->hash_buffer[cur_offset >> this->log_size_ratio]), HashSize), fs::ResultHierarchicalSha256HashVerificationFailed());
clear_guard.Cancel();
}
/* Advance. */
cur_offset += cur_size;
remaining_size -= cur_size;
}
return ResultSuccess();
}
Result HierarchicalSha256Storage::Write(s64 offset, const void *buffer, size_t size) {
/* Succeed if zero-size. */
R_SUCCEED_IF(size == 0);
/* Validate that we have a buffer to read into. */
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
/* Validate preconditions. */
R_UNLESS(util::IsAligned(offset, this->hash_target_block_size), fs::ResultInvalidArgument());
R_UNLESS(util::IsAligned(size, this->hash_target_block_size), fs::ResultInvalidArgument());
/* Setup tracking variables. */
const size_t reduced_size = static_cast<size_t>(std::min<s64>(this->base_storage_size, util::AlignUp(offset + size, this->hash_target_block_size) - offset));
auto cur_offset = offset;
auto remaining_size = reduced_size;
while (remaining_size > 0) {
/* Generate the hash of the region we're validating. */
u8 hash[HashSize];
const auto cur_size = static_cast<size_t>(std::min<s64>(this->hash_target_block_size, remaining_size));
{
/* Temporarily increase our thread priority. */
ScopedThreadPriorityChanger cp(+1, ScopedThreadPriorityChanger::Mode::Relative);
crypto::GenerateSha256Hash(hash, sizeof(hash), static_cast<const u8 *>(buffer) + (cur_offset - offset), cur_size);
}
/* Write the data. */
R_TRY(this->base_storage->Write(cur_offset, static_cast<const u8 *>(buffer) + (cur_offset - offset), cur_size));
/* Write the hash. */
{
std::scoped_lock lk(this->mutex);
std::memcpy(std::addressof(this->hash_buffer[cur_offset >> this->log_size_ratio]), hash, HashSize);
}
/* Advance. */
cur_offset += cur_size;
remaining_size -= cur_size;
}
return ResultSuccess();
}
Result HierarchicalSha256Storage::OperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) {
/* Succeed if zero-size. */
R_SUCCEED_IF(size == 0);
/* Validate preconditions. */
R_UNLESS(util::IsAligned(offset, this->hash_target_block_size), fs::ResultInvalidArgument());
R_UNLESS(util::IsAligned(size, this->hash_target_block_size), fs::ResultInvalidArgument());
/* Determine size to use. */
const auto reduced_size = std::min<s64>(this->base_storage_size, util::AlignUp(offset + size, this->hash_target_block_size) - offset);
/* Operate on the base storage. */
return this->base_storage->OperateRange(dst, dst_size, op_id, offset, reduced_size, src, src_size);
}
}

View File

@@ -0,0 +1,57 @@
/*
* 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.hpp>
namespace ams::fssystem {
class HierarchicalSha256Storage : public ::ams::fs::IStorage, public ::ams::fs::impl::Newable {
NON_COPYABLE(HierarchicalSha256Storage);
NON_MOVEABLE(HierarchicalSha256Storage);
public:
static constexpr s32 LayerCount = 3;
static constexpr size_t HashSize = crypto::Sha256Generator::HashSize;
private:
os::Mutex mutex;
IStorage *base_storage;
s64 base_storage_size;
char *hash_buffer;
size_t hash_buffer_size;
s32 hash_target_block_size;
s32 log_size_ratio;
public:
HierarchicalSha256Storage() : mutex(false) { /* ... */ }
Result Initialize(IStorage **base_storages, s32 layer_count, size_t htbs, void *hash_buf, size_t hash_buf_size);
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 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 {
return this->base_storage->GetSize(out);
}
virtual Result Flush() override {
return ResultSuccess();
}
virtual Result SetSize(s64 size) override {
return fs::ResultUnsupportedOperationInHierarchicalSha256StorageA();
}
};
}

View File

@@ -0,0 +1,177 @@
/*
* 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/>.
*/
#include <stratosphere.hpp>
namespace ams::fssystem {
Result IndirectStorage::Initialize(IAllocator *allocator, fs::SubStorage table_storage) {
/* Read and verify the bucket tree header. */
BucketTree::Header header;
R_TRY(table_storage.Read(0, std::addressof(header), sizeof(header)));
R_TRY(header.Verify());
/* Determine extents. */
const auto node_storage_size = QueryNodeStorageSize(header.entry_count);
const auto entry_storage_size = QueryEntryStorageSize(header.entry_count);
const auto node_storage_offset = QueryHeaderStorageSize();
const auto entry_storage_offset = node_storage_offset + node_storage_size;
/* Initialize. */
return this->Initialize(allocator, fs::SubStorage(std::addressof(table_storage), node_storage_offset, node_storage_size), fs::SubStorage(std::addressof(table_storage), entry_storage_offset, entry_storage_size), header.entry_count);
}
void IndirectStorage::Finalize() {
if (this->IsInitialized()) {
this->table.Finalize();
for (auto i = 0; i < StorageCount; i++) {
this->data_storage[i] = fs::SubStorage();
}
}
}
Result IndirectStorage::GetEntryList(Entry *out_entries, s32 *out_entry_count, s32 entry_count, s64 offset, s64 size) {
/* Validate pre-conditions. */
AMS_ASSERT(offset >= 0);
AMS_ASSERT(size >= 0);
AMS_ASSERT(this->IsInitialized());
/* Clear the out count. */
R_UNLESS(out_entry_count != nullptr, fs::ResultNullptrArgument());
*out_entry_count = 0;
/* Succeed if there's no range. */
R_SUCCEED_IF(size == 0);
/* If we have an output array, we need it to be non-null. */
R_UNLESS(out_entries != nullptr || entry_count == 0, fs::ResultNullptrArgument());
/* Check that our range is valid. */
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 loop over entries. */
const auto end_offset = offset + static_cast<s64>(size);
s32 count = 0;
auto cur_entry = *visitor.Get<Entry>();
while (cur_entry.GetVirtualOffset() < end_offset) {
/* Try to write the entry to the out list */
if (entry_count != 0) {
if (count >= entry_count) {
break;
}
std::memcpy(out_entries + count, std::addressof(cur_entry), sizeof(Entry));
}
count++;
/* Advance. */
if (visitor.CanMoveNext()) {
R_TRY(visitor.MoveNext());
cur_entry = *visitor.Get<Entry>();
} else {
break;
}
}
/* Write the output count. */
*out_entry_count = count;
return ResultSuccess();
}
Result IndirectStorage::Read(s64 offset, void *buffer, size_t size) {
/* Validate pre-conditions. */
AMS_ASSERT(offset >= 0);
AMS_ASSERT(this->IsInitialized());
/* Succeed if there's nothing to read. */
R_SUCCEED_IF(size == 0);
/* Ensure that we have a buffer to read to. */
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
R_TRY(this->OperatePerEntry<true>(offset, size, [=](fs::IStorage *storage, s64 data_offset, s64 cur_offset, s64 cur_size) -> Result {
R_TRY(storage->Read(data_offset, reinterpret_cast<u8 *>(buffer) + (cur_offset - offset), static_cast<size_t>(cur_size)));
return ResultSuccess();
}));
return ResultSuccess();
}
Result IndirectStorage::OperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) {
switch (op_id) {
case fs::OperationId::InvalidateCache:
{
if (size > 0) {
/* Validate arguments. */
R_UNLESS(this->table.Includes(offset, size), fs::ResultOutOfRange());
if (!this->table.IsEmpty()) {
/* Invalidate our table's cache. */
R_TRY(this->table.InvalidateCache());
/* Operate on our entries. */
R_TRY(this->OperatePerEntry<false>(offset, size, [=](fs::IStorage *storage, s64 data_offset, s64 cur_offset, s64 cur_size) -> Result {
R_TRY(storage->OperateRange(dst, dst_size, op_id, data_offset, cur_size, src, src_size));
return ResultSuccess();
}));
}
return ResultSuccess();
}
return ResultSuccess();
}
case fs::OperationId::QueryRange:
{
/* Validate that we have an output range info. */
R_UNLESS(dst != nullptr, fs::ResultNullptrArgument());
R_UNLESS(dst_size == sizeof(fs::QueryRangeInfo), fs::ResultInvalidSize());
if (size > 0) {
/* Validate arguments. */
R_UNLESS(this->table.Includes(offset, size), fs::ResultOutOfRange());
if (!this->table.IsEmpty()) {
/* Create a new info. */
fs::QueryRangeInfo merged_info;
merged_info.Clear();
/* Operate on our entries. */
R_TRY(this->OperatePerEntry<false>(offset, size, [=, &merged_info](fs::IStorage *storage, s64 data_offset, s64 cur_offset, s64 cur_size) -> Result {
fs::QueryRangeInfo cur_info;
R_TRY(storage->OperateRange(std::addressof(cur_info), sizeof(cur_info), op_id, data_offset, cur_size, src, src_size));
merged_info.Merge(cur_info);
return ResultSuccess();
}));
/* Write the merged info. */
*reinterpret_cast<fs::QueryRangeInfo *>(dst) = merged_info;
}
}
return ResultSuccess();
}
default:
return fs::ResultUnsupportedOperationInIndirectStorageC();
}
return ResultSuccess();
}
}

View File

@@ -0,0 +1,45 @@
/*
* 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/>.
*/
#include <stratosphere.hpp>
namespace ams::fssystem {
Result IntegrityRomFsStorage::Initialize(save::HierarchicalIntegrityVerificationInformation level_hash_info, Hash master_hash, save::HierarchicalIntegrityVerificationStorage::HierarchicalStorageInformation storage_info, IBufferManager *bm) {
/* Validate preconditions. */
AMS_ASSERT(bm != nullptr);
/* Set master hash. */
this->master_hash = master_hash;
this->master_hash_storage = std::make_unique<fs::MemoryStorage>(std::addressof(this->master_hash), sizeof(Hash));
R_UNLESS(this->master_hash_storage != nullptr, fs::ResultAllocationFailureInIntegrityRomFsStorageA());
/* Set the master hash storage. */
storage_info[0] = fs::SubStorage(this->master_hash_storage.get(), 0, sizeof(Hash));
/* Set buffers. */
for (size_t i = 0; i < util::size(this->buffers.buffers); ++i) {
this->buffers.buffers[i] = bm;
}
/* Initialize our integrity storage. */
return this->integrity_storage.Initialize(level_hash_info, storage_info, std::addressof(this->buffers), std::addressof(this->mutex), fs::StorageType_RomFs);
}
void IntegrityRomFsStorage::Finalize() {
this->integrity_storage.Finalize();
}
}

View File

@@ -0,0 +1,133 @@
/*
* 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.hpp>
namespace ams::fssystem {
class KeySlotCacheAccessor : public ::ams::fs::impl::Newable {
NON_COPYABLE(KeySlotCacheAccessor);
NON_MOVEABLE(KeySlotCacheAccessor);
private:
std::unique_lock<os::Mutex> lk;
const s32 slot_index;
public:
KeySlotCacheAccessor(s32 idx, std::unique_lock<os::Mutex> &&l) : lk(std::move(l)), slot_index(idx) { /* ... */ }
s32 GetKeySlotIndex() const { return this->slot_index; }
};
class KeySlotCacheEntry : public util::IntrusiveListBaseNode<KeySlotCacheEntry> {
NON_COPYABLE(KeySlotCacheEntry);
NON_MOVEABLE(KeySlotCacheEntry);
public:
static constexpr size_t KeySize = crypto::AesDecryptor128::KeySize;
private:
const s32 slot_index;
u8 key1[KeySize];
s32 key2;
public:
explicit KeySlotCacheEntry(s32 idx) : slot_index(idx), key2(-1) {
std::memset(this->key1, 0, sizeof(this->key1));
}
bool Contains(const void *key, size_t key_size, s32 key2) const {
AMS_ASSERT(key_size == KeySize);
return key2 == this->key2 && std::memcmp(this->key1, key, KeySize) == 0;
}
s32 GetKeySlotIndex() const { return this->slot_index; }
void SetKey(const void *key, size_t key_size, s32 key2) {
AMS_ASSERT(key_size == KeySize);
std::memcpy(this->key1, key, key_size);
this->key2 = key2;
}
};
class KeySlotCache {
NON_COPYABLE(KeySlotCache);
NON_MOVEABLE(KeySlotCache);
private:
using KeySlotCacheEntryList = util::IntrusiveListBaseTraits<KeySlotCacheEntry>::ListType;
private:
os::Mutex mutex;
KeySlotCacheEntryList high_priority_mru_list;
KeySlotCacheEntryList low_priority_mru_list;
public:
constexpr KeySlotCache() : mutex(false), high_priority_mru_list(), low_priority_mru_list() { /* ... */ }
Result AllocateHighPriority(std::unique_ptr<KeySlotCacheAccessor> *out, const void *key, size_t key_size, s32 key2) {
return this->AllocateFromLru(out, this->high_priority_mru_list, key, key_size, key2);
}
Result AllocateLowPriority(std::unique_ptr<KeySlotCacheAccessor> *out, const void *key, size_t key_size, s32 key2) {
return this->AllocateFromLru(out, this->high_priority_mru_list, key, key_size, key2);
}
Result Find(std::unique_ptr<KeySlotCacheAccessor> *out, const void *key, size_t key_size, s32 key2) {
std::unique_lock lk(this->mutex);
KeySlotCacheEntryList *lists[2] = { std::addressof(this->high_priority_mru_list), std::addressof(this->low_priority_mru_list) };
for (auto list : lists) {
for (auto it = list->begin(); it != list->end(); ++it) {
if (it->Contains(key, key_size, key2)) {
std::unique_ptr accessor = std::make_unique<KeySlotCacheAccessor>(it->GetKeySlotIndex(), std::move(lk));
R_UNLESS(accessor != nullptr, fs::ResultAllocationFailure());
*out = std::move(accessor);
this->UpdateMru(list, it);
return ResultSuccess();
}
}
}
return fs::ResultTargetNotFound();
}
void AddEntry(KeySlotCacheEntry *entry) {
std::unique_lock lk(this->mutex);
this->low_priority_mru_list.push_front(*entry);
}
private:
Result AllocateFromLru(std::unique_ptr<KeySlotCacheAccessor> *out, KeySlotCacheEntryList &dst_list, const void *key, size_t key_size, s32 key2) {
std::unique_lock lk(this->mutex);
KeySlotCacheEntryList &src_list = this->low_priority_mru_list.empty() ? this->high_priority_mru_list : this->low_priority_mru_list;
AMS_ASSERT(!src_list.empty());
auto it = src_list.rbegin();
std::unique_ptr accessor = std::make_unique<KeySlotCacheAccessor>(it->GetKeySlotIndex(), std::move(lk));
*out = std::move(accessor);
it->SetKey(key, key_size, key2);
auto *entry = std::addressof(*it);
src_list.pop_back();
dst_list.push_front(*entry);
return ResultSuccess();
}
void UpdateMru(KeySlotCacheEntryList *list, KeySlotCacheEntryList::iterator it) {
auto *entry = std::addressof(*it);
list->erase(it);
list->push_front(*entry);
}
};
}

View File

@@ -0,0 +1,89 @@
/*
* 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.hpp>
namespace ams::fssystem {
template<typename Key, typename Value>
class LruListCache {
NON_COPYABLE(LruListCache);
NON_MOVEABLE(LruListCache);
public:
class Node : public ::ams::fs::impl::Newable {
NON_COPYABLE(Node);
NON_MOVEABLE(Node);
public:
Key key;
Value value;
util::IntrusiveListNode mru_list_node;
public:
explicit Node(const Value &value) : value(value) { /* ... */ }
};
private:
using MruList = typename util::IntrusiveListMemberTraits<&Node::mru_list_node>::ListType;
private:
MruList mru_list;
public:
constexpr LruListCache() : mru_list() { /* ... */ }
bool FindValueAndUpdateMru(Value *out, const Key &key) {
for (auto it = this->mru_list.begin(); it != this->mru_list.end(); ++it) {
if (it->key == key) {
*out = it->value;
this->mru_list.erase(it);
this->mru_list.push_front(*it);
return true;
}
}
return false;
}
std::unique_ptr<Node> PopLruNode() {
AMS_ABORT_UNLESS(!this->mru_list.empty());
Node *lru = std::addressof(*this->mru_list.rbegin());
this->mru_list.pop_back();
return std::unique_ptr<Node>(lru);
}
void PushMruNode(std::unique_ptr<Node> &&node, const Key &key) {
node->key = key;
this->mru_list.push_front(*node);
node.release();
}
void DeleteAllNodes() {
while (!this->mru_list.empty()) {
Node *lru = std::addressof(*this->mru_list.rbegin());
this->mru_list.erase(this->mru_list.iterator_to(*lru));
delete lru;
}
}
size_t GetSize() const {
return this->mru_list.size();
}
bool IsEmpty() const {
return this->mru_list.empty();
}
};
}

File diff suppressed because it is too large Load Diff

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/>.
*/
#include <stratosphere.hpp>
namespace ams::fssystem {
u8 NcaHeader::GetProperKeyGeneration() const {
return std::max(this->key_generation, this->key_generation_2);
}
bool NcaPatchInfo::HasIndirectTable() const {
return this->indirect_size != 0;
}
bool NcaPatchInfo::HasAesCtrExTable() const {
return this->aes_ctr_ex_size != 0;
}
}

View File

@@ -0,0 +1,434 @@
/*
* 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/>.
*/
#include <stratosphere.hpp>
namespace ams::fssystem {
namespace {
constexpr inline u32 SdkAddonVersionMin = 0x000B0000;
constexpr Result CheckNcaMagic(u32 magic) {
/* Verify the magic is not a deprecated one. */
R_UNLESS(magic != NcaHeader::Magic0, fs::ResultUnsupportedSdkVersion());
R_UNLESS(magic != NcaHeader::Magic1, fs::ResultUnsupportedSdkVersion());
R_UNLESS(magic != NcaHeader::Magic2, fs::ResultUnsupportedSdkVersion());
/* Verify the magic is the current one. */
R_UNLESS(magic == NcaHeader::Magic3, fs::ResultInvalidNcaSignature());
return ResultSuccess();
}
}
NcaReader::NcaReader() : shared_base_storage(), header_storage(), body_storage(), decrypt_aes_ctr(), decrypt_aes_ctr_external(), is_software_aes_prioritized(false), header_encryption_type(NcaHeader::EncryptionType::Auto) {
std::memset(std::addressof(this->header), 0, sizeof(this->header));
std::memset(std::addressof(this->decryption_keys), 0, sizeof(this->decryption_keys));
std::memset(std::addressof(this->external_decryption_key), 0, sizeof(this->external_decryption_key));
}
NcaReader::~NcaReader() {
/* ... */
}
Result NcaReader::Initialize(std::shared_ptr<fs::IStorage> base_storage, const NcaCryptoConfiguration &crypto_cfg) {
this->shared_base_storage = base_storage;
return this->Initialize(this->shared_base_storage.get(), crypto_cfg);
}
Result NcaReader::Initialize(fs::IStorage *base_storage, const NcaCryptoConfiguration &crypto_cfg) {
/* Validate preconditions. */
AMS_ASSERT(base_storage != nullptr);
AMS_ASSERT(this->body_storage == nullptr);
R_UNLESS(crypto_cfg.generate_key != nullptr, fs::ResultInvalidArgument());
/* Generate keys for header. */
u8 header_decryption_keys[NcaCryptoConfiguration::HeaderEncryptionKeyCount][NcaCryptoConfiguration::Aes128KeySize];
for (size_t i = 0; i < NcaCryptoConfiguration::HeaderEncryptionKeyCount; i++) {
crypto_cfg.generate_key(header_decryption_keys[i], AesXtsStorage::KeySize, crypto_cfg.header_encrypted_encryption_keys[i], AesXtsStorage::KeySize, static_cast<s32>(KeyType::NcaHeaderKey), crypto_cfg);
}
/* Create the header storage. */
const u8 header_iv[AesXtsStorage::IvSize] = {};
std::unique_ptr<fs::IStorage> work_header_storage = std::make_unique<AesXtsStorage>(base_storage, header_decryption_keys[0], header_decryption_keys[1], AesXtsStorage::KeySize, header_iv, AesXtsStorage::IvSize, NcaHeader::XtsBlockSize);
R_UNLESS(work_header_storage != nullptr, fs::ResultAllocationFailureInNcaReaderA());
/* Read the header. */
R_TRY(work_header_storage->Read(0, std::addressof(this->header), sizeof(this->header)));
/* Validate the magic. */
if (Result magic_result = CheckNcaMagic(this->header.magic); R_FAILED(magic_result)) {
/* If we're not allowed to use plaintext headers, stop here. */
R_UNLESS(crypto_cfg.is_plaintext_header_available, magic_result);
/* Try to use a plaintext header. */
R_TRY(base_storage->Read(0, std::addressof(this->header), sizeof(this->header)));
R_UNLESS(R_SUCCEEDED(CheckNcaMagic(this->header.magic)), magic_result);
/* Configure to use the plaintext header. */
s64 base_storage_size;
R_TRY(base_storage->GetSize(std::addressof(base_storage_size)));
work_header_storage.reset(new fs::SubStorage(base_storage, 0, base_storage_size));
R_UNLESS(work_header_storage != nullptr, fs::ResultAllocationFailureInNcaReaderA());
this->header_encryption_type = NcaHeader::EncryptionType::None;
}
/* Validate the fixed key signature. */
R_UNLESS(this->header.header1_signature_key_generation <= NcaCryptoConfiguration::Header1SignatureKeyGenerationMax, fs::ResultInvalidNcaHeader1SignatureKeyGeneration());
const u8 *header_1_sign_key_modulus = crypto_cfg.header_1_sign_key_moduli[this->header.header1_signature_key_generation];
AMS_ABORT_UNLESS(header_1_sign_key_modulus != nullptr);
{
const u8 *sig = this->header.header_sign_1;
const size_t sig_size = NcaHeader::HeaderSignSize;
const u8 *mod = header_1_sign_key_modulus;
const size_t mod_size = NcaCryptoConfiguration::Rsa2048KeyModulusSize;
const u8 *exp = crypto_cfg.header_1_sign_key_public_exponent;
const size_t exp_size = NcaCryptoConfiguration::Rsa2048KeyPublicExponentSize;
const u8 *msg = static_cast<const u8 *>(static_cast<const void *>(std::addressof(this->header.magic)));
const size_t msg_size = NcaHeader::Size - NcaHeader::HeaderSignSize * NcaHeader::HeaderSignCount;
const bool is_signature_valid = crypto::VerifyRsa2048PssSha256(sig, sig_size, mod, mod_size, exp, exp_size, msg, msg_size);
R_UNLESS(is_signature_valid, fs::ResultNcaHeaderSignature1VerificationFailed());
}
/* Validate the sdk version. */
R_UNLESS(this->header.sdk_addon_version >= SdkAddonVersionMin, fs::ResultUnsupportedSdkVersion());
/* Validate the key index. */
R_UNLESS(this->header.key_index < NcaCryptoConfiguration::KeyAreaEncryptionKeyIndexCount, fs::ResultInvalidNcaKeyIndex());
/* Check if we have a rights id. */
constexpr const u8 ZeroRightsId[NcaHeader::RightsIdSize] = {};
if (crypto::IsSameBytes(ZeroRightsId, this->header.rights_id, NcaHeader::RightsIdSize)) {
/* If we do, then we don't have an external key, so we need to generate decryption keys. */
crypto_cfg.generate_key(this->decryption_keys[NcaHeader::DecryptionKey_AesCtr], crypto::AesDecryptor128::KeySize, this->header.encrypted_key_area + NcaHeader::DecryptionKey_AesCtr * crypto::AesDecryptor128::KeySize, crypto::AesDecryptor128::KeySize, GetKeyTypeValue(this->header.key_index, this->header.GetProperKeyGeneration()), crypto_cfg);
/* Copy the hardware speed emulation key. */
std::memcpy(this->decryption_keys[NcaHeader::DecryptionKey_AesCtrHw], this->header.encrypted_key_area + NcaHeader::DecryptionKey_AesCtrHw * crypto::AesDecryptor128::KeySize, crypto::AesDecryptor128::KeySize);
}
/* Clear the external decryption key. */
std::memset(this->external_decryption_key, 0, sizeof(this->external_decryption_key));
/* Set our decryptor functions. */
this->decrypt_aes_ctr = crypto_cfg.decrypt_aes_ctr;
this->decrypt_aes_ctr_external = crypto_cfg.decrypt_aes_ctr_external;
/* Set our storages. */
this->header_storage = std::move(work_header_storage);
this->body_storage = base_storage;
return ResultSuccess();
}
fs::IStorage *NcaReader::GetBodyStorage() {
return this->body_storage;
}
u32 NcaReader::GetMagic() const {
AMS_ASSERT(this->body_storage != nullptr);
return this->header.magic;
}
NcaHeader::DistributionType NcaReader::GetDistributionType() const {
AMS_ASSERT(this->body_storage != nullptr);
return this->header.distribution_type;
}
NcaHeader::ContentType NcaReader::GetContentType() const {
AMS_ASSERT(this->body_storage != nullptr);
return this->header.content_type;
}
u8 NcaReader::GetKeyGeneration() const {
AMS_ASSERT(this->body_storage != nullptr);
return this->header.GetProperKeyGeneration();
}
u8 NcaReader::GetKeyIndex() const {
AMS_ASSERT(this->body_storage != nullptr);
return this->header.key_index;
}
u64 NcaReader::GetContentSize() const {
AMS_ASSERT(this->body_storage != nullptr);
return this->header.content_size;
}
u64 NcaReader::GetProgramId() const {
AMS_ASSERT(this->body_storage != nullptr);
return this->header.program_id;
}
u32 NcaReader::GetContentIndex() const {
AMS_ASSERT(this->body_storage != nullptr);
return this->header.content_index;
}
u32 NcaReader::GetSdkAddonVersion() const {
AMS_ASSERT(this->body_storage != nullptr);
return this->header.sdk_addon_version;
}
void NcaReader::GetRightsId(u8 *dst, size_t dst_size) const {
AMS_ASSERT(dst != nullptr);
AMS_ASSERT(dst_size >= NcaHeader::RightsIdSize);
std::memcpy(dst, this->header.rights_id, NcaHeader::RightsIdSize);
}
bool NcaReader::HasFsInfo(s32 index) const {
AMS_ASSERT(0 <= index && index < NcaHeader::FsCountMax);
return this->header.fs_info[index].start_sector != 0 || this->header.fs_info[index].end_sector != 0;
}
s32 NcaReader::GetFsCount() const {
AMS_ASSERT(this->body_storage != nullptr);
for (s32 i = 0; i < NcaHeader::FsCountMax; i++) {
if (!this->HasFsInfo(i)) {
return i;
}
}
return NcaHeader::FsCountMax;
}
const Hash &NcaReader::GetFsHeaderHash(s32 index) const {
AMS_ASSERT(this->body_storage != nullptr);
AMS_ASSERT(0 <= index && index < NcaHeader::FsCountMax);
return this->header.fs_header_hash[index];
}
void NcaReader::GetFsHeaderHash(Hash *dst, s32 index) const {
AMS_ASSERT(this->body_storage != nullptr);
AMS_ASSERT(0 <= index && index < NcaHeader::FsCountMax);
AMS_ASSERT(dst != nullptr);
std::memcpy(dst, std::addressof(this->header.fs_header_hash[index]), sizeof(*dst));
}
void NcaReader::GetFsInfo(NcaHeader::FsInfo *dst, s32 index) const {
AMS_ASSERT(this->body_storage != nullptr);
AMS_ASSERT(0 <= index && index < NcaHeader::FsCountMax);
AMS_ASSERT(dst != nullptr);
std::memcpy(dst, std::addressof(this->header.fs_info[index]), sizeof(*dst));
}
u64 NcaReader::GetFsOffset(s32 index) const {
AMS_ASSERT(this->body_storage != nullptr);
AMS_ASSERT(0 <= index && index < NcaHeader::FsCountMax);
return NcaHeader::SectorToByte(this->header.fs_info[index].start_sector);
}
u64 NcaReader::GetFsEndOffset(s32 index) const {
AMS_ASSERT(this->body_storage != nullptr);
AMS_ASSERT(0 <= index && index < NcaHeader::FsCountMax);
return NcaHeader::SectorToByte(this->header.fs_info[index].end_sector);
}
u64 NcaReader::GetFsSize(s32 index) const {
AMS_ASSERT(this->body_storage != nullptr);
AMS_ASSERT(0 <= index && index < NcaHeader::FsCountMax);
return NcaHeader::SectorToByte(this->header.fs_info[index].end_sector - this->header.fs_info[index].start_sector);
}
void NcaReader::GetEncryptedKey(void *dst, size_t size) const {
AMS_ASSERT(this->body_storage != nullptr);
AMS_ASSERT(dst != nullptr);
AMS_ASSERT(size >= NcaHeader::EncryptedKeyAreaSize);
std::memcpy(dst, this->header.encrypted_key_area, NcaHeader::EncryptedKeyAreaSize);
}
const void *NcaReader::GetDecryptionKey(s32 index) const {
AMS_ASSERT(this->body_storage != nullptr);
AMS_ASSERT(0 <= index && index < NcaHeader::DecryptionKey_Count);
return this->decryption_keys[index];
}
bool NcaReader::HasValidInternalKey() const {
constexpr const u8 ZeroKey[crypto::AesDecryptor128::KeySize] = {};
for (s32 i = 0; i < NcaHeader::DecryptionKey_Count; i++) {
if (!crypto::IsSameBytes(ZeroKey, this->header.encrypted_key_area + i * crypto::AesDecryptor128::KeySize, crypto::AesDecryptor128::KeySize)) {
return true;
}
}
return false;
}
bool NcaReader::HasInternalDecryptionKeyForAesHardwareSpeedEmulation() const {
constexpr const u8 ZeroKey[crypto::AesDecryptor128::KeySize] = {};
return !crypto::IsSameBytes(ZeroKey, this->GetDecryptionKey(NcaHeader::DecryptionKey_AesCtrHw), crypto::AesDecryptor128::KeySize);
}
bool NcaReader::IsSoftwareAesPrioritized() const {
return this->is_software_aes_prioritized;
}
void NcaReader::PrioritizeSoftwareAes() {
this->is_software_aes_prioritized = true;
}
bool NcaReader::HasExternalDecryptionKey() const {
constexpr const u8 ZeroKey[crypto::AesDecryptor128::KeySize] = {};
return !crypto::IsSameBytes(ZeroKey, this->GetExternalDecryptionKey(), crypto::AesDecryptor128::KeySize);
}
const void *NcaReader::GetExternalDecryptionKey() const {
return this->external_decryption_key;
}
void NcaReader::SetExternalDecryptionKey(const void *src, size_t size) {
AMS_ASSERT(src != nullptr);
AMS_ASSERT(size == sizeof(this->external_decryption_key));
std::memcpy(this->external_decryption_key, src, sizeof(this->external_decryption_key));
}
void NcaReader::GetRawData(void *dst, size_t dst_size) const {
AMS_ASSERT(this->body_storage != nullptr);
AMS_ASSERT(dst != nullptr);
AMS_ASSERT(dst_size >= sizeof(NcaHeader));
std::memcpy(dst, std::addressof(this->header), sizeof(NcaHeader));
}
DecryptAesCtrFunction NcaReader::GetExternalDecryptAesCtrFunction() const {
AMS_ASSERT(this->decrypt_aes_ctr != nullptr);
return this->decrypt_aes_ctr;
}
DecryptAesCtrFunction NcaReader::GetExternalDecryptAesCtrFunctionForExternalKey() const {
AMS_ASSERT(this->decrypt_aes_ctr_external != nullptr);
return this->decrypt_aes_ctr_external;
}
NcaHeader::EncryptionType NcaReader::GetEncryptionType() const {
return this->header_encryption_type;
}
Result NcaReader::ReadHeader(NcaFsHeader *dst, s32 index) const {
AMS_ASSERT(dst != nullptr);
AMS_ASSERT(0 <= index && index < NcaHeader::FsCountMax);
const s64 offset = sizeof(NcaHeader) + sizeof(NcaFsHeader) * index;
return this->header_storage->Read(offset, dst, sizeof(NcaFsHeader));
}
Result NcaReader::VerifyHeaderSign2(const void *mod, size_t mod_size) {
AMS_ASSERT(this->body_storage != nullptr);
constexpr const u8 HeaderSign2KeyPublicExponent[] = { 0x01, 0x00, 0x01 };
const u8 *sig = this->header.header_sign_2;
const size_t sig_size = NcaHeader::HeaderSignSize;
const u8 *exp = HeaderSign2KeyPublicExponent;
const size_t exp_size = sizeof(HeaderSign2KeyPublicExponent);
const u8 *msg = static_cast<const u8 *>(static_cast<const void *>(std::addressof(this->header.magic)));
const size_t msg_size = NcaHeader::Size - NcaHeader::HeaderSignSize * NcaHeader::HeaderSignCount;
const bool is_signature_valid = crypto::VerifyRsa2048PssSha256(sig, sig_size, mod, mod_size, exp, exp_size, msg, msg_size);
R_UNLESS(is_signature_valid, fs::ResultNcaHeaderSignature2VerificationFailed());
return ResultSuccess();
}
Result NcaFsHeaderReader::Initialize(const NcaReader &reader, s32 index) {
/* Reset ourselves to uninitialized. */
this->fs_index = -1;
/* Read the header. */
R_TRY(reader.ReadHeader(std::addressof(this->data), index));
/* Generate the hash. */
Hash hash;
crypto::GenerateSha256Hash(std::addressof(hash), sizeof(hash), std::addressof(this->data), sizeof(NcaFsHeader));
/* Validate the hash. */
R_UNLESS(crypto::IsSameBytes(std::addressof(reader.GetFsHeaderHash(index)), std::addressof(hash), sizeof(Hash)), fs::ResultNcaFsHeaderHashVerificationFailed());
/* Set our index. */
this->fs_index = index;
return ResultSuccess();
}
void NcaFsHeaderReader::GetRawData(void *dst, size_t dst_size) const {
AMS_ASSERT(this->IsInitialized());
AMS_ASSERT(dst != nullptr);
AMS_ASSERT(dst_size >= sizeof(NcaFsHeader));
std::memcpy(dst, std::addressof(this->data), sizeof(NcaFsHeader));
}
NcaFsHeader::HashData &NcaFsHeaderReader::GetHashData() {
AMS_ASSERT(this->IsInitialized());
return this->data.hash_data;
}
const NcaFsHeader::HashData &NcaFsHeaderReader::GetHashData() const {
AMS_ASSERT(this->IsInitialized());
return this->data.hash_data;
}
u16 NcaFsHeaderReader::GetVersion() const {
AMS_ASSERT(this->IsInitialized());
return this->data.version;
}
s32 NcaFsHeaderReader::GetFsIndex() const {
AMS_ASSERT(this->IsInitialized());
return this->fs_index;
}
NcaFsHeader::FsType NcaFsHeaderReader::GetFsType() const {
AMS_ASSERT(this->IsInitialized());
return this->data.fs_type;
}
NcaFsHeader::HashType NcaFsHeaderReader::GetHashType() const {
AMS_ASSERT(this->IsInitialized());
return this->data.hash_type;
}
NcaFsHeader::EncryptionType NcaFsHeaderReader::GetEncryptionType() const {
AMS_ASSERT(this->IsInitialized());
return this->data.encryption_type;
}
NcaPatchInfo &NcaFsHeaderReader::GetPatchInfo() {
AMS_ASSERT(this->IsInitialized());
return this->data.patch_info;
}
const NcaPatchInfo &NcaFsHeaderReader::GetPatchInfo() const {
AMS_ASSERT(this->IsInitialized());
return this->data.patch_info;
}
const NcaAesCtrUpperIv NcaFsHeaderReader::GetAesCtrUpperIv() const {
AMS_ASSERT(this->IsInitialized());
return this->data.aes_ctr_upper_iv;
}
bool NcaFsHeaderReader::ExistsSparseLayer() const {
AMS_ASSERT(this->IsInitialized());
return this->data.sparse_info.generation != 0;
}
NcaSparseInfo &NcaFsHeaderReader::GetSparseInfo() {
AMS_ASSERT(this->IsInitialized());
return this->data.sparse_info;
}
const NcaSparseInfo &NcaFsHeaderReader::GetSparseInfo() const {
AMS_ASSERT(this->IsInitialized());
return this->data.sparse_info;
}
}

View File

@@ -0,0 +1,139 @@
/*
* 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.hpp>
#include "fssystem_lru_list_cache.hpp"
namespace ams::fssystem {
class ReadOnlyBlockCacheStorage : public ::ams::fs::IStorage, public ::ams::fs::impl::Newable {
NON_COPYABLE(ReadOnlyBlockCacheStorage);
NON_MOVEABLE(ReadOnlyBlockCacheStorage);
private:
using BlockCache = LruListCache<s64, char *>;
private:
os::Mutex mutex;
BlockCache block_cache;
fs::IStorage * const base_storage;
s32 block_size;
public:
ReadOnlyBlockCacheStorage(IStorage *bs, s32 bsz, char *buf, size_t buf_size, s32 cache_block_count) : mutex(false), base_storage(bs), block_size(bsz) {
/* Validate preconditions. */
AMS_ASSERT(buf_size >= static_cast<size_t>(this->block_size));
AMS_ASSERT(util::IsPowerOfTwo(this->block_size));
AMS_ASSERT(cache_block_count > 0);
AMS_ASSERT(buf_size >= static_cast<size_t>(this->block_size * cache_block_count));
/* Create a node for each cache block. */
for (auto i = 0; i < cache_block_count; i++) {
std::unique_ptr node = std::make_unique<BlockCache::Node>(buf + this->block_size * i);
AMS_ASSERT(node != nullptr);
if (node != nullptr) {
this->block_cache.PushMruNode(std::move(node), -1);
}
}
}
~ReadOnlyBlockCacheStorage() {
this->block_cache.DeleteAllNodes();
}
virtual Result Read(s64 offset, void *buffer, size_t size) override {
/* Validate preconditions. */
AMS_ASSERT(util::IsAligned(offset, this->block_size));
AMS_ASSERT(util::IsAligned(size, this->block_size));
if (size == static_cast<size_t>(this->block_size)) {
char *cached_buffer = nullptr;
/* Try to find a cached copy of the data. */
{
std::scoped_lock lk(this->mutex);
bool found = this->block_cache.FindValueAndUpdateMru(std::addressof(cached_buffer), offset / this->block_size);
if (found) {
std::memcpy(buffer, cached_buffer, size);
return ResultSuccess();
}
}
/* We failed to get a cache hit, so read in the data. */
R_TRY(this->base_storage->Read(offset, buffer, size));
/* Add the block to the cache. */
{
std::scoped_lock lk(this->mutex);
auto lru = this->block_cache.PopLruNode();
std::memcpy(lru->value, buffer, this->block_size);
this->block_cache.PushMruNode(std::move(lru), offset / this->block_size);
}
return ResultSuccess();
} else {
return this->base_storage->Read(offset, buffer, size);
}
}
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 {
/* Validate preconditions. */
AMS_ASSERT(util::IsAligned(offset, this->block_size));
AMS_ASSERT(util::IsAligned(size, this->block_size));
/* If invalidating cache, invalidate our blocks. */
if (op_id == fs::OperationId::InvalidateCache) {
R_UNLESS(offset >= 0, fs::ResultInvalidOffset());
std::scoped_lock lk(this->mutex);
const size_t cache_block_count = this->block_cache.GetSize();
BlockCache valid_cache;
for (size_t count = 0; count < cache_block_count; ++count) {
auto lru = this->block_cache.PopLruNode();
if (offset <= lru->key && lru->key < offset + size) {
this->block_cache.PushMruNode(std::move(lru), -1);
} else {
valid_cache.PushMruNode(std::move(lru), lru->key);
}
}
while (!valid_cache.IsEmpty()) {
auto lru = valid_cache.PopLruNode();
this->block_cache.PushMruNode(std::move(lru), lru->key);
}
}
/* Operate on the base storage. */
return this->base_storage->OperateRange(dst, dst_size, op_id, offset, size, src, src_size);
}
virtual Result GetSize(s64 *out) override {
return this->base_storage->GetSize(out);
}
virtual Result Flush() override {
return ResultSuccess();
}
virtual Result Write(s64 offset, const void *buffer, size_t size) override {
return fs::ResultUnsupportedOperationInReadOnlyBlockCacheStorageA();
}
virtual Result SetSize(s64 size) override {
return fs::ResultUnsupportedOperationInReadOnlyBlockCacheStorageB();
}
};
}

View File

@@ -0,0 +1,44 @@
/*
* 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/>.
*/
#include <stratosphere.hpp>
namespace ams::fssystem {
Result SparseStorage::Read(s64 offset, void *buffer, size_t size) {
/* Validate preconditions. */
AMS_ASSERT(offset >= 0);
AMS_ASSERT(this->IsInitialized());
/* Allow zero size. */
R_SUCCEED_IF(size == 0);
/* Validate arguments. */
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
if (this->GetEntryTable().IsEmpty()) {
R_UNLESS(this->GetEntryTable().Includes(offset, size), fs::ResultOutOfRange());
std::memset(buffer, 0, size);
} else {
R_TRY(this->OperatePerEntry<false>(offset, size, [=](fs::IStorage *storage, s64 data_offset, s64 cur_offset, s64 cur_size) -> Result {
R_TRY(storage->Read(data_offset, reinterpret_cast<u8 *>(buffer) + (cur_offset - offset), static_cast<size_t>(cur_size)));
return ResultSuccess();
}));
}
return ResultSuccess();
}
}

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/>.
*/
#include <stratosphere.hpp>
namespace ams::fssystem {
namespace {
std::atomic<::ams::fs::SpeedEmulationMode> g_speed_emulation_mode = ::ams::fs::SpeedEmulationMode::None;
}
void SpeedEmulationConfiguration::SetSpeedEmulationMode(::ams::fs::SpeedEmulationMode mode) {
g_speed_emulation_mode = mode;
}
::ams::fs::SpeedEmulationMode SpeedEmulationConfiguration::GetSpeedEmulationMode() {
return g_speed_emulation_mode;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,352 @@
/*
* 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/>.
*/
#include <stratosphere.hpp>
namespace ams::fssystem::save {
namespace {
constexpr inline u32 IntegrityVerificationStorageMagic = util::FourCC<'I','V','F','C'>::Code;
constexpr inline u32 IntegrityVerificationStorageVersion = 0x00020000;
constexpr inline u32 IntegrityVerificationStorageVersionMask = 0xFFFF0000;
constexpr inline auto MaxSaveDataFsDataCacheEntryCount = 32;
constexpr inline auto MaxSaveDataFsHashCacheEntryCount = 4;
constexpr inline auto MaxRomFsDataCacheEntryCount = 24;
constexpr inline auto MaxRomFsHashCacheEntryCount = 8;
constexpr inline auto AccessCountMax = 5;
constexpr inline auto AccessTimeout = TimeSpan::FromMilliSeconds(10);
os::Semaphore g_read_semaphore(AccessCountMax, AccessCountMax);
os::Semaphore g_write_semaphore(AccessCountMax, AccessCountMax);
constexpr inline const char MasterKey[] = "HierarchicalIntegrityVerificationStorage::Master";
constexpr inline const char L1Key[] = "HierarchicalIntegrityVerificationStorage::L1";
constexpr inline const char L2Key[] = "HierarchicalIntegrityVerificationStorage::L2";
constexpr inline const char L3Key[] = "HierarchicalIntegrityVerificationStorage::L3";
constexpr inline const char L4Key[] = "HierarchicalIntegrityVerificationStorage::L4";
constexpr inline const char L5Key[] = "HierarchicalIntegrityVerificationStorage::L5";
constexpr inline const struct {
const char *key;
size_t size;
} KeyArray[] = {
{ MasterKey, sizeof(MasterKey) },
{ L1Key, sizeof(L1Key) },
{ L2Key, sizeof(L2Key) },
{ L3Key, sizeof(L3Key) },
{ L4Key, sizeof(L4Key) },
{ L5Key, sizeof(L5Key) },
};
}
/* Instantiate the global random generation function. */
HierarchicalIntegrityVerificationStorage::GenerateRandomFunction HierarchicalIntegrityVerificationStorage::s_generate_random = nullptr;
Result HierarchicalIntegrityVerificationStorageControlArea::QuerySize(HierarchicalIntegrityVerificationSizeSet *out, const InputParam &input_param, s32 layer_count, s64 data_size) {
/* Validate preconditions. */
AMS_ASSERT(out != nullptr);
AMS_ASSERT((static_cast<s32>(IntegrityMinLayerCount) <= layer_count) && (layer_count <= static_cast<s32>(IntegrityMaxLayerCount)));
for (s32 level = 0; level < (layer_count - 1); ++level) {
AMS_ASSERT(input_param.level_block_size[level] > 0);
AMS_ASSERT(IsPowerOfTwo(static_cast<s32>(input_param.level_block_size[level])));
}
/* Set the control size. */
out->control_size = sizeof(HierarchicalIntegrityVerificationMetaInformation);
/* Determine the level sizes. */
s64 level_size[IntegrityMaxLayerCount];
s32 level = layer_count - 1;
level_size[level] = util::AlignUp(data_size, input_param.level_block_size[level - 1]);
--level;
for (/* ... */; level > 0; --level) {
level_size[level] = util::AlignUp(level_size[level + 1] / input_param.level_block_size[level] * HashSize, input_param.level_block_size[level - 1]);
}
/* Determine the master size. */
level_size[0] = level_size[1] / input_param.level_block_size[0] * HashSize;
/* Set the master size. */
out->master_hash_size = level_size[0];
/* Set the level sizes. */
for (level = 1; level < layer_count - 1; ++level) {
out->layered_hash_sizes[level - 1] = level_size[level];
}
return ResultSuccess();
}
Result HierarchicalIntegrityVerificationStorageControlArea::Expand(fs::SubStorage meta_storage, const HierarchicalIntegrityVerificationMetaInformation &meta) {
/* Check the meta size. */
{
s64 meta_size = 0;
R_TRY(meta_storage.GetSize(std::addressof(meta_size)));
R_UNLESS(meta_size >= static_cast<s64>(sizeof(meta)), fs::ResultInvalidSize());
}
/* Validate both the previous and new metas. */
{
/* Read the previous meta. */
HierarchicalIntegrityVerificationMetaInformation prev_meta = {};
R_TRY(meta_storage.Read(0, std::addressof(prev_meta), sizeof(prev_meta)));
/* Validate both magics. */
R_UNLESS(prev_meta.magic == IntegrityVerificationStorageMagic, fs::ResultIncorrectIntegrityVerificationMagic());
R_UNLESS(prev_meta.magic == meta.magic, fs::ResultIncorrectIntegrityVerificationMagic());
/* Validate both versions. */
R_UNLESS(prev_meta.version == IntegrityVerificationStorageVersion, fs::ResultUnsupportedVersion());
R_UNLESS(prev_meta.version == meta.version, fs::ResultUnsupportedVersion());
}
/* Write the new meta. */
R_TRY(meta_storage.Write(0, std::addressof(meta), sizeof(meta)));
R_TRY(meta_storage.Flush());
return ResultSuccess();
}
Result HierarchicalIntegrityVerificationStorageControlArea::Initialize(fs::SubStorage meta_storage) {
/* Check the meta size. */
{
s64 meta_size = 0;
R_TRY(meta_storage.GetSize(std::addressof(meta_size)));
R_UNLESS(meta_size >= static_cast<s64>(sizeof(this->meta)), fs::ResultInvalidSize());
}
/* Set the storage and read the meta. */
this->storage = meta_storage;
R_TRY(this->storage.Read(0, std::addressof(this->meta), sizeof(this->meta)));
/* Validate the meta magic. */
R_UNLESS(this->meta.magic == IntegrityVerificationStorageMagic, fs::ResultIncorrectIntegrityVerificationMagic());
/* Validate the meta version. */
R_UNLESS((this->meta.version & IntegrityVerificationStorageVersionMask) == (IntegrityVerificationStorageVersion & IntegrityVerificationStorageVersionMask), fs::ResultUnsupportedVersion());
return ResultSuccess();
}
void HierarchicalIntegrityVerificationStorageControlArea::Finalize() {
this->storage = fs::SubStorage();
}
Result HierarchicalIntegrityVerificationStorage::Initialize(const HierarchicalIntegrityVerificationInformation &info, HierarchicalStorageInformation storage, FileSystemBufferManagerSet *bufs, os::Mutex *mtx, fs::StorageType storage_type) {
/* Validate preconditions. */
AMS_ASSERT(bufs != nullptr);
AMS_ASSERT(IntegrityMinLayerCount <= info.max_layers && info.max_layers <= IntegrityMaxLayerCount);
/* Set member variables. */
this->max_layers = info.max_layers;
this->buffers = bufs;
this->mutex = mtx;
/* Determine our cache counts. */
const auto max_data_cache_entry_count = (storage_type == fs::StorageType_SaveData) ? MaxSaveDataFsDataCacheEntryCount : MaxRomFsDataCacheEntryCount;
const auto max_hash_cache_entry_count = (storage_type == fs::StorageType_SaveData) ? MaxSaveDataFsHashCacheEntryCount : MaxRomFsHashCacheEntryCount;
/* Initialize the top level verification storage. */
{
fs::HashSalt mac;
crypto::GenerateHmacSha256Mac(mac.value, sizeof(mac), info.seed.value, sizeof(info.seed), KeyArray[0].key, KeyArray[0].size);
this->verify_storages[0].Initialize(storage[HierarchicalStorageInformation::MasterStorage], storage[HierarchicalStorageInformation::Layer1Storage], static_cast<s64>(1) << info.info[0].block_order, HashSize, this->buffers->buffers[this->max_layers - 2], mac, false, storage_type);
}
/* Ensure we don't leak state if further initialization goes wrong. */
auto top_verif_guard = SCOPE_GUARD {
this->verify_storages[0].Finalize();
this->data_size = -1;
this->buffers = nullptr;
this->mutex = nullptr;
};
/* Initialize the top level buffer storage. */
R_TRY(this->buffer_storages[0].Initialize(this->buffers->buffers[0], this->mutex, std::addressof(this->verify_storages[0]), info.info[0].size, static_cast<s64>(1) << info.info[0].block_order, max_hash_cache_entry_count, false, 0x10, false, storage_type));
auto top_buffer_guard = SCOPE_GUARD { this->buffer_storages[0].Finalize(); };
/* Prepare to initialize the level storages. */
s32 level = 0;
/* Ensure we don't leak state if further initialization goes wrong. */
auto level_guard = SCOPE_GUARD {
this->verify_storages[level + 1].Finalize();
for (/* ... */; level > 0; --level) {
this->buffer_storages[level].Finalize();
this->verify_storages[level].Finalize();
}
};
/* Initialize the level storages. */
for (/* ... */; level < this->max_layers - 3; ++level) {
/* Initialize the verification storage. */
{
fs::SubStorage buffer_storage(std::addressof(this->buffer_storages[level]), 0, info.info[level].size);
fs::HashSalt mac;
crypto::GenerateHmacSha256Mac(mac.value, sizeof(mac), info.seed.value, sizeof(info.seed), KeyArray[level + 1].key, KeyArray[level + 1].size);
this->verify_storages[level + 1].Initialize(buffer_storage, storage[level + 2], static_cast<s64>(1) << info.info[level + 1].block_order, static_cast<s64>(1) << info.info[level].block_order, this->buffers->buffers[this->max_layers - 2], mac, false, storage_type);
}
/* Initialize the buffer storage. */
R_TRY(this->buffer_storages[level + 1].Initialize(this->buffers->buffers[level + 1], this->mutex, std::addressof(this->verify_storages[level + 1]), info.info[level + 1].size, static_cast<s64>(1) << info.info[level + 1].block_order, max_hash_cache_entry_count, false, 0x11 + static_cast<s8>(level), false, storage_type));
}
/* Initialize the final level storage. */
{
/* Initialize the verification storage. */
{
fs::SubStorage buffer_storage(std::addressof(this->buffer_storages[level]), 0, info.info[level].size);
fs::HashSalt mac;
crypto::GenerateHmacSha256Mac(mac.value, sizeof(mac), info.seed.value, sizeof(info.seed), KeyArray[level + 1].key, KeyArray[level + 1].size);
this->verify_storages[level + 1].Initialize(buffer_storage, storage[level + 2], static_cast<s64>(1) << info.info[level + 1].block_order, static_cast<s64>(1) << info.info[level].block_order, this->buffers->buffers[this->max_layers - 2], mac, true, storage_type);
}
/* Initialize the buffer storage. */
R_TRY(this->buffer_storages[level + 1].Initialize(this->buffers->buffers[level + 1], this->mutex, std::addressof(this->verify_storages[level + 1]), info.info[level + 1].size, static_cast<s64>(1) << info.info[level + 1].block_order, max_data_cache_entry_count, true, 0x11 + static_cast<s8>(level), true, storage_type));
}
/* Set the data size. */
this->data_size = info.info[level + 1].size;
/* We succeeded. */
level_guard.Cancel();
top_buffer_guard.Cancel();
top_verif_guard.Cancel();
return ResultSuccess();
}
void HierarchicalIntegrityVerificationStorage::Finalize() {
if (this->data_size >= 0) {
this->data_size = 0;
this->buffers = nullptr;
this->mutex = nullptr;
for (s32 level = this->max_layers - 2; level >= 0; --level) {
this->buffer_storages[level].Finalize();
this->verify_storages[level].Finalize();
}
this->data_size = -1;
}
}
Result HierarchicalIntegrityVerificationStorage::Read(s64 offset, void *buffer, size_t size) {
/* Validate preconditions. */
AMS_ASSERT(this->data_size >= 0);
/* Succeed if zero-size. */
R_SUCCEED_IF(size == 0);
/* Validate arguments. */
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
/* Acquire access to the read semaphore. */
if (!g_read_semaphore.TimedAcquire(AccessTimeout)) {
for (auto level = this->max_layers - 2; level >= 0; --level) {
R_TRY(this->buffer_storages[level].Flush());
}
g_read_semaphore.Acquire();
}
/* Ensure that we release the semaphore when done. */
ON_SCOPE_EXIT { g_read_semaphore.Release(); };
/* Read the data. */
R_TRY(this->buffer_storages[this->max_layers - 2].Read(offset, buffer, size));
return ResultSuccess();
}
Result HierarchicalIntegrityVerificationStorage::Write(s64 offset, const void *buffer, size_t size) {
/* Validate preconditions. */
AMS_ASSERT(this->data_size >= 0);
/* Succeed if zero-size. */
R_SUCCEED_IF(size == 0);
/* Validate arguments. */
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
/* Acquire access to the write semaphore. */
if (!g_write_semaphore.TimedAcquire(AccessTimeout)) {
for (auto level = this->max_layers - 2; level >= 0; --level) {
R_TRY(this->buffer_storages[level].Flush());
}
g_write_semaphore.Acquire();
}
/* Ensure that we release the semaphore when done. */
ON_SCOPE_EXIT { g_write_semaphore.Release(); };
/* Write the data. */
R_TRY(this->buffer_storages[this->max_layers - 2].Write(offset, buffer, size));
this->is_written_for_rollback = true;
return ResultSuccess();
}
Result HierarchicalIntegrityVerificationStorage::GetSize(s64 *out) {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(this->data_size >= 0);
*out = this->data_size;
return ResultSuccess();
}
Result HierarchicalIntegrityVerificationStorage::Flush() {
return ResultSuccess();
}
Result HierarchicalIntegrityVerificationStorage::OperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) {
switch (op_id) {
case fs::OperationId::Clear:
case fs::OperationId::ClearSignature:
{
R_TRY(this->buffer_storages[this->max_layers - 2].OperateRange(dst, dst_size, op_id, offset, size, src, src_size));
this->is_written_for_rollback = true;
return ResultSuccess();
}
case fs::OperationId::InvalidateCache:
case fs::OperationId::QueryRange:
{
R_TRY(this->buffer_storages[this->max_layers - 2].OperateRange(dst, dst_size, op_id, offset, size, src, src_size));
return ResultSuccess();
}
default:
return fs::ResultUnsupportedOperationInHierarchicalIntegrityVerificationStorageB();
}
}
Result HierarchicalIntegrityVerificationStorage::Commit() {
for (s32 level = this->max_layers - 2; level >= 0; --level) {
R_TRY(this->buffer_storages[level].Commit());
}
return ResultSuccess();
}
Result HierarchicalIntegrityVerificationStorage::OnRollback() {
for (s32 level = this->max_layers - 2; level >= 0; --level) {
R_TRY(this->buffer_storages[level].OnRollback());
}
this->is_written_for_rollback = false;
return ResultSuccess();
}
}

View File

@@ -0,0 +1,484 @@
/*
* 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/>.
*/
#include <stratosphere.hpp>
namespace ams::fssystem::save {
Result IntegrityVerificationStorage::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) {
/* Validate preconditions. */
AMS_ASSERT(verif_block_size >= HashSize);
AMS_ASSERT(bm != nullptr);
/* Set storages. */
this->hash_storage = hs;
this->data_storage = ds;
/* Set verification block sizes. */
this->verification_block_size = verif_block_size;
this->verification_block_order = ILog2(static_cast<u32>(verif_block_size));
AMS_ASSERT(this->verification_block_size == (1l << this->verification_block_order));
/* Set buffer manager. */
this->buffer_manager = bm;
/* Set upper layer block sizes. */
upper_layer_verif_block_size = std::max(upper_layer_verif_block_size, HashSize);
this->upper_layer_verification_block_size = upper_layer_verif_block_size;
this->upper_layer_verification_block_order = ILog2(static_cast<u32>(upper_layer_verif_block_size));
AMS_ASSERT(this->upper_layer_verification_block_size == (1l << this->upper_layer_verification_block_order));
/* Validate sizes. */
{
s64 hash_size = 0;
s64 data_size = 0;
AMS_ASSERT(R_SUCCEEDED(hash_storage.GetSize(std::addressof(hash_size))));
AMS_ASSERT(R_SUCCEEDED(data_storage.GetSize(std::addressof(hash_size))));
AMS_ASSERT(((hash_size / HashSize) * this->verification_block_size) >= data_size);
}
/* Set salt. */
std::memcpy(this->salt.value, salt.value, fs::HashSalt::Size);
/* Set data and storage type. */
this->is_real_data = is_real_data;
this->storage_type = storage_type;
return ResultSuccess();
}
void IntegrityVerificationStorage::Finalize() {
if (this->buffer_manager != nullptr) {
this->hash_storage = fs::SubStorage();
this->data_storage = fs::SubStorage();
this->buffer_manager = nullptr;
}
}
Result IntegrityVerificationStorage::Read(s64 offset, void *buffer, size_t size) {
/* Although we support zero-size reads, we expect non-zero sizes. */
AMS_ASSERT(size != 0);
/* Validate other preconditions. */
AMS_ASSERT(util::IsAligned(offset, static_cast<size_t>(this->verification_block_size)));
AMS_ASSERT(util::IsAligned(size, static_cast<size_t>(this->verification_block_size)));
/* Succeed if zero size. */
R_SUCCEED_IF(size == 0);
/* Validate arguments. */
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
/* Validate the offset. */
s64 data_size;
R_TRY(this->data_storage.GetSize(std::addressof(data_size)));
R_UNLESS(offset <= data_size, fs::ResultInvalidOffset());
/* Validate the access range. */
R_UNLESS(IStorage::IsRangeValid(offset, size, util::AlignUp(data_size, static_cast<size_t>(this->verification_block_size))), fs::ResultOutOfRange());
/* Determine the read extents. */
size_t read_size = size;
if (static_cast<s64>(offset + read_size) > data_size) {
/* Determine the padding sizes. */
s64 padding_offset = data_size - offset;
size_t padding_size = static_cast<size_t>(this->verification_block_size - (padding_offset & (this->verification_block_size - 1)));
AMS_ASSERT(static_cast<s64>(padding_size) < this->verification_block_size);
/* Clear the padding. */
std::memset(static_cast<u8 *>(buffer) + padding_offset, 0, padding_size);
/* Set the new in-bounds size. */
read_size = static_cast<size_t>(data_size - offset);
}
/* Perform the read. */
{
auto clear_guard = SCOPE_GUARD { std::memset(buffer, 0, size); };
R_TRY(this->data_storage.Read(offset, buffer, read_size));
clear_guard.Cancel();
}
/* Prepare to validate the signatures. */
const auto signature_count = size >> this->verification_block_order;
PooledBuffer signature_buffer(signature_count * sizeof(BlockHash), sizeof(BlockHash));
const auto buffer_count = std::min(signature_count, signature_buffer.GetSize() / sizeof(BlockHash));
/* Verify the signatures. */
Result verify_hash_result = ResultSuccess();
size_t verified_count = 0;
while (verified_count < signature_count) {
/* Read the current signatures. */
const auto cur_count = std::min(buffer_count, signature_count - verified_count);
auto cur_result = this->ReadBlockSignature(signature_buffer.GetBuffer(), signature_buffer.GetSize(), offset + (verified_count << this->verification_block_order), cur_count << this->verification_block_order);
/* Temporarily increase our priority. */
ScopedThreadPriorityChanger cp(+1, ScopedThreadPriorityChanger::Mode::Relative);
/* Loop over each signature we read. */
for (size_t i = 0; i < cur_count && R_SUCCEEDED(cur_result); ++i) {
const auto verified_size = (verified_count + i) << this->verification_block_order;
u8 *cur_buf = static_cast<u8 *>(buffer) + verified_size;
cur_result = this->VerifyHash(cur_buf, reinterpret_cast<BlockHash *>(signature_buffer.GetBuffer()) + i);
/* If the data is corrupted, clear the corrupted parts. */
if (fs::ResultIntegrityVerificationStorageCorrupted::Includes(cur_result)) {
std::memset(cur_buf, 0, this->verification_block_size);
/* Set the result if we should. */
if (!fs::ResultClearedRealDataVerificationFailed::Includes(cur_result) && this->storage_type != fs::StorageType_Authoring) {
verify_hash_result = cur_result;
}
cur_result = ResultSuccess();
}
}
/* If we failed, clear and return. */
if (R_FAILED(cur_result)) {
std::memset(buffer, 0, size);
return cur_result;
}
/* Advance. */
verified_count += cur_count;
}
return verify_hash_result;
}
Result IntegrityVerificationStorage::Write(s64 offset, const void *buffer, size_t size) {
/* Succeed if zero size. */
R_SUCCEED_IF(size == 0);
/* Validate arguments. */
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
R_UNLESS(IStorage::IsOffsetAndSizeValid(offset, size), fs::ResultInvalidOffset());
/* Validate the offset. */
s64 data_size;
R_TRY(this->data_storage.GetSize(std::addressof(data_size)));
R_UNLESS(offset < data_size, fs::ResultInvalidOffset());
/* Validate the access range. */
R_UNLESS(IStorage::IsRangeValid(offset, size, util::AlignUp(data_size, static_cast<size_t>(this->verification_block_size))), fs::ResultOutOfRange());
/* Validate preconditions. */
AMS_ASSERT(util::IsAligned(offset, this->verification_block_size));
AMS_ASSERT(util::IsAligned(size, this->verification_block_size));
AMS_ASSERT(offset <= data_size);
AMS_ASSERT(static_cast<s64>(offset + size) < data_size + this->verification_block_size);
/* Validate that if writing past the end, all extra data is zero padding. */
if (static_cast<s64>(offset + size) > data_size) {
const u8 *padding_cur = static_cast<const u8 *>(buffer) + data_size - offset;
const u8 *padding_end = padding_cur + (offset + size - data_size);
while (padding_cur < padding_end) {
AMS_ASSERT((*padding_cur) == 0);
++padding_cur;
}
}
/* Determine the unpadded size to write. */
auto write_size = size;
if (static_cast<s64>(offset + write_size) > data_size) {
write_size = static_cast<size_t>(data_size - offset);
R_SUCCEED_IF(write_size == 0);
}
/* Determine the size we're writing in blocks. */
const auto aligned_write_size = util::AlignUp(write_size, this->verification_block_size);
/* Write the updated block signatures. */
Result update_result = ResultSuccess();
size_t updated_count = 0;
{
const auto signature_count = aligned_write_size >> this->verification_block_order;
PooledBuffer signature_buffer(signature_count * sizeof(BlockHash), sizeof(BlockHash));
const auto buffer_count = std::min(signature_count, signature_buffer.GetSize() / sizeof(BlockHash));
while (updated_count < signature_count) {
const auto cur_count = std::min(buffer_count, signature_count - updated_count);
/* Calculate the hash with temporarily increased priority. */
{
ScopedThreadPriorityChanger cp(+1, ScopedThreadPriorityChanger::Mode::Relative);
for (size_t i = 0; i < cur_count; ++i) {
const auto updated_size = (updated_count + i) << this->verification_block_order;
this->CalcBlockHash(reinterpret_cast<BlockHash *>(signature_buffer.GetBuffer()) + i, reinterpret_cast<const u8 *>(buffer) + updated_size);
}
}
/* Write the new block signatures. */
if (R_FAILED((update_result = this->WriteBlockSignature(signature_buffer.GetBuffer(), signature_buffer.GetSize(), offset + (updated_count << this->verification_block_order), cur_count << this->verification_block_order)))) {
break;
}
/* Advance. */
updated_count += cur_count;
}
}
/* Write the data. */
R_TRY(this->data_storage.Write(offset, buffer, std::min(write_size, updated_count << this->verification_block_order)));
return update_result;
}
Result IntegrityVerificationStorage::GetSize(s64 *out) {
return this->data_storage.GetSize(out);
}
Result IntegrityVerificationStorage::Flush() {
/* Flush both storages. */
R_TRY(this->hash_storage.Flush());
R_TRY(this->data_storage.Flush());
return ResultSuccess();
}
Result IntegrityVerificationStorage::OperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) {
/* Validate preconditions. */
AMS_ASSERT(util::IsAligned(offset, static_cast<size_t>(this->verification_block_size)));
AMS_ASSERT(util::IsAligned(size, static_cast<size_t>(this->verification_block_size)));
switch (op_id) {
case fs::OperationId::Clear:
{
/* Clear should only be called for save data. */
AMS_ASSERT(this->storage_type == fs::StorageType_SaveData);
/* Validate the range. */
s64 data_size = 0;
R_TRY(this->data_storage.GetSize(std::addressof(data_size)));
R_UNLESS(0 <= offset && offset <= data_size, fs::ResultInvalidOffset());
/* Determine the extents to clear. */
const auto sign_offset = (offset >> this->verification_block_order) * HashSize;
const auto sign_size = (std::min(size, data_size - offset) >> this->verification_block_order) * HashSize;
/* Allocate a work buffer. */
const auto buf_size = static_cast<size_t>(std::min(sign_size, static_cast<s64>(1) << (this->upper_layer_verification_block_order + 2)));
std::unique_ptr<char[], fs::impl::Deleter> buf = fs::impl::MakeUnique<char[]>(buf_size);
R_UNLESS(buf != nullptr, fs::ResultAllocationFailureInIntegrityVerificationStorageA());
/* Clear the work buffer. */
std::memset(buf.get(), 0, buf_size);
/* Clear in chunks. */
auto remaining_size = sign_size;
while (remaining_size > 0) {
const auto cur_size = static_cast<size_t>(std::min(remaining_size, static_cast<s64>(buf_size)));
R_TRY(this->hash_storage.Write(sign_offset + sign_size - remaining_size, buf.get(), cur_size));
remaining_size -= cur_size;
}
return ResultSuccess();
}
case fs::OperationId::ClearSignature:
{
/* Clear Signature should only be called for save data. */
AMS_ASSERT(this->storage_type == fs::StorageType_SaveData);
/* Validate the range. */
s64 data_size = 0;
R_TRY(this->data_storage.GetSize(std::addressof(data_size)));
R_UNLESS(0 <= offset && offset <= data_size, fs::ResultInvalidOffset());
/* Determine the extents to clear the signature for. */
const auto sign_offset = (offset >> this->verification_block_order) * HashSize;
const auto sign_size = (std::min(size, data_size - offset) >> this->verification_block_order) * HashSize;
/* Allocate a work buffer. */
std::unique_ptr<char[], fs::impl::Deleter> buf = fs::impl::MakeUnique<char[]>(sign_size);
R_UNLESS(buf != nullptr, fs::ResultAllocationFailureInIntegrityVerificationStorageB());
/* Read the existing signature. */
R_TRY(this->hash_storage.Read(sign_offset, buf.get(), sign_size));
/* Clear the signature. */
/* This sets all bytes to FF, with the verification bit cleared. */
for (auto i = 0; i < sign_size; ++i) {
buf[i] ^= ((i + 1) % HashSize == 0 ? 0x7F : 0xFF);
}
/* Write the cleared signature. */
return this->hash_storage.Write(sign_offset, buf.get(), sign_size);
}
case fs::OperationId::InvalidateCache:
{
/* Only allow cache invalidation for RomFs. */
R_UNLESS(this->storage_type != fs::StorageType_SaveData, fs::ResultUnsupportedOperationInIntegrityVerificationStorageB());
/* Validate the range. */
s64 data_size = 0;
R_TRY(this->data_storage.GetSize(std::addressof(data_size)));
R_UNLESS(0 <= offset && offset <= data_size, fs::ResultInvalidOffset());
/* Determine the extents to invalidate. */
const auto sign_offset = (offset >> this->verification_block_order) * HashSize;
const auto sign_size = (std::min(size, data_size - offset) >> this->verification_block_order) * HashSize;
/* Operate on our storages. */
R_TRY(this->hash_storage.OperateRange(dst, dst_size, op_id, sign_offset, sign_size, src, src_size));
R_TRY(this->data_storage.OperateRange(dst, dst_size, op_id, sign_offset, sign_size, src, src_size));
return ResultSuccess();
}
case fs::OperationId::QueryRange:
{
/* Validate the range. */
s64 data_size = 0;
R_TRY(this->data_storage.GetSize(std::addressof(data_size)));
R_UNLESS(0 <= offset && offset <= data_size, fs::ResultInvalidOffset());
/* Determine the real size to query. */
const auto actual_size = std::min(size, data_size - offset);
/* Query the data storage. */
R_TRY(this->data_storage.OperateRange(dst, dst_size, op_id, offset, actual_size, src, src_size));
return ResultSuccess();
}
default:
return fs::ResultUnsupportedOperationInIntegrityVerificationStorageC();
}
}
void IntegrityVerificationStorage::CalcBlockHash(BlockHash *out, const void *buffer, size_t block_size) const {
/* Create a sha256 generator. */
crypto::Sha256Generator sha;
sha.Initialize();
/* If calculating for save data, hash the salt. */
if (this->storage_type == fs::StorageType_SaveData) {
sha.Update(this->salt.value, sizeof(this->salt));
}
/* Update with the buffer and get the hash. */
sha.Update(buffer, block_size);
sha.GetHash(out, sizeof(*out));
/* Set the validation bit, if the hash is for save data. */
if (this->storage_type == fs::StorageType_SaveData) {
SetValidationBit(out);
}
}
Result IntegrityVerificationStorage::ReadBlockSignature(void *dst, size_t dst_size, s64 offset, size_t size) {
/* Validate preconditions. */
AMS_ASSERT(dst != nullptr);
AMS_ASSERT(util::IsAligned(offset, static_cast<size_t>(this->verification_block_size)));
AMS_ASSERT(util::IsAligned(size, static_cast<size_t>(this->verification_block_size)));
/* Determine where to read the signature. */
const s64 sign_offset = (offset >> this->verification_block_order) * HashSize;
const auto sign_size = static_cast<size_t>((size >> this->verification_block_order) * HashSize);
AMS_ASSERT(dst_size >= sign_size);
/* Create a guard in the event of failure. */
auto clear_guard = SCOPE_GUARD { std::memset(dst, 0, sign_size); };
/* Validate that we can read the signature. */
s64 hash_size;
R_TRY(this->hash_storage.GetSize(std::addressof(hash_size)));
const bool range_valid = static_cast<s64>(sign_offset + sign_size) <= hash_size;
AMS_ASSERT(range_valid);
R_UNLESS(range_valid, fs::ResultOutOfRange());
/* Read the signature. */
R_TRY(this->hash_storage.Read(sign_offset, dst, sign_size));
/* We succeeded. */
clear_guard.Cancel();
return ResultSuccess();
}
Result IntegrityVerificationStorage::WriteBlockSignature(const void *src, size_t src_size, s64 offset, size_t size) {
/* Validate preconditions. */
AMS_ASSERT(src != nullptr);
AMS_ASSERT(util::IsAligned(offset, static_cast<size_t>(this->verification_block_size)));
/* Determine where to write the signature. */
const s64 sign_offset = (offset >> this->verification_block_order) * HashSize;
const auto sign_size = static_cast<size_t>((size >> this->verification_block_order) * HashSize);
AMS_ASSERT(src_size >= sign_size);
/* Write the signature. */
R_TRY(this->hash_storage.Write(sign_offset, src, sign_size));
/* We succeeded. */
return ResultSuccess();
}
Result IntegrityVerificationStorage::VerifyHash(const void *buf, BlockHash *hash) {
/* Validate preconditions. */
AMS_ASSERT(buf != nullptr);
AMS_ASSERT(hash != nullptr);
/* Get the comparison hash. */
auto &cmp_hash = *hash;
/* If save data, check if the data is uninitialized. */
if (this->storage_type == fs::StorageType_SaveData) {
bool is_cleared = false;
R_TRY(this->IsCleared(std::addressof(is_cleared), cmp_hash));
R_UNLESS(!is_cleared, fs::ResultClearedRealDataVerificationFailed());
}
/* Get the calculated hash. */
BlockHash calc_hash;
this->CalcBlockHash(std::addressof(calc_hash), buf);
/* Check that the signatures are equal. */
if (!crypto::IsSameBytes(std::addressof(cmp_hash), std::addressof(calc_hash), sizeof(BlockHash))) {
/* Clear the comparison hash. */
std::memset(std::addressof(cmp_hash), 0, sizeof(cmp_hash));
/* Return the appropriate result. */
if (this->is_real_data) {
return fs::ResultUnclearedRealDataVerificationFailed();
} else {
return fs::ResultNonRealDataVerificationFailed();
}
}
return ResultSuccess();
}
Result IntegrityVerificationStorage::IsCleared(bool *is_cleared, const BlockHash &hash) {
/* Validate preconditions. */
AMS_ASSERT(is_cleared != nullptr);
AMS_ASSERT(this->storage_type == fs::StorageType_SaveData);
/* Default to uncleared. */
*is_cleared = false;
/* Succeed if the validation bit is set. */
R_SUCCEED_IF(IsValidationBit(std::addressof(hash)));
/* Otherwise, we expect the hash to be all zero. */
for (size_t i = 0; i < sizeof(hash.hash); ++i) {
R_UNLESS(hash.hash[i] == 0, fs::ResultInvalidZeroHash());
}
/* Set cleared. */
*is_cleared = true;
return ResultSuccess();
}
}