Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / loader / macho.c
blob59b195e27eaba9ff77d3bf5a0bb8ac23427fb9a4
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/machoload.h>
27 #include <grub/file.h>
28 #include <grub/misc.h>
29 #include <grub/mm.h>
30 #include <grub/i18n.h>
31 #include <grub/dl.h>
33 GRUB_MOD_LICENSE ("GPLv3+");
35 grub_err_t
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);
47 grub_free (macho);
49 if (file)
50 grub_file_close (file);
52 return grub_errno;
55 grub_macho_t
56 grub_macho_file (grub_file_t file, const char *filename, int is_64bit)
58 grub_macho_t macho;
59 union grub_macho_filestart filestart;
61 macho = grub_malloc (sizeof (*macho));
62 if (! macho)
63 return 0;
65 macho->file = file;
66 macho->offset32 = -1;
67 macho->offset64 = -1;
68 macho->end32 = -1;
69 macho->end64 = -1;
70 macho->cmds32 = 0;
71 macho->cmds64 = 0;
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)
78 goto fail;
80 if (grub_file_read (macho->file, &filestart, sizeof (filestart))
81 != sizeof (filestart))
83 if (!grub_errno)
84 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
85 filename);
86 goto fail;
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;
93 int i, narchs;
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))
98 == (grub_off_t) -1)
99 goto fail;
100 archs = grub_malloc (sizeof (struct grub_macho_fat_arch) * narchs);
101 if (!archs)
102 goto fail;
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)
107 grub_free (archs);
108 if (!grub_errno)
109 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
110 filename);
111 goto fail;
114 for (i = 0; i < narchs; i++)
116 if ((archs[i].cputype
117 == grub_cpu_to_be32_compile_time (GRUB_MACHO_CPUTYPE_IA32))
118 && !is_64bit)
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))
126 && is_64bit)
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);
133 grub_free (archs);
136 /* Is it a thin 32-bit file? */
137 if (filestart.thin32.magic == GRUB_MACHO_MAGIC32 && !is_64bit)
139 macho->offset32 = 0;
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)
146 macho->offset64 = 0;
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)
153 macho->offset32 = 0;
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)
161 macho->offset64 = 0;
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");
172 goto fail;
175 if (macho->offset64 == -1 && is_64bit)
177 grub_error (GRUB_ERR_BAD_OS,
178 "Mach-O doesn't contain suitable 64-bit architecture");
179 goto fail;
182 return macho;
184 fail:
185 macho->file = 0;
186 grub_macho_close (macho);
187 return 0;
190 grub_macho_t
191 grub_macho_open (const char *name, int is_64bit)
193 grub_file_t file;
194 grub_macho_t macho;
196 file = grub_file_open (name);
197 if (! file)
198 return 0;
200 macho = grub_macho_file (file, name, is_64bit);
201 if (! macho)
202 grub_file_close (file);
204 return macho;