Technical Information about the Boot Loader
a supplemental section in the first chapter in our series on How
to Write an Operating System
What the bootloader does for you:
- puts the i386 into protected mode
- counts available memory
- loads the SBBB image or runs the netboot system
- maps in pages to give you access to all of physical memory (up to
252MB worth)
- creates a boot page table, with identity mapped memory (so it looks
like you're in physical memory mode), except for page 0 (this is
used to catch null references).
- creates a boot stack (for function calls and local variables)
- passes execution to the entry point specified in the SBBB
directory. This is user customizable in the .ini file for
bootmaker.
When calling the _start function in the first file, the boot loader
passes as parameters the amount of memory it counted, the parameter
string, a pointer to the SBBB/Directory.
All of the segments defined by the boot loader allow use of the entire
32-bit address space from 0-4GB. In the following descriptions,
kernel means ring 0 and user means ring 3. These are just
some recommended segments, the uBoot loader is not garenteed to setup
these segments for you. Remember, one of the first things you should do
in your booting proccess is to construct the GDT that you will use.
- 0x08 - kernel 32-bit code segment
- 0x10 - kernel 32-bit data segment
- 0x18 - kernel 32-bit stack segment (not necessary)
- 0x20 - kernel 16-bit code segment (used for return to real
mode)
- 0x28 - kernel 16-bit stack segment (used for return to real
mode)
- 0x33 - user 32-bit code segment (RPL of 3)
- 0x3b - user 32-bit data segment (RPL of 3)
- 0x43 - user 32-bit stack segment (RPL of 3)
Returning to DOS
To return cleanly to DOS, you need to make sure that:
- the lower 1MB of memory isn't torn up;
- the GDTR is restored (or at least, the first 6 entries (48 or 0x30
bytes) are what they were when the kernel gained control;
- PIC1 is set at 08h, PIC2 at 70h, with all interrupts unmasked.
You don't need to worry about the last two items, unless you've changed
the GDT or the PIC registers (you can't do this without some serious
effort). However, staying out of the lower 1MB of memory is a bit
tougher, even with NULL references taken care of.
The Executable and Linking Format (ELF)
You can get a copy of the Executable and Linking Format (ELF)
specification here.
Source Code
Check out the assembly source
code for the boot loader. (Note: this is older source code for the
boot.com that is not compatible with the new uBoot system. that source
code should be up shortly, we apologize for any inconvenience.)