Explorar el Código

Add SATA/AHCI disk support and FAT32 writes

The target's MCP79 SATA controller now works in both BIOS modes: the new
AHCI driver finds the disks when SATA mode is AHCI (10DE:0AB8, class
0106:01), and ata.c uses the PCI IDE interface's native BARs in
IDE/compatibility mode (10DE:0AB4), where the legacy ports 0x1F0/0x170
are not decoded.

ahci.c/h (new): polled AHCI driver, no IRQs, no NCQ -- PCI discovery,
port bring-up with COMRESET, IDENTIFY (shared ata_identify_parse() with
ata.c) and READ/WRITE DMA EXT (LBA48) through a page-allocated bounce
buffer so nothing large goes into BSS.  Disks register as sata0..sataN
via blkdev.c and mount like IDE disks (sata0p1 for an MBR partition).

ata.c/h: ata_init_ports() scans for a PCI IDE interface and points the
two channels at its native per-channel I/O BARs when the prog-if says
so, keeping the legacy ports otherwise (QEMU's PIIX path unchanged).
The IDENTIFY parsing moved into ata_identify_parse() and 48-bit sector
counts are clamped to 32 bits for >2 TB disks.

fat.c/h: FAT32 is writable now -- directory entries carry the high half
of the first cluster, mkdir sets it for "." and "..", and allocation
uses an FSInfo-seeded next_free hint so it does not rescan the whole
FAT.  fatformat.c zero-fills the reserved area, both FATs and the root
directory in 128 KB chunks (page-allocated), so multi-GB volumes format
in seconds instead of millions of single-sector writes.

format.c: the y/n confirmation sets a new raw-keyboard mode
(io_set_raw_keyboard(), io.c/h, wm.c) -- the WM loop was draining the
scancode ring and eating the key, so format hung forever with a USB
keyboard.  After a successful format the VFS re-mounts the new
filesystem so save/ls/cat work immediately.

storage.c: present disks print model + size; vfs/blkdev handle BLK_SATA
like IDE (mounts, present checks, per-device FAT lines); save/mkdir/rm/
rmdir drop the stale "read-only filesystem" error text.

Diagnostics for serial-less boards: ahci.c logs every storage-class PCI
function (id/class/prog-if) and the matching controller's BAR, ata.c
logs the resolved channel port bases, all once per boot.

AGENTS.md: SATA/AHCI and FAT read/write sections, run-ahci target,
kexec staging cap 0x38200 -> 0x3A200 (the kernel region is 0x42000; also
fixed in kexec.h/bootstrap.c).

Verified on QEMU ich9-ahci (format FAT32, save/cat/mkdir/rm/rmdir,
persistence across reboot) with mtools reading/writing the image from
the host, and on the ASUS AT3IONT-I (160 GB Hitachi on AHCI port 0).
Johannes Zellner hace 1 mes
padre
commit
8d5812c924
Se han modificado 24 ficheros con 936 adiciones y 109 borrados
  1. 18 5
      AGENTS.md
  2. 30 8
      Makefile
  3. 476 0
      ahci.c
  4. 31 0
      ahci.h
  5. 109 8
      ata.c
  6. 11 0
      ata.h
  7. 43 2
      blkdev.c
  8. 8 7
      blkdev.h
  9. 1 1
      bootstrap.c
  10. 53 24
      fat.c
  11. 2 1
      fat.h
  12. 68 21
      fatformat.c
  13. 10 1
      format.c
  14. 12 0
      io.c
  15. 6 0
      io.h
  16. 1 1
      kexec.h
  17. 1 1
      mkdir.c
  18. 1 1
      rm.c
  19. 1 1
      rmdir.c
  20. 1 1
      save.c
  21. 36 14
      storage.c
  22. 13 11
      vfs.c
  23. 1 1
      vfs.h
  24. 3 0
      wm.c

+ 18 - 5
AGENTS.md

@@ -13,6 +13,7 @@ make all        # floppy.img usb.img final.bin root.img
 make run        # boot floppy.img + root.img in QEMU (fallback: qemu-system-x86_64)
 make run        # boot floppy.img + root.img in QEMU (fallback: qemu-system-x86_64)
 make run-usb    # boot usb.img as an IDE disk
 make run-usb    # boot usb.img as an IDE disk
 make run-uhci   # like `make run` but with a UHCI controller + USB mouse
 make run-uhci   # like `make run` but with a UHCI controller + USB mouse
+make run-ahci   # like `make run` + an ICH9 AHCI controller and a 512 MB SATA disk (sata0)
 make test-usb   # headless USB mouse/keyboard regression tests (QEMU + python3)
 make test-usb   # headless USB mouse/keyboard regression tests (QEMU + python3)
 make clean
 make clean
 ```
 ```
@@ -51,7 +52,7 @@ make clean
 | `0x42F00` | Program API struct (`PROGRAM_API_ADDR`, `run_api.h`) |
 | `0x42F00` | Program API struct (`PROGRAM_API_ADDR`, `run_api.h`) |
 | `0x43000` | User program load address (`run.c`, `user.ld`) |
 | `0x43000` | User program load address (`run.c`, `user.ld`) |
 | `0x50000` | Fallback process stack pool (8 × 4 KB; primary stacks come from the frame allocator, ≥ `0x900000`, see `process.c`/`mm.c`) |
 | `0x50000` | Fallback process stack pool (8 × 4 KB; primary stacks come from the frame allocator, ≥ `0x900000`, see `process.c`/`mm.c`) |
-| `0x58000` | kexec staging buffer (received kernel image, up to 0x38200 B; ends below the VGA window) |
+| `0x58000` | kexec staging buffer (received kernel image, up to 0x3A200 B; ends below the VGA window) |
 | `0x100000`| VGA shadow buffer (`VGA_SHADOW_BASE`, `vga.h`) |
 | `0x100000`| VGA shadow buffer (`VGA_SHADOW_BASE`, `vga.h`) |
 
 
 ## Key files
 ## Key files
@@ -96,9 +97,18 @@ make clean
 - **Shell**: `terminal.c` — history, line editing, command dispatch (hand-rolled
 - **Shell**: `terminal.c` — history, line editing, command dispatch (hand-rolled
   `is_cmd_*` parsers). Runs in a window (`term_run(struct term *)`); reads keys
   `is_cmd_*` parsers). Runs in a window (`term_run(struct term *)`); reads keys
   from its per-terminal queue (filled by the WM).
   from its per-terminal queue (filled by the WM).
-- **Filesystem**: `fat.c` (FAT12/16/32 read + FAT12/16 write, device-parameterized),
+- **Filesystem**: `fat.c` (FAT12/16/32 read/write, device-parameterized),
   `vfs.c` (mounts every FAT device under `/`), `fatdetect.c`/`fatformat.c`,
   `vfs.c` (mounts every FAT device under `/`), `fatdetect.c`/`fatformat.c`,
-  `blkdev.c` (floppy + IDE registry), `ata.c` (ATA PIO), `disk.c` (FDC + ISA DMA ch2, retries).
+  `blkdev.c` (floppy + IDE + SATA registry), `ata.c` (ATA PIO, either the
+  legacy ports or the PCI IDE interface's native BARs — `ata_init_ports()`),
+  `disk.c` (FDC + ISA DMA ch2, retries).
+- **SATA/AHCI**: `ahci.c/h` — polled AHCI driver (first controller only, PCI
+  class `01:06:01`, no IRQs, no NCQ): port bring-up with COMRESET, IDENTIFY
+  (shared `ata_identify_parse()` with `ata.c`) and READ/WRITE DMA EXT through
+  a page-allocated bounce buffer (nothing large in BSS). Disks are registered
+  as `sata0`..`sataN` (named by AHCI port index) and mount like IDE disks
+  (`sata0p1` for an MBR partition). >2 TB disks are clamped to 32-bit LBA;
+  a disk added after boot needs a rescan (no COMRESET hotplug).
 - **Serial**: `serial.c`, `serial_ring_buf.c/h`, `bin_recv.c/h`, `serial_recv.c`,
 - **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.
   `serial_loop.c`, `send_serial.c` — 16550 UART, IRQ-driven ring buffer, XMODEM.
 - **Network**: `netdev.c/h` — NIC driver registry (`struct netdev`, first probe
 - **Network**: `netdev.c/h` — NIC driver registry (`struct netdev`, first probe
@@ -479,7 +489,10 @@ Caveats:
   isolation: everything runs ring-0 and shares the address space.
   isolation: everything runs ring-0 and shares the address space.
 - **All drawing goes into the shadow buffer** (`vga_put_pixel_rgb` etc.); a 30 Hz
 - **All drawing goes into the shadow buffer** (`vga_put_pixel_rgb` etc.); a 30 Hz
   flush copies dirty rows to VRAM. Full-screen programs call `vga_clear_rgb`.
   flush copies dirty rows to VRAM. Full-screen programs call `vga_clear_rgb`.
-- **FAT is read-only** — parse/read only; no write/create/delete.
+- **FAT is read/write** — FAT12/16/32 read plus write/delete/mkdir/rmdir.
+  FAT32 allocation uses an FSInfo-seeded free-cluster hint; the FSInfo
+  free-count is not updated and a completely full FAT32 directory is not
+  grown (new cluster allocation for directories is not implemented).
 - **Keep the memory map in sync** — hardcoded addresses appear in `.S`, `.c`, and
 - **Keep the memory map in sync** — hardcoded addresses appear in `.S`, `.c`, and
   linker scripts; changing one without the others breaks boot/loading.
   linker scripts; changing one without the others breaks boot/loading.
 
 
@@ -490,7 +503,7 @@ Caveats:
 2. `vga.c` `draw_flush_debug_square()` — leftover debug artifact, still draws a
 2. `vga.c` `draw_flush_debug_square()` — leftover debug artifact, still draws a
    color-cycling 32×32 square in the corner on every flush.
    color-cycling 32×32 square in the corner on every flush.
 3. No per-process address spaces or process isolation (paging is identity-mapped).
 3. No per-process address spaces or process isolation (paging is identity-mapped).
-4. Read-only FAT filesystem.
+4. FAT32: FSInfo free-count not updated, no directory-chain growth.
 5. Implicit, hardcoded memory map (see table above).
 5. Implicit, hardcoded memory map (see table above).
 6. `disk.c` `fdc_reset()` and `fdc_init_once()` overlap/duplicate logic.
 6. `disk.c` `fdc_reset()` and `fdc_init_once()` overlap/duplicate logic.
 7. `terminal.c` command dispatch is ~30 near-identical `is_cmd_*` functions;
 7. `terminal.c` command dispatch is ~30 near-identical `is_cmd_*` functions;

+ 30 - 8
Makefile

@@ -13,7 +13,7 @@ ASFLAGS  = --32
 BOOT_LDFLAGS   = -T linker.ld -nostdlib -m elf_i386
 BOOT_LDFLAGS   = -T linker.ld -nostdlib -m elf_i386
 KERNEL_LDFLAGS = -T kernel.ld -nostdlib -m elf_i386
 KERNEL_LDFLAGS = -T kernel.ld -nostdlib -m elf_i386
 
 
-.PHONY: all clean run run-floppy run-usb run-uhci test-usb run-serial floppy usb flash space prog bootstrap dev net-dev netmon monitor
+.PHONY: all clean run run-floppy run-usb run-uhci run-ahci test-usb run-serial floppy usb flash space prog bootstrap dev net-dev netmon monitor
 
 
 all: floppy.img usb.img final.bin root.img
 all: floppy.img usb.img final.bin root.img
 
 
@@ -173,22 +173,25 @@ netdev.o: netdev.c netdev.h ne2000.h
 net.o: net.c net.h io.h netdev.h
 net.o: net.c net.h io.h netdev.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
-blkdev.o: blkdev.c blkdev.h ata.h disk.h
+blkdev.o: blkdev.c blkdev.h ata.h ahci.h disk.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
-ata.o: ata.c ata.h pci.h timer.h
+ata.o: ata.c ata.h pci.h serial.h timer.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+ahci.o: ahci.c ahci.h ata.h mm.h paging.h pci.h process.h serial.h timer.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
 fatdetect.o: fatdetect.c fatdetect.h fat.h blkdev.h
 fatdetect.o: fatdetect.c fatdetect.h fat.h blkdev.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
-storage.o: storage.c blkdev.h vfs.h io.h
+storage.o: storage.c ahci.h blkdev.h vfs.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
-fatformat.o: fatformat.c fatformat.h blkdev.h
+fatformat.o: fatformat.c fatformat.h blkdev.h mm.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
-format.o: format.c blkdev.h disk.h fatformat.h io.h vga.h
+format.o: format.c blkdev.h disk.h fatformat.h io.h vga.h vfs.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
 mouse.o: mouse.c mouse.h cirrus.h radeon.h timer.h usbhid.h vga.h
 mouse.o: mouse.c mouse.h cirrus.h radeon.h timer.h usbhid.h vga.h
@@ -260,7 +263,7 @@ syscall_stub.o: syscall_stub.S
 mem.o: mem.c mm.h io.h
 mem.o: mem.c mm.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(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 nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o ohci.o usb.o usbhid.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
+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 nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.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 $@ $^
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -408,6 +411,25 @@ run-uhci: floppy.img root.img
 	qemu-system-i386 -M pc -device piix3-usb-uhci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
 	qemu-system-i386 -M pc -device piix3-usb-uhci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
 	qemu-system-x86_64 -M pc -device piix3-usb-uhci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
 	qemu-system-x86_64 -M pc -device piix3-usb-uhci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
 
 
+# SATA/AHCI variant: an ICH9 AHCI controller with a test disk on port 0.
+# The disk image lives outside the repo and is created on first use; boot
+# stays on the floppy (order=a), the SATA disk shows up as sata0.
+SATA_IMG ?= /tmp/opencode/nebos-sata.img
+run-ahci: floppy.img root.img
+	@test -f $(SATA_IMG) || truncate -s 512M $(SATA_IMG)
+	qemu-system-i386 -M pc -boot order=a -device pci-ohci -device usb-mouse \
+	  -drive file=floppy.img,format=raw,if=floppy \
+	  -drive file=root.img,format=raw,if=floppy \
+	  -device ich9-ahci,id=sata \
+	  -drive file=$(SATA_IMG),format=raw,if=none,id=satadisk \
+	  -device ide-hd,drive=satadisk,bus=sata.0 2>/dev/null || \
+	qemu-system-x86_64 -M pc -boot order=a -device pci-ohci -device usb-mouse \
+	  -drive file=floppy.img,format=raw,if=floppy \
+	  -drive file=root.img,format=raw,if=floppy \
+	  -device ich9-ahci,id=sata \
+	  -drive file=$(SATA_IMG),format=raw,if=none,id=satadisk \
+	  -device ide-hd,drive=satadisk,bus=sata.0
+
 # Headless USB HID regression tests (QEMU + python3): cursor movement via
 # Headless USB HID regression tests (QEMU + python3): cursor movement via
 # injected USB mouse reports and keyboard input via injected key events,
 # injected USB mouse reports and keyboard input via injected key events,
 # including hotplug.  See tools/*.py.
 # including hotplug.  See tools/*.py.
@@ -485,4 +507,4 @@ net-dev: kernel.bin netkexec_send
 	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
 	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
 
 
 clean:
 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 nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o ohci.o usb.o usbhid.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
+	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 nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.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

+ 476 - 0
ahci.c

@@ -0,0 +1,476 @@
+/* AHCI (SATA) driver: PCI discovery, port bring-up, IDENTIFY and DMA
+ * read/write (READ/WRITE DMA EXT, LBA48), all polled (no IRQs).
+ *
+ * Target: the ION/AT3IONT-I MCP79 SATA controller in AHCI mode (the same
+ * hardware family Linux drives with board_ahci_noncq), and QEMU's ich9-ahci
+ * for testing. Only non-NCQ commands are used.
+ *
+ * All buffers (command list, FIS receive area, command table, DMA bounce
+ * buffer) come from the page allocator -- the kernel lives under a tight
+ * linker bound, so nothing large may go into BSS. */
+
+#include "ahci.h"
+#include "ata.h"
+#include "mm.h"
+#include "paging.h"
+#include "pci.h"
+#include "process.h"
+#include "serial.h"
+#include "timer.h"
+
+#define AHCI_DEBUG 0
+
+/* ---- generic host control registers (ABAR-relative) ---- */
+#define H_CAP       0x00
+#define H_GHC       0x04
+#define H_PI        0x0C
+
+#define GHC_AE      0x80000000u   /* AHCI enable */
+
+/* ---- port registers (0x100 + 0x80 * port) ---- */
+#define P_CLB       0x00
+#define P_CLBU      0x04
+#define P_FB        0x08
+#define P_FBU       0x0C
+#define P_IS        0x10
+#define P_IE        0x14
+#define P_CMD       0x18
+#define P_TFD       0x20
+#define P_SIG       0x24
+#define P_SSTS      0x28
+#define P_SCTL      0x2C
+#define P_SERR      0x30
+#define P_CI        0x38
+
+#define CMD_ST      0x00000001u   /* start */
+#define CMD_FRE     0x00000010u   /* FIS receive enable */
+#define CMD_FR      0x00004000u   /* FIS receive running */
+#define CMD_CR      0x00008000u   /* command list running */
+
+#define TFD_ERR     0x01
+#define TFD_BSY     0x80
+#define TFD_DRQ     0x08
+#define TFD_ERR_BITS (TFD_ERR | TFD_BSY | TFD_DRQ)
+
+#define IS_TFES     0x40000000u   /* task file error status */
+
+#define SIG_ATA     0x00000101u
+
+/* Register Host-to-Device FIS. */
+#define FIS_H2D     0x27
+#define FIS_C       0x80
+
+#define ATA_IDENTIFY        0xEC
+#define ATA_READ_DMA_EXT    0x25
+#define ATA_WRITE_DMA_EXT   0x35
+
+#define AHCI_CHUNK  255           /* sectors per command (128 KB-ish) */
+#define BOUNCE_PAGES 33           /* 255 * 512 = 130560 B < 33 * 4096 */
+
+static struct {
+	int inited;
+	int ok;
+	unsigned int abar;
+	unsigned int nports;
+	unsigned int pi;
+	unsigned int cl;              /* command list (1 page) */
+	unsigned int fis;             /* FIS receive area (1 page) */
+	unsigned int ct;              /* command table (1 page) */
+	unsigned int bounce;          /* data bounce buffer (BOUNCE_PAGES) */
+} ahci;
+
+static unsigned int rd(unsigned int off)
+{
+	return *(volatile unsigned int *)(ahci.abar + off);
+}
+
+static void wr(unsigned int off, unsigned int v)
+{
+	*(volatile unsigned int *)(ahci.abar + off) = v;
+}
+
+static unsigned int prd(unsigned int port, unsigned int off)
+{
+	return *(volatile unsigned int *)(ahci.abar + 0x100 + 0x80 * port + off);
+}
+
+static void pwr(unsigned int port, unsigned int off, unsigned int v)
+{
+	*(volatile unsigned int *)(ahci.abar + 0x100 + 0x80 * port + off) = v;
+}
+
+static void mem_copy(unsigned char *dst, const unsigned char *src, unsigned int n)
+{
+	while (n--)
+		*dst++ = *src++;
+}
+
+static void delay_ms(unsigned int ms)
+{
+	unsigned int t0 = get_ticks_ms();
+	while (get_ticks_ms() - t0 < ms)
+		;
+}
+
+/* ---- PCI discovery ---- */
+
+static int ahci_find(unsigned int *out_bus, unsigned int *out_dev, unsigned int *out_func)
+{
+	unsigned int bus, dev, func;
+
+	for (bus = 0; bus < 256; bus++) {
+		for (dev = 0; dev < 32; dev++) {
+			unsigned int id = pci_cfg_read32(bus, dev, 0, 0x00);
+			unsigned int htype, nfunc;
+
+			if (id == 0xFFFFFFFFu || id == 0)
+				continue;
+			htype = (pci_cfg_read32(bus, dev, 0, 0x0C) >> 16) & 0xFFu;
+			nfunc = (htype & 0x80u) ? 8u : 1u;
+			for (func = 0; func < nfunc; func++) {
+				unsigned int idf = pci_cfg_read32(bus, dev, func, 0x00);
+				unsigned int crev, cls, sub, prog;
+
+				if (idf == 0xFFFFFFFFu || idf == 0)
+					continue;
+				crev = pci_cfg_read32(bus, dev, func, 0x08);
+				cls  = (crev >> 24) & 0xFFu;
+				sub  = (crev >> 16) & 0xFFu;
+				prog = (crev >> 8) & 0xFFu;
+				if (cls != 0x01)
+					continue;
+				/* One line per storage function so a board whose
+				 * SATA mode does not match can be diagnosed from
+				 * the serial/netlog output (class CCDD, prog-if,
+				 * vendor:device). */
+				serial_puts(SERIAL_COM1, "ahci: storage fn ");
+				serial_putdec(SERIAL_COM1, bus);
+				serial_putc(SERIAL_COM1, ':');
+				serial_putdec(SERIAL_COM1, dev);
+				serial_putc(SERIAL_COM1, '.');
+				serial_putdec(SERIAL_COM1, func);
+				serial_puts(SERIAL_COM1, " id ");
+				serial_puthex(SERIAL_COM1, idf);
+				serial_puts(SERIAL_COM1, " class ");
+				serial_puthex(SERIAL_COM1, (cls << 8) | sub);
+				serial_puts(SERIAL_COM1, " prog ");
+				serial_puthex(SERIAL_COM1, prog);
+				serial_putc(SERIAL_COM1, '\n');
+				if (sub == 0x06 && prog == 0x01) {
+					*out_bus = bus;
+					*out_dev = dev;
+					*out_func = func;
+					return 0;
+				}
+			}
+		}
+	}
+	return -1;
+}
+
+/* ---- port bring-up ---- */
+
+static void ahci_port_init(unsigned int port)
+{
+	unsigned int t0, c;
+
+	/* Stop the port and its FIS receive engine. */
+	c = prd(port, P_CMD);
+	if (c & CMD_ST) {
+		pwr(port, P_CMD, c & ~CMD_ST);
+		t0 = get_ticks_ms();
+		while ((prd(port, P_CMD) & CMD_CR) && get_ticks_ms() - t0 < 500)
+			;
+	}
+	c = prd(port, P_CMD);
+	if (c & CMD_FRE) {
+		pwr(port, P_CMD, c & ~CMD_FRE);
+		t0 = get_ticks_ms();
+		while ((prd(port, P_CMD) & CMD_FR) && get_ticks_ms() - t0 < 500)
+			;
+	}
+
+	pwr(port, P_CLB, ahci.cl);
+	pwr(port, P_CLBU, 0);
+	pwr(port, P_FB, ahci.fis);
+	pwr(port, P_FBU, 0);
+
+	pwr(port, P_SERR, 0xFFFFFFFFu);
+	pwr(port, P_IS, 0xFFFFFFFFu);
+	pwr(port, P_IE, 0);
+
+	pwr(port, P_CMD, prd(port, P_CMD) | CMD_FRE);
+	t0 = get_ticks_ms();
+	while (!(prd(port, P_CMD) & CMD_FR) && get_ticks_ms() - t0 < 500)
+		;
+
+	/* Cold plug: reset the link if it is not established yet. */
+	if ((prd(port, P_SSTS) & 0x0Fu) != 3) {
+		pwr(port, P_SCTL, 1);   /* DET = 1: initiate COMRESET */
+		t0 = get_ticks_ms();
+		while ((prd(port, P_SSTS) & 0x0Fu) != 3 && get_ticks_ms() - t0 < 1000)
+			;
+	}
+
+	pwr(port, P_CMD, prd(port, P_CMD) | CMD_ST);
+	t0 = get_ticks_ms();
+	while (!(prd(port, P_CMD) & CMD_CR) && get_ticks_ms() - t0 < 500)
+		;
+}
+
+int ahci_init(void)
+{
+	unsigned int bus, dev, func, bar, cmd, cap, i;
+
+	if (ahci.inited)
+		return ahci.ok ? 0 : -1;
+	ahci.inited = 1;
+
+	if (ahci_find(&bus, &dev, &func) != 0) {
+		serial_puts(SERIAL_COM1, "ahci: no matching controller (need 0106:01)\n");
+		return -1;
+	}
+
+	bar = pci_cfg_read32(bus, dev, func, 0x24);
+	if (bar & 1u) {
+		serial_puts(SERIAL_COM1, "ahci: BAR5 is not MMIO\n");
+		return -1;
+	}
+	bar &= 0xFFFFFFF0u;
+	if (!bar || bar == 0xFFFFFFF0u) {
+		serial_puts(SERIAL_COM1, "ahci: BAR5 is unassigned\n");
+		return -1;
+	}
+
+	cmd = pci_cfg_read32(bus, dev, func, 0x04);
+	pci_cfg_write32(bus, dev, func, 0x04, cmd | 0x0006u);  /* mem + bus master */
+
+	process_map_mmio_all(bar, 0x2000);
+	ahci.abar = bar;
+
+	cap = rd(H_CAP);
+	if (cap == 0 || cap == 0xFFFFFFFFu) {
+		serial_puts(SERIAL_COM1, "ahci: no register response\n");
+		return -1;
+	}
+
+	ahci.cl     = mm_alloc_page();
+	ahci.fis    = mm_alloc_page();
+	ahci.ct     = mm_alloc_page();
+	ahci.bounce = mm_alloc_pages(BOUNCE_PAGES);
+	if (!ahci.cl || !ahci.fis || !ahci.ct || !ahci.bounce) {
+		serial_puts(SERIAL_COM1, "ahci: no memory for DMA buffers\n");
+		return -1;
+	}
+	{
+		unsigned int *p;
+		unsigned int n;
+		for (p = (unsigned int *)ahci.cl, n = 0; n < 0x400; n++) p[n] = 0;
+		for (p = (unsigned int *)ahci.fis, n = 0; n < 0x400; n++) p[n] = 0;
+		for (p = (unsigned int *)ahci.ct, n = 0; n < 0x400; n++) p[n] = 0;
+		for (p = (unsigned int *)ahci.bounce, n = 0; n < BOUNCE_PAGES * 0x400; n++)
+			p[n] = 0;
+	}
+
+	wr(H_GHC, rd(H_GHC) | GHC_AE);
+
+	ahci.nports = (cap & 0x1Fu) + 1;
+	if (ahci.nports > 32)
+		ahci.nports = 32;
+	ahci.pi = rd(H_PI);
+
+	serial_puts(SERIAL_COM1, "ahci: ctrl ");
+	serial_putdec(SERIAL_COM1, bus);
+	serial_putc(SERIAL_COM1, ':');
+	serial_putdec(SERIAL_COM1, dev);
+	serial_putc(SERIAL_COM1, '.');
+	serial_putdec(SERIAL_COM1, func);
+	serial_puts(SERIAL_COM1, " bar=");
+	serial_puthex(SERIAL_COM1, bar);
+	serial_puts(SERIAL_COM1, " pi=");
+	serial_puthex(SERIAL_COM1, ahci.pi);
+	serial_putc(SERIAL_COM1, '\n');
+
+	for (i = 0; i < ahci.nports; i++) {
+		if (ahci.pi & (1u << i))
+			ahci_port_init(i);
+	}
+
+	ahci.ok = 1;
+	return 0;
+}
+
+int ahci_controller_present(void)
+{
+	ahci_init();
+	return ahci.ok;
+}
+
+/* ---- command issue (slot 0, polled) ---- */
+
+static int ahci_issue(unsigned int port, const unsigned char fis[20], int write,
+		      unsigned int dma_addr, unsigned int bytes)
+{
+	unsigned int *hdr = (unsigned int *)ahci.cl;
+	unsigned char *ct = (unsigned char *)ahci.ct;
+	unsigned int t0, i;
+
+	/* Wait for the port to be idle (previous command finished). */
+	t0 = get_ticks_ms();
+	while (prd(port, P_CI) & 1u) {
+		if (get_ticks_ms() - t0 > 4000)
+			return -1;
+	}
+	if (prd(port, P_TFD) & TFD_BSY)
+		return -1;
+
+	for (i = 0; i < 64; i++)
+		ct[i] = 0;
+	for (i = 0; i < 20; i++)
+		ct[i] = fis[i];
+	/* PRDT entry 0: data buffer + byte count. */
+	((unsigned int *)(ct + 0x80))[0] = dma_addr;
+	((unsigned int *)(ct + 0x80))[1] = 0;
+	((unsigned int *)(ct + 0x80))[2] = 0;
+	((unsigned int *)(ct + 0x80))[3] = ((bytes - 1) & 0x3FFFFFu) | 0x80000000u;
+
+	/* Command header: CFL=5 dwords, W (write), PRDTL=1. */
+	hdr[0] = 5u | (write ? (1u << 6) : 0) | (1u << 16);
+	hdr[1] = 0;
+	hdr[2] = ahci.ct;
+	hdr[3] = 0;
+
+	pwr(port, P_IS, 0xFFFFFFFFu);
+	pwr(port, P_CI, 1u);
+
+	t0 = get_ticks_ms();
+	while (prd(port, P_CI) & 1u) {
+		if (get_ticks_ms() - t0 > 4000) {
+#if AHCI_DEBUG
+			serial_puts(SERIAL_COM1, "ahci: command timeout\n");
+#endif
+			return -1;
+		}
+	}
+	if (prd(port, P_IS) & IS_TFES) {
+#if AHCI_DEBUG
+		serial_puts(SERIAL_COM1, "ahci: task file error, tfd=");
+		serial_puthex(SERIAL_COM1, prd(port, P_TFD));
+		serial_putc(SERIAL_COM1, '\n');
+#endif
+		return -1;
+	}
+	if (prd(port, P_TFD) & TFD_ERR_BITS)
+		return -1;
+	return 0;
+}
+
+static int ahci_identify_port(unsigned int port, unsigned int *sectors, char model[41])
+{
+	unsigned char fis[20];
+	unsigned int attempt, i;
+	int r = -1;
+
+	for (i = 0; i < 20; i++)
+		fis[i] = 0;
+	fis[0] = FIS_H2D;
+	fis[1] = FIS_C;
+	fis[2] = ATA_IDENTIFY;
+	fis[7] = 0xA0;                     /* device 0 */
+
+	for (attempt = 0; attempt < 3; attempt++) {
+		r = ahci_issue(port, fis, 0, ahci.bounce, 512);
+		if (r == 0)
+			break;
+		delay_ms(20);
+	}
+	if (r != 0)
+		return -1;
+
+	return ata_identify_parse((const unsigned short *)ahci.bounce, sectors, model);
+}
+
+static int ahci_xfer(unsigned int port, unsigned int lba, void *buf,
+		     unsigned int count, int write)
+{
+	unsigned char *p = buf;
+	unsigned char *bounce = (unsigned char *)ahci.bounce;
+	unsigned int done = 0;
+
+	if (!ahci.ok || count == 0)
+		return -1;
+
+	while (done < count) {
+		unsigned int chunk = count - done;
+		unsigned int cur = lba + done;
+		unsigned char fis[20];
+		unsigned int i;
+
+		if (chunk > AHCI_CHUNK)
+			chunk = AHCI_CHUNK;
+
+		for (i = 0; i < 20; i++)
+			fis[i] = 0;
+		fis[0] = FIS_H2D;
+		fis[1] = FIS_C;
+		fis[2] = write ? ATA_WRITE_DMA_EXT : ATA_READ_DMA_EXT;
+		fis[4] = (unsigned char)(cur & 0xFF);
+		fis[5] = (unsigned char)((cur >> 8) & 0xFF);
+		fis[6] = (unsigned char)((cur >> 16) & 0xFF);
+		fis[7] = 0x40;                 /* LBA mode */
+		fis[8] = (unsigned char)((cur >> 24) & 0xFF);
+		fis[9] = 0;                    /* LBA4/LBA5: 32-bit LBA only */
+		fis[10] = 0;
+		fis[12] = (unsigned char)(chunk & 0xFF);
+		fis[13] = (unsigned char)((chunk >> 8) & 0xFF);
+
+		if (write)
+			mem_copy(bounce, p + done * 512u, chunk * 512u);
+		if (ahci_issue(port, fis, write, ahci.bounce, chunk * 512u) != 0)
+			return -1;
+		if (!write)
+			mem_copy(p + done * 512u, bounce, chunk * 512u);
+		done += chunk;
+	}
+	return 0;
+}
+
+int ahci_read_sectors(unsigned int port, unsigned int lba, void *buf, unsigned int count)
+{
+	return ahci_xfer(port, lba, buf, count, 0);
+}
+
+int ahci_write_sectors(unsigned int port, unsigned int lba, const void *buf,
+		       unsigned int count)
+{
+	return ahci_xfer(port, lba, (void *)buf, count, 1);
+}
+
+unsigned int ahci_scan(struct ahci_disk *out, unsigned int max)
+{
+	unsigned int port, n = 0;
+
+	if (ahci_init() != 0)
+		return 0;
+
+	for (port = 0; port < ahci.nports && n < max; port++) {
+		if (!(ahci.pi & (1u << port)))
+			continue;
+		if ((prd(port, P_SSTS) & 0x0Fu) != 3)
+			continue;
+		if (prd(port, P_SIG) != SIG_ATA)
+			continue;              /* ATAPI / port multiplier / SEMB */
+		if (ahci_identify_port(port, &out[n].sectors, out[n].model) != 0)
+			continue;
+		out[n].port = port;
+		serial_puts(SERIAL_COM1, "ahci: port ");
+		serial_putdec(SERIAL_COM1, port);
+		serial_puts(SERIAL_COM1, " ");
+		serial_puts(SERIAL_COM1, out[n].model);
+		serial_puts(SERIAL_COM1, " ");
+		serial_putdec(SERIAL_COM1, out[n].sectors);
+		serial_puts(SERIAL_COM1, " sectors\n");
+		n++;
+	}
+	return n;
+}

+ 31 - 0
ahci.h

@@ -0,0 +1,31 @@
+/* AHCI (SATA) driver: polled, no IRQs, no NCQ.
+ * Finds the first AHCI controller (PCI class 01:06:01), brings its ports up
+ * and exposes the attached ATA disks. Read/write use DMA (READ/WRITE DMA EXT,
+ * LBA48) through a bounce buffer. */
+#ifndef AHCI_H
+#define AHCI_H
+
+#define AHCI_MAX_DISKS 8
+
+struct ahci_disk {
+	unsigned int port;          /* controller port number */
+	unsigned int sectors;       /* total sectors (clamped to 32 bits) */
+	char model[41];
+};
+
+/* Initialise the first AHCI controller. Idempotent; returns 0 if a controller
+ * was found and brought up, -1 otherwise. */
+int ahci_init(void);
+
+/* 1 if an AHCI controller was found and initialised. */
+int ahci_controller_present(void);
+
+/* Collect present ATA disks. Returns the number written (<= max). */
+unsigned int ahci_scan(struct ahci_disk *out, unsigned int max);
+
+/* Read/write `count` sectors at LBA `lba` on the given controller port.
+ * Return 0 on success, -1 on error. */
+int ahci_read_sectors(unsigned int port, unsigned int lba, void *buf, unsigned int count);
+int ahci_write_sectors(unsigned int port, unsigned int lba, const void *buf, unsigned int count);
+
+#endif

+ 109 - 8
ata.c

@@ -1,12 +1,17 @@
 /* ATA PIO driver: IDENTIFY + LBA28/LBA48 read/write, polled (no IRQ).
 /* ATA PIO driver: IDENTIFY + LBA28/LBA48 read/write, polled (no IRQ).
- * The ATA IRQs (14/15) stay masked at boot, so we poll BSY/DRQ/ERR. */
+ * The ATA IRQs (14/15) stay masked at boot, so we poll BSY/DRQ/ERR.
+ *
+ * Channel port bases default to the legacy primary/secondary ports and are
+ * replaced by the PCI IDE interface's BARs when it runs in native mode
+ * (nVidia MCP79 "compatibility mode" and friends). */
 
 
 #include "ata.h"
 #include "ata.h"
 #include "pci.h"
 #include "pci.h"
+#include "serial.h"
 #include "timer.h"
 #include "timer.h"
 
 
-static const unsigned short ata_base[2] = { 0x1F0, 0x170 };
-static const unsigned short ata_ctrl[2] = { 0x3F6, 0x376 };
+static unsigned short ata_base[2] = { 0x1F0, 0x170 };
+static unsigned short ata_ctrl[2] = { 0x3F6, 0x376 };
 
 
 /* Register offsets from the I/O base. */
 /* Register offsets from the I/O base. */
 #define REG_DATA    0
 #define REG_DATA    0
@@ -31,6 +36,94 @@ static const unsigned short ata_ctrl[2] = { 0x3F6, 0x376 };
 #define CMD_READ_EXT  0x24   /* LBA48 */
 #define CMD_READ_EXT  0x24   /* LBA48 */
 #define CMD_WRITE_EXT 0x34   /* LBA48 */
 #define CMD_WRITE_EXT 0x34   /* LBA48 */
 
 
+/* Point the primary/secondary channels at the PCI IDE interface's native
+ * per-channel I/O BARs when it uses native mode, keeping the legacy ports
+ * otherwise.  Runs once; without a PCI IDE function the defaults stay. */
+void ata_init_ports(void)
+{
+	static int inited;
+	unsigned int bus, dev, func;
+
+	if (inited)
+		return;
+	inited = 1;
+
+	for (bus = 0; bus < 256; bus++) {
+		for (dev = 0; dev < 32; dev++) {
+			unsigned int id = pci_cfg_read32(bus, dev, 0, 0x00);
+			unsigned int htype, nfunc;
+
+			if (id == 0xFFFFFFFFu || id == 0)
+				continue;
+			htype = (pci_cfg_read32(bus, dev, 0, 0x0C) >> 16) & 0xFFu;
+			nfunc = (htype & 0x80u) ? 8u : 1u;
+			for (func = 0; func < nfunc; func++) {
+				unsigned int idf = pci_cfg_read32(bus, dev, func, 0x00);
+				unsigned int crev, cls, sub, prog;
+				unsigned int bar0, bar1, bar2, bar3, cmd;
+				unsigned short b0, c0, b1, c1;
+
+				if (idf == 0xFFFFFFFFu || idf == 0)
+					continue;
+				crev = pci_cfg_read32(bus, dev, func, 0x08);
+				cls  = (crev >> 24) & 0xFFu;
+				sub  = (crev >> 16) & 0xFFu;
+				prog = (crev >> 8) & 0xFFu;
+				if (cls != 0x01 || sub != 0x01)
+					continue;
+
+				cmd = pci_cfg_read32(bus, dev, func, 0x04);
+				pci_cfg_write32(bus, dev, func, 0x04, cmd | 0x0005u);
+				bar0 = pci_cfg_read32(bus, dev, func, 0x10);
+				bar1 = pci_cfg_read32(bus, dev, func, 0x14);
+				bar2 = pci_cfg_read32(bus, dev, func, 0x18);
+				bar3 = pci_cfg_read32(bus, dev, func, 0x1C);
+
+				/* prog-if bit 0/2: primary/secondary native mode. */
+				if ((prog & 0x01u) && (bar0 & 0x1u)) {
+					b0 = (unsigned short)(bar0 & 0xFFFCu);
+					c0 = (bar1 & 0x1u)
+						? (unsigned short)(bar1 & 0xFFFCu)
+						: (unsigned short)(b0 + 8);
+				} else {
+					b0 = 0x1F0; c0 = 0x3F6;
+				}
+				if ((prog & 0x04u) && (bar2 & 0x1u)) {
+					b1 = (unsigned short)(bar2 & 0xFFFCu);
+					c1 = (bar3 & 0x1u)
+						? (unsigned short)(bar3 & 0xFFFCu)
+						: (unsigned short)(b1 + 8);
+				} else {
+					b1 = 0x170; c1 = 0x376;
+				}
+
+				ata_base[0] = b0; ata_ctrl[0] = c0;
+				ata_base[1] = b1; ata_ctrl[1] = c1;
+
+				serial_puts(SERIAL_COM1, "ata: pci ide ");
+				serial_putdec(SERIAL_COM1, bus);
+				serial_putc(SERIAL_COM1, ':');
+				serial_putdec(SERIAL_COM1, dev);
+				serial_putc(SERIAL_COM1, '.');
+				serial_putdec(SERIAL_COM1, func);
+				serial_puts(SERIAL_COM1, " prog ");
+				serial_puthex(SERIAL_COM1, prog);
+				serial_puts(SERIAL_COM1, " ch0 ");
+				serial_puthex(SERIAL_COM1, b0);
+				serial_putc(SERIAL_COM1, '-');
+				serial_puthex(SERIAL_COM1, c0);
+				serial_puts(SERIAL_COM1, " ch1 ");
+				serial_puthex(SERIAL_COM1, b1);
+				serial_putc(SERIAL_COM1, '-');
+				serial_puthex(SERIAL_COM1, c1);
+				serial_putc(SERIAL_COM1, '\n');
+				return;
+			}
+		}
+	}
+	serial_puts(SERIAL_COM1, "ata: no PCI IDE function, legacy ports\n");
+}
+
 /* Delay 400 ns by reading the alt-status register a few times. */
 /* Delay 400 ns by reading the alt-status register a few times. */
 static void ata_400ns(unsigned short ctrl)
 static void ata_400ns(unsigned short ctrl)
 {
 {
@@ -98,14 +191,10 @@ static int ata_identify(int bus, int unit, unsigned short *id)
 	return 0;
 	return 0;
 }
 }
 
 
-int ata_probe(int bus, int unit, unsigned int *total_sectors, char model[41])
+int ata_identify_parse(const unsigned short *id, unsigned int *total_sectors, char model[41])
 {
 {
-	static unsigned short id[256];
 	unsigned int sectors, lo, hi, i;
 	unsigned int sectors, lo, hi, i;
 
 
-	if (ata_identify(bus, unit, id) != 0)
-		return -1;
-
 	if (id[0] & 0x8000)     /* ATAPI (cdrom/zip): not a data disk */
 	if (id[0] & 0x8000)     /* ATAPI (cdrom/zip): not a data disk */
 		return -1;
 		return -1;
 
 
@@ -129,11 +218,23 @@ int ata_probe(int bus, int unit, unsigned int *total_sectors, char model[41])
 		hi = (unsigned int)id[102] | ((unsigned int)id[103] << 16);
 		hi = (unsigned int)id[102] | ((unsigned int)id[103] << 16);
 		if (lo || hi)
 		if (lo || hi)
 			sectors = lo;
 			sectors = lo;
+		if (hi)
+			sectors = 0xFFFFFFFFu;  /* > 2 TB: clamp to 32-bit LBA */
 	}
 	}
 	*total_sectors = sectors;
 	*total_sectors = sectors;
 	return 0;
 	return 0;
 }
 }
 
 
+int ata_probe(int bus, int unit, unsigned int *total_sectors, char model[41])
+{
+	static unsigned short id[256];
+
+	if (ata_identify(bus, unit, id) != 0)
+		return -1;
+
+	return ata_identify_parse(id, total_sectors, model);
+}
+
 static inline void ata_insw(unsigned short port, void *dst, unsigned int count)
 static inline void ata_insw(unsigned short port, void *dst, unsigned int count)
 {
 {
 	__asm__ volatile ("rep insw"
 	__asm__ volatile ("rep insw"

+ 11 - 0
ata.h

@@ -6,6 +6,17 @@
  * Returns 0 and fills total_sectors/model if an ATA disk is present, else -1. */
  * Returns 0 and fills total_sectors/model if an ATA disk is present, else -1. */
 int ata_probe(int bus, int unit, unsigned int *total_sectors, char model[41]);
 int ata_probe(int bus, int unit, unsigned int *total_sectors, char model[41]);
 
 
+/* Configure the channel port bases.  Scans for a PCI IDE interface
+ * (class 01:01): in native mode (prog-if bit 0/2) the command/control blocks
+ * live in BAR0..BAR3, otherwise the fixed legacy ports are used.  Idempotent;
+ * call before ata_probe(). */
+void ata_init_ports(void);
+
+/* Parse an IDENTIFY DEVICE response (256 words): fills total_sectors (clamped
+ * to 32 bits) and a trimmed 40-char model string. Returns 0 for an ATA disk,
+ * -1 for a non-disk (e.g. ATAPI). Shared by the PATA and AHCI drivers. */
+int ata_identify_parse(const unsigned short *id, unsigned int *total_sectors, char model[41]);
+
 /* Read/write sectors via ATA PIO. Return 0 on success, -1 on error. */
 /* Read/write sectors via ATA PIO. Return 0 on success, -1 on error. */
 int ata_read_sectors(int bus, int unit, unsigned int lba, void *buf, unsigned int count);
 int ata_read_sectors(int bus, int unit, unsigned int lba, void *buf, unsigned int count);
 int ata_write_sectors(int bus, int unit, unsigned int lba, const void *buf, unsigned int count);
 int ata_write_sectors(int bus, int unit, unsigned int lba, const void *buf, unsigned int count);

+ 43 - 2
blkdev.c

@@ -1,12 +1,29 @@
-/* Block device registry: floppy (via FDC) + IDE (via ATA). */
+/* Block device registry: floppy (via FDC) + IDE (via ATA PIO) + SATA (AHCI). */
 
 
 #include "blkdev.h"
 #include "blkdev.h"
+#include "ahci.h"
 #include "ata.h"
 #include "ata.h"
 #include "disk.h"
 #include "disk.h"
 
 
 blk_dev_t blk_devs[BLKDEV_MAX];
 blk_dev_t blk_devs[BLKDEV_MAX];
 unsigned int blk_dev_count;
 unsigned int blk_dev_count;
 
 
+/* Names for SATA disks, built from the AHCI port number ("sata0".."sata31"). */
+static char sata_names[AHCI_MAX_DISKS][8];
+
+static const char *sata_name(unsigned int idx, unsigned int port)
+{
+	char *b = sata_names[idx];
+	unsigned int i = 4;
+
+	b[0] = 's'; b[1] = 'a'; b[2] = 't'; b[3] = 'a';
+	if (port >= 10)
+		b[i++] = (char)('0' + (port / 10) % 10);
+	b[i++] = (char)('0' + port % 10);
+	b[i] = '\0';
+	return b;
+}
+
 static blk_dev_t *add(const char *name, blk_kind_t kind,
 static blk_dev_t *add(const char *name, blk_kind_t kind,
 		      unsigned int bus, unsigned int unit)
 		      unsigned int bus, unsigned int unit)
 {
 {
@@ -36,7 +53,9 @@ void blk_scan(void)
 	d = add("floppy1", BLK_FLOPPY, 1, 0);
 	d = add("floppy1", BLK_FLOPPY, 1, 0);
 	if (d) { d->present = 1; d->total_sectors = 2880; }
 	if (d) { d->present = 1; d->total_sectors = 2880; }
 
 
-	/* IDE: primary + secondary, master + slave. */
+	/* IDE: primary + secondary, master + slave.  The channel port bases
+	 * come from the PCI IDE interface (native mode) or the legacy default. */
+	ata_init_ports();
 	{
 	{
 		static const char *names[4] = { "ide0", "ide1", "ide2", "ide3" };
 		static const char *names[4] = { "ide0", "ide1", "ide2", "ide3" };
 		unsigned int i;
 		unsigned int i;
@@ -56,6 +75,24 @@ void blk_scan(void)
 			}
 			}
 		}
 		}
 	}
 	}
+
+	/* SATA/AHCI: only present disks are registered, named by port index. */
+	{
+		struct ahci_disk disks[AHCI_MAX_DISKS];
+		unsigned int n = ahci_scan(disks, AHCI_MAX_DISKS);
+		unsigned int i;
+		for (i = 0; i < n; i++) {
+			unsigned int j;
+			d = add(sata_name(i, disks[i].port), BLK_SATA, 0, disks[i].port);
+			if (!d)
+				break;
+			d->present = 1;
+			d->total_sectors = disks[i].sectors;
+			for (j = 0; j < 40; j++)
+				d->model[j] = disks[i].model[j];
+			d->model[40] = '\0';
+		}
+	}
 }
 }
 
 
 static int name_eq(const char *a, const char *b)
 static int name_eq(const char *a, const char *b)
@@ -86,6 +123,8 @@ int blk_read(blk_dev_t *d, unsigned int lba, void *buf, unsigned int count)
 		disk_set_drive(d->bus);
 		disk_set_drive(d->bus);
 		return disk_read_sectors(lba, buf, count);
 		return disk_read_sectors(lba, buf, count);
 	}
 	}
+	if (d->kind == BLK_SATA)
+		return ahci_read_sectors(d->unit, lba, buf, count);
 	return ata_read_sectors(d->bus, d->unit, lba, buf, count);
 	return ata_read_sectors(d->bus, d->unit, lba, buf, count);
 }
 }
 
 
