Exemple #1
0
/* Load .kext. */
static grub_err_t
grub_xnu_load_driver (char *infoplistname, grub_file_t binaryfile)
{
  grub_macho_t macho;
  grub_err_t err;
  grub_file_t infoplist;
  struct grub_xnu_extheader *exthead;
  int neededspace = sizeof (*exthead);
  grub_uint8_t *buf;
  grub_size_t infoplistsize = 0, machosize = 0;
  char *name, *nameend;
  int namelen;

  name = get_name_ptr (infoplistname);
  nameend = grub_strchr (name, '/');

  if (nameend)
    namelen = nameend - name;
  else
    namelen = grub_strlen (name);

  neededspace += namelen + 1;

  if (! grub_xnu_heap_size)
    return grub_error (GRUB_ERR_BAD_OS, "no xnu kernel loaded");

  /* Compute the needed space. */
  if (binaryfile)
    {
      macho = grub_macho_file (binaryfile);
      if (! macho || ! grub_macho_contains_macho32 (macho))
	{
	  if (macho)
	    grub_macho_close (macho);
	  return grub_error (GRUB_ERR_BAD_OS,
			     "extension doesn't contain suitable architecture");
	}
      if (grub_xnu_is_64bit)
	machosize = grub_macho_filesize64 (macho);
      else
	machosize = grub_macho_filesize32 (macho);
      neededspace += machosize;
    }
  else
    macho = 0;

  if (infoplistname)
    infoplist = grub_gzfile_open (infoplistname, 1);
  else
    infoplist = 0;
  grub_errno = GRUB_ERR_NONE;
  if (infoplist)
    {
      infoplistsize = grub_file_size (infoplist);
      neededspace += infoplistsize + 1;
    }
  else
    infoplistsize = 0;

  /* Allocate the space. */
  err = grub_xnu_align_heap (GRUB_XNU_PAGESIZE);
  if (err)
    return err;
  buf = grub_xnu_heap_malloc (neededspace);

  exthead = (struct grub_xnu_extheader *) buf;
  grub_memset (exthead, 0, sizeof (*exthead));
  buf += sizeof (*exthead);

  /* Load the binary. */
  if (macho)
    {
      exthead->binaryaddr = (buf - (grub_uint8_t *) grub_xnu_heap_start)
	+ grub_xnu_heap_will_be_at;
      exthead->binarysize = machosize;
      if (grub_xnu_is_64bit)
	err = grub_macho_readfile64 (macho, buf);
      else
	err = grub_macho_readfile32 (macho, buf);
      if (err)
	{
	  grub_macho_close (macho);
	  return err;
	}
      grub_macho_close (macho);
      buf += machosize;
    }
  grub_errno = GRUB_ERR_NONE;

  /* Load the plist. */
  if (infoplist)
    {
      exthead->infoplistaddr = (buf - (grub_uint8_t *) grub_xnu_heap_start)
	+ grub_xnu_heap_will_be_at;
      exthead->infoplistsize = infoplistsize + 1;
      if (grub_file_read (infoplist, buf, infoplistsize)
	  != (grub_ssize_t) (infoplistsize))
	{
	  grub_file_close (infoplist);
	  grub_error_push ();
	  return grub_error (GRUB_ERR_BAD_OS, "couldn't read file %s: ",
			     infoplistname);
	}
      grub_file_close (infoplist);
      buf[infoplistsize] = 0;
      buf += infoplistsize + 1;
    }
  grub_errno = GRUB_ERR_NONE;

  exthead->nameaddr = (buf - (grub_uint8_t *) grub_xnu_heap_start)
    + grub_xnu_heap_will_be_at;
  exthead->namesize = namelen + 1;
  grub_memcpy (buf, name, namelen);
  buf[namelen] = 0;
  buf += namelen + 1;

  /* Announce to kernel */
  return grub_xnu_register_memory ("Driver-", &driversnum, exthead,
				   neededspace);
}
Exemple #2
0
static grub_err_t
grub_cmd_file (grub_extcmd_context_t ctxt, int argc, char **args)
{
  grub_file_t file = 0;
  grub_elf_t elf = 0;
  grub_err_t err;
  int type = -1, i;
  int ret = 0;
  grub_macho_t macho = 0;

  if (argc == 0)
    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  for (i = OPT_TYPE_MIN; i <= OPT_TYPE_MAX; i++)
    if (ctxt->state[i].set)
      {
	if (type == -1)
	  {
	    type = i;
	    continue;
	  }
	return grub_error (GRUB_ERR_BAD_ARGUMENT, "multiple types specified");
      }
  if (type == -1)
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "no type specified");

  file = grub_file_open (args[0]);
  if (!file)
    return grub_errno;
  switch (type)
    {
    case IS_BIOS_BOOTSECTOR:
      {
	grub_uint16_t sig;
	if (grub_file_size (file) != 512)
	  break;
	if (grub_file_seek (file, 510) == (grub_size_t) -1)
	  break;
	if (grub_file_read (file, &sig, 2) != 2)
	  break;
	if (sig != grub_cpu_to_le16_compile_time (0xaa55))
	  break;
	ret = 1;
	break;
      }
    case IS_IA64_LINUX:
      {
	Elf64_Ehdr ehdr;

	if (grub_file_read (file, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
	  break;

	if (ehdr.e_ident[EI_MAG0] != ELFMAG0
	    || ehdr.e_ident[EI_MAG1] != ELFMAG1
	    || ehdr.e_ident[EI_MAG2] != ELFMAG2
	    || ehdr.e_ident[EI_MAG3] != ELFMAG3
	    || ehdr.e_ident[EI_VERSION] != EV_CURRENT
	    || ehdr.e_version != EV_CURRENT)
	  break;

	if (ehdr.e_ident[EI_CLASS] != ELFCLASS64
	    || ehdr.e_ident[EI_DATA] != ELFDATA2LSB
	    || ehdr.e_machine != grub_cpu_to_le16_compile_time (EM_IA_64))
	  break;

	ret = 1;

	break;
      }

    case IS_SPARC64_LINUX:
      {
	Elf64_Ehdr ehdr;

	if (grub_file_read (file, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
	  break;

	if (ehdr.e_ident[EI_MAG0] != ELFMAG0
	    || ehdr.e_ident[EI_MAG1] != ELFMAG1
	    || ehdr.e_ident[EI_MAG2] != ELFMAG2
	    || ehdr.e_ident[EI_MAG3] != ELFMAG3
	    || ehdr.e_ident[EI_VERSION] != EV_CURRENT
	    || ehdr.e_version != EV_CURRENT)
	  break;

	if (ehdr.e_ident[EI_CLASS] != ELFCLASS64
	    || ehdr.e_ident[EI_DATA] != ELFDATA2MSB)
	  break;

	if (ehdr.e_machine != grub_cpu_to_le16_compile_time (EM_SPARCV9)
	    || ehdr.e_type != grub_cpu_to_be16_compile_time (ET_EXEC))
	  break;

	ret = 1;

	break;
      }

    case IS_POWERPC_LINUX:
      {
	Elf32_Ehdr ehdr;

	if (grub_file_read (file, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
	  break;

	if (ehdr.e_ident[EI_MAG0] != ELFMAG0
	    || ehdr.e_ident[EI_MAG1] != ELFMAG1
	    || ehdr.e_ident[EI_MAG2] != ELFMAG2
	    || ehdr.e_ident[EI_MAG3] != ELFMAG3
	    || ehdr.e_ident[EI_VERSION] != EV_CURRENT
	    || ehdr.e_version != EV_CURRENT)
	  break;

	if (ehdr.e_ident[EI_DATA] != ELFDATA2MSB
	    || (ehdr.e_machine != grub_cpu_to_le16_compile_time (EM_PPC)
		&& ehdr.e_machine !=
		grub_cpu_to_le16_compile_time (EM_PPC64)))
	  break;

	if (ehdr.e_type != grub_cpu_to_be16_compile_time (ET_EXEC)
	    && ehdr.e_type != grub_cpu_to_be16_compile_time (ET_DYN))
	  break;

	ret = 1;

	break;
      }

    case IS_MIPS_LINUX:
      {
	Elf32_Ehdr ehdr;

	if (grub_file_read (file, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
	  break;

	if (ehdr.e_ident[EI_MAG0] != ELFMAG0
	    || ehdr.e_ident[EI_MAG1] != ELFMAG1
	    || ehdr.e_ident[EI_MAG2] != ELFMAG2
	    || ehdr.e_ident[EI_MAG3] != ELFMAG3
	    || ehdr.e_ident[EI_VERSION] != EV_CURRENT
	    || ehdr.e_version != EV_CURRENT)
	  break;

	if (ehdr.e_ident[EI_DATA] != ELFDATA2MSB
	    || ehdr.e_machine != grub_cpu_to_be16_compile_time (EM_MIPS)
	    || ehdr.e_type != grub_cpu_to_be16_compile_time (ET_EXEC))
	  break;

	ret = 1;

	break;
      }

    case IS_X86_KNETBSD:
    case IS_X86_KNETBSD32:
    case IS_X86_KNETBSD64:
      {
	int is32, is64;

	elf = grub_elf_file (file, file->name);

	if (elf->ehdr.ehdr32.e_type != grub_cpu_to_le16_compile_time (ET_EXEC)
	    || elf->ehdr.ehdr32.e_ident[EI_DATA] != ELFDATA2LSB)
	  break;

	is32 = grub_elf_is_elf32 (elf);
	is64 = grub_elf_is_elf64 (elf);
	if (!is32 && !is64)
	  break;
	if (!is32 && type == IS_X86_KNETBSD32)
	  break;
	if (!is64 && type == IS_X86_KNETBSD64)
	  break;
	if (is64)
	  ret = grub_file_check_netbsd64 (elf);
	if (is32)
	  ret = grub_file_check_netbsd32 (elf);
	break;
      }

    case IS_X86_KFREEBSD:
    case IS_X86_KFREEBSD32:
    case IS_X86_KFREEBSD64:
      {
	Elf32_Ehdr ehdr;
	int is32, is64;

	if (grub_file_read (file, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
	  break;

	if (ehdr.e_ident[EI_MAG0] != ELFMAG0
	    || ehdr.e_ident[EI_MAG1] != ELFMAG1
	    || ehdr.e_ident[EI_MAG2] != ELFMAG2
	    || ehdr.e_ident[EI_MAG3] != ELFMAG3
	    || ehdr.e_ident[EI_VERSION] != EV_CURRENT
	    || ehdr.e_version != EV_CURRENT)
	  break;

	if (ehdr.e_type != grub_cpu_to_le16_compile_time (ET_EXEC)
	    || ehdr.e_ident[EI_DATA] != ELFDATA2LSB)
	  break;

	if (ehdr.e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
	  break;

	is32 = (ehdr.e_machine == grub_cpu_to_le16_compile_time (EM_386)
		&& ehdr.e_ident[EI_CLASS] == ELFCLASS32);
	is64 = (ehdr.e_machine == grub_cpu_to_le16_compile_time (EM_X86_64)
		&& ehdr.e_ident[EI_CLASS] == ELFCLASS64);
	if (!is32 && !is64)
	  break;
	if (!is32 && (type == IS_X86_KFREEBSD32 || type == IS_X86_KNETBSD32))
	  break;
	if (!is64 && (type == IS_X86_KFREEBSD64 || type == IS_X86_KNETBSD64))
	  break;
	ret = 1;

	break;
      }


    case IS_MIPSEL_LINUX:
      {
	Elf32_Ehdr ehdr;

	if (grub_file_read (file, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
	  break;

	if (ehdr.e_ident[EI_MAG0] != ELFMAG0
	    || ehdr.e_ident[EI_MAG1] != ELFMAG1
	    || ehdr.e_ident[EI_MAG2] != ELFMAG2
	    || ehdr.e_ident[EI_MAG3] != ELFMAG3
	    || ehdr.e_ident[EI_VERSION] != EV_CURRENT
	    || ehdr.e_version != EV_CURRENT)
	  break;

	if (ehdr.e_machine != grub_cpu_to_le16_compile_time (EM_MIPS)
	    || ehdr.e_type != grub_cpu_to_le16_compile_time (ET_EXEC))
	  break;

	ret = 1;

	break;
      }
    case IS_ARM_LINUX:
      {
	grub_uint32_t sig, sig_pi;
	if (grub_file_read (file, &sig_pi, 4) != 4)
	  break;
	/* Raspberry pi.  */
	if (sig_pi == grub_cpu_to_le32_compile_time (0xea000006))
	  {
	    ret = 1;
	    break;
	  }

	if (grub_file_seek (file, 0x24) == (grub_size_t) -1)
	  break;
	if (grub_file_read (file, &sig, 4) != 4)
	  break;
	if (sig == grub_cpu_to_le32_compile_time (0x016f2818))
	  {
	    ret = 1;
	    break;
	  }
	break;
      }
    case IS_ARM64_LINUX:
      {
	grub_uint32_t sig;

	if (grub_file_seek (file, 0x38) == (grub_size_t) -1)
	  break;
	if (grub_file_read (file, &sig, 4) != 4)
	  break;
	if (sig == grub_cpu_to_le32_compile_time (0x644d5241))
	  {
	    ret = 1;
	    break;
	  }
	break;
      }
    case IS_PAE_DOMU ... IS_DOM0:
      {
	struct grub_xen_file_info xen_inf;
	elf = grub_xen_file (file);
	if (!elf)
	  break;
	err = grub_xen_get_info (elf, &xen_inf);
	if (err)
	  break;
	/* Unfortuntely no way to check if kernel supports dom0.  */
	if (type == IS_DOM0)
	  ret = 1;
	if (type == IS_PAE_DOMU)
	  ret = (xen_inf.arch == GRUB_XEN_FILE_I386_PAE
		 || xen_inf.arch == GRUB_XEN_FILE_I386_PAE_BIMODE);
	if (type == IS_64_DOMU)
	  ret = (xen_inf.arch == GRUB_XEN_FILE_X86_64);
	break;
      }
    case IS_MULTIBOOT:
    case IS_MULTIBOOT2:
      {
	grub_uint32_t *buffer;
	grub_ssize_t len;
	grub_size_t search_size;
	grub_uint32_t *header;
	grub_uint32_t magic;
	grub_size_t step;

	if (type == IS_MULTIBOOT2)
	  {
	    search_size = 32768;
	    magic = grub_cpu_to_le32_compile_time (0xe85250d6);
	    step = 2;
	  }
	else
	  {
	    search_size = 8192;
	    magic = grub_cpu_to_le32_compile_time (0x1BADB002);
	    step = 1;
	  }

	buffer = grub_malloc (search_size);
	if (!buffer)
	  break;

	len = grub_file_read (file, buffer, search_size);
	if (len < 32)
	  {
	    grub_free (buffer);
	    break;
	  }

	/* Look for the multiboot header in the buffer.  The header should
	   be at least 12 bytes and aligned on a 4-byte boundary.  */
	for (header = buffer;
	     ((char *) header <=
	      (char *) buffer + len - (type == IS_MULTIBOOT2 ? 16 : 12))
	     || (header = 0); header += step)
	  {
	    if (header[0] == magic
		&& !(grub_le_to_cpu32 (header[0])
		     + grub_le_to_cpu32 (header[1])
		     + grub_le_to_cpu32 (header[2])
		     + (type == IS_MULTIBOOT2
			? grub_le_to_cpu32 (header[3]) : 0)))
	      break;
	  }

	if (header != 0)
	  ret = 1;
	grub_free (buffer);
	break;
      }
    case IS_X86_LINUX32:
    case IS_X86_LINUX:
      {
	struct linux_kernel_header lh;
	if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
	  break;
	if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
	  break;

	if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
	  break;

	/* FIXME: some really old kernels (< 1.3.73) will fail this.  */
	if (lh.header !=
	    grub_cpu_to_le32_compile_time (GRUB_LINUX_MAGIC_SIGNATURE)
	    || grub_le_to_cpu16 (lh.version) < 0x0200)
	  break;

	if (type == IS_X86_LINUX)
	  {
	    ret = 1;
	    break;
	  }

	/* FIXME: 2.03 is not always good enough (Linux 2.4 can be 2.03 and
	   still not support 32-bit boot.  */
	if (lh.header !=
	    grub_cpu_to_le32_compile_time (GRUB_LINUX_MAGIC_SIGNATURE)
	    || grub_le_to_cpu16 (lh.version) < 0x0203)
	  break;

	if (!(lh.loadflags & GRUB_LINUX_FLAG_BIG_KERNEL))
	  break;
	ret = 1;
	break;
      }
    case IS_HIBERNATED:
      {
	grub_uint8_t hibr_file_magic[4];
	if (grub_file_read (file, &hibr_file_magic, sizeof (hibr_file_magic))
	    != sizeof (hibr_file_magic))
	  break;
	if (grub_memcmp ("hibr", hibr_file_magic, sizeof (hibr_file_magic)) ==
	    0
	    || grub_memcmp ("HIBR", hibr_file_magic,
			    sizeof (hibr_file_magic)) == 0)
	  ret = 1;
	break;
      }
    case IS_XNU64:
    case IS_XNU32:
      {
	macho = grub_macho_open (args[0], (type == IS_XNU64));
	if (!macho)
	  break;
	/* FIXME: more checks?  */
	ret = 1;
	break;
      }
    case IS_XNU_HIBR:
      {
	struct grub_xnu_hibernate_header hibhead;
	if (grub_file_read (file, &hibhead, sizeof (hibhead))
	    != sizeof (hibhead))
	  break;
	if (hibhead.magic !=
	    grub_cpu_to_le32_compile_time (GRUB_XNU_HIBERNATE_MAGIC))
	  break;
	ret = 1;
	break;
      }
    case IS_32_EFI:
    case IS_64_EFI:
    case IS_IA_EFI:
    case IS_ARM64_EFI:
    case IS_ARM_EFI:
      {
	char signature[4];
	grub_uint32_t pe_offset;
	struct grub_pe32_coff_header coff_head;

	if (grub_file_read (file, signature, 2) != 2)
	  break;
	if (signature[0] != 'M' || signature[1] != 'Z')
	  break;
	if ((grub_ssize_t) grub_file_seek (file, 0x3c) == -1)
	  break;
	if (grub_file_read (file, &pe_offset, 4) != 4)
	  break;
	if ((grub_ssize_t) grub_file_seek (file, grub_le_to_cpu32 (pe_offset))
	    == -1)
	  break;
	if (grub_file_read (file, signature, 4) != 4)
	  break;
	if (signature[0] != 'P' || signature[1] != 'E'
	    || signature[2] != '\0' || signature[3] != '\0')
	  break;

	if (grub_file_read (file, &coff_head, sizeof (coff_head))
	    != sizeof (coff_head))
	  break;
	if (type == IS_32_EFI
	    && coff_head.machine !=
	    grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_I386))
	  break;
	if (type == IS_64_EFI
	    && coff_head.machine !=
	    grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_X86_64))
	  break;
	if (type == IS_IA_EFI
	    && coff_head.machine !=
	    grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_IA64))
	  break;
	if (type == IS_ARM64_EFI
	    && coff_head.machine !=
	    grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_ARM64))
	  break;
	if (type == IS_ARM_EFI
	    && coff_head.machine !=
	    grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_ARMTHUMB_MIXED))
	  break;
	if (type == IS_IA_EFI || type == IS_64_EFI || type == IS_ARM64_EFI)
	  {
	    struct grub_pe64_optional_header o64;
	    if (grub_file_read (file, &o64, sizeof (o64)) != sizeof (o64))
	      break;
	    if (o64.magic !=
		grub_cpu_to_le16_compile_time (GRUB_PE32_PE64_MAGIC))
	      break;
	    if (o64.subsystem !=
		grub_cpu_to_le16_compile_time
		(GRUB_PE32_SUBSYSTEM_EFI_APPLICATION))
	      break;
	    ret = 1;
	    break;
	  }
	if (type == IS_32_EFI || type == IS_ARM_EFI)
	  {
	    struct grub_pe32_optional_header o32;
	    if (grub_file_read (file, &o32, sizeof (o32)) != sizeof (o32))
	      break;
	    if (o32.magic !=
		grub_cpu_to_le16_compile_time (GRUB_PE32_PE32_MAGIC))
	      break;
	    if (o32.subsystem !=
		grub_cpu_to_le16_compile_time
		(GRUB_PE32_SUBSYSTEM_EFI_APPLICATION))
	      break;
	    ret = 1;
	    break;
	  }
	break;
      }
    }

  if (elf)
    grub_elf_close (elf);
  else if (macho)
    grub_macho_close (macho);
  else if (file)
    grub_file_close (file);

  if (!ret && (grub_errno == GRUB_ERR_BAD_OS || grub_errno == GRUB_ERR_NONE))
    /* TRANSLATORS: it's a standalone boolean value,
       opposite of "true".  */
    grub_error (GRUB_ERR_TEST_FAILURE, N_("false"));
  return grub_errno;
}
Exemple #3
0
static grub_err_t
grub_cmd_xnu_kernel64 (grub_command_t cmd __attribute__ ((unused)),
		       int argc, char *args[])
{
  grub_err_t err;
  grub_macho_t macho;
  grub_uint64_t startcode, endcode;
  int i;
  char *ptr, *loadaddr;

  if (argc < 1)
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required");

  grub_xnu_unload ();

  macho = grub_macho_open (args[0]);
  if (! macho)
    return grub_errno;
  if (! grub_macho_contains_macho64 (macho))
    {
      grub_macho_close (macho);
      return grub_error (GRUB_ERR_BAD_OS,
			 "kernel doesn't contain suitable 64-bit architecture");
    }

  err = grub_macho_size64 (macho, &startcode, &endcode, GRUB_MACHO_NOBSS);
  if (err)
    {
      grub_macho_close (macho);
      grub_xnu_unload ();
      return err;
    }

  startcode &= 0x0fffffff;
  endcode &= 0x0fffffff;

  grub_dprintf ("xnu", "endcode = %lx, startcode = %lx\n",
		(unsigned long) endcode, (unsigned long) startcode);

  loadaddr = grub_xnu_heap_malloc (endcode - startcode);
  grub_xnu_heap_will_be_at = startcode;

  if (! loadaddr)
    {
      grub_macho_close (macho);
      grub_xnu_unload ();
      return grub_error (GRUB_ERR_OUT_OF_MEMORY,
			 "not enough memory to load kernel");
    }

  /* Load kernel. */
  err = grub_macho_load64 (macho, loadaddr - startcode, GRUB_MACHO_NOBSS);
  if (err)
    {
      grub_macho_close (macho);
      grub_xnu_unload ();
      return err;
    }

  grub_xnu_entry_point = grub_macho_get_entry_point64 (macho) & 0x0fffffff;
  if (! grub_xnu_entry_point)
    {
      grub_macho_close (macho);
      grub_xnu_unload ();
      return grub_error (GRUB_ERR_BAD_OS, "couldn't find entry point");
    }

  grub_macho_close (macho);

  err = grub_xnu_align_heap (GRUB_XNU_PAGESIZE);
  if (err)
    {
      grub_xnu_unload ();
      return err;
    }

  /* Copy parameters to kernel command line. */
  ptr = grub_xnu_cmdline;
  for (i = 1; i < argc; i++)
    {
      if (ptr + grub_strlen (args[i]) + 1
	  >= grub_xnu_cmdline + sizeof (grub_xnu_cmdline))
	break;
      grub_memcpy (ptr, args[i], grub_strlen (args[i]));
      ptr += grub_strlen (args[i]);
      *ptr = ' ';
      ptr++;
    }

  /* Replace last space by '\0'. */
  if (ptr != grub_xnu_cmdline)
    *(ptr - 1) = 0;

  grub_loader_set (grub_xnu_boot, grub_xnu_unload, 0);

  grub_xnu_lock ();
  grub_xnu_is_64bit = 1;

  return 0;
}
Exemple #4
0
grub_macho_t
grub_macho_file (grub_file_t file, const char *filename, int is_64bit)
{
  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;
  macho->uncompressed32 = 0;
  macho->uncompressed64 = 0;
  macho->compressed32 = 0;
  macho->compressed64 = 0;

  if (grub_file_seek (macho->file, 0) == (grub_off_t) -1)
    goto fail;

  if (grub_file_read (macho->file, &filestart, sizeof (filestart))
      != sizeof (filestart))
    {
      if (!grub_errno)
	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
		    filename);
      goto fail;
    }

  /* Is it a fat file? */
  if (filestart.fat.magic == grub_cpu_to_be32_compile_time (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, archs,
			  sizeof (struct grub_macho_fat_arch) * narchs)
	  != (grub_ssize_t) sizeof(struct grub_macho_fat_arch) * narchs)
	{
	  grub_free (archs);
	  if (!grub_errno)
	    grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
			filename);
	  goto fail;
	}

      for (i = 0; i < narchs; i++)
	{
	  if ((archs[i].cputype
	       == grub_cpu_to_be32_compile_time (GRUB_MACHO_CPUTYPE_IA32))
	      && !is_64bit)
	    {
	      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 ((archs[i].cputype
	       == grub_cpu_to_be32_compile_time (GRUB_MACHO_CPUTYPE_AMD64))
	      && is_64bit)
	    {
	      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 && !is_64bit)
    {
      macho->offset32 = 0;
      macho->end32 = grub_file_size (file);
    }

  /* Is it a thin 64-bit file? */
  if (filestart.thin64.magic == GRUB_MACHO_MAGIC64 && is_64bit)
    {
      macho->offset64 = 0;
      macho->end64 = grub_file_size (file);
    }

  if (grub_memcmp (filestart.lzss.magic, GRUB_MACHO_LZSS_MAGIC,
		   sizeof (filestart.lzss.magic)) == 0 && !is_64bit)
    {
      macho->offset32 = 0;
      macho->end32 = grub_file_size (file);
    }

  /* Is it a thin 64-bit file? */
  if (grub_memcmp (filestart.lzss.magic, GRUB_MACHO_LZSS_MAGIC,
		   sizeof (filestart.lzss.magic)) == 0 && is_64bit)
    {
      macho->offset64 = 0;
      macho->end64 = grub_file_size (file);
    }

  grub_macho_parse32 (macho, filename);
  grub_macho_parse64 (macho, filename);

  if (macho->offset32 == -1 && !is_64bit)
    {
      grub_error (GRUB_ERR_BAD_OS,
		  "Mach-O doesn't contain suitable 32-bit architecture");
      goto fail;
    }

  if (macho->offset64 == -1 && is_64bit)
    {
      grub_error (GRUB_ERR_BAD_OS,
		  "Mach-O doesn't contain suitable 64-bit architecture");
      goto fail;
    }

  return macho;

fail:
  macho->file = 0;
  grub_macho_close (macho);
  return 0;
}