grub/lib/relocator.c

138 lines
4.1 KiB
C
Raw Normal View History

2009-11-25 22:39:59 +00:00
/*
* 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))
2009-11-26 21:51:00 +00:00
#define PRE_REGION_SIZE (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN)
2009-11-25 22:39:59 +00:00
void *
PREFIX (alloc) (grub_size_t size)
{
char *playground;
playground = grub_malloc (size + MAX_OVERHEAD);
2009-11-25 22:39:59 +00:00
if (!playground)
return 0;
*(grub_size_t *) playground = size;
2009-11-26 21:51:00 +00:00
return playground + PRE_REGION_SIZE;
2009-11-25 22:39:59 +00:00
}
void *
PREFIX (realloc) (void *relocator, grub_size_t size)
{
char *playground;
if (!relocator)
return PREFIX (alloc) (size);
2009-11-26 21:51:00 +00:00
playground = (char *) relocator - PRE_REGION_SIZE;
2009-11-25 22:39:59 +00:00
playground = grub_realloc (playground, size + MAX_OVERHEAD);
2009-11-25 22:39:59 +00:00
if (!playground)
return 0;
*(grub_size_t *) playground = size;
2009-11-26 21:51:00 +00:00
return playground + PRE_REGION_SIZE;
2009-11-25 22:39:59 +00:00
}
void
PREFIX(free) (void *relocator)
{
if (relocator)
2009-11-26 21:51:00 +00:00
grub_free ((char *) relocator - PRE_REGION_SIZE);
2009-11-25 22:39:59 +00:00
}
grub_err_t
PREFIX (boot) (void *relocator, grub_uint32_t dest,
struct grub_relocator32_state state)
{
grub_size_t size;
char *playground;
2009-11-26 21:51:00 +00:00
playground = (char *) relocator - PRE_REGION_SIZE;
2009-11-25 22:39:59 +00:00
size = *(grub_size_t *) playground;
grub_dprintf ("relocator",
2009-11-27 09:57:52 +00:00
"Relocator: source: %p, destination: 0x%x, size: 0x%lx\n",
relocator, (unsigned) dest, (unsigned long) size);
2009-11-25 22:39:59 +00:00
/* Very unlikely condition: Relocator may risk overwrite itself.
Just move it a bit up. */
if ((grub_addr_t) dest < (grub_addr_t) relocator
2009-12-23 Felix Zielcke <fzielcke@z-51.de> * commands/i386/pc/drivemap.c: Remove all trailing whitespace. * commands/lspci.c: Likewise. * commands/probe.c: Likewise. * commands/xnu_uuid.c: Likewise. * conf/i386-coreboot.rmk: Likewise. * conf/i386-efi.rmk: Likewise. * conf/i386-ieee1275.rmk: Likewise. * conf/i386-pc.rmk: Likewise. * conf/powerpc-ieee1275.rmk: Likewise. * conf/sparc64-ieee1275.rmk: Likewise. * conf/x86_64-efi.rmk: Likewise. * fs/i386/pc/pxe.c: Likewise. * gettext/gettext.c: Likewise. * include/grub/efi/graphics_output.h: Likewise. * include/grub/i386/pc/memory.h: Likewise. * kern/env.c: Likewise. * kern/i386/qemu/startup.S: Likewise. * lib/i386/pc/biosnum.c: Likewise. * lib/i386/relocator.c: Likewise. * lib/i386/relocator_asm.S: Likewise. * lib/relocator.c: Likewise. * loader/i386/bsd.c: Likewise. * loader/i386/multiboot.c: Likewise. * loader/i386/pc/chainloader.c: Likewise. * loader/i386/xnu.c: Likewise. * loader/xnu.c: Likewise. * normal/main.c: Likewise. * normal/menu_text.c: Likewise. * util/getroot.c: Likewise. * util/grub-mkconfig_lib.in: Likewise. * util/grub.d/00_header.in: Likewise. * util/i386/pc/grub-mkimage.c: Likewise. * util/mkisofs/eltorito.c: Likewise. * util/mkisofs/exclude.h: Likewise. * util/mkisofs/hash.c: Likewise. * util/mkisofs/iso9660.h: Likewise. * util/mkisofs/joliet.c: Likewise. * util/mkisofs/mkisofs.c: Likewise. * util/mkisofs/mkisofs.h: Likewise. * util/mkisofs/multi.c: Likewise. * util/mkisofs/name.c: Likewise. * util/mkisofs/rock.c: Likewise. * util/mkisofs/tree.c: Likewise. * util/mkisofs/write.c: Likewise. * video/efi_gop.c: Likewise.
2009-12-23 16:41:32 +00:00
+ (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)
2009-11-25 22:39:59 +00:00
{
int overhead;
2009-11-26 21:51:00 +00:00
overhead = dest -
2009-11-25 22:39:59 +00:00
ALIGN_UP (dest - RELOCATOR_SIZEOF (backward) - RELOCATOR_ALIGN,
RELOCATOR_ALIGN);
grub_dprintf ("relocator",
"Backward relocator: code %p, source: %p, "
2009-11-27 09:57:52 +00:00
"destination: 0x%x, size: 0x%lx\n",
2009-11-25 22:39:59 +00:00
(char *) relocator - overhead,
2009-12-23 Felix Zielcke <fzielcke@z-51.de> * commands/i386/pc/drivemap.c: Remove all trailing whitespace. * commands/lspci.c: Likewise. * commands/probe.c: Likewise. * commands/xnu_uuid.c: Likewise. * conf/i386-coreboot.rmk: Likewise. * conf/i386-efi.rmk: Likewise. * conf/i386-ieee1275.rmk: Likewise. * conf/i386-pc.rmk: Likewise. * conf/powerpc-ieee1275.rmk: Likewise. * conf/sparc64-ieee1275.rmk: Likewise. * conf/x86_64-efi.rmk: Likewise. * fs/i386/pc/pxe.c: Likewise. * gettext/gettext.c: Likewise. * include/grub/efi/graphics_output.h: Likewise. * include/grub/i386/pc/memory.h: Likewise. * kern/env.c: Likewise. * kern/i386/qemu/startup.S: Likewise. * lib/i386/pc/biosnum.c: Likewise. * lib/i386/relocator.c: Likewise. * lib/i386/relocator_asm.S: Likewise. * lib/relocator.c: Likewise. * loader/i386/bsd.c: Likewise. * loader/i386/multiboot.c: Likewise. * loader/i386/pc/chainloader.c: Likewise. * loader/i386/xnu.c: Likewise. * loader/xnu.c: Likewise. * normal/main.c: Likewise. * normal/menu_text.c: Likewise. * util/getroot.c: Likewise. * util/grub-mkconfig_lib.in: Likewise. * util/grub.d/00_header.in: Likewise. * util/i386/pc/grub-mkimage.c: Likewise. * util/mkisofs/eltorito.c: Likewise. * util/mkisofs/exclude.h: Likewise. * util/mkisofs/hash.c: Likewise. * util/mkisofs/iso9660.h: Likewise. * util/mkisofs/joliet.c: Likewise. * util/mkisofs/mkisofs.c: Likewise. * util/mkisofs/mkisofs.h: Likewise. * util/mkisofs/multi.c: Likewise. * util/mkisofs/name.c: Likewise. * util/mkisofs/rock.c: Likewise. * util/mkisofs/tree.c: Likewise. * util/mkisofs/write.c: Likewise. * video/efi_gop.c: Likewise.
2009-12-23 16:41:32 +00:00
(char *) relocator - overhead,
2009-11-27 09:57:52 +00:00
(unsigned) dest - overhead,
(unsigned long) size + overhead);
2009-11-25 22:39:59 +00:00
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, "
2009-11-27 09:57:52 +00:00
"destination: 0x%x, size: 0x%lx\n",
2009-11-25 22:39:59 +00:00
(char *) relocator + size + overhead
- RELOCATOR_SIZEOF (forward),
2009-11-27 09:57:52 +00:00
relocator, (unsigned) dest,
(unsigned long) size + overhead);
2009-11-25 22:39:59 +00:00
write_call_relocator_fw ((char *) relocator + size + overhead
- RELOCATOR_SIZEOF (forward),
relocator, dest, size + overhead, state);
}
/* Not reached. */
return GRUB_ERR_NONE;
}