]> git.ipfire.org Git - thirdparty/mdadm.git/blob - probe_roms.c
Improve partition table code.
[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 <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;
35 static unsigned long rom_align;
36
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
81 int probe_roms_init(unsigned long align)
82 {
83 int fd = -1;
84 int rc = 0;
85
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
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 if (fd >= 0)
112 close(fd);
113 probe_roms_exit();
114 }
115 return rc;
116 }
117
118 /**
119 * isa_bus_to_virt - convert physical address to mmap'd region
120 * @addr - address to convert
121 *
122 * Only valid between a successful call to probe_roms_init and the
123 * corresponding probe_roms_exit
124 */
125 static void *isa_bus_to_virt(unsigned long addr)
126 {
127 return rom_mem + (addr - 0xc0000);
128 }
129
130 struct resource {
131 unsigned long start;
132 unsigned long end;
133 const char *name;
134 };
135
136 static struct resource system_rom_resource = {
137 .name = "System ROM",
138 .start = 0xf0000,
139 .end = 0xfffff,
140 };
141
142 static struct resource extension_rom_resource = {
143 .name = "Extension ROM",
144 .start = 0xe0000,
145 .end = 0xeffff,
146 };
147
148 static struct resource adapter_rom_resources[] = { {
149 .name = "Adapter ROM",
150 .start = 0xc8000,
151 .end = 0,
152 }, {
153 .name = "Adapter ROM",
154 .start = 0,
155 .end = 0,
156 }, {
157 .name = "Adapter ROM",
158 .start = 0,
159 .end = 0,
160 }, {
161 .name = "Adapter ROM",
162 .start = 0,
163 .end = 0,
164 }, {
165 .name = "Adapter ROM",
166 .start = 0,
167 .end = 0,
168 }, {
169 .name = "Adapter ROM",
170 .start = 0,
171 .end = 0,
172 } };
173
174 static struct resource video_rom_resource = {
175 .name = "Video ROM",
176 .start = 0xc0000,
177 .end = 0xc7fff,
178 };
179
180 #define ROMSIGNATURE 0xaa55
181
182 static int romsignature(const unsigned char *rom)
183 {
184 const unsigned short * const ptr = (const unsigned short *)rom;
185 unsigned short sig = 0;
186
187 return probe_address16(ptr, &sig) == 0 && sig == ROMSIGNATURE;
188 }
189
190 static int romchecksum(const unsigned char *rom, unsigned long length)
191 {
192 unsigned char sum, c;
193
194 for (sum = 0; length && probe_address8(rom++, &c) == 0; length--)
195 sum += c;
196 return !length && !sum;
197 }
198
199 int scan_adapter_roms(scan_fn fn)
200 {
201 /* let scan_fn examing each of the adapter roms found by probe_roms */
202 int i;
203 int found;
204
205 if (rom_fd < 0)
206 return 0;
207
208 found = 0;
209 for (i = 0; i < ARRAY_SIZE(adapter_rom_resources); i++) {
210 struct resource *res = &adapter_rom_resources[i];
211
212 if (res->start) {
213 found = fn(isa_bus_to_virt(res->start),
214 isa_bus_to_virt(res->end));
215 if (found)
216 break;
217 } else
218 break;
219 }
220
221 return found;
222 }
223
224 static unsigned long align(unsigned long addr, unsigned long alignment)
225 {
226 return (addr + alignment - 1) & ~(alignment - 1);
227 }
228
229 void probe_roms(void)
230 {
231 const void *rom;
232 unsigned long start, length, upper;
233 unsigned char c;
234 int i;
235
236 if (rom_fd < 0)
237 return;
238
239 /* video rom */
240 upper = adapter_rom_resources[0].start;
241 for (start = video_rom_resource.start; start < upper; start += rom_align) {
242 rom = isa_bus_to_virt(start);
243 if (!romsignature(rom))
244 continue;
245
246 video_rom_resource.start = start;
247
248 if (probe_address8(rom + 2, &c) != 0)
249 continue;
250
251 /* 0 < length <= 0x7f * 512, historically */
252 length = c * 512;
253
254 /* if checksum okay, trust length byte */
255 if (length && romchecksum(rom, length))
256 video_rom_resource.end = start + length - 1;
257 break;
258 }
259
260 start = align(video_rom_resource.end + 1, rom_align);
261 if (start < upper)
262 start = upper;
263
264 /* system rom */
265 upper = system_rom_resource.start;
266
267 /* check for extension rom (ignore length byte!) */
268 rom = isa_bus_to_virt(extension_rom_resource.start);
269 if (romsignature(rom)) {
270 length = extension_rom_resource.end - extension_rom_resource.start + 1;
271 if (romchecksum(rom, length))
272 upper = extension_rom_resource.start;
273 }
274
275 /* check for adapter roms on 2k boundaries */
276 for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += rom_align) {
277 rom = isa_bus_to_virt(start);
278 if (!romsignature(rom))
279 continue;
280
281 if (probe_address8(rom + 2, &c) != 0)
282 continue;
283
284 /* 0 < length <= 0x7f * 512, historically */
285 length = c * 512;
286
287 /* but accept any length that fits if checksum okay */
288 if (!length || start + length > upper || !romchecksum(rom, length))
289 continue;
290
291 adapter_rom_resources[i].start = start;
292 adapter_rom_resources[i].end = start + length - 1;
293
294 start = adapter_rom_resources[i++].end & ~(rom_align - 1);
295 }
296 }
297