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 static void roms_deinit(void);
39 static int roms_init(void);
41 static void sigbus(int sig
)
46 static int probe_address8(const __u8
*ptr
, __u8
*val
)
58 static int probe_address16(const __u16
*ptr
, __u16
*val
)
70 void probe_roms_exit(void)
72 signal(SIGBUS
, SIG_DFL
);
77 if (rom_mem
!= MAP_FAILED
) {
78 munmap(rom_mem
, rom_len
);
84 int probe_roms_init(unsigned long align
)
89 /* valid values are 2048 and 512. 512 is for PCI-3.0 compliant
90 * systems, or systems that do not have dangerous/legacy ISA
91 * devices. 2048 should always be safe
93 if (align
== 512 || align
== 2048)
101 if (signal(SIGBUS
, sigbus
) == SIG_ERR
)
104 fd
= open("/dev/mem", O_RDONLY
);
109 rom_mem
= mmap(NULL
, rom_len
, PROT_READ
, MAP_PRIVATE
, fd
, 0xc0000);
110 if (rom_mem
== MAP_FAILED
)
125 * isa_bus_to_virt - convert physical address to mmap'd region
126 * @addr - address to convert
128 * Only valid between a successful call to probe_roms_init and the
129 * corresponding probe_roms_exit
131 static void *isa_bus_to_virt(unsigned long addr
)
133 return rom_mem
+ (addr
- 0xc0000);
141 struct resource
*next
;
144 static struct resource system_rom_resource
= {
145 .name
= "System ROM",
151 static struct resource extension_rom_resource
= {
152 .name
= "Extension ROM",
158 static struct resource
*adapter_rom_resources
;
160 static struct resource video_rom_resource
= {
167 static int roms_init(void)
169 adapter_rom_resources
= malloc(sizeof(struct resource
));
170 if (adapter_rom_resources
== NULL
)
172 adapter_rom_resources
->name
= "Adapter ROM";
173 adapter_rom_resources
->start
= 0xc8000;
174 adapter_rom_resources
->data
= 0;
175 adapter_rom_resources
->end
= 0;
176 adapter_rom_resources
->next
= NULL
;
180 static void roms_deinit(void)
182 struct resource
*res
;
184 res
= adapter_rom_resources
;
186 struct resource
*tmp
= res
;
193 #define ROMSIGNATURE 0xaa55
196 static int romsignature(const unsigned char *rom
)
198 const unsigned short * const ptr
= (const unsigned short *)rom
;
199 unsigned short sig
= 0;
201 return probe_address16(ptr
, &sig
) == 0 && sig
== ROMSIGNATURE
;
204 static int romchecksum(const unsigned char *rom
, unsigned long length
)
206 unsigned char sum
, c
;
208 for (sum
= 0; length
&& probe_address8(rom
++, &c
) == 0; length
--)
210 return !length
&& !sum
;
213 int scan_adapter_roms(scan_fn fn
)
215 /* let scan_fn examing each of the adapter roms found by probe_roms */
216 struct resource
*res
= adapter_rom_resources
;
225 found
= fn(isa_bus_to_virt(res
->start
),
226 isa_bus_to_virt(res
->end
),
227 isa_bus_to_virt(res
->data
));
238 static unsigned long align(unsigned long addr
, unsigned long alignment
)
240 return (addr
+ alignment
- 1) & ~(alignment
- 1);
243 void probe_roms(void)
246 unsigned long start
, length
, upper
;
248 struct resource
*res
= adapter_rom_resources
;
256 for (start
= video_rom_resource
.start
; start
< upper
; start
+= rom_align
) {
257 rom
= isa_bus_to_virt(start
);
258 if (!romsignature(rom
))
261 video_rom_resource
.start
= start
;
263 if (probe_address8(rom
+ 2, &c
) != 0)
266 /* 0 < length <= 0x7f * 512, historically */
269 /* if checksum okay, trust length byte */
270 if (length
&& romchecksum(rom
, length
))
271 video_rom_resource
.end
= start
+ length
- 1;
275 start
= align(video_rom_resource
.end
+ 1, rom_align
);
280 upper
= system_rom_resource
.start
;
282 /* check for extension rom (ignore length byte!) */
283 rom
= isa_bus_to_virt(extension_rom_resource
.start
);
284 if (romsignature(rom
)) {
285 length
= extension_rom_resource
.end
- extension_rom_resource
.start
+ 1;
286 if (romchecksum(rom
, length
))
287 upper
= extension_rom_resource
.start
;
290 struct resource
*prev_res
= res
;
291 /* check for adapter roms on 2k boundaries */
292 for (; start
< upper
; start
+= rom_align
) {
293 rom
= isa_bus_to_virt(start
);
294 if (!romsignature(rom
))
297 if (probe_address8(rom
+ 2, &c
) != 0)
300 /* 0 < length <= 0x7f * 512, historically */
303 /* Retrieve 16-bit pointer to PCI Data Structure (offset 18h-19h)
304 * The data can be within 64KB forward of the first location
305 * of this code image. The pointer is in little-endian order
308 if (probe_address16(rom
+ 0x18, &val
) != 0)
310 val
= __le16_to_cpu(val
);
312 /* but accept any length that fits if checksum okay */
313 if (!length
|| start
+ length
> upper
|| !romchecksum(rom
, length
))
317 res
= calloc(1, sizeof(struct resource
));
320 prev_res
->next
= res
;
324 res
->data
= start
+ (unsigned long)val
;
325 res
->end
= start
+ length
- 1;
327 start
= res
->end
& ~(rom_align
- 1);