Skip to content

Commit 0c52833

Browse files
committed
Install NSP ticket first prior to any contents
1 parent f085611 commit 0c52833

File tree

3 files changed

+49
-48
lines changed

3 files changed

+49
-48
lines changed

Goldleaf/include/nsp/nsp_Installer.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,13 @@ namespace nsp {
7373
std::vector<NcmContentInfo> contents;
7474
std::vector<InstallableContent> inst_contents;
7575

76-
Result StartProgramInstallation(const InstallableContent &program);
77-
7876
public:
7977
Installer(const std::string &path, fs::Explorer *exp, const NcmStorageId st_id) : pfs0_file(exp, path), storage_id(st_id), contents(), inst_contents() {}
8078
~Installer();
8179

8280
Result PrepareInstallation();
83-
Result StartInstallation();
81+
Result InstallTicketCertificate();
82+
Result UpdateRecordAndContentMetas();
8483

8584
inline constexpr bool HasTicket() {
8685
return this->tik_file_size > 0;

Goldleaf/source/nsp/nsp_Installer.cpp

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ namespace nsp {
173173
}
174174

175175
Installer::~Installer() {
176-
FinalizeInstallation();
176+
this->FinalizeInstallation();
177177
}
178178

179179
Result Installer::PrepareInstallation() {
@@ -347,7 +347,49 @@ namespace nsp {
347347
GLEAF_RC_SUCCEED;
348348
}
349349

350-
Result Installer::StartInstallation() {
350+
Result Installer::InstallTicketCertificate() {
351+
if(this->tik_file_size > 0) {
352+
auto tik_buf = fs::AllocateWorkBuffer(this->tik_file_size);
353+
ScopeGuard on_exit([&]() {
354+
fs::DeleteWorkBuffer(tik_buf);
355+
});
356+
357+
const auto tik_path = GLEAF_PATH_NAND_INSTALL_TEMP_DIR "/" + this->tik_file_name;
358+
auto nand_sys_explorer = fs::GetNANDSystemExplorer();
359+
nand_sys_explorer->ReadFile(tik_path, 0, this->tik_file.GetFullSize(), tik_buf);
360+
361+
auto tik_signature = *reinterpret_cast<cnt::TicketSignature*>(tik_buf);
362+
auto tik_data = reinterpret_cast<cnt::TicketData*>(tik_buf + cnt::GetTicketSignatureSize(tik_signature));
363+
364+
// Make temporary tickets permanent
365+
if(static_cast<bool>(tik_data->flags & cnt::TicketFlags::Temporary)) {
366+
tik_data->flags = tik_data->flags & ~cnt::TicketFlags::Temporary;
367+
}
368+
369+
const auto cert_path = GLEAF_PATH_NAND_INSTALL_TEMP_DIR "/" + fs::GetFileName(this->tik_file_name) + ".cert";
370+
if(nand_sys_explorer->IsFile(cert_path)) {
371+
const auto cert_file_size = nand_sys_explorer->GetFileSize(cert_path);
372+
auto cert_buf = fs::AllocateWorkBuffer(cert_file_size);
373+
ScopeGuard on_exit([&]() {
374+
fs::DeleteWorkBuffer(cert_buf);
375+
});
376+
nand_sys_explorer->ReadFile(cert_path, 0, cert_file_size, cert_buf);
377+
378+
GLEAF_LOG_FMT("Importing ticket with cert!");
379+
GLEAF_RC_TRY(esImportTicket(tik_buf, this->tik_file_size, cert_buf, cert_file_size));
380+
}
381+
else {
382+
GLEAF_LOG_FMT("Importing ticket!");
383+
GLEAF_RC_TRY(esImportTicket(tik_buf, this->tik_file_size, es::CommonCertificateData, es::CommonCertificateSize));
384+
}
385+
386+
// We installed a ticket, so we need to refresh the ticket list for future uses
387+
cnt::NotifyTicketsChanged();
388+
}
389+
GLEAF_RC_SUCCEED;
390+
}
391+
392+
Result Installer::UpdateRecordAndContentMetas() {
351393
const auto &main_program = this->inst_contents.front();
352394
const auto base_app_id = cnt::GetBaseApplicationId(main_program.meta_key.id, static_cast<NcmContentMetaType>(main_program.meta_key.type));
353395

@@ -388,44 +430,6 @@ namespace nsp {
388430
nsextDeleteApplicationRecord(base_app_id);
389431
GLEAF_RC_TRY(nsextPushApplicationRecord(base_app_id, NsExtApplicationEvent_Present, content_storage_meta_keys.data(), content_storage_meta_keys.size()));
390432

391-
if(this->tik_file_size > 0) {
392-
auto tik_buf = fs::AllocateWorkBuffer(this->tik_file_size);
393-
ScopeGuard on_exit([&]() {
394-
fs::DeleteWorkBuffer(tik_buf);
395-
});
396-
397-
const auto tik_path = GLEAF_PATH_NAND_INSTALL_TEMP_DIR "/" + this->tik_file_name;
398-
auto nand_sys_explorer = fs::GetNANDSystemExplorer();
399-
nand_sys_explorer->ReadFile(tik_path, 0, this->tik_file.GetFullSize(), tik_buf);
400-
401-
auto tik_signature = *reinterpret_cast<cnt::TicketSignature*>(tik_buf);
402-
auto tik_data = reinterpret_cast<cnt::TicketData*>(tik_buf + cnt::GetTicketSignatureSize(tik_signature));
403-
404-
// Make temporary tickets permanent
405-
if(static_cast<bool>(tik_data->flags & cnt::TicketFlags::Temporary)) {
406-
tik_data->flags = tik_data->flags & ~cnt::TicketFlags::Temporary;
407-
}
408-
409-
const auto cert_path = GLEAF_PATH_NAND_INSTALL_TEMP_DIR "/" + fs::GetFileName(this->tik_file_name) + ".cert";
410-
if(nand_sys_explorer->IsFile(cert_path)) {
411-
const auto cert_file_size = nand_sys_explorer->GetFileSize(cert_path);
412-
auto cert_buf = fs::AllocateWorkBuffer(cert_file_size);
413-
ScopeGuard on_exit([&]() {
414-
fs::DeleteWorkBuffer(cert_buf);
415-
});
416-
nand_sys_explorer->ReadFile(cert_path, 0, cert_file_size, cert_buf);
417-
418-
GLEAF_LOG_FMT("Importing ticket with cert!");
419-
GLEAF_RC_TRY(esImportTicket(tik_buf, this->tik_file_size, cert_buf, cert_file_size));
420-
}
421-
else {
422-
GLEAF_LOG_FMT("Importing ticket!");
423-
GLEAF_RC_TRY(esImportTicket(tik_buf, this->tik_file_size, es::CommonCertificateData, es::CommonCertificateSize));
424-
}
425-
426-
// We installed a ticket, so we need to refresh the ticket list for future uses
427-
cnt::NotifyTicketsChanged();
428-
}
429433
GLEAF_RC_SUCCEED;
430434
}
431435

Goldleaf/source/ui/ui_InstallLayout.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,11 @@ namespace ui {
355355
}
356356

357357
if(do_install) {
358-
/*
359-
rc = nsp_installer.StartInstallation();
358+
rc = nsp_installer.InstallTicketCertificate();
360359
if(R_FAILED(rc)) {
361-
HandleInstallationFailure(rc, nsp_installer);
360+
// Nothing has been installed yet, so no need to rollback
362361
return;
363362
}
364-
*/
365363

366364
hos::LockExit();
367365

@@ -448,7 +446,7 @@ namespace ui {
448446
g_MainApplication->CallForRender();
449447
});
450448

451-
rc = nsp_installer.StartInstallation();
449+
rc = nsp_installer.UpdateRecordAndContentMetas();
452450
if(R_FAILED(rc)) {
453451
HandleInstallationFailure(rc, nsp_installer);
454452
return;

0 commit comments

Comments
 (0)