From 2bb444b94313f801544d6947fa3dd3acee92f5b0 Mon Sep 17 00:00:00 2001 From: Stuart Bennett Date: Mon, 10 Mar 2008 22:17:50 +0000 Subject: [PATCH] Handle ROMs reporting a zero size properly --- src/linux_devmem.c | 2 +- src/linux_sysfs.c | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/linux_devmem.c b/src/linux_devmem.c index a68ea11..21b45ef 100644 --- a/src/linux_devmem.c +++ b/src/linux_devmem.c @@ -124,7 +124,7 @@ pci_device_linux_devmem_read_rom(struct pci_device *dev, void *buffer) else { size_t bytes; - for (bytes = 0; bytes < priv->base.rom_size; /* empty */) { + for (bytes = 0; bytes < rom_size; /* empty */) { const ssize_t got = pread(fd, buffer, rom_size - bytes, rom_base + bytes); if (got == -1) { diff --git a/src/linux_sysfs.c b/src/linux_sysfs.c index 84cdb84..873dc02 100644 --- a/src/linux_sysfs.c +++ b/src/linux_sysfs.c @@ -307,6 +307,7 @@ pci_device_linux_sysfs_read_rom( struct pci_device * dev, void * buffer ) int fd; struct stat st; int err = 0; + size_t rom_size; size_t total_bytes; @@ -331,6 +332,9 @@ pci_device_linux_sysfs_read_rom( struct pci_device * dev, void * buffer ) return errno; } + rom_size = st.st_size; + if ( rom_size == 0 ) + rom_size = 0x10000; /* This is a quirky thing on Linux. Even though the ROM and the file * for the ROM in sysfs are read-only, the string "1" must be written to @@ -340,9 +344,9 @@ pci_device_linux_sysfs_read_rom( struct pci_device * dev, void * buffer ) write( fd, "1", 1 ); lseek( fd, 0, SEEK_SET ); - for ( total_bytes = 0 ; total_bytes < st.st_size ; /* empty */ ) { + for ( total_bytes = 0 ; total_bytes < rom_size ; /* empty */ ) { const int bytes = read( fd, (char *) buffer + total_bytes, - st.st_size - total_bytes ); + rom_size - total_bytes ); if ( bytes == -1 ) { err = errno; break; -- 1.5.3.2