Second Stage Loader Advice

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Omega
Member
Member
Posts: 250
Joined: Sun May 25, 2008 2:04 am
Location: United States
Contact:

Second Stage Loader Advice

Post by Omega »

Hi. I am a bit confused as how this works actually. My current design is a Single Stage Loader that just loads sectors to an address and jmps to it which would be my kernel code. I recently found out the hard way why Single Stage loaders suck, so I need some help understanding more about it.

I currently start Pmode then jump to the kernel, what if I wait to do Pmode in my Kernel; would I be outside that 512 limit?

I would like to know before I change anything. thanks
svdmeer
Member
Member
Posts: 87
Joined: Tue May 06, 2008 9:32 am
Location: The Netherlands

Post by svdmeer »

There are no fixed definitions of a 'stage 2' loader, so you can let it do what you like.

I think a stage 2 loader is nothing else than a small binary that can be loaded from the bootsector. That binary is large enough to held some code to load one of more large files, using standard BIOS-functions.

At the PC, you start in realmode, not allowing to load more than 640 kb (-size of bootloader) of data. You can switch to realmode, but in that case you can not use the BIOS. There are different solutions. My advice is to temperary switch to protected mode when memory is full, move your loaded data to high memory, and switch back to realmode to load another part.
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Post by bewing »

Yes, there are many things that you will need to do while you are still in Real mode. It is completely impossible to do them in a measley 510 bytes. So yes, your bootsector must either 1) load more bootloader code, or 2) it must load your kernel, and your kernel must start in Real mode, and must call all the BIOS functions itself.

It is certainly possible for your kernel to have an initialization section that runs in Real mode.

The only reason to have a second stage bootloader is to separate the Real mode BIOS function calls from the rest of your kernel -- so the kernel can be completely Pmode (which makes it easier to compile, and makes the kernel less hardware dependent).

Technically, it is not precisely necessary to create a second stage bootloader. All filesystems that I know of can easily support "bootsectors" that are longer than one sector. The BIOS/MBR will probably only load the first one, but the code in the first ("real") bootsector can load sectors 2 through X of the partition, at some address, and jump to it. You don't even have to know how many sectors. You can just load "a bunch of them", and then make sure that you stay within that limit. (But if you try to load more than 17, then you have to be careful about crossing "cylinder boundaries".)

Alternately, the second stage bootloader can be a file in your filesystem. All the first stage bootloader needs to do is parse the filesystem, load all the sectors of the file into memory at some address, and jump to it.
User avatar
Omega
Member
Member
Posts: 250
Joined: Sun May 25, 2008 2:04 am
Location: United States
Contact:

Post by Omega »

Technically, it is not precisely necessary to create a second stage bootloader. All filesystems that I know of can easily support "bootsectors" that are longer than one sector. The BIOS/MBR will probably only load the first one, but the code in the first ("real") bootsector can load sectors 2 through X of the partition, at some address, and jump to it. You don't even have to know how many sectors. You can just load "a bunch of them", and then make sure that you stay within that limit. (But if you try to load more than 17, then you have to be careful about crossing "cylinder boundaries".)
I'll second that. I ran into that exact problem. So, it isn't that Pmode makes you great, it is you that makes being in Pmode great.
Post Reply