2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2010,2012,2013 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21 #include <grub/util/misc.h>
22 #include <grub/util/install.h>
23 #include <grub/i18n.h>
24 #include <grub/term.h>
25 #include <grub/macho.h>
36 write_fat (FILE *in32, FILE *in64, FILE *out, const char *out_filename,
37 const char *name32, const char *name64)
39 struct grub_macho_fat_header head;
40 struct grub_macho_fat_arch arch32, arch64;
41 grub_uint32_t size32, size64;
44 fseek (in32, 0, SEEK_END);
45 size32 = ftell (in32);
46 fseek (in32, 0, SEEK_SET);
47 fseek (in64, 0, SEEK_END);
48 size64 = ftell (in64);
49 fseek (in64, 0, SEEK_SET);
51 head.magic = grub_cpu_to_le32_compile_time (GRUB_MACHO_FAT_EFI_MAGIC);
52 head.nfat_arch = grub_cpu_to_le32_compile_time (2);
53 arch32.cputype = grub_cpu_to_le32_compile_time (GRUB_MACHO_CPUTYPE_IA32);
54 arch32.cpusubtype = grub_cpu_to_le32_compile_time (3);
55 arch32.offset = grub_cpu_to_le32_compile_time (sizeof (head)
58 arch32.size = grub_cpu_to_le32 (size32);
61 arch64.cputype = grub_cpu_to_le32_compile_time (GRUB_MACHO_CPUTYPE_AMD64);
62 arch64.cpusubtype = grub_cpu_to_le32_compile_time (3);
63 arch64.offset = grub_cpu_to_le32 (sizeof (head) + sizeof (arch32)
64 + sizeof (arch64) + size32);
65 arch64.size = grub_cpu_to_le32 (size64);
67 if (fwrite (&head, 1, sizeof (head), out) != sizeof (head)
68 || fwrite (&arch32, 1, sizeof (arch32), out) != sizeof (arch32)
69 || fwrite (&arch64, 1, sizeof (arch64), out) != sizeof (arch64))
72 grub_util_error ("cannot write to `%s': %s",
73 out_filename, strerror (errno));
75 grub_util_error ("cannot write to the stdout: %s", strerror (errno));
78 buf = xmalloc (size32);
79 if (fread (buf, 1, size32, in32) != size32)
80 grub_util_error (_("cannot read `%s': %s"), name32,
82 if (fwrite (buf, 1, size32, out) != size32)
85 grub_util_error ("cannot write to `%s': %s",
86 out_filename, strerror (errno));
88 grub_util_error ("cannot write to the stdout: %s", strerror (errno));
92 buf = xmalloc (size64);
93 if (fread (buf, 1, size64, in64) != size64)
94 grub_util_error (_("cannot read `%s': %s"), name64,
96 if (fwrite (buf, 1, size64, out) != size64)
99 grub_util_error ("cannot write to `%s': %s",
100 out_filename, strerror (errno));
102 grub_util_error ("cannot write to the stdout: %s", strerror (errno));
108 grub_util_glue_efi (const char *file32, const char *file64, const char *outname)
110 FILE *in32, *in64, *out;
112 in32 = grub_util_fopen (file32, "r");
115 grub_util_error (_("cannot open `%s': %s"), file32,
118 in64 = grub_util_fopen (file64, "r");
120 grub_util_error (_("cannot open `%s': %s"), file64,
124 out = grub_util_fopen (outname, "wb");
130 grub_util_error (_("cannot open `%s': %s"), outname ? : "stdout",
134 write_fat (in32, in64, out, outname,