2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
24 * File: kern/mach_fat.c
27 * Fat file support routines.
31 #include <sys/param.h>
32 #include <sys/types.h>
34 #include <sys/vnode.h>
35 #include <vm/vm_kern.h>
36 #include <mach/kern_return.h>
37 #include <mach/vm_param.h>
38 #include <kern/cpu_number.h>
39 #include <mach-o/fat.h>
40 #include <kern/mach_loader.h>
41 #include <architecture/byte_order.h>
43 /* XXX should be in common header */
44 extern int grade_binary(cpu_type_t exectype
, cpu_subtype_t execsubtype
);
46 #define CPU_TYPE_NATIVE (cpu_type())
47 #define CPU_TYPE_CLASSIC CPU_TYPE_POWERPC
49 /**********************************************************************
50 * Routine: fatfile_getarch2()
52 * Function: Locate the architecture-dependant contents of a fat
53 * file that match this CPU.
55 * Args: vp: The vnode for the fat file.
56 * header: A pointer to the fat file header.
57 * req_cpu_type: The required cpu type.
58 * mask_bits: Bits to mask from the sub-image type when
59 * grading it vs. the req_cpu_type
60 * archret (out): Pointer to fat_arch structure to hold
63 * Returns: KERN_SUCCESS: Valid architecture found.
64 * KERN_FAILURE: No valid architecture found.
65 **********************************************************************/
71 __unused
struct vnode
*vp
,
74 cpu_type_t req_cpu_type
,
76 struct fat_arch
*archret
)
78 /* vm_pager_t pager; */
82 struct fat_arch
*arch
;
83 struct fat_arch
*best_arch
;
88 struct fat_header
*header
;
94 * Get the pager for the file.
97 header
= (struct fat_header
*)data_ptr
;
100 * Map portion that must be accessible directly into
103 nfat_arch
= NXSwapBigLongToHost(header
->nfat_arch
);
105 end_of_archs
= sizeof(struct fat_header
)
106 + nfat_arch
* sizeof(struct fat_arch
);
108 filesize
= ubc_getsize(vp
);
109 if (end_of_archs
> (int)filesize
) {
110 return(LOAD_BADMACHO
);
114 /* This is beacuse we are reading only 512 bytes */
116 if (end_of_archs
> 512)
117 return(LOAD_BADMACHO
);
119 * Round size of fat_arch structures up to page boundry.
121 size
= round_page_32(end_of_archs
);
123 return(LOAD_BADMACHO
);
126 * Scan the fat_arch's looking for the best one.
131 arch
= (struct fat_arch
*) (addr
+ sizeof(struct fat_header
));
132 for (; nfat_arch
-- > 0; arch
++) {
135 * Check to see if right cpu type.
137 if(((cpu_type_t
)NXSwapBigIntToHost(arch
->cputype
) & ~mask_bits
) != req_cpu_type
)
141 * Get the grade of the cpu subtype.
143 grade
= grade_binary(
144 NXSwapBigIntToHost(arch
->cputype
),
145 NXSwapBigIntToHost(arch
->cpusubtype
));
148 * Remember it if it's the best we've seen.
150 if (grade
> best_grade
) {
157 * Return our results.
159 if (best_arch
== NULL
) {
163 NXSwapBigIntToHost(best_arch
->cputype
);
164 archret
->cpusubtype
=
165 NXSwapBigIntToHost(best_arch
->cpusubtype
);
167 NXSwapBigLongToHost(best_arch
->offset
);
169 NXSwapBigLongToHost(best_arch
->size
);
171 NXSwapBigLongToHost(best_arch
->align
);
177 * Free the memory we allocated and return.
182 extern char classichandler
[];
185 fatfile_getarch_affinity(
187 vm_offset_t data_ptr
,
188 struct fat_arch
*archret
,
192 int handler
= (classichandler
[0] != 0);
193 cpu_type_t primary_type
, fallback_type
;
195 if (handler
&& affinity
) {
196 primary_type
= CPU_TYPE_CLASSIC
;
197 fallback_type
= CPU_TYPE_NATIVE
;
199 primary_type
= CPU_TYPE_NATIVE
;
200 fallback_type
= CPU_TYPE_CLASSIC
;
203 * Ignore the architectural bits when determining if an image
204 * in a fat file should be skipped or graded.
206 lret
= fatfile_getarch2(vp
, data_ptr
, primary_type
, CPU_ARCH_MASK
, archret
);
207 if ((lret
!= 0) && handler
) {
208 lret
= fatfile_getarch2(vp
, data_ptr
, fallback_type
,
214 /**********************************************************************
215 * Routine: fatfile_getarch()
217 * Function: Locate the architecture-dependant contents of a fat
218 * file that match this CPU.
220 * Args: vp: The vnode for the fat file.
221 * header: A pointer to the fat file header.
222 * archret (out): Pointer to fat_arch structure to hold
225 * Returns: KERN_SUCCESS: Valid architecture found.
226 * KERN_FAILURE: No valid architecture found.
227 **********************************************************************/
231 vm_offset_t data_ptr
,
232 struct fat_arch
*archret
)
234 return fatfile_getarch2(vp
, data_ptr
, CPU_TYPE_NATIVE
, 0, archret
);
237 /**********************************************************************
238 * Routine: fatfile_getarch_with_bits()
240 * Function: Locate the architecture-dependant contents of a fat
241 * file that match this CPU.
243 * Args: vp: The vnode for the fat file.
244 * archbits: Architecture specific feature bits
245 * header: A pointer to the fat file header.
246 * archret (out): Pointer to fat_arch structure to hold
249 * Returns: KERN_SUCCESS: Valid architecture found.
250 * KERN_FAILURE: No valid architecture found.
251 **********************************************************************/
253 fatfile_getarch_with_bits(
256 vm_offset_t data_ptr
,
257 struct fat_arch
*archret
)
259 return fatfile_getarch2(vp
, data_ptr
, archbits
| CPU_TYPE_NATIVE
, 0, archret
);