示例#1
0
        /// <summary>
        /// Uses Jedi Firmware Utility to validate we have a well contructed firmware file that is valid for a specified device
        /// </summary>
        /// <param name="assetId"></param>
        /// <returns></returns>
        public PluginExecutionResult ValidateFirmwareBundles(string assetId, string password, string address)
        {
            //if (!_activityData.ValidateFWBundles)
            //{
            //    return new PluginExecutionResult(PluginResult.Passed);
            //}

            string[] separator = { Environment.NewLine };
            UpdateStatus("Validating Firmware bundle for the selected asset");
            //extract the file;
            var tempDumpDirectory   = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "Dump"));
            var dumpUtilityFileName = Path.Combine(tempDumpDirectory.FullName, "FimDumpUtility.exe");

            File.WriteAllBytes(dumpUtilityFileName, ResourceDump.FimDumpUtility);

            if (!Directory.Exists(_activityData.FimBundlesLocation))
            {
                return(new PluginExecutionResult(PluginResult.Failed, "Failed to find FW bundle directory"));
            }

            string[] files = Directory.GetFiles(_activityData.FimBundlesLocation, "*.bdl");

            if (files.Length == 0)
            {
                return(new PluginExecutionResult(PluginResult.Failed, "Unable to find .bdl files in the directory"));;
            }


            string firmwareFile = _activityData.AssetMapping[assetId].FirmwareFile;

            FirmwareData fwData = new FirmwareData();

            string fwFileName = Path.Combine(tempDumpDirectory.FullName, Path.GetFileName(firmwareFile));

            File.Copy(firmwareFile, fwFileName, true);



            var result      = ProcessUtil.Execute(dumpUtilityFileName, $"-o {tempDumpDirectory.FullName} \"{fwFileName}\"");
            var outputLines = result.StandardOutput.Split(separator, StringSplitOptions.None);

            var revision = outputLines.FirstOrDefault(x => x.Contains("Version"));

            if (string.IsNullOrEmpty(revision))
            {
                return(new PluginExecutionResult(PluginResult.Failed, "Failed to find FW bundle revision"));;
            }
            revision = revision.Substring(revision.IndexOf(':') + 1).Trim();
            fwData.FirmwareRevision = revision.Split(' ').First();

            var version = outputLines.FirstOrDefault(x => x.Contains("Description"))?.Trim();

            if (string.IsNullOrEmpty(version))
            {
                return(new PluginExecutionResult(PluginResult.Failed, "Failed to find FW bundle version"));
            }
            version = version.Substring(version.IndexOf(':') + 1);
            fwData.FWBundleVersion = version;

            var dateCode = revision.Substring(revision.IndexOf('(') + 1, revision.LastIndexOf(')') - (revision.IndexOf('(') + 1));

            fwData.FirmwareDateCode = dateCode;

            var name = outputLines.FirstOrDefault(x => x.Contains("Name"));

            if (string.IsNullOrEmpty(name))
            {
                return(new PluginExecutionResult(PluginResult.Failed, "Failed to find FW bundle name"));
            }
            name = name.Substring(name.IndexOf(':') + 1).Trim();
            fwData.FWModelName = name;
            var pFamily = outputLines.FirstOrDefault(x => x.Contains("Identifier"));

            pFamily = pFamily.Substring(pFamily.IndexOf(':') + 1).Trim();
            fwData.ProductFamily = pFamily;

            if (_activityData.ValidateFWBundles)
            {
                if (!_activityData.FWBundleInfo.Any(
                        x => x.ProductFamily == fwData.ProductFamily &&
                        x.FWModelName == fwData.FWModelName &&
                        x.FWBundleVersion == fwData.FWBundleVersion &&
                        x.FirmwareRevision == fwData.FirmwareRevision &&
                        x.FirmwareDateCode == fwData.FirmwareDateCode))
                {
                    return(new PluginExecutionResult(PluginResult.Failed, "Failed to find FW bundle match"));
                }
            }
            else
            {
                JediDevice dev  = new JediDevice(address, password);
                var        temp = dev.GetDeviceInfo();

                if (fwData.FirmwareRevision == temp.FirmwareRevision)
                {
                    return(new PluginExecutionResult(PluginResult.Skipped, "Firmware on device is the same as the bundle"));
                }
            }

            ///Check to see that the FW is still tied to a device.
            if (_activityData.AssetMapping.Where(x => x.Value.ProductFamily == pFamily).Count() == 0)
            {
                return(new PluginExecutionResult(PluginResult.Failed, "Unable to match a device to a FW bundle"));
            }

            _activityData.AssetMapping.Where(x => x.Value.ProductFamily == pFamily).First().Value.FirmwareFile = fwFileName;

            UpdateStatus($"Validated firmware for device");
            return(new PluginExecutionResult(PluginResult.Passed));
        }
        private void ReadFirmwareDump(string[] bundleFiles)
        {
            string[] separator = { Environment.NewLine };
            //extract the file;
            var tempDumpDirectory   = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "Dump"));
            var dumpUtilityFileName = Path.Combine(tempDumpDirectory.FullName, "FimDumpUtility.exe");

            File.WriteAllBytes(dumpUtilityFileName, ResourceDump.FimDumpUtility);

            //Get .bdl files from directory
            //Extract data
            //populate datagridview
            //refresh

            _data.FWBundleInfo.Clear();

            AssetIdCollection assetIds = assetSelectionControl.AssetSelectionData.SelectedAssets;

            AssetInfoCollection assets = Framework.ConfigurationServices.AssetInventory.GetAssets(assetIds);
            var col = assets.OfType <PrintDeviceInfo>();

            Dictionary <string, ModelFileMap> nameModel = new Dictionary <string, ModelFileMap>();
            string endpoint = "fim";
            string urn      = "urn:hp:imaging:con:service:fim:FIMService";

            foreach (var printer in col)
            {
                SetDefaultPassword(printer.Address, printer.AdminPassword);
                ///Get way of finding the product family

                if (!nameModel.ContainsKey(printer.AssetId))
                {
                    ModelFileMap map = new ModelFileMap();

                    JediDevice device = new JediDevice(printer.Address, printer.AdminPassword);

                    WebServiceTicket tic = device.WebServices.GetDeviceTicket(endpoint, urn);
                    var ident            = tic.FindElements("AssetIdentifier").First().Value;



                    map.ProductFamily = ident;// "6D6670-0055";// Bugatti"696D66-0015";
                    nameModel.Add(printer.AssetId, map);
                }
            }

            //_data.AssetMapping = nameModel;


            foreach (string firmwareFile in bundleFiles)
            {
                FirmwareData fwData = new FirmwareData();

                FileInfo fInfo      = new FileInfo(firmwareFile);
                var      fileSize   = fInfo.Length / (1024 * 1024);
                var      fileSizeMb = (int)((fileSize / 50.0) * 6);
                fwData.FlashTimeOutPeriod = (int)TimeSpan.FromMinutes(fileSizeMb).TotalMilliseconds;

                var result = ProcessUtil.Execute(dumpUtilityFileName,
                                                 $"-o {tempDumpDirectory.FullName} \"{firmwareFile}\"");

                var outputLines = result.StandardOutput.Split(separator, StringSplitOptions.None);

                var revision = outputLines.FirstOrDefault(x => x.Contains("Version"));
                if (string.IsNullOrEmpty(revision))
                {
                    MessageBox.Show(
                        $@"An error occurred while reading firmware revision information. Please check the firmware file {firmwareFile} and try again. Read Aborted",
                        @"Firmware File Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    return;
                }
                revision = revision.Substring(revision.IndexOf(':') + 1).Trim();
                fwData.FirmwareRevision = revision.Split(' ').First();

                var version = outputLines.FirstOrDefault(x => x.Contains("Description"))?.Trim();
                if (string.IsNullOrEmpty(version))
                {
                    MessageBox.Show(
                        @"An error occurred while reading firmware version information. Please check the firmware file again and try",
                        @"Firmware File Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    return;
                }
                version = version.Substring(version.IndexOf(':') + 1);
                fwData.FWBundleVersion = version;

                var dateCode = revision.Substring(revision.IndexOf('(') + 1, revision.LastIndexOf(')') - (revision.IndexOf('(') + 1));
                fwData.FirmwareDateCode = dateCode;

                var name = outputLines.FirstOrDefault(x => x.Contains("Name"));
                if (string.IsNullOrEmpty(name))
                {
                    MessageBox.Show(
                        @"An error occurred while reading firmware Name information. Please check the firmware file again and try",
                        @"Firmware File Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    return;
                }
                name = name.Substring(name.IndexOf(':') + 1).Trim();
                fwData.FWModelName = name;


                var pfamily = outputLines.FirstOrDefault(x => x.Contains("Identifier"));
                pfamily = pfamily.Substring(pfamily.IndexOf(':') + 1).Trim();
                fwData.ProductFamily = pfamily;

                if (nameModel.Where(x => x.Value.ProductFamily == pfamily).Count() == 0)
                {
                    MessageBox.Show(
                        $@"Failed to match the firmware bundle to an existing device. Please check the firmware files and selected assets and try again. Model: {name}",
                        @"Firmware File Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    return;
                }
                else
                {
                    var devs = nameModel.Where(x => x.Value.ProductFamily == pfamily).Select(x => x.Key);
                    foreach (var dev in devs)
                    {
                        nameModel[dev].FirmwareFile = firmwareFile;
                    }
                }


                _data.FWBundleInfo.Add(fwData);
            }


            if (nameModel.Where(x => x.Value.FirmwareFile == string.Empty).Count() > 0)
            {
                string devices = nameModel.Where(x => x.Value.FirmwareFile == string.Empty).Select(x => x.Key).Aggregate((current, next) => current + ", " + next);
                MessageBox.Show(
                    $@"Failed to match the following devices with firmware: {devices}. Please check the firmware files and selected assets and try again.",
                    @"Firmware File Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                return;
            }
            _data.AssetMapping = nameModel;
        }