Initial commit

This commit is contained in:
igor725 2024-04-01 15:37:43 +03:00
parent 4ecffd2f6c
commit bd3585bb03
No known key found for this signature in database
GPG Key ID: 46F13BBE46F8569D
17 changed files with 222 additions and 56 deletions

View File

@ -6,6 +6,7 @@ add_library(fileManager OBJECT
types/type_null.cpp types/type_null.cpp
types/type_file.cpp types/type_file.cpp
types/type_random.cpp types/type_random.cpp
types/type_rng.cpp
) )
add_dependencies(fileManager third_party psOff_utility) add_dependencies(fileManager third_party psOff_utility)

View File

@ -63,5 +63,23 @@ class IFile {
*/ */
virtual int64_t lseek(int64_t offset, SceWhence whence) = 0; virtual int64_t lseek(int64_t offset, SceWhence whence) = 0;
/**
* @brief Manipulates the underlying device parameters of special files.
*
* @param handle
* @param request
* @return int error code, 0:no error
*/
virtual int ioctl(int request, void* argp) = 0;
/**
* @brief Performs one of the operations on the open file descriptor.
*
* @param handle
* @param cmd
* @return int error code, 0:no error
*/
virtual int fcntl(int cmd, void* argp) = 0;
virtual void* getNative() = 0; virtual void* getNative() = 0;
}; };

View File

