Ejemplo n.º 1
0
 def __init__(self, dbdf, debug_level="critical"):
     super(LinuxPCIDevice, self).__init__(dbdf, debug_level)
     self._pci_conf_file = "{0}/{1}/config".format(LinuxPCIDevice.PCI_CONF_FILE_BASE_PATH, self.dbdf)
     self._bin_file = BinaryFile(self._pci_conf_file)
     if self._is_valid_device() == False:
         self.logger.debug("Device is not supported to save/load pci configurations")
         raise NotSupportedDeviceException("Device is not supported to save/load pci configurations")
     self._pci_express_offset = self._get_pci_express_offset()
Ejemplo n.º 2
0
    def read(self, offset, size, skip_offset_list=None):
        super(FreeBSDPCIDevice, self).read(offset, size, skip_offset_list)
        try:
            if skip_offset_list:
                read_intervals = BinaryFile._get_read_intervals(skip_offset_list, offset, size)
            else:
                read_intervals = [(offset, size)]
            bytes_as_string = []
            for interval_start, interval_size in read_intervals:
                if interval_size:
                    address_start = self.align_to(number=interval_start, base=4, round="down")
                    address_end = self.align_to(number=(interval_start + interval_size), base=4, round="up") - 1
                    read_cmd = "pciconf -r {0} {1}:{2}".format(self.dbdf, address_start, address_end)
                    rc, out, err = exec_cmd(read_cmd)
                    if rc:
                        raise RuntimeError("Failed to read using cmd {0}, Error[{1}]".format(read_cmd, err))
                    pci_hex_data_list = out.replace("\n", " ").split(" ") # Remove new lines + split on spaces
                    for hex_str in pci_hex_data_list:
                        if hex_str != '':  # little endian 
                            bytes_as_string += [hex_str[6:8] , hex_str[4:6] , hex_str[2:4] , hex_str[0:2]]
                else:
                    bytes_as_string.append("00")
        except Exception as e:
            raise RuntimeError("Failed to read offset [{0}] with size [{1}] for PCI device [{2}]. Error is [{3}]".format(offset, size, self.dbdf, e))

        bytes_list = []
        start = offset % 4
        end = start + size
        bytes_as_string = bytes_as_string[start:end]
        for byte in bytes_as_string:
            if byte != "":
                bytes_list.append(int(byte, 16))
            else:
                bytes_list.append(None)
        return bytes_list
Ejemplo n.º 3
0
 def get_texture(path):
     binary = BinaryFile(path)
     compliant_textures = (texture_class for texture_class in TextureBuilder.get_texture_types()
                           if texture_class.has_signature(binary))
     for texture_class in compliant_textures:
         return texture_class(path)
         break
     else:
         return None
Ejemplo n.º 4
0
class LinuxPCIDevice(PCIDeviceBase):
    """
    PCI device for Linux
    """
    PCI_CONF_FILE_BASE_PATH = "/sys/bus/pci/devices"
    def __init__(self, dbdf, debug_level="critical"):
        super(LinuxPCIDevice, self).__init__(dbdf, debug_level)
        self._pci_conf_file = "{0}/{1}/config".format(LinuxPCIDevice.PCI_CONF_FILE_BASE_PATH, self.dbdf)
        self._bin_file = BinaryFile(self._pci_conf_file)
        self._pci_express_offset = self._get_pci_express_offset()

    def read(self, offset, size, skip_offset_list=None):
        super(LinuxPCIDevice, self).read(offset, size, skip_offset_list)
        try:
            return self._bin_file.read(offset=offset, size=size, skip_offset_list=skip_offset_list)
        except Exception as e:
            raise RuntimeError("Failed to read offset [{0}] with size [{1}] for PCI device [{2}]. Error is [{3}]".format(offset, size, self.dbdf, e))
    
    def read_byte(self, offset):
        return self._bin_file.read_byte(offset)
    
    def read_word(self, offset):
        assert offset % 2 == 0, "Offset should be aligned to 2"
        return self._bin_file.read_word(offset)

    def read_long(self, offset):
        assert offset % 4 == 0, "Offset should be aligned to 4"
        return self._bin_file.read_long(offset)

    def write(self, offset, size, bytes_list):
        super(LinuxPCIDevice, self).write(offset, size, bytes_list)
        self._bin_file.write(bytes_list=bytes_list, offset=offset, size=size)
        self.logger.debug("Data was Written [{0}] to  offset[{1}] for PCI device [{2}]".format(bytes_list, offset, self.dbdf))    
Ejemplo n.º 5
0
    def read(self, offset, size, skip_offset_list=None):
        super(FreeBSDPCIDevice, self).read(offset, size, skip_offset_list)
        try:
            if skip_offset_list:
                read_intervals = BinaryFile._get_read_intervals(
                    skip_offset_list, offset, size)
            else:
                read_intervals = [(offset, offset + size)]

            bytes_as_string = []
            for interval_start, interval_size in read_intervals:
                if interval_size:
                    pciconf_aligned_offset = (
                        (interval_start / 4) * 4
                    )  # pciconf reads in aligment of 4 bytes
                    pciconf_size_offset = offset % 4
                    interval_size = interval_size + pciconf_size_offset  # in case you want to read from adress not devidable by 4
                    read_cmd = "pciconf -r {0} {1}:{2}".format(
                        self.dbdf, pciconf_aligned_offset, interval_size)
                    rc, out, err = exec_cmd(read_cmd)
                    if rc:
                        raise RuntimeError(
                            "Failed to read using cmd {0}, Error[{1}]".format(
                                read_cmd, err))
                    pci_hex_data_list = out.replace("\n", " ").split(
                        " ")  # Remove new lines + split on spaces
                    for hex_str in pci_hex_data_list:
                        if hex_str != '':  # little endian
                            bytes_as_string += [
                                hex_str[6:8], hex_str[4:6], hex_str[2:4],
                                hex_str[0:2]
                            ]
                else:
                    bytes_as_string.append("00")
        except Exception as e:
            raise RuntimeError(
                "Failed to read offset [{0}] with size [{1}] for PCI device [{2}]. Error is [{3}]"
                .format(offset, size, self.dbdf, e))

        bytes_list = []
        actual_offset = offset - pciconf_aligned_offset
        bytes_as_string = bytes_as_string[actual_offset:actual_offset + size]
        for byte in bytes_as_string:
            if byte != "":
                bytes_list.append(int(byte, 16))
            else:
                bytes_list.append(None)
        return bytes_list
Ejemplo n.º 6
0
 def __init__(self, dbdf, debug_level="critical"):
     super(LinuxPCIDevice, self).__init__(dbdf, debug_level)
     self._pci_conf_file = "{0}/{1}/config".format(LinuxPCIDevice.PCI_CONF_FILE_BASE_PATH, self.dbdf)
     self._bin_file = BinaryFile(self._pci_conf_file)
     self._pci_express_offset = self._get_pci_express_offset()