Writing a bootsector
Click to view the Updated Version
Have you ever imagined how an OS is loaded or how does your Linux or Windows load? This all starts with a bootsector. Now this tiny 512bytes file (sector) on your hard disk is responsible for everything what happens. From the power button to ROM, ROM to BIOS, BIOS to boot sector. (Actually that’s too big but not needed).
What is a boot sector?
Ok well, A boot sector is the first sector of a disk that determines the disk is bootable or not. It has to be 512 bytes, no more or less. This sector of the disk is loaded by the BIOS through int 0x19 function of the BIOS at 0x7C00 (This is a location in the memory).A boot sector’s function is to load an operating system or a second sector (containing a bigger code, example: bootloader). These are stored with the Master Boot Record (MBR) of the disk. Its last 2 bytes are known as boot signature and must be 0xAA55 to be a valid bootsector.It got its name as “boot sector” as it is the sector that determines the disk is bootable or not. Everytime the BIOS loads a boot sector, you will be in 16bit Real Mode. This is the old 8086 Mode(20 years old Intel processor mode).Even though you have a 32bit or a 64bit machine, your boot sector would be loaded in Real Mode with just 1MB of memory.(This also is the same, even you have 1GB or 4GB, you get just 1MB).Serious, not a joke. To get into your actual mode your processor is capable of ,we have some steps for that.32bit mode is called protected mode and 64bit as Long Mode(64bit Mode is available on 64bit processors).You might be wondering where shall I get my whole RAM. For that you need to enable the 20bit of the address bus. This (Enabling 20th bit) can be done through BIOS (You get the BIOS only in Real Mode), Keyboard Controller, Keyboard Output Port or the System Port.
This is optional, skip this if you are not interested to know why are these limitations are for. (1MB,16bit)
You may think Intel is stupid do all this, but there is a reason for it.Going back to 8086 processors, they were 16bit processors with a 20bit Address Bus.Address Bus are a set of lines (each line representing a single bit) on the motherboard that helps in passing address between IO-Devices,Memory Controller,etc.In a memory(RAM) each byte has its own address.As it had 20bit Address Bus, the memory the processor would support is 2 to power 20 bytes(2 because computer uses binary which is base 2), there fore converting it to MB's it is 1MB. OK, now is the time to know why it these are still available in the latest processors.These all were done are done to maintain compatibly with 8086 Programs(DOS). Intel placed a Electronic OR gate at the 21th bit(It functions exactly as a C OR "|").Now that is what is we need to enable to get all the memory available.So we press the power button we are always in Real Mode until we write to the CR0 Register.Now don't ask who is still using 8086 Programs.
Protected Mode vs Real Mode:
Protected Mode has various advantages over Real Mode.
NOTE:Widows and Linux or Mac OS X all run in Protected Mode(64bit versions run in Long Mode).
Points to be noted:
Tools required:
All these tools are free and are available on Windows platform, Linux users you might need alternate software’s instead of these such as Virtual Box, etc.NASM is also available for Linux.
NOTE:
What is a boot sector?
Ok well, A boot sector is the first sector of a disk that determines the disk is bootable or not. It has to be 512 bytes, no more or less. This sector of the disk is loaded by the BIOS through int 0x19 function of the BIOS at 0x7C00 (This is a location in the memory).A boot sector’s function is to load an operating system or a second sector (containing a bigger code, example: bootloader). These are stored with the Master Boot Record (MBR) of the disk. Its last 2 bytes are known as boot signature and must be 0xAA55 to be a valid bootsector.It got its name as “boot sector” as it is the sector that determines the disk is bootable or not. Everytime the BIOS loads a boot sector, you will be in 16bit Real Mode. This is the old 8086 Mode(20 years old Intel processor mode).Even though you have a 32bit or a 64bit machine, your boot sector would be loaded in Real Mode with just 1MB of memory.(This also is the same, even you have 1GB or 4GB, you get just 1MB).Serious, not a joke. To get into your actual mode your processor is capable of ,we have some steps for that.32bit mode is called protected mode and 64bit as Long Mode(64bit Mode is available on 64bit processors).You might be wondering where shall I get my whole RAM. For that you need to enable the 20bit of the address bus. This (Enabling 20th bit) can be done through BIOS (You get the BIOS only in Real Mode), Keyboard Controller, Keyboard Output Port or the System Port.
This is optional, skip this if you are not interested to know why are these limitations are for. (1MB,16bit)
You may think Intel is stupid do all this, but there is a reason for it.Going back to 8086 processors, they were 16bit processors with a 20bit Address Bus.Address Bus are a set of lines (each line representing a single bit) on the motherboard that helps in passing address between IO-Devices,Memory Controller,etc.In a memory(RAM) each byte has its own address.As it had 20bit Address Bus, the memory the processor would support is 2 to power 20 bytes(2 because computer uses binary which is base 2), there fore converting it to MB's it is 1MB. OK, now is the time to know why it these are still available in the latest processors.These all were done are done to maintain compatibly with 8086 Programs(DOS). Intel placed a Electronic OR gate at the 21th bit(It functions exactly as a C OR "|").Now that is what is we need to enable to get all the memory available.So we press the power button we are always in Real Mode until we write to the CR0 Register.Now don't ask who is still using 8086 Programs.
Protected Mode vs Real Mode:
Protected Mode has various advantages over Real Mode.
- Real Mode has no memory protection which allows other programs write to other programs memory, therefore leading to crash
- Everything is just 16bit in Real Mode
- No Virtual Memory for Real Mode
- 1MB limitation is in Real Mode
NOTE:Widows and Linux or Mac OS X all run in Protected Mode(64bit versions run in Long Mode).
Points to be noted:
- A bootsector must be 512 Bytes.
- It is the first sector of the disk
- It must have its last 2 bytes as (boot signature) 0xAA55
- We are loaded at 16bit Real Mode with just 1MB of memory.
- We get just 1MB of memory until we enable A20 Gate.
Tools required:
- Bochs -Virtual Machine (Emulator) – This is used to test your boot sector.You can also use any other virtual machine you are comfortable with such as Virtual Box, Microsoft Virtual PC, QEMU, etc
- NASM – Assembler -Very important, we shall use this for assembling our code
- Partcopy, RawWritten, etc – Any of one would work- These are used to write you boot sector to an image or a disk
- Optional: VFD – Virtual Floppy Disk -This tool is optional; it helps in mounting floppy images.
All these tools are free and are available on Windows platform, Linux users you might need alternate software’s instead of these such as Virtual Box, etc.NASM is also available for Linux.
NOTE:
- You need to have a minimum knowledge of Assembly and some hardware.
- You have to use NASM Assembler as assembly codes are sometimes assembler dependent
w was that simple code? OK, must be happy, that’s all to code for a simple boot sector.So we have a simple boot sector which set up you with A20 enabled, Protected Mode.
Okay, time to assemble it.
Save the assembly script in a file with extension “asm”.
Assembling the bootsector
Using the following command to assemble your boot sector:
nasm -f bin bootsec.asm -o bootsec.bin
So you if you get the output file as “bootsec.bin”, that means you got your boot sector ready.
You have succeeded in developing a bootsector. One thing to know, many fail even to display their name on the screen.If it works you must be happy. Now let’s write the sector to an image and test it with Bochs Emulator.
Creating Virtual Floppy Disk
Now to mount a floppy image, I use VFD Tool (It is free). Download it and follow these steps.
Download and start vfdwin.exe as administrator (Win XP you can directly run it)
Go to the driver tab and click on install if you are running for the first time.
Then click on start to load the driver. Done, now we need to create an image.
Go to Drive0 Tab and click on Open/Create. Select RAM and set the media to 3.5, 1.44MB and then press create.
Assign a drive letter to it, by clicking change.
Format it by clicking format button.
Done with VFD Tool, now to write the sector.
Installing the bootsector
You need to run the following in Command Prompt
partcopy bootsec.bin 0 200 –f0
Done, that was easy.
Testing the bootsector
Exciting part, testing it.
Use the following Bochs Script to test using Bochs Emulator.
Okay, time to assemble it.
Save the assembly script in a file with extension “asm”.
Assembling the bootsector
Using the following command to assemble your boot sector:
nasm -f bin bootsec.asm -o bootsec.bin
So you if you get the output file as “bootsec.bin”, that means you got your boot sector ready.
You have succeeded in developing a bootsector. One thing to know, many fail even to display their name on the screen.If it works you must be happy. Now let’s write the sector to an image and test it with Bochs Emulator.
Creating Virtual Floppy Disk
Now to mount a floppy image, I use VFD Tool (It is free). Download it and follow these steps.
Download and start vfdwin.exe as administrator (Win XP you can directly run it)
Go to the driver tab and click on install if you are running for the first time.
Then click on start to load the driver. Done, now we need to create an image.
Go to Drive0 Tab and click on Open/Create. Select RAM and set the media to 3.5, 1.44MB and then press create.
Assign a drive letter to it, by clicking change.
Format it by clicking format button.
Done with VFD Tool, now to write the sector.
Installing the bootsector
You need to run the following in Command Prompt
partcopy bootsec.bin 0 200 –f0
Done, that was easy.
Testing the bootsector
Exciting part, testing it.
Use the following Bochs Script to test using Bochs Emulator.
Ok, almost over, save the Bochs Script as bochscr.bxrc.
If you have installed Bochs,the file would have a symbol of Bochs.
What you need to do is now just double click on the bochsrc file, and there you go.Your bootsector is booting, your text must come if you edited printmsg of the script.
Oh, quick go and replace the printmsg with your name ,got to show others.
So now if you want, you can save the mounted floppy into an image and test it on real hardware.Actually you made your own Operating System.
What is left for you?
Now, what is left for you is to add support of reading sectors and jumping to the 2nd stage of the bootloader, there you'll need to write code for various filesystems and Storage Devices(ATA,IDE,USB,etc, so that you bootloader can boot from HardDisks,CD Roms,PenDrive,etc) then a keyboard to select a option in a menu and then just boot accordingly.You can also try for a header the way the GRUB provides.
Remove the A20 and the PMode Code from the bootsector, you can do it in the 2nd stage as we are limited to 512bytes. Use the BIOS functions to load the second stage. The 2nd Stage can be of any size, therefore you can implement many things with no limit.
If you have installed Bochs,the file would have a symbol of Bochs.
What you need to do is now just double click on the bochsrc file, and there you go.Your bootsector is booting, your text must come if you edited printmsg of the script.
Oh, quick go and replace the printmsg with your name ,got to show others.
So now if you want, you can save the mounted floppy into an image and test it on real hardware.Actually you made your own Operating System.
What is left for you?
Now, what is left for you is to add support of reading sectors and jumping to the 2nd stage of the bootloader, there you'll need to write code for various filesystems and Storage Devices(ATA,IDE,USB,etc, so that you bootloader can boot from HardDisks,CD Roms,PenDrive,etc) then a keyboard to select a option in a menu and then just boot accordingly.You can also try for a header the way the GRUB provides.
Remove the A20 and the PMode Code from the bootsector, you can do it in the 2nd stage as we are limited to 512bytes. Use the BIOS functions to load the second stage. The 2nd Stage can be of any size, therefore you can implement many things with no limit.