Rolling Your Own Bootloader: Difference between revisions

m
Bot: Replace deprecated source tag with syntaxhighlight
[unchecked revision][unchecked revision]
m (added bootloaders category link)
m (Bot: Replace deprecated source tag with syntaxhighlight)
 
(4 intermediate revisions by 4 users not shown)
Line 1:
{{rating|2}}
Some people prefer to use their own software for everything, or wish to try their hand at coding a bootloader. This page attempts to describe what steps to take when you write your own bootloader. Before you start writing one, it is best that you know the background [[Bootloader|theory]].
 
== What and Why ==
=== Disclaimer ===
 
Okay. You are here because you don't want to use a mainstream boot loader. You may also want to code your own bootloader as a learning experience to better understand how they function. We also have pages on many [[Bootloaders]] developed by this community, there's the [[Bare bones]] but still people complain we don't have a page explaining how the bootloader can be coded.
 
I won't try to give you full code that works because if that was what you were looking for, you'd be using one of the [[:Category:Bootloaders|premade bootloaders]] instead. This page plans to tell you what is needed and what could be wished in a bootloader, and optionally points at parts of the FAQ that can help you achieving the goals.
 
Whether or not you'll use your own bootloader or reuse an existing tool is completely up to you. If you get the feeling you don't understand a thing, make sure you read our page about the [[Boot Sequence]] first.<br />
 
A good reason to have a custom bootloader would be a custom filesystem.
A good reason to have a custom bootloader would be a custom filesystem, though you could add support for your filesystem to [[GRUB]] or perhaps some other bootloader. (GRUB is designed to accommodate such additions, other bootloaders may not be.)
 
=== What you need to do ===
Line 60 ⟶ 61:
[[BIOS]] interrupt 13h. Get info about it at [[Ralf Brown's Interrupt List]], make sure you know (the now outdated) floppies may fail one or two times, that you cannot read more than a track at once, you have to use CHS addressing and you're done.
 
To read from the hard drive (which is the preferred way these days, also used by CDROMs and USB-sticks), you probably want int 13h, ah=0x42, drive number 0x80 that uses simple [[LBA]] addressing. Details in the interrupt list.
 
If you need guidance, feel free to check [http://clicker.cvs.sourceforge.net/clicker/c32-lxsdk/kernel/src/sosflppy/lowlevel.asm?view=log lowlevel.asm]
Line 105 ⟶ 106:
To enter protected mode you should first disable interrupts and set global descriptor table. After it set PE bit of CR0:
 
<sourcesyntaxhighlight lang="asm">
mov eax,cr0
or eax,1
mov cr0,eax
</syntaxhighlight>
</source>
 
After it set registers and do a far jump to kernel.
If data selector is 10h, code selector is 8 and kernel offset is 10000h do:
<sourcesyntaxhighlight lang="asm">
mov ax,10h
mov ds,ax
Line 121 ⟶ 122:
mov ss,ax
jmp 8:10000h
</syntaxhighlight>
</source>
 
Notes:
Line 134 ⟶ 135:
 
== Help I'm Stuck! ==
The only way to figure out what's wrong with your boot loader is to debug it in a VM. You could probably print out variables, but the limited space makes this uneccesairlyuneccesarily hard.
Also reading [[My_Bootloader_Does_Not_Work|common mistakes and gotchas]] may give you ideas about your issue.