Sfoglia il codice sorgente

Always show the mouse

Johannes Zellner 8 mesi fa
parent
commit
cc48a44b1a
8 ha cambiato i file con 392 aggiunte e 163 eliminazioni
  1. 8 5
      Makefile
  2. 2 0
      kernel.c
  3. 278 0
      mouse.c
  4. 12 0
      mouse.h
  5. 28 150
      mousecmd.c
  6. 11 8
      terminal.c
  7. 47 0
      vga.c
  8. 6 0
      vga.h

+ 8 - 5
Makefile

@@ -20,7 +20,7 @@ all: floppy.img usb.img final.bin root.img
 boot.o: boot.S
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.o: kernel.c idle.h io.h process.h terminal.h timer.h serial.h vga.h
+kernel.o: kernel.c idle.h io.h mouse.h process.h terminal.h timer.h serial.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 io.o: io.c io.h fbtext.h
@@ -32,7 +32,7 @@ font_8x8.o: font_8x8.c font_8x8.h
 fbtext.o: fbtext.c fbtext.h font_8x8.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-terminal.o: terminal.c terminal.h io.h fbtext.h idle.h process.h rtc.h timer.h disk.h
+terminal.o: terminal.c terminal.h io.h fbtext.h idle.h mouse.h process.h rtc.h timer.h disk.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 hello.o: hello.c io.h
@@ -89,7 +89,10 @@ display.o: display.c io.h vga.h gpu.h ati.h fbtext.h
 cpuinfo.o: cpuinfo.c io.h timer.h idle.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-mousecmd.o: mousecmd.c io.h timer.h
+mousecmd.o: mousecmd.c io.h mouse.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+mouse.o: mouse.c mouse.h timer.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 idle.o: idle.c idle.h
@@ -110,7 +113,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 gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.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 vga.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
@@ -191,4 +194,4 @@ run-usb: usb.img root.img
 	qemu-system-x86_64 -M pc -boot c -drive file=usb.img,format=raw,if=ide -drive file=root.img,format=raw,if=floppy
 
 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 gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.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 vga.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

+ 2 - 0
kernel.c

@@ -2,6 +2,7 @@
 
 #include "idle.h"
 #include "io.h"
+#include "mouse.h"
 #include "process.h"
 #include "terminal.h"
 #include "timer.h"
@@ -131,6 +132,7 @@ void kernel_main(void)
 	io_init();
 	idle_init();
 	timer_init();
+	mouse_init();
 	process_init();
 	terminal_run();
 }

+ 278 - 0
mouse.c

