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