浏览代码

Add ability to run programs from fat

Johannes Zellner 8 月之前
父节点
当前提交
699c0a5fb1
共有 6 个文件被更改,包括 183 次插入 和 3 次删除
  1. 16 3
      Makefile
  2. 28 0
      prog.c
  3. 24 0
      prog_api.h
  4. 76 0
      run.c
  5. 27 0
      terminal.c
  6. 12 0
      user.ld

+ 16 - 3
Makefile

@@ -53,6 +53,9 @@ ls.o: ls.c fat.h io.h
 cat.o: cat.c fat.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
+run.o: run.c fat.h io.h fbtext.h prog_api.h
+	$(CC) $(CFLAGS) -o $@ $<
+
 rtc.o: rtc.c rtc.h
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -77,7 +80,7 @@ boot.elf: boot.o
 timer.o: timer.S
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.elf: kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o disk.o fat.o ls.o cat.o vga.o ati.o font_8x8.o fbtext.o clock.o colors.o display.o
+kernel.elf: kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o disk.o fat.o ls.o cat.o run.o vga.o ati.o font_8x8.o fbtext.o clock.o colors.o display.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 boot.bin: boot.elf
@@ -105,15 +108,25 @@ final.bin: boot.bin kernel.bin
 	cat boot.bin.patched kernel.bin > $@; \
 	rm -f boot.bin.patched
 
-root.img:
+prog.o: prog.c prog_api.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+prog.elf: prog.o
+	$(LD) -T user.ld -nostdlib -m elf_i386 -o $@ $<
+
+prog.bin: prog.elf
+	$(OBJCOPY) -O binary $< $@
+
+root.img: prog.bin
 	dd if=/dev/zero of=$@ bs=512 count=2880 2>/dev/null
 	mkfs.fat -F 12 -n "ROOTDISK" $@
 	echo "Hello from the second floppy!" | mcopy -i $@ - ::HELLO.TXT
 	echo "This is a test file on drive 1." | mcopy -i $@ - ::TEST.TXT
+	mcopy -i $@ prog.bin ::PROG.BIN
 
 run: image.bin root.img
 	qemu-system-i386 -M pc -drive file=image.bin,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
 	qemu-system-x86_64 -M pc -drive file=image.bin,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
 
 clean:
-	rm -f boot.o kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o fat.o ls.o cat.o disk.o vga.o ati.o font_8x8.o fbtext.o clock.o colors.o display.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin final.bin root.img
+	rm -f boot.o kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o fat.o ls.o cat.o run.o disk.o vga.o ati.o font_8x8.o fbtext.o clock.o colors.o display.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin final.bin root.img prog.o prog.elf prog.bin

+ 28 - 0
prog.c

@@ -0,0 +1,28 @@
+/* Example user program: draws "omg!" centered, runs until ESC. Uses kernel API at PROGRAM_API_ADDR. */
+
+#include "prog_api.h"
+
+/* Character cell size used by fbtext (8px wide, 10px row height). */
+#define CHAR_WIDTH  8
+#define ROW_HEIGHT  10
+
+static const struct prog_api *const api = (const struct prog_api *)PROGRAM_API_ADDR;
+
+void _start(void)
+{
+	unsigned int cols, rows, cx, cy;
+	const char *msg = "omg!";
+
+	cols = api->get_cols();
+	rows = api->get_rows();
+	/* Center "omg!" (4 chars): x = (cols*8)/2 - (4*8)/2, y = (rows*10)/2 - 5 */
+	cx = (cols * CHAR_WIDTH) / 2 - (4 * CHAR_WIDTH) / 2;
+	cy = (rows * ROW_HEIGHT) / 2 - ROW_HEIGHT / 2;
+
+	api->draw_string_xy(cx, cy, msg, 255, 255, 255, 0);
+
+	while (api->poll_key() != IO_KEY_ESC)
+		;
+
+	return;
+}

+ 24 - 0
prog_api.h

@@ -0,0 +1,24 @@
+/* API passed to loaded program at PROGRAM_API_ADDR. Used by run.c and prog.c. */
+
+#ifndef PROG_API_H
+#define PROG_API_H
+
+#define PROGRAM_API_ADDR 0x20F00
+#define IO_KEY_ESC       0x1B
+
+/* Function pointer types for the program API. */
+typedef void (*prog_draw_string_xy_t)(unsigned int x0, unsigned int y0,
+	const char *str, unsigned int r_fg, unsigned int g_fg, unsigned int b_fg,
+	int transparent_bg);
+typedef unsigned int (*prog_get_cols_t)(void);
+typedef unsigned int (*prog_get_rows_t)(void);
+typedef int (*prog_poll_key_t)(void);
+
+struct prog_api {
+	prog_draw_string_xy_t draw_string_xy;
+	prog_get_cols_t       get_cols;
+	prog_get_rows_t       get_rows;
+	prog_poll_key_t       poll_key;
+};
+
+#endif

