GRUB Stage 1 and 2 Design

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
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

GRUB Stage 1 and 2 Design

Post 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
C8H10N4O2 | #446691 | Trust the nodes.
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: GRUB Stage 1 and 2 Design

Post 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
"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 ]
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Re: GRUB Stage 1 and 2 Design

Post by Alboin »

(and optionally Stage 1.5)
It's optional, no?
C8H10N4O2 | #446691 | Trust the nodes.
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: GRUB Stage 1 and 2 Design

Post 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)
"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 ]
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Re: GRUB Stage 1 and 2 Design

Post 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?
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: GRUB Stage 1 and 2 Design

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Re: GRUB Stage 1 and 2 Design

Post 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
C8H10N4O2 | #446691 | Trust the nodes.
Post Reply