Blob Blame History Raw
From 4c5f9d222491cdfcc015b52a77706a64f305c024 Mon Sep 17 00:00:00 2001
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Date: Sat, 13 Apr 2013 00:38:04 +0200
Subject: [PATCH 296/364] 	Better support Apple Intel Macs on CD.

---
 ChangeLog                 |   4 +
 Makefile.util.def         |  14 +++
 configure.ac              |   1 +
 include/grub/i386/macho.h |   8 +-
 include/grub/macho.h      |   7 ++
 util/grub-glue-efi.c      | 219 ++++++++++++++++++++++++++++++++++++++++++++++
 util/grub-mkrescue.in     |  19 ++++
 7 files changed, 268 insertions(+), 4 deletions(-)
 create mode 100644 util/grub-glue-efi.c

diff --git a/ChangeLog b/ChangeLog
index f9d5dca..80067df 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2013-04-12  Vladimir Serbinenko  <phcoder@gmail.com>
 
+	Better support Apple Intel Macs on CD.
+
+2013-04-12  Vladimir Serbinenko  <phcoder@gmail.com>
+
 	Replace stpcpy with grub_stpcpy in tools.
 
 2013-04-12  Vladimir Serbinenko  <phcoder@gmail.com>
diff --git a/Makefile.util.def b/Makefile.util.def
index a231b40..ef3c4ea 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -776,6 +776,20 @@ program = {
 };
 
 program = {
+  name = grub-glue-efi;
+  mansection = 1;
+
+  common = util/grub-glue-efi.c;
+  common = grub-core/kern/emu/argp_common.c;
+
+  ldadd = libgrubmods.a;
+  ldadd = libgrubgcry.a;
+  ldadd = libgrubkern.a;
+  ldadd = grub-core/gnulib/libgnu.a;
+  ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
+};
+
+program = {
   name = grub-render-label;
   mansection = 1;
 
diff --git a/configure.ac b/configure.ac
index 19febfd..ca180c6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -56,6 +56,7 @@ grub_TRANSFORM([grub-install])
 grub_TRANSFORM([grub-mkconfig])
 grub_TRANSFORM([grub-mkfont])
 grub_TRANSFORM([grub-mkimage])
+grub_TRANSFORM([grub-glue-efi])
 grub_TRANSFORM([grub-mklayout])
 grub_TRANSFORM([grub-mkpasswd-pbkdf2])
 grub_TRANSFORM([grub-mkrelpath])
diff --git a/include/grub/i386/macho.h b/include/grub/i386/macho.h
index 5ee9f9e..437fa03 100644
--- a/include/grub/i386/macho.h
+++ b/include/grub/i386/macho.h
@@ -21,12 +21,12 @@
 
 #include <grub/macho.h>
 
-#define GRUB_MACHO_CPUTYPE_IS_HOST32(x) ((x)==0x00000007)
-#define GRUB_MACHO_CPUTYPE_IS_HOST64(x) ((x)==0x01000007)
+#define GRUB_MACHO_CPUTYPE_IS_HOST32(x) ((x) == GRUB_MACHO_CPUTYPE_IA32)
+#define GRUB_MACHO_CPUTYPE_IS_HOST64(x) ((x) == GRUB_MACHO_CPUTYPE_AMD64)
 #ifdef __x86_64__
-#define GRUB_MACHO_CPUTYPE_IS_HOST_CURRENT(x) ((x)==0x01000007)
+#define GRUB_MACHO_CPUTYPE_IS_HOST_CURRENT(x) ((x) == GRUB_MACHO_CPUTYPE_AMD64)
 #else
-#define GRUB_MACHO_CPUTYPE_IS_HOST_CURRENT(x) ((x)==0x00000007)
+#define GRUB_MACHO_CPUTYPE_IS_HOST_CURRENT(x) ((x) == GRUB_MACHO_CPUTYPE_IA32)
 #endif
 
 struct grub_macho_thread32
diff --git a/include/grub/macho.h b/include/grub/macho.h
index 21f0714..18434ff 100644
--- a/include/grub/macho.h
+++ b/include/grub/macho.h
@@ -26,6 +26,13 @@ struct grub_macho_fat_header
   grub_uint32_t magic;
   grub_uint32_t nfat_arch;
 } __attribute__ ((packed));
+
+enum
+  {
+    GRUB_MACHO_CPUTYPE_IA32 = 0x00000007,
+    GRUB_MACHO_CPUTYPE_AMD64 = 0x01000007
+  };
+
 #define GRUB_MACHO_FAT_MAGIC 0xcafebabe
 #define GRUB_MACHO_FAT_EFI_MAGIC 0x0ef1fab9
 
