Răsfoiți Sursa

Add FAT12 write/delete + save/rm commands

- fat_write_file / fat_delete_file: free-cluster alloc, 12-bit FAT chain
  read-modify-write (incl. sector-boundary case), dir-entry write
- save <file> <text> and rm <file> commands
Johannes Zellner 1 lună în urmă
părinte
comite
7e5e8d4d96
6 a modificat fișierele cu 580 adăugiri și 10 ștergeri
  1. 8 2
      Makefile
  2. 341 5
      fat.c
  3. 10 3
      fat.h
  4. 75 0
      rm.c
  5. 88 0
      save.c
  6. 58 0
      terminal.c

+ 8 - 2
Makefile

@@ -122,6 +122,12 @@ kexec_tramp.o: kexec_tramp.S
 dbg.o: dbg.c dbg.h serial.h
 	$(CC) $(CFLAGS) -o $@ $<
 
+save.o: save.c fat.h io.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+rm.o: rm.c fat.h io.h
+	$(CC) $(CFLAGS) -o $@ $<
+
 boot.elf: boot.o
 	$(LD) $(BOOT_LDFLAGS) -o $@ $<
 
@@ -137,7 +143,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
+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
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -286,4 +292,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 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 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

+ 341 - 5
fat.c

@@ -1,4 +1,4 @@
-/* FAT12 read-only: BPB parse, root list, read file by following FAT chain. */
+/* FAT12: BPB parse, root list, read/write files by following FAT chain. */
 
 #include "disk.h"
 #include "fat.h"
@@ -15,6 +15,8 @@
 #define BPB_NUM_FATS        0x10
 #define BPB_ROOT_ENTRIES    0x11
 #define BPB_SEC_PER_FAT     0x16
+#define BPB_TOTAL_SEC16     0x13   /* total sectors (16-bit) */
+#define BPB_TOTAL_SEC32     0x20   /* total sectors (32-bit) */
 #define BPB_SIGNATURE       0x1FE   /* 0x55 0xAA at end of boot sector */
 
 static unsigned int fat_reserved;
@@ -22,6 +24,10 @@ static unsigned int fat_root_start;
 static unsigned int fat_root_sectors;
 static unsigned int fat_data_start;
 static unsigned int fat_sec_per_cluster;
+static unsigned int fat_num_fats;
+static unsigned int fat_sec_per_fat;
+static unsigned int fat_total_sectors;
+static unsigned int fat_max_cluster;  /* 2 + number of data clusters */
 static int fat_ready;
 
 static unsigned char sector_buf[SECTOR_SIZE];
@@ -48,7 +54,7 @@ static unsigned int fat_get_entry(unsigned int cluster_idx)
 
 int fat_init(void)
 {
-	unsigned int num_fats, root_entries, sec_per_fat;
+	unsigned int root_entries;
 
 	if (fat_read_sector(0) != 0) {
 		io_putstr("run: disk read error (boot sector LBA=0)\n");
@@ -57,14 +63,27 @@ int fat_init(void)
 	if (sector_buf[BPB_SIGNATURE] != 0x55 || sector_buf[BPB_SIGNATURE + 1] != 0xAA)
 		return -1;  /* no valid FAT filesystem */
 	fat_reserved = (unsigned int)sector_buf[BPB_RESERVED] | ((unsigned int)sector_buf[BPB_RESERVED + 1] << 8);
-	num_fats = sector_buf[BPB_NUM_FATS];
+	fat_num_fats = sector_buf[BPB_NUM_FATS];
 	root_entries = (unsigned int)sector_buf[BPB_ROOT_ENTRIES] | ((unsigned int)sector_buf[BPB_ROOT_ENTRIES + 1] << 8);
-	sec_per_fat = (unsigned int)sector_buf[BPB_SEC_PER_FAT] | ((unsigned int)sector_buf[BPB_SEC_PER_FAT + 1] << 8);
+	fat_sec_per_fat = (unsigned int)sector_buf[BPB_SEC_PER_FAT] | ((unsigned int)sector_buf[BPB_SEC_PER_FAT + 1] << 8);
 	fat_sec_per_cluster = sector_buf[BPB_SEC_PER_CLUSTER];
 
-	fat_root_start = fat_reserved + num_fats * sec_per_fat;
+	fat_total_sectors = (unsigned int)sector_buf[BPB_TOTAL_SEC16] | ((unsigned int)sector_buf[BPB_TOTAL_SEC16 + 1] << 8);
+	if (fat_total_sectors == 0)
+		fat_total_sectors = (unsigned int)sector_buf[BPB_TOTAL_SEC32]
+			| ((unsigned int)sector_buf[BPB_TOTAL_SEC32 + 1] << 8)
+			| ((unsigned int)sector_buf[BPB_TOTAL_SEC32 + 2] << 16)
+			| ((unsigned int)sector_buf[BPB_TOTAL_SEC32 + 3] << 24);
+
+	fat_root_start = fat_reserved + fat_num_fats * fat_sec_per_fat;
 	fat_root_sectors = (root_entries * ROOT_ENTRY_SIZE + SECTOR_SIZE - 1) / SECTOR_SIZE;
 	fat_data_start = fat_root_start + fat_root_sectors;
+
+	if (fat_total_sectors > fat_data_start && fat_sec_per_cluster > 0)
+		fat_max_cluster = 2 + (fat_total_sectors - fat_data_start) / fat_sec_per_cluster;
+	else
+		fat_max_cluster = 2;
+
 	fat_ready = 1;
 	return 0;
 }
@@ -229,3 +248,320 @@ int fat_read_file(const char *name_8_3, void *buf, unsigned int max_len)
 	}
 	return (int)copied;
 }