@@ -97,5 +136,7 @@ int blk_write(blk_dev_t *d, unsigned int lba, const void *buf, unsigned int coun
 		disk_set_drive(d->bus);
 		disk_set_drive(d->bus);
 		return disk_write_sectors(lba, buf, count);
 		return disk_write_sectors(lba, buf, count);
 	}
 	}
+	if (d->kind == BLK_SATA)
+		return ahci_write_sectors(d->unit, lba, buf, count);
 	return ata_write_sectors(d->bus, d->unit, lba, buf, count);
 	return ata_write_sectors(d->bus, d->unit, lba, buf, count);
 }
 }

+ 8 - 7
blkdev.h

@@ -3,19 +3,20 @@
 #ifndef BLKDEV_H
 #ifndef BLKDEV_H
 #define BLKDEV_H
 #define BLKDEV_H
 
 
-typedef enum { BLK_FLOPPY, BLK_IDE } blk_kind_t;
+typedef enum { BLK_FLOPPY, BLK_IDE, BLK_SATA } blk_kind_t;
 
 
 typedef struct {
 typedef struct {
-	const char *name;          /* "floppy0","floppy1","ide0".."ide3" */
+	const char *name;          /* "floppy0","floppy1","ide0".."ide3","sata0".. */
 	blk_kind_t kind;
 	blk_kind_t kind;
-	unsigned int bus;          /* floppy: drive #; ide: 0=primary,1=secondary */
-	unsigned int unit;         /* ide: 0=master,1=slave */
-	int present;               /* IDE: set by IDENTIFY; floppy: always listed */
+	unsigned int bus;          /* floppy: drive #; ide: 0=primary,1=secondary;
+				    * sata: controller index (0) */
+	unsigned int unit;         /* ide: 0=master,1=slave; sata: AHCI port */
+	int present;               /* set by IDENTIFY; floppy: always listed */
 	unsigned int total_sectors;/* 0 if unknown (floppy) */
 	unsigned int total_sectors;/* 0 if unknown (floppy) */
-	char model[41];            /* IDE model string */
+	char model[41];            /* IDE/SATA model string */
 } blk_dev_t;
 } blk_dev_t;
 
 
