]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/mach_fat.c
7c9baaeec92c70c7283bc28d6ab61ec920472c22
[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 * 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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
23 *
24 * File: kern/mach_fat.c
25 * Author: Peter King
26 *
27 * Fat file support routines.
28 *
29 */
30
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/uio.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 <libkern/OSByteOrder.h>
42 #include <machine/exec.h>
43
44 /**********************************************************************
45 * Routine: fatfile_getarch2()
46 *
47 * Function: Locate the architecture-dependant contents of a fat
48 * file that match this CPU.
49 *
50 * Args: vp: The vnode for the fat file.
51 * header: A pointer to the fat file header.
52 * req_cpu_type: The required cpu type.
53 * mask_bits: Bits to mask from the sub-image type when
54 * grading it vs. the req_cpu_type
55 * archret (out): Pointer to fat_arch structure to hold
56 * the results.
57 *
58 * Returns: KERN_SUCCESS: Valid architecture found.
59 * KERN_FAILURE: No valid architecture found.
60 **********************************************************************/
61 static load_return_t
62 fatfile_getarch2(
63 #if 0
64 struct vnode *vp,
65 #else
66 __unused struct vnode *vp,
67 #endif
68 vm_offset_t data_ptr,
69 cpu_type_t req_cpu_type,
70 cpu_type_t mask_bits,
71 struct fat_arch *archret)
72 {
73 /* vm_pager_t pager; */
74 vm_offset_t addr;
75 vm_size_t size;
76 load_return_t lret;
77 struct fat_arch *arch;
78 struct fat_arch *best_arch;
79 int grade;
80 int best_grade;
81 int nfat_arch;
82 int end_of_archs;
83 struct fat_header *header;
84 #if 0
85 off_t filesize;
86 #endif
87
88 /*
89 * Get the pager for the file.
90 */
91
92 header = (struct fat_header *)data_ptr;
93
94 /*
95 * Map portion that must be accessible directly into
96 * kernel's map.
97 */
98 nfat_arch = OSSwapBigToHostInt32(header->nfat_arch);
99
100 end_of_archs = sizeof(struct fat_header)
101 + nfat_arch * sizeof(struct fat_arch);
102 #if 0
103 filesize = ubc_getsize(vp);
104 if (end_of_archs > (int)filesize) {
105 return(LOAD_BADMACHO);
106 }
107 #endif
108
109 /* This is beacuse we are reading only 512 bytes */
110
111 if (end_of_archs > 512)
112 return(LOAD_BADMACHO);
113 /*
114 * Round size of fat_arch structures up to page boundry.
115 */
116 size = round_page_32(end_of_archs);
117 if (size == 0)
118 return(LOAD_BADMACHO);
119
120 /*
121 * Scan the fat_arch's looking for the best one.
122 */
123 addr = data_ptr;
124 best_arch = NULL;
125 best_grade = 0;
126 arch = (struct fat_arch *) (addr + sizeof(struct fat_header));
127 for (; nfat_arch-- > 0; arch++) {
128
129 /*
130 * Check to see if right cpu type.
131 */
132 if(((cpu_type_t)OSSwapBigToHostInt32(arch->cputype) & ~mask_bits) != req_cpu_type)
133 continue;
134
135 /*
136 * Get the grade of the cpu subtype.
137 */
138 grade = grade_binary(
139 OSSwapBigToHostInt32(arch->cputype),
140 OSSwapBigToHostInt32(arch->cpusubtype));
141
142 /*
143 * Remember it if it's the best we've seen.
144 */
145 if (grade > best_grade) {
146 best_grade = grade;
147 best_arch = arch;
148 }
149 }
150
151 /*
152 * Return our results.
153 */
154 if (best_arch == NULL) {
155 lret = LOAD_BADARCH;
156 } else {
157 archret->cputype =
158 OSSwapBigToHostInt32(best_arch->cputype);
159 archret->cpusubtype =
160 OSSwapBigToHostInt32(best_arch->cpusubtype);
161 archret->offset =
162 OSSwapBigToHostInt32(best_arch->offset);
163 archret->size =
164 OSSwapBigToHostInt32(best_arch->size);
165 archret->align =
166 OSSwapBigToHostInt32(best_arch->align);
167
168 lret = LOAD_SUCCESS;
169 }
170
171 /*
172 * Free the memory we allocated and return.
173 */
174 return(lret);
175 }
176
177 load_return_t
178 fatfile_getarch_affinity(
179 struct vnode *vp,
180 vm_offset_t data_ptr,
181 struct fat_arch *archret,
182 int affinity)
183 {
184 load_return_t lret;
185 int handler = (exec_archhandler_ppc.path[0] != 0);
186 cpu_type_t primary_type, fallback_type;
187
188 if (handler && affinity) {
189 primary_type = CPU_TYPE_POWERPC;
190 fallback_type = cpu_type();
191 } else {
192 primary_type = cpu_type();
193 fallback_type = CPU_TYPE_POWERPC;
194 }
195 /*
196 * Ignore the architectural bits when determining if an image
197 * in a fat file should be skipped or graded.
198 */
199 lret = fatfile_getarch2(vp, data_ptr, primary_type, CPU_ARCH_MASK, archret);
200 if ((lret != 0) && handler) {
201 lret = fatfile_getarch2(vp, data_ptr, fallback_type,
202 0, archret);
203 }
204 return lret;
205 }
206
207 /**********************************************************************
208 * Routine: fatfile_getarch()
209 *
210 * Function: Locate the architecture-dependant contents of a fat
211 * file that match this CPU.
212 *
213 * Args: vp: The vnode for the fat file.
214 * header: A pointer to the fat file header.
215 * archret (out): Pointer to fat_arch structure to hold
216 * the results.
217 *
218 * Returns: KERN_SUCCESS: Valid architecture found.
219 * KERN_FAILURE: No valid architecture found.
220 **********************************************************************/
221 load_return_t
222 fatfile_getarch(
223 struct vnode *vp,
224 vm_offset_t data_ptr,
225 struct fat_arch *archret)
226 {
227 return fatfile_getarch2(vp, data_ptr, cpu_type(), 0, archret);
228 }
229
230 /**********************************************************************
231 * Routine: fatfile_getarch_with_bits()
232 *
233 * Function: Locate the architecture-dependant contents of a fat
234 * file that match this CPU.
235 *
236 * Args: vp: The vnode for the fat file.
237 * archbits: Architecture specific feature bits
238 * header: A pointer to the fat file header.
239 * archret (out): Pointer to fat_arch structure to hold
240 * the results.
241 *
242 * Returns: KERN_SUCCESS: Valid architecture found.
243 * KERN_FAILURE: No valid architecture found.
244 **********************************************************************/
245 load_return_t
246 fatfile_getarch_with_bits(
247 struct vnode *vp,
248 integer_t archbits,
249 vm_offset_t data_ptr,
250 struct fat_arch *archret)
251 {
252 return fatfile_getarch2(vp, data_ptr, archbits | cpu_type(), 0, archret);
253 }
254