+
+/* ---- write support ---- */
+
+/* Read a 12-bit FAT entry for `cluster`, handling the case where the two
+ * bytes straddle a FAT sector boundary. Returns 0, or -2 on disk error. */
+static int fat_entry_read(unsigned int cluster, unsigned int *value)
+{
+	unsigned int byte_off = (cluster * 3) / 2;
+	unsigned int sec = fat_reserved + byte_off / SECTOR_SIZE;
+	unsigned int within = byte_off % SECTOR_SIZE;
+	unsigned char b0, b1;
+
+	if (within == SECTOR_SIZE - 1) {
+		if (fat_read_sector(sec) != 0)
+			return -2;
+		b0 = sector_buf[within];
+		if (fat_read_sector(sec + 1) != 0)
+			return -2;
+		b1 = sector_buf[0];
+	} else {
+		if (fat_read_sector(sec) != 0)
+			return -2;
+		b0 = sector_buf[within];
+		b1 = sector_buf[within + 1];
+	}
+
+	if (cluster & 1)
+		*value = ((unsigned int)b0 >> 4) | ((unsigned int)b1 << 4);
+	else
+		*value = (unsigned int)b0 | (((unsigned int)b1 & 0x0F) << 8);
+	return 0;
+}
+
+/* Write a 12-bit FAT entry for `cluster` (read-modify-write), handling the
+ * sector-boundary case. Returns 0, or -2 on disk error. */
+static int fat_entry_write(unsigned int cluster, unsigned int value)
+{
+	unsigned int byte_off = (cluster * 3) / 2;
+	unsigned int sec = fat_reserved + byte_off / SECTOR_SIZE;
+	unsigned int within = byte_off % SECTOR_SIZE;
+
+	value &= 0xFFF;
+
+	if (within == SECTOR_SIZE - 1) {
+		/* Entry straddles into the next FAT sector. */
+		if (cluster & 1) {
+			if (fat_read_sector(sec) != 0) return -2;
+			sector_buf[within] = (sector_buf[within] & 0x0F) | (unsigned char)((value << 4) & 0xF0);
+			if (disk_write_sectors(sec, sector_buf, 1) != 0) return -2;
+			if (fat_read_sector(sec + 1) != 0) return -2;
+			sector_buf[0] = (unsigned char)((value >> 4) & 0xFF);
+			if (disk_write_sectors(sec + 1, sector_buf, 1) != 0) return -2;
+		} else {
+			if (fat_read_sector(sec) != 0) return -2;
+			sector_buf[within] = (unsigned char)(value & 0xFF);
+			if (disk_write_sectors(sec, sector_buf, 1) != 0) return -2;
+			if (fat_read_sector(sec + 1) != 0) return -2;
+			sector_buf[0] = (sector_buf[0] & 0xF0) | (unsigned char)((value >> 8) & 0x0F);
+			if (disk_write_sectors(sec + 1, sector_buf, 1) != 0) return -2;
+		}
+	} else {
+		if (fat_read_sector(sec) != 0) return -2;
+		if (cluster & 1) {
+			sector_buf[within] = (sector_buf[within] & 0x0F) | (unsigned char)((value << 4) & 0xF0);
+			sector_buf[within + 1] = (unsigned char)((value >> 4) & 0xFF);
+		} else {
+			sector_buf[within] = (unsigned char)(value & 0xFF);
+			sector_buf[within + 1] = (sector_buf[within + 1] & 0xF0) | (unsigned char)((value >> 8) & 0x0F);
+		}
+		if (disk_write_sectors(sec, sector_buf, 1) != 0) return -2;
+	}
+	return 0;
+}
+
+/* Find a free data cluster (FAT entry == 0x000). Returns cluster >= 2, -1 if
+ * the disk is full, -2 on disk error. */
+static int fat_find_free_cluster(void)
+{
+	unsigned int cluster;
+
+	for (cluster = 2; cluster < fat_max_cluster; cluster++) {
+		unsigned int val;
+		if (fat_entry_read(cluster, &val) != 0)
+			return -2;
+		if (val == 0x000)
+			return (int)cluster;
+	}
+	return -1;
+}
+
+/* Mark every cluster in a chain as free (0x000). */
+static int fat_free_chain(unsigned int cluster)
+{
+	while (cluster >= 2 && cluster < fat_max_cluster) {
+		unsigned int next;
+		if (fat_entry_read(cluster, &next) != 0)
+			return -2;
+		if (fat_entry_write(cluster, 0x000) != 0)
+			return -2;
+		if (next < 2 || next >= FAT12_EOC_MIN)
+			break;
+		cluster = next;
+	}
+	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)
+{
+	unsigned int lba = byte_off / SECTOR_SIZE;
+	unsigned int within = byte_off % SECTOR_SIZE;
+
+	if (fat_read_sector(lba) != 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);
+	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)
+{
+	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;
+	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);
+
+	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])
+{
+	unsigned int sec, off;
+
+	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;
+}
+
+/* Find a free (0x00 or 0xE5) root entry; return its byte offset, -1, or -2. */
+static int fat_find_free_root_entry(void)
+{
+	unsigned int sec, off;
+
+	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) {
+			if (sector_buf[off] == 0x00 || sector_buf[off] == 0xE5)
+				return (int)((fat_root_start + sec) * SECTOR_SIZE + off);
+		}
+	}
+	return -1;
+}
+
+int fat_write_file(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;
+
+	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]);
+
+	byte_off = fat_find_root_entry_off(name);
+	if (byte_off == -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)
+			return -2;
+		if (byte_off < 0) {
+			io_putstr("write: root directory full\n");
+			return -1;
+		}
+	}
+
+	/* Allocate clusters, link the chain, and write the data. */
+	if (remaining > 0) {
+		unsigned int cluster_size = fat_sec_per_cluster * SECTOR_SIZE;
+
+		while (remaining > 0) {
+			int c = fat_find_free_cluster();
+			if (c == -2)
+				return -2;
+			if (c < 0) {
+				if (first_cluster)
+					fat_free_chain(first_cluster);
+				io_putstr("write: disk full\n");
+				return -1;
+			}
+			cluster = (unsigned int)c;
+			if (first_cluster == 0)
+				first_cluster = cluster;
+			else if (fat_entry_write(prev, cluster) != 0)
+				return -2;
+
+			{
+				unsigned int chunk = (remaining < cluster_size) ? remaining : cluster_size;
+				unsigned int lba = 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)
+						return -2;
+					src += full_secs * SECTOR_SIZE;
+					lba += full_secs;
+				}
+				if (rem_bytes > 0) {
+					unsigned int j;
+					for (j = 0; j < rem_bytes; j++)
+						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)
+						return -2;
+					src += rem_bytes;
+				}
+				remaining -= chunk;
+			}
+			prev = cluster;
+		}
+		if (fat_entry_write(prev, 0xFFF) != 0)
+			return -2;
+	}
+
+	if (fat_dir_entry_write((unsigned int)byte_off, name, 0x20, first_cluster, size) != 0)
+		return -2;
+
+	return 0;
+}
+
+int fat_delete_file(const char *name_8_3)
+{
+	unsigned char name[11];
+	unsigned int i, cluster = 0;
+	int byte_off;
+
+	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]);
+
+	byte_off = fat_find_root_entry_off(name);
+	if (byte_off == -2)
+		return -2;
+	if (byte_off < 0)
+		return -1;
+
+	if (fat_dir_entry_read((unsigned int)byte_off, &cluster, 0) != 0)
+		return -2;
+	if (cluster >= 2)
+		fat_free_chain(cluster);
+
+	{
+		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)
+			return -2;
+	}
+
+	return 0;
+}

