def getVmxTimerRate(self, msrpaths, offset, vmxbitsize): # check for MSR vmxTimerRate = 0 MSRfound = False for path in msrpaths: try: # Try to find MSR file vmxTimerRate = self.getVmxFromMSR(path, offset, vmxbitsize) except IOError: pass else: MSRfound = True break if not MSRfound: # MSRFileNotFound errormsg = "MSR could not be located at directories:\n" for path in msrpaths: errormsg += ("%s\n" % path) errormsg += ("vmxTimerRate could not be found. Try 'modprobe msr' to " "probe for MSR, then run the tool again.") message.addError(errormsg) return vmxTimerRate
def getIoports(self, devicepath): ioports = [] try: resourceData = extractor.extractData( os.path.join(devicepath, "resource")) except IOError: message.addError("Could not obtain ioport information for device: " "%s" % os.path.basename( devicepath), False) else: ioportcount = 0 for line in resourceData.splitlines(): tokens = line.split(' ') if tokens[2][-3] == '1': # if line represents ioport information ioPort = Element("ioPort", "ioPortType") ioPort["name"] = "ioport%d" % ioportcount ioPort["start"] = util.toWord64(tokens[0]) ioPort["end"] = util.toWord64(tokens[1]) ioportcount += 1 ioports.append(ioPort) return ioports
def getIommuAGAW(self, iommuaddr, devmem, capoffset, capbytesize, agawbitstart): "Gets the AGAW name from a given iommuaddr, at the capability offset" AGAW_39_BITNO = 1 AGAW_48_BITNO = 2 AGAW_39_NAME = "39" AGAW_48_NAME = "48" name = "agaw" try: startaddr = int(iommuaddr, 16) + int(capoffset, 16) capreg = extractor.extractBinaryData(devmem, startaddr, capbytesize) except IOError: message.addError("Could not access file: %s" % devmem, False) else: agaw = (int(capreg, 16) >> agawbitstart) & 0x1F # See 5 bits if util.getBit(agaw, AGAW_39_BITNO): name = AGAW_39_NAME elif util.getBit(agaw, AGAW_48_BITNO): name = AGAW_48_NAME else: message.addError("AGAW Capability could not be found for IOMMU " "device at: %s" % iommuaddr, False) return name
def getSerialAddresses(self, ioportspath): "Gets serial addresses in form (startaddr, endaddr)" serialAddresses = [] KEYWORD = "serial" # Get all lines which include KEYWORD try: ioportdata = extractor.extractData(ioportspath) except IOError: message.addError("Could not access location: %s\n" % ioportspath + "Serial device addresses not found.", False) else: try: lines = parseutil.findLines(ioportdata, KEYWORD) except customExceptions.KeyNotFound: message.addMessage( "No serial devices found from file: %s" % ioportspath) else: # Retrieve (start,end) data for serial devices for line in lines: serialAddresses.append(self.getAddressFromLine(line)) print "Serial devices found: %d\n------------------" % len(serialAddresses) for addr in serialAddresses: print " Start: ", "0x" + addr.start, " End: ", "0x" + addr.end return serialAddresses
def getMemoryBlocks(self, path): memoryBlockList = [] def walkError(excep): message.addError("Could not access memory block data: " + str(excep), False) memdirs = [] for root, subdirs, files in os.walk(path, onerror=walkError): if not subdirs: # at end of paths memdirs.append(root) memdirs.sort(key=lambda x: int(os.path.basename(x))) # Sort paths found by numerical order for root in memdirs: endfile = root + "/" + "end" typefile = root + "/" + "type" startfile = root + "/" + "start" try: memoryBlock = self.generateMemoryBlock(endfile, typefile, startfile) except IOError: message.addError("Could not retrieve complete memory data", False) else: # Adds newly created memoryBlock element to memoryBlockList memoryBlockList.append(memoryBlock) # Filter out memoryBlocks that do not meet requirements memoryBlockList = self.filterMemoryBlocks(memoryBlockList) return memoryBlockList
def getDeviceMemoryBlocks(self, devicepath, minsize=PAGE_MIN_SIZE): devmemblocks = [] try: resourceData = extractor.extractData( os.path.join(devicepath, "resource")) except IOError: message.addError("Could not obtain memory information for device: " "%s" % os.path.basename( devicepath), False) else: memcount = 0 for line in resourceData.splitlines(): tokens = line.split(' ') if tokens[2][-3] == '2': # if line represents a memory block memory = Element("memory", "deviceMemoryType") memory["name"] = "mem%d" % memcount memory["physicalAddress"] = util.toWord64(tokens[0]) # Rounds memsize up to minsize memsize = util.sizeOf(tokens[1], tokens[0]) if int(memsize, 16) < int(minsize, 16): memrounded = util.hexFloor(memsize, minsize) print ("Mem size %s for device %s rounded to: %s" % (memsize, os.path.basename(devicepath), memrounded)) memsize = memrounded memory["size"] = util.toWord64(memsize) memory["caching"] = "UC" # TODO devmemblocks.append(memory) memcount += 1 return devmemblocks
def getDeviceShortNames(self, devicepaths, pciids): shortnames = OrderedDict() namecount = {} try: # Initialise PciIdsParser, throws customExceptions.PciIdsFileNotFound # if fail pciIdsParser = parseutil.PciIdsParser(pciids) except customExceptions.PciIdsFileNotFound: message.addError("pci.ids file could not be located in tool " "directory: %s. " % paths.CURRENTDIR + "Device " "names could not be obtained. Please ensure that " "the file is in the directory.", False) else: for devicepath in devicepaths: # Add entry to dictionary shortnames shortnames[devicepath] = self.getClassName( devicepath, pciIdsParser) namelist = [] for value in shortnames.itervalues(): namelist.append(value) listnumberer = util.ListNumberer(namelist) for devicepath in shortnames.iterkeys(): shortnames[devicepath] = listnumberer.getName( shortnames[devicepath]) return shortnames
def _genDMAR_copyDMAR(self, src, dest): success = True # Check if DMAR exists try: open(src, "r") except IOError: # DMARFileNotFound message.addMessage("No DMAR file found at: '%s'; " % src + "No IOMMU devices found.") success = False else: # Copy DMAR to temp folder try: shutil.copyfile(src, dest) except IOError: message.addMessage("DMAR table at: '%s' " % src + "could not be copied to location: '%s'" % dest) # DmarFileNotCopied message.addError("Could not obtain DMAR information; IOMMU device " "information not found.", False) success = False if success: self.copiedDMAR = dest return success
def getIrq(self, devicepath): irq = None try: irqNo = extractor.extractData(os.path.join(devicepath, "irq")) except IOError: message.addError("Could not obtain irq number for device: %s" % os.path.basename(devicepath), False) else: if irqNo is not "0": irq = Element("irq", "irqType") irq["name", "number"] = "irq", irqNo return irq
def init_DevicecapManager(self, devicepaths): "Initialises the DeviceCapManager" print "Getting device capabilities..." devicecapmgr = devicecap.DevicecapManager() try: devicecapmgr.extractCapabilities(devicepaths) except customExceptions.DeviceCapabilitiesNotRead: message.addError("Not enough permissions to access capabilities " "of devices. It is advised to run the tool again " "with the proper permissions.", False) return devicecapmgr
def getSpeed(self, dmesgpath): "Gets speed value from dmesg" PARSE_KEYWORD = "Refined TSC clocksource calibration" result = 0 try: data = extractor.extractData(dmesgpath) speedstr = parseutil.parseData_Sep(data, PARSE_KEYWORD, ":") result = util.getSpeedValue(speedstr, PROCESSOR_SPEED_KEYWORDS) except IOError: errstr = ("Could not read file: %s\n" % dmesgpath + "Processor speed not found.") message.addError(errstr) except customExceptions.KeyNotFound: errstr = ("Could not read find refined TSC clocksource calibration " "results in: %s\n" % dmesgpath + "Processor speed not found.") message.addError(errstr) else: return result
def parseDMAR(self, dmarfilepath=None): "Parses dmarfile with iasl" success = True if dmarfilepath is None: dmarfilepath = self.getCopiedDmarPath() dmarfilepath_noext, ext = os.path.splitext(dmarfilepath) outfile = dmarfilepath_noext + ".dsl" try: self._runIasl(dmarfilepath) except OSError: # IaslToolNotFound message.addMessage("iasl tool not found in the system. " + "Try 'apt-get install iasl' to install.") message.addError("Could not obtain DMAR information; IOMMU device " "information not found.", False) success = False else: self.parsedDMAR = outfile return success
def getIommuAddrs(self, parsedDMAR=None): "Retrieves Register Base Addresses of IOMMUs from parsed DMAR" iommuaddrs = [] KEY = "Register Base Address" if parsedDMAR is None: parsedDMAR = self.getParsedDmarPath() try: dmardata = extractor.extractData(parsedDMAR) except IOError: message.addError("Could not find parsed DMAR file in location: %s." % parsedDMAR, False) else: for line in dmardata.splitlines(): try: addr = parseLine_Sep(line, KEY, ":") addr = addr.lstrip("0") addr = "0x" + addr addr = addr.lower() iommuaddrs.append(addr) except customExceptions.KeyNotFound: pass return iommuaddrs
def walkError(excep): message.addError("Could not access memory block data: " + str(excep), False)