diff --git a/util/grub-glue-efi.c b/util/grub-glue-efi.c
new file mode 100644
index 0000000..47e393a
--- /dev/null
+++ b/util/grub-glue-efi.c
@@ -0,0 +1,219 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2010,2012,2013 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 <config.h>
+
+#include <grub/util/misc.h>
+#include <grub/i18n.h>
+#include <grub/term.h>
+#include <grub/macho.h>
+
+#define _GNU_SOURCE	1
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <argp.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "progname.h"
+
+struct arguments
+{
+  char *input32;
+  char *input64;
+  char *output;
+  int verbosity;
+};
+
+static struct argp_option options[] = {
+  {"input32",  '3', N_("FILE"), 0,
+   N_("set input filename for 32-bit part."), 0},
+  {"input64",  '6', N_("FILE"), 0,
+   N_("set input filename for 64-bit part."), 0},
+  {"output",  'o', N_("FILE"), 0,
+   N_("set output filename. Default is STDOUT"), 0},
+  {"verbose",     'v', 0,      0, N_("print verbose messages."), 0},
+  { 0, 0, 0, 0, 0, 0 }
+};
+
+static error_t
+argp_parser (int key, char *arg, struct argp_state *state)
+{
+  /* Get the input argument from argp_parse, which we
+     know is a pointer to our arguments structure. */
+  struct arguments *arguments = state->input;
+
+  switch (key)
+    {
+    case '6':
+      arguments->input64 = xstrdup (arg);
+      break;
+    case '3':
+      arguments->input32 = xstrdup (arg);
+      break;
+
+    case 'o':
+      arguments->output = xstrdup (arg);
+      break;
+
+    case 'v':
+      arguments->verbosity++;
+      break;
+
+    default:
+      return ARGP_ERR_UNKNOWN;
+    }
+
+  return 0;
+}
+
+static struct argp argp = {
+  options, argp_parser, N_("[OPTIONS]"),
+  N_("Glue 32-bit and 64-binary into Apple fat one."),
+  NULL, NULL, NULL
+};
+
+static void
+write_fat (FILE *in32, FILE *in64, FILE *out, const char *out_filename,
+	   const char *name32, const char *name64)
+{
+  struct grub_macho_fat_header head;
+  struct grub_macho_fat_arch arch32, arch64;
+  grub_uint32_t size32, size64;
+  char *buf;
+
+  fseek (in32, 0, SEEK_END);
+  size32 = ftell (in32);
+  fseek (in32, 0, SEEK_SET);
+  fseek (in64, 0, SEEK_END);
+  size64 = ftell (in64);
+  fseek (in64, 0, SEEK_SET);
+
+  head.magic = grub_cpu_to_le32_compile_time (GRUB_MACHO_FAT_EFI_MAGIC);
+  head.nfat_arch = grub_cpu_to_le32_compile_time (2);
+  arch32.cputype = grub_cpu_to_le32_compile_time (GRUB_MACHO_CPUTYPE_IA32);
+  arch32.cpusubtype = grub_cpu_to_le32_compile_time (3);
+  arch32.offset = grub_cpu_to_le32_compile_time (sizeof (head)
+						 + sizeof (arch32)
+						 + sizeof (arch64));
+  arch32.size = grub_cpu_to_le32 (size32);
+  arch32.align = 0;
+
+  arch64.cputype = grub_cpu_to_le32_compile_time (GRUB_MACHO_CPUTYPE_AMD64);
+  arch64.cpusubtype = grub_cpu_to_le32_compile_time (3);
+  arch64.offset = grub_cpu_to_le32 (sizeof (head) + sizeof (arch32)
+				    + sizeof (arch64) + size32);
+  arch64.size = grub_cpu_to_le32 (size64);
+  arch64.align = 0;
+  if (fwrite (&head, 1, sizeof (head), out) != sizeof (head)
+      || fwrite (&arch32, 1, sizeof (arch32), out) != sizeof (arch32)
+      || fwrite (&arch64, 1, sizeof (arch64), out) != sizeof (arch64))
+    {
+      if (out_filename)
+	grub_util_error ("cannot write to `%s': %s",
+			 out_filename, strerror (errno));
+      else
+	grub_util_error ("cannot write to the stdout: %s", strerror (errno));
+    }
+
+  buf = xmalloc (size32);
+  if (fread (buf, 1, size32, in32) != size32)
+    grub_util_error (_("cannot read `%s': %s"), name32,
+                     strerror (errno));
+  if (fwrite (buf, 1, size32, out) != size32)
+    {
+      if (out_filename)
+	grub_util_error ("cannot write to `%s': %s", 
+			 out_filename, strerror (errno));
+      else
+	grub_util_error ("cannot write to the stdout: %s", strerror (errno));
+    }
+  free (buf);
+
+  buf = xmalloc (size64);
+  if (fread (buf, 1, size64, in64) != size64)
+    grub_util_error (_("cannot read `%s': %s"), name64,
+                     strerror (errno));
+  if (fwrite (buf, 1, size64, out) != size64)
+    {
+      if (out_filename)
+	grub_util_error ("cannot write to `%s': %s",
+			 out_filename, strerror (errno));
+      else
+	grub_util_error ("cannot write to the stdout: %s", strerror (errno));
+    }
+  free (buf);
+}
+
+int
+main (int argc, char *argv[])
+{
+  FILE *in32, *in64, *out;
+  struct arguments arguments;
+
+  set_program_name (argv[0]);
+
+  /* Check for options.  */
+  memset (&arguments, 0, sizeof (struct arguments));
+  if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
+    {
+      fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
+      exit(1);
+    }
+
+  if (!arguments.input32 || !arguments.input64)
+    {
+      fprintf (stderr, "%s", _("Missing input file\n"));
+      exit(1);
+    }
+
+  in32 = fopen (arguments.input32, "r");
+
+  if (!in32)
+    grub_util_error (_("cannot open `%s': %s"), arguments.input32,
+		     strerror (errno));
+
+  in64 = fopen (arguments.input64, "r");
+  if (!in64)
+    grub_util_error (_("cannot open `%s': %s"), arguments.input64,
+		     strerror (errno));
+
+  if (arguments.output)
+    out = fopen (arguments.output, "wb");
+  else
+    out = stdout;
+
+  if (!out)
+    {
+      grub_util_error (_("cannot open `%s': %s"), arguments.output ? : "stdout",
+		       strerror (errno));
+    }
+
+  write_fat (in32, in64, out, arguments.output,
+	     arguments.input32, arguments.input64);
+
+  fclose (in32);
+  fclose (in64);
+
+  if (out != stdout)
+    fclose (out);
+
+  return 0;
+}
diff --git a/util/grub-mkrescue.in b/util/grub-mkrescue.in
index 510d95f..6a38f84 100644
--- a/util/grub-mkrescue.in
+++ b/util/grub-mkrescue.in
@@ -49,6 +49,7 @@ rom_directory=
 override_dir=
 grub_mkimage="${bindir}/@grub_mkimage@"
 grub_render_label="${bindir}/@grub_render_label@"
