]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/mach_fat.c
96669065a9fb0ca712f0644aafdbe55b33bed296
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
27 * File: kern/mach_fat.c
30 * Fat file support routines.
34 #include <sys/param.h>
35 #include <sys/types.h>
37 #include <sys/vnode.h>
38 #include <vm/vm_kern.h>
39 #include <mach/kern_return.h>
40 #include <mach/vm_param.h>
41 #include <kern/cpu_number.h>
42 #include <mach-o/fat.h>
43 #include <kern/mach_loader.h>
44 #include <architecture/byte_order.h>
46 #define CPU_TYPE_NATIVE (machine_slot[cpu_number()].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 * cpu_type: The required cpu type.
58 * archret (out): Pointer to fat_arch structure to hold
61 * Returns: KERN_SUCCESS: Valid architecture found.
62 * KERN_FAILURE: No valid architecture found.
63 **********************************************************************/
69 struct fat_arch
*archret
)
71 /* vm_pager_t pager; */
76 struct fat_arch
*arch
;
77 struct fat_arch
*best_arch
;
82 struct fat_header
*header
;
86 * Get the pager for the file.
89 header
= (struct fat_header
*)data_ptr
;
92 * Map portion that must be accessible directly into
95 nfat_arch
= NXSwapBigLongToHost(header
->nfat_arch
);
97 end_of_archs
= sizeof(struct fat_header
)
98 + nfat_arch
* sizeof(struct fat_arch
);
100 filesize
= ubc_getsize(vp
);
101 if (end_of_archs
> (int)filesize
) {
102 return(LOAD_BADMACHO
);
106 /* This is beacuse we are reading only 512 bytes */
108 if (end_of_archs
> 512)
109 return(LOAD_BADMACHO
);
111 * Round size of fat_arch structures up to page boundry.
113 size
= round_page_32(end_of_archs
);
115 return(LOAD_BADMACHO
);
118 * Scan the fat_arch's looking for the best one.
123 arch
= (struct fat_arch
*) (addr
+ sizeof(struct fat_header
));
124 for (; nfat_arch
-- > 0; arch
++) {
127 * Check to see if right cpu type.
129 if(NXSwapBigIntToHost(arch
->cputype
) != cpu_type
)
133 * Get the grade of the cpu subtype.
135 grade
= grade_cpu_subtype(
136 NXSwapBigIntToHost(arch
->cpusubtype
));
139 * Remember it if it's the best we've seen.
141 if (grade
> best_grade
) {
148 * Return our results.
150 if (best_arch
== NULL
) {
154 NXSwapBigIntToHost(best_arch
->cputype
);
155 archret
->cpusubtype
=
156 NXSwapBigIntToHost(best_arch
->cpusubtype
);
158 NXSwapBigLongToHost(best_arch
->offset
);
160 NXSwapBigLongToHost(best_arch
->size
);
162 NXSwapBigLongToHost(best_arch
->align
);
168 * Free the memory we allocated and return.
173 extern char classichandler
[];
176 fatfile_getarch_affinity(
178 vm_offset_t data_ptr
,
179 struct fat_arch
*archret
,
183 int handler
= (classichandler
[0] != 0);
184 cpu_type_t primary_type
, fallback_type
;
186 if (handler
&& affinity
) {
187 primary_type
= CPU_TYPE_CLASSIC
;
188 fallback_type
= CPU_TYPE_NATIVE
;
190 primary_type
= CPU_TYPE_NATIVE
;
191 fallback_type
= CPU_TYPE_CLASSIC
;
193 lret
= fatfile_getarch2(vp
, data_ptr
, primary_type
, archret
);
194 if ((lret
!= 0) && handler
) {
195 lret
= fatfile_getarch2(vp
, data_ptr
, fallback_type
,
201 /**********************************************************************
202 * Routine: fatfile_getarch()
204 * Function: Locate the architecture-dependant contents of a fat
205 * file that match this CPU.
207 * Args: vp: The vnode for the fat file.
208 * header: A pointer to the fat file header.
209 * archret (out): Pointer to fat_arch structure to hold
212 * Returns: KERN_SUCCESS: Valid architecture found.
213 * KERN_FAILURE: No valid architecture found.
214 **********************************************************************/
218 vm_offset_t data_ptr
,
219 struct fat_arch
*archret
)
221 return fatfile_getarch2(vp
, data_ptr
, CPU_TYPE_NATIVE
, archret
);