GRUB-1.98 changes
[grub2/jjazz.git] / loader / macho.c
blob199d6f11188b1bc22e7bf1e6ad463e772fa703b4
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 grub_err_t
34 grub_macho_close (grub_macho_t macho)
36 grub_file_t file = macho->file;
38 grub_free (macho->cmds32);
39 grub_free (macho->cmds64);
41 grub_free (macho);
43 if (file)
44 grub_file_close (file);
46 return grub_errno;
49 grub_macho_t
50 grub_macho_file (grub_file_t file)
52 grub_macho_t macho;
53 union grub_macho_filestart filestart;
55 macho = grub_malloc (sizeof (*macho));
56 if (! macho)
57 return 0;
59 macho->file = file;
60 macho->offset32 = -1;
61 macho->offset64 = -1;
62 macho->end32 = -1;
63 macho->end64 = -1;
64 macho->cmds32 = 0;
65 macho->cmds64 = 0;
67 if (grub_file_seek (macho->file, 0) == (grub_off_t) -1)
68 goto fail;
70 if (grub_file_read (macho->file, &filestart, sizeof (filestart))
71 != sizeof (filestart))
73 grub_error_push ();
74 grub_error (GRUB_ERR_READ_ERROR, "cannot read Mach-O header");
75 goto fail;
78 /* Is it a fat file? */
79 if (filestart.fat.magic == grub_be_to_cpu32 (GRUB_MACHO_FAT_MAGIC))
81 struct grub_macho_fat_arch *archs;
82 int i, narchs;
84 /* Load architecture description. */
85 narchs = grub_be_to_cpu32 (filestart.fat.nfat_arch);
86 if (grub_file_seek (macho->file, sizeof (struct grub_macho_fat_header))
87 == (grub_off_t) -1)
88 goto fail;
89 archs = grub_malloc (sizeof (struct grub_macho_fat_arch) * narchs);
90 if (!archs)
91 goto fail;
92 if (grub_file_read (macho->file, archs,
93 sizeof (struct grub_macho_fat_arch) * narchs)
94 != (grub_ssize_t)sizeof(struct grub_macho_fat_arch) * narchs)
96 grub_free (archs);
97 grub_error_push ();
98 grub_error (GRUB_ERR_READ_ERROR, "cannot read Mach-O header");
99 goto fail;
102 for (i = 0; i < narchs; i++)
104 if (GRUB_MACHO_CPUTYPE_IS_HOST32
105 (grub_be_to_cpu32 (archs[i].cputype)))
107 macho->offset32 = grub_be_to_cpu32 (archs[i].offset);
108 macho->end32 = grub_be_to_cpu32 (archs[i].offset)
109 + grub_be_to_cpu32 (archs[i].size);
111 if (GRUB_MACHO_CPUTYPE_IS_HOST64
112 (grub_be_to_cpu32 (archs[i].cputype)))
114 macho->offset64 = grub_be_to_cpu32 (archs[i].offset);
115 macho->end64 = grub_be_to_cpu32 (archs[i].offset)
116 + grub_be_to_cpu32 (archs[i].size);
119 grub_free (archs);
122 /* Is it a thin 32-bit file? */
123 if (filestart.thin32.magic == GRUB_MACHO_MAGIC32)
125 macho->offset32 = 0;
126 macho->end32 = grub_file_size (file);
129 /* Is it a thin 64-bit file? */
130 if (filestart.thin64.magic == GRUB_MACHO_MAGIC64)
132 macho->offset64 = 0;
133 macho->end64 = grub_file_size (file);
136 grub_macho_parse32 (macho);
137 grub_macho_parse64 (macho);
139 return macho;
141 fail:
142 grub_macho_close (macho);
143 return 0;
146 grub_macho_t
147 grub_macho_open (const char *name)
149 grub_file_t file;
150 grub_macho_t macho;
152 file = grub_gzfile_open (name, 1);
153 if (! file)
154 return 0;
156 macho = grub_macho_file (file);
157 if (! macho)
158 grub_file_close (file);
160 return macho;