diff options
author | bunnei <bunneidev@gmail.com> | 2018-08-12 01:25:30 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-12 01:25:30 +0200 |
commit | bc286c169fb8b07d21e082e05152cfd6bc611b33 (patch) | |
tree | 512bc4cca3adbe98a16cae454377c2ec2638b5e3 /src/core/file_sys | |
parent | Merge pull request #1018 from Subv/ssy_sync (diff) | |
parent | game_list: Reorder error checks (diff) | |
download | yuzu-bc286c169fb8b07d21e082e05152cfd6bc611b33.tar yuzu-bc286c169fb8b07d21e082e05152cfd6bc611b33.tar.gz yuzu-bc286c169fb8b07d21e082e05152cfd6bc611b33.tar.bz2 yuzu-bc286c169fb8b07d21e082e05152cfd6bc611b33.tar.lz yuzu-bc286c169fb8b07d21e082e05152cfd6bc611b33.tar.xz yuzu-bc286c169fb8b07d21e082e05152cfd6bc611b33.tar.zst yuzu-bc286c169fb8b07d21e082e05152cfd6bc611b33.zip |
Diffstat (limited to 'src/core/file_sys')
-rw-r--r-- | src/core/file_sys/card_image.cpp | 19 | ||||
-rw-r--r-- | src/core/file_sys/content_archive.cpp | 86 | ||||
-rw-r--r-- | src/core/file_sys/content_archive.h | 5 | ||||
-rw-r--r-- | src/core/file_sys/partition_filesystem.cpp | 8 | ||||
-rw-r--r-- | src/core/file_sys/program_metadata.cpp | 12 |
5 files changed, 95 insertions, 35 deletions
diff --git a/src/core/file_sys/card_image.cpp b/src/core/file_sys/card_image.cpp index e897d9913..a4823353e 100644 --- a/src/core/file_sys/card_image.cpp +++ b/src/core/file_sys/card_image.cpp @@ -12,14 +12,16 @@ namespace FileSys { +constexpr std::array<const char*, 0x4> partition_names = {"update", "normal", "secure", "logo"}; + XCI::XCI(VirtualFile file_) : file(std::move(file_)), partitions(0x4) { if (file->ReadObject(&header) != sizeof(GamecardHeader)) { - status = Loader::ResultStatus::ErrorInvalidFormat; + status = Loader::ResultStatus::ErrorBadXCIHeader; return; } if (header.magic != Common::MakeMagic('H', 'E', 'A', 'D')) { - status = Loader::ResultStatus::ErrorInvalidFormat; + status = Loader::ResultStatus::ErrorBadXCIHeader; return; } @@ -31,9 +33,6 @@ XCI::XCI(VirtualFile file_) : file(std::move(file_)), partitions(0x4) { return; } - static constexpr std::array<const char*, 0x4> partition_names = {"update", "normal", "secure", - "logo"}; - for (XCIPartition partition : {XCIPartition::Update, XCIPartition::Normal, XCIPartition::Secure, XCIPartition::Logo}) { auto raw = main_hfs.GetFile(partition_names[static_cast<size_t>(partition)]); @@ -130,15 +129,21 @@ bool XCI::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) { Loader::ResultStatus XCI::AddNCAFromPartition(XCIPartition part) { if (partitions[static_cast<size_t>(part)] == nullptr) { - return Loader::ResultStatus::ErrorInvalidFormat; + return Loader::ResultStatus::ErrorXCIMissingPartition; } for (const VirtualFile& file : partitions[static_cast<size_t>(part)]->GetFiles()) { if (file->GetExtension() != "nca") continue; auto nca = std::make_shared<NCA>(file); - if (nca->GetStatus() == Loader::ResultStatus::Success) + if (nca->GetStatus() == Loader::ResultStatus::Success) { ncas.push_back(std::move(nca)); + } else { + const u16 error_id = static_cast<u16>(nca->GetStatus()); + LOG_CRITICAL(Loader, "Could not load NCA {}/{}, failed with error code {:04X} ({})", + partition_names[static_cast<size_t>(part)], nca->GetName(), error_id, + Loader::GetMessageForResultStatus(nca->GetStatus())); + } } return Loader::ResultStatus::Success; diff --git a/src/core/file_sys/content_archive.cpp b/src/core/file_sys/content_archive.cpp index d3007d981..47afcad9b 100644 --- a/src/core/file_sys/content_archive.cpp +++ b/src/core/file_sys/content_archive.cpp @@ -113,17 +113,27 @@ boost::optional<Core::Crypto::Key128> NCA::GetKeyAreaKey(NCASectionCryptoType ty return out; } -boost::optional<Core::Crypto::Key128> NCA::GetTitlekey() const { +boost::optional<Core::Crypto::Key128> NCA::GetTitlekey() { const auto master_key_id = GetCryptoRevision(); u128 rights_id{}; memcpy(rights_id.data(), header.rights_id.data(), 16); - if (rights_id == u128{}) + if (rights_id == u128{}) { + status = Loader::ResultStatus::ErrorInvalidRightsID; return boost::none; + } auto titlekey = keys.GetKey(Core::Crypto::S128KeyType::Titlekey, rights_id[1], rights_id[0]); - if (titlekey == Core::Crypto::Key128{}) + if (titlekey == Core::Crypto::Key128{}) { + status = Loader::ResultStatus::ErrorMissingTitlekey; + return boost::none; + } + + if (!keys.HasKey(Core::Crypto::S128KeyType::Titlekek, master_key_id)) { + status = Loader::ResultStatus::ErrorMissingTitlekek; return boost::none; + } + Core::Crypto::AESCipher<Core::Crypto::Key128> cipher( keys.GetKey(Core::Crypto::S128KeyType::Titlekek, master_key_id), Core::Crypto::Mode::ECB); cipher.Transcode(titlekey.data(), titlekey.size(), titlekey.data(), Core::Crypto::Op::Decrypt); @@ -131,7 +141,7 @@ boost::optional<Core::Crypto::Key128> NCA::GetTitlekey() const { return titlekey; } -VirtualFile NCA::Decrypt(NCASectionHeader s_header, VirtualFile in, u64 starting_offset) const { +VirtualFile NCA::Decrypt(NCASectionHeader s_header, VirtualFile in, u64 starting_offset) { if (!encrypted) return in; @@ -143,15 +153,22 @@ VirtualFile NCA::Decrypt(NCASectionHeader s_header, VirtualFile in, u64 starting LOG_DEBUG(Crypto, "called with mode=CTR, starting_offset={:016X}", starting_offset); { boost::optional<Core::Crypto::Key128> key = boost::none; - if (std::find_if_not(header.rights_id.begin(), header.rights_id.end(), - [](char c) { return c == 0; }) == header.rights_id.end()) { - key = GetKeyAreaKey(NCASectionCryptoType::CTR); - } else { + if (has_rights_id) { + status = Loader::ResultStatus::Success; key = GetTitlekey(); + if (key == boost::none) { + if (status == Loader::ResultStatus::Success) + status = Loader::ResultStatus::ErrorMissingTitlekey; + return nullptr; + } + } else { + key = GetKeyAreaKey(NCASectionCryptoType::CTR); + if (key == boost::none) { + status = Loader::ResultStatus::ErrorMissingKeyAreaKey; + return nullptr; + } } - if (key == boost::none) - return nullptr; auto out = std::make_shared<Core::Crypto::CTREncryptionLayer>( std::move(in), key.value(), starting_offset); std::vector<u8> iv(16); @@ -170,16 +187,31 @@ VirtualFile NCA::Decrypt(NCASectionHeader s_header, VirtualFile in, u64 starting } NCA::NCA(VirtualFile file_) : file(std::move(file_)) { + status = Loader::ResultStatus::Success; + if (file == nullptr) { - status = Loader::ResultStatus::ErrorInvalidFormat; + status = Loader::ResultStatus::ErrorNullFile; return; } - if (sizeof(NCAHeader) != file->ReadObject(&header)) + + if (sizeof(NCAHeader) != file->ReadObject(&header)) { LOG_ERROR(Loader, "File reader errored out during header read."); + status = Loader::ResultStatus::ErrorBadNCAHeader; + return; + } encrypted = false; if (!IsValidNCA(header)) { + if (header.magic == Common::MakeMagic('N', 'C', 'A', '2')) { + status = Loader::ResultStatus::ErrorNCA2; + return; + } + if (header.magic == Common::MakeMagic('N', 'C', 'A', '0')) { + status = Loader::ResultStatus::ErrorNCA0; + return; + } + NCAHeader dec_header{}; Core::Crypto::AESCipher<Core::Crypto::Key256> cipher( keys.GetKey(Core::Crypto::S256KeyType::Header), Core::Crypto::Mode::XTS); @@ -189,14 +221,26 @@ NCA::NCA(VirtualFile file_) : file(std::move(file_)) { header = dec_header; encrypted = true; } else { + if (dec_header.magic == Common::MakeMagic('N', 'C', 'A', '2')) { + status = Loader::ResultStatus::ErrorNCA2; + return; + } + if (dec_header.magic == Common::MakeMagic('N', 'C', 'A', '0')) { + status = Loader::ResultStatus::ErrorNCA0; + return; + } + if (!keys.HasKey(Core::Crypto::S256KeyType::Header)) - status = Loader::ResultStatus::ErrorMissingKeys; + status = Loader::ResultStatus::ErrorMissingHeaderKey; else - status = Loader::ResultStatus::ErrorDecrypting; + status = Loader::ResultStatus::ErrorIncorrectHeaderKey; return; } } + has_rights_id = std::find_if_not(header.rights_id.begin(), header.rights_id.end(), + [](char c) { return c == '\0'; }) != header.rights_id.end(); + const std::ptrdiff_t number_sections = std::count_if(std::begin(header.section_tables), std::end(header.section_tables), [](NCASectionTableEntry entry) { return entry.media_offset > 0; }); @@ -229,7 +273,12 @@ NCA::NCA(VirtualFile file_) : file(std::move(file_)) { files.push_back(std::move(dec)); romfs = files.back(); } else { - status = Loader::ResultStatus::ErrorMissingKeys; + if (status != Loader::ResultStatus::Success) + return; + if (has_rights_id) + status = Loader::ResultStatus::ErrorIncorrectTitlekeyOrTitlekek; + else + status = Loader::ResultStatus::ErrorIncorrectKeyAreaKey; return; } } else if (section.raw.header.filesystem_type == NCASectionFilesystemType::PFS0) { @@ -249,7 +298,12 @@ NCA::NCA(VirtualFile file_) : file(std::move(file_)) { exefs = dirs.back(); } } else { - status = Loader::ResultStatus::ErrorMissingKeys; + if (status != Loader::ResultStatus::Success) + return; + if (has_rights_id) + status = Loader::ResultStatus::ErrorIncorrectTitlekeyOrTitlekek; + else + status = Loader::ResultStatus::ErrorIncorrectKeyAreaKey; return; } } diff --git a/src/core/file_sys/content_archive.h b/src/core/file_sys/content_archive.h index 5cfd5031a..b82e65ad5 100644 --- a/src/core/file_sys/content_archive.h +++ b/src/core/file_sys/content_archive.h @@ -98,8 +98,8 @@ protected: private: u8 GetCryptoRevision() const; boost::optional<Core::Crypto::Key128> GetKeyAreaKey(NCASectionCryptoType type) const; - boost::optional<Core::Crypto::Key128> GetTitlekey() const; - VirtualFile Decrypt(NCASectionHeader header, VirtualFile in, u64 starting_offset) const; + boost::optional<Core::Crypto::Key128> GetTitlekey(); + VirtualFile Decrypt(NCASectionHeader header, VirtualFile in, u64 starting_offset); std::vector<VirtualDir> dirs; std::vector<VirtualFile> files; @@ -109,6 +109,7 @@ private: VirtualFile file; NCAHeader header{}; + bool has_rights_id{}; Loader::ResultStatus status{}; diff --git a/src/core/file_sys/partition_filesystem.cpp b/src/core/file_sys/partition_filesystem.cpp index 47e032b19..c377edc9c 100644 --- a/src/core/file_sys/partition_filesystem.cpp +++ b/src/core/file_sys/partition_filesystem.cpp @@ -24,19 +24,19 @@ bool PartitionFilesystem::Header::HasValidMagicValue() const { PartitionFilesystem::PartitionFilesystem(std::shared_ptr<VfsFile> file) { // At least be as large as the header if (file->GetSize() < sizeof(Header)) { - status = Loader::ResultStatus::Error; + status = Loader::ResultStatus::ErrorBadPFSHeader; return; } // For cartridges, HFSs can get very large, so we need to calculate the size up to // the actual content itself instead of just blindly reading in the entire file. if (sizeof(Header) != file->ReadObject(&pfs_header)) { - status = Loader::ResultStatus::Error; + status = Loader::ResultStatus::ErrorBadPFSHeader; return; } if (!pfs_header.HasValidMagicValue()) { - status = Loader::ResultStatus::ErrorInvalidFormat; + status = Loader::ResultStatus::ErrorBadPFSHeader; return; } @@ -51,7 +51,7 @@ PartitionFilesystem::PartitionFilesystem(std::shared_ptr<VfsFile> file) { const size_t total_size = file_data.size(); if (total_size != metadata_size) { - status = Loader::ResultStatus::Error; + status = Loader::ResultStatus::ErrorIncorrectPFSFileSize; return; } diff --git a/src/core/file_sys/program_metadata.cpp b/src/core/file_sys/program_metadata.cpp index 63d4b6e4f..279f987d4 100644 --- a/src/core/file_sys/program_metadata.cpp +++ b/src/core/file_sys/program_metadata.cpp @@ -12,26 +12,26 @@ namespace FileSys { Loader::ResultStatus ProgramMetadata::Load(VirtualFile file) { size_t total_size = static_cast<size_t>(file->GetSize()); if (total_size < sizeof(Header)) - return Loader::ResultStatus::Error; + return Loader::ResultStatus::ErrorBadNPDMHeader; // TODO(DarkLordZach): Use ReadObject when Header/AcidHeader becomes trivially copyable. std::vector<u8> npdm_header_data = file->ReadBytes(sizeof(Header)); if (sizeof(Header) != npdm_header_data.size()) - return Loader::ResultStatus::Error; + return Loader::ResultStatus::ErrorBadNPDMHeader; std::memcpy(&npdm_header, npdm_header_data.data(), sizeof(Header)); std::vector<u8> acid_header_data = file->ReadBytes(sizeof(AcidHeader), npdm_header.acid_offset); if (sizeof(AcidHeader) != acid_header_data.size()) - return Loader::ResultStatus::Error; + return Loader::ResultStatus::ErrorBadACIDHeader; std::memcpy(&acid_header, acid_header_data.data(), sizeof(AcidHeader)); if (sizeof(AciHeader) != file->ReadObject(&aci_header, npdm_header.aci_offset)) - return Loader::ResultStatus::Error; + return Loader::ResultStatus::ErrorBadACIHeader; if (sizeof(FileAccessControl) != file->ReadObject(&acid_file_access, acid_header.fac_offset)) - return Loader::ResultStatus::Error; + return Loader::ResultStatus::ErrorBadFileAccessControl; if (sizeof(FileAccessHeader) != file->ReadObject(&aci_file_access, aci_header.fah_offset)) - return Loader::ResultStatus::Error; + return Loader::ResultStatus::ErrorBadFileAccessHeader; return Loader::ResultStatus::Success; } |