Просмотр исходного кода

Implement sending and running over serial

Johannes Zellner 5 месяцев назад
Родитель
Сommit
18f31d2ded
9 измененных файлов с 326 добавлено и 12 удалено
  1. 22 4
      Makefile
  2. 80 0
      bin_recv.c
  3. 7 0
      bin_recv.h
  4. 31 0
      run.c
  5. 127 0
      send_serial.c
  6. 2 0
      serial.h
  7. 1 2
      serial_loop.c
  8. 30 0
      serial_recv.c
  9. 26 6
      terminal.c

+ 22 - 4
Makefile

@@ -13,7 +13,7 @@ ASFLAGS  = --32
 BOOT_LDFLAGS   = -T linker.ld -nostdlib -m elf_i386
 BOOT_LDFLAGS   = -T linker.ld -nostdlib -m elf_i386
 KERNEL_LDFLAGS = -T kernel.ld -nostdlib -m elf_i386
 KERNEL_LDFLAGS = -T kernel.ld -nostdlib -m elf_i386
 
 
-.PHONY: all clean run run-floppy run-usb run-serial floppy usb
+.PHONY: all clean run run-floppy run-usb run-serial floppy usb space prog
 
 
 all: floppy.img usb.img final.bin root.img
 all: floppy.img usb.img final.bin root.img
 
 
@@ -68,6 +68,12 @@ serial.o: serial.c serial.h pci.h
 serial_loop.o: serial_loop.c serial.h io.h timer.h
 serial_loop.o: serial_loop.c serial.h io.h timer.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
+bin_recv.o: bin_recv.c bin_recv.h serial.h io.h timer.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+serial_recv.o: serial_recv.c serial.h bin_recv.h io.h
+	$(CC) $(CFLAGS) -o $@ $<
+
 vga.o: vga.c vga.h pci.h gpu.h ati.h
 vga.o: vga.c vga.h pci.h gpu.h ati.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
