summaryrefslogtreecommitdiffstats
path: root/src/core/file_sys/nca_patch.cpp
blob: dd684c38e1d67a83c9af53493baee4e3d40e27d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2018 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include "common/assert.h"
#include "core/crypto/aes_util.h"
#include "core/file_sys/nca_patch.h"

namespace FileSys {

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_)
    : base_romfs(std::move(base_romfs_)), bktr_romfs(std::move(bktr_romfs_)),
      relocation(relocation_), relocation_buckets(std::move(relocation_buckets_)),
      subsection(subsection_), subsection_buckets(std::move(subsection_buckets_)),
      encrypted(is_encrypted_), key(key_), base_offset(base_offset_), ivfc_offset(ivfc_offset_),
      section_ctr(std::move(section_ctr_)) {
    for (size_t i = 0; i < relocation.number_buckets - 1; ++i) {
        relocation_buckets[i].entries.push_back({relocation.base_offsets[i + 1], 0, 0});
    }

    for (size_t i = 0; i < subsection.number_buckets - 1; ++i) {
        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});
}

size_t BKTR::Read(u8* data, size_t length, size_t offset) const {
    // 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);

    if (offset + length <= next_relocation.address_patch) {
        if (bktr_read) {
            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
            std::vector<u8> iv(16);
            auto subsection_ctr = subsection.ctr;
            auto offset_iv = section_offset + base_offset;
            for (u8 i = 0; i < 8; ++i)
                iv[i] = section_ctr[0x8 - i - 1];
            offset_iv >>= 4;
            for (size_t i = 0; i < 8; ++i) {
                iv[0xF - i] = static_cast<u8>(offset_iv & 0xFF);
                offset_iv >>= 8;
            }
            for (size_t i = 0; i < 4; ++i) {
                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 auto block_offset = section_offset & 0xF;
                if (block_offset != 0) {
                    auto block = bktr_romfs->ReadBytes(0x10, section_offset & ~0xF);
                    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;
            } else {
                const u64 partition = next_subsection.address_patch - section_offset;
                return Read(data, partition, offset) +
                       Read(data + partition, length - partition, offset + partition);
            }
        } else {
            ASSERT(section_offset > ivfc_offset, "Offset calculation negative.");
            return base_romfs->Read(data, length, section_offset);
        }
    } else {
        const u64 partition = next_relocation.address_patch - offset;
        return Read(data, partition, offset) +
               Read(data + partition, length - partition, offset + partition);
    }
}

template <bool Subsection, typename BlockType, typename BucketType>
std::pair<size_t, size_t> BKTR::SearchBucketEntry(u64 offset, BlockType block,
                                                  BucketType buckets) const {
    if constexpr (Subsection) {
        const auto last_bucket = buckets[block.number_buckets - 1];
        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.");
    }

    size_t bucket_id = 0;
    for (size_t i = 1; i < block.number_buckets; ++i) {
        if (block.base_offsets[i] <= offset)
            ++bucket_id;
    }

    const auto bucket = buckets[bucket_id];

    if (bucket.number_entries == 1)
        return {bucket_id, 0};

    size_t low = 0;
    size_t mid = 0;
    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.");
}

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

size_t BKTR::GetSize() const {
    return relocation.size;
}

bool BKTR::Resize(size_t new_size) {
    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;
}

size_t BKTR::Write(const u8* data, size_t length, size_t offset) {
    return 0;
}

bool BKTR::Rename(std::string_view name) {
    return base_romfs->Rename(name);
}

} // namespace FileSys