]>
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" | |
23 | #include <unistd.h> | |
24 | #include <signal.h> | |
25 | #include <fcntl.h> | |
26 | #include <sys/mman.h> | |
27 | #include <sys/stat.h> | |
28 | #include <sys/types.h> | |
29 | #include <asm/types.h> | |
30 | ||
31 | static void *rom_mem = MAP_FAILED; | |
32 | static int rom_fd = -1; | |
33 | const static int rom_len = 0xf0000 - 0xc0000; /* option-rom memory region */ | |
34 | static int _sigbus; | |
969c2555 DW |
35 | static unsigned long rom_align; |
36 | ||
39795f9c DW |
37 | #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) |
38 | ||
39 | static void sigbus(int sig) | |
40 | { | |
41 | _sigbus = 1; | |
42 | } | |
43 | ||
44 | static int probe_address8(const __u8 *ptr, __u8 *val) | |
45 | { | |
46 | int rc = 0; | |
47 | ||
48 | *val = *ptr; | |
49 | if (_sigbus) | |
50 | rc = -1; | |
51 | _sigbus = 0; | |
52 | ||
53 | return rc; | |
54 | } | |
55 | ||
56 | static int probe_address16(const __u16 *ptr, __u16 *val) | |
57 | { | |
58 | int rc = 0; | |
59 | ||
60 | *val = *ptr; | |
61 | if (_sigbus) | |
62 | rc = -1; | |
63 | _sigbus = 0; | |
64 | ||
65 | return rc; | |
66 | } | |
67 | ||
68 | void probe_roms_exit(void) | |
69 | { | |
70 | signal(SIGBUS, SIG_DFL); | |
71 | if (rom_fd >= 0) { | |
72 | close(rom_fd); | |
73 | rom_fd = -1; | |
74 | } | |
75 | if (rom_mem != MAP_FAILED) { | |
76 | munmap(rom_mem, rom_len); | |
77 | rom_mem = MAP_FAILED; | |
78 | } | |
79 | } | |
80 | ||
969c2555 | 81 | int probe_roms_init(unsigned long align) |
39795f9c DW |
82 | { |
83 | int fd; | |
84 | int rc = 0; | |
85 | ||
969c2555 DW |
86 | /* valid values are 2048 and 512. 512 is for PCI-3.0 compliant |
87 | * systems, or systems that do not have dangerous/legacy ISA | |
88 | * devices. 2048 should always be safe | |
89 | */ | |
90 | if (align == 512 || align == 2048) | |
91 | rom_align = align; | |
92 | else | |
93 | return -1; | |
94 | ||
39795f9c DW |
95 | if (signal(SIGBUS, sigbus) == SIG_ERR) |
96 | rc = -1; | |
97 | if (rc == 0) { | |
98 | fd = open("/dev/mem", O_RDONLY); | |
99 | if (fd < 0) | |
100 | rc = -1; | |
101 | } | |
102 | if (rc == 0) { | |
103 | rom_mem = mmap(NULL, rom_len, PROT_READ, MAP_PRIVATE, fd, 0xc0000); | |
104 | if (rom_mem == MAP_FAILED) | |
105 | rc = -1; | |
106 | } | |
107 | ||
108 | if (rc == 0) | |
109 | rom_fd = fd; | |
110 | else | |
111 | probe_roms_exit(); | |
112 | ||
113 | return rc; | |
114 | } | |
115 | ||
116 | /** | |
117 | * isa_bus_to_virt - convert physical address to mmap'd region | |
118 | * @addr - address to convert | |
119 | * | |
120 | * Only valid between a successful call to probe_roms_init and the | |
121 | * corresponding probe_roms_exit | |
122 | */ | |
123 | static void *isa_bus_to_virt(unsigned long addr) | |
124 | { | |
125 | return rom_mem + (addr - 0xc0000); | |
126 | } | |
127 | ||
128 | struct resource { | |
129 | unsigned long start; | |
130 | unsigned long end; | |
131 | const char *name; | |
132 | }; | |
133 | ||
134 | static struct resource system_rom_resource = { | |
135 | .name = "System ROM", | |
136 | .start = 0xf0000, | |
137 | .end = 0xfffff, | |
138 | }; | |
139 | ||
140 | static struct resource extension_rom_resource = { | |
141 | .name = "Extension ROM", | |
142 | .start = 0xe0000, | |
143 | .end = 0xeffff, | |
144 | }; | |
145 | ||
146 | static struct resource adapter_rom_resources[] = { { | |
147 | .name = "Adapter ROM", | |
148 | .start = 0xc8000, | |
149 | .end = 0, | |
150 | }, { | |
151 | .name = "Adapter ROM", | |
152 | .start = 0, | |
153 | .end = 0, | |
154 | }, { | |
155 | .name = "Adapter ROM", | |
156 | .start = 0, | |
157 | .end = 0, | |
158 | }, { | |
159 | .name = "Adapter ROM", | |
160 | .start = 0, | |
161 | .end = 0, | |
162 | }, { | |
163 | .name = "Adapter ROM", | |
164 | .start = 0, | |
165 | .end = 0, | |
166 | }, { | |
167 | .name = "Adapter ROM", | |
168 | .start = 0, | |
169 | .end = 0, | |
170 | } }; | |
171 | ||
172 | static struct resource video_rom_resource = { | |
173 | .name = "Video ROM", | |
174 | .start = 0xc0000, | |
175 | .end = 0xc7fff, | |
176 | }; | |
177 | ||
178 | #define ROMSIGNATURE 0xaa55 | |
179 | ||
180 | static int romsignature(const unsigned char *rom) | |
181 | { | |
182 | const unsigned short * const ptr = (const unsigned short *)rom; | |
183 | unsigned short sig = 0; | |
184 | ||
185 | return probe_address16(ptr, &sig) == 0 && sig == ROMSIGNATURE; | |
186 | } | |
187 | ||
188 | static int romchecksum(const unsigned char *rom, unsigned long length) | |
189 | { | |
190 | unsigned char sum, c; | |
191 | ||
192 | for (sum = 0; length && probe_address8(rom++, &c) == 0; length--) | |
193 | sum += c; | |
194 | return !length && !sum; | |
195 | } | |
196 | ||
197 | int scan_adapter_roms(scan_fn fn) | |
198 | { | |
199 | /* let scan_fn examing each of the adapter roms found by probe_roms */ | |
200 | int i; | |
201 | int found; | |
202 | ||
203 | if (rom_fd < 0) | |
204 | return 0; | |
205 | ||
206 | found = 0; | |
207 | for (i = 0; i < ARRAY_SIZE(adapter_rom_resources); i++) { | |
208 | struct resource *res = &adapter_rom_resources[i]; | |
209 | ||
210 | if (res->start) { | |
211 | found = fn(isa_bus_to_virt(res->start), | |
212 | isa_bus_to_virt(res->end)); | |
213 | if (found) | |
214 | break; | |
215 | } else | |
216 | break; | |
217 | } | |
218 | ||
219 | return found; | |
220 | } | |
221 | ||
969c2555 DW |
222 | static unsigned long align(unsigned long addr, unsigned long alignment) |
223 | { | |
224 | return (addr + alignment - 1) & ~(alignment - 1); | |
225 | } | |
226 | ||
39795f9c DW |
227 | void probe_roms(void) |
228 | { | |
229 | const void *rom; | |
230 | unsigned long start, length, upper; | |
231 | unsigned char c; | |
232 | int i; | |
233 | ||
234 | if (rom_fd < 0) | |
235 | return; | |
236 | ||
237 | /* video rom */ | |
238 | upper = adapter_rom_resources[0].start; | |
969c2555 | 239 | for (start = video_rom_resource.start; start < upper; start += rom_align) { |
39795f9c DW |
240 | rom = isa_bus_to_virt(start); |
241 | if (!romsignature(rom)) | |
242 | continue; | |
243 | ||
244 | video_rom_resource.start = start; | |
245 | ||
246 | if (probe_address8(rom + 2, &c) != 0) | |
247 | continue; | |
248 | ||
249 | /* 0 < length <= 0x7f * 512, historically */ | |
250 | length = c * 512; | |
251 | ||
252 | /* if checksum okay, trust length byte */ | |
253 | if (length && romchecksum(rom, length)) | |
254 | video_rom_resource.end = start + length - 1; | |
255 | break; | |
256 | } | |
257 | ||
969c2555 | 258 | start = align(video_rom_resource.end + 1, rom_align); |
39795f9c DW |
259 | if (start < upper) |
260 | start = upper; | |
261 | ||
262 | /* system rom */ | |
263 | upper = system_rom_resource.start; | |
264 | ||
265 | /* check for extension rom (ignore length byte!) */ | |
266 | rom = isa_bus_to_virt(extension_rom_resource.start); | |
267 | if (romsignature(rom)) { | |
268 | length = extension_rom_resource.end - extension_rom_resource.start + 1; | |
269 | if (romchecksum(rom, length)) | |
270 | upper = extension_rom_resource.start; | |
271 | } | |
272 | ||
273 | /* check for adapter roms on 2k boundaries */ | |
969c2555 | 274 | for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += rom_align) { |
39795f9c DW |
275 | rom = isa_bus_to_virt(start); |
276 | if (!romsignature(rom)) | |
277 | continue; | |
278 | ||
279 | if (probe_address8(rom + 2, &c) != 0) | |
280 | continue; | |
281 | ||
282 | /* 0 < length <= 0x7f * 512, historically */ | |
283 | length = c * 512; | |
284 | ||
285 | /* but accept any length that fits if checksum okay */ | |
286 | if (!length || start + length > upper || !romchecksum(rom, length)) | |
287 | continue; | |
288 | ||
289 | adapter_rom_resources[i].start = start; | |
290 | adapter_rom_resources[i].end = start + length - 1; | |
291 | ||
969c2555 | 292 | start = adapter_rom_resources[i++].end & ~(rom_align - 1); |
39795f9c DW |
293 | } |
294 | } | |
295 |