-#define BLKDEV_MAX 8
+#define BLKDEV_MAX 16
 extern blk_dev_t blk_devs[BLKDEV_MAX];
 extern blk_dev_t blk_devs[BLKDEV_MAX];
 extern unsigned int blk_dev_count;
 extern unsigned int blk_dev_count;
 
 

+ 1 - 1
bootstrap.c

@@ -20,7 +20,7 @@
 #define KERNEL_LOAD_ADDR 0x7E00
 #define KERNEL_LOAD_ADDR 0x7E00
 #define BOOT_STACK       0x42800
 #define BOOT_STACK       0x42800
 #define MAGIC            0x4345584B /* "KXEC" little-endian */
 #define MAGIC            0x4345584B /* "KXEC" little-endian */
-#define MAX_SIZE         0x38200
+#define MAX_SIZE         0x3A200
 
 
 static inline void outb(unsigned short port, unsigned char v)
 static inline void outb(unsigned short port, unsigned char v)
 {
 {

+ 53 - 24
fat.c

@@ -1,5 +1,4 @@
-/* FAT12/16/32 filesystem, device-parameterized (reads/writes via blk_read/blk_write).
- * FAT32 is read-only; writes (write/delete/mkdir/rmdir) target FAT12/16. */
+/* FAT12/16/32 filesystem, device-parameterized (reads/writes via blk_read/blk_write). */
 
 
 #include "fat.h"
 #include "fat.h"
 #include "io.h"
 #include "io.h"
@@ -176,6 +175,7 @@ int fat_mount(blk_dev_t *dev, unsigned int part_lba, fat_fs_t *fs)
 	unsigned char *b = sector_buf;
 	unsigned char *b = sector_buf;
 	unsigned int root_entries, total, sec_per_fat16, sec_per_fat32;
 	unsigned int root_entries, total, sec_per_fat16, sec_per_fat32;
 	unsigned int root_dir_sectors, data_sectors, clusters, spc, media;
 	unsigned int root_dir_sectors, data_sectors, clusters, spc, media;
+	unsigned int fsinfo_sec;
 
 
 	if (!dev || !fs)
 	if (!dev || !fs)
 		return -1;
 		return -1;
@@ -254,6 +254,10 @@ int fat_mount(blk_dev_t *dev, unsigned int part_lba, fat_fs_t *fs)
 		fs->label[k] = '\0';
 		fs->label[k] = '\0';
 	}
 	}
 
 
