]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/mach_fat.c
xnu-792.21.3.tar.gz
[apple/xnu.git] / bsd / kern / mach_fat.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
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.
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
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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>
21362eb3
A
47#include <architecture/byte_order.h>
48
49/* XXX should be in common header */
50extern 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
1c79356b
A
54
55/**********************************************************************
55e303ae 56 * Routine: fatfile_getarch2()
1c79356b
A
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.
91447636
A
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
1c79356b
A
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 **********************************************************************/
55e303ae
A
72static load_return_t
73fatfile_getarch2(
91447636 74#if 0
55e303ae 75 struct vnode *vp,
91447636
A
76#else
77 __unused struct vnode *vp,
78#endif
55e303ae 79 vm_offset_t data_ptr,
91447636
A
80 cpu_type_t req_cpu_type,
81 cpu_type_t mask_bits,
55e303ae 82 struct fat_arch *archret)
1c79356b
A
83{
84 /* vm_pager_t pager; */
85 vm_offset_t addr;
86 vm_size_t size;
1c79356b 87 load_return_t lret;
1c79356b
A
88 struct fat_arch *arch;
89 struct fat_arch *best_arch;
90 int grade;
91 int best_grade;
92 int nfat_arch;
8f6c56a5 93 off_t end_of_archs;
1c79356b 94 struct fat_header *header;
91447636 95#if 0
1c79356b 96 off_t filesize;
91447636 97#endif
1c79356b
A
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 */
21362eb3 109 nfat_arch = NXSwapBigLongToHost(header->nfat_arch);
1c79356b 110
8f6c56a5
A
111 end_of_archs = (off_t)nfat_arch * sizeof(struct fat_arch) +
112 sizeof(struct fat_header);
1c79356b
A
113#if 0
114 filesize = ubc_getsize(vp);
0b4e3aa0 115 if (end_of_archs > (int)filesize) {
1c79356b
A
116 return(LOAD_BADMACHO);
117 }
118#endif
119
8f6c56a5
A
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)))
1c79356b
A
126 return(LOAD_BADMACHO);
127 /*
128 * Round size of fat_arch structures up to page boundry.
129 */
55e303ae 130 size = round_page_32(end_of_archs);
91447636 131 if (size == 0)
1c79356b
A
132 return(LOAD_BADMACHO);
133
134 /*
135 * Scan the fat_arch's looking for the best one.
136 */
137 addr = data_ptr;
1c79356b
A
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 */
21362eb3 146 if(((cpu_type_t)NXSwapBigIntToHost(arch->cputype) & ~mask_bits) != req_cpu_type)
1c79356b
A
147 continue;
148
149 /*
150 * Get the grade of the cpu subtype.
151 */
91447636 152 grade = grade_binary(
21362eb3
A
153 NXSwapBigIntToHost(arch->cputype),
154 NXSwapBigIntToHost(arch->cpusubtype));
1c79356b
A
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 =
21362eb3 172 NXSwapBigIntToHost(best_arch->cputype);
1c79356b 173 archret->cpusubtype =
21362eb3 174 NXSwapBigIntToHost(best_arch->cpusubtype);
1c79356b 175 archret->offset =
21362eb3 176 NXSwapBigLongToHost(best_arch->offset);
1c79356b 177 archret->size =
21362eb3 178 NXSwapBigLongToHost(best_arch->size);
1c79356b 179 archret->align =
21362eb3 180 NXSwapBigLongToHost(best_arch->align);
1c79356b
A
181
182 lret = LOAD_SUCCESS;
183 }
184
185 /*
186 * Free the memory we allocated and return.
187 */
188 return(lret);
189}
190
21362eb3
A
191extern char classichandler[];
192
55e303ae
A
193load_return_t
194fatfile_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;
21362eb3 201 int handler = (classichandler[0] != 0);
55e303ae
A
202 cpu_type_t primary_type, fallback_type;
203
204 if (handler && affinity) {
21362eb3
A
205 primary_type = CPU_TYPE_CLASSIC;
206 fallback_type = CPU_TYPE_NATIVE;
55e303ae 207 } else {
21362eb3
A
208 primary_type = CPU_TYPE_NATIVE;
209 fallback_type = CPU_TYPE_CLASSIC;
55e303ae 210 }
91447636
A
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);
55e303ae
A
216 if ((lret != 0) && handler) {
217 lret = fatfile_getarch2(vp, data_ptr, fallback_type,
91447636 218 0, archret);
55e303ae
A
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 **********************************************************************/
237load_return_t
238fatfile_getarch(
239 struct vnode *vp,
240 vm_offset_t data_ptr,
241 struct fat_arch *archret)
242{
21362eb3 243 return fatfile_getarch2(vp, data_ptr, CPU_TYPE_NATIVE, 0, archret);
91447636
A
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 **********************************************************************/
261load_return_t
262fatfile_getarch_with_bits(
263 struct vnode *vp,
264 integer_t archbits,
265 vm_offset_t data_ptr,
266 struct fat_arch *archret)
267{
21362eb3 268 return fatfile_getarch2(vp, data_ptr, archbits | CPU_TYPE_NATIVE, 0, archret);
55e303ae 269}
1c79356b 270