summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Zach Hilman2018-08-26 10:53:31 -0400
committerGravatar Zach Hilman2018-09-04 16:24:02 -0400
commit9664ce255db09f4501db642c1e82d8cf8f274a22 (patch)
tree5548a6d79f3806c64d7e0d6ee559f619dfe4c6c0 /src
parentmain: Make game updates installable (diff)
downloadyuzu-9664ce255db09f4501db642c1e82d8cf8f274a22.tar.gz
yuzu-9664ce255db09f4501db642c1e82d8cf8f274a22.tar.xz
yuzu-9664ce255db09f4501db642c1e82d8cf8f274a22.zip
bktr: Fix missing includes and optimize style
Diffstat (limited to 'src')
-rw-r--r--src/core/file_sys/nca_patch.cpp126
-rw-r--r--src/core/file_sys/nca_patch.h5
-rw-r--r--src/core/file_sys/patch_manager.cpp35
-rw-r--r--src/core/file_sys/patch_manager.h9
-rw-r--r--src/core/file_sys/registered_cache.cpp21
-rw-r--r--src/core/file_sys/registered_cache.h2
-rw-r--r--src/core/loader/deconstructed_rom_directory.cpp2
-rw-r--r--src/core/loader/deconstructed_rom_directory.h2
-rw-r--r--src/core/loader/loader.h2
-rw-r--r--src/core/loader/nro.cpp2
-rw-r--r--src/core/loader/nro.h2
-rw-r--r--src/yuzu/main.cpp4
12 files changed, 109 insertions, 103 deletions
diff --git a/src/core/file_sys/nca_patch.cpp b/src/core/file_sys/nca_patch.cpp
index dd684c38e..22fbba573 100644
--- a/src/core/file_sys/nca_patch.cpp
+++ b/src/core/file_sys/nca_patch.cpp
@@ -17,7 +17,7 @@ BKTR::BKTR(VirtualFile base_romfs_, VirtualFile bktr_romfs_, RelocationBlock rel
17 relocation(relocation_), relocation_buckets(std::move(relocation_buckets_)), 17 relocation(relocation_), relocation_buckets(std::move(relocation_buckets_)),
18 subsection(subsection_), subsection_buckets(std::move(subsection_buckets_)), 18 subsection(subsection_), subsection_buckets(std::move(subsection_buckets_)),
19 encrypted(is_encrypted_), key(key_), base_offset(base_offset_), ivfc_offset(ivfc_offset_), 19 encrypted(is_encrypted_), key(key_), base_offset(base_offset_), ivfc_offset(ivfc_offset_),
20 section_ctr(std::move(section_ctr_)) { 20 section_ctr(section_ctr_) {
21 for (size_t i = 0; i < relocation.number_buckets - 1; ++i) { 21 for (size_t i = 0; i < relocation.number_buckets - 1; ++i) {
22 relocation_buckets[i].entries.push_back({relocation.base_offsets[i + 1], 0, 0}); 22 relocation_buckets[i].entries.push_back({relocation.base_offsets[i + 1], 0, 0});
23 } 23 }
@@ -31,6 +31,8 @@ BKTR::BKTR(VirtualFile base_romfs_, VirtualFile bktr_romfs_, RelocationBlock rel
31 relocation_buckets.back().entries.push_back({relocation.size, 0, 0}); 31 relocation_buckets.back().entries.push_back({relocation.size, 0, 0});
32} 32}
33 33
34BKTR::~BKTR() = default;
35
34size_t BKTR::Read(u8* data, size_t length, size_t offset) const { 36size_t BKTR::Read(u8* data, size_t length, size_t offset) const {
35 // Read out of bounds. 37 // Read out of bounds.
36 if (offset >= relocation.size) 38 if (offset >= relocation.size)
@@ -41,68 +43,66 @@ size_t BKTR::Read(u8* data, size_t length, size_t offset) const {
41 43
42 const auto next_relocation = GetNextRelocationEntry(offset); 44 const auto next_relocation = GetNextRelocationEntry(offset);
43 45
44 if (offset + length <= next_relocation.address_patch) { 46 if (offset + length >= next_relocation.address_patch) {
45 if (bktr_read) {
46 if (!encrypted) {
47 return bktr_romfs->Read(data, length, section_offset);
48 }
49
50 const auto subsection = GetSubsectionEntry(section_offset);
51 Core::Crypto::AESCipher<Core::Crypto::Key128> cipher(key, Core::Crypto::Mode::CTR);
52
53 // Calculate AES IV
54 std::vector<u8> iv(16);
55 auto subsection_ctr = subsection.ctr;
56 auto offset_iv = section_offset + base_offset;
57 for (u8 i = 0; i < 8; ++i)
58 iv[i] = section_ctr[0x8 - i - 1];
59 offset_iv >>= 4;
60 for (size_t i = 0; i < 8; ++i) {
61 iv[0xF - i] = static_cast<u8>(offset_iv & 0xFF);
62 offset_iv >>= 8;
63 }
64 for (size_t i = 0; i < 4; ++i) {
65 iv[0x7 - i] = static_cast<u8>(subsection_ctr & 0xFF);
66 subsection_ctr >>= 8;
67 }
68 cipher.SetIV(iv);
69
70 const auto next_subsection = GetNextSubsectionEntry(section_offset);
71
72 if (section_offset + length <= next_subsection.address_patch) {
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(),
77 Core::Crypto::Op::Decrypt);
78 if (length + block_offset < 0x10) {
79 std::memcpy(data, block.data() + block_offset,
80 std::min(length, block.size()));
81 return std::min(length, block.size());
82 }
83
84 const auto read = 0x10 - block_offset;
85 std::memcpy(data, block.data() + block_offset, read);
86 return read + Read(data + read, length - read, offset + read);
87 }
88
89 const auto raw_read = bktr_romfs->Read(data, length, section_offset);
90 cipher.Transcode(data, raw_read, data, Core::Crypto::Op::Decrypt);
91 return raw_read;
92 } else {
93 const u64 partition = next_subsection.address_patch - section_offset;
94 return Read(data, partition, offset) +
95 Read(data + partition, length - partition, offset + partition);
96 }
97 } else {
98 ASSERT(section_offset > ivfc_offset, "Offset calculation negative.");
99 return base_romfs->Read(data, length, section_offset);
100 }
101 } else {
102 const u64 partition = next_relocation.address_patch - offset; 47 const u64 partition = next_relocation.address_patch - offset;
103 return Read(data, partition, offset) + 48 return Read(data, partition, offset) +
104 Read(data + partition, length - partition, offset + partition); 49 Read(data + partition, length - partition, offset + partition);
105 } 50 }
51
52 if (!bktr_read) {
53 ASSERT_MSG(section_offset > ivfc_offset, "Offset calculation negative.");
54 return base_romfs->Read(data, length, section_offset);
55 }
56
57 if (!encrypted) {
58 return bktr_romfs->Read(data, length, section_offset);
59 }
60
61 const auto subsection = GetSubsectionEntry(section_offset);
62 Core::Crypto::AESCipher<Core::Crypto::Key128> cipher(key, Core::Crypto::Mode::CTR);
63
64 // Calculate AES IV
65 std::vector<u8> iv(16);
66 auto subsection_ctr = subsection.ctr;
67 auto offset_iv = section_offset + base_offset;
68 for (size_t i = 0; i < section_ctr.size(); ++i)
69 iv[i] = section_ctr[0x8 - i - 1];
70 offset_iv >>= 4;
71 for (size_t i = 0; i < sizeof(u64); ++i) {
72 iv[0xF - i] = static_cast<u8>(offset_iv & 0xFF);
73 offset_iv >>= 8;
74 }
75 for (size_t i = 0; i < sizeof(u32); ++i) {
76 iv[0x7 - i] = static_cast<u8>(subsection_ctr & 0xFF);
77 subsection_ctr >>= 8;
78 }
79 cipher.SetIV(iv);
80
81 const auto next_subsection = GetNextSubsectionEntry(section_offset);
82
83 if (section_offset + length > next_subsection.address_patch) {
84 const u64 partition = next_subsection.address_patch - section_offset;
85 return Read(data, partition, offset) +
86 Read(data + partition, length - partition, offset + partition);
87 }
88
89 const auto block_offset = section_offset & 0xF;
90 if (block_offset != 0) {
91 auto block = bktr_romfs->ReadBytes(0x10, section_offset & ~0xF);
92 cipher.Transcode(block.data(), block.size(), block.data(), Core::Crypto::Op::Decrypt);
93 if (length + block_offset < 0x10) {
94 std::memcpy(data, block.data() + block_offset, std::min(length, block.size()));
95 return std::min(length, block.size());
96 }
97
98 const auto read = 0x10 - block_offset;
99 std::memcpy(data, block.data() + block_offset, read);
100 return read + Read(data + read, length - read, offset + read);
101 }
102
103 const auto raw_read = bktr_romfs->Read(data, length, section_offset);
104 cipher.Transcode(data, raw_read, data, Core::Crypto::Op::Decrypt);
105 return raw_read;
106} 106}
107 107
108template <bool Subsection, typename BlockType, typename BucketType> 108template <bool Subsection, typename BlockType, typename BucketType>
@@ -116,11 +116,9 @@ std::pair<size_t, size_t> BKTR::SearchBucketEntry(u64 offset, BlockType block,
116 ASSERT_MSG(offset <= block.size, "Offset is out of bounds in BKTR relocation block."); 116 ASSERT_MSG(offset <= block.size, "Offset is out of bounds in BKTR relocation block.");
117 } 117 }
118 118
119 size_t bucket_id = 0; 119 size_t bucket_id = std::count_if(block.base_offsets.begin() + 1,
120 for (size_t i = 1; i < block.number_buckets; ++i) { 120 block.base_offsets.begin() + block.number_buckets,
121 if (block.base_offsets[i] <= offset) 121 [&offset](u64 base_offset) { return base_offset < offset; });
122 ++bucket_id;
123 }
124 122
125 const auto bucket = buckets[bucket_id]; 123 const auto bucket = buckets[bucket_id];
126 124
diff --git a/src/core/file_sys/nca_patch.h b/src/core/file_sys/nca_patch.h
index 8b8d0a4f5..0d9ad95f5 100644
--- a/src/core/file_sys/nca_patch.h
+++ b/src/core/file_sys/nca_patch.h
@@ -4,9 +4,11 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <array>
8#include <vector>
9#include <common/common_funcs.h>
7#include "core/crypto/key_manager.h" 10#include "core/crypto/key_manager.h"
8#include "core/file_sys/romfs.h" 11#include "core/file_sys/romfs.h"
9#include "core/loader/loader.h"
10 12
11namespace FileSys { 13namespace FileSys {
12 14
@@ -91,6 +93,7 @@ public:
91 std::vector<RelocationBucket> relocation_buckets, SubsectionBlock subsection, 93 std::vector<RelocationBucket> relocation_buckets, SubsectionBlock subsection,
92 std::vector<SubsectionBucket> subsection_buckets, bool is_encrypted, 94 std::vector<SubsectionBucket> subsection_buckets, bool is_encrypted,
93 Core::Crypto::Key128 key, u64 base_offset, u64 ivfc_offset, std::array<u8, 8> section_ctr); 95 Core::Crypto::Key128 key, u64 base_offset, u64 ivfc_offset, std::array<u8, 8> section_ctr);
96 ~BKTR() override;
94 97
95 size_t Read(u8* data, size_t length, size_t offset) const override; 98 size_t Read(u8* data, size_t length, size_t offset) const override;
96 99
diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp
index 697b8a4c9..5e853c2c0 100644
--- a/src/core/file_sys/patch_manager.cpp
+++ b/src/core/file_sys/patch_manager.cpp
@@ -8,24 +8,19 @@
8 8
9namespace FileSys { 9namespace FileSys {
10 10
11union TitleVersion { 11constexpr u64 SINGLE_BYTE_MODULUS = 0x100;
12 u32 version; 12
13 13std::string FormatTitleVersion(u32 version, TitleVersionFormat format) {
14 struct { 14 std::array<u8, sizeof(u32)> bytes{};
15 u8 v_revision; 15 bytes[0] = version % SINGLE_BYTE_MODULUS;
16 u8 v_micro; 16 for (size_t i = 1; i < bytes.size(); ++i) {
17 u8 v_minor; 17 version /= SINGLE_BYTE_MODULUS;
18 u8 v_major; 18 bytes[i] = version % SINGLE_BYTE_MODULUS;
19 }; 19 }
20};
21
22std::string FormatTitleVersion(u32 version_, bool full) {
23 TitleVersion ver{};
24 ver.version = version_;
25 20
26 if (full) 21 if (format == TitleVersionFormat::FourElements)
27 return fmt::format("v{}.{}.{}.{}", ver.v_major, ver.v_minor, ver.v_minor, ver.v_revision); 22 return fmt::format("v{}.{}.{}.{}", bytes[3], bytes[2], bytes[1], bytes[0]);
28 return fmt::format("v{}.{}.{}", ver.v_major, ver.v_minor, ver.v_micro); 23 return fmt::format("v{}.{}.{}", bytes[3], bytes[2], bytes[1]);
29} 24}
30 25
31constexpr std::array<const char*, 1> PATCH_TYPE_NAMES{ 26constexpr std::array<const char*, 1> PATCH_TYPE_NAMES{
@@ -49,8 +44,9 @@ VirtualDir PatchManager::PatchExeFS(VirtualDir exefs) const {
49 const auto update = installed->GetEntry(update_tid, ContentRecordType::Program); 44 const auto update = installed->GetEntry(update_tid, ContentRecordType::Program);
50 if (update != nullptr) { 45 if (update != nullptr) {
51 if (update->GetStatus() == Loader::ResultStatus::ErrorMissingBKTRBaseRomFS && 46 if (update->GetStatus() == Loader::ResultStatus::ErrorMissingBKTRBaseRomFS &&
52 update->GetExeFS() != nullptr) 47 update->GetExeFS() != nullptr) {
53 exefs = update->GetExeFS(); 48 exefs = update->GetExeFS();
49 }
54 } 50 }
55 51
56 return exefs; 52 return exefs;
@@ -81,8 +77,9 @@ std::map<PatchType, u32> PatchManager::GetPatchVersionNames() const {
81 const auto update_tid = GetUpdateTitleID(title_id); 77 const auto update_tid = GetUpdateTitleID(title_id);
82 const auto update_version = installed->GetEntryVersion(update_tid); 78 const auto update_version = installed->GetEntryVersion(update_tid);
83 if (update_version != boost::none && 79 if (update_version != boost::none &&
84 installed->HasEntry(update_tid, ContentRecordType::Program)) 80 installed->HasEntry(update_tid, ContentRecordType::Program)) {
85 out[PatchType::Update] = update_version.get(); 81 out[PatchType::Update] = update_version.get();
82 }
86 83
87 return out; 84 return out;
88} 85}
diff --git a/src/core/file_sys/patch_manager.h b/src/core/file_sys/patch_manager.h
index 2a39c473a..803bcb2a2 100644
--- a/src/core/file_sys/patch_manager.h
+++ b/src/core/file_sys/patch_manager.h
@@ -5,12 +5,19 @@
5#pragma once 5#pragma once
6 6
7#include <map> 7#include <map>
8#include <string>
8#include "common/common_types.h" 9#include "common/common_types.h"
9#include "core/file_sys/vfs.h" 10#include "core/file_sys/vfs.h"
10 11
11namespace FileSys { 12namespace FileSys {
12 13
13std::string FormatTitleVersion(u32 version, bool full = false); 14enum class TitleVersionFormat : u8 {
15 ThreeElements, ///< vX.Y.Z
16 FourElements, ///< vX.Y.Z.W
17};
18
19std::string FormatTitleVersion(u32 version,
20 TitleVersionFormat format = TitleVersionFormat::ThreeElements);
14 21
15enum class PatchType { 22enum class PatchType {
16 Update, 23 Update,
diff --git a/src/core/file_sys/registered_cache.cpp b/src/core/file_sys/registered_cache.cpp
index 39c0710e1..7361a67be 100644
--- a/src/core/file_sys/registered_cache.cpp
+++ b/src/core/file_sys/registered_cache.cpp
@@ -281,10 +281,14 @@ VirtualFile RegisteredCache::GetEntryUnparsed(RegisteredCacheEntry entry) const
281} 281}
282 282
283boost::optional<u32> RegisteredCache::GetEntryVersion(u64 title_id) const { 283boost::optional<u32> RegisteredCache::GetEntryVersion(u64 title_id) const {
284 if (meta.find(title_id) != meta.end()) 284 const auto meta_iter = meta.find(title_id);
285 return meta.at(title_id).GetTitleVersion(); 285 if (meta_iter != meta.end())
286 if (yuzu_meta.find(title_id) != yuzu_meta.end()) 286 return meta_iter->second.GetTitleVersion();
287 return yuzu_meta.at(title_id).GetTitleVersion(); 287
288 const auto yuzu_meta_iter = yuzu_meta.find(title_id);
289 if (yuzu_meta_iter != yuzu_meta.end())
290 return yuzu_meta_iter->second.GetTitleVersion();
291
288 return boost::none; 292 return boost::none;
289} 293}
290 294
@@ -516,12 +520,9 @@ void RegisteredCacheUnion::Refresh() {
516} 520}
517 521
518bool RegisteredCacheUnion::HasEntry(u64 title_id, ContentRecordType type) const { 522bool RegisteredCacheUnion::HasEntry(u64 title_id, ContentRecordType type) const {
519 for (const auto& c : caches) { 523 return std::any_of(caches.begin(), caches.end(), [title_id, type](const auto& cache) {
520 if (c->HasEntry(title_id, type)) 524 return cache->HasEntry(title_id, type);
521 return true; 525 });
522 }
523
524 return false;
525} 526}
526 527
527bool RegisteredCacheUnion::HasEntry(RegisteredCacheEntry entry) const { 528bool RegisteredCacheUnion::HasEntry(RegisteredCacheEntry entry) const {
diff --git a/src/core/file_sys/registered_cache.h b/src/core/file_sys/registered_cache.h
index dcce3fd16..f487b0cf0 100644
--- a/src/core/file_sys/registered_cache.h
+++ b/src/core/file_sys/registered_cache.h
@@ -43,7 +43,7 @@ struct RegisteredCacheEntry {
43 std::string DebugInfo() const; 43 std::string DebugInfo() const;
44}; 44};
45 45
46constexpr inline u64 GetUpdateTitleID(u64 base_title_id) { 46constexpr u64 GetUpdateTitleID(u64 base_title_id) {
47 return base_title_id | 0x800; 47 return base_title_id | 0x800;
48} 48}
49 49
diff --git a/src/core/loader/deconstructed_rom_directory.cpp b/src/core/loader/deconstructed_rom_directory.cpp
index 6b2230269..223570431 100644
--- a/src/core/loader/deconstructed_rom_directory.cpp
+++ b/src/core/loader/deconstructed_rom_directory.cpp
@@ -189,7 +189,7 @@ ResultStatus AppLoader_DeconstructedRomDirectory::ReadTitle(std::string& title)
189 return ResultStatus::Success; 189 return ResultStatus::Success;
190} 190}
191 191
192bool AppLoader_DeconstructedRomDirectory::IsRomFSUpdatable() { 192bool AppLoader_DeconstructedRomDirectory::IsRomFSUpdatable() const {
193 return false; 193 return false;
194} 194}
195 195
diff --git a/src/core/loader/deconstructed_rom_directory.h b/src/core/loader/deconstructed_rom_directory.h
index a8644516b..8a0dc1b1e 100644
--- a/src/core/loader/deconstructed_rom_directory.h
+++ b/src/core/loader/deconstructed_rom_directory.h
@@ -44,7 +44,7 @@ public:
44 ResultStatus ReadIcon(std::vector<u8>& buffer) override; 44 ResultStatus ReadIcon(std::vector<u8>& buffer) override;
45 ResultStatus ReadProgramId(u64& out_program_id) override; 45 ResultStatus ReadProgramId(u64& out_program_id) override;
46 ResultStatus ReadTitle(std::string& title) override; 46 ResultStatus ReadTitle(std::string& title) override;
47 bool IsRomFSUpdatable() override; 47 bool IsRomFSUpdatable() const override;
48 48
49private: 49private:
50 FileSys::ProgramMetadata metadata; 50 FileSys::ProgramMetadata metadata;
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h
index b4a3a6573..225c05127 100644
--- a/src/core/loader/loader.h
+++ b/src/core/loader/loader.h
@@ -210,7 +210,7 @@ public:
210 * the base game it should be set to false. 210 * the base game it should be set to false.
211 * @return bool whether or not updatable. 211 * @return bool whether or not updatable.
212 */ 212 */
213 virtual bool IsRomFSUpdatable() { 213 virtual bool IsRomFSUpdatable() const {
214 return true; 214 return true;
215 } 215 }
216 216
diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp
index 96f5cd9e5..bb89a9da3 100644
--- a/src/core/loader/nro.cpp
+++ b/src/core/loader/nro.cpp
@@ -233,7 +233,7 @@ ResultStatus AppLoader_NRO::ReadTitle(std::string& title) {
233 return ResultStatus::Success; 233 return ResultStatus::Success;
234} 234}
235 235
236bool AppLoader_NRO::IsRomFSUpdatable() { 236bool AppLoader_NRO::IsRomFSUpdatable() const {
237 return false; 237 return false;
238} 238}
239 239
diff --git a/src/core/loader/nro.h b/src/core/loader/nro.h
index c35c99d14..96d2de305 100644
--- a/src/core/loader/nro.h
+++ b/src/core/loader/nro.h
@@ -39,7 +39,7 @@ public:
39 ResultStatus ReadProgramId(u64& out_program_id) override; 39 ResultStatus ReadProgramId(u64& out_program_id) override;
40 ResultStatus ReadRomFS(FileSys::VirtualFile& dir) override; 40 ResultStatus ReadRomFS(FileSys::VirtualFile& dir) override;
41 ResultStatus ReadTitle(std::string& title) override; 41 ResultStatus ReadTitle(std::string& title) override;
42 bool IsRomFSUpdatable() override; 42 bool IsRomFSUpdatable() const override;
43 43
44private: 44private:
45 bool LoadNro(FileSys::VirtualFile file, VAddr load_base); 45 bool LoadNro(FileSys::VirtualFile file, VAddr load_base);
diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp
index 3d438df47..b7ce0248b 100644
--- a/src/yuzu/main.cpp
+++ b/src/yuzu/main.cpp
@@ -871,8 +871,8 @@ void GMainWindow::OnMenuInstallToNAND() {
871 const auto id = nca->GetStatus(); 871 const auto id = nca->GetStatus();
872 872
873 // Game updates necessary are missing base RomFS 873 // Game updates necessary are missing base RomFS
874 if (nca->GetStatus() != Loader::ResultStatus::Success && 874 if (id != Loader::ResultStatus::Success &&
875 nca->GetStatus() != Loader::ResultStatus::ErrorMissingBKTRBaseRomFS) { 875 id != Loader::ResultStatus::ErrorMissingBKTRBaseRomFS) {
876 failed(); 876 failed();
877 return; 877 return;
878 } 878 }