]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/mach_fat.c
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 #define CPU_TYPE_NATIVE (machine_slot[cpu_number()].cpu_type)
44 #define CPU_TYPE_CLASSIC CPU_TYPE_POWERPC
46 /**********************************************************************
47 * Routine: fatfile_getarch2()
49 * Function: Locate the architecture-dependant contents of a fat
50 * file that match this CPU.
52 * Args: vp: The vnode for the fat file.
53 * header: A pointer to the fat file header.
54 * cpu_type: The required 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 struct fat_arch
*archret
)
68 /* vm_pager_t pager; */
73 struct fat_arch
*arch
;
74 struct fat_arch
*best_arch
;
79 struct fat_header
*header
;
83 * Get the pager for the file.
86 header
= (struct fat_header
*)data_ptr
;
89 * Map portion that must be accessible directly into
92 nfat_arch
= NXSwapBigLongToHost(header
->nfat_arch
);
94 end_of_archs
= sizeof(struct fat_header
)
95 + nfat_arch
* sizeof(struct fat_arch
);
97 filesize
= ubc_getsize(vp
);
98 if (end_of_archs
> (int)filesize
) {
99 return(LOAD_BADMACHO
);
103 /* This is beacuse we are reading only 512 bytes */
105 if (end_of_archs
> 512)
106 return(LOAD_BADMACHO
);
108 * Round size of fat_arch structures up to page boundry.
110 size
= round_page_32(end_of_archs
);
112 return(LOAD_BADMACHO
);
115 * Scan the fat_arch's looking for the best one.
120 arch
= (struct fat_arch
*) (addr
+ sizeof(struct fat_header
));
121 for (; nfat_arch
-- > 0; arch
++) {
124 * Check to see if right cpu type.
126 if(NXSwapBigIntToHost(arch
->cputype
) != cpu_type
)
130 * Get the grade of the cpu subtype.
132 grade
= grade_cpu_subtype(
133 NXSwapBigIntToHost(arch
->cpusubtype
));
136 * Remember it if it's the best we've seen.
138 if (grade
> best_grade
) {
145 * Return our results.
147 if (best_arch
== NULL
) {
151 NXSwapBigIntToHost(best_arch
->cputype
);
152 archret
->cpusubtype
=
153 NXSwapBigIntToHost(best_arch
->cpusubtype
);
155 NXSwapBigLongToHost(best_arch
->offset
);
157 NXSwapBigLongToHost(best_arch
->size
);
159 NXSwapBigLongToHost(best_arch
->align
);
165 * Free the memory we allocated and return.
170 extern char classichandler
[];
173 fatfile_getarch_affinity(
175 vm_offset_t data_ptr
,
176 struct fat_arch
*archret
,
180 int handler
= (classichandler
[0] != 0);
181 cpu_type_t primary_type
, fallback_type
;
183 if (handler
&& affinity
) {
184 primary_type
= CPU_TYPE_CLASSIC
;
185 fallback_type
= CPU_TYPE_NATIVE
;
187 primary_type
= CPU_TYPE_NATIVE
;
188 fallback_type
= CPU_TYPE_CLASSIC
;
190 lret
= fatfile_getarch2(vp
, data_ptr
, primary_type
, archret
);
191 if ((lret
!= 0) && handler
) {
192 lret
= fatfile_getarch2(vp
, data_ptr
, fallback_type
,
198 /**********************************************************************
199 * Routine: fatfile_getarch()
201 * Function: Locate the architecture-dependant contents of a fat
202 * file that match this CPU.
204 * Args: vp: The vnode for the fat file.
205 * header: A pointer to the fat file header.
206 * archret (out): Pointer to fat_arch structure to hold
209 * Returns: KERN_SUCCESS: Valid architecture found.
210 * KERN_FAILURE: No valid architecture found.
211 **********************************************************************/
215 vm_offset_t data_ptr
,
216 struct fat_arch
*archret
)
218 return fatfile_getarch2(vp
, data_ptr
, CPU_TYPE_NATIVE
, archret
);