Where does code of GRUB stage 1.5 reside on disk and what is the address it is loaded?
Asked Answered
S

1

6

I have grub v1.98 installed and after disassembling the MBR I find the following code snippet that I don't understand:

xor ax,ax
mov [si+0x4],ax
inc ax
mov [si-0x1],al
mov [si+0x2],ax
mov word [si],0x10
mov ebx,[0x7c5c]
mov [si+0x8],ebx
mov ebx,[0x7c60]
mov [si+0xc],ebx
mov word [si+0x6],0x7000
mov ah,0x42
int 0x13

It seems this piece of code tries to set up disk address of stage 1.5 code, then load and run it. However, how could I figure out which physical block it tries to read? What's more, what is the destination of the stage 1.5 code? 0x7000?

I refer to MBR for Windows 7, where subsequent boot up code is loaded 0x7c00. Given MBR is first loaded at address 0x7c00, it contains a piece of code copying MBR from 0x7c00 to 0x0600 and then branch to 0x0600 in case the original code corrupted. Will loading stage 1.5 code to address 0x7000 conflict the original code? What's more, I also find:

jmp short 0x65
nop
sar byte [si+0x7c00],1
mov es,ax
mov ds,ax
mov si,0x7c00
mov di,0x600
mov cx,0x200
cld
rep movsb
push ax
push word 0x61c
retf

at the beginning of the MBR. It seems the code tries to do the same thing as in MBR of windows 7 to copy the original MBR from 0x7c00 to 0x0600, except for the first jmp instruction. Will these codes in fact executed? If yes, when will control jumps here.(I believe the answer is YES, but am confused by the leading jmp).

Shlomo answered 6/7, 2012 at 7:39 Comment(0)
C
5

GRUB 1.98 is GRUB version 2. In version 2, there is no stage 1.5 anymore. The stage 1.5 had a fixed place between MBR and first partition. It was (most often) unused space on the hard drive. GPT partitioning and other (unusual) layouts do not provide this space.

In GRUB v2 stage 1 loads core.img, which can be stored at any LBA48 location, typically between MBR and first partition, but it can also be stored within a partition. In the non-EFI case of GPT, a custom partition should be created for it. The location is hardwired into stage 1.

See also: http://www.gnu.org/software/grub/manual/grub.html#Images

Crinkle answered 29/7, 2012 at 0:54 Comment(3)
grub-install first try to put core.img in a free space between partitions. You'll see an error if there is no enough space in there. You can force grub to write core.img within partition only using the switch. Your answer is misleading.Sherfield
grub-install gives an error message if there is not enough space between the MBR and the first partition.Tubing
Sorry, but Grub 2 still recommends putting core.img in the MBR gap (unless the disk is GPT of course): "With this partition table format [MBR], there are two ways to install GRUB: it can be embedded in the area between the MBR and the first partition ... The GRUB development team generally recommends embedding GRUB before the first partition, unless you have special requirements." Grub 2.02.Yuma

© 2022 - 2024 — McMap. All rights reserved.