]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/mach_fat.c
ef4fe20b5bbe11c385ffcadf88803c173ee113c2
[apple/xnu.git] / bsd / kern / mach_fat.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
31 *
32 * File: kern/mach_fat.c
33 * Author: Peter King
34 *
35 * Fat file support routines.
36 *
37 */
38
39 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/uio.h>
42 #include <sys/vnode.h>
43 #include <vm/vm_kern.h>
44 #include <mach/kern_return.h>
45 #include <mach/vm_param.h>
46 #include <kern/cpu_number.h>
47 #include <mach-o/fat.h>
48 #include <kern/mach_loader.h>
49 #include <libkern/OSByteOrder.h>
50 #include <machine/exec.h>
51
52 /**********************************************************************
53 * Routine: fatfile_getarch2()
54 *
55 * Function: Locate the architecture-dependant contents of a fat
56 * file that match this CPU.
57 *
58 * Args: vp: The vnode for the fat file.
59 * header: A pointer to the fat file header.
60 * req_cpu_type: The required cpu type.
61 * mask_bits: Bits to mask from the sub-image type when
62 * grading it vs. the req_cpu_type
63 * archret (out): Pointer to fat_arch structure to hold
64 * the results.
65 *
66 * Returns: KERN_SUCCESS: Valid architecture found.
67 * KERN_FAILURE: No valid architecture found.
68 **********************************************************************/
69 static load_return_t
70 fatfile_getarch2(
71 #if 0
72 struct vnode *vp,
73 #else
74 __unused struct vnode *vp,
75 #endif
76 vm_offset_t data_ptr,
77 cpu_type_t req_cpu_type,
78 cpu_type_t mask_bits,
79 struct fat_arch *archret)
80 {
81 /* vm_pager_t pager; */
82 vm_offset_t addr;
83 vm_size_t size;
84 load_return_t lret;
85 struct fat_arch *arch;
86 struct fat_arch *best_arch;
87 int grade;
88 int best_grade;
89 int nfat_arch;
90 int end_of_archs;
91 struct fat_header *header;
92 #if 0
93 off_t filesize;
94 #endif
95
96 /*
97 * Get the pager for the file.
98 */
99
100 header = (struct fat_header *)data_ptr;
101
102 /*
103 * Map portion that must be accessible directly into
104 * kernel's map.
105 */
106 nfat_arch = OSSwapBigToHostInt32(header->nfat_arch);
107
108 end_of_archs = sizeof(struct fat_header)
109 + nfat_arch * sizeof(struct fat_arch);
110 #if 0
111 filesize = ubc_getsize(vp);
112 if (end_of_archs > (int)filesize) {
113 return(LOAD_BADMACHO);
114 }
115 #endif
116
117 /* This is beacuse we are reading only 512 bytes */
118
119 if (end_of_archs > 512)
120 return(LOAD_BADMACHO);
121 /*
122 * Round size of fat_arch structures up to page boundry.
123 */
124 size = round_page_32(end_of_archs);
125 if (size == 0)
126 return(LOAD_BADMACHO);
127
128 /*
129 * Scan the fat_arch's looking for the best one.
130 */
131 addr = data_ptr;
132 best_arch = NULL;
133 best_grade = 0;
134 arch = (struct fat_arch *) (addr + sizeof(struct fat_header));
135 for (; nfat_arch-- > 0; arch++) {
136
137 /*
138 * Check to see if right cpu type.
139 */
140 if(((cpu_type_t)OSSwapBigToHostInt32(arch->cputype) & ~mask_bits) != req_cpu_type)
141 continue;
142
143 /*
144 * Get the grade of the cpu subtype.
145 */
146 grade = grade_binary(
147 OSSwapBigToHostInt32(arch->cputype),
148 OSSwapBigToHostInt32(arch->cpusubtype));
149
150 /*
151 * Remember it if it's the best we've seen.
152 */
153 if (grade > best_grade) {
154 best_grade = grade;
155 best_arch = arch;
156 }
157 }
158
159 /*
160 * Return our results.
161 */
162 if (best_arch == NULL) {
163 lret = LOAD_BADARCH;
164 } else {
165 archret->cputype =
166 OSSwapBigToHostInt32(best_arch->cputype);
167 archret->cpusubtype =
168 OSSwapBigToHostInt32(best_arch->cpusubtype);
169 archret->offset =
170 OSSwapBigToHostInt32(best_arch->offset);
171 archret->size =
172 OSSwapBigToHostInt32(best_arch->size);
173 archret->align =
174 OSSwapBigToHostInt32(best_arch->align);
175
176 lret = LOAD_SUCCESS;
177 }
178
179 /*
180 * Free the memory we allocated and return.
181 */
182 return(lret);
183 }
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 = (exec_archhandler_ppc.path[0] != 0);
194 cpu_type_t primary_type, fallback_type;
195
196 if (handler && affinity) {
197 primary_type = CPU_TYPE_POWERPC;
198 fallback_type = cpu_type();
199 } else {
200 primary_type = cpu_type();
201 fallback_type = CPU_TYPE_POWERPC;
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(), 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(), 0, archret);
261 }
262