+ 76 - 0
run.c

@@ -0,0 +1,76 @@
+/* run command: load raw binary from FAT floppy to 0x21000 and execute. */
+
+#include "fat.h"
+#include "fbtext.h"
+#include "io.h"
+#include "prog_api.h"
+
+#define PROGRAM_LOAD_ADDR 0x21000
+#define PROGRAM_LOAD_MAX  0x10000
+#define NAME_BUF_SIZE     12
+
+static void to_8_3(const char *in, unsigned int in_len, char *out)
+{
+	unsigned int i, j, dot;
+	for (i = 0; i < in_len && in[i] == ' '; i++)
+		;
+	dot = i;
+	while (dot < in_len && in[dot] != '.' && in[dot] != ' ')
+		dot++;
+	j = 0;
+	while (i < dot && j < 8) {
+		out[j++] = (char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
+		i++;
+	}
+	while (j < 8)
+		out[j++] = ' ';
+	if (i < in_len && in[i] == '.')
+		i++;
+	while (i < in_len && in[i] != ' ' && j < 11) {
+		out[j++] = (char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
+		i++;
+	}
+	while (j < 11)
+		out[j++] = ' ';
+	out[11] = '\0';
+}
+
+void program_run_main(const char *filename)
+{
+	char name_8_3[NAME_BUF_SIZE];
+	int n;
+
+	if (!filename || !filename[0]) {
+		io_putstr("run: usage run <filename>\n");
+		return;
+	}
+	to_8_3(filename, 64, name_8_3);
+	n = fat_init();
+	if (n == -2) {
+		io_putstr("run: disk read error\n");
+		return;
+	}
+	if (n != 0) {
+		io_putstr("run: no FAT filesystem found\n");
+		return;
+	}
+	n = fat_read_file(name_8_3, (void *)PROGRAM_LOAD_ADDR, PROGRAM_LOAD_MAX);
+	if (n == -2) {
+		io_putstr("run: disk read error\n");
+		return;
+	}
+	if (n <= 0) {
+		io_putstr("run: file not found\n");
+		return;
+	}
+	{
+		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,
+		};
+		*(struct prog_api *)PROGRAM_API_ADDR = api;
+	}
+	((void (*)(void))PROGRAM_LOAD_ADDR)();
+}

+ 27 - 0
terminal.c

@@ -10,6 +10,7 @@ extern void program_hello_main(void);
 extern void program_reboot_main(void);
 extern void program_ls_main(void);
 extern void program_cat_main(const char *filename);
+extern void program_run_main(const char *filename);
 extern void program_clock_main(void);
 extern void program_colors_main(void);
 extern void program_display_main(void);
@@ -189,6 +190,27 @@ static int is_cmd_cat(const char *line, unsigned int line_len, char *filename_bu
 	return (j > 0);
 }
 
+static int is_cmd_run(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
+{
+	unsigned int i = 0, j;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 3 > line_len)
+		return 0;
+	if (line[i] != 'r' || line[i+1] != 'u' || line[i+2] != 'n')
+		return 0;
+	i += 3;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i >= line_len)
+		return 0;
+	j = 0;
+	while (i < line_len && line[i] != ' ' && j < buf_size - 1)
+		filename_buf[j++] = line[i++];
+	filename_buf[j] = '\0';
+	return (j > 0);
+}
+
 static int is_cmd_drive(const char *line, unsigned int line_len, unsigned int *drive_num)
 {
 	unsigned int i = 0;
@@ -327,6 +349,10 @@ void terminal_run(void)
 						program_cat_main(cat_fname);
 						io_drain_keyboard();
 						draw_header_row();
+					} else if (is_cmd_run(line, line_len, cat_fname, CAT_FNAME_MAX)) {
+						program_run_main(cat_fname);
+						io_drain_keyboard();
+						draw_header_row();
 					} else if (is_cmd_display(line, line_len)) {
 						program_display_main();
 					} else if (is_cmd_drive(line, line_len, &drive_sel)) {
@@ -341,6 +367,7 @@ void terminal_run(void)
 						io_putstr("clock\n");
 						io_putstr("colors\n");
 						io_putstr("cat <file>\n");
+						io_putstr("run <file>\n");
 						io_putstr("display\n");
 						io_putstr("drive <0|1>\n");
 						io_putstr("help\n");

+ 12 - 0
user.ld

@@ -0,0 +1,12 @@
+/* User program: load at 0x21000, same as run.c PROGRAM_LOAD_ADDR. */
+ENTRY(_start)
+
+SECTIONS
+{
+	. = 0x21000;
+	.text : { *(.text .text.*) }
+	.rodata : { *(.rodata .rodata.*) }
+	.data : { *(.data .data.*) }
+	.bss : { *(.bss .bss.*) *(COMMON) }
+	_end = .;
+}