2 * Copyright (c) 2006 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 <architecture/byte_order.h>
51 /* XXX should be in common header */
52 extern int grade_binary(cpu_type_t exectype
, cpu_subtype_t execsubtype
);
54 #define CPU_TYPE_NATIVE (cpu_type())
55 #define CPU_TYPE_CLASSIC CPU_TYPE_POWERPC
57 /**********************************************************************
58 * Routine: fatfile_getarch2()
60 * Function: Locate the architecture-dependant contents of a fat
61 * file that match this CPU.
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
71 * Returns: KERN_SUCCESS: Valid architecture found.
72 * KERN_FAILURE: No valid architecture found.
73 **********************************************************************/
79 __unused
struct vnode
*vp
,
82 cpu_type_t req_cpu_type
,
84 struct fat_arch
*archret
)
86 /* vm_pager_t pager; */
90 struct fat_arch
*arch
;
91 struct fat_arch
*best_arch
;
96 struct fat_header
*header
;
102 * Get the pager for the file.
105 header
= (struct fat_header
*)data_ptr
;
108 * Map portion that must be accessible directly into
111 nfat_arch
= NXSwapBigLongToHost(header
->nfat_arch
);
113 end_of_archs
= sizeof(struct fat_header
)
114 + nfat_arch
* sizeof(struct fat_arch
);
116 filesize
= ubc_getsize(vp
);
117 if (end_of_archs
> (int)filesize
) {
118 return(LOAD_BADMACHO
);
122 /* This is beacuse we are reading only 512 bytes */
124 if (end_of_archs
> 512)
125 return(LOAD_BADMACHO
);
127 * Round size of fat_arch structures up to page boundry.
129 size
= round_page_32(end_of_archs
);
131 return(LOAD_BADMACHO
);
134 * Scan the fat_arch's looking for the best one.
139 arch
= (struct fat_arch
*) (addr
+ sizeof(struct fat_header
));
140 for (; nfat_arch
-- > 0; arch
++) {
143 * Check to see if right cpu type.
145 if(((cpu_type_t
)NXSwapBigIntToHost(arch
->cputype
) & ~mask_bits
) != req_cpu_type
)
149 * Get the grade of the cpu subtype.
151 grade
= grade_binary(
152 NXSwapBigIntToHost(arch
->cputype
),
153 NXSwapBigIntToHost(arch
->cpusubtype
));
156 * Remember it if it's the best we've seen.
158 if (grade
> best_grade
) {
165 * Return our results.
167 if (best_arch
== NULL
) {
171 NXSwapBigIntToHost(best_arch
->cputype
);
172 archret
->cpusubtype
=
173 NXSwapBigIntToHost(best_arch
->cpusubtype
);
175 NXSwapBigLongToHost(best_arch
->offset
);
177 NXSwapBigLongToHost(best_arch
->size
);
179 NXSwapBigLongToHost(best_arch
->align
);
185 * Free the memory we allocated and return.
190 extern char classichandler
[];
193 fatfile_getarch_affinity(
195 vm_offset_t data_ptr
,
196 struct fat_arch
*archret
,
200 int handler
= (classichandler
[0] != 0);
201 cpu_type_t primary_type
, fallback_type
;
203 if (handler
&& affinity
) {
204 primary_type
= CPU_TYPE_CLASSIC
;
205 fallback_type
= CPU_TYPE_NATIVE
;
207 primary_type
= CPU_TYPE_NATIVE
;
208 fallback_type
= CPU_TYPE_CLASSIC
;
211 * Ignore the architectural bits when determining if an image
212 * in a fat file should be skipped or graded.
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
,
222 /**********************************************************************
223 * Routine: fatfile_getarch()
225 * Function: Locate the architecture-dependant contents of a fat
226 * file that match this CPU.
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
233 * Returns: KERN_SUCCESS: Valid architecture found.
234 * KERN_FAILURE: No valid architecture found.
235 **********************************************************************/
239 vm_offset_t data_ptr
,
240 struct fat_arch
*archret
)
242 return fatfile_getarch2(vp
, data_ptr
, CPU_TYPE_NATIVE
, 0, archret
);
245 /**********************************************************************
246 * Routine: fatfile_getarch_with_bits()
248 * Function: Locate the architecture-dependant contents of a fat
249 * file that match this CPU.
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
257 * Returns: KERN_SUCCESS: Valid architecture found.
258 * KERN_FAILURE: No valid architecture found.
259 **********************************************************************/
261 fatfile_getarch_with_bits(
264 vm_offset_t data_ptr
,
265 struct fat_arch
*archret
)
267 return fatfile_getarch2(vp
, data_ptr
, archbits
| CPU_TYPE_NATIVE
, 0, archret
);