1 /* macho.c - load Mach-O files. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 /* This Mach-O loader is incomplete and can load only non-relocatable segments.
21 This is however enough to boot xnu (otool -l and Mach-O specs for more info).
25 #include <grub/macho.h>
26 #include <grub/machoload.h>
27 #include <grub/file.h>
28 #include <grub/misc.h>
30 #include <grub/i18n.h>
33 GRUB_MOD_LICENSE ("GPLv3+");
36 grub_macho_close (grub_macho_t macho
)
38 grub_file_t file
= macho
->file
;
40 if (!macho
->uncompressed32
)
41 grub_free (macho
->cmds32
);
42 grub_free (macho
->uncompressed32
);
43 if (!macho
->uncompressed64
)
44 grub_free (macho
->cmds64
);
45 grub_free (macho
->uncompressed64
);
50 grub_file_close (file
);
56 grub_macho_file (grub_file_t file
, const char *filename
, int is_64bit
)
59 union grub_macho_filestart filestart
;
61 macho
= grub_malloc (sizeof (*macho
));
72 macho
->uncompressed32
= 0;
73 macho
->uncompressed64
= 0;
74 macho
->compressed32
= 0;
75 macho
->compressed64
= 0;
77 if (grub_file_seek (macho
->file
, 0) == (grub_off_t
) -1)
80 if (grub_file_read (macho
->file
, &filestart
, sizeof (filestart
))
81 != sizeof (filestart
))
84 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
89 /* Is it a fat file? */
90 if (filestart
.fat
.magic
== grub_cpu_to_be32_compile_time (GRUB_MACHO_FAT_MAGIC
))
92 struct grub_macho_fat_arch
*archs
;
95 /* Load architecture description. */
96 narchs
= grub_be_to_cpu32 (filestart
.fat
.nfat_arch
);
97 if (grub_file_seek (macho
->file
, sizeof (struct grub_macho_fat_header
))
100 archs
= grub_malloc (sizeof (struct grub_macho_fat_arch
) * narchs
);
103 if (grub_file_read (macho
->file
, archs
,
104 sizeof (struct grub_macho_fat_arch
) * narchs
)
105 != (grub_ssize_t
) sizeof(struct grub_macho_fat_arch
) * narchs
)
109 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
114 for (i
= 0; i
< narchs
; i
++)
116 if ((archs
[i
].cputype
117 == grub_cpu_to_be32_compile_time (GRUB_MACHO_CPUTYPE_IA32
))
120 macho
->offset32
= grub_be_to_cpu32 (archs
[i
].offset
);
121 macho
->end32
= grub_be_to_cpu32 (archs
[i
].offset
)
122 + grub_be_to_cpu32 (archs
[i
].size
);
124 if ((archs
[i
].cputype
125 == grub_cpu_to_be32_compile_time (GRUB_MACHO_CPUTYPE_AMD64
))
128 macho
->offset64
= grub_be_to_cpu32 (archs
[i
].offset
);
129 macho
->end64
= grub_be_to_cpu32 (archs
[i
].offset
)
130 + grub_be_to_cpu32 (archs
[i
].size
);
136 /* Is it a thin 32-bit file? */
137 if (filestart
.thin32
.magic
== GRUB_MACHO_MAGIC32
&& !is_64bit
)
140 macho
->end32
= grub_file_size (file
);
143 /* Is it a thin 64-bit file? */
144 if (filestart
.thin64
.magic
== GRUB_MACHO_MAGIC64
&& is_64bit
)
147 macho
->end64
= grub_file_size (file
);
150 if (grub_memcmp (filestart
.lzss
.magic
, GRUB_MACHO_LZSS_MAGIC
,
151 sizeof (filestart
.lzss
.magic
)) == 0 && !is_64bit
)
154 macho
->end32
= grub_file_size (file
);
157 /* Is it a thin 64-bit file? */
158 if (grub_memcmp (filestart
.lzss
.magic
, GRUB_MACHO_LZSS_MAGIC
,
159 sizeof (filestart
.lzss
.magic
)) == 0 && is_64bit
)
162 macho
->end64
= grub_file_size (file
);
165 grub_macho_parse32 (macho
, filename
);
166 grub_macho_parse64 (macho
, filename
);
168 if (macho
->offset32
== -1 && !is_64bit
)
170 grub_error (GRUB_ERR_BAD_OS
,
171 "Mach-O doesn't contain suitable 32-bit architecture");
175 if (macho
->offset64
== -1 && is_64bit
)
177 grub_error (GRUB_ERR_BAD_OS
,
178 "Mach-O doesn't contain suitable 64-bit architecture");
186 grub_macho_close (macho
);
191 grub_macho_open (const char *name
, int is_64bit
)
196 file
= grub_file_open (name
);
200 macho
= grub_macho_file (file
, name
, is_64bit
);
202 grub_file_close (file
);