Reorganize code

This commit is contained in:
Pengfei
2021-08-09 16:34:03 +08:00
parent b331f50b91
commit 9fbdf6f529
6 changed files with 306 additions and 321 deletions
+192 -193
View File
@@ -42,218 +42,217 @@ bool NCCHContainer::Load() {
if (is_loaded) if (is_loaded)
return true; return true;
if (file->IsOpen()) { if (!file->IsOpen()) {
// Reset read pointer in case this file has been read before. LOG_WARNING(Service_FS, "Failed to open");
file->Seek(0, SEEK_SET); return false;
}
if (file->ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header)) { // Reset read pointer in case this file has been read before.
LOG_ERROR(Service_FS, "Could not read from file"); file->Seek(0, SEEK_SET);
return false;
}
// Verify we are loading the correct file type... if (file->ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header)) {
if (MakeMagic('N', 'C', 'C', 'H') != ncch_header.magic) { LOG_ERROR(Service_FS, "Could not read from file");
LOG_ERROR(Service_FS, "Invalid magic, file may be corrupted"); return false;
return false; }
}
bool failed_to_decrypt = false; // Verify we are loading the correct file type...
if (!ncch_header.no_crypto) { if (MakeMagic('N', 'C', 'C', 'H') != ncch_header.magic) {
is_encrypted = true; LOG_ERROR(Service_FS, "Invalid magic, file may be corrupted");
return false;
}
// Find primary and secondary keys bool failed_to_decrypt = false;
if (ncch_header.fixed_key) { if (!ncch_header.no_crypto) {
LOG_DEBUG(Service_FS, "Fixed-key crypto"); is_encrypted = true;
primary_key.fill(0);
secondary_key.fill(0); // Find primary and secondary keys
if (ncch_header.fixed_key) {
LOG_DEBUG(Service_FS, "Fixed-key crypto");
primary_key.fill(0);
secondary_key.fill(0);
} else {
std::array<u8, 16> key_y_primary, key_y_secondary;
std::copy(ncch_header.signature, ncch_header.signature + key_y_primary.size(),
key_y_primary.begin());
if (!ncch_header.seed_crypto) {
key_y_secondary = key_y_primary;
} else { } else {
std::array<u8, 16> key_y_primary, key_y_secondary; auto opt{Seeds::GetSeed(ncch_header.program_id)};
if (!opt.has_value()) {
std::copy(ncch_header.signature, ncch_header.signature + key_y_primary.size(), LOG_ERROR(Service_FS, "Seed for program {:016X} not found",
key_y_primary.begin()); ncch_header.program_id);
if (!ncch_header.seed_crypto) {
key_y_secondary = key_y_primary;
} else {
auto opt{Seeds::GetSeed(ncch_header.program_id)};
if (!opt.has_value()) {
LOG_ERROR(Service_FS, "Seed for program {:016X} not found",
ncch_header.program_id);
failed_to_decrypt = true;
} else {
auto seed{*opt};
std::array<u8, 32> input;
std::memcpy(input.data(), key_y_primary.data(), key_y_primary.size());
std::memcpy(input.data() + key_y_primary.size(), seed.data(), seed.size());
CryptoPP::SHA256 sha;
std::array<u8, CryptoPP::SHA256::DIGESTSIZE> hash;
sha.CalculateDigest(hash.data(), input.data(), input.size());
std::memcpy(key_y_secondary.data(), hash.data(), key_y_secondary.size());
}
}
Key::SetKeyY(Key::NCCHSecure1, key_y_primary);
if (!Key::IsNormalKeyAvailable(Key::NCCHSecure1)) {
LOG_ERROR(Service_FS, "Secure1 KeyX missing");
failed_to_decrypt = true; failed_to_decrypt = true;
} } else {
primary_key = Key::GetNormalKey(Key::NCCHSecure1); auto seed{*opt};
std::array<u8, 32> input;
const auto SetSecondaryKey = [this, &failed_to_decrypt, std::memcpy(input.data(), key_y_primary.data(), key_y_primary.size());
&key_y_secondary](Key::KeySlotID slot) { std::memcpy(input.data() + key_y_primary.size(), seed.data(), seed.size());
Key::SetKeyY(slot, key_y_secondary); CryptoPP::SHA256 sha;
if (!Key::IsNormalKeyAvailable(slot)) { std::array<u8, CryptoPP::SHA256::DIGESTSIZE> hash;
LOG_ERROR(Service_FS, "{:#04X} KeyX missing", slot); sha.CalculateDigest(hash.data(), input.data(), input.size());
failed_to_decrypt = true; std::memcpy(key_y_secondary.data(), hash.data(), key_y_secondary.size());
}
secondary_key = Key::GetNormalKey(slot);
};
switch (ncch_header.secondary_key_slot) {
case 0:
LOG_DEBUG(Service_FS, "Secure1 crypto");
SetSecondaryKey(Key::NCCHSecure1);
break;
case 1:
LOG_DEBUG(Service_FS, "Secure2 crypto");
SetSecondaryKey(Key::NCCHSecure2);
break;
case 10:
LOG_DEBUG(Service_FS, "Secure3 crypto");
SetSecondaryKey(Key::NCCHSecure3);
break;
case 11:
LOG_DEBUG(Service_FS, "Secure4 crypto");
SetSecondaryKey(Key::NCCHSecure4);
break;
} }
} }
// Find CTR for each section Key::SetKeyY(Key::NCCHSecure1, key_y_primary);
// Written with reference to if (!Key::IsNormalKeyAvailable(Key::NCCHSecure1)) {
// https://github.com/d0k3/GodMode9/blob/99af6a73be48fa7872649aaa7456136da0df7938/arm9/source/game/ncch.c#L34-L52 LOG_ERROR(Service_FS, "Secure1 KeyX missing");
if (ncch_header.version == 0 || ncch_header.version == 2) {
LOG_DEBUG(Loader, "NCCH version 0/2");
// In this version, CTR for each section is a magic number prefixed by partition ID
// (reverse order)
std::reverse_copy(ncch_header.partition_id, ncch_header.partition_id + 8,
exheader_ctr.begin());
exefs_ctr = romfs_ctr = exheader_ctr;
exheader_ctr[8] = 1;
exefs_ctr[8] = 2;
romfs_ctr[8] = 3;
} else if (ncch_header.version == 1) {
LOG_DEBUG(Loader, "NCCH version 1");
// In this version, CTR for each section is the section offset prefixed by partition
// ID, as if the entire NCCH image is encrypted using a single CTR stream.
std::copy(ncch_header.partition_id, ncch_header.partition_id + 8,
exheader_ctr.begin());
exefs_ctr = romfs_ctr = exheader_ctr;
auto u32ToBEArray = [](u32 value) -> std::array<u8, 4> {
return std::array<u8, 4>{
static_cast<u8>(value >> 24),
static_cast<u8>((value >> 16) & 0xFF),
static_cast<u8>((value >> 8) & 0xFF),
static_cast<u8>(value & 0xFF),
};
};
auto offset_exheader = u32ToBEArray(0x200); // exheader offset
auto offset_exefs = u32ToBEArray(ncch_header.exefs_offset * kBlockSize);
auto offset_romfs = u32ToBEArray(ncch_header.romfs_offset * kBlockSize);
std::copy(offset_exheader.begin(), offset_exheader.end(),
exheader_ctr.begin() + 12);
std::copy(offset_exefs.begin(), offset_exefs.end(), exefs_ctr.begin() + 12);
std::copy(offset_romfs.begin(), offset_romfs.end(), romfs_ctr.begin() + 12);
} else {
LOG_ERROR(Service_FS, "Unknown NCCH version {}", ncch_header.version);
failed_to_decrypt = true; failed_to_decrypt = true;
} }
} else { primary_key = Key::GetNormalKey(Key::NCCHSecure1);
LOG_DEBUG(Service_FS, "No crypto");
is_encrypted = false;
}
// System archives and DLC don't have an extended header but have RomFS const auto SetSecondaryKey = [this, &failed_to_decrypt,
if (ncch_header.extended_header_size) { &key_y_secondary](Key::KeySlotID slot) {
if (file->ReadBytes(&exheader_header, sizeof(exheader_header)) != Key::SetKeyY(slot, key_y_secondary);
sizeof(exheader_header)) { if (!Key::IsNormalKeyAvailable(slot)) {
LOG_ERROR(Service_FS, "Could not read exheader from file"); LOG_ERROR(Service_FS, "{:#04X} KeyX missing", slot);
return false; failed_to_decrypt = true;
}
if (is_encrypted) {
// This ID check is masked to low 32-bit as a toleration to ill-formed ROM created
// by merging games and its updates.
if ((exheader_header.system_info.jump_id & 0xFFFFFFFF) ==
(ncch_header.program_id & 0xFFFFFFFF)) {
LOG_WARNING(Service_FS, "NCCH is marked as encrypted but with decrypted "
"exheader. Force no crypto scheme.");
is_encrypted = false;
} else {
if (failed_to_decrypt) {
LOG_ERROR(Service_FS, "Failed to decrypt");
return false;
}
CryptoPP::byte* data = reinterpret_cast<CryptoPP::byte*>(&exheader_header);
CryptoPP::CTR_Mode<CryptoPP::AES>::Decryption(
primary_key.data(), primary_key.size(), exheader_ctr.data())
.ProcessData(data, data, sizeof(exheader_header));
} }
secondary_key = Key::GetNormalKey(slot);
};
switch (ncch_header.secondary_key_slot) {
case 0:
LOG_DEBUG(Service_FS, "Secure1 crypto");
SetSecondaryKey(Key::NCCHSecure1);
break;
case 1:
LOG_DEBUG(Service_FS, "Secure2 crypto");
SetSecondaryKey(Key::NCCHSecure2);
break;
case 10:
LOG_DEBUG(Service_FS, "Secure3 crypto");
SetSecondaryKey(Key::NCCHSecure3);
break;
case 11:
LOG_DEBUG(Service_FS, "Secure4 crypto");
SetSecondaryKey(Key::NCCHSecure4);
break;
} }
u32 entry_point = exheader_header.codeset_info.text.address;
u32 code_size = exheader_header.codeset_info.text.code_size;
u32 stack_size = exheader_header.codeset_info.stack_size;
u32 bss_size = exheader_header.codeset_info.bss_size;
u32 core_version = exheader_header.arm11_system_local_caps.core_version;
u8 priority = exheader_header.arm11_system_local_caps.priority;
u8 resource_limit_category =
exheader_header.arm11_system_local_caps.resource_limit_category;
LOG_DEBUG(Service_FS, "Name: {}",
exheader_header.codeset_info.name);
LOG_DEBUG(Service_FS, "Program ID: {:016X}", ncch_header.program_id);
LOG_DEBUG(Service_FS, "Entry point: 0x{:08X}", entry_point);
LOG_DEBUG(Service_FS, "Code size: 0x{:08X}", code_size);
LOG_DEBUG(Service_FS, "Stack size: 0x{:08X}", stack_size);
LOG_DEBUG(Service_FS, "Bss size: 0x{:08X}", bss_size);
LOG_DEBUG(Service_FS, "Core version: {}", core_version);
LOG_DEBUG(Service_FS, "Thread priority: 0x{:X}", priority);
LOG_DEBUG(Service_FS, "Resource limit category: {}", resource_limit_category);
LOG_DEBUG(Service_FS, "System Mode: {}",
static_cast<int>(exheader_header.arm11_system_local_caps.system_mode));
has_exheader = true;
} }
// DLC can have an ExeFS and a RomFS but no extended header // Find CTR for each section
if (ncch_header.exefs_size) { // Written with reference to
exefs_offset = ncch_header.exefs_offset * kBlockSize; // https://github.com/d0k3/GodMode9/blob/99af6a73be48fa7872649aaa7456136da0df7938/arm9/source/game/ncch.c#L34-L52
u32 exefs_size = ncch_header.exefs_size * kBlockSize; if (ncch_header.version == 0 || ncch_header.version == 2) {
LOG_DEBUG(Loader, "NCCH version 0/2");
LOG_DEBUG(Service_FS, "ExeFS offset: 0x{:08X}", exefs_offset); // In this version, CTR for each section is a magic number prefixed by partition ID
LOG_DEBUG(Service_FS, "ExeFS size: 0x{:08X}", exefs_size); // (reverse order)
file->Seek(exefs_offset, SEEK_SET); std::reverse_copy(ncch_header.partition_id, ncch_header.partition_id + 8,
if (file->ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header)) { exheader_ctr.begin());
LOG_ERROR(Service_FS, "Could not read ExeFS header from file"); exefs_ctr = romfs_ctr = exheader_ctr;
return false; exheader_ctr[8] = 1;
} exefs_ctr[8] = 2;
romfs_ctr[8] = 3;
if (is_encrypted) { } else if (ncch_header.version == 1) {
CryptoPP::byte* data = reinterpret_cast<CryptoPP::byte*>(&exefs_header); LOG_DEBUG(Loader, "NCCH version 1");
CryptoPP::CTR_Mode<CryptoPP::AES>::Decryption(primary_key.data(), // In this version, CTR for each section is the section offset prefixed by partition
primary_key.size(), exefs_ctr.data()) // ID, as if the entire NCCH image is encrypted using a single CTR stream.
.ProcessData(data, data, sizeof(exefs_header)); std::copy(ncch_header.partition_id, ncch_header.partition_id + 8, exheader_ctr.begin());
} exefs_ctr = romfs_ctr = exheader_ctr;
auto u32ToBEArray = [](u32 value) -> std::array<u8, 4> {
exefs_file = file; return std::array<u8, 4>{
has_exefs = true; static_cast<u8>(value >> 24),
static_cast<u8>((value >> 16) & 0xFF),
static_cast<u8>((value >> 8) & 0xFF),
static_cast<u8>(value & 0xFF),
};
};
auto offset_exheader = u32ToBEArray(0x200); // exheader offset
auto offset_exefs = u32ToBEArray(ncch_header.exefs_offset * kBlockSize);
auto offset_romfs = u32ToBEArray(ncch_header.romfs_offset * kBlockSize);
std::copy(offset_exheader.begin(), offset_exheader.end(), exheader_ctr.begin() + 12);
std::copy(offset_exefs.begin(), offset_exefs.end(), exefs_ctr.begin() + 12);
std::copy(offset_romfs.begin(), offset_romfs.end(), romfs_ctr.begin() + 12);
} else {
LOG_ERROR(Service_FS, "Unknown NCCH version {}", ncch_header.version);
failed_to_decrypt = true;
} }
} else {
if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) LOG_DEBUG(Service_FS, "No crypto");
has_romfs = true; is_encrypted = false;
} }
// System archives and DLC don't have an extended header but have RomFS
if (ncch_header.extended_header_size) {
if (file->ReadBytes(&exheader_header, sizeof(exheader_header)) != sizeof(exheader_header)) {
LOG_ERROR(Service_FS, "Could not read exheader from file");
return false;
}
if (is_encrypted) {
// This ID check is masked to low 32-bit as a toleration to ill-formed ROM created
// by merging games and its updates.
if ((exheader_header.system_info.jump_id & 0xFFFFFFFF) ==
(ncch_header.program_id & 0xFFFFFFFF)) {
LOG_WARNING(Service_FS, "NCCH is marked as encrypted but with decrypted "
"exheader. Force no crypto scheme.");
is_encrypted = false;
} else {
if (failed_to_decrypt) {
LOG_ERROR(Service_FS, "Failed to decrypt");
return false;
}
CryptoPP::byte* data = reinterpret_cast<CryptoPP::byte*>(&exheader_header);
CryptoPP::CTR_Mode<CryptoPP::AES>::Decryption(
primary_key.data(), primary_key.size(), exheader_ctr.data())
.ProcessData(data, data, sizeof(exheader_header));
}
}
u32 entry_point = exheader_header.codeset_info.text.address;
u32 code_size = exheader_header.codeset_info.text.code_size;
u32 stack_size = exheader_header.codeset_info.stack_size;
u32 bss_size = exheader_header.codeset_info.bss_size;
u32 core_version = exheader_header.arm11_system_local_caps.core_version;
u8 priority = exheader_header.arm11_system_local_caps.priority;
u8 resource_limit_category =
exheader_header.arm11_system_local_caps.resource_limit_category;
LOG_DEBUG(Service_FS, "Name: {}", exheader_header.codeset_info.name);
LOG_DEBUG(Service_FS, "Program ID: {:016X}", ncch_header.program_id);
LOG_DEBUG(Service_FS, "Entry point: 0x{:08X}", entry_point);
LOG_DEBUG(Service_FS, "Code size: 0x{:08X}", code_size);
LOG_DEBUG(Service_FS, "Stack size: 0x{:08X}", stack_size);
LOG_DEBUG(Service_FS, "Bss size: 0x{:08X}", bss_size);
LOG_DEBUG(Service_FS, "Core version: {}", core_version);
LOG_DEBUG(Service_FS, "Thread priority: 0x{:X}", priority);
LOG_DEBUG(Service_FS, "Resource limit category: {}", resource_limit_category);
LOG_DEBUG(Service_FS, "System Mode: {}",
static_cast<int>(exheader_header.arm11_system_local_caps.system_mode));
has_exheader = true;
}
// DLC can have an ExeFS and a RomFS but no extended header
if (ncch_header.exefs_size) {
exefs_offset = ncch_header.exefs_offset * kBlockSize;
u32 exefs_size = ncch_header.exefs_size * kBlockSize;
LOG_DEBUG(Service_FS, "ExeFS offset: 0x{:08X}", exefs_offset);
LOG_DEBUG(Service_FS, "ExeFS size: 0x{:08X}", exefs_size);
file->Seek(exefs_offset, SEEK_SET);
if (file->ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header)) {
LOG_ERROR(Service_FS, "Could not read ExeFS header from file");
return false;
}
if (is_encrypted) {
CryptoPP::byte* data = reinterpret_cast<CryptoPP::byte*>(&exefs_header);
CryptoPP::CTR_Mode<CryptoPP::AES>::Decryption(primary_key.data(), primary_key.size(),
exefs_ctr.data())
.ProcessData(data, data, sizeof(exefs_header));
}
exefs_file = file;
has_exefs = true;
}
if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0)
has_romfs = true;
is_loaded = true; is_loaded = true;
return true; return true;
} }
+35 -57
View File
@@ -535,10 +535,29 @@ bool SDMCImporter::LoadTMD(const ContentSpecifier& specifier, TitleMetadata& out
return LoadTMD(specifier.type, specifier.id, out); return LoadTMD(specifier.type, specifier.id, out);
} }
std::shared_ptr<FileUtil::IOFile> SDMCImporter::OpenContent(const ContentSpecifier& specifier,
u32 content_id) const {
if (IsNandTitle(specifier.type)) {
const auto path =
fmt::format("{}{:08x}/{:08x}/content/{:08x}.app", config.system_titles_path,
(specifier.id >> 32), (specifier.id & 0xFFFFFFFF), content_id);
return std::make_shared<FileUtil::IOFile>(path, "rb");
} else {
// For DLCs, there one subfolder every 256 titles, but in practice hardcoded 00000000
// should be fine (also matches GodMode9 behaviour)
const auto format_str = specifier.type == ContentType::DLC
? "/title/{:08x}/{:08x}/content/00000000/{:08x}.app"
: "/title/{:08x}/{:08x}/content/{:08x}.app";
const auto path =
fmt::format(format_str, (specifier.id >> 32), (specifier.id & 0xFFFFFFFF), content_id);
return std::make_shared<SDMCFile>(config.sdmc_path, path, "rb");
}
}
// English short title name, extdata id, icon // English short title name, extdata id, icon
using TitleData = std::tuple<std::string, u64, std::vector<u16>>; using TitleData = std::tuple<std::string, u64, std::vector<u16>>;
TitleData LoadTitleData(NCCHContainer& ncch) { static TitleData LoadTitleData(NCCHContainer& ncch) {
std::string codeset_name; std::string codeset_name;
ncch.ReadCodesetName(codeset_name); ncch.ReadCodesetName(codeset_name);
@@ -634,11 +653,7 @@ bool SDMCImporter::DumpCXI(const ContentSpecifier& specifier, std::string destin
return false; return false;
} }
const auto boot_content_path = dump_cxi_ncch = std::make_unique<NCCHContainer>(OpenContent(specifier, tmd.GetBootContentID()));
fmt::format("/title/{:08x}/{:08x}/content/{:08x}.app", specifier.id >> 32,
(specifier.id & 0xFFFFFFFF), tmd.GetBootContentID());
dump_cxi_ncch = std::make_unique<NCCHContainer>(
std::make_shared<SDMCFile>(config.sdmc_path, boot_content_path, "rb"));
if (destination.back() == '/' || destination.back() == '\\') { if (destination.back() == '/' || destination.back() == '\\') {
auto_filename = true; auto_filename = true;
@@ -703,13 +718,6 @@ bool SDMCImporter::BuildCIA(CIABuildType build_type, const ContentSpecifier& spe
return false; return false;
} }
const bool is_nand = IsNandTitle(specifier.type);
const auto physical_path =
is_nand ? fmt::format("{}{:08x}/{:08x}/content/", config.system_titles_path,
(specifier.id >> 32), (specifier.id & 0xFFFFFFFF))
: fmt::format("{}title/{:08x}/{:08x}/content/", config.sdmc_path,
(specifier.id >> 32), (specifier.id & 0xFFFFFFFF));
if (destination.back() == '/' || destination.back() == '\\') { if (destination.back() == '/' || destination.back() == '\\') {
auto_filename = true; auto_filename = true;
} }
@@ -722,15 +730,12 @@ bool SDMCImporter::BuildCIA(CIABuildType build_type, const ContentSpecifier& spe
if (destination.back() != '/' && destination.back() != '\\') { if (destination.back() != '/' && destination.back() != '\\') {
destination.push_back('/'); destination.push_back('/');
} }
const auto boot_content_path = auto file = OpenContent(specifier, tmd.GetBootContentID());
fmt::format("{}{:08x}.app", physical_path, tmd.GetBootContentID()); if (!file) {
NCCHContainer ncch; LOG_ERROR(Core, "Could not open boot content");
if (is_nand) { return false;
ncch.OpenFile(std::make_shared<FileUtil::IOFile>(boot_content_path, "rb"));
} else {
const auto relative_path = boot_content_path.substr(config.sdmc_path.size() - 1);
ncch.OpenFile(std::make_shared<SDMCFile>(config.sdmc_path, relative_path, "rb"));
} }
NCCHContainer ncch(std::move(file));
const auto filename = const auto filename =
fmt::format("{} (v{}).{}", GetTitleFileName(ncch), tmd.GetTitleVersionString(), fmt::format("{} (v{}).{}", GetTitleFileName(ncch), tmd.GetTitleVersionString(),
BuildTypeExts.at(static_cast<std::size_t>(build_type))); BuildTypeExts.at(static_cast<std::size_t>(build_type)));
@@ -749,28 +754,18 @@ bool SDMCImporter::BuildCIA(CIABuildType build_type, const ContentSpecifier& spe
} }
for (const auto& tmd_chunk : tmd.tmd_chunks) { for (const auto& tmd_chunk : tmd.tmd_chunks) {
// For DLCs, there one subfolder every 256 titles, but in practice hardcoded 00000000 auto file = OpenContent(specifier, tmd_chunk.id);
// should be fine (also matches GodMode9) if (!file) {
const auto sub_folder =
specifier.type == ContentType::DLC ? physical_path + "00000000/" : physical_path;
const auto path = fmt::format("{}{:08x}.app", sub_folder, static_cast<u32>(tmd_chunk.id));
if (!FileUtil::Exists(path)) {
if (static_cast<u16>(tmd_chunk.type) & 0x4000) { // optional if (static_cast<u16>(tmd_chunk.type) & 0x4000) { // optional
continue; continue;
} }
LOG_ERROR(Core, "Content {:08x} does not exist", static_cast<u32>(tmd_chunk.id)); LOG_ERROR(Core, "Could not open content {:08x}", static_cast<u32>(tmd_chunk.id));
ret = false; ret = false;
return false; return false;
} }
if (is_nand) { NCCHContainer ncch(std::move(file));
NCCHContainer ncch(std::make_shared<FileUtil::IOFile>(path, "rb")); ret = cia_builder->AddContent(tmd_chunk.id, ncch);
ret = cia_builder->AddContent(tmd_chunk.id, ncch);
} else {
const auto relative_path = path.substr(config.sdmc_path.size() - 1);
NCCHContainer ncch(std::make_shared<SDMCFile>(config.sdmc_path, relative_path, "rb"));
ret = cia_builder->AddContent(tmd_chunk.id, ncch);
}
if (!ret) { if (!ret) {
return false; return false;
} }
@@ -821,35 +816,18 @@ bool SDMCImporter::CheckTitleContents(const ContentSpecifier& specifier,
Common::ProgressCallbackWrapper wrapper{specifier.maximum_size}; Common::ProgressCallbackWrapper wrapper{specifier.maximum_size};
const bool is_nand = IsNandTitle(specifier.type);
const auto physical_path =
is_nand ? fmt::format("{}{:08x}/{:08x}/content/", config.system_titles_path,
(specifier.id >> 32), (specifier.id & 0xFFFFFFFF))
: fmt::format("{}title/{:08x}/{:08x}/content/", config.sdmc_path,
(specifier.id >> 32), (specifier.id & 0xFFFFFFFF));
for (const auto& tmd_chunk : tmd.tmd_chunks) { for (const auto& tmd_chunk : tmd.tmd_chunks) {
// For DLCs, there one subfolder every 256 titles, but in practice hardcoded 00000000 auto file = OpenContent(specifier, tmd_chunk.id);
// should be fine (also matches GodMode9) if (!file) {
const auto sub_folder =
specifier.type == ContentType::DLC ? physical_path + "00000000/" : physical_path;
const auto path = fmt::format("{}{:08x}.app", sub_folder, static_cast<u32>(tmd_chunk.id));
if (!FileUtil::Exists(path)) {
if (static_cast<u16>(tmd_chunk.type) & 0x4000) { // optional if (static_cast<u16>(tmd_chunk.type) & 0x4000) { // optional
continue; continue;
} }
LOG_INFO(Core, "Content {:08x} does not exist", static_cast<u32>(tmd_chunk.id)); LOG_INFO(Core, "Could not open content {:08x}", static_cast<u32>(tmd_chunk.id));
return false; return false;
} }
std::shared_ptr<FileUtil::IOFile> source_file;
if (is_nand) {
source_file = std::make_shared<FileUtil::IOFile>(path, "rb");
} else {
const auto relative_path = path.substr(config.sdmc_path.size() - 1);
source_file = std::make_shared<SDMCFile>(config.sdmc_path, relative_path, "rb");
}
std::shared_ptr<HashOnlyFile> dest_file = std::make_shared<HashOnlyFile>(); std::shared_ptr<HashOnlyFile> dest_file = std::make_shared<HashOnlyFile>();
if (!file_decryptor.CryptAndWriteFile(source_file, source_file->GetSize(), dest_file, if (!file_decryptor.CryptAndWriteFile(file, file->GetSize(), dest_file,
wrapper.Wrap(callback))) { wrapper.Wrap(callback))) {
return false; return false;
} }
+2 -2
View File
@@ -176,8 +176,8 @@ public:
bool LoadTMD(const ContentSpecifier& specifier, TitleMetadata& out) const; bool LoadTMD(const ContentSpecifier& specifier, TitleMetadata& out) const;
std::string GetTitleContentsPath(const ContentSpecifier& specifier) const; std::string GetTitleContentsPath(const ContentSpecifier& specifier) const;
std::shared_ptr<FileUtil::IOFile> OpenBootContent(const ContentSpecifier& specifier, std::shared_ptr<FileUtil::IOFile> OpenContent(const ContentSpecifier& specifier,
const TitleMetadata& tmd) const; u32 content_id) const;
std::shared_ptr<TicketDB>& GetTicketDB() { std::shared_ptr<TicketDB>& GetTicketDB() {
return ticket_db; return ticket_db;
+1 -1
View File
@@ -490,7 +490,7 @@ void ImportDialog::OnContextMenu(const QPoint& point) {
[this, specifier] { StartBuildingCIASingle(specifier); }); [this, specifier] { StartBuildingCIASingle(specifier); });
QAction* show_title_info = context_menu.addAction(tr("Show Title Info")); QAction* show_title_info = context_menu.addAction(tr("Show Title Info"));
connect(show_title_info, &QAction::triggered, [this, specifier] { connect(show_title_info, &QAction::triggered, [this, specifier] {
TitleInfoDialog dialog(this, config, *importer, specifier); TitleInfoDialog dialog(this, *importer, specifier);
dialog.exec(); dialog.exec();
}); });
} }
+68 -65
View File
@@ -15,8 +15,8 @@
#include "frontend/title_info_dialog.h" #include "frontend/title_info_dialog.h"
#include "ui_title_info_dialog.h" #include "ui_title_info_dialog.h"
TitleInfoDialog::TitleInfoDialog(QWidget* parent, const Core::Config& config, TitleInfoDialog::TitleInfoDialog(QWidget* parent, Core::SDMCImporter& importer_,
Core::SDMCImporter& importer_, Core::ContentSpecifier specifier_) Core::ContentSpecifier specifier_)
: QDialog(parent), ui(std::make_unique<Ui::TitleInfoDialog>()), importer(importer_), : QDialog(parent), ui(std::make_unique<Ui::TitleInfoDialog>()), importer(importer_),
specifier(std::move(specifier_)) { specifier(std::move(specifier_)) {
@@ -25,39 +25,62 @@ TitleInfoDialog::TitleInfoDialog(QWidget* parent, const Core::Config& config,
const double scale = qApp->desktop()->logicalDpiX() / 96.0; const double scale = qApp->desktop()->logicalDpiX() / 96.0;
resize(static_cast<int>(width() * scale), static_cast<int>(height() * scale)); resize(static_cast<int>(width() * scale), static_cast<int>(height() * scale));
LoadInfo(config); LoadInfo();
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &TitleInfoDialog::accept); connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &TitleInfoDialog::accept);
} }
TitleInfoDialog::~TitleInfoDialog() = default; TitleInfoDialog::~TitleInfoDialog() = default;
void TitleInfoDialog::LoadInfo(const Core::Config& config) { void TitleInfoDialog::LoadInfo() {
// Load TMD & boot NCCH
Core::TitleMetadata tmd; Core::TitleMetadata tmd;
if (!importer.LoadTMD(specifier, tmd)) { Core::NCCHContainer ncch;
if (!importer.LoadTMD(specifier, tmd) ||
!ncch.OpenFile(importer.OpenContent(specifier, tmd.GetBootContentID()))) {
QMessageBox::warning(this, tr("threeSD"), tr("Could not load title information.")); QMessageBox::warning(this, tr("threeSD"), tr("Could not load title information."));
reject(); reject();
return; return;
} }
ui->versionLineEdit->setText(QString::fromStdString(tmd.GetTitleVersionString()));
ui->titleIDLineEdit->setText(QStringLiteral("%1").arg(specifier.id, 16, 16, QLatin1Char{'0'}));
const bool is_nand = Core::IsNandTitle(specifier.type); // Load SMDH from boot NCCH
const auto physical_path = bool has_smdh = false;
is_nand ? fmt::format("{}{:08x}/{:08x}/content/", config.system_titles_path, std::vector<u8> smdh_buffer;
(specifier.id >> 32), (specifier.id & 0xFFFFFFFF)) if (ncch.LoadSectionExeFS("icon", smdh_buffer) && smdh_buffer.size() == sizeof(Core::SMDH) &&
: fmt::format("{}title/{:08x}/{:08x}/content/", config.sdmc_path, Core::IsValidSMDH(smdh_buffer)) {
(specifier.id >> 32), (specifier.id & 0xFFFFFFFF));
const auto boot_content_path = has_smdh = true;
fmt::format("{}{:08x}.app", physical_path, tmd.GetBootContentID()); std::memcpy(&smdh, smdh_buffer.data(), smdh_buffer.size());
Core::NCCHContainer ncch;
if (is_nand) {
ncch.OpenFile(std::make_shared<FileUtil::IOFile>(boot_content_path, "rb"));
} else {
const auto relative_path = boot_content_path.substr(config.sdmc_path.size() - 1);
ncch.OpenFile(std::make_shared<Core::SDMCFile>(config.sdmc_path, relative_path, "rb"));
} }
// Basic info
ui->versionLineEdit->setText(QString::fromStdString(tmd.GetTitleVersionString()));
LoadEncryption(ncch);
ui->titleIDLineEdit->setText(QStringLiteral("%1").arg(specifier.id, 16, 16, QLatin1Char{'0'}));
// Icons
if (has_smdh) {
ui->iconLargeLabel->setPixmap(
QPixmap::fromImage(QImage(reinterpret_cast<const uchar*>(smdh.GetIcon(true).data()), 48,
48, QImage::Format::Format_RGB16)));
ui->iconSmallLabel->setPixmap(
QPixmap::fromImage(QImage(reinterpret_cast<const uchar*>(smdh.GetIcon(false).data()),
24, 24, QImage::Format::Format_RGB16)));
}
// Names
if (has_smdh) {
InitializeLanguageComboBox();
} else {
ui->namesGroupBox->setVisible(false);
}
// Checks
InitializeChecks(tmd);
}
void TitleInfoDialog::LoadEncryption(Core::NCCHContainer& ncch) {
static const std::unordered_map<Core::EncryptionType, const char*> EncryptionTypeMap{{ static const std::unordered_map<Core::EncryptionType, const char*> EncryptionTypeMap{{
{Core::EncryptionType::None, QT_TR_NOOP("None")}, {Core::EncryptionType::None, QT_TR_NOOP("None")},
{Core::EncryptionType::FixedKey, QT_TR_NOOP("FixedKey")}, {Core::EncryptionType::FixedKey, QT_TR_NOOP("FixedKey")},
@@ -78,50 +101,6 @@ void TitleInfoDialog::LoadInfo(const Core::Config& config) {
encryption_text.append(tr(" (Seed)")); encryption_text.append(tr(" (Seed)"));
} }
ui->encryptionLineEdit->setText(encryption_text); ui->encryptionLineEdit->setText(encryption_text);
// Checks
const bool tmd_legit = tmd.ValidateSignature() && tmd.VerifyHashes();
if (tmd_legit) {
ui->tmdCheckLabel->setText(tr("Legit"));
} else {
ui->tmdCheckLabel->setText(tr("Illegit"));
}
if (const auto& ticket_db = importer.GetTicketDB();
ticket_db && ticket_db->tickets.count(specifier.id)) {
const bool ticket_legit = ticket_db->tickets.at(specifier.id).ValidateSignature();
if (ticket_legit) {
ui->ticketCheckLabel->setText(tr("Legit"));
} else {
ui->ticketCheckLabel->setText(tr("Illegit"));
}
} else {
ui->ticketCheckLabel->setText(tr("Missing"));
}
connect(ui->contentsCheckButton, &QPushButton::clicked, this,
&TitleInfoDialog::ExecuteContentsCheck);
// Load SMDH
std::vector<u8> smdh_buffer;
if (!ncch.LoadSectionExeFS("icon", smdh_buffer) || smdh_buffer.size() != sizeof(Core::SMDH) ||
!Core::IsValidSMDH(smdh_buffer)) {
LOG_WARNING(Core, "Failed to load SMDH");
ui->namesGroupBox->setEnabled(false);
return;
}
std::memcpy(&smdh, smdh_buffer.data(), smdh_buffer.size());
// Load icon
ui->iconLargeLabel->setPixmap(
QPixmap::fromImage(QImage(reinterpret_cast<const uchar*>(smdh.GetIcon(true).data()), 48, 48,
QImage::Format::Format_RGB16)));
ui->iconSmallLabel->setPixmap(
QPixmap::fromImage(QImage(reinterpret_cast<const uchar*>(smdh.GetIcon(false).data()), 24,
24, QImage::Format::Format_RGB16)));
// Load names
InitializeLanguageComboBox();
} }
void TitleInfoDialog::InitializeLanguageComboBox() { void TitleInfoDialog::InitializeLanguageComboBox() {
@@ -172,6 +151,30 @@ void TitleInfoDialog::UpdateNames() {
QString::fromStdString(Common::UTF16BufferToUTF8(title.publisher))); QString::fromStdString(Common::UTF16BufferToUTF8(title.publisher)));
} }
void TitleInfoDialog::InitializeChecks(Core::TitleMetadata& tmd) {
const bool tmd_legit = tmd.ValidateSignature() && tmd.VerifyHashes();
if (tmd_legit) {
ui->tmdCheckLabel->setText(tr("Legit"));
} else {
ui->tmdCheckLabel->setText(tr("Illegit"));
}
if (const auto& ticket_db = importer.GetTicketDB();
ticket_db && ticket_db->tickets.count(specifier.id)) {
const bool ticket_legit = ticket_db->tickets.at(specifier.id).ValidateSignature();
if (ticket_legit) {
ui->ticketCheckLabel->setText(tr("Legit"));
} else {
ui->ticketCheckLabel->setText(tr("Illegit"));
}
} else {
ui->ticketCheckLabel->setText(tr("Missing"));
}
connect(ui->contentsCheckButton, &QPushButton::clicked, this,
&TitleInfoDialog::ExecuteContentsCheck);
}
void TitleInfoDialog::ExecuteContentsCheck() { void TitleInfoDialog::ExecuteContentsCheck() {
auto* job = new SimpleJob( auto* job = new SimpleJob(
this, this,
+8 -3
View File
@@ -9,6 +9,7 @@
namespace Core { namespace Core {
struct Config; struct Config;
class ContentSpecifier; class ContentSpecifier;
class NCCHContainer;
class SDMCImporter; class SDMCImporter;
class TitleMetadata; class TitleMetadata;
} // namespace Core } // namespace Core
@@ -21,13 +22,17 @@ class TitleInfoDialog : public QDialog {
Q_OBJECT Q_OBJECT
public: public:
explicit TitleInfoDialog(QWidget* parent, const Core::Config& config, explicit TitleInfoDialog(QWidget* parent, Core::SDMCImporter& importer,
Core::SDMCImporter& importer, Core::ContentSpecifier specifier); Core::ContentSpecifier specifier);
~TitleInfoDialog(); ~TitleInfoDialog();
private: private:
void LoadInfo(const Core::Config& config); void LoadInfo();
void LoadEncryption(Core::NCCHContainer& ncch);
void InitializeLanguageComboBox(); void InitializeLanguageComboBox();
void InitializeChecks(Core::TitleMetadata& tmd);
void UpdateNames(); void UpdateNames();
void ExecuteContentsCheck(); void ExecuteContentsCheck();