]>
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; | |
76 | struct proc *p = current_proc(); /* XXXX */ | |
77 | struct fat_header *header; | |
78 | off_t filesize; | |
79 | ||
80 | /* | |
81 | * Get the pager for the file. | |
82 | */ | |
83 | ||
84 | header = (struct fat_header *)data_ptr; | |
85 | ||
86 | /* | |
87 | * Map portion that must be accessible directly into | |
88 | * kernel's map. | |
89 | */ | |
90 | nfat_arch = NXSwapBigLongToHost(header->nfat_arch); | |
91 | ||
92 | end_of_archs = sizeof(struct fat_header) | |
93 | + nfat_arch * sizeof(struct fat_arch); | |
94 | #if 0 | |
95 | filesize = ubc_getsize(vp); | |
96 | if (end_of_archs > (int)filesize) | |
97 | return(LOAD_BADMACHO); | |
98 | } | |
99 | #endif | |
100 | ||
101 | /* This is beacuse we are reading only 512 bytes */ | |
102 | ||
103 | if (end_of_archs > 512) | |
104 | return(LOAD_BADMACHO); | |
105 | /* | |
106 | * Round size of fat_arch structures up to page boundry. | |
107 | */ | |
108 | size = round_page(end_of_archs); | |
109 | if (size <= 0) | |
110 | return(LOAD_BADMACHO); | |
111 | ||
112 | /* | |
113 | * Scan the fat_arch's looking for the best one. | |
114 | */ | |
115 | addr = data_ptr; | |
116 | ms = &machine_slot[cpu_number()]; | |
117 | best_arch = NULL; | |
118 | best_grade = 0; | |
119 | arch = (struct fat_arch *) (addr + sizeof(struct fat_header)); | |
120 | for (; nfat_arch-- > 0; arch++) { | |
121 | ||
122 | /* | |
123 | * Check to see if right cpu type. | |
124 | */ | |
125 | if(NXSwapBigIntToHost(arch->cputype) != ms->cpu_type) | |
126 | continue; | |
127 | ||
128 | /* | |
129 | * Get the grade of the cpu subtype. | |
130 | */ | |
131 | grade = grade_cpu_subtype( | |
132 | NXSwapBigIntToHost(arch->cpusubtype)); | |
133 | ||
134 | /* | |
135 | * Remember it if it's the best we've seen. | |
136 | */ | |
137 | if (grade > best_grade) { | |
138 | best_grade = grade; | |
139 | best_arch = arch; | |
140 | } | |
141 | } | |
142 | ||
143 | /* | |
144 | * Return our results. | |
145 | */ | |
146 | if (best_arch == NULL) { | |
147 | lret = LOAD_BADARCH; | |
148 | } else { | |
149 | archret->cputype = | |
150 | NXSwapBigIntToHost(best_arch->cputype); | |
151 | archret->cpusubtype = | |
152 | NXSwapBigIntToHost(best_arch->cpusubtype); | |
153 | archret->offset = | |
154 | NXSwapBigLongToHost(best_arch->offset); | |
155 | archret->size = | |
156 | NXSwapBigLongToHost(best_arch->size); | |
157 | archret->align = | |
158 | NXSwapBigLongToHost(best_arch->align); | |
159 | ||
160 | lret = LOAD_SUCCESS; | |
161 | } | |
162 | ||
163 | /* | |
164 | * Free the memory we allocated and return. | |
165 | */ | |
166 | return(lret); | |
167 | } | |
168 | ||
169 |