Ever wondered what a boot loader is….??
A boot loader is a computer program (assembly language) that loads the Operating System to the main memory. Boot loaders generally reside on the first sector of the memory. When we switch on a computer, the first program that runs is the BIOS which performs certain tests and them passes the CPU control to the Master Boot Record(MBR) which contains the boot loader program. Some of the most common linux boot loaders are LInux LOader(LILO), GRand Unified Boot loader(GRUB), LOAD LInux(LOADLIN), etc.
Again i was given the task of creating a custom boot loader program. First thing that came up when i researched, was that the boot loader programs are written in low level assembly language. Then we have to compile it using the NASM assembler. Then create an image of the boot loader to load in the memory. Lets take a step by step approh.
Step 1 :
Create the boot loader program
BITS 16 jmp main nop main: mov ax, 07C0h add ax, 288 mov ss, ax mov sp, 4096 mov ax, 07C0h mov ds, ax call PrintHelloWorld jmp .InfiniteLoop .InfiniteLoop: jmp .InfiniteLoop HelloWorld db "Hello World. This is from the bootloader", 0x0d, 0x0a, 0x00 PrintHelloWorld: mov si, HelloWorld call PrintStr ret PrintStr: push ax mov ah, 0Eh .loop: lodsb cmp al, 0x00 je .done int 10h jmp .loop .done: pop ax ret times 510-($-$$) db 0 dw 0xAA55
Compile the Code
You can compile the boot loader program using ‘nasm’ command.
nasm -f bin -o hello_world_bootloader.bin hello_world_bootloader.asm
Create Boot loader image
You need to create an empty file that will serve as the final image holding the bootloader. The command to create the file is given below.
dd if=/dev/zero of=hello_world_bootloader.img bs=512 count=2880
Now copy the bin file content to the image file
dd status=noxfer conv=notrunc if=hello_world_bootloader.bin of=hello_world_bootloader.img
Testing your boot loader program
You can test your program using ‘qemu’ emulator
Command for 32-bit systems :
Command for 64-bit systems :
You can load the image file(.img) created in step 3 to a virtual machine and see your boot loader in action.