@@ -0,0 +1,278 @@
+#include "mouse.h"
+#include "timer.h"
+#include "vga.h"
+
+#define PS2_DATA   0x60
+#define PS2_STATUS 0x64
+#define PS2_CMD    0x64
+
+#define STATUS_OUT_FULL 0x01
+#define STATUS_IN_FULL  0x02
+#define STATUS_AUX_DATA 0x20
+
+#define MOUSE_ACK     0xFA
+#define MOUSE_RESEND  0xFE
+
+#define MOUSE_CUR_W 16
+#define MOUSE_CUR_H 16
+
+static const unsigned short mouse_pointer_bits[MOUSE_CUR_H] = {
+	0x0000,
+	0x8000, 0xC000, 0xE000, 0xF000, 0xF800, 0xFC00, 0xFE00, 0xFF00,
+	0xFF80, 0x9780, 0x0380, 0x0180, 0x0180, 0x0000, 0x0000
+};
+
+static int mouse_present_flag;
+static int mouse_streaming_flag;
+static unsigned int mouse_id;
+static int mouse_cursor_flag;
+
+static unsigned int fbw, fbh;
+static unsigned int cur_x, cur_y;
+static unsigned char cur_saved[MOUSE_CUR_W * MOUSE_CUR_H * 4];
+static unsigned char pkt[3];
+static unsigned int pkt_idx;
+
+static inline unsigned char inb(unsigned short port)
+{
+	unsigned char v;
+	__asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
+	return v;
+}
+
+static inline void outb(unsigned short port, unsigned char v)
+{
+	__asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
+}
+
+static void ps2_wait_input_empty(void)
+{
+	unsigned spin = 100000;
+	while (spin-- && (inb(PS2_STATUS) & STATUS_IN_FULL))
+		;
+}
+
+static void enable_aux_port(void)
+{
+	ps2_wait_input_empty();
+	outb(PS2_CMD, 0xA8);
+}
+
+static void mouse_cmd_write(unsigned char val)
+{
+	ps2_wait_input_empty();
+	outb(PS2_CMD, 0xD4);
+	ps2_wait_input_empty();
+	outb(PS2_DATA, val);
+}
+
+static int mouse_read_aux(unsigned char *out, unsigned int timeout_ticks)
+{
+	unsigned int t0 = get_ticks_ms();
+	while (get_ticks_ms() - t0 < timeout_ticks) {
+		unsigned char st = inb(PS2_STATUS);
+		if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) ==
+		    (STATUS_OUT_FULL | STATUS_AUX_DATA)) {
+			*out = inb(PS2_DATA);
+			return 0;
+		}
+	}
+	return -1;
+}
+
+static int mouse_expect_ack(void)
+{
+	unsigned char b;
+	if (mouse_read_aux(&b, 500) != 0)
+		return -1;
+	if (b == MOUSE_RESEND)
+		return -2;
+	if (b != MOUSE_ACK)
+		return -1;
+	return 0;
+}
+
+static int mouse_send_cmd_ack(unsigned char cmd)
+{
+	int r;
+	mouse_cmd_write(cmd);
+	r = mouse_expect_ack();
+	if (r == -2) {
+		mouse_cmd_write(cmd);
+		r = mouse_expect_ack();
+	}
+	return r;
+}
+
+static int mouse_bit_at(int r, int c)
+{
+	if (r < 0 || c < 0 || r >= (int)MOUSE_CUR_H || c >= (int)MOUSE_CUR_W)
+		return 0;
+	return (mouse_pointer_bits[(unsigned int)r] & (0x8000u >> c)) != 0;
+}
+
+static void mouse_draw_cursor(unsigned int x0, unsigned int y0)
+{
+	unsigned int r, c;
+
+	for (r = 0; r < MOUSE_CUR_H; r++) {
+		for (c = 0; c < MOUSE_CUR_W; c++) {
+			if (mouse_bit_at((int)r, (int)c))
+				vga_put_pixel_rgb(x0 + c, y0 + r, 255, 255, 255);
+		}
+	}
+	for (r = 0; r < MOUSE_CUR_H; r++) {
+		for (c = 0; c < MOUSE_CUR_W; c++) {
+			if (!mouse_bit_at((int)r, (int)c)) {
+				if (mouse_bit_at((int)r - 1, (int)c) ||
+				    mouse_bit_at((int)r + 1, (int)c) ||
+				    mouse_bit_at((int)r, (int)c - 1) ||
+				    mouse_bit_at((int)r, (int)c + 1))
+					vga_put_pixel_rgb(x0 + c, y0 + r, 0, 0, 0);
+			}
+		}
+	}
+}
+
+static void mouse_clamp_ptr(long *nx, long *ny)
+{
+	long maxx = (long)fbw - (long)MOUSE_CUR_W;
+	long maxy = (long)fbh - (long)MOUSE_CUR_H;
+
+	if (maxx < 0)
+		maxx = 0;
+	if (maxy < 0)
+		maxy = 0;
+	if (*nx < 0)
+		*nx = 0;
+	else if (*nx > maxx)
+		*nx = maxx;
+	if (*ny < 0)
+		*ny = 0;
+	else if (*ny > maxy)
+		*ny = maxy;
+}
+
+static void mouse_cursor_setup(void)
+{
+	mouse_cursor_flag = 0;
+	if (!vga_has_vesa())
+		return;
+	fbw = vga_fb_width();
+	fbh = vga_fb_height();
+	if (fbw < MOUSE_CUR_W || fbh < MOUSE_CUR_H)
+		return;
+	/* Start cursor at a fixed visible spot (clamped to the framebuffer).
+	   Using a constant offset avoids any overlap with the terminal header/prompt. */
+	cur_x = 100;
+	cur_y = 100;
+	{
+		long nx = (long)cur_x;
+		long ny = (long)cur_y;
+		mouse_clamp_ptr(&nx, &ny);
+		cur_x = (unsigned int)nx;
+		cur_y = (unsigned int)ny;
+	}
+	vga_fb_read_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H, cur_saved);
+	mouse_draw_cursor(cur_x, cur_y);
+	mouse_cursor_flag = 1;
+}
+
+void mouse_init(void)
+{
+	unsigned char id = 0;
+
+	mouse_present_flag = 0;
+	mouse_streaming_flag = 0;
+	mouse_id = 0;
+	mouse_cursor_flag = 0;
+	pkt_idx = 0;
+
+	enable_aux_port();
+
+	if (mouse_send_cmd_ack(0xF5) != 0)
+		return;
+
+	mouse_cmd_write(0xF2);
+	if (mouse_expect_ack() != 0)
+		return;
+	if (mouse_read_aux(&id, 500) != 0)
+		return;
+
+	mouse_present_flag = 1;
+	mouse_id = id;
+	mouse_cursor_setup();
+
+	if (mouse_send_cmd_ack(0xF4) != 0)
+		return;
+	mouse_streaming_flag = 1;
+}
+
+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)
+		return;
+
+	for (;;) {
+		unsigned char st = inb(PS2_STATUS);
+		unsigned char b0;
+		int dx, dy;
+
+		if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) !=
+		    (STATUS_OUT_FULL | STATUS_AUX_DATA))
+			return;
+
+		pkt[pkt_idx++] = inb(PS2_DATA);
+		if (pkt_idx < 3)
+			continue;
+		pkt_idx = 0;
+
+		b0 = pkt[0];
+		dx = (int)(signed char)pkt[1];
+		dy = (int)(signed char)pkt[2];
+		if (b0 & 0x10)
+			dx |= ~0xFF;
+		if (b0 & 0x20)
+			dy |= ~0xFF;
+		if ((b0 & 0xC0) != 0)
+			continue;
+
+		if (mouse_cursor_flag) {
+			long nx = (long)cur_x + dx;
+			long ny = (long)cur_y - dy;
+			mouse_clamp_ptr(&nx, &ny);
+			if ((unsigned int)nx != cur_x || (unsigned int)ny != cur_y) {
+				vga_fb_write_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H,
+					cur_saved);
+				cur_x = (unsigned int)nx;
+				cur_y = (unsigned int)ny;
+				vga_fb_read_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H,
+					cur_saved);
+				mouse_draw_cursor(cur_x, cur_y);
+			}
+		}
+	}
+}
+
+int mouse_is_present(void)
+{
+	return mouse_present_flag;
+}
+
+int mouse_streaming_enabled(void)
+{
+	return mouse_streaming_flag;
+}
+
+unsigned int mouse_device_id(void)
+{
+	return mouse_id;
+}
+
+int mouse_cursor_visible(void)
+{
+	return mouse_cursor_flag;
+}