+	/* Capture the FSInfo sector number before the media check clobbers
+	 * sector_buf (see the allocation-hint block below). */
+	fsinfo_sec = (fs->type == FAT32) ? rd16(b + 0x30) : 0;
+
 	/* Validate the FAT media byte: rejects a boot disk whose BPB is fake
 	/* Validate the FAT media byte: rejects a boot disk whose BPB is fake
 	 * (its FAT area is actually kernel code). */
 	 * (its FAT area is actually kernel code). */
 	media = b[0x15];
 	media = b[0x15];
@@ -262,6 +266,18 @@ int fat_mount(blk_dev_t *dev, unsigned int part_lba, fat_fs_t *fs)
 	if (sector_buf[0] != media)
 	if (sector_buf[0] != media)
 		return -1;
 		return -1;
 
 
+	/* Allocation hint: for FAT32 start from the FSInfo "next free cluster"
+	 * when it is sane, so repeated writes do not rescan the whole FAT. */
+	fs->next_free = 2;
+	if (fs->type == FAT32 && fsinfo_sec != 0 &&
+	    fat_read_sector(fs, fsinfo_sec) == 0 &&
+	    rd32(sector_buf + 0x000) == 0x41615252u &&
+	    rd32(sector_buf + 0x1FC) == 0xAA550000u) {
+		unsigned int nf = rd32(sector_buf + 0x1EC);
+		if (nf >= 2 && nf < fs->max_cluster)
+			fs->next_free = nf;
+	}
+
 	fs->mounted = 1;
 	fs->mounted = 1;
 	return 0;
 	return 0;
 }
 }
