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