mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-05-04 03:04:48 +12:00
Implement reading from RomFS
This commit is contained in:
parent
272cdefca1
commit
df4cd0642d
10 changed files with 157 additions and 33 deletions
src/core
62
src/core/filesystem/archive_ncch.cpp
Normal file
62
src/core/filesystem/archive_ncch.cpp
Normal file
|
@ -0,0 +1,62 @@
|
|||
#include "fs/archive_ncch.hpp"
|
||||
#include <memory>
|
||||
|
||||
bool SelfNCCHArchive::openFile(const FSPath& path) {
|
||||
if (path.type != PathType::Binary) {
|
||||
printf("Invalid SelfNCCH path type");
|
||||
return false;
|
||||
}
|
||||
|
||||
// We currently only know how to read from an NCCH's RomFS
|
||||
if (mem.read32(path.pointer) != 0) {
|
||||
Helpers::panic("Read from NCCH's non-RomFS section!");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
ArchiveBase* SelfNCCHArchive::openArchive(FSPath& path) {
|
||||
if (path.type != PathType::Empty) {
|
||||
printf("Invalid path type for SelfNCCH archive: %d\n", path.type);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
std::optional<u32> SelfNCCHArchive::readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) {
|
||||
auto cxi = mem.getCXI();
|
||||
if (cxi == nullptr) {
|
||||
Helpers::panic("Tried to read file from non-existent CXI");
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
if (!cxi->hasRomFS()) {
|
||||
Helpers::panic("Tried to read from CXI without RomFS");
|
||||
}
|
||||
|
||||
const u32 romFSSize = cxi->romFS.size;
|
||||
const u32 romFSOffset = cxi->romFS.offset;
|
||||
if ((offset >> 32) || (offset >= romFSSize) || (offset + size >= romFSSize)) {
|
||||
Helpers::panic("Tried to read from SelfNCCH with too big of an offset");
|
||||
}
|
||||
|
||||
IOFile& ioFile = mem.CXIFile;
|
||||
if (!ioFile.seek(cxi->fileOffset + romFSOffset + offset)) {
|
||||
Helpers::panic("Failed to seek while reading from RomFS");
|
||||
}
|
||||
|
||||
std::unique_ptr<u8[]> data(new u8[size]);
|
||||
auto [success, bytesRead] = ioFile.readBytes(&data[0], size);
|
||||
|
||||
if (!success) {
|
||||
Helpers::panic("Failed to read from RomFS");
|
||||
}
|
||||
|
||||
for (u64 i = 0; i < bytesRead; i++) {
|
||||
mem.write8(dataPointer + i, data[i]);
|
||||
printf("%x\n", data[i]);
|
||||
}
|
||||
|
||||
return bytesRead;
|
||||
}
|
|
@ -2,17 +2,64 @@
|
|||
|
||||
namespace FileOps {
|
||||
enum : u32 {
|
||||
Read = 0x080200C2
|
||||
Read = 0x080200C2,
|
||||
Close = 0x08080000
|
||||
};
|
||||
}
|
||||
|
||||
namespace Result {
|
||||
enum : u32 {
|
||||
Success = 0
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
void Kernel::handleFileOperation(u32 messagePointer, Handle file) {
|
||||
const u32 cmd = mem.read32(messagePointer);
|
||||
switch (cmd) {
|
||||
case FileOps::Close: closeFile(messagePointer, file); break;
|
||||
case FileOps::Read: readFile(messagePointer, file); break;
|
||||
default: Helpers::panic("Unknown file operation: %08X", cmd);
|
||||
}
|
||||
}
|
||||
|
||||
void Kernel::readFile(u32 messagePointer, Handle file) {
|
||||
|
||||
void Kernel::closeFile(u32 messagePointer, Handle fileHandle) {
|
||||
logFileIO("Closed file %X\n", fileHandle);
|
||||
|
||||
const auto p = getObject(fileHandle, KernelObjectType::File);
|
||||
if (p == nullptr) [[unlikely]] {
|
||||
Helpers::panic("Called CloseFile on non-existent file");
|
||||
}
|
||||
|
||||
p->getData<FileSession>()->isOpen = false;
|
||||
mem.write32(messagePointer + 4, Result::Success);
|
||||
}
|
||||
|
||||
void Kernel::readFile(u32 messagePointer, Handle fileHandle) {
|
||||
u64 offset = mem.read64(messagePointer + 4);
|
||||
u32 size = mem.read32(messagePointer + 12);
|
||||
u32 dataPointer = mem.read32(messagePointer + 20);
|
||||
|
||||
logFileIO("Trying to read %X bytes from file %X, starting from offset %llX into memory address %08X\n",
|
||||
size, fileHandle, offset, dataPointer);
|
||||
|
||||
const auto p = getObject(fileHandle, KernelObjectType::File);
|
||||
if (p == nullptr) [[unlikely]] {
|
||||
Helpers::panic("Called ReadFile on non-existent file");
|
||||
}
|
||||
|
||||
FileSession* file = p->getData<FileSession>();
|
||||
if (!file->isOpen) {
|
||||
Helpers::panic("Tried to read closed file");
|
||||
}
|
||||
|
||||
auto archive = file->archive;
|
||||
|
||||
std::optional<u32> bytesRead = archive->readFile(file, offset, size, dataPointer);
|
||||
if (!bytesRead.has_value()) {
|
||||
Helpers::panic("Kernel::ReadFile failed");
|
||||
} else {
|
||||
mem.write32(messagePointer + 4, Result::Success);
|
||||
mem.write32(messagePointer + 8, bytesRead.value());
|
||||
}
|
||||
}
|
|
@ -118,5 +118,9 @@ std::optional<NCSD> Memory::loadNCSD(const std::filesystem::path& path) {
|
|||
|
||||
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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue