فهرست منبع

Add serial debug command

Johannes Zellner 5 ماه پیش
والد
کامیت
e6201468c5
9فایلهای تغییر یافته به همراه166 افزوده شده و 58 حذف شده
  1. 5 2
      Makefile
  2. 1 1
      kernel.c
  3. 24 2
      mouse.c
  4. 1 0
      mouse.h
  5. 1 1
      mousecmd.c
  6. 32 24
      serial.c
  7. 11 6
      serial.h
  8. 34 0
      serial_loop.c
  9. 57 22
      terminal.c

+ 5 - 2
Makefile

@@ -65,6 +65,9 @@ rtc.o: rtc.c rtc.h
 serial.o: serial.c serial.h pci.h
 	$(CC) $(CFLAGS) -o $@ $<
 
+serial_loop.o: serial_loop.c serial.h io.h timer.h
+	$(CC) $(CFLAGS) -o $@ $<
+
 vga.o: vga.c vga.h pci.h gpu.h ati.h
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -116,7 +119,7 @@ switch.o: switch.S
 process.o: process.c process.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o vga.o vga_copy.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o idle.o
+kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o serial_loop.o vga.o vga_copy.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o idle.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 boot.bin: boot.elf
@@ -205,4 +208,4 @@ scp-root: root.img
 	ssh root@192.168.0.136 dd if=root.img of=/dev/fd0 bs=512
 
 clean:
-	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o vga.o vga_copy.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o idle.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin
+	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o serial_loop.o vga.o vga_copy.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o idle.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin

+ 1 - 1
kernel.c

@@ -128,7 +128,7 @@ void kernel_main(void)
 	while (p < __bss_end)
 		*p++ = 0;
 
-	serial_init();
+	serial_init(SERIAL_COM1);
 	io_init();
 	idle_init();
 	timer_init();

+ 24 - 2
mouse.c

@@ -42,6 +42,7 @@ static int mouse_present_flag;
 static int mouse_streaming_flag;
 static unsigned int mouse_id;
 static int mouse_cursor_flag;
+static unsigned int mouse_pkt_size;
 
 static unsigned int fbw, fbh;
 static unsigned int cur_x, cur_y;
@@ -184,6 +185,19 @@ static void mouse_cursor_setup(void)
 	mouse_cursor_flag = 1;
 }
 
