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 const static int rom_len
= 0xf0000 - 0xc0000; /* option-rom memory region */
35 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
37 static void sigbus(int sig
)
42 static int probe_address8(const __u8
*ptr
, __u8
*val
)
54 static int probe_address16(const __u16
*ptr
, __u16
*val
)
66 void probe_roms_exit(void)
68 signal(SIGBUS
, SIG_DFL
);
73 if (rom_mem
!= MAP_FAILED
) {
74 munmap(rom_mem
, rom_len
);
79 int probe_roms_init(void)
84 if (signal(SIGBUS
, sigbus
) == SIG_ERR
)
87 fd
= open("/dev/mem", O_RDONLY
);
92 rom_mem
= mmap(NULL
, rom_len
, PROT_READ
, MAP_PRIVATE
, fd
, 0xc0000);
93 if (rom_mem
== MAP_FAILED
)
106 * isa_bus_to_virt - convert physical address to mmap'd region
107 * @addr - address to convert
109 * Only valid between a successful call to probe_roms_init and the
110 * corresponding probe_roms_exit
112 static void *isa_bus_to_virt(unsigned long addr
)
114 return rom_mem
+ (addr
- 0xc0000);
123 static struct resource system_rom_resource
= {
124 .name
= "System ROM",
129 static struct resource extension_rom_resource
= {
130 .name
= "Extension ROM",
135 static struct resource adapter_rom_resources
[] = { {
136 .name
= "Adapter ROM",
140 .name
= "Adapter ROM",
144 .name
= "Adapter ROM",
148 .name
= "Adapter ROM",
152 .name
= "Adapter ROM",
156 .name
= "Adapter ROM",
161 static struct resource video_rom_resource
= {
167 #define ROMSIGNATURE 0xaa55
169 static int romsignature(const unsigned char *rom
)
171 const unsigned short * const ptr
= (const unsigned short *)rom
;
172 unsigned short sig
= 0;
174 return probe_address16(ptr
, &sig
) == 0 && sig
== ROMSIGNATURE
;
177 static int romchecksum(const unsigned char *rom
, unsigned long length
)
179 unsigned char sum
, c
;
181 for (sum
= 0; length
&& probe_address8(rom
++, &c
) == 0; length
--)
183 return !length
&& !sum
;
186 int scan_adapter_roms(scan_fn fn
)
188 /* let scan_fn examing each of the adapter roms found by probe_roms */
196 for (i
= 0; i
< ARRAY_SIZE(adapter_rom_resources
); i
++) {
197 struct resource
*res
= &adapter_rom_resources
[i
];
200 found
= fn(isa_bus_to_virt(res
->start
),
201 isa_bus_to_virt(res
->end
));
211 void probe_roms(void)
214 unsigned long start
, length
, upper
;
222 upper
= adapter_rom_resources
[0].start
;
223 for (start
= video_rom_resource
.start
; start
< upper
; start
+= 2048) {
224 rom
= isa_bus_to_virt(start
);
225 if (!romsignature(rom
))
228 video_rom_resource
.start
= start
;
230 if (probe_address8(rom
+ 2, &c
) != 0)
233 /* 0 < length <= 0x7f * 512, historically */
236 /* if checksum okay, trust length byte */
237 if (length
&& romchecksum(rom
, length
))
238 video_rom_resource
.end
= start
+ length
- 1;
242 start
= (video_rom_resource
.end
+ 1 + 2047) & ~2047UL;
247 upper
= system_rom_resource
.start
;
249 /* check for extension rom (ignore length byte!) */
250 rom
= isa_bus_to_virt(extension_rom_resource
.start
);
251 if (romsignature(rom
)) {
252 length
= extension_rom_resource
.end
- extension_rom_resource
.start
+ 1;
253 if (romchecksum(rom
, length
))
254 upper
= extension_rom_resource
.start
;
257 /* check for adapter roms on 2k boundaries */
258 for (i
= 0; i
< ARRAY_SIZE(adapter_rom_resources
) && start
< upper
; start
+= 2048) {
259 rom
= isa_bus_to_virt(start
);
260 if (!romsignature(rom
))
263 if (probe_address8(rom
+ 2, &c
) != 0)
266 /* 0 < length <= 0x7f * 512, historically */
269 /* but accept any length that fits if checksum okay */
270 if (!length
|| start
+ length
> upper
|| !romchecksum(rom
, length
))
273 adapter_rom_resources
[i
].start
= start
;
274 adapter_rom_resources
[i
].end
= start
+ length
- 1;
276 start
= adapter_rom_resources
[i
++].end
& ~2047UL;