Skip to content

Repository files navigation

RealMode-Shim

This project implements a multi-stage bootloader shim designed to transition from a 32-bit Multiboot environment (like QEMU's -kernel option) or a 64-bit UEFI environment back to 16-bit real mode. The included MS-DOS boot is provided as an example payload.

Current State

The QEMU -kernel code path successfully loads and executes the MS-DOS io.sys file. Upon execution, io.sys displays:

Non-System disk or error
Replace and press any key when ready

This error occurs because no formatted hard drives are available for DOS to read from. The MS-DOS payload is included as an example and served as the initial motivation for the project.

Future Goals

  • Implement INT 0x13 HDD simulation pointing to an in-memory FAT12 disk image containing msdos.sys and command.com
  • Initialize VGA BIOS after exiting UEFI to enable VGA functionality for real-mode applications on modern 64-bit computers
  • Test on real hardware
  • Develop a driver for mounting DOS RAM drives in extended memory, accessible to real-mode DOS, potentially using techniques similar to EMM386 (hosting real-mode DOS in a virtual machine)

Architecture

The boot process consists of three stages, with different entry points depending on the boot method:

Common Payload

  • Stage 2 (realmode.asm):
    • Mode: 16-bit Real Mode
    • Entry: 0x4000 (0x0400:0x0000)
    • Function: Sets Video Mode 3 (80x25 VGA), loads the embedded MS-DOS io.sys to 0x0070:0x0000, and executes it.

Legacy BIOS (Multiboot) Path

  1. Stage 0 (loader.asm):
    • Mode: 32-bit Protected Mode
    • Entry: 0x100000 (Multiboot)
    • Function: Copies Stage 1 to 0x3200 and jumps to it.
  2. Stage 1 (stage1_32_protected.asm):
    • Mode: 32-bit Protected Mode
    • Entry: 0x3200
    • Function: Copies Stage 2 to 0x4000, switches to 16-bit Real Mode, and jumps to Stage 2.

UEFI Path

  1. UEFI Loader (uefi_loader.c):
    • Mode: 64-bit Long Mode (UEFI)
    • Function: Allocates memory at 0x1000, copies the Trampoline to 0x1000 and Stage 1 to 0x3200, sets Console Mode 0, exits Boot Services, and jumps to the Trampoline.
  2. Trampoline (uefi_entry.asm):
    • Mode: 64-bit Long Mode
    • Entry: 0x1000
    • Function: Switches CPU to 32-bit Compatibility Mode, disables Paging/Long Mode to enter 32-bit Protected Mode, and jumps to Stage 1 at 0x3200.
  3. Stage 1 (stage1_32_protected.asm):
    • Mode: 32-bit Protected Mode
    • Entry: 0x3200
    • Function: Same as Legacy Path (Copies Stage 2, switches to Real Mode).

Prerequisites

  • NASM: Assembler
  • GCC: C Compiler (native Linux)
  • GNU-EFI: UEFI development libraries (libgnuefi, libefi)
  • QEMU: Emulator (qemu-system-i386 and qemu-system-x86_64)
  • OVMF: UEFI Firmware for QEMU

Usage

Legacy BIOS (Multiboot)

To assemble and link the kernel:

make

To boot the kernel in QEMU:

make run

UEFI

To build the UEFI application (BOOTX64.EFI):

make -f Makefile.uefi

To run the UEFI application in QEMU:

make -f Makefile.uefi run

Clean

To remove build artifacts:

make clean
make -f Makefile.uefi clean

About

This project implements a multi-stage bootloader shim designed to transition from a 32-bit Multiboot environment (like QEMU's `-kernel` option) or a 64-bit UEFI environment back to 16-bit real mode.

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages