Page 1 of 2

How to load the kernel after entering pmode

Posted: Mon Nov 17, 2008 10:51 am
by thestew42
So I wrote a boot sector, then I had it jump to a second stage boot loader which enables protected mode, a20 etc... But now I am faced with the problem of how to load the kernel. I cant use the BIOS interrupt so I would have to write a floppy driver within my second stage boot loader. This seems like a bad idea because (1)The OS may not always boot from a floppy and (2)I would like to not have to write a floppy driver twice.

So first I tried to link the boot loader and the kernel, but there was some problem linking the 16 bit loader code. Then I tried to load the boot sector into memory before jumping to protected mode. I was planning on jumping to the kernel in memory after entering protected mode :^o. The problem was that I ended up overwriting the boot loader with the kernel code, causing a crash. Then when I tried to put the kernel at a higher address I get this error from bochs:

00351951398p[FDD ] >>PANIC<< io: norm r/w parms out of range: sec#0bh cyl#fbh eot#00h head#01h

Although this error seems to suggest an error with FAT traversal, it does not occur when I have a lower address that overwrites the loader code. Now thinking about it, I guess that makes sense, because the code would be overwritten before the FAT error occurs.

How do most people load the kernel in a situation like this? It seems like I'm going about this the wrong way.

Re: How to load the kernel after entering pmode

Posted: Mon Nov 17, 2008 10:58 am
by Combuster
The second stage bootloader is still 16 bit, and can thus still access the bios interrupts, load the kernel and only then jump to protected mode.

Re: How to load the kernel after entering pmode

Posted: Mon Nov 17, 2008 10:59 am
by 01000101
My setup:

Start bootloader,
Enter UnReal mode (for loading kernel into higher memory),
Depending on the boot device, load kernel from floppy (using INT's) or load from HDD (using IO).
After loading kernel, load second stage bootloader into low mem for later use.
Jump to second stage bootloader to setup PMode and then LMode, and jump to kernel.

Re: How to load the kernel after entering pmode

Posted: Mon Nov 17, 2008 11:12 am
by thestew42
Combuster wrote:The second stage bootloader is still 16 bit, and can thus still access the bios interrupts, load the kernel and only then jump to protected mode.
Oh. I completely forgot that it was still in real mode until you jump. That makes things easier. Thank you.
01000101 wrote:My setup:

Start bootloader,
Enter UnReal mode (for loading kernel into higher memory),
Depending on the boot device, load kernel from floppy (using INT's) or load from HDD (using IO).
After loading kernel, load second stage bootloader into low mem for later use.
Jump to second stage bootloader to setup PMode and then LMode, and jump to kernel.
Does your program detect whether its on a floppy or the HDD or do you have two versions?

How high in memory do people usually put the kernel?

Re: How to load the kernel after entering pmode

Posted: Mon Nov 17, 2008 11:24 am
by Combuster
The de-facto standard seems to be at 1M (for protected- and longmode OSes)

Re: How to load the kernel after entering pmode

Posted: Mon Nov 17, 2008 11:38 am
by 01000101
My first-stage bootloader detects the boot medium (dl on boot) and the floppy and hdd read code is small enough to fit in the first stage. I used to use two separate files for each type of boot, but I found it unnecessary.

Oh btw, I just reviewed my newer version of DiNS and found out that I no longer use a second stage. The first-stage sets up pmode and lmode itself and jumps to the kernel, the 'second-stage' i was thinking of is just the AP trampoline code.

I load my kernel at 0x1000400 [16MB] (I copy the BSP and AP boot-sectors as well). But as already said, the de-facto is at 0x100000 [1MB].

Re: How to load the kernel after entering pmode

Posted: Mon Nov 17, 2008 2:22 pm
by AJ
01000101 wrote:I load my kernel at 0x1000400 [16MB]
#-o

Re: How to load the kernel after entering pmode

Posted: Mon Nov 17, 2008 2:31 pm
by 01000101
oops typo: supposed to be 0x2000400 [32MB + 0x400 (for bsp+ap bootsect)]

[edit]Also, why is that worthy of a #-o [/edit]

Re: How to load the kernel after entering pmode

Posted: Mon Nov 17, 2008 2:50 pm
by AJ
Ignore me. It's been a long day today =P~

I didn't see your proviso about the 2 boot sectors and thought you were saying 0x1000400 was 16MB - apologies.

Cheers,
Adam

Re: How to load the kernel after entering pmode

Posted: Mon Nov 17, 2008 2:55 pm
by 01000101
I put 0x1000000 as that is where I mirror the kernel before I copy it to the hdd.

And no worries, I was just concerned that maybe there was something I didn't know about occupying that space lol. :)

Re: How to load the kernel after entering pmode

Posted: Mon Nov 17, 2008 5:48 pm
by neon
My bootloader enters protected mode asap and uses its services routine that switches between real and protected modes to call bios interrupts during its execution. Its a little slower with this method do to the processor mode switches but works very well as everything is done through the BIOS but from protected mode. Its basically a <dos.h> int86() like routine that I can use within protected mode.

Re: How to load the kernel after entering pmode

Posted: Mon Nov 17, 2008 10:42 pm
by Brendan
Hi,
01000101 wrote:My first-stage bootloader detects the boot medium (dl on boot) and the floppy and hdd read code is small enough to fit in the first stage.
Umm...

So, if the BIOS says the boot device was the "first floppy" (dl = 0x00), then you use enhanced disk services to find out if it really is the first floppy, or if it's the second floppy (with the "swap a: and b:" BIOS option), or if it's a flash drive or CD-ROM emulating a floppy; and if the BIOS says the boot device was the "first hard drive" (dl = 0x80) then you find out if it's the first IDE/ATA/SATA hard drive or if it's a SCSI drive, or if it's a flash drive or CD-ROM emulating a hard drive. Then, you've got device drivers for floppies, IDE/ATA/SATA (and ATAPI) and every kind of SCSI controller, plus code for each type of USB controller and code to handle several USB devices (USB flash, USB floppy, USB hard disk and USB CD-ROM). And, all of this fits in your first stage boot loader? I'm impressed.... ;)


Cheers,

Brendan

Re: How to load the kernel after entering pmode

Posted: Tue Nov 18, 2008 12:11 am
by 01000101
Remember, I'm programming for an incredibly narrow hardware range. Also, that small hardware range is specified when assembled and thus the configuration does not vary.

I don't need to check for half of those things listed as they will be disabled in the BIOS and will not have any sort of implementation in any non-debug mode. Basically the goal is to only program what needs to be programmed for, and leave the rest without any handling at all (unless that is more of a security issue itself). Also, the future method will involve BIOS locking so that the startup cannot vary, thus ruling out things such as floppy drives (of any fashion).

Re: How to load the kernel after entering pmode

Posted: Thu Nov 20, 2008 1:51 pm
by sebihepp
I had this problem, too. But I am solving it this way:
My 2nd stage boot loader contains 16Bit Code and 32 BitCode.
After jump to PMode everytime I want to read from floppy I jump to
UnrealMode, use the BIOS, jump back to PMode and copy the readed
sector to the destination.

Cheers Sebihepp

Re: How to load the kernel after entering pmode

Posted: Sun Dec 14, 2008 5:49 am
by djsilence
HI!

So, If I know right, int13 can load not more than 64Kb file? If it is so, then how can I load more than 64Kb and not to 1Mb? For example i'd like to load at 0xC0000000? (3GB)

Thanks!