@ -87,6 +87,8 @@ class TypeFile: public IFile {
size_t read(void* buf, size_t nbytes) final; size_t read(void* buf, size_t nbytes) final;
size_t write(void* buf, size_t nbytes) final; size_t write(void* buf, size_t nbytes) final;
void sync() final; void sync() final;
int ioctl(int request, void* argp) final;
int fcntl(int cmd, void* argp) final;
int64_t lseek(int64_t offset, SceWhence whence) final; int64_t lseek(int64_t offset, SceWhence whence) final;
virtual void* getNative() final { return m_file; } virtual void* getNative() final { return m_file; }
@ -133,6 +135,14 @@ void TypeFile::sync() {
FlushFileBuffers(m_file); FlushFileBuffers(m_file);
} }
int TypeFile::ioctl(int request, void* argp) {
return 0;
}
int TypeFile::fcntl(int cmd, void* argp) {
return 0;
}
int64_t TypeFile::lseek(int64_t offset, SceWhence whence) { int64_t TypeFile::lseek(int64_t offset, SceWhence whence) {
static int _whence[] = { static int _whence[] = {
FILE_BEGIN, FILE_BEGIN,

View File

@ -1,10 +1,7 @@
#include "type_in.h" #include "type_in.h"
#include "logging.h"
#include "modules_include/common.h" #include "modules_include/common.h"
LOG_DEFINE_MODULE(filesystem);
class TypeIn: public IFile { class TypeIn: public IFile {
public: public:
TypeIn(): IFile(FileType::Device) {} TypeIn(): IFile(FileType::Device) {}
@ -14,8 +11,10 @@ class TypeIn: public IFile {
// ### Interface // ### Interface
size_t read(void* buf, size_t nbytes) final; size_t read(void* buf, size_t nbytes) final;
size_t write(void* buf, size_t nbytes) final; size_t write(void* buf, size_t nbytes) final;
int64_t lseek(int64_t offset, SceWhence whence) final;
void sync() final; void sync() final;
int ioctl(int request, void* argp) final;
int fcntl(int cmd, void* argp) final;
int64_t lseek(int64_t offset, SceWhence whence) final;
void* getNative() final { return nullptr; } void* getNative() final { return nullptr; }
}; };
@ -34,8 +33,16 @@ size_t TypeIn::write(void* buf, size_t nbytes) {
return nbytes; return nbytes;
} }
void TypeIn::sync() {}
int TypeIn::ioctl(int request, void* argp) {
return 0;
}
int TypeIn::fcntl(int cmd, void* argp) {
return 0;
}
int64_t TypeIn::lseek(int64_t offset, SceWhence whence) { int64_t TypeIn::lseek(int64_t offset, SceWhence whence) {
return -1; return -1;
} }
void TypeIn::sync() {}

View File

@ -11,8 +11,10 @@ class TypeNull: public IFile {
// ### Interface // ### Interface
size_t read(void* buf, size_t nbytes) final; size_t read(void* buf, size_t nbytes) final;
size_t write(void* buf, size_t nbytes) final; size_t write(void* buf, size_t nbytes) final;
int64_t lseek(int64_t offset, SceWhence whence) final;
void sync() final; void sync() final;
int ioctl(int request, void* argp) final;
int fcntl(int cmd, void* argp) final;
int64_t lseek(int64_t offset, SceWhence whence) final;
void* getNative() final { return nullptr; } void* getNative() final { return nullptr; }
}; };
@ -29,8 +31,16 @@ size_t TypeNull::write(void* buf, size_t nbytes) {
return nbytes; return nbytes;
} }
int64_t TypeNull::lseek(int64_t offset, SceWhence whence) { void TypeNull::sync() {}
int TypeNull::ioctl(int request, void* argp) {
return -1; return -1;
} }
void TypeNull::sync() {} int TypeNull::fcntl(int cmd, void* argp) {
return -1;
}
int64_t TypeNull::lseek(int64_t offset, SceWhence whence) {
return -1;
}

View File

@ -2,7 +2,7 @@
#include "logging.h" #include "logging.h"
LOG_DEFINE_MODULE(filesystem); LOG_DEFINE_MODULE(IODevice_OUT);
class TypeOut: public IFile { class TypeOut: public IFile {
const SceFileOutChannel m_channel = SCE_TYPEOUT_ERROR; const SceFileOutChannel m_channel = SCE_TYPEOUT_ERROR;
@ -15,8 +15,10 @@ class TypeOut: public IFile {
// ### Interface // ### Interface
size_t read(void* buf, size_t nbytes) final; size_t read(void* buf, size_t nbytes) final;
size_t write(void* buf, size_t nbytes) final; size_t write(void* buf, size_t nbytes) final;
int64_t lseek(int64_t offset, SceWhence whence) final;
void sync() final; void sync() final;
int ioctl(int request, void* argp) final;
int fcntl(int cmd, void* argp) final;
int64_t lseek(int64_t offset, SceWhence whence) final;
void* getNative() final { return nullptr; } void* getNative() final { return nullptr; }
}; };
@ -30,7 +32,7 @@ size_t TypeOut::read(void* buf, size_t nbytes) {
} }
size_t TypeOut::write(void* buf, size_t nbytes) { size_t TypeOut::write(void* buf, size_t nbytes) {
LOG_USE_MODULE(filesystem); LOG_USE_MODULE(IODevice_OUT);
std::string str((const char*)buf, nbytes); std::string str((const char*)buf, nbytes);
while (str.back() == '\n') while (str.back() == '\n')
@ -51,8 +53,16 @@ size_t TypeOut::write(void* buf, size_t nbytes) {
return nbytes; return nbytes;
} }
void TypeOut::sync() {}
int TypeOut::ioctl(int request, void* argp) {
return 0;
}
int TypeOut::fcntl(int cmd, void* argp) {
return 0;
}
int64_t TypeOut::lseek(int64_t offset, SceWhence whence) { int64_t TypeOut::lseek(int64_t offset, SceWhence whence) {
return -1; return -1;
} }
void TypeOut::sync() {}

View File

@ -9,8 +9,10 @@ class TypeRandom: public IFile {
// ### Interface // ### Interface
size_t read(void* buf, size_t nbytes) final; size_t read(void* buf, size_t nbytes) final;
size_t write(void* buf, size_t nbytes) final; size_t write(void* buf, size_t nbytes) final;
int64_t lseek(int64_t offset, SceWhence whence) final;
void sync() final; void sync() final;
int ioctl(int request, void* argp) final;
int fcntl(int cmd, void* argp) final;
int64_t lseek(int64_t offset, SceWhence whence) final;
void* getNative() final { return nullptr; } void* getNative() final { return nullptr; }
}; };
@ -33,8 +35,16 @@ size_t TypeRandom::write(void* buf, size_t nbytes) {
return 0; return 0;
} }
void TypeRandom::sync() {}
int TypeRandom::ioctl(int request, void* argp) {
return 0;
}
int TypeRandom::fcntl(int cmd, void* argp) {
return 0;
}
int64_t TypeRandom::lseek(int64_t offset, SceWhence whence) { int64_t TypeRandom::lseek(int64_t offset, SceWhence whence) {
return -1; return -1;
} }
void TypeRandom::sync() {}

View File

@ -0,0 +1,63 @@
#include "logging.h"
#include "type_random.h"
LOG_DEFINE_MODULE(IODevice_RNG);
class TypeRNG: public IFile {
public:
TypeRNG(): IFile(FileType::Device) {}
virtual ~TypeRNG() {}
// ### Interface
size_t read(void* buf, size_t nbytes) final;
size_t write(void* buf, size_t nbytes) final;
void sync() final;
int ioctl(int request, void* argp) final;
int fcntl(int cmd, void* argp) final;
int64_t lseek(int64_t offset, SceWhence whence) final;
void* getNative() final { return nullptr; }
};
std::unique_ptr<IFile> createType_rng() {
std::srand(std::time(nullptr));
return std::make_unique<TypeRNG>();
}
size_t TypeRNG::read(void* buf, size_t nbytes) {
auto cbuf = static_cast<char*>(buf);
for (size_t i = 0; i < nbytes; i++)
cbuf[i] = std::rand() & 0xFF;
return nbytes;
}
size_t TypeRNG::write(void* buf, size_t nbytes) {
return 0;
}
void TypeRNG::sync() {}
int TypeRNG::ioctl(int request, void* argp) {
LOG_USE_MODULE(IODevice_RNG);
switch (request) {
// These are unknown for now
case 0x40445301: break;
case 0x40445302: break;
default: LOG_ERR(L"Unknown ioctl request to /dev/rng: %d", request); return -1;
}
return 0;
}
int TypeRNG::fcntl(int cmd, void* argp) {
return 0;
}
int64_t TypeRNG::lseek(int64_t offset, SceWhence whence) {
return -1;
}

View File

@ -0,0 +1,7 @@
#pragma once
#include "../ifile.h"
#include <memory>
std::unique_ptr<IFile> createType_rng();

View File

@ -9,8 +9,10 @@ class TypeZero: public IFile {
// ### Interface // ### Interface
size_t read(void* buf, size_t nbytes) final; size_t read(void* buf, size_t nbytes) final;
size_t write(void* buf, size_t nbytes) final; size_t write(void* buf, size_t nbytes) final;
int64_t lseek(int64_t offset, SceWhence whence) final;
void sync() final; void sync() final;
int ioctl(int request, void* argp) final;
int fcntl(int cmd, void* argp) final;
int64_t lseek(int64_t offset, SceWhence whence) final;
void* getNative() final { return nullptr; } void* getNative() final { return nullptr; }
}; };
@ -31,8 +33,16 @@ size_t TypeZero::write(void* buf, size_t nbytes) {
return 0; return 0;
} }
void TypeZero::sync() {}
int TypeZero::ioctl(int request, void* argp) {
return 0;
}
int TypeZero::fcntl(int cmd, void* argp) {
return 0;
}
int64_t TypeZero::lseek(int64_t offset, SceWhence whence) { int64_t TypeZero::lseek(int64_t offset, SceWhence whence) {
return -1; return -1;
} }
void TypeZero::sync() {}

