Test Your Bootloader


Testing Your Bootloader in a Virtual Machine

In the previous article, we wrote a simple bootloader and saved it to a file named boot.bin. Now, we want to test this bootloader in a virtual machine to ensure it functions as expected. Here’s how we can achieve this:

Creating a Floppy Image:

  1. Format a floppy disk: Open a terminal and run the command sudo fdisk /dev/floppy0. This will format the floppy disk.
  2. Write the bootloader: Use the dd command to write the boot.bin file to the floppy disk. The command should look like this:
sudo dd bs=512 count=1 if=boot.bin of=/dev/floppy0

Starting the Virtual Machine:

  1. Download the QEMU virtual machine: You can download a pre-built QEMU image for the x86 architecture from the official website: https://www.qemu.org/download/
  2. Extract the downloaded image: Extract the downloaded archive to a directory on your computer.
  3. Run QEMU: Open a terminal and navigate to the extracted directory. Then, run the following command to launch the virtual machine:
qemu-system-x86_64 -boot d -drive file=/dev/floppy0,format=raw

Explanation of the Command:

  • qemu-system-x86_64: This specifies the x86_64 architecture for the virtual machine.
  • -boot d: This option tells QEMU to boot from the first hard disk drive (index 0).
  • -drive file=/dev/floppy0,format=raw: This option adds a floppy disk image to the virtual machine. The file option specifies the floppy disk image file (floppy0.img) and the format option specifies the floppy disk format (raw).

Note:

  • The boot.bin file should be in the same directory as the extracted QEMU image.
  • The virtual machine will boot and display the BIOS prompt.
  • Since the bootloader doesn’t load any operating system, it will halt after displaying the BIOS prompt.

Testing the Bootloader:

  1. Verify the BIOS prompt: The BIOS prompt should look similar to the following:
Booting from floppy disk...
Press any key to interrupt boot...
  1. Press any key: Pressing any key will interrupt the boot process and bring you to the command prompt.

  2. Verify the floppy disk is recognized: Type the following command to list the available drives:

ls /dev/fd*
  1. Confirm the floppy disk is recognized: If the floppy disk is recognized, the output should show the floppy disk device name (e.g., /dev/fd0).

  2. Test the bootloader: Type the following command to test the bootloader:

reboot
  1. Monitor the output: The virtual machine should reboot and display the BIOS prompt again.

Additional Notes:

  • If the virtual machine doesn’t boot or display the BIOS prompt, check if the floppy disk image is properly created and the QEMU command is correct.
  • You can also use a tool like qemu-img to create the floppy disk image from the boot.bin file.
  • This method can be used to test any bootloader, not just the one we wrote in the previous article.

By following these steps, you can test your bootloader in a virtual machine and ensure it functions as expected. This is a valuable step in the development process, as it allows you to debug and troubleshoot any issues before moving on to more complex operating systems.