Simple bootloader

  • Duration: 2008 - 2010
  • Language used: Intel assembly, C++
  • Platform: Windows Vista

Brief

This is a simple bootloader I wrote for self-interest during my Bachelors, that loads, relocates, and runs a 16 bit C++ executable into memory from a bootable CD. You can run it on an Intel machine, or use VirtualBox.

Details

I started getting interested in Operating Systems after reading parts of the MINIX book. Later, I read the Intel Software Developer's Manual, which explains 32-protected mode, paging, and how to switch between 16 bit to 32 bit mode during boot. To understand the boot process better, I decided to write a simple bootloader myself - a tiny step forward to understanding Operating Systems.

I first tried two experiments - booting from a Floppy and booting from a CD. In the floppy experiment, I wrote a simple assembly code (a COM file) that starts execution from 07c00h of the RAM - it simply prints "Hello" on the screen using BIOS routines. After going through the "El-Torito Bootable CD Specification", I wrote a program that generates a bootable iso image file from a binary image file, to load the COM file, but from a CD instead. After this, I started going through Linux Internals to understand better about virtual memory & paging (MINIX doesn't support paging) from the book Understanding the Linux Kernel. At the same time, I started reading another excellent book - The Indispensable PC Hardware Book - this book explains the old PC AT XT architectures, and lots more.

So far, I was using a real machine for testing. I came across a emulator called Bochs (and later VirtualBox, which is much faster since it runs directly on the hardware). I wrote a Makefile sequence that would compile an image, convert it into a bootable ISO CD image using MagicISO, and run it in Bochs.

With the build system in place, I wrote a simple bootloader to load, relocate, and run a C++ program into memory from the bootable CD image. Later, I switched to a GNU Makefile setup and the GNU assembler. The GNU linker script makes it much easier to control the structure of the output image.

Boot sequence

  1. The bootloader is loaded into RAM by the computer, from the bootable CD image
  2. The bootloader loads the C++ image into a suitable location in memory
  3. The bootloader relocates the addresses in the C++ image, using the information in the relocation table
  4. The bootloader calls all the global C++ object constructors, using information from the exe file
  5. The bootloader jumps into the C++ image and starts execution

This is a screenshot in Bochs after boot. The C++ program simply prints out the letter 'B' multiple times on screen:

Bootloader execution screenshot

Experience gained

Boot procedure, writing linker scripts, relocation, EXE file format, C++ object initialization

References

  1. Operating Systems Design and Implementation (The MINIX Book): For MINIX OS internals
  2. Operating System Principles (The Dinosaur Book): For OS concepts
  3. Intel Software Developer's Manuals: For understanding the Intel 32-bit protected mode
  4. El-Torito Bootable CD Specification: Structure of the bootable ISO CD image
  5. Understanding the Linux Kernel: For Linux OS internals
  6. Indispensable PC Hardware Book: For bus system operation

Downloads