GRUB: Difference between revisions

1,333 bytes removed ,  7 years ago
→‎GRUB for EFI: rewrite section; remove stupid clutter
[unchecked revision][unchecked revision]
(→‎GRUB for EFI: rewrite section; remove stupid clutter)
Line 292:
Now you have a working GRUB 2 that has the required files to build an image that boots on i386 platforms.
 
==GRUB for EFIUEFI==
===Compiling GRUB===
Older GRUB versions are riddled with nasty bugs. As this probably includes the version from your package management, you should be compiling GRUB from source. As we're compiling for UEFI, you should pass the appropriate flags to configure. An example invocation might look something like this:
<source lang="text">
../grub-2.02~rc2/configure --prefix="$HOME/opt/grub" --target=x86_64 --with-platform=efi
</source>
 
After completing the build, GRUB refused to do anything as it was missing a font file. To fix this, run
This section will tell you how to compile, build and install GRUB 2 on an EFI system. This have been tested on an MacBook Pro6,1
<source lang="text">
bin/grub-mkfont -o share/grub/unicode.pf2 /usr/share/fonts/truetype/unifont/unifont.ttf
</source>
 
GRUB might warn you about share/locale/ missing. To solution is to create the missing directory.
===Compile===
 
===Building a GRUB UEFI binary (BOOTX64.EFI)===
First you need a x86_64 Linux with GCC and Binutils. I downloaded a Ubuntu 10.4 LiveCD, booted it in VirtualBox and installed it to disk.
This method builds a standalone GRUB binary you can copy to a FAT partition. However, note that some UEFI implementations assume that it is located at /EFI/BOOT/BOOTX64.EFI for x86_64 platforms.
 
Generally, all compiled modules are included in this binary; if you want to cut down on its size, you can specify what modules to include.
You need to make sure GCC, Bison, Flex etc are installed. Do this with the apt package manager. apt-get install gcc bison flex is what i did.
 
This method uses two separate configuration files; this is needed as all GRUB data is located within the binary, but we'll be working around that. The binary contains a memdisk, which also serves as the prefix. As far as I'm aware, there's no way of working around this fact directly. We can, however, use the memdisk grub.cfg to load a configuration file from the disk. This way, we don't have to recreate the binary for every configuration change.
You need the GRUB 2 source code. I got that from ftp://alpha.gnu.org/gnu/grub/
 
Start off with creating the memdisk grub.cfg. I saved it at build/grub.cfg (we'll need this path later on):
Download grub-1.9-rc2.tar.gz and unpack it.
<source lang="text">
insmod part_msdos
configfile (hd0,msdos1)/boot/grub/grub.cfg
</source>
All this does is loading the module for reading the disk partitions (line 1) and loading the configuration file from the disk (line 2). Note that it doesn't have to be located at /boot/grub/grub.cfg; you can change this path on the disk to whatever you like, just remember to apply the changes to the memdisk grub.cfg.
 
The second grub.cfg (the one on the disk) is pretty much up to you, except that you have to load the part_msdos module and set the root (which by default is memdisk):
Next thing to do is <pre>./configure --with-platform=efi --target=x86_64</pre>
<source lang="text">
insmod part_msdos
set root=(hd0,msdos1)
</source>
Add your regular GRUB2 configuration below the <code>set root</code> line.
 
Finally, all that's left is to create the binary. Note that you have to specify what file to include at what path in the memdisk:
next step is to compile:
<source lang="text">
bin/grub-mkstandalone -O x86_64-efi -o BOOTX64.EFI "boot/grub/grub.cfg=build/grub.cfg"
</source>
 
===Blessing the binary on macOS===
<pre>make</pre>
Macs require bootable binaries to be 'blessed' by a utility:
 
<source lang="text">
Now you should have compiled GRUB 2 EFI if everything went ok.
bless --verbose --folder=/Volumes/EFI --file=/Volumes/EFI/EFI/BOOT/BOOTX64.EFI --setBoot
 
</source>
===Build Grub EFI binary (bootx64.efi)===
 
grub.efi / bootx64.efi is a bundle of tools including GRUB itself. You need to make the EFI image by selecting what modules you want.
 
For GRUB to be able to read disk partitions you need a module for that. Either you want plain-old bootsector and msdos style partition map or you want GPT (new fancy one) you need to select correct module when bundling grub.efi.
 
For your GRUB to be functional you should include as many filesystems as possible and also Linux, Multiboot, Multiboot 2 etc. so that you can read any type of kernel from any type of filesystem.
 
So... Lets build grub.efi / bootx64.efi
 
Go to "grub-core" folder.
 
Execute: ../grub-mkimage -d . -o bootx64.efi -O x86_64-efi -p /efi/boot [list of modules without .mod]
 
where "-p /efi/boot" tells where "grub.cfg" is after booting and it will be loaded from this path on booting device.
 
A easy way of adding all modules is:
 
<pre>../grub-mkimage -d . -o bootx64.efi -O x86_64-efi -p /efi/boot `find *.mod | xargs | sed -e 's/\.mod//g'`</pre>
 
That will build "bootx64.efi" in the subfolder and this can now be copied to a USB-disk, harddisk etc.
 
From UEFI specification the EFI BIOS will try to load & boot from /efi/boot/bootx64.efi on a FAT formated drive. This is for x86_64 plattform.
 
===Configuring===
 
Add a "grub.cfg" file to /efi/boot/gruf.cfg or wherever you like. Make sure to put it where you told grub to look for its files "-p option".
 
To use graphical menu you must also provide a font file. I used unicode.pf2 that i found in /usr/share/grub2 or something.
 
I did like this:
<pre>
set timeout=30
set default=0
#set debug=all
 
loadfont /efi/boot/unicode.pf2
#set gfxmode=1024x768
set gfxmode=auto
set gfxpayload=keep
terminal_output gfxterm
 
background_image -m normal /efi/boot/background.jpg
 
menuentry "OSKernel" {
set debug=all
multiboot /kernel32.exe
module /null.sys
module /console.sys
module /serial.sys
module /keyboard.sys
module /random.sys
sleep 5
}</pre>
 
Some nice to know info is that GRUB 2 (1.99rc2 atleast) parses the video mode information in the Multiboot header. It also tries to satisfy your request. This means that you can request text/video, WxHxD from the Multiboot header. Video information about mode, framebuffer address etc should be available in the Multiboot info aswell. Sweet isn't it? :)
 
===Installing===
 
I partitioned my USB Disk with OS X diskutil, but any partition tool should/would do. I created a small 100MB partition as first partition and created a huge secondary partition for files. I formated the partition to HFS+, but i guess FAT should be more compatible since i dont think other EFI firmwares have HFS+ filesystem support like the Mac...
 
Anyway i created "/efi/boot/" folder and copied bootx64.efi to /efi/boot/bootx64.efi on my disk. I then created a grub.cfg file alongside with it.
 
To make OS X find the bootx64.efi image on boot i did;
 
<pre>sudo bless --verbose --folder=/Volumes/EFI --file=/Volumes/EFI/efi/boot/bootx64.efi --setBoot</pre>
 
where /Volumes/EFI is the first partition on my usb disk...
 
Now i saved my work and rebooted... Press/hold "option" key during boot will bring up boot menu. I now get an option to boot en EFI DISK. I select this and now GRUB 2 shows like any other GRUB...
 
Configuration of grub and config file is like any other GRUB 2 syntax.
 
The "blessing" procedure should not be needed on UEFI compatible EFI versions. These will always look for /efi/boot/bootx64.efi.
 
== See Also ==
Anonymous user