ieee1275: split up grub_machine_get_bootlocation
[grub.git] / util / glue-efi.c
1 /*
2  *  GRUB  --  GRand Unified Bootloader
3  *  Copyright (C) 2010,2012,2013 Free Software Foundation, Inc.
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <config.h>
20
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>
26
27 #define _GNU_SOURCE     1
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <errno.h>
34
35 static void
36 write_fat (FILE *in32, FILE *in64, FILE *out, const char *out_filename,
37            const char *name32, const char *name64)
38 {
39   struct grub_macho_fat_header head;
40   struct grub_macho_fat_arch arch32, arch64;
41   grub_uint32_t size32, size64;
42   char *buf;
43
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);
50
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)
56                                                  + sizeof (arch32)
57                                                  + sizeof (arch64));
58   arch32.size = grub_cpu_to_le32 (size32);
59   arch32.align = 0;
60
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);
66   arch64.align = 0;
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))
70     {
71       if (out_filename)
72         grub_util_error ("cannot write to `%s': %s",
73                          out_filename, strerror (errno));
74       else
75         grub_util_error ("cannot write to the stdout: %s", strerror (errno));
76     }
77
78   buf = xmalloc (size32);
79   if (fread (buf, 1, size32, in32) != size32)
80     grub_util_error (_("cannot read `%s': %s"), name32,
81                      strerror (errno));
82   if (fwrite (buf, 1, size32, out) != size32)
83     {
84       if (out_filename)
85         grub_util_error ("cannot write to `%s': %s", 
86                          out_filename, strerror (errno));
87       else
88         grub_util_error ("cannot write to the stdout: %s", strerror (errno));
89     }
90   free (buf);
91
92   buf = xmalloc (size64);
93   if (fread (buf, 1, size64, in64) != size64)
94     grub_util_error (_("cannot read `%s': %s"), name64,
95                      strerror (errno));
96   if (fwrite (buf, 1, size64, out) != size64)
97     {
98       if (out_filename)
99         grub_util_error ("cannot write to `%s': %s",
100                          out_filename, strerror (errno));
101       else
102         grub_util_error ("cannot write to the stdout: %s", strerror (errno));
103     }
104   free (buf);
105 }
106
107 void
108 grub_util_glue_efi (const char *file32, const char *file64, const char *outname)
109 {
110   FILE *in32, *in64, *out;
111
112   in32 = grub_util_fopen (file32, "r");
113
114   if (!in32)
115     grub_util_error (_("cannot open `%s': %s"), file32,
116                      strerror (errno));
117
118   in64 = grub_util_fopen (file64, "r");
119   if (!in64)
120     grub_util_error (_("cannot open `%s': %s"), file64,
121                      strerror (errno));
122
123   if (outname)
124     out = grub_util_fopen (outname, "wb");
125   else
126     out = stdout;
127
128   if (!out)
129     {
130       grub_util_error (_("cannot open `%s': %s"), outname ? : "stdout",
131                        strerror (errno));
132     }
133
134   write_fat (in32, in64, out, outname,
135              file32, file64);
136
137   fclose (in32);
138   fclose (in64);
139
140   if (out != stdout)
141     fclose (out);
142 }
143