@@ -490,18 +506,35 @@ int fat_read_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_
 	return (int)copied;
 	return (int)copied;
 }
 }
 
 
-/* ---- write support (FAT12/16 only) ---- */
+/* ---- write support ---- */
 
 
 static int fat_find_free_cluster(fat_fs_t *fs)
 static int fat_find_free_cluster(fat_fs_t *fs)
 {
 {
-	unsigned int cluster;
+	unsigned int c, start;
 
 
-	for (cluster = 2; cluster < fs->max_cluster; cluster++) {
+	/* Scan forward from the last allocation (wrapping once).  Without the
+	 * hint a FAT32 root scan would re-read the whole FAT every time. */
+	start = fs->next_free;
+	if (start < 2 || start >= fs->max_cluster)
+		start = 2;
+
+	for (c = start; c < fs->max_cluster; c++) {
+		unsigned int val;
+		if (fat_entry_get(fs, c, &val) != 0)
+			return -2;
+		if (val == 0x000) {
+			fs->next_free = c + 1;
+			return (int)c;
+		}
+	}
+	for (c = 2; c < start; c++) {
 		unsigned int val;
 		unsigned int val;
-		if (fat_entry_get(fs, cluster, &val) != 0)
+		if (fat_entry_get(fs, c, &val) != 0)
 			return -2;
 			return -2;
-		if (val == 0x000)
-			return (int)cluster;
+		if (val == 0x000) {
+			fs->next_free = c + 1;
+			return (int)c;
+		}
 	}
 	}
 	return -1;
 	return -1;
 }
 }
