2009-05-03 Vladimir Serbinenko <phcoder@gmail.com>

xnu support

	* conf/i386-efi.rmk (kernel_mod_HEADERS): added i386/pit.h
	(pkglib_MODULES): add xnu.mod
	(xnu_mod_SOURCES): new variable
	(xnu_mod_CFLAGS): likewise
	(xnu_mod_LDFLAGS): likewise
	(xnu_mod_ASFLAGS): likewise
	* conf/i386-pc.rmk: likewise
	* conf/x86_64-efi.rmk: likewise
	* include/grub/efi/efi.h (grub_efi_finish_boot_services): 
	new declaration
	* include/grub/i386/macho.h: new file
	* include/grub/i386/xnu.h: likewise
	* include/grub/macho.h: likewise
	* include/grub/machoload.h: likewise
	* include/grub/x86_64/macho.h: likewise
	* include/grub/x86_64/xnu.h: likewise
	* include/grub/xnu.h: likewise
	* kern/efi/efi.c (grub_efi_finish_boot_services): new function
	* kern/efi/mm.c (MAX_HEAP_SIZE): increase
	* loader/i386/efi/xnu.c: new file
	* loader/i386/pc/xnu.c: likewise
	* loader/i386/xnu.c: likewise
	* loader/i386/xnu_helper.S: likewise
	* loader/macho.c: likewise
	* loader/xnu.c: likewise
	* loader/xnu_resume.c: likewise
	* util/grub-dumpdevtree: likewise
	* include/grub/i386/pit.h: include grub/err.h
	(grub_pit_wait): export
	* util/grub.d/30_os-prober.in: support Darwin/Mac OS X
This commit is contained in:
phcoder 2009-05-02 23:19:20 +00:00
parent 5caf964d75
commit bbee0f2b56
24 changed files with 3378 additions and 6 deletions

177
loader/i386/efi/xnu.c Normal file
View file

@ -0,0 +1,177 @@
/*
* 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/env.h>
#include <grub/xnu.h>
#include <grub/cpu/xnu.h>
#include <grub/efi/api.h>
#include <grub/efi/efi.h>
#include <grub/efi/uga_draw.h>
#include <grub/pci.h>
#include <grub/misc.h>
/* Setup video for xnu. Big parts are copied from linux.c. */
static grub_efi_guid_t uga_draw_guid = GRUB_EFI_UGA_DRAW_GUID;
#define RGB_MASK 0xffffff
#define RGB_MAGIC 0x121314
#define LINE_MIN 800
#define LINE_MAX 4096
#define FBTEST_STEP (0x10000 >> 2)
#define FBTEST_COUNT 8
static int
find_line_len (grub_uint32_t *fb_base, grub_uint32_t *line_len)
{
grub_uint32_t *base = (grub_uint32_t *) (grub_target_addr_t) *fb_base;
int i;
for (i = 0; i < FBTEST_COUNT; i++, base += FBTEST_STEP)
{
if ((*base & RGB_MASK) == RGB_MAGIC)
{
int j;
for (j = LINE_MIN; j <= LINE_MAX; j++)
{
if ((base[j] & RGB_MASK) == RGB_MAGIC)
{
*fb_base = (grub_uint32_t) (grub_target_addr_t) base;
*line_len = j << 2;
return 1;
}
}
break;
}
}
return 0;
}
static int
find_framebuf (grub_uint32_t *fb_base, grub_uint32_t *line_len)
{
int found = 0;
auto int NESTED_FUNC_ATTR find_card (int bus, int dev, int func,
grub_pci_id_t pciid);
int NESTED_FUNC_ATTR find_card (int bus, int dev, int func,
grub_pci_id_t pciid)
{
grub_pci_address_t addr;
addr = grub_pci_make_address (bus, dev, func, 2);
if (grub_pci_read (addr) >> 24 == 0x3)
{
int i;
grub_printf ("Display controller: %d:%d.%d\nDevice id: %x\n",
bus, dev, func, pciid);
addr += 8;
for (i = 0; i < 6; i++, addr += 4)
{
grub_uint32_t old_bar1, old_bar2, type;
grub_uint64_t base64;
old_bar1 = grub_pci_read (addr);
if ((! old_bar1) || (old_bar1 & GRUB_PCI_ADDR_SPACE_IO))
continue;
type = old_bar1 & GRUB_PCI_ADDR_MEM_TYPE_MASK;
if (type == GRUB_PCI_ADDR_MEM_TYPE_64)
{
if (i == 5)
break;
old_bar2 = grub_pci_read (addr + 4);
}
else
old_bar2 = 0;
base64 = old_bar2;
base64 <<= 32;
base64 |= (old_bar1 & GRUB_PCI_ADDR_MEM_MASK);
grub_printf ("%s(%d): 0x%llx\n",
((old_bar1 & GRUB_PCI_ADDR_MEM_PREFETCH) ?
"VMEM" : "MMIO"), i,
(unsigned long long) base64);
if ((old_bar1 & GRUB_PCI_ADDR_MEM_PREFETCH) && (! found))
{
*fb_base = base64;
if (find_line_len (fb_base, line_len))
found++;
}
if (type == GRUB_PCI_ADDR_MEM_TYPE_64)
{
i++;
addr += 4;
}
}
}
return found;
}
grub_pci_iterate (find_card);
return found;
}
grub_err_t
grub_xnu_set_video (struct grub_xnu_boot_params *params)
{
grub_efi_uga_draw_protocol_t *c;
grub_uint32_t width, height, depth, rate, pixel, fb_base, line_len;
int ret;
c = grub_efi_locate_protocol (&uga_draw_guid, 0);
if (! c)
return grub_error (GRUB_ERR_IO, "Couldn't find UGADraw");
if (efi_call_5 (c->get_mode, c, &width, &height, &depth, &rate))
return grub_error (GRUB_ERR_IO, "Couldn't retrieve video mode");
grub_printf ("Video mode: %ux%u-%u@%u\n", width, height, depth, rate);
grub_efi_set_text_mode (0);
pixel = RGB_MAGIC;
efi_call_10 (c->blt, c, (struct grub_efi_uga_pixel *) &pixel,
GRUB_EFI_UGA_VIDEO_FILL, 0, 0, 0, 0, 1, height, 0);
ret = find_framebuf (&fb_base, &line_len);
grub_efi_set_text_mode (1);
if (! ret)
return grub_error (GRUB_ERR_IO, "Can\'t find frame buffer address\n");
grub_printf ("Frame buffer base: 0x%x\n", fb_base);
grub_printf ("Video line length: %d\n", line_len);
params->lfb_width = width;
params->lfb_height = height;
params->lfb_depth = depth;
params->lfb_line_len = line_len;
params->lfb_mode = GRUB_XNU_VIDEO_TEXT_IN_VIDEO;
params->lfb_base = fb_base;
return GRUB_ERR_NONE;
}

