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

also update programs with make net-dev

Johannes Zellner 1 месяц назад
Родитель
Сommit
8a395a1cc0
4 измененных файлов с 127 добавлено и 24 удалено
  1. 10 2
      AGENTS.md
  2. 10 2
      Makefile
  3. 85 18
      run.c
  4. 22 2
      terminal.c

+ 10 - 2
AGENTS.md

@@ -115,12 +115,20 @@ make clean
   `lsusb` command.
 - **Power/idle**: `idle.c` — `mwait` C-states, `hlt` fallback.
 - **User programs**: `run.c`, `run_api.h`, `prog.c`, `space.c` — raw binaries at
-  `0x41000`, API function-pointer struct at `0x40F00`. `space` = Space Invaders.
+  `0x41000`, API function-pointer struct at `0x40F00`.  `prog`/`space` are
+  **embedded in the kernel image** (`prog_embed.o`/`space_embed.o`, like
+  `hello.elf`), so `make dev`/`make net-dev` update them too and a board with
+  no readable filesystem can still run them: `run <name>` and the `prog`/
+  `space` commands use the embedded copy, `run /<device>/<file>` still loads
+  from FAT.  Rebuilds are wired (`space.c` -> `space.bin` -> `space_embed.o` ->
+  `kernel.elf`).  Note: full-screen programs draw over the WM, which keeps
+  repainting its desktop — running them windowed/cleanly is a follow-up.
 
 ## Shell commands
 
 `cd [path]`, `ls [path]`, `cat <path>`, `save <path> <text>`, `rm <path>`,
-`mkdir <path>`, `rmdir <path>`, `hello`, `reboot`, `poweroff`, `run <file>`, `space`,
+`mkdir <path>`, `rmdir <path>`, `hello`, `reboot`, `poweroff`, `run <name>|<file>`, `space`,
+`prog`,
 `clock`, `colors`, `display`, `cpuinfo`, `mouseinfo`, `lsusb`, `lspci [id]`, `peek <addr> [len]`, `storage`, `mem`, `format <device>`,
 `ps`, `kill <pid>`, `help`, `clear`, `serial test|recv`, `kexec`, `dbg`.
 

+ 10 - 2
Makefile

@@ -251,7 +251,7 @@ syscall_stub.o: syscall_stub.S
 mem.o: mem.c mm.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o mem.o screen.o io.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o kdbg.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o lspci.o peek.o ne2000.o r8168.o netdev.o netcmd.o net.o netkexec.o netlog.o dhcp.o hello_embed.o
+kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o mem.o screen.o io.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o kdbg.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o lspci.o peek.o ne2000.o r8168.o netdev.o netcmd.o net.o netkexec.o netlog.o dhcp.o hello_embed.o prog_embed.o space_embed.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -348,6 +348,14 @@ hello.elf: hello.o
 hello_embed.o: hello.elf
 	$(LD) -m elf_i386 -r -b binary -o hello_embed.o hello.elf
 
+# User programs ship inside the kernel image so `make dev`/`make net-dev`
+# update them too (boards without a readable filesystem can still run them).
+prog_embed.o: prog.bin
+	$(LD) -m elf_i386 -r -b binary -o prog_embed.o prog.bin
+
+space_embed.o: space.bin
+	$(LD) -m elf_i386 -r -b binary -o space_embed.o space.bin
+
 bootstrap.o: bootstrap.c
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -451,4 +459,4 @@ net-dev: kernel.bin netkexec_send
 	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
 
 clean:
-	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o mem.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o kdbg.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o lspci.o peek.o ne2000.o r8168.o netdev.o netcmd.o net.o netkexec.o netlog.o dhcp.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.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 hello.elf hello_embed.o bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor
+	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o mem.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o kdbg.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o lspci.o peek.o ne2000.o r8168.o netdev.o netcmd.o net.o netkexec.o netlog.o dhcp.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.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 hello.elf hello_embed.o prog_embed.o space_embed.o bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor

+ 85 - 18
run.c

@@ -13,6 +13,79 @@
 #define PROGRAM_LOAD_ADDR 0x41000
 #define PROGRAM_LOAD_MAX  0x10000
 
