romsize is an uint32_t variable. Specifying -1 as an uint32_t value is
obscure way to denote UINT32_MAX.
Worse, if int is wider than 32-bit, it will change the behavior of a
construct like the following:
romsize = -1;
if (romsize != -1) {
    ...
}
When -1 is assigned to romsize, -1 will be implicitly casted into
uint32_t, resulting in UINT32_MAX. On contrary, when evaluating
romsize != -1, romsize will be casted into int, and it will be a
comparison of UINT32_MAX and -1, and result in false.
Replace -1 with UINT32_MAX for statements involving the variable to
clarify the intent and prevent potential breakage.
Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20240627-reuse-v10-10-7ca0b8ed3d9f@daynix.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
		
	
			
		
			
				
	
	
		
			93 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			93 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/*
 | 
						|
 * This is splited from hw/i386/kvm/pci-assign.c
 | 
						|
 */
 | 
						|
#include "qemu/osdep.h"
 | 
						|
#include "qapi/error.h"
 | 
						|
#include "qemu/error-report.h"
 | 
						|
#include "hw/loader.h"
 | 
						|
#include "hw/pci/pci.h"
 | 
						|
#include "xen_pt.h"
 | 
						|
 | 
						|
/*
 | 
						|
 * Scan the assigned devices for the devices that have an option ROM, and then
 | 
						|
 * load the corresponding ROM data to RAM. If an error occurs while loading an
 | 
						|
 * option ROM, we just ignore that option ROM and continue with the next one.
 | 
						|
 */
 | 
						|
void *pci_assign_dev_load_option_rom(PCIDevice *dev,
 | 
						|
                                     int *size, unsigned int domain,
 | 
						|
                                     unsigned int bus, unsigned int slot,
 | 
						|
                                     unsigned int function)
 | 
						|
{
 | 
						|
    char name[32], rom_file[64];
 | 
						|
    FILE *fp;
 | 
						|
    uint8_t val;
 | 
						|
    struct stat st;
 | 
						|
    void *ptr = NULL;
 | 
						|
    Object *owner = OBJECT(dev);
 | 
						|
 | 
						|
    /* If loading ROM from file, pci handles it */
 | 
						|
    if (dev->romfile || !dev->rom_bar) {
 | 
						|
        return NULL;
 | 
						|
    }
 | 
						|
 | 
						|
    snprintf(rom_file, sizeof(rom_file),
 | 
						|
             "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/rom",
 | 
						|
             domain, bus, slot, function);
 | 
						|
 | 
						|
    /* Write "1" to the ROM file to enable it */
 | 
						|
    fp = fopen(rom_file, "r+");
 | 
						|
    if (fp == NULL) {
 | 
						|
        if (errno != ENOENT) {
 | 
						|
            error_report("pci-assign: Cannot open %s: %s", rom_file, strerror(errno));
 | 
						|
        }
 | 
						|
        return NULL;
 | 
						|
    }
 | 
						|
    if (fstat(fileno(fp), &st) == -1) {
 | 
						|
        error_report("pci-assign: Cannot stat %s: %s", rom_file, strerror(errno));
 | 
						|
        goto close_rom;
 | 
						|
    }
 | 
						|
 | 
						|
    val = 1;
 | 
						|
    if (fwrite(&val, 1, 1, fp) != 1) {
 | 
						|
        goto close_rom;
 | 
						|
    }
 | 
						|
    fseek(fp, 0, SEEK_SET);
 | 
						|
 | 
						|
    if (dev->romsize != UINT_MAX) {
 | 
						|
        if (st.st_size > dev->romsize) {
 | 
						|
            error_report("ROM BAR \"%s\" (%ld bytes) is too large for ROM size %u",
 | 
						|
                         rom_file, (long) st.st_size, dev->romsize);
 | 
						|
            goto close_rom;
 | 
						|
        }
 | 
						|
    } else {
 | 
						|
        dev->romsize = st.st_size;
 | 
						|
    }
 | 
						|
 | 
						|
    snprintf(name, sizeof(name), "%s.rom", object_get_typename(owner));
 | 
						|
    memory_region_init_ram(&dev->rom, owner, name, dev->romsize, &error_abort);
 | 
						|
    ptr = memory_region_get_ram_ptr(&dev->rom);
 | 
						|
    memset(ptr, 0xff, dev->romsize);
 | 
						|
 | 
						|
    if (!fread(ptr, 1, st.st_size, fp)) {
 | 
						|
        error_report("pci-assign: Cannot read from host %s", rom_file);
 | 
						|
        error_printf("Device option ROM contents are probably invalid "
 | 
						|
                     "(check dmesg).\nSkip option ROM probe with rombar=0, "
 | 
						|
                     "or load from file with romfile=\n");
 | 
						|
        goto close_rom;
 | 
						|
    }
 | 
						|
 | 
						|
    pci_register_bar(dev, PCI_ROM_SLOT, 0, &dev->rom);
 | 
						|
    dev->has_rom = true;
 | 
						|
    *size = st.st_size;
 | 
						|
close_rom:
 | 
						|
    /* Write "0" to disable ROM */
 | 
						|
    fseek(fp, 0, SEEK_SET);
 | 
						|
    val = 0;
 | 
						|
    if (!fwrite(&val, 1, 1, fp)) {
 | 
						|
        XEN_PT_WARN(dev, "%s\n", "Failed to disable pci-sysfs rom file");
 | 
						|
    }
 | 
						|
    fclose(fp);
 | 
						|
 | 
						|
    return ptr;
 | 
						|
}
 |