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:
- Format a floppy disk: Open a terminal and run the command
sudo fdisk /dev/floppy0. This will format the floppy disk. - Write the bootloader: Use the
ddcommand to write theboot.binfile 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:
- 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/
- Extract the downloaded image: Extract the downloaded archive to a directory on your computer.
- 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. Thefileoption specifies the floppy disk image file (floppy0.img) and theformatoption specifies the floppy disk format (raw).
Note:
- The
boot.binfile 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:
- Verify the BIOS prompt: The BIOS prompt should look similar to the following:
Booting from floppy disk...
Press any key to interrupt boot...
-
Press any key: Pressing any key will interrupt the boot process and bring you to the command prompt.
-
Verify the floppy disk is recognized: Type the following command to list the available drives:
ls /dev/fd*
-
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).
-
Test the bootloader: Type the following command to test the bootloader:
reboot
- 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-imgto create the floppy disk image from theboot.binfile. - 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.