@@ -514,6 +547,8 @@ static int fat_free_chain(fat_fs_t *fs, unsigned int cluster)
 			return -2;
 			return -2;
 		if (fat_entry_set(fs, cluster, 0x000) != 0)
 		if (fat_entry_set(fs, cluster, 0x000) != 0)
 			return -2;
 			return -2;
+		if (cluster < fs->next_free)
+			fs->next_free = cluster;
 		if (next < 2 || fat_is_eoc(fs, next))
 		if (next < 2 || fat_is_eoc(fs, next))
 			break;
 			break;
 		cluster = next;
 		cluster = next;
@@ -569,6 +604,10 @@ static int fat_dir_entry_write(fat_fs_t *fs, unsigned int lba, unsigned int offs
 		sector_buf[offset + i] = 0;
 		sector_buf[offset + i] = 0;
 	sector_buf[offset + 26] = (unsigned char)(cluster & 0xFF);
 	sector_buf[offset + 26] = (unsigned char)(cluster & 0xFF);
 	sector_buf[offset + 27] = (unsigned char)((cluster >> 8) & 0xFF);
 	sector_buf[offset + 27] = (unsigned char)((cluster >> 8) & 0xFF);
+	if (fs->type == FAT32) {   /* high 16 bits of the first cluster */
+		sector_buf[offset + 20] = (unsigned char)((cluster >> 16) & 0xFF);
+		sector_buf[offset + 21] = (unsigned char)((cluster >> 24) & 0xFF);
+	}
 	sector_buf[offset + 28] = (unsigned char)(size & 0xFF);
 	sector_buf[offset + 28] = (unsigned char)(size & 0xFF);
 	sector_buf[offset + 29] = (unsigned char)((size >> 8) & 0xFF);
 	sector_buf[offset + 29] = (unsigned char)((size >> 8) & 0xFF);
 	sector_buf[offset + 30] = (unsigned char)((size >> 16) & 0xFF);
 	sector_buf[offset + 30] = (unsigned char)((size >> 16) & 0xFF);
@@ -585,14 +624,6 @@ static int fat_mark_deleted(fat_fs_t *fs, unsigned int lba, unsigned int offset)
 	return (fat_write_sector(fs, lba) != 0) ? -2 : 0;
 	return (fat_write_sector(fs, lba) != 0) ? -2 : 0;
 }
 }
 
 
-static int fat_writable(fat_fs_t *fs)
-{
-	if (fs->type != FAT32)
-		return 1;
-	io_putstr("read-only filesystem (FAT32)\n");
-	return 0;
-}
-
 int fat_write_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_3,
 int fat_write_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_3,
 		      const void *buf, unsigned int size)
 		      const void *buf, unsigned int size)
 {
 {
@@ -607,8 +638,6 @@ int fat_write_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8
 
 
 	if (!fs || !fs->mounted || !name_8_3)
 	if (!fs || !fs->mounted || !name_8_3)
 		return -1;
 		return -1;
-	if (!fat_writable(fs))
-		return -1;
 
 
 	for (i = 0; i < 11; i++)
 	for (i = 0; i < 11; i++)
 		name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
 		name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
@@ -698,8 +727,6 @@ int fat_delete_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_
 
 
 	if (!fs || !fs->mounted || !name_8_3)
 	if (!fs || !fs->mounted || !name_8_3)
 		return -1;
 		return -1;
-	if (!fat_writable(fs))
-		return -1;
 
 
 	for (i = 0; i < 11; i++)
 	for (i = 0; i < 11; i++)
 		name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
 		name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
@@ -740,8 +767,6 @@ int fat_mkdir(fat_fs_t *fs, unsigned int parent_cluster, const char *name_8_3)
 
 
 	if (!fs || !fs->mounted || !name_8_3)
 	if (!fs || !fs->mounted || !name_8_3)
 		return -1;
 		return -1;
-	if (!fat_writable(fs))
-		return -1;
 
 
 	for (i = 0; i < 11; i++)
 	for (i = 0; i < 11; i++)
 		name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
 		name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
@@ -784,6 +809,12 @@ int fat_mkdir(fat_fs_t *fs, unsigned int parent_cluster, const char *name_8_3)
 		sector_buf[43] = 0x10;
 		sector_buf[43] = 0x10;
 		sector_buf[58] = (unsigned char)(parent_cluster & 0xFF);
 		sector_buf[58] = (unsigned char)(parent_cluster & 0xFF);
 		sector_buf[59] = (unsigned char)((parent_cluster >> 8) & 0xFF);
 		sector_buf[59] = (unsigned char)((parent_cluster >> 8) & 0xFF);
+		if (fs->type == FAT32) {   /* high halves of "." and ".." */
+			sector_buf[20] = (unsigned char)((cluster >> 16) & 0xFF);
+			sector_buf[21] = (unsigned char)((cluster >> 24) & 0xFF);
+			sector_buf[52] = (unsigned char)((parent_cluster >> 16) & 0xFF);
+			sector_buf[53] = (unsigned char)((parent_cluster >> 24) & 0xFF);
+		}
 		if (fat_write_sector(fs, base) != 0)
 		if (fat_write_sector(fs, base) != 0)
 			return -2;
 			return -2;
 	}
 	}
@@ -809,8 +840,6 @@ int fat_rmdir(fat_fs_t *fs, unsigned int parent_cluster, const char *name_8_3)
 
 
 	if (!fs || !fs->mounted || !name_8_3)
 	if (!fs || !fs->mounted || !name_8_3)
 		return -1;
 		return -1;
-	if (!fat_writable(fs))
-		return -1;
 
 
 	for (i = 0; i < 11; i++)
 	for (i = 0; i < 11; i++)
 		name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
 		name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'

+ 2 - 1
fat.h

@@ -29,6 +29,7 @@ typedef struct {
 	unsigned int data_start;        /* LBA of first data cluster */
 	unsigned int data_start;        /* LBA of first data cluster */
 	unsigned int total_sectors;
 	unsigned int total_sectors;
 	unsigned int max_cluster;
 	unsigned int max_cluster;
+	unsigned int next_free;         /* allocation hint (FAT32: from FSInfo) */
 	char label[12];                /* trimmed 8.3 volume label + NUL */
 	char label[12];                /* trimmed 8.3 volume label + NUL */
 	int mounted;
 	int mounted;
 } fat_fs_t;
 } fat_fs_t;
@@ -44,7 +45,7 @@ int fat_list_dir(fat_fs_t *fs, unsigned int dir_cluster, fat_root_callback_t cal
 int fat_read_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_3,
 int fat_read_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_3,
 		     void *buf, unsigned int max_len);
 		     void *buf, unsigned int max_len);
 
 
-/* Write / delete / mkdir / rmdir. Return -1 if FAT32 (read-only). */
+/* Write / delete / mkdir / rmdir (FAT12/16/32). */
 int fat_write_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_3,
 int fat_write_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_3,
 		      const void *buf, unsigned int size);
 		      const void *buf, unsigned int size);
 int fat_delete_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_3);
 int fat_delete_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_3);

+ 68 - 21
fatformat.c

@@ -1,10 +1,33 @@
 /* Create a fresh FAT filesystem on a block device.
 /* Create a fresh FAT filesystem on a block device.
- * Floppy: FAT12 superfloppy (no MBR). IDE: MBR + single FAT16/32 partition. */
+ * Floppy: FAT12 superfloppy (no MBR). IDE/SATA: MBR + single FAT16/32 partition. */
 
 
 #include "fatformat.h"
 #include "fatformat.h"
+#include "mm.h"
 
 
 #define SEC_SIZE 512
 #define SEC_SIZE 512
 
 
