merge mainline into crypto
This commit is contained in:
commit
ba136b293a
165 changed files with 4910 additions and 1858 deletions
|
@ -30,9 +30,9 @@
|
|||
static const struct grub_arg_option help_options[] =
|
||||
{
|
||||
{"help", SHORT_ARG_HELP, 0,
|
||||
"display this help and exit", 0, ARG_TYPE_NONE},
|
||||
"Display this help and exit.", 0, ARG_TYPE_NONE},
|
||||
{"usage", SHORT_ARG_USAGE, 0,
|
||||
"display the usage of this command and exit", 0, ARG_TYPE_NONE},
|
||||
"Display the usage of this command and exit.", 0, ARG_TYPE_NONE},
|
||||
{0, 0, 0, 0, 0, 0}
|
||||
};
|
||||
|
||||
|
@ -355,7 +355,7 @@ grub_arg_parse (grub_extcmd_t cmd, int argc, char **argv,
|
|||
{
|
||||
char *tail;
|
||||
|
||||
grub_strtoul (option, &tail, 0);
|
||||
grub_strtoull (option, &tail, 0);
|
||||
if (tail == 0 || tail == option || *tail != '\0' || grub_errno)
|
||||
{
|
||||
grub_error (GRUB_ERR_BAD_ARGUMENT,
|
||||
|
|
116
lib/charset.c
Normal file
116
lib/charset.c
Normal file
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,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/>.
|
||||
*/
|
||||
|
||||
/* Convert a (possibly null-terminated) UTF-8 string of at most SRCSIZE
|
||||
bytes (if SRCSIZE is -1, it is ignored) in length to a UTF-16 string.
|
||||
Return the number of characters converted. DEST must be able to hold
|
||||
at least DESTSIZE characters. If an invalid sequence is found, return -1.
|
||||
If SRCEND is not NULL, then *SRCEND is set to the next byte after the
|
||||
last byte used in SRC. */
|
||||
|
||||
#include <grub/charset.h>
|
||||
|
||||
grub_ssize_t
|
||||
grub_utf8_to_utf16 (grub_uint16_t *dest, grub_size_t destsize,
|
||||
const grub_uint8_t *src, grub_size_t srcsize,
|
||||
const grub_uint8_t **srcend)
|
||||
{
|
||||
grub_uint16_t *p = dest;
|
||||
int count = 0;
|
||||
grub_uint32_t code = 0;
|
||||
|
||||
if (srcend)
|
||||
*srcend = src;
|
||||
|
||||
while (srcsize && destsize)
|
||||
{
|
||||
grub_uint32_t c = *src++;
|
||||
if (srcsize != (grub_size_t)-1)
|
||||
srcsize--;
|
||||
if (count)
|
||||
{
|
||||
if ((c & GRUB_UINT8_2_LEADINGBITS) != GRUB_UINT8_1_LEADINGBIT)
|
||||
{
|
||||
/* invalid */
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
code <<= 6;
|
||||
code |= (c & GRUB_UINT8_6_TRAILINGBITS);
|
||||
count--;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (c == 0)
|
||||
break;
|
||||
|
||||
if ((c & GRUB_UINT8_1_LEADINGBIT) == 0)
|
||||
code = c;
|
||||
else if ((c & GRUB_UINT8_3_LEADINGBITS) == GRUB_UINT8_2_LEADINGBITS)
|
||||
{
|
||||
count = 1;
|
||||
code = c & GRUB_UINT8_5_TRAILINGBITS;
|
||||
}
|
||||
else if ((c & GRUB_UINT8_4_LEADINGBITS) == GRUB_UINT8_3_LEADINGBITS)
|
||||
{
|
||||
count = 2;
|
||||
code = c & GRUB_UINT8_4_TRAILINGBITS;
|
||||
}
|
||||
else if ((c & GRUB_UINT8_5_LEADINGBITS) == GRUB_UINT8_4_LEADINGBITS)
|
||||
{
|
||||
count = 3;
|
||||
code = c & GRUB_UINT8_3_TRAILINGBITS;
|
||||
}
|
||||
else if ((c & GRUB_UINT8_6_LEADINGBITS) == GRUB_UINT8_5_LEADINGBITS)
|
||||
{
|
||||
count = 4;
|
||||
code = c & GRUB_UINT8_2_TRAILINGBITS;
|
||||
}
|
||||
else if ((c & GRUB_UINT8_7_LEADINGBITS) == GRUB_UINT8_6_LEADINGBITS)
|
||||
{
|
||||
count = 5;
|
||||
code = c & GRUB_UINT8_1_TRAILINGBIT;
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (count == 0)
|
||||
{
|
||||
if (destsize < 2 && code >= GRUB_UCS2_LIMIT)
|
||||
break;
|
||||
if (code >= GRUB_UCS2_LIMIT)
|
||||
{
|
||||
*p++ = GRUB_UTF16_UPPER_SURROGATE (code);
|
||||
*p++ = GRUB_UTF16_LOWER_SURROGATE (code);
|
||||
destsize -= 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
*p++ = code;
|
||||
destsize--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (srcend)
|
||||
*srcend = src;
|
||||
return p - dest;
|
||||
}
|
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"
|
248
lib/i386/relocator_asm.S
Normal file
248
lib/i386/relocator_asm.S
Normal file
|
@ -0,0 +1,248 @@
|
|||
/*
|
||||
* 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 RDI %rdi
|
||||
#define RSI %rdi
|
||||
#else
|
||||
#define RAX %eax
|
||||
#define RCX %ecx
|
||||
#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 RCX, 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. */
|
||||
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 CODE_SEGMENT
|
||||
|
||||
/* 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"
|
93
lib/mips/relocator_asm.S
Normal file
93
lib/mips/relocator_asm.S
Normal file
|
@ -0,0 +1,93 @@
|
|||
/*
|
||||
* 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
|
||||
bne $10, $0, copycont1
|
||||
|
||||
move $9, $12
|
||||
move $10, $13
|
||||
cachecont1a:
|
||||
cache 1,0($12)
|
||||
addiu $12, $12, 0x1
|
||||
addiu $13, $13, 0xffff
|
||||
bne $13, $0, cachecont1a
|
||||
|
||||
sync
|
||||
|
||||
move $12, $9
|
||||
move $13, $10
|
||||
cachecont1b:
|
||||
cache 0,0($12)
|
||||
addiu $12, $12, 0x1
|
||||
addiu $13, $13, 0xffff
|
||||
bne $13, $0, cachecont1b
|
||||
|
||||
sync
|
||||
|
||||
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)
|
||||
addiu $8, $8, 0xffff
|
||||
addiu $9, $9, 0xffff
|
||||
addiu $10, 0xffff
|
||||
bne $10, $0, copycont2
|
||||
|
||||
move $9, $12
|
||||
move $10, $13
|
||||
cachecont2a:
|
||||
cache 1,0($12)
|
||||
addiu $12, $12, 0x1
|
||||
addiu $13, $13, 0xffff
|
||||
bne $13, $0, cachecont2a
|
||||
|
||||
sync
|
||||
|
||||
move $12, $9
|
||||
move $13, $10
|
||||
cachecont2b:
|
||||
cache 0,0($12)
|
||||
addiu $12, $12, 0x1
|
||||
addiu $13, $13, 0xffff
|
||||
bne $13, $0, cachecont2b
|
||||
|
||||
sync
|
||||
VARIABLE (grub_relocator32_backward_end)
|
137
lib/relocator.c
Normal file
137
lib/relocator.c
Normal file
|
@ -0,0 +1,137 @@
|
|||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#define MAX_OVERHEAD ((RELOCATOR_SIZEOF (forward) + RELOCATOR_ALIGN) \
|
||||
+ (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN) \
|
||||
+ (RELOCATOR_SIZEOF (forward) + RELOCATOR_ALIGN) \
|
||||
+ (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN))
|
||||
#define PRE_REGION_SIZE (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN)
|
||||
|
||||
void *
|
||||
PREFIX (alloc) (grub_size_t size)
|
||||
{
|
||||
char *playground;
|
||||
|
||||
playground = grub_malloc (size + MAX_OVERHEAD);
|
||||
if (!playground)
|
||||
return 0;
|
||||
|
||||
*(grub_size_t *) playground = size;
|
||||
|
||||
return playground + PRE_REGION_SIZE;
|
||||
}
|
||||
|
||||
void *
|
||||
PREFIX (realloc) (void *relocator, grub_size_t size)
|
||||
{
|
||||
char *playground;
|
||||
|
||||
if (!relocator)
|
||||
return PREFIX (alloc) (size);
|
||||
|
||||
playground = (char *) relocator - PRE_REGION_SIZE;
|
||||
|
||||
playground = grub_realloc (playground, size + MAX_OVERHEAD);
|
||||
if (!playground)
|
||||
return 0;
|
||||
|
||||
*(grub_size_t *) playground = size;
|
||||
|
||||
return playground + PRE_REGION_SIZE;
|
||||
}
|
||||
|
||||
void
|
||||
PREFIX(free) (void *relocator)
|
||||
{
|
||||
if (relocator)
|
||||
grub_free ((char *) relocator - PRE_REGION_SIZE);
|
||||
}
|
||||
|
||||
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 - PRE_REGION_SIZE;
|
||||
size = *(grub_size_t *) playground;
|
||||
|
||||
grub_dprintf ("relocator",
|
||||
"Relocator: source: %p, destination: 0x%x, size: 0x%lx\n",
|
||||
relocator, (unsigned) dest, (unsigned long) size);
|
||||
|
||||
/* Very unlikely condition: Relocator may risk overwrite itself.
|
||||
Just move it a bit up. */
|
||||
if ((grub_addr_t) dest < (grub_addr_t) relocator
|
||||
+ (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN)
|
||||
&& (grub_addr_t) dest + (RELOCATOR_SIZEOF (forward) + RELOCATOR_ALIGN)
|
||||
> (grub_addr_t) relocator)
|
||||
{
|
||||
void *relocator_new = ((grub_uint8_t *) relocator)
|
||||
+ (RELOCATOR_SIZEOF (forward) + RELOCATOR_ALIGN)
|
||||
+ (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN);
|
||||
grub_dprintf ("relocator", "Overwrite condition detected moving "
|
||||
"relocator from %p to %p\n", relocator, relocator_new);
|
||||
grub_memmove (relocator_new, relocator,
|
||||
(RELOCATOR_SIZEOF (forward) + RELOCATOR_ALIGN)
|
||||
+ size
|
||||
+ (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN));
|
||||
relocator = relocator_new;
|
||||
}
|
||||
|
||||
if ((grub_addr_t) dest >= (grub_addr_t) relocator)
|
||||
{
|
||||
int overhead;
|
||||
overhead = dest -
|
||||
ALIGN_UP (dest - RELOCATOR_SIZEOF (backward) - RELOCATOR_ALIGN,
|
||||
RELOCATOR_ALIGN);
|
||||
grub_dprintf ("relocator",
|
||||
"Backward relocator: code %p, source: %p, "
|
||||
"destination: 0x%x, size: 0x%lx\n",
|
||||
(char *) relocator - overhead,
|
||||
(char *) relocator - overhead,
|
||||
(unsigned) dest - overhead,
|
||||
(unsigned long) 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%lx\n",
|
||||
(char *) relocator + size + overhead
|
||||
- RELOCATOR_SIZEOF (forward),
|
||||
relocator, (unsigned) dest,
|
||||
(unsigned long) 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