Page 1 of 1

How do you pass control from bootloader to kernel?

Posted: Sun Aug 31, 2008 6:40 pm
by daBKLYNdoorman
So I have myself a bootloader written in assembly code, how would I go about loading the kernel? My bootloader is composed of two stages if that helps.

And if its just adding a couple of extra lines to the second stage of the bootloader or something similar, can someone please post those instructions in assembly?

Thanks,
Robert

Re: How do you pass control from bootloader to kernel?

Posted: Sun Aug 31, 2008 6:51 pm
by 01000101
first off, you need to write the bootloader code that can load sectors from the medium in which your kernel resides (floppy, hdd, cd, flash, etc...). This can be done by just reading raw sectors to load a flat binary into memory and then jumping to the load address, or parsing filesystem structures to load the kernel file. Either way, the basic steps are to load the kernel into memory, and then jump to your kernel from your bootloader, that will transfer you out of your bootloader.

Re: How do you pass control from bootloader to kernel?

Posted: Mon Sep 01, 2008 12:44 am
by egos
daBKLYNdoorman wrote:How do you pass control from bootloader to kernel?

Code: Select all

jmp 0:0x8000
or something like this.

Re: How do you pass control from bootloader to kernel?

Posted: Mon Sep 01, 2008 2:02 am
by AJ
Remember that once you have loaded your second stage binary in to memory, jumpting to a location in that is no different to jumping to a location in your first stage boot loader (the only difference is that you need to specify the address of the jump rather than using a label to get the assembler to do it for you).

So, if your first stage boot loader is at 0x7C00 and you load the second stage at 0x7E00, you just do a far jump as egos suggested (jumping to 0x07E0:0x0000 rather than 0x0000:0x7E00 will give you more space before you wrap around the segment, but I guess your boot loader is unlikely to need 64k?). Using the code segment register to initialise the jump also means that the second stage boot loader can be linked at 0x0000 (ORG 0x0000) and can then be run from the start of any segment, if you want scope for choosing a different load address in future.

Of course, if you are linking the second stage you need to ensure that you are either using a flat binary with the entry point at offset 0x0000, or a formatted executable (such as ELF) where you read the entry point from the headers.

Cheers,
Adam

Re: How do you pass control from bootloader to kernel?

Posted: Mon Sep 01, 2008 3:55 am
by egos
AJ wrote:jumping to 0x07E0:0x0000 rather than 0x0000:0x7E00 will give you more space before you wrap around the segment, but I guess your boot loader is unlikely to need 64k?
Addressing within the segment with zero base address coincides with linear addressing. 32 kb is more than sufficient for me to make RM initialization. However, the specific value of cs:ip is not required by specification. The position-independent code in the first paragraph of kernel binary file must contain jump instruction to the actual entry point.