Class Notes, Codes, CSCI402-OS

[Grad OS]Booting

The Dutchman must have a captain
Bootstrap Bill

Before the logo appears( before windows loading or Apple logo spinning or even hardware one ), the hardware went through a process called booting. This process occurs right after Power button is pressed to activate a program, BootLoader, hardwired inside the motherboard or ROM , specifically first 512Byte of the MasterBootRecord. Basically, it is a chainload machine, keeps loading one program after another until we got to Windows or Linux, which is itself a program that is copied to RAM once it got kickstarted.


Weenix seems to be going through two stage


-It first check if A20Line enabled . Line A20 is a relic of the past history. Computer relies on register EIP(Instruction Pointer)’s value to tell it where to go execute next, EIP contains a address. Old CPU have only 20 lines of address, which is to say it could only access 2^20 address(1MB). New CPU (80286) has access to more lines but to ensure backward compatibility, these new CPU will emulate old CPU by turning off extended address access (EAA – beyond 20lines protected mode ) by default. One can turn it on by writing to P/S 2 port which is a standard. This A20 Handler check is therefore carried on from 286 to nowadays for compatibility reason.

When booting Weenix, depending on situation, we could see the log line: “A20 enabled” or “A20 disabled”


Loading disk 2 starts by reading program from floppy disk A offloading all work to INT13(disk I/O).

Command issued by the following format :

int13 ah, al, ch,cl, dh, dl

( a*,c*,d* : registers, ah:function code, al:#sector, ch:start_track, cl:sector#, dh:start_head, dl:drive number[0:floopy A] es:bx – segment:offset or the buffer address to read, to go to adress use this formula : segment*16 + offset .Note setting register value is required before calling int13 by moving values into registers ).

Specifically, it first resets disk A( seeking to beginning ) using function 0, then starts reading disk A beginning at (track 0, sector 2, head 0 ) with function 02. If no carry flags are set, it jumps to  stage2_start. ( Also, the end of the sector is marked 0xaa55 for bootsector magic ???? )[ Still wondering about reading from floppy, relic of the past?]

The kernel then setup a( user?) stack : 0x9000 – 0xFFFF while interrupt is disabled, setup(query?) memory map, interrupt enabled again prior. Memory map is a giant array that can map codes in and out whenever a piece of code needs running( I’m really confused about this concept but we will figure it out as were pacing through the code base I guess ). Memory map is set up using INT15 function 0xe820, each entry 24 bytes( or 20bytes old style ), first entry is the count of entries.

After that, the kernel set up its stack by copying codes starting at sectors 0x03 size 0x0a00( huh ?). Then it set up Global descriptor Table and Pagetable( 2nd point of confusion but we figure out later ). GDT is invoked by lgdt instruction that loads kernel data ( 0x9A, 0xCF??? ) and kernel segment ( 0x92, 0xCF??? same as data, off by 2 ??? what is that ?) , into the table(?). Page table is related to memory map above. Will update later….

All of the above is in Real mode( bare hardware ????). Weenix enters protected mode afterward by setting the first bit of CR register to 1. Then it enters stage3( voila a final stage. ) that bootstrap an entry function to c main() function code( This is what I guess, because I see it calculates kernel stack size and then jump to kernel start text)…And we are booted!!!! EDIT: It calls function from entry.c to call kmain and then disable interrupt to pause the screen with halt function…..

A pattern shown throughout is whenever one does some important work, one needs to turn off interrupt and turn it on upon finish doing the work.

3 important registers: cs( CODE_SEGMENT : pointer to the code location ), ss( STACK_SEGMENT : pointer to the hardware stack ), ds( DATA_SEGMENT :????)

Here are some Assembly discoveries…..

To zero out register : xor ah, ah

mov comes in alot of variants: mov(b | s | w| l | q | ) depending on the size of immediate/register. For e.g: movb 0x10, al ( move byte )but movl 0x10, eax( move long )

eax : return value from function

esp, ebp : base and stack pointer. normally used to setup funcion context in intel architecture.

int10 is used for printf. Man, it seems like software is fake, just relying on a big ground hardware.

Just like the Dutchman must have a captain, the computer must have a bootloader.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s