Просмотр исходного кода

Add UEFI boot and dual-boot USB image

The x86-64 loader in efi/ picks a GOP mode, converts GetMemoryMap into the
E820 map at 0x1000, stages kernel.bin below 4 GB, fills the new boot-info
block at 0x500 and the framebuffer info at 0x600, exits boot services,
leaves IA-32e mode (PG/LME/PAE/PGE/PCIDE/SMEP/SMAP/LA57) and jumps to
kernel_entry with paging off and flat 32-bit segments.

bootinfo.h carries the ACPI RSDP, EFI and RGBX flags with a checksum;
vesa_stage clears the magic on BIOS boots.  acpi.c prefers the EFI RSDP,
vga.c/panic.c swizzle RGBX scanouts, and the VBE-only ATI CRTC reset,
NVIDIA stolen-VRAM alias and Cirrus/Radeon hardware cursors are skipped
under EFI.

usb-efi.img is usb.img plus an 8 MB FAT16 ESP in a third MBR partition at
LBA 131072.  The legacy boot code and partitions 1/2 are unchanged except
that the fake FAT16 BPB's FATSize16 is cleared, otherwise UEFI firmware
mounts the raw-kernel partition and writes FAT/root pages over the kernel
at LBA 64.  make flash now flashes the dual-boot image; make uefi,
run-uefi and test-uefi build/run it under OVMF.  make all still builds only
the legacy images, and make flash FLASH_IMG=usb.img keeps the legacy path.
Johannes Zellner 2 недель назад
Родитель
Сommit
a720645d8d
15 измененных файлов с 1271 добавлено и 30 удалено
  1. 2 0
      .gitignore
  2. 62 9
      AGENTS.md
  3. 105 9
      Makefile
  4. 19 0
      acpi.c
  5. 72 0
      bootinfo.h
  6. 195 0
      efi/efi.h
  7. 637 0
      efi/main.c
  8. 16 0
      efi/payload.S
  9. 97 0
      efi/tramp.S
  10. 13 0
      efi/tramp.ld
  11. 4 2
      flash.sh
  12. 6 3
      mouse.c
  13. 5 2
      panic.c
  14. 4 0
      vesa_stage.S
  15. 34 5
      vga.c

+ 2 - 0
.gitignore

@@ -3,6 +3,8 @@
 *.elf
 *.elf
 *.img
 *.img
 *.pack
 *.pack
+BOOTX64.EFI
+efi/efi_entry.h
 send_serial
 send_serial
 kexec_send
 kexec_send
 netkexec_send
 netkexec_send

+ 62 - 9
AGENTS.md

@@ -16,6 +16,10 @@ 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-net    # like `make run` + a PCI NE2000 NIC on QEMU's user network (slirp; DHCP/DNS/NAT)
 make run-net    # like `make run` + a PCI NE2000 NIC on QEMU's user network (slirp; DHCP/DNS/NAT)
 make run-ahci   # like `make run` + an ICH9 AHCI controller and a 512 MB SATA disk (sata0)
 make run-ahci   # like `make run` + an ICH9 AHCI controller and a 512 MB SATA disk (sata0)
+make uefi       # build BOOTX64.EFI and the dual-boot usb-efi.img
+make run-uefi   # boot usb-efi.img under OVMF (x86-64 UEFI; serial log in /tmp/opencode)
+make test-uefi  # headless OVMF boot test (greps the serial log for the boot line)
+make flash      # write the dual-boot image (legacy BIOS + UEFI) to a USB stick
 make test-usb   # headless USB mouse/keyboard regression tests (QEMU + python3)
 make test-usb   # headless USB mouse/keyboard regression tests (QEMU + python3)
 make test-images  # host image-decoder + blit tests (ASan variant: test-images-asan)
 make test-images  # host image-decoder + blit tests (ASan variant: test-images-asan)
 make clean
 make clean
@@ -27,6 +31,21 @@ make clean
   the external browser (`BROWSER`) and its `DEMO.HTM`/`PIC.BMP` test page, plus
   the external browser (`BROWSER`) and its `DEMO.HTM`/`PIC.BMP` test page, plus
   a few test files; it requires `mtools` to build.
   a few test files; it requires `mtools` to build.
 - `make send PROGRAM=prog` transfers a binary to a real machine over serial.
 - `make send PROGRAM=prog` transfers a binary to a real machine over serial.
