vaseboot/VasEBoot-core/loader/i386/xnu.c

1162 lines
32 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/env.h>
#include <VasEBoot/file.h>
#include <VasEBoot/disk.h>
#include <VasEBoot/xnu.h>
#include <VasEBoot/cpu/xnu.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/loader.h>
#include <VasEBoot/autoefi.h>
#include <VasEBoot/i386/tsc.h>
#include <VasEBoot/i386/cpuid.h>
#include <VasEBoot/efi/api.h>
#include <VasEBoot/i386/pit.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/charset.h>
#include <VasEBoot/term.h>
#include <VasEBoot/command.h>
#include <VasEBoot/i18n.h>
#include <VasEBoot/bitmap_scale.h>
#include <VasEBoot/cpu/io.h>
#include <VasEBoot/random.h>
#define min(a,b) (((a) < (b)) ? (a) : (b))
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define DEFAULT_VIDEO_MODE "auto"
char VasEBoot_xnu_cmdline[1024];
VasEBoot_uint32_t VasEBoot_xnu_entry_point, VasEBoot_xnu_arg1, VasEBoot_xnu_stack;
/* Aliases set for some tables. */
struct tbl_alias
{
VasEBoot_guid_t guid;
const char *name;
};
static struct tbl_alias table_aliases[] =
{
{VAS_EBOOT_EFI_ACPI_20_TABLE_GUID, "ACPI_20"},
{VAS_EBOOT_EFI_ACPI_TABLE_GUID, "ACPI"},
};
struct VasEBoot_xnu_devprop_device_descriptor
{
struct VasEBoot_xnu_devprop_device_descriptor *next;
struct VasEBoot_xnu_devprop_device_descriptor **prev;
struct property_descriptor *properties;
struct VasEBoot_efi_device_path *path;
int pathlen;
};
static int
utf16_strlen (VasEBoot_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 VasEBoot_uint64_t
readfrequency (const char *str)
{
VasEBoot_uint64_t num = 0;
int mul = 1000000;
int found = 0;
while (*str)
{
unsigned long digit;
digit = VasEBoot_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 = VasEBoot_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 VasEBoot_uint64_t
guessfsb (void)
{
const VasEBoot_uint64_t sane_value = 100000000;
VasEBoot_uint32_t manufacturer[3], max_cpuid, capabilities, msrlow;
VasEBoot_uint32_t a, b, d, divisor;
if (! VasEBoot_cpu_is_cpuid_supported ())
return sane_value;
VasEBoot_cpuid (0, max_cpuid, manufacturer[0], manufacturer[2], manufacturer[1]);
/* Only Intel for now is done. */
if (VasEBoot_memcmp (manufacturer, "GenuineIntel", 12) != 0)
return sane_value;
/* Check Speedstep. */
if (max_cpuid < 1)
return sane_value;
VasEBoot_cpuid (1, a, b, capabilities, d);
if (! (capabilities & (1 << 7)))
return sane_value;
/* Read the multiplier. */
asm volatile ("movl $0x198, %%ecx\n"
"rdmsr"
: "=d" (msrlow)
:
: "%ecx", "%eax");
VasEBoot_uint64_t v;
VasEBoot_uint32_t r;
/* (2000ULL << 32) / VasEBoot_tsc_rate */
/* Assumption: TSC frequency is over 2 MHz. */
v = 0xffffffff / VasEBoot_tsc_rate;
v *= 2000;
/* v is at most 2000 off from (2000ULL << 32) / VasEBoot_tsc_rate.
Since VasEBoot_tsc_rate < 2^32/2^11=2^21, so no overflow.
*/
r = (2000ULL << 32) - v * VasEBoot_tsc_rate;
v += r / VasEBoot_tsc_rate;
divisor = ((msrlow >> 7) & 0x3e) | ((msrlow >> 14) & 1);
if (divisor == 0)
return sane_value;
return VasEBoot_divmod64 (v, divisor, 0);
}
struct property_descriptor
{
struct property_descriptor *next;
struct property_descriptor **prev;
VasEBoot_uint8_t *name;
VasEBoot_uint16_t *name16;
int name16len;
int length;
void *data;
};
static struct VasEBoot_xnu_devprop_device_descriptor *devices = 0;
VasEBoot_err_t
VasEBoot_xnu_devprop_remove_property (struct VasEBoot_xnu_devprop_device_descriptor *dev,
char *name)
{
struct property_descriptor *prop;
prop = VasEBoot_named_list_find (VAS_EBOOT_AS_NAMED_LIST (dev->properties), name);
if (!prop)
return VAS_EBOOT_ERR_NONE;
VasEBoot_free (prop->name);
VasEBoot_free (prop->name16);
VasEBoot_free (prop->data);
VasEBoot_list_remove (VAS_EBOOT_AS_LIST (prop));
return VAS_EBOOT_ERR_NONE;
}
VasEBoot_err_t
VasEBoot_xnu_devprop_remove_device (struct VasEBoot_xnu_devprop_device_descriptor *dev)
{
void *t;
struct property_descriptor *prop;
VasEBoot_list_remove (VAS_EBOOT_AS_LIST (dev));
for (prop = dev->properties; prop; )
{
VasEBoot_free (prop->name);
VasEBoot_free (prop->name16);
VasEBoot_free (prop->data);
t = prop;
prop = prop->next;
VasEBoot_free (t);
}
VasEBoot_free (dev->path);
VasEBoot_free (dev);
return VAS_EBOOT_ERR_NONE;
}
struct VasEBoot_xnu_devprop_device_descriptor *
VasEBoot_xnu_devprop_add_device (struct VasEBoot_efi_device_path *path, int length)
{
struct VasEBoot_xnu_devprop_device_descriptor *ret;
ret = VasEBoot_zalloc (sizeof (*ret));
if (!ret)
return 0;
ret->path = VasEBoot_malloc (length);
if (!ret->path)
{
VasEBoot_free (ret);
return 0;
}
ret->pathlen = length;
VasEBoot_memcpy (ret->path, path, length);
VasEBoot_list_push (VAS_EBOOT_AS_LIST_P (&devices), VAS_EBOOT_AS_LIST (ret));
return ret;
}
static VasEBoot_err_t
VasEBoot_xnu_devprop_add_property (struct VasEBoot_xnu_devprop_device_descriptor *dev,
VasEBoot_uint8_t *utf8, VasEBoot_uint16_t *utf16,
int utf16len, void *data, int datalen)
{
struct property_descriptor *prop;
prop = VasEBoot_malloc (sizeof (*prop));
if (!prop)
return VasEBoot_errno;
prop->data = VasEBoot_malloc (datalen);
if (!prop->data)
{
VasEBoot_free (prop);
return VasEBoot_errno;
}
VasEBoot_memcpy (prop->data, data, datalen);
prop->name = utf8;
prop->name16 = utf16;
prop->name16len = utf16len;
prop->length = datalen;
VasEBoot_list_push (VAS_EBOOT_AS_LIST_P (&dev->properties),
VAS_EBOOT_AS_LIST (prop));
return VAS_EBOOT_ERR_NONE;
}
VasEBoot_err_t
VasEBoot_xnu_devprop_add_property_utf8 (struct VasEBoot_xnu_devprop_device_descriptor *dev,
char *name, void *data, int datalen)
{
VasEBoot_uint8_t *utf8;
VasEBoot_uint16_t *utf16;
int len, utf16len;
VasEBoot_err_t err;
utf8 = (VasEBoot_uint8_t *) VasEBoot_strdup (name);
if (!utf8)
return VasEBoot_errno;
len = VasEBoot_strlen (name);
utf16 = VasEBoot_calloc (len, sizeof (VasEBoot_uint16_t));
if (!utf16)
{
VasEBoot_free (utf8);
return VasEBoot_errno;
}
utf16len = VasEBoot_utf8_to_utf16 (utf16, len, utf8, len, NULL);
if (utf16len < 0)
{
VasEBoot_free (utf8);
VasEBoot_free (utf16);
return VasEBoot_errno;
}
err = VasEBoot_xnu_devprop_add_property (dev, utf8, utf16,
utf16len, data, datalen);
if (err)
{
VasEBoot_free (utf8);
VasEBoot_free (utf16);
return err;
}
return VAS_EBOOT_ERR_NONE;
}
VasEBoot_err_t
VasEBoot_xnu_devprop_add_property_utf16 (struct VasEBoot_xnu_devprop_device_descriptor *dev,
VasEBoot_uint16_t *name, int namelen,
void *data, int datalen)
{
VasEBoot_uint8_t *utf8;
VasEBoot_uint16_t *utf16;
VasEBoot_err_t err;
utf16 = VasEBoot_calloc (namelen, sizeof (VasEBoot_uint16_t));
if (!utf16)
return VasEBoot_errno;
VasEBoot_memcpy (utf16, name, sizeof (VasEBoot_uint16_t) * namelen);
utf8 = VasEBoot_malloc (namelen * 4 + 1);
if (!utf8)
{
VasEBoot_free (utf16);
return VasEBoot_errno;
}
*VasEBoot_utf16_to_utf8 ((VasEBoot_uint8_t *) utf8, name, namelen) = '\0';
err = VasEBoot_xnu_devprop_add_property (dev, utf8, utf16,
namelen, data, datalen);
if (err)
{
VasEBoot_free (utf8);
VasEBoot_free (utf16);
return err;
}
return VAS_EBOOT_ERR_NONE;
}
void
VasEBoot_cpu_xnu_unload (void)
{
struct VasEBoot_xnu_devprop_device_descriptor *dev1, *dev2;
for (dev1 = devices; dev1; )
{
dev2 = dev1->next;
VasEBoot_xnu_devprop_remove_device (dev1);
dev1 = dev2;
}
}
static VasEBoot_err_t
VasEBoot_cpu_xnu_fill_devprop (void)
{
struct VasEBoot_xnu_devtree_key *efikey;
int total_length = sizeof (struct VasEBoot_xnu_devprop_header);
struct VasEBoot_xnu_devtree_key *devprop;
struct VasEBoot_xnu_devprop_device_descriptor *device;
void *ptr;
struct VasEBoot_xnu_devprop_header *head;
void *t;
int numdevs = 0;
/* The key "efi". */
efikey = VasEBoot_xnu_create_key (&VasEBoot_xnu_devtree_root, "efi");
if (! efikey)
return VasEBoot_errno;
for (device = devices; device; device = device->next)
{
struct property_descriptor *propdesc;
total_length += sizeof (struct VasEBoot_xnu_devprop_device_header);
total_length += device->pathlen;
for (propdesc = device->properties; propdesc; propdesc = propdesc->next)
{
total_length += sizeof (VasEBoot_uint32_t);
total_length += sizeof (VasEBoot_uint16_t)
* (propdesc->name16len + 1);
total_length += sizeof (VasEBoot_uint32_t);
total_length += propdesc->length;
}
numdevs++;
}
devprop = VasEBoot_xnu_create_value (&(efikey->first_child), "device-properties");
if (!devprop)
return VasEBoot_errno;
devprop->data = VasEBoot_malloc (total_length);
devprop->datasize = total_length;
ptr = devprop->data;
head = ptr;
ptr = head + 1;
head->length = total_length;
head->alwaysone = 1;
head->num_devices = numdevs;
for (device = devices; device; )
{
struct VasEBoot_xnu_devprop_device_header *devhead;
struct property_descriptor *propdesc;
devhead = ptr;
devhead->num_values = 0;
ptr = devhead + 1;
VasEBoot_memcpy (ptr, device->path, device->pathlen);
ptr = (char *) ptr + device->pathlen;
for (propdesc = device->properties; propdesc; )
{
VasEBoot_uint32_t *len;
VasEBoot_uint16_t *name;
void *data;
len = ptr;
*len = 2 * propdesc->name16len + sizeof (VasEBoot_uint16_t)
+ sizeof (VasEBoot_uint32_t);
ptr = len + 1;
name = ptr;
VasEBoot_memcpy (name, propdesc->name16, 2 * propdesc->name16len);
name += propdesc->name16len;
/* NUL terminator. */
*name = 0;
ptr = name + 1;
len = ptr;
*len = propdesc->length + sizeof (VasEBoot_uint32_t);
data = len + 1;
ptr = data;
VasEBoot_memcpy (ptr, propdesc->data, propdesc->length);
ptr = (char *) ptr + propdesc->length;
VasEBoot_free (propdesc->name);
VasEBoot_free (propdesc->name16);
VasEBoot_free (propdesc->data);
t = propdesc;
propdesc = propdesc->next;
VasEBoot_free (t);
devhead->num_values++;
}
devhead->length = (char *) ptr - (char *) devhead;
t = device;
device = device->next;
VasEBoot_free (t);
}
devices = 0;
return VAS_EBOOT_ERR_NONE;
}
static VasEBoot_err_t
VasEBoot_cmd_devprop_load (VasEBoot_command_t cmd __attribute__ ((unused)),
int argc, char *args[])
{
VasEBoot_file_t file;
void *buf, *bufstart, *bufend;
struct VasEBoot_xnu_devprop_header *head;
VasEBoot_size_t size;
unsigned i, j;
if (argc != 1)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("filename expected"));
file = VasEBoot_file_open (args[0], VAS_EBOOT_FILE_XNU_DEVPROP);
if (! file)
return VasEBoot_errno;
size = VasEBoot_file_size (file);
buf = VasEBoot_malloc (size);
if (!buf)
{
VasEBoot_file_close (file);
return VasEBoot_errno;
}
if (VasEBoot_file_read (file, buf, size) != (VasEBoot_ssize_t) size)
{
VasEBoot_file_close (file);
return VasEBoot_errno;
}
VasEBoot_file_close (file);
bufstart = buf;
bufend = (char *) buf + size;
head = buf;
buf = head + 1;
for (i = 0; i < VasEBoot_le_to_cpu32 (head->num_devices) && buf < bufend; i++)
{
struct VasEBoot_efi_device_path *dp, *dpstart;
struct VasEBoot_xnu_devprop_device_descriptor *dev;
struct VasEBoot_xnu_devprop_device_header *devhead;
devhead = buf;
buf = devhead + 1;
dp = dpstart = buf;
while (VAS_EBOOT_EFI_DEVICE_PATH_VALID (dp) && buf < bufend)
{
buf = (char *) buf + VAS_EBOOT_EFI_DEVICE_PATH_LENGTH (dp);
if (VAS_EBOOT_EFI_END_ENTIRE_DEVICE_PATH (dp))
break;
dp = buf;
}
dev = VasEBoot_xnu_devprop_add_device (dpstart, (char *) buf
- (char *) dpstart);
for (j = 0; j < VasEBoot_le_to_cpu32 (devhead->num_values) && buf < bufend;
j++)
{
VasEBoot_uint32_t *namelen;
VasEBoot_uint32_t *datalen;
VasEBoot_uint16_t *utf16;
void *data;
VasEBoot_err_t err;
namelen = buf;
buf = namelen + 1;
if (buf >= bufend)
break;
utf16 = buf;
buf = (char *) buf + *namelen - sizeof (VasEBoot_uint32_t);
if (buf >= bufend)
break;
datalen = buf;
buf = datalen + 1;
if (buf >= bufend)
break;
data = buf;
buf = (char *) buf + *datalen - sizeof (VasEBoot_uint32_t);
if (buf >= bufend)
break;
err = VasEBoot_xnu_devprop_add_property_utf16
(dev, utf16, (*namelen - sizeof (VasEBoot_uint32_t)
- sizeof (VasEBoot_uint16_t)) / sizeof (VasEBoot_uint16_t),
data, *datalen - sizeof (VasEBoot_uint32_t));
if (err)
{
VasEBoot_free (bufstart);
return err;
}
}
}
VasEBoot_free (bufstart);
return VAS_EBOOT_ERR_NONE;
}
/* Fill device tree. */
/* FIXME: some entries may be platform-agnostic. Move them to loader/xnu.c. */
static VasEBoot_err_t
VasEBoot_cpu_xnu_fill_devicetree (VasEBoot_uint64_t *fsbfreq_out)
{
struct VasEBoot_xnu_devtree_key *efikey;
struct VasEBoot_xnu_devtree_key *chosenkey;
struct VasEBoot_xnu_devtree_key *cfgtablekey;
struct VasEBoot_xnu_devtree_key *curval;
struct VasEBoot_xnu_devtree_key *runtimesrvkey;
struct VasEBoot_xnu_devtree_key *platformkey;
unsigned i, j;
VasEBoot_err_t err;
chosenkey = VasEBoot_xnu_create_key (&VasEBoot_xnu_devtree_root, "chosen");
if (! chosenkey)
return VasEBoot_errno;
/* Random seed. */
curval = VasEBoot_xnu_create_value (&(chosenkey->first_child), "random-seed");
if (! curval)
return VasEBoot_errno;
curval->datasize = 64;
curval->data = VasEBoot_malloc (curval->datasize);
if (! curval->data)
return VasEBoot_errno;
/* Our random is not peer-reviewed but xnu uses this seed only for
ASLR in kernel. */
err = VasEBoot_crypto_get_random (curval->data, curval->datasize);
if (err)
return err;
/* The value "model". */
/* FIXME: may this value be sometimes different? */
curval = VasEBoot_xnu_create_value (&VasEBoot_xnu_devtree_root, "model");
if (! curval)
return VasEBoot_errno;
curval->datasize = sizeof ("ACPI");
curval->data = VasEBoot_strdup ("ACPI");
curval = VasEBoot_xnu_create_value (&VasEBoot_xnu_devtree_root, "compatible");
if (! curval)
return VasEBoot_errno;
curval->datasize = sizeof ("ACPI");
curval->data = VasEBoot_strdup ("ACPI");
/* The key "efi". */
efikey = VasEBoot_xnu_create_key (&VasEBoot_xnu_devtree_root, "efi");
if (! efikey)
return VasEBoot_errno;
/* Information about firmware. */
curval = VasEBoot_xnu_create_value (&(efikey->first_child), "firmware-revision");
if (! curval)
return VasEBoot_errno;
curval->datasize = (SYSTEM_TABLE_SIZEOF (firmware_revision));
curval->data = VasEBoot_malloc (curval->datasize);
if (! curval->data)
return VasEBoot_errno;
VasEBoot_memcpy (curval->data, (SYSTEM_TABLE_VAR(firmware_revision)),
curval->datasize);
curval = VasEBoot_xnu_create_value (&(efikey->first_child), "firmware-vendor");
if (! curval)
return VasEBoot_errno;
curval->datasize =
2 * (utf16_strlen (SYSTEM_TABLE_PTR (firmware_vendor)) + 1);
curval->data = VasEBoot_malloc (curval->datasize);
if (! curval->data)
return VasEBoot_errno;
VasEBoot_memcpy (curval->data, SYSTEM_TABLE_PTR (firmware_vendor),
curval->datasize);
curval = VasEBoot_xnu_create_value (&(efikey->first_child), "firmware-abi");
if (! curval)
return VasEBoot_errno;
curval->datasize = sizeof ("EFI32");
curval->data = VasEBoot_malloc (curval->datasize);
if (! curval->data)
return VasEBoot_errno;
if (SIZEOF_OF_UINTN == 4)
VasEBoot_memcpy (curval->data, "EFI32", curval->datasize);
else
VasEBoot_memcpy (curval->data, "EFI64", curval->datasize);
/* The key "platform". */
platformkey = VasEBoot_xnu_create_key (&(efikey->first_child),
"platform");
if (! platformkey)
return VasEBoot_errno;
/* Pass FSB frequency to the kernel. */
curval = VasEBoot_xnu_create_value (&(platformkey->first_child), "FSBFrequency");
if (! curval)
return VasEBoot_errno;
curval->datasize = sizeof (VasEBoot_uint64_t);
curval->data = VasEBoot_malloc (curval->datasize);
if (!curval->data)
return VasEBoot_errno;
/* First see if user supplies the value. */
const char *fsbvar = VasEBoot_env_get ("fsb");
VasEBoot_uint64_t fsbfreq = 0;
if (fsbvar)
fsbfreq = readfrequency (fsbvar);
/* Try autodetect. */
if (! fsbfreq)
fsbfreq = guessfsb ();
*((VasEBoot_uint64_t *) curval->data) = fsbfreq;
*fsbfreq_out = fsbfreq;
VasEBoot_dprintf ("xnu", "fsb autodetected as %llu\n",
(unsigned long long) *((VasEBoot_uint64_t *) curval->data));
cfgtablekey = VasEBoot_xnu_create_key (&(efikey->first_child),
"configuration-table");
if (!cfgtablekey)
return VasEBoot_errno;
/* Fill "configuration-table" key. */
for (i = 0; i < SYSTEM_TABLE (num_table_entries); i++)
{
void *ptr;
struct VasEBoot_xnu_devtree_key *curkey;
VasEBoot_packed_guid_t guid;
char guidbuf[64];
/* Retrieve current key. */
#ifdef VAS_EBOOT_MACHINE_EFI
{
ptr = (void *)
VasEBoot_efi_system_table->configuration_table[i].vendor_table;
guid = VasEBoot_efi_system_table->configuration_table[i].vendor_guid;
}
#else
if (SIZEOF_OF_UINTN == 4)
{
ptr = (void *) (VasEBoot_addr_t) ((VasEBoot_efiemu_configuration_table32_t *)
SYSTEM_TABLE_PTR (configuration_table))[i]
.vendor_table;
guid =
((VasEBoot_efiemu_configuration_table32_t *)
SYSTEM_TABLE_PTR (configuration_table))[i].vendor_guid;
}
else
{
ptr = (void *) (VasEBoot_addr_t) ((VasEBoot_efiemu_configuration_table64_t *)
SYSTEM_TABLE_PTR (configuration_table))[i]
.vendor_table;
guid =
((VasEBoot_efiemu_configuration_table64_t *)
SYSTEM_TABLE_PTR (configuration_table))[i].vendor_guid;
}
#endif
/* The name of key for new table. */
VasEBoot_snprintf (guidbuf, sizeof (guidbuf), "%pG", &guid);
/* 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 = VasEBoot_xnu_create_key (&(cfgtablekey->first_child), guidbuf);
if (! curkey)
return VasEBoot_errno;
curval = VasEBoot_xnu_create_value (&(curkey->first_child), "guid");
if (! curval)
return VasEBoot_errno;
curval->datasize = sizeof (guid);
curval->data = VasEBoot_malloc (curval->datasize);
if (! curval->data)
return VasEBoot_errno;
VasEBoot_memcpy (curval->data, &guid, curval->datasize);
/* The value "table". */
curval = VasEBoot_xnu_create_value (&(curkey->first_child), "table");
if (! curval)
return VasEBoot_errno;
curval->datasize = SIZEOF_OF_UINTN;
curval->data = VasEBoot_malloc (curval->datasize);
if (! curval->data)
return VasEBoot_errno;
if (SIZEOF_OF_UINTN == 4)
*((VasEBoot_uint32_t *) curval->data) = (VasEBoot_addr_t) ptr;
else
*((VasEBoot_uint64_t *) curval->data) = (VasEBoot_addr_t) ptr;
/* Create alias. */
for (j = 0; j < ARRAY_SIZE(table_aliases); j++)
if (VasEBoot_memcmp (&table_aliases[j].guid, &guid, sizeof (guid)) == 0)
break;
if (j != ARRAY_SIZE(table_aliases))
{
curval = VasEBoot_xnu_create_value (&(curkey->first_child), "alias");
if (!curval)
return VasEBoot_errno;
curval->datasize = VasEBoot_strlen (table_aliases[j].name) + 1;
curval->data = VasEBoot_malloc (curval->datasize);
if (!curval->data)
return VasEBoot_errno;
VasEBoot_memcpy (curval->data, table_aliases[j].name, curval->datasize);
}
}
/* Create and fill "runtime-services" key. */
runtimesrvkey = VasEBoot_xnu_create_key (&(efikey->first_child),
"runtime-services");
if (! runtimesrvkey)
return VasEBoot_errno;
curval = VasEBoot_xnu_create_value (&(runtimesrvkey->first_child), "table");
if (! curval)
return VasEBoot_errno;
curval->datasize = SIZEOF_OF_UINTN;
curval->data = VasEBoot_malloc (curval->datasize);
if (! curval->data)
return VasEBoot_errno;
if (SIZEOF_OF_UINTN == 4)
*((VasEBoot_uint32_t *) curval->data)
= (VasEBoot_addr_t) SYSTEM_TABLE_PTR (runtime_services);
else
*((VasEBoot_uint64_t *) curval->data)
= (VasEBoot_addr_t) SYSTEM_TABLE_PTR (runtime_services);
return VAS_EBOOT_ERR_NONE;
}
VasEBoot_err_t
VasEBoot_xnu_boot_resume (void)
{
struct VasEBoot_relocator32_state state = {0};
state.esp = VasEBoot_xnu_stack;
state.ebp = VasEBoot_xnu_stack;
state.eip = VasEBoot_xnu_entry_point;
state.eax = VasEBoot_xnu_arg1;
return VasEBoot_relocator32_boot (VasEBoot_xnu_relocator, state, 0);
}
/* Setup video for xnu. */
static VasEBoot_err_t
VasEBoot_xnu_set_video (struct VasEBoot_xnu_boot_params_common *params)
{
struct VasEBoot_video_mode_info mode_info;
char *tmp;
const char *modevar;
void *framebuffer;
VasEBoot_err_t err;
struct VasEBoot_video_bitmap *bitmap = NULL;
modevar = VasEBoot_env_get ("gfxpayload");
/* Consider only graphical 32-bit deep modes. */
if (! modevar || *modevar == 0)
err = VasEBoot_video_set_mode (DEFAULT_VIDEO_MODE,
VAS_EBOOT_VIDEO_MODE_TYPE_PURE_TEXT
| VAS_EBOOT_VIDEO_MODE_TYPE_DEPTH_MASK,
32 << VAS_EBOOT_VIDEO_MODE_TYPE_DEPTH_POS);
else
{
tmp = VasEBoot_xasprintf ("%s;" DEFAULT_VIDEO_MODE, modevar);
if (! tmp)
return VasEBoot_errno;
err = VasEBoot_video_set_mode (tmp,
VAS_EBOOT_VIDEO_MODE_TYPE_PURE_TEXT
| VAS_EBOOT_VIDEO_MODE_TYPE_DEPTH_MASK,
32 << VAS_EBOOT_VIDEO_MODE_TYPE_DEPTH_POS);
VasEBoot_free (tmp);
}
if (err)
return err;
err = VasEBoot_video_get_info (&mode_info);
if (err)
return err;
if (VasEBoot_xnu_bitmap)
{
if (VasEBoot_xnu_bitmap_mode == VAS_EBOOT_XNU_BITMAP_STRETCH)
err = VasEBoot_video_bitmap_create_scaled (&bitmap,
mode_info.width,
mode_info.height,
VasEBoot_xnu_bitmap,
VAS_EBOOT_VIDEO_BITMAP_SCALE_METHOD_BEST);
else
bitmap = VasEBoot_xnu_bitmap;
}
if (bitmap)
{
if (VasEBoot_xnu_bitmap_mode == VAS_EBOOT_XNU_BITMAP_STRETCH)
err = VasEBoot_video_bitmap_create_scaled (&bitmap,
mode_info.width,
mode_info.height,
VasEBoot_xnu_bitmap,
VAS_EBOOT_VIDEO_BITMAP_SCALE_METHOD_BEST);
else
bitmap = VasEBoot_xnu_bitmap;
}
if (bitmap)
{
int x, y;
x = mode_info.width - bitmap->mode_info.width;
x /= 2;
y = mode_info.height - bitmap->mode_info.height;
y /= 2;
err = VasEBoot_video_blit_bitmap (bitmap,
VAS_EBOOT_VIDEO_BLIT_REPLACE,
x > 0 ? x : 0,
y > 0 ? y : 0,
x < 0 ? -x : 0,
y < 0 ? -y : 0,
min (bitmap->mode_info.width,
mode_info.width),
min (bitmap->mode_info.height,
mode_info.height));
}
if (err)
{
VasEBoot_print_error ();
VasEBoot_errno = VAS_EBOOT_ERR_NONE;
bitmap = 0;
}
err = VasEBoot_video_get_info_and_fini (&mode_info, &framebuffer);
if (err)
return err;
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 = (VasEBoot_addr_t) framebuffer;
params->lfb_mode = bitmap ? VAS_EBOOT_XNU_VIDEO_SPLASH
: VAS_EBOOT_XNU_VIDEO_TEXT_IN_VIDEO;
return VAS_EBOOT_ERR_NONE;
}
static int
total_ram_hook (VasEBoot_uint64_t addr __attribute__ ((unused)), VasEBoot_uint64_t size,
VasEBoot_memory_type_t type,
void *data)
{
VasEBoot_uint64_t *result = data;
if (type != VAS_EBOOT_MEMORY_AVAILABLE)
return 0;
*result += size;
return 0;
}
static VasEBoot_uint64_t
get_total_ram (void)
{
VasEBoot_uint64_t result = 0;
VasEBoot_mmap_iterate (total_ram_hook, &result);
return result;
}
/* Boot xnu. */
VasEBoot_err_t
VasEBoot_xnu_boot (void)
{
union VasEBoot_xnu_boot_params_any *bootparams;
struct VasEBoot_xnu_boot_params_common *bootparams_common;
void *bp_in;
VasEBoot_addr_t bootparams_target;
VasEBoot_err_t err;
VasEBoot_efi_uintn_t memory_map_size = 0;
void *memory_map;
VasEBoot_addr_t memory_map_target;
VasEBoot_efi_uintn_t map_key = 0;
VasEBoot_efi_uintn_t descriptor_size = 0;
VasEBoot_efi_uint32_t descriptor_version = 0;
VasEBoot_uint64_t firstruntimepage, lastruntimepage;
VasEBoot_uint64_t curruntimepage;
VasEBoot_addr_t devtree_target;
VasEBoot_size_t devtreelen;
int i;
struct VasEBoot_relocator32_state state = {0};
VasEBoot_uint64_t fsbfreq = 100000000;
int v2 = (VasEBoot_xnu_darwin_version >= 11);
VasEBoot_uint32_t efi_system_table = 0;
err = VasEBoot_autoefi_prepare ();
if (err)
return err;
err = VasEBoot_cpu_xnu_fill_devprop ();
if (err)
return err;
err = VasEBoot_cpu_xnu_fill_devicetree (&fsbfreq);
if (err)
return err;
err = VasEBoot_xnu_fill_devicetree ();
if (err)
return err;
/* Page-align to avoid following parts to be inadvertently freed. */
err = VasEBoot_xnu_align_heap (VAS_EBOOT_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;
VasEBoot_dprintf ("xnu", "eip=%x, efi=%p\n", VasEBoot_xnu_entry_point,
VasEBoot_autoefi_system_table);
const char *debug = VasEBoot_env_get ("debug");
if (debug && (VasEBoot_strword (debug, "all") || VasEBoot_strword (debug, "xnu")))
{
VasEBoot_puts_ (N_("Press any key to launch xnu"));
VasEBoot_getkey ();
}
/* Relocate the boot parameters to heap. */
err = VasEBoot_xnu_heap_malloc (sizeof (*bootparams),
&bp_in, &bootparams_target);
if (err)
return err;
bootparams = bp_in;
VasEBoot_memset (bootparams, 0, sizeof (*bootparams));
if (v2)
{
bootparams_common = &bootparams->v2.common;
bootparams->v2.fsbfreq = fsbfreq;
bootparams->v2.ram_size = get_total_ram();
}
else
bootparams_common = &bootparams->v1.common;
/* Set video. */
err = VasEBoot_xnu_set_video (bootparams_common);
if (err != VAS_EBOOT_ERR_NONE)
{
VasEBoot_print_error ();
VasEBoot_errno = VAS_EBOOT_ERR_NONE;
VasEBoot_puts_ (N_("Booting in blind mode"));
bootparams_common->lfb_mode = 0;
bootparams_common->lfb_width = 0;
bootparams_common->lfb_height = 0;
bootparams_common->lfb_depth = 0;
bootparams_common->lfb_line_len = 0;
bootparams_common->lfb_base = 0;
}
if (VasEBoot_autoefi_get_memory_map (&memory_map_size, memory_map,
&map_key, &descriptor_size,
&descriptor_version) < 0)
return VasEBoot_errno;
/* We will do few allocations later. Reserve some space for possible
memory map growth. */
memory_map_size += 20 * descriptor_size;
err = VasEBoot_xnu_heap_malloc (memory_map_size,
&memory_map, &memory_map_target);
if (err)
return err;
err = VasEBoot_xnu_writetree_toheap (&devtree_target, &devtreelen);
if (err)
return err;
VasEBoot_memcpy (bootparams_common->cmdline, VasEBoot_xnu_cmdline,
sizeof (bootparams_common->cmdline));
bootparams_common->devtree = devtree_target;
bootparams_common->devtreelen = devtreelen;
err = VasEBoot_autoefi_finish_boot_services (&memory_map_size, memory_map,
&map_key, &descriptor_size,
&descriptor_version);
if (err)
return err;
if (v2)
bootparams->v2.efi_system_table = (VasEBoot_addr_t) VasEBoot_autoefi_system_table;
else
bootparams->v1.efi_system_table = (VasEBoot_addr_t) VasEBoot_autoefi_system_table;
firstruntimepage = (((VasEBoot_addr_t) VasEBoot_xnu_heap_target_start
+ VasEBoot_xnu_heap_size + VAS_EBOOT_XNU_PAGESIZE - 1)
/ VAS_EBOOT_XNU_PAGESIZE) + 20;
curruntimepage = firstruntimepage;
for (i = 0; (unsigned) i < memory_map_size / descriptor_size; i++)
{
VasEBoot_efi_memory_descriptor_t *curdesc = (VasEBoot_efi_memory_descriptor_t *)
((char *) memory_map + descriptor_size * i);
curdesc->virtual_start = curdesc->physical_start;
if (curdesc->type == VAS_EBOOT_EFI_RUNTIME_SERVICES_DATA
|| curdesc->type == VAS_EBOOT_EFI_RUNTIME_SERVICES_CODE)
{
curdesc->virtual_start = curruntimepage << 12;
curruntimepage += curdesc->num_pages;
if (curdesc->physical_start
<= (VasEBoot_addr_t) VasEBoot_autoefi_system_table
&& curdesc->physical_start + (curdesc->num_pages << 12)
> (VasEBoot_addr_t) VasEBoot_autoefi_system_table)
efi_system_table
= (VasEBoot_addr_t) VasEBoot_autoefi_system_table
- curdesc->physical_start + curdesc->virtual_start;
if (SIZEOF_OF_UINTN == 8 && VasEBoot_xnu_is_64bit)
curdesc->virtual_start |= 0xffffff8000000000ULL;
}
}
lastruntimepage = curruntimepage;
if (v2)
{
bootparams->v2.efi_uintnbits = SIZEOF_OF_UINTN * 8;
bootparams->v2.verminor = VAS_EBOOT_XNU_BOOTARGSV2_VERMINOR;
bootparams->v2.vermajor = VAS_EBOOT_XNU_BOOTARGSV2_VERMAJOR;
bootparams->v2.efi_system_table = efi_system_table;
}
else
{
bootparams->v1.efi_uintnbits = SIZEOF_OF_UINTN * 8;
bootparams->v1.verminor = VAS_EBOOT_XNU_BOOTARGSV1_VERMINOR;
bootparams->v1.vermajor = VAS_EBOOT_XNU_BOOTARGSV1_VERMAJOR;
bootparams->v1.efi_system_table = efi_system_table;
}
bootparams_common->efi_runtime_first_page = firstruntimepage;
bootparams_common->efi_runtime_npages = lastruntimepage - firstruntimepage;
bootparams_common->efi_mem_desc_size = descriptor_size;
bootparams_common->efi_mem_desc_version = descriptor_version;
bootparams_common->efi_mmap = memory_map_target;
bootparams_common->efi_mmap_size = memory_map_size;
bootparams_common->heap_start = VasEBoot_xnu_heap_target_start;
bootparams_common->heap_size = curruntimepage * VAS_EBOOT_XNU_PAGESIZE - VasEBoot_xnu_heap_target_start;
/* Parameters for asm helper. */
VasEBoot_xnu_stack = bootparams_common->heap_start
+ bootparams_common->heap_size + VAS_EBOOT_XNU_PAGESIZE;
VasEBoot_xnu_arg1 = bootparams_target;
VasEBoot_autoefi_set_virtual_address_map (memory_map_size, descriptor_size,
descriptor_version, memory_map);
state.eip = VasEBoot_xnu_entry_point;
state.eax = VasEBoot_xnu_arg1;
state.esp = VasEBoot_xnu_stack;
state.ebp = VasEBoot_xnu_stack;
/* XNU uses only APIC. Disable PIC. */
VasEBoot_outb (0xff, 0x21);
VasEBoot_outb (0xff, 0xa1);
return VasEBoot_relocator32_boot (VasEBoot_xnu_relocator, state, 0);
}
static VasEBoot_command_t cmd_devprop_load;
void
VasEBoot_cpu_xnu_init (void)
{
cmd_devprop_load = VasEBoot_register_command ("xnu_devprop_load",
VasEBoot_cmd_devprop_load,
/* TRANSLATORS: `device-properties'
is a variable name,
not a program. */
0, N_("Load `device-properties' dump."));
}
void
VasEBoot_cpu_xnu_fini (void)
{
VasEBoot_unregister_command (cmd_devprop_load);
}