Boot image

Grub starts execution with the boot image. The boot image is 512 bytes and can be found in file form at /boot/grub/boot.img. The boot image is 512 bytes because that is how big the master boot record is. On a BIOS-based computer, a disk is booted by reading the first 512 bytes of a disk into memory address 0x7c00 and jumping to that location to begin execution. On an x86 or x86-64 system, the source code for that boot image can be found in the grub source code in assembly as grub-core/boot/i386/pc/boot.S. The bootloader is written in 16-bit assembly so it may help to be familiar with the 16-bit instruction set as well as the register set used. It's also useful to know how interrupts work since they get enabled and disabled at various times. Intel makes their manuals specifying these details available at http://www.intel.com/products/processor/manuals.

/*-*-Asm-*-*/

This is just a magic comment that tells certain text editors to treat this as assembly and edit in an appropriate mode.

/** GRUB -- GRand Unified Bootloader
* Copyright (C)1999,2000,2001,2002,2005,2006,2007,2008,2009 Free Software Foundation,Inc.** GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,or*(at your option) any later version.** GRUB is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.** You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.*/

This tells the assembler that we are starting the boot.S logical file.

.text

This tells the assembler that the text portion of the executable format starts here.

/* Tell GAS to generate 16-bit instructions so that this code works
in real mode.*/.code16

This tells the assembler to output 16-bit instructions. This seems to only apply to ARM though according to the documentation.

.globl _start,start;
_start:start:/** _start is loaded at0x7c00and is jumped to with CS:IP 0:0x7c00*//** Beginning of the sector is compatible with the FAT/HPFS BIOS
* parameter block.*/jmp LOCAL(after_BPB)nop/*do I care about this ??? */

This reserves space for the BIOS parameter block. The BIOS parameter block is not necessary in the MBR, but Grub uses the same image for a volume boot records as well. A volume boot record may have a BIOS parameter block, so space is made for it.

This is the first instruction after the jump. The cli instruction clears the interrupts flag so that the processor does not respond to external maskable interrupts.

/** This is a workaround for buggy BIOSes which don't pass boot
* drive correctly. If GRUB is installed into a HDD, check if
* DL is masked correctly. If not, assume that the BIOS passed
* a bogus value and set DL to 0x80, since this is the only
* possible boot drive. If GRUB is installed into a floppy,
* this does nothing (only jump).
*/
. = _start + GRUB_BOOT_MACHINE_DRIVE_CHECK
boot_drive_check:
jmp 3f /* grub-setup may overwrite this jump */
testb $0x80, %dl
jz 2f
3:
/* Ignore %dl different from 0-0x0f and 0x80-0x8f. */
testb $0x70, %dl
jz 1f
2:
movb $0x80, %dl
1:
/*
* ljmp to the next instruction because some bogus BIOSes
* jump to 07C0:0000 instead of 0000:7C00.
*/
ljmp $0, $real_start

Part of the BIOS interface is indicating which disk is being booted from. Some BIOSes get this wrong so this uses some heuristics to make a fallback guess if the BIOS did something dumb. The other thing done here is to deal with BIOSes that jump to 07C0:0000 instead of 0000:7C00. This is actually the same physical address, but 0000 is a different segment than 07C0. This can be problematic, so a long jump is made to allow execution to continue with a known segment.

real_start:/* set up %dsand%ss as offset from 0*/
xorw %ax,%ax
movw %ax,%ds
movw %ax,%ss/* set up the REAL stack*/
movw $GRUB_BOOT_MACHINE_STACK_SEG,%spsti/* we're safe again */

The data segment and stack segment registers are zeroed out, and the stack pointer is set up. At this point, interrupts can be re-enabled.

/** BIOS Geometry translation error (past the end of the disk geometry!).*/
LOCAL(geometry_error):
ERR(geometry_error_string)
</syntaxhighlight lang="asm">
This is where Grub prints the error message when there is an issue with the disk geometry.
<syntaxhighlight lang="asm">
/** Read error on the disk.*/
LOCAL(read_error):
movw $read_error_string,%si
LOCAL(error_message):call LOCAL(message)
LOCAL(general_error):
MSG(general_error_string)/* go here when you need to stop the machine hard after an error condition *//* tell the BIOS a boot failure, which may result in no effect */int$0x18
LOCAL(stop):jmp LOCAL(stop)