+ 10 - 3
fat.h

@@ -1,12 +1,12 @@
-/* FAT12 read-only filesystem. Root directory only.
- * Error conventions:  0 = success, -1 = not found / not ready, -2 = disk I/O error. */
+/* FAT12 filesystem. Root directory only.
+ * Error conventions:  0 = success, -1 = not found / not ready / full, -2 = disk I/O error. */
 
 #ifndef FAT_H
 #define FAT_H
 
 typedef void (*fat_root_callback_t)(const char *name, int is_dir, unsigned int size);
 
-/* Parse BPB from sector 0. Call once before list/read.
+/* Parse BPB from sector 0. Call once before list/read/write.
  * Returns 0 on success, -1 if no valid FAT filesystem, -2 on disk I/O error. */
 int fat_init(void);
 
@@ -18,4 +18,11 @@ int fat_list_root(fat_root_callback_t callback);
  * Returns bytes read on success, -1 if not found, -2 on disk I/O error. */
 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. */
+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. */
+int fat_delete_file(const char *name_8_3);
+
 #endif

+ 75 - 0
rm.c

@@ -0,0 +1,75 @@
+/* rm command: delete a file from the FAT12 floppy. */
+
+#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] != '.' && 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_rm_main(const char *arg)
+{
+	char name_8_3[NAME_BUF_SIZE];
+	unsigned int i, len;
+	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;
+	}
+	io_putstr("removed\n");
+}

