Răsfoiți Sursa

Read the legacy rootfs from the first 1.44 MB

The X201 still hung on the rootfs header read at LBA 112455.  Put the
rootfs partition immediately behind the kernel (LBA 532) and shrink
usb.img to 9 MB, so the header and the 242 KB gzip pack sit inside the
first 1.44 MB that even a CHS-only USB BIOS can read.  Keep the ESP at
LBA 16916 (17 MB image), print the drive/LBA/EDD mode on screen and a
dot after the rootfs header, and keep the EDD LBA probe plus the CHS
fallback for floppies.
Johannes Zellner 2 săptămâni în urmă
părinte
comite
ce7cb221d2
4 a modificat fișierele cu 76 adăugiri și 31 ștergeri
  1. 13 9
      AGENTS.md
  2. 10 10
      Makefile
  3. 11 11
      mbr.S
  4. 42 1
      vesa_stage.S

+ 13 - 9
AGENTS.md

@@ -47,12 +47,14 @@ make clean
   targets, an OVMF firmware (`OVMF=/path/to/OVMF.4m.fd` overrides the default).
   `make all` does not build the UEFI image, so a machine without mingw still
   builds the legacy floppy/USB images normally.
-- `usb.img` is 64 MB: partition 1 (type `0x06`, LBA 63) is the raw kernel
-  blob and partition 2 (type `0x7F`, LBA 112455) is the 8 MB rootfs image
-  (`cmds.img`, at the old dummy partition-2 slot below the 64 MB mark).  The
-  boot sector's `pack_lba` field points the VESA stage at partition 2.
-  `usb-efi.img` (72 MB) adds an 8 MB FAT16 ESP as partition 3 (type `0xEF`,
-  LBA 131072); `BOOTX64.EFI` embeds the kernel + gz pack.
+- `usb.img` is 9 MB: partition 1 (type `0x06`, LBA 63, 469 sectors) is the
+  boot sector + kernel and partition 2 (type `0x7F`, LBA 532) is the 8 MB
+  rootfs image (`cmds.img`), immediately behind the kernel.  The header and
+  the 242 KB gzip pack therefore live in the first 1.44 MB, which even a
+  CHS-only USB BIOS can read.  The boot sector's `pack_lba` field points the
+  VESA stage at partition 2.  `usb-efi.img` (17 MB) adds an 8 MB FAT16 ESP
+  as partition 3 (type `0xEF`, LBA 16916); `BOOTX64.EFI` embeds the kernel +
+  gz pack.
   `make flash` writes the dual image; `make flash FLASH_IMG=usb.img` writes
   the legacy-only image.  One BPB byte is cleared in the dual image (disk
   offset `0x7E16`, FATSize16): without that, UEFI firmware scans partition 1,
@@ -72,7 +74,9 @@ make clean
    (≥16 bpp, LFB, fits VRAM, ≤1280px), writes mode info to `0x600` and the
    12-dword `struct boot_info` (magic/checksum/rsdp/flags/pack range) to
    `0x500`, enters protected mode and jumps to `kernel_entry` (`entry.S`,
-   re-points ESP to the current stack top, then `kernel_main`).
+   re-points ESP to the current stack top, then `kernel_main`).  The stage
+   prints `Loading rootfs [d=<drive> lba=<hex> e=<0|1>]...`, then a dot
+   after the header and one per 32 KB chunk, so a BIOS hang is localised.
 3. `kernel.c` — `kernel_main` zeroes BSS then inits serial → memory/paging →
    ACPI/MADT → I/O → idle → processes → timer (PIT; 1 kHz busy, 100 Hz idle + IDT) → SMP AP
    bring-up → mouse → VGA shadow buffer → terminal.
@@ -1097,5 +1101,5 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     IOAPIC setup: OVMF delivers the ISA IRQs through the 8259 path, but a
     platform that routes them through the IOAPIC may need a mask-all step after
     ACPI init.  UEFI runtime services are not used, so `reboot`/`poweroff` still
-    use the legacy ports.  The image is 72 MB: the 64 MB kernel partition
-    (with the rootfs at LBA 112455) and the 8 MB ESP.
+    use the legacy ports.  The image is 17 MB: the 9 MB kernel + rootfs disk
+    and the 8 MB ESP.

+ 10 - 10
Makefile