@@ -119,7 +125,7 @@ switch.o: switch.S
 process.o: process.c process.h io.h
 process.o: process.c process.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(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 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
+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 serial_recv.o bin_recv.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 $@ $^
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 
 boot.bin: boot.elf
 boot.bin: boot.elf
@@ -167,6 +173,10 @@ prog.elf: prog.o
 prog.bin: prog.elf
 prog.bin: prog.elf
 	$(OBJCOPY) -O binary $< $@
 	$(OBJCOPY) -O binary $< $@
 
 
+space: space.bin
+
+prog: prog.bin
+
 space.o: space.c run_api.h
 space.o: space.c run_api.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
@@ -209,12 +219,20 @@ scp-root: root.img
 
 
 SERIAL_DEV ?= /dev/ttyUSB1
 SERIAL_DEV ?= /dev/ttyUSB1
 SERIAL_BAUD ?= 9600
 SERIAL_BAUD ?= 9600
+SZ ?= /usr/bin/lrzsz-sz
 
 
 run-serial:
 run-serial:
 	@echo "Starting serial monitor on $(SERIAL_DEV) at $(SERIAL_BAUD) baud..."
 	@echo "Starting serial monitor on $(SERIAL_DEV) at $(SERIAL_BAUD) baud..."
-	@echo "Type 'serial' in the nebos terminal to start the serial loop."
+	@echo "Type 'serial test' or 'serial recv' in the nebos terminal."
 	@echo "Press Ctrl-A Ctrl-X to exit picocom."
 	@echo "Press Ctrl-A Ctrl-X to exit picocom."
 	picocom $(SERIAL_DEV) --baud $(SERIAL_BAUD)
 	picocom $(SERIAL_DEV) --baud $(SERIAL_BAUD)
 
 
+send: $(PROGRAM).bin send_serial
+	@echo "Sending $(PROGRAM).bin via serial..."
+	./send_serial $(SERIAL_DEV) $(SERIAL_BAUD) $(PROGRAM).bin
+
+send_serial: send_serial.c
+	gcc -O2 -o $@ $<
+
 clean:
 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 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
+	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 serial_recv.o bin_recv.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 send_serial

+ 80 - 0
bin_recv.c

@@ -0,0 +1,80 @@
+/* Simple binary receiver: size (4 bytes BE) + data + checksum (1 byte).
+   Returns bytes received, or -1 on error/timeout.
+   Prints '.' via io_putchar for each 512-byte chunk received. */
+
+#include "bin_recv.h"
+#include "serial.h"
+#include "io.h"
+#include "timer.h"
+
+#define TIMEOUT_MS 5000
+
+static int wait_byte(unsigned int port, unsigned char *out, unsigned int timeout_ms)
+{
+	unsigned int start = get_ticks_ms();
+	while (get_ticks_ms() - start < timeout_ms) {
+		if (serial_has_data(port)) {
+			*out = (unsigned char)serial_getc_nb(port);
+			return 1;
+		}
+	}
+	return 0;
+}
+
+int bin_recv(unsigned int port, void *buf, unsigned int max_size)
+{
+	unsigned char *p = (unsigned char *)buf;
+	unsigned int total = 0;
+	unsigned char c;
+	unsigned int i;
+	unsigned int size = 0;
+	unsigned char checksum = 0;
+	unsigned char recv_sum = 0;
+
+	/* Send 'R' to signal ready, wait for sender to start. */
+	serial_putc(port, 'R');
+
+	/* Read 4-byte size (big-endian). */
+	for (i = 0; i < 4; i++) {
+		if (!wait_byte(port, &c, TIMEOUT_MS)) {
+			io_putstr("timeout reading size\n");
+			return -1;
+		}
+		size = (size << 8) | c;
+	}
+
+	if (size == 0 || size > max_size) {
+		io_putstr("bad size\n");
+		return -1;
+	}
+
+	/* Read data bytes. */
+	for (i = 0; i < size; i++) {
+		if (!wait_byte(port, &c, TIMEOUT_MS)) {
+			io_putstr("timeout at byte ");
+			serial_putdec(port, i);
+			io_putstr("\n");
+			return -1;
+		}
+		p[i] = c;
+		checksum += c;
+		total++;
+		if ((i + 1) % 512 == 0)
+			io_putchar('.');
+	}
+	io_putchar('.');
+
+	/* Read checksum byte. */
+	if (!wait_byte(port, &recv_sum, TIMEOUT_MS)) {
+		io_putstr("timeout reading checksum\n");
+		return -1;
+	}
+
+	if (checksum != recv_sum) {
+		io_putstr("checksum mismatch\n");
+		return -1;
+	}
+
+	io_putchar('\n');
+	return (int)total;
+}

+ 7 - 0
bin_recv.h

@@ -0,0 +1,7 @@
+/* Simple binary receiver for serial transfer. */
+#ifndef BIN_RECV_H
+#define BIN_RECV_H
+
+int bin_recv(unsigned int port, void *buf, unsigned int max_size);
+
+#endif

+ 31 - 0
run.c

@@ -4,6 +4,7 @@
 #include "fbtext.h"
 #include "fbtext.h"
 #include "io.h"
 #include "io.h"
 #include "run_api.h"
 #include "run_api.h"
+#include "serial.h"
 #include "timer.h"
 #include "timer.h"
 #include "vga.h"
 #include "vga.h"
 
 
@@ -83,3 +84,33 @@ void program_run_main(const char *filename)
 	}
 	}
 	((void (*)(void))PROGRAM_LOAD_ADDR)();
 	((void (*)(void))PROGRAM_LOAD_ADDR)();
 }
 }
+
+/* run serial: execute the binary already loaded at 0x21000 via serial_recv. */
+
+void program_run_serial_main(const char *arg)
+{
+	(void)arg;
+
+	if (serial_recv_size == 0) {
+		io_putstr("run serial: no binary received (use 'serial recv' first)\n");
+		return;
+	}
+
+	io_drain_keyboard();
+	{
+		struct prog_api api = {
+			.draw_string_xy = fbtext_draw_string_xy,
+			.get_cols       = fbtext_get_cols,
+			.get_rows       = fbtext_get_rows,
+			.poll_key       = io_poll_key,
+			.poll_key_with_hw = io_poll_key_with_hw,
+			.get_fb_width   = vga_fb_width,
+			.get_fb_height  = vga_fb_height,
+			.clear_rgb      = vga_clear_rgb,
+			.put_pixel_rgb  = vga_put_pixel_rgb,
+			.get_ticks_ms   = get_ticks_ms,
+		};
+		*(struct prog_api *)PROGRAM_API_ADDR = api;
+	}
+	((void (*)(void))PROGRAM_LOAD_ADDR)();
+}

+ 127 - 0
send_serial.c