80
loader/i386/pc/xnu.c Normal file
View file

@ -0,0 +1,80 @@
/*
* 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/env.h>
#include <grub/misc.h>
#include <grub/xnu.h>
#include <grub/cpu/xnu.h>
#include <grub/machine/vbe.h>
#include <grub/machine/vga.h>
#define min(a,b) (((a) < (b)) ? (a) : (b))
#define max(a,b) (((a) > (b)) ? (a) : (b))
/* Setup video for xnu. */
grub_err_t
grub_xnu_set_video (struct grub_xnu_boot_params *params)
{
struct grub_video_mode_info mode_info;
struct grub_video_render_target *render_target;
int ret;
int x,y;
grub_err_t err;
ret = grub_video_get_info (&mode_info);
if (ret)
return grub_error (GRUB_ERR_IO, "couldn't retrieve video parameters");
ret = grub_video_get_active_render_target (&render_target);
if (ret)
return grub_error (GRUB_ERR_IO, "couldn't retrieve video parameters");
err = GRUB_ERR_NONE;
x = mode_info.width - grub_xnu_bitmap->mode_info.width;
x /= 2;
y = mode_info.height - grub_xnu_bitmap->mode_info.height;
y /= 2;
err = grub_video_blit_bitmap (grub_xnu_bitmap,
GRUB_VIDEO_BLIT_REPLACE,
x > 0 ? x : 0,
y > 0 ? y : 0,
x < 0 ? -x : 0,
y < 0 ? -y : 0,
min (grub_xnu_bitmap->mode_info.width,
mode_info.width),
min (grub_xnu_bitmap->mode_info.height,
mode_info.height));
if (err)
{
grub_print_error ();
grub_errno = GRUB_ERR_NONE;
grub_xnu_bitmap = 0;
}
params->lfb_width = mode_info.width;
params->lfb_height = mode_info.height;
params->lfb_depth = mode_info.bpp;
params->lfb_line_len = mode_info.pitch;
params->lfb_base = PTR_TO_UINT32 (render_target->data);
params->lfb_mode = grub_xnu_bitmap
? GRUB_XNU_VIDEO_SPLASH : GRUB_XNU_VIDEO_TEXT_IN_VIDEO;
return GRUB_ERR_NONE;
}

539
loader/i386/xnu.c Normal file
View file

