]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 1991-2005 Apple Computer, Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b | 27 | */ |
1c79356b A |
28 | #include <sys/param.h> |
29 | #include <sys/types.h> | |
30 | #include <sys/uio.h> | |
31 | #include <sys/vnode.h> | |
32 | #include <vm/vm_kern.h> | |
33 | #include <mach/kern_return.h> | |
34 | #include <mach/vm_param.h> | |
35 | #include <kern/cpu_number.h> | |
36 | #include <mach-o/fat.h> | |
37 | #include <kern/mach_loader.h> | |
b0d623f7 | 38 | #include <kern/mach_fat.h> |
0c530ab8 A |
39 | #include <libkern/OSByteOrder.h> |
40 | #include <machine/exec.h> | |
1c79356b A |
41 | |
42 | /********************************************************************** | |
55e303ae | 43 | * Routine: fatfile_getarch2() |
1c79356b A |
44 | * |
45 | * Function: Locate the architecture-dependant contents of a fat | |
46 | * file that match this CPU. | |
47 | * | |
48 | * Args: vp: The vnode for the fat file. | |
49 | * header: A pointer to the fat file header. | |
91447636 A |
50 | * req_cpu_type: The required cpu type. |
51 | * mask_bits: Bits to mask from the sub-image type when | |
52 | * grading it vs. the req_cpu_type | |
1c79356b A |
53 | * archret (out): Pointer to fat_arch structure to hold |
54 | * the results. | |
55 | * | |
56 | * Returns: KERN_SUCCESS: Valid architecture found. | |
57 | * KERN_FAILURE: No valid architecture found. | |
58 | **********************************************************************/ | |
55e303ae A |
59 | static load_return_t |
60 | fatfile_getarch2( | |
91447636 | 61 | #if 0 |
55e303ae | 62 | struct vnode *vp, |
91447636 A |
63 | #else |
64 | __unused struct vnode *vp, | |
65 | #endif | |
55e303ae | 66 | vm_offset_t data_ptr, |
91447636 A |
67 | cpu_type_t req_cpu_type, |
68 | cpu_type_t mask_bits, | |
55e303ae | 69 | struct fat_arch *archret) |
1c79356b A |
70 | { |
71 | /* vm_pager_t pager; */ | |
72 | vm_offset_t addr; | |
73 | vm_size_t size; | |
1c79356b | 74 | load_return_t lret; |
1c79356b A |
75 | struct fat_arch *arch; |
76 | struct fat_arch *best_arch; | |
77 | int grade; | |
78 | int best_grade; | |
79 | int nfat_arch; | |
2d21ac55 A |
80 | off_t end_of_archs; |
81 | cpu_type_t testtype; | |
82 | cpu_type_t testsubtype; | |
1c79356b | 83 | struct fat_header *header; |
91447636 | 84 | #if 0 |
1c79356b | 85 | off_t filesize; |
91447636 | 86 | #endif |
1c79356b A |
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 | */ | |
0c530ab8 | 98 | nfat_arch = OSSwapBigToHostInt32(header->nfat_arch); |
1c79356b | 99 | |
2d21ac55 A |
100 | end_of_archs = (off_t)nfat_arch * sizeof(struct fat_arch) + |
101 | sizeof(struct fat_header); | |
1c79356b A |
102 | #if 0 |
103 | filesize = ubc_getsize(vp); | |
0b4e3aa0 | 104 | if (end_of_archs > (int)filesize) { |
1c79356b A |
105 | return(LOAD_BADMACHO); |
106 | } | |
107 | #endif | |
108 | ||
8f6c56a5 A |
109 | /* |
110 | * This check is limited on the top end because we are reading | |
111 | * only PAGE_SIZE bytes | |
112 | */ | |
113 | if (end_of_archs > PAGE_SIZE || | |
b0d623f7 | 114 | end_of_archs < (off_t)(sizeof(struct fat_header)+sizeof(struct fat_arch))) |
1c79356b | 115 | return(LOAD_BADMACHO); |
0c530ab8 | 116 | |
1c79356b A |
117 | /* |
118 | * Round size of fat_arch structures up to page boundry. | |
119 | */ | |
b0d623f7 | 120 | size = round_page(end_of_archs); |
91447636 | 121 | if (size == 0) |
1c79356b A |
122 | return(LOAD_BADMACHO); |
123 | ||
124 | /* | |
2d21ac55 A |
125 | * Ignore LIB64 flag so that binary slices with the flag set |
126 | * don't choke in grade_binary. | |
1c79356b | 127 | */ |
2d21ac55 A |
128 | mask_bits |= CPU_SUBTYPE_LIB64; |
129 | ||
130 | /* | |
131 | * Scan the fat_arch's looking for the best one. */ | |
1c79356b | 132 | addr = data_ptr; |
1c79356b A |
133 | best_arch = NULL; |
134 | best_grade = 0; | |
135 | arch = (struct fat_arch *) (addr + sizeof(struct fat_header)); | |
136 | for (; nfat_arch-- > 0; arch++) { | |
137 | ||
2d21ac55 A |
138 | /* |
139 | * Collect flags from both cputype and cpusubtype | |
140 | */ | |
141 | testtype = OSSwapBigToHostInt32(arch->cputype) | | |
142 | (OSSwapBigToHostInt32(arch->cpusubtype) & | |
143 | CPU_SUBTYPE_MASK); | |
144 | testsubtype = OSSwapBigToHostInt32(arch->cpusubtype) | |
145 | & ~CPU_SUBTYPE_MASK; | |
146 | ||
1c79356b A |
147 | /* |
148 | * Check to see if right cpu type. | |
149 | */ | |
2d21ac55 | 150 | if((testtype & ~mask_bits) != req_cpu_type) { |
1c79356b | 151 | continue; |
2d21ac55 | 152 | } |
1c79356b A |
153 | |
154 | /* | |
2d21ac55 | 155 | * Get the grade of the cpu subtype (without feature flags) |
1c79356b | 156 | */ |
2d21ac55 A |
157 | grade = grade_binary( |
158 | (testtype & ~CPU_SUBTYPE_LIB64), | |
159 | testsubtype); | |
1c79356b A |
160 | |
161 | /* | |
162 | * Remember it if it's the best we've seen. | |
163 | */ | |
164 | if (grade > best_grade) { | |
165 | best_grade = grade; | |
166 | best_arch = arch; | |
167 | } | |
168 | } | |
169 | ||
170 | /* | |
171 | * Return our results. | |
172 | */ | |
173 | if (best_arch == NULL) { | |
174 | lret = LOAD_BADARCH; | |
175 | } else { | |
176 | archret->cputype = | |
0c530ab8 | 177 | OSSwapBigToHostInt32(best_arch->cputype); |
1c79356b | 178 | archret->cpusubtype = |
0c530ab8 | 179 | OSSwapBigToHostInt32(best_arch->cpusubtype); |
1c79356b | 180 | archret->offset = |
0c530ab8 | 181 | OSSwapBigToHostInt32(best_arch->offset); |
1c79356b | 182 | archret->size = |
0c530ab8 | 183 | OSSwapBigToHostInt32(best_arch->size); |
1c79356b | 184 | archret->align = |
0c530ab8 | 185 | OSSwapBigToHostInt32(best_arch->align); |
1c79356b A |
186 | |
187 | lret = LOAD_SUCCESS; | |
188 | } | |
189 | ||
190 | /* | |
191 | * Free the memory we allocated and return. | |
192 | */ | |
193 | return(lret); | |
194 | } | |
195 | ||
55e303ae A |
196 | load_return_t |
197 | fatfile_getarch_affinity( | |
198 | struct vnode *vp, | |
199 | vm_offset_t data_ptr, | |
200 | struct fat_arch *archret, | |
316670eb | 201 | int affinity __unused) |
55e303ae | 202 | { |
91447636 | 203 | /* |
2d21ac55 | 204 | * Ignore all architectural bits when determining if an image |
91447636 A |
205 | * in a fat file should be skipped or graded. |
206 | */ | |
316670eb | 207 | return fatfile_getarch2(vp, data_ptr, cpu_type(), |
2d21ac55 | 208 | CPU_ARCH_MASK, archret); |
55e303ae A |
209 | } |
210 | ||
211 | /********************************************************************** | |
212 | * Routine: fatfile_getarch() | |
213 | * | |
214 | * Function: Locate the architecture-dependant contents of a fat | |
215 | * file that match this CPU. | |
216 | * | |
217 | * Args: vp: The vnode for the fat file. | |
218 | * header: A pointer to the fat file header. | |
219 | * archret (out): Pointer to fat_arch structure to hold | |
220 | * the results. | |
221 | * | |
222 | * Returns: KERN_SUCCESS: Valid architecture found. | |
223 | * KERN_FAILURE: No valid architecture found. | |
224 | **********************************************************************/ | |
225 | load_return_t | |
226 | fatfile_getarch( | |
227 | struct vnode *vp, | |
228 | vm_offset_t data_ptr, | |
229 | struct fat_arch *archret) | |
230 | { | |
2d21ac55 A |
231 | return fatfile_getarch2(vp, data_ptr, cpu_type(), |
232 | CPU_SUBTYPE_LIB64, archret); | |
91447636 A |
233 | } |
234 | ||
235 | /********************************************************************** | |
236 | * Routine: fatfile_getarch_with_bits() | |
237 | * | |
238 | * Function: Locate the architecture-dependant contents of a fat | |
239 | * file that match this CPU. | |
240 | * | |
241 | * Args: vp: The vnode for the fat file. | |
242 | * archbits: Architecture specific feature bits | |
243 | * header: A pointer to the fat file header. | |
244 | * archret (out): Pointer to fat_arch structure to hold | |
245 | * the results. | |
246 | * | |
247 | * Returns: KERN_SUCCESS: Valid architecture found. | |
248 | * KERN_FAILURE: No valid architecture found. | |
249 | **********************************************************************/ | |
250 | load_return_t | |
251 | fatfile_getarch_with_bits( | |
252 | struct vnode *vp, | |
253 | integer_t archbits, | |
254 | vm_offset_t data_ptr, | |
255 | struct fat_arch *archret) | |
256 | { | |
2d21ac55 A |
257 | return fatfile_getarch2(vp, data_ptr, archbits | cpu_type(), |
258 | CPU_SUBTYPE_LIB64, archret); | |
55e303ae | 259 | } |
1c79356b | 260 |