[FS] Implement GetFileSize [PTM] Stub step functions

This commit is contained in:
wheremyfoodat 2023-01-23 03:24:13 +02:00
parent 49dcc25795
commit e052c16a36
5 changed files with 76 additions and 7 deletions

View file

@ -75,17 +75,17 @@ public:
return write(data, count, sizeof(std::uint8_t));
}
std::uint64_t size() {
if (!isOpen()) return 0;
std::optional<std::uint64_t> size() {
if (!isOpen()) return {};
std::uint64_t pos = ftello(handle);
if (fseeko(handle, 0, SEEK_END) != 0) {
return 0;
return {};
}
std::uint64_t size = ftello(handle);
if ((size != pos) && (fseeko(handle, pos, SEEK_SET) != 0)) {
return 0;
return {};
}
return size;

View file

@ -120,6 +120,7 @@ private:
void handleFileOperation(u32 messagePointer, Handle file);
void closeFile(u32 messagePointer, Handle file);
void readFile(u32 messagePointer, Handle file);
void writeFile(u32 messagePointer, Handle file);
void getFileSize(u32 messagePointer, Handle file);
public:

View file

@ -10,6 +10,8 @@ class PTMService {
MAKE_LOG_FUNCTION(log, ptmLogger)
// Service commands
void getStepHistory(u32 messagePointer);
void getTotalStepCount(u32 messagePointer);
public:
PTMService(Memory& mem) : mem(mem) {}

View file

@ -3,6 +3,7 @@
namespace FileOps {
enum : u32 {
Read = 0x080200C2,
Write = 0x08030102,
GetSize = 0x08040000,
Close = 0x08080000
};
@ -21,6 +22,7 @@ void Kernel::handleFileOperation(u32 messagePointer, Handle file) {
case FileOps::Close: closeFile(messagePointer, file); break;
case FileOps::GetSize: getFileSize(messagePointer, file); break;
case FileOps::Read: readFile(messagePointer, file); break;
case FileOps::Write: writeFile(messagePointer, file); break;
default: Helpers::panic("Unknown file operation: %08X", cmd);
}
}
@ -66,6 +68,44 @@ void Kernel::readFile(u32 messagePointer, Handle fileHandle) {
}
}
void Kernel::writeFile(u32 messagePointer, Handle fileHandle) {
u64 offset = mem.read64(messagePointer + 4);
u32 size = mem.read32(messagePointer + 12);
u32 writeOption = mem.read32(messagePointer + 16);
u32 dataPointer = mem.read32(messagePointer + 24);
logFileIO("Trying to write %X bytes to file %X, starting from file offset %llX and 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 write closed file");
}
if (!file->fd)
Helpers::panic("[Kernel::File::WriteFile] Tried to write to file without a valid file descriptor");
std::unique_ptr<u8[]> data(new u8[size]);
for (size_t i = 0; i < size; i++) {
data[i] = mem.read8(dataPointer + i);
}
IOFile f(file->fd);
auto [success, bytesWritten] = f.writeBytes(data.get(), size);
if (!success) {
Helpers::panic("Kernel::WriteFile failed");
} else {
mem.write32(messagePointer + 4, Result::Success);
mem.write32(messagePointer + 8, bytesWritten);
}
}
void Kernel::getFileSize(u32 messagePointer, Handle fileHandle) {
logFileIO("Getting size of file %X\n", fileHandle);
@ -79,7 +119,18 @@ void Kernel::getFileSize(u32 messagePointer, Handle fileHandle) {
Helpers::panic("Tried to get size of closed file");
}
mem.write32(messagePointer + 4, Result::Success);
mem.write64(messagePointer + 8, 0); // Size here
Helpers::panic("TODO: Implement FileOp::GetSize");
if (file->fd) {
IOFile f(file->fd);
std::optional<u64> size = f.size();
if (size.has_value()) {
mem.write32(messagePointer + 4, Result::Success);
mem.write64(messagePointer + 8, size.value());
} else {
Helpers::panic("FileOp::GetFileSize failed");
}
} else {
Helpers::panic("Tried to get file size of file without file descriptor");
}
}

View file

@ -2,6 +2,8 @@
namespace PTMCommands {
enum : u32 {
GetStepHistory = 0x000B00C2,
GetTotalStepCount = 0x000C0000
};
}
@ -16,6 +18,19 @@ void PTMService::reset() {}
void PTMService::handleSyncRequest(u32 messagePointer) {
const u32 command = mem.read32(messagePointer);
switch (command) {
case PTMCommands::GetStepHistory: getStepHistory(messagePointer); break;
case PTMCommands::GetTotalStepCount: getTotalStepCount(messagePointer); break;
default: Helpers::panic("PTM service requested. Command: %08X\n", command);
}
}
void PTMService::getStepHistory(u32 messagePointer) {
log("PTM::GetStepHistory [stubbed]\n");
mem.write32(messagePointer + 4, Result::Success);
}
void PTMService::getTotalStepCount(u32 messagePointer) {
log("PTM::GetTotalStepCount\n");
mem.write32(messagePointer + 4, Result::Success);
mem.write32(messagePointer + 8, 3); // We walk a lot
}