3

I have always wondered how does GRUB 2 work. While reading it's source code, I saw that there are some #include directives which include seemingly nonexistent "grub/machine/...", for example in grub-core/boot/i386/pc/boot.S bootloader file there is

#include <grub/machine/boot.h>

As it seems, it provides some architecture-specific macros, and my current guess is that it is created from sort of a mixin of files and macros from include/grub when compiling for a specific architecture (e.g. #include <grub/machine/boot.h> might be directly copied from include/grub/i386/pc/boot.h, which I can assume by looking at macro references in boot.S and corresponding definitions in i386/pc/boot.h), but I am very unsure about my assumption as I have failed to find any documentation or article about that. Also I'm failing to understand, in case my assumptions are at least somewhat correct, why are some directories in include/grub not just not similar to eachother (with, for example, having some files in common, like arm/ having it's own boot.h), but, in some cases, completely different, which seems counterintuitive from my perspective.

I would be glad to get some explanation on what is really going on behind those curtains as I'm trying to learn kernel development myself.

NotYourFox
  • 35
  • 5

1 Answers1

1

In ./configure.ac, there is this line

      AC_CONFIG_LINKS([include/grub/machine:include/grub/$cpudir/$platform])

That line creates a symlink to the right CPU architecture ../../include/grub/i386/pc after I ran ./autogen.sh && ./configure && make on my machine. Inside of that directory will be the architecture dependent boot.h file.

I find it easier to understand repos like this running some of the build commands locally.

yxre
  • 3,576
  • 21
  • 20
  • But what about folders which do not contain the boot.h file, for example, arm? Is the header still being inherited from somewhere? – NotYourFox Jun 18 '23 at 11:54
  • 1
    It looks like `grub/machine/boot.h` is used only in architecture specific files. For arm, it relies on `uboot.h`. It looks like `boot.h` is used for multiple architectures, and `uboot.h` is used for others. – yxre Jun 18 '23 at 12:16