@@ -0,0 +1,127 @@
+/* Simple binary sender for nebos serial_recv.
+   Protocol: receiver sends 'R', then sender sends:
+     4 bytes: size (big-endian)
+     N bytes: data
+     1 byte: checksum (sum of data bytes mod 256)
+   Usage: send_serial <device> <baud> <file> */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <termios.h>
+#include <stdint.h>
+
+int main(int argc, char *argv[])
+{
+	int fd;
+	FILE *fp;
+	unsigned char *data;
+	long size;
+	unsigned int cksum;
+	unsigned char buf[5];
+	unsigned char r;
+	struct termios tty;
+
+	if (argc != 4) {
+		fprintf(stderr, "Usage: %s <device> <baud> <file>\n", argv[0]);
+		return 1;
+	}
+
+	fd = open(argv[1], O_RDWR | O_NOCTTY);
+	if (fd < 0) {
+		perror("open");
+		return 1;
+	}
+
+	/* Configure serial port. */
+	memset(&tty, 0, sizeof(tty));
+	if (tcgetattr(fd, &tty) != 0) {
+		perror("tcgetattr");
+		return 1;
+	}
+
+	cfsetispeed(&tty, atoi(argv[2]));
+	cfsetospeed(&tty, atoi(argv[2]));
+
+	tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
+	tty.c_cflag |= CLOCAL | CREAD;
+	tty.c_cflag &= ~(PARENB | PARODD | CRTSCTS | CSTOPB);
+	tty.c_iflag &= ~(IXON | IXOFF | IXANY | IGNBRK | BRKINT | ICRNL | INLCR);
+	tty.c_oflag &= ~OPOST;
+	tty.c_lflag = 0;
+	tty.c_cc[VMIN] = 1;
+	tty.c_cc[VTIME] = 0;
+
+	if (tcsetattr(fd, TCSANOW, &tty) != 0) {
+		perror("tcsetattr");
+		return 1;
+	}
+
+	/* Read file. */
+	fp = fopen(argv[3], "rb");
+	if (!fp) {
+		perror("fopen");
+		return 1;
+	}
+	fseek(fp, 0, SEEK_END);
+	size = ftell(fp);
+	fseek(fp, 0, SEEK_SET);
+
+	data = malloc(size);
+	if (!data) {
+		perror("malloc");
+		return 1;
+	}
+	if (fread(data, 1, size, fp) != (size_t)size) {
+		perror("fread");
+		return 1;
+	}
+	fclose(fp);
+
+	/* Compute checksum. */
+	cksum = 0;
+	for (long i = 0; i < size; i++)
+		cksum += data[i];
+	cksum &= 0xFF;
+
+	/* Wait for 'R' from receiver. */
+	fprintf(stderr, "Waiting for receiver...\n");
+	if (read(fd, &r, 1) != 1 || r != 'R') {
+		fprintf(stderr, "No 'R' received\n");
+		return 1;
+	}
+	fprintf(stderr, "Got 'R', sending %ld bytes...\n", size);
+
+	/* Send size (4 bytes big-endian). */
+	buf[0] = (size >> 24) & 0xFF;
+	buf[1] = (size >> 16) & 0xFF;
+	buf[2] = (size >> 8) & 0xFF;
+	buf[3] = size & 0xFF;
+	if (write(fd, buf, 4) != 4) {
+		perror("write size");
+		return 1;
+	}
+	tcdrain(fd);
+
+	/* Send data. */
+	if (write(fd, data, size) != size) {
+		perror("write data");
+		return 1;
+	}
+	tcdrain(fd);
+
+	/* Send checksum. */
+	buf[0] = cksum;
+	if (write(fd, buf, 1) != 1) {
+		perror("write checksum");
+		return 1;
+	}
+	tcdrain(fd);
+
+	fprintf(stderr, "Done.\n");
+	free(data);
+	close(fd);
+	return 0;
+}

+ 2 - 0
serial.h

@@ -13,4 +13,6 @@ void serial_putdec(unsigned int port, unsigned int n);
 int serial_has_data(unsigned int port);
 int serial_has_data(unsigned int port);
 char serial_getc_nb(unsigned int port);
 char serial_getc_nb(unsigned int port);
 
 
+extern unsigned int serial_recv_size;
+
 #endif
 #endif

+ 1 - 2
serial_loop.c

@@ -9,8 +9,7 @@ void program_serial_loop_main(const char *arg)
 	unsigned int last_send;
 	unsigned int last_send;
 	const char msg[] = "hello serial\r\n";
 	const char msg[] = "hello serial\r\n";
 
 
-	if (arg && arg[0] == '2')
-		port = SERIAL_COM2;
+	(void)arg;
 
 
 	serial_init(port);
 	serial_init(port);
 	last_send = get_ticks_ms();
 	last_send = get_ticks_ms();

+ 30 - 0
serial_recv.c

@@ -0,0 +1,30 @@
+/* Serial receive command: receives a binary via serial into 0x21000. */
+
+#include "io.h"
+#include "serial.h"
+#include "bin_recv.h"
+
+#define PROGRAM_LOAD_ADDR 0x21000
+#define PROGRAM_LOAD_MAX  0x10000
+
+unsigned int serial_recv_size;
+
+void program_serial_recv_main(const char *arg)
+{
+	int n;
+
+	(void)arg;
+
+	serial_recv_size = 0;
+
+	n = bin_recv(SERIAL_COM1, (void *)PROGRAM_LOAD_ADDR, PROGRAM_LOAD_MAX);
+	if (n < 0) {
+		io_putstr("serial_recv: transfer failed\n");
+		return;
+	}
+
+	serial_recv_size = (unsigned int)n;
+	io_putstr("serial_recv: received ");
+	serial_putdec(SERIAL_COM1, serial_recv_size);
+	io_putstr(" bytes\n");
+}

