]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/mach_fat.c
96669065a9fb0ca712f0644aafdbe55b33bed296
[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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
26 *
27 * File: kern/mach_fat.c
28 * Author: Peter King
29 *
30 * Fat file support routines.
31 *
32 */
33
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/uio.h>
37 #include <sys/vnode.h>
38 #include <vm/vm_kern.h>
39 #include <mach/kern_return.h>
40 #include <mach/vm_param.h>
41 #include <kern/cpu_number.h>
42 #include <mach-o/fat.h>
43 #include <kern/mach_loader.h>
44 #include <architecture/byte_order.h>
45
46 #define CPU_TYPE_NATIVE (machine_slot[cpu_number()].cpu_type)
47 #define CPU_TYPE_CLASSIC CPU_TYPE_POWERPC
48
49 /**********************************************************************
50 * Routine: fatfile_getarch2()
51 *
52 * Function: Locate the architecture-dependant contents of a fat
53 * file that match this CPU.
54 *
55 * Args: vp: The vnode for the fat file.
56 * header: A pointer to the fat file header.
57 * cpu_type: The required cpu type.
58 * archret (out): Pointer to fat_arch structure to hold
59 * the results.
60 *
61 * Returns: KERN_SUCCESS: Valid architecture found.
62 * KERN_FAILURE: No valid architecture found.
63 **********************************************************************/
64 static load_return_t
65 fatfile_getarch2(
66 struct vnode *vp,
67 vm_offset_t data_ptr,
68 cpu_type_t cpu_type,
69 struct fat_arch *archret)
70 {
71 /* vm_pager_t pager; */
72 vm_offset_t addr;
73 vm_size_t size;
74 kern_return_t kret;
75 load_return_t lret;
76 struct fat_arch *arch;
77 struct fat_arch *best_arch;
78 int grade;
79 int best_grade;
80 int nfat_arch;
81 int end_of_archs;
82 struct fat_header *header;
83 off_t filesize;
84
85 /*
86 * Get the pager for the file.
87 */
88
89 header = (struct fat_header *)data_ptr;
90
91 /*
92 * Map portion that must be accessible directly into
93 * kernel's map.
94 */
95 nfat_arch = NXSwapBigLongToHost(header->nfat_arch);
96
97 end_of_archs = sizeof(struct fat_header)
98 + nfat_arch * sizeof(struct fat_arch);
99 #if 0
100 filesize = ubc_getsize(vp);
101 if (end_of_archs > (int)filesize) {
102 return(LOAD_BADMACHO);
103 }
104 #endif
105
106 /* This is beacuse we are reading only 512 bytes */
107
108 if (end_of_archs > 512)
109 return(LOAD_BADMACHO);
110 /*
111 * Round size of fat_arch structures up to page boundry.
112 */
113 size = round_page_32(end_of_archs);
114 if (size <= 0)
115 return(LOAD_BADMACHO);
116
117 /*
118 * Scan the fat_arch's looking for the best one.
119 */
120 addr = data_ptr;
121 best_arch = NULL;
122 best_grade = 0;
123 arch = (struct fat_arch *) (addr + sizeof(struct fat_header));
124 for (; nfat_arch-- > 0; arch++) {
125
126 /*
127 * Check to see if right cpu type.
128 */
129 if(NXSwapBigIntToHost(arch->cputype) != cpu_type)
130 continue;
131
132 /*
133 * Get the grade of the cpu subtype.
134 */
135 grade = grade_cpu_subtype(
136 NXSwapBigIntToHost(arch->cpusubtype));
137
138 /*
139 * Remember it if it's the best we've seen.
140 */
141 if (grade > best_grade) {
142 best_grade = grade;
143 best_arch = arch;
144 }
145 }
146
147 /*
148 * Return our results.
149 */
150 if (best_arch == NULL) {
151 lret = LOAD_BADARCH;
152 } else {
153 archret->cputype =
154 NXSwapBigIntToHost(best_arch->cputype);
155 archret->cpusubtype =
156 NXSwapBigIntToHost(best_arch->cpusubtype);
157 archret->offset =
158 NXSwapBigLongToHost(best_arch->offset);
159 archret->size =
160 NXSwapBigLongToHost(best_arch->size);
161 archret->align =
162 NXSwapBigLongToHost(best_arch->align);
163
164 lret = LOAD_SUCCESS;
165 }
166
167 /*
168 * Free the memory we allocated and return.
169 */
170 return(lret);
171 }
172
173 extern char classichandler[];
174
175 load_return_t
176 fatfile_getarch_affinity(
177 struct vnode *vp,
178 vm_offset_t data_ptr,
179 struct fat_arch *archret,
180 int affinity)
181 {
182 load_return_t lret;
183 int handler = (classichandler[0] != 0);
184 cpu_type_t primary_type, fallback_type;
185
186 if (handler && affinity) {
187 primary_type = CPU_TYPE_CLASSIC;
188 fallback_type = CPU_TYPE_NATIVE;
189 } else {
190 primary_type = CPU_TYPE_NATIVE;
191 fallback_type = CPU_TYPE_CLASSIC;
192 }
193 lret = fatfile_getarch2(vp, data_ptr, primary_type, archret);
194 if ((lret != 0) && handler) {
195 lret = fatfile_getarch2(vp, data_ptr, fallback_type,
196 archret);
197 }
198 return lret;
199 }
200
201 /**********************************************************************
202 * Routine: fatfile_getarch()
203 *
204 * Function: Locate the architecture-dependant contents of a fat
205 * file that match this CPU.
206 *
207 * Args: vp: The vnode for the fat file.
208 * header: A pointer to the fat file header.
209 * archret (out): Pointer to fat_arch structure to hold
210 * the results.
211 *
212 * Returns: KERN_SUCCESS: Valid architecture found.
213 * KERN_FAILURE: No valid architecture found.
214 **********************************************************************/
215 load_return_t
216 fatfile_getarch(
217 struct vnode *vp,
218 vm_offset_t data_ptr,
219 struct fat_arch *archret)
220 {
221 return fatfile_getarch2(vp, data_ptr, CPU_TYPE_NATIVE, archret);
222 }
223