Split relocators from mips branch
This commit is contained in:
parent
6717926e6e
commit
636813f768
19 changed files with 1356 additions and 216 deletions
102
lib/i386/relocator.c
Normal file
102
lib/i386/relocator.c
Normal file
|
@ -0,0 +1,102 @@
|
|||
/*
|
||||
* 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;
|
||||
extern grub_addr_t grub_relocator32_backward_src;
|
||||
|
||||
extern grub_uint32_t grub_relocator32_forward_dest;
|
||||
extern grub_uint32_t grub_relocator32_forward_size;
|
||||
extern grub_addr_t grub_relocator32_forward_src;
|
||||
|
||||
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
|
||||
#define PREFIX(x) grub_relocator32_ ## x
|
||||
|
||||
static void
|
||||
write_call_relocator_bw (void *ptr, void *src, grub_uint32_t dest,
|
||||
grub_size_t size, struct grub_relocator32_state state)
|
||||
{
|
||||
grub_relocator32_backward_dest = dest;
|
||||
grub_relocator32_backward_src = PTR_TO_UINT64 (src);
|
||||
grub_relocator32_backward_size = size;
|
||||
|
||||
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 (ptr,
|
||||
&grub_relocator32_backward_start,
|
||||
RELOCATOR_SIZEOF (backward));
|
||||
((void (*) (void)) ptr) ();
|
||||
}
|
||||
|
||||
static void
|
||||
write_call_relocator_fw (void *ptr, void *src, grub_uint32_t dest,
|
||||
grub_size_t size, struct grub_relocator32_state state)
|
||||
{
|
||||
|
||||
grub_relocator32_forward_dest = dest;
|
||||
grub_relocator32_forward_src = PTR_TO_UINT64 (src);
|
||||
grub_relocator32_forward_size = size;
|
||||
|
||||
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 (ptr,
|
||||
&grub_relocator32_forward_start,
|
||||
RELOCATOR_SIZEOF (forward));
|
||||
((void (*) (void)) ptr) ();
|
||||
}
|
||||
|
||||
#include "../relocator.c"
|
250
lib/i386/relocator_asm.S
Normal file
250
lib/i386/relocator_asm.S
Normal file
|
@ -0,0 +1,250 @@
|
|||
/*
|
||||
* 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>
|
||||
#include <grub/i386/memory.h>
|
||||
|
||||
#ifdef BACKWARD
|
||||
#define RELOCATOR_VARIABLE(x) VARIABLE(grub_relocator32_backward_ ## x)
|
||||
#else
|
||||
#define RELOCATOR_VARIABLE(x) VARIABLE(grub_relocator32_forward_ ## x)
|
||||
#endif
|
||||
#ifdef __x86_64__
|
||||
#define RAX %rax
|
||||
#define RCX %rcx
|
||||
#define RDX %rdx
|
||||
#define RDI %rdi
|
||||
#define RSI %rdi
|
||||
#else
|
||||
#define RAX %eax
|
||||
#define RCX %ecx
|
||||
#define RDX %edx
|
||||
#define RDI %edi
|
||||
#define RSI %esi
|
||||
#endif
|
||||
|
||||
/* The code segment of the protected mode. */
|
||||
#define CODE_SEGMENT 0x10
|
||||
|
||||
/* The data segment of the protected mode. */
|
||||
#define DATA_SEGMENT 0x18
|
||||
|
||||
.p2align 4 /* force 16-byte alignment */
|
||||
|
||||
RELOCATOR_VARIABLE(start)
|
||||
#ifdef BACKWARD
|
||||
LOCAL(base):
|
||||
#endif
|
||||
cli
|
||||
|
||||
#ifndef __x86_64__
|
||||
/* mov imm32, %eax */
|
||||
.byte 0xb8
|
||||
RELOCATOR_VARIABLE(dest)
|
||||
.long 0
|
||||
movl %eax, %edi
|
||||
|
||||
/* mov imm32, %eax */
|
||||
.byte 0xb8
|
||||
RELOCATOR_VARIABLE(src)
|
||||
.long 0
|
||||
movl %eax, %esi
|
||||
|
||||
/* mov imm32, %ecx */
|
||||
.byte 0xb9
|
||||
RELOCATOR_VARIABLE(size)
|
||||
.long 0
|
||||
#else
|
||||
xorq %rax, %rax
|
||||
|
||||
/* mov imm32, %eax */
|
||||
.byte 0xb8
|
||||
RELOCATOR_VARIABLE(dest)
|
||||
.long 0
|
||||
movq %rax, %rdi
|
||||
|
||||
/* mov imm64, %rax */
|
||||
.byte 0x48
|
||||
.byte 0xb8
|
||||
RELOCATOR_VARIABLE(src)
|
||||
.long 0, 0
|
||||
movq %rax, %rsi
|
||||
|
||||
xorq %rcx, %rcx
|
||||
/* mov imm32, %ecx */
|
||||
.byte 0xb9
|
||||
RELOCATOR_VARIABLE(size)
|
||||
.long 0
|
||||
|
||||
#endif
|
||||
|
||||
mov RDI, RAX
|
||||
|
||||
#ifdef BACKWARD
|
||||
add RCX, RSI
|
||||
add RDX, RDI
|
||||
#endif
|
||||
|
||||
#ifndef BACKWARD
|
||||
add RCX, RAX
|
||||
#endif
|
||||
add $0x3, RCX
|
||||
shr $2, RCX
|
||||
|
||||
|
||||
#ifdef BACKWARD
|
||||
/* Backward movsl is implicitly off-by-four. compensate that. */
|
||||
sub $4, RSI
|
||||
sub $4, RDI
|
||||
|
||||
/* Backward copy. */
|
||||
std
|
||||
|
||||
rep
|
||||
movsl
|
||||
|
||||
#else
|
||||
/* Forward copy. */
|
||||
cld
|
||||
rep
|
||||
movsl
|
||||
#endif
|
||||
|
||||
/* %rax contains now our new 'base'. */
|
||||
mov RAX, RSI
|
||||
add $(LOCAL(cont0) - LOCAL(base)), RAX
|
||||
jmp *RAX
|
||||
LOCAL(cont0):
|
||||
lea (LOCAL(cont1) - LOCAL(base)) (RSI, 1), RAX
|
||||
movl %eax, (LOCAL(jump_vector) - LOCAL(base)) (RSI, 1)
|
||||
|
||||
lea (LOCAL(gdt) - LOCAL(base)) (RSI, 1), RAX
|
||||
mov RAX, (LOCAL(gdt_addr) - LOCAL(base)) (RSI, 1)
|
||||
|
||||
/* Switch to compatibility mode. */
|
||||
|
||||
lgdt (LOCAL(gdtdesc) - LOCAL(base)) (RSI, 1)
|
||||
|
||||
/* Update %cs. Thanks to David Miller for pointing this mistake out. */
|
||||
ljmp *(LOCAL(jump_vector) - LOCAL(base)) (RSI, 1)
|
||||
|
||||
LOCAL(cont1):
|
||||
.code32
|
||||
|
||||
/* Update other registers. */
|
||||
movl $DATA_SEGMENT, %eax
|
||||
movl %eax, %ds
|
||||
movl %eax, %es
|
||||
movl %eax, %fs
|
||||
movl %eax, %gs
|
||||
movl %eax, %ss
|
||||
|
||||
/* Disable paging. */
|
||||
movl %cr0, %eax
|
||||
andl $(~GRUB_MEMORY_CPU_CR0_PAGING_ON), %eax
|
||||
movl %eax, %cr0
|
||||
|
||||
/* Disable amd64. */
|
||||
movl $GRUB_MEMORY_CPU_AMD64_MSR, %ecx
|
||||
rdmsr
|
||||
andl $(~GRUB_MEMORY_CPU_AMD64_MSR_ON), %eax
|
||||
wrmsr
|
||||
|
||||
/* Turn off PAE. */
|
||||
movl %cr4, %eax
|
||||
andl $GRUB_MEMORY_CPU_CR4_PAE_ON, %eax
|
||||
movl %eax, %cr4
|
||||
|
||||
jmp LOCAL(cont2)
|
||||
LOCAL(cont2):
|
||||
.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
|
||||
|
||||
/* GDT. Copied from loader/i386/linux.c. */
|
||||
.p2align 4
|
||||
LOCAL(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
|
||||
|
||||
.p2align 4
|
||||
LOCAL(gdtdesc):
|
||||
.word 0x27
|
||||
LOCAL(gdt_addr):
|
||||
#ifdef __x86_64__
|
||||
/* Filled by the code. */
|
||||
.quad 0
|
||||
#else
|
||||
/* Filled by the code. */
|
||||
.long 0
|
||||
#endif
|
||||
|
||||
.p2align 4
|
||||
LOCAL(jump_vector):
|
||||
/* Jump location. Is filled by the code */
|
||||
.long 0
|
||||
.long CODE_SEGMENT
|
||||
|
||||
#ifndef BACKWARD
|
||||
LOCAL(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"
|
109
lib/mips/relocator.c
Normal file
109
lib/mips/relocator.c
Normal file
|
@ -0,0 +1,109 @@
|
|||
/*
|
||||
* 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/cache.h>
|
||||
|
||||
#include <grub/mips/relocator.h>
|
||||
|
||||
/* Remark: doesn't work with source outside of 4G.
|
||||
Use relocator64 in this case.
|
||||
*/
|
||||
|
||||
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;
|
||||
|
||||
#define REGW_SIZEOF (2 * sizeof (grub_uint32_t))
|
||||
#define JUMP_SIZEOF (sizeof (grub_uint32_t))
|
||||
|
||||
#define RELOCATOR_SRC_SIZEOF(x) (&grub_relocator32_##x##_end \
|
||||
- &grub_relocator32_##x##_start)
|
||||
#define RELOCATOR_SIZEOF(x) (RELOCATOR_SRC_SIZEOF(x) \
|
||||
+ REGW_SIZEOF * (31 + 3) + JUMP_SIZEOF)
|
||||
#define RELOCATOR_ALIGN 16
|
||||
|
||||
#define PREFIX(x) grub_relocator32_ ## x
|
||||
|
||||
static void
|
||||
write_reg (int regn, grub_uint32_t val, void **target)
|
||||
{
|
||||
/* lui $r, (val+0x8000). */
|
||||
*(grub_uint32_t *) *target = ((0x3c00 | regn) << 16) | ((val + 0x8000) >> 16);
|
||||
*target = ((grub_uint32_t *) *target) + 1;
|
||||
/* addiu $r, $r, val. */
|
||||
*(grub_uint32_t *) *target = (((0x2400 | regn << 5 | regn) << 16)
|
||||
| (val & 0xffff));
|
||||
*target = ((grub_uint32_t *) *target) + 1;
|
||||
}
|
||||
|
||||
static void
|
||||
write_jump (int regn, void **target)
|
||||
{
|
||||
/* j $r. */
|
||||
*(grub_uint32_t *) *target = (regn<<21) | 0x8;
|
||||
*target = ((grub_uint32_t *) *target) + 1;
|
||||
}
|
||||
|
||||
static void
|
||||
write_call_relocator_bw (void *ptr0, void *src, grub_uint32_t dest,
|
||||
grub_size_t size, struct grub_relocator32_state state)
|
||||
{
|
||||
void *ptr = ptr0;
|
||||
int i;
|
||||
write_reg (8, (grub_uint32_t) src, &ptr);
|
||||
write_reg (9, dest, &ptr);
|
||||
write_reg (10, size, &ptr);
|
||||
grub_memcpy (ptr, &grub_relocator32_backward_start,
|
||||
RELOCATOR_SRC_SIZEOF (backward));
|
||||
ptr = (grub_uint8_t *) ptr + RELOCATOR_SRC_SIZEOF (backward);
|
||||
for (i = 1; i < 32; i++)
|
||||
write_reg (i, state.gpr[i], &ptr);
|
||||
write_jump (state.jumpreg, &ptr);
|
||||
grub_arch_sync_caches (ptr0, (grub_uint8_t *) ptr - (grub_uint8_t *) ptr0);
|
||||
grub_dprintf ("relocator", "Backward relocator: about to jump to %p\n", ptr0);
|
||||
((void (*) (void)) ptr0) ();
|
||||
}
|
||||
|
||||
static void
|
||||
write_call_relocator_fw (void *ptr0, void *src, grub_uint32_t dest,
|
||||
grub_size_t size, struct grub_relocator32_state state)
|
||||
{
|
||||
void *ptr = ptr0;
|
||||
int i;
|
||||
write_reg (8, (grub_uint32_t) src, &ptr);
|
||||
write_reg (9, dest, &ptr);
|
||||
write_reg (10, size, &ptr);
|
||||
grub_memcpy (ptr, &grub_relocator32_forward_start,
|
||||
RELOCATOR_SRC_SIZEOF (forward));
|
||||
ptr = (grub_uint8_t *) ptr + RELOCATOR_SRC_SIZEOF (forward);
|
||||
for (i = 1; i < 32; i++)
|
||||
write_reg (i, state.gpr[i], &ptr);
|
||||
write_jump (state.jumpreg, &ptr);
|
||||
grub_arch_sync_caches (ptr0, (grub_uint8_t *) ptr - (grub_uint8_t *) ptr0);
|
||||
grub_dprintf ("relocator", "Forward relocator: about to jump to %p\n", ptr0);
|
||||
((void (*) (void)) ptr0) ();
|
||||
}
|
||||
|
||||
#include "../relocator.c"
|
71
lib/mips/relocator_asm.S
Normal file
71
lib/mips/relocator_asm.S
Normal file
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* 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>
|
||||
|
||||
.p2align 4 /* force 16-byte alignment */
|
||||
|
||||
VARIABLE (grub_relocator32_forward_start)
|
||||
move $12, $9
|
||||
move $13, $10
|
||||
|
||||
copycont1:
|
||||
lb $11,0($8)
|
||||
sb $11,0($9)
|
||||
addiu $8, $8, 0x1
|
||||
addiu $9, $9, 0x1
|
||||
addiu $10, $10, 0xffff
|
||||
subu $11,$10,$0
|
||||
bne $11, $0, copycont1
|
||||
|
||||
cachecont1:
|
||||
cache 1,0($12)
|
||||
cache 0,0($12)
|
||||
addiu $12, $12, 0x1
|
||||
addiu $13, $13, 0xffff
|
||||
subu $11,$13,$0
|
||||
bne $11, $0, cachecont1
|
||||
VARIABLE (grub_relocator32_forward_end)
|
||||
|
||||
VARIABLE (grub_relocator32_backward_start)
|
||||
move $12, $9
|
||||
move $13, $10
|
||||
|
||||
addu $9, $9, $10
|
||||
addu $8, $8, $10
|
||||
/* Backward movsl is implicitly off-by-one. compensate that. */
|
||||
addiu $9, $9, 0xffff
|
||||
addiu $8, $8, 0xffff
|
||||
copycont2:
|
||||
lb $11,0($8)
|
||||
sb $11,0($9)
|
||||
cache 1, 0($9)
|
||||
cache 0, 0($9)
|
||||
addiu $8, $8, 0xffff
|
||||
addiu $9, $9, 0xffff
|
||||
addiu $10, 0xffff
|
||||
subu $11,$10,$0
|
||||
bne $11, $0, copycont2
|
||||
cachecont2:
|
||||
cache 1,0($12)
|
||||
cache 0,0($12)
|
||||
addiu $12, $12, 0x1
|
||||
addiu $13, $13, 0xffff
|
||||
subu $11,$13,$0
|
||||
bne $11, $0, cachecont2
|
||||
VARIABLE (grub_relocator32_backward_end)
|
113
lib/relocator.c
Normal file
113
lib/relocator.c
Normal file
|
@ -0,0 +1,113 @@
|
|||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
void *
|
||||
PREFIX (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 *
|
||||
PREFIX (realloc) (void *relocator, grub_size_t size)
|
||||
{
|
||||
char *playground;
|
||||
|
||||
playground = (char *) relocator - RELOCATOR_SIZEOF (forward);
|
||||
|
||||
playground = grub_realloc (playground,
|
||||
(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
|
||||
PREFIX(free) (void *relocator)
|
||||
{
|
||||
if (relocator)
|
||||
grub_free ((char *) relocator - RELOCATOR_SIZEOF (forward));
|
||||
}
|
||||
|
||||
grub_err_t
|
||||
PREFIX (boot) (void *relocator, grub_uint32_t dest,
|
||||
struct grub_relocator32_state state)
|
||||
{
|
||||
grub_size_t size;
|
||||
char *playground;
|
||||
|
||||
playground = (char *) relocator - RELOCATOR_SIZEOF (forward);
|
||||
size = *(grub_size_t *) playground;
|
||||
|
||||
grub_dprintf ("relocator",
|
||||
"Relocator: source: %p, destination: 0x%x, size: 0x%x\n",
|
||||
relocator, dest, size);
|
||||
|
||||
if (UINT_TO_PTR (dest) >= relocator)
|
||||
{
|
||||
int overhead;
|
||||
overhead =
|
||||
ALIGN_UP (dest - RELOCATOR_SIZEOF (backward) - RELOCATOR_ALIGN,
|
||||
RELOCATOR_ALIGN);
|
||||
grub_dprintf ("relocator",
|
||||
"Backward relocator: code %p, source: %p, "
|
||||
"destination: 0x%x, size: 0x%x\n",
|
||||
(char *) relocator - overhead,
|
||||
(char *) relocator - overhead,
|
||||
dest - overhead, size + overhead);
|
||||
|
||||
write_call_relocator_bw ((char *) relocator - overhead,
|
||||
(char *) relocator - overhead,
|
||||
dest - overhead, size + overhead, state);
|
||||
}
|
||||
else
|
||||
{
|
||||
int overhead;
|
||||
|
||||
overhead = ALIGN_UP (dest + size, RELOCATOR_ALIGN)
|
||||
+ RELOCATOR_SIZEOF (forward) - (dest + size);
|
||||
grub_dprintf ("relocator",
|
||||
"Forward relocator: code %p, source: %p, "
|
||||
"destination: 0x%x, size: 0x%x\n",
|
||||
(char *) relocator + size + overhead
|
||||
- RELOCATOR_SIZEOF (forward),
|
||||
relocator, dest, size + overhead);
|
||||
|
||||
write_call_relocator_fw ((char *) relocator + size + overhead
|
||||
- RELOCATOR_SIZEOF (forward),
|
||||
relocator, dest, size + overhead, state);
|
||||
}
|
||||
|
||||
/* Not reached. */
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue