2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
25 * File: kern/mach_fat.c
28 * Fat file support routines.
32 #include <sys/param.h>
33 #include <sys/types.h>
35 #include <sys/vnode.h>
36 #include <vm/vm_kern.h>
37 #include <mach/kern_return.h>
38 #include <mach/vm_param.h>
39 #include <kern/cpu_number.h>
40 #include <mach-o/fat.h>
41 #include <kern/mach_loader.h>
42 #include <architecture/byte_order.h>
44 /* XXX should be in common header */
45 extern int grade_binary(cpu_type_t exectype
, cpu_subtype_t execsubtype
);
47 #define CPU_TYPE_NATIVE (cpu_type())
48 #define CPU_TYPE_CLASSIC CPU_TYPE_POWERPC
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
= NXSwapBigLongToHost(header
->nfat_arch
);
106 end_of_archs
= sizeof(struct fat_header
)
107 + nfat_arch
* sizeof(struct fat_arch
);
109 filesize
= ubc_getsize(vp
);
110 if (end_of_archs
> (int)filesize
) {
111 return(LOAD_BADMACHO
);
115 /* This is beacuse we are reading only 512 bytes */
117 if (end_of_archs
> 512)
118 return(LOAD_BADMACHO
);
120 * Round size of fat_arch structures up to page boundry.
122 size
= round_page_32(end_of_archs
);
124 return(LOAD_BADMACHO
);
127 * Scan the fat_arch's looking for the best one.
132 arch
= (struct fat_arch
*) (addr
+ sizeof(struct fat_header
));
133 for (; nfat_arch
-- > 0; arch
++) {
136 * Check to see if right cpu type.
138 if(((cpu_type_t
)NXSwapBigIntToHost(arch
->cputype
) & ~mask_bits
) != req_cpu_type
)
142 * Get the grade of the cpu subtype.
144 grade
= grade_binary(
145 NXSwapBigIntToHost(arch
->cputype
),
146 NXSwapBigIntToHost(arch
->cpusubtype
));
149 * Remember it if it's the best we've seen.
151 if (grade
> best_grade
) {
158 * Return our results.
160 if (best_arch
== NULL
) {
164 NXSwapBigIntToHost(best_arch
->cputype
);
165 archret
->cpusubtype
=
166 NXSwapBigIntToHost(best_arch
->cpusubtype
);
168 NXSwapBigLongToHost(best_arch
->offset
);
170 NXSwapBigLongToHost(best_arch
->size
);
172 NXSwapBigLongToHost(best_arch
->align
);
178 * Free the memory we allocated and return.
183 extern char classichandler
[];
186 fatfile_getarch_affinity(
188 vm_offset_t data_ptr
,
189 struct fat_arch
*archret
,
193 int handler
= (classichandler
[0] != 0);
194 cpu_type_t primary_type
, fallback_type
;
196 if (handler
&& affinity
) {
197 primary_type
= CPU_TYPE_CLASSIC
;
198 fallback_type
= CPU_TYPE_NATIVE
;
200 primary_type
= CPU_TYPE_NATIVE
;
201 fallback_type
= CPU_TYPE_CLASSIC
;
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_NATIVE
, 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_NATIVE
, 0, archret
);