I'm having great difficulty understanding how core.img is able to reference a filesystem and load modules. After boot.img is loaded from the MBR, which then loads core.img (located in the post-MBR gap), core.img's job is to load the MBR and initramfs into RAM alongside a kernel, then & pass control over to it. However, to do that, it has to be able to interact with hardware and understand filesystems, which, in my opinion, implies that it needs a kernel and a module to understand filesystems. However, I can't figure this part out through their documentation. Can someone explain how the initramfs gets loaded, since it's typically located in /boot/grub/, which would imply an ability to read a filesystem and be able to communicate with a hard disk to locate the temporary initramfs + kernel to load into RAM. But without a kernel, how could those packages be obtained and put into RAM?
2 Answers
GRUB has its own kernel, which provides common services and frameworks for the rest of the bootloader. This can run in a variety of contexts, including “bare metal” with direct hardware access, simple BIOS-style services (as used in MBR-based x86 configurations) and less basic environments like EFI (which is closer to a single-tasking operating system, with support for running programs, allocating memory etc.).
core.img’s job isn’t to load the OS and kernel; it’s to allow GRUB to find its configuration file and any other modules it may need. To do this, core.img itself contains more than the GRUB kernel; it also includes the drivers necessary for GRUB to find the rest of itself, so at least those required to access the relevant disks and file systems. See the “GRUB image files” chapter in the user manual for details. This combined image is how GRUB can access the files it needs to load; on an MBR x86 PC with /boot using Ext2 for example, it would include the biosdisk module to access disks using the system BIOS, the part_msdos module to interpret the partition table, and the ext2 module to access the contents of the Ext2 file system. Thus GRUB can find its configuration file, and then load any other module it needs. Ultimately the combination of modules in core.img and /boot enable it to initialise the display, show its menu if necessary, find the appropriate kernel and initramfs, load them into memory and boot the kernel.
Since you’re curious about how GRUB works, you might find the GRUB developers manual interesting, in particular the “Finding your way around” chapter.
“Modules” is a rather generic term; it doesn’t necessarily refer to kernel modules.
-
1Although modern GRUB can indeed include a bare-metal driver for certain common disk controllers like AHCI, I think it is far more common for the MBR version of GRUB to rely on BIOS calls: INT 10h for display access, INT 13h (in its numerous variations) for disk access, and INT 16h for keyboard.telcoM– telcoM2024-08-29 08:21:51 +00:00Commented Aug 29, 2024 at 8:21
-
@telcoM indeed, thanks, I’ve updated the answer to distinguish BIOS-based setups from bare metal.Stephen Kitt– Stephen Kitt2024-08-29 08:30:08 +00:00Commented Aug 29, 2024 at 8:30
-
Can you provide reference for grub having its own kernel, as I don't believe this. As far as I'm aware initrd holds a kernel, which is loaded into ram, with the goal of passing over the root filesystem to a disk. Grubs job as a bootloader, is to find bootable disks, provide boot options, and load the initrd. How the initrd is loaded into RAM is the real question. Assuming grub uses core.img to load /boot/initrd.img into RAM, it must require knowledge filesystems and have drivers for the disk. It feels like a cut-down set of code is used to be able to achieve this, but I don't know howjohn smith– john smith2024-08-29 09:01:31 +00:00Commented Aug 29, 2024 at 9:01
-
@johnsmith there’s a link to the GRUB kernel in my answer. It’s not the Linux kernel, it’s a GRUB-specific kernel.Stephen Kitt– Stephen Kitt2024-08-29 09:21:39 +00:00Commented Aug 29, 2024 at 9:21
I'll post my own answer as I think I understand enough now about how the files can be located without a kernel. In the documentation (https://www.gnu.org/software/grub/manual/grub/grub.html) it states:
"One of the important features in GRUB is flexibility; GRUB understands filesystems and kernel executable formats, so you can load an arbitrary operating system the way you like, without recording the physical position of your kernel on the disk. Thus you can load the kernel just by specifying its file name and the drive and partition where the kernel resides. "
Additionally, I researched the how modules are loaded in RAM with GRUB when using MBR as opposed to UEFI and found that it loads modules located in the directory below:
/boot/grub/i386-pc
As such one of those modules is named ext2.mod, which subsequently is used to load other versions of ext such as ext3 and ext4, and this directory is still used with x64 MBR based systems.
I then discovered how the disks drivers/modules are loaded, and there is a module in that same directory called: biosdisk.mod . This is a series of software interrupt routines provided by the BIOS (Basic Input/Output System) that handle low-level disk operations. They are used by GRUB to perform disk-related tasks before the operating system's own drivers take over.
As such, this leads me to me answer as to how it's possible to be able to communicate with hardware, and understand a filesystem without a kernel. It's a series of modules within GRUB that can be used to locate files on the disk. This is why GRUB is able to locate the initramfs, and kernel to load a temporary filesystem in RAM.
Took me a really long time to find this info, so I hope this helps someone else. I'm also quite glad that I'm technically capable enough to ignore incorrect answers, even when I get downvoted for being right.
-
Your answer hasn’t been downvoted... How does GRUB access
/boot/grub/i386-pc? Your answer doesn’t explain that. (My “incorrect” one does, but I take it you don’t agree. Thanks for the downvote by the way.)Stephen Kitt– Stephen Kitt2024-08-29 15:29:05 +00:00Commented Aug 29, 2024 at 15:29