From a5919e515cd568a62bfb2cf6c9993d8a43ca4793 Mon Sep 17 00:00:00 2001 From: hinus Date: Thu, 10 Jun 2021 16:59:28 +0800 Subject: [PATCH] Title: Set cursor position. Issue: https://gitee.com/hinus/linux_kernel_011/issues/I3V6OR Description: Set cursor position. --- include/asm/io.h | 10 +++++++ include/asm/system.h | 9 +++++++ include/linux/tty.h | 2 ++ kernel/chr_drv/Makefile | 2 +- kernel/chr_drv/console.c | 58 ++++++++++++++++++++++++++++++++++++++++ kernel/chr_drv/tty_io.c | 4 +++ setup.S | 1 + 7 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 include/asm/io.h create mode 100644 include/asm/system.h diff --git a/include/asm/io.h b/include/asm/io.h new file mode 100644 index 0000000..bc51561 --- /dev/null +++ b/include/asm/io.h @@ -0,0 +1,10 @@ +#ifndef _IO_H +#define _IO_H + +#define outb_p(value, port) \ +__asm__("outb %%al, %%dx\n" \ + "\tjmp 1f\n" \ + "1:\tjmp 1f\n" \ + "1:" :: "a"(value), "d"(port)) + +#endif diff --git a/include/asm/system.h b/include/asm/system.h new file mode 100644 index 0000000..5090d22 --- /dev/null +++ b/include/asm/system.h @@ -0,0 +1,9 @@ +#ifndef _SYSTEM_H +#define _SYSTEM_H + +#define sti() __asm__("sti"::) +#define cli() __asm__("cli"::) +#define nop() __asm__("nop"::) +#define iret() __asm__("iret"::) + +#endif diff --git a/include/linux/tty.h b/include/linux/tty.h index 542afc7..7142292 100644 --- a/include/linux/tty.h +++ b/include/linux/tty.h @@ -4,4 +4,6 @@ void con_init(); void tty_init(); +void con_write(); + #endif diff --git a/kernel/chr_drv/Makefile b/kernel/chr_drv/Makefile index afb3e4e..56430e6 100644 --- a/kernel/chr_drv/Makefile +++ b/kernel/chr_drv/Makefile @@ -1,7 +1,7 @@ AR := ar LD := ld GCC := gcc -CCFLAG := -m32 -I../../include -nostdinc -Wall -fomit-frame-pointer -c +CCFLAG := -m32 -I../../include -nostdinc -Wall -fomit-frame-pointer -fno-stack-protector -c OBJS := tty_io.o console.o tty_io.o : tty_io.c diff --git a/kernel/chr_drv/console.c b/kernel/chr_drv/console.c index 5cbd4f2..11dfbf2 100644 --- a/kernel/chr_drv/console.c +++ b/kernel/chr_drv/console.c @@ -1,5 +1,10 @@ #include +#include +#include + +#define ORIG_X (*(unsigned char *)0x90000) +#define ORIG_Y (*(unsigned char *)0x90001) #define ORIG_VIDEO_PAGE (*(unsigned short *)0x90004) #define ORIG_VIDEO_MODE ((*(unsigned short *)0x90006) & 0xff) #define ORIG_VIDEO_COLS (((*(unsigned short *)0x90006) & 0xff00) >> 8) @@ -23,6 +28,34 @@ static unsigned char video_page; /* Initial video page */ static unsigned short video_port_reg; /* Video register select port */ static unsigned short video_port_val; /* Video register value port */ +static unsigned long origin; +static unsigned long scr_end; +static unsigned long pos; +static unsigned long x, y; +static unsigned long top, bottom; +static unsigned long attr = 0x07; + +static char buf[8]; + + +static inline void gotoxy(int new_x,unsigned int new_y) { + if (new_x > video_num_columns || new_y >= video_num_lines) + return; + + x = new_x; + y = new_y; + pos = origin + y*video_size_row + (x << 1); +} + +static inline void set_cursor() { + cli(); + outb_p(14, video_port_reg); + outb_p(0xff&((pos-video_mem_base)>>9), video_port_val); + outb_p(15, video_port_reg); + outb_p(0xff&((pos-video_mem_base)>>1), video_port_val); + sti(); +} + void con_init() { char * display_desc = "????"; char * display_ptr; @@ -70,5 +103,30 @@ void con_init() { *display_ptr++ = *display_desc++; *display_ptr++; } + + origin = video_mem_base; + scr_end = video_mem_base + video_num_lines * video_size_row; + top = 0; + bottom = video_num_lines; + + gotoxy(ORIG_X, ORIG_Y); + set_cursor(); + con_write("hello", 5); +} + +void con_write(const char* buf, int nr) { + char* t = (char*)pos; + char* s = buf; + int i = 0; + + for (i = 0; i < nr; i++) { + *t++ = *(s++); + *t++ = 0xf; + x++; + } + + pos = t; + gotoxy(x, y); + set_cursor(); } diff --git a/kernel/chr_drv/tty_io.c b/kernel/chr_drv/tty_io.c index 724ec13..eeeb1c2 100644 --- a/kernel/chr_drv/tty_io.c +++ b/kernel/chr_drv/tty_io.c @@ -4,3 +4,7 @@ void tty_init() { con_init(); } +void tty_write(unsigned channel, char* buf, int nr) { + con_write(buf, nr); +} + diff --git a/setup.S b/setup.S index a30a5dd..33a28fc 100644 --- a/setup.S +++ b/setup.S @@ -40,6 +40,7 @@ _start_setup: movw %ax, (8) movw %bx, (10) movw %cx, (12) + movw $0x5019, (14) movw $0x0000, %ax movw %ax, %ds -- Gitee