+/* Zero-fill chunk size for the FAT/reserved/root areas.  Large disks would
+ * otherwise need millions of single-sector writes (a FAT32 on a 1 TB disk has
+ * ~2 M FAT sectors). */
+#define ZERO_CHUNK_PAGES 32
+
+/* Write `count` zero sectors at `lba` in `chunk`-sector pieces. */
+static int write_zeros(blk_dev_t *d, unsigned int lba, unsigned int count,
+		       const unsigned char *buf, unsigned int chunk)
+{
+	unsigned int done = 0;
+
+	while (done < count) {
+		unsigned int n = count - done;
+		if (n > chunk)
+			n = chunk;
+		if (blk_write(d, lba + done, buf, n) != 0)
+			return -1;
+		done += n;
+	}
+	return 0;
+}
+
 static void put16(unsigned char *p, unsigned int v)
 static void put16(unsigned char *p, unsigned int v)
 {
 {
 	p[0] = (unsigned char)(v & 0xFF);
 	p[0] = (unsigned char)(v & 0xFF);
@@ -147,6 +170,8 @@ int fat_format(blk_dev_t *d)
 	static unsigned char sec[SEC_SIZE];
 	static unsigned char sec[SEC_SIZE];
 	static unsigned char fat_sec[SEC_SIZE];
 	static unsigned char fat_sec[SEC_SIZE];
 	static unsigned char zero[SEC_SIZE];
 	static unsigned char zero[SEC_SIZE];
+	unsigned char *zbuf;
+	unsigned int zchunk, zpages;
 	unsigned int fs_sectors, part_start;
 	unsigned int fs_sectors, part_start;
 	unsigned int type, spc, root_entries;
 	unsigned int type, spc, root_entries;
 	unsigned int reserved, num_fats = 2, sec_per_fat, root_dir_sectors;
 	unsigned int reserved, num_fats = 2, sec_per_fat, root_dir_sectors;
@@ -190,7 +215,27 @@ int fat_format(blk_dev_t *d)
 
 
 	zero_sector(zero);
 	zero_sector(zero);
 
 
-	/* 1. MBR (IDE only). */
+	/* Bulk zero buffer (a 1 TB FAT32 has ~2 M FAT sectors; single-sector
+	 * writes would take minutes).  Real disks only -- floppies keep the
+	 * 512-byte path. */
+	zbuf = zero;
+	zchunk = 1;
+	zpages = 0;
+	if (!is_floppy) {
+		zbuf = (unsigned char *)mm_alloc_pages(ZERO_CHUNK_PAGES);
+		if (zbuf) {
+			unsigned int k;
+			for (k = 0; k < ZERO_CHUNK_PAGES * PAGE_SIZE; k++)
+				zbuf[k] = 0;
+			zchunk = ZERO_CHUNK_PAGES * (PAGE_SIZE / SEC_SIZE);
+			zpages = ZERO_CHUNK_PAGES;
+		} else {
+			zbuf = zero;
+			zchunk = 1;
+		}
+	}
+
+	/* 1. MBR (IDE/SATA only). */
 	if (!is_floppy) {
 	if (!is_floppy) {
 		unsigned char *e;
 		unsigned char *e;
 		zero_sector(sec);
 		zero_sector(sec);
@@ -210,10 +255,8 @@ int fat_format(blk_dev_t *d)
 	/* 2. Reserved area + boot sector (+ FSInfo/backup for FAT32). */
 	/* 2. Reserved area + boot sector (+ FSInfo/backup for FAT32). */
 	build_boot_sector(sec, type, spc, reserved, num_fats, root_entries,
 	build_boot_sector(sec, type, spc, reserved, num_fats, root_entries,
 			  sec_per_fat, fs_sectors, part_start, media);
 			  sec_per_fat, fs_sectors, part_start, media);
-	for (i = 0; i < reserved; i++) {
-		if (blk_write(d, part_start + i, zero, 1) != 0)
-			return -1;
-	}
+	if (write_zeros(d, part_start, reserved, zbuf, zchunk) != 0)
+		return -1;
 	if (blk_write(d, part_start, sec, 1) != 0)
 	if (blk_write(d, part_start, sec, 1) != 0)
 		return -1;
 		return -1;
 	if (type == 32) {
 	if (type == 32) {
@@ -224,28 +267,32 @@ int fat_format(blk_dev_t *d)
 			return -1;
 			return -1;
 	}
 	}
 
 
-	/* 3. FATs (two copies). */
+	/* 3. FATs (two copies).  The first sector carries the media byte and
+	 * the end-of-chain markers; the rest must be zero. */
 	build_fat_first_sector(fat_sec, type, media);
 	build_fat_first_sector(fat_sec, type, media);
 	for (i = 0; i < num_fats; i++) {
 	for (i = 0; i < num_fats; i++) {
-		unsigned int j;
-		for (j = 0; j < sec_per_fat; j++) {
-			const unsigned char *s = (j == 0) ? fat_sec : zero;
-			if (blk_write(d, part_start + fat_start + i * sec_per_fat + j,
-				      s, 1) != 0)
-				return -1;
-		}
+		unsigned int base = part_start + fat_start + i * sec_per_fat;
+		if (blk_write(d, base, fat_sec, 1) != 0)
+			return -1;
+		if (sec_per_fat > 1 &&
+		    write_zeros(d, base + 1, sec_per_fat - 1, zbuf, zchunk) != 0)
+			return -1;
 	}
 	}
 
 
 	/* 4. Root directory (FAT12/16) or root cluster (FAT32). */
 	/* 4. Root directory (FAT12/16) or root cluster (FAT32). */
 	if (type == 32) {
 	if (type == 32) {
-		/* Root dir is cluster 2 at the start of the data region. */
-		for (i = 0; i < spc; i++)
-			if (blk_write(d, part_start + data_start + i, zero, 1) != 0)
-				return -1;
+		if (write_zeros(d, part_start + data_start, spc, zbuf, zchunk) != 0)
+			return -1;
 	} else {
 	} else {
-		for (i = 0; i < root_dir_sectors; i++)
-			if (blk_write(d, part_start + root_start + i, zero, 1) != 0)
-				return -1;
+		if (write_zeros(d, part_start + root_start, root_dir_sectors,
+				zbuf, zchunk) != 0)
+			return -1;
+	}
+
+	if (zpages) {
+		unsigned int k;
+		for (k = 0; k < zpages; k++)
+			mm_free_page((unsigned int)zbuf + k * PAGE_SIZE);
 	}
 	}
 
 
 	return 0;
 	return 0;

+ 10 - 1
format.c

@@ -4,6 +4,7 @@
 #include "fatformat.h"
 #include "fatformat.h"
 #include "io.h"
 #include "io.h"
 #include "vga.h"
 #include "vga.h"
+#include "vfs.h"
 
 
 static void list_devices(void)
 static void list_devices(void)
 {
 {
@@ -55,7 +56,7 @@ void program_format_main(const char *arg)
 		return;
 		return;
 	}
 	}
 
 
-	if (d->kind == BLK_IDE && !d->present) {
+	if (d->kind != BLK_FLOPPY && !d->present) {
 		io_putstr("format: ");
 		io_putstr("format: ");
 		io_putstr(d->name);
 		io_putstr(d->name);
 		io_putstr(": not present\n");
 		io_putstr(": not present\n");
@@ -67,15 +68,20 @@ void program_format_main(const char *arg)
 	io_putstr("? this destroys all data (y/n) ");
 	io_putstr("? this destroys all data (y/n) ");
 	vga_fb_flush();
 	vga_fb_flush();
 
 
+	/* Keep the WM from draining the keyboard ring while we poll for the
+	 * confirmation (works for PS/2 and USB keyboards alike). */
+	io_set_raw_keyboard(1);
 	for (;;) {
 	for (;;) {
 		k = io_poll_key_with_hw();
 		k = io_poll_key_with_hw();
 		if (k == 'y' || k == 'Y')
 		if (k == 'y' || k == 'Y')
 			break;
 			break;
 		if (k == 'n' || k == 'N') {
 		if (k == 'n' || k == 'N') {
+			io_set_raw_keyboard(0);
 			io_putstr("aborted\n");
 			io_putstr("aborted\n");
 			return;
 			return;
 		}
 		}
 	}
 	}
+	io_set_raw_keyboard(0);
 	io_putchar('\n');
 	io_putchar('\n');
 
 
 	io_putstr("formatting ");
 	io_putstr("formatting ");
@@ -88,5 +94,8 @@ void program_format_main(const char *arg)
 		return;
 		return;
 	}
 	}
 
 
+	/* Make the fresh filesystem immediately usable (save/ls/cat). */
+	vfs_mount_all();
+
 	io_putstr("done\n");
 	io_putstr("done\n");
 }
 }

+ 12 - 0
io.c

@@ -334,3 +334,15 @@ void io_inject_scancode(unsigned char sc)
 	}
 	}
 	__asm__ volatile ("pushl %0; popfl" : : "r"(flags));
 	__asm__ volatile ("pushl %0; popfl" : : "r"(flags));
 }
 }
+
+static int kbd_raw;
+
+void io_set_raw_keyboard(int on)
+{
+	kbd_raw = on;
+}
+
+int io_keyboard_raw(void)
+{
+	return kbd_raw;
+}

+ 6 - 0
io.h

@@ -42,4 +42,10 @@ unsigned char io_last_scancode(void);
    it is then decoded by the normal io_poll_key path. */
    it is then decoded by the normal io_poll_key path. */
 void io_inject_scancode(unsigned char sc);
 void io_inject_scancode(unsigned char sc);
 
 
+/* While set, the WM stops draining the keyboard ring: an inline command
+   (e.g. `format`'s confirmation) is polling for keys itself.  The WM still
+   runs and the USB backend still queues scancodes. */
+void io_set_raw_keyboard(int on);
+int io_keyboard_raw(void);
+
 #endif
 #endif

+ 1 - 1
kexec.h

@@ -8,7 +8,7 @@ void program_kexec_main(void);
  * (the kernel region 0x7E00..0x42000).  Below the VGA window; see AGENTS.md. */
  * (the kernel region 0x7E00..0x42000).  Below the VGA window; see AGENTS.md. */
 #define KEXEC_STAGE 0x58000
 #define KEXEC_STAGE 0x58000
 #define KEXEC_LOAD  0x7E00
 #define KEXEC_LOAD  0x7E00
-#define KEXEC_MAX   0x38200
+#define KEXEC_MAX   0x3A200
 
 
 /* Copy the staged image (KEXEC_STAGE, `size` bytes) over the kernel region,
 /* Copy the staged image (KEXEC_STAGE, `size` bytes) over the kernel region,
  * relocate the trampoline and jump to `entry`.  Never returns. */
  * relocate the trampoline and jump to `entry`.  Never returns. */

+ 1 - 1
mkdir.c

@@ -18,7 +18,7 @@ void program_mkdir_main(const char *arg)
 	}
 	}
 
 
 	if (vfs_resolve(arg, &fs, &fs_path) != 0) {
 	if (vfs_resolve(arg, &fs, &fs_path) != 0) {
-		io_putstr("mkdir: read-only filesystem\n");
+		io_putstr("mkdir: unknown device or invalid path\n");
 		return;
 		return;
 	}
 	}
 
 

+ 1 - 1
rm.c

@@ -18,7 +18,7 @@ void program_rm_main(const char *arg)
 	}
 	}
 
 
 	if (vfs_resolve(arg, &fs, &fs_path) != 0) {
 	if (vfs_resolve(arg, &fs, &fs_path) != 0) {
-		io_putstr("rm: read-only filesystem\n");
+		io_putstr("rm: unknown device or invalid path\n");
 		return;
 		return;
 	}
 	}
 
 

+ 1 - 1
rmdir.c