+static int mouse_intellimouse_init(void)
+{
+	static const unsigned char rates[] = { 0xC8, 0x64, 0x50, 0x32 };
+	unsigned int i;
+	for (i = 0; i < sizeof(rates); i++) {
+		if (mouse_send_cmd_ack(0xF3) != 0)
+			return -1;
+		if (mouse_send_cmd_ack(rates[i]) != 0)
+			return -1;
+	}
+	return 0;
+}
+
 void mouse_init(void)
 {
 	unsigned char id = 0;
@@ -192,6 +206,7 @@ void mouse_init(void)
 	mouse_streaming_flag = 0;
 	mouse_id = 0;
 	mouse_cursor_flag = 0;
+	mouse_pkt_size = 3;
 	pkt_idx = 0;
 
 	enable_aux_port();
@@ -199,6 +214,8 @@ void mouse_init(void)
 	if (mouse_send_cmd_ack(0xF5) != 0)
 		return;
 
+	mouse_intellimouse_init();
+
 	mouse_cmd_write(0xF2);
 	if (mouse_expect_ack() != 0)
 		return;
@@ -207,6 +224,7 @@ void mouse_init(void)
 
 	mouse_present_flag = 1;
 	mouse_id = id;
+	mouse_pkt_size = (id == 0x03 || id == 0x04) ? 4u : 3u;
 	mouse_cursor_setup();
 
 	if (mouse_send_cmd_ack(0xF4) != 0)
@@ -216,7 +234,6 @@ void mouse_init(void)
 
 void mouse_poll_update(void)
 {
-	/* If video mode info wasn't ready at mouse_init time, try again. */
 	if (mouse_present_flag && !mouse_cursor_flag)
 		mouse_cursor_setup();
 	if (!mouse_streaming_flag)
@@ -232,7 +249,7 @@ void mouse_poll_update(void)
 			return;
 
 		pkt[pkt_idx++] = inb(PS2_DATA);
-		if (pkt_idx < 3)
+		if (pkt_idx < mouse_pkt_size)
 			continue;
 		pkt_idx = 0;
 
@@ -282,3 +299,8 @@ int mouse_cursor_visible(void)
 {
 	return mouse_cursor_flag;
 }
+
+unsigned int mouse_packet_size(void)
+{
+	return mouse_pkt_size;
+}

+ 1 - 0
mouse.h

@@ -8,5 +8,6 @@ int mouse_is_present(void);
 int mouse_streaming_enabled(void);
 unsigned int mouse_device_id(void);
 int mouse_cursor_visible(void);
+unsigned int mouse_packet_size(void);
 
 #endif

+ 1 - 1
mousecmd.c

@@ -46,7 +46,7 @@ void program_mouseinfo_main(void)
 	else
 		io_putstr("standard ps/2\n");
 	io_putstr("mouse packet bytes ");
-	put_uint((id == 0x03 || id == 0x04) ? 4u : 3u);
+	put_uint(mouse_packet_size());
 	io_putchar('\n');
 	io_putstr("mouse streaming ");
 	io_putstr(mouse_streaming_enabled() ? "on\n" : "off\n");

+ 32 - 24
serial.c

@@ -1,58 +1,66 @@
-/* Serial port (COM1 0x3F8) debug output for bare-metal diagnostics. */
-
 #include "serial.h"
 #include "pci.h"
 
-#define COM1 0x3F8
-
-void serial_init(void)
+void serial_init(unsigned int port)
 {
-	port_outb(COM1 + 1, 0x00);   /* disable interrupts */
-	port_outb(COM1 + 3, 0x80);   /* DLAB on */
-	port_outb(COM1 + 0, 0x0C);   /* divisor 12 = 9600 baud */
-	port_outb(COM1 + 1, 0x00);
-	port_outb(COM1 + 3, 0x03);   /* 8N1, DLAB off */
-	port_outb(COM1 + 2, 0xC7);   /* enable FIFO */
-	port_outb(COM1 + 4, 0x03);   /* RTS + DTR */
+	port_outb(port + 1, 0x00);
+	port_outb(port + 3, 0x80);
+	port_outb(port + 0, 0x0C);
+	port_outb(port + 1, 0x00);
+	port_outb(port + 3, 0x03);
+	port_outb(port + 2, 0xC7);
+	port_outb(port + 4, 0x03);
 }
 
-void serial_putc(char c)
+void serial_putc(unsigned int port, char c)
 {
-	while (!(port_inb(COM1 + 5) & 0x20))
+	while (!(port_inb(port + 5) & 0x20))
 		;
-	port_outb(COM1, (unsigned char)c);
+	port_outb(port, (unsigned char)c);
 }
 
-void serial_puts(const char *s)
+void serial_puts(unsigned int port, const char *s)
 {
 	while (*s) {
 		if (*s == '\n')
-			serial_putc('\r');
-		serial_putc(*s++);
+			serial_putc(port, '\r');
+		serial_putc(port, *s++);
 	}
 }
 
-void serial_puthex(unsigned int n)
+void serial_puthex(unsigned int port, unsigned int n)
 {
 	static const char hex[] = "0123456789ABCDEF";
 	int i;
-	serial_puts("0x");
+	serial_puts(port, "0x");
 	for (i = 28; i >= 0; i -= 4)
-		serial_putc(hex[(n >> i) & 0xF]);
+		serial_putc(port, hex[(n >> i) & 0xF]);
 }
 
-void serial_putdec(unsigned int n)
+void serial_putdec(unsigned int port, unsigned int n)
 {
 	char buf[12];
 	char *p = buf + sizeof(buf) - 1;
 	*p = '\0';
 	if (n == 0) {
-		serial_putc('0');
+		serial_putc(port, '0');
 		return;
 	}
 	while (n) {
 		*--p = (char)('0' + (n % 10));
 		n /= 10;
 	}
-	serial_puts(p);
+	serial_puts(port, p);
+}
+
+int serial_has_data(unsigned int port)
+{
+	return (port_inb(port + 5) & 0x01) != 0;
+}
+
+char serial_getc_nb(unsigned int port)
+{
+	if (!serial_has_data(port))
+		return 0;
+	return (char)port_inb(port);
 }

+ 11 - 6
serial.h

@@ -1,11 +1,16 @@
-/* Serial port (COM1) debug output. */
+/* Serial port (COM1/COM2) debug output. */
 #ifndef SERIAL_H
 #define SERIAL_H
 
-void serial_init(void);
-void serial_putc(char c);
-void serial_puts(const char *s);
-void serial_puthex(unsigned int n);
-void serial_putdec(unsigned int n);
+#define SERIAL_COM1 0x3F8
+#define SERIAL_COM2 0x2F8
+
+void serial_init(unsigned int port);
+void serial_putc(unsigned int port, char c);
+void serial_puts(unsigned int port, const char *s);
+void serial_puthex(unsigned int port, unsigned int n);
+void serial_putdec(unsigned int port, unsigned int n);
+int serial_has_data(unsigned int port);
+char serial_getc_nb(unsigned int port);
 
 #endif

+ 34 - 0
serial_loop.c

@@ -0,0 +1,34 @@
+#include "io.h"
+#include "serial.h"
+#include "timer.h"
+#include "idle.h"
+
+void program_serial_loop_main(const char *arg)
+{
+	unsigned int port = SERIAL_COM1;
+	unsigned int last_send;
+	const char msg[] = "hello serial\r\n";
+
+	if (arg && arg[0] == '2')
+		port = SERIAL_COM2;
+
+	serial_init(port);
+	last_send = get_ticks_ms();
+
+	for (;;) {
+		unsigned int now = get_ticks_ms();
+
+		if (now - last_send >= 1000) {
+			last_send = now;
+			serial_puts(port, msg);
+		}
+
+		while (serial_has_data(port)) {
+			char c = serial_getc_nb(port);
+			if (c)
+				io_putchar(c);
+		}
+
+		cpu_idle();
+	}
+}

+ 57 - 22
terminal.c

@@ -23,6 +23,7 @@ extern void program_display_main(void);
 extern void program_cpuinfo_main(void);
 extern void program_mouseinfo_main(void);
 extern void program_power_main(const char *args);
+extern void program_serial_loop_main(const char *arg);
 extern void disk_set_drive(unsigned int drive);
 extern unsigned int disk_get_drive(void);
 
@@ -384,6 +385,34 @@ static int is_cmd_kill(const char *line, unsigned int line_len, unsigned int *pi
 	return 1;
 }
 
+static int is_cmd_serial(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
+{
+	unsigned int i = 0, j = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 6 > line_len)
+		return 0;
+	if (line[i] != 's' || line[i+1] != 'e' || line[i+2] != 'r' ||
+	    line[i+3] != 'i' || line[i+4] != 'a' || line[i+5] != 'l')
+		return 0;
+	i += 6;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i >= line_len) {
+		if (buf_size > 0)
+			arg_buf[0] = '\0';
+		return 1;
+	}
+	while (i < line_len && j + 1 < buf_size)
+		arg_buf[j++] = line[i++];
+	while (j > 0 && arg_buf[j - 1] == ' ')
+		j--;
+	arg_buf[j] = '\0';
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
+
 #define POWER_ARGS_MAX 32
 static int is_cmd_power(const char *line, unsigned int line_len, char *args_buf, unsigned int buf_size)
 {
@@ -625,15 +654,20 @@ void terminal_run(void)
 						io_putstr("mouseinfo\n");
 						io_putstr("power [info|set <low|balanced|max>]\n");
 						io_putstr("drive <0|1>\n");
-						io_putstr("ps        kill <pid>\n");
-						io_putstr("help      clear\n");
-					} else if (is_cmd_clear(line, line_len)) {
-						io_clear_screen();
-						draw_header_row();
-						io_set_cursor(IO_CONTENT_OFFSET);
-						io_putstr(prompt_first);
-						line_start = io_get_cursor();
-						skip_prompt = 1;
+					io_putstr("ps        kill <pid>\n");
+					io_putstr("serial [1|2] help      clear\n");
+				} else if (is_cmd_clear(line, line_len)) {
+					io_clear_screen();
+					draw_header_row();
+					io_set_cursor(IO_CONTENT_OFFSET);
+					io_putstr(prompt_first);
+					line_start = io_get_cursor();
+					skip_prompt = 1;
+				} else {
+					static char serial_arg[4];
+					if (is_cmd_serial(line, line_len, serial_arg, sizeof(serial_arg))) {
+						if (process_create_arg("serial_loop", program_serial_loop_main, serial_arg) < 0)
+							io_putstr("error: too many processes\n");
 					} else {
 						unsigned int i = 0;
 						while (i < line_len && line[i] == ' ')
@@ -641,19 +675,20 @@ void terminal_run(void)
 						if (i < line_len)
 							io_putstr("command not found\n");
 					}
-					line_len  = 0;
-					line_pos  = 0;
-					line[0]   = '\0';
-					if (!skip_prompt) {
-						io_putstr(prompt);
-						line_start = io_get_cursor();
-					}
-					if (io_did_scroll())
-						redraw_datetime();
-				} else {
-					{
-						unsigned char u = (unsigned char)k;
-						if (u >= 0x20 && u < 0x7F && line_len < LINE_MAX - 1) {
+				}
+				line_len  = 0;
+				line_pos  = 0;
+				line[0]   = '\0';
+				if (!skip_prompt) {
+					io_putstr(prompt);
+					line_start = io_get_cursor();
+				}
+				if (io_did_scroll())
+					redraw_datetime();
+			} else {
+				{
+					unsigned char u = (unsigned char)k;
+					if (u >= 0x20 && u < 0x7F && line_len < LINE_MAX - 1) {
 							unsigned int i;
 							history_browse = 0;
 							for (i = line_len; i > line_pos; i--)