Explorar o código

Add subdirectory support (folders)

- generalize fat.c to a directory-based API (root = cluster 0)
- fat_mkdir/fat_rmdir, fat_resolve_path/fat_resolve_dir
- ls/cat/save/rm now accept paths (a/b/c)
- fix fat_mkdir not marking the new dir cluster allocated in the FAT
Johannes Zellner hai 1 mes
pai
achega
fd0f28eecf
Modificáronse 11 ficheiros con 731 adicións e 262 borrados
  1. 3 3
      AGENTS.md
  2. 8 2
      Makefile
  3. 18 32
      cat.c
  4. 488 91
      fat.c
  5. 32 8
      fat.h
  6. 21 9
      ls.c
  7. 29 0
      mkdir.c
  8. 13 59
      rm.c
  9. 29 0
      rmdir.c
  10. 23 52
      save.c
  11. 67 6
      terminal.c

+ 3 - 3
AGENTS.md

@@ -59,7 +59,7 @@ make clean
   8×8 font, rainbow separator, live datetime header, blinking cursor.
 - **Shell**: `terminal.c` — history, line editing, command dispatch (hand-rolled
   `is_cmd_*` parsers).
-- **Filesystem**: `fat.c` (FAT12 read/write), `disk.c` (FDC + ISA DMA ch2, retries).
+- **Filesystem**: `fat.c` (FAT12 read/write + subdirectories), `disk.c` (FDC + ISA DMA ch2, retries).
 - **Serial**: `serial.c`, `serial_ring_buf.c/h`, `bin_recv.c/h`, `serial_recv.c`,
   `serial_loop.c`, `send_serial.c` — 16550 UART, IRQ-driven ring buffer, XMODEM.
 - **Hot-reload (kexec)**: `kexec.c/h`, `kexec_tramp.S`, `bootstrap.c`, and the
@@ -72,8 +72,8 @@ make clean
 
 ## Shell commands
 
-`hello`, `reboot`, `poweroff`, `ls`, `cat <file>`, `save <file> <text>`, `rm <file>`,
-`run <file>`, `space`, `clock`,
+`hello`, `reboot`, `poweroff`, `ls [path]`, `cat <path>`, `save <path> <text>`, `rm <path>`,
+`mkdir <path>`, `rmdir <path>`, `run <file>`, `space`, `clock`,
 `colors`, `display`, `cpuinfo`, `mouseinfo`, `power [info|set …]`, `drive <0|1>`,
 `ps`, `kill <pid>`, `help`, `clear`, `serial test|recv`, `kexec`, `dbg`.
 

+ 8 - 2
Makefile

@@ -128,6 +128,12 @@ save.o: save.c fat.h io.h
 rm.o: rm.c fat.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
+mkdir.o: mkdir.c fat.h io.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+rmdir.o: rmdir.c fat.h io.h
+	$(CC) $(CFLAGS) -o $@ $<
+
 boot.elf: boot.o
 	$(LD) $(BOOT_LDFLAGS) -o $@ $<
 
@@ -143,7 +149,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 serial_ring_buf.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 kexec.o kexec_tramp.o dbg.o save.o rm.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_ring_buf.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 kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -292,4 +298,4 @@ dev: kernel.bin kexec_send
 	./kexec_send $(SERIAL_DEV) $(SERIAL_BAUD) $$entry kernel.bin
 
 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_ring_buf.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 kexec.o kexec_tramp.o dbg.o save.o rm.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 bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send
+	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_ring_buf.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 kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.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 bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send

+ 18 - 32
cat.c

@@ -1,49 +1,23 @@
-/* cat command: print file contents to screen. */
+/* cat command: print file contents to screen (supports paths). */
 
 #include "fat.h"
 #include "io.h"
 
 #define DUMP_BUF_SIZE 4096
