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:
parent
5caf964d75
commit
bbee0f2b56
24 changed files with 3378 additions and 6 deletions
177
loader/i386/efi/xnu.c
Normal file
177
loader/i386/efi/xnu.c
Normal 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
80
loader/i386/pc/xnu.c
Normal 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
539
loader/i386/xnu.c
Normal 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
192
loader/i386/xnu_helper.S
Normal 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)
|
Loading…
Add table
Add a link
Reference in a new issue