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