nca_patch.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/assert.h"
  5. #include "core/crypto/aes_util.h"
  6. #include "core/file_sys/nca_patch.h"
  7. namespace FileSys {
  8. BKTR::BKTR(VirtualFile base_romfs_, VirtualFile bktr_romfs_, RelocationBlock relocation_,
  9. std::vector<RelocationBucket> relocation_buckets_, SubsectionBlock subsection_,
  10. std::vector<SubsectionBucket> subsection_buckets_, bool is_encrypted_,
  11. Core::Crypto::Key128 key_, u64 base_offset_, u64 ivfc_offset_,
  12. std::array<u8, 8> section_ctr_)
  13. : base_romfs(std::move(base_romfs_)), bktr_romfs(std::move(bktr_romfs_)),
  14. relocation(relocation_), relocation_buckets(std::move(relocation_buckets_)),
  15. subsection(subsection_), subsection_buckets(std::move(subsection_buckets_)),
  16. encrypted(is_encrypted_), key(key_), base_offset(base_offset_), ivfc_offset(ivfc_offset_),
  17. section_ctr(section_ctr_) {
  18. for (size_t i = 0; i < relocation.number_buckets - 1; ++i) {
  19. relocation_buckets[i].entries.push_back({relocation.base_offsets[i + 1], 0, 0});
  20. }
  21. for (size_t i = 0; i < subsection.number_buckets - 1; ++i) {
  22. subsection_buckets[i].entries.push_back({subsection_buckets[i + 1].entries[0].address_patch,
  23. {0},
  24. subsection_buckets[i + 1].entries[0].ctr});
  25. }
  26. relocation_buckets.back().entries.push_back({relocation.size, 0, 0});
  27. }
  28. BKTR::~BKTR() = default;
  29. size_t BKTR::Read(u8* data, size_t length, size_t offset) const {
  30. // Read out of bounds.
  31. if (offset >= relocation.size)
  32. return 0;
  33. const auto relocation = GetRelocationEntry(offset);
  34. const auto section_offset = offset - relocation.address_patch + relocation.address_source;
  35. const auto bktr_read = relocation.from_patch;
  36. const auto next_relocation = GetNextRelocationEntry(offset);
  37. if (offset + length >= next_relocation.address_patch) {
  38. const u64 partition = next_relocation.address_patch - offset;
  39. return Read(data, partition, offset) +
  40. Read(data + partition, length - partition, offset + partition);
  41. }
  42. if (!bktr_read) {
  43. ASSERT_MSG(section_offset > ivfc_offset, "Offset calculation negative.");
  44. return base_romfs->Read(data, length, section_offset);
  45. }
  46. if (!encrypted) {
  47. return bktr_romfs->Read(data, length, section_offset);
  48. }
  49. const auto subsection = GetSubsectionEntry(section_offset);
  50. Core::Crypto::AESCipher<Core::Crypto::Key128> cipher(key, Core::Crypto::Mode::CTR);
  51. // Calculate AES IV
  52. std::vector<u8> iv(16);
  53. auto subsection_ctr = subsection.ctr;
  54. auto offset_iv = section_offset + base_offset;
  55. for (size_t i = 0; i < section_ctr.size(); ++i)
  56. iv[i] = section_ctr[0x8 - i - 1];
  57. offset_iv >>= 4;
  58. for (size_t i = 0; i < sizeof(u64); ++i) {
  59. iv[0xF - i] = static_cast<u8>(offset_iv & 0xFF);
  60. offset_iv >>= 8;
  61. }
  62. for (size_t i = 0; i < sizeof(u32); ++i) {
  63. iv[0x7 - i] = static_cast<u8>(subsection_ctr & 0xFF);
  64. subsection_ctr >>= 8;
  65. }
  66. cipher.SetIV(iv);
  67. const auto next_subsection = GetNextSubsectionEntry(section_offset);
  68. if (section_offset + length > next_subsection.address_patch) {
  69. const u64 partition = next_subsection.address_patch - section_offset;
  70. return Read(data, partition, offset) +
  71. Read(data + partition, length - partition, offset + partition);
  72. }
  73. const auto block_offset = section_offset & 0xF;
  74. if (block_offset != 0) {
  75. auto block = bktr_romfs->ReadBytes(0x10, section_offset & ~0xF);
  76. cipher.Transcode(block.data(), block.size(), block.data(), Core::Crypto::Op::Decrypt);
  77. if (length + block_offset < 0x10) {
  78. std::memcpy(data, block.data() + block_offset, std::min(length, block.size()));
  79. return std::min(length, block.size());
  80. }
  81. const auto read = 0x10 - block_offset;
  82. std::memcpy(data, block.data() + block_offset, read);
  83. return read + Read(data + read, length - read, offset + read);
  84. }
  85. const auto raw_read = bktr_romfs->Read(data, length, section_offset);
  86. cipher.Transcode(data, raw_read, data, Core::Crypto::Op::Decrypt);
  87. return raw_read;
  88. }
  89. template <bool Subsection, typename BlockType, typename BucketType>
  90. std::pair<size_t, size_t> BKTR::SearchBucketEntry(u64 offset, BlockType block,
  91. BucketType buckets) const {
  92. if constexpr (Subsection) {
  93. const auto last_bucket = buckets[block.number_buckets - 1];
  94. if (offset >= last_bucket.entries[last_bucket.number_entries].address_patch)
  95. return {block.number_buckets - 1, last_bucket.number_entries};
  96. } else {
  97. ASSERT_MSG(offset <= block.size, "Offset is out of bounds in BKTR relocation block.");
  98. }
  99. size_t bucket_id = std::count_if(block.base_offsets.begin() + 1,
  100. block.base_offsets.begin() + block.number_buckets,
  101. [&offset](u64 base_offset) { return base_offset < offset; });
  102. const auto bucket = buckets[bucket_id];
  103. if (bucket.number_entries == 1)
  104. return {bucket_id, 0};
  105. size_t low = 0;
  106. size_t mid = 0;
  107. size_t high = bucket.number_entries - 1;
  108. while (low <= high) {
  109. mid = (low + high) / 2;
  110. if (bucket.entries[mid].address_patch > offset) {
  111. high = mid - 1;
  112. } else {
  113. if (mid == bucket.number_entries - 1 ||
  114. bucket.entries[mid + 1].address_patch > offset) {
  115. return {bucket_id, mid};
  116. }
  117. low = mid + 1;
  118. }
  119. }
  120. UNREACHABLE_MSG("Offset could not be found in BKTR block.");
  121. }
  122. RelocationEntry BKTR::GetRelocationEntry(u64 offset) const {
  123. const auto res = SearchBucketEntry<false>(offset, relocation, relocation_buckets);
  124. return relocation_buckets[res.first].entries[res.second];
  125. }
  126. RelocationEntry BKTR::GetNextRelocationEntry(u64 offset) const {
  127. const auto res = SearchBucketEntry<false>(offset, relocation, relocation_buckets);
  128. const auto bucket = relocation_buckets[res.first];
  129. if (res.second + 1 < bucket.entries.size())
  130. return bucket.entries[res.second + 1];
  131. return relocation_buckets[res.first + 1].entries[0];
  132. }
  133. SubsectionEntry BKTR::GetSubsectionEntry(u64 offset) const {
  134. const auto res = SearchBucketEntry<true>(offset, subsection, subsection_buckets);
  135. return subsection_buckets[res.first].entries[res.second];
  136. }
  137. SubsectionEntry BKTR::GetNextSubsectionEntry(u64 offset) const {
  138. const auto res = SearchBucketEntry<true>(offset, subsection, subsection_buckets);
  139. const auto bucket = subsection_buckets[res.first];
  140. if (res.second + 1 < bucket.entries.size())
  141. return bucket.entries[res.second + 1];
  142. return subsection_buckets[res.first + 1].entries[0];
  143. }
  144. std::string BKTR::GetName() const {
  145. return base_romfs->GetName();
  146. }
  147. size_t BKTR::GetSize() const {
  148. return relocation.size;
  149. }
  150. bool BKTR::Resize(size_t new_size) {
  151. return false;
  152. }
  153. std::shared_ptr<VfsDirectory> BKTR::GetContainingDirectory() const {
  154. return base_romfs->GetContainingDirectory();
  155. }
  156. bool BKTR::IsWritable() const {
  157. return false;
  158. }
  159. bool BKTR::IsReadable() const {
  160. return true;
  161. }
  162. size_t BKTR::Write(const u8* data, size_t length, size_t offset) {
  163. return 0;
  164. }
  165. bool BKTR::Rename(std::string_view name) {
  166. return base_romfs->Rename(name);
  167. }
  168. } // namespace FileSys