+/* User programs embedded in the kernel image (like hello.elf): they travel
+ * with `make dev`/`make net-dev`, so a board with no readable filesystem can
+ * still run them.  Rebuilt from prog.c/space.c by the Makefile. */
+extern unsigned char _binary_prog_bin_start[], _binary_prog_bin_end[];
+extern unsigned char _binary_space_bin_start[], _binary_space_bin_end[];
+
+struct embedded_prog {
+	const char *name;
+	const unsigned char *start;
+	const unsigned char *end;
+};
+
+static const struct embedded_prog embedded_progs[] = {
+	{ "prog",  _binary_prog_bin_start,  _binary_prog_bin_end },
+	{ "space", _binary_space_bin_start, _binary_space_bin_end },
+};
+
+static int name_eq(const char *a, const char *b)
+{
+	unsigned int i = 0;
+
+	while (a[i] && b[i] && a[i] == b[i])
+		i++;
+	return a[i] == '\0' && b[i] == '\0';
+}
+
+/* Install the program API and hand control to the image at PROGRAM_LOAD_ADDR. */
+static void jump_to_program(void)
+{
+	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,
+	};
+
+	io_drain_keyboard();
+	*(struct prog_api *)PROGRAM_API_ADDR = api;
+	((void (*)(void))PROGRAM_LOAD_ADDR)();
+}
+
+/* Load the named embedded program and run it (0 = name not found). */
+static int run_embedded(const char *name)
+{
+	unsigned int i;
+
+	for (i = 0; i < sizeof(embedded_progs) / sizeof(embedded_progs[0]); i++) {
+		const struct embedded_prog *p = &embedded_progs[i];
+		unsigned int size;
+		unsigned char *dst = (unsigned char *)PROGRAM_LOAD_ADDR;
+		const unsigned char *src;
+
+		if (!name_eq(name, p->name))
+			continue;
+		size = (unsigned int)(p->end - p->start);
+		if (size == 0 || size > PROGRAM_LOAD_MAX) {
+			io_putstr("run: embedded image size out of range\n");
+			return 1;
+		}
+		for (src = p->start; src < p->end; )
+			*dst++ = *src++;
+		jump_to_program();
+		return 1;
+	}
+	return 0;
+}
+
 void program_run_main(const char *filename)
 {
 	fat_fs_t *fs;
@@ -22,10 +95,14 @@ void program_run_main(const char *filename)
 	int n;
 
 	if (!filename || !filename[0]) {
-		io_putstr("run: usage run /<device>/<file>\n");
+		io_putstr("run: usage run <name>|/<device>/<file>\n");
 		return;
 	}
 
+	/* Names of embedded programs first: `run prog`, `run space`. */
+	if (run_embedded(filename))
+		return;
+
 	if (vfs_resolve(filename, &fs, &fs_path) != 0) {
 		io_putstr("run: unknown device or invalid path\n");
 		return;
@@ -80,22 +157,12 @@ void program_run_serial_main(const char *arg)
 		io_putstr("run serial: no binary received (use 'serial recv' first)\n");
 		return;
 	}
+	jump_to_program();
+}
 
-	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)();
+/* Command entry for the embedded programs (`prog`, `space`). */
+void program_run_embedded_main(const char *name)
+{
+	if (!run_embedded(name))
+		io_putstr("run: no such embedded program\n");
 }

+ 22 - 2
terminal.c

@@ -28,6 +28,7 @@ extern void program_mouseinfo_main(void);
 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 program_run_embedded_main(const char *name);
 extern void program_lsusb_main(void);
 extern void program_storage_main(void);
 extern void program_mem_main(void);
@@ -313,6 +314,22 @@ static int is_cmd_peek(const char *line, unsigned int line_len, char *arg_buf, u
 	return (i == line_len);
 }
 
+static int is_cmd_prog(const char *line, unsigned int line_len)
+{
+	unsigned int i = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 4 > line_len)
+		return 0;
+	if (line[i] != 'p' || line[i+1] != 'r' || line[i+2] != 'o' ||
+	    line[i+3] != 'g')
+		return 0;
+	i += 4;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
+
 static int is_cmd_netinfo(const char *line, unsigned int line_len)
 {
 	unsigned int i = 0;
@@ -1278,7 +1295,9 @@ void term_run(struct term *t)
 							io_putstr("rm: path too long\n");
 						}
 					} else if (is_cmd_space(t->line, t->line_len)) {
-						io_putstr("space: not available in windowed mode yet\n");
+						term_spawn_arg(t, "space", program_run_embedded_main, "space");
+					} else if (is_cmd_prog(t->line, t->line_len)) {
+						term_spawn_arg(t, "prog", program_run_embedded_main, "prog");
 					} else if (is_cmd_display(t->line, t->line_len)) {
 						term_spawn(t, "display", program_display_main);
 					} else if (is_cmd_cpuinfo(t->line, t->line_len)) {
@@ -1317,8 +1336,9 @@ void term_run(struct term *t)
 						io_putstr("hello\n");
 						io_putstr("reboot\n");
 						io_putstr("poweroff\n");
-						io_putstr("run <file>\n");
+						io_putstr("run <name>|<file>\n");
 						io_putstr("space\n");
+						io_putstr("prog\n");
 						io_putstr("clock\n");
 						io_putstr("colors\n");
 						io_putstr("display\n");