]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/mach_fat.c
7c9baaeec92c70c7283bc28d6ab61ec920472c22
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 <libkern/OSByteOrder.h>
42 #include <machine/exec.h>
44 /**********************************************************************
45 * Routine: fatfile_getarch2()
47 * Function: Locate the architecture-dependant contents of a fat
48 * file that match this CPU.
50 * Args: vp: The vnode for the fat file.
51 * header: A pointer to the fat file header.
52 * req_cpu_type: The required cpu type.
53 * mask_bits: Bits to mask from the sub-image type when
54 * grading it vs. the req_cpu_type
55 * archret (out): Pointer to fat_arch structure to hold
58 * Returns: KERN_SUCCESS: Valid architecture found.
59 * KERN_FAILURE: No valid architecture found.
60 **********************************************************************/
66 __unused
struct vnode
*vp
,
69 cpu_type_t req_cpu_type
,
71 struct fat_arch
*archret
)
73 /* vm_pager_t pager; */
77 struct fat_arch
*arch
;
78 struct fat_arch
*best_arch
;
83 struct fat_header
*header
;
89 * Get the pager for the file.
92 header
= (struct fat_header
*)data_ptr
;
95 * Map portion that must be accessible directly into
98 nfat_arch
= OSSwapBigToHostInt32(header
->nfat_arch
);
100 end_of_archs
= sizeof(struct fat_header
)
101 + nfat_arch
* sizeof(struct fat_arch
);
103 filesize
= ubc_getsize(vp
);
104 if (end_of_archs
> (int)filesize
) {
105 return(LOAD_BADMACHO
);
109 /* This is beacuse we are reading only 512 bytes */
111 if (end_of_archs
> 512)
112 return(LOAD_BADMACHO
);
114 * Round size of fat_arch structures up to page boundry.
116 size
= round_page_32(end_of_archs
);
118 return(LOAD_BADMACHO
);
121 * Scan the fat_arch's looking for the best one.
126 arch
= (struct fat_arch
*) (addr
+ sizeof(struct fat_header
));
127 for (; nfat_arch
-- > 0; arch
++) {
130 * Check to see if right cpu type.
132 if(((cpu_type_t
)OSSwapBigToHostInt32(arch
->cputype
) & ~mask_bits
) != req_cpu_type
)
136 * Get the grade of the cpu subtype.
138 grade
= grade_binary(
139 OSSwapBigToHostInt32(arch
->cputype
),
140 OSSwapBigToHostInt32(arch
->cpusubtype
));
143 * Remember it if it's the best we've seen.
145 if (grade
> best_grade
) {
152 * Return our results.
154 if (best_arch
== NULL
) {
158 OSSwapBigToHostInt32(best_arch
->cputype
);
159 archret
->cpusubtype
=
160 OSSwapBigToHostInt32(best_arch
->cpusubtype
);
162 OSSwapBigToHostInt32(best_arch
->offset
);
164 OSSwapBigToHostInt32(best_arch
->size
);
166 OSSwapBigToHostInt32(best_arch
->align
);
172 * Free the memory we allocated and return.
178 fatfile_getarch_affinity(
180 vm_offset_t data_ptr
,
181 struct fat_arch
*archret
,
185 int handler
= (exec_archhandler_ppc
.path
[0] != 0);
186 cpu_type_t primary_type
, fallback_type
;
188 if (handler
&& affinity
) {
189 primary_type
= CPU_TYPE_POWERPC
;
190 fallback_type
= cpu_type();
192 primary_type
= cpu_type();
193 fallback_type
= CPU_TYPE_POWERPC
;
196 * Ignore the architectural bits when determining if an image
197 * in a fat file should be skipped or graded.
199 lret
= fatfile_getarch2(vp
, data_ptr
, primary_type
, CPU_ARCH_MASK
, archret
);
200 if ((lret
!= 0) && handler
) {
201 lret
= fatfile_getarch2(vp
, data_ptr
, fallback_type
,
207 /**********************************************************************
208 * Routine: fatfile_getarch()
210 * Function: Locate the architecture-dependant contents of a fat
211 * file that match this CPU.
213 * Args: vp: The vnode for the fat file.
214 * header: A pointer to the fat file header.
215 * archret (out): Pointer to fat_arch structure to hold
218 * Returns: KERN_SUCCESS: Valid architecture found.
219 * KERN_FAILURE: No valid architecture found.
220 **********************************************************************/
224 vm_offset_t data_ptr
,
225 struct fat_arch
*archret
)
227 return fatfile_getarch2(vp
, data_ptr
, cpu_type(), 0, archret
);
230 /**********************************************************************
231 * Routine: fatfile_getarch_with_bits()
233 * Function: Locate the architecture-dependant contents of a fat
234 * file that match this CPU.
236 * Args: vp: The vnode for the fat file.
237 * archbits: Architecture specific feature bits
238 * header: A pointer to the fat file header.
239 * archret (out): Pointer to fat_arch structure to hold
242 * Returns: KERN_SUCCESS: Valid architecture found.
243 * KERN_FAILURE: No valid architecture found.
244 **********************************************************************/
246 fatfile_getarch_with_bits(
249 vm_offset_t data_ptr
,
250 struct fat_arch
*archret
)
252 return fatfile_getarch2(vp
, data_ptr
, archbits
| cpu_type(), 0, archret
);