@@ -364,11 +364,11 @@ BPB_HIDDEN_OFFSET = 28
 BPB_TOTAL_OFFSET = 32
 
 # USB rootfs partition (type 0x7F) and UEFI ESP (type 0xEF).  The rootfs
-# lives at the old dummy partition-2 start (LBA 112455, ~55 MB), below the
-# 64 MB mark that some legacy USB BIOSes handle badly.
-ROOTFS_LBA     = 112455
+# sits immediately behind the kernel (LBA 532), so even a CHS-only USB BIOS
+# finds the header and the 242 KB gzip pack inside the first 1.44 MB.
+ROOTFS_LBA     = 532
 ROOTFS_SECTORS = 16384
-ESP_LBA        = 131072
+ESP_LBA        = 16916
 
 # Patch a little-endian 16/32-bit value into a boot-sector copy.  Usage:
 #   $(call patch16,file,offset,value)
@@ -402,13 +402,13 @@ usb.img: mbr.bin boot.bin kernel.bin cmds.img
 	$(call patch32,$@.boot.tmp,$(ROOTFS_LBA),$(PACK_LBA_OFFSET)); \
 	printf '\xff\x00' | dd of=$@.boot.tmp bs=1 seek=$(BPB_HEADS_OFFSET) conv=notrunc 2>/dev/null; \
 	printf '\x3f\x00\x00\x00' | dd of=$@.boot.tmp bs=1 seek=$(BPB_HIDDEN_OFFSET) conv=notrunc 2>/dev/null; \
-	printf '\x08\xb7\x01\x00' | dd of=$@.boot.tmp bs=1 seek=$(BPB_TOTAL_OFFSET) conv=notrunc 2>/dev/null; \
+	printf '\xd5\x01\x00\x00' | dd of=$@.boot.tmp bs=1 seek=$(BPB_TOTAL_OFFSET) conv=notrunc 2>/dev/null; \
 	cat mbr.bin > $@; \
 	dd if=/dev/zero bs=512 count=62 2>/dev/null >> $@; \
 	cat $@.boot.tmp kernel.bin >> $@; \
-	truncate -s 64M $@; \
+	truncate -s 9M $@; \
 	dd if=cmds.img of=$@ bs=512 seek=$(ROOTFS_LBA) conv=notrunc 2>/dev/null; \
-	printf '\x00\x00\x01\x07\x7f\x05\x04\x08\x47\xb7\x01\x00\x00\x40\x00\x00' | \
+	printf '\x00\x08\x1d\x00\x7f\x0d\x20\x01\x14\x02\x00\x00\x00\x40\x00\x00' | \
 		dd of=$@ bs=1 seek=462 conv=notrunc 2>/dev/null; \
 	rm -f $@.boot.tmp
 
@@ -474,9 +474,9 @@ usb-efi.img: usb.img esp.img
 	# the kernel at LBA 64.  Clear BPB FATSize16 so the firmware rejects the
 	# fake volume; boot.S and the BIOS never read that field.
 	printf '\x00\x00' | dd of=$@ bs=1 seek=$$((63 * 512 + 0x16)) conv=notrunc 2>/dev/null
-	# Partition 3: ESP at LBA 131072, type 0xEF, 8 MB (partition 2 is the
-	# rootfs at LBA 112455 written by usb.img).
-	printf '\x00\xfe\xff\xff\xef\xfe\xff\xff\x00\x00\x02\x00\x00\x40\x00\x00' | \
+	# Partition 3: ESP at LBA 16916, type 0xEF, 8 MB (partition 2 is the
+	# rootfs at LBA 532 written by usb.img).
+	printf '\x00\xfe\xff\xff\xef\xfe\xff\xff\x14\x42\x00\x00\x00\x40\x00\x00' | \
 		dd of=$@ bs=1 seek=478 conv=notrunc 2>/dev/null
 	dd if=esp.img of=$@ bs=512 seek=$(ESP_LBA) conv=notrunc 2>/dev/null
 	truncate -s $$(( ($(ESP_LBA) + $(ESP_SECTORS)) * 512 )) $@

+ 11 - 11
mbr.S

@@ -146,22 +146,22 @@ part_table:
 	.byte 0x80              /* bootable */
 	.byte 0x01, 0x01, 0x00  /* CHS start: C=0 H=1 S=1 (LBA 63) */
 	.byte 0x06              /* type: FAT16 */
