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