allow installation of multi-purpose NSP's

fix a few memory leaks
remove some legacy tinfoil code for installation from sd
This commit is contained in:
HookedBehemoth 2019-12-17 00:15:40 +01:00
parent 88be93d706
commit ac7da23cef
24 changed files with 248 additions and 795 deletions

View file

@ -55,9 +55,9 @@ ARCH := -march=armv8-a+crc+crypto -mtune=cortex-a57 -mtp=soft -fPIE
CFLAGS := -g -Wall -O2 -ffunction-sections \
$(ARCH) $(DEFINES)
CFLAGS += $(INCLUDE) -D__SWITCH__ -Wall #-D__DEBUG__ -DNXLINK_DEBUG
CFLAGS += $(INCLUDE) -D__SWITCH__ -Wall -Werror -D__DEBUG__ -DNXLINK_DEBUG
CXXFLAGS := $(CFLAGS) -fno-rtti -std=gnu++17 -Wall
CXXFLAGS := $(CFLAGS) -fno-rtti -std=gnu++17 -Wall -Werror
ASFLAGS := -g $(ARCH)

View file

@ -22,12 +22,12 @@ SOFTWARE.
#pragma once
#include "install/remote_nsp.hpp"
#include "install/nsp.hpp"
#include <memory>
namespace tin::install::nsp
{
class HTTPNSP : public RemoteNSP
class HTTPNSP : public NSP
{
public:
tin::network::HTTPDownload m_download;

View file

@ -44,12 +44,11 @@ namespace tin::install
protected:
const NcmStorageId m_destStorageId;
bool m_ignoreReqFirmVersion = false;
bool declinedValidation = false;
bool m_declinedValidation = false;
std::vector<nx::ncm::ContentMeta> m_contentMeta;
Install(NcmStorageId destStorageId, bool ignoreReqFirmVersion);
virtual ~Install();
virtual std::vector<std::tuple<nx::ncm::ContentMeta, NcmContentInfo>> ReadCNMT() = 0;
@ -59,6 +58,8 @@ namespace tin::install
virtual void InstallNCA(const NcmContentId &ncaId) = 0;
public:
virtual ~Install();
virtual void Prepare();
virtual void Begin();

View file

@ -23,17 +23,16 @@ SOFTWARE.
#pragma once
#include <switch.h>
#include <string>
#include "install/install.hpp"
#include "install/simple_filesystem.hpp"
#include "nx/content_meta.hpp"
#include "nx/ipc/tin_ipc.h"
#include "install/nsp.hpp"
namespace tin::install::nsp
{
class NSPInstallTask : public Install
class NSPInstall : public Install
{
private:
tin::install::nsp::SimpleFileSystem* const m_simpleFileSystem;
const std::shared_ptr<NSP> m_NSP;
protected:
std::vector<std::tuple<nx::ncm::ContentMeta, NcmContentInfo>> ReadCNMT() override;
@ -41,7 +40,6 @@ namespace tin::install::nsp
void InstallTicketCert() override;
public:
NSPInstallTask(tin::install::nsp::SimpleFileSystem& simpleFileSystem, NcmStorageId destStorageId, bool ignoreReqFirmVersion);
NSPInstall(NcmStorageId destStorageId, bool ignoreReqFirmVersion, const std::shared_ptr<NSP>& remoteNSP);
};
};
}

View file

@ -1,52 +0,0 @@
/*
Copyright (c) 2017-2018 Adubbz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <switch/services/fs.h>
typedef enum
{
InstallSourceType_None,
InstallSourceType_Extracted,
InstallSourceType_Nsp,
} InstallSourceType;
typedef struct
{
InstallSourceType sourceType;
char path[FS_MAX_PATH];
} InstallContext;
typedef struct
{
char name[FS_MAX_PATH];
size_t size;
size_t nspAbsoluteOffset;
FsFileSystem fsFileSystem;
FsFile fsFile;
} InstallFile;
Result openInstallFile(InstallFile *out, InstallContext *context, const char *name);
Result openInstallFileWithExt(InstallFile *out, InstallContext *context, const char *ext);
Result readInstallFile(InstallContext *context, InstallFile *file, size_t off, void *buff, size_t len);
void closeInstallFile(InstallContext *context, InstallFile *file);

View file

@ -33,7 +33,7 @@ namespace tin::install::xci
class XCIInstallTask : public Install
{
private:
tin::install::xci::XCI* const m_xci;
const std::shared_ptr<tin::install::xci::XCI> m_xci;
protected:
std::vector<std::tuple<nx::ncm::ContentMeta, NcmContentInfo>> ReadCNMT() override;
@ -41,7 +41,7 @@ namespace tin::install::xci
void InstallTicketCert() override;
public:
XCIInstallTask(NcmStorageId destStorageId, bool ignoreReqFirmVersion, tin::install::xci::XCI* xci);
XCIInstallTask(NcmStorageId destStorageId, bool ignoreReqFirmVersion, const std::shared_ptr<XCI>& xci);
};
};

View file

@ -32,12 +32,12 @@ SOFTWARE.
namespace tin::install::nsp
{
class RemoteNSP
class NSP
{
protected:
std::vector<u8> m_headerBytes;
RemoteNSP();
NSP();
public:
virtual void StreamToPlaceholder(std::shared_ptr<nx::ncm::ContentStorage>& contentStorage, NcmContentId placeholderId) = 0;
@ -50,7 +50,7 @@ namespace tin::install::nsp
virtual const PFS0FileEntry* GetFileEntry(unsigned int index);
virtual const PFS0FileEntry* GetFileEntryByName(std::string name);
virtual const PFS0FileEntry* GetFileEntryByNcaId(const NcmContentId& ncaId);
virtual const PFS0FileEntry* GetFileEntryByExtension(std::string extension);
virtual std::vector<const PFS0FileEntry*> GetFileEntriesByExtension(std::string extension);
virtual const char* GetFileEntryName(const PFS0FileEntry* fileEntry);
};

View file

@ -22,24 +22,19 @@ SOFTWARE.
#pragma once
#include <switch.h>
#include <string>
#include "install/install.hpp"
#include "install/remote_nsp.hpp"
#include "install/nsp.hpp"
namespace tin::install::nsp
{
class RemoteNSPInstall : public Install
class SDMCNSP : public NSP
{
private:
RemoteNSP* m_remoteNSP;
protected:
std::vector<std::tuple<nx::ncm::ContentMeta, NcmContentInfo>> ReadCNMT() override;
void InstallNCA(const NcmContentId& ncaId) override;
void InstallTicketCert() override;
public:
RemoteNSPInstall(NcmStorageId destStorageId, bool ignoreReqFirmVersion, RemoteNSP* remoteNSP);
SDMCNSP(std::string path);
~SDMCNSP();
virtual void StreamToPlaceholder(std::shared_ptr<nx::ncm::ContentStorage>& contentStorage, NcmContentId ncaId) override;
virtual void BufferData(void* buf, off_t offset, size_t size) override;
private:
FILE* m_nspFile;
};
}

View file

@ -26,11 +26,11 @@ SOFTWARE.
namespace tin::install::xci
{
class LocalXCI : public XCI
class SDMCXCI : public XCI
{
public:
LocalXCI(std::string path);
~LocalXCI();
SDMCXCI(std::string path);
~SDMCXCI();
virtual void StreamToPlaceholder(std::shared_ptr<nx::ncm::ContentStorage>& contentStorage, NcmContentId ncaId) override;
virtual void BufferData(void* buf, off_t offset, size_t size) override;

View file

@ -23,11 +23,11 @@ SOFTWARE.
#pragma once
#include <string>
#include "install/remote_nsp.hpp"
#include "install/nsp.hpp"
namespace tin::install::nsp
{
class USBNSP : public RemoteNSP
class USBNSP : public NSP
{
private:
std::string m_nspName;

View file

@ -1,55 +0,0 @@
/*
Copyright (c) 2017-2018 Adubbz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#ifdef __VERIFY_NSP__
#include <string>
namespace tin::install
{
enum class VerificationErrorType
{
CRITICAL, WARNING
};
enum class VerificationLevel
{
ERROR_ALL, ERROR_CRITICAL_WARN, ERROR_CRITICAL_IGNORE_WARN, WARN_ALL
};
class NSPVerifier
{
private:
std::string m_nspPath;
public:
NSPVerifier(std::string nspPath);
bool PerformVerification();
void PrintCritical(std::string text);
void PrintWarning(std::string text);
void PrintSuccess(std::string text);
};
}
#endif

View file

@ -22,24 +22,18 @@ SOFTWARE.
#include "install/install_nsp.hpp"
#include <cstdlib>
#include <cstring>
#include <stdexcept>
#include <memory>
#include <string>
#include <machine/endian.h>
#include <thread>
#include "nx/ncm.hpp"
#include "install/nca.hpp"
#include "nx/fs.hpp"
#include "nx/ncm.hpp"
#include "util/config.hpp"
#include "util/crypto.hpp"
#include "nx/nca_writer.h"
#include "util/debug.h"
#include "util/error.hpp"
#include "util/file_util.hpp"
#include "util/title_util.hpp"
#include "sdInstall.hpp"
#include "util/debug.h"
#include "util/error.hpp"
#include "ui/MainApplication.hpp"
#include "util/util.hpp"
@ -49,64 +43,49 @@ namespace inst::ui {
namespace tin::install::nsp
{
NSPInstallTask::NSPInstallTask(tin::install::nsp::SimpleFileSystem& simpleFileSystem, NcmStorageId destStorageId, bool ignoreReqFirmVersion) :
Install(destStorageId, ignoreReqFirmVersion), m_simpleFileSystem(&simpleFileSystem)
NSPInstall::NSPInstall(NcmStorageId destStorageId, bool ignoreReqFirmVersion, const std::shared_ptr<NSP>& remoteNSP) :
Install(destStorageId, ignoreReqFirmVersion), m_NSP(remoteNSP)
{
m_NSP->RetrieveHeader();
}
std::vector<std::tuple<nx::ncm::ContentMeta, NcmContentInfo>> NSPInstallTask::ReadCNMT()
std::vector<std::tuple<nx::ncm::ContentMeta, NcmContentInfo>> NSPInstall::ReadCNMT()
{
NcmContentInfo cnmtRecord = tin::util::CreateNSPCNMTContentRecord(this->m_simpleFileSystem->m_absoluteRootPath.substr(0, this->m_simpleFileSystem->m_absoluteRootPath.size() - 1));
std::vector<std::tuple<nx::ncm::ContentMeta, NcmContentInfo>> CNMTList;
for (const PFS0FileEntry* fileEntry : m_NSP->GetFileEntriesByExtension("cnmt.nca")) {
std::string cnmtNcaName(m_NSP->GetFileEntryName(fileEntry));
NcmContentId cnmtContentId = tin::util::GetNcaIdFromString(cnmtNcaName);
size_t cnmtNcaSize = fileEntry->fileSize;
nx::ncm::ContentStorage contentStorage(m_destStorageId);
this->InstallNCA(cnmtRecord.content_id);
std::string cnmtNCAFullPath = contentStorage.GetPath(cnmtRecord.content_id);
return { { tin::util::GetContentMetaFromNCA(cnmtNCAFullPath), cnmtRecord } };
LOG_DEBUG("CNMT Name: %s\n", cnmtNcaName.c_str());
// We install the cnmt nca early to read from it later
this->InstallNCA(cnmtContentId);
std::string cnmtNCAFullPath = contentStorage.GetPath(cnmtContentId);
NcmContentInfo cnmtContentInfo;
cnmtContentInfo.content_id = cnmtContentId;
*(u64*)&cnmtContentInfo.size = cnmtNcaSize & 0xFFFFFFFFFFFF;
cnmtContentInfo.content_type = NcmContentType_Meta;
CNMTList.push_back( { tin::util::GetContentMetaFromNCA(cnmtNCAFullPath), cnmtContentInfo } );
}
void NSPInstallTask::InstallTicketCert()
{
// Read the tik file and put it into a buffer
auto tikName = m_simpleFileSystem->GetFileNameFromExtension("", "tik");
LOG_DEBUG("> Getting tik size\n");
auto tikFile = m_simpleFileSystem->OpenFile(tikName);
u64 tikSize = tikFile.GetSize();
auto tikBuf = std::make_unique<u8[]>(tikSize);
LOG_DEBUG("> Reading tik\n");
tikFile.Read(0x0, tikBuf.get(), tikSize);
// Read the cert file and put it into a buffer
auto certName = m_simpleFileSystem->GetFileNameFromExtension("", "cert");
LOG_DEBUG("> Getting cert size\n");
auto certFile = m_simpleFileSystem->OpenFile(certName);
u64 certSize = certFile.GetSize();
auto certBuf = std::make_unique<u8[]>(certSize);
LOG_DEBUG("> Reading cert\n");
certFile.Read(0x0, certBuf.get(), certSize);
// Finally, let's actually import the ticket
ASSERT_OK(esImportTicket(tikBuf.get(), tikSize, certBuf.get(), certSize), "Failed to import ticket");
return CNMTList;
}
void NSPInstallTask::InstallNCA(const NcmContentId &ncaId)
void NSPInstall::InstallNCA(const NcmContentId& ncaId)
{
std::string ncaName = tin::util::GetNcaIdString(ncaId);
const PFS0FileEntry* fileEntry = m_NSP->GetFileEntryByNcaId(ncaId);
std::string ncaFileName = m_NSP->GetFileEntryName(fileEntry);
if (m_simpleFileSystem->HasFile(ncaName + ".nca"))
ncaName += ".nca";
else if (m_simpleFileSystem->HasFile(ncaName + ".cnmt.nca"))
ncaName += ".cnmt.nca";
else if (m_simpleFileSystem->HasFile(ncaName + ".ncz"))
ncaName += ".ncz";
else if (m_simpleFileSystem->HasFile(ncaName + ".cnmt.ncz"))
ncaName += ".cnmt.ncz";
else
{
THROW_FORMAT(("Failed to find NCA file " + ncaName + ".nca/.cnmt.nca/.ncz/.cnmt.ncz").c_str());
}
LOG_DEBUG("NcaId: %s\n", ncaName.c_str());
LOG_DEBUG("Dest storage Id: %u\n", m_destStorageId);
#ifdef NXLINK_DEBUG
size_t ncaSize = fileEntry->fileSize;
LOG_DEBUG("Installing %s to storage Id %u\n", ncaFileName.c_str(), m_destStorageId);
#endif
std::shared_ptr<nx::ncm::ContentStorage> contentStorage(new nx::ncm::ContentStorage(m_destStorageId));
@ -117,87 +96,42 @@ namespace tin::install::nsp
}
catch (...) {}
auto ncaFile = m_simpleFileSystem->OpenFile(ncaName);
LOG_DEBUG("Size: 0x%lx\n", ncaSize);
if (inst::config::validateNCAs && !declinedValidation)
if (inst::config::validateNCAs && !m_declinedValidation)
{
tin::install::NcaHeader header;
ncaFile.Read(0, &header, 0xc00);
Crypto::AesXtr decryptor(Crypto::Keys().headerKey, false);
decryptor.decrypt(&header, &header, sizeof(header), 0, 0x200);
tin::install::NcaHeader* header = new NcaHeader;
m_NSP->BufferData(header, m_NSP->GetDataOffset() + fileEntry->dataOffset, sizeof(tin::install::NcaHeader));
if (header.magic != MAGIC_NCA3)
Crypto::AesXtr crypto(Crypto::Keys().headerKey, false);
crypto.decrypt(header, header, sizeof(tin::install::NcaHeader), 0, 0x200);
if (header->magic != MAGIC_NCA3)
THROW_FORMAT("Invalid NCA magic");
if (!Crypto::rsa2048PssVerify(&header.magic, 0x200, header.fixed_key_sig, Crypto::NCAHeaderSignature))
if (!Crypto::rsa2048PssVerify(&header->magic, 0x200, header->fixed_key_sig, Crypto::NCAHeaderSignature))
{
std::thread audioThread(inst::util::playAudio,"romfs:/audio/bark.wav");
int rc = inst::ui::mainApp->CreateShowDialog("Invalid NCA signature detected!", "Improperly signed software should only be installed from trustworthy\nsources. Files containing cartridge repacks and DLC unlockers will always\nshow this warning. You can disable this check in Awoo Installer's settings.\n\nAre you sure you want to continue the installation?", {"Cancel", "Yes, I understand the risks"}, false);
audioThread.join();
if (rc != 1)
THROW_FORMAT(("The requested NCA (" + tin::util::GetNcaIdString(ncaId) + ") is not properly signed").c_str());
declinedValidation = true;
} else {
LOG_DEBUG("NCA signature is valid\n")
m_declinedValidation = true;
}
delete header;
}
size_t ncaSize = ncaFile.GetSize();
u64 fileOff = 0;
size_t readSize = 0x400000; // 4MB buff
auto readBuffer = std::make_unique<u8[]>(readSize);
if (readBuffer == NULL)
THROW_FORMAT(("Failed to allocate read buffer for " + ncaName).c_str());
LOG_DEBUG("Size: 0x%lx\n", ncaSize);
NcaWriter writer(ncaId, contentStorage);
float progress;
bool failed = false;
try
{
inst::ui::setInstInfoText("Installing " + ncaName + "...");
inst::ui::setInstBarPerc(0);
while (fileOff < ncaSize)
{
progress = (float)fileOff / (float)ncaSize;
if (fileOff % (0x400000 * 3) == 0) {
inst::ui::setInstBarPerc((double)(progress * 100.0));
}
if (fileOff + readSize >= ncaSize) readSize = ncaSize - fileOff;
ncaFile.Read(fileOff, readBuffer.get(), readSize);
writer.write(readBuffer.get(), readSize);
fileOff += readSize;
}
inst::ui::setInstBarPerc(100);
}
catch (...)
{
failed = true;
}
writer.close();
m_NSP->StreamToPlaceholder(contentStorage, ncaId);
LOG_DEBUG("Registering placeholder...\n");
if (!failed)
{
try
{
contentStorage->Register(*(NcmPlaceHolderId*)&ncaId, ncaId);
}
catch (...)
{
LOG_DEBUG(("Failed to register " + ncaName + ". It may already exist.\n").c_str());
}
LOG_DEBUG(("Failed to register " + ncaFileName + ". It may already exist.\n").c_str());
}
try
@ -206,4 +140,38 @@ namespace tin::install::nsp
}
catch (...) {}
}
void NSPInstall::InstallTicketCert()
{
// Read the tik files and put it into a buffer
std::vector<const PFS0FileEntry*> tikFileEntries = m_NSP->GetFileEntriesByExtension("tik");
std::vector<const PFS0FileEntry*> certFileEntries = m_NSP->GetFileEntriesByExtension("cert");
for (size_t i = 0; i < tikFileEntries.size(); i++)
{
if (tikFileEntries[i] == nullptr) {
LOG_DEBUG("Remote tik file is missing.\n");
THROW_FORMAT("Remote tik file is not present!");
}
u64 tikSize = tikFileEntries[i]->fileSize;
auto tikBuf = std::make_unique<u8[]>(tikSize);
LOG_DEBUG("> Reading tik\n");
m_NSP->BufferData(tikBuf.get(), m_NSP->GetDataOffset() + tikFileEntries[i]->dataOffset, tikSize);
if (certFileEntries[i] == nullptr)
{
LOG_DEBUG("Remote cert file is missing.\n");
THROW_FORMAT("Remote cert file is not present!");
}
u64 certSize = certFileEntries[i]->fileSize;
auto certBuf = std::make_unique<u8[]>(certSize);
LOG_DEBUG("> Reading cert\n");
m_NSP->BufferData(certBuf.get(), m_NSP->GetDataOffset() + certFileEntries[i]->dataOffset, certSize);
// Finally, let's actually import the ticket
ASSERT_OK(esImportTicket(tikBuf.get(), tikSize, certBuf.get(), certSize), "Failed to import ticket");
}
}
}

View file

@ -1,175 +0,0 @@
/*
Copyright (c) 2017-2018 Adubbz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "install/install_nsp_remote.hpp"
#include <machine/endian.h>
#include <thread>
#include "install/nca.hpp"
#include "nx/fs.hpp"
#include "nx/ncm.hpp"
#include "util/config.hpp"
#include "util/crypto.hpp"
#include "util/file_util.hpp"
#include "util/title_util.hpp"
#include "util/debug.h"
#include "util/error.hpp"
#include "ui/MainApplication.hpp"
#include "util/util.hpp"
namespace inst::ui {
extern MainApplication *mainApp;
}
namespace tin::install::nsp
{
RemoteNSPInstall::RemoteNSPInstall(NcmStorageId destStorageId, bool ignoreReqFirmVersion, RemoteNSP* remoteNSP) :
Install(destStorageId, ignoreReqFirmVersion), m_remoteNSP(remoteNSP)
{
m_remoteNSP->RetrieveHeader();
}
std::vector<std::tuple<nx::ncm::ContentMeta, NcmContentInfo>> RemoteNSPInstall::ReadCNMT()
{
const PFS0FileEntry* fileEntry = m_remoteNSP->GetFileEntryByExtension("cnmt.nca");
if (fileEntry == nullptr)
THROW_FORMAT("Failed to find cnmt file entry!\n");
std::string cnmtNcaName(m_remoteNSP->GetFileEntryName(fileEntry));
NcmContentId cnmtContentId = tin::util::GetNcaIdFromString(cnmtNcaName);
size_t cnmtNcaSize = fileEntry->fileSize;
nx::ncm::ContentStorage contentStorage(m_destStorageId);
LOG_DEBUG("CNMT Name: %s\n", cnmtNcaName.c_str());
// We install the cnmt nca early to read from it later
this->InstallNCA(cnmtContentId);
std::string cnmtNCAFullPath = contentStorage.GetPath(cnmtContentId);
NcmContentInfo cnmtContentInfo;
cnmtContentInfo.content_id = cnmtContentId;
*(u64*)&cnmtContentInfo.size = cnmtNcaSize & 0xFFFFFFFFFFFF;
cnmtContentInfo.content_type = NcmContentType_Meta;
return { { tin::util::GetContentMetaFromNCA(cnmtNCAFullPath), cnmtContentInfo } };
}
void RemoteNSPInstall::InstallNCA(const NcmContentId& ncaId)
{
const PFS0FileEntry* fileEntry = m_remoteNSP->GetFileEntryByNcaId(ncaId);
std::string ncaFileName = m_remoteNSP->GetFileEntryName(fileEntry);
#ifdef NXLINK_DEBUG
size_t ncaSize = fileEntry->fileSize;
LOG_DEBUG("Installing %s to storage Id %u\n", ncaFileName.c_str(), m_destStorageId);
#endif
std::shared_ptr<nx::ncm::ContentStorage> contentStorage(new nx::ncm::ContentStorage(m_destStorageId));
// Attempt to delete any leftover placeholders
try
{
contentStorage->DeletePlaceholder(*(NcmPlaceHolderId*)&ncaId);
}
catch (...) {}
LOG_DEBUG("Size: 0x%lx\n", ncaSize);
if (inst::config::validateNCAs && !declinedValidation)
{
tin::install::NcaHeader* header = new NcaHeader;
m_remoteNSP->BufferData(header, m_remoteNSP->GetDataOffset() + fileEntry->dataOffset, sizeof(tin::install::NcaHeader));
Crypto::AesXtr crypto(Crypto::Keys().headerKey, false);
crypto.decrypt(header, header, sizeof(tin::install::NcaHeader), 0, 0x200);
if (header->magic != MAGIC_NCA3)
THROW_FORMAT("Invalid NCA magic");
if (!Crypto::rsa2048PssVerify(&header->magic, 0x200, header->fixed_key_sig, Crypto::NCAHeaderSignature))
{
std::thread audioThread(inst::util::playAudio,"romfs:/audio/bark.wav");
int rc = inst::ui::mainApp->CreateShowDialog("Invalid NCA signature detected!", "Improperly signed software should only be installed from trustworthy\nsources. Files containing cartridge repacks and DLC unlockers will always\nshow this warning. You can disable this check in Awoo Installer's settings.\n\nAre you sure you want to continue the installation?", {"Cancel", "Yes, I understand the risks"}, false);
audioThread.join();
if (rc != 1)
THROW_FORMAT(("The requested NCA (" + tin::util::GetNcaIdString(ncaId) + ") is not properly signed").c_str());
declinedValidation = true;
}
}
m_remoteNSP->StreamToPlaceholder(contentStorage, ncaId);
LOG_DEBUG("Registering placeholder...\n");
try
{
contentStorage->Register(*(NcmPlaceHolderId*)&ncaId, ncaId);
}
catch (...)
{
LOG_DEBUG(("Failed to register " + ncaFileName + ". It may already exist.\n").c_str());
}
try
{
contentStorage->DeletePlaceholder(*(NcmPlaceHolderId*)&ncaId);
}
catch (...) {}
}
void RemoteNSPInstall::InstallTicketCert()
{
// Read the tik file and put it into a buffer
const PFS0FileEntry* tikFileEntry = m_remoteNSP->GetFileEntryByExtension("tik");
if (tikFileEntry == nullptr)
{
LOG_DEBUG("Remote tik file is missing.\n");
THROW_FORMAT("Remote tik file is not present!");
}
u64 tikSize = tikFileEntry->fileSize;
auto tikBuf = std::make_unique<u8[]>(tikSize);
LOG_DEBUG("> Reading tik\n");
m_remoteNSP->BufferData(tikBuf.get(), m_remoteNSP->GetDataOffset() + tikFileEntry->dataOffset, tikSize);
// Read the cert file and put it into a buffer
const PFS0FileEntry* certFileEntry = m_remoteNSP->GetFileEntryByExtension("cert");
if (certFileEntry == nullptr)
{
LOG_DEBUG("Remote cert file is missing.\n");
THROW_FORMAT("Remote cert file is not present!");
}
u64 certSize = certFileEntry->fileSize;
auto certBuf = std::make_unique<u8[]>(certSize);
LOG_DEBUG("> Reading cert\n");
m_remoteNSP->BufferData(certBuf.get(), m_remoteNSP->GetDataOffset() + certFileEntry->dataOffset, certSize);
// Finally, let's actually import the ticket
ASSERT_OK(esImportTicket(tikBuf.get(), tikSize, certBuf.get(), certSize), "Failed to import ticket");
}
}

View file

@ -40,7 +40,7 @@ namespace inst::ui {
namespace tin::install::xci
{
XCIInstallTask::XCIInstallTask(NcmStorageId destStorageId, bool ignoreReqFirmVersion, tin::install::xci::XCI* xci) :
XCIInstallTask::XCIInstallTask(NcmStorageId destStorageId, bool ignoreReqFirmVersion, const std::shared_ptr<XCI>& xci) :
Install(destStorageId, ignoreReqFirmVersion), m_xci(xci)
{
m_xci->RetrieveHeader();
@ -51,9 +51,6 @@ namespace tin::install::xci
std::vector<std::tuple<nx::ncm::ContentMeta, NcmContentInfo>> CNMTList;
for (const HFS0FileEntry* fileEntry : m_xci->GetFileEntriesByExtension("cnmt.nca")) {
if (fileEntry == nullptr)
THROW_FORMAT("Failed to find cnmt file entry!\n");
std::string cnmtNcaName(m_xci->GetFileEntryName(fileEntry));
NcmContentId cnmtContentId = tin::util::GetNcaIdFromString(cnmtNcaName);
size_t cnmtNcaSize = fileEntry->fileSize;
@ -98,7 +95,7 @@ namespace tin::install::xci
LOG_DEBUG("Size: 0x%lx\n", ncaSize);
if (inst::config::validateNCAs && !declinedValidation)
if (inst::config::validateNCAs && !m_declinedValidation)
{
tin::install::NcaHeader* header = new NcaHeader;
m_xci->BufferData(header, m_xci->GetDataOffset() + fileEntry->dataOffset, sizeof(tin::install::NcaHeader));
@ -116,8 +113,9 @@ namespace tin::install::xci
audioThread.join();
if (rc != 1)
THROW_FORMAT(("The requested NCA (" + tin::util::GetNcaIdString(ncaId) + ") is not properly signed").c_str());
declinedValidation = true;
m_declinedValidation = true;
}
delete header;
}
m_xci->StreamToPlaceholder(contentStorage, ncaId);

View file

@ -20,7 +20,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "install/remote_nsp.hpp"
#include "install/nsp.hpp"
#include <threads.h>
#include "data/buffered_placeholder_writer.hpp"
@ -30,13 +30,10 @@ SOFTWARE.
namespace tin::install::nsp
{
RemoteNSP::RemoteNSP()
{
}
NSP::NSP() {}
// TODO: Do verification: PFS0 magic, sizes not zero
void RemoteNSP::RetrieveHeader()
void NSP::RetrieveHeader()
{
LOG_DEBUG("Retrieving remote NSP header...\n");
@ -56,7 +53,7 @@ namespace tin::install::nsp
printBytes(m_headerBytes.data(), m_headerBytes.size(), true);
}
const PFS0FileEntry* RemoteNSP::GetFileEntry(unsigned int index)
const PFS0FileEntry* NSP::GetFileEntry(unsigned int index)
{
if (index >= this->GetBaseHeader()->numFiles)
THROW_FORMAT("File entry index is out of bounds\n")
@ -69,8 +66,10 @@ namespace tin::install::nsp
return reinterpret_cast<PFS0FileEntry*>(m_headerBytes.data() + fileEntryOffset);
}
const PFS0FileEntry* RemoteNSP::GetFileEntryByExtension(std::string extension)
std::vector<const PFS0FileEntry*> NSP::GetFileEntriesByExtension(std::string extension)
{
std::vector<const PFS0FileEntry*> entryList;
for (unsigned int i = 0; i < this->GetBaseHeader()->numFiles; i++)
{
const PFS0FileEntry* fileEntry = this->GetFileEntry(i);
@ -78,13 +77,13 @@ namespace tin::install::nsp
auto foundExtension = name.substr(name.find(".") + 1);
if (foundExtension == extension)
return fileEntry;
entryList.push_back(fileEntry);
}
return nullptr;
return entryList;
}
const PFS0FileEntry* RemoteNSP::GetFileEntryByName(std::string name)
const PFS0FileEntry* NSP::GetFileEntryByName(std::string name)
{
for (unsigned int i = 0; i < this->GetBaseHeader()->numFiles; i++)
{
@ -98,7 +97,7 @@ namespace tin::install::nsp
return nullptr;
}
const PFS0FileEntry* RemoteNSP::GetFileEntryByNcaId(const NcmContentId& ncaId)
const PFS0FileEntry* NSP::GetFileEntryByNcaId(const NcmContentId& ncaId)
{
const PFS0FileEntry* fileEntry = nullptr;
std::string ncaIdStr = tin::util::GetNcaIdString(ncaId);
@ -120,13 +119,13 @@ namespace tin::install::nsp
return fileEntry;
}
const char* RemoteNSP::GetFileEntryName(const PFS0FileEntry* fileEntry)
const char* NSP::GetFileEntryName(const PFS0FileEntry* fileEntry)
{
u64 stringTableStart = sizeof(PFS0BaseHeader) + this->GetBaseHeader()->numFiles * sizeof(PFS0FileEntry);
return reinterpret_cast<const char*>(m_headerBytes.data() + stringTableStart + fileEntry->stringTableOffset);
}
const PFS0BaseHeader* RemoteNSP::GetBaseHeader()
const PFS0BaseHeader* NSP::GetBaseHeader()
{
if (m_headerBytes.empty())
THROW_FORMAT("Cannot retrieve header as header bytes are empty. Have you retrieved it yet?\n");
@ -134,7 +133,7 @@ namespace tin::install::nsp
return reinterpret_cast<PFS0BaseHeader*>(m_headerBytes.data());
}
u64 RemoteNSP::GetDataOffset()
u64 NSP::GetDataOffset()
{
if (m_headerBytes.empty())
THROW_FORMAT("Cannot get data offset as header is empty. Have you retrieved it yet?\n");

View file

@ -0,0 +1,73 @@
#include "install/sdmc_nsp.hpp"
#include "error.hpp"
#include "debug.h"
#include "nx/nca_writer.h"
#include "sdInstall.hpp"
namespace tin::install::nsp
{
SDMCNSP::SDMCNSP(std::string path)
{
m_nspFile = fopen((path).c_str(), "rb");
if (!m_nspFile)
THROW_FORMAT("can't open file at %s\n", path.c_str());
}
SDMCNSP::~SDMCNSP()
{
fclose(m_nspFile);
}
void SDMCNSP::StreamToPlaceholder(std::shared_ptr<nx::ncm::ContentStorage>& contentStorage, NcmContentId ncaId)
{
const PFS0FileEntry* fileEntry = this->GetFileEntryByNcaId(ncaId);
std::string ncaFileName = this->GetFileEntryName(fileEntry);
LOG_DEBUG("Retrieving %s\n", ncaFileName.c_str());
size_t ncaSize = fileEntry->fileSize;
NcaWriter writer(ncaId, contentStorage);
float progress;
u64 fileStart = GetDataOffset() + fileEntry->dataOffset;
u64 fileOff = 0;
size_t readSize = 0x400000; // 4MB buff
auto readBuffer = std::make_unique<u8[]>(readSize);
try
{
inst::ui::setInstInfoText("Installing " + ncaFileName + "...");
inst::ui::setInstBarPerc(0);
while (fileOff < ncaSize)
{
progress = (float) fileOff / (float) ncaSize;
if (fileOff % (0x400000 * 3) == 0) {
LOG_DEBUG("> Progress: %lu/%lu MB (%d%s)\r", (fileOff / 1000000), (ncaSize / 1000000), (int)(progress * 100.0), "%");
inst::ui::setInstBarPerc((double)(progress * 100.0));
}
if (fileOff + readSize >= ncaSize) readSize = ncaSize - fileOff;
this->BufferData(readBuffer.get(), fileOff + fileStart, readSize);
writer.write(readBuffer.get(), readSize);
fileOff += readSize;
}
inst::ui::setInstBarPerc(100);
}
catch (std::exception& e)
{
LOG_DEBUG("something went wrong: %s\n", e.what());
}
writer.close();
}
void SDMCNSP::BufferData(void* buf, off_t offset, size_t size)
{
fseeko(m_nspFile, offset, SEEK_SET);
fread(buf, 1, size, m_nspFile);
}
}

View file

@ -1,4 +1,4 @@
#include "install/local_xci.hpp"
#include "install/sdmc_xci.hpp"
#include "error.hpp"
#include "debug.h"
#include "nx/nca_writer.h"
@ -6,19 +6,19 @@
namespace tin::install::xci
{
LocalXCI::LocalXCI(std::string path)
SDMCXCI::SDMCXCI(std::string path)
{
m_xciFile = fopen((path).c_str(), "rb");
if (!m_xciFile)
THROW_FORMAT("can't open file at %s\n", path.c_str());
}
LocalXCI::~LocalXCI()
SDMCXCI::~SDMCXCI()
{
fclose(m_xciFile);
}
void LocalXCI::StreamToPlaceholder(std::shared_ptr<nx::ncm::ContentStorage>& contentStorage, NcmContentId ncaId)
void SDMCXCI::StreamToPlaceholder(std::shared_ptr<nx::ncm::ContentStorage>& contentStorage, NcmContentId ncaId)
{
const HFS0FileEntry* fileEntry = this->GetFileEntryByNcaId(ncaId);
std::string ncaFileName = this->GetFileEntryName(fileEntry);
@ -65,7 +65,7 @@ namespace tin::install::xci
writer.close();
}
void LocalXCI::BufferData(void* buf, off_t offset, size_t size)
void SDMCXCI::BufferData(void* buf, off_t offset, size_t size)
{
fseeko(m_xciFile, offset, SEEK_SET);
fread(buf, 1, size, m_xciFile);

View file

@ -1,205 +0,0 @@
/*
Copyright (c) 2017-2018 Adubbz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "install/verify_nsp.hpp"
#include <exception>
#include <string>
#include <tuple>
#include <switch.h>
#include "install/simple_filesystem.hpp"
#include "install/verify_nsp.hpp"
#include "nx/fs.hpp"
#include "nx/ncm.hpp"
#include "util/file_util.hpp"
#include "util/title_util.hpp"
#ifdef __VERIFY_NSP__
namespace tin::install
{
NSPVerifier::NSPVerifier(std::string nspPath) :
m_nspPath(nspPath)
{
}
bool NSPVerifier::PerformVerification()
{
bool isComplete = true;
nx::fs::IFileSystem nspFileSystem;
try
{
nspFileSystem.OpenFileSystemWithId(m_nspPath, FsFileSystemType_ApplicationPackage, 0);
}
catch (std::exception& e)
{
this->PrintCritical("NSP is invalid and cannot be opened! Error: " + std::string(e.what()));
LOG_DEBUG("> NSP Path: %s\n", m_nspPath.c_str());
return false;
}
this->PrintSuccess("PFS0 structure is valid");
tin::install::nsp::SimpleFileSystem simpleFS(nspFileSystem, "/", m_nspPath + "/");
std::string cnmtNCAPath;
NcmContentInfo cnmtContentRecord;
nx::ncm::ContentMeta contentMeta;
try
{
std::tie(cnmtNCAPath, cnmtContentRecord) = tin::util::GetCNMTNCAInfo(m_nspPath);
}
catch (std::exception& e)
{
this->PrintCritical("Failed to find CNMT NCA. Error: " + std::string(e.what()));
return false;
}
try
{
contentMeta = tin::util::GetContentMetaFromNCA(cnmtNCAPath);
}
catch (std::exception& e)
{
this->PrintCritical("Content meta could not be read. Error: " + std::string(e.what()));
return false;
}
this->PrintSuccess("Successfully read content meta.");
if (!simpleFS.HasFile(tin::util::GetNcaIdString(cnmtContentRecord.content_id) + ".cnmt.xml"))
this->PrintWarning("CNMT XML is absent!");
else
this->PrintSuccess("CNMT XML is present.");
auto contentMetaHeader = contentMeta.GetContentMetaHeader();
u64 titleId = tin::util::GetBaseTitleId(contentMetaHeader.titleId, contentMetaHeader.type);
for (NcmContentInfo contentRecord : contentMeta.GetContentRecords())
{
std::string ncaIdStr = tin::util::GetNcaIdString(contentRecord.content_id);
std::string ncaName = ncaIdStr + ".nca";
std::string xmlName = ncaIdStr + ".";
if (simpleFS.HasFile(ncaName))
this->PrintSuccess(ncaName + " is present.");
else
{
this->PrintCritical(ncaName + " is missing!");
return false;
}
// control, legalinfo
switch (contentRecord.contentType)
{
case NcmContentType_Program:
xmlName += "programinfo.xml";
break;
case NcmContentType_Data:
xmlName = "";
break;
case NcmContentType_Control:
try
{
nx::fs::IFileSystem controlFileSystem;
controlFileSystem.OpenFileSystemWithId(m_nspPath + "/" + ncaName, FsFileSystemType_ContentControl, titleId);
}
catch (std::exception& e)
{
this->PrintCritical("Control NCA could not be read. Error: " + std::string(e.what()));
return false;
}
this->PrintSuccess("Control NCA is valid.");
xmlName += "nacp.xml";
break;
case NcmContentType_HtmlDocument:
xmlName += "htmldocument.xml";
break;
case NcmContentType_LegalInformation:
try
{
nx::fs::IFileSystem legalInfoFileSystem;
legalInfoFileSystem.OpenFileSystemWithId(m_nspPath + "/" + ncaName, FsFileSystemType_ContentManual, titleId);
}
catch (std::exception& e)
{
this->PrintCritical("Legal information NCA could not be read. Error: " + std::string(e.what()));
return false;
}
this->PrintSuccess("Legal information NCA is valid.");
xmlName += "legalinfo.xml";
break;
// We ignore delta fragments (for now) since they aren't all included,
// and we don't install them
case NcmContentType_DeltaFragment:
continue;
default:
this->PrintCritical("Unrecognized content type!");
return false;
}
if (xmlName == "")
continue;
if (simpleFS.HasFile(xmlName))
this->PrintSuccess(xmlName + " is present.");
else
{
this->PrintWarning(xmlName + " is missing!");
isComplete = false;
}
}
if (isComplete)
this->PrintSuccess("NSP is valid.");
else
this->PrintWarning("NSP is installable, but incomplete.");
return true;
}
void NSPVerifier::PrintCritical(std::string text)
{
LOG_DEBUG("[%sCRITICAL%s] %s\n", CONSOLE_RED, CONSOLE_RESET, text.c_str());
}
void NSPVerifier::PrintWarning(std::string text)
{
LOG_DEBUG("[%sWARNING%s] %s\n", CONSOLE_YELLOW, CONSOLE_RESET, text.c_str());
}
void NSPVerifier::PrintSuccess(std::string text)
{
LOG_DEBUG("[%sOK%s] %s\n", CONSOLE_GREEN, CONSOLE_RESET, text.c_str());
}
}
#endif

View file

@ -30,7 +30,7 @@ SOFTWARE.
#include <switch.h>
#include "util/network_util.hpp"
#include "install/install_nsp_remote.hpp"
#include "install/install_nsp.hpp"
#include "install/http_nsp.hpp"
#include "install/install_xci.hpp"
#include "install/http_xci.hpp"
@ -146,22 +146,20 @@ namespace netInstStuff{
for (urlItr = 0; urlItr < ourUrlList.size(); urlItr++) {
LOG_DEBUG("%s %s\n", "Install request from", ourUrlList[urlItr].c_str());
inst::ui::setTopInstInfoText("Installing " + urlNames[urlItr] + ourSource);
tin::install::Install* installTask;
std::unique_ptr<tin::install::Install> installTask;
if (inst::curl::downloadToBuffer(ourUrlList[urlItr], 0x100, 0x103) == "HEAD") {
auto httpXCI = new tin::install::xci::HTTPXCI(ourUrlList[urlItr]);
installTask = new tin::install::xci::XCIInstallTask(m_destStorageId, inst::config::ignoreReqVers, httpXCI);
auto httpXCI = std::make_shared<tin::install::xci::HTTPXCI>(ourUrlList[urlItr]);
installTask = std::make_unique<tin::install::xci::XCIInstallTask>(m_destStorageId, inst::config::ignoreReqVers, httpXCI);
} else {
auto httpNSP = new tin::install::nsp::HTTPNSP(ourUrlList[urlItr]);
installTask = new tin::install::nsp::RemoteNSPInstall(m_destStorageId, inst::config::ignoreReqVers, httpNSP);
auto httpNSP = std::make_shared<tin::install::nsp::HTTPNSP>(ourUrlList[urlItr]);
installTask = std::make_unique<tin::install::nsp::NSPInstall>(m_destStorageId, inst::config::ignoreReqVers, httpNSP);
}
LOG_DEBUG("%s\n", "Preparing installation");
inst::ui::setInstInfoText("Preparing installation...");
inst::ui::setInstBarPerc(0);
installTask->Prepare();
installTask->Begin();
}
}

View file

@ -25,10 +25,12 @@ SOFTWARE.
#include <filesystem>
#include <ctime>
#include <thread>
#include <memory>
#include "install/install_nsp.hpp"
#include "install/install_xci.hpp"
#include "install/local_xci.hpp"
#include "install/sdmc_xci.hpp"
#include "install/sdmc_nsp.hpp"
#include "nx/fs.hpp"
#include "util/file_util.hpp"
#include "util/title_util.hpp"
@ -104,39 +106,23 @@ namespace nspInstStuff {
{
for (titleItr = 0; titleItr < ourTitleList.size(); titleItr++) {
inst::ui::setTopInstInfoText("Installing " + inst::util::shortenString(ourTitleList[titleItr].filename().string(), 40, true) + " from SD card");
tin::install::Install* installTask;
std::unique_ptr<tin::install::Install> installTask;
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);
inst::ui::setInstInfoText("Preparing installation...");
inst::ui::setInstBarPerc(0);
installTask->Prepare();
installTask->Begin();
auto sdmcXCI = std::make_shared<tin::install::xci::SDMCXCI>(ourTitleList[titleItr]);
installTask = std::make_unique<tin::install::xci::XCIInstallTask>(m_destStorageId, inst::config::ignoreReqVers, sdmcXCI);
} else {
if (ourTitleList[titleItr].extension() == ".nsz") {
oldNamesOfFiles.push_back(ourTitleList[titleItr]);
std::string newfilename = ourTitleList[titleItr].string().substr(0, ourTitleList[titleItr].string().find_last_of('.'))+"_temp.nsp";
rename(ourTitleList[titleItr], newfilename);
filesToBeRenamed.push_back(newfilename);
ourTitleList[titleItr] = newfilename;
auto sdmcNSP = std::make_shared<tin::install::nsp::SDMCNSP>(ourTitleList[titleItr]);
installTask = std::make_unique<tin::install::nsp::NSPInstall>(m_destStorageId, inst::config::ignoreReqVers, sdmcNSP);
}
std::string path = "@Sdcard://" + ourTitleList[titleItr].string().erase(0, 6);
nx::fs::IFileSystem fileSystem;
fileSystem.OpenFileSystemWithId(path, FsFileSystemType_ApplicationPackage, 0);
tin::install::nsp::SimpleFileSystem simpleFS(fileSystem, "/", path + "/");
installTask = new tin::install::nsp::NSPInstallTask(simpleFS, m_destStorageId, inst::config::ignoreReqVers);
LOG_DEBUG("%s\n", "Preparing installation");
inst::ui::setInstInfoText("Preparing installation...");
inst::ui::setInstBarPerc(0);
installTask->Prepare();
installTask->Begin();
}
}
}
catch (std::exception& e)
{
LOG_DEBUG("Failed to install");

View file

@ -3,7 +3,7 @@
#include "util/error.hpp"
#include "usbInstall.hpp"
#include "install/usb_nsp.hpp"
#include "install/install_nsp_remote.hpp"
#include "install/install_nsp.hpp"
#include "util/usb_util.hpp"
#include "util/util.hpp"
#include "util/config.hpp"
@ -85,15 +85,14 @@ namespace usbInstStuff {
try {
for (fileItr = 0; fileItr < ourTitleList.size(); fileItr++) {
inst::ui::setTopInstInfoText("Installing " + fileNames[fileItr] + " over USB");
tin::install::Install* installTask;
std::unique_ptr<tin::install::Install> installTask;
if (ourTitleList[fileItr].compare(ourTitleList[fileItr].size() - 3, 2, "xc") == 0) {
auto usbXCI = new tin::install::xci::USBXCI(ourTitleList[fileItr]);
installTask = new tin::install::xci::XCIInstallTask(m_destStorageId, inst::config::ignoreReqVers, usbXCI);
auto usbXCI = std::make_shared<tin::install::xci::USBXCI>(ourTitleList[fileItr]);
installTask = std::make_unique<tin::install::xci::XCIInstallTask>(m_destStorageId, inst::config::ignoreReqVers, usbXCI);
} else {
auto usbNSP = new tin::install::nsp::USBNSP(ourTitleList[fileItr]);
installTask = new tin::install::nsp::RemoteNSPInstall(m_destStorageId, inst::config::ignoreReqVers, usbNSP);
auto usbNSP = std::make_shared<tin::install::nsp::USBNSP>(ourTitleList[fileItr]);
installTask = std::make_unique<tin::install::nsp::NSPInstall>(m_destStorageId, inst::config::ignoreReqVers, usbNSP);
}
LOG_DEBUG("%s\n", "Preparing installation");

View file

@ -31,44 +31,7 @@ SOFTWARE.
namespace tin::util
{
NcmContentInfo CreateNSPCNMTContentRecord(const std::string& nspPath)
{
// Open filesystem
nx::fs::IFileSystem fileSystem;
std::string nspExt = ".nsp";
std::string nszExt = ".nsz";
std::string rootPath = "/";
std::string absolutePath = nspPath + "/";
// Check if this is an nsp file
if (nspPath.compare(nspPath.size() - nspExt.size(), nspExt.size(), nspExt) == 0 || nspPath.compare(nspPath.size() - nszExt.size(), nszExt.size(), nszExt) == 0)
{
fileSystem.OpenFileSystemWithId(nspPath, FsFileSystemType_ApplicationPackage, 0);
}
else // Otherwise assume this is an extracted NSP directory
{
fileSystem.OpenSdFileSystem();
rootPath = nspPath.substr(9) + "/";
absolutePath = nspPath + "/";
}
tin::install::nsp::SimpleFileSystem simpleFS(fileSystem, rootPath, absolutePath);
// Create the path of the cnmt NCA
auto cnmtNCAName = simpleFS.GetFileNameFromExtension("", "cnmt.nca");
auto cnmtNCAFile = simpleFS.OpenFile(cnmtNCAName);
u64 cnmtNCASize = cnmtNCAFile.GetSize();
// Prepare cnmt content record
NcmContentInfo contentRecord;
contentRecord.content_id = tin::util::GetNcaIdFromString(cnmtNCAName);
*(u64*)contentRecord.size = cnmtNCASize & 0xFFFFFFFFFFFF;
contentRecord.content_type = NcmContentType_Meta;
return contentRecord;
}
// NOTE: As of 7.0.0, this will only work with installed cnmt nca paths
// TODO: do this manually so we don't have to "install" the cnmt's
nx::ncm::ContentMeta GetContentMetaFromNCA(const std::string& ncaPath)
{
// Create the cnmt filesystem
@ -87,42 +50,4 @@ namespace tin::util
return nx::ncm::ContentMeta(cnmtBuf.GetData(), cnmtBuf.GetSize());
}
std::vector<std::string> GetNSPList()
{
std::vector<std::string> nspList;
nx::fs::IFileSystem fileSystem;
fileSystem.OpenSdFileSystem();
nx::fs::IDirectory dir = fileSystem.OpenDirectory("/tinfoil/nsp/", FsDirOpenMode_ReadFiles);
u64 entryCount = dir.GetEntryCount();
auto dirEntries = std::make_unique<FsDirectoryEntry[]>(entryCount);
dir.Read(0, dirEntries.get(), entryCount);
for (unsigned int i = 0; i < entryCount; i++)
{
FsDirectoryEntry dirEntry = dirEntries[i];
std::string dirEntryName(dirEntry.name);
std::string nspExt = ".nsp";
std::string nszExt = ".nsz";
std::string xciExt = ".xci";
std::string xczExt = ".xcz";
if (dirEntry.type != FsDirEntryType_File ||
(
dirEntryName.compare(dirEntryName.size() - nspExt.size(), nspExt.size(), nspExt) != 0 &&
dirEntryName.compare(dirEntryName.size() - nszExt.size(), nszExt.size(), nszExt) != 0 &&
dirEntryName.compare(dirEntryName.size() - xciExt.size(), xciExt.size(), xciExt) != 0 &&
dirEntryName.compare(dirEntryName.size() - xczExt.size(), xczExt.size(), xczExt) != 0
)
)
continue;
nspList.push_back(dirEntry.name);
}
return nspList;
}
}