]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/mach_fat.c
677ae0008abd65bcfcd08e9aad5ce6b11de60935
[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 <libkern/OSByteOrder.h>
48 #include <machine/exec.h>
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 off_t 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 = OSSwapBigToHostInt32(header->nfat_arch);
105
106 end_of_archs = (off_t)nfat_arch * sizeof(struct fat_arch) +
107 sizeof(struct fat_header);
108 #if 0
109 filesize = ubc_getsize(vp);
110 if (end_of_archs > (int)filesize) {
111 return(LOAD_BADMACHO);
112 }
113 #endif
114
115 /*
116 * This check is limited on the top end because we are reading
117 * only PAGE_SIZE bytes
118 */
119 if (end_of_archs > PAGE_SIZE ||
120 end_of_archs < (sizeof(struct fat_header)+sizeof(struct fat_arch)))
121 return(LOAD_BADMACHO);
122
123 /*
124 * Round size of fat_arch structures up to page boundry.
125 */
126 size = round_page_32(end_of_archs);
127 if (size == 0)
128 return(LOAD_BADMACHO);
129
130 /*
131 * Scan the fat_arch's looking for the best one.
132 */
133 addr = data_ptr;
134 best_arch = NULL;
135 best_grade = 0;
136 arch = (struct fat_arch *) (addr + sizeof(struct fat_header));
137 for (; nfat_arch-- > 0; arch++) {
138
139 /*
140 * Check to see if right cpu type.
141 */
142 if(((cpu_type_t)OSSwapBigToHostInt32(arch->cputype) & ~mask_bits) != req_cpu_type)
143 continue;
144
145 /*
146 * Get the grade of the cpu subtype.
147 */
148 grade = grade_binary(
149 OSSwapBigToHostInt32(arch->cputype),
150 OSSwapBigToHostInt32(arch->cpusubtype));
151
152 /*
153 * Remember it if it's the best we've seen.
154 */
155 if (grade > best_grade) {
156 best_grade = grade;
157 best_arch = arch;
158 }
159 }
160
161 /*
162 * Return our results.
163 */
164 if (best_arch == NULL) {
165 lret = LOAD_BADARCH;
166 } else {
167 archret->cputype =
168 OSSwapBigToHostInt32(best_arch->cputype);
169 archret->cpusubtype =
170 OSSwapBigToHostInt32(best_arch->cpusubtype);
171 archret->offset =
172 OSSwapBigToHostInt32(best_arch->offset);
173 archret->size =
174 OSSwapBigToHostInt32(best_arch->size);
175 archret->align =
176 OSSwapBigToHostInt32(best_arch->align);
177
178 lret = LOAD_SUCCESS;
179 }
180
181 /*
182 * Free the memory we allocated and return.
183 */
184 return(lret);
185 }
186
187 load_return_t
188 fatfile_getarch_affinity(
189 struct vnode *vp,
190 vm_offset_t data_ptr,
191 struct fat_arch *archret,
192 int affinity)
193 {
194 load_return_t lret;
195 int handler = (exec_archhandler_ppc.path[0] != 0);
196 cpu_type_t primary_type, fallback_type;
197
198 if (handler && affinity) {
199 primary_type = CPU_TYPE_POWERPC;
200 fallback_type = cpu_type();
201 } else {
202 primary_type = cpu_type();
203 fallback_type = CPU_TYPE_POWERPC;
204 }
205 /*
206 * Ignore the architectural bits when determining if an image
207 * in a fat file should be skipped or graded.
208 */
209 lret = fatfile_getarch2(vp, data_ptr, primary_type, CPU_ARCH_MASK, archret);
210 if ((lret != 0) && handler) {
211 lret = fatfile_getarch2(vp, data_ptr, fallback_type,
212 0, archret);
213 }
214 return lret;
215 }
216
217 /**********************************************************************
218 * Routine: fatfile_getarch()
219 *
220 * Function: Locate the architecture-dependant contents of a fat
221 * file that match this CPU.
222 *
223 * Args: vp: The vnode for the fat file.
224 * header: A pointer to the fat file header.
225 * archret (out): Pointer to fat_arch structure to hold
226 * the results.
227 *
228 * Returns: KERN_SUCCESS: Valid architecture found.
229 * KERN_FAILURE: No valid architecture found.
230 **********************************************************************/
231 load_return_t
232 fatfile_getarch(
233 struct vnode *vp,
234 vm_offset_t data_ptr,
235 struct fat_arch *archret)
236 {
237 return fatfile_getarch2(vp, data_ptr, cpu_type(), 0, archret);
238 }
239
240 /**********************************************************************
241 * Routine: fatfile_getarch_with_bits()
242 *
243 * Function: Locate the architecture-dependant contents of a fat
244 * file that match this CPU.
245 *
246 * Args: vp: The vnode for the fat file.
247 * archbits: Architecture specific feature bits
248 * header: A pointer to the fat file header.
249 * archret (out): Pointer to fat_arch structure to hold
250 * the results.
251 *
252 * Returns: KERN_SUCCESS: Valid architecture found.
253 * KERN_FAILURE: No valid architecture found.
254 **********************************************************************/
255 load_return_t
256 fatfile_getarch_with_bits(
257 struct vnode *vp,
258 integer_t archbits,
259 vm_offset_t data_ptr,
260 struct fat_arch *archret)
261 {
262 return fatfile_getarch2(vp, data_ptr, archbits | cpu_type(), 0, archret);
263 }
264