+ 88 - 0
save.c

@@ -0,0 +1,88 @@
+/* save command: write a text string to a file on the FAT12 floppy.
+ * Usage: save <file> <text...>  (text may contain spaces). */
+
+#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] != '.' && 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_save_main(const char *arg)
+{
+	char name_8_3[NAME_BUF_SIZE];
+	const char *text;
+	unsigned int i, fn_len, text_len;
+	int r;
+
+	if (!arg || !arg[0]) {
+		io_putstr("save: usage save <file> <text>\n");
+		return;
+	}
+
+	/* filename = first token */
+	i = 0;
+	while (arg[i] == ' ')
+		i++;
+	fn_len = 0;
+	while (arg[i + fn_len] && arg[i + fn_len] != ' ')
+		fn_len++;
+	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) */
+	i += fn_len;
+	while (arg[i] == ' ')
+		i++;
+	text = arg + i;
+	text_len = 0;
+	while (text[text_len])
+		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;
+	}
+	io_putstr("saved\n");
+}

+ 58 - 0
terminal.c

@@ -20,6 +20,8 @@ extern void program_poweroff_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_save_main(const char *arg);
+extern void program_rm_main(const char *arg);
 extern void program_clock_main(void);
 extern void program_colors_main(void);
 extern void program_display_main(void);
@@ -295,6 +297,53 @@ static int is_cmd_run(const char *line, unsigned int line_len, char *filename_bu
 	return (j > 0);
 }
 
+#define SAVE_ARGS_MAX 128
+#define RM_FNAME_MAX 64
+
+static int is_cmd_save(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
+{
+	unsigned int i = 0, j = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 4 > line_len)
+		return 0;
+	if (line[i] != 's' || line[i+1] != 'a' || line[i+2] != 'v' || line[i+3] != 'e')
+		return 0;
+	i += 4;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i >= line_len)
+		return 0;
+	while (i < line_len && j + 1 < buf_size)
+		arg_buf[j++] = line[i++];
+	while (j > 0 && arg_buf[j - 1] == ' ')
+		j--;
+	arg_buf[j] = '\0';
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len && j > 0);
+}
+
+static int is_cmd_rm(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
+{
+	unsigned int i = 0, j = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 2 > line_len)
+		return 0;
+	if (line[i] != 'r' || line[i+1] != 'm')
+		return 0;
+	i += 2;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i >= line_len)
+		return 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;
@@ -626,6 +675,8 @@ void terminal_run(void)
 				} else if (k == '\n') {
 					static char cat_fname[CAT_FNAME_MAX];
 					static char power_args[POWER_ARGS_MAX];
+					static char save_args[SAVE_ARGS_MAX];
+					static char rm_fname[RM_FNAME_MAX];
 					unsigned int drive_sel, kill_pid;
 					int skip_prompt = 0;
 					io_putchar('\n');
@@ -661,6 +712,12 @@ void terminal_run(void)
 							if (process_create_arg("run", program_run_main, cat_fname) < 0)
 								io_putstr("error: too many processes\n");
 						}
+					} 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_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");
 					} else if (is_cmd_space(line, line_len)) {
 						if (process_create_arg("space", program_run_main, "space.bin") < 0)
 							io_putstr("error: too many processes\n");
@@ -703,6 +760,7 @@ void terminal_run(void)
 					} else if (is_cmd_help(line, line_len)) {
 						io_putstr("hello     reboot    poweroff\n");
 						io_putstr("ls        cat <file>\n");
+						io_putstr("save <f> <text>  rm <file>\n");
 						io_putstr("run <file>  space\n");
 						io_putstr("clock     colors\n");
 						io_putstr("display   cpuinfo\n");