def main()

in UefiTestingPkg/AuditTests/UefiVarLockAudit/Windows/UefiVarAudit.py [0:0]


def main():

    parser = argparse.ArgumentParser(description='Variable Audit Tool')

    #Output debug log
    parser.add_argument("-l", dest="OutputLog", help="Create an output log file: ie -l out.txt", default=None)
    parser.add_argument("--OutputXml", dest="OutputXml", help="Output Xml file that contains final results", default=None)
    parser.add_argument("--InputXml", dest="InputXml", help="Input Xml file", default=None)
    
    #Turn on dubug level logging
    parser.add_argument("--debug", action="store_true", dest="debug", help="turn on debug logging level for file log",  default=False)
    options = parser.parse_args()

    #setup file based logging if outputReport specified
    if(options.OutputLog):
        if(len(options.OutputLog) < 2):
            logging.critical("the output log file parameter is invalid")
            return -2
        else:
            #setup file based logging
            filelogger = logging.FileHandler(filename=options.OutputLog, mode='w')
            if(options.debug):
                filelogger.setLevel(logging.DEBUG)
            else:
                filelogger.setLevel(logging.INFO)

            filelogger.setFormatter(formatter)
            logging.getLogger('').addHandler(filelogger)

    logging.info("Log Started: " + datetime.datetime.strftime(datetime.datetime.now(), "%A, %B %d, %Y %I:%M%p" ))

    #Check for required input parameters
    if(not options.InputXml) or (not os.path.isfile(options.InputXml)):  
        logging.critical("No Input Xml file specified")
        return -1

    if(not options.OutputXml):
        logging.critical("Output Xml file path not specified")
        return -2
    
    Uefi = UefiVariable()

    #read in XML file as doc    
    XmlFile = ET.parse(options.InputXml)
    XmlRoot = XmlFile.getroot()

    for var in XmlRoot.findall("Variable"):
        name = var.get("Name")
        guid = var.get("Guid")
        (ReadStatus, Data, ReadErrorString) = Uefi.GetUefiVar(name, guid)
        (WriteSuccess, ErrorCode, WriteErrorString)= Uefi.SetUefiVar(name, guid)
        if(WriteSuccess != 0):
            logging.info("Must Restore Var %s:%s" % (name, guid))
            (RestoreSuccess, RestoreEC, RestoreErrorString) = Uefi.SetUefiVar(name, guid, Data)
            if (RestoreSuccess == 0):
                logging.critical("Restoring failed for Var %s:%s  0x%X  ErrorCode: 0x%X %s" % (name, guid, RestoreSuccess, RestoreEC, RestoreErrorString))
        #append
        #<FromOs>
        #<ReadStatus>0x0 Success</ReadStatus>
        #<WriteStatus>0x8000000000000002 Invalid Parameter</WriteStatus>  
        ele = Element("FromOs")
        rs = Element("ReadStatus")
        ws = Element("WriteStatus")
        rs.text = "0x%lX" % (ReadStatus)
        if(ReadErrorString is not None):
            rs.text = rs.text + " %s" % ReadErrorString
        ws.text = "0x%lX" % ErrorCode
        if(WriteErrorString is not None):
            ws.text = ws.text + " %s" % WriteErrorString
        ele.append(rs)
        ele.append(ws)
        var.append(ele)

    XmlFile.write(options.OutputXml)
    return 0