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 <architecture/byte_order.h>
49 /* XXX should be in common header */
50 extern int grade_binary(cpu_type_t exectype
, cpu_subtype_t execsubtype
);
52 #define CPU_TYPE_NATIVE (cpu_type())
53 #define CPU_TYPE_CLASSIC CPU_TYPE_POWERPC
55 /**********************************************************************
56 * Routine: fatfile_getarch2()
58 * Function: Locate the architecture-dependant contents of a fat
59 * file that match this CPU.
61 * Args: vp: The vnode for the fat file.
62 * header: A pointer to the fat file header.
63 * req_cpu_type: The required cpu type.
64 * mask_bits: Bits to mask from the sub-image type when
65 * grading it vs. the req_cpu_type
66 * archret (out): Pointer to fat_arch structure to hold
69 * Returns: KERN_SUCCESS: Valid architecture found.
70 * KERN_FAILURE: No valid architecture found.
71 **********************************************************************/
77 __unused
struct vnode
*vp
,
80 cpu_type_t req_cpu_type
,
82 struct fat_arch
*archret
)
84 /* vm_pager_t pager; */
88 struct fat_arch
*arch
;
89 struct fat_arch
*best_arch
;
94 struct fat_header
*header
;
100 * Get the pager for the file.
103 header
= (struct fat_header
*)data_ptr
;
106 * Map portion that must be accessible directly into
109 nfat_arch
= NXSwapBigLongToHost(header
->nfat_arch
);
111 end_of_archs
= (off_t
)nfat_arch
* sizeof(struct fat_arch
) +
112 sizeof(struct fat_header
);
114 filesize
= ubc_getsize(vp
);
115 if (end_of_archs
> (int)filesize
) {
116 return(LOAD_BADMACHO
);
121 * This check is limited on the top end because we are reading
122 * only PAGE_SIZE bytes
124 if (end_of_archs
> PAGE_SIZE
||
125 end_of_archs
< (sizeof(struct fat_header
)+sizeof(struct fat_arch
)))
126 return(LOAD_BADMACHO
);
128 * Round size of fat_arch structures up to page boundry.
130 size
= round_page_32(end_of_archs
);
132 return(LOAD_BADMACHO
);
135 * Scan the fat_arch's looking for the best one.
140 arch
= (struct fat_arch
*) (addr
+ sizeof(struct fat_header
));
141 for (; nfat_arch
-- > 0; arch
++) {
144 * Check to see if right cpu type.
146 if(((cpu_type_t
)NXSwapBigIntToHost(arch
->cputype
) & ~mask_bits
) != req_cpu_type
)
150 * Get the grade of the cpu subtype.
152 grade
= grade_binary(
153 NXSwapBigIntToHost(arch
->cputype
),
154 NXSwapBigIntToHost(arch
->cpusubtype
));
157 * Remember it if it's the best we've seen.
159 if (grade
> best_grade
) {
166 * Return our results.
168 if (best_arch
== NULL
) {
172 NXSwapBigIntToHost(best_arch
->cputype
);
173 archret
->cpusubtype
=
174 NXSwapBigIntToHost(best_arch
->cpusubtype
);
176 NXSwapBigLongToHost(best_arch
->offset
);
178 NXSwapBigLongToHost(best_arch
->size
);
180 NXSwapBigLongToHost(best_arch
->align
);
186 * Free the memory we allocated and return.
191 extern char classichandler
[];
194 fatfile_getarch_affinity(
196 vm_offset_t data_ptr
,
197 struct fat_arch
*archret
,
201 int handler
= (classichandler
[0] != 0);
202 cpu_type_t primary_type
, fallback_type
;
204 if (handler
&& affinity
) {
205 primary_type
= CPU_TYPE_CLASSIC
;
206 fallback_type
= CPU_TYPE_NATIVE
;
208 primary_type
= CPU_TYPE_NATIVE
;
209 fallback_type
= CPU_TYPE_CLASSIC
;
212 * Ignore the architectural bits when determining if an image
213 * in a fat file should be skipped or graded.
215 lret
= fatfile_getarch2(vp
, data_ptr
, primary_type
, CPU_ARCH_MASK
, archret
);
216 if ((lret
!= 0) && handler
) {
217 lret
= fatfile_getarch2(vp
, data_ptr
, fallback_type
,
223 /**********************************************************************
224 * Routine: fatfile_getarch()
226 * Function: Locate the architecture-dependant contents of a fat
227 * file that match this CPU.
229 * Args: vp: The vnode for the fat file.
230 * header: A pointer to the fat file header.
231 * archret (out): Pointer to fat_arch structure to hold
234 * Returns: KERN_SUCCESS: Valid architecture found.
235 * KERN_FAILURE: No valid architecture found.
236 **********************************************************************/
240 vm_offset_t data_ptr
,
241 struct fat_arch
*archret
)
243 return fatfile_getarch2(vp
, data_ptr
, CPU_TYPE_NATIVE
, 0, archret
);
246 /**********************************************************************
247 * Routine: fatfile_getarch_with_bits()
249 * Function: Locate the architecture-dependant contents of a fat
250 * file that match this CPU.
252 * Args: vp: The vnode for the fat file.
253 * archbits: Architecture specific feature bits
254 * header: A pointer to the fat file header.
255 * archret (out): Pointer to fat_arch structure to hold
258 * Returns: KERN_SUCCESS: Valid architecture found.
259 * KERN_FAILURE: No valid architecture found.
260 **********************************************************************/
262 fatfile_getarch_with_bits(
265 vm_offset_t data_ptr
,
266 struct fat_arch
*archret
)
268 return fatfile_getarch2(vp
, data_ptr
, archbits
| CPU_TYPE_NATIVE
, 0, archret
);