Where to put the second stage boot loader

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
User avatar
Revelation
Member
Member
Posts: 47
Joined: Sat Jun 21, 2008 8:15 am

Where to put the second stage boot loader

Post by Revelation »

Hi,

Now that my kernel is ripe for memory management, I need a memory map. Unfortunately, I can't fit the code to load it in my bootloader. So I decided to build a bootloader in two stages. The only question I am asking myself is: where should I put the second stage bootloader on the disk?

First I thought of letting the kernel start with the second stage bootcode, but I have never seen anyone do that. What should I do?
Now is the winter of my disk content.
PatrickV
Member
Member
Posts: 151
Joined: Sun Jul 06, 2008 7:50 pm
Location: New Zealand
Contact:

Re: Where to put the second stage boot loader

Post by PatrickV »

The best thing to do is put it in the next sector after your first stage.
User avatar
Zenith
Member
Member
Posts: 224
Joined: Tue Apr 10, 2007 4:42 pm

Re: Where to put the second stage boot loader

Post by Zenith »

Sadly enough, the statement (the easiest thing to do == the best thing to do) is rarely true when it comes to OSdev.

If you were desperate/lazy and willing to make your floppy (if that is the medium you are using) unreadable by most OSes, then go ahead and put the second stage loader in the next sector. The truth is, this method is hardly flexible and I don't advise it in the long run.

Most x86 bootloaders do one of the following:
- Parse the filesystem for a specific file which is the second stage and load it. This of course, requires filesystem support, and so you may have different bootloaders for floppies, CDs, and other mediums with different file systems.
- The LBA and drive of the second stage bootloader is hardcoded into the bootloader upon install. This method is filesystem independent, but it does require you to make sure in some way that the second-stage loader NEVER gets moved.
"Sufficiently advanced stupidity is indistinguishable from malice."
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: Where to put the second stage boot loader

Post by 01000101 »

I personally place mine in the following sectors of the floppy, but my OS doesn't need to be read in other OS's and it doesn't need to have any sort of compliance with FS standards.
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Re: Where to put the second stage boot loader

Post by Alboin »

I would suggest doing what Zenith advised against, and, later, if you do need to have some FS support on your boot floppy, add FS support to your bootloader.

Evolving an OS is a lot easier that spontaneously creating one.
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
Masterkiller
Member
Member
Posts: 153
Joined: Sat May 05, 2007 6:20 pm

Re: Where to put the second stage boot loader

Post by Masterkiller »

Zenith wrote:If you were desperate/lazy and willing to make your floppy (if that is the medium you are using) unreadable by most OSes
Most common FileSystem for 1.44 Floppy image is FAT12. I don't know what term you have in mind with "Most OSes". Generally it is impossible things portable for all the OSes, so Linux and Windows will be good enough. Both OSes can read FAT12 and the common structure of FAT12 is:

1. Boot Record
2. Reserved Sectors (Number of sectors is specified in Boot Record)
3. FAT two times (Information about size of this field is available at Boot Record)
4. ROOT directory (Number of entries in ROOT field in Boot Record/16)

By Default reserved sectors field is zero and the FAT table comes just after the Boot Record. If you set it to one, the disk becomes unreadable until you move all sectors of FAT tables and Root directory one sector up. Freed sector is available to use for the second stage loader and also is guaranteed not to be overwritten while other OS writing to your floppy diskette.
ALCA OS: Project temporarity suspended!
Current state: real-mode kernel-FS reader...
User avatar
Zenith
Member
Member
Posts: 224
Joined: Tue Apr 10, 2007 4:42 pm

Re: Where to put the second stage boot loader

Post by Zenith »

You're right, it should be supported by a lot of OSes, but from experience, I had troubles getting Linux and (sometimes) Windows to recognize a floppy with more than 1 reserved sector (honestly, I also used the consecutive sector method in my old bootloader). But this of course assumes you're using a floppy drive instead of some other boot medium like a CD.
"Sufficiently advanced stupidity is indistinguishable from malice."
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Where to put the second stage boot loader

Post by Combuster »

A well designed bootloader can work off a floppy, HD and CD without any changes in code. There are enough similarities between them. In the CD case, El Torito provides everything and the kitchen sink.

In fact, the more challenging task is portability to the harddisk.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply