mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-04-08 23:25:40 +12:00
Add CXI ROM support
Co-Authored-By: Kelpsy <138107494+Kelpsyberry@users.noreply.github.com>
This commit is contained in:
parent
4e5eb884ed
commit
187feb5772
4 changed files with 118 additions and 54 deletions
|
@ -13,7 +13,7 @@
|
||||||
#include "memory.hpp"
|
#include "memory.hpp"
|
||||||
#include "gl_state.hpp"
|
#include "gl_state.hpp"
|
||||||
|
|
||||||
enum class ROMType { None, ELF, NCSD };
|
enum class ROMType { None, ELF, NCSD, CXI };
|
||||||
|
|
||||||
class Emulator {
|
class Emulator {
|
||||||
CPU cpu;
|
CPU cpu;
|
||||||
|
@ -54,7 +54,7 @@ class Emulator {
|
||||||
void runFrame();
|
void runFrame();
|
||||||
|
|
||||||
bool loadROM(const std::filesystem::path& path);
|
bool loadROM(const std::filesystem::path& path);
|
||||||
bool loadNCSD(const std::filesystem::path& path);
|
bool loadNCSD(const std::filesystem::path& path, ROMType type);
|
||||||
bool loadELF(const std::filesystem::path& path);
|
bool loadELF(const std::filesystem::path& path);
|
||||||
bool loadELF(std::ifstream& file);
|
bool loadELF(std::ifstream& file);
|
||||||
void initGraphicsContext();
|
void initGraphicsContext();
|
||||||
|
|
|
@ -150,7 +150,10 @@ public:
|
||||||
void* getReadPointer(u32 address);
|
void* getReadPointer(u32 address);
|
||||||
void* getWritePointer(u32 address);
|
void* getWritePointer(u32 address);
|
||||||
std::optional<u32> loadELF(std::ifstream& file);
|
std::optional<u32> loadELF(std::ifstream& file);
|
||||||
std::optional<NCSD> loadNCSD(Crypto::AESEngine &aesEngine, const std::filesystem::path& path);
|
std::optional<NCSD> loadNCSD(Crypto::AESEngine& aesEngine, const std::filesystem::path& path);
|
||||||
|
std::optional<NCSD> loadCXI(Crypto::AESEngine& aesEngine, const std::filesystem::path& path);
|
||||||
|
|
||||||
|
bool mapCXI(NCSD& ncsd, NCCH& cxi);
|
||||||
|
|
||||||
u8 read8(u32 vaddr);
|
u8 read8(u32 vaddr);
|
||||||
u16 read16(u32 vaddr);
|
u16 read16(u32 vaddr);
|
||||||
|
|
|
@ -3,6 +3,56 @@
|
||||||
#include "loader/ncsd.hpp"
|
#include "loader/ncsd.hpp"
|
||||||
#include "memory.hpp"
|
#include "memory.hpp"
|
||||||
|
|
||||||
|
bool Memory::mapCXI(NCSD& ncsd, NCCH& cxi) {
|
||||||
|
printf("Text address = %08X, size = %08X\n", cxi.text.address, cxi.text.size);
|
||||||
|
printf("Rodata address = %08X, size = %08X\n", cxi.rodata.address, cxi.rodata.size);
|
||||||
|
printf("Data address = %08X, size = %08X\n", cxi.data.address, cxi.data.size);
|
||||||
|
|
||||||
|
// Map code file to memory
|
||||||
|
auto& code = cxi.codeFile;
|
||||||
|
u32 bssSize = (cxi.bssSize + 0xfff) & ~0xfff; // Round BSS size up to a page boundary
|
||||||
|
// Total memory to allocate for loading
|
||||||
|
u32 totalSize = (cxi.text.pageCount + cxi.rodata.pageCount + cxi.data.pageCount) * pageSize + bssSize;
|
||||||
|
code.resize(code.size() + bssSize, 0); // Pad the .code file with zeroes for the BSS segment
|
||||||
|
|
||||||
|
if (code.size() < totalSize) {
|
||||||
|
Helpers::panic("Total code size as reported by the exheader is larger than the .code file");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto opt = findPaddr(totalSize);
|
||||||
|
if (!opt.has_value()) {
|
||||||
|
Helpers::panic("Failed to find paddr to map CXI file's code to");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto paddr = opt.value();
|
||||||
|
std::memcpy(&fcram[paddr], &code[0], totalSize); // Copy the 3 segments + BSS to FCRAM
|
||||||
|
|
||||||
|
// Map the ROM on the kernel side
|
||||||
|
u32 textOffset = 0;
|
||||||
|
u32 textAddr = cxi.text.address;
|
||||||
|
u32 textSize = cxi.text.pageCount * pageSize;
|
||||||
|
|
||||||
|
u32 rodataOffset = textOffset + textSize;
|
||||||
|
u32 rodataAddr = cxi.rodata.address;
|
||||||
|
u32 rodataSize = cxi.rodata.pageCount * pageSize;
|
||||||
|
|
||||||
|
u32 dataOffset = rodataOffset + rodataSize;
|
||||||
|
u32 dataAddr = cxi.data.address;
|
||||||
|
u32 dataSize = cxi.data.pageCount * pageSize + bssSize; // We're merging the data and BSS segments, as BSS is just pre-initted .data
|
||||||
|
|
||||||
|
allocateMemory(textAddr, paddr + textOffset, textSize, true, true, false, true); // Text is R-X
|
||||||
|
allocateMemory(rodataAddr, paddr + rodataOffset, rodataSize, true, true, false, false); // Rodata is R--
|
||||||
|
allocateMemory(dataAddr, paddr + dataOffset, dataSize, true, true, true, false); // Data+BSS is RW-
|
||||||
|
|
||||||
|
ncsd.entrypoint = textAddr;
|
||||||
|
|
||||||
|
// Back the IOFile for accessing the ROM, as well as the ROM's CXI partition, in the memory class.
|
||||||
|
CXIFile = ncsd.file;
|
||||||
|
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"))
|
||||||
|
@ -69,53 +119,60 @@ std::optional<NCSD> Memory::loadNCSD(Crypto::AESEngine &aesEngine, const std::fi
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Text address = %08X, size = %08X\n", cxi.text.address, cxi.text.size);
|
if (!mapCXI(ncsd, cxi)) {
|
||||||
printf("Rodata address = %08X, size = %08X\n", cxi.rodata.address, cxi.rodata.size);
|
printf("Failed to map CXI\n");
|
||||||
printf("Data address = %08X, size = %08X\n", cxi.data.address, cxi.data.size);
|
return std::nullopt;
|
||||||
|
}
|
||||||
// Map code file to memory
|
|
||||||
auto& code = cxi.codeFile;
|
|
||||||
u32 bssSize = (cxi.bssSize + 0xfff) & ~0xfff; // Round BSS size up to a page boundary
|
|
||||||
// Total memory to allocate for loading
|
|
||||||
u32 totalSize = (cxi.text.pageCount + cxi.rodata.pageCount + cxi.data.pageCount) * pageSize + bssSize;
|
|
||||||
code.resize(code.size() + bssSize, 0); // Pad the .code file with zeroes for the BSS segment
|
|
||||||
|
|
||||||
if (code.size() < totalSize) {
|
|
||||||
Helpers::panic("Total code size as reported by the exheader is larger than the .code file");
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto opt = findPaddr(totalSize);
|
|
||||||
if (!opt.has_value()) {
|
|
||||||
Helpers::panic("Failed to find paddr to map CXI file's code to");
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto paddr = opt.value();
|
|
||||||
std::memcpy(&fcram[paddr], &code[0], totalSize); // Copy the 3 segments + BSS to FCRAM
|
|
||||||
|
|
||||||
// Map the ROM on the kernel side
|
|
||||||
u32 textOffset = 0;
|
|
||||||
u32 textAddr = cxi.text.address;
|
|
||||||
u32 textSize = cxi.text.pageCount * pageSize;
|
|
||||||
|
|
||||||
u32 rodataOffset = textOffset + textSize;
|
|
||||||
u32 rodataAddr = cxi.rodata.address;
|
|
||||||
u32 rodataSize = cxi.rodata.pageCount * pageSize;
|
|
||||||
|
|
||||||
u32 dataOffset = rodataOffset + rodataSize;
|
|
||||||
u32 dataAddr = cxi.data.address;
|
|
||||||
u32 dataSize = cxi.data.pageCount * pageSize + bssSize; // We're merging the data and BSS segments, as BSS is just pre-initted .data
|
|
||||||
|
|
||||||
allocateMemory(textAddr, paddr + textOffset, textSize, true, true, false, true); // Text is R-X
|
|
||||||
allocateMemory(rodataAddr, paddr + rodataOffset, rodataSize, true, true, false, false); // Rodata is R--
|
|
||||||
allocateMemory(dataAddr, paddr + dataOffset, dataSize, true, true, true, false); // Data+BSS is RW-
|
|
||||||
|
|
||||||
ncsd.entrypoint = textAddr;
|
|
||||||
|
|
||||||
// Back the IOFile for accessing the ROM, as well as the ROM's CXI partition, in the memory class.
|
|
||||||
CXIFile = ncsd.file;
|
|
||||||
loadedCXI = cxi;
|
|
||||||
|
|
||||||
return ncsd;
|
return ncsd;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We are lazy so we take CXI files, easily "convert" them to NCSD internally, then use our existing NCSD infrastructure
|
||||||
|
// This is easy because NCSD is just CXI + some more NCCH partitions, which we can make empty when converting to NCSD
|
||||||
|
std::optional<NCSD> Memory::loadCXI(Crypto::AESEngine& aesEngine, const std::filesystem::path& path) {
|
||||||
|
NCSD ncsd;
|
||||||
|
if (!ncsd.file.open(path, "rb")) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make partitions 1 through 8 of the converted NCSD empty
|
||||||
|
// Partition 0 (CXI partition of an NCSD) is the only one we care about
|
||||||
|
for (int i = 1; i < 8; i++) {
|
||||||
|
auto& partition = ncsd.partitions[i];
|
||||||
|
NCCH& ncch = partition.ncch;
|
||||||
|
partition.offset = 0ull;
|
||||||
|
partition.length = 0ull;
|
||||||
|
|
||||||
|
ncch.partitionIndex = i;
|
||||||
|
ncch.fileOffset = partition.offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto& cxiPartition = ncsd.partitions[0];
|
||||||
|
auto& cxi = cxiPartition.ncch;
|
||||||
|
|
||||||
|
std::optional<u64> size = ncsd.file.size();
|
||||||
|
if (!size.has_value()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
cxiPartition.offset = 0ull;
|
||||||
|
cxiPartition.length = size.value();
|
||||||
|
NCCH::FSInfo cxiInfo{.offset = cxiPartition.offset, .size = cxiPartition.length, .hashRegionSize = 0, .encryptionInfo = std::nullopt};
|
||||||
|
|
||||||
|
if (!cxi.loadFromHeader(aesEngine, ncsd.file, cxiInfo)) {
|
||||||
|
printf("Invalid CXI partition\n");
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!cxi.hasExtendedHeader() || !cxi.hasCode()) {
|
||||||
|
printf("CXI does not have exheader or code file?\n");
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!mapCXI(ncsd, cxi)) {
|
||||||
|
printf("Failed to map CXI\n");
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ncsd;
|
||||||
}
|
}
|
|
@ -288,16 +288,20 @@ bool Emulator::loadROM(const std::filesystem::path& path) {
|
||||||
if (extension == ".elf" || extension == ".axf")
|
if (extension == ".elf" || extension == ".axf")
|
||||||
return loadELF(path);
|
return loadELF(path);
|
||||||
else if (extension == ".3ds")
|
else if (extension == ".3ds")
|
||||||
return loadNCSD(path);
|
return loadNCSD(path, ROMType::NCSD);
|
||||||
|
else if (extension == ".cxi")
|
||||||
|
return loadNCSD(path, ROMType::CXI);
|
||||||
else {
|
else {
|
||||||
printf("Unknown file type\n");
|
printf("Unknown file type\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Emulator::loadNCSD(const std::filesystem::path& path) {
|
// Used for loading both CXI and NCSD files since they are both so similar and use the same interface
|
||||||
romType = ROMType::NCSD;
|
// (We promote CXI files to NCSD internally for ease)
|
||||||
std::optional<NCSD> opt = memory.loadNCSD(aesEngine, path);
|
bool Emulator::loadNCSD(const std::filesystem::path& path, ROMType type) {
|
||||||
|
romType = type;
|
||||||
|
std::optional<NCSD> opt = (type == ROMType::NCSD) ? memory.loadNCSD(aesEngine, path) : memory.loadCXI(aesEngine, path);
|
||||||
|
|
||||||
if (!opt.has_value()) {
|
if (!opt.has_value()) {
|
||||||
return false;
|
return false;
|
||||||
|
|
Loading…
Add table
Reference in a new issue