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/cpu/macho.h>
27 #include <grub/machoload.h>
28 #include <grub/file.h>
29 #include <grub/misc.h>
31 #include <grub/i18n.h>
34 grub_macho_close (grub_macho_t macho
)
36 grub_file_t file
= macho
->file
;
38 if (!macho
->uncompressed32
)
39 grub_free (macho
->cmds32
);
40 grub_free (macho
->uncompressed32
);
41 if (!macho
->uncompressed64
)
42 grub_free (macho
->cmds64
);
43 grub_free (macho
->uncompressed64
);
48 grub_file_close (file
);
54 grub_macho_file (grub_file_t file
, const char *filename
, int is_64bit
)
57 union grub_macho_filestart filestart
;
59 macho
= grub_malloc (sizeof (*macho
));
70 macho
->uncompressed32
= 0;
71 macho
->uncompressed64
= 0;
72 macho
->compressed32
= 0;
73 macho
->compressed64
= 0;
75 if (grub_file_seek (macho
->file
, 0) == (grub_off_t
) -1)
78 if (grub_file_read (macho
->file
, &filestart
, sizeof (filestart
))
79 != sizeof (filestart
))
82 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
87 /* Is it a fat file? */
88 if (filestart
.fat
.magic
== grub_be_to_cpu32 (GRUB_MACHO_FAT_MAGIC
))
90 struct grub_macho_fat_arch
*archs
;
93 /* Load architecture description. */
94 narchs
= grub_be_to_cpu32 (filestart
.fat
.nfat_arch
);
95 if (grub_file_seek (macho
->file
, sizeof (struct grub_macho_fat_header
))
98 archs
= grub_malloc (sizeof (struct grub_macho_fat_arch
) * narchs
);
101 if (grub_file_read (macho
->file
, archs
,
102 sizeof (struct grub_macho_fat_arch
) * narchs
)
103 != (grub_ssize_t
) sizeof(struct grub_macho_fat_arch
) * narchs
)
107 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
112 for (i
= 0; i
< narchs
; i
++)
114 if (GRUB_MACHO_CPUTYPE_IS_HOST32
115 (grub_be_to_cpu32 (archs
[i
].cputype
)) && !is_64bit
)
117 macho
->offset32
= grub_be_to_cpu32 (archs
[i
].offset
);
118 macho
->end32
= grub_be_to_cpu32 (archs
[i
].offset
)
119 + grub_be_to_cpu32 (archs
[i
].size
);
121 if (GRUB_MACHO_CPUTYPE_IS_HOST64
122 (grub_be_to_cpu32 (archs
[i
].cputype
)) && is_64bit
)
124 macho
->offset64
= grub_be_to_cpu32 (archs
[i
].offset
);
125 macho
->end64
= grub_be_to_cpu32 (archs
[i
].offset
)
126 + grub_be_to_cpu32 (archs
[i
].size
);
132 /* Is it a thin 32-bit file? */
133 if (filestart
.thin32
.magic
== GRUB_MACHO_MAGIC32
&& !is_64bit
)
136 macho
->end32
= grub_file_size (file
);
139 /* Is it a thin 64-bit file? */
140 if (filestart
.thin64
.magic
== GRUB_MACHO_MAGIC64
&& is_64bit
)
143 macho
->end64
= grub_file_size (file
);
146 if (grub_memcmp (filestart
.lzss
.magic
, GRUB_MACHO_LZSS_MAGIC
,
147 sizeof (filestart
.lzss
.magic
)) == 0 && !is_64bit
)
150 macho
->end32
= grub_file_size (file
);
153 /* Is it a thin 64-bit file? */
154 if (grub_memcmp (filestart
.lzss
.magic
, GRUB_MACHO_LZSS_MAGIC
,
155 sizeof (filestart
.lzss
.magic
)) == 0 && is_64bit
)
158 macho
->end64
= grub_file_size (file
);
161 grub_macho_parse32 (macho
, filename
);
162 grub_macho_parse64 (macho
, filename
);
164 if (macho
->offset32
== -1 && !is_64bit
)
166 grub_error (GRUB_ERR_BAD_OS
,
167 "Mach-O doesn't contain suitable 32-bit architecture");
171 if (macho
->offset64
== -1 && is_64bit
)
173 grub_error (GRUB_ERR_BAD_OS
,
174 "Mach-O doesn't contain suitable 64-bit architecture");
182 grub_macho_close (macho
);
187 grub_macho_open (const char *name
, int is_64bit
)
192 file
= grub_file_open (name
);
196 macho
= grub_macho_file (file
, name
, is_64bit
);
198 grub_file_close (file
);