2 * probe_roms - scan for Adapter ROMS
4 * (based on linux-2.6:arch/x86/kernel/probe_roms_32.c)
6 * Copyright (C) 2008 Intel Corporation
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.
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
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.
22 #include "probe_roms.h"
28 #include <sys/types.h>
29 #include <asm/types.h>
31 static void *rom_mem
= MAP_FAILED
;
32 static int rom_fd
= -1;
33 static const int rom_len
= 0xf0000 - 0xc0000; /* option-rom memory region */
35 static unsigned long rom_align
;
37 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
39 static void sigbus(int sig
)
44 static int probe_address8(const __u8
*ptr
, __u8
*val
)
56 static int probe_address16(const __u16
*ptr
, __u16
*val
)
68 void probe_roms_exit(void)
70 signal(SIGBUS
, SIG_DFL
);
75 if (rom_mem
!= MAP_FAILED
) {
76 munmap(rom_mem
, rom_len
);
81 int probe_roms_init(unsigned long align
)
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
90 if (align
== 512 || align
== 2048)
95 if (signal(SIGBUS
, sigbus
) == SIG_ERR
)
98 fd
= open("/dev/mem", O_RDONLY
);
103 rom_mem
= mmap(NULL
, rom_len
, PROT_READ
, MAP_PRIVATE
, fd
, 0xc0000);
104 if (rom_mem
== MAP_FAILED
)
119 * isa_bus_to_virt - convert physical address to mmap'd region
120 * @addr - address to convert
122 * Only valid between a successful call to probe_roms_init and the
123 * corresponding probe_roms_exit
125 static void *isa_bus_to_virt(unsigned long addr
)
127 return rom_mem
+ (addr
- 0xc0000);
136 static struct resource system_rom_resource
= {
137 .name
= "System ROM",
142 static struct resource extension_rom_resource
= {
143 .name
= "Extension ROM",
148 static struct resource adapter_rom_resources
[] = { {
149 .name
= "Adapter ROM",
153 .name
= "Adapter ROM",
157 .name
= "Adapter ROM",
161 .name
= "Adapter ROM",
165 .name
= "Adapter ROM",
169 .name
= "Adapter ROM",
174 static struct resource video_rom_resource
= {
180 #define ROMSIGNATURE 0xaa55
182 static int romsignature(const unsigned char *rom
)
184 const unsigned short * const ptr
= (const unsigned short *)rom
;
185 unsigned short sig
= 0;
187 return probe_address16(ptr
, &sig
) == 0 && sig
== ROMSIGNATURE
;
190 static int romchecksum(const unsigned char *rom
, unsigned long length
)
192 unsigned char sum
, c
;
194 for (sum
= 0; length
&& probe_address8(rom
++, &c
) == 0; length
--)
196 return !length
&& !sum
;
199 int scan_adapter_roms(scan_fn fn
)
201 /* let scan_fn examing each of the adapter roms found by probe_roms */
209 for (i
= 0; i
< ARRAY_SIZE(adapter_rom_resources
); i
++) {
210 struct resource
*res
= &adapter_rom_resources
[i
];
213 found
= fn(isa_bus_to_virt(res
->start
),
214 isa_bus_to_virt(res
->end
));
224 static unsigned long align(unsigned long addr
, unsigned long alignment
)
226 return (addr
+ alignment
- 1) & ~(alignment
- 1);
229 void probe_roms(void)
232 unsigned long start
, length
, upper
;
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
))
246 video_rom_resource
.start
= start
;
248 if (probe_address8(rom
+ 2, &c
) != 0)
251 /* 0 < length <= 0x7f * 512, historically */
254 /* if checksum okay, trust length byte */
255 if (length
&& romchecksum(rom
, length
))
256 video_rom_resource
.end
= start
+ length
- 1;
260 start
= align(video_rom_resource
.end
+ 1, rom_align
);
265 upper
= system_rom_resource
.start
;
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
;
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
))
281 if (probe_address8(rom
+ 2, &c
) != 0)
284 /* 0 < length <= 0x7f * 512, historically */
287 /* but accept any length that fits if checksum okay */
288 if (!length
|| start
+ length
> upper
|| !romchecksum(rom
, length
))
291 adapter_rom_resources
[i
].start
= start
;
292 adapter_rom_resources
[i
].end
= start
+ length
- 1;
294 start
= adapter_rom_resources
[i
++].end
& ~(rom_align
- 1);