How does GRUB's stage 1 boot loader loads the stage 2 bootloader?
Asked Answered
B

1

7

From the past week I am puzzled with the question that how the grub first stage boot loader which is only 446 bytes is able to search for the second stage, when the second stage is in a complex file system! How does it locate the second stage?

When a complex partitioning scheme of windows and linux is in place, and the linux system is entirely in the extended partition, then how does the stage 1 finds the stage 2? Even stage 1.5?

All of the grub tutorials skim through this important part. I have searched though the internet but couldn't find anything that explains this. Sadly, I am not an assembly programmer.

I want to understand the boot process intricately in terms of which sectors of the hard disk are tickled (and roughly how) during bootup. *Please point me to a good resource or answer here. It will greatly help me to play with grub wisely.*

Some Resources Searched:

  1. How Linux Works : What every superuser should know by Brian Ward
  2. http://www.dedoimedo.com/computers/grub.html
  3. some past stackoverflow questions.
Bookrest answered 10/5, 2013 at 12:41 Comment(0)
D
4

Stage 1 "knows" where Stage 2 is by entries in a block-list loading table embedded in it. It loads the lists of blocks off of the booting drive, then jumps to a specified CS:IP in 16-bit real mode. These are described in the page on embedded data. It queries the BIOS for the disk geometry, and maps the linear block numbers there to C:H:S addresses used by the INT 13h BIOS interface.

http://www.uruk.org/orig-grub/technical.html

Also a lot of time with new disk alignment, there is just under 1-2 MB of unallocated space between the MBR and the first partitions and stage 2 is sometimes stuck there.

Diadem answered 11/5, 2013 at 18:22 Comment(2)
Thanks a lot! The document is quite explaining and i got the big picture. Since the second stage is on a file system the file system should not move the second stage at any cost (since the addresses are absolute C:H:S. The system would break if stage 2 is moved, as I understand. I am a bit confused with the 8GB disk access limit. Does the second stage has to be within this limit? The document does mention things related to it but i am confused.Bookrest
The document is fairly old. In 2003 and 48-bit adressing mode was introduced to the ATA specification (and thus most BIOS's) that allowed the bios to access up to the first 2 Terabytes of a drive. Now EFI systems have an even bigger methods for addressing drives by blocks, (not to mention that it's possible to install filestytem drivers in EFI so a stage 2 can be loaded by name in some circumstances. And when people had that 512MB limit they would make sure the stage 2 was below it by keeping a separate /boot partition that was entirely below the limitation imposed by the BIOS.Diadem

© 2022 - 2024 — McMap. All rights reserved.