GRUB Stage2 Load Location

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

GRUB Stage2 Load Location

Post 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.
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: GRUB Stage2 Load Location

Post 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.
If you have seen bad English in my words, tell me what's wrong, please.
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: GRUB Stage2 Load Location

Post 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?
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: GRUB Stage2 Load Location

Post 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
If you have seen bad English in my words, tell me what's wrong, please.
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: GRUB Stage2 Load Location

Post 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.
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: GRUB Stage2 Load Location

Post 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:
If you have seen bad English in my words, tell me what's wrong, please.
Bietje
Member
Member
Posts: 100
Joined: Wed Apr 20, 2011 6:57 am

Re: GRUB Stage2 Load Location

Post 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
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: GRUB Stage2 Load Location

Post 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.
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: GRUB Stage2 Load Location

Post 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.
If you have seen bad English in my words, tell me what's wrong, please.
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: GRUB Stage2 Load Location

Post by Nessphoro »

Yes! That worked, many thanks to you sir.
Post Reply