+grub_glue_efi="${bindir}/@grub_glue_efi@"
 label_font="${pkgdatadir}/unicode.pf2"
 label_color="black"
 label_bgcolor="white"
@@ -82,6 +83,7 @@ usage () {
     # TRANSLATORS: xorriso is a program for creating ISOs and burning CDs
     print_option_help "--xorriso=$filetrans" "$(gettext "use FILE as xorriso [optional]")"
     print_option_help "--grub-mkimage=$filetrans" "$(gettext "use FILE as grub-mkimage")"
+    print_option_help "--grub-glue-efi=$filetrans" "$(gettext "use FILE as grub-glue-efi")"
     print_option_help "--grub-render-label=$filetrans" "$(gettext "use FILE as grub-render-label")"
     print_option_help "--label-font=$filetrans" "$(gettext "use FILE as font for label")"
     print_option_help "--label-color=$(gettext "COLOR")" "$(gettext "use COLOR for label")"
@@ -160,6 +162,11 @@ do
     --grub-mkimage=*)
 	grub_mkimage=`echo "$option" | sed 's/--grub-mkimage=//'` ;;
 
+    --grub-glue-efi)
+	grub_glue_efi=`argument $option "$@"`; shift ;;
+    --grub-glue-efi=*)
+	grub_glue_efi=`echo "$option" | sed 's/--grub-glue-efi=//'` ;;
+
     --grub-render-label)
 	grub_render_label=`argument $option "$@"`; shift ;;
     --grub-render-label=*)
@@ -359,6 +366,18 @@ if test -e "${efi64_dir}" || test -e "${efi32_dir}" || test -e "${ia64_dir}"; th
 	cp "${efi_dir}"/efi/boot/bootia32.efi "${efi_dir}"/efi/boot/boot.efi
     fi
 
+    if [ -e "${efi_dir}"/efi/boot/bootx64.efi ] || [ -e "${efi_dir}"/efi/boot/bootia32.efi ]; then
+	mkdir -p "${iso9660_dir}"/System/Library/CoreServices
+    fi
+
+    if [ -e "${efi_dir}"/efi/boot/bootx64.efi ] && [ -e "${efi_dir}"/efi/boot/bootia32.efi ]; then
+	"$grub_glue_efi" -6 "${efi_dir}"/efi/boot/bootx64.efi -3 "${efi_dir}"/efi/boot/bootia32.efi -o "${iso9660_dir}"/System/Library/CoreServices/boot.efi
+    elif [ -e "${efi_dir}"/efi/boot/bootx64.efi ]; then
+	cp "${efi_dir}"/efi/boot/bootx64.efi "${iso9660_dir}"/System/Library/CoreServices/boot.efi
+    elif [ -e "${efi_dir}"/efi/boot/bootia32.efi ]; then
+	cp "${efi_dir}"/efi/boot/bootia32.efi "${iso9660_dir}"/System/Library/CoreServices/boot.efi
+    fi
+
     mformat -C -f 2880 -L 16 -i "${iso9660_dir}"/efi.img ::
     mcopy -s -i "${iso9660_dir}"/efi.img ${efi_dir}/efi ::/
     rm -rf ${efi_dir}
-- 
1.8.1.4