Prechádzať zdrojové kódy

9.707u-mods: add support for serial ports

rain 5 rokov pred
rodič
commit
85daf4ac2d
12 zmenil súbory, kde vykonal 298 pridanie a 73 odobranie
  1. 3 0
      LINES
  2. 6 0
      README.md
  3. 20 0
      boot/boot.asm
  4. 1 0
      boot/loader.asm
  5. 3 23
      include/kernel/init.h
  6. 27 1
      include/kernel/tty.h
  7. 39 0
      include/modules/serial.h
  8. 5 0
      init/main.c
  9. 3 48
      kernel/console.c
  10. 35 0
      kernel/tty.c
  11. 1 1
      modules/Makefile
  12. 155 0
      modules/serial.c

+ 3 - 0
LINES

@@ -44,3 +44,6 @@
 2021-8-23 9.640: 9640 lines, 63 files, rain
 2021-8-23 9.380: 9380 lines, 61 files, rain
 2021-8-24 9.742: 9742 lines, 65 files, rain
+2021-8-24 9.742: 9742 lines, 65 files, rain
+2021-8-24 9.966: 9966 lines, 67 files, rain
+2021-8-24 9.707: 9707 lines, 65 files, rain

+ 6 - 0
README.md

@@ -169,9 +169,15 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 &nbsp;&nbsp;&nbsp;&nbsp;now we use another mode to `register_irq`: a new api that `set_irq`. 
 &nbsp;&nbsp;&nbsp;&nbsp;`set_irq` just need 2 arguments, it's simplier, and faster. 
 
+<<<<<<< HEAD
 9.380u-mods:  
 &nbsp;&nbsp;&nbsp;&nbsp;move the ahci-driver to the dev branch, because it's unworkable (well...). 
 
 9.742u-mods:  
 &nbsp;&nbsp;&nbsp;&nbsp;add support for beep (in pit). 
 
+9.742u-mods:  
+&nbsp;&nbsp;&nbsp;&nbsp;add support for beep (in pit). 
+
+9.966u-mods:  
+&nbsp;&nbsp;&nbsp;&nbsp;add support for serial port: it creates a new tty 'tty1' (COM0). 

+ 20 - 0
boot/boot.asm

@@ -29,6 +29,12 @@ VMODE	 	equ 0x7c01
 SCREENX 	equ 0x7c02
 SCREENY 	equ 0x7c04
 FB_ADDR 	equ 0x7c06
+CURSORX 	equ 0x7c0a
+CURSORY 	equ 0x7c0b
+COM0 		equ 0x7c0c
+COM1 		equ 0x7c0e
+COM2 		equ 0x7c10
+COM3 		equ 0x7c12
 
 
 _start:
@@ -76,6 +82,20 @@ _start:
 	cmp ebx, 0
 	jne .get_loop
 
+.serial_init:
+	; get serial ports, and save it to bootarg
+	mov ax, [0x400]
+	mov [COM0], ax
+
+	mov ax, [0x402]
+	mov [COM1], ax
+
+	mov ax, [0x404]
+	mov [COM2], ax
+
+	mov ax, [0x406]
+	mov [COM3], ax
+
 	; jump to loader, 3488 bytes max 
 	jmp 0x0000:0x7e60
 

+ 1 - 0
boot/loader.asm

@@ -34,6 +34,7 @@ CURSORX		equ 0x7c0a
 CURSORY 	equ 0x7c0b
 
 
+
 _start:
 	xor ax, ax
 

+ 3 - 23
include/kernel/init.h

@@ -78,6 +78,7 @@
 #define SEEK_END 0x0003
 
 