+ 12 - 0
mouse.h

@@ -0,0 +1,12 @@
+#ifndef MOUSE_H
+#define MOUSE_H
+
+void mouse_init(void);
+void mouse_poll_update(void);
+
+int mouse_is_present(void);
+int mouse_streaming_enabled(void);
+unsigned int mouse_device_id(void);
+int mouse_cursor_visible(void);
+
+#endif

+ 28 - 150
mousecmd.c

@@ -1,99 +1,7 @@
-/* mouse command: poll PS/2 aux (mouse) for 10s, print packets (relative dx/dy + accumulators). */
+/* mouseinfo command: print mouse hardware/protocol info and exit. */
 
 #include "io.h"
-#include "timer.h"
-
-#define PS2_DATA   0x60
-#define PS2_STATUS 0x64
-#define PS2_CMD    0x64
-
-#define STATUS_OUT_FULL 0x01
-#define STATUS_IN_FULL  0x02
-#define STATUS_AUX_DATA 0x20
-
-#define MOUSE_ACK     0xFA
-#define MOUSE_RESEND  0xFE
-
-static inline unsigned char inb(unsigned short port)
-{
-	unsigned char v;
-	__asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
-	return v;
-}
-
-static inline void outb(unsigned short port, unsigned char v)
-{
-	__asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
-}
-
-static void ps2_wait_input_empty(void)
-{
-	unsigned spin = 100000;
-	while (spin-- && (inb(PS2_STATUS) & STATUS_IN_FULL))
-		;
-}
-
-static void mouse_cmd_write(unsigned char val)
-{
-	ps2_wait_input_empty();
-	outb(PS2_CMD, 0xD4);
-	ps2_wait_input_empty();
-	outb(PS2_DATA, val);
-}
-
-/* Read next byte from aux port (trackpoint/touchpad), with timeout in timer ticks (~ms). */
-static int mouse_read_aux(unsigned char *out, unsigned int timeout_ticks)
-{
-	unsigned int t0 = get_ticks_ms();
-	while (get_ticks_ms() - t0 < timeout_ticks) {
-		unsigned char st = inb(PS2_STATUS);
-		if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) ==
-		    (STATUS_OUT_FULL | STATUS_AUX_DATA)) {
-			*out = inb(PS2_DATA);
-			return 0;
-		}
-	}
-	return -1;
-}
-
-static int mouse_expect_ack(void)
-{
-	unsigned char b;
-	if (mouse_read_aux(&b, 500) != 0)
-		return -1;
-	if (b == MOUSE_RESEND) {
-		/* one retry path left to caller */
-		return -2;
-	}
-	if (b != MOUSE_ACK)
-		return -1;
-	return 0;
-}
-
-static void enable_aux_port(void)
-{
-	ps2_wait_input_empty();
-	outb(PS2_CMD, 0xA8);
-}
-
-static int mouse_enable_streaming(void)
-{
-	int r;
-	enable_aux_port();
-	mouse_cmd_write(0xF4);
-	r = mouse_expect_ack();
-	if (r == -2) {
-		mouse_cmd_write(0xF4);
-		r = mouse_expect_ack();
-	}
-	return r;
-}
-
-static void mouse_disable_streaming(void)
-{
-	mouse_cmd_write(0xF5);
-	(void)mouse_expect_ack();
-}
+#include "mouse.h"
 
 static void put_uint(unsigned int n)
 {
@@ -111,67 +19,37 @@ static void put_uint(unsigned int n)
 	io_putstr(buf + i + 1);
 }
 
