Blame SOURCES/libpciaccess-rom-size.patch

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