]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/mach_fat.c
ef4fe20b5bbe11c385ffcadf88803c173ee113c2
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
32 * File: kern/mach_fat.c
35 * Fat file support routines.
39 #include <sys/param.h>
40 #include <sys/types.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 <libkern/OSByteOrder.h>
50 #include <machine/exec.h>
52 /**********************************************************************
53 * Routine: fatfile_getarch2()
55 * Function: Locate the architecture-dependant contents of a fat
56 * file that match this CPU.
58 * Args: vp: The vnode for the fat file.
59 * header: A pointer to the fat file header.
60 * req_cpu_type: The required cpu type.
61 * mask_bits: Bits to mask from the sub-image type when
62 * grading it vs. the req_cpu_type
63 * archret (out): Pointer to fat_arch structure to hold
66 * Returns: KERN_SUCCESS: Valid architecture found.
67 * KERN_FAILURE: No valid architecture found.
68 **********************************************************************/
74 __unused
struct vnode
*vp
,
77 cpu_type_t req_cpu_type
,
79 struct fat_arch
*archret
)
81 /* vm_pager_t pager; */
85 struct fat_arch
*arch
;
86 struct fat_arch
*best_arch
;
91 struct fat_header
*header
;
97 * Get the pager for the file.
100 header
= (struct fat_header
*)data_ptr
;
103 * Map portion that must be accessible directly into
106 nfat_arch
= OSSwapBigToHostInt32(header
->nfat_arch
);
108 end_of_archs
= sizeof(struct fat_header
)
109 + nfat_arch
* sizeof(struct fat_arch
);
111 filesize
= ubc_getsize(vp
);
112 if (end_of_archs
> (int)filesize
) {
113 return(LOAD_BADMACHO
);
117 /* This is beacuse we are reading only 512 bytes */
119 if (end_of_archs
> 512)
120 return(LOAD_BADMACHO
);
122 * Round size of fat_arch structures up to page boundry.
124 size
= round_page_32(end_of_archs
);
126 return(LOAD_BADMACHO
);
129 * Scan the fat_arch's looking for the best one.
134 arch
= (struct fat_arch
*) (addr
+ sizeof(struct fat_header
));
135 for (; nfat_arch
-- > 0; arch
++) {
138 * Check to see if right cpu type.
140 if(((cpu_type_t
)OSSwapBigToHostInt32(arch
->cputype
) & ~mask_bits
) != req_cpu_type
)
144 * Get the grade of the cpu subtype.
146 grade
= grade_binary(
147 OSSwapBigToHostInt32(arch
->cputype
),
148 OSSwapBigToHostInt32(arch
->cpusubtype
));
151 * Remember it if it's the best we've seen.
153 if (grade
> best_grade
) {
160 * Return our results.
162 if (best_arch
== NULL
) {
166 OSSwapBigToHostInt32(best_arch
->cputype
);
167 archret
->cpusubtype
=
168 OSSwapBigToHostInt32(best_arch
->cpusubtype
);
170 OSSwapBigToHostInt32(best_arch
->offset
);
172 OSSwapBigToHostInt32(best_arch
->size
);
174 OSSwapBigToHostInt32(best_arch
->align
);
180 * Free the memory we allocated and return.
186 fatfile_getarch_affinity(
188 vm_offset_t data_ptr
,
189 struct fat_arch
*archret
,
193 int handler
= (exec_archhandler_ppc
.path
[0] != 0);
194 cpu_type_t primary_type
, fallback_type
;
196 if (handler
&& affinity
) {
197 primary_type
= CPU_TYPE_POWERPC
;
198 fallback_type
= cpu_type();
200 primary_type
= cpu_type();
201 fallback_type
= CPU_TYPE_POWERPC
;
204 * Ignore the architectural bits when determining if an image
205 * in a fat file should be skipped or graded.
207 lret
= fatfile_getarch2(vp
, data_ptr
, primary_type
, CPU_ARCH_MASK
, archret
);
208 if ((lret
!= 0) && handler
) {
209 lret
= fatfile_getarch2(vp
, data_ptr
, fallback_type
,
215 /**********************************************************************
216 * Routine: fatfile_getarch()
218 * Function: Locate the architecture-dependant contents of a fat
219 * file that match this CPU.
221 * Args: vp: The vnode for the fat file.
222 * header: A pointer to the fat file header.
223 * archret (out): Pointer to fat_arch structure to hold
226 * Returns: KERN_SUCCESS: Valid architecture found.
227 * KERN_FAILURE: No valid architecture found.
228 **********************************************************************/
232 vm_offset_t data_ptr
,
233 struct fat_arch
*archret
)
235 return fatfile_getarch2(vp
, data_ptr
, cpu_type(), 0, archret
);
238 /**********************************************************************
239 * Routine: fatfile_getarch_with_bits()
241 * Function: Locate the architecture-dependant contents of a fat
242 * file that match this CPU.
244 * Args: vp: The vnode for the fat file.
245 * archbits: Architecture specific feature bits
246 * header: A pointer to the fat file header.
247 * archret (out): Pointer to fat_arch structure to hold
250 * Returns: KERN_SUCCESS: Valid architecture found.
251 * KERN_FAILURE: No valid architecture found.
252 **********************************************************************/
254 fatfile_getarch_with_bits(
257 vm_offset_t data_ptr
,
258 struct fat_arch
*archret
)
260 return fatfile_getarch2(vp
, data_ptr
, archbits
| cpu_type(), 0, archret
);