Saturday, May 19, 2012

Boot it.


A while back we talked about computer systems having five parts. One of those parts is the software. Software can be divided into two categories. Operating systems control and interface the hardware. Application software allow users to solve problems. The application software can not run without the operating system.

Our focus will be on the operating system and how it starts up, Most traditional systems have what is known as a bios (basic input output system) that does the most basic interaction with the hardware .Newer systems use EFI which is a whole other animal.  it’s primary purpose is to have to software to easily interact with the motherboard and to load in an operating system that has more power to interact with the hardware.  You turn on the machine and the bios cycles thought the available hardware to load in an operating system. So it looks for what is known as a boot loader or a part of the media than can chain the operating system to load in.

Here we will use a virtual machine to simulate the booting of a machine.  Let’s make a virtual floppy.

$ sudo mkdosfs -C newdisk.img 1440
mkdosfs 3.0.12 (29 Oct 2011)

Now that we have a virtual floppy disk we must create a boot loader. We will type some code that will be converted to the binary ones and zeros the computer understands. You will need a program known as nasm to complete this process..  Here is the code:

BITS 16            ;Tells the assembler that its a 16 bit code
mov ax, 07C0h      ;Origin, tell the assembler that where the code will
mov ds, ax         ;be in memory after it is been loaded
mov si, bootstring ;Store string pointer to SI
call print_string
jmp $              ;Infinite loop, hang it here. Not normally what you want to do.

bootstring db "[ ---", 10, 13, "[ Your computer booted!", 10, 13, "[ ---", 13, 0
print_string:    ;Print bootstring on the screen. Assume that ASCII value is in register AL
mov ah, 0Eh      ;Tell BIOS that we need to print one character on screen.
.loop:           ;Print a character at a time till all characters are printed.

lodsb            ;Loads a byte from the source operand into the AL register.
cmp al, 0        ;Reason for appending the 0 at the end of the string to know to quit.
je .finish       ;End of loop
int 10h          ;Print that character

jmp .loop        ;Do it again
.finish:
ret              ;End of call

times 510-($-$$) db 0   ;Fill the rest of sector with 0
dw 0AA55h        ;To be a valid boot sector, the two-byte hexadecimal sequence 0x55, 0xAA                             
                 ;(called the boot sector signature) must exist at the end of the sector.

You will need a text editor to type in (or cut and paste) the code so we can convert it. Using nasm. lets convert the source code to a BINary file to be executed from the floppy.

$ nasm -f bin -o helloboot.bin helloboot.asm

Now that we have the binary file helloboot.bin, it needs to be installed on the virtual floppy at the beginning of the media where the bios will see it.

$  sudo dd if=helloboot.bin of=newdisk.img conv=notrunc
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.000335593 s, 1.5 MB/s

Now we need to try it out. So fire up up your virtual machine (aka vm). Qemu is a popular easy to get virtual machine software you can use for this project. Set your vm to boot from the virtual floppy disk and choose newdisk.img as the floppy file to boot from.



Now you need to launch the virtual machine And TADA, you have loaded your boot sector.. What really happens is a kernel is loaded in and then the operating system takes over.  More about that later.
















Update: Some x86_64 users may need to use the command line to get the virtual floppy.

 

More information at:
http://mikeos.berlios.de/
http://www.tinkernut.com/2010/08/02/how-to-make-an-operating-system/


No comments:

Post a Comment