From 3cc72f1f246be8f4706e5f5b982983b20f005bae Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sun, 11 Jun 2023 14:55:47 +0100 Subject: [PATCH] kernel: tty: support printing output from printk() --- include/socks/tty.h | 2 ++ init/main.c | 26 +++++++++++++++++--------- kernel/tty/device.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/include/socks/tty.h b/include/socks/tty.h index 03faf4a..4a40ebd 100644 --- a/include/socks/tty.h +++ b/include/socks/tty.h @@ -93,6 +93,8 @@ extern kern_status_t tty_bootstrap(void); extern struct device *tty_device_create(void); extern kern_status_t tty_device_register(struct device *dev, struct tty_driver *owner, struct device *parent); +extern void tty_set_printk_output(struct device *tty); + extern struct tty_driver *tty_driver_create(struct kext *self, const char *name); extern kern_status_t tty_driver_destroy(struct tty_driver *drv); extern kern_status_t tty_driver_register(struct tty_driver *drv); diff --git a/init/main.c b/init/main.c index be01fe3..da5eecd 100644 --- a/init/main.c +++ b/init/main.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -192,9 +193,25 @@ void kernel_init(uintptr_t arg) struct object *fb; status = object_get("/dev/video/fb0", &fb); if (status == KERN_OK) { +#if 0 + struct framebuffer_varinfo fb_mode; + struct device *fbdev = cast_to_device(fb); + framebuffer_get_varinfo(fbdev, &fb_mode); + fb_mode.fb_xres = 1024; + fb_mode.fb_yres = 768; + fb_mode.fb_bpp = 24; + fb_mode.fb_flags = FB_MODE_RGB; + framebuffer_set_varinfo(fbdev, &fb_mode); +#endif + start_console_on_framebuffer(cast_to_device(fb)); } #endif + struct object *tty0; + status = object_get("/dev/tty/tty0", &tty0); + if (status == KERN_OK) { + tty_set_printk_output(cast_to_device(tty0)); + } create_kernel_thread(background_thread); @@ -202,15 +219,6 @@ void kernel_init(uintptr_t arg) run_all_tests(); - struct object *tty0; - status = object_get("/dev/tty/tty0", &tty0); - if (status == KERN_OK) { - size_t q; - object_write(tty0, "hello", 5, &q, 0); - } else { - printk("tty: unavailable"); - } - status = object_get("/dev/input/input0", &kbd); if (status != KERN_OK) { printk("no keyboard available"); diff --git a/kernel/tty/device.c b/kernel/tty/device.c index 1cc6f69..9aaf374 100644 --- a/kernel/tty/device.c +++ b/kernel/tty/device.c @@ -1,5 +1,6 @@ #include #include +#include #include static struct char_device_ops tty_ops = { @@ -7,6 +8,19 @@ static struct char_device_ops tty_ops = { .write = tty_write, }; +static struct device *tty_printk_output = NULL; + +static void tty_console_write(struct console *con, const char *s, unsigned int len) +{ + size_t nr_written; + tty_write(tty_printk_output, s, len, &nr_written, 0); +} + +static struct console tty_console = { + .c_name = "tty", + .c_write = tty_console_write, +}; + struct device *tty_device_create(void) { struct char_device *cdev = char_device_create(); @@ -47,3 +61,17 @@ kern_status_t tty_device_register(struct device *dev, struct tty_driver *owner, return object_namespace_create_link(global_namespace(), link_path, &dev->dev_base); } + +void tty_set_printk_output(struct device *tty) +{ + bool console_init = false; + if (tty_printk_output) { + console_init = true; + } + + tty_printk_output = tty; + + if (!console_init) { + console_register(&tty_console); + } +}