View File

@ -4,6 +4,7 @@
#include "core/fileManager/fileManager.h" #include "core/fileManager/fileManager.h"
#include "core/fileManager/types/type_file.h" #include "core/fileManager/types/type_file.h"
#include "core/fileManager/types/type_random.h" #include "core/fileManager/types/type_random.h"
#include "core/fileManager/types/type_rng.h"
#include "core/memory/memory.h" #include "core/memory/memory.h"
#include "logging.h" #include "logging.h"
@ -35,6 +36,8 @@ std::unique_ptr<IFile> createType_dev(std::filesystem::path path, std::ios_base:
// todo: /dev/rng? No ioctl for now // todo: /dev/rng? No ioctl for now
if (path == "/dev/urandom" || path == "/dev/urandom") { if (path == "/dev/urandom" || path == "/dev/urandom") {
return createType_random(); return createType_random();
} else if (path == "/dev/rng") {
return createType_rng();
} else { // todo: other devices } else { // todo: other devices
LOG_CRIT(L"%S: unknown device!", path.c_str()); LOG_CRIT(L"%S: unknown device!", path.c_str());
} }
@ -196,6 +199,26 @@ int64_t write(int handle, const void* buf, size_t nbytes) {
return count; return count;
} }
int ioctl(int handle, int request, void* argp) {
LOG_USE_MODULE(filesystem);
auto file = accessFileManager().accessFile(handle);
if (file == nullptr) {
LOG_ERR(L"KernelIoctl[%d] file==nullptr: request:0x%08llx argp:0x%08llx", handle, request, (uint64_t)argp);
return getErr(ErrCode::_EBADF);
}
int const ret = file->ioctl(request, argp);
LOG_TRACE(L"KernelIoctl[%d] request:0x%08llx argp:0x%08llx, ret:%d", handle, request, (uint64_t)argp, ret);
if (auto err = file->getErr(); err != Ok) {
return getErr((ErrCode)err);
}
return ret;
}
int open(const char* path, SceOpen flags, SceKernelMode kernelMode) { int open(const char* path, SceOpen flags, SceKernelMode kernelMode) {
LOG_USE_MODULE(filesystem); LOG_USE_MODULE(filesystem);
@ -329,10 +352,24 @@ int fdatasync(int fd) {
return Ok; return Ok;
} }
int fcntl(int fd, int cmd, va_list args) { int fcntl(int handle, int cmd, void* argp) {
LOG_USE_MODULE(filesystem); LOG_USE_MODULE(filesystem);
LOG_ERR(L"todo %S %d", __FUNCTION__, fd);
return Ok; auto file = accessFileManager().accessFile(handle);
if (file == nullptr) {
LOG_ERR(L"KernelFcntl[%d] file==nullptr: cmd:0x%08lx argp:0x%08llx", handle, cmd, (uint64_t)argp);
return getErr(ErrCode::_EBADF);
}
int const ret = file->fcntl(cmd, argp);
LOG_TRACE(L"KernelFcntl[%d] request:0x%08llx argp:0x%08lx, ret:%d", handle, cmd, (uint64_t)argp, ret);
if (auto err = file->getErr(); err != Ok) {
return getErr((ErrCode)err);
}
return ret;
} }
size_t readv(int handle, const SceKernelIovec* iov, int iovcnt) { size_t readv(int handle, const SceKernelIovec* iov, int iovcnt) {

View File

@ -97,6 +97,7 @@ typedef struct Sce_iovec SceKernelIovec;
__APICALL int mmap(void* addr, size_t len, int prot, SceMap flags, int fd, int64_t offset, void** res); __APICALL int mmap(void* addr, size_t len, int prot, SceMap flags, int fd, int64_t offset, void** res);
__APICALL int munmap(void* address, size_t len); __APICALL int munmap(void* address, size_t len);
__APICALL size_t read(int handle, void* buf, size_t nbytes); __APICALL size_t read(int handle, void* buf, size_t nbytes);
__APICALL int ioctl(int handle, int request, void* argp);
__APICALL int64_t write(int handle, const void* buf, size_t nbytes); __APICALL int64_t write(int handle, const void* buf, size_t nbytes);
__APICALL int open(const char* path, SceOpen flags, SceKernelMode kernelMode); __APICALL int open(const char* path, SceOpen flags, SceKernelMode kernelMode);
__APICALL int close(int handle); __APICALL int close(int handle);
@ -106,7 +107,7 @@ __APICALL int checkReachability(const char* path);
__APICALL void sync(void); __APICALL void sync(void);
__APICALL int fsync(int handle); __APICALL int fsync(int handle);
__APICALL int fdatasync(int fd); __APICALL int fdatasync(int fd);
__APICALL int fcntl(int fd, int cmd, va_list args); __APICALL int fcntl(int fd, int cmd, void* args);
__APICALL size_t readv(int handle, const SceKernelIovec* iov, int iovcnt); __APICALL size_t readv(int handle, const SceKernelIovec* iov, int iovcnt);
__APICALL size_t writev(int handle, const SceKernelIovec* iov, int iovcnt); __APICALL size_t writev(int handle, const SceKernelIovec* iov, int iovcnt);
__APICALL int fchmod(int fd, SceKernelMode mode); __APICALL int fchmod(int fd, SceKernelMode mode);
@ -134,4 +135,4 @@ __APICALL int64_t lwfsLseek(int fd, int64_t offset, int whence);
__APICALL size_t lwfsWrite(int fd, const void* buf, size_t nbytes); __APICALL size_t lwfsWrite(int fd, const void* buf, size_t nbytes);
#undef __APICALL #undef __APICALL
}; // namespace filesystem }; // namespace filesystem

View File

@ -4,8 +4,6 @@
#include "logging.h" #include "logging.h"
#include "types.h" #include "types.h"
#include <stdarg.h>
LOG_DEFINE_MODULE(fs_posix); LOG_DEFINE_MODULE(fs_posix);
extern "C" { extern "C" {
@ -18,6 +16,10 @@ EXPORT SYSV_ABI int64_t __NID(write)(int handle, const void* buf, size_t nbytes)
return POSIX_CALL(filesystem::write(handle, buf, nbytes)); return POSIX_CALL(filesystem::write(handle, buf, nbytes));
} }
EXPORT SYSV_ABI int __NID(ioctl)(int handle, int request, void* argp) {
return POSIX_CALL(filesystem::ioctl(handle, request, argp));
}
EXPORT SYSV_ABI int __NID(open)(const char* path, filesystem::SceOpen flags, filesystem::SceKernelMode kernelMode) { EXPORT SYSV_ABI int __NID(open)(const char* path, filesystem::SceOpen flags, filesystem::SceKernelMode kernelMode) {
return POSIX_CALL(filesystem::open(path, flags, kernelMode)); return POSIX_CALL(filesystem::open(path, flags, kernelMode));
} }
@ -42,16 +44,8 @@ EXPORT SYSV_ABI int __NID(fsync)(int handle) {
return POSIX_CALL(filesystem::fsync(handle)); return POSIX_CALL(filesystem::fsync(handle));
} }
EXPORT SYSV_ABI int __NID(fcntl)(int fd, int cmd, ...) { EXPORT SYSV_ABI int __NID(fcntl)(int handle, int cmd, void* argp) {
LOG_USE_MODULE(fs_posix); return POSIX_CALL(filesystem::fcntl(handle, cmd, argp));
LOG_ERR(L"todo %S", __FUNCTION__);
// todo own va_start etc.. for sysv_abi
// va_list args;
// va_start(args, cmd);
// return filesystem::fcntl(fd, cmd, args);
// va_end(args);
return Ok;
} }
EXPORT SYSV_ABI size_t __NID(readv)(int handle, const filesystem::SceKernelIovec* iov, int iovcnt) { EXPORT SYSV_ABI size_t __NID(readv)(int handle, const filesystem::SceKernelIovec* iov, int iovcnt) {
@ -129,4 +123,4 @@ EXPORT SYSV_ABI int __NID(truncate)(const char* path, int64_t length) {
EXPORT SYSV_ABI int __NID(ftruncate)(int fd, int64_t length) { EXPORT SYSV_ABI int __NID(ftruncate)(int fd, int64_t length) {
return POSIX_CALL(filesystem::ftruncate(fd, length)); return POSIX_CALL(filesystem::ftruncate(fd, length));
} }
} }

View File

@ -5,8 +5,6 @@
#include "logging.h" #include "logging.h"
#include "types.h" #include "types.h"
#include <stdarg.h>
LOG_DEFINE_MODULE(mman_posix); LOG_DEFINE_MODULE(mman_posix);
extern "C" { extern "C" {

View File

@ -6,8 +6,6 @@
#include "logging.h" #include "logging.h"
#include "types.h" #include "types.h"
#include <stdarg.h>
LOG_DEFINE_MODULE(pthread); LOG_DEFINE_MODULE(pthread);
extern "C" { extern "C" {

View File

@ -4,8 +4,6 @@
#include "logging.h" #include "logging.h"
#include "types.h" #include "types.h"
#include <stdarg.h>
LOG_DEFINE_MODULE(fs); LOG_DEFINE_MODULE(fs);
extern "C" { extern "C" {
@ -54,16 +52,8 @@ EXPORT SYSV_ABI int sceKernelFdatasync(int fd) {
return filesystem::fdatasync(fd); return filesystem::fdatasync(fd);
} }
EXPORT SYSV_ABI int sceKernelFcntl(int fd, int cmd, ...) { EXPORT SYSV_ABI int sceKernelFcntl(int handle, int cmd, void* argp) {
LOG_USE_MODULE(fs); return POSIX_CALL(filesystem::fcntl(handle, cmd, argp));
LOG_ERR(L"todo %S", __FUNCTION__);
// todo own va_start etc.. for sysv_abi
// va_list args;
// va_start(args, cmd);
// return filesystem::fcntl(fd, cmd, args);
// va_end(args);
return Ok;
} }
EXPORT SYSV_ABI size_t sceKernelReadv(int handle, const filesystem::SceKernelIovec* iov, int iovcnt) { EXPORT SYSV_ABI size_t sceKernelReadv(int handle, const filesystem::SceKernelIovec* iov, int iovcnt) {
@ -146,6 +136,10 @@ EXPORT SYSV_ABI int __NID(_write)(int handle, const void* buf, size_t nbytes) {
return POSIX_CALL(filesystem::write(handle, buf, nbytes)); return POSIX_CALL(filesystem::write(handle, buf, nbytes));
} }
EXPORT SYSV_ABI int __NID(_ioctl)(int handle, int request, void* argp) {
return POSIX_CALL(filesystem::ioctl(handle, request, argp));
}
EXPORT SYSV_ABI int sceKernelMmap(void* addr, size_t len, int prot, filesystem::SceMap flags, int fd, int64_t offset, void** res) { EXPORT SYSV_ABI int sceKernelMmap(void* addr, size_t len, int prot, filesystem::SceMap flags, int fd, int64_t offset, void** res) {
return filesystem::mmap(addr, len, prot, flags, fd, offset, res); return filesystem::mmap(addr, len, prot, flags, fd, offset, res);
} }

View File

@ -4,8 +4,6 @@
#include "logging.h" #include "logging.h"
#include "types.h" #include "types.h"
#include <stdarg.h>
LOG_DEFINE_MODULE(pthread); LOG_DEFINE_MODULE(pthread);
extern "C" { extern "C" {