mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-04-20 12:39:13 +12:00
More NCSD loading
This commit is contained in:
parent
9a040e1cde
commit
f6c2e390c1
11 changed files with 142 additions and 17 deletions
58
src/core/loader/elf.cpp
Normal file
58
src/core/loader/elf.cpp
Normal file
|
@ -0,0 +1,58 @@
|
|||
#include "memory.hpp"
|
||||
|
||||
#include <cstring>
|
||||
#include "elfio/elfio.hpp"
|
||||
|
||||
using namespace ELFIO;
|
||||
|
||||
std::optional<u32> Memory::loadELF(std::ifstream& file) {
|
||||
elfio reader;
|
||||
if (!file.good() || !reader.load(file)) {
|
||||
printf("Woops failed to load ELF\n");
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
auto segNum = reader.segments.size();
|
||||
printf("Number of segments: %d\n", segNum);
|
||||
printf(" # Perms Vaddr File Size Mem Size\n");
|
||||
for (int i = 0; i < segNum; ++i) {
|
||||
const auto seg = reader.segments[i];
|
||||
const auto flags = seg->get_flags();
|
||||
const u32 vaddr = static_cast<u32>(seg->get_virtual_address()); // Vaddr the segment is loaded in
|
||||
u32 fileSize = static_cast<u32>(seg->get_file_size()); // Size of segment in file
|
||||
u32 memorySize = static_cast<u32>(seg->get_memory_size()); // Size of segment in memory
|
||||
u8* data = (u8*)seg->get_data();
|
||||
|
||||
// Get read/write/execute permissions for segment
|
||||
const bool r = (flags & 0b100) != 0;
|
||||
const bool w = (flags & 0b010) != 0;
|
||||
const bool x = (flags & 0b001) != 0;
|
||||
|
||||
printf("[%d] (%c%c%c)\t%08X\t%08X\t%08X\n", i, r ? 'r' : '-', w ? 'w' : '-', x ? 'x' : '-', vaddr, fileSize, memorySize);
|
||||
|
||||
// Assert that the segment will be loaded in the executable region. If it isn't then panic.
|
||||
// The executable region starts at 0x00100000 and has a maximum size of 0x03F00000
|
||||
u64 endAddress = (u64)vaddr + (u64)fileSize;
|
||||
const bool isGood = vaddr >= VirtualAddrs::ExecutableStart && endAddress < VirtualAddrs::ExecutableEnd;
|
||||
if (!isGood) {
|
||||
// We're ignoring this for now because some ELFs define a segment at the vaddr for IPC buffer mapping
|
||||
// Helpers::panic("ELF is loaded at invalid place");
|
||||
// return std::nullopt;
|
||||
}
|
||||
|
||||
if (memorySize & pageMask) {
|
||||
// Round up the size of the ELF segment to a page (4KB) boundary, as the OS can only alloc this way
|
||||
memorySize = (memorySize + pageSize - 1) & -pageSize;
|
||||
Helpers::warn("Rounding ELF segment size to %08X\n", memorySize);
|
||||
}
|
||||
|
||||
// This should also assert that findPaddr doesn't fail
|
||||
u32 fcramAddr = findPaddr(memorySize).value();
|
||||
std::memcpy(&fcram[fcramAddr], data, fileSize);
|
||||
|
||||
// Allocate the segment on the OS side
|
||||
allocateMemory(vaddr, fcramAddr, memorySize, true, r, w, x);
|
||||
}
|
||||
|
||||
return static_cast<u32>(reader.get_entry());
|
||||
}
|
30
src/core/loader/ncch.cpp
Normal file
30
src/core/loader/ncch.cpp
Normal 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
68
src/core/loader/ncsd.cpp
Normal 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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue