Professional Documents
Culture Documents
INTRODUCTION
When the computer starts, the processor starts executing instructions at the memory address 0xffff. This is usually a location in the BIOS ROM. Thus the BIOS code is executed by the processor. The first thing that occurs is that it sends a signal to motherboard which in turn starts the power supply. After supplying the correct amount of power to each device, it sends a signal called "Power OK" to BIOS which resides on motherboard.Once the BIOS receive the "Power OK" signal, it starts the booting process by first initializing a process called POST (Power On Self Test). POST first checks that every device has right amount of power and then it checks whether the memory is not corrupted. Then it initializes each device and finally it gives control to BIOS for further booting.Now the final process of booting begins. Boot loader or Bootsector is a program situated at the first sector of the hard drive; and it is the sector where the boot starts from. For this the BIOS first finds 512 bytes of image called MBR (Master Boot Record) or Bootsector from the floppy disk or hard disk which is used for booting. The priority of boot devices is set by the user in BIOS setting. The normal priority is floppy disk first, then hard disk. Once BIOS finds the bootsector it loads the image in memory and executes it. If a valid bootsector is not found, BIOS checks for next drive in boot sequence until it finds a valid bootsector. If BIOS fails to get valid bootsector, generally it stops the execution and gives an error message "Disk boot failure".It is bootsectors responsibility to load the operating system in memory and execute it.
offset 0x0000 - 0x01bd, followed by table entries for the four primary partitions, using sixteen bytes per entry (0x01be - 0x01fd), and the two-byte signature (0x01fe - 0x01ff). The layout of the table entries is as follows:
Offset Size (bytes) Description
0x00
0x01
0x02
0x04
0x05
0x06
0x08
0x0C
TESTING BOOTLOADER
Boot loader can be tested on the real hardware or using specially designed for such purposes virtual machine VirtualBox. VirtualBox is an open-source hypervisor platform which creates a virtualized computing environment for virtual machines. This allows a host computer to run multiple guest operating systems simultaneously. It creates virtual hardware devices that guest operating systems can install on and us. To test boot loader we created a new virtual machine with minimal disk size for example 1 Gb.
NASM
The assembler used here is NASM (Netwide Assembler). NASM is an 80x86 assembler designed for portability and modularity. It supports a range of object file formats, including Linux and NetBSD/FreeBSD a.out, ELF, COFF, Microsoft 16bit OBJ and Win32. It outputs plain binary files.
nasm -f bin -o myfirst.bin myfirst.asm
Here we assemble the code from our text file into a raw binary file of machinecode instructions. With the -f bin flag, we tell NASM that we want a plain binary file (not a complicated Linux executable - we want it as plain as possible!). The -o myfirst.bin part tells NASM to generate the resulting binary in a file called myfirst.bin.
Floppy Disk
VirtualBox can create a virtual floppy disk drive that uses a floppy disk image file on the computer. The virtual machine can access the floppy disk image like a real floppy disk drive, including booting from the floppy disk image inside the virtual machine.Floppy disk image is created from Virtual Machines Storage category in the settings window,then adding Floppy Device in the Floppy Controller.Then clicking on Choose Disk creates floppy disk image file in the disk_images/ directory.
Compiling / Assembling
Now when the code is developed we need to transform it to the file for the 16-bit OS. Such files are .com files. We can start each of compilers (for Assembler and C, C++) from the command line, transmit necessary parameters to them and obtain several object files as the result. Next we start linker to transform all .obj files to the one executable file with .com extension.We compile our bootloader using NASM. For that the command is:
nasm ourbootloader.asm -f bin -o boot.bin
This uses the 'dd' utility to directly copy our kernel to the first sector of the floppy disk image.
COMMANDS IMPLEMENTED
Restart
When the computer starts, the processor starts executing instructions at the memory address FFFF:0000. This is usually a location in the BIOS ROM. Thus the BIOS code is executed by the processor which finds the boot device.Thus Jump to location memory address FFFF:0000 is made.
[ORG 0x7C00]: Then we have used [ORG 0x7C00] which tell assembler to assemble the instructions from Origin 0x7C00. BIOS loads bootloader at physical address 0x7C00 hence we have assemble our bootloader starting from that location. JMP $: JMP at location $ means jumping to the same location. Thus this nothing but an infinite loop. We just want to hang our code here. TIMES 510 - ($ - $$) db 0: As bootloader is always of length 512 bytes, our code does not fit in this size as its small. We need to use rest of memory and hence we clear it out using TIMES directive. $ stands for start of instruction and $$ stands for start of program. Thus ($ - $$) means length of our code. DW 0xAA55: This is boot signature. This tells the BIOS that this is a valid bootloader. If bios does not get 0x55 and 0xAA at the end of the bootloader than it will treat bootloader as invalid. Thus we provide this two bytes at the end of our bootloader. For printing we will use BIOS video interrupt int 0x10.
INT 0x10 is a BIOS video interrupt. All the video related calls are made through this interrupt. To use this interrupt we need to set the values of some register. AL = ASCII value of character to display AH = 0x0E ;Teletype mode (This will tell bios that we want to print one character on screen) BL = Text Attribute (This will be the fore ground and background color of character to be displayed. 0x07 in our case.) BH = Page Number (0x00 for most of the cases) Once all the registers all filled with appropriate value, we can call interrupt.