“Hello World” Boot Loader

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

Step 2
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

Step 3
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

Step 4
Testing your boot loader program

You can test your program using ‘qemu’ emulator

Command for 32-bit systems :

qemu-system-i386 hellow_world_bootloader.bin

Command for 64-bit systems :

qemu-system-x86_64 hello_world_bootloader.bin

Or

You can load the image file(.img) created in step 3 to a virtual machine and see your boot loader in action.

Advertisements

One comment on ““Hello World” Boot Loader

  1. Pingback: Qemu and simple Bootloader | misrapankaj

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s