-static void put_sdec(int v)
+static void put_hex8(unsigned int x)
 {
-	if (v < 0) {
-		io_putchar('-');
-		put_uint(0u - (unsigned int)v);
-	} else
-		put_uint((unsigned int)v);
+	static const char hex[] = "0123456789ABCDEF";
+	io_putchar(hex[(x >> 4) & 0xF]);
+	io_putchar(hex[x & 0xF]);
 }
 
-void program_mouse_main(void)
+void program_mouseinfo_main(void)
 {
-	enum { RUN_MS = 10000 };
-	unsigned int t_start;
-	int acc_x = 0, acc_y = 0;
-	unsigned char pkt[3];
-	unsigned int pkt_idx = 0;
+	unsigned int id = mouse_device_id();
 
-	io_putstr("PS/2 mouse: 10s (aux poll, 3-byte packets)\n");
-
-	if (mouse_enable_streaming() != 0) {
-		io_putstr("mouse: enable failed (no ACK?)\n");
+	io_putstr("mouse present ");
+	io_putstr(mouse_is_present() ? "yes\n" : "no\n");
+	if (!mouse_is_present())
 		return;
-	}
-
-	t_start = get_ticks_ms();
-	while (get_ticks_ms() - t_start < RUN_MS) {
-		unsigned char st = inb(PS2_STATUS);
-		if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) ==
-		    (STATUS_OUT_FULL | STATUS_AUX_DATA)) {
-			unsigned char b = inb(PS2_DATA);
-			pkt[pkt_idx++] = b;
-			if (pkt_idx == 3) {
-				unsigned char b0 = pkt[0];
-				int dx = (int)(signed char)pkt[1];
-				int dy = (int)(signed char)pkt[2];
-				if (b0 & 0x10)
-					dx |= ~0xFF;
-				if (b0 & 0x20)
-					dy |= ~0xFF;
-				/* overflow bits in b0: do not add to accumulators */
-				if (!(b0 & 0xC0)) {
-					acc_x += dx;
-					acc_y += dy;
-				}
-				put_uint(get_ticks_ms() - t_start);
-				io_putchar(' ');
-				put_uint(b0 & 7);
-				io_putchar(' ');
-				put_sdec(dx);
-				io_putchar(' ');
-				put_sdec(dy);
-				io_putchar(' ');
-				put_sdec(acc_x);
-				io_putchar(' ');
-				put_sdec(acc_y);
-				io_putchar('\n');
-				pkt_idx = 0;
-			}
-		}
-	}
 
