GRUB Legacy

From OSDev Wiki
Jump to navigation Jump to search

GRUB stands for GRand Unified Bootloader and is a bootloader and the reference implementation of the Multiboot Specification. GRUB can boot any Multiboot-compliant OS (e.g. Hurd, among others), however it can also boot some non-Multiboot-compliant OSes, e.g. Windows, via a chain-loading function, and some others via special support, e.g. Linux. mbchk can be used to determine if a file is Multiboot-compliant.

This article concentrates on GRUB Legacy. Information regarding modern GRUB, a full rewrite, can be found in a separate article.

Features

GRUB takes away all the complexities out of trying to boot your OS by doing it for you. GRUB can handle a variety of filesystems: from MS-DOS FAT systems over Linux ext2fs (Second Extended File System) to BSD FFS, and can load kernels in various binary formats, including "generic" ELF, FreeBSD a.out, flat binary, and "generic" executables (any file with a valid Multiboot header).

One major advantage of GRUB (or more precisely, a Multiboot-compliant bootloader) is that the kernel will be entered in a known state, which includes the A20 Line having been enabled, and Protected mode having been entered. This takes a lot of the pain out of writing a kernel, rendering GRUB a very useful tool for the amateur, or anyone who wants to spend more time on the intricacies of the kernel rather than worrying about these generic start-up procedures.

Consult the Multiboot Specification if you want to know what GRUB expects and what you can expect from it.

Installation and booting

This section describes how to install GRUB on your actual computer, if you are using some kind of emulator (e.g. Bochs) to test your OS in you're probably more interested in the setup described in e.g. Booting the kernel or Environment setup. However, you might still be interested in how to load your kernel over TFTP.

Installing to floppy

This tutorial will show you how to install GRUB and your self-made kernel on a floppy disk, to avoid screwing up your hard drive. Installing GRUB as your system bootloader depends heavily on your system setup and is beyond the scope of this tutorial.

GRUB cannot be installed directly to the intended boot device. Once an operating system is up and running, GRUB's probing the BIOS for available drives might yield wrong results. Thus, a four-step process is used (using two floppy disks: the boot disk to-be, and an auxiliary disk).

NOTE: creating your own GRUB bootdisk requires a Unix-like environment. If you're working under Windows (or don't feel like setting up a GRUB disk with Linux), check out our Disk Images page to get a ready-to-use image.

  1. Prepare the boot disk;
  2. create a rudimentary GRUB installation on an auxiliary disk;
  3. boot from the auxiliary disk, and complete the GRUB installation on the boot disk.

You will find the source as well as precompiled binaries at the GRUB web page. (Windows users will want the grub-0.9x-i386-pc.tar.gz package.) Download and unpack the archive.

Preparing the boot disk

Format a floppy disk; this will be your boot disk. Create a directory boot on that disk, and copy the files stage1 and stage2 from the tarball (or your build directory) to boot. Note that, after the installation, stage2 must not be relocated (e.g. by Windows' disk defragmentation), or the bootloader will be unable to find it.

Auxiliary GRUB install

Remove the boot disk from the floppy drive and insert the auxiliary disk. The files stage1 and stage2 from the GRUB distribution must be written to this disk, to sector 0 and sector 1+ respectively. In your shell (for Windows users, this means the Cygwin shell), type:

dd if=<stage1> of=/dev/fd0 bs=512 count=1
dd if=<stage2> of=/dev/fd0 bs=512 seek=1

Now GRUB will be able to boot stage1 and stage2 from this disk. We cannot use it as our boot disk, though, since writing stage2 to sector 1 destroyed the floppy's format structure (you can't write files to the disk anymore, and any previously written boot directory could no longer be found). That's why we will now set up the other disk to boot GRUB and have a valid structure.

Leave the auxiliary disk in the drive, and boot from that floppy.

Completing the install

GRUB will boot into its command shell. (If your system does boot from hard drive instead, check your BIOS boot drive settings.) When the grub > prompt appears, remove the auxiliary disk from the drive and replace the boot disk. From the GRUB shell, you can now complete the installation on the boot disk:

install (fd0)/boot/stage1 (fd0) (fd0)/boot/stage2 (fd0)/boot/menu.cfg

The parameters tell GRUB:

  • where to find the stage1 file;
  • which device to install GRUB on (the floppy drive) - this writes stage1 to the boot sector, you can delete boot/stage1 afterwards;
  • where to find the stage2 file (hardcoding the sector number of that file into the boot sector stage1; that's why stage2 must not be moved after this procedure);
  • where stage2 should look for the configuration file.

Remove the boot disk from the drive, and reboot your operating system so you can edit /boot/menu.cfg.

Configuring GRUB

When you boot from the just-created boot floppy, GRUB stage1 is read from the floppy's boot sector. This stage1 "knows" where to find stage2 (it was told so during installation), and loads that. The stage2 does lots of interesting stuff (we'll come to that), but most importantly, it "knows" how to read file systems, so it can find the configuration file, boot/menu.cfg.

