Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / loader / macho.c
blobbf0bf74f78f2cea634e387b18f8744c93b57f33a
1 /* macho.c - load Mach-O files. */
2 /*
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).
24 #include <grub/err.h>
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>
30 #include <grub/mm.h>
31 #include <grub/i18n.h>
33 grub_err_t
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);
45 grub_free (macho);
47 if (file)
48 grub_file_close (file);
50 return grub_errno;
53 grub_macho_t
54 grub_macho_file (grub_file_t file, const char *filename, int is_64bit)
56 grub_macho_t macho;
57 union grub_macho_filestart filestart;
59 macho = grub_malloc (sizeof (*macho));
60 if (! macho)
61 return 0;
63 macho->file = file;
64 macho->offset32 = -1;
65 macho->offset64 = -1;
66 macho->end32 = -1;
67 macho->end64 = -1;
68 macho->cmds32 = 0;
69 macho->cmds64 = 0;
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)
76 goto fail;
78 if (grub_file_read (macho->file, &filestart, sizeof (filestart))
79 != sizeof (filestart))
81 if (!grub_errno)
82 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
83 filename);
84 goto fail;
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;
91 int i, narchs;
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))
96 == (grub_off_t) -1)
97 goto fail;
98 archs = grub_malloc (sizeof (struct grub_macho_fat_arch) * narchs);
99 if (!archs)
100 goto fail;
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)
105 grub_free (archs);
106 if (!grub_errno)
107 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
108 filename);
109 goto fail;
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);
129 grub_free (archs);
132 /* Is it a thin 32-bit file? */
133 if (filestart.thin32.magic == GRUB_MACHO_MAGIC32 && !is_64bit)
135 macho->offset32 = 0;
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)
142 macho->offset64 = 0;
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)
149 macho->offset32 = 0;
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)
157 macho->offset64 = 0;
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");
168 goto fail;
171 if (macho->offset64 == -1 && is_64bit)
173 grub_error (GRUB_ERR_BAD_OS,
174 "Mach-O doesn't contain suitable 64-bit architecture");
175 goto fail;
178 return macho;
180 fail:
181 macho->file = 0;
182 grub_macho_close (macho);
183 return 0;
186 grub_macho_t
187 grub_macho_open (const char *name, int is_64bit)
189 grub_file_t file;
190 grub_macho_t macho;
192 file = grub_file_open (name);
193 if (! file)
194 return 0;
196 macho = grub_macho_file (file, name, is_64bit);
197 if (! macho)
198 grub_file_close (file);
200 return macho;