@ -0,0 +1,539 @@
/*
* 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/env.h>
#include <grub/file.h>
#include <grub/disk.h>
#include <grub/xnu.h>
#include <grub/cpu/xnu.h>
#include <grub/mm.h>
#include <grub/loader.h>
#include <grub/autoefi.h>
#include <grub/i386/tsc.h>
#include <grub/i386/pit.h>
#include <grub/misc.h>
#include <grub/term.h>
char grub_xnu_cmdline[1024];
/* Aliases set for some tables. */
struct tbl_alias
{
grub_efi_guid_t guid;
char *name;
};
struct tbl_alias table_aliases[] =
{
{GRUB_EFI_ACPI_20_TABLE_GUID, "ACPI_20"},
{GRUB_EFI_ACPI_TABLE_GUID, "ACPI"},
};
/* The following function is used to be able to debug xnu loader
with grub-emu. */
#ifdef GRUB_UTIL
static grub_err_t
grub_xnu_launch (void)
{
grub_printf ("Fake launch %x:%p:%p", grub_xnu_entry_point, grub_xnu_arg1,
grub_xnu_stack);
grub_getkey ();
return 0;
}
#else
static void (*grub_xnu_launch) (void) = 0;
#endif
static int
utf16_strlen (grub_uint16_t *in)
{
int i;
for (i = 0; in[i]; i++);
return i;
}
/* Read frequency from a string in MHz and return it in Hz. */
static grub_uint64_t
readfrequency (const char *str)
{
grub_uint64_t num = 0;
int mul = 1000000;
int found = 0;
while (*str)
{
unsigned long digit;
digit = grub_tolower (*str) - '0';
if (digit > 9)
break;
found = 1;
num = num * 10 + digit;
str++;
}
num *= 1000000;
if (*str == '.')
{
str++;
while (*str)
{
unsigned long digit;
digit = grub_tolower (*str) - '0';
if (digit > 9)
break;
found = 1;
mul /= 10;
num = num + mul * digit;
str++;
}
}
if (! found)
return 0;
return num;
}
/* Thanks to Kabyl for precious information about Intel architecture. */
static grub_uint64_t
guessfsb (void)
{
const grub_uint64_t sane_value = 100000000;
grub_uint32_t manufacturer[3], max_cpuid, capabilities, msrlow;
grub_uint64_t start_tsc;
grub_uint64_t end_tsc;
grub_uint64_t tsc_ticks_per_ms;
if (! grub_cpu_is_cpuid_supported ())
return sane_value;
asm volatile ("movl $0, %%eax\n"
"cpuid"
: "=a" (max_cpuid), "=b" (manufacturer[0]),
"=d" (manufacturer[1]), "=c" (manufacturer[2]));
/* Only Intel for now is done. */
if (grub_memcmp (manufacturer, "GenuineIntel", 12) != 0)
return sane_value;
/* Check Speedstep. */
if (max_cpuid < 1)
return sane_value;
asm volatile ("movl $1, %%eax\n"
"cpuid"
: "=c" (capabilities):
: "%eax", "%ebx", "%edx");
if (! (capabilities & (1 << 7)))
return sane_value;
/* Calibrate the TSC rate. */
start_tsc = grub_get_tsc ();
grub_pit_wait (0xffff);
end_tsc = grub_get_tsc ();
tsc_ticks_per_ms = grub_divmod64 (end_tsc - start_tsc, 55, 0);
/* Read the multiplier. */
asm volatile ("movl $0x198, %%ecx\n"
"rdmsr"
: "=d" (msrlow)
:
: "%ecx", "%eax");
return grub_divmod64 (2000 * tsc_ticks_per_ms,
((msrlow >> 7) & 0x3e) + ((msrlow >> 14) & 1), 0);
}
/* Fill device tree. */
/* FIXME: some entries may be platform-agnostic. Move them to loader/xnu.c. */
grub_err_t
grub_cpu_xnu_fill_devicetree (void)
{
struct grub_xnu_devtree_key *efikey;
struct grub_xnu_devtree_key *cfgtablekey;
struct grub_xnu_devtree_key *curval;
struct grub_xnu_devtree_key *runtimesrvkey;
struct grub_xnu_devtree_key *platformkey;
unsigned i, j;
grub_err_t err;
err = grub_autoefi_prepare ();
if (err)
return err;
/* The value "model". */
/* FIXME: may this value be sometimes different? */
curval = grub_xnu_create_value (&grub_xnu_devtree_root, "model");
if (! curval)
return grub_errno;
curval->datasize = sizeof ("ACPI");
curval->data = grub_strdup ("ACPI");
curval = grub_xnu_create_value (&grub_xnu_devtree_root, "compatible");
if (! curval)
return grub_errno;
curval->datasize = sizeof ("ACPI");
curval->data = grub_strdup ("ACPI");
/* The key "efi". */
efikey = grub_xnu_create_key (&grub_xnu_devtree_root, "efi");
if (! efikey)
return grub_errno;
/* Information about firmware. */
curval = grub_xnu_create_value (&(efikey->first_child), "firmware-revision");
if (! curval)
return grub_errno;
curval->datasize = (SYSTEM_TABLE_SIZEOF (firmware_revision));
curval->data = grub_malloc (curval->datasize);
if (! curval->data)
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't create device tree");
grub_memcpy (curval->data, (SYSTEM_TABLE_VAR(firmware_revision)),
curval->datasize);
curval = grub_xnu_create_value (&(efikey->first_child), "firmware-vendor");
if (! curval)
return grub_errno;
curval->datasize =
2 * (utf16_strlen (SYSTEM_TABLE_PTR (firmware_vendor)) + 1);
curval->data = grub_malloc (curval->datasize);
if (! curval->data)
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't create device tree");
grub_memcpy (curval->data, SYSTEM_TABLE_PTR (firmware_vendor),
curval->datasize);
curval = grub_xnu_create_value (&(efikey->first_child), "firmware-abi");
if (! curval)
return grub_errno;
curval->datasize = sizeof ("EFI32");
curval->data = grub_malloc (curval->datasize);
if (! curval->data)
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't create device tree");
if (SIZEOF_OF_UINTN == 4)
grub_memcpy (curval->data, "EFI32", curval->datasize);
else
grub_memcpy (curval->data, "EFI64", curval->datasize);
/* The key "platform". */
platformkey = grub_xnu_create_key (&(efikey->first_child),
"platform");
if (! platformkey)
return grub_errno;
/* Pass FSB frequency to the kernel. */
curval = grub_xnu_create_value (&(platformkey->first_child), "FSBFrequency");
if (! curval)
return grub_errno;
curval->datasize = sizeof (grub_uint64_t);
curval->data = grub_malloc (curval->datasize);
if (!curval->data)
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't create device tree");
/* First see if user supplies the value. */
char *fsbvar = grub_env_get ("fsb");
if (! fsbvar)
*((grub_uint64_t *) curval->data) = 0;
else
*((grub_uint64_t *) curval->data) = readfrequency (fsbvar);
/* Try autodetect. */
if (! *((grub_uint64_t *) curval->data))
*((grub_uint64_t *) curval->data) = guessfsb ();
grub_dprintf ("xnu", "fsb autodetected as %llu\n",
(unsigned long long) *((grub_uint64_t *) curval->data));
cfgtablekey = grub_xnu_create_key (&(efikey->first_child),
"configuration-table");
if (!cfgtablekey)
return grub_errno;
/* Fill "configuration-table" key. */
for (i = 0; i < SYSTEM_TABLE (num_table_entries); i++)
{
void *ptr;
struct grub_xnu_devtree_key *curkey;
grub_efi_guid_t guid;
char guidbuf[64];
/* Retrieve current key. */
#ifdef GRUB_MACHINE_EFI
{
ptr = (void *)
grub_efi_system_table->configuration_table[i].vendor_table;
guid = grub_efi_system_table->configuration_table[i].vendor_guid;
}
#else
if (SIZEOF_OF_UINTN == 4)
{
ptr = UINT_TO_PTR (((grub_efiemu_configuration_table32_t *)
SYSTEM_TABLE_PTR (configuration_table))[i]
.vendor_table);
guid =
((grub_efiemu_configuration_table32_t *)
SYSTEM_TABLE_PTR (configuration_table))[i].vendor_guid;
}
else
{
ptr = UINT_TO_PTR (((grub_efiemu_configuration_table64_t *)
SYSTEM_TABLE_PTR (configuration_table))[i]
.vendor_table);
guid =
((grub_efiemu_configuration_table64_t *)
SYSTEM_TABLE_PTR (configuration_table))[i].vendor_guid;
}
#endif
/* The name of key for new table. */
grub_sprintf (guidbuf, "%08x-%04x-%04x-%02x%02x-",
guid.data1, guid.data2, guid.data3, guid.data4[0],
guid.data4[1]);
for (j = 2; j < 8; j++)
grub_sprintf (guidbuf + grub_strlen (guidbuf), "%02x", guid.data4[j]);
/* For some reason GUID has to be in uppercase. */
for (j = 0; guidbuf[j] ; j++)
if (guidbuf[j] >= 'a' && guidbuf[j] <= 'f')
guidbuf[j] += 'A' - 'a';
curkey = grub_xnu_create_key (&(cfgtablekey->first_child), guidbuf);
if (! curkey)
return grub_errno;
curval = grub_xnu_create_value (&(curkey->first_child), "guid");
if (! curval)
return grub_errno;
curval->datasize = sizeof (guid);
curval->data = grub_malloc (curval->datasize);
if (! curval->data)
return grub_error (GRUB_ERR_OUT_OF_MEMORY,
"couldn't create device tree");
grub_memcpy (curval->data, &guid, curval->datasize);
/* The value "table". */
curval = grub_xnu_create_value (&(curkey->first_child), "table");
if (! curval)
return grub_errno;
curval->datasize = SIZEOF_OF_UINTN;
curval->data = grub_malloc (curval->datasize);
if (! curval->data)
return grub_error (GRUB_ERR_OUT_OF_MEMORY,
"couldn't create device tree");
if (SIZEOF_OF_UINTN == 4)
*((grub_uint32_t *)curval->data) = PTR_TO_UINT32 (ptr);
else
*((grub_uint64_t *)curval->data) = PTR_TO_UINT64 (ptr);
/* Create alias. */
for (j = 0; j < sizeof (table_aliases) / sizeof (table_aliases[0]); j++)
if (grub_memcmp (&table_aliases[j].guid, &guid, sizeof (guid)) == 0)
break;
if (j != sizeof (table_aliases) / sizeof (table_aliases[0]))
{
curval = grub_xnu_create_value (&(curkey->first_child), "alias");
if (!curval)
return grub_errno;
curval->datasize = grub_strlen (table_aliases[j].name) + 1;
curval->data = grub_malloc (curval->datasize);
if (!curval->data)
return grub_error (GRUB_ERR_OUT_OF_MEMORY,
"couldn't create device tree");
grub_memcpy (curval->data, table_aliases[j].name, curval->datasize);
}
}
/* Create and fill "runtime-services" key. */
runtimesrvkey = grub_xnu_create_key (&(efikey->first_child),
"runtime-services");
if (! runtimesrvkey)
return grub_errno;
curval = grub_xnu_create_value (&(runtimesrvkey->first_child), "table");
if (! curval)
return grub_errno;
curval->datasize = SIZEOF_OF_UINTN;
curval->data = grub_malloc (curval->datasize);
if (! curval->data)
return grub_error (GRUB_ERR_OUT_OF_MEMORY,
"couldn't create device tree");
if (SIZEOF_OF_UINTN == 4)
*((grub_uint32_t *) curval->data)
= PTR_TO_UINT32 (SYSTEM_TABLE_PTR (runtime_services));
else
*((grub_uint64_t *) curval->data)
= PTR_TO_UINT64 (SYSTEM_TABLE_PTR (runtime_services));
return GRUB_ERR_NONE;
}
/* Boot xnu. */
grub_err_t
grub_xnu_boot (void)
{
struct grub_xnu_boot_params *bootparams_relloc;
grub_off_t bootparams_relloc_off;
grub_off_t mmap_relloc_off;
grub_err_t err;
grub_efi_uintn_t memory_map_size = 0;
grub_efi_memory_descriptor_t *memory_map;
grub_efi_uintn_t map_key = 0;
grub_efi_uintn_t descriptor_size = 0;
grub_efi_uint32_t descriptor_version = 0;
grub_uint64_t firstruntimeaddr, lastruntimeaddr;
void *devtree;
grub_size_t devtreelen;
int i;
/* Page-align to avoid following parts to be inadvertently freed. */
err = grub_xnu_align_heap (GRUB_XNU_PAGESIZE);
if (err)
return err;
/* Pass memory map to kernel. */
memory_map_size = 0;
memory_map = 0;
map_key = 0;
descriptor_size = 0;
descriptor_version = 0;
if (grub_autoefi_get_memory_map (&memory_map_size, memory_map,
&map_key, &descriptor_size,
&descriptor_version) < 0)
return grub_errno;
memory_map = grub_xnu_heap_malloc (memory_map_size);
if (! memory_map)
return grub_errno;
if (grub_autoefi_get_memory_map (&memory_map_size, memory_map,
&map_key, &descriptor_size,
&descriptor_version) <= 0)
return grub_errno;
mmap_relloc_off = (grub_uint8_t *) memory_map
- (grub_uint8_t *) grub_xnu_heap_start;
firstruntimeaddr = (grub_uint64_t) (-1);
lastruntimeaddr = 0;
for (i = 0; (unsigned) i < memory_map_size / descriptor_size; i++)
{
grub_efi_memory_descriptor_t *curdesc = (grub_efi_memory_descriptor_t *)
((char *) memory_map + descriptor_size * i);
/* Some EFI implementations set physical_start to 0 which
causes XNU crash. */
curdesc->virtual_start = curdesc->physical_start;
if (curdesc->type == GRUB_EFI_RUNTIME_SERVICES_DATA
|| curdesc->type == GRUB_EFI_RUNTIME_SERVICES_CODE)
{
if (firstruntimeaddr > curdesc->physical_start)
firstruntimeaddr = curdesc->physical_start;
if (lastruntimeaddr < curdesc->physical_start
+ curdesc->num_pages * 4096)
lastruntimeaddr = curdesc->physical_start
+ curdesc->num_pages * 4096;
}
}
/* Relocate the boot parameters to heap. */
bootparams_relloc = grub_xnu_heap_malloc (sizeof (*bootparams_relloc));
if (! bootparams_relloc)
return grub_errno;
bootparams_relloc_off = (grub_uint8_t *) bootparams_relloc
- (grub_uint8_t *) grub_xnu_heap_start;
err = grub_xnu_writetree_toheap (&devtree, &devtreelen);
if (err)
return err;
bootparams_relloc = (struct grub_xnu_boot_params *)
(bootparams_relloc_off + (grub_uint8_t *) grub_xnu_heap_start);
grub_memcpy (bootparams_relloc->cmdline, grub_xnu_cmdline,
sizeof (bootparams_relloc->cmdline));
bootparams_relloc->devtree = ((char *) devtree - grub_xnu_heap_start)
+ grub_xnu_heap_will_be_at;
bootparams_relloc->devtreelen = devtreelen;
bootparams_relloc->heap_start = grub_xnu_heap_will_be_at;
bootparams_relloc->heap_size = grub_xnu_heap_size;
bootparams_relloc->efi_mmap = grub_xnu_heap_will_be_at + mmap_relloc_off;
bootparams_relloc->efi_mmap_size = memory_map_size;
bootparams_relloc->efi_mem_desc_size = descriptor_size;
bootparams_relloc->efi_mem_desc_version = descriptor_version;
bootparams_relloc->efi_runtime_first_page = firstruntimeaddr
/ GRUB_XNU_PAGESIZE;
bootparams_relloc->efi_runtime_npages
= ((lastruntimeaddr + GRUB_XNU_PAGESIZE - 1) / GRUB_XNU_PAGESIZE)
- (firstruntimeaddr / GRUB_XNU_PAGESIZE);
bootparams_relloc->efi_uintnbits = SIZEOF_OF_UINTN * 8;
bootparams_relloc->efi_system_table
= PTR_TO_UINT32 (grub_autoefi_system_table);
bootparams_relloc->verminor = GRUB_XNU_BOOTARGS_VERMINOR;
bootparams_relloc->vermajor = GRUB_XNU_BOOTARGS_VERMAJOR;
/* Parameters for asm helper. */
grub_xnu_stack = bootparams_relloc->heap_start
+ bootparams_relloc->heap_size + GRUB_XNU_PAGESIZE;
grub_xnu_arg1 = bootparams_relloc_off + grub_xnu_heap_will_be_at;
#ifndef GRUB_UTIL
grub_xnu_launch = (void (*) (void))
(grub_xnu_heap_start + grub_xnu_heap_size);
#endif
grub_dprintf ("xnu", "eip=%x\n", grub_xnu_entry_point);
grub_dprintf ("xnu", "launch=%p\n", grub_xnu_launch);
const char *debug = grub_env_get ("debug");
if (debug && (grub_strword (debug, "all") || grub_strword (debug, "xnu")))
{
grub_printf ("Press any key to launch xnu\n");
grub_getkey ();
}
/* Set video. */
err = grub_xnu_set_video (bootparams_relloc);
if (err != GRUB_ERR_NONE)
{
grub_print_error ();
grub_errno = GRUB_ERR_NONE;
grub_printf ("Booting in blind mode\n");
bootparams_relloc->lfb_mode = 0;
bootparams_relloc->lfb_width = 0;
bootparams_relloc->lfb_height = 0;
bootparams_relloc->lfb_depth = 0;
bootparams_relloc->lfb_line_len = 0;
bootparams_relloc->lfb_base = 0;
}
grub_memcpy (grub_xnu_heap_start + grub_xnu_heap_size,
grub_xnu_launcher_start,
grub_xnu_launcher_end - grub_xnu_launcher_start);
if (! grub_autoefi_finish_boot_services ())
return grub_error (GRUB_ERR_IO, "can't exit boot services");
grub_xnu_launch ();
/* Never reaches here. */
return 0;
}

