Blame SOURCES/0297-Better-support-Apple-Intel-Macs-on-CD.patch

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