]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/mach_fat.c
xnu-517.7.7.tar.gz
[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 <architecture/byte_order.h>
42
43 #define CPU_TYPE_NATIVE (machine_slot[cpu_number()].cpu_type)
44 #define CPU_TYPE_CLASSIC CPU_TYPE_POWERPC
45
46 /**********************************************************************
47 * Routine: fatfile_getarch2()
48 *
49 * Function: Locate the architecture-dependant contents of a fat
50 * file that match this CPU.
51 *
52 * Args: vp: The vnode for the fat file.
53 * header: A pointer to the fat file header.
54 * cpu_type: The required 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 struct vnode *vp,
64 vm_offset_t data_ptr,
65 cpu_type_t cpu_type,
66 struct fat_arch *archret)
67 {
68 /* vm_pager_t pager; */
69 vm_offset_t addr;
70 vm_size_t size;
71 kern_return_t kret;
72 load_return_t lret;
73 struct fat_arch *arch;
74 struct fat_arch *best_arch;
75 int grade;
76 int best_grade;
77 int nfat_arch;
78 int end_of_archs;
79 struct fat_header *header;
80 off_t filesize;
81
82 /*
83 * Get the pager for the file.
84 */
85
86 header = (struct fat_header *)data_ptr;
87
88 /*
89 * Map portion that must be accessible directly into
90 * kernel's map.
91 */
92 nfat_arch = NXSwapBigLongToHost(header->nfat_arch);
93
94 end_of_archs = sizeof(struct fat_header)
95 + nfat_arch * sizeof(struct fat_arch);
96 #if 0
97 filesize = ubc_getsize(vp);
98 if (end_of_archs > (int)filesize) {
99 return(LOAD_BADMACHO);
100 }
101 #endif
102
103 /* This is beacuse we are reading only 512 bytes */
104
105 if (end_of_archs > 512)
106 return(LOAD_BADMACHO);
107 /*
108 * Round size of fat_arch structures up to page boundry.
109 */
110 size = round_page_32(end_of_archs);
111 if (size <= 0)
112 return(LOAD_BADMACHO);
113
114 /*
115 * Scan the fat_arch's looking for the best one.
116 */
117 addr = data_ptr;
118 best_arch = NULL;
119 best_grade = 0;
120 arch = (struct fat_arch *) (addr + sizeof(struct fat_header));
121 for (; nfat_arch-- > 0; arch++) {
122
123 /*
124 * Check to see if right cpu type.
125 */
126 if(NXSwapBigIntToHost(arch->cputype) != cpu_type)
127 continue;
128
129 /*
130 * Get the grade of the cpu subtype.
131 */
132 grade = grade_cpu_subtype(
133 NXSwapBigIntToHost(arch->cpusubtype));
134
135 /*
136 * Remember it if it's the best we've seen.
137 */
138 if (grade > best_grade) {
139 best_grade = grade;
140 best_arch = arch;
141 }
142 }
143
144 /*
145 * Return our results.
146 */
147 if (best_arch == NULL) {
148 lret = LOAD_BADARCH;
149 } else {
150 archret->cputype =
151 NXSwapBigIntToHost(best_arch->cputype);
152 archret->cpusubtype =
153 NXSwapBigIntToHost(best_arch->cpusubtype);
154 archret->offset =
155 NXSwapBigLongToHost(best_arch->offset);
156 archret->size =
157 NXSwapBigLongToHost(best_arch->size);
158 archret->align =
159 NXSwapBigLongToHost(best_arch->align);
160
161 lret = LOAD_SUCCESS;
162 }
163
164 /*
165 * Free the memory we allocated and return.
166 */
167 return(lret);
168 }
169
170 extern char classichandler[];
171
172 load_return_t
173 fatfile_getarch_affinity(
174 struct vnode *vp,
175 vm_offset_t data_ptr,
176 struct fat_arch *archret,
177 int affinity)
178 {
179 load_return_t lret;
180 int handler = (classichandler[0] != 0);
181 cpu_type_t primary_type, fallback_type;
182
183 if (handler && affinity) {
184 primary_type = CPU_TYPE_CLASSIC;
185 fallback_type = CPU_TYPE_NATIVE;
186 } else {
187 primary_type = CPU_TYPE_NATIVE;
188 fallback_type = CPU_TYPE_CLASSIC;
189 }
190 lret = fatfile_getarch2(vp, data_ptr, primary_type, archret);
191 if ((lret != 0) && handler) {
192 lret = fatfile_getarch2(vp, data_ptr, fallback_type,
193 archret);
194 }
195 return lret;
196 }
197
198 /**********************************************************************
199 * Routine: fatfile_getarch()
200 *
201 * Function: Locate the architecture-dependant contents of a fat
202 * file that match this CPU.
203 *
204 * Args: vp: The vnode for the fat file.
205 * header: A pointer to the fat file header.
206 * archret (out): Pointer to fat_arch structure to hold
207 * the results.
208 *
209 * Returns: KERN_SUCCESS: Valid architecture found.
210 * KERN_FAILURE: No valid architecture found.
211 **********************************************************************/
212 load_return_t
213 fatfile_getarch(
214 struct vnode *vp,
215 vm_offset_t data_ptr,
216 struct fat_arch *archret)
217 {
218 return fatfile_getarch2(vp, data_ptr, CPU_TYPE_NATIVE, archret);
219 }
220