What is a boot Loader
Before we start writing a boot loader, let us know what exactly a boot loader is, A boot loader is a small program (sequence of instructions ) that gets executed when the system boots. This instructions is responsible to boot the rest of the operating system.A boot loader must be written in the first 512 bytes of any storage device. On the POST process the BIOS check the first 512 bytes of each sector in order to check weather the device is bootable or not.Thus there should be some delimiter which indicates that the current disk is bootable. This is achieved by a signature in 511 and 512 th bytes of the first sector of the device, They should be respectively aa55 ( which makes 2 byte ).This signature makes BIOS treat that the device is bootable and it starts executing the instructions from the 0 th byte in the disk.
NASM code to write a very basic boot loader
loop: jmp $ ; Waits for infinite time ( a infinite loop )
times 510 - ($ - $$ ) db 0 ; fill all the 510 bytes with 0's ( except the jmp instruction )
dw 0xAA55 ; signature of boot loader
The above code will make a empty boot loader , It does nothing but halts on boot up.
NASM code to print hello in boot loader
mov ah,0x0e ; to print a character 0 x 0 emov al,'h' ; Print ' hello'
int 0x10
mov al,'e'
int 0x10
mov al,'l'
int 0x10
mov al,'l'
int 0x10
mov al,''
int 0x10
loop: jmp $ ; Waits for infinite time ( a infinite loop )
times 510 - ($ - $$ ) db 0 ; fill all the 510 bytes with 0's ( except the jmp instruction )
dw 0xAA55 ; signature of boot loader
Compile your bootloader into a binary file
In Linux Systems : nasm ./bootloader.asm -f bin -o ./bootloaderThe above command will create a .bin ( binary image ) of your boot loader, you can explore the binary file with GHEX tool for verification.
Burn the Boot loader into a USB device
To burn the boot loader into your USB device use the DD command in Linux systemsdd if="./bootloader.bin" od="/dev/sdb" bs=512
The above command will burn the boot loader into first 512 Byte of the pen drive making it bootable.
Now finally restart the computer and enjoy the boot loader, Also you can extend the boot loader with other BIOS interrupts .
0 comments:
Post a Comment