Browse Source

Load the legacy rootfs below the 64 MB mark

The X201-class legacy USB BIOS hangs reading the stick at LBA 131072
(exactly 64 MB).  Move the rootfs partition to the old dummy partition-2
slot at LBA 112455, shrink usb.img/usb-efi.img to 64/72 MB and put the
ESP back at LBA 131072.  Probe EDD at stage entry so a stick presented
as a floppy still uses LBA, keep LBA for fixed disks, and print a
progress dot after the rootfs header so a remaining hang can be
localised.  Rename the message to "Loading rootfs...".
Johannes Zellner 2 weeks ago
parent
commit
4bf9aa4a11
4 changed files with 47 additions and 22 deletions
  1. 8 7
      AGENTS.md
  2. 9 9
      Makefile
  3. 6 5
      mbr.S
  4. 24 1
      vesa_stage.S

+ 8 - 7
AGENTS.md

@@ -47,11 +47,12 @@ 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 72 MB: partition 1 (type `0x06`, LBA 63) is the raw kernel
-  blob and partition 2 (type `0x7F`, LBA 131072) is the 8 MB rootfs image
-  (`cmds.img`).  The boot sector's `pack_lba` field points the VESA stage at
-  partition 2.  `usb-efi.img` (80 MB) adds an 8 MB FAT16 ESP as partition 3
-  (type `0xEF`, LBA 147456); `BOOTX64.EFI` embeds the kernel + gz pack.
+- `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.
   `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,
@@ -1096,5 +1097,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 80 MB: the 64 MB kernel partition,
-    the 8 MB rootfs partition and the 8 MB ESP.
+    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.

+ 9 - 9
Makefile

@@ -363,10 +363,12 @@ BPB_HEADS_OFFSET = 26
 BPB_HIDDEN_OFFSET = 28
 BPB_TOTAL_OFFSET = 32
 
-# USB rootfs partition (type 0x7F) and UEFI ESP (type 0xEF).
-ROOTFS_LBA     = 131072
+# 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
 ROOTFS_SECTORS = 16384
-ESP_LBA        = 147456
+ESP_LBA        = 131072
 
 # Patch a little-endian 16/32-bit value into a boot-sector copy.  Usage:
 #   $(call patch16,file,offset,value)
@@ -406,8 +408,7 @@ usb.img: mbr.bin boot.bin kernel.bin cmds.img
 	cat $@.boot.tmp kernel.bin >> $@; \
 	truncate -s 64M $@; \
 	dd if=cmds.img of=$@ bs=512 seek=$(ROOTFS_LBA) conv=notrunc 2>/dev/null; \
-	truncate -s 72M $@; \
-	printf '\x00\xfe\xff\xff\x7f\xfe\xff\xff\x00\x00\x02\x00\x00\x40\x00\x00' | \
+	printf '\x00\x00\x01\x07\x7f\x05\x04\x08\x47\xb7\x01\x00\x00\x40\x00\x00' | \
 		dd of=$@ bs=1 seek=462 conv=notrunc 2>/dev/null; \
 	rm -f $@.boot.tmp
 
@@ -456,7 +457,6 @@ efi/tramp.bin: efi/tramp.elf
 BOOTX64.EFI: efi/main.o efi/payload.o
 	$(EFI_CC) $(EFI_LDFLAGS) -o $@ efi/main.o efi/payload.o
 
-ESP_LBA     = 147456
 ESP_SECTORS = 16384
 
 esp.img: BOOTX64.EFI
@@ -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 147456, type 0xEF, 8 MB (partition 2 is the
-	# rootfs at LBA 131072 written by usb.img).
-	printf '\x00\xfe\xff\xff\xef\xfe\xff\xff\x00\x40\x02\x00\x00\x40\x00\x00' | \
+	# 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' | \
 		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 )) $@

+ 6 - 5
mbr.S

@@ -150,11 +150,12 @@ part_table:
 	.long 63                /* LBA start (boot sector) */
 	.long 112392            /* sector count */
 
-	/* Partition 2: dummy. Old AMI BIOSes (e.g. the ASUS AT3IONT-I)
-	 * classify a single-partition MBR as a USB-ZIP disk, which is never
-	 * offered as a hard-disk boot option; a second valid entry makes the
-	 * stick look like a USB hard disk (same trick RMPrepUSB/Easy2Boot
-	 * use). It is never booted: only partition 1 is active. */
+	/* 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. */
 	.byte 0x00              /* not bootable */
 	.byte 0x00, 0x01, 0x07  /* CHS start: C=7 H=0 S=1 (LBA 112455) */
 	.byte 0x21              /* type: dummy */

+ 24 - 1
vesa_stage.S

@@ -35,6 +35,23 @@ _vesa_stage:
 	cld
 	movb  %dl, stage_drive
 
+	/* Ask the BIOS whether this drive supports EDD LBA reads.  USB sticks
+	 * are sometimes presented as floppies (DL=0); the CHS geometry used
+	 * below cannot reach a rootfs image there. */
+	movb  stage_drive, %dl
+	movb  $0x41, %ah
+	movw  $0x55AA, %bx
+	int   $0x13
+	jc    1f
+	cmpw  $0xAA55, %bx
+	jne   1f
+	testb $0x01, %cl
+	jz    1f
+	movb  $1, use_lba
+1:	xorw  %ax, %ax
+	movw  %ax, %ds
+	movw  %ax, %es
+
 	/* No UEFI boot-info block on the BIOS path (see bootinfo.h).  The boot
 	 * sector used 0x500 as its disk-address packet, so clear the magic;
 	 * write_bootinfo fills in the real block before the PM jump. */
@@ -110,6 +127,8 @@ e820_done:
 	cmpl  $PACK_MAX, %eax
 	ja    pack_fail
 	movl  %eax, pack_bytes
+	movw  $msg_dot, %si
+	call  print_str
 
 	call  compute_pack_addr
 	testl %eax, %eax
@@ -350,9 +369,12 @@ read_retry:
 	movw  $0x8000, %ax
 	movw  %ax, %es
 	movb  stage_drive, %dl
+	cmpb  $0, use_lba
+	jne   read_lba
 	cmpb  $0x80, %dl
 	jb    read_chs
 
+read_lba:
 	/* EDD LBA read */
 	movw  $0x10, 0x500
 	movw  $1, 0x502
@@ -595,6 +617,7 @@ bios_gdt:
 /* ---- Stage variables --------------------------------------------------- */
 .align 4
 stage_drive:    .byte 0
+use_lba:        .byte 0
 pack_lba:       .long 0
 pack_bytes:     .long 0
 pack_addr:      .long 0
@@ -608,7 +631,7 @@ rd_off:         .word 0
 rd_left:        .word 0
 rd_tries:       .word 0
 
-msg_pack:       .asciz "Loading commands... "
+msg_pack:       .asciz "Loading rootfs... "
 msg_dot:        .asciz "."
 msg_ok:         .asciz " ok\r\n"
 msg_noroom:     .asciz " skipped (no room)\r\n"