]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/mach_fat.c
24c9145c09a31cd0f19adb993bb0a5d6fd1ec030
[apple/xnu.git] / bsd / kern / mach_fat.c
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
31 *
32 * File: kern/mach_fat.c
33 * Author: Peter King
34 *
35 * Fat file support routines.
36 *
37 */
38
39 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/uio.h>
42 #include <sys/vnode.h>
43 #include <vm/vm_kern.h>
44 #include <mach/kern_return.h>
45 #include <mach/vm_param.h>
46 #include <kern/cpu_number.h>
47 #include <mach-o/fat.h>
48 #include <kern/mach_loader.h>
49 #include <architecture/byte_order.h>
50
51 /* XXX should be in common header */
52 extern int grade_binary(cpu_type_t exectype, cpu_subtype_t execsubtype);
53
54 #define CPU_TYPE_NATIVE (cpu_type())
55 #define CPU_TYPE_CLASSIC CPU_TYPE_POWERPC
56
57 /**********************************************************************
58 * Routine: fatfile_getarch2()
59 *
60 * Function: Locate the architecture-dependant contents of a fat
61 * file that match this CPU.
62 *
63 * Args: vp: The vnode for the fat file.
64 * header: A pointer to the fat file header.
65 * req_cpu_type: The required cpu type.
66 * mask_bits: Bits to mask from the sub-image type when
67 * grading it vs. the req_cpu_type
68 * archret (out): Pointer to fat_arch structure to hold
69 * the results.
70 *
71 * Returns: KERN_SUCCESS: Valid architecture found.
72 * KERN_FAILURE: No valid architecture found.
73 **********************************************************************/
74 static load_return_t
75 fatfile_getarch2(
76 #if 0
77 struct vnode *vp,
78 #else
79 __unused struct vnode *vp,
80 #endif
81 vm_offset_t data_ptr,
82 cpu_type_t req_cpu_type,
83 cpu_type_t mask_bits,
84 struct fat_arch *archret)
85 {
86 /* vm_pager_t pager; */
87 vm_offset_t addr;
88 vm_size_t size;
89 load_return_t lret;
90 struct fat_arch *arch;
91 struct fat_arch *best_arch;
92 int grade;
93 int best_grade;
94 int nfat_arch;
95 int end_of_archs;
96 struct fat_header *header;
97 #if 0
98 off_t filesize;
99 #endif
100
101 /*
102 * Get the pager for the file.
103 */
104
105 header = (struct fat_header *)data_ptr;
106
107 /*
108 * Map portion that must be accessible directly into
109 * kernel's map.
110 */
111 nfat_arch = NXSwapBigLongToHost(header->nfat_arch);
112
113 end_of_archs = sizeof(struct fat_header)
114 + nfat_arch * sizeof(struct fat_arch);
115 #if 0
116 filesize = ubc_getsize(vp);
117 if (end_of_archs > (int)filesize) {
118 return(LOAD_BADMACHO);
119 }
120 #endif
121
122 /* This is beacuse we are reading only 512 bytes */
123
124 if (end_of_archs > 512)
125 return(LOAD_BADMACHO);
126 /*
127 * Round size of fat_arch structures up to page boundry.
128 */
129 size = round_page_32(end_of_archs);
130 if (size == 0)
131 return(LOAD_BADMACHO);
132
133 /*
134 * Scan the fat_arch's looking for the best one.
135 */
136 addr = data_ptr;
137 best_arch = NULL;
138 best_grade = 0;
139 arch = (struct fat_arch *) (addr + sizeof(struct fat_header));
140 for (; nfat_arch-- > 0; arch++) {
141
142 /*
143 * Check to see if right cpu type.
144 */
145 if(((cpu_type_t)NXSwapBigIntToHost(arch->cputype) & ~mask_bits) != req_cpu_type)
146 continue;
147
148 /*
149 * Get the grade of the cpu subtype.
150 */
151 grade = grade_binary(
152 NXSwapBigIntToHost(arch->cputype),
153 NXSwapBigIntToHost(arch->cpusubtype));
154
155 /*
156 * Remember it if it's the best we've seen.
157 */
158 if (grade > best_grade) {
159 best_grade = grade;
160 best_arch = arch;
161 }
162 }
163
164 /*
165 * Return our results.
166 */
167 if (best_arch == NULL) {
168 lret = LOAD_BADARCH;
169 } else {
170 archret->cputype =
171 NXSwapBigIntToHost(best_arch->cputype);
172 archret->cpusubtype =
173 NXSwapBigIntToHost(best_arch->cpusubtype);
174 archret->offset =
175 NXSwapBigLongToHost(best_arch->offset);
176 archret->size =
177 NXSwapBigLongToHost(best_arch->size);
178 archret->align =
179 NXSwapBigLongToHost(best_arch->align);
180
181 lret = LOAD_SUCCESS;
182 }
183
184 /*
185 * Free the memory we allocated and return.
186 */
187 return(lret);
188 }
189
190 extern char classichandler[];
191
192 load_return_t
193 fatfile_getarch_affinity(
194 struct vnode *vp,
195 vm_offset_t data_ptr,
196 struct fat_arch *archret,
197 int affinity)
198 {
199 load_return_t lret;
200 int handler = (classichandler[0] != 0);
201 cpu_type_t primary_type, fallback_type;
202
203 if (handler && affinity) {
204 primary_type = CPU_TYPE_CLASSIC;
205 fallback_type = CPU_TYPE_NATIVE;
206 } else {
207 primary_type = CPU_TYPE_NATIVE;
208 fallback_type = CPU_TYPE_CLASSIC;
209 }
210 /*
211 * Ignore the architectural bits when determining if an image
212 * in a fat file should be skipped or graded.
213 */
214 lret = fatfile_getarch2(vp, data_ptr, primary_type, CPU_ARCH_MASK, archret);
215 if ((lret != 0) && handler) {
216 lret = fatfile_getarch2(vp, data_ptr, fallback_type,
217 0, archret);
218 }
219 return lret;
220 }
221
222 /**********************************************************************
223 * Routine: fatfile_getarch()
224 *
225 * Function: Locate the architecture-dependant contents of a fat
226 * file that match this CPU.
227 *
228 * Args: vp: The vnode for the fat file.
229 * header: A pointer to the fat file header.
230 * archret (out): Pointer to fat_arch structure to hold
231 * the results.
232 *
233 * Returns: KERN_SUCCESS: Valid architecture found.
234 * KERN_FAILURE: No valid architecture found.
235 **********************************************************************/
236 load_return_t
237 fatfile_getarch(
238 struct vnode *vp,
239 vm_offset_t data_ptr,
240 struct fat_arch *archret)
241 {
242 return fatfile_getarch2(vp, data_ptr, CPU_TYPE_NATIVE, 0, archret);
243 }
244
245 /**********************************************************************
246 * Routine: fatfile_getarch_with_bits()
247 *
248 * Function: Locate the architecture-dependant contents of a fat
249 * file that match this CPU.
250 *
251 * Args: vp: The vnode for the fat file.
252 * archbits: Architecture specific feature bits
253 * header: A pointer to the fat file header.
254 * archret (out): Pointer to fat_arch structure to hold
255 * the results.
256 *
257 * Returns: KERN_SUCCESS: Valid architecture found.
258 * KERN_FAILURE: No valid architecture found.
259 **********************************************************************/
260 load_return_t
261 fatfile_getarch_with_bits(
262 struct vnode *vp,
263 integer_t archbits,
264 vm_offset_t data_ptr,
265 struct fat_arch *archret)
266 {
267 return fatfile_getarch2(vp, data_ptr, archbits | CPU_TYPE_NATIVE, 0, archret);
268 }
269