Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
XorTroll committed Apr 12, 2024
1 parent 574b25d commit 55fd68a
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Goldleaf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ LIBS := -lcurl -lz -lmbedtls -lmbedcrypto -lmbedx509 -lnx -lusbhsfs -lntfs-3g -l
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS := $(PORTLIBS) $(LIBNX) $(CURDIR)/../Plutonium/Plutonium $(CURDIR)/../libusbhsfs
LIBDIRS := $(PORTLIBS) $(LIBNX) /run/media/xor/MDA/Nintendo/Switch/Proyectos/Plutonium/Plutonium $(CURDIR)/../libusbhsfs


#---------------------------------------------------------------------------------
Expand Down
16 changes: 14 additions & 2 deletions Goldleaf/include/base_Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ inline constexpr size_t operator ""_GB(unsigned long long n) {
#define GLEAF_PATH_REPORTS_DIR GLEAF_PATH_ROOT_DIR "/reports"
#define GLEAF_PATH_USER_DATA_DIR GLEAF_PATH_ROOT_DIR "/userdata"

#define GLEAF_DEFINE_FLAG_ENUM(enum_type, base_type) \
inline constexpr enum_type operator|(const enum_type lhs, const enum_type rhs) { \
return static_cast<const enum_type>(static_cast<const base_type>(lhs) | static_cast<const base_type>(rhs)); \
} \
inline constexpr enum_type operator&(const enum_type lhs, const enum_type rhs) { \
return static_cast<const enum_type>(static_cast<const base_type>(lhs) & static_cast<const base_type>(rhs)); \
} \
inline constexpr enum_type operator~(const enum_type enm) { \
return static_cast<const enum_type>(~static_cast<const base_type>(enm)); \
}


enum class ExecutableMode {
NSO,
NRO
Expand Down Expand Up @@ -231,8 +243,8 @@ struct Version {
};

Result Initialize();
void NORETURN Close(const Result rc);
void NORETURN Exit(const Result rc);
void NX_NORETURN Close(const Result rc);
void NX_NORETURN Exit(const Result rc);

inline ExecutableMode GetExecutableMode() {
return envIsNso() ? ExecutableMode::NSO : ExecutableMode::NRO;
Expand Down
24 changes: 20 additions & 4 deletions Goldleaf/include/hos/hos_Titles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace hos {
RSA_4096_SHA256 = 0x10003,
RSA_2048_SHA256 = 0x10004,
ECDSA_SHA256 = 0x10005,
HMAC_SHA1_160 = 0x10006
};

inline constexpr bool IsValidTicketSignature(const TicketSignature sig) {
Expand Down Expand Up @@ -147,18 +148,30 @@ namespace hos {
std::string ToString() const;
};

enum class TicketFlags : u8 {
PreInstalled = BIT(0),
Shared = BIT(1),
AllContents = BIT(2),
DeviceLinkIndependent = BIT(3),
Temporary = BIT(4)
};
GLEAF_DEFINE_FLAG_ENUM(TicketFlags, u8);

struct TicketData {
u8 issuer[0x40];
u8 title_key_block[0x100];
u8 unk[0x6];
u8 ticket_version;
u8 title_key_type;
u8 unk_1[0x2];
u8 license_type;
u8 master_key_gen;
u8 unk_2;
u8 unk_3[0x8];
TicketFlags flags;
u8 unk_2[0x8];
u8 ticket_id[0x8];
u8 device_id[0x8];
es::RightsId rights_id;
u8 account_id[0x4];
u8 unk_4[0xC];
u8 unk_3[0xC];

std::string GetTitleKey() const;
};
Expand All @@ -178,6 +191,9 @@ namespace hos {
case TicketSignature::ECDSA_SHA256: {
return 0x3C + 0x40;
}
case TicketSignature::HMAC_SHA1_160: {
return 0x14 + 0x28;
}
default: {
return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions Goldleaf/include/nfp/nfp_Amiibo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ namespace nfp {
struct CharacterId {
u16 game_character_id;
u8 character_variant;
} PACKED;
} NX_PACKED;
static_assert(sizeof(CharacterId) == 3);

struct AmiiboId {
CharacterId character_id;
u8 series;
u16 model_number;
u8 figure_type;
} PACKED;
} NX_PACKED;
static_assert(sizeof(AmiiboId) == 7);

struct AdminInfo {
Expand Down
2 changes: 2 additions & 0 deletions Goldleaf/include/nsp/nsp_Installer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ namespace nsp {
u64 base_app_id;
u64 tik_file_size;
std::string tik_file_name;
u64 cert_file_size;
std::string cert_file_name;
NcmContentInfo meta_cnt_info;
std::vector<NcmContentInfo> contents;
std::vector<InstallableProgram> programs;
Expand Down
2 changes: 1 addition & 1 deletion Goldleaf/source/base_Abort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extern "C" {

// This way, any kind of result abort by us or libnx gets saved to a simple report, and Goldleaf simply closes itself

NORETURN void diagAbortWithResult(Result rc) {
NX_NORETURN void diagAbortWithResult(Result rc) {
const auto crash_log_file = "fatal_" + hos::FormatHex(rc) + ".log";
std::string crash_data = "\nGoldleaf fatal report\n\n";
crash_data += " - Goldleaf version: " GOLDLEAF_VERSION "\n - Current time: " + hos::GetCurrentTime(false) + "\n - Fatal result: " + hos::FormatHex(rc);
Expand Down
6 changes: 3 additions & 3 deletions Goldleaf/source/base_Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ void EnsureDirectories() {
extern "C" {

void __appExit();
void NORETURN __nx_exit(Result rc, LoaderReturnFn ret_addr);
void NX_NORETURN __nx_exit(Result rc, LoaderReturnFn ret_addr);

void NORETURN __libnx_exit(Result rc) {
void NX_NORETURN __libnx_exit(Result rc) {
void __libc_fini_array(void);
__libc_fini_array();

Expand Down Expand Up @@ -247,7 +247,7 @@ Result Initialize() {
return 0;
}

void NORETURN Exit(const Result rc) {
void NX_NORETURN Exit(const Result rc) {
if(g_MainApplication) {
g_MainApplication->Close();
}
Expand Down
43 changes: 37 additions & 6 deletions Goldleaf/source/nsp/nsp_Installer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,18 @@ namespace nsp {
auto cnmt_nca_file_idx = fs::PFS0::InvalidFileIndex;
u64 cnmt_nca_file_size = 0;
auto tik_file_idx = fs::PFS0::InvalidFileIndex;
tik_file_size = 0;
this->tik_file_size = 0;
auto cert_file_idx = fs::PFS0::InvalidFileIndex;
const auto pfs0_files = pfs0_file.GetFiles();
for(u32 i = 0; i < pfs0_files.size(); i++) {
const auto file = pfs0_files.at(i);
if(fs::GetExtension(file) == "tik") {
tik_file_name = file;
this->tik_file_name = file;
tik_file_idx = i;
tik_file_size = pfs0_file.GetFileSize(i);
this->tik_file_size = pfs0_file.GetFileSize(i);
}
else if(fs::GetExtension(file) == "cert") {
cert_file_idx = i;
}
else {
if(file.length() >= __builtin_strlen("cnmt.nca")) {
Expand All @@ -207,9 +211,12 @@ namespace nsp {
nand_sys_explorer->CreateDirectory("Contents/temp");

if(tik_file_size > 0) {
auto tik_path = nand_sys_explorer->FullPathFor("Contents/temp/" + tik_file_name);
const auto tik_path = nand_sys_explorer->FullPathFor("Contents/temp/" + tik_file_name);
this->pfs0_file.SaveFile(tik_file_idx, nand_sys_explorer, tik_path);
this->tik_file = hos::ReadTicket(tik_path);

const auto cert_path = fs::GetFileName(tik_path) + ".cert";
this->pfs0_file.SaveFile(cert_file_idx, nand_sys_explorer, cert_path);
}

const auto cnmt_nca_temp_path = nand_sys_explorer->FullPathFor("Contents/temp/" + cnmt_nca_file_name);
Expand Down Expand Up @@ -353,13 +360,16 @@ namespace nsp {
std::vector<ns::ContentStorageMetaKey> content_storage_meta_keys;
if(content_meta_count > 0) {
auto cnt_storage_meta_key_buf = new ns::ContentStorageMetaKey[content_meta_count]();
ScopeGuard on_exit([&]() {
delete[] cnt_storage_meta_key_buf;
});

u32 real_count = 0;
GLEAF_RC_TRY(ns::ListApplicationRecordContentMeta(0, base_app_id, cnt_storage_meta_key_buf, content_meta_count, &real_count));
content_storage_meta_keys.reserve(real_count);
for(u32 i = 0; i < real_count; i++) {
content_storage_meta_keys.push_back(cnt_storage_meta_key_buf[i]);
}
delete[] cnt_storage_meta_key_buf;
}

const ns::ContentStorageMetaKey cnt_storage_meta_key = {
Expand All @@ -379,7 +389,28 @@ namespace nsp {
const auto tik_path = "Contents/temp/" + this->tik_file_name;
auto nand_sys_explorer = fs::GetNANDSystemExplorer();
nand_sys_explorer->ReadFile(tik_path, 0, this->tik_file.GetFullSize(), tik_buf);
GLEAF_RC_TRY(es::ImportTicket(tik_buf, this->tik_file_size, es::CommonCertificateData, es::CommonCertificateSize));

auto tik_signature = *reinterpret_cast<hos::TicketSignature*>(tik_buf);
auto tik_data = reinterpret_cast<hos::TicketData*>(tik_buf + hos::GetTicketSignatureSize(tik_signature));
if(static_cast<bool>(tik_data->flags & hos::TicketFlags::Temporary)) {
tik_data->flags = tik_data->flags & ~hos::TicketFlags::Temporary;
}

const auto cert_path = "Contents/temp/" + fs::GetFileName(this->tik_file_name) + ".cert";
if(nand_sys_explorer->IsFile(cert_path)) {
const auto cert_file_size = nand_sys_explorer->GetFileSize(cert_path);
auto cert_buf = fs::AllocateWorkBuffer(cert_file_size);
ScopeGuard on_exit([&]() {
fs::DeleteWorkBuffer(cert_buf);
});
nand_sys_explorer->ReadFile(cert_path, 0, cert_file_size, cert_buf);

GLEAF_LOG_FMT("Importing ticket with genuine cert!");
GLEAF_RC_TRY(es::ImportTicket(tik_buf, this->tik_file_size, cert_buf, cert_file_size));
}
else {
GLEAF_RC_TRY(es::ImportTicket(tik_buf, this->tik_file_size, es::CommonCertificateData, es::CommonCertificateSize));
}
}
GLEAF_RC_SUCCEED;
}
Expand Down
Loading

0 comments on commit 55fd68a

Please sign in to comment.