-	mouse_disable_streaming();
-	io_putstr("PS/2 mouse: done\n");
+	io_putstr("mouse id 0x");
+	put_hex8(id & 0xFFu);
+	io_putchar('\n');
+	io_putstr("mouse protocol ");
+	if (id == 0x03)
+		io_putstr("intellimouse (wheel)\n");
+	else if (id == 0x04)
+		io_putstr("intellimouse explorer (5-button)\n");
+	else
+		io_putstr("standard ps/2\n");
+	io_putstr("mouse packet bytes ");
+	put_uint((id == 0x03 || id == 0x04) ? 4u : 3u);
+	io_putchar('\n');
+	io_putstr("mouse streaming ");
+	io_putstr(mouse_streaming_enabled() ? "on\n" : "off\n");
+	io_putstr("mouse cursor ");
+	io_putstr(mouse_cursor_visible() ? "visible\n" : "hidden\n");
 }

+ 11 - 8
terminal.c

@@ -4,6 +4,7 @@
 #include "io.h"
 #include "fbtext.h"
 #include "idle.h"
+#include "mouse.h"
 #include "process.h"
 #include "rtc.h"
 #include "terminal.h"
@@ -19,7 +20,7 @@ extern void program_clock_main(void);
 extern void program_colors_main(void);
 extern void program_display_main(void);
 extern void program_cpuinfo_main(void);
-extern void program_mouse_main(void);
+extern void program_mouseinfo_main(void);
 extern void program_power_main(const char *args);
 extern void disk_set_drive(unsigned int drive);
 extern unsigned int disk_get_drive(void);
@@ -211,17 +212,18 @@ static int is_cmd_cpuinfo(const char *line, unsigned int line_len)
 	return (i == line_len);
 }
 
-static int is_cmd_mouse(const char *line, unsigned int line_len)
+static int is_cmd_mouseinfo(const char *line, unsigned int line_len)
 {
 	unsigned int i = 0;
 	while (i < line_len && line[i] == ' ')
 		i++;
-	if (i + 5 > line_len)
+	if (i + 9 > line_len)
 		return 0;
 	if (line[i] != 'm' || line[i+1] != 'o' || line[i+2] != 'u' ||
-	    line[i+3] != 's' || line[i+4] != 'e')
+	    line[i+3] != 's' || line[i+4] != 'e' || line[i+5] != 'i' ||
+	    line[i+6] != 'n' || line[i+7] != 'f' || line[i+8] != 'o')
 		return 0;
