2018-08-26 01:03:03 +02:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-09-06 19:56:13 +02:00
|
|
|
#include <algorithm>
|
2020-08-03 20:14:39 +02:00
|
|
|
#include <array>
|
2018-09-06 19:56:13 +02:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
|
|
|
|
2018-08-26 01:03:03 +02:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "core/crypto/aes_util.h"
|
|
|
|
#include "core/file_sys/nca_patch.h"
|
|
|
|
|
|
|
|
namespace FileSys {
|
2020-09-15 15:06:43 +02:00
|
|
|
namespace {
|
|
|
|
template <bool Subsection, typename BlockType, typename BucketType>
|
2020-09-15 15:10:55 +02:00
|
|
|
std::pair<std::size_t, std::size_t> SearchBucketEntry(u64 offset, const BlockType& block,
|
|
|
|
const BucketType& buckets) {
|
2020-09-15 15:06:43 +02:00
|
|
|
if constexpr (Subsection) {
|
2020-09-15 15:10:55 +02:00
|
|
|
const auto& last_bucket = buckets[block.number_buckets - 1];
|
2020-09-15 15:06:43 +02:00
|
|
|
if (offset >= last_bucket.entries[last_bucket.number_entries].address_patch) {
|
|
|
|
return {block.number_buckets - 1, last_bucket.number_entries};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ASSERT_MSG(offset <= block.size, "Offset is out of bounds in BKTR relocation block.");
|
|
|
|
}
|
|
|
|
|
2020-10-21 04:07:39 +02:00
|
|
|
std::size_t bucket_id = std::count_if(
|
2020-09-15 15:06:43 +02:00
|
|
|
block.base_offsets.begin() + 1, block.base_offsets.begin() + block.number_buckets,
|
2020-10-21 04:07:39 +02:00
|
|
|
[&offset](u64 base_offset) { return base_offset <= offset; });
|
2020-09-15 15:06:43 +02:00
|
|
|
|
2020-09-15 15:10:55 +02:00
|
|
|
const auto& bucket = buckets[bucket_id];
|
2020-09-15 15:06:43 +02:00
|
|
|
|
|
|
|
if (bucket.number_entries == 1) {
|
|
|
|
return {bucket_id, 0};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t low = 0;
|
|
|
|
std::size_t mid = 0;
|
|
|
|
std::size_t high = bucket.number_entries - 1;
|
|
|
|
while (low <= high) {
|
|
|
|
mid = (low + high) / 2;
|
|
|
|
if (bucket.entries[mid].address_patch > offset) {
|
|
|
|
high = mid - 1;
|
|
|
|
} else {
|
|
|
|
if (mid == bucket.number_entries - 1 ||
|
|
|
|
bucket.entries[mid + 1].address_patch > offset) {
|
|
|
|
return {bucket_id, mid};
|
|
|
|
}
|
|
|
|
|
|
|
|
low = mid + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UNREACHABLE_MSG("Offset could not be found in BKTR block.");
|
|
|
|
}
|
|
|
|
} // Anonymous namespace
|
2018-08-26 01:03:03 +02:00
|
|
|
|
|
|
|
BKTR::BKTR(VirtualFile base_romfs_, VirtualFile bktr_romfs_, RelocationBlock relocation_,
|
|
|
|
std::vector<RelocationBucket> relocation_buckets_, SubsectionBlock subsection_,
|
|
|
|
std::vector<SubsectionBucket> subsection_buckets_, bool is_encrypted_,
|
|
|
|
Core::Crypto::Key128 key_, u64 base_offset_, u64 ivfc_offset_,
|
|
|
|
std::array<u8, 8> section_ctr_)
|
2018-09-06 20:00:09 +02:00
|
|
|
: relocation(relocation_), relocation_buckets(std::move(relocation_buckets_)),
|
2018-08-26 01:03:03 +02:00
|
|
|
subsection(subsection_), subsection_buckets(std::move(subsection_buckets_)),
|
2018-09-06 20:00:09 +02:00
|
|
|
base_romfs(std::move(base_romfs_)), bktr_romfs(std::move(bktr_romfs_)),
|
2018-08-26 01:03:03 +02:00
|
|
|
encrypted(is_encrypted_), key(key_), base_offset(base_offset_), ivfc_offset(ivfc_offset_),
|
2018-08-26 16:53:31 +02:00
|
|
|
section_ctr(section_ctr_) {
|
2018-09-15 15:21:06 +02:00
|
|
|
for (std::size_t i = 0; i < relocation.number_buckets - 1; ++i) {
|
2018-08-26 01:03:03 +02:00
|
|
|
relocation_buckets[i].entries.push_back({relocation.base_offsets[i + 1], 0, 0});
|
|
|
|
}
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
for (std::size_t i = 0; i < subsection.number_buckets - 1; ++i) {
|
2018-08-26 01:03:03 +02:00
|
|
|
subsection_buckets[i].entries.push_back({subsection_buckets[i + 1].entries[0].address_patch,
|
|
|
|
{0},
|
|
|
|
subsection_buckets[i + 1].entries[0].ctr});
|
|
|
|
}
|
|
|
|
|
|
|
|
relocation_buckets.back().entries.push_back({relocation.size, 0, 0});
|
|
|
|
}
|
|
|
|
|
2018-08-26 16:53:31 +02:00
|
|
|
BKTR::~BKTR() = default;
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
std::size_t BKTR::Read(u8* data, std::size_t length, std::size_t offset) const {
|
2018-08-26 01:03:03 +02:00
|
|
|
// Read out of bounds.
|
|
|
|
if (offset >= relocation.size)
|
|
|
|
return 0;
|
|
|
|
const auto relocation = GetRelocationEntry(offset);
|
|
|
|
const auto section_offset = offset - relocation.address_patch + relocation.address_source;
|
|
|
|
const auto bktr_read = relocation.from_patch;
|
|
|
|
|
|
|
|
const auto next_relocation = GetNextRelocationEntry(offset);
|
|
|
|
|
2018-08-29 04:38:35 +02:00
|
|
|
if (offset + length > next_relocation.address_patch) {
|
2018-08-26 01:03:03 +02:00
|
|
|
const u64 partition = next_relocation.address_patch - offset;
|
|
|
|
return Read(data, partition, offset) +
|
|
|
|
Read(data + partition, length - partition, offset + partition);
|
|
|
|
}
|
2018-08-26 16:53:31 +02:00
|
|
|
|
|
|
|
if (!bktr_read) {
|
2018-09-04 23:01:40 +02:00
|
|
|
ASSERT_MSG(section_offset >= ivfc_offset, "Offset calculation negative.");
|
2018-08-29 04:37:42 +02:00
|
|
|
return base_romfs->Read(data, length, section_offset - ivfc_offset);
|
2018-08-26 16:53:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!encrypted) {
|
|
|
|
return bktr_romfs->Read(data, length, section_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto subsection = GetSubsectionEntry(section_offset);
|
|
|
|
Core::Crypto::AESCipher<Core::Crypto::Key128> cipher(key, Core::Crypto::Mode::CTR);
|
|
|
|
|
|
|
|
// Calculate AES IV
|
2020-08-03 20:14:39 +02:00
|
|
|
std::array<u8, 16> iv{};
|
2018-08-26 16:53:31 +02:00
|
|
|
auto subsection_ctr = subsection.ctr;
|
|
|
|
auto offset_iv = section_offset + base_offset;
|
2018-09-15 15:21:06 +02:00
|
|
|
for (std::size_t i = 0; i < section_ctr.size(); ++i)
|
2018-08-26 16:53:31 +02:00
|
|
|
iv[i] = section_ctr[0x8 - i - 1];
|
|
|
|
offset_iv >>= 4;
|
2018-09-15 15:21:06 +02:00
|
|
|
for (std::size_t i = 0; i < sizeof(u64); ++i) {
|
2018-08-26 16:53:31 +02:00
|
|
|
iv[0xF - i] = static_cast<u8>(offset_iv & 0xFF);
|
|
|
|
offset_iv >>= 8;
|
|
|
|
}
|
2018-09-15 15:21:06 +02:00
|
|
|
for (std::size_t i = 0; i < sizeof(u32); ++i) {
|
2018-08-26 16:53:31 +02:00
|
|
|
iv[0x7 - i] = static_cast<u8>(subsection_ctr & 0xFF);
|
|
|
|
subsection_ctr >>= 8;
|
|
|
|
}
|
|
|
|
cipher.SetIV(iv);
|
|
|
|
|
|
|
|
const auto next_subsection = GetNextSubsectionEntry(section_offset);
|
|
|
|
|
|
|
|
if (section_offset + length > next_subsection.address_patch) {
|
|
|
|
const u64 partition = next_subsection.address_patch - section_offset;
|
|
|
|
return Read(data, partition, offset) +
|
|
|
|
Read(data + partition, length - partition, offset + partition);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto block_offset = section_offset & 0xF;
|
|
|
|
if (block_offset != 0) {
|
2020-10-21 04:07:39 +02:00
|
|
|
auto block = bktr_romfs->ReadBytes(0x10, section_offset & ~0xF);
|
2018-08-26 16:53:31 +02:00
|
|
|
cipher.Transcode(block.data(), block.size(), block.data(), Core::Crypto::Op::Decrypt);
|
|
|
|
if (length + block_offset < 0x10) {
|
|
|
|
std::memcpy(data, block.data() + block_offset, std::min(length, block.size()));
|
|
|
|
return std::min(length, block.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto read = 0x10 - block_offset;
|
|
|
|
std::memcpy(data, block.data() + block_offset, read);
|
|
|
|
return read + Read(data + read, length - read, offset + read);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto raw_read = bktr_romfs->Read(data, length, section_offset);
|
|
|
|
cipher.Transcode(data, raw_read, data, Core::Crypto::Op::Decrypt);
|
|
|
|
return raw_read;
|
2018-08-26 01:03:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
RelocationEntry BKTR::GetRelocationEntry(u64 offset) const {
|
|
|
|
const auto res = SearchBucketEntry<false>(offset, relocation, relocation_buckets);
|
|
|
|
return relocation_buckets[res.first].entries[res.second];
|
|
|
|
}
|
|
|
|
|
|
|
|
RelocationEntry BKTR::GetNextRelocationEntry(u64 offset) const {
|
|
|
|
const auto res = SearchBucketEntry<false>(offset, relocation, relocation_buckets);
|
|
|
|
const auto bucket = relocation_buckets[res.first];
|
|
|
|
if (res.second + 1 < bucket.entries.size())
|
|
|
|
return bucket.entries[res.second + 1];
|
|
|
|
return relocation_buckets[res.first + 1].entries[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
SubsectionEntry BKTR::GetSubsectionEntry(u64 offset) const {
|
|
|
|
const auto res = SearchBucketEntry<true>(offset, subsection, subsection_buckets);
|
|
|
|
return subsection_buckets[res.first].entries[res.second];
|
|
|
|
}
|
|
|
|
|
|
|
|
SubsectionEntry BKTR::GetNextSubsectionEntry(u64 offset) const {
|
|
|
|
const auto res = SearchBucketEntry<true>(offset, subsection, subsection_buckets);
|
|
|
|
const auto bucket = subsection_buckets[res.first];
|
|
|
|
if (res.second + 1 < bucket.entries.size())
|
|
|
|
return bucket.entries[res.second + 1];
|
|
|
|
return subsection_buckets[res.first + 1].entries[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string BKTR::GetName() const {
|
|
|
|
return base_romfs->GetName();
|
|
|
|
}
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
std::size_t BKTR::GetSize() const {
|
2018-08-26 01:03:03 +02:00
|
|
|
return relocation.size;
|
|
|
|
}
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
bool BKTR::Resize(std::size_t new_size) {
|
2018-08-26 01:03:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<VfsDirectory> BKTR::GetContainingDirectory() const {
|
|
|
|
return base_romfs->GetContainingDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BKTR::IsWritable() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BKTR::IsReadable() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
std::size_t BKTR::Write(const u8* data, std::size_t length, std::size_t offset) {
|
2018-08-26 01:03:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BKTR::Rename(std::string_view name) {
|
|
|
|
return base_romfs->Rename(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace FileSys
|