+#include <arch/io.h>
 
 struct bootarg {
 	__uint8_t bootdev;
@@ -91,6 +92,8 @@ struct bootarg {
 	/* cursor position x and y */
 	__uint8_t x;
 	__uint8_t y;
+
+	__io_t com0, com1, com2, com3;
 } __attribute__((packed));
 
 /* init/main.c */
@@ -157,29 +160,6 @@ struct file_opt {
 	void (* close) (int);
 };
 
-
-struct tty {
-	struct fifo *input_fifo;
-
-	/* unreadable (wr-only) */
-#define T_NORD 		0x01
-	/* tty_read returns character / line */
-#define T_COOKED 	0x02
-
-	/* put the input character to screen (in tty_read) */
-#define T_INPUT_BACK	0x04
-
-	__uint8_t mode;
-
-	/* the controller process id */
-	__uint32_t ctl;
-
-	__uint8_t vx, vy;
-
-	int (* putc) (struct tty *, char c);
-};
-
-
 struct time {
 	__uint16_t year;
 	__uint8_t month;

+ 27 - 1
include/kernel/tty.h

@@ -28,11 +28,37 @@
 #define _TTY_H
 
 #include <kernel/init.h>
+#include <kernel/types.h>
 
 int tty_read(struct tty *, char *, size_t);
 int tty_write(struct tty *, char *, size_t);
 
-extern struct tty stdin, stdout, stderr;
+
+struct tty {
+	struct fifo *input_fifo;
+
+	/* unreadable (wr-only) */
+#define T_NORD 		0x01
+	/* tty_read returns character / line */
+#define T_COOKED 	0x02
+
+	/* put the input character to screen (in tty_read) */
+#define T_INPUT_BACK	0x04
+
+	__uint8_t mode;
+
+	/* the controller process id */
+	__uint32_t ctl;
+
+	__uint8_t vx, vy;
+
+	int (* putc) (struct tty *, char c);
+
+	struct tty *next;
+};
+
+/* tty0: system console (0xb8000)  tty1: serial port 0 (if exists) */
+extern struct tty tty0, tty1;
 
 #endif
 

+ 39 - 0
include/modules/serial.h

@@ -0,0 +1,39 @@
+/* Copyright (C) 2021 Rain */
+
+/* This file is part of XNIX. */
+
+/* 
+  XNIX is free software: you can redistribute it and/or modify 
+  it under the terms of the GNU General Public License as published by 
+  the Free Software Foundation, either version 3 of the License, or 
+  (at your option) and later version. 
+*/
+
+/*
+  XNIX is distributed in the hope that it will be useful, 
+  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+  GNU General Public License for more details. 
+*/
+
+/*
+  You should have received a copy of the GNU General Public License 
+   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
+*/
+
+
+
+#include <kernel/init.h>
+#include <kernel/errno.h>
+#include <kernel/types.h>
+
+#include <kernel/tty.h>
+#include <arch/io.h>
+
+#define serial_info(fmt, ...) printk(yellow, "serial: " fmt, ##__VA_ARGS__)
+
+errno_t serial_init(void);
+void com_init(__io_t port);
+void serial_write(__io_t port, __uint8_t);
+int serial_putc(struct tty *, char);
+

+ 5 - 0
init/main.c

@@ -44,6 +44,7 @@
 #include <sys/syscall.h>
 
 #include <modules/beep.h>
+#include <modules/serial.h>
 
 #include <arch/io.h>
 #include <arch/sys.h>
@@ -105,6 +106,10 @@ int init(void) {
 
 	time_init();
 
+	serial_init();
+
+	tty_write(&tty1, "hello, world. \n", 15);
+
 	schk_leave();
 
 	return 0;

+ 3 - 48
kernel/console.c

@@ -31,25 +31,18 @@
 #include <kernel/vsprintf.h>
 #include <kernel/schk.h>
 
+#include <kernel/tty.h>
 #include <lib/string.h>
 #include <arch/io.h>
 
 #include <stdarg.h>
 
 
-
-static char buffer[256];
-
 static __uint16_t *fb = FB_ADDR;
 __uint8_t x, y;
 
 struct tty tty0;
 
-struct tty stdin;
-struct tty stdout;
-struct tty stderr;
-
-
 extern struct fifo kbd_fifo;
 int nc_putc(struct tty *, char);
 
@@ -74,16 +67,7 @@ errno_t console_init(void) {
 
 	tty0.putc = nc_putc;
 
-	stdin.input_fifo = &kbd_fifo;
-	stdin.mode = T_COOKED;
-
-	stdin.ctl = 0;
-
-	stdin.vx = x;
-	stdin.vy = y;
-
-	memcpy(&stdout, &tty0, sizeof(struct tty));
-	memcpy(&stdout, &tty0, sizeof(struct tty));
+	tty0.next = NULL;
 
 	/* well, this is the end */
 
@@ -113,7 +97,7 @@ int nc_putc(struct tty *tty, char c) {
 	}
 
 	else if (c == '\t') {
-		__uint64_t ax = (tty->vx + 3) & ~3;
+		__uint64_t ax = (tty->vx + TAB_SIZE) & ~TAB_SIZE;
 
 		if (ax == tty->vx) 
 			tty->vx += TAB_SIZE;
@@ -141,35 +125,6 @@ int nc_putc(struct tty *tty, char c) {
 }
 
 
-int printk(__uint8_t color, char *s, ...) {
-	va_list args;
-	int i;
-
-	/* I think 256 is enough in kernel */
-	/* too long, return it. */
-
-	va_start(args, s);
-
-	/* it maybe overflow, but that's in .data section and we did check. */
-
-	i = vsprintf(buffer, s, args);
-	print(color, buffer);
-	
-	memset(buffer, 0, 256);
-
-	va_end(args);
-
-	return i;
-}
-
-void print(__uint8_t color, char *s) {
-	while (*s) 
-		putc(*s++, color);
-
-	return;
-}
-
-
 void putc(char c, __uint8_t color) {
 	if (c == '\n') {
 		/* we do it like \r\n */

+ 35 - 0
kernel/tty.c

@@ -30,10 +30,16 @@
 
 #include <kernel/console.h>
 #include <kernel/schk.h>
+#include <kernel/vsprintf.h>
+#include <kernel/tty.h>
 
+#include <lib/string.h>
 #include <lib/fifo.h>
 
 
+
+static char buffer[256];
+
 /* read a character from tty (keyboard) with non-cooked */
 
 int tty_read(struct tty *tty, char *buf, size_t nbytes) {
@@ -79,3 +85,32 @@ int tty_write(struct tty *tty, char *buf, size_t nbytes) {
 	return i;
 }
 
+
+
+int printk(__uint8_t color, char *s, ...) {
+	va_list args;
+	int i;
+
+	/* I think 256 is enough in kernel */
+	/* too long, return it. */
+
+	va_start(args, s);
+
+	/* it maybe overflow, but that's in .data section and we did check. */
+
+	i = vsprintf(buffer, s, args);
+	print(color, buffer);
+	
+	memset(buffer, 0, 256);
+
+	va_end(args);
+
+	return i;
+}
+
+void print(__uint8_t color, char *s) {
+	while (*s) 
+		putc(*s++, color);
+
+	return;
+}

+ 1 - 1
modules/Makefile

@@ -1,4 +1,4 @@
-OBJS = ide.o beep.o 
+OBJS = ide.o beep.o serial.o 
 CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include -Wall -Werror -g
 
 all: $(OBJS) modules.o 

+ 155 - 0
modules/serial.c

@@ -0,0 +1,155 @@
+/* Copyright (C) 2021 Rain */
+
+/* This file is part of XNIX. */
+
+/* 
+  XNIX is free software: you can redistribute it and/or modify 
+  it under the terms of the GNU General Public License as published by 
+  the Free Software Foundation, either version 3 of the License, or 
+  (at your option) and later version. 
+*/
+
+/*
+  XNIX is distributed in the hope that it will be useful, 
+  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+  GNU General Public License for more details. 
+*/
+
+/*
+  You should have received a copy of the GNU General Public License 
+   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
+*/
+
+
+
+
+#include <kernel/init.h>
+#include <kernel/errno.h>
+#include <kernel/types.h>
+
+#include <kernel/console.h>
+#include <kernel/mm.h>
+#include <kernel/tty.h>
+
+#include <kernel/int.h>
+#include <kernel/panic.h>
+
+#include <arch/io.h>
+#include <modules/serial.h>
+
+
+
+struct tty tty1;
+
+
+errno_t serial_init(void) {
+	serial_info("COM0: %w  ", bootarg->com0);
+	serial_info("COM1: %w\n", bootarg->com1);
+	serial_info("COM2: %w  ", bootarg->com2);
+	serial_info("COM3: %w\n", bootarg->com3);
+
+	if (bootarg->com0) com_init(bootarg->com0);
+	if (bootarg->com1) com_init(bootarg->com1);
+	if (bootarg->com2) com_init(bootarg->com2);
+	if (bootarg->com3) com_init(bootarg->com3);
+
+	if (!bootarg->com0) return 0;
+
+	tty1.input_fifo = NULL;
+	tty1.mode = T_COOKED | T_NORD;
+
+	tty1.ctl = 0;
+	tty1.vx = tty1.vy = 0;
+
+	tty1.putc = serial_putc;
+	tty1.next = NULL;
+
+	/* register a new tty if com0 exists */
+
+	tty0.next = &tty1;
+
+	return 0;
+}
+
+void com_init(__io_t port) {
+	outb(0x00, port + 1);
+
+	outb(0x80, port + 3);
+	outb(0x03, port + 0);
+	outb(0x00, port + 1);
+
+	outb(0x03, port + 3);
+	outb(0xc7, port + 2);
+	outb(0x0b, port + 4);
+	outb(0x1e, port + 4);
+
+	outb(0xae, port + 0);
+
+	if (inb(port + 0) != 0xae) {
+		serial_info("com_init: warning: port %w not responed. \n", 
+				port);
+		return;
+	}
+
+	outb(0x0f, port + 4);
+	return;
+}
+
+
+/* the tab size of serial (on qemu) */
+#define SERIAL_TAB_SIZE 	8
+
+int serial_putc(struct tty *tty, char c) {
+	if (c == '\n') {
+		tty->vx = 0;
+		tty->vy = 0;
+		
+		serial_write(bootarg->com0, '\r');
+		serial_write(bootarg->com0, '\n');
+
+		return 0;
+	}
+
+	if (c == '\r') {
+		tty->vx = 0;
+		serial_write(bootarg->com0, '\r');
+
+		return 0;
+	}
+
+	if (c == '\b') {
+		tty->vx--;
+		serial_write(bootarg->com0, '\b');
+
+		return 0;
+	}
+
+	if (c == '\t') {
+		int ax = (tty->vx + SERIAL_TAB_SIZE) & ~SERIAL_TAB_SIZE;
+
+		if (ax == tty->vx) 
+			tty->vx += SERIAL_TAB_SIZE;
+
+		else 
+			tty->vx = ax;
+
+		serial_write(bootarg->com0, '\t');
+
+		return 0;
+	}
+
+	serial_write(bootarg->com0, c);
+	tty->vx++;
+
+	return 0;
+}
+
+
+void serial_write(__io_t port, __uint8_t data) {
+	while (!(inb(port + 5) & 0x20));
+
+	outb(data, port + 0);
+
+	return;
+}