Ok.. If you've made it this far.. well.. there's not really much to say. You installed a couple programs. Well, now you will use those programs to create a bootable program - the First part of Operating System Development.
A Bootloader takes a flat binary file and boots it. Simple as that. However, there are many types of Bootloaders. There are:
- Single Stage Bootloaders - These are small 512B files that are the Bootloader AND the Operating System Kernel.
- Dual Stage Bootloaders - These use a small 512B file to load the Operating System. The 512B file is the Bootloader.
- Tri Stage Bootloaders - These use a small 512B file to load another Bootloader. The second Bootloader can be any size, and it can prepare things for the Operating System. We will be making this kind of Bootloader.
- Generic Bootloaders - These are Bootloaders like NTLDR and GRUB.
Now that you know what kinds of Bootloaders there are, and which one we'll be using, it's time to start coding!
Ok.. Let's start with a Bootloader Template.
- Code:
-
BITS 16
ORG 0x7C00
CLI
HLT
TIMES 510 - ($-$$) DB 0
DW 0xAA55
This Template starts off with
BITS 16 - This defines to the Assembler that we are producing 16BIT output.
Next comes
ORG 0x7C00 - This tells where to place the binary file in memory when booting. 0x7C00 is a hex number that points to where the BIOS gives control to a Bootloader.
Then comes
CLI - This clears all interrupts. Interrupts are the processor's way of saying "Something Happened that Requires me to do Something." The operating System sends interrupts to the processor, and it pauses whatever it is doing and executes whatever is in the IVT (Interrupt Vector Table - 16BIT) or the IDT (Interrupt Descriptor Table - 32BIT). It then returns to whatever it was doing before.
After
CLI comes
HLT - This halts the Processor, which tells it to do nothing until a reboot. This insures that no random code will be executed afterwords.
Lower down we see
TIMES 510 - ($-$$) DB 0 - This fills 510B of the program with 0's. This makes it so the program is exactly 512B in size. If it is not, it won't boot.
Lastly comes
DW 0xAA55 - This is the Boot Signature. This is required by the BIOS to verify that it is a Bootloader. Without it, it won't boot.
Ok, now that you have that typed in, save it as
BOOTFILE1.ASM.
Next, Open up CMD.EXE (MS-DOS Prompt). In it, type
cd "Path of BOOTFILE1.ASM" (without the quotes). This will change your current directory to the one where you saved the code.
Next, type
nasm -f bin BOOTFILE1.ASM -o BOOTFILE1.BIN into the command line. Hit enter. You have now assembled your first Bootloader. Next, inside the Bochs directory, you will find an application called
bximage.exe. Run it, and type in the following (hit enter between every line)
- Code:
-
fd
1.44
BOOTFILE1.IMG
Right click
BOOTFILE1.IMG, and select
Open With.... Select
Choose Default Program.... Next click
Browse.... Go to the folder where HxD was installed and select the HxD program. Hit OK on both windows.
After HxD Opens, do the same thing for
BOOTFILE1.BIN.
Copy the first 512B from
BOOTFILE1.BIN to
BOOTFILE1.IMG. Save and close both windows.
Now you have a floppy disk to work with. Right click it and select
Mount as ImDisk Virtual Disk.
Now go back to the Bochs directory. Inside it will be a file called
bochsrc-sample.txt. Copy it and rename the copied file as
bochsrc.txt. Open the file.
Edit the contents to look exactly like these:
- Code:
-
romimage: file="BIOS-bochs-latest", address=0xe0000
vgaromimage: file="VGABIOS-lgpl-latest"
floppya: 1_44=a:, status=inserted # Boot from drive A
log: OSDev.log # All errors and info logs will output to OSDev.log
error: action=report
info: action=report
Save that, and run Bochs. It will boot, and do nothing!
Congratulations! You made a bootable Program!!
Next step: Creating a bootable program that actually does something! This file you made will turn into a simple Bootloader that outputs text!