-	i += 5;
+	i += 9;
 	while (i < line_len && line[i] == ' ')
 		i++;
 	return (i == line_len);
@@ -508,6 +510,7 @@ void terminal_run(void)
 	line_start = io_get_cursor();
 
 	for (;;) {
+		mouse_poll_update();
 		update_datetime();
 		update_cursor_blink();
 		{
@@ -593,8 +596,8 @@ void terminal_run(void)
 					} else if (is_cmd_cpuinfo(line, line_len)) {
 						if (process_create("cpuinfo", program_cpuinfo_main) < 0)
 							io_putstr("error: too many processes\n");
-					} else if (is_cmd_mouse(line, line_len)) {
-						if (process_create("mouse", program_mouse_main) < 0)
+					} else if (is_cmd_mouseinfo(line, line_len)) {
+						if (process_create("mouseinfo", program_mouseinfo_main) < 0)
 							io_putstr("error: too many processes\n");
 					} else if (is_cmd_power(line, line_len, power_args, POWER_ARGS_MAX)) {
 						if (process_create_arg("power", program_power_main, power_args) < 0)
@@ -617,7 +620,7 @@ void terminal_run(void)
 						io_putstr("run <file>  space\n");
 						io_putstr("clock     colors\n");
 						io_putstr("display   cpuinfo\n");
-						io_putstr("mouse\n");
+						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");

+ 47 - 0
vga.c

@@ -185,6 +185,53 @@ void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned
 	vga_lfb_put_pixel(x, y, r, g, b);
 }
 
+static unsigned int vga_pixel_bytes(void)
+{
+	if (vga_fb_bpp_val == 16)
+		return 2;
+	if (vga_fb_bpp_val == 24)
+		return 3;
+	return 4;
+}
+
+void vga_fb_read_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
+		unsigned char *buf)
+{
+	unsigned int pitch, pb, row, col;
+	unsigned char *fb;
+	volatile unsigned char *src;
+
+	vga_cache_boot_fb();
+	if (!vga_fb_lfb || vga_fb_w == 0 || w == 0 || h == 0)
+		return;
+	pitch = vga_fb_pitch_val;
+	pb = vga_pixel_bytes();
+	fb = (unsigned char *)vga_fb_lfb;
+	for (row = 0; row < h; row++) {
+		src = fb + (unsigned long)(y + row) * pitch + x * pb;
+		for (col = 0; col < w * pb; col++)
+			buf[row * w * pb + col] = src[col];
+	}
+}
+
+void vga_fb_write_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
+		const unsigned char *buf)
+{
+	unsigned int pitch, pb, row, col;
+	volatile unsigned char *dst;
+
+	vga_cache_boot_fb();
+	if (!vga_fb_lfb || vga_fb_w == 0 || w == 0 || h == 0)
+		return;
+	pitch = vga_fb_pitch_val;
+	pb = vga_pixel_bytes();
+	for (row = 0; row < h; row++) {
+		dst = vga_fb_lfb + (unsigned long)(y + row) * pitch + x * pb;
+		for (col = 0; col < w * pb; col++)
+			dst[col] = buf[row * w * pb + col];
+	}
+}
+
 void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
 {
 	unsigned int pitch = vga_fb_pitch_val;

+ 6 - 0
vga.h

@@ -29,4 +29,10 @@ int vga_has_vesa(void);
 void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b);
 void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b);
 
+/* Tightly packed row-major: each row is w * pixel_bytes contiguous (pixel_bytes from bpp). */
+void vga_fb_read_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
+		unsigned char *buf);
+void vga_fb_write_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
+		const unsigned char *buf);
+
 #endif