From this article we are going to see Creating a Boot loader with hanging , Print a Char and Print a custom string on system start up.
Let we see the Operating systems and there booting sequence, so this part starts from the basics. In early days booting are done through the floppy disks.Floppy disks are 512 bytes . Here we use the NASM Netwide assembler to assemble the code.
When we power on the computer we will note that computer do a self test that is known as POST (Power On Self Test), Which have so many activities including search for bootable device. A device is bootable if and only it carries the following things
- Boot Sector with Byte Sequence 0x55, 0xAA , When we see this in bytes 511, 512
- Boot Sector is loaded into memory location , Normally in location 0, 0x7c00 some bios are loaded into 0x7c00 to 0
[BITS 16] - by this we are indicating the assembler that this is 16 bit.
[ORG 0x7C00] - by this we are indicating the assembler where the code will be in memory after loaded
At start the boot sector will look like this
ORG 0
jmp 0x7c00:start
start;
(or)
ORG 0x7c00
jmp 0x0000:start
start;
MBR table Entries uses, 16 bytes per entry this is written by Disk Partition program.
Offset | Size (bytes) | Description |
0x00 | 1 | Boot Indicator (0x80=bootable, 0x00=not bootable) |
0x01 | 1 | Starting Head Number |
0x02 | 2 | Starting Cylinder Number (10 bits) and Sector (6 bits) |
0x04 | 1 | Descriptor (Type of partition/filesystem) |
0x05 | 1 | Ending Head Number |
0x06 | 2 | Ending Cylinder and Sector numbers |
0x08 | 4 | Starting Sector (relative to beginning of disk) |
0x0C | 4 | Number of Sectors in partition |
Now we see an example how to make a bootable from a floppy using NASM.
CPU must starts in Real Mode . BIOS load the code at address 0 to 0x7c00 . This porgram will Filling the 512 bytes with zeros
; bootsec.asm
hanging:
jmp hanging
times 512-($-$$)db 0
Now we make a boot signature at the End 0xAA,0x55
; bootsec.asm
hanging:
jmp hanging
times 510-($-$$) db 0 ; reduce the 2 bytes for boot signature
db 0x55
db 0xAA
You can see the that cursor is blink in the screen and load is done , If you press the Ctrl+Alt+delete to make a reboot. How this is happening an Interrupt is being generated 0x09.
How we can avoid the restart ?
We can clear the interrupt flags.by place the key CLI
; bootsec.asm
cli
hanging:
jmp hanging
times 510-($-$$) db 0 ; reduce the 2 bytes for boot signature
db 0x55
db 0xAA
Now you cant reboot with Ctrl+Alt+Delete. Now you assembled the code in NASM and use partcopy to copy the files to floppy or hdd .Now we see the full source code
Open a Text editor and save the following code as bootloader.asm
Try Hanging BootLoader
*****************************************************************
[BITS 16]
[ORG 0x7C00]
JMP $ ; Infinte loop
TIMES 510 - ($ - $$) db 0
DW oxAA55
*****************************************************************
1. JMP $ - Means jump to the same location that means goes for infinite loop
2. Times 510 -($ - $$) - A boot loader is always 512 bytes , so we need to resize of memory using Times Directive $ stands for start of instruction and $$ stands for start of program . ($ - $$) Length of our program.
3. DW 0xAA55 indicates boot signature. if this is not present that indicate this in invalid boot loader.
*****************************************************************
Try Compile using NASM
nasm bootloader.asm -f bin -o boot.bin
Try Copy to floppy
partcopy boot.bin 0 200 -fo - Windows user
dd if=boot .bin bs=512 of=/dev/fdo - Linux user , Insert the floppy don't mount it
*****************************************************************
Now insert the floppy in system and see it will hanged.Same for Disc copy the boot file to CD
*****************************************************************
Print a character in Boot Loader:
For printing we will use BIOS video interrupt int 0x10.
To use this interrupt we need to set some values for following register.
AL - ASCII Value of character to display
AH - 0x0E, What character we want to print on screen
BL - Text Attribute (Forground and Background) 0x07
BH - Page number 0x00
Save the following code as bootloader.asm
[BITS 16]
[ORG 0x7C00]
MOV AL, 65
CALL Print
JMP $
Print:
MOV AH,0x0E
MOV BH,0x00
MOV BL,0x07
INT 0x10 ; Call video interrupt
RET ; Return to called procedure
TIMES 510 - ($ - $$) db 0
DW oxAA55
*****************************************************************
Try Compile using NASM
nasm bootloader.asm -f bin -o boot.bin
Try Copy to floppy
partcopy boot.bin 0 200 -fo - Windows user
dd if=boot .bin bs=512 of=/dev/fdo - Linux user , Insert the floppy don't mount it
*****************************************************************
Now insert the floppy in system and see it will print A and hanged.Same for Disc copy the boot file to CD
*****************************************************************
Print a String in Boot Loader:
*****************************************************************
[BITS 16]
[ORG 0x7C00]
MOV SI, Hello
CALL String
JMP $
Print:
MOV AH,0x0E
MOV BH,0x00
MOV BL,0x07
INT 0x10 ; Call video interrupt
RET ; Return to called procedure
String:
Next:
MOV AL,[SI]
INC SI
OR AL,AL ; check AL value is 0
JZ exit_function ; IF End then return
CALL Print ; Else Print Char
JMP Next
exit_function: ; End Lablel
RET ; Return
;DATA
Hello db 'Hello Rajesh', 0 ; Hello Rajesh string ending with 0
TIMES 510 - ($ - $$) db 0
DW oxAA55
*****************************************************************
From this article we can learn how to create a basic boot loader and print our string in the system boot.I hope this will help all of them to understand clearly about boot loader.