-	.byte 0xFE, 0x3F, 0x06  /* CHS end: C=6 H=254 S=63 (LBA 112454) */
+	.byte 0x08, 0x1C, 0x00  /* CHS end: C=0 H=8 S=28 (LBA 531) */
 	.long 63                /* LBA start (boot sector) */
-	.long 112392            /* sector count */
+	.long 469               /* boot sector + kernel sectors */
 
 	/* Partition 2: dummy in the bare MBR.  usb.img replaces this entry
-	 * with the rootfs partition (same start, LBA 112455); keeping a second
-	 * valid entry makes the stick look like a USB hard disk to old AMI
-	 * BIOSes (e.g. the ASUS AT3IONT-I), which classify a single-partition
-	 * MBR as a USB-ZIP disk and never offer it.  Only partition 1 is
-	 * active. */
+	 * with the rootfs partition (same start, LBA 532, immediately behind
+	 * the kernel); keeping a second valid entry makes the stick look like
+	 * a USB hard disk to old AMI BIOSes (e.g. the ASUS AT3IONT-I), which
+	 * classify a single-partition MBR as a USB-ZIP disk and never offer
+	 * it.  Only partition 1 is active. */
 	.byte 0x00              /* not bootable */
-	.byte 0x00, 0x01, 0x07  /* CHS start: C=7 H=0 S=1 (LBA 112455) */
+	.byte 0x08, 0x1d, 0x00  /* CHS start: C=0 H=8 S=29 (LBA 532) */
 	.byte 0x21              /* type: dummy */
-	.byte 0x28, 0x20, 0x08  /* CHS end: C=8 H=40 S=32 (LBA 131071) */
-	.long 112455
-	.long 18617
+	.byte 0x0d, 0x20, 0x01  /* CHS end: C=1 H=13 S=32 (LBA 16915) */
+	.long 532
+	.long 16384
 	.space 32, 0            /* 2 empty entries */
 
 	/* Boot signature at offset 510 */

+ 42 - 1
vesa_stage.S

@@ -98,6 +98,21 @@ e820_done:
 
 	movw  $msg_pack, %si
 	call  print_str
+	movzbl stage_drive, %eax
+	call  print_hex32
+	movw  $msg_lba, %si
+	call  print_str
+	movl  pack_lba, %eax
+	call  print_hex32
+	movw  $msg_e, %si
+	call  print_str
+	movzbl use_lba, %eax
+	addl  $'0', %eax
+	movb  $0x0E, %ah
+	movb  $0x00, %bh
+	int   $0x10
+	movw  $msg_end, %si
+	call  print_str
 
 	movl  0x7C42, %eax            /* boot-sector pack_lba field */
 	movl  %eax, pack_lba
@@ -528,6 +543,29 @@ print_str:
 	popw  %ax
 	ret
 
+/* ---- Hex print (EAX, 8 digits) ----------------------------------------- */
+print_hex32:
+	movl  $8, %ecx
+	jmp   print_hex
+
+print_hex:
+	pushal
+	movl  %eax, %ebx
+1:	roll  $4, %ebx
+	movl  %ebx, %eax
+	andl  $0x0F, %eax
+	cmpl  $10, %eax
+	jb    2f
+	addl  $55, %eax
+	jmp   3f
+2:	addl  $48, %eax
+3:	movb  $0x0E, %ah
+	movb  $0x00, %bh
+	int   $0x10
+	loop  1b
+	popal
+	ret
+
 /* ---- Boot-info block at 0x500 (bootinfo.h) ----------------------------
  * twelve dwords: magic, version, checksum, rsdp, flags, pack_addr,
  * pack_size, reserved[5]; the checksum makes the sum zero. */
@@ -631,7 +669,10 @@ rd_off:         .word 0
 rd_left:        .word 0
 rd_tries:       .word 0
 
-msg_pack:       .asciz "Loading rootfs... "
+msg_pack:       .asciz "Loading rootfs [d="
+msg_lba:        .asciz " lba="
+msg_e:          .asciz " e="
+msg_end:        .asciz "]... "
 msg_dot:        .asciz "."
 msg_ok:         .asciz " ok\r\n"
 msg_noroom:     .asciz " skipped (no room)\r\n"