192
loader/i386/xnu_helper.S Normal file
View file

@ -0,0 +1,192 @@
/*
* 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_xnu_launcher_start)
base:
cli
#ifndef __x86_64__
/* mov imm32, %eax */
.byte 0xb8
VARIABLE(grub_xnu_heap_will_be_at)
.long 0
mov %eax, %edi
/* mov imm32, %eax */
.byte 0xb8
VARIABLE(grub_xnu_heap_start)
.long 0
mov %eax, %esi
/* mov imm32, %ecx */
.byte 0xb9
VARIABLE(grub_xnu_heap_size)
.long 0
mov %edi, %eax
add %ecx, %eax
/* %rax now contains our starting position after relocation. */
/* One more page to copy: ourselves. */
add $0x403, %ecx
shr $2, %ecx
/* Forward copy. */
cld
rep
movsl
mov %eax, %esi
add $(cont0-base), %eax
jmp *%eax
cont0:
#else
xorq %rax, %rax
/* mov imm32, %eax */
.byte 0xb8
VARIABLE(grub_xnu_heap_will_be_at)
.long 0
mov %rax, %rdi
/* mov imm32, %rax */
.byte 0x48
.byte 0xb8
VARIABLE(grub_xnu_heap_start)
.long 0
.long 0
mov %rax, %rsi
/* mov imm32, %rcx */
.byte 0x48
.byte 0xb9
VARIABLE(grub_xnu_heap_size)
.long 0
.long 0
mov %rdi, %rax
add %rcx, %rax
/* %rax now contains our starting position after relocation. */
/* One more page to copy: ourselves. */
add $0x403, %rcx
shr $2, %rcx
/* Forward copy. */
cld
rep
movsl
mov %rax, %rsi
add $(cont0-base), %rax
jmp *%rax
cont0:
lea (cont1-base)(%rsi, 1), %rax
mov %eax, (jump_vector-base)(%rsi,1)
lea (gdt-base)(%rsi, 1), %rax
mov %rax, (gdt_addr-base)(%rsi,1)
/* Switch to compatibility mode. */
lgdt (gdtdesc-base)(%rsi,1)
/* Update %cs. Thanks to David Miller for pointing this mistake out. */
ljmp *(jump_vector-base)(%rsi,1)
cont1:
.code32
/* Update other registers. */
mov $0x18, %eax
mov %eax, %ds
mov %eax, %es
mov %eax, %fs
mov %eax, %gs
mov %eax, %ss
/* Disable paging. */
mov %cr0, %eax
and $0x7fffffff, %eax
mov %eax, %cr0
/* Disable amd64. */
mov $0xc0000080, %ecx
rdmsr
and $0xfffffeff, %eax
wrmsr
/* Turn off PAE. */
movl %cr4, %eax
and $0xffffffcf, %eax
mov %eax, %cr4
jmp cont2
cont2:
#endif
.code32
/* Registers on XNU boot: eip, esp and eax. */
/* mov imm32, %ecx */
.byte 0xb9
VARIABLE (grub_xnu_entry_point)
.long 0
/* mov imm32, %eax */
.byte 0xb8
VARIABLE (grub_xnu_arg1)
.long 0
/* mov imm32, %ebx */
.byte 0xbb
VARIABLE (grub_xnu_stack)
.long 0
movl %ebx, %esp
jmp *%ecx
#ifdef __x86_64__
/* GDT. Copied from loader/i386/linux.c. */
.p2align 4
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
gdtdesc:
.word 31
gdt_addr:
/* Filled by the code. */
.quad 0
.p2align 4
jump_vector:
/* Jump location. Is filled by the code */
.long 0
.long 0x10
#endif
VARIABLE(grub_xnu_launcher_end)

