lib/relocator
This commit is contained in:
parent
f84114f583
commit
e7573c5ce2
9 changed files with 509 additions and 120 deletions
|
@ -14,3 +14,9 @@ pkglib_MODULES += vga_text.mod
|
|||
vga_text_mod_SOURCES = term/i386/pc/vga_text.c term/i386/vga_common.c
|
||||
vga_text_mod_CFLAGS = $(COMMON_CFLAGS)
|
||||
vga_text_mod_LDFLAGS = $(COMMON_LDFLAGS)
|
||||
|
||||
pkglib_MODULES += relocator.mod
|
||||
relocator_mod_SOURCES = lib/i386/relocator.c lib/i386/relocator_asm.S lib/i386/relocator_backward.S
|
||||
relocator_mod_CFLAGS = $(COMMON_CFLAGS)
|
||||
relocator_mod_ASFLAGS = $(COMMON_ASFLAGS)
|
||||
relocator_mod_LDFLAGS = $(COMMON_LDFLAGS)
|
||||
|
|
|
@ -27,16 +27,11 @@ void grub_multiboot2_real_boot (grub_addr_t entry,
|
|||
struct grub_multiboot_info *mbi)
|
||||
__attribute__ ((noreturn));
|
||||
|
||||
extern grub_addr_t grub_multiboot_payload_orig;
|
||||
extern grub_uint32_t grub_multiboot_payload_eip;
|
||||
extern char *grub_multiboot_payload_orig;
|
||||
extern grub_addr_t grub_multiboot_payload_dest;
|
||||
extern grub_size_t grub_multiboot_payload_size;
|
||||
extern grub_uint32_t grub_multiboot_payload_entry_offset;
|
||||
|
||||
extern grub_uint8_t grub_multiboot_forward_relocator;
|
||||
extern grub_uint8_t grub_multiboot_forward_relocator_end;
|
||||
extern grub_uint8_t grub_multiboot_backward_relocator;
|
||||
extern grub_uint8_t grub_multiboot_backward_relocator_end;
|
||||
|
||||
#define RELOCATOR_SIZEOF(x) (&grub_multiboot_##x##_relocator_end - &grub_multiboot_##x##_relocator)
|
||||
#define GRUB_MULTIBOOT_STACK_SIZE 4096
|
||||
|
||||
#endif /* ! GRUB_MULTIBOOT_CPU_HEADER */
|
||||
|
|
40
include/grub/i386/relocator.h
Normal file
40
include/grub/i386/relocator.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 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/>.
|
||||
*/
|
||||
|
||||
#ifndef GRUB_RELOCATOR_CPU_HEADER
|
||||
#define GRUB_RELOCATOR_CPU_HEADER 1
|
||||
|
||||
struct grub_relocator32_state
|
||||
{
|
||||
grub_uint32_t esp;
|
||||
grub_uint32_t eax;
|
||||
grub_uint32_t ebx;
|
||||
grub_uint32_t ecx;
|
||||
grub_uint32_t edx;
|
||||
grub_uint32_t eip;
|
||||
};
|
||||
|
||||
void *
|
||||
grub_relocator32_alloc (grub_size_t size);
|
||||
grub_err_t
|
||||
grub_relocator32_boot (void *relocator, grub_uint32_t dest,
|
||||
struct grub_relocator32_state state);
|
||||
void
|
||||
grub_relocator32_free (void *relocator);
|
||||
|
||||
#endif /* ! GRUB_RELOCATOR_CPU_HEADER */
|
148
lib/i386/relocator.c
Normal file
148
lib/i386/relocator.c
Normal file
|
@ -0,0 +1,148 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 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/>.
|
||||
*/
|
||||
|
||||
#include <grub/mm.h>
|
||||
#include <grub/misc.h>
|
||||
|
||||
#include <grub/types.h>
|
||||
#include <grub/types.h>
|
||||
#include <grub/err.h>
|
||||
|
||||
#include <grub/i386/relocator.h>
|
||||
|
||||
extern grub_uint8_t grub_relocator32_forward_start;
|
||||
extern grub_uint8_t grub_relocator32_forward_end;
|
||||
extern grub_uint8_t grub_relocator32_backward_start;
|
||||
extern grub_uint8_t grub_relocator32_backward_end;
|
||||
|
||||
extern grub_uint32_t grub_relocator32_backward_dest;
|
||||
extern grub_uint32_t grub_relocator32_backward_size;
|
||||
#ifdef __x86_64__
|
||||
extern grub_uint64_t grub_relocator32_backward_src;
|
||||
#else
|
||||
extern grub_uint32_t grub_relocator32_backward_src;
|
||||
#endif
|
||||
|
||||
extern grub_uint32_t grub_relocator32_forward_dest;
|
||||
extern grub_uint32_t grub_relocator32_forward_size;
|
||||
#ifdef __x86_64__
|
||||
extern grub_uint64_t grub_relocator32_forward_src;
|
||||
#else
|
||||
extern grub_uint32_t grub_relocator32_forward_src;
|
||||
#endif
|
||||
|
||||
extern grub_uint32_t grub_relocator32_forward_eax;
|
||||
extern grub_uint32_t grub_relocator32_forward_ebx;
|
||||
extern grub_uint32_t grub_relocator32_forward_ecx;
|
||||
extern grub_uint32_t grub_relocator32_forward_edx;
|
||||
extern grub_uint32_t grub_relocator32_forward_eip;
|
||||
extern grub_uint32_t grub_relocator32_forward_esp;
|
||||
|
||||
extern grub_uint32_t grub_relocator32_backward_eax;
|
||||
extern grub_uint32_t grub_relocator32_backward_ebx;
|
||||
extern grub_uint32_t grub_relocator32_backward_ecx;
|
||||
extern grub_uint32_t grub_relocator32_backward_edx;
|
||||
extern grub_uint32_t grub_relocator32_backward_eip;
|
||||
extern grub_uint32_t grub_relocator32_backward_esp;
|
||||
|
||||
#define RELOCATOR_SIZEOF(x) (&grub_relocator32_##x##_end - &grub_relocator32_##x##_start)
|
||||
#define RELOCATOR_ALIGN 16
|
||||
|
||||
void *
|
||||
grub_relocator32_alloc (grub_size_t size)
|
||||
{
|
||||
char *playground;
|
||||
|
||||
playground = grub_malloc ((RELOCATOR_SIZEOF(forward) + RELOCATOR_ALIGN)
|
||||
+ size
|
||||
+ (RELOCATOR_SIZEOF(backward) + RELOCATOR_ALIGN));
|
||||
if (! playground)
|
||||
return 0;
|
||||
|
||||
*(grub_size_t *) playground = size;
|
||||
|
||||
return playground + RELOCATOR_SIZEOF(forward);
|
||||
}
|
||||
|
||||
void
|
||||
grub_relocator32_free (void *relocator)
|
||||
{
|
||||
if (relocator)
|
||||
grub_free ((char *) relocator - RELOCATOR_SIZEOF(forward));
|
||||
}
|
||||
|
||||
|
||||
grub_err_t
|
||||
grub_relocator32_boot (void *relocator, grub_uint32_t dest,
|
||||
struct grub_relocator32_state state)
|
||||
{
|
||||
grub_size_t size;
|
||||
char *playground;
|
||||
void (*entry) ();
|
||||
|
||||
playground = (char *) relocator - RELOCATOR_SIZEOF(forward);
|
||||
size = *(grub_size_t *) playground;
|
||||
|
||||
if (UINT_TO_PTR (dest) >= relocator)
|
||||
{
|
||||
int overhead;
|
||||
|
||||
overhead = ALIGN_UP (dest - RELOCATOR_SIZEOF(backward) - RELOCATOR_ALIGN, RELOCATOR_ALIGN);
|
||||
grub_relocator32_backward_dest = dest - overhead;
|
||||
grub_relocator32_backward_src = PTR_TO_UINT64 (relocator - overhead);
|
||||
grub_relocator32_backward_size = size + overhead;
|
||||
|
||||
grub_relocator32_backward_eax = state.eax;
|
||||
grub_relocator32_backward_ebx = state.ebx;
|
||||
grub_relocator32_backward_ecx = state.ecx;
|
||||
grub_relocator32_backward_edx = state.edx;
|
||||
grub_relocator32_backward_eip = state.eip;
|
||||
grub_relocator32_backward_esp = state.esp;
|
||||
|
||||
grub_memmove (relocator - overhead,
|
||||
&grub_relocator32_backward_start,
|
||||
RELOCATOR_SIZEOF(backward));
|
||||
entry = (void (*) ()) (relocator - overhead);
|
||||
}
|
||||
else
|
||||
{
|
||||
int overhead;
|
||||
|
||||
overhead = ALIGN_UP (dest + size, RELOCATOR_ALIGN)
|
||||
+ RELOCATOR_SIZEOF(forward) - (dest + size);
|
||||
|
||||
grub_relocator32_forward_dest = dest;
|
||||
grub_relocator32_forward_src = PTR_TO_UINT64 (relocator);
|
||||
grub_relocator32_forward_size = size + overhead;
|
||||
|
||||
grub_relocator32_forward_eax = state.eax;
|
||||
grub_relocator32_forward_ebx = state.ebx;
|
||||
grub_relocator32_forward_ecx = state.ecx;
|
||||
grub_relocator32_forward_edx = state.edx;
|
||||
grub_relocator32_forward_eip = state.eip;
|
||||
grub_relocator32_forward_esp = state.esp;
|
||||
|
||||
grub_memmove (relocator + size + overhead - RELOCATOR_SIZEOF(forward),
|
||||
&grub_relocator32_forward_start, RELOCATOR_SIZEOF(forward));
|
||||
entry = (void (*) ()) (relocator + size + overhead - RELOCATOR_SIZEOF(forward));
|
||||
}
|
||||
entry ();
|
||||
|
||||
/* Not reached. */
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
269
lib/i386/relocator_asm.S
Normal file
269
lib/i386/relocator_asm.S
Normal file
|
@ -0,0 +1,269 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 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/>.
|
||||
*/
|
||||
|
||||
#include <grub/symbol.h>
|
||||
|
||||
#ifdef BACKWARD
|
||||
#define RELOCATOR_VARIABLE(x) VARIABLE(grub_relocator32_backward_ ## x)
|
||||
#else
|
||||
#define RELOCATOR_VARIABLE(x) VARIABLE(grub_relocator32_forward_ ## x)
|
||||
#endif
|
||||
.p2align 4 /* force 16-byte alignment */
|
||||
|
||||
RELOCATOR_VARIABLE(start)
|
||||
#ifdef BACKWARD
|
||||
base:
|
||||
#endif
|
||||
cli
|
||||
|
||||
#ifndef __x86_64__
|
||||
/* mov imm32, %eax */
|
||||
.byte 0xb8
|
||||
RELOCATOR_VARIABLE(dest)
|
||||
.long 0
|
||||
mov %eax, %edi
|
||||
|
||||
/* mov imm32, %eax */
|
||||
.byte 0xb8
|
||||
RELOCATOR_VARIABLE(src)
|
||||
.long 0
|
||||
mov %eax, %esi
|
||||
|
||||
/* mov imm32, %ecx */
|
||||
.byte 0xb9
|
||||
RELOCATOR_VARIABLE(size)
|
||||
.long 0
|
||||
mov %edi, %eax
|
||||
#ifndef BACKWARD
|
||||
add %ecx, %eax
|
||||
#endif
|
||||
add $0x3, %ecx
|
||||
shr $2, %ecx
|
||||
|
||||
#ifdef BACKWARD
|
||||
/* Backward movsl is implicitly off-by-four. compensate that. */
|
||||
subl $4, %esi
|
||||
subl $4, %edi
|
||||
|
||||
/* Backward copy. */
|
||||
std
|
||||
addl %ecx, %esi
|
||||
addl %ecx, %edi
|
||||
|
||||
rep
|
||||
movsl
|
||||
|
||||
#else
|
||||
/* Forward copy. */
|
||||
cld
|
||||
rep
|
||||
movsl
|
||||
#endif
|
||||
/* %eax contains now our new base. */
|
||||
mov %eax, %esi
|
||||
add $(cont0 - base), %eax
|
||||
jmp *%eax
|
||||
cont0:
|
||||
#else
|
||||
xorq %rax, %rax
|
||||
|
||||
/* mov imm32, %eax */
|
||||
.byte 0xb8
|
||||
RELOCATOR_VARIABLE(dest)
|
||||
.long 0
|
||||
mov %eax, %edi
|
||||
|
||||
/* mov imm64, %rax */
|
||||
.byte 0xb8
|
||||
RELOCATOR_VARIABLE(src)
|
||||
.long 0, 0
|
||||
mov %rax, %rsi
|
||||
|
||||
xorl %rcx, %rcx
|
||||
/* mov imm32, %ecx */
|
||||
.byte 0xb9
|
||||
RELOCATOR_VARIABLE(size)
|
||||
.long 0
|
||||
|
||||
mov %rdi, %rax
|
||||
#ifndef BACKWARD
|
||||
add %rcx, %rax
|
||||
#endif
|
||||
add $0x3, %rcx
|
||||
shr $2, %rcx
|
||||
|
||||
#ifdef BACKWARD
|
||||
/* Backward movsl is implicitly off-by-four. compensate that. */
|
||||
subl $4, %rsi
|
||||
subl $4, %rdi
|
||||
|
||||
/* Backward copy. */
|
||||
std
|
||||
addl %rcx, %rsi
|
||||
addl %rcx, %rdi
|
||||
|
||||
rep
|
||||
movsl
|
||||
|
||||
#else
|
||||
/* Forward copy. */
|
||||
cld
|
||||
rep
|
||||
movsl
|
||||
#endif
|
||||
|
||||
/* %rax contains now our new 'base'. */
|
||||
mov %rax, %rsi
|
||||
#ifdef APPLE_CC
|
||||
add $(cont0 - base), %eax
|
||||
#else
|
||||
add $(cont0 - base), %rax
|
||||
#endif
|
||||
jmp *%rax
|
||||
cont0:
|
||||
#ifdef APPLE_CC
|
||||
lea (cont1 - base) (%esi, 1), %eax
|
||||
mov %eax, (jump_vector - base) (%esi, 1)
|
||||
|
||||
lea (gdt - base) (%esi, 1), %eax
|
||||
mov %eax, (gdt_addr - base) (%esi, 1)
|
||||
|
||||
/* Switch to compatibility mode. */
|
||||
|
||||
lgdt (gdtdesc - base) (%esi, 1)
|
||||
|
||||
/* Update %cs. Thanks to David Miller for pointing this mistake out. */
|
||||
ljmp *(jump_vector - base) (%esi,1)
|
||||
#else
|
||||
lea (cont1 - base) (%rsi, 1), %rax
|
||||
mov %eax, (jump_vector - base) (%rsi, 1)
|
||||
|
||||
lea (gdt - base) (%rsi, 1), %rax
|
||||
mov %rax, (gdt_addr - base) (%rsi, 1)
|
||||
|
||||
/* Switch to compatibility mode. */
|
||||
|
||||
lgdt (gdtdesc - base) (%rsi, 1)
|
||||
|
||||
/* Update %cs. Thanks to David Miller for pointing this mistake out. */
|
||||
ljmp *(jump_vector - base) (%rsi, 1)
|
||||
#endif
|
||||
|
||||
cont1:
|
||||
.code32
|
||||
|
||||
/* Update other registers. */
|
||||
mov $0x18, %eax
|
||||
mov %eax, %ds
|
||||
mov %eax, %es
|
||||
mov %eax, %fs
|
||||
mov %eax, %gs
|
||||
mov %eax, %ss
|
||||
|
||||
/* Disable paging. */
|
||||
mov %cr0, %eax
|
||||
and $0x7fffffff, %eax
|
||||
mov %eax, %cr0
|
||||
|
||||
/* Disable amd64. */
|
||||
mov $0xc0000080, %ecx
|
||||
rdmsr
|
||||
and $0xfffffeff, %eax
|
||||
wrmsr
|
||||
|
||||
/* Turn off PAE. */
|
||||
movl %cr4, %eax
|
||||
and $0xffffffcf, %eax
|
||||
mov %eax, %cr4
|
||||
|
||||
jmp cont2
|
||||
cont2:
|
||||
#endif
|
||||
.code32
|
||||
|
||||
/* mov imm32, %eax */
|
||||
.byte 0xb8
|
||||
RELOCATOR_VARIABLE (esp)
|
||||
.long 0
|
||||
|
||||
movl %eax, %esp
|
||||
|
||||
/* mov imm32, %eax */
|
||||
.byte 0xb8
|
||||
RELOCATOR_VARIABLE (eax)
|
||||
.long 0
|
||||
|
||||
/* mov imm32, %ebx */
|
||||
.byte 0xbb
|
||||
RELOCATOR_VARIABLE (ebx)
|
||||
.long 0
|
||||
|
||||
/* mov imm32, %ecx */
|
||||
.byte 0xb9
|
||||
RELOCATOR_VARIABLE (ecx)
|
||||
.long 0
|
||||
|
||||
/* mov imm32, %edx */
|
||||
.byte 0xba
|
||||
RELOCATOR_VARIABLE (edx)
|
||||
.long 0
|
||||
|
||||
/* Cleared direction flag is of no problem with any current
|
||||
payload and makes this implementation easier. */
|
||||
cld
|
||||
|
||||
.byte 0xea
|
||||
RELOCATOR_VARIABLE (eip)
|
||||
.long 0
|
||||
.word 0x08
|
||||
|
||||
#ifdef __x86_64__
|
||||
/* GDT. Copied from loader/i386/linux.c. */
|
||||
.p2align 4
|
||||
gdt:
|
||||
/* NULL. */
|
||||
.byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
|
||||
/* Reserved. */
|
||||
.byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
|
||||
/* Code segment. */
|
||||
.byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x9A, 0xCF, 0x00
|
||||
|
||||
/* Data segment. */
|
||||
.byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x92, 0xCF, 0x00
|
||||
|
||||
gdtdesc:
|
||||
.word 31
|
||||
gdt_addr:
|
||||
/* Filled by the code. */
|
||||
.quad 0
|
||||
|
||||
.p2align 4
|
||||
jump_vector:
|
||||
/* Jump location. Is filled by the code */
|
||||
.long 0
|
||||
.long 0x10
|
||||
#endif
|
||||
|
||||
#ifndef BACKWARD
|
||||
base:
|
||||
#endif
|
||||
|
||||
|
||||
RELOCATOR_VARIABLE(end)
|
2
lib/i386/relocator_backward.S
Normal file
2
lib/i386/relocator_backward.S
Normal file
|
@ -0,0 +1,2 @@
|
|||
#define BACKWARD
|
||||
#include "relocator_asm.S"
|
|
@ -48,18 +48,35 @@
|
|||
#include <grub/device.h>
|
||||
#include <grub/partition.h>
|
||||
#endif
|
||||
#include <grub/i386/relocator.h>
|
||||
|
||||
extern grub_dl_t my_mod;
|
||||
static struct grub_multiboot_info *mbi, *mbi_dest;
|
||||
static grub_addr_t entry;
|
||||
|
||||
static char *playground;
|
||||
static grub_size_t code_size;
|
||||
|
||||
char *grub_multiboot_payload_orig;
|
||||
grub_addr_t grub_multiboot_payload_dest;
|
||||
grub_size_t grub_multiboot_payload_size;
|
||||
grub_uint32_t grub_multiboot_payload_eip;
|
||||
grub_uint32_t grub_multiboot_payload_esp;
|
||||
|
||||
static grub_err_t
|
||||
grub_multiboot_boot (void)
|
||||
{
|
||||
grub_multiboot_real_boot (entry, mbi_dest);
|
||||
struct grub_relocator32_state state =
|
||||
{
|
||||
.eax = MULTIBOOT_MAGIC2,
|
||||
.ebx = PTR_TO_UINT32 (mbi_dest),
|
||||
.ecx = 0,
|
||||
.edx = 0,
|
||||
.eip = grub_multiboot_payload_eip,
|
||||
.esp = grub_multiboot_payload_esp
|
||||
};
|
||||
|
||||
grub_relocator32_boot (grub_multiboot_payload_orig,
|
||||
grub_multiboot_payload_dest,
|
||||
state);
|
||||
|
||||
/* Not reached. */
|
||||
return GRUB_ERR_NONE;
|
||||
|
@ -248,11 +265,8 @@ grub_multiboot (int argc, char *argv[])
|
|||
goto fail;
|
||||
}
|
||||
|
||||
if (playground)
|
||||
{
|
||||
grub_free (playground);
|
||||
playground = NULL;
|
||||
}
|
||||
grub_relocator32_free (grub_multiboot_payload_orig);
|
||||
grub_multiboot_payload_orig = NULL;
|
||||
|
||||
mmap_length = grub_get_multiboot_mmap_len ();
|
||||
|
||||
|
@ -267,11 +281,13 @@ grub_multiboot (int argc, char *argv[])
|
|||
((void *) ((x) + code_size + cmdline_length))
|
||||
#define mbi_addr(x) ((void *) ((x) + code_size + cmdline_length + boot_loader_name_length))
|
||||
#define mmap_addr(x) ((void *) ((x) + code_size + cmdline_length + boot_loader_name_length + sizeof (struct grub_multiboot_info)))
|
||||
#define stack_addr(x) ((void *) ((x) + code_size + cmdline_length + boot_loader_name_length + sizeof (struct grub_multiboot_info) + mmap_length + GRUB_MULTIBOOT_STACK_SIZE))
|
||||
|
||||
grub_multiboot_payload_size = cmdline_length
|
||||
/* boot_loader_name_length might need to grow for mbi,etc to be aligned (see below) */
|
||||
+ boot_loader_name_length + 3
|
||||
+ sizeof (struct grub_multiboot_info) + mmap_length;
|
||||
+ sizeof (struct grub_multiboot_info) + mmap_length
|
||||
+ GRUB_MULTIBOOT_STACK_SIZE;
|
||||
|
||||
if (header->flags & MULTIBOOT_AOUT_KLUDGE)
|
||||
{
|
||||
|
@ -287,11 +303,12 @@ grub_multiboot (int argc, char *argv[])
|
|||
grub_multiboot_payload_dest = header->load_addr;
|
||||
|
||||
grub_multiboot_payload_size += code_size;
|
||||
playground = grub_malloc (RELOCATOR_SIZEOF(forward) + grub_multiboot_payload_size + RELOCATOR_SIZEOF(backward));
|
||||
if (! playground)
|
||||
goto fail;
|
||||
|
||||
grub_multiboot_payload_orig = (long) playground + RELOCATOR_SIZEOF(forward);
|
||||
grub_multiboot_payload_orig
|
||||
= grub_relocator32_alloc (grub_multiboot_payload_size);
|
||||
|
||||
if (! grub_multiboot_payload_orig)
|
||||
goto fail;
|
||||
|
||||
if ((grub_file_seek (file, offset)) == (grub_off_t) - 1)
|
||||
goto fail;
|
||||
|
@ -304,7 +321,7 @@ grub_multiboot (int argc, char *argv[])
|
|||
grub_memset ((void *) (grub_multiboot_payload_orig + load_size), 0,
|
||||
header->bss_end_addr - header->load_addr - load_size);
|
||||
|
||||
grub_multiboot_payload_entry_offset = header->entry_addr - header->load_addr;
|
||||
grub_multiboot_payload_eip = header->entry_addr;
|
||||
|
||||
}
|
||||
else if (grub_multiboot_load_elf (file, buffer) != GRUB_ERR_NONE)
|
||||
|
@ -325,23 +342,6 @@ grub_multiboot (int argc, char *argv[])
|
|||
mbi->mmap_addr = (grub_uint32_t) mmap_addr (grub_multiboot_payload_dest);
|
||||
mbi->flags |= MULTIBOOT_INFO_MEM_MAP;
|
||||
|
||||
if (grub_multiboot_payload_dest >= grub_multiboot_payload_orig)
|
||||
{
|
||||
grub_memmove (playground, &grub_multiboot_forward_relocator, RELOCATOR_SIZEOF(forward));
|
||||
entry = (grub_addr_t) playground;
|
||||
}
|
||||
else
|
||||
{
|
||||
grub_memmove ((char *) (grub_multiboot_payload_orig + grub_multiboot_payload_size),
|
||||
&grub_multiboot_backward_relocator, RELOCATOR_SIZEOF(backward));
|
||||
entry = (grub_addr_t) grub_multiboot_payload_orig + grub_multiboot_payload_size;
|
||||
}
|
||||
|
||||
grub_dprintf ("multiboot_loader", "dest=%p, size=0x%x, entry_offset=0x%x\n",
|
||||
(void *) grub_multiboot_payload_dest,
|
||||
grub_multiboot_payload_size,
|
||||
grub_multiboot_payload_entry_offset);
|
||||
|
||||
/* Convert from bytes to kilobytes. */
|
||||
mbi->mem_lower = grub_mmap_get_lower () / 1024;
|
||||
mbi->mem_upper = grub_mmap_get_upper () / 1024;
|
||||
|
@ -371,6 +371,8 @@ grub_multiboot (int argc, char *argv[])
|
|||
if (grub_multiboot_get_bootdev (&mbi->boot_device))
|
||||
mbi->flags |= MULTIBOOT_INFO_BOOTDEV;
|
||||
|
||||
grub_multiboot_payload_esp = PTR_TO_UINT32 (stack_addr (grub_multiboot_payload_dest));
|
||||
|
||||
grub_loader_set (grub_multiboot_boot, grub_multiboot_unload, 1);
|
||||
|
||||
fail:
|
||||
|
|
|
@ -32,6 +32,8 @@
|
|||
#error "I'm confused"
|
||||
#endif
|
||||
|
||||
#include <grub/i386/relocator.h>
|
||||
|
||||
#define CONCAT(a,b) CONCAT_(a, b)
|
||||
#define CONCAT_(a,b) a ## b
|
||||
|
||||
|
@ -99,11 +101,12 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, void *buffer)
|
|||
grub_multiboot_payload_dest = phdr(lowest_segment)->p_paddr;
|
||||
|
||||
grub_multiboot_payload_size += code_size;
|
||||
playground = grub_malloc (RELOCATOR_SIZEOF(forward) + grub_multiboot_payload_size + RELOCATOR_SIZEOF(backward));
|
||||
if (! playground)
|
||||
return grub_errno;
|
||||
|
||||
grub_multiboot_payload_orig = (long) playground + RELOCATOR_SIZEOF(forward);
|
||||
grub_multiboot_payload_orig
|
||||
= grub_relocator32_alloc (grub_multiboot_payload_size);
|
||||
|
||||
if (!grub_multiboot_payload_orig)
|
||||
return grub_errno;
|
||||
|
||||
/* Load every loadable segment in memory. */
|
||||
for (i = 0; i < ehdr->e_phnum; i++)
|
||||
|
@ -135,8 +138,7 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, void *buffer)
|
|||
if (phdr(i)->p_vaddr <= ehdr->e_entry
|
||||
&& phdr(i)->p_vaddr + phdr(i)->p_memsz > ehdr->e_entry)
|
||||
{
|
||||
grub_multiboot_payload_entry_offset = (ehdr->e_entry - phdr(i)->p_vaddr)
|
||||
+ (phdr(i)->p_paddr - phdr(lowest_segment)->p_paddr);
|
||||
grub_multiboot_payload_eip = ehdr->e_entry;
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
@ -22,81 +22,6 @@
|
|||
|
||||
.p2align 2 /* force 4-byte alignment */
|
||||
|
||||
/*
|
||||
* This starts the multiboot kernel.
|
||||
*/
|
||||
|
||||
VARIABLE(grub_multiboot_payload_size)
|
||||
.long 0
|
||||
VARIABLE(grub_multiboot_payload_orig)
|
||||
.long 0
|
||||
VARIABLE(grub_multiboot_payload_dest)
|
||||
.long 0
|
||||
VARIABLE(grub_multiboot_payload_entry_offset)
|
||||
.long 0
|
||||
|
||||
/*
|
||||
* The relocators below understand the following parameters:
|
||||
* ecx: Size of the block to be copied.
|
||||
* esi: Where to copy from (always lowest address, even if we're relocating
|
||||
* backwards).
|
||||
* edi: Where to copy to (likewise).
|
||||
* edx: Offset of the entry point (relative to the beginning of the block).
|
||||
*/
|
||||
|
||||
VARIABLE(grub_multiboot_forward_relocator)
|
||||
/* Add entry offset. */
|
||||
addl %edi, %edx
|
||||
|
||||
/* Forward copy. */
|
||||
cld
|
||||
rep
|
||||
movsb
|
||||
|
||||
jmp *%edx
|
||||
VARIABLE(grub_multiboot_forward_relocator_end)
|
||||
|
||||
VARIABLE(grub_multiboot_backward_relocator)
|
||||
/* Add entry offset (before %edi is mangled). */
|
||||
addl %edi, %edx
|
||||
|
||||
/* Backward movsb is implicitly off-by-one. compensate that. */
|
||||
decl %esi
|
||||
decl %edi
|
||||
|
||||
/* Backward copy. */
|
||||
std
|
||||
addl %ecx, %esi
|
||||
addl %ecx, %edi
|
||||
rep
|
||||
movsb
|
||||
|
||||
cld
|
||||
jmp *%edx
|
||||
VARIABLE(grub_multiboot_backward_relocator_end)
|
||||
|
||||
FUNCTION(grub_multiboot_real_boot)
|
||||
/* Push the entry address on the stack. */
|
||||
pushl %eax
|
||||
/* Move the address of the multiboot information structure to ebx. */
|
||||
movl %edx,%ebx
|
||||
|
||||
/* Interrupts should be disabled. */
|
||||
cli
|
||||
|
||||
/* Where do we copy what from. */
|
||||
movl EXT_C(grub_multiboot_payload_size), %ecx
|
||||
movl EXT_C(grub_multiboot_payload_orig), %esi
|
||||
movl EXT_C(grub_multiboot_payload_dest), %edi
|
||||
movl EXT_C(grub_multiboot_payload_entry_offset), %edx
|
||||
|
||||
/* Move the magic value into eax. */
|
||||
movl $MULTIBOOT_MAGIC2, %eax
|
||||
|
||||
/* Jump to the relocator. */
|
||||
popl %ebp
|
||||
jmp *%ebp
|
||||
|
||||
/*
|
||||
* This starts the multiboot 2 kernel.
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue