Page 1 of 2

How to load karnel from a bootloader?

Posted: Mon Sep 07, 2009 3:56 am
by electritwist
Hello there,

I have a question, I have a boot loader but how to load kernel from a boot loader if it's in a separate file? I mean, the boot loader has a separate file and so is kernel. Thanks for the reply.

Re: How to load karnel from a bootloader?

Posted: Mon Sep 07, 2009 4:26 am
by AJ
Hi,

It depends on a few things. Basically you need two steps - get your kernel in to memory (relocate if necessary) and execute it.

Load in to memory:

Firstly, what is your boot medium and FS? If you are using a standard floppy with FAT12, your loader needs to read the root directory in to memory, find your file and then use the FAT to load all sectors in to memory (using int 0x13). If you are using a CDRom (El-Torito), you need to read the Primary Volume Descriptor and either use the directory structure or path table. Here, you can use int 0x13 extensions.

Execute:

Next, if your kernel is a flat binary, you simply jump to the first instruction. If using a structured executable such as ELF / COFF, you need to parse the program headers and relocate the executable. You then read the entry point value and jump to it.

Whether you need to switch to PMode / LMode before loading the executable (and switch to and from real mode to use BIOS interrupts) or not depends on the kernel mode and how big the kernel is expected to get. If the kernel needs loading above 1MiB, you'll need to mode switch. If you need this extra functionality, you'll have to decide whether to use a large boot loader or a stage 1.5 loader...

Cheers,
Adam

Re: How to load karnel from a bootloader?

Posted: Mon Sep 07, 2009 4:29 am
by torshie
You need read hard disk or floppy, and parse partition table, filesystem ...

I suggest you use grub or other bootloader. Implement a bootloader is very time consuming.

Re: How to load karnel from a bootloader?

Posted: Mon Sep 07, 2009 5:04 am
by AJ
Hi,

Yes - the other way is, of course, to use a third party boot loader. If you do still want to roll your own, I always found the FAT12 boot loader from BOS a very well commented and easy to understand version. Also, have a look at our very own Bootloader wiki page.

Cheers,
Adam

Re: How to load karnel from a bootloader?

Posted: Mon Sep 07, 2009 5:09 am
by Solar
Location:
  • Dumb Bootloader: Hardcoded. That's the way LILO does it. The bootloader "knows" the location of the kernel. Every time you update the kernel binary, you have to run a configuration program that writes the new location of the kernel to the bootloader.
  • Smart Bootloader: FS parsing. The bootloader knows the filename of the kernel (hardcoded or - like GRUB - through a configuration file). It is "smart" enough to parse the file system and find the location of the kernel.
