]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/mach_fat.c
677ae0008abd65bcfcd08e9aad5ce6b11de60935
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
30 * File: kern/mach_fat.c
33 * Fat file support routines.
37 #include <sys/param.h>
38 #include <sys/types.h>
40 #include <sys/vnode.h>
41 #include <vm/vm_kern.h>
42 #include <mach/kern_return.h>
43 #include <mach/vm_param.h>
44 #include <kern/cpu_number.h>
45 #include <mach-o/fat.h>
46 #include <kern/mach_loader.h>
47 #include <libkern/OSByteOrder.h>
48 #include <machine/exec.h>
50 /**********************************************************************
51 * Routine: fatfile_getarch2()
53 * Function: Locate the architecture-dependant contents of a fat
54 * file that match this CPU.
56 * Args: vp: The vnode for the fat file.
57 * header: A pointer to the fat file header.
58 * req_cpu_type: The required cpu type.
59 * mask_bits: Bits to mask from the sub-image type when
60 * grading it vs. the req_cpu_type
61 * archret (out): Pointer to fat_arch structure to hold
64 * Returns: KERN_SUCCESS: Valid architecture found.
65 * KERN_FAILURE: No valid architecture found.
66 **********************************************************************/
72 __unused
struct vnode
*vp
,
75 cpu_type_t req_cpu_type
,
77 struct fat_arch
*archret
)
79 /* vm_pager_t pager; */
83 struct fat_arch
*arch
;
84 struct fat_arch
*best_arch
;
89 struct fat_header
*header
;
95 * Get the pager for the file.
98 header
= (struct fat_header
*)data_ptr
;
101 * Map portion that must be accessible directly into
104 nfat_arch
= OSSwapBigToHostInt32(header
->nfat_arch
);
106 end_of_archs
= (off_t
)nfat_arch
* sizeof(struct fat_arch
) +
107 sizeof(struct fat_header
);
109 filesize
= ubc_getsize(vp
);
110 if (end_of_archs
> (int)filesize
) {
111 return(LOAD_BADMACHO
);
116 * This check is limited on the top end because we are reading
117 * only PAGE_SIZE bytes
119 if (end_of_archs
> PAGE_SIZE
||
120 end_of_archs
< (sizeof(struct fat_header
)+sizeof(struct fat_arch
)))
121 return(LOAD_BADMACHO
);
124 * Round size of fat_arch structures up to page boundry.
126 size
= round_page_32(end_of_archs
);
128 return(LOAD_BADMACHO
);
131 * Scan the fat_arch's looking for the best one.
136 arch
= (struct fat_arch
*) (addr
+ sizeof(struct fat_header
));
137 for (; nfat_arch
-- > 0; arch
++) {
140 * Check to see if right cpu type.
142 if(((cpu_type_t
)OSSwapBigToHostInt32(arch
->cputype
) & ~mask_bits
) != req_cpu_type
)
146 * Get the grade of the cpu subtype.
148 grade
= grade_binary(
149 OSSwapBigToHostInt32(arch
->cputype
),
150 OSSwapBigToHostInt32(arch
->cpusubtype
));
153 * Remember it if it's the best we've seen.
155 if (grade
> best_grade
) {
162 * Return our results.
164 if (best_arch
== NULL
) {
168 OSSwapBigToHostInt32(best_arch
->cputype
);
169 archret
->cpusubtype
=
170 OSSwapBigToHostInt32(best_arch
->cpusubtype
);
172 OSSwapBigToHostInt32(best_arch
->offset
);
174 OSSwapBigToHostInt32(best_arch
->size
);
176 OSSwapBigToHostInt32(best_arch
->align
);
182 * Free the memory we allocated and return.
188 fatfile_getarch_affinity(
190 vm_offset_t data_ptr
,
191 struct fat_arch
*archret
,
195 int handler
= (exec_archhandler_ppc
.path
[0] != 0);
196 cpu_type_t primary_type
, fallback_type
;
198 if (handler
&& affinity
) {
199 primary_type
= CPU_TYPE_POWERPC
;
200 fallback_type
= cpu_type();
202 primary_type
= cpu_type();
203 fallback_type
= CPU_TYPE_POWERPC
;
206 * Ignore the architectural bits when determining if an image
207 * in a fat file should be skipped or graded.
209 lret
= fatfile_getarch2(vp
, data_ptr
, primary_type
, CPU_ARCH_MASK
, archret
);
210 if ((lret
!= 0) && handler
) {
211 lret
= fatfile_getarch2(vp
, data_ptr
, fallback_type
,
217 /**********************************************************************
218 * Routine: fatfile_getarch()
220 * Function: Locate the architecture-dependant contents of a fat
221 * file that match this CPU.
223 * Args: vp: The vnode for the fat file.
224 * header: A pointer to the fat file header.
225 * archret (out): Pointer to fat_arch structure to hold
228 * Returns: KERN_SUCCESS: Valid architecture found.
229 * KERN_FAILURE: No valid architecture found.
230 **********************************************************************/
234 vm_offset_t data_ptr
,
235 struct fat_arch
*archret
)
237 return fatfile_getarch2(vp
, data_ptr
, cpu_type(), 0, archret
);
240 /**********************************************************************
241 * Routine: fatfile_getarch_with_bits()
243 * Function: Locate the architecture-dependant contents of a fat
244 * file that match this CPU.
246 * Args: vp: The vnode for the fat file.
247 * archbits: Architecture specific feature bits
248 * header: A pointer to the fat file header.
249 * archret (out): Pointer to fat_arch structure to hold
252 * Returns: KERN_SUCCESS: Valid architecture found.
253 * KERN_FAILURE: No valid architecture found.
254 **********************************************************************/
256 fatfile_getarch_with_bits(
259 vm_offset_t data_ptr
,
260 struct fat_arch
*archret
)
262 return fatfile_getarch2(vp
, data_ptr
, archbits
| cpu_type(), 0, archret
);