@@ -18,7 +18,7 @@ void program_rmdir_main(const char *arg)
 	}
 	}
 
 
 	if (vfs_resolve(arg, &fs, &fs_path) != 0) {
 	if (vfs_resolve(arg, &fs, &fs_path) != 0) {
-		io_putstr("rmdir: read-only filesystem\n");
+		io_putstr("rmdir: unknown device or invalid path\n");
 		return;
 		return;
 	}
 	}
 
 

+ 1 - 1
save.c

@@ -49,7 +49,7 @@ void program_save_main(const char *arg)
 		text_len++;
 		text_len++;
 
 
 	if (vfs_resolve(fn, &fs, &fs_path) != 0) {
 	if (vfs_resolve(fn, &fs, &fs_path) != 0) {
-		io_putstr("save: read-only filesystem\n");
+		io_putstr("save: unknown device or invalid path\n");
 		return;
 		return;
 	}
 	}
 
 

+ 36 - 14
storage.c

@@ -1,5 +1,6 @@
 /* storage command: refresh the filesystem cache and report devices + mounts. */
 /* storage command: refresh the filesystem cache and report devices + mounts. */
 
 
+#include "ahci.h"
 #include "blkdev.h"
 #include "blkdev.h"
 #include "io.h"
 #include "io.h"
 #include "vfs.h"
 #include "vfs.h"
@@ -20,20 +21,21 @@ static void put_dec(unsigned int n)
 	io_putstr(buf + i);
 	io_putstr(buf + i);
 }
 }
 
 
-static void put_size(unsigned int bytes)
+/* Print a size given in 512-byte sectors (shifts avoid 32-bit overflow). */
+static void put_size_sectors(unsigned int sectors)
 {
 {
-	if (bytes >= 1024u * 1024u * 1024u) {
-		unsigned int gb = bytes / (1024u * 1024u * 1024u);
-		unsigned int tenths = (bytes % (1024u * 1024u * 1024u)) * 10u / (1024u * 1024u * 1024u);
+	if (sectors >= 2u * 1024u * 1024u) {           /* >= 1 GiB */
+		unsigned int gb = sectors >> 21;
+		unsigned int tenths = ((sectors & 0x1FFFFFu) * 10u) >> 21;
 		put_dec(gb);
 		put_dec(gb);
 		if (tenths) {
 		if (tenths) {
 			io_putchar('.');
 			io_putchar('.');
 			put_dec(tenths);
 			put_dec(tenths);
 		}
 		}
 		io_putstr(" GB");
 		io_putstr(" GB");
-	} else if (bytes >= 1024u * 1024u) {
-		unsigned int mb = bytes / (1024u * 1024u);
-		unsigned int tenths = (bytes % (1024u * 1024u)) * 10u / (1024u * 1024u);
+	} else if (sectors >= 2048u) {                 /* >= 1 MiB */
+		unsigned int mb = sectors >> 11;
+		unsigned int tenths = ((sectors & 0x7FFu) * 10u) >> 11;
 		put_dec(mb);
 		put_dec(mb);
 		if (tenths) {
 		if (tenths) {
 			io_putchar('.');
 			io_putchar('.');
@@ -41,7 +43,7 @@ static void put_size(unsigned int bytes)
 		}
 		}
 		io_putstr(" MB");
 		io_putstr(" MB");
 	} else {
 	} else {
-		put_dec(bytes / 1024u);
+		put_dec(sectors / 2u);
 		io_putstr(" KB");
 		io_putstr(" KB");
 	}
 	}
 }
 }
@@ -62,7 +64,7 @@ static void report_fs(const char *name, const fat_fs_t *fs)
 	io_putstr(": ");
 	io_putstr(": ");
 	io_putstr(fat_name(fs->type));
 	io_putstr(fat_name(fs->type));
 	io_putchar(' ');
 	io_putchar(' ');
-	put_size(fs->total_sectors * fs->bytes_per_sector);
+	put_size_sectors(fs->total_sectors);
 	if (fs->label[0]) {
 	if (fs->label[0]) {
 		io_putstr("  \"");
 		io_putstr("  \"");
 		io_putstr(fs->label);
 		io_putstr(fs->label);
@@ -96,22 +98,42 @@ void program_storage_main(void)
 		blk_dev_t *d = &blk_devs[i];
 		blk_dev_t *d = &blk_devs[i];
 		int found = 0;
 		int found = 0;
 
 
-		if (d->kind == BLK_IDE && !d->present) {
+		if (d->kind != BLK_FLOPPY && !d->present) {
 			io_putstr(d->name);
 			io_putstr(d->name);
 			io_putstr(": not present\n");
 			io_putstr(": not present\n");
 			continue;
 			continue;
 		}
 		}
 
 
-		for (j = 0; j < vfs_mount_count; j++) {
+		for (j = 0; j < vfs_mount_count; j++)
 			if (mount_belongs_to(vfs_mounts[j].name, d->name)) {
 			if (mount_belongs_to(vfs_mounts[j].name, d->name)) {
-				report_fs(vfs_mounts[j].name, &vfs_mounts[j].fs);
 				found = 1;
 				found = 1;
+				break;
 			}
 			}
-		}
 
 
-		if (!found) {
+		/* Real disks get a header line with model and size, floppies
+		 * keep the old single-line output. */
+		if (d->kind != BLK_FLOPPY) {
+			io_putstr(d->name);
+			io_putstr(": ");
+			if (d->model[0]) {
+				io_putstr(d->model);
+				io_putchar(' ');
+			}
+			put_size_sectors(d->total_sectors);
+			if (!found)
+				io_putstr("  no valid FAT filesystem");
+			io_putchar('\n');
+		} else if (!found) {
 			io_putstr(d->name);
 			io_putstr(d->name);
 			io_putstr(": no valid FAT filesystem\n");
 			io_putstr(": no valid FAT filesystem\n");
 		}
 		}
+
+		if (found)
+			for (j = 0; j < vfs_mount_count; j++)
+				if (mount_belongs_to(vfs_mounts[j].name, d->name))
+					report_fs(vfs_mounts[j].name, &vfs_mounts[j].fs);
 	}
 	}
+
+	if (!ahci_controller_present())
+		io_putstr("sata: no AHCI controller\n");
 }
 }

+ 13 - 11
vfs.c

@@ -23,20 +23,22 @@ static void put_dec(unsigned int n)
 	io_putstr(buf + i);
 	io_putstr(buf + i);
 }
 }
 
 
-static void put_size(unsigned int bytes)
+/* Print a size given in 512-byte sectors.  Uses shifts (not bytes) so a
+ * >4 GB filesystem/disk does not overflow 32-bit arithmetic. */
+static void put_size_sectors(unsigned int sectors)
 {
 {
-	if (bytes >= 1024u * 1024u * 1024u) {
-		unsigned int gb = bytes / (1024u * 1024u * 1024u);
-		unsigned int tenths = (bytes % (1024u * 1024u * 1024u)) * 10u / (1024u * 1024u * 1024u);
+	if (sectors >= 2u * 1024u * 1024u) {           /* >= 1 GiB */
+		unsigned int gb = sectors >> 21;
+		unsigned int tenths = ((sectors & 0x1FFFFFu) * 10u) >> 21;
 		put_dec(gb);
 		put_dec(gb);
 		if (tenths) {
 		if (tenths) {
 			io_putchar('.');
 			io_putchar('.');
 			put_dec(tenths);
 			put_dec(tenths);
 		}
 		}
 		io_putstr(" GB");
 		io_putstr(" GB");
-	} else if (bytes >= 1024u * 1024u) {
-		unsigned int mb = bytes / (1024u * 1024u);
-		unsigned int tenths = (bytes % (1024u * 1024u)) * 10u / (1024u * 1024u);
+	} else if (sectors >= 2048u) {                 /* >= 1 MiB */
+		unsigned int mb = sectors >> 11;
+		unsigned int tenths = ((sectors & 0x7FFu) * 10u) >> 11;
 		put_dec(mb);
 		put_dec(mb);
 		if (tenths) {
 		if (tenths) {
 			io_putchar('.');
 			io_putchar('.');
@@ -44,7 +46,7 @@ static void put_size(unsigned int bytes)
 		}
 		}
 		io_putstr(" MB");
 		io_putstr(" MB");
 	} else {
 	} else {
-		put_dec(bytes / 1024u);
+		put_dec(sectors / 2u);
 		io_putstr(" KB");
 		io_putstr(" KB");
 	}
 	}
 }
 }
@@ -100,7 +102,7 @@ void vfs_mount_all(void)
 	for (i = 0; i < blk_dev_count; i++) {
 	for (i = 0; i < blk_dev_count; i++) {
 		blk_dev_t *d = &blk_devs[i];
 		blk_dev_t *d = &blk_devs[i];
 
 
-		if (d->kind == BLK_IDE && !d->present)
+		if (d->kind != BLK_FLOPPY && !d->present)
 			continue;
 			continue;
 
 
 		if (d->kind == BLK_FLOPPY) {
 		if (d->kind == BLK_FLOPPY) {
@@ -108,7 +110,7 @@ void vfs_mount_all(void)
 			continue;
 			continue;
 		}
 		}
 
 
-		/* IDE: MBR -> FAT partitions, else superfloppy. */
+		/* IDE/SATA: MBR -> FAT partitions, else superfloppy. */
 		{
 		{
 			mbr_part_t parts[4];
 			mbr_part_t parts[4];
 			int n, j;
 			int n, j;
@@ -156,7 +158,7 @@ void vfs_list_root(void)
 		io_putstr("  ");
 		io_putstr("  ");
 		io_putstr(fat_name(fs->type));
 		io_putstr(fat_name(fs->type));
 		io_putchar(' ');
 		io_putchar(' ');
-		put_size(fs->total_sectors * fs->bytes_per_sector);
+		put_size_sectors(fs->total_sectors);
 		if (fs->label[0]) {
 		if (fs->label[0]) {
 			io_putstr("  \"");
 			io_putstr("  \"");
 			io_putstr(fs->label);
 			io_putstr(fs->label);

+ 1 - 1
vfs.h

@@ -12,7 +12,7 @@ typedef struct {
 	int mounted;
 	int mounted;
 } vfs_mount_t;
 } vfs_mount_t;
 
 
-#define VFS_MAX_MOUNTS 12
+#define VFS_MAX_MOUNTS 24
 #define VFS_PATH_MAX   64
 #define VFS_PATH_MAX   64
 extern vfs_mount_t vfs_mounts[VFS_MAX_MOUNTS];
 extern vfs_mount_t vfs_mounts[VFS_MAX_MOUNTS];
 extern unsigned int vfs_mount_count;
 extern unsigned int vfs_mount_count;

+ 3 - 0
wm.c

@@ -387,6 +387,9 @@ static void wm_handle_keyboard(void)
 	int handled = 0;
 	int handled = 0;
 	int n;
 	int n;
 
 
+	if (io_keyboard_raw())
+		return;   /* an inline command is polling for keys itself */
+
 	if (!fw || !fw->term)
 	if (!fw || !fw->term)
 		return;
 		return;