Operating System is just a software which has to be loaded on the hardware. This Software will be loaded with the help of BIOS (Basic Input/Output Software). When hardware starts BIOS kicks in initialises and tests critical hardware components like keyboard, memory etc. and look for the boot sector (typically it is located in Cylinder 0, Head 0, Sector 0 - It is the way to address the memory). As there could be multiple sectors, there should be a way to identify a sector which is a boot sector. BIOS will identify any sector as a boot sector when it finds the magic number (0xaa55) at the end of sector.
Note: Size of a sector is 512 bytes.
After finding boot sector, BIOS will load boot sector which intern loads the operating system. This boot sector should have never ending loop otherwise soon after loading the sector hardware will shut down. Operating System is a never ending software unless triggered with Shutdown and/or Restart option.
There are 2 ways to write boot block.
+ Hex bytes using Hex/binary file editor. - writing actual machine code instructions (this is CPU specific)
+ Assembly code and building it into binary.
To write 512 bytes using any editor is not an easy task. We should know how to keep never ending loop for any given hardware architecture and we should have exact 512 bytes in length. So here I have opted for writing the Assembly code. Below boot_block.asm is the file which contains the assembly code.
boot_block.asm:
;never ending loopNote: except never ending loop and MAGIC_NUMBER (0xaa55), everything else is padded with 0's
loop:
jmp loop
; padding with 0's
times 510-($-$$) db 0
; MAGIC_NUMBER , to indicate this sector is boot sector
dw 0xaa55
This can be compiled to binary with the help of nasm assembler tool using
nasm boot_block.asm -f bin -o <output_file_name>.bin
When we print the <output_file_name>.bin using
od -t x1 -A n <output_file_name>.bin or using hexdump using hexdump -C <output_file_name>.bin it will look like,
eb fe 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa
we can notice the MAGIC_NUMBER is stored as 55 aa. This is because Intel x86 is BigEndian architecture. Most Significant Byte (MSB) will be stored last in the memory (0xaa55, 0xaa will be stored last)
0xeb 0xfe ==> are the machine code instructions to perform the infinite/never ending loop. This is defined by CPU manufacturer.
Written boot sector can be tested in multiple ways
+ using Virtual box - Writing/Flashing <output_file_name>.bin to CD-Drive/USB/Floppy Disk/Hard disk image
+ using qemu - Qick Emulator
For now I have used qemu with the help of following command:
qemu-system-x86_64 <output_file_name>.bin
Our boot sector is loaded by BIOS of specified architecture in the command (x86_64) and it will look like below:
![]() |
Loading boot block using qemu |
It says, "Booting from Hard Disk..." but, for now there is nothing to boot from hard disk.
As we progress we can add stuffs here to load our operating system!
Next - Printing something by writing some stuffs in Boot sector.
No comments:
Post a Comment