]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/mach_fat.c
xnu-792.6.56.tar.gz
[apple/xnu.git] / bsd / kern / mach_fat.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
24 *
25 * File: kern/mach_fat.c
26 * Author: Peter King
27 *
28 * Fat file support routines.
29 *
30 */
31
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/uio.h>
35 #include <sys/vnode.h>
36 #include <vm/vm_kern.h>
37 #include <mach/kern_return.h>
38 #include <mach/vm_param.h>
39 #include <kern/cpu_number.h>
40 #include <mach-o/fat.h>
41 #include <kern/mach_loader.h>
42 #include <architecture/byte_order.h>
43
44 /* XXX should be in common header */
45 extern int grade_binary(cpu_type_t exectype, cpu_subtype_t execsubtype);
46
47 #define CPU_TYPE_NATIVE (cpu_type())
48 #define CPU_TYPE_CLASSIC CPU_TYPE_POWERPC
49
50 /**********************************************************************
51 * Routine: fatfile_getarch2()
52 *
53 * Function: Locate the architecture-dependant contents of a fat
54 * file that match this CPU.
55 *
56 * Args: vp: The vnode for the fat file.
57 * header: A pointer to the fat file header.
58 * req_cpu_type: The required cpu type.
59 * mask_bits: Bits to mask from the sub-image type when
60 * grading it vs. the req_cpu_type
61 * archret (out): Pointer to fat_arch structure to hold
62 * the results.
63 *
64 * Returns: KERN_SUCCESS: Valid architecture found.
65 * KERN_FAILURE: No valid architecture found.
66 **********************************************************************/
67 static load_return_t
68 fatfile_getarch2(
69 #if 0
70 struct vnode *vp,
71 #else
72 __unused struct vnode *vp,
73 #endif
74 vm_offset_t data_ptr,
75 cpu_type_t req_cpu_type,
76 cpu_type_t mask_bits,
77 struct fat_arch *archret)
78 {
79 /* vm_pager_t pager; */
80 vm_offset_t addr;
81 vm_size_t size;
82 load_return_t lret;
83 struct fat_arch *arch;
84 struct fat_arch *best_arch;
85 int grade;
86 int best_grade;
87 int nfat_arch;
88 int end_of_archs;
89 struct fat_header *header;
90 #if 0
91 off_t filesize;
92 #endif
93
94 /*
95 * Get the pager for the file.
96 */
97
98 header = (struct fat_header *)data_ptr;
99
100 /*
101 * Map portion that must be accessible directly into
102 * kernel's map.
103 */
104 nfat_arch = NXSwapBigLongToHost(header->nfat_arch);
105
106 end_of_archs = sizeof(struct fat_header)
107 + nfat_arch * sizeof(struct fat_arch);
108 #if 0
109 filesize = ubc_getsize(vp);
110 if (end_of_archs > (int)filesize) {
111 return(LOAD_BADMACHO);
112 }
113 #endif
114
115 /* This is beacuse we are reading only 512 bytes */
116
117 if (end_of_archs > 512)
118 return(LOAD_BADMACHO);
119 /*
120 * Round size of fat_arch structures up to page boundry.
121 */
122 size = round_page_32(end_of_archs);
123 if (size == 0)
124 return(LOAD_BADMACHO);
125
126 /*
127 * Scan the fat_arch's looking for the best one.
128 */
129 addr = data_ptr;
130 best_arch = NULL;
131 best_grade = 0;
132 arch = (struct fat_arch *) (addr + sizeof(struct fat_header));
133 for (; nfat_arch-- > 0; arch++) {
134
135 /*
136 * Check to see if right cpu type.
137 */
138 if(((cpu_type_t)NXSwapBigIntToHost(arch->cputype) & ~mask_bits) != req_cpu_type)
139 continue;
140
141 /*
142 * Get the grade of the cpu subtype.
143 */
144 grade = grade_binary(
145 NXSwapBigIntToHost(arch->cputype),
146 NXSwapBigIntToHost(arch->cpusubtype));
147
148 /*
149 * Remember it if it's the best we've seen.
150 */
151 if (grade > best_grade) {
152 best_grade = grade;
153 best_arch = arch;
154 }
155 }
156
157 /*
158 * Return our results.
159 */
160 if (best_arch == NULL) {
161 lret = LOAD_BADARCH;
162 } else {
163 archret->cputype =
164 NXSwapBigIntToHost(best_arch->cputype);
165 archret->cpusubtype =
166 NXSwapBigIntToHost(best_arch->cpusubtype);
167 archret->offset =
168 NXSwapBigLongToHost(best_arch->offset);
169 archret->size =
170 NXSwapBigLongToHost(best_arch->size);
171 archret->align =
172 NXSwapBigLongToHost(best_arch->align);
173
174 lret = LOAD_SUCCESS;
175 }
176
177 /*
178 * Free the memory we allocated and return.
179 */
180 return(lret);
181 }
182
183 extern char classichandler[];
184
185 load_return_t
186 fatfile_getarch_affinity(
187 struct vnode *vp,
188 vm_offset_t data_ptr,
189 struct fat_arch *archret,
190 int affinity)
191 {
192 load_return_t lret;
193 int handler = (classichandler[0] != 0);
194 cpu_type_t primary_type, fallback_type;
195
196 if (handler && affinity) {
197 primary_type = CPU_TYPE_CLASSIC;
198 fallback_type = CPU_TYPE_NATIVE;
199 } else {
200 primary_type = CPU_TYPE_NATIVE;
201 fallback_type = CPU_TYPE_CLASSIC;
202 }
203 /*
204 * Ignore the architectural bits when determining if an image
205 * in a fat file should be skipped or graded.
206 */
207 lret = fatfile_getarch2(vp, data_ptr, primary_type, CPU_ARCH_MASK, archret);
208 if ((lret != 0) && handler) {
209 lret = fatfile_getarch2(vp, data_ptr, fallback_type,
210 0, archret);
211 }
212 return lret;
213 }
214
215 /**********************************************************************
216 * Routine: fatfile_getarch()
217 *
218 * Function: Locate the architecture-dependant contents of a fat
219 * file that match this CPU.
220 *
221 * Args: vp: The vnode for the fat file.
222 * header: A pointer to the fat file header.
223 * archret (out): Pointer to fat_arch structure to hold
224 * the results.
225 *
226 * Returns: KERN_SUCCESS: Valid architecture found.
227 * KERN_FAILURE: No valid architecture found.
228 **********************************************************************/
229 load_return_t
230 fatfile_getarch(
231 struct vnode *vp,
232 vm_offset_t data_ptr,
233 struct fat_arch *archret)
234 {
235 return fatfile_getarch2(vp, data_ptr, CPU_TYPE_NATIVE, 0, archret);
236 }
237
238 /**********************************************************************
239 * Routine: fatfile_getarch_with_bits()
240 *
241 * Function: Locate the architecture-dependant contents of a fat
242 * file that match this CPU.
243 *
244 * Args: vp: The vnode for the fat file.
245 * archbits: Architecture specific feature bits
246 * header: A pointer to the fat file header.
247 * archret (out): Pointer to fat_arch structure to hold
248 * the results.
249 *
250 * Returns: KERN_SUCCESS: Valid architecture found.
251 * KERN_FAILURE: No valid architecture found.
252 **********************************************************************/
253 load_return_t
254 fatfile_getarch_with_bits(
255 struct vnode *vp,
256 integer_t archbits,
257 vm_offset_t data_ptr,
258 struct fat_arch *archret)
259 {
260 return fatfile_getarch2(vp, data_ptr, archbits | CPU_TYPE_NATIVE, 0, archret);
261 }
262