Blame SOURCES/libpciaccess-rom-size.patch

be576a
diff --git a/src/linux_sysfs.c b/src/linux_sysfs.c
be576a
index 8c3cf67..2ea78c1 100644
be576a
--- a/src/linux_sysfs.c
be576a
+++ b/src/linux_sysfs.c
be576a
@@ -227,6 +227,7 @@ pci_device_linux_sysfs_probe( struct pci_device * dev )
be576a
     pciaddr_t bytes;
be576a
     unsigned i;
be576a
     int err;
be576a
+    struct stat  st;
be576a
 
be576a
 
be576a
     err = pci_device_linux_sysfs_read( dev, config, 0, 256, & bytes );
be576a
@@ -293,11 +294,28 @@ pci_device_linux_sysfs_probe( struct pci_device * dev )
be576a
 	    flags = strtoull( next, & next, 16 );
be576a
 	    if ( low_addr != 0 ) {
be576a
 		priv->rom_base = low_addr;
be576a
-		dev->rom_size = (high_addr - low_addr) + 1;
be576a
 	    }
be576a
 	}
be576a
     }
be576a
 
be576a
+    snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/rom",
be576a
+	      SYS_BUS_PCI,
be576a
+	      dev->domain,
be576a
+	      dev->bus,
be576a
+	      dev->dev,
be576a
+	      dev->func );
be576a
+    
be576a
+    fd = open( name, O_RDWR );
be576a
+    if ( fd == -1 ) {
be576a
+	dev->rom_size = 0x10000;
be576a
+    } else if ( fstat( fd, & st ) == -1 ) {
be576a
+	close( fd );
be576a
+	dev->rom_size = 0x10000;
be576a
+    } else {
be576a
+	close( fd );
be576a
+	dev->rom_size = st.st_size == 0 ? 0x10000 : st.st_size;
be576a
+    }
be576a
+
be576a
     return err;
be576a
 }
be576a
 
be576a
@@ -309,7 +327,6 @@ pci_device_linux_sysfs_read_rom( struct pci_device * dev, void * buffer )
be576a
     int fd;
be576a
     struct stat  st;
be576a
     int err = 0;
be576a
-    size_t rom_size;
be576a
     size_t total_bytes;
be576a
 
be576a
 
be576a
@@ -334,10 +351,6 @@ pci_device_linux_sysfs_read_rom( struct pci_device * dev, void * buffer )
be576a
 	return errno;
be576a
     }
be576a
 
be576a
-    rom_size = st.st_size;
be576a
-    if ( rom_size == 0 )
be576a
-	rom_size = 0x10000;
be576a
-
be576a
     /* This is a quirky thing on Linux.  Even though the ROM and the file
be576a
      * for the ROM in sysfs are read-only, the string "1" must be written to
be576a
      * the file to enable the ROM.  After the data has been read, "0" must be
be576a
@@ -346,9 +359,9 @@ pci_device_linux_sysfs_read_rom( struct pci_device * dev, void * buffer )
be576a
     write( fd, "1", 1 );
be576a
     lseek( fd, 0, SEEK_SET );
be576a
 
be576a
-    for ( total_bytes = 0 ; total_bytes < rom_size ; /* empty */ ) {
be576a
+    for ( total_bytes = 0 ; total_bytes < dev->rom_size ; /* empty */ ) {
be576a
 	const int bytes = read( fd, (char *) buffer + total_bytes,
be576a
-				rom_size - total_bytes );
be576a
+				dev->rom_size - total_bytes );
be576a
 	if ( bytes == -1 ) {
be576a
 	    err = errno;
be576a
 	    break;