From ab7b9eda0ef33b59aa1bd077f28b27e10e571fc4 Mon Sep 17 00:00:00 2001 From: HookedBehemoth Date: Wed, 11 Dec 2019 00:10:49 +0100 Subject: [PATCH] fix ncz installation fix xci from sd installation --- include/install/nca.hpp | 2 +- include/nx/nca_writer.h | 1 + source/nx/nca_writer.cpp | 63 +++++++++++++++++++++------------------- source/sdInstall.cpp | 3 ++ 4 files changed, 38 insertions(+), 31 deletions(-) diff --git a/include/install/nca.hpp b/include/install/nca.hpp index 9803ea5..7a8674d 100755 --- a/include/install/nca.hpp +++ b/include/install/nca.hpp @@ -2,7 +2,7 @@ #include -#define NCA_HEADER_SIZE 0xC00 +#define NCA_HEADER_SIZE 0x4000 #define MAGIC_NCA3 0x3341434E /* "NCA3" */ namespace tin::install { diff --git a/include/nx/nca_writer.h b/include/nx/nca_writer.h index 2a0bd72..622fa47 100755 --- a/include/nx/nca_writer.h +++ b/include/nx/nca_writer.h @@ -52,6 +52,7 @@ public: bool isOpen() const; bool close(); u64 write(const u8* ptr, u64 sz); + void flushHeader(); protected: NcmContentId m_ncaId; diff --git a/source/nx/nca_writer.cpp b/source/nx/nca_writer.cpp index 8360bfd..1ce5484 100755 --- a/source/nx/nca_writer.cpp +++ b/source/nx/nca_writer.cpp @@ -372,8 +372,7 @@ bool NcaWriter::close() { if(isOpen()) { - m_contentStorage->CreatePlaceholder(m_ncaId, *(NcmPlaceHolderId*)&m_ncaId, m_buffer.size()); - m_contentStorage->WritePlaceholder(*(NcmPlaceHolderId*)&m_ncaId, 0, m_buffer.data(), m_buffer.size()); + flushHeader(); } m_buffer.resize(0); @@ -408,34 +407,7 @@ u64 NcaWriter::write(const u8* ptr, u64 sz) if (m_buffer.size() == NCA_HEADER_SIZE) { - tin::install::NcaHeader header; - memcpy(&header, m_buffer.data(), sizeof(header)); - Crypto::AesXtr decryptor(Crypto::Keys().headerKey, false); - Crypto::AesXtr encryptor(Crypto::Keys().headerKey, true); - decryptor.decrypt(&header, &header, sizeof(header), 0, 0x200); - - if (header.magic == MAGIC_NCA3) - { - if(isOpen()) - { - m_contentStorage->CreatePlaceholder(m_ncaId, *(NcmPlaceHolderId*)&m_ncaId, header.nca_size); - } - } - else - { - throw "Invalid NCA magic"; - } - - if (header.distribution == 1) - { - header.distribution = 0; - } - encryptor.encrypt(m_buffer.data(), &header, sizeof(header), 0, 0x200); - - if(isOpen()) - { - m_contentStorage->WritePlaceholder(*(NcmPlaceHolderId*)&m_ncaId, 0, m_buffer.data(), m_buffer.size()); - } + flushHeader(); } } @@ -473,3 +445,34 @@ u64 NcaWriter::write(const u8* ptr, u64 sz) return sz; } +void NcaWriter::flushHeader() +{ + tin::install::NcaHeader header; + memcpy(&header, m_buffer.data(), sizeof(header)); + Crypto::AesXtr decryptor(Crypto::Keys().headerKey, false); + Crypto::AesXtr encryptor(Crypto::Keys().headerKey, true); + decryptor.decrypt(&header, &header, sizeof(header), 0, 0x200); + + if (header.magic == MAGIC_NCA3) + { + if(isOpen()) + { + m_contentStorage->CreatePlaceholder(m_ncaId, *(NcmPlaceHolderId*)&m_ncaId, header.nca_size); + } + } + else + { + throw std::runtime_error("Invalid NCA magic"); + } + + if (header.distribution == 1) + { + header.distribution = 0; + } + encryptor.encrypt(m_buffer.data(), &header, sizeof(header), 0, 0x200); + + if(isOpen()) + { + m_contentStorage->WritePlaceholder(*(NcmPlaceHolderId*)&m_ncaId, 0, m_buffer.data(), m_buffer.size()); + } +} diff --git a/source/sdInstall.cpp b/source/sdInstall.cpp index a113035..8801e55 100755 --- a/source/sdInstall.cpp +++ b/source/sdInstall.cpp @@ -110,6 +110,9 @@ namespace nspInstStuff { if (ourTitleList[titleItr].extension() == ".xci" || ourTitleList[titleItr].extension() == ".xcz") { auto localXCI = new tin::install::xci::LocalXCI(ourTitleList[titleItr]); installTask = new tin::install::xci::XCIInstallTask(m_destStorageId, inst::config::ignoreReqVers, localXCI); + + installTask->Prepare(); + installTask->Begin(); } else { if (ourTitleList[titleItr].extension() == ".nsz") { oldNamesOfFiles.push_back(ourTitleList[titleItr]);