Commit | Line | Data |
---|---|---|
1c79356b A |
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 | ||
44 | /********************************************************************** | |
45 | * Routine: fatfile_getarch() | |
46 | * | |
47 | * Function: Locate the architecture-dependant contents of a fat | |
48 | * file that match this CPU. | |
49 | * | |
50 | * Args: vp: The vnode for the fat file. | |
51 | * header: A pointer to the fat file header. | |
52 | * archret (out): Pointer to fat_arch structure to hold | |
53 | * the results. | |
54 | * | |
55 | * Returns: KERN_SUCCESS: Valid architecture found. | |
56 | * KERN_FAILURE: No valid architecture found. | |
57 | **********************************************************************/ | |
58 | load_return_t | |
59 | fatfile_getarch( | |
60 | struct vnode *vp, | |
61 | vm_offset_t data_ptr, | |
62 | struct fat_arch *archret) | |
63 | { | |
64 | /* vm_pager_t pager; */ | |
65 | vm_offset_t addr; | |
66 | vm_size_t size; | |
67 | kern_return_t kret; | |
68 | load_return_t lret; | |
69 | struct machine_slot *ms; | |
70 | struct fat_arch *arch; | |
71 | struct fat_arch *best_arch; | |
72 | int grade; | |
73 | int best_grade; | |
74 | int nfat_arch; | |
75 | int end_of_archs; | |
1c79356b A |
76 | struct fat_header *header; |
77 | off_t filesize; | |
78 | ||
79 | /* | |
80 | * Get the pager for the file. | |
81 | */ | |
82 | ||
83 | header = (struct fat_header *)data_ptr; | |
84 | ||
85 | /* | |
86 | * Map portion that must be accessible directly into | |
87 | * kernel's map. | |
88 | */ | |
89 | nfat_arch = NXSwapBigLongToHost(header->nfat_arch); | |
90 | ||
91 | end_of_archs = sizeof(struct fat_header) | |
92 | + nfat_arch * sizeof(struct fat_arch); | |
93 | #if 0 | |
94 | filesize = ubc_getsize(vp); | |
0b4e3aa0 | 95 | if (end_of_archs > (int)filesize) { |
1c79356b A |
96 | return(LOAD_BADMACHO); |
97 | } | |
98 | #endif | |
99 | ||
100 | /* This is beacuse we are reading only 512 bytes */ | |
101 | ||
102 | if (end_of_archs > 512) | |
103 | return(LOAD_BADMACHO); | |
104 | /* | |
105 | * Round size of fat_arch structures up to page boundry. | |
106 | */ | |
107 | size = round_page(end_of_archs); | |
108 | if (size <= 0) | |
109 | return(LOAD_BADMACHO); | |
110 | ||
111 | /* | |
112 | * Scan the fat_arch's looking for the best one. | |
113 | */ | |
114 | addr = data_ptr; | |
115 | ms = &machine_slot[cpu_number()]; | |
116 | best_arch = NULL; | |
117 | best_grade = 0; | |
118 | arch = (struct fat_arch *) (addr + sizeof(struct fat_header)); | |
119 | for (; nfat_arch-- > 0; arch++) { | |
120 | ||
121 | /* | |
122 | * Check to see if right cpu type. | |
123 | */ | |
124 | if(NXSwapBigIntToHost(arch->cputype) != ms->cpu_type) | |
125 | continue; | |
126 | ||
127 | /* | |
128 | * Get the grade of the cpu subtype. | |
129 | */ | |
130 | grade = grade_cpu_subtype( | |
131 | NXSwapBigIntToHost(arch->cpusubtype)); | |
132 | ||
133 | /* | |
134 | * Remember it if it's the best we've seen. | |
135 | */ | |
136 | if (grade > best_grade) { | |
137 | best_grade = grade; | |
138 | best_arch = arch; | |
139 | } | |
140 | } | |
141 | ||
142 | /* | |
143 | * Return our results. | |
144 | */ | |
145 | if (best_arch == NULL) { | |
146 | lret = LOAD_BADARCH; | |
147 | } else { | |
148 | archret->cputype = | |
149 | NXSwapBigIntToHost(best_arch->cputype); | |
150 | archret->cpusubtype = | |
151 | NXSwapBigIntToHost(best_arch->cpusubtype); | |
152 | archret->offset = | |
153 | NXSwapBigLongToHost(best_arch->offset); | |
154 | archret->size = | |
155 | NXSwapBigLongToHost(best_arch->size); | |
156 | archret->align = | |
157 | NXSwapBigLongToHost(best_arch->align); | |
158 | ||
159 | lret = LOAD_SUCCESS; | |
160 | } | |
161 | ||
162 | /* | |
163 | * Free the memory we allocated and return. | |
164 | */ | |
165 | return(lret); | |
166 | } | |
167 | ||
168 |