vaseboot/VasEBoot-core/loader/xnu_resume.c

189 lines
5.5 KiB
C

/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 2009 Free Software Foundation, Inc.
*
* VAS_EBOOT 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.
*
* VAS_EBOOT 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 VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/normal.h>
#include <VasEBoot/dl.h>
#include <VasEBoot/file.h>
#include <VasEBoot/disk.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/xnu.h>
#include <VasEBoot/cpu/xnu.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/loader.h>
#include <VasEBoot/i18n.h>
static void *VasEBoot_xnu_hibernate_image;
static VasEBoot_err_t
VasEBoot_xnu_resume_unload (void)
{
/* Free loaded image */
if (VasEBoot_xnu_hibernate_image)
VasEBoot_free (VasEBoot_xnu_hibernate_image);
VasEBoot_xnu_hibernate_image = 0;
VasEBoot_xnu_unlock ();
return VAS_EBOOT_ERR_NONE;
}
VasEBoot_err_t
VasEBoot_xnu_resume (char *imagename)
{
VasEBoot_file_t file;
VasEBoot_size_t total_header_size;
struct VasEBoot_xnu_hibernate_header hibhead;
void *code;
void *image;
VasEBoot_uint32_t codedest;
VasEBoot_uint32_t codesize;
VasEBoot_addr_t target_image;
VasEBoot_err_t err;
file = VasEBoot_file_open (imagename, VAS_EBOOT_FILE_TYPE_XNU_HIBERNATE_IMAGE
| VAS_EBOOT_FILE_TYPE_NO_DECOMPRESS);
if (! file)
return 0;
/* Read the header. */
if (VasEBoot_file_read (file, &hibhead, sizeof (hibhead))
!= sizeof (hibhead))
{
VasEBoot_file_close (file);
if (!VasEBoot_errno)
VasEBoot_error (VAS_EBOOT_ERR_READ_ERROR,
N_("premature end of file %s"), imagename);
return VasEBoot_errno;
}
/* Check the header. */
if (hibhead.magic != VAS_EBOOT_XNU_HIBERNATE_MAGIC)
{
VasEBoot_file_close (file);
return VasEBoot_error (VAS_EBOOT_ERR_BAD_OS,
"hibernate header has incorrect magic number");
}
if (hibhead.encoffset)
{
VasEBoot_file_close (file);
return VasEBoot_error (VAS_EBOOT_ERR_BAD_OS,
"encrypted images aren't supported yet");
}
if (hibhead.image_size == 0)
{
VasEBoot_file_close (file);
return VasEBoot_error (VAS_EBOOT_ERR_BAD_OS,
"hibernate image is empty");
}
codedest = hibhead.launchcode_target_page;
codedest *= VAS_EBOOT_XNU_PAGESIZE;
codesize = hibhead.launchcode_numpages;
codesize *= VAS_EBOOT_XNU_PAGESIZE;
/* FIXME: check that codedest..codedest+codesize is available. */
/* Calculate total size before pages to copy. */
total_header_size = hibhead.extmapsize + sizeof (hibhead);
/* Unload image if any. */
if (VasEBoot_xnu_hibernate_image)
VasEBoot_free (VasEBoot_xnu_hibernate_image);
/* Try to allocate necessary space.
FIXME: mm isn't good enough yet to handle huge allocations.
*/
VasEBoot_xnu_relocator = VasEBoot_relocator_new ();
if (!VasEBoot_xnu_relocator)
{
VasEBoot_file_close (file);
return VasEBoot_errno;
}
{
VasEBoot_relocator_chunk_t ch;
err = VasEBoot_relocator_alloc_chunk_addr (VasEBoot_xnu_relocator, &ch, codedest,
codesize + VAS_EBOOT_XNU_PAGESIZE);
if (err)
{
VasEBoot_file_close (file);
return err;
}
code = get_virtual_current_address (ch);
}
{
VasEBoot_relocator_chunk_t ch;
err = VasEBoot_relocator_alloc_chunk_align (VasEBoot_xnu_relocator, &ch, 0,
UP_TO_TOP32 (hibhead.image_size),
hibhead.image_size,
VAS_EBOOT_XNU_PAGESIZE,
VAS_EBOOT_RELOCATOR_PREFERENCE_NONE, 0);
if (err)
{
VasEBoot_file_close (file);
return err;
}
image = get_virtual_current_address (ch);
target_image = get_physical_target_address (ch);
}
/* Read code part. */
if (VasEBoot_file_seek (file, total_header_size) == (VasEBoot_off_t) -1
|| VasEBoot_file_read (file, code, codesize)
!= (VasEBoot_ssize_t) codesize)
{
VasEBoot_file_close (file);
if (!VasEBoot_errno)
VasEBoot_error (VAS_EBOOT_ERR_READ_ERROR,
N_("premature end of file %s"), imagename);
return VasEBoot_errno;
}
/* Read image. */
if (VasEBoot_file_seek (file, 0) == (VasEBoot_off_t) -1
|| VasEBoot_file_read (file, image, hibhead.image_size)
!= (VasEBoot_ssize_t) hibhead.image_size)
{
VasEBoot_file_close (file);
if (!VasEBoot_errno)
VasEBoot_error (VAS_EBOOT_ERR_READ_ERROR,
N_("premature end of file %s"), imagename);
return VasEBoot_errno;
}
VasEBoot_file_close (file);
/* Setup variables needed by asm helper. */
VasEBoot_xnu_heap_target_start = codedest;
VasEBoot_xnu_heap_size = target_image + hibhead.image_size - codedest;
VasEBoot_xnu_stack = (codedest + hibhead.stack);
VasEBoot_xnu_entry_point = (codedest + hibhead.entry_point);
VasEBoot_xnu_arg1 = target_image;
VasEBoot_dprintf ("xnu", "entry point 0x%x\n", codedest + hibhead.entry_point);
VasEBoot_dprintf ("xnu", "image at 0x%x\n",
codedest + codesize + VAS_EBOOT_XNU_PAGESIZE);
/* We're ready now. */
VasEBoot_loader_set (VasEBoot_xnu_boot_resume,
VasEBoot_xnu_resume_unload, 0);
/* Prevent module from unloading. */
VasEBoot_xnu_lock ();
return VAS_EBOOT_ERR_NONE;
}