How to load second stage boot loader from first stage?

On x86 you would do the following (simplified):

  • Have the bootloader load the n-th sector of the disk/floppy (wherever you’re booting from) into memory and execute it (i.e. load segment/offset and do retf). A better alternative is to search the filesystem for a certain filename (e.g. KERNEL.BIN) — but you’d need to know the file system type (e.g. FAT12 if you’re testing from a floppy image).
  • The kernel would then start in real mode. It sets up code descriptors, GDT, and so on, activates 32-bit addressing (you should have heard of “A20”) and finally enters protected mode. Then you need a far jump to a 32-bit code segment (kernel file must be linked together in a way that the 32-bit code is at an absolute position, e.g. at offset 512, right after the 16-bit real mode stuff).
  • The 32-bit kernel assembly, then, just defines EXTERN _mykernel (for example) and calls that symbol.
  • Then you can begin writing your kernel as C function mykernel.

Okay that was a short overview of what I did a few years ago (with lots of copy&paste from the Internet ;). If that isn’t helpful, here are some good web resources on OS development:

Hope that helps ^^

Leave a Comment