Kernel Format (see AJ's post):
  • Flat Binary. Load, and jump to entry point.
  • Executable Format (e.g. ELF). Load, parse, relocate, jump to entry point.
A dumb, flat-binary bootloader is simple to implement, but unwieldly to use. You have to use special flags when compiling a new kernel (to get a flat binary), and you have to re-run the bootloader configuration after each kernel update.

A smart, executable-format bootloader is much more difficult to implement, but very easy to use.

If you have already written your bootloader, and have had the experience and learned the lessons from it, I'd suggest you archive it somewhere, and use a ready-made, proven bootloader like GRUB (which is a smart / executable-format bootloader that even does the switch to protected mode for you).

Re: How to load karnel from a bootloader?

Posted: Mon Sep 07, 2009 5:20 am
by torshie
If you just want to write your own kernel not your own bootloader, I strongly suggest you to use grub. If you have some other requirements that grub cannot satisfy, you can implement a second stage bootloader and use gas directive ".incbin" to enclose you kernel binary directly into you second stage bootloader.
To write a bootloader as good as grub/grub2 is much more difficult that to write a toy kernel.

Re: How to load karnel from a bootloader?

Posted: Mon Sep 07, 2009 5:20 am
by electritwist
AJ wrote:Hi,

Yes - the other way is, of course, to use a third party boot loader. If you do still want to roll your own, I always found the FAT12 boot loader from BOS a very well commented and easy to understand version. Also, have a look at our very own Bootloader wiki page.

Cheers,
Adam
No, I'm not interested in something that's already made, but thank you anyway, I want to write my own boot loader, which clearly means that I'm not interested using Minix or Linux.

Adam, I don't get the idea how from a boot loader to switch to PMode (how to switch from RMode to PMode perhaps) and how to know where exactly the kernel is (in my case it's on the hard disk it's copied) because unlike from boot loader which is in the first sector, kernel isn't, so, how to know where it is? And yes, I'm using the hard disk. I want to load perhaps the Hello World kernel, but I want to load two files - boot loader and kernel, how to load that Hello world from the kernel file. I just want to understand how exactly it happens. Ah, as a side note, in all examples of boot loader, this Hello world is written in the same boot loader, but I want to get it out from the kernel.

Re: How to load karnel from a bootloader?

Posted: Mon Sep 07, 2009 5:35 am
by AJ
Hi,

How to load the boot loader:
* Don't bother - the BIOS does this for you.

How to Switch Modes:
* Protected Mode - Small article, but good links. Also see the Real Mode article for some background info.

How to find the kernel location (some of this repeated from Solar):
* Hard code the kernel location - boot loader needs altering every time you re-copy your kernel.
* Search the file system - this will depend hugely on which file system you are using. Generally, the file system will provide some sort of table / list of file names that you have to search. Once you've found the file name, the directory entry will then point you to the file.

The wiki has a lot of info and links on file systems, but if you're using an unusual FS, ask and we'll try to help. If you are not using a FS, you'll need to hard code the kernel location somewhere. I guess that if you are using a hard disk, you are probably using FAT32 or EXT2/3.

Cheers,
Adam

Re: How to load karnel from a bootloader?

Posted: Mon Sep 07, 2009 5:52 am
by Solar
electritwist wrote:I want to write my own boot loader, which clearly means that I'm not interested using Minix or Linux.
You are free to write the bootloader yourself, of course. But I thought I should mention that GRUB is not tied to Minix or Linux in any way.

Re: How to load karnel from a bootloader?

Posted: Mon Sep 07, 2009 5:08 pm
by ClarionCoder51
Alright. You need to make sure that the bootloader is able to load the kernel from sector 2, assuming that the kernel is at sector 2 of course. Now how to do that, you need to have this code in the loader:

Code: Select all

mov ah, 02h ;read function
mov al, 3 ;how many sectors to read
mov ch, 0 ;cylinder of drive
mov cl, 2 ;sector of drive
mov dh, 0 ;drive head
; dl is the drive letter. Don't worry 'bout this.

; es:bx points for receiving data buffer:
mov bx, 0800h
mov es, bx
mov bx, 0

;read it!!
int 13h

;finally, transfer control to kernel
jmp 0800h:0000h
Depending on your compiler, 02h might be 02x0 or somethin' like that, because some compilers need hex values to look a certain way.

Re: How to load karnel from a bootloader?

Posted: Mon Sep 07, 2009 6:00 pm
by pcmattman
You need to make sure that the bootloader is able to load the kernel from sector 2
Alternatively, read the filesystem, as has been suggested by AJ and company. There's nothing that says you "need" to make sure that the bootloader can load from sector 2, that's a design decision ;).

Re: How to load karnel from a bootloader?

Posted: Mon Sep 07, 2009 6:11 pm
by ClarionCoder51
pcmattman wrote:
You need to make sure that the bootloader is able to load the kernel from sector 2
Alternatively, read the filesystem, as has been suggested by AJ and company. There's nothing that says you "need" to make sure that the bootloader can load from sector 2, that's a design decision ;).
...assuming that there is no filesystem. Besides, it wouldn't make very much sense to have an FS when its just a 'Hello World' kernel. If he's gonna add some meat to the kernel in the future, I guess it would. ;)

Re: How to load karnel from a bootloader?

Posted: Mon Sep 07, 2009 6:15 pm
by pcmattman
ClarionCoder51 wrote:...assuming that there is no filesystem.
Even with no filesystem, there's no "need" to load from sector 2. If I wanted to, I could design it to have the kernel at sector 42. My point is, "need" isn't the right word; "can" is better suited to your remark.
Besides, it wouldn't make very much sense to have an FS when its just a 'Hello World' kernel. If he's gonna add some meat to the kernel in the future, I guess it would. ;)
Who only gets to "Hello World" then stops ;)? Aim for extensibility in the future, so you don't have to worry about it in the future.

Re: How to load karnel from a bootloader?

Posted: Tue Sep 08, 2009 5:59 am
by ClarionCoder51
'Can', 'want', 'need', whatever. We get the point.

Re: How to load karnel from a bootloader?

Posted: Tue Sep 08, 2009 12:59 pm
by electritwist
Thank you all for the answers, but they didn't help me out, I'm trying to figure it out with a friend.
off topic: Can anyone give me an easy and short example of how to write boot loader written with C language? Is it even possible?