Page 1 of 1

GRUB Stage2 Load Location

Posted: Sat Aug 13, 2011 11:53 pm
by Nessphoro
I've got a rather simple question today, simply where does stage1 loads stage2, or where does it expect to be loaded, and also, does it require PM before it is being called.

Re: GRUB Stage2 Load Location

Posted: Sun Aug 14, 2011 4:46 am
by egos

Code: Select all

struc kernelstart
{
  .boot_version db MAJOR_VERSION,MINOR_VERSION
  .boot_drive db 0xFF
  .force_lba db 0
  .kernel_address dw KERNEL_ADDR
  .kernel_sector dd KERNEL_SECT
  .kernel_segment dw KERNEL_SEG
}

Code: Select all

if STAGE1_5
kernel_address=2000h
kernel_sector=fatstage_base/512
kernel_segment=200h
else
kernel_address=8000h
kernel_sector=stage2_base/512
kernel_segment=800h
end if
...does it require PM before it is being called.
No.

Re: GRUB Stage2 Load Location

Posted: Sun Aug 14, 2011 4:00 pm
by Nessphoro
Hold on, I can't get it working,

So I int13 the fat_stage_1_5 (located on the first sector on the hard drive)

and jump to 0x200:0x2000?

EDIT: Or if you know how to install grub from within the OS?

Re: GRUB Stage2 Load Location

Posted: Sun Aug 14, 2011 11:26 pm
by egos
Nessphoro wrote:Hold on, I can't get it working,

So I int13 the fat_stage_1_5 (located on the first sector on the hard drive)

and jump to 0x200:0x2000?
No. You should jump to 0:0x2000. Why do you not use GRUB stage1? Anyway you should know that GRUB requires additional configuring after/before it was placed on the disk. Default value of field kernel_sector for stage1_5 is 1. It is mean that stage1_5 usually is located on the disk after MBR, i.e. after stage1. Also you should know that kernelstart.kernel_sector holds the number only for first sector of the "kernel" (stage1_5 or stage2). Geneneral location of the kernel is described in the structure located within its first sector.
EDIT: Or if you know how to install grub from within the OS?
GRUB Manual - Installation

Re: GRUB Stage2 Load Location

Posted: Mon Aug 15, 2011 12:43 am
by Nessphoro
Hmm I'm looking at Ubuntu's stage1 and if I copy it to the disk it'll overwrite the partition table

And I meant install GRUB from within non-UNIX os.

Re: GRUB Stage2 Load Location

Posted: Mon Aug 15, 2011 8:34 am
by egos
Nessphoro wrote:Hmm I'm looking at Ubuntu's stage1 and if I copy it to the disk it'll overwrite the partition table
It is floppy specific code. If you use stage1 as MBR boot loader you can replace its code starting from offset 446 with partition table.
And I meant install GRUB from within non-UNIX os.
Hex editor? :mrgreen:

Re: GRUB Stage2 Load Location

Posted: Mon Aug 15, 2011 10:32 am
by Bietje
Long answer:

The boot sector loads first the sector to a buffer

Code: Select all

	
#define GRUB_BOOT_MACHINE_BUFFER_SEG	0x7000


        movw	$GRUB_BOOT_MACHINE_BUFFER_SEG, %bx
	movw	%bx, %es	/* load %es segment with disk buffer */

	xorw	%bx, %bx	/* %bx = 0, put it at 0 in the segment */
	movw	$0x0201, %ax	/* function 2 */
	int	$0x13

	jc	LOCAL(read_error)
and then copies it to its final location

Code: Select all

#define GRUB_BOOT_I386_PC_KERNEL_SEG	0x800
#define GRUB_BOOT_MACHINE_KERNEL_SEG GRUB_OFFSETS_CONCAT (GRUB_BOOT_, GRUB_MACHINE, _KERNEL_SEG)
LOCAL(copy_buffer):
	/*
	 * We need to save %cx and %si because the startup code in
	 * kernel uses them without initializing them.
	 */
	pusha
	pushw	%ds

	movw	$0x100, %cx
	movw	%bx, %ds
	xorw	%si, %si
	movw	$GRUB_BOOT_MACHINE_KERNEL_ADDR, %di
	movw	%si, %es

	cld

	rep
	movsw

	popw	%ds
	popa
Short answer:

As far as I can see.. to 0x8000

Re: GRUB Stage2 Load Location

Posted: Mon Aug 15, 2011 3:19 pm
by Nessphoro
egos wrote:
Nessphoro wrote:Hmm I'm looking at Ubuntu's stage1 and if I copy it to the disk it'll overwrite the partition table
It is floppy specific code. If you use stage1 as MBR boot loader you can replace its code starting from offset 446 with partition table.
And I meant install GRUB from within non-UNIX os.
Hex editor? :mrgreen:
You're gonna laugh - but I actually did that.

Re: GRUB Stage2 Load Location

Posted: Mon Aug 15, 2011 9:00 pm
by egos
It is seriously. OK, I can help you. Follow next steps.

1. Put first 446 (440) bytes of stage1 to your MBR.

2. Patch this code:
- store byte 80h at 40h (it's variative; you can pass it) ; boot_drive field
- store word 2000h at 42h ; kernel_address field
- store dword 1 at 44h ; kernel_sector field
- store word 200h at 48h ; kernel_segment field

3. Put fat_stage1_5 (or other stage1_5) after MBR (starting from sector number 1).

4. Patch this code:
- store dword 2 at 200h+1F8h ; start field of kernelblock
- store word size_of_stage1_5_in_sectors-1 at 200h+1FCh ; len field of kernelblock
- store word 220h at 200h+1FEh ; seg field of kernelblock
- store byte partition_number_where_stage2_is_located at 200h+219h ; 0 - first primary partition, etc.
- store byte 80h at 200h+21Ah (it's variative too)
(200h is location of stage1_5 on the disk where you are storing it)

5. Ensure that path (and name) of stage2 stored at 200h+21Bh is correct.

That's all.

Re: GRUB Stage2 Load Location

Posted: Mon Aug 15, 2011 10:55 pm
by Nessphoro
Yes! That worked, many thanks to you sir.