-#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] != '\0' && 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] != '\0' && 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_cat_main(const char *filename)
 {
 	static unsigned char buf[DUMP_BUF_SIZE];
-	char name_8_3[NAME_BUF_SIZE];
+	unsigned int dir_cluster;
+	char name_8_3[12];
 	int n;
 	unsigned int i;
 
 	if (!filename || !filename[0]) {
-		io_putstr("cat: usage cat <filename>\n");
+		io_putstr("cat: usage cat <file>\n");
 		return;
 	}
-	to_8_3(filename, 64, name_8_3);
+
 	n = fat_init();
 	if (n == -2) {
 		io_putstr("cat: disk read error\n");
@@ -53,7 +27,18 @@ void program_cat_main(const char *filename)
 		io_putstr("cat: no FAT filesystem found\n");
 		return;
 	}
-	n = fat_read_file(name_8_3, buf, DUMP_BUF_SIZE);
+
+	n = fat_resolve_path(filename, &dir_cluster, name_8_3);
+	if (n == -2) {
+		io_putstr("cat: disk error\n");
+		return;
+	}
+	if (n != 0) {
+		io_putstr("cat: not found\n");
+		return;
+	}
+
+	n = fat_read_file_in(dir_cluster, name_8_3, buf, DUMP_BUF_SIZE);
 	if (n == -2) {
 		io_putstr("cat: disk read error\n");
 		return;
@@ -62,6 +47,7 @@ void program_cat_main(const char *filename)
 		io_putstr("cat: file not found\n");
 		return;
 	}
+
 	for (i = 0; i < (unsigned int)n; i++) {
 		unsigned char c = buf[i];
 		if (c >= 0x20 && c < 0x7F)

+ 488 - 91
fat.c

@@ -354,97 +354,277 @@ static int fat_free_chain(unsigned int cluster)
 	return 0;
 }
 
-/* Read cluster/size from a 32-byte directory entry at absolute byte offset. */
-static int fat_dir_entry_read(unsigned int byte_off, unsigned int *cluster, unsigned int *size)
+/* Convert a name (length `len`) into an 11-byte 8.3 name (uppercase, padded). */
+static void fat_name_to_8_3(const char *in, unsigned int len, unsigned char out[11])
 {
-	unsigned int lba = byte_off / SECTOR_SIZE;
-	unsigned int within = byte_off % SECTOR_SIZE;
+	unsigned int i, j, dot;
+
+	for (i = 0; i < len && in[i] == ' '; i++)
+		;
+	dot = i;
+	while (dot < len && in[dot] != '.' && in[dot] != ' ')
+		dot++;
+	j = 0;
+	while (i < dot && j < 8) {
+		out[j++] = (unsigned char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
+		i++;
+	}
+	while (j < 8)
+		out[j++] = ' ';
+	if (i < len && in[i] == '.')
+		i++;
+	while (i < len && in[i] != ' ' && j < 11) {
+		out[j++] = (unsigned char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
+		i++;
+	}
+	while (j < 11)
+		out[j++] = ' ';
+}
 
-	if (fat_read_sector(lba) != 0)
+/* Iterate the entries of a directory (0 = root, else first cluster). For each
+ * entry load the sector and call cb(lba, offset, ctx); cb returns non-zero to
+ * stop. Returns 0, or -2 on disk error. */
+static int dir_each_entry(unsigned int dir_cluster,
+			  int (*cb)(unsigned int lba, unsigned int offset, void *ctx),
+			  void *ctx)
+{
+	if (dir_cluster == 0) {
+		unsigned int sec;
+		for (sec = 0; sec < fat_root_sectors; sec++) {
+			unsigned int off;
+			if (fat_read_sector(fat_root_start + sec) != 0)
+				return -2;
+			for (off = 0; off < SECTOR_SIZE; off += ROOT_ENTRY_SIZE)
+				if (cb(fat_root_start + sec, off, ctx))
+					return 0;
+		}
+	} else {
+		unsigned int cluster = dir_cluster;
+		while (cluster >= 2 && cluster < FAT12_EOC_MIN) {
+			unsigned int base = fat_data_start + (cluster - 2) * fat_sec_per_cluster;
+			unsigned int i;
+			for (i = 0; i < fat_sec_per_cluster; i++) {
+				unsigned int off;
+				if (fat_read_sector(base + i) != 0)
+					return -2;
+				for (off = 0; off < SECTOR_SIZE; off += ROOT_ENTRY_SIZE)
+					if (cb(base + i, off, ctx))
+						return 0;
+			}
+			if (fat_entry_read(cluster, &cluster) != 0)
+				return -2;
+		}
+	}
+	return 0;
+}
+
+struct lookup_ctx {
+	const unsigned char *name;
+	unsigned int cluster, size;
+	unsigned char attr;
+	unsigned int lba, offset;
+	int found;
+};
+
+static int lookup_cb(unsigned int lba, unsigned int offset, void *vctx)
+{
+	struct lookup_ctx *c = vctx;
+	unsigned char *e = sector_buf + offset;
+	unsigned int i;
+
+	if (e[0] == 0x00)
+		return 1;  /* end of directory */
+	if (e[0] == 0xE5)
+		return 0;  /* deleted */
+	for (i = 0; i < 11; i++)
+		if ((e[i] >= 'a' ? e[i] - 32 : e[i]) != c->name[i])
+			break;
+	if (i == 11) {
+		c->cluster = (unsigned int)e[26] | ((unsigned int)e[27] << 8);
+		c->size = (unsigned int)e[28] | ((unsigned int)e[29] << 8)
+			| ((unsigned int)e[30] << 16) | ((unsigned int)e[31] << 24);
+		c->attr = e[11];
+		c->lba = lba;
+		c->offset = offset;
+		c->found = 1;
+		return 1;
+	}
+	return 0;
+}
+
+/* Look up `name` (11-byte, uppercase) in directory `dir_cluster`. */
+static int fat_lookup(unsigned int dir_cluster, const unsigned char name[11],
+		      unsigned int *entry_cluster, unsigned int *size, unsigned char *attr,
+		      unsigned int *entry_lba, unsigned int *entry_offset)
+{
+	struct lookup_ctx c;
+
+	c.name = name;
+	c.found = 0;
+	c.cluster = c.size = 0;
+	c.attr = 0;
+	if (dir_each_entry(dir_cluster, lookup_cb, &c) != 0)
 		return -2;
-	if (cluster)
-		*cluster = (unsigned int)sector_buf[within + 26] | ((unsigned int)sector_buf[within + 27] << 8);
-	if (size)
-		*size = (unsigned int)sector_buf[within + 28] | ((unsigned int)sector_buf[within + 29] << 8)
-			| ((unsigned int)sector_buf[within + 30] << 16) | ((unsigned int)sector_buf[within + 31] << 24);
+	if (!c.found)
+		return -1;
+	if (entry_cluster) *entry_cluster = c.cluster;
+	if (size) *size = c.size;
+	if (attr) *attr = c.attr;
+	if (entry_lba) *entry_lba = c.lba;
+	if (entry_offset) *entry_offset = c.offset;
 	return 0;
 }
 
-/* Write a 32-byte directory entry at absolute byte offset. */
-static int fat_dir_entry_write(unsigned int byte_off, const unsigned char name[11],
-			       unsigned char attr, unsigned int cluster, unsigned int size)
+struct free_ctx {
+	unsigned int lba, offset;
+	int found;
+};
+
+static int find_free_cb(unsigned int lba, unsigned int offset, void *vctx)
+{
+	struct free_ctx *c = vctx;
+	if (sector_buf[offset] == 0x00 || sector_buf[offset] == 0xE5) {
+		c->lba = lba;
+		c->offset = offset;
+		c->found = 1;
+		return 1;
+	}
+	return 0;
+}
+
+/* Find a free (0x00 or 0xE5) entry in `dir_cluster`. Returns 0 and fills
+ * lba/offset, or -1 if the directory is full, -2 on disk error. */
+static int dir_find_free(unsigned int dir_cluster, unsigned int *lba, unsigned int *offset)
+{
+	struct free_ctx c;
+	c.found = 0;
+	if (dir_each_entry(dir_cluster, find_free_cb, &c) != 0)
+		return -2;
+	if (!c.found)
+		return -1;
+	*lba = c.lba;
+	*offset = c.offset;
+	return 0;
+}
+
+/* Write a 32-byte directory entry at (lba, offset). */
+static int fat_dir_entry_write(unsigned int lba, unsigned int offset,
+			       const unsigned char name[11], unsigned char attr,
+			       unsigned int cluster, unsigned int size)
 {
-	unsigned int lba = byte_off / SECTOR_SIZE;
-	unsigned int within = byte_off % SECTOR_SIZE;
 	unsigned int i;
 
 	if (fat_read_sector(lba) != 0)
 		return -2;
 
 	for (i = 0; i < 11; i++)
-		sector_buf[within + i] = name[i];
-	sector_buf[within + 11] = attr;
+		sector_buf[offset + i] = name[i];
+	sector_buf[offset + 11] = attr;
 	for (i = 12; i < 26; i++)
-		sector_buf[within + i] = 0;
-	sector_buf[within + 26] = (unsigned char)(cluster & 0xFF);
-	sector_buf[within + 27] = (unsigned char)((cluster >> 8) & 0xFF);
-	sector_buf[within + 28] = (unsigned char)(size & 0xFF);
-	sector_buf[within + 29] = (unsigned char)((size >> 8) & 0xFF);
-	sector_buf[within + 30] = (unsigned char)((size >> 16) & 0xFF);
-	sector_buf[within + 31] = (unsigned char)((size >> 24) & 0xFF);
+		sector_buf[offset + i] = 0;
+	sector_buf[offset + 26] = (unsigned char)(cluster & 0xFF);
+	sector_buf[offset + 27] = (unsigned char)((cluster >> 8) & 0xFF);
+	sector_buf[offset + 28] = (unsigned char)(size & 0xFF);
+	sector_buf[offset + 29] = (unsigned char)((size >> 8) & 0xFF);
+	sector_buf[offset + 30] = (unsigned char)((size >> 16) & 0xFF);
+	sector_buf[offset + 31] = (unsigned char)((size >> 24) & 0xFF);
 
 	return (disk_write_sectors(lba, sector_buf, 1) != 0) ? -2 : 0;
 }
 
-/* Find the byte offset of a root entry matching `name`, or -1 / -2. */
-static int fat_find_root_entry_off(const unsigned char name[11])
+/* Mark the entry at (lba, offset) deleted (0xE5). */
+static int fat_mark_deleted(unsigned int lba, unsigned int offset)
 {
-	unsigned int sec, off;
+	if (fat_read_sector(lba) != 0)
+		return -2;
+	sector_buf[offset] = 0xE5;
+	return (disk_write_sectors(lba, sector_buf, 1) != 0) ? -2 : 0;
+}
 
-	for (sec = 0; sec < fat_root_sectors; sec++) {
-		if (fat_read_sector(fat_root_start + sec) != 0)
-			return -2;
-		for (off = 0; off < SECTOR_SIZE; off += ROOT_ENTRY_SIZE) {
-			unsigned char *e = sector_buf + off;
-			unsigned int i;
-			if (e[0] == 0x00)
-				return -1;
-			if (e[0] == 0xE5 || (e[11] & 0x10))
-				continue;
-			for (i = 0; i < 11; i++)
-				if ((e[i] >= 'a' ? e[i] - 32 : e[i]) != name[i])
-					break;
-			if (i == 11)
-				return (int)((fat_root_start + sec) * SECTOR_SIZE + off);
-		}
-	}
-	return -1;
+/* ---- public directory API ---- */
+
+struct list_ctx {
+	fat_root_callback_t callback;
+};
+
+static int list_cb(unsigned int lba, unsigned int offset, void *vctx)
+{
+	struct list_ctx *c = vctx;
+	unsigned char *e = sector_buf + offset;
+	char name_buf[12];
+	unsigned int size;
+
+	(void)lba;
+	if (e[0] == 0x00)
+		return 1;  /* end of directory */
+	if (e[0] == 0xE5)
+		return 0;
+	if ((e[11] & 0x18) == 0x08)
+		return 0;  /* volume label */
+	trim_name_8_3((const char *)e, name_buf);
+	size = (unsigned int)e[28] | ((unsigned int)e[29] << 8)
+		| ((unsigned int)e[30] << 16) | ((unsigned int)e[31] << 24);
+	c->callback(name_buf, (e[11] & 0x10) ? 1 : 0, size);
+	return 0;
 }
 
-/* Find a free (0x00 or 0xE5) root entry; return its byte offset, -1, or -2. */
-static int fat_find_free_root_entry(void)
+int fat_list_dir(unsigned int dir_cluster, fat_root_callback_t callback)
 {
-	unsigned int sec, off;
+	struct list_ctx c;
+	c.callback = callback;
+	if (!fat_ready || !callback)
+		return -1;
+	return (dir_each_entry(dir_cluster, list_cb, &c) != 0) ? -2 : 0;
+}
 
-	for (sec = 0; sec < fat_root_sectors; sec++) {
-		if (fat_read_sector(fat_root_start + sec) != 0)
+int fat_read_file_in(unsigned int dir_cluster, const char *name_8_3, void *buf, unsigned int max_len)
+{
+	unsigned int cluster, size;
+	unsigned char attr;
+	unsigned int copied = 0;
+	unsigned char *dst = buf;
+	int r;
+
+	if (!fat_ready || !buf || !name_8_3)
+		return -1;
+
+	r = fat_lookup(dir_cluster, (const unsigned char *)name_8_3, &cluster, &size, &attr, 0, 0);
+	if (r == -2)
+		return -2;
+	if (r != 0 || (attr & 0x10))
+		return -1;
+
+	if (size > max_len)
+		size = max_len;
+
+	while (cluster >= 2 && cluster < FAT12_EOC_MIN && copied < size) {
+		unsigned int sector = fat_data_start + (cluster - 2) * fat_sec_per_cluster;
+		unsigned int chunk = fat_sec_per_cluster * SECTOR_SIZE;
+		unsigned int n_sec;
+		if (chunk > size - copied)
+			chunk = size - copied;
+		n_sec = (chunk + SECTOR_SIZE - 1) / SECTOR_SIZE;
+		if (disk_read_sectors(sector, dst + copied, n_sec) != 0)
+			return -2;
+		copied += chunk;
+		if (copied >= size)
+			break;
+		if (fat_entry_read(cluster, &cluster) != 0)
 			return -2;
-		for (off = 0; off < SECTOR_SIZE; off += ROOT_ENTRY_SIZE) {
-			if (sector_buf[off] == 0x00 || sector_buf[off] == 0xE5)
-				return (int)((fat_root_start + sec) * SECTOR_SIZE + off);
-		}
 	}
-	return -1;
+	return (int)copied;
 }
 
-int fat_write_file(const char *name_8_3, const void *buf, unsigned int size)
+int fat_write_file_in(unsigned int dir_cluster, const char *name_8_3, const void *buf, unsigned int size)
 {
 	unsigned char name[11];
 	unsigned int i;
-	int byte_off;
 	unsigned int first_cluster = 0, prev = 0, cluster;
 	unsigned int remaining = size;
-	const unsigned char *src = (const unsigned char *)buf;
+	const unsigned char *src = buf;
+	unsigned int lba = 0, offset = 0, old_cluster = 0;
+	unsigned char old_attr = 0;
+	int r;
 
 	if (!fat_ready || !name_8_3)
 		return -1;
@@ -453,27 +633,27 @@ int fat_write_file(const char *name_8_3, const void *buf, unsigned int size)
 		name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
 			? name_8_3[i] - 32 : name_8_3[i]);
 
-	byte_off = fat_find_root_entry_off(name);
-	if (byte_off == -2)
+	r = fat_lookup(dir_cluster, name, &old_cluster, 0, &old_attr, &lba, &offset);
+	if (r == -2)
 		return -2;
-	if (byte_off >= 0) {
-		/* Overwrite: free the existing chain first. */
-		unsigned int old_cluster = 0;
-		if (fat_dir_entry_read((unsigned int)byte_off, &old_cluster, 0) != 0)
-			return -2;
-		if (old_cluster >= 2)
-			fat_free_chain(old_cluster);
-	} else {
-		byte_off = fat_find_free_root_entry();
-		if (byte_off == -2)
+	if (r == 0 && (old_attr & 0x10)) {
+		io_putstr("write: is a directory\n");
+		return -1;
+	}
+	if (r != 0) {
+		/* not found: find a free entry */
+		r = dir_find_free(dir_cluster, &lba, &offset);
+		if (r == -2)
 			return -2;
-		if (byte_off < 0) {
-			io_putstr("write: root directory full\n");
+		if (r != 0) {
+			io_putstr("write: directory full\n");
 			return -1;
 		}
+	} else if (old_cluster >= 2) {
+		/* overwrite: free the old chain */
+		fat_free_chain(old_cluster);
 	}
 
-	/* Allocate clusters, link the chain, and write the data. */
 	if (remaining > 0) {
 		unsigned int cluster_size = fat_sec_per_cluster * SECTOR_SIZE;
 
@@ -495,15 +675,15 @@ int fat_write_file(const char *name_8_3, const void *buf, unsigned int size)
 
 			{
 				unsigned int chunk = (remaining < cluster_size) ? remaining : cluster_size;
-				unsigned int lba = fat_data_start + (cluster - 2) * fat_sec_per_cluster;
+				unsigned int lba2 = fat_data_start + (cluster - 2) * fat_sec_per_cluster;
 				unsigned int full_secs = chunk / SECTOR_SIZE;
 				unsigned int rem_bytes = chunk % SECTOR_SIZE;
 
 				if (full_secs > 0) {
-					if (disk_write_sectors(lba, src, full_secs) != 0)
+					if (disk_write_sectors(lba2, src, full_secs) != 0)
 						return -2;
 					src += full_secs * SECTOR_SIZE;
-					lba += full_secs;
+					lba2 += full_secs;
 				}
 				if (rem_bytes > 0) {
 					unsigned int j;
@@ -511,7 +691,7 @@ int fat_write_file(const char *name_8_3, const void *buf, unsigned int size)
 						sector_buf[j] = src[j];
 					for (j = rem_bytes; j < SECTOR_SIZE; j++)
 						sector_buf[j] = 0;
-					if (disk_write_sectors(lba, sector_buf, 1) != 0)
+					if (disk_write_sectors(lba2, sector_buf, 1) != 0)
 						return -2;
 					src += rem_bytes;
 				}
@@ -523,17 +703,19 @@ int fat_write_file(const char *name_8_3, const void *buf, unsigned int size)
 			return -2;
 	}
 
-	if (fat_dir_entry_write((unsigned int)byte_off, name, 0x20, first_cluster, size) != 0)
+	if (fat_dir_entry_write(lba, offset, name, 0x20, first_cluster, size) != 0)
 		return -2;
 
 	return 0;
 }
 
-int fat_delete_file(const char *name_8_3)
+int fat_delete_file_in(unsigned int dir_cluster, const char *name_8_3)
 {
 	unsigned char name[11];
 	unsigned int i, cluster = 0;
-	int byte_off;
+	unsigned char attr = 0;
+	unsigned int lba, offset;
+	int r;
 
 	if (!fat_ready || !name_8_3)
 		return -1;
@@ -542,26 +724,241 @@ int fat_delete_file(const char *name_8_3)
 		name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
 			? name_8_3[i] - 32 : name_8_3[i]);
 
-	byte_off = fat_find_root_entry_off(name);
-	if (byte_off == -2)
+	r = fat_lookup(dir_cluster, name, &cluster, 0, &attr, &lba, &offset);
+	if (r == -2)
 		return -2;
-	if (byte_off < 0)
+	if (r != 0)
 		return -1;
+	if (attr & 0x10)
+		return -1;  /* it's a directory; use rmdir */
 
-	if (fat_dir_entry_read((unsigned int)byte_off, &cluster, 0) != 0)
-		return -2;
 	if (cluster >= 2)
 		fat_free_chain(cluster);
+	return fat_mark_deleted(lba, offset);
+}
+
+static int dir_empty_cb(unsigned int lba, unsigned int offset, void *vctx)
+{
+	int *nonempty = vctx;
+	unsigned char *e = sector_buf + offset;
+	(void)lba;
+	if (e[0] == 0x00 || e[0] == 0xE5)
+		return 0;
+	if (e[0] == '.')
+		return 0;  /* "." or ".." */
+	*nonempty = 1;
+	return 1;
+}
+
+int fat_mkdir(unsigned int parent_cluster, const char *name_8_3)
+{
+	unsigned char name[11];
+	unsigned int i, cluster;
+	unsigned int lba, offset;
+	int r;
+
+	if (!fat_ready || !name_8_3)
+		return -1;
+
+	for (i = 0; i < 11; i++)
+		name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
+			? name_8_3[i] - 32 : name_8_3[i]);
+
+	r = fat_lookup(parent_cluster, name, 0, 0, 0, 0, 0);
+	if (r == -2)
+		return -2;
+	if (r == 0) {
+		io_putstr("mkdir: already exists\n");
+		return -1;
+	}
+
+	r = fat_find_free_cluster();
+	if (r == -2)
+		return -2;
+	if (r < 0) {
+		io_putstr("mkdir: disk full\n");
+		return -1;
+	}
+	cluster = (unsigned int)r;
+
+	/* Mark the cluster allocated (end of chain) so it's not reused. */
+	if (fat_entry_write(cluster, 0xFFF) != 0)
+		return -2;
 
+	/* Write "." and ".." entries into the cluster's first sector. */
 	{
-		unsigned int lba = (unsigned int)byte_off / SECTOR_SIZE;
-		unsigned int within = (unsigned int)byte_off % SECTOR_SIZE;
-		if (fat_read_sector(lba) != 0)
-			return -2;
-		sector_buf[within] = 0xE5;
-		if (disk_write_sectors(lba, sector_buf, 1) != 0)
+		unsigned int base = fat_data_start + (cluster - 2) * fat_sec_per_cluster;
+		for (i = 0; i < SECTOR_SIZE; i++)
+			sector_buf[i] = 0;
+		sector_buf[0] = '.';
+		for (i = 1; i < 11; i++)
+			sector_buf[i] = ' ';
+		sector_buf[11] = 0x10;
+		sector_buf[26] = (unsigned char)(cluster & 0xFF);
+		sector_buf[27] = (unsigned char)((cluster >> 8) & 0xFF);
+		sector_buf[32] = '.';
+		sector_buf[33] = '.';
+		for (i = 34; i < 43; i++)
+			sector_buf[i] = ' ';
+		sector_buf[43] = 0x10;
+		sector_buf[58] = (unsigned char)(parent_cluster & 0xFF);
+		sector_buf[59] = (unsigned char)((parent_cluster >> 8) & 0xFF);
+		if (disk_write_sectors(base, sector_buf, 1) != 0)
 			return -2;
 	}
 
+	r = dir_find_free(parent_cluster, &lba, &offset);
+	if (r == -2)
+		return -2;
+	if (r != 0) {
+		fat_free_chain(cluster);
+		io_putstr("mkdir: directory full\n");
+		return -1;
+	}
+	return fat_dir_entry_write(lba, offset, name, 0x10, cluster, 0);
+}
+
+int fat_rmdir(unsigned int parent_cluster, const char *name_8_3)
+{
+	unsigned char name[11];
+	unsigned int i, cluster = 0;
+	unsigned char attr = 0;
+	unsigned int lba, offset;
+	int r, nonempty = 0;
+
+	if (!fat_ready || !name_8_3)
+		return -1;
+
+	for (i = 0; i < 11; i++)
+		name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
+			? name_8_3[i] - 32 : name_8_3[i]);
+
+	r = fat_lookup(parent_cluster, name, &cluster, 0, &attr, &lba, &offset);
+	if (r == -2)
+		return -2;
+	if (r != 0)
+		return -1;
+	if (!(attr & 0x10)) {
+		io_putstr("rmdir: not a directory\n");
+		return -1;
+	}
+
+	if (dir_each_entry(cluster, dir_empty_cb, &nonempty) != 0)
+		return -2;
+	if (nonempty) {
+		io_putstr("rmdir: directory not empty\n");
+		return -1;
+	}
+
+	if (cluster >= 2)
+		fat_free_chain(cluster);
+	return fat_mark_deleted(lba, offset);
+}
+
+/* Resolve a path "a/b/c" into (parent dir cluster, final 8.3 name). */
+int fat_resolve_path(const char *path, unsigned int *dir_cluster, char name_8_3[12])
+{
+	unsigned int dir = 0;
+	const char *p = path;
+
+	if (!fat_ready || !path || !dir_cluster || !name_8_3)
+		return -1;
+
+	for (;;) {
+		const char *start;
+		unsigned int len;
+
+		while (*p == '/')
+			p++;
+		if (!*p)
+			return -1;  /* empty path */
+
+		start = p;
+		len = 0;
+		while (p[len] && p[len] != '/')
+			len++;
+
+		if (p[len] == '/') {
+			/* intermediate component: must be an existing directory */
+			unsigned char name[11];
+			unsigned int cluster, size;
+			unsigned char attr;
+			int r;
+			fat_name_to_8_3(start, len, name);
+			r = fat_lookup(dir, name, &cluster, &size, &attr, 0, 0);
+			if (r == -2)
+				return -2;
+			if (r != 0 || !(attr & 0x10))
+				return -1;
+			dir = cluster;
+			p += len + 1;
+		} else {
+			fat_name_to_8_3(start, len, (unsigned char *)name_8_3);
+			name_8_3[11] = '\0';
+			*dir_cluster = dir;
+			return 0;
+		}
+	}
+}
+
+/* Resolve a full path (all components must be directories) to a directory
+ * cluster. An empty path resolves to the root (cluster 0). */
+int fat_resolve_dir(const char *path, unsigned int *dir_cluster)
+{
+	unsigned int dir = 0;
+	const char *p = path;
+
+	if (!fat_ready || !path || !dir_cluster)
+		return -1;
+
+	if (!*p) {
+		*dir_cluster = 0;
+		return 0;
+	}
+
+	for (;;) {
+		const char *start;
+		unsigned int len;
+
+		while (*p == '/')
+			p++;
+		if (!*p)
+			break;  /* trailing slash */
+
+		start = p;
+		len = 0;
+		while (p[len] && p[len] != '/')
+			len++;
+
+		{
+			unsigned char name[11];
+			unsigned int cluster, size;
+			unsigned char attr;
+			int r;
+			fat_name_to_8_3(start, len, name);
+			r = fat_lookup(dir, name, &cluster, &size, &attr, 0, 0);
+			if (r == -2)
+				return -2;
+			if (r != 0 || !(attr & 0x10))
+				return -1;
+			dir = cluster;
+		}
+		if (!p[len])
+			break;
+		p += len + 1;
+	}
+
+	*dir_cluster = dir;
 	return 0;
 }
+
+/* Root-directory wrappers (dir_cluster = 0). */
+int fat_write_file(const char *name_8_3, const void *buf, unsigned int size)
+{
+	return fat_write_file_in(0, name_8_3, buf, size);
+}
+
+int fat_delete_file(const char *name_8_3)
+{
+	return fat_delete_file_in(0, name_8_3);
+}

+ 32 - 8
fat.h

@@ -1,4 +1,5 @@
-/* FAT12 filesystem. Root directory only.
+/* FAT12 filesystem with subdirectory support.
+ * A "directory" is identified by its first data cluster; 0 means the root dir.
  * Error conventions:  0 = success, -1 = not found / not ready / full, -2 = disk I/O error. */
 
 #ifndef FAT_H
@@ -10,19 +11,42 @@ typedef void (*fat_root_callback_t)(const char *name, int is_dir, unsigned int s
  * Returns 0 on success, -1 if no valid FAT filesystem, -2 on disk I/O error. */
 int fat_init(void);
 
-/* Iterate root directory, call callback for each file/dir.
- * Returns 0 on success, -1 if not ready, -2 on disk I/O error. */
+/* Iterate root directory, call callback for each file/dir. */
 int fat_list_root(fat_root_callback_t callback);
 
-/* Read file by 8.3 name into buf, max max_len bytes.
- * Returns bytes read on success, -1 if not found, -2 on disk I/O error. */
+/* Iterate a directory (0 = root, else first cluster). */
+int fat_list_dir(unsigned int dir_cluster, fat_root_callback_t callback);
+
+/* Read file by 8.3 name into buf, max max_len bytes (root only). */
 int fat_read_file(const char *name_8_3, void *buf, unsigned int max_len);
 
-/* Create or overwrite a root file with `size` bytes from `buf`.
- * Returns 0 on success, -1 if not ready / disk full, -2 on disk I/O error. */
+/* Read a file in a directory (0 = root). */
+int fat_read_file_in(unsigned int dir_cluster, const char *name_8_3, void *buf, unsigned int max_len);
+
+/* Create or overwrite a file in the root directory. */
 int fat_write_file(const char *name_8_3, const void *buf, unsigned int size);
 
-/* Delete a root file. Returns 0, -1 if not found, -2 on disk I/O error. */
+/* Create or overwrite a file in a directory (0 = root). */
+int fat_write_file_in(unsigned int dir_cluster, const char *name_8_3, const void *buf, unsigned int size);
+
+/* Delete a file from the root directory. */
 int fat_delete_file(const char *name_8_3);
 
+/* Delete a file from a directory (0 = root). */
+int fat_delete_file_in(unsigned int dir_cluster, const char *name_8_3);
+
+/* Create a subdirectory in `parent_cluster` (0 = root). */
+int fat_mkdir(unsigned int parent_cluster, const char *name_8_3);
+
+/* Remove an empty subdirectory from `parent_cluster` (0 = root). */
+int fat_rmdir(unsigned int parent_cluster, const char *name_8_3);
+
+/* Resolve a path "a/b/c" into (parent dir cluster, final 8.3 name).
+ * The returned name_8_3 is uppercase and space-padded (11 bytes + NUL). */
+int fat_resolve_path(const char *path, unsigned int *dir_cluster, char name_8_3[12]);
+
+/* Resolve a full path (all components directories) to a directory cluster.
+ * An empty path resolves to the root (cluster 0). */
+int fat_resolve_dir(const char *path, unsigned int *dir_cluster);
+
 #endif

+ 21 - 9
ls.c

@@ -1,4 +1,4 @@
-/* ls command: list root directory of FAT12 floppy. */
+/* ls command: list a directory (root by default, or a path). */
 
 #include "fat.h"
 #include "io.h"
@@ -11,13 +11,15 @@ static void list_cb(const char *name, int is_dir, unsigned int size)
 	char buf[BUF_SIZE];
 	unsigned int n;
 
-	(void)is_dir;
 	for (i = 0; i < 11 && name[i]; i++)
 		io_putchar(name[i] == ' ' ? ' ' : name[i]);
 	if (i < 11)
 		io_putchar(' ');
+	if (is_dir) {
+		io_putstr("  <DIR>\n");
+		return;
+	}
 	io_putstr(" ");
-	/* Simple decimal print for size */
 	n = size;
 	if (n == 0) {
 		io_putchar('0');
@@ -34,8 +36,9 @@ static void list_cb(const char *name, int is_dir, unsigned int size)
 	io_putstr("\n");
 }
 
-void program_ls_main(void)
+void program_ls_main(const char *path)
 {
+	unsigned int dir_cluster = 0;
 	int r;
 
 	r = fat_init();
@@ -47,13 +50,22 @@ void program_ls_main(void)
 		io_putstr("ls: no FAT filesystem found\n");
 		return;
 	}
-	r = fat_list_root(list_cb);
+
+	if (path && path[0]) {
+		r = fat_resolve_dir(path, &dir_cluster);
+		if (r == -2) {
+			io_putstr("ls: disk error\n");
+			return;
+		}
+		if (r != 0) {
+			io_putstr("ls: not a directory\n");
+			return;
+		}
+	}
+
+	r = fat_list_dir(dir_cluster, list_cb);
 	if (r == -2) {
 		io_putstr("ls: disk read error\n");
 		return;
 	}
-	if (r != 0) {
-		io_putstr("ls: list failed\n");
-		return;
-	}
 }

+ 29 - 0
mkdir.c

@@ -0,0 +1,29 @@
+/* mkdir command: create a subdirectory. Usage: mkdir <path>. */
+
+#include "fat.h"
+#include "io.h"
+
+void program_mkdir_main(const char *arg)
+{
+	unsigned int dir_cluster;
+	char name_8_3[12];
+	int r;
+
+	if (!arg || !arg[0]) {
+		io_putstr("mkdir: usage mkdir <dir>\n");
+		return;
+	}
+
+	r = fat_init();
+	if (r == -2) { io_putstr("mkdir: disk read error\n"); return; }
+	if (r != 0) { io_putstr("mkdir: no FAT filesystem found\n"); return; }
+
+	r = fat_resolve_path(arg, &dir_cluster, name_8_3);
+	if (r == -2) { io_putstr("mkdir: disk error\n"); return; }
+	if (r != 0) { io_putstr("mkdir: invalid path\n"); return; }
+
+	r = fat_mkdir(dir_cluster, name_8_3);
+	if (r == -2) { io_putstr("mkdir: disk error\n"); return; }
+	if (r != 0) return;  /* fat_mkdir already printed */
+	io_putstr("mkdir ok\n");
+}

+ 13 - 59
rm.c

@@ -1,75 +1,29 @@
-/* rm command: delete a file from the FAT12 floppy. */
+/* rm command: delete a file. Usage: rm <path>. */
 
 #include "fat.h"
 #include "io.h"
 
-#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] != '\0' && 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] != '\0' && 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_rm_main(const char *arg)
 {
-	char name_8_3[NAME_BUF_SIZE];
-	unsigned int i, len;
+	unsigned int dir_cluster;
+	char name_8_3[12];
 	int r;
 
 	if (!arg || !arg[0]) {
 		io_putstr("rm: usage rm <file>\n");
 		return;
 	}
-	i = 0;
-	while (arg[i] == ' ')
-		i++;
-	len = 0;
-	while (arg[i + len])
-		len++;
-	if (len == 0) {
-		io_putstr("rm: usage rm <file>\n");
-		return;
-	}
-	to_8_3(arg + i, len, name_8_3);
 
 	r = fat_init();
-	if (r == -2) {
-		io_putstr("rm: disk read error\n");
-		return;
-	}
-	if (r != 0) {
-		io_putstr("rm: no FAT filesystem found\n");
-		return;
-	}
-	r = fat_delete_file(name_8_3);
-	if (r == -2) {
-		io_putstr("rm: disk error\n");
-		return;
-	}
-	if (r != 0) {
-		io_putstr("rm: file not found\n");
-		return;
-	}
+	if (r == -2) { io_putstr("rm: disk read error\n"); return; }
+	if (r != 0) { io_putstr("rm: no FAT filesystem found\n"); return; }
+
+	r = fat_resolve_path(arg, &dir_cluster, name_8_3);
+	if (r == -2) { io_putstr("rm: disk error\n"); return; }
+	if (r != 0) { io_putstr("rm: not found\n"); return; }
+
+	r = fat_delete_file_in(dir_cluster, name_8_3);
+	if (r == -2) { io_putstr("rm: disk error\n"); return; }
+	if (r != 0) { io_putstr("rm: file not found\n"); return; }
 	io_putstr("removed\n");
 }

+ 29 - 0
rmdir.c

@@ -0,0 +1,29 @@
+/* rmdir command: remove an empty subdirectory. Usage: rmdir <path>. */
+
+#include "fat.h"
+#include "io.h"
+
+void program_rmdir_main(const char *arg)
+{
+	unsigned int dir_cluster;
+	char name_8_3[12];
+	int r;
+
+	if (!arg || !arg[0]) {
+		io_putstr("rmdir: usage rmdir <dir>\n");
+		return;
+	}
+
+	r = fat_init();
+	if (r == -2) { io_putstr("rmdir: disk read error\n"); return; }
+	if (r != 0) { io_putstr("rmdir: no FAT filesystem found\n"); return; }
+
+	r = fat_resolve_path(arg, &dir_cluster, name_8_3);
+	if (r == -2) { io_putstr("rmdir: disk error\n"); return; }
+	if (r != 0) { io_putstr("rmdir: invalid path\n"); return; }
+
+	r = fat_rmdir(dir_cluster, name_8_3);
+	if (r == -2) { io_putstr("rmdir: disk error\n"); return; }
+	if (r != 0) return;  /* fat_rmdir already printed */
+	io_putstr("rmdir ok\n");
+}

+ 23 - 52
save.c

@@ -1,42 +1,15 @@
-/* save command: write a text string to a file on the FAT12 floppy.
- * Usage: save <file> <text...>  (text may contain spaces). */
+/* save command: write a text string to a file. Usage: save <file> <text...>. */
 
 #include "fat.h"
 #include "io.h"
 
-#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] != '\0' && 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] != '\0' && 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_save_main(const char *arg)
 {
-	char name_8_3[NAME_BUF_SIZE];
-	const char *text;
+	char fn[64];
 	unsigned int i, fn_len, text_len;
+	unsigned int dir_cluster;
+	char name_8_3[12];
+	const char *text;
 	int r;
 
 	if (!arg || !arg[0]) {
@@ -49,15 +22,20 @@ void program_save_main(const char *arg)
 	while (arg[i] == ' ')
 		i++;
 	fn_len = 0;
-	while (arg[i + fn_len] && arg[i + fn_len] != ' ')
+	while (arg[i + fn_len] && arg[i + fn_len] != ' ' && fn_len < sizeof(fn) - 1)
 		fn_len++;
+	{
+		unsigned int j;
+		for (j = 0; j < fn_len; j++)
+			fn[j] = arg[i + j];
+	}
+	fn[fn_len] = '\0';
 	if (fn_len == 0) {
 		io_putstr("save: usage save <file> <text>\n");
 		return;
 	}
-	to_8_3(arg + i, fn_len, name_8_3);
 
-	/* text = the rest (trimmed) */
+	/* text = the rest */
 	i += fn_len;
 	while (arg[i] == ' ')
 		i++;
@@ -67,22 +45,15 @@ void program_save_main(const char *arg)
 		text_len++;
 
 	r = fat_init();
-	if (r == -2) {
-		io_putstr("save: disk read error\n");
-		return;
-	}
-	if (r != 0) {
-		io_putstr("save: no FAT filesystem found\n");
-		return;
-	}
-	r = fat_write_file(name_8_3, text, text_len);
-	if (r == -2) {
-		io_putstr("save: disk write error\n");
-		return;
-	}
-	if (r != 0) {
-		io_putstr("save: write failed\n");
-		return;
-	}
+	if (r == -2) { io_putstr("save: disk read error\n"); return; }
+	if (r != 0) { io_putstr("save: no FAT filesystem found\n"); return; }
+
+	r = fat_resolve_path(fn, &dir_cluster, name_8_3);
+	if (r == -2) { io_putstr("save: disk error\n"); return; }
+	if (r != 0) { io_putstr("save: invalid path\n"); return; }
+
+	r = fat_write_file_in(dir_cluster, name_8_3, text, text_len);
+	if (r == -2) { io_putstr("save: disk write error\n"); return; }
+	if (r != 0) { io_putstr("save: write failed\n"); return; }
 	io_putstr("saved\n");
 }

+ 67 - 6
terminal.c

@@ -17,11 +17,13 @@
 extern void program_hello_main(void);
 extern void program_reboot_main(void);
 extern void program_poweroff_main(void);
-extern void program_ls_main(void);
+extern void program_ls_main(const char *path);
 extern void program_cat_main(const char *filename);
 extern void program_run_main(const char *filename);
 extern void program_save_main(const char *arg);
 extern void program_rm_main(const char *arg);
+extern void program_mkdir_main(const char *arg);
+extern void program_rmdir_main(const char *arg);
 extern void program_clock_main(void);
 extern void program_colors_main(void);
 extern void program_display_main(void);
@@ -142,9 +144,9 @@ static int is_cmd_poweroff(const char *line, unsigned int line_len)
 	return (i == line_len);
 }
 
-static int is_cmd_ls(const char *line, unsigned int line_len)
+static int is_cmd_ls(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
 {
-	unsigned int i = 0;
+	unsigned int i = 0, j = 0;
 	while (i < line_len && line[i] == ' ')
 		i++;
 	if (i + 2 > line_len)
@@ -152,6 +154,16 @@ static int is_cmd_ls(const char *line, unsigned int line_len)
 	if (line[i] != 'l' || line[i+1] != 's')
 		return 0;
 	i += 2;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i >= line_len) {
+		if (buf_size > 0)
+			path_buf[0] = '\0';
+		return 1;
+	}
+	while (i < line_len && j + 1 < buf_size)
+		path_buf[j++] = line[i++];
+	path_buf[j] = '\0';
 	while (i < line_len && line[i] == ' ')
 		i++;
 	return (i == line_len);
@@ -299,6 +311,7 @@ static int is_cmd_run(const char *line, unsigned int line_len, char *filename_bu
 
 #define SAVE_ARGS_MAX 128
 #define RM_FNAME_MAX 64
+#define LS_PATH_MAX 64
 
 static int is_cmd_save(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
 {
@@ -344,6 +357,46 @@ static int is_cmd_rm(const char *line, unsigned int line_len, char *filename_buf
 	return (j > 0);
 }
 
+static int is_cmd_mkdir(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
+{
+	unsigned int i = 0, j = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 5 > line_len)
+		return 0;
+	if (line[i] != 'm' || line[i+1] != 'k' || line[i+2] != 'd' || line[i+3] != 'i' || line[i+4] != 'r')
+		return 0;
+	i += 5;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i >= line_len)
+		return 0;
+	while (i < line_len && line[i] != ' ' && j < buf_size - 1)
+		path_buf[j++] = line[i++];
+	path_buf[j] = '\0';
+	return (j > 0);
+}
+
+static int is_cmd_rmdir(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
+{
+	unsigned int i = 0, j = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 5 > line_len)
+		return 0;
+	if (line[i] != 'r' || line[i+1] != 'm' || line[i+2] != 'd' || line[i+3] != 'i' || line[i+4] != 'r')
+		return 0;
+	i += 5;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i >= line_len)
+		return 0;
+	while (i < line_len && line[i] != ' ' && j < buf_size - 1)
+		path_buf[j++] = line[i++];
+	path_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;
@@ -677,6 +730,7 @@ void terminal_run(void)
 					static char power_args[POWER_ARGS_MAX];
 					static char save_args[SAVE_ARGS_MAX];
 					static char rm_fname[RM_FNAME_MAX];
+					static char ls_path[LS_PATH_MAX];
 					unsigned int drive_sel, kill_pid;
 					int skip_prompt = 0;
 					io_putchar('\n');
@@ -689,8 +743,8 @@ void terminal_run(void)
 						program_reboot_main();
 					} else if (is_cmd_poweroff(line, line_len)) {
 						program_poweroff_main();
-					} else if (is_cmd_ls(line, line_len)) {
-						if (process_create("ls", program_ls_main) < 0)
+					} else if (is_cmd_ls(line, line_len, ls_path, LS_PATH_MAX)) {
+						if (process_create_arg("ls", program_ls_main, ls_path) < 0)
 							io_putstr("error: too many processes\n");
 					} else if (is_cmd_clock(line, line_len)) {
 						if (process_create("clock", program_clock_main) < 0)
@@ -715,6 +769,12 @@ void terminal_run(void)
 					} else if (is_cmd_save(line, line_len, save_args, SAVE_ARGS_MAX)) {
 						if (process_create_arg("save", program_save_main, save_args) < 0)
 							io_putstr("error: too many processes\n");
+					} else if (is_cmd_mkdir(line, line_len, rm_fname, RM_FNAME_MAX)) {
+						if (process_create_arg("mkdir", program_mkdir_main, rm_fname) < 0)
+							io_putstr("error: too many processes\n");
+					} else if (is_cmd_rmdir(line, line_len, rm_fname, RM_FNAME_MAX)) {
+						if (process_create_arg("rmdir", program_rmdir_main, rm_fname) < 0)
+							io_putstr("error: too many processes\n");
 					} else if (is_cmd_rm(line, line_len, rm_fname, RM_FNAME_MAX)) {
 						if (process_create_arg("rm", program_rm_main, rm_fname) < 0)
 							io_putstr("error: too many processes\n");
@@ -759,8 +819,9 @@ void terminal_run(void)
 						program_dbg_main();
 					} else if (is_cmd_help(line, line_len)) {
 						io_putstr("hello     reboot    poweroff\n");
-						io_putstr("ls        cat <file>\n");
+						io_putstr("ls [dir]  cat <file>\n");
 						io_putstr("save <f> <text>  rm <file>\n");
+						io_putstr("mkdir <dir>  rmdir <dir>\n");
 						io_putstr("run <file>  space\n");
 						io_putstr("clock     colors\n");
 						io_putstr("display   cpuinfo\n");