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"
29 #include <sys/types.h>
30 #include <asm/types.h>
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 */
36 static unsigned long rom_align
;
38 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
40 static void sigbus(int sig
)
45 static int probe_address8(const __u8
*ptr
, __u8
*val
)
57 static int probe_address16(const __u16
*ptr
, __u16
*val
)
69 void probe_roms_exit(void)
71 signal(SIGBUS
, SIG_DFL
);
76 if (rom_mem
!= MAP_FAILED
) {
77 munmap(rom_mem
, rom_len
);
82 int probe_roms_init(unsigned long align
)
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
91 if (align
== 512 || align
== 2048)
96 if (signal(SIGBUS
, sigbus
) == SIG_ERR
)
99 fd
= open("/dev/mem", O_RDONLY
);
104 rom_mem
= mmap(NULL
, rom_len
, PROT_READ
, MAP_PRIVATE
, fd
, 0xc0000);
105 if (rom_mem
== MAP_FAILED
)
120 * isa_bus_to_virt - convert physical address to mmap'd region
121 * @addr - address to convert
123 * Only valid between a successful call to probe_roms_init and the
124 * corresponding probe_roms_exit
126 static void *isa_bus_to_virt(unsigned long addr
)
128 return rom_mem
+ (addr
- 0xc0000);
138 static struct resource system_rom_resource
= {
139 .name
= "System ROM",
145 static struct resource extension_rom_resource
= {
146 .name
= "Extension ROM",
152 static struct resource adapter_rom_resources
[] = { {
153 .name
= "Adapter ROM",
158 .name
= "Adapter ROM",
163 .name
= "Adapter ROM",
168 .name
= "Adapter ROM",
173 .name
= "Adapter ROM",
178 .name
= "Adapter ROM",
184 static struct resource video_rom_resource
= {
191 #define ROMSIGNATURE 0xaa55
193 static int romsignature(const unsigned char *rom
)
195 const unsigned short * const ptr
= (const unsigned short *)rom
;
196 unsigned short sig
= 0;
198 return probe_address16(ptr
, &sig
) == 0 && sig
== ROMSIGNATURE
;
201 static int romchecksum(const unsigned char *rom
, unsigned long length
)
203 unsigned char sum
, c
;
205 for (sum
= 0; length
&& probe_address8(rom
++, &c
) == 0; length
--)
207 return !length
&& !sum
;
210 int scan_adapter_roms(scan_fn fn
)
212 /* let scan_fn examing each of the adapter roms found by probe_roms */
220 for (i
= 0; i
< ARRAY_SIZE(adapter_rom_resources
); i
++) {
221 struct resource
*res
= &adapter_rom_resources
[i
];
224 found
= fn(isa_bus_to_virt(res
->start
),
225 isa_bus_to_virt(res
->end
),
226 isa_bus_to_virt(res
->data
));
236 static unsigned long align(unsigned long addr
, unsigned long alignment
)
238 return (addr
+ alignment
- 1) & ~(alignment
- 1);
241 void probe_roms(void)
244 unsigned long start
, length
, upper
;
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
))
259 video_rom_resource
.start
= start
;
261 if (probe_address8(rom
+ 2, &c
) != 0)
264 /* 0 < length <= 0x7f * 512, historically */
267 /* if checksum okay, trust length byte */
268 if (length
&& romchecksum(rom
, length
))
269 video_rom_resource
.end
= start
+ length
- 1;
273 start
= align(video_rom_resource
.end
+ 1, rom_align
);
278 upper
= system_rom_resource
.start
;
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
;
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
))
294 if (probe_address8(rom
+ 2, &c
) != 0)
297 /* 0 < length <= 0x7f * 512, historically */
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
305 if (probe_address16(rom
+ 0x18, &val
) != 0)
307 val
= __le16_to_cpu(val
);
309 /* but accept any length that fits if checksum okay */
310 if (!length
|| start
+ length
> upper
|| !romchecksum(rom
, length
))
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;
317 start
= adapter_rom_resources
[i
++].end
& ~(rom_align
- 1);