From c31c00e8cd35c79ae04fb23d4e6fc1c53783b40a Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Fri, 5 Sep 2025 23:00:57 +0200 Subject: [PATCH] Nice wrappers around ioctl() syscall --- ulib/system/ioctl.c | 24 ++++++++++++++++++++++++ ulib/system/ioctl.h | 12 ++++++++++++ user/init/main.c | 17 ++++++++--------- 3 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 ulib/system/ioctl.c create mode 100644 ulib/system/ioctl.h diff --git a/ulib/system/ioctl.c b/ulib/system/ioctl.c new file mode 100644 index 0000000..b772cea --- /dev/null +++ b/ulib/system/ioctl.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#include + +int32_t ioctl_openfile(const char *path, uint64_t flags) { + IOCtlOF cfg = { .path = path, .flags = flags }; + return ioctl(IOCTL_NOHANDLE, IOCTL_OPENF, &cfg); +} + +int32_t ioctl_writefile(int32_t ioh, const uint8_t *const buffer, size_t len, size_t off) { + IOCtlWF cfg = { .buffer = buffer, .len = len, .off = off }; + return ioctl(ioh, IOCTL_WRITE, &cfg); +} + +int32_t ioctl_readfile(int32_t ioh, uint8_t *const buffer, size_t len, size_t off) { + IOCtlRF cfg = { .buffer = buffer, .len = len, off = off }; + return ioctl(ioh, IOCTL_READ, &cfg); +} + +int32_t ioctl_closefile(int32_t ioh) { + return ioctl(ioh, IOCTL_CLOSEF, NULL); +} diff --git a/ulib/system/ioctl.h b/ulib/system/ioctl.h new file mode 100644 index 0000000..5edc011 --- /dev/null +++ b/ulib/system/ioctl.h @@ -0,0 +1,12 @@ +#ifndef ULIB_SYSTEM_IOCTL_H_ +#define ULIB_SYSTEM_IOCTL_H_ + +#include +#include + +int32_t ioctl_openfile(const char *path, uint64_t flags); +int32_t ioctl_writefile(int32_t ioh, const uint8_t *const buffer, size_t len, size_t off); +int32_t ioctl_readfile(int32_t ioh, uint8_t *const buffer, size_t len, size_t off); +int32_t ioctl_closefile(int32_t ioh); + +#endif // ULIB_SYSTEM_IOCTL_H_ diff --git a/user/init/main.c b/user/init/main.c index e4c41a2..664a8e1 100644 --- a/user/init/main.c +++ b/user/init/main.c @@ -1,23 +1,22 @@ -#include +#include #include +#include +#include #include void main(void) { debugprint("Hello world from userspace in C"); - IOCtlOF ioctlof = { .path = "base:/hello.txt", .flags = IOCTL_F_WRITE | IOCTL_F_READ | IOCTL_F_MAKE }; - int32_t ioh = ioctl(IOCTL_NOHANDLE, IOCTL_OPENF, &ioctlof); + int32_t ioh = ioctl_openfile("base:/hello.txt", IOCTL_F_WRITE | IOCTL_F_READ | IOCTL_F_MAKE); - char *text = "Write to a file"; - IOCtlWF ioctlwf = { .buffer = text, .len = string_len(text), .off = 0 }; - ioctl(ioh, IOCTL_WRITE, &ioctlwf); + char *text = "Hello from the filesystem"; + ioctl_writefile(ioh, (const uint8_t *const)text, string_len(text), 0); char buf[0x100]; - IOCtlRF ioctlrf = { .buffer = buf, .len = string_len(text), .off = 0 }; - ioctl(ioh, IOCTL_READ, &ioctlrf); + ioctl_readfile(ioh, (uint8_t *const)buf, string_len(text), 0); debugprint(buf); - ioctl(ioh, IOCTL_CLOSEF, NULL); + ioctl_closefile(ioh); }