I have been reading about Grub legacy boot loader. And what struck me is how Grub handles file systems. Grub supports a large subset of file systems. My doubt is, How Grub recognizes these file systems from one another? Even if they are recognized, How are they been processed? Like every file system has their own implementation. Does Grub loads runtime libraries from disk?
I know the question is too broad. But please give me some tips to kick start.
Thanks.
The typical setup is that GRUB stage 1 (which is 1-sector binary) knows location of stage 2 and loads it. This location is written into stage 1 by installation command (setup
or install
). (More precisely, start of stage 2 file is updated with list of physical locations of other file parts; stage 1 loads a starting sector of stage 2, and stage 2 continues this bootstrapping with rest of stage 2.) If stage 1 isn't updated with real stage 2 setup, it expects stage 2 in sectors just after stage 1 (a variant e.g. for GRUB diskette).
Once stage 2 is loaded, it can do all following operation. Stage 2 embeds a set of read-only drivers for all supported FSes; such drivers are much smaller than full-function drivers. Installation process includes also coding of boot drive and partition into stage 2, so, after boot it knows where to find the effective config ([/boot]/grub/menu.lst
for this version). Suddenly, FS type isn't coded in stage 2 config aread. Detection of FS type is done in *_mount()
functions (file set stage2/fsys_*.c
); each one does its best to detect that it's for it, including signature check and partition type check in partition table. I don't know why this style is choosed, but seems they expected FS type detection to be rock stable.
Since stage 2 is loaded, fed with proper internal config (device, partition) and opened its FS, it is able to load the menu and continue with higher level logic.
(This description didn't include stage1.5 because the latter is used under rather specific circumstances.)
So, answering your specific questions,
How Grub recognizes these file systems from one another?
Both with signatures and partition types (DOS, BSD...)
Does Grub loads runtime libraries from disk?
No, it uses own read-only drivers.
But please give me some tips to kick start.
If you can read C, the best way is to clone its repository, checkout grub-legacy
branch and read its sources.