]> git.proxmox.com Git - grub2.git/blob - loader/macho.c
* loader/multiboot.c (GRUB_MOD_INIT) [GRUB_USE_MULTIBOOT2]:
[grub2.git] / loader / macho.c
1 /* macho.c - load Mach-O files. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2009 Free Software Foundation, Inc.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
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).
22 */
23
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>
32
33 grub_err_t
34 grub_macho_close (grub_macho_t macho)
35 {
36 grub_file_t file = macho->file;
37
38 grub_free (macho->cmds32);
39 grub_free (macho->cmds64);
40
41 grub_free (macho);
42
43 if (file)
44 grub_file_close (file);
45
46 return grub_errno;
47 }
48
49 grub_macho_t
50 grub_macho_file (grub_file_t file)
51 {
52 grub_macho_t macho;
53 union grub_macho_filestart filestart;
54
55 macho = grub_malloc (sizeof (*macho));
56 if (! macho)
57 return 0;
58
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;
66
67 if (grub_file_seek (macho->file, 0) == (grub_off_t) -1)
68 goto fail;
69
70 if (grub_file_read (macho->file, &filestart, sizeof (filestart))
71 != sizeof (filestart))
72 {
73 grub_error_push ();
74 grub_error (GRUB_ERR_READ_ERROR, "cannot read Mach-O header");
75 goto fail;
76 }
77
78 /* Is it a fat file? */
79 if (filestart.fat.magic == grub_be_to_cpu32 (GRUB_MACHO_FAT_MAGIC))
80 {
81 struct grub_macho_fat_arch *archs;
82 int i, narchs;
83
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)
95 {
96 grub_free (archs);
97 grub_error_push ();
98 grub_error (GRUB_ERR_READ_ERROR, "cannot read Mach-O header");
99 goto fail;
100 }
101
102 for (i = 0; i < narchs; i++)
103 {
104 if (GRUB_MACHO_CPUTYPE_IS_HOST32
105 (grub_be_to_cpu32 (archs[i].cputype)))
106 {
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);
110 }
111 if (GRUB_MACHO_CPUTYPE_IS_HOST64
112 (grub_be_to_cpu32 (archs[i].cputype)))
113 {
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);
117 }
118 }
119 grub_free (archs);
120 }
121
122 /* Is it a thin 32-bit file? */
123 if (filestart.thin32.magic == GRUB_MACHO_MAGIC32)
124 {
125 macho->offset32 = 0;
126 macho->end32 = grub_file_size (file);
127 }
128
129 /* Is it a thin 64-bit file? */
130 if (filestart.thin64.magic == GRUB_MACHO_MAGIC64)
131 {
132 macho->offset64 = 0;
133 macho->end64 = grub_file_size (file);
134 }
135
136 grub_macho_parse32 (macho);
137 grub_macho_parse64 (macho);
138
139 return macho;
140
141 fail:
142 grub_macho_close (macho);
143 return 0;
144 }
145
146 grub_macho_t
147 grub_macho_open (const char *name)
148 {
149 grub_file_t file;
150 grub_macho_t macho;
151
152 file = grub_gzfile_open (name, 1);
153 if (! file)
154 return 0;
155
156 macho = grub_macho_file (file);
157 if (! macho)
158 grub_file_close (file);
159
160 return macho;
161 }