395
loader/macho.c Normal file
View file

@ -0,0 +1,395 @@
/* macho.c - load Mach-O files. */
/*
* 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/>.
*/
/* This Mach-O loader is incomplete and can load only non-relocatable segments.
This is however enough to boot xnu (otool -l and Mach-O specs for more info).
*/
#include <grub/err.h>
#include <grub/macho.h>
#include <grub/cpu/macho.h>
#include <grub/machoload.h>
#include <grub/file.h>
#include <grub/gzio.h>
#include <grub/misc.h>
#include <grub/mm.h>
#define min(a,b) (((a) < (b)) ? (a) : (b))
/* 32-bit. */
int
grub_macho_contains_macho32 (grub_macho_t macho)
{
return macho->offset32 != -1;
}
static void
grub_macho_parse32 (grub_macho_t macho)
{
struct grub_macho_header32 head;
/* Is there any candidate at all? */
if (macho->offset32 == -1)
return;
/* Read header and check magic*/
if (grub_file_seek (macho->file, macho->offset32) == (grub_off_t) -1
|| grub_file_read (macho->file, (char *) &head, sizeof (head))
!= sizeof(head))
{
grub_error (GRUB_ERR_READ_ERROR, "Cannot read Mach-O header.");
macho->offset32 = -1;
return;
}
if (head.magic != GRUB_MACHO_MAGIC32)
{
grub_error (GRUB_ERR_BAD_OS, "Invalid Mach-O 32-bit header.");
macho->offset32 = -1;
return;
}
/* Read commands. */
macho->ncmds32 = head.ncmds;
macho->cmdsize32 = head.sizeofcmds;
macho->cmds32 = grub_malloc(macho->cmdsize32);
if (! macho->cmds32)
{
grub_error (GRUB_ERR_OUT_OF_MEMORY, "not enough memory to read commands");
return;
}
if (grub_file_read (macho->file, (char *) macho->cmds32,
(grub_size_t) macho->cmdsize32)
!= (grub_ssize_t) macho->cmdsize32)
{
grub_error (GRUB_ERR_READ_ERROR, "Cannot read Mach-O header.");
macho->offset32 = -1;
}
}
typedef int NESTED_FUNC_ATTR (*grub_macho_iter_hook_t)
(grub_macho_t , struct grub_macho_cmd *,
void *);
static grub_err_t
grub_macho32_cmds_iterate (grub_macho_t macho,
grub_macho_iter_hook_t hook,
void *hook_arg)
{
grub_uint8_t *hdrs = macho->cmds32;
int i;
if (! macho->cmds32)
return grub_error (GRUB_ERR_BAD_OS, "Couldn't find 32-bit Mach-O");
for (i = 0; i < macho->ncmds32; i++)
{
struct grub_macho_cmd *hdr = (struct grub_macho_cmd *) hdrs;
if (hook (macho, hdr, hook_arg))
break;
hdrs += hdr->cmdsize;
}
return grub_errno;
}
grub_size_t
grub_macho32_filesize (grub_macho_t macho)
{
if (grub_macho_contains_macho32 (macho))
return macho->end32 - macho->offset32;
return 0;
}
grub_err_t
grub_macho32_readfile (grub_macho_t macho, void *dest)
{
grub_ssize_t read;
if (! grub_macho_contains_macho32 (macho))
return grub_error (GRUB_ERR_BAD_OS,
"Couldn't read arcitecture-specific part");
if (grub_file_seek (macho->file, macho->offset32) == (grub_off_t) -1)
{
grub_error_push ();
return grub_error (GRUB_ERR_BAD_OS,
"Invalid offset in program header.");
}
read = grub_file_read (macho->file, dest,
macho->end32 - macho->offset32);
if (read != (grub_ssize_t) (macho->end32 - macho->offset32))
{
grub_error_push ();
return grub_error (GRUB_ERR_BAD_OS,
"Couldn't read arcitecture-specific part");
}
return GRUB_ERR_NONE;
}
/* Calculate the amount of memory spanned by the segments. */
grub_err_t
grub_macho32_size (grub_macho_t macho, grub_addr_t *segments_start,
grub_addr_t *segments_end, int flags)
{
int nr_phdrs = 0;
/* Run through the program headers to calculate the total memory size we
should claim. */
auto int NESTED_FUNC_ATTR calcsize (grub_macho_t _macho,
struct grub_macho_cmd *phdr, void *_arg);
int NESTED_FUNC_ATTR calcsize (grub_macho_t UNUSED _macho,
struct grub_macho_cmd *hdr0, void UNUSED *_arg)
{
struct grub_macho_segment32 *hdr = (struct grub_macho_segment32 *) hdr0;
if (hdr->cmd != GRUB_MACHO_CMD_SEGMENT32)
return 0;
if (! hdr->filesize && (flags & GRUB_MACHO_NOBSS))
return 0;
nr_phdrs++;
if (hdr->vmaddr < *segments_start)
*segments_start = hdr->vmaddr;
if (hdr->vmaddr + hdr->vmsize > *segments_end)
*segments_end = hdr->vmaddr + hdr->vmsize;
return 0;
}
*segments_start = (grub_uint32_t) -1;
*segments_end = 0;
grub_macho32_cmds_iterate (macho, calcsize, 0);
if (nr_phdrs == 0)
return grub_error (GRUB_ERR_BAD_OS, "No program headers present");
if (*segments_end < *segments_start)
/* Very bad addresses. */
return grub_error (GRUB_ERR_BAD_OS, "Bad program header load addresses");
return GRUB_ERR_NONE;
}
/* Load every loadable segment into memory specified by `_load_hook'. */
grub_err_t
grub_macho32_load (grub_macho_t macho, char *offset, int flags)
{
grub_err_t err = 0;
auto int NESTED_FUNC_ATTR do_load(grub_macho_t _macho,
struct grub_macho_cmd *hdr0,
void UNUSED *_arg);
int NESTED_FUNC_ATTR do_load(grub_macho_t _macho,
struct grub_macho_cmd *hdr0,
void UNUSED *_arg)
{
struct grub_macho_segment32 *hdr = (struct grub_macho_segment32 *) hdr0;
if (hdr->cmd != GRUB_MACHO_CMD_SEGMENT32)
return 0;
if (! hdr->filesize && (flags & GRUB_MACHO_NOBSS))
return 0;
if (! hdr->vmsize)
return 0;
if (grub_file_seek (_macho->file, hdr->fileoff
+ _macho->offset32) == (grub_off_t) -1)
{
grub_error_push ();
grub_error (GRUB_ERR_BAD_OS,
"Invalid offset in program header.");
return 1;
}
if (hdr->filesize)
{
grub_ssize_t read;
read = grub_file_read (_macho->file, offset + hdr->vmaddr,
min (hdr->filesize, hdr->vmsize));
if (read != (grub_ssize_t) min (hdr->filesize, hdr->vmsize))
{
/* XXX How can we free memory from `load_hook'? */
grub_error_push ();
err=grub_error (GRUB_ERR_BAD_OS,
"Couldn't read segment from file: "
"wanted 0x%lx bytes; read 0x%lx bytes.",
hdr->filesize, read);
return 1;
}
}
if (hdr->filesize < hdr->vmsize)
grub_memset (offset + hdr->vmaddr + hdr->filesize,
0, hdr->vmsize - hdr->filesize);
return 0;
}
grub_macho32_cmds_iterate (macho, do_load, 0);
return err;
}
grub_uint32_t
grub_macho32_get_entry_point (grub_macho_t macho)
{
grub_uint32_t entry_point = 0;
auto int NESTED_FUNC_ATTR hook(grub_macho_t _macho,
struct grub_macho_cmd *hdr,
void UNUSED *_arg);
int NESTED_FUNC_ATTR hook(grub_macho_t UNUSED _macho,
struct grub_macho_cmd *hdr,
void UNUSED *_arg)
{
if (hdr->cmd == GRUB_MACHO_CMD_THREAD)
entry_point = ((struct grub_macho_thread32 *) hdr)->entry_point;
return 0;
}
grub_macho32_cmds_iterate (macho, hook, 0);
return entry_point;
}
grub_err_t
grub_macho_close (grub_macho_t macho)
{
grub_file_t file = macho->file;
grub_free (macho->cmds32);
grub_free (macho->cmds64);
grub_free (macho);
if (file)
grub_file_close (file);
return grub_errno;
}
grub_macho_t
grub_macho_file (grub_file_t file)
{
grub_macho_t macho;
union grub_macho_filestart filestart;
macho = grub_malloc (sizeof (*macho));
if (! macho)
return 0;
macho->file = file;
macho->offset32 = -1;
macho->offset64 = -1;
macho->end32 = -1;
macho->end64 = -1;
macho->cmds32 = 0;
macho->cmds64 = 0;
if (grub_file_seek (macho->file, 0) == (grub_off_t) -1)
goto fail;
if (grub_file_read (macho->file, (char *) &filestart, sizeof (filestart))
!= sizeof (filestart))
{
grub_error_push ();
grub_error (GRUB_ERR_READ_ERROR, "Cannot read Mach-O header.");
goto fail;
}
/* Is it a fat file? */
if (filestart.fat.magic == grub_be_to_cpu32 (GRUB_MACHO_FAT_MAGIC))
{
struct grub_macho_fat_arch *archs;
int i, narchs;
/* Load architecture description. */
narchs = grub_be_to_cpu32 (filestart.fat.nfat_arch);
if (grub_file_seek (macho->file, sizeof (struct grub_macho_fat_header))
== (grub_off_t) -1)
goto fail;
archs = grub_malloc (sizeof (struct grub_macho_fat_arch) * narchs);
if (!archs)
goto fail;
if (grub_file_read (macho->file, (char *) archs,
sizeof (struct grub_macho_fat_arch) * narchs)
!= (grub_ssize_t)sizeof(struct grub_macho_fat_arch) * narchs)
{
grub_free (archs);
grub_error_push ();
grub_error (GRUB_ERR_READ_ERROR, "Cannot read Mach-O header.");
goto fail;
}
for (i = 0; i < narchs; i++)
{
if (GRUB_MACHO_CPUTYPE_IS_HOST32
(grub_be_to_cpu32 (archs[i].cputype)))
{
macho->offset32 = grub_be_to_cpu32 (archs[i].offset);
macho->end32 = grub_be_to_cpu32 (archs[i].offset)
+ grub_be_to_cpu32 (archs[i].size);
}
if (GRUB_MACHO_CPUTYPE_IS_HOST64
(grub_be_to_cpu32 (archs[i].cputype)))
{
macho->offset64 = grub_be_to_cpu32 (archs[i].offset);
macho->end64 = grub_be_to_cpu32 (archs[i].offset)
+ grub_be_to_cpu32 (archs[i].size);
}
}
grub_free (archs);
}
/* Is it a thin 32-bit file? */
if (filestart.thin32.magic == GRUB_MACHO_MAGIC32)
{
macho->offset32 = 0;
macho->end32 = grub_file_size (file);
}
/* Is it a thin 64-bit file? */
if (filestart.thin64.magic == GRUB_MACHO_MAGIC64)
{
macho->offset64 = 0;
macho->end64 = grub_file_size (file);
}
grub_macho_parse32 (macho);
/* FIXME: implement 64-bit.*/
/* grub_macho_parse64 (macho); */
return macho;
fail:
grub_macho_close (macho);
return 0;
}
grub_macho_t
grub_macho_open (const char *name)
{
grub_file_t file;
grub_macho_t macho;
file = grub_gzfile_open (name, 1);
if (! file)
return 0;
macho = grub_macho_file (file);
if (! macho)
grub_file_close (file);
return macho;
}

