2022-10-10 18:21:39 +00:00
|
|
|
#define MODULE "stdio"
|
|
|
|
|
|
|
|
#include "errno.h"
|
2022-09-29 17:17:43 +00:00
|
|
|
#include "interrupts/Context.h"
|
|
|
|
#include "io/Serial.h"
|
2022-10-10 18:21:39 +00:00
|
|
|
#include "log/Log.h"
|
2022-10-01 12:27:45 +00:00
|
|
|
#include "render/TextRenderer.h"
|
2022-10-10 18:21:39 +00:00
|
|
|
#include "thread/Scheduler.h"
|
|
|
|
#include "thread/Task.h"
|
|
|
|
|
2022-10-11 19:06:12 +00:00
|
|
|
#define OPEN_READ 1
|
|
|
|
#define OPEN_WRITE 2
|
|
|
|
|
2022-10-12 13:28:52 +00:00
|
|
|
#define SEEK_SET 0
|
|
|
|
#define SEEK_CUR 1
|
|
|
|
#define SEEK_END 2
|
|
|
|
|
2022-10-10 18:21:39 +00:00
|
|
|
#define STDIO_FAIL(function, error) kwarnln("%s failed with %s", #function, #error)
|
2022-09-29 17:17:43 +00:00
|
|
|
|
2022-10-12 13:28:52 +00:00
|
|
|
void sys_seek(Context* context, int fd, long offset, int whence)
|
|
|
|
{
|
|
|
|
if (whence < SEEK_SET || whence > SEEK_END)
|
|
|
|
{
|
|
|
|
STDIO_FAIL(seek, EINVAL);
|
|
|
|
context->rax = -EINVAL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fd >= TASK_MAX_FDS || fd < 0)
|
|
|
|
{
|
|
|
|
STDIO_FAIL(seek, EBADF);
|
|
|
|
context->rax = -EBADF;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Task* current_task = Scheduler::current_task();
|
|
|
|
if (!current_task->files[fd].is_open())
|
|
|
|
{
|
|
|
|
STDIO_FAIL(seek, EBADF);
|
|
|
|
context->rax = -EBADF;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
long new_offset;
|
|
|
|
if (whence == SEEK_SET) new_offset = offset;
|
|
|
|
else if (whence == SEEK_CUR)
|
|
|
|
new_offset = offset + current_task->files[fd].offset();
|
|
|
|
else if (whence == SEEK_END)
|
|
|
|
new_offset = current_task->files[fd].length() + offset;
|
|
|
|
else
|
|
|
|
__builtin_unreachable();
|
|
|
|
if (new_offset < 0)
|
|
|
|
{
|
|
|
|
STDIO_FAIL(seek, EINVAL);
|
|
|
|
context->rax = -EINVAL; // FIXME: Is this the right error?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int result = current_task->files[fd].seek(new_offset);
|
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
context->rax = result;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
context->rax = new_offset;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-11 19:06:12 +00:00
|
|
|
void sys_write(Context* context, int fd, size_t size, const char* addr)
|
2022-09-29 17:17:43 +00:00
|
|
|
{
|
2022-10-11 19:06:12 +00:00
|
|
|
if (!addr)
|
|
|
|
{
|
|
|
|
STDIO_FAIL(write, EINVAL);
|
|
|
|
context->rax = -EINVAL; // FIXME: This should probably return EFAULT.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fd >= TASK_MAX_FDS || fd < 0)
|
|
|
|
{
|
|
|
|
STDIO_FAIL(write, EBADF);
|
|
|
|
context->rax = -EBADF;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Task* current_task = Scheduler::current_task();
|
|
|
|
if (!current_task->files[fd].is_open())
|
|
|
|
{
|
|
|
|
STDIO_FAIL(write, EBADF);
|
|
|
|
context->rax = -EBADF;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!current_task->files[fd].can_write())
|
|
|
|
{
|
|
|
|
STDIO_FAIL(write, EBADF);
|
|
|
|
context->rax = -EBADF;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ssize_t result = current_task->files[fd].write(size, addr);
|
|
|
|
context->rax = (size_t)result;
|
|
|
|
return;
|
2022-10-10 18:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sys_open(Context* context, const char* filename, int flags)
|
|
|
|
{
|
|
|
|
Task* current_task = Scheduler::current_task();
|
2022-10-11 15:03:16 +00:00
|
|
|
int fd;
|
2022-10-10 18:21:39 +00:00
|
|
|
for (fd = 0; fd < TASK_MAX_FDS; fd++)
|
|
|
|
{
|
|
|
|
if (!current_task->files[fd].is_open()) break;
|
|
|
|
}
|
2022-10-11 19:06:12 +00:00
|
|
|
|
2022-10-11 15:03:16 +00:00
|
|
|
if (fd == TASK_MAX_FDS)
|
2022-10-10 18:21:39 +00:00
|
|
|
{
|
|
|
|
STDIO_FAIL(open, EMFILE);
|
|
|
|
context->rax = -EMFILE;
|
|
|
|
return;
|
|
|
|
}
|
2022-10-11 19:06:12 +00:00
|
|
|
|
2022-10-10 18:21:39 +00:00
|
|
|
VFS::Node* node = VFS::resolve_path(filename);
|
|
|
|
if (!node)
|
|
|
|
{
|
|
|
|
STDIO_FAIL(open, ENOENT);
|
|
|
|
context->rax = -ENOENT;
|
|
|
|
return;
|
|
|
|
}
|
2022-10-11 19:06:12 +00:00
|
|
|
|
|
|
|
bool can_read = (flags & OPEN_READ) > 0;
|
|
|
|
bool can_write = (flags & OPEN_WRITE) > 0;
|
|
|
|
if (!can_read && !can_write)
|
|
|
|
{
|
|
|
|
STDIO_FAIL(open, EINVAL);
|
|
|
|
context->rax = -EINVAL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
kdbgln("open(): opening %s %s, allocated file descriptor %d", filename,
|
|
|
|
(can_read && can_write) ? "rw"
|
|
|
|
: can_read ? "r-"
|
|
|
|
: "-w",
|
|
|
|
fd);
|
|
|
|
|
|
|
|
current_task->files[fd].open(node, can_read, can_write);
|
2022-10-10 18:21:39 +00:00
|
|
|
context->rax = fd;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sys_read(Context* context, int fd, size_t size, char* buffer)
|
|
|
|
{
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
STDIO_FAIL(read, EINVAL);
|
2022-10-11 15:10:44 +00:00
|
|
|
context->rax = -EINVAL; // FIXME: This should probably return EFAULT.
|
2022-10-10 18:21:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-10-11 15:10:44 +00:00
|
|
|
if (fd >= TASK_MAX_FDS || fd < 0)
|
2022-10-10 18:21:39 +00:00
|
|
|
{
|
|
|
|
STDIO_FAIL(read, EBADF);
|
|
|
|
context->rax = -EBADF;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Task* current_task = Scheduler::current_task();
|
2022-10-11 15:10:44 +00:00
|
|
|
if (!current_task->files[fd].is_open() || !current_task->files[fd].can_read())
|
2022-10-10 18:21:39 +00:00
|
|
|
{
|
|
|
|
STDIO_FAIL(read, EBADF);
|
|
|
|
context->rax = -EBADF;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ssize_t result = current_task->files[fd].read(size, buffer);
|
|
|
|
context->rax = (size_t)result;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sys_close(Context* context, int fd)
|
|
|
|
{
|
2022-10-11 15:10:44 +00:00
|
|
|
if (fd >= TASK_MAX_FDS || fd < 0)
|
2022-10-10 18:21:39 +00:00
|
|
|
{
|
|
|
|
STDIO_FAIL(close, EBADF);
|
|
|
|
context->rax = -EBADF;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Task* current_task = Scheduler::current_task();
|
|
|
|
if (!current_task->files[fd].is_open())
|
|
|
|
{
|
|
|
|
STDIO_FAIL(close, EBADF);
|
|
|
|
context->rax = -EBADF;
|
|
|
|
return;
|
|
|
|
}
|
2022-10-11 15:19:03 +00:00
|
|
|
kdbgln("close(): releasing file descriptor %d", fd);
|
2022-10-10 18:21:39 +00:00
|
|
|
current_task->files[fd].close();
|
|
|
|
context->rax = 0;
|
|
|
|
return;
|
2022-09-29 17:17:43 +00:00
|
|
|
}
|