The configuration file tells GRUB where to look for the actual kernel image(s). You can set it up so it offers you a selection of various different kernels, even allowing you to boot your "real" OS from hard drive after booting GRUB from floppy. (In the end, you will probably want to install both GRUB and your own OS kernel on the hard drive, but we'll stick with the floppy solution for now.)

Add an entry like this to the configuration file:

title   MyOS
root    (fd0)
kernel  /boot/kernel.bin

Now all we have to do is to compile a kernel image and save it as /boot/kernel.bin. GRUB will find, load, and start it. For more details, menu options etc., refer to the GRUB manuals.

Installing to USB

NOTE: I have tried many tools to install GRUB onto a USB device under windows, however, I have not been able to figure out how. So, to install, either use a Linux distro on your PC of use a Linux Live CD.

Putting GRUB onto a USB device is as easy as following the above example of installing to a standard hard disk. There is one extra step involved, finding out under which mount point your USB device is located. You may even have to mount it yourself if it is not already mounted. To install use the following steps:

If you need to mount your USB device: (Exchange sdb1 with your device name.)

mkdir /mnt/sdb1
mount /dev/sdb1 /mnt/sdb1

Create required directories:

mkdir /mnt/sdb1/boot
mkdir /mnt/sdb1/boot/grub

Locate the stage1, stage2, and *_stage1_5 files:

find / -name stage1

Copy stage1 and stage2 files, /usr/lib/grub/ is an example location, use the location returned by the find command:

cp /usr/lib/grub/stage1 /mnt/sdb1/boot/grub/stage1
cp /usr/lib/grub/stage2 /mnt/sdb1/boot/grub/stage2

For FAT formatted USB device use:

cp /usr/lib/grub/fat_stage1_5 /mnt/sdb1/boot/grub/fat_stage1_5

For ext2 formatted USB device use:

cp /usr/lib/grub/e2fs_stage_1_5 /mnt/sdb1/boot/grub/e2fs_stage_1_5

Then execute the GRUB install application:

sudo grub

Enter the following commands:

root (hd1,1)
setup (hd1)
quit

if you don't know where your kernel is, you can use the command "find /boot/grub/e2fs_stage_1_5" to get the parameters which would be used in the command root (sdb1,1) and setup (sdb1).

Installing to hard disk

See Installation in the official GRUB manual.

Installing to CD

See Bootable El-Torito CD with GRUB Legacy.

Load your kernel over network (TFTP)

This page or section refers to its readers or editors using I, my, we or us. It should be edited to be in an encyclopedic tone.

You can also use GRUB's builtin network support to increase your build/test turn around time. You can boot another machine with a kernel directly from your build directory without having to copy it over manually. See also, How to set up your network in the official GRUB manual.

Getting GRUB to support your card

First you need a version of GRUB that supports your network card. Most likely you will have to build one from source, so grab it and extract it to a work directly.

Run

./configure --help

to see a list of options. In my case, I use Netgear tulip cards, so I execute:

./configure --enable-tulip
make
make install

You may want to consider using the --prefix option if you don't want to overwrite your existing GRUB install or do not have root access.

NOTE: this was done on a Linux box, I assume using Cygwin under Windows would work as well.

Set up a TFTP server

You will need a TFTP server running on your build machine and pointing to your build directory. I've been using the 3CServer utility from 3COM, but there are several out there for most platforms.

Make sure any firewalls you have installed allow incoming connections on the TFTP ports.

Install as you would normally

On your target machine install the GRUB binary you just built as documented above.

Configure GRUB

I use the following entry in my menu.lst to boot my kernel.

title Network Boot
# Obtain an IP address for this card
dhcp
# I don't have DHCP setup to deliver the TFTP server's IP address,
# so I set it manually this is my build machine
tftpserver 192.168.0.50
# The root device is the network
root (nd)
# My kernel is directly under the TFTP directory
kernel /kernel.exe
# I also load a module for testing out multitasking
module /module.exe

And that should be it. If you're not running DHCP you can use ifconfig to manually set addresses, gateways, etc. Your can also use the BOOTP or RARP protocol.

Other loaders that are Multiboot compliant

Chris Giese's MBload also works with Multiboot-compliant kernels. Check out the related thread for more info.

See Also

Wiki Pages

Tutorials

Threads

External Links