Page 1 of 1

GRUB Stage 1 and 2 Design

Posted: Sat Dec 27, 2008 1:54 pm
by Alboin
This may be a stupid question.

According to GRUB's documentation, and my knowledge of GRUB, stage 2 is put in the file system of a disk as a normal file.
We embed Stage 1 in a MBR or in the boot sector of a partition, and place Stage 2 in a filesystem.
Now, if stage 1 is loaded onto the MBR of the disk, which then loads the first sector of stage 2, and stage 2 is a normal file in the file system, how does stage 1 find stage 2?

In stage1.S, stage1 uses int 13h to load logical sector 1.

Code: Select all

stage2_sector:
	.long	1
How is stage2 guaranteed to be in sector 1?

Thanks,
Alboin

Re: GRUB Stage 1 and 2 Design

Posted: Sat Dec 27, 2008 4:57 pm
by Combuster
It may also be "stage 1.5", which holds the filesystem-dependent code and acts as an intermediate step between stage 1 and stage 2

Re: GRUB Stage 1 and 2 Design

Posted: Sat Dec 27, 2008 5:05 pm
by Alboin
(and optionally Stage 1.5)
It's optional, no?

Re: GRUB Stage 1 and 2 Design

Posted: Sat Dec 27, 2008 5:14 pm
by Combuster
Being optional doesn't mean its not the default :wink:

The only moment you'd not be using it is on a non-formatted media (floppy with grub alone)

Re: GRUB Stage 1 and 2 Design

Posted: Sat Dec 27, 2008 5:26 pm
by Alboin
So, on, say, a hard disk, GRUB would write stage1.5 on the sectors following the MBR, and then that would load stage 2 following its own load on stage 1? Thus, stage1.5 has all of the drivers and FS stuff in it?

Isn't it rather ugly for both stage1.5 and stage2 to contain the same FS and driver code duplicated?

Re: GRUB Stage 1 and 2 Design

Posted: Sun Dec 28, 2008 12:31 am
by Brendan
Hi,
Alboin wrote:Isn't it rather ugly for both stage1.5 and stage2 to contain the same FS and driver code duplicated?
Some of GRUB's stages might use a list of sectors. For example, when the boot loader is installed you find out which sectors are being used for the next stage and record them somewhere, so that the previous stage only needs to load the sectors in the list and doesn't need to contain any file system code.

Also, there's no reason why the next stage can't call functions, etc in the previous stage...


Cheers,

Brendan

Re: GRUB Stage 1 and 2 Design

Posted: Sun Dec 28, 2008 1:34 pm
by Alboin
Wow. GRUB is a lot uglier than I thought. It's not the pretty mystery box it's all cracked up to be...

Thanks for the help,
Alboin