+ 26 - 6
terminal.c

@@ -24,6 +24,8 @@ extern void program_cpuinfo_main(void);
 extern void program_mouseinfo_main(void);
 extern void program_mouseinfo_main(void);
 extern void program_power_main(const char *args);
 extern void program_power_main(const char *args);
 extern void program_serial_loop_main(const char *arg);
 extern void program_serial_loop_main(const char *arg);
+extern void program_serial_recv_main(const char *arg);
+extern void program_run_serial_main(const char *arg);
 extern void disk_set_drive(unsigned int drive);
 extern void disk_set_drive(unsigned int drive);
 extern unsigned int disk_get_drive(void);
 extern unsigned int disk_get_drive(void);
 
 
@@ -616,8 +618,16 @@ void terminal_run(void)
 						if (process_create_arg("cat", program_cat_main, cat_fname) < 0)
 						if (process_create_arg("cat", program_cat_main, cat_fname) < 0)
 							io_putstr("error: too many processes\n");
 							io_putstr("error: too many processes\n");
 					} else if (is_cmd_run(line, line_len, cat_fname, CAT_FNAME_MAX)) {
 					} else if (is_cmd_run(line, line_len, cat_fname, CAT_FNAME_MAX)) {
-						if (process_create_arg("run", program_run_main, cat_fname) < 0)
-							io_putstr("error: too many processes\n");
+						if (cat_fname[0] == 's' && cat_fname[1] == 'e' &&
+						    cat_fname[2] == 'r' && cat_fname[3] == 'i' &&
+						    cat_fname[4] == 'a' && cat_fname[5] == 'l' &&
+						    cat_fname[6] == '\0') {
+							if (process_create_arg("run_serial", program_run_serial_main, cat_fname) < 0)
+								io_putstr("error: too many processes\n");
+						} else {
+							if (process_create_arg("run", program_run_main, cat_fname) < 0)
+								io_putstr("error: too many processes\n");
+						}
 					} else if (is_cmd_space(line, line_len)) {
 					} else if (is_cmd_space(line, line_len)) {
 						if (process_create_arg("space", program_run_main, "space.bin") < 0)
 						if (process_create_arg("space", program_run_main, "space.bin") < 0)
 							io_putstr("error: too many processes\n");
 							io_putstr("error: too many processes\n");
@@ -655,7 +665,7 @@ void terminal_run(void)
 						io_putstr("power [info|set <low|balanced|max>]\n");
 						io_putstr("power [info|set <low|balanced|max>]\n");
 						io_putstr("drive <0|1>\n");
 						io_putstr("drive <0|1>\n");
 					io_putstr("ps        kill <pid>\n");
 					io_putstr("ps        kill <pid>\n");
-					io_putstr("serial [1|2] help      clear\n");
+					io_putstr("serial test|recv  clear\n");
 				} else if (is_cmd_clear(line, line_len)) {
 				} else if (is_cmd_clear(line, line_len)) {
 					io_clear_screen();
 					io_clear_screen();
 					draw_header_row();
 					draw_header_row();
@@ -664,10 +674,20 @@ void terminal_run(void)
 					line_start = io_get_cursor();
 					line_start = io_get_cursor();
 					skip_prompt = 1;
 					skip_prompt = 1;
 				} else {
 				} else {
-					static char serial_arg[4];
+					static char serial_arg[8];
 					if (is_cmd_serial(line, line_len, serial_arg, sizeof(serial_arg))) {
 					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");
+						if (serial_arg[0] == 't' && serial_arg[1] == 'e' &&
+						    serial_arg[2] == 's' && serial_arg[3] == 't' &&
+						    serial_arg[4] == '\0') {
+							if (process_create_arg("serial_loop", program_serial_loop_main, serial_arg) < 0)
+								io_putstr("error: too many processes\n");
+						} else if (serial_arg[0] == 'r' && serial_arg[1] == 'e' &&
+						           serial_arg[2] == 'c' && serial_arg[3] == 'v' &&
+						           serial_arg[4] == '\0') {
+							program_serial_recv_main(serial_arg);
+						} else {
+							io_putstr("serial: usage serial test|recv\n");
+						}
 					} else {
 					} else {
 						unsigned int i = 0;
 						unsigned int i = 0;
 						while (i < line_len && line[i] == ' ')
 						while (i < line_len && line[i] == ' ')