2009-05-02 22:40:21 +00:00
|
|
|
/* Load runtime image of EFIemu. Functions common to 32/64-bit mode */
|
|
|
|
/*
|
|
|
|
* 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/file.h>
|
|
|
|
#include <grub/err.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/efiemu/efiemu.h>
|
|
|
|
#include <grub/cpu/efiemu.h>
|
|
|
|
|
|
|
|
/* Are we in 32 or 64-bit mode?*/
|
|
|
|
static grub_efiemu_mode_t grub_efiemu_mode = GRUB_EFIEMU_NOTLOADED;
|
|
|
|
/* Runtime ELF file */
|
|
|
|
static grub_ssize_t efiemu_core_size;
|
|
|
|
static void *efiemu_core = 0;
|
|
|
|
/* Linked list of segments */
|
|
|
|
static grub_efiemu_segment_t efiemu_segments = 0;
|
|
|
|
|
|
|
|
/* equivalent to sizeof (grub_efi_uintn_t) but taking the mode into account*/
|
|
|
|
int
|
|
|
|
grub_efiemu_sizeof_uintn_t (void)
|
|
|
|
{
|
|
|
|
if (grub_efiemu_mode == GRUB_EFIEMU32)
|
|
|
|
return 4;
|
|
|
|
if (grub_efiemu_mode == GRUB_EFIEMU64)
|
|
|
|
return 8;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the header and set mode */
|
|
|
|
static grub_err_t
|
2009-06-10 21:04:23 +00:00
|
|
|
grub_efiemu_check_header (void *ehdr, grub_size_t size,
|
2009-05-02 22:40:21 +00:00
|
|
|
grub_efiemu_mode_t *mode)
|
|
|
|
{
|
|
|
|
/* Check the magic numbers. */
|
|
|
|
if ((*mode == GRUB_EFIEMU_NOTLOADED || *mode == GRUB_EFIEMU32)
|
|
|
|
&& grub_efiemu_check_header32 (ehdr,size))
|
|
|
|
{
|
|
|
|
*mode = GRUB_EFIEMU32;
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
if ((*mode == GRUB_EFIEMU_NOTLOADED || *mode == GRUB_EFIEMU64)
|
|
|
|
&& grub_efiemu_check_header64 (ehdr,size))
|
|
|
|
{
|
|
|
|
*mode = GRUB_EFIEMU64;
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
return grub_error (GRUB_ERR_BAD_OS, "invalid ELF magic");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unload segments */
|
|
|
|
static int
|
|
|
|
grub_efiemu_unload_segs (grub_efiemu_segment_t seg)
|
|
|
|
{
|
|
|
|
grub_efiemu_segment_t segn;
|
|
|
|
for (; seg; seg = segn)
|
|
|
|
{
|
|
|
|
segn = seg->next;
|
|
|
|
grub_efiemu_mm_return_request (seg->handle);
|
|
|
|
grub_free (seg);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
grub_err_t
|
|
|
|
grub_efiemu_loadcore_unload(void)
|
|
|
|
{
|
|
|
|
switch (grub_efiemu_mode)
|
|
|
|
{
|
|
|
|
case GRUB_EFIEMU32:
|
|
|
|
grub_efiemu_loadcore_unload32 ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GRUB_EFIEMU64:
|
|
|
|
grub_efiemu_loadcore_unload64 ();
|
|
|
|
break;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-05-02 22:40:21 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_efiemu_mode = GRUB_EFIEMU_NOTLOADED;
|
|
|
|
|
|
|
|
grub_free (efiemu_core);
|
|
|
|
efiemu_core = 0;
|
|
|
|
|
|
|
|
grub_efiemu_unload_segs (efiemu_segments);
|
|
|
|
efiemu_segments = 0;
|
|
|
|
|
|
|
|
grub_efiemu_free_syms ();
|
|
|
|
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2009-06-10 21:04:23 +00:00
|
|
|
/* Load runtime file and do some initial preparations */
|
2009-05-02 22:40:21 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_efiemu_loadcore_init (grub_file_t file)
|
|
|
|
{
|
|
|
|
grub_err_t err;
|
|
|
|
|
|
|
|
efiemu_core_size = grub_file_size (file);
|
|
|
|
efiemu_core = 0;
|
|
|
|
efiemu_core = grub_malloc (efiemu_core_size);
|
|
|
|
if (! efiemu_core)
|
|
|
|
return grub_errno;
|
|
|
|
|
2009-06-10 21:04:23 +00:00
|
|
|
if (grub_file_read (file, efiemu_core, efiemu_core_size)
|
2009-05-02 22:40:21 +00:00
|
|
|
!= (int) efiemu_core_size)
|
|
|
|
{
|
|
|
|
grub_free (efiemu_core);
|
|
|
|
efiemu_core = 0;
|
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
|
2009-06-10 21:04:23 +00:00
|
|
|
if (grub_efiemu_check_header (efiemu_core, efiemu_core_size,
|
2009-05-02 22:40:21 +00:00
|
|
|
&grub_efiemu_mode))
|
|
|
|
{
|
|
|
|
grub_free (efiemu_core);
|
|
|
|
efiemu_core = 0;
|
|
|
|
return GRUB_ERR_BAD_MODULE;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (grub_efiemu_mode)
|
|
|
|
{
|
|
|
|
case GRUB_EFIEMU32:
|
2012-02-08 18:26:01 +00:00
|
|
|
err = grub_efiemu_loadcore_init32 (efiemu_core, efiemu_core_size,
|
|
|
|
&efiemu_segments);
|
|
|
|
if (err)
|
2009-05-02 22:40:21 +00:00
|
|
|
{
|
|
|
|
grub_free (efiemu_core);
|
|
|
|
efiemu_core = 0;
|
|
|
|
grub_efiemu_mode = GRUB_EFIEMU_NOTLOADED;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GRUB_EFIEMU64:
|
2012-02-08 18:26:01 +00:00
|
|
|
err = grub_efiemu_loadcore_init64 (efiemu_core, efiemu_core_size,
|
|
|
|
&efiemu_segments);
|
|
|
|
if (err)
|
2009-05-02 22:40:21 +00:00
|
|
|
{
|
|
|
|
grub_free (efiemu_core);
|
|
|
|
efiemu_core = 0;
|
|
|
|
grub_efiemu_mode = GRUB_EFIEMU_NOTLOADED;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
break;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-05-02 22:40:21 +00:00
|
|
|
default:
|
2012-02-08 18:26:01 +00:00
|
|
|
return grub_error (GRUB_ERR_BUG, "unknown EFI runtime");
|
2009-05-02 22:40:21 +00:00
|
|
|
}
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_err_t
|
|
|
|
grub_efiemu_loadcore_load (void)
|
|
|
|
{
|
|
|
|
grub_err_t err;
|
|
|
|
switch (grub_efiemu_mode)
|
|
|
|
{
|
|
|
|
case GRUB_EFIEMU32:
|
2012-02-09 21:57:51 +00:00
|
|
|
err = grub_efiemu_loadcore_load32 (efiemu_core, efiemu_core_size,
|
|
|
|
efiemu_segments);
|
|
|
|
if (err)
|
|
|
|
grub_efiemu_loadcore_unload ();
|
2009-05-02 22:40:21 +00:00
|
|
|
return err;
|
|
|
|
case GRUB_EFIEMU64:
|
2012-02-09 21:57:51 +00:00
|
|
|
err = grub_efiemu_loadcore_load64 (efiemu_core, efiemu_core_size,
|
|
|
|
efiemu_segments);
|
|
|
|
if (err)
|
|
|
|
grub_efiemu_loadcore_unload ();
|
2009-05-02 22:40:21 +00:00
|
|
|
return err;
|
|
|
|
default:
|
2012-02-08 18:26:01 +00:00
|
|
|
return grub_error (GRUB_ERR_BUG, "unknown EFI runtime");
|
2009-05-02 22:40:21 +00:00
|
|
|
}
|
|
|
|
}
|