clang doormat

This commit is contained in:
wheremyfoodat 2023-07-06 16:24:23 +03:00
parent 187feb5772
commit ef0ef45e94

View file

@ -1,6 +1,8 @@
#include "loader/ncsd.hpp"
#include <cstring> #include <cstring>
#include <optional> #include <optional>
#include "loader/ncsd.hpp"
#include "memory.hpp" #include "memory.hpp"
bool Memory::mapCXI(NCSD& ncsd, NCCH& cxi) { bool Memory::mapCXI(NCSD& ncsd, NCCH& cxi) {
@ -53,78 +55,77 @@ bool Memory::mapCXI(NCSD& ncsd, NCCH& cxi) {
loadedCXI = cxi; loadedCXI = cxi;
} }
std::optional<NCSD> Memory::loadNCSD(Crypto::AESEngine &aesEngine, const std::filesystem::path& path) { std::optional<NCSD> Memory::loadNCSD(Crypto::AESEngine& aesEngine, const std::filesystem::path& path) {
NCSD ncsd; NCSD ncsd;
if (!ncsd.file.open(path, "rb")) if (!ncsd.file.open(path, "rb")) return std::nullopt;
return std::nullopt;
u8 magic[4]; // Must be "NCSD" u8 magic[4]; // Must be "NCSD"
ncsd.file.seek(0x100); ncsd.file.seek(0x100);
auto [success, bytes] = ncsd.file.readBytes(magic, 4); auto [success, bytes] = ncsd.file.readBytes(magic, 4);
if (!success || bytes != 4) { if (!success || bytes != 4) {
printf("Failed to read NCSD magic\n"); printf("Failed to read NCSD magic\n");
return std::nullopt; return std::nullopt;
} }
if (magic[0] != 'N' || magic[1] != 'C' || magic[2] != 'S' || magic[3] != 'D') { if (magic[0] != 'N' || magic[1] != 'C' || magic[2] != 'S' || magic[3] != 'D') {
printf("NCSD with wrong magic value\n"); printf("NCSD with wrong magic value\n");
return std::nullopt; return std::nullopt;
} }
std::tie(success, bytes) = ncsd.file.readBytes(&ncsd.size, 4); std::tie(success, bytes) = ncsd.file.readBytes(&ncsd.size, 4);
if (!success || bytes != 4) { if (!success || bytes != 4) {
printf("Failed to read NCSD size\n"); printf("Failed to read NCSD size\n");
return std::nullopt; return std::nullopt;
} }
ncsd.size *= NCSD::mediaUnit; // Convert size to bytes ncsd.size *= NCSD::mediaUnit; // Convert size to bytes
// Read partition data // Read partition data
ncsd.file.seek(0x120); ncsd.file.seek(0x120);
// 2 u32s per partition (offset and length), 8 partitions total // 2 u32s per partition (offset and length), 8 partitions total
constexpr size_t partitionDataSize = 8 * 2; // Size of partition in u32s constexpr size_t partitionDataSize = 8 * 2; // Size of partition in u32s
u32 partitionData[8 * 2]; u32 partitionData[8 * 2];
std::tie(success, bytes) = ncsd.file.read(partitionData, partitionDataSize, sizeof(u32)); std::tie(success, bytes) = ncsd.file.read(partitionData, partitionDataSize, sizeof(u32));
if (!success || bytes != partitionDataSize) { if (!success || bytes != partitionDataSize) {
printf("Failed to read NCSD partition data\n"); printf("Failed to read NCSD partition data\n");
return std::nullopt; return std::nullopt;
} }
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
auto& partition = ncsd.partitions[i]; auto& partition = ncsd.partitions[i];
NCCH& ncch = partition.ncch; NCCH& ncch = partition.ncch;
partition.offset = u64(partitionData[i * 2]) * NCSD::mediaUnit; partition.offset = u64(partitionData[i * 2]) * NCSD::mediaUnit;
partition.length = u64(partitionData[i * 2 + 1]) * NCSD::mediaUnit; partition.length = u64(partitionData[i * 2 + 1]) * NCSD::mediaUnit;
ncch.partitionIndex = i; ncch.partitionIndex = i;
ncch.fileOffset = partition.offset; ncch.fileOffset = partition.offset;
if (partition.length != 0) { // Initialize the NCCH of each partition if (partition.length != 0) { // Initialize the NCCH of each partition
NCCH::FSInfo ncchFsInfo; NCCH::FSInfo ncchFsInfo;
ncchFsInfo.offset = partition.offset; ncchFsInfo.offset = partition.offset;
ncchFsInfo.size = partition.length; ncchFsInfo.size = partition.length;
if (!ncch.loadFromHeader(aesEngine, ncsd.file, ncchFsInfo)) { if (!ncch.loadFromHeader(aesEngine, ncsd.file, ncchFsInfo)) {
printf("Invalid NCCH partition\n"); printf("Invalid NCCH partition\n");
return std::nullopt; return std::nullopt;
} }
} }
} }
auto& cxi = ncsd.partitions[0].ncch; auto& cxi = ncsd.partitions[0].ncch;
if (!cxi.hasExtendedHeader() || !cxi.hasCode()) { if (!cxi.hasExtendedHeader() || !cxi.hasCode()) {
printf("NCSD with an invalid CXI in partition 0?\n"); printf("NCSD with an invalid CXI in partition 0?\n");
return std::nullopt; return std::nullopt;
} }
if (!mapCXI(ncsd, cxi)) { if (!mapCXI(ncsd, cxi)) {
printf("Failed to map CXI\n"); printf("Failed to map CXI\n");
return std::nullopt; return std::nullopt;
} }
return ncsd; return ncsd;
} }
// We are lazy so we take CXI files, easily "convert" them to NCSD internally, then use our existing NCSD infrastructure // We are lazy so we take CXI files, easily "convert" them to NCSD internally, then use our existing NCSD infrastructure