2009-07-07 Pavel Roskin <proski@gnu.org>
[grub2/bean.git] / loader / macho.c
blobbd460b8102e0a222f027164d9226bbed53a63221
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/gzio.h>
30 #include <grub/misc.h>
31 #include <grub/mm.h>
33 #define min(a,b) (((a) < (b)) ? (a) : (b))
35 /* 32-bit. */
37 int
38 grub_macho_contains_macho32 (grub_macho_t macho)
40 return macho->offset32 != -1;
43 static void
44 grub_macho_parse32 (grub_macho_t macho)
46 struct grub_macho_header32 head;
48 /* Is there any candidate at all? */
49 if (macho->offset32 == -1)
50 return;
52 /* Read header and check magic*/
53 if (grub_file_seek (macho->file, macho->offset32) == (grub_off_t) -1
54 || grub_file_read (macho->file, &head, sizeof (head))
55 != sizeof(head))
57 grub_error (GRUB_ERR_READ_ERROR, "Cannot read Mach-O header.");
58 macho->offset32 = -1;
59 return;
61 if (head.magic != GRUB_MACHO_MAGIC32)
63 grub_error (GRUB_ERR_BAD_OS, "Invalid Mach-O 32-bit header.");
64 macho->offset32 = -1;
65 return;
68 /* Read commands. */
69 macho->ncmds32 = head.ncmds;
70 macho->cmdsize32 = head.sizeofcmds;
71 macho->cmds32 = grub_malloc(macho->cmdsize32);
72 if (! macho->cmds32)
74 grub_error (GRUB_ERR_OUT_OF_MEMORY, "not enough memory to read commands");
75 return;
77 if (grub_file_read (macho->file, macho->cmds32,
78 (grub_size_t) macho->cmdsize32)
79 != (grub_ssize_t) macho->cmdsize32)
81 grub_error (GRUB_ERR_READ_ERROR, "Cannot read Mach-O header.");
82 macho->offset32 = -1;
86 typedef int NESTED_FUNC_ATTR (*grub_macho_iter_hook_t)
87 (grub_macho_t , struct grub_macho_cmd *,
88 void *);
90 static grub_err_t
91 grub_macho32_cmds_iterate (grub_macho_t macho,
92 grub_macho_iter_hook_t hook,
93 void *hook_arg)
95 grub_uint8_t *hdrs = macho->cmds32;
96 int i;
97 if (! macho->cmds32)
98 return grub_error (GRUB_ERR_BAD_OS, "Couldn't find 32-bit Mach-O");
99 for (i = 0; i < macho->ncmds32; i++)
101 struct grub_macho_cmd *hdr = (struct grub_macho_cmd *) hdrs;
102 if (hook (macho, hdr, hook_arg))
103 break;
104 hdrs += hdr->cmdsize;
107 return grub_errno;
110 grub_size_t
111 grub_macho32_filesize (grub_macho_t macho)
113 if (grub_macho_contains_macho32 (macho))
114 return macho->end32 - macho->offset32;
115 return 0;
118 grub_err_t
119 grub_macho32_readfile (grub_macho_t macho, void *dest)
121 grub_ssize_t read;
122 if (! grub_macho_contains_macho32 (macho))
123 return grub_error (GRUB_ERR_BAD_OS,
124 "Couldn't read architecture-specific part");
126 if (grub_file_seek (macho->file, macho->offset32) == (grub_off_t) -1)
128 grub_error_push ();
129 return grub_error (GRUB_ERR_BAD_OS,
130 "Invalid offset in program header.");
133 read = grub_file_read (macho->file, dest,
134 macho->end32 - macho->offset32);
135 if (read != (grub_ssize_t) (macho->end32 - macho->offset32))
137 grub_error_push ();
138 return grub_error (GRUB_ERR_BAD_OS,
139 "Couldn't read architecture-specific part");
141 return GRUB_ERR_NONE;
144 /* Calculate the amount of memory spanned by the segments. */
145 grub_err_t
146 grub_macho32_size (grub_macho_t macho, grub_addr_t *segments_start,
147 grub_addr_t *segments_end, int flags)
149 int nr_phdrs = 0;
151 /* Run through the program headers to calculate the total memory size we
152 should claim. */
153 auto int NESTED_FUNC_ATTR calcsize (grub_macho_t _macho,
154 struct grub_macho_cmd *phdr, void *_arg);
155 int NESTED_FUNC_ATTR calcsize (grub_macho_t UNUSED _macho,
156 struct grub_macho_cmd *hdr0, void UNUSED *_arg)
158 struct grub_macho_segment32 *hdr = (struct grub_macho_segment32 *) hdr0;
159 if (hdr->cmd != GRUB_MACHO_CMD_SEGMENT32)
160 return 0;
161 if (! hdr->filesize && (flags & GRUB_MACHO_NOBSS))
162 return 0;
164 nr_phdrs++;
165 if (hdr->vmaddr < *segments_start)
166 *segments_start = hdr->vmaddr;
167 if (hdr->vmaddr + hdr->vmsize > *segments_end)
168 *segments_end = hdr->vmaddr + hdr->vmsize;
169 return 0;
172 *segments_start = (grub_uint32_t) -1;
173 *segments_end = 0;
175 grub_macho32_cmds_iterate (macho, calcsize, 0);
177 if (nr_phdrs == 0)
178 return grub_error (GRUB_ERR_BAD_OS, "No program headers present");
180 if (*segments_end < *segments_start)
181 /* Very bad addresses. */
182 return grub_error (GRUB_ERR_BAD_OS, "Bad program header load addresses");
184 return GRUB_ERR_NONE;
187 /* Load every loadable segment into memory specified by `_load_hook'. */
188 grub_err_t
189 grub_macho32_load (grub_macho_t macho, char *offset, int flags)
191 grub_err_t err = 0;
192 auto int NESTED_FUNC_ATTR do_load(grub_macho_t _macho,
193 struct grub_macho_cmd *hdr0,
194 void UNUSED *_arg);
195 int NESTED_FUNC_ATTR do_load(grub_macho_t _macho,
196 struct grub_macho_cmd *hdr0,
197 void UNUSED *_arg)
199 struct grub_macho_segment32 *hdr = (struct grub_macho_segment32 *) hdr0;
201 if (hdr->cmd != GRUB_MACHO_CMD_SEGMENT32)
202 return 0;
204 if (! hdr->filesize && (flags & GRUB_MACHO_NOBSS))
205 return 0;
206 if (! hdr->vmsize)
207 return 0;
209 if (grub_file_seek (_macho->file, hdr->fileoff
210 + _macho->offset32) == (grub_off_t) -1)
212 grub_error_push ();
213 grub_error (GRUB_ERR_BAD_OS,
214 "Invalid offset in program header.");
215 return 1;
218 if (hdr->filesize)
220 grub_ssize_t read;
221 read = grub_file_read (_macho->file, offset + hdr->vmaddr,
222 min (hdr->filesize, hdr->vmsize));
223 if (read != (grub_ssize_t) min (hdr->filesize, hdr->vmsize))
225 /* XXX How can we free memory from `load_hook'? */
226 grub_error_push ();
227 err=grub_error (GRUB_ERR_BAD_OS,
228 "Couldn't read segment from file: "
229 "wanted 0x%lx bytes; read 0x%lx bytes.",
230 hdr->filesize, read);
231 return 1;
235 if (hdr->filesize < hdr->vmsize)
236 grub_memset (offset + hdr->vmaddr + hdr->filesize,
237 0, hdr->vmsize - hdr->filesize);
238 return 0;
241 grub_macho32_cmds_iterate (macho, do_load, 0);
243 return err;
246 grub_uint32_t
247 grub_macho32_get_entry_point (grub_macho_t macho)
249 grub_uint32_t entry_point = 0;
250 auto int NESTED_FUNC_ATTR hook(grub_macho_t _macho,
251 struct grub_macho_cmd *hdr,
252 void UNUSED *_arg);
253 int NESTED_FUNC_ATTR hook(grub_macho_t UNUSED _macho,
254 struct grub_macho_cmd *hdr,
255 void UNUSED *_arg)
257 if (hdr->cmd == GRUB_MACHO_CMD_THREAD)
258 entry_point = ((struct grub_macho_thread32 *) hdr)->entry_point;
259 return 0;
261 grub_macho32_cmds_iterate (macho, hook, 0);
262 return entry_point;
266 grub_err_t
267 grub_macho_close (grub_macho_t macho)
269 grub_file_t file = macho->file;
271 grub_free (macho->cmds32);
272 grub_free (macho->cmds64);
274 grub_free (macho);
276 if (file)
277 grub_file_close (file);
279 return grub_errno;
282 grub_macho_t
283 grub_macho_file (grub_file_t file)
285 grub_macho_t macho;
286 union grub_macho_filestart filestart;
288 macho = grub_malloc (sizeof (*macho));
289 if (! macho)
290 return 0;
292 macho->file = file;
293 macho->offset32 = -1;
294 macho->offset64 = -1;
295 macho->end32 = -1;
296 macho->end64 = -1;
297 macho->cmds32 = 0;
298 macho->cmds64 = 0;
300 if (grub_file_seek (macho->file, 0) == (grub_off_t) -1)
301 goto fail;
303 if (grub_file_read (macho->file, &filestart, sizeof (filestart))
304 != sizeof (filestart))
306 grub_error_push ();
307 grub_error (GRUB_ERR_READ_ERROR, "Cannot read Mach-O header.");
308 goto fail;
311 /* Is it a fat file? */
312 if (filestart.fat.magic == grub_be_to_cpu32 (GRUB_MACHO_FAT_MAGIC))
314 struct grub_macho_fat_arch *archs;
315 int i, narchs;
317 /* Load architecture description. */
318 narchs = grub_be_to_cpu32 (filestart.fat.nfat_arch);
319 if (grub_file_seek (macho->file, sizeof (struct grub_macho_fat_header))
320 == (grub_off_t) -1)
321 goto fail;
322 archs = grub_malloc (sizeof (struct grub_macho_fat_arch) * narchs);
323 if (!archs)
324 goto fail;
325 if (grub_file_read (macho->file, archs,
326 sizeof (struct grub_macho_fat_arch) * narchs)
327 != (grub_ssize_t)sizeof(struct grub_macho_fat_arch) * narchs)
329 grub_free (archs);
330 grub_error_push ();
331 grub_error (GRUB_ERR_READ_ERROR, "Cannot read Mach-O header.");
332 goto fail;
335 for (i = 0; i < narchs; i++)
337 if (GRUB_MACHO_CPUTYPE_IS_HOST32
338 (grub_be_to_cpu32 (archs[i].cputype)))
340 macho->offset32 = grub_be_to_cpu32 (archs[i].offset);
341 macho->end32 = grub_be_to_cpu32 (archs[i].offset)
342 + grub_be_to_cpu32 (archs[i].size);
344 if (GRUB_MACHO_CPUTYPE_IS_HOST64
345 (grub_be_to_cpu32 (archs[i].cputype)))
347 macho->offset64 = grub_be_to_cpu32 (archs[i].offset);
348 macho->end64 = grub_be_to_cpu32 (archs[i].offset)
349 + grub_be_to_cpu32 (archs[i].size);
352 grub_free (archs);
355 /* Is it a thin 32-bit file? */
356 if (filestart.thin32.magic == GRUB_MACHO_MAGIC32)
358 macho->offset32 = 0;
359 macho->end32 = grub_file_size (file);
362 /* Is it a thin 64-bit file? */
363 if (filestart.thin64.magic == GRUB_MACHO_MAGIC64)
365 macho->offset64 = 0;
366 macho->end64 = grub_file_size (file);
369 grub_macho_parse32 (macho);
370 /* FIXME: implement 64-bit.*/
371 /* grub_macho_parse64 (macho); */
373 return macho;
375 fail:
376 grub_macho_close (macho);
377 return 0;
380 grub_macho_t
381 grub_macho_open (const char *name)
383 grub_file_t file;
384 grub_macho_t macho;
386 file = grub_gzfile_open (name, 1);
387 if (! file)
388 return 0;
390 macho = grub_macho_file (file);
391 if (! macho)
392 grub_file_close (file);
394 return macho;