]>
Commit | Line | Data |
---|---|---|
39795f9c DW |
1 | /* |
2 | * probe_roms - scan for Adapter ROMS | |
3 | * | |
4 | * (based on linux-2.6:arch/x86/kernel/probe_roms_32.c) | |
5 | * | |
6 | * Copyright (C) 2008 Intel Corporation | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify it | |
9 | * under the terms and conditions of the GNU General Public License, | |
10 | * version 2, as published by the Free Software Foundation. | |
11 | * | |
12 | * This program is distributed in the hope it will be useful, but WITHOUT | |
13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
15 | * more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License along with | |
18 | * this program; if not, write to the Free Software Foundation, Inc., | |
19 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. | |
20 | */ | |
21 | ||
22 | #include "probe_roms.h" | |
3c8bfb5d | 23 | #include "mdadm.h" |
39795f9c DW |
24 | #include <unistd.h> |
25 | #include <signal.h> | |
26 | #include <fcntl.h> | |
27 | #include <sys/mman.h> | |
28 | #include <sys/stat.h> | |
29 | #include <sys/types.h> | |
30 | #include <asm/types.h> | |
31 | ||
32 | static void *rom_mem = MAP_FAILED; | |
33 | static int rom_fd = -1; | |
f21e18ca | 34 | static const int rom_len = 0xf0000 - 0xc0000; /* option-rom memory region */ |
39795f9c | 35 | static int _sigbus; |
969c2555 DW |
36 | static unsigned long rom_align; |
37 | ||
39795f9c DW |
38 | static void sigbus(int sig) |
39 | { | |
40 | _sigbus = 1; | |
41 | } | |
42 | ||
43 | static int probe_address8(const __u8 *ptr, __u8 *val) | |
44 | { | |
45 | int rc = 0; | |
46 | ||
47 | *val = *ptr; | |
48 | if (_sigbus) | |
49 | rc = -1; | |
50 | _sigbus = 0; | |
51 | ||
52 | return rc; | |
53 | } | |
54 | ||
55 | static int probe_address16(const __u16 *ptr, __u16 *val) | |
56 | { | |
57 | int rc = 0; | |
58 | ||
59 | *val = *ptr; | |
60 | if (_sigbus) | |
61 | rc = -1; | |
62 | _sigbus = 0; | |
63 | ||
64 | return rc; | |
65 | } | |
66 | ||
67 | void probe_roms_exit(void) | |
68 | { | |
69 | signal(SIGBUS, SIG_DFL); | |
70 | if (rom_fd >= 0) { | |
71 | close(rom_fd); | |
72 | rom_fd = -1; | |
73 | } | |
74 | if (rom_mem != MAP_FAILED) { | |
75 | munmap(rom_mem, rom_len); | |
76 | rom_mem = MAP_FAILED; | |
77 | } | |
78 | } | |
79 | ||
969c2555 | 80 | int probe_roms_init(unsigned long align) |
39795f9c | 81 | { |
922f66a9 | 82 | int fd = -1; |
39795f9c DW |
83 | int rc = 0; |
84 | ||
969c2555 DW |
85 | /* valid values are 2048 and 512. 512 is for PCI-3.0 compliant |
86 | * systems, or systems that do not have dangerous/legacy ISA | |
87 | * devices. 2048 should always be safe | |
88 | */ | |
89 | if (align == 512 || align == 2048) | |
90 | rom_align = align; | |
91 | else | |
92 | return -1; | |
93 | ||
39795f9c DW |
94 | if (signal(SIGBUS, sigbus) == SIG_ERR) |
95 | rc = -1; | |
96 | if (rc == 0) { | |
97 | fd = open("/dev/mem", O_RDONLY); | |
98 | if (fd < 0) | |
99 | rc = -1; | |
100 | } | |
101 | if (rc == 0) { | |
102 | rom_mem = mmap(NULL, rom_len, PROT_READ, MAP_PRIVATE, fd, 0xc0000); | |
103 | if (rom_mem == MAP_FAILED) | |
104 | rc = -1; | |
105 | } | |
106 | ||
107 | if (rc == 0) | |
108 | rom_fd = fd; | |
922f66a9 | 109 | else { |
1011e834 | 110 | if (fd >= 0) |
922f66a9 | 111 | close(fd); |
39795f9c | 112 | probe_roms_exit(); |
922f66a9 | 113 | } |
39795f9c DW |
114 | return rc; |
115 | } | |
116 | ||
117 | /** | |
118 | * isa_bus_to_virt - convert physical address to mmap'd region | |
119 | * @addr - address to convert | |
120 | * | |
121 | * Only valid between a successful call to probe_roms_init and the | |
122 | * corresponding probe_roms_exit | |
123 | */ | |
124 | static void *isa_bus_to_virt(unsigned long addr) | |
125 | { | |
126 | return rom_mem + (addr - 0xc0000); | |
127 | } | |
128 | ||
129 | struct resource { | |
130 | unsigned long start; | |
131 | unsigned long end; | |
3c8bfb5d | 132 | unsigned long data; |
39795f9c DW |
133 | const char *name; |
134 | }; | |
135 | ||
136 | static struct resource system_rom_resource = { | |
137 | .name = "System ROM", | |
138 | .start = 0xf0000, | |
3c8bfb5d | 139 | .data = 0, |
39795f9c DW |
140 | .end = 0xfffff, |
141 | }; | |
142 | ||
143 | static struct resource extension_rom_resource = { | |
144 | .name = "Extension ROM", | |
145 | .start = 0xe0000, | |
3c8bfb5d | 146 | .data = 0, |
39795f9c DW |
147 | .end = 0xeffff, |
148 | }; | |
149 | ||
150 | static struct resource adapter_rom_resources[] = { { | |
1011e834 | 151 | .name = "Adapter ROM", |
39795f9c | 152 | .start = 0xc8000, |
3c8bfb5d | 153 | .data = 0, |
39795f9c DW |
154 | .end = 0, |
155 | }, { | |
1011e834 | 156 | .name = "Adapter ROM", |
39795f9c | 157 | .start = 0, |
3c8bfb5d | 158 | .data = 0, |
39795f9c DW |
159 | .end = 0, |
160 | }, { | |
1011e834 | 161 | .name = "Adapter ROM", |
39795f9c | 162 | .start = 0, |
3c8bfb5d | 163 | .data = 0, |
39795f9c DW |
164 | .end = 0, |
165 | }, { | |
1011e834 | 166 | .name = "Adapter ROM", |
39795f9c | 167 | .start = 0, |
3c8bfb5d | 168 | .data = 0, |
39795f9c DW |
169 | .end = 0, |
170 | }, { | |
1011e834 | 171 | .name = "Adapter ROM", |
39795f9c | 172 | .start = 0, |
3c8bfb5d | 173 | .data = 0, |
39795f9c DW |
174 | .end = 0, |
175 | }, { | |
1011e834 | 176 | .name = "Adapter ROM", |
39795f9c | 177 | .start = 0, |
3c8bfb5d | 178 | .data = 0, |
39795f9c DW |
179 | .end = 0, |
180 | } }; | |
181 | ||
182 | static struct resource video_rom_resource = { | |
1011e834 | 183 | .name = "Video ROM", |
39795f9c | 184 | .start = 0xc0000, |
3c8bfb5d | 185 | .data = 0, |
39795f9c DW |
186 | .end = 0xc7fff, |
187 | }; | |
188 | ||
189 | #define ROMSIGNATURE 0xaa55 | |
190 | ||
191 | static int romsignature(const unsigned char *rom) | |
192 | { | |
193 | const unsigned short * const ptr = (const unsigned short *)rom; | |
194 | unsigned short sig = 0; | |
195 | ||
196 | return probe_address16(ptr, &sig) == 0 && sig == ROMSIGNATURE; | |
197 | } | |
198 | ||
199 | static int romchecksum(const unsigned char *rom, unsigned long length) | |
200 | { | |
201 | unsigned char sum, c; | |
202 | ||
203 | for (sum = 0; length && probe_address8(rom++, &c) == 0; length--) | |
204 | sum += c; | |
205 | return !length && !sum; | |
206 | } | |
207 | ||
208 | int scan_adapter_roms(scan_fn fn) | |
209 | { | |
210 | /* let scan_fn examing each of the adapter roms found by probe_roms */ | |
f21e18ca | 211 | unsigned int i; |
39795f9c DW |
212 | int found; |
213 | ||
214 | if (rom_fd < 0) | |
215 | return 0; | |
216 | ||
217 | found = 0; | |
218 | for (i = 0; i < ARRAY_SIZE(adapter_rom_resources); i++) { | |
219 | struct resource *res = &adapter_rom_resources[i]; | |
220 | ||
221 | if (res->start) { | |
222 | found = fn(isa_bus_to_virt(res->start), | |
3c8bfb5d LM |
223 | isa_bus_to_virt(res->end), |
224 | isa_bus_to_virt(res->data)); | |
39795f9c DW |
225 | if (found) |
226 | break; | |
227 | } else | |
228 | break; | |
229 | } | |
230 | ||
231 | return found; | |
232 | } | |
233 | ||
969c2555 DW |
234 | static unsigned long align(unsigned long addr, unsigned long alignment) |
235 | { | |
236 | return (addr + alignment - 1) & ~(alignment - 1); | |
237 | } | |
238 | ||
39795f9c DW |
239 | void probe_roms(void) |
240 | { | |
241 | const void *rom; | |
242 | unsigned long start, length, upper; | |
243 | unsigned char c; | |
f21e18ca | 244 | unsigned int i; |
3c8bfb5d | 245 | __u16 val=0; |
39795f9c DW |
246 | |
247 | if (rom_fd < 0) | |
248 | return; | |
249 | ||
250 | /* video rom */ | |
251 | upper = adapter_rom_resources[0].start; | |
969c2555 | 252 | for (start = video_rom_resource.start; start < upper; start += rom_align) { |
39795f9c DW |
253 | rom = isa_bus_to_virt(start); |
254 | if (!romsignature(rom)) | |
255 | continue; | |
256 | ||
257 | video_rom_resource.start = start; | |
258 | ||
259 | if (probe_address8(rom + 2, &c) != 0) | |
260 | continue; | |
261 | ||
262 | /* 0 < length <= 0x7f * 512, historically */ | |
263 | length = c * 512; | |
264 | ||
265 | /* if checksum okay, trust length byte */ | |
266 | if (length && romchecksum(rom, length)) | |
267 | video_rom_resource.end = start + length - 1; | |
268 | break; | |
269 | } | |
270 | ||
969c2555 | 271 | start = align(video_rom_resource.end + 1, rom_align); |
39795f9c DW |
272 | if (start < upper) |
273 | start = upper; | |
274 | ||
275 | /* system rom */ | |
276 | upper = system_rom_resource.start; | |
277 | ||
278 | /* check for extension rom (ignore length byte!) */ | |
279 | rom = isa_bus_to_virt(extension_rom_resource.start); | |
280 | if (romsignature(rom)) { | |
281 | length = extension_rom_resource.end - extension_rom_resource.start + 1; | |
282 | if (romchecksum(rom, length)) | |
283 | upper = extension_rom_resource.start; | |
284 | } | |
285 | ||
286 | /* check for adapter roms on 2k boundaries */ | |
969c2555 | 287 | for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += rom_align) { |
39795f9c DW |
288 | rom = isa_bus_to_virt(start); |
289 | if (!romsignature(rom)) | |
290 | continue; | |
291 | ||
292 | if (probe_address8(rom + 2, &c) != 0) | |
293 | continue; | |
294 | ||
295 | /* 0 < length <= 0x7f * 512, historically */ | |
296 | length = c * 512; | |
297 | ||
3c8bfb5d LM |
298 | /* Retrieve 16-bit pointer to PCI Data Structure (offset 18h-19h) |
299 | * The data can be within 64KB forward of the first location | |
300 | * of this code image. The pointer is in little-endian order | |
301 | */ | |
302 | ||
303 | if (probe_address16(rom + 0x18, &val) != 0) | |
304 | continue; | |
305 | val = __le16_to_cpu(val); | |
306 | ||
39795f9c DW |
307 | /* but accept any length that fits if checksum okay */ |
308 | if (!length || start + length > upper || !romchecksum(rom, length)) | |
309 | continue; | |
310 | ||
311 | adapter_rom_resources[i].start = start; | |
3c8bfb5d | 312 | adapter_rom_resources[i].data = start + (unsigned long) val; |
39795f9c DW |
313 | adapter_rom_resources[i].end = start + length - 1; |
314 | ||
969c2555 | 315 | start = adapter_rom_resources[i++].end & ~(rom_align - 1); |
39795f9c DW |
316 | } |
317 | } |