1368
loader/xnu.c Normal file

File diff suppressed because it is too large Load diff

136
loader/xnu_resume.c Normal file
View file

@ -0,0 +1,136 @@
/*
* 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/normal.h>
#include <grub/dl.h>
#include <grub/file.h>
#include <grub/disk.h>
#include <grub/misc.h>
#include <grub/xnu.h>
#include <grub/cpu/xnu.h>
#include <grub/mm.h>
#include <grub/loader.h>
static void *grub_xnu_hibernate_image;
static grub_err_t
grub_xnu_resume_unload (void)
{
/* Free loaded image */
if (grub_xnu_hibernate_image)
grub_free (grub_xnu_hibernate_image);
grub_xnu_hibernate_image = 0;
grub_xnu_unlock ();
return GRUB_ERR_NONE;
}
grub_err_t
grub_xnu_resume (char *imagename)
{
grub_file_t file;
grub_size_t total_header_size;
struct grub_xnu_hibernate_header hibhead;
char *buf, *codetmp;
grub_uint32_t codedest;
grub_uint32_t codesize;
file = grub_file_open (imagename);
if (! file)
return 0;
/* Read the header. */
if (grub_file_read (file, (char *) &hibhead, sizeof (hibhead))
!=sizeof (hibhead))
{
grub_file_close (file);
return grub_error (GRUB_ERR_READ_ERROR,
"cannot read the hibernate header");
}
/* Check the header. */
if (hibhead.magic != GRUB_XNU_HIBERNATE_MAGIC)
{
grub_file_close (file);
return grub_error (GRUB_ERR_BAD_OS,
"hibernate header has incorrect magic number");
}
if (hibhead.encoffset)
{
grub_file_close (file);
return grub_error (GRUB_ERR_BAD_OS,
"encrypted images aren't supported yet");
}
codedest = hibhead.launchcode_target_page;
codedest *= GRUB_XNU_PAGESIZE;
codesize = hibhead.launchcode_numpages;
codesize *= GRUB_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 (grub_xnu_hibernate_image)
grub_free (grub_xnu_hibernate_image);
/* Try to allocate necessary space.
FIXME: mm isn't good enough yet to handle huge allocations.
*/
grub_xnu_hibernate_image = buf = grub_malloc (hibhead.image_size);
if (! buf)
{
grub_file_close (file);
return grub_error (GRUB_ERR_OUT_OF_MEMORY,
"not enough memory to load image");
}
/* Read image. */
if (grub_file_seek (file, 0) == (grub_off_t)-1
|| grub_file_read (file, buf, hibhead.image_size)
!= (grub_ssize_t) hibhead.image_size)
{
grub_file_close (file);
return grub_error (GRUB_ERR_READ_ERROR, "Cannot read resume image.");
}
grub_file_close (file);
codetmp = grub_memalign (GRUB_XNU_PAGESIZE, codesize + GRUB_XNU_PAGESIZE);
/* Setup variables needed by asm helper. */
grub_xnu_heap_will_be_at = codedest;
grub_xnu_heap_start = codetmp;
grub_xnu_heap_size = codesize;
grub_xnu_stack = (codedest + hibhead.stack);
grub_xnu_entry_point = (codedest + hibhead.entry_point);
grub_xnu_arg1 = (long) buf;
/* Prepare asm helper. */
grub_memcpy (codetmp, ((grub_uint8_t *) buf) + total_header_size, codesize);
grub_memcpy (codetmp + codesize, grub_xnu_launcher_start,
grub_xnu_launcher_end - grub_xnu_launcher_start);
/* We're ready now. */
grub_loader_set ((grub_err_t (*) (void)) (codetmp + codesize),
grub_xnu_resume_unload, 0);
/* Prevent module from unloading. */
grub_xnu_lock ();
return GRUB_ERR_NONE;
}