/// <summary> /// check if the driver we are about to install has already been installed /// if it has and is firmware then rollback if is not then just uninstall /// </summary> /// <param name="infNameToTest"></param> /// <param name="expectedDriverVersion"></param> /// <returns></returns> internal static bool CheckInstalled(string line, string hardwareID, string friendlyDriverName, string infNameToTest, string expectedDriverVersion, string expectedDriverDate) { Logger.FunctionEnter(); string installedDriversCSVPath = Program.dirName + @"\installledDrivers.csv"; string csvFileName = "DeviceName-InfName.czv"; string csvFileFullPath = Program.dirName + csvFileName; string infName = string.Empty; string infNameFromReg = string.Empty; string installedDriverVersion = string.Empty; string installedDeviceName = string.Empty; bool result = false; string classGUID = GetData.GetClassGUID(line); if (string.IsNullOrEmpty(hardwareID)) { //do nothing for now hardwareID = "0x00000000"; Console.WriteLine("not getting a hardwareID...??"); Logger.Comment("no hardwareID in the INF so we can't use it to check if driver is installed"); } else { infNameFromReg = GetDataFromReg.GetOEMinfNameFromRegSTR(infName, hardwareID, classGUID); } try { using (ManagementObjectSearcher s2 = new ManagementObjectSearcher( "root\\CIMV2", "SELECT * FROM Win32_PnPSignedDriver")) foreach (ManagementObject WmiObject in s2.Get()) { if (WmiObject["InfName"] == null) { continue; } else { infName = WmiObject["InfName"].ToString(); } if (WmiObject["DriverVersion"] == null) { continue; } else { installedDriverVersion = WmiObject["DriverVersion"].ToString(); } if (WmiObject["DeviceName"] == null) { continue; } else { installedDeviceName = WmiObject["DeviceName"].ToString(); } // there are zeros in driver versions in inf files that are not in the devMgr // and viceVersa will have to remove\replace all zeros with "" if (expectedDriverVersion.EndsWith("0")) { expectedDriverVersion = expectedDriverVersion.TrimEnd('0'); expectedDriverVersion = expectedDriverVersion.TrimEnd('.'); expectedDriverVersion = expectedDriverVersion.TrimEnd('0'); } if (installedDriverVersion.EndsWith("0")) { installedDriverVersion = installedDriverVersion.TrimEnd('0'); installedDriverVersion = installedDriverVersion.TrimEnd('.'); installedDriverVersion = installedDriverVersion.TrimEnd('0'); } if (installedDeviceName.Contains(" ")) { installedDeviceName = installedDeviceName.Replace(" ", ""); } if (infName.Contains(" ")) { infName = infName.Replace(" ", ""); } if (infNameToTest.Contains(" ")) { infNameToTest = infNameToTest.Replace(" ", ""); } if (friendlyDriverName.Contains(" ")) { friendlyDriverName = friendlyDriverName.Replace(" ", ""); } infNameToTest = infNameToTest.Split('.')[0]; if (infNameToTest.Equals("SurfaceEC")) { infNameToTest = "SurfaceEmbeddedControllerFirmware"; } string lineContains = string.Empty; switch (lineContains) { case string infNameFromR when infName.Equals(infNameFromReg): if (expectedDriverVersion.Equals(installedDriverVersion)) { Logger.Comment(infNameToTest); result = true; Logger.Comment("result from checkInstalled TRUE infName/version : "); } break; case string installedDeviceNam when friendlyDriverName.Equals(installedDeviceName): if (expectedDriverVersion.Equals(installedDriverVersion)) { Logger.Comment(infNameToTest); result = true; Logger.Comment("result from checkInstalled TRUE infName/version : "); } break; case string failedResult when Regex.Match(infName, friendlyDriverName).Success: if (expectedDriverVersion.Equals(installedDriverVersion)) { Logger.Comment(infNameToTest); result = true; Logger.Comment("result from checkInstalled TRUE infName/version : "); } break; case string successinstallResult when infName.Equals(infNameFromReg): if (expectedDriverVersion.Equals(installedDriverVersion)) { Logger.Comment(infNameToTest); result = true; Logger.Comment("result from checkInstalled TRUE infName/version : "); } break; case string last when installedDeviceName.Contains(infNameToTest): if (expectedDriverVersion.Equals(installedDriverVersion)) { Logger.Comment(infNameToTest); result = true; Logger.Comment("result from checkInstalled TRUE infName/version : "); } break; default: continue; } } } catch (Exception ex) { GetData.GetExceptionMessage(ex); } Logger.Comment("result actually being returned from checkInstalled : " + result); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("result actually being returned from checkInstalled : " + result); Console.ForegroundColor = ConsoleColor.White; Logger.FunctionLeave(); return(result); }
/// <summary> /// firmware or regular driver will still call this to do the actual installation /// </summary> /// <param name="infName"></param> /// <param name="line"></param> /// <param name="installer"></param> /// <param name="installArgs"></param> /// <param name="InputTestFilePath"></param> /// <param name="rebootRequired"></param> /// <param name="hardwareID"></param> /// SafeNativeMethods.InstallUninstallCall(seedIndex, rebootRequired, infName, line, installer, InputTestFilePath); internal static void InstallUninstallCall(int seedIndex, bool rebootRequired, string infName, string line, string installer, string InputTestFilePath) { try { Logger.FunctionEnter(); string expectedDriverVersion = GetData.GetDriverVersion(line); string infNameToTest = Path.GetFileNameWithoutExtension(line); string expectedDriverDate = GetData.GetDriverDate(line); string infPath = Path.GetDirectoryName(line); string installArgs; string friendlyDriverName = XMLReader.GetFriendlyDriverName(InputTestFilePath, line); Console.WriteLine(line + " and " + seedIndex); Console.WriteLine("RemoveXMLElement now"); XMLWriter.RemoveXMLElemnt(Program.InputTestFilePath, line, seedIndex); Console.WriteLine("check the xml please.."); string hardwareID = GetData.FirmwareInstallGetHID(line); bool isInstalled = CheckWhatInstalled.CheckInstalled(line, hardwareID, friendlyDriverName, infNameToTest, expectedDriverVersion, expectedDriverDate); if (isInstalled) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("THIS PnP DRIVER VERSION IS INSTALLED UN-INSTALLING NOW...."); Console.WriteLine(infName); Console.WriteLine("----------------------------------------------------------"); Console.ForegroundColor = ConsoleColor.White; Logger.Comment("rebootRequired is showing as : " + rebootRequired); installArgs = " /C /U " + line + " /Q /D"; Install_Inf(line, installer, installArgs, seedIndex); Logger.Comment("Operation should be complete: " + line); string classGUID = GetData.GetClassGUID(line); GetDataFromReg.GetOEMinfNameFromReg(infName, hardwareID, classGUID); if (RegCheck.IsRebootPending()) { Logger.Comment("there is a pending reboot..."); RebootAndContinue.RebootCmd(true); } } else { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("---------------------------------------------------------------"); Console.WriteLine("THIS PnP DRIVER VERSION WAS NOT INSTALLED YET INSTALLING NOW..."); Console.WriteLine(infName); Console.WriteLine("---------------------------------------------------------------"); Console.ForegroundColor = ConsoleColor.White; Logger.Comment("rebootRequired is showing as : " + rebootRequired); installArgs = " /C /A /Q /SE /F /PATH " + infPath; Install_Inf(line, installer, installArgs, seedIndex); string classGUID = GetData.GetClassGUID(line); XMLWriter.SetLastInstalled(line); //can we check registry simiarly to firmware for PnP drivers? Logger.Comment("Operation should be complete: " + line); if (RegCheck.IsRebootPending()) { Logger.Comment("there is a pending reboot..."); RebootAndContinue.RebootCmd(true); } } Logger.FunctionLeave(); } catch (Exception ex) { GetData.GetExceptionMessage(ex); } }
/// <summary> /// this is called when installing firmware to perform the extra actions needed such as regkey writes and reboot /// </summary> /// <param name="infName"></param> /// <param name="infFileContent"></param> /// <param name="hardwareID"></param> /// <param name="rebootRequired"></param> /// <param name="rollBackLine"></param> /// <param name="installer"></param> /// <param name="installArgs"></param> /// <param name="InputTestFilePath"></param> internal static void RollbackInstall(int seedIndex, string line, string infName, string infFileContent, string hardwareID, bool rebootRequired, string InputTestFilePath) { Logger.FunctionEnter(); string installArgs; string fullRollBackFile = string.Empty; string fullRollBackDIR = string.Empty; string fullRollBackDIRdir = string.Empty; try { XMLWriter.RemoveXMLElemnt(Program.InputTestFilePath, line, seedIndex); RegCheck.CreatePolicyRegKeyAndSetValue(hardwareID, rebootRequired.Equals(true)); fullRollBackDIR = GetData.CheckRollbacksExist(line, infName); if (string.IsNullOrEmpty(fullRollBackDIR)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("should i even be trying to rollback at all???"); Console.ForegroundColor = ConsoleColor.White; } else { fullRollBackDIRdir = Path.GetDirectoryName(fullRollBackDIR); } installArgs = " /C /U " + line + " /Q /D"; string tmpfileName = string.Empty; string tmpVersion = GetData.GetDriverVersion(line); var binsList = Directory.EnumerateFiles(@"C:\Windows\Firmware\"); foreach (string binFileToDelete in binsList) { tmpfileName = Path.GetFileNameWithoutExtension(binFileToDelete); tmpVersion = tmpVersion.TrimEnd('0').TrimEnd('0').TrimEnd('.'); if (Regex.Match(tmpfileName, tmpVersion).Success) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("binFileToDelete exists : " + tmpfileName); File.Delete(tmpfileName); Console.ForegroundColor = ConsoleColor.White; break; } else { continue; } } Install_Inf(line, Program.installer, installArgs, seedIndex); Logger.Comment("Uninstall Operation should be complete: " + line); installArgs = " /C /A /Q /SE /F /PATH " + fullRollBackDIRdir; Logger.Comment("start rollback..."); XMLWriter.SetLastInstalled(fullRollBackDIR); Install_Inf(line, Program.installer, installArgs, seedIndex); string classGUID = GetData.GetClassGUID(line); string expectedDriverVersion = GetData.GetDriverVersion(line); GetDataFromReg.GetOEMinfNameFromReg(infName, hardwareID, classGUID); if (RegCheck.IsRebootPending()) { Logger.Comment("there is a pending reboot..."); RebootAndContinue.RebootCmd(true); } } catch (Exception ex) { GetData.GetExceptionMessage(ex); } Logger.FunctionLeave(); }
/// <summary> /// just another check to be sure driver is installed /// this will run after there is a reboot so is mostly for validation of firmware installs /// GetData.IsInstalledAfterReboot(); /// </summary> internal static string IsInstalledAfterReboot(string line) { string result = "pass"; string returnCode = "pass"; try { // get the file attributes for file or directory FileAttributes attr = File.GetAttributes(line); // detect whether its a directory or file if ((attr & FileAttributes.Directory) == FileAttributes.Directory) { foreach (string tmpPath in Directory.EnumerateFiles(line)) { if (tmpPath.EndsWith(".inf")) { line = tmpPath; } else { continue; } } } string TMPexpectedVersion = GetDriverVersion(line); string TMPinfName = Path.GetFileNameWithoutExtension(line); string TMPhardwareID = FirmwareInstallGetHID(line); string classGUID = GetClassGUID(line); int TMPinfListCount = XMLReader.GetInfsPathListCount(Program.InputTestFilePathBAK); int executionCount = XMLReader.GetExecutionCount(Program.InputTestFilePath); bool TMPisCapsule = CheckDriverIsFirmware(line, executionCount, TMPinfListCount); bool stopOnError = XMLReader.GetStopOnError(Program.InputTestFilePathBAK); if (TMPisCapsule) { string CapsuleDidInstall = GetDataFromReg.CheckRegCapsuleIsInstalled(TMPinfName, TMPhardwareID, TMPexpectedVersion, line); if (stopOnError.Equals(true)) { switch (returnCode) { case string failed when CapsuleDidInstall.Equals("unsuccessful"): returnCode = "unsuccessful registry code is 1 "; XMLErrorMessage(returnCode, line); break; case string InsufficientResources when CapsuleDidInstall.Equals("InsufficientResources"): returnCode = "InsufficientResources registry code is 2 "; XMLErrorMessage(returnCode, line); break; case string IncorrectVersion when CapsuleDidInstall.Equals("IncorrectVersion"): returnCode = "IncorrectVersion registry code is 3 "; XMLErrorMessage(returnCode, line); break; case string invalidImage when CapsuleDidInstall.Equals("invalidImage"): returnCode = "invalidImage registry code is 4 "; XMLErrorMessage(returnCode, line); break; case string authenticationERR when CapsuleDidInstall.Equals("authenticationERR"): returnCode = "authenticationERR registry code is 5 "; XMLErrorMessage(returnCode, line); break; case string ACnotConnected when CapsuleDidInstall.Equals("ACnotConnected"): returnCode = "ACnotConnected registry code is 6 "; XMLErrorMessage(returnCode, line); break; case string insufficientPower when CapsuleDidInstall.Equals("insufficientPower"): returnCode = "insufficientBatteryPower registry code is 7 "; XMLErrorMessage(returnCode, line); break; default: returnCode = "pass"; result = returnCode; Logger.Comment("checked the registry after reboot but this firmware installed correctly " + returnCode + " " + line); Console.WriteLine("checked the registry after reboot but this firmware installed correctly " + returnCode + " " + line); break; } } } return(result); } catch (Exception ex) { GetExceptionMessage(ex); return(result); } }