More NCSD loading

This commit is contained in:
wheremyfoodat 2022-10-01 16:21:05 +03:00
parent 9a040e1cde
commit f6c2e390c1
11 changed files with 142 additions and 17 deletions

30
src/core/loader/ncch.cpp Normal file
View file

@ -0,0 +1,30 @@
#include "loader/ncch.hpp"
bool NCCH::loadFromHeader(u8* header) {
const u8* exheader = &header[0x200]; // Extended NCCH header
if (header[0x100] != 'N' || header[0x101] != 'C' || header[0x102] != 'C' || header[0x103] != 'H') {
printf("Invalid header on NCCH\n");
return false;
}
size = u64(*(u32*)&header[0x104]) * mediaUnit; // TODO: Maybe don't type pun because big endian will break
// Read NCCH flags
isNew3DS = header[0x188 + 4] == 2;
mountRomFS = (header[0x188 + 7] & 0x1) != 0x1;
encrypted = (header[0x188 + 7] & 0x4) != 0x4;
compressExeFS = (exheader[0xD] & 1) != 0;
if (compressExeFS) {
Helpers::panic("Compressed ExeFS");
}
if (encrypted) {
Helpers::panic("Encrypted NCCH partition");
}
initialized = true;
return true;
}

68
src/core/loader/ncsd.cpp Normal file
View file

@ -0,0 +1,68 @@
#include <optional>
#include "loader/ncsd.hpp"
#include "memory.hpp"
std::optional<NCSD> Memory::loadNCSD(const std::filesystem::path& path) {
NCSD ncsd;
if (!ncsd.file.open(path, "rb"))
return std::nullopt;
u8 magic[4]; // Must be "NCSD"
ncsd.file.seek(0x100);
auto [success, bytes] = ncsd.file.readBytes(magic, 4);
if (!success || bytes != 4) {
printf("Failed to read NCSD magic\n");
return std::nullopt;
}
if (magic[0] != 'N' || magic[1] != 'C' || magic[2] != 'S' || magic[3] != 'D') {
printf("NCSD with wrong magic value\n");
return std::nullopt;
}
std::tie(success, bytes) = ncsd.file.readBytes(&ncsd.size, 4);
if (!success || bytes != 4) {
printf("Failed to read NCSD size\n");
return std::nullopt;
}
ncsd.size *= NCSD::mediaUnit; // Convert size to bytes
// Read partition data
ncsd.file.seek(0x120);
// 2 u32s per partition (offset and length), 8 partitions total
constexpr size_t partitionDataSize = 8 * 2; // Size of partition in u32s
u32 partitionData[8 * 2];
std::tie(success, bytes) = ncsd.file.read(partitionData, partitionDataSize, sizeof(u32));
if (!success || bytes != partitionDataSize) {
printf("Failed to read NCSD partition data\n");
return std::nullopt;
}
for (int i = 0; i < 8; i++) {
auto& partition = ncsd.partitions[i];
partition.offset = u64(partitionData[i * 2]) * NCSD::mediaUnit;
partition.length = u64(partitionData[i * 2 + 1]) * NCSD::mediaUnit;
if (partition.length != 0) { // Initialize the NCCH of each partition
ncsd.file.seek(partition.offset);
// 0x200 bytes for the NCCH header and another 0x800 for the exheader
constexpr u64 headerSize = 0x200 + 0x800;
u8 ncchHeader[headerSize];
std::tie(success, bytes) = ncsd.file.readBytes(ncchHeader, headerSize);
if (!success || bytes != headerSize) {
printf("Failed to read NCCH header\n");
return std::nullopt;
}
if (!partition.ncch.loadFromHeader(ncchHeader)) {
printf("Invalid NCCH partition\n");
return std::nullopt;
}
}
}
return ncsd;
}

View file

@ -11,7 +11,7 @@ void Emulator::reset() {
// Otherwise resetting the kernel or cpu might nuke them
cpu.setReg(13, VirtualAddrs::StackTop); // Set initial SP
if (romType == ROMType::ELF) { // Reload ELF if we're using one
loadELF(loadedROM);
loadELF(loadedELF);
}
}
@ -54,26 +54,38 @@ bool Emulator::loadROM(const std::filesystem::path& path) {
if (extension == ".elf" || extension == ".axf")
return loadELF(path);
else if (extension == ".3ds")
Helpers::panic("3DS file");
return loadNCSD(path);
else {
printf("Unknown file type\n");
return false;
}
}
bool Emulator::loadNCSD(const std::filesystem::path& path) {
romType = ROMType::NCSD;
std::optional<NCSD> opt = memory.loadNCSD(path);
if (!opt.has_value()) {
return false;
}
loadedNCSD = opt.value();
return true;
}
bool Emulator::loadELF(const std::filesystem::path& path) {
loadedROM.open(path, std::ios_base::binary); // Open ROM in binary mode
loadedELF.open(path, std::ios_base::binary); // Open ROM in binary mode
romType = ROMType::ELF;
return loadELF(loadedROM);
return loadELF(loadedELF);
}
bool Emulator::loadELF(std::ifstream& file) {
// Rewind ifstream
loadedROM.clear();
loadedROM.seekg(0);
loadedELF.clear();
loadedELF.seekg(0);
std::optional<u32> entrypoint = memory.loadELF(loadedROM);
std::optional<u32> entrypoint = memory.loadELF(loadedELF);
if (!entrypoint.has_value())
return false;

View file

@ -9,10 +9,10 @@ int main (int argc, char *argv[]) {
emu.initGraphicsContext();
auto elfPath = std::filesystem::current_path() / (argc > 1 ? argv[1] : "Metroid2.3ds");
if (!emu.loadROM(elfPath)) {
auto romPath = std::filesystem::current_path() / (argc > 1 ? argv[1] : "SuperMario3DLand.3ds");
if (!emu.loadROM(romPath)) {
// For some reason just .c_str() doesn't show the proper path
Helpers::panic("Failed to load ROM file: %s", elfPath.string().c_str());
Helpers::panic("Failed to load ROM file: %s", romPath.string().c_str());
}
emu.run();