Kernel Bootstrap

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
tykl

Kernel Bootstrap

Post by tykl »

I'm intrested in trying to create a simple microkernel(For learning).
I have found a lot of useful material on many parts of a kernel
but I have not found usable information on a bootstrap.
I have found example bootstraps and some that are even explained.
Most expect you to get the kernel from the hard drive or
that you have a filesystem on a floppy, or are just plain badly
written. What I want to do is write a bootstrap and a kernel image
to a raw floppy disk and have the bootstrap load the kernel which may
load a built-in mini-shell or a shell located directly after
it on the floppy. The operating system I'm developing on is
Linux (Kernel 2.4.18), so I'd probably use dd to write the images
to disk. Are there any very detailed tutorials on how to do this
for newbies to kernel devlepment?
J.

RE:Kernel Bootstrap

Post by J. »

I wrote a little tut on booting that might help.  I wrote it a long time ago, though... '97, I think.  Everything's still relevant, though.  You can find it at:

http://www.nondot.org/sabre/os/files/Bo ... olyOS.html

Really, all you wanna do is use the int 13h, ah=02 service, to load in sectors after the boot sector (sector 1, according to the bios... sector 0, everywhere else :) and jump to them.

Jeff

PS: Please note that it was brought to my attention that my tut doesn't initialize pmode correctly, which is true... I don't load a valid GDT/IDT
(simply because I hadn't written either at that point), and I haven't loaded
all the registers with valid protected mode segments... this is fairly
inconsequential to your purposes, though... I'd test your loading code in
real mode first... remove the compilication of pmode until later.
Schol-R-LEA

The limits of tutorials

Post by Schol-R-LEA »

Unfortunately, there do not sem to be any tutorials that quite match your intentions, at least none that I know of. Different OS desiginers have different goals and visions, and each one reflects the trade-offs and priorities of the original designer. Also, most of them were not written to be tutorials, and so many are rather confusing to follow. Generally, those tutorials that are out there are either documentation of an existing design added as an afterthought, or else are limited 'demonstration' programs written solely to show one part of the process without really accomplishing much. Ironically, the part most often skipped is the actual loading of and transfer of control to the second stage, which is the most important (and most difficult, despite the complexities of p-mode and file systems) part of a boot loader, by definition. Finally, many have been written rather carelessly, and often the actual 'tutorial' code doesn't not actually work.

Generally, most boot loaders follow one of two directions, which can be called 'file-system first' and 'p-mode first'. These generally reflect whether the design is going from high-level to low-level or vis versa. Some truly butch coders may wish to try to do both, but usually, the space limitations are such that it is not feasible.

The reasoning behind the 'filesystem first' approach is simply that, while it requires a greater code commitement that is often difficult to fit into a bootloader, it allows you to use ordinary executable files as your kernel, rather than a raw binary. This makes it far easier to use the system itself as a development platform, measn that, in your disk, only the boot sector itself is a special case not part of the filesystem. Also, many amateur OS developers want to use an established filesystem - FAT in particular, as it is well known and trivial to implement - which usually means that the sectors following the boot sector (not to mention the DIB in the boot sector itself) are already spoken for by the file systems design. Thus, if you want to use FAT (or ext2fs), you have to read the filesystem to get the second stage for loading.

The 'p-mode first' approach is more common among designers for whom the filesystem is a secondary consideration. The usual goal here is to make the transition from real mode to protected mode either before or during the transition to the second stage, thus ensuring that the system code is all 'p-mode pure', and they can avoid having to worry about having to deal with 16-bit code after that point. While this is in many was a nice idea, it is also means that you often have to set up the GDT and related tables on relatively limited information, usually hardwiring the system assumptions that could be a problem later. Also, it means that the second stage has to be loaded as raw code (unless you've also managed to fit the FS code), which makes the process of recompiling and reloading the kernel more difficult, especially if the size of the kernel changes.

I know it isn't reall what you want, but I would recommend taking a look at my own bootloader example at
http://204.215.248.28/board.jsp?message=1539 (it is also at
http://www.mega-tokyo.com/forum/index.p ... readid=775 , which is probably a bit easier to read). It is the closest thing to what you are talking about that I know of, and while it is sparesly commented right now, I have ben considering rewriting it as a tutorial (with substantial additions to cover some specific circumnstances, and considerably clarified code). ALso, while they are hardly a comprehenisve example, you may want to look through CrazyBuddha's "Baby Steps" pages on Megatokyo
(http://www.mega-tokyo.com/forum/index.php?board=1). HTH, somewhat at least.
Schol-R-LEA

Two other points to make

Post by Schol-R-LEA »

First, it would help if you could tell us more of what you goals wih the design are, since that will have considerable impact on what advice is relevant. A few of the questions that could be important:
- What is the intention behind your kernel? Do you intend it as an experiment, or a production system? Is it meant for a special-purpose system (i.e., a floopy-bootable dedicated router). Do you intend to write a prototype first and redesign from that, or will it be 'hot coded'?
- How large do you expect the kernel to be? How will the boot loader determine the size of the code it has to load in?
- Is this a floppy-only design, or will you create a modified version for booting from a hard disk?
- What kind of file system, if any, do you intend to use?
- Will your kernel in real mode, unreal mode, or protected mode? If it is a p-mode system, will it support v86 mode?
- Do you intend to use only assembly language for the kernel itself, or will you use a HLL like C, Pascal, LISP, etc? Will it have loadable modules loaded from secondary files?

You get the drift. The more information, the more we might be able to help.

Second, if this is meant as a production system, you may want to reconsider the need to write your own bootstrap at all. While it is a very good idea to write one or more as an experiment, to familiarize youself with the boot process, writing your own working, robust bootloader is a considerably more daunting task than writing a test boot loader that only needs to work on a single testbed machine. This becomoes even more the case when dealing with hard drives. The variations in PC hardware and BIOS support are enough to make it impractical to code one of your own for the final version of you system. The best advice I know of is to try using GRUB, which has the advantage of being the reference implementation of the Multiboot specification, and will work with nearly any Unix-like OS; further, it handles the switch to p-mode, and can boot from a variety of file systems. Unless your OS boot process is *really* unusual, or you don't intend to ever boot from a hard disk, you should seriously consider using tit once you are certain you understnad the boot-up process throughly. Just my recommendation, as probably somewhat premature to be considering it at this stage.
geezer

RE:Kernel Bootstrap

Post by geezer »

Things a bootloader could do:
http://www.execpc.com/~geezer/osd/boot/steps.txt

PC bootstrap process in general:
http://www.execpc.com/~geezer/osd/boot

First-stage loaders -
works well:
http://www.execpc.com/~geezer/temp/fat12.asm

untested:
http://www.execpc.com/~geezer/temp/fat16.asm

works with 1.44 meg floppies but not well-tested:
http://www.execpc.com/~geezer/temp/ext2.asm
http://www.execpc.com/~geezer/temp/e2boot.zip

if you want to ignore the filesystem and just load
a bunch of contiguous sectors (untested):
http://www.execpc.com/~geezer/temp/contig.asm

Now if I could just write a decent second-stage...
Compuboy

RE:Kernel Bootstrap

Post by Compuboy »

  I've written a tutorial that goes over exactly what you want to do in a lot of detail. You may be interested in checking it out. It's at http://www.electrichamster.net/Lucie, in the download section. It's a work in progress but I've already written all about loading a Bootstrap.
Post Reply