+- UEFI needs `x86_64-w64-mingw32-gcc` (the 64-bit loader) and, for the QEMU
+  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` remains the legacy-only image.  `make flash` uses `usb-efi.img`,
+  which is `usb.img` plus an 8 MB FAT16 ESP in a new third MBR partition
+  (type `0xEF`, LBA 131072).  The MBR boot code, partition entries 1/2 and the
+  active partition 1 are unchanged except for one BPB byte: FATSize16 is
+  cleared in the dual image (disk offset `0x7E16`).  Without that, UEFI
+  firmware scans the partition, sees the FAT16-looking boot sector and mounts
+  the raw kernel blob as a volume, writing FAT/root pages over the kernel at
+  LBA 64.  `boot.S` and the BIOS never read FATSize16.  Legacy boot from the
+  patched image is tested; `make flash FLASH_IMG=usb.img` flashes the
+  untouched legacy-only image.  `make flash` needs the mingw toolchain because
+  it builds `usb-efi.img`.
 
 
 ## Boot chain
 ## Boot chain
 
 
@@ -39,7 +58,16 @@ make clean
 3. `kernel.c` — `kernel_main` zeroes BSS then inits serial → memory/paging →
 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
    ACPI/MADT → I/O → idle → processes → timer (PIT; 1 kHz busy, 100 Hz idle + IDT) → SMP AP
    bring-up → mouse → VGA shadow buffer → terminal.
    bring-up → mouse → VGA shadow buffer → terminal.
-4. `mbr.S` — MBR (USB/HDD boot); loads partition boot sector at LBA 1.
+4. `mbr.S` — MBR (USB/HDD boot); loads the active partition's boot sector
+   (partition 1 at LBA 63).
+5. **UEFI (`efi/`)** — an x86-64 PE32+ `BOOTX64.EFI` loads from the ESP. It
+   picks a GOP mode, converts `GetMemoryMap` into the E820 map at `0x1000`,
+   stages `kernel.bin` below 4 GB, fills `0x500` (boot info) and `0x600`
+   (framebuffer), calls `ExitBootServices`, copies a 192-byte trampoline to
+   `0x800`, leaves IA-32e mode (`CR0.PG`, `EFER.LME`, `CR4.PAE/PGE/PCIDE/
+   SMEP/SMAP/LA57`), copies the kernel to `0x7E00` and jumps to
+   `kernel_entry`.  The BIOS boot sector / VESA stage are not executed on this
+   path; the legacy chain is unchanged.
 
 
 SMP is ACPI-only: RSDP → RSDT/XSDT → MADT, then `LAPIC` + `INIT-SIPI-SIPI`.
 SMP is ACPI-only: RSDP → RSDT/XSDT → MADT, then `LAPIC` + `INIT-SIPI-SIPI`.
 There is deliberately no Intel MP-table parser.  If ACPI, a usable MADT, the
 There is deliberately no Intel MP-table parser.  If ACPI, a usable MADT, the
@@ -51,12 +79,13 @@ a Pentium 1 (and a Pentium II board without ACPI) on the UP path.
 
 
 | Address    | Purpose |
 | Address    | Purpose |
 |-----------|---------|
 |-----------|---------|
-| `0x600`   | Boot framebuffer info (`struct vga_boot_fb_info`, see `vga.h`) |
-| `0x7C00`  | Boot sector load address |
-| `0x7E00`  | Kernel load / VESA-stage entry (`kernel.ld`) |
-| `0x800`   | kexec trampoline (relocated at runtime; below the kernel) |
-| `0x900`   | kexec flat GDT + GDTR (built at runtime; below the kernel) |
-| `0x1000`  | E820 memory map (`struct e820_map`, written by `vesa_stage.S`, parsed by `mm.c`); also the kexec trampoline's private stack top (grows down, below the map) |
+| `0x500`   | UEFI boot info (`struct boot_info`, `bootinfo.h`): magic/checksum, ACPI RSDP, EFI/RGBX flags; `vesa_stage.S` clears the magic on BIOS boot |
+| `0x600`   | Boot framebuffer info (`struct vga_boot_fb_info`, see `vga.h`; written by `vesa_stage.S` or the UEFI loader) |
+| `0x7C00`  | Boot sector load address; also the UEFI handoff stack top |
+| `0x7E00`  | Kernel load / VESA-stage entry (`kernel.ld`); the UEFI loader copies the kernel here and jumps to `kernel_entry` |
+| `0x800`   | kexec and UEFI handoff trampoline (relocated at runtime; below the kernel) |
+| `0x900`   | kexec/UEFI flat GDT + GDTR (built at runtime; below the kernel) |
+| `0x1000`  | E820 memory map (`struct e820_map`, written by `vesa_stage.S` or the UEFI loader, parsed by `mm.c`); also the kexec trampoline's private stack top (grows down, below the map) |
 | `0x3000`  | UHCI frame list (1024 × 4 B, 4 KB-aligned; `uhci.c`) |
 | `0x3000`  | UHCI frame list (1024 × 4 B, 4 KB-aligned; `uhci.c`) |
 | `0x4000`  | ISA DMA buffer, one sector (`disk.c`) |
 | `0x4000`  | ISA DMA buffer, one sector (`disk.c`) |
 | `0x6000`  | AP real/protected-mode trampoline (`ap_tramp.S`/`ap_tramp.ld`; SIPI vector 0x06, image ends below `0x6C00`) |
 | `0x6000`  | AP real/protected-mode trampoline (`ap_tramp.S`/`ap_tramp.ld`; SIPI vector 0x06, image ends below `0x6C00`) |
@@ -70,8 +99,10 @@ a Pentium 1 (and a Pentium II board without ACPI) on the UP path.
 
 
 Everything else is dynamic: process stacks and the kexec staging buffer come
 Everything else is dynamic: process stacks and the kexec staging buffer come
 from the frame allocator (≥ `0x900000`), and the fallback stack pool is a
 from the frame allocator (≥ `0x900000`), and the fallback stack pool is a
-static BSS array.  The kernel region itself only has one wall (`_end`
-below the run area), so adding code no longer moves any address.
+static BSS array.  The UEFI loader's kernel staging buffer comes from
+`AllocatePages` below 4 GB and only lives until the `0x800` trampoline copies
+it to `0x7E00`.  The kernel region itself only has one wall (`_end` below the
+run area), so adding code no longer moves any address.
 
 
 ## Key files
 ## Key files
 
 
@@ -120,6 +151,16 @@ below the run area), so adding code no longer moves any address.
   allocator on top of the frame allocator (used for PCBs and command context).
   allocator on top of the frame allocator (used for PCBs and command context).
   Process stacks (≥ `0x900000`) come from the frame allocator, with an 8-stack
   Process stacks (≥ `0x900000`) come from the frame allocator, with an 8-stack
   fallback pool (a static BSS array; no fixed address).
   fallback pool (a static BSS array; no fixed address).
+- **UEFI boot**: `bootinfo.h` (the `0x500` block and its checksum/accessors),
+  `efi/efi.h` (minimal UEFI types and table/protocol layouts), `efi/main.c`
+  (GOP mode choice, `GetMemoryMap` → E820, staging below 4 GB, boot-info/
+  framebuffer blocks, `ExitBootServices`), `efi/tramp.S` + `efi/tramp.ld` (the
+  `0x800` long-mode exit, kernel copy and jump) and `efi/payload.S` (embeds
+  `kernel.bin` and the trampoline in `BOOTX64.EFI`).  `vesa_stage.S` clears
+  the boot-info magic; `vga.c`/`panic.c` honour the RGBX flag and skip the
+  VBE-only ATI CRTC reset / NVIDIA stolen-VRAM alias under EFI; `mouse.c`
+  forces the software cursor; `acpi.c` prefers the passed RSDP and maps its
+  page before validating it.
 - **Paging**: `paging.c/h` — identity-maps RAM (4 MB pages via CR4.PSE) + the
 - **Paging**: `paging.c/h` — identity-maps RAM (4 MB pages via CR4.PSE) + the
   LFB (uncacheable) and enables CR0.PG/WP. Each process gets a page directory
   LFB (uncacheable) and enables CR0.PG/WP. Each process gets a page directory
   cloned from the kernel identity map (kernel stacks are mapped everywhere, so
   cloned from the kernel identity map (kernel stacks are mapped everywhere, so
@@ -926,3 +967,15 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     verification); `https` is shown as unverified.  The disk app is loaded
     verification); `https` is shown as unverified.  The disk app is loaded
     from FAT at spawn time, so a cold floppy start takes a few seconds; the
     from FAT at spawn time, so a cold floppy start takes a few seconds; the
     8 KB user stack means the layout state is deliberately static.
     8 KB user stack means the layout state is deliberately static.
+
+24. UEFI boot is x86-64 firmware only (no `BOOTIA32.EFI` yet) and the loader
+    is unsigned, so Secure Boot must be off.  The loader needs a GOP mode with
+    BGRX or RGBX 32bpp, width ≤ 1280, `pitch * height` ≤ 8 MB and the
+    framebuffer below 4 GB; otherwise the kernel starts headless (serial only).
+    There is no xHCI/EHCI driver, so many modern UEFI laptops/desktops boot the
+    UI without a USB keyboard or mouse (PS/2 may still work), and there is no
+    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 hybrid image is 72 MB because the ESP is a
+    third partition after the untouched 64 MB legacy layout.

+ 105 - 9
Makefile

@@ -13,7 +13,18 @@ ASFLAGS  = --32 -I.
 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-smp run-floppy run-usb run-uhci run-net run-ahci test-usb run-serial floppy usb flash space prog bootstrap dev net-dev cmds-dev net-cmds browser-dev netmon monitor test-crypto test-images test-images-asan
+# UEFI loader: a PE32+ x86-64 EFI application built with the mingw toolchain.
+# The kernel itself stays 32-bit; the loader leaves long mode before jumping.
+EFI_CC   ?= x86_64-w64-mingw32-gcc
+EFI_AS   ?= x86_64-w64-mingw32-as
+EFI_CFLAGS = -ffreestanding -fno-stack-protector -mno-red-zone -fno-builtin \
+             -fno-asynchronous-unwind-tables -fno-unwind-tables -O2 \
+             -Wall -Wextra -I. -Iefi
+EFI_LDFLAGS = -nostdlib -Wl,--subsystem,10 -Wl,-e,efi_main \
+              -Wl,--image-base,0x0 -Wl,--dynamicbase \
+              -Wl,--enable-reloc-section -Wl,--no-insert-timestamp
+
+.PHONY: all clean run run-smp run-floppy run-usb run-uhci run-net run-ahci test-usb run-serial floppy usb uefi run-uefi test-uefi flash space prog bootstrap dev net-dev cmds-dev net-cmds browser-dev netmon monitor test-crypto test-images test-images-asan
 
 
 all: floppy.img usb.img final.bin root.img
 all: floppy.img usb.img final.bin root.img
 
 
@@ -86,7 +97,7 @@ bin_recv.o: bin_recv.c bin_recv.h serial.h io.h timer.h
 serial_recv.o: serial_recv.c serial.h bin_recv.h io.h
 serial_recv.o: serial_recv.c serial.h bin_recv.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
-vga.o: vga.c vga.h blit.h pci.h gpu.h ati.h process.h
+vga.o: vga.c vga.h bootinfo.h blit.h pci.h gpu.h ati.h process.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
 blit.o: blit.c blit.h vga.h gfx.h cpu.h
 blit.o: blit.c blit.h vga.h gfx.h cpu.h
@@ -152,7 +163,7 @@ usbhid.o: usbhid.c usbhid.h io.h usb.h usb_hcd.h serial.h timer.h
 lsusb.o: lsusb.c io.h usb.h
 lsusb.o: lsusb.c io.h usb.h
 	$(CC) $(CFLAGS) -Os -o $@ $<
 	$(CC) $(CFLAGS) -Os -o $@ $<
 
 
-panic.o: panic.c panic.h font_8x8.h process.h serial.h timer.h vga.h
+panic.o: panic.c panic.h bootinfo.h font_8x8.h process.h serial.h timer.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
 lspci.o: lspci.c io.h pci.h
 lspci.o: lspci.c io.h pci.h
@@ -206,7 +217,7 @@ fatformat.o: fatformat.c fatformat.h blkdev.h io.h mm.h sched.h
 format.o: format.c blkdev.h disk.h fatformat.h io.h process.h sched.h vga.h vfs.h
 format.o: format.c blkdev.h disk.h fatformat.h io.h process.h sched.h vga.h vfs.h
 	$(CC) $(CFLAGS) -Os -o $@ $<
 	$(CC) $(CFLAGS) -Os -o $@ $<
 
 
-mouse.o: mouse.c mouse.h cirrus.h radeon.h timer.h usbhid.h vga.h
+mouse.o: mouse.c mouse.h bootinfo.h cirrus.h radeon.h timer.h usbhid.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
 idle.o: idle.c idle.h
 idle.o: idle.c idle.h
@@ -242,7 +253,7 @@ entry.o: entry.S
 sched.o: sched.c sched.h process.h paging.h smp.h spinlock.h sys_fs.h thermal.h timer.h tss.h vga.h
 sched.o: sched.c sched.h process.h paging.h smp.h spinlock.h sys_fs.h thermal.h timer.h tss.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
-acpi.o: acpi.c acpi.h paging.h serial.h
+acpi.o: acpi.c acpi.h bootinfo.h paging.h serial.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
 lapic.o: lapic.c lapic.h timer.h
 lapic.o: lapic.c lapic.h timer.h
@@ -370,6 +381,87 @@ final.bin: boot.bin kernel.bin
 	cat $@.boot.tmp kernel.bin > $@; \
 	cat $@.boot.tmp kernel.bin > $@; \
 	rm -f $@.boot.tmp
 	rm -f $@.boot.tmp
 
 
+# ---- UEFI loader and dual-boot USB image -------------------------------
+#
+# The legacy image (usb.img) is left byte-for-byte alone.  usb-efi.img copies
+# it, adds a third MBR partition (type 0xEF, starting at the old 64 MB end)
+# and writes a small FAT16 ESP there.  BIOS booting still uses the active
+# partition 1 at LBA 63; UEFI finds BOOTX64.EFI in the ESP.
+
+efi/efi_entry.h: kernel.elf
+	@mkdir -p efi
+	@entry=$$(nm kernel.elf | awk '/ T kernel_entry$$/{print $$1}'); \
+	if [ -z "$$entry" ]; then echo "efi: kernel_entry not found" >&2; exit 1; fi; \
+	printf '#define KERNEL_ENTRY 0x%s\n' "$$entry" > $@
+
+efi/main.o: efi/main.c efi/efi.h bootinfo.h vga.h efi/efi_entry.h
+	$(EFI_CC) $(EFI_CFLAGS) -c -o $@ $<
+
+efi/payload.o: efi/payload.S kernel.bin efi/tramp.bin
+	$(EFI_AS) --64 -o $@ $<
+
+efi/tramp.o: efi/tramp.S
+	$(AS) --64 -o $@ $<
+
+efi/tramp.elf: efi/tramp.o efi/tramp.ld
+	$(LD) -T efi/tramp.ld -nostdlib -m elf_x86_64 -o $@ $<
+
+efi/tramp.bin: efi/tramp.elf
+	$(OBJCOPY) -O binary $< $@
+
+BOOTX64.EFI: efi/main.o efi/payload.o
+	$(EFI_CC) $(EFI_LDFLAGS) -o $@ efi/main.o efi/payload.o
+
+ESP_LBA     = 131072
+ESP_SECTORS = 16384
+
+esp.img: BOOTX64.EFI
+	truncate -s $$(( $(ESP_SECTORS) * 512 )) $@
+	mkfs.fat -F 16 -s 2 -n NEBOSESP $@
+	mmd -i $@ ::/EFI
+	mmd -i $@ ::/EFI/BOOT
+	mcopy -i $@ BOOTX64.EFI ::/EFI/BOOT/BOOTX64.EFI
+
+usb-efi.img: usb.img esp.img
+	cp usb.img $@
+	# The legacy partition is a raw kernel blob with a FAT16-looking BPB (the
+	# BIOS only needs the boot sector and LBA 64).  UEFI firmware scans FAT
+	# partitions and would mount it as a volume, writing FAT/root pages over
+	# 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
+	printf '\x00\x28\x21\x08\xef\x2d\x24\x09\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 )) $@
+
+uefi: usb-efi.img
+
+OVMF ?= $(firstword $(wildcard /usr/share/ovmf/x64/OVMF.4m.fd /usr/share/OVMF/OVMF.4m.fd))
+
+# root.img is deliberately not attached: a running QEMU may hold its write
+# lock, and the UEFI boot test does not need a second filesystem.
+run-uefi: usb-efi.img
+	@test -n "$(OVMF)" || { echo "OVMF not found; set OVMF=/path/to/OVMF.4m.fd" >&2; exit 1; }
+	qemu-system-x86_64 -M pc -m 256 -smp 4 -bios $(OVMF) -snapshot \
+	  -drive file=usb-efi.img,format=raw,if=ide \
+	  -device pci-ohci -device usb-mouse \
+	  -device ne2k_pci,netdev=n0 -netdev user,id=n0 \
+	  -serial file:/tmp/opencode/nebos-uefi-serial.log
+
+test-uefi: usb-efi.img
+	@test -n "$(OVMF)" || { echo "OVMF not found; set OVMF=/path/to/OVMF.4m.fd" >&2; exit 1; }
+	@rm -f /tmp/opencode/nebos-uefi-serial.log
+	timeout 40 qemu-system-x86_64 -M pc -m 256 -smp 4 -bios $(OVMF) -snapshot \
+	  -drive file=usb-efi.img,format=raw,if=ide \
+	  -device pci-ohci -device usb-mouse \
+	  -device ne2k_pci,netdev=n0 -netdev user,id=n0 \
+	  -display none -serial file:/tmp/opencode/nebos-uefi-serial.log \
+	  -no-reboot >/dev/null 2>&1 || true
+	@grep -q "booted" /tmp/opencode/nebos-uefi-serial.log && \
+		echo "UEFI boot: OK (/tmp/opencode/nebos-uefi-serial.log)" || \
+		{ echo "UEFI boot: FAILED"; tail -c 4000 /tmp/opencode/nebos-uefi-serial.log; exit 1; }
+
 prog.o: prog.c run_api.h
 prog.o: prog.c run_api.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
@@ -616,10 +708,14 @@ floppy: floppy.img
 
 
 usb: usb.img
 usb: usb.img
 
 
-# Write usb.img to a USB stick; prompts before destroying anything.
+# Write the dual-boot USB image (legacy BIOS + UEFI) to a stick; prompts
+# before destroying anything.
 #   make flash USB=/dev/sdX          FORCE=1 skips the removable-device check.
 #   make flash USB=/dev/sdX          FORCE=1 skips the removable-device check.
-flash: usb.img
-	@./flash.sh "$(USB)" "$(FORCE)"
+#   make flash FLASH_IMG=usb.img     legacy-only image
+# The UEFI half needs the mingw toolchain (EFI_CC); the legacy half does not.
+FLASH_IMG ?= usb-efi.img
+flash: $(FLASH_IMG)
+	@./flash.sh "$(USB)" "$(FORCE)" "$(FLASH_IMG)"
 
 
 run-floppy: floppy.img root.img
 run-floppy: floppy.img root.img
 	qemu-system-i386 -M pc -device pci-ohci -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 pci-ohci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
@@ -756,4 +852,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 entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o thermal.o it87.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o panic.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o inflate.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp.o ap_tramp.elf ap_tramp.bin ap_tramp_embed.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 cmds.pack cmds_pack_gz.bin cmds_pack.o ramfs.o share.o payload.o mkpack uwm_lib.o ngui.o img.o img_mem.o img_blit.o img_png.o img_bmp.o img_gif.o img_jpeg.o img_tga.o img_pnm.o img_ico.o img_pam.o img_gif_anim.o img_file.o browser.o browser_html.o uhttp.o browser.elf img_test img_test_asan *.boot.tmp $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) $(TLSLIB_OBJS) tls_test bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor
+	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o thermal.o it87.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o panic.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o inflate.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp.o ap_tramp.elf ap_tramp.bin ap_tramp_embed.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 cmds.pack cmds_pack_gz.bin cmds_pack.o ramfs.o share.o payload.o mkpack uwm_lib.o ngui.o img.o img_mem.o img_blit.o img_png.o img_bmp.o img_gif.o img_jpeg.o img_tga.o img_pnm.o img_ico.o img_pam.o img_gif_anim.o img_file.o browser.o browser_html.o uhttp.o browser.elf img_test img_test_asan *.boot.tmp $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) $(TLSLIB_OBJS) tls_test bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor BOOTX64.EFI esp.img usb-efi.img efi/main.o efi/payload.o efi/tramp.o efi/tramp.elf efi/tramp.bin efi/efi_entry.h

+ 19 - 0
acpi.c

@@ -7,6 +7,7 @@
  */
  */
 
 
 #include "acpi.h"
 #include "acpi.h"
+#include "bootinfo.h"
 #include "paging.h"
 #include "paging.h"
 #include "serial.h"
 #include "serial.h"
 
 
@@ -104,6 +105,24 @@ static const struct acpi_rsdp *find_rsdp(void)
 {
 {
 	volatile unsigned short *ebda_seg = (volatile unsigned short *)0x40E;
 	volatile unsigned short *ebda_seg = (volatile unsigned short *)0x40E;
 	unsigned int ebda, i;
 	unsigned int ebda, i;
+	unsigned int given = bootinfo_rsdp();
+
+	/* The UEFI loader passes the RSDP address from the system table.  UEFI
+	 * places ACPI tables outside the 128 MB identity map often enough that
+	 * the scan below would never see them, so map the page first. */
+	if (given && given < 0xFFF00000u) {
+		const struct acpi_rsdp *r;
+
+		paging_map_ram(given & 0xFFC00000u, 0x800000u);
+		r = (const struct acpi_rsdp *)given;
+		if (rsdp_valid(r)) {
+			serial_puts(SERIAL_COM1, "acpi: EFI RSDP at ");
+			serial_puthex(SERIAL_COM1, given);
+			serial_puts(SERIAL_COM1, "\n");
+			return r;
+		}
+		serial_puts(SERIAL_COM1, "acpi: EFI RSDP invalid, scanning\n");
+	}
 
 
 	/* Opaque asm keeps -Warray-bounds quiet about the fixed BIOS pointer. */
 	/* Opaque asm keeps -Warray-bounds quiet about the fixed BIOS pointer. */
 	__asm__ volatile ("" : "+r"(ebda_seg));
 	__asm__ volatile ("" : "+r"(ebda_seg));

+ 72 - 0
bootinfo.h

@@ -0,0 +1,72 @@
+/* Boot information block at 0x500.
+ *
+ * The legacy VESA stage (vesa_stage.S) does not fill this in; it clears the
+ * magic so a BIOS boot takes exactly the old code paths.  The UEFI loader
+ * fills it in with the ACPI RSDP address, the boot source and the GOP pixel
+ * order.  A checksum over the eight dwords (with the checksum field set so
+ * the sum is zero) keeps stale low memory from being mistaken for a valid
+ * block after a kexec or a legacy boot.
+ */
+#ifndef BOOTINFO_H
+#define BOOTINFO_H
+
+#define BOOT_INFO_ADDR    0x500
+#define BOOT_INFO_MAGIC   0x4E425349u   /* "NBSI" */
+#define BOOT_INFO_VERSION 1u
+
+#define BOOTINFO_FLAG_EFI  0x00000001u
+#define BOOTINFO_FLAG_RGBX 0x00000002u  /* GOP 32bpp byte order R,G,B,X */
+
+struct boot_info {
+	unsigned int magic;
+	unsigned int version;
+	unsigned int checksum;
+	unsigned int rsdp;          /* physical ACPI RSDP address, 0 = scan */
+	unsigned int flags;
+	unsigned int reserved[3];
+} __attribute__((packed));
+
+/* Fixed low-memory pointer, hidden from -Warray-bounds like the other boot
+ * structures (see vga.c / acpi.c). */
+static inline const volatile unsigned int *bootinfo_words(void)
+{
+	const volatile unsigned int *p =
+		(const volatile unsigned int *)BOOT_INFO_ADDR;
+
+	__asm__ volatile ("" : "+r"(p));
+	return p;
+}
+
+static inline int bootinfo_valid(void)
+{
+	const volatile unsigned int *p = bootinfo_words();
+	unsigned int i, sum = 0;
+
+	if (p[0] != BOOT_INFO_MAGIC || p[1] != BOOT_INFO_VERSION)
+		return 0;
+	for (i = 0; i < 8; i++)
+		sum += p[i];
+	return sum == 0;
+}
+
+static inline unsigned int bootinfo_flags(void)
+{
+	return bootinfo_valid() ? bootinfo_words()[4] : 0;
+}
+
+static inline int bootinfo_efi(void)
+{
+	return (bootinfo_flags() & BOOTINFO_FLAG_EFI) != 0;
+}
+
+static inline int bootinfo_rgbx(void)
+{
+	return (bootinfo_flags() & BOOTINFO_FLAG_RGBX) != 0;
+}
+
+static inline unsigned int bootinfo_rsdp(void)
+{
+	return bootinfo_valid() ? bootinfo_words()[3] : 0;
+}
+
+#endif

+ 195 - 0
efi/efi.h

@@ -0,0 +1,195 @@
+/* Minimal UEFI definitions for the Nebs OS x86-64 loader.
+ *
+ * Only the tables and protocols the loader uses are declared, but the fields
+ * before them are in their exact UEFI order so the offsets match the
+ * firmware's structures.  x86_64-w64-mingw32 uses the Microsoft x64 calling
+ * convention, which is the UEFI ABI, so no ms_abi attributes are needed.
+ */
+#ifndef NEBOS_EFI_H
+#define NEBOS_EFI_H
+
+typedef unsigned char      UINT8;
+typedef unsigned short     UINT16;
+typedef unsigned int       UINT32;
+typedef unsigned long long UINT64;
+typedef unsigned long long UINTN;
+typedef UINTN              EFI_STATUS;
+typedef void              *EFI_HANDLE;
+typedef UINT64             EFI_PHYSICAL_ADDRESS;
+typedef UINT64             EFI_VIRTUAL_ADDRESS;
+typedef UINT16             CHAR16;
+typedef UINT8              BOOLEAN;
+
+#define EFI_SUCCESS 0
+
+typedef struct {
+	UINT32 Data1;
+	UINT16 Data2;
+	UINT16 Data3;
+	UINT8  Data4[8];
+} __attribute__((packed)) EFI_GUID;
+
+typedef struct {
+	UINT64 Signature;
+	UINT32 Revision;
+	UINT32 HeaderSize;
+	UINT32 CRC32;
+	UINT32 Reserved;
+} EFI_TABLE_HEADER;
+
+typedef struct {
+	UINT32 Type;
+	UINT32 Pad;
+	EFI_PHYSICAL_ADDRESS PhysicalStart;
+	EFI_VIRTUAL_ADDRESS  VirtualStart;
+	UINT64 NumberOfPages;
+	UINT64 Attribute;
+} __attribute__((packed)) EFI_MEMORY_DESCRIPTOR;
+
+typedef struct {
+	EFI_GUID VendorGuid;
+	void    *VendorTable;
+} EFI_CONFIGURATION_TABLE;
+
+typedef struct {
+	void *Reset;
+	EFI_STATUS (*OutputString)(void *This, CHAR16 *String);
+	void *TestString;
+	void *QueryMode;
+	void *SetMode;
+	void *SetAttribute;
+	void *ClearScreen;
+	void *SetCursorPosition;
+	void *EnableCursor;
+	void *Mode;
+} EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL;
+
+/* Field order matches EFI_BOOT_SERVICES; only the used entries have typed
+ * signatures, the rest are void * but must still be present in order. */
+typedef struct {
+	EFI_TABLE_HEADER Hdr;
+	void *RaiseTPL;
+	void *RestoreTPL;
+	EFI_STATUS (*AllocatePages)(UINT32 Type, UINT32 MemoryType,
+				    UINTN Pages, EFI_PHYSICAL_ADDRESS *Memory);
+	void *FreePages;
+	EFI_STATUS (*GetMemoryMap)(UINTN *MemoryMapSize,
+				   EFI_MEMORY_DESCRIPTOR *MemoryMap,
+				   UINTN *MapKey, UINTN *DescriptorSize,
+				   UINT32 *DescriptorVersion);
+	void *AllocatePool;
+	void *FreePool;
+	void *CreateEvent;
+	void *SetTimer;
+	void *WaitForEvent;
+	void *SignalEvent;
+	void *CloseEvent;
+	void *CheckEvent;
+	void *InstallProtocolInterface;
+	void *ReinstallProtocolInterface;
+	void *UninstallProtocolInterface;
+	void *HandleProtocol;
+	void *Reserved;
+	void *RegisterProtocolNotify;
+	void *LocateHandle;
+	void *LocateDevicePath;
+	void *InstallConfigurationTable;
+	void *LoadImage;
+	void *StartImage;
+	void *Exit;
+	void *UnloadImage;
+	EFI_STATUS (*ExitBootServices)(EFI_HANDLE ImageHandle, UINTN MapKey);
+	void *GetNextMonotonicCount;
+	void *Stall;
+	EFI_STATUS (*SetWatchdogTimer)(UINTN Timeout, UINT64 WatchdogCode,
+				       UINTN DataSize, CHAR16 *WatchdogData);
+	void *ConnectController;
+	void *DisconnectController;
+	void *OpenProtocol;
+	void *CloseProtocol;
+	void *OpenProtocolInformation;
+	void *ProtocolsPerHandle;
+	void *LocateHandleBuffer;
+	EFI_STATUS (*LocateProtocol)(EFI_GUID *Protocol, void *Registration,
+				     void **Interface);
+} EFI_BOOT_SERVICES;
+
+typedef struct {
+	EFI_TABLE_HEADER Hdr;
+	CHAR16 *FirmwareVendor;
+	UINT32 FirmwareRevision;
+	UINT32 Pad;
+	EFI_HANDLE ConsoleInHandle;
+	void *ConIn;
+	EFI_HANDLE ConsoleOutHandle;
+	EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *ConOut;
+	EFI_HANDLE StandardErrorHandle;
+	EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *StdErr;
+	void *RuntimeServices;
+	EFI_BOOT_SERVICES *BootServices;
+	UINTN NumberOfTableEntries;
+	EFI_CONFIGURATION_TABLE *ConfigurationTable;
+} EFI_SYSTEM_TABLE;
+
+typedef struct {
+	UINT32 RedMask;
+	UINT32 GreenMask;
+	UINT32 BlueMask;
+	UINT32 ReservedMask;
+} EFI_PIXEL_BITMASK;
+
+typedef struct {
+	UINT32 Version;
+	UINT32 HorizontalResolution;
+	UINT32 VerticalResolution;
+	UINT32 PixelFormat;
+	EFI_PIXEL_BITMASK PixelInformation;
+	UINT32 PixelsPerScanLine;
+} EFI_GRAPHICS_OUTPUT_MODE_INFORMATION;
+
+typedef struct {
+	UINT32 MaxMode;
+	UINT32 Mode;
+	EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;
+	UINTN SizeOfInfo;
+	EFI_PHYSICAL_ADDRESS FrameBufferBase;
+	UINTN FrameBufferSize;
+} EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE;
+
+typedef struct {
+	EFI_STATUS (*QueryMode)(void *This, UINT32 ModeNumber,
+				UINTN *SizeOfInfo,
+				EFI_GRAPHICS_OUTPUT_MODE_INFORMATION **Info);
+	EFI_STATUS (*SetMode)(void *This, UINT32 ModeNumber);
+	void *Blt;
+	EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *Mode;
+} EFI_GRAPHICS_OUTPUT_PROTOCOL;
+
+#define EFI_PIXEL_RGBX     0
+#define EFI_PIXEL_BGRX     1
+#define EFI_PIXEL_BITMASK  2
+#define EFI_PIXEL_BLTONLY  3
+
+#define EFI_ALLOCATE_ANY_PAGES    0
+#define EFI_ALLOCATE_MAX_ADDRESS  1
+#define EFI_ALLOCATE_ADDRESS      2
+
+#define EFI_LOADER_DATA            2
+#define EFI_BOOT_SERVICES_CODE     3
+#define EFI_BOOT_SERVICES_DATA     4
+#define EFI_CONVENTIONAL_MEMORY    7
+
+static const EFI_GUID efi_gop_guid = {
+	0x9042A9DEu, 0x23DC, 0x4A38,
+	{ 0x96, 0xFB, 0x7A, 0xDE, 0xD0, 0x80, 0x51, 0x6A }
+};
+static const EFI_GUID efi_acpi20_guid = {
+	0x8868E871u, 0xE4F1, 0x11D3,
+	{ 0xBC, 0x22, 0x00, 0x80, 0xC7, 0x3C, 0x88, 0x81 }
+};
+static const EFI_GUID efi_acpi10_guid = {
+	0xEB9D2D30u, 0x2D88, 0x11D3,
+	{ 0x9A, 0x16, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }
+};
+
+#endif

+ 637 - 0
efi/main.c

@@ -0,0 +1,637 @@
+/* Nebs OS UEFI loader (x86-64).
+ *
+ * The kernel is a 32-bit i586 image linked at 0x7E00.  This loader gives it
+ * the same two boot structures the real-mode VESA stage produces:
+ *
+ *   0x500  struct boot_info      (EFI flag, GOP pixel order, ACPI RSDP)
+ *   0x600  struct vga_boot_fb_info
+ *   0x1000 struct e820_map
+ *
+ * then leaves long mode and jumps to kernel_entry with paging off and flat
+ * 32-bit segments.  Nothing in the legacy boot chain is used here.
+ */
+
+#include "efi.h"
+#include "bootinfo.h"
+#include "vga.h"
+#include "efi_entry.h"
+
+#define MAX_PHYS    0x08000000u          /* MM_MAX_PHYS in mm.h */
+#define PAGE_SIZE_  0x1000u
+#define MAX_PAGES   (MAX_PHYS / PAGE_SIZE_)
+#define E820_ADDR   0x1000u
+
+/* The kernel image is linked to live at 0x7E00 and must stay below the run
+ * area at 0x80000 (kernel.ld asserts _end <= 0x7F000). */
+#define KERNEL_DEST 0x7E00u
+#define KERNEL_MAX  0x70000u
+
+/* Trampoline copy address: below the kernel, same low-memory slot kexec uses
+ * (trampoline at 0x800, flat GDT inside the blob, stack at 0x7C00). */
+#define TRAMP_ADDR  0x800u
+#define TRAMP_MAX   0x800u
+
+extern unsigned char kernel_image_start[];
+extern unsigned char kernel_image_end[];
+extern unsigned char tramp_blob_start[];
+extern unsigned char tramp_blob_end[];
+
+/* GetMemoryMap buffer.  A firmware map is normally a few KB; 64 KB is very
+ * generous and keeps the retry loop simple. */
+static UINT8 memmap_buf[65536] __attribute__((aligned(8)));
+
+/* One bit per 4 KB page over the kernel's 128 MB identity window.  Starts
+ * all-used; only EfiConventional/BootServices memory is freed.  EfiLoader*
+ * (this image and the staging buffer) stays reserved, which is safe and
+ * costs only a few hundred KB. */
+static UINT8 page_used[MAX_PAGES / 8];
+
+struct fb_saved {
+	int      valid;
+	int      rgbx;
+	UINT32   width;
+	UINT32   height;
+	UINT32   pitch;
+	UINT32   bpp;
+	UINT64   base;
+	UINT64   size;
+};
+
+struct e820_entry_disk {
+	UINT64 base;
+	UINT64 length;
+	UINT32 type;
+	UINT32 attrs;
+} __attribute__((packed));
+
+/* ---- serial console (COM1, 115200 8N1) --------------------------------- */
+
+static inline void outb(UINT16 port, UINT8 v)
+{
+	__asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
+}
+
+static inline UINT8 inb(UINT16 port)
+{
+	UINT8 v;
+	__asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
+	return v;
+}
+
+static void com1_init(void)
+{
+	outb(0x3F9, 0x00);   /* IER */
+	outb(0x3FB, 0x80);   /* LCR: DLAB */
+	outb(0x3F8, 0x01);   /* DLL: 115200 */
+	outb(0x3F9, 0x00);   /* DLM */
+	outb(0x3FB, 0x03);   /* LCR: 8N1 */
+	outb(0x3FA, 0xC7);   /* FIFO */
+	outb(0x3FC, 0x0B);   /* MCR: DTR/RTS/OUT2 */
+}
+
+static void com1_putc(char c)
+{
+	unsigned int guard = 100000;
+
+	while (!(inb(0x3FD) & 0x20) && guard--)
+		;
+	outb(0x3F8, (UINT8)c);
+}
+
+static void com1_puts(const char *s)
+{
+	while (*s) {
+		if (*s == '\n')
+			com1_putc('\r');
+		com1_putc(*s++);
+	}
+}
+
+static void com1_hex(UINT64 v, unsigned int digits)
+{
+	static const char h[] = "0123456789ABCDEF";
+	unsigned int i;
+
+	for (i = digits; i > 0; i--)
+		com1_putc(h[(v >> (4 * (i - 1))) & 0xF]);
+}
+
+static void com1_dec(UINT32 v)
+{
+	char buf[11];
+	unsigned int i = 0;
+
+	if (v == 0) {
+		com1_putc('0');
+		return;
+	}
+	while (v) {
+		buf[i++] = (char)('0' + v % 10);
+		v /= 10;
+	}
+	while (i)
+		com1_putc(buf[--i]);
+}
+
+static void conout_puts(EFI_SYSTEM_TABLE *st, const char *s)
+{
+	CHAR16 buf[128];
+	unsigned int i = 0;
+
+	if (!st || !st->ConOut || !st->ConOut->OutputString)
+		return;
+	while (*s) {
+		if (*s == '\n')
+			buf[i++] = '\r';
+		buf[i++] = (CHAR16)(UINT8)*s++;
+		if (i >= 126) {
+			buf[i] = 0;
+			st->ConOut->OutputString(st->ConOut, buf);
+			i = 0;
+		}
+	}
+	if (i) {
+		buf[i] = 0;
+		st->ConOut->OutputString(st->ConOut, buf);
+	}
+}
+
+static void halt_forever(void) __attribute__((noreturn));
+static void halt_forever(void)
+{
+	for (;;)
+		__asm__ volatile ("cli; hlt");
+}
+
+static void fail_before(EFI_SYSTEM_TABLE *st, const char *msg)
+	__attribute__((noreturn));
+static void fail_before(EFI_SYSTEM_TABLE *st, const char *msg)
+{
+	com1_puts("nebos-efi: ");
+	com1_puts(msg);
+	com1_puts("\n");
+	conout_puts(st, "Nebs OS EFI loader: ");
+	conout_puts(st, msg);
+	conout_puts(st, "\n");
+	halt_forever();
+}
+
+static void fail_after(const char *msg) __attribute__((noreturn));
+static void fail_after(const char *msg)
+{
+	com1_puts("nebos-efi: ");
+	com1_puts(msg);
+	com1_puts("\n");
+	halt_forever();
+}
+
+/* ---- helpers ----------------------------------------------------------- */
+
+static int guid_eq(const EFI_GUID *a, const EFI_GUID *b)
+{
+	const UINT8 *pa = (const UINT8 *)a;
+	const UINT8 *pb = (const UINT8 *)b;
+	unsigned int i;
+
+	for (i = 0; i < 16; i++)
+		if (pa[i] != pb[i])
+			return 0;
+	return 1;
+}
+
+static UINT32 find_rsdp(EFI_SYSTEM_TABLE *st)
+{
+	UINTN i;
+
+	if (!st->ConfigurationTable)
+		return 0;
+	for (i = 0; i < st->NumberOfTableEntries; i++) {
+		EFI_CONFIGURATION_TABLE *ct = &st->ConfigurationTable[i];
+		UINT64 addr;
+
+		if (!guid_eq(&ct->VendorGuid, &efi_acpi20_guid) &&
+		    !guid_eq(&ct->VendorGuid, &efi_acpi10_guid))
+			continue;
+		addr = (UINT64)(UINTN)ct->VendorTable;
+		if (addr && addr <= 0xFFFFFFFFull)
+			return (UINT32)addr;
+	}
+	return 0;
+}
+
+/* ---- GOP --------------------------------------------------------------- */
+
+static int gop_init(EFI_SYSTEM_TABLE *st, EFI_GRAPHICS_OUTPUT_PROTOCOL **out_gop,
+		    EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *out_info)
+{
+	EFI_GRAPHICS_OUTPUT_PROTOCOL *gop = 0;
+	EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
+	EFI_STATUS status;
+	UINTN size;
+	UINT32 i, best = 0, best_score = 0;
+	UINT64 fb_base, fb_size;
+
+	status = st->BootServices->LocateProtocol((EFI_GUID *)&efi_gop_guid,
+						  0, (void **)&gop);
+	if (status || !gop || !gop->Mode)
+		return 0;
+
+	for (i = 0; i < gop->Mode->MaxMode; i++) {
+		UINT64 bytes;
+		UINT32 score;
+
+		status = gop->QueryMode(gop, i, &size, &info);
+		if (status || !info)
+			continue;
+		if (info->PixelFormat != EFI_PIXEL_BGRX &&
+		    info->PixelFormat != EFI_PIXEL_RGBX)
+			continue;
+		if (info->HorizontalResolution < 320 ||
+		    info->VerticalResolution < 200)
+			continue;
+		if (info->HorizontalResolution > 1280 ||
+		    info->VerticalResolution > 65535)
+			continue;
+		if (info->PixelsPerScanLine < info->HorizontalResolution)
+			continue;
+		if (info->PixelsPerScanLine > 0x3FFF)  /* pitch must fit u16 */
+			continue;
+		bytes = (UINT64)info->PixelsPerScanLine * 4 *
+			info->VerticalResolution;
+		if (bytes > 8ull * 1024 * 1024)
+			continue;
+		score = info->HorizontalResolution * info->VerticalResolution;
+		if (info->PixelFormat == EFI_PIXEL_BGRX)
+			score++;   /* prefer BGRX on equal area */
+		if (score > best_score) {
+			best_score = score;
+			best = i;
+		}
+	}
+	if (!best_score)
+		return 0;
+
+	status = gop->SetMode(gop, best);
+	if (status)
+		return 0;
+
+	info = gop->Mode->Info;
+	if (!info)
+		return 0;
+	if (info->PixelFormat != EFI_PIXEL_BGRX &&
+	    info->PixelFormat != EFI_PIXEL_RGBX)
+		return 0;
+	if (info->HorizontalResolution < 320 ||
+	    info->HorizontalResolution > 1280 ||
+	    info->VerticalResolution < 200 ||
+	    info->VerticalResolution > 65535 ||
+	    info->PixelsPerScanLine < info->HorizontalResolution ||
+	    info->PixelsPerScanLine > 0x3FFF)
+		return 0;
+	{
+		UINT64 bytes = (UINT64)info->PixelsPerScanLine * 4 *
+			info->VerticalResolution;
+
+		if (bytes > 8ull * 1024 * 1024)
+			return 0;
+	}
+	fb_base = gop->Mode->FrameBufferBase;
+	fb_size = gop->Mode->FrameBufferSize;
+	if (!fb_base || fb_base > 0xFFFFFFFFull)
+		return 0;
+	if (fb_base + fb_size > 0x100000000ull)
+		return 0;
+
+	out_info->Version = info->Version;
+	out_info->HorizontalResolution = info->HorizontalResolution;
+	out_info->VerticalResolution = info->VerticalResolution;
+	out_info->PixelFormat = info->PixelFormat;
+	out_info->PixelInformation.RedMask = info->PixelInformation.RedMask;
+	out_info->PixelInformation.GreenMask = info->PixelInformation.GreenMask;
+	out_info->PixelInformation.BlueMask = info->PixelInformation.BlueMask;
+	out_info->PixelInformation.ReservedMask =
+		info->PixelInformation.ReservedMask;
+	out_info->PixelsPerScanLine = info->PixelsPerScanLine;
+	*out_gop = gop;
+	return 1;
+}
+
+/* ---- E820 conversion --------------------------------------------------- */
+
+static inline int page_is_used(UINT32 p)
+{
+	return (page_used[p >> 3] >> (p & 7)) & 1;
+}
+
+static inline void page_set_used(UINT32 p)
+{
+	page_used[p >> 3] |= (UINT8)(1u << (p & 7));
+}
+
+static inline void page_set_free(UINT32 p)
+{
+	page_used[p >> 3] &= (UINT8)~(1u << (p & 7));
+}
+
+/* Free only pages fully inside [base, base+len). */
+static void free_range(UINT64 base, UINT64 len)
+{
+	UINT64 end;
+	UINT32 p0, p1, p;
+
+	if (base >= MAX_PHYS)
+		return;
+	end = base + len;
+	if (end > MAX_PHYS)
+		end = MAX_PHYS;
+	p0 = (UINT32)((base + PAGE_SIZE_ - 1) / PAGE_SIZE_);
+	p1 = (UINT32)(end / PAGE_SIZE_);
+	for (p = p0; p < p1; p++)
+		page_set_free(p);
+}
+
+/* Reserve every page touched by [base, base+len). */
+static void use_range(UINT64 base, UINT64 len)
+{
+	UINT64 end;
+	UINT32 p0, p1, p;
+
+	if (base >= MAX_PHYS)
+		return;
+	end = base + len;
+	if (end > MAX_PHYS)
+		end = MAX_PHYS;
+	p0 = (UINT32)(base / PAGE_SIZE_);
+	p1 = (UINT32)((end + PAGE_SIZE_ - 1) / PAGE_SIZE_);
+	for (p = p0; p < p1; p++)
+		page_set_used(p);
+}
+
+static void build_e820(EFI_MEMORY_DESCRIPTOR *map, UINTN map_size,
+		       UINTN desc_size, const struct fb_saved *fb)
+{
+	volatile UINT32 *hdr = (volatile UINT32 *)E820_ADDR;
+	volatile struct e820_entry_disk *entries =
+		(volatile struct e820_entry_disk *)(E820_ADDR + 8);
+	UINTN n = desc_size ? map_size / desc_size : 0;
+
+	__asm__ volatile ("" : "+r"(hdr), "+r"(entries));
+	UINTN i;
+	UINT32 p, count = 0;
+	UINT64 total = 0;
+
+	for (i = 0; i < sizeof(page_used); i++)
+		page_used[i] = 0xFF;
+
+	for (i = 0; i < n; i++) {
+		EFI_MEMORY_DESCRIPTOR *d =
+			(EFI_MEMORY_DESCRIPTOR *)((UINT8 *)map + i * desc_size);
+		UINT64 len = d->NumberOfPages * PAGE_SIZE_;
+
+		if (d->Type == EFI_CONVENTIONAL_MEMORY ||
+		    d->Type == EFI_BOOT_SERVICES_CODE ||
+		    d->Type == EFI_BOOT_SERVICES_DATA)
+			free_range(d->PhysicalStart, len);
+	}
+
+	/* The scanout is MMIO, not RAM; make sure it can never be handed out
+	 * even when its BAR happens to sit below the 128 MB ceiling. */
+	if (fb->valid)
+		use_range(fb->base, fb->size);
+
+	for (p = 0; p < MAX_PAGES; ) {
+		UINT32 start;
+
+		if (page_is_used(p)) {
+			p++;
+			continue;
+		}
+		start = p;
+		while (p < MAX_PAGES && !page_is_used(p))
+			p++;
+		if (count < 32) {
+			UINT64 len = (UINT64)(p - start) * PAGE_SIZE_;
+
+			entries[count].base = (UINT64)start * PAGE_SIZE_;
+			entries[count].length = len;
+			entries[count].type = 1;   /* E820_RAM */
+			entries[count].attrs = 0;
+			count++;
+			total += len;
+		}
+	}
+
+	hdr[0] = 0xE820E820u;
+	hdr[1] = count;
+
+	com1_puts("nebos-efi: E820 ");
+	com1_dec(count);
+	com1_puts(" entries, ");
+	com1_dec((UINT32)(total >> 20));
+	com1_puts(" MB usable\n");
+}
+
+/* ---- boot structures --------------------------------------------------- */
+
+static void write_bootinfo(UINT32 rsdp, int have_fb, int rgbx)
+{
+	volatile struct boot_info *bi =
+		(volatile struct boot_info *)BOOT_INFO_ADDR;
+	UINT32 words[8];
+	unsigned int i;
+	UINT32 sum = 0;
+
+	__asm__ volatile ("" : "+r"(bi));
+
+	words[0] = BOOT_INFO_MAGIC;
+	words[1] = BOOT_INFO_VERSION;
+	words[2] = 0;
+	words[3] = rsdp;
+	words[4] = BOOTINFO_FLAG_EFI |
+		((have_fb && rgbx) ? BOOTINFO_FLAG_RGBX : 0);
+	words[5] = words[6] = words[7] = 0;
+	for (i = 0; i < 8; i++)
+		sum += words[i];
+	words[2] = 0u - sum;   /* checksum over the eight dwords is zero */
+
+	bi->magic = words[0];
+	bi->version = words[1];
+	bi->checksum = words[2];
+	bi->rsdp = words[3];
+	bi->flags = words[4];
+	for (i = 0; i < 3; i++)
+		bi->reserved[i] = words[5 + i];
+}
+
+static void write_fbinfo(const struct fb_saved *fb)
+{
+	volatile struct vga_boot_fb_info *info = VGA_BOOT_FB;
+
+	__asm__ volatile ("" : "+r"(info));
+	if (!fb->valid) {
+		info->valid = 0;
+		info->width = 0;
+		info->height = 0;
+		info->pitch = 0;
+		info->bpp = 0;
+		info->lfb_base = 0;
+		info->vram_64k = 0;
+		return;
+	}
+
+	info->valid = 1;
+	info->width = (unsigned short)fb->width;
+	info->height = (unsigned short)fb->height;
+	info->pitch = (unsigned short)fb->pitch;
+	info->bpp = (unsigned char)fb->bpp;
+	info->lfb_base = (unsigned int)fb->base;
+	{
+		UINT64 blocks = fb->size >> 16;
+
+		info->vram_64k = (unsigned short)(blocks > 0xFFFF ? 0xFFFF :
+						  blocks);
+	}
+}
+
+static void copy_trampoline(void)
+{
+	UINT32 n = (UINT32)(tramp_blob_end - tramp_blob_start);
+	UINT8 *dst = (UINT8 *)(UINTN)TRAMP_ADDR;
+	UINT32 i;
+
+	if (n == 0 || n > TRAMP_MAX)
+		fail_after("trampoline blob size invalid");
+	__asm__ volatile ("" : "+r"(dst));
+	for (i = 0; i < n; i++)
+		dst[i] = tramp_blob_start[i];
+	com1_puts("nebos-efi: trampoline ");
+	com1_dec(n);
+	com1_puts(" bytes at 0x");
+	com1_hex(TRAMP_ADDR, 3);
+	com1_puts("\n");
+}
+
+/* ---- entry ------------------------------------------------------------- */
+
+EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *st)
+{
+	EFI_BOOT_SERVICES *bs;
+	EFI_GRAPHICS_OUTPUT_PROTOCOL *gop = 0;
+	EFI_GRAPHICS_OUTPUT_MODE_INFORMATION info;
+	struct fb_saved fb;
+	EFI_PHYSICAL_ADDRESS staging = 0xFFFFFFFFull;
+	UINTN map_size, map_key = 0, desc_size = 0;
+	UINT32 desc_ver = 0;
+	UINT32 rsdp;
+	UINT32 kernel_size;
+	UINTN pages, i, tries;
+	EFI_STATUS status;
+
+	com1_init();
+	com1_puts("nebos-efi: start\n");
+
+	if (!st || !st->BootServices)
+		fail_before(st, "no boot services");
+	bs = st->BootServices;
+
+	bs->SetWatchdogTimer(0, 0, 0, 0);
+
+	kernel_size = (UINT32)(kernel_image_end - kernel_image_start);
+	if (kernel_size == 0 || kernel_size > KERNEL_MAX)
+		fail_before(st, "kernel image size invalid");
+	com1_puts("nebos-efi: kernel ");
+	com1_dec(kernel_size);
+	com1_puts(" bytes, entry 0x");
+	com1_hex((UINT64)KERNEL_ENTRY, 8);
+	com1_puts("\n");
+
+	/* Video: prefer BGRX (the byte order the kernel's shadow buffer uses);
+	 * RGBX is handled by a swizzling flush. */
+	fb.valid = 0;
+	fb.rgbx = 0;
+	if (gop_init(st, &gop, &info)) {
+		fb.valid = 1;
+		fb.rgbx = (info.PixelFormat == EFI_PIXEL_RGBX);
+		fb.width = info.HorizontalResolution;
+		fb.height = info.VerticalResolution;
+		fb.pitch = info.PixelsPerScanLine * 4;
+		fb.bpp = 32;
+		fb.base = gop->Mode->FrameBufferBase;
+		fb.size = gop->Mode->FrameBufferSize;
+		com1_puts("nebos-efi: GOP ");
+		com1_dec(fb.width);
+		com1_puts("x");
+		com1_dec(fb.height);
+		com1_puts(fb.rgbx ? " RGBX" : " BGRX");
+		com1_puts(" pitch ");
+		com1_dec(fb.pitch);
+		com1_puts(" fb 0x");
+		com1_hex(fb.base, 8);
+		com1_puts("\n");
+	} else {
+		com1_puts("nebos-efi: no usable GOP mode, booting headless\n");
+	}
+
+	rsdp = find_rsdp(st);
+	if (rsdp) {
+		com1_puts("nebos-efi: ACPI RSDP 0x");
+		com1_hex(rsdp, 8);
+		com1_puts("\n");
+	}
+
+	/* Stage the kernel below 4 GB; the low-memory trampoline copies it to
+	 * 0x7E00 after ExitBootServices, when low memory is ours. */
+	pages = (kernel_size + PAGE_SIZE_ - 1) / PAGE_SIZE_;
+	status = bs->AllocatePages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
+				   pages, &staging);
+	if (status || staging > 0xFFFFFFFFull || staging < 0x100000ull ||
+	    staging + kernel_size > 0x100000000ull)
+		fail_before(st, "cannot allocate kernel staging buffer");
+	for (i = 0; i < kernel_size; i++)
+		((UINT8 *)(UINTN)staging)[i] = kernel_image_start[i];
+	com1_puts("nebos-efi: staged at 0x");
+	com1_hex(staging, 8);
+	com1_puts("\n");
+
+	/* GetMemoryMap + ExitBootServices.  A map-key change makes the call
+	 * fail; fetch a fresh map and retry. */
+	for (tries = 0; tries < 4; tries++) {
+		map_size = sizeof(memmap_buf);
+		status = bs->GetMemoryMap(&map_size,
+					  (EFI_MEMORY_DESCRIPTOR *)memmap_buf,
+					  &map_key, &desc_size, &desc_ver);
+		if (status) {
+			com1_puts("nebos-efi: GetMemoryMap status 0x");
+			com1_hex(status, 16);
+			com1_puts("\n");
+			fail_before(st, "GetMemoryMap failed");
+		}
+		status = bs->ExitBootServices(image, map_key);
+		if (!status)
+			break;
+	}
+	if (status)
+		fail_before(st, "ExitBootServices failed");
+
+	/* From here on no firmware services are allowed.  The firmware IDT and
+	 * timer are still live until the kernel installs its own, so keep
+	 * interrupts off. */
+	__asm__ volatile ("cli");
+	com1_puts("nebos-efi: boot services exited\n");
+
+	if (!desc_size || map_size < desc_size)
+		fail_after("memory map is empty");
+	build_e820((EFI_MEMORY_DESCRIPTOR *)memmap_buf, map_size, desc_size,
+		   &fb);
+	write_bootinfo(rsdp, fb.valid, fb.rgbx);
+	write_fbinfo(&fb);
+	copy_trampoline();
+
+	com1_puts("nebos-efi: jumping to kernel\n");
+	((void (*)(UINT32, UINT32, UINT32))(UINTN)TRAMP_ADDR)
+		((UINT32)staging, kernel_size, (UINT32)KERNEL_ENTRY);
+
+	/* The trampoline never returns. */
+	halt_forever();
+	return EFI_SUCCESS;
+}

+ 16 - 0
efi/payload.S

@@ -0,0 +1,16 @@
+/* Flat kernel image and the 0x800 trampoline, embedded in BOOTX64.EFI. */
+
+.section .data,"dw"
+.globl kernel_image_start
+.globl kernel_image_end
+.align 16
+kernel_image_start:
+.incbin "kernel.bin"
+kernel_image_end:
+
+.align 16
+.globl tramp_blob_start
+.globl tramp_blob_end
+tramp_blob_start:
+.incbin "efi/tramp.bin"
+tramp_blob_end:

+ 97 - 0
efi/tramp.S

@@ -0,0 +1,97 @@
+/*
+ * UEFI -> 32-bit kernel handoff trampoline.
+ *
+ * Linked at 0x800 and copied there by the loader after ExitBootServices.
+ * Everything internal uses 0x800-based absolute addresses, so the blob is
+ * position-independent until it is copied to its linked address.
+ *
+ * Entered in 64-bit mode with the Microsoft x64 ABI:
+ *   ecx = staging physical address (< 4 GB)
+ *   edx = byte count
+ *   r8d = kernel_entry address
+ *
+ * Leaves long mode (compatibility mode, then protected mode), copies the
+ * staged image to 0x7E00, and jumps to the kernel with paging off.
+ */
+
+.code64
+.section .text
+.globl _start
+_start:
+	cli
+	movl	%ecx, param_staging(%rip)
+	movl	%edx, param_size(%rip)
+	movl	%r8d, param_entry(%rip)
+
+	lgdt	gdt_desc
+	movl	$0x08, %eax
+	pushq	%rax
+	movl	$compat, %edx
+	pushq	%rdx
+	lretq
+
+.code32
+compat:
+	/* Compatibility mode: 32-bit code, still paging in the firmware's
+	 * 64-bit page tables.  Leave IA-32e mode in the documented order. */
+	movl	%cr0, %eax
+	andl	$0x7FFFFFFF, %eax	/* CR0.PG = 0 */
+	movl	%eax, %cr0
+
+	movl	$0xC0000080, %ecx	/* IA32_EFER */
+	rdmsr
+	andl	$0xFFFFFEFF, %eax	/* LME = 0 */
+	wrmsr
+
+	movl	%cr4, %eax
+	/* Clear PAE(5), PGE(7), LA57(12), PCIDE(17), SMEP(20), SMAP(21).
+	 * The kernel's page tables are non-PAE and its ring-0 code accesses
+	 * user pages; the firmware's global TLB entries must not survive. */
+	andl	$0xFFCDEF5F, %eax
+	movl	%eax, %cr4
+
+	movl	%cr0, %eax
+	andl	$0xFFFFFFF3, %eax	/* TS = 0, EM = 0 */
+	movl	%eax, %cr0
+
+	ljmp	$0x08, $copy32
+
+copy32:
+	movw	$0x10, %ax
+	movw	%ax, %ds
+	movw	%ax, %es
+	movw	%ax, %fs
+	movw	%ax, %gs
+	movw	%ax, %ss
+	movl	$0x7C00, %esp
+
+	movl	param_staging, %esi
+	movl	$0x7E00, %edi
+	movl	param_size, %ecx
+	cld
+	rep	movsb
+	wbinvd
+
+	movl	param_entry, %edx
+	jmp	*%edx
+
+.align 8
+gdt:
+	.quad	0
+	.word	0xFFFF, 0x0000
+	.byte	0x00, 0x9A, 0xCF, 0x00	/* 0x08: 32-bit code */
+	.word	0xFFFF, 0x0000
+	.byte	0x00, 0x92, 0xCF, 0x00	/* 0x10: data */
+gdt_end:
+	.align 2
+gdt_desc:
+	.word	gdt_end - gdt - 1
+	.quad	gdt
+
+.align 4
+param_staging:
+	.long	0
+param_size:
+	.long	0
+param_entry:
+	.long	0

+ 13 - 0
efi/tramp.ld

@@ -0,0 +1,13 @@
+/* Handoff trampoline at 0x800 (the same low-memory slot kexec uses). */
+ENTRY(_start)
+
+SECTIONS
+{
+	. = 0x800;
+	.text : {
+		*(.text)
+		*(.data .data.*)
+		*(.rodata .rodata.*)
+	}
+	/DISCARD/ : { *(.eh_frame) *(.note*) *(.comment) }
+}

+ 4 - 2
flash.sh

@@ -8,12 +8,14 @@
 # prints a summary and needs "yes" before writing anything.
 # prints a summary and needs "yes" before writing anything.
 set -eu
 set -eu
 
 
-IMG=usb.img
+# Third argument (or IMG= in the environment) selects the image.  Passing it
+# as an argument keeps it alive across the sudo re-exec below.
+IMG=${3:-${IMG:-usb.img}}
 dev=${1:-}
 dev=${1:-}
 force=${2:-}
 force=${2:-}
 
 
 if [ -z "$dev" ]; then
 if [ -z "$dev" ]; then
-	echo "usage: make flash USB=/dev/sdX [FORCE=1]" >&2
+	echo "usage: make flash USB=/dev/sdX [FORCE=1] [IMAGE]" >&2
 	exit 1
 	exit 1
 fi
 fi
 
 

+ 6 - 3
mouse.c

@@ -1,4 +1,5 @@
 #include "mouse.h"
 #include "mouse.h"
+#include "bootinfo.h"
 #include "cirrus.h"
 #include "cirrus.h"
 #include "radeon.h"
 #include "radeon.h"
 #include "timer.h"
 #include "timer.h"
@@ -193,8 +194,10 @@ static void mouse_cursor_setup(void)
 	if (fbw < MOUSE_CUR_W || fbh < MOUSE_CUR_H)
 	if (fbw < MOUSE_CUR_W || fbh < MOUSE_CUR_H)
 		return;
 		return;
 
 
-	/* Cirrus hardware cursor: the card overlays it; no save/restore. */
-	if (cirrus_available()) {
+	/* Cirrus hardware cursor: the card overlays it; no save/restore.
+	 * UEFI GOP modes are not programmed through the legacy VGA sequencer,
+	 * so use the software cursor there. */
+	if (!bootinfo_efi() && cirrus_available()) {
 		mouse_hw_cursor = 1;
 		mouse_hw_cursor = 1;
 		cur_x = 100;
 		cur_x = 100;
 		cur_y = 100;
 		cur_y = 100;
@@ -205,7 +208,7 @@ static void mouse_cursor_setup(void)
 	}
 	}
 
 
 	/* Radeon (R200/RV280) hardware cursor. */
 	/* Radeon (R200/RV280) hardware cursor. */
-	if (radeon_available()) {
+	if (!bootinfo_efi() && radeon_available()) {
 		mouse_hw_cursor = 2;
 		mouse_hw_cursor = 2;
 		cur_x = 100;
 		cur_x = 100;
 		cur_y = 100;
 		cur_y = 100;

+ 5 - 2
panic.c

@@ -1,6 +1,7 @@
 /* Fatal exception screen: see panic.h. */
 /* Fatal exception screen: see panic.h. */
 
 
 #include "panic.h"
 #include "panic.h"
+#include "bootinfo.h"
 #include "font_8x8.h"
 #include "font_8x8.h"
 #include "process.h"
 #include "process.h"
 #include "serial.h"
 #include "serial.h"
@@ -15,6 +16,7 @@
 static unsigned int fb_w, fb_h, fb_pitch, fb_bpp;
 static unsigned int fb_w, fb_h, fb_pitch, fb_bpp;
 static volatile unsigned char *fb_lfb;
 static volatile unsigned char *fb_lfb;
 static int fb_ok;
 static int fb_ok;
+static int fb_swap_rb;
 static unsigned int panic_line;
 static unsigned int panic_line;
 
 
 static const char hx[] = "0123456789ABCDEF";
 static const char hx[] = "0123456789ABCDEF";
@@ -34,6 +36,7 @@ static void cache_fb(void)
 	fb_pitch = vga_fb_pitch();
 	fb_pitch = vga_fb_pitch();
 	fb_bpp = vga_fb_bpp();
 	fb_bpp = vga_fb_bpp();
 	fb_lfb = (volatile unsigned char *)(unsigned long)vga_fb_lfb_addr();
 	fb_lfb = (volatile unsigned char *)(unsigned long)vga_fb_lfb_addr();
+	fb_swap_rb = bootinfo_rgbx();
 	fb_ok = (fb_w > 0 && fb_h > 0 && fb_pitch > 0 && fb_lfb &&
 	fb_ok = (fb_w > 0 && fb_h > 0 && fb_pitch > 0 && fb_lfb &&
 		 (fb_bpp == 16 || fb_bpp == 24 || fb_bpp == 32));
 		 (fb_bpp == 16 || fb_bpp == 24 || fb_bpp == 32));
 }
 }
@@ -51,9 +54,9 @@ static void px(unsigned int x, unsigned int y,
 		p[0] = (unsigned char)(v & 0xFF);
 		p[0] = (unsigned char)(v & 0xFF);
 		p[1] = (unsigned char)(v >> 8);
 		p[1] = (unsigned char)(v >> 8);
 	} else {
 	} else {
-		p[0] = (unsigned char)b;
+		p[0] = (unsigned char)(fb_swap_rb ? r : b);
 		p[1] = (unsigned char)g;
 		p[1] = (unsigned char)g;
-		p[2] = (unsigned char)r;
+		p[2] = (unsigned char)(fb_swap_rb ? b : r);
 		if (fb_bpp == 32)
 		if (fb_bpp == 32)
 			p[3] = 0;
 			p[3] = 0;
 	}
 	}

+ 4 - 0
vesa_stage.S

@@ -23,6 +23,10 @@ _vesa_stage:
 	movw  %ax, %ds
 	movw  %ax, %ds
 	movw  %ax, %es
 	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. */
+	movl  $0, 0x500
+
 	/* ---- Probe INT 15h E820 memory map into 0:0x1000 ----
 	/* ---- Probe INT 15h E820 memory map into 0:0x1000 ----
 	 * Layout (see mm.h): [magic u32][count u32][24-byte entries...].
 	 * Layout (see mm.h): [magic u32][count u32][24-byte entries...].
 	 * Stored below the kernel (0x7E00) so it survives kexec reloads,
 	 * Stored below the kernel (0x7E00) so it survives kexec reloads,

+ 34 - 5
vga.c

@@ -1,6 +1,7 @@
 /* VGA: VESA LFB (from boot) with a shadow buffer and a damage-rect flush. */
 /* VGA: VESA LFB (from boot) with a shadow buffer and a damage-rect flush. */
 
 
 #include "vga.h"
 #include "vga.h"
+#include "bootinfo.h"
 #include "blit.h"
 #include "blit.h"
 #include "pci.h"
 #include "pci.h"
 #include "gpu.h"
 #include "gpu.h"
@@ -17,6 +18,9 @@ static unsigned int vga_fb_w, vga_fb_h, vga_fb_pitch_val, vga_fb_bpp_val;
 static unsigned int vga_fb_vram_kb_val;
 static unsigned int vga_fb_vram_kb_val;
 static volatile unsigned char *vga_fb_lfb;
 static volatile unsigned char *vga_fb_lfb;
 static int vga_fb_cached;
 static int vga_fb_cached;
+/* UEFI GOP can hand us RGBX instead of the VBE/BGRX byte order.  The shadow
+ * buffer is always BGRX, so only the shadow->LFB flush has to swizzle. */
+static int lfb_swap_rb;
 
 
 /* Shadow buffer state. */
 /* Shadow buffer state. */
 static unsigned char *vga_shadow_buf;
 static unsigned char *vga_shadow_buf;
@@ -58,6 +62,15 @@ static inline void irq_restore(unsigned int flags)
 	__asm__ volatile ("pushl %0; popfl" :: "r"(flags));
 	__asm__ volatile ("pushl %0; popfl" :: "r"(flags));
 }
 }
 
 
+/* Shadow pixels are BGRX (B in the low byte, X in the high byte).  A GOP
+ * RGBX scanout wants R in the low byte; swap the two colour bytes and keep
+ * green and the reserved byte where they are. */
+static inline unsigned int swap_rb32(unsigned int v)
+{
+	return (v & 0xFF00FF00u) | ((v & 0x000000FFu) << 16) |
+	       ((v >> 16) & 0x000000FFu);
+}
+
 static void damage_add_locked(unsigned int x0, unsigned int y0,
 static void damage_add_locked(unsigned int x0, unsigned int y0,
 			      unsigned int x1, unsigned int y1)
 			      unsigned int x1, unsigned int y1)
 {
 {
@@ -233,6 +246,7 @@ static void vga_cache_boot_fb(void)
 	if (vga_fb_cached)
 	if (vga_fb_cached)
 		return;
 		return;
 	vga_fb_cached = 1;
 	vga_fb_cached = 1;
+	lfb_swap_rb = bootinfo_rgbx();
 	info = vga_get_boot_fb();
 	info = vga_get_boot_fb();
 	if (!info->valid) {
 	if (!info->valid) {
 		vga_fb_w = vga_fb_h = vga_fb_pitch_val = vga_fb_bpp_val = 0;
 		vga_fb_w = vga_fb_h = vga_fb_pitch_val = vga_fb_bpp_val = 0;
@@ -266,7 +280,10 @@ static void vga_cache_boot_fb(void)
 			vga_fb_h = max_h;
 			vga_fb_h = max_h;
 	}
 	}
 	gpu_init(info->lfb_base);
 	gpu_init(info->lfb_base);
-	if (gpu_vendor() == GPU_VENDOR_ATI && !ati_is_radeon())
+	/* The CRTC reset is a VBE-mode-set fixup.  Under UEFI the GOP owns the
+	 * mode; programming the legacy CRTC would break it. */
+	if (gpu_vendor() == GPU_VENDOR_ATI && !ati_is_radeon() &&
+	    !bootinfo_efi())
 		vga_reset_crtc();
 		vga_reset_crtc();
 }
 }
 
 
@@ -535,7 +552,10 @@ void vga_shadow_init(void)
 	 * instead, with a WC MTRR over the stolen range -- and only switch once
 	 * instead, with a WC MTRR over the stolen range -- and only switch once
 	 * that entry is in place, otherwise the alias would be cached and the
 	 * that entry is in place, otherwise the alias would be cached and the
 	 * screen would not update. */
 	 * screen would not update. */
-	if (gpu_vendor() == GPU_VENDOR_NVIDIA) {
+	/* The stolen-VRAM alias hunt is for the VBE BAR3 window on ION-class
+	 * hardware.  A GOP framebuffer is already the display buffer; touching
+	 * the BARs / scanning VRAM there can only break the firmware's mode. */
+	if (gpu_vendor() == GPU_VENDOR_NVIDIA && !bootinfo_efi()) {
 		unsigned int alias = nvidia_lfb_in_vram();
 		unsigned int alias = nvidia_lfb_in_vram();
 
 
 		if (alias) {
 		if (alias) {
@@ -611,11 +631,20 @@ static unsigned int shadow_copy_damage(unsigned int max_bytes)
 
 
 			/* Streaming stores: VRAM is WC (or UC on machines where the
 			/* Streaming stores: VRAM is WC (or UC on machines where the
 			 * MTRR cannot cover it) and rep movsl does not combine
 			 * MTRR cannot cover it) and rep movsl does not combine
-			 * there, while movntdq does. */
-			if (use_sse2)
+			 * there, while movntdq does.  A GOP RGBX scanout needs the
+			 * BGRX shadow swizzled while it is copied. */
+			if (lfb_swap_rb && pb == 4) {
+				const unsigned int *s32 = (const unsigned int *)s;
+				unsigned int *t32 = (unsigned int *)t;
+				unsigned int k;
+
+				for (k = 0; k < dwords; k++)
+					t32[k] = swap_rb32(s32[k]);
+			} else if (use_sse2) {
 				vga_fb_copy_row_nt(t, s, dwords);
 				vga_fb_copy_row_nt(t, s, dwords);
-			else
+			} else {
 				vga_fb_copy_row(t, s, dwords);
 				vga_fb_copy_row(t, s, dwords);
+			}
 			if (tail) {
 			if (tail) {
 				unsigned int k;
 				unsigned int k;
 				for (k = dwords * 4; k < row_bytes; k++)
 				for (k = dwords * 4; k < row_bytes; k++)