summaryrefslogtreecommitdiff
path: root/src/user/driver
diff options
context:
space:
mode:
authordzwdz2022-07-24 15:07:57 +0200
committerdzwdz2022-07-24 15:07:57 +0200
commit9f3fdb830f61cd8c8c1f1db9d03cba1c546c1a7e (patch)
treef5ec030c736f5201332c51cb99169b288185b84b /src/user/driver
parent0228be3fd404cdebecf6d21b8964f6063f12dfbe (diff)
user: change the directory structure to prepare for multiple binaries
Diffstat (limited to 'src/user/driver')
-rw-r--r--src/user/driver/ansiterm.c99
-rw-r--r--src/user/driver/driver.h7
-rw-r--r--src/user/driver/ps2.c86
-rw-r--r--src/user/driver/termcook.c71
-rw-r--r--src/user/driver/tmpfs.c132
5 files changed, 0 insertions, 395 deletions
diff --git a/src/user/driver/ansiterm.c b/src/user/driver/ansiterm.c
deleted file mode 100644
index 4b9f0d0..0000000
--- a/src/user/driver/ansiterm.c
+++ /dev/null
@@ -1,99 +0,0 @@
-#include <user/driver/driver.h>
-#include <shared/syscalls.h>
-#include <stdbool.h>
-
-struct vga_cell {
- unsigned char c;
- unsigned char style;
-} __attribute__((__packed__));
-static struct vga_cell vga[80 * 25];
-
-static handle_t vga_fd;
-
-static struct {int x, y;} cursor = {0};
-static bool dirty = false;
-static bool pendingFlush = false;
-
-static void flush(void) {
- size_t off = 0;
- /* we have to do multiple write() calls if we're behind a shitty passthrough fs
- * i don't like this either */
- while (off < sizeof(vga))
- off += _syscall_write(vga_fd, (void*)vga + off, sizeof(vga) - off, off);
- dirty = false;
- pendingFlush = false;
-}
-
-static void scroll(void) {
- for (size_t i = 0; i < 80 * 24; i++)
- vga[i] = vga[i + 80];
- for (size_t i = 80 * 24; i < 80 * 25; i++)
- vga[i].c = ' ';
- cursor.y--;
- pendingFlush = true;
-}
-
-static void in_char(char c) {
- switch (c) {
- case '\n':
- cursor.x = 0;
- cursor.y++;
- pendingFlush = true;
- break;
- case '\b':
- if (--cursor.x < 0) cursor.x = 0;
- break;
- default:
- vga[cursor.y * 80 + cursor.x++].c = c;
- }
-
- if (cursor.x >= 80) {
- cursor.x = 0;
- cursor.y++;
- }
- while (cursor.y >= 25) scroll();
- dirty = true;
-}
-
-void ansiterm_drv(void) {
- vga_fd = _syscall_open("/kdev/vga", 9, 0);
- _syscall_read(vga_fd, vga, sizeof vga, 0);
-
- // find first empty line
- for (cursor.y = 0; cursor.y < 25; cursor.y++) {
- for (cursor.x = 0; cursor.x < 80; cursor.x++) {
- char c = vga[cursor.y * 80 + cursor.x].c;
- if (c != ' ' && c != '\0') break;
- }
- if (cursor.x == 80) break;
- }
- cursor.x = 0;
-
- for (int i = 0; i < 80 * 25; i++)
- vga[i].style = 0x70;
- flush();
-
- static char buf[512];
- struct fs_wait_response res;
- while (!_syscall_fs_wait(buf, sizeof buf, &res)) {
- switch (res.op) {
- case VFSOP_OPEN:
- // TODO check path
- _syscall_fs_respond(NULL, 0, 0);
- break;
-
- case VFSOP_WRITE:
- for (size_t i = 0; i < res.len; i++)
- in_char(buf[i]);
- /* if (pendingFlush) */ flush();
- _syscall_fs_respond(NULL, res.len, 0);
- break;
-
- default:
- _syscall_fs_respond(NULL, -1, 0);
- break;
- }
- }
-
- _syscall_exit(1);
-}
diff --git a/src/user/driver/driver.h b/src/user/driver/driver.h
deleted file mode 100644
index d6e7343..0000000
--- a/src/user/driver/driver.h
+++ /dev/null
@@ -1,7 +0,0 @@
-#pragma once
-
-void ansiterm_drv(void);
-void ps2_drv(void);
-void tmpfs_drv(void);
-
-void termcook(void);
diff --git a/src/user/driver/ps2.c b/src/user/driver/ps2.c
deleted file mode 100644
index fd81882..0000000
--- a/src/user/driver/ps2.c
+++ /dev/null
@@ -1,86 +0,0 @@
-#include <user/driver/driver.h>
-#include <shared/container/ring.h>
-#include <shared/syscalls.h>
-#include <stdbool.h>
-
-
-static const char keymap_lower[] = {
- '\0', '\0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b', '\t',
- 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\r', '\0', 'a', 's',
- 'd', 'f', 'g', 'h', 'j', 'k', 'l', '\0', '\'', '`', '\0', '\\', 'z', 'x', 'c', 'v',
- 'b', 'n', 'm', ',', '.', '/', '\0', '*', '\0', ' ', '\0', '\0', '\0', '\0', '\0', '\0',
- '\0', '\0', '\0', '\0', '\0', '\0', '\0', '7', '8', '9', '-', '4', '5', '6', '+', '1',
- '2', '3', '0', '.', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
- '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
- '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
-};
-
-static const char keymap_upper[] = {
- '\0', '\0', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '\b', '\t',
- 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\r', '\0', 'A', 'S',
- 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', '\0', '|', 'Z', 'X', 'C', 'V',
- 'B', 'N', 'M', '<', '>', '?', '\0', '*', '\0', ' ', '\0', '\0', '\0', '\0', '\0', '\0',
- '\0', '\0', '\0', '\0', '\0', '\0', '\0', '7', '8', '9', '-', '4', '5', '6', '+', '1',
- '2', '3', '0', '.', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
- '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
- '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
-};
-
-
-static volatile uint8_t backlog_buf[16];
-static volatile ring_t backlog = {(void*)backlog_buf, sizeof backlog_buf, 0, 0};
-
-static handle_t fd;
-
-static bool keys[0x80] = {0};
-
-static void parse_scancode(uint8_t s) {
- bool ctrl = keys[0x1D];
- bool shift = keys[0x2A] || keys[0x36];
- bool down = !(s & 0x80);
- char c;
- s &= 0x7f;
- keys[s] = down;
-
- c = shift ? keymap_upper[s] : keymap_lower[s];
- if (ctrl && keymap_upper[s] >= 'A' && keymap_upper[s] <= 'Z')
- c = keymap_upper[s] - 'A' + 1;
- if (down && c) ring_put1b((void*)&backlog, c);
-}
-
-static void main_loop(void) {
- static char buf[512];
- struct fs_wait_response res;
- int ret;
- while (!_syscall_fs_wait(buf, sizeof buf, &res)) {
- switch (res.op) {
- case VFSOP_OPEN:
- _syscall_fs_respond(NULL, 1, 0);
- break;
-
- case VFSOP_READ:
- while (ring_size((void*)&backlog) == 0) {
- /* read raw input until we have something to output */
- int len = _syscall_read(fd, buf, sizeof buf, 0);
- if (len == 0) break;
- for (int i = 0; i < len; i++)
- parse_scancode(buf[i]);
- }
- ret = ring_get((void*)&backlog, buf, res.capacity);
- _syscall_fs_respond(buf, ret, 0);
- break;
-
- default:
- _syscall_fs_respond(NULL, -1, 0);
- break;
- }
- }
-}
-
-void ps2_drv(void) {
- fd = _syscall_open("/kdev/ps2", 9, 0);
- if (fd < 0) _syscall_exit(1);
-
- main_loop();
- _syscall_exit(0);
-}
diff --git a/src/user/driver/termcook.c b/src/user/driver/termcook.c
deleted file mode 100644
index 56ac77c..0000000
--- a/src/user/driver/termcook.c
+++ /dev/null
@@ -1,71 +0,0 @@
-#include <shared/syscalls.h>
-#include <user/lib/stdlib.h>
-#include <user/driver/driver.h>
-
-static void w_output(handle_t output, const char *buf, size_t len) {
- size_t pos = 0;
- while (pos < len) {
- int ret = _syscall_write(output, buf + pos, len - pos, pos);
- if (ret < 0) break;
- pos += ret;
- }
-}
-
-static void line_editor(handle_t input, handle_t output) {
- char readbuf[16], linebuf[256];
- size_t linepos = 0;
- for (;;) {
- int readlen = _syscall_read(input, readbuf, sizeof readbuf, -1);
- if (readlen < 0) return;
- for (int i = 0; i < readlen; i++) {
- char c = readbuf[i];
- switch (c) {
- case '\b':
- case 0x7f:
- if (linepos != 0) {
- printf("\b \b");
- linepos--;
- }
- break;
- case 4: /* EOT, C-d */
- if (linepos > 0) {
- w_output(output, linebuf, linepos);
- linepos = 0;
- } else {
- _syscall_write(output, NULL, 0, 0); // eof
- }
- break;
- case '\n':
- case '\r':
- printf("\n");
- if (linepos < sizeof linebuf)
- linebuf[linepos++] = '\n';
- w_output(output, linebuf, linepos);
- linepos = 0;
- break;
- default:
- if (linepos < sizeof linebuf) {
- linebuf[linepos++] = c;
- printf("%c", c);
- }
- break;
- }
- }
- }
-}
-
-void termcook(void) {
- handle_t stdin_pipe[2] = {-1, -1};
- if (_syscall_pipe(stdin_pipe, 0) < 0)
- return;
-
- if (!fork()) {
- close(stdin_pipe[0]);
- line_editor(0, stdin_pipe[1]);
- _syscall_exit(0);
- }
- /* 0 is stdin, like in unix */
- _syscall_dup(stdin_pipe[0], 0, 0);
- close(stdin_pipe[0]);
- close(stdin_pipe[1]);
-}
diff --git a/src/user/driver/tmpfs.c b/src/user/driver/tmpfs.c
deleted file mode 100644
index 6e5b45e..0000000
--- a/src/user/driver/tmpfs.c
+++ /dev/null
@@ -1,132 +0,0 @@
-#include <user/lib/malloc.h>
-#include <shared/mem.h>
-#include <shared/syscalls.h>
-#include <stddef.h>
-
-struct node {
- const char *name;
- size_t namelen;
- struct node *next;
- char *buf;
- size_t size, capacity;
-};
-
-struct node *root = NULL;
-static struct node special_root;
-
-static struct node *lookup(const char *path, size_t len) {
- for (struct node *iter = root; iter; iter = iter->next) {
- if (iter->namelen == len && !memcmp(path, iter->name, len))
- return iter;
- }
- return NULL;
-}
-
-static struct node *tmpfs_open(const char *path, struct fs_wait_response *res) {
- struct node *node;
- if (res->len == 0) return NULL;
- path++;
- res->len--;
-
- if (res->len == 0) return &special_root;
-
- // no directory support (yet)
- if (memchr(path, '/', res->len)) return NULL;
-
- node = lookup(path, res->len);
- if (!node && (res->flags & OPEN_CREATE)) {
- node = malloc(sizeof *node);
- memset(node, 0, sizeof *node);
-
- char *namebuf = malloc(res->len);
- memcpy(namebuf, path, res->len);
- node->name = namebuf;
- node->namelen = res->len;
- node->next = root;
- root = node;
- }
- return node;
-}
-
-void tmpfs_drv(void) {
- // TODO replace all the static allocations in drivers with mallocs
- static char buf[512];
- struct fs_wait_response res;
- struct node *ptr;
- while (!_syscall_fs_wait(buf, sizeof buf, &res)) {
- switch (res.op) {
- case VFSOP_OPEN:
- ptr = tmpfs_open(buf, &res);
- _syscall_fs_respond(ptr, ptr ? 0 : -1, 0);
- break;
-
- case VFSOP_READ:
- ptr = (void*)res.id;
- if (ptr == &special_root) {
- size_t buf_pos = 0;
- size_t to_skip = res.offset;
-
- for (struct node *iter = root; iter; iter = iter->next) {
- if (iter->namelen <= to_skip) {
- to_skip -= iter->namelen;
- continue;
- }
-
- if (iter->namelen + buf_pos - to_skip >= sizeof(buf)) {
- memcpy(buf + buf_pos, iter->name + to_skip, sizeof(buf) - buf_pos - to_skip);
- buf_pos = sizeof(buf);
- break;
- }
- memcpy(buf + buf_pos, iter->name + to_skip, iter->namelen - to_skip);
- buf_pos += iter->namelen - to_skip;
- buf[buf_pos++] = '\0';
- to_skip = 0;
- }
- _syscall_fs_respond(buf, buf_pos, 0);
- } else {
- // TODO offset
- if (res.offset)
- _syscall_fs_respond(NULL, 0, 0);
- else
- _syscall_fs_respond(ptr->buf, ptr->size, 0);
- break;
- }
- break;
-
- case VFSOP_WRITE:
- ptr = (void*)res.id;
- if (ptr == &special_root) {
- _syscall_fs_respond(NULL, -1, 0);
- break;
- }
- if (res.len == 0) {
- _syscall_fs_respond(NULL, 0, 0);
- break;
- }
- if (!ptr->buf) {
- ptr->buf = malloc(256);
- if (!ptr->buf) {
- _syscall_fs_respond(NULL, -1, 0);
- break;
- }
- memset(ptr->buf, 0, 256);
- ptr->capacity = 256;
- }
-
- size_t len = res.len;
- if (len > ptr->capacity - res.offset)
- len = ptr->capacity - res.offset;
- memcpy(ptr->buf + res.offset, buf, len);
- if (ptr->size < res.offset + len)
- ptr->size = res.offset + len;
- _syscall_fs_respond(NULL, len, 0);
- break;
-
- default:
- _syscall_fs_respond(NULL, -1, 0);
- break;
- }
- }
-
- _syscall_exit(1);
-}