Esempio n. 1
0
        internal SBL3(string FileName)
        {
            Binary = null;

            // First try to parse as FFU
            try
            {
                if (FFU.IsFFU(FileName))
                {
                    FFU FFUFile = new(FileName);
                    Binary = FFUFile.GetPartition("SBL3");
                }
            }
            catch { }

            // If not succeeded, then try to parse it as raw image
            if (Binary == null)
            {
                byte[] SBL3Pattern = new byte[] { 0x18, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x8F, 0xFF, 0xFF, 0xFF, 0xFF };
                byte[] SBL3Mask    = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF };

                UInt32?Offset = ByteOperations.FindPatternInFile(FileName, SBL3Pattern, SBL3Mask, out byte[] SBL3Header);

                if (Offset != null)
                {
                    UInt32 Length = ByteOperations.ReadUInt32(SBL3Header, 0x10) + 0x28; // SBL3 Image Size + Header Size
                    Binary = new byte[Length];

                    FileStream Stream = new(FileName, FileMode.Open, FileAccess.Read);
                    Stream.Seek((long)Offset, SeekOrigin.Begin);
                    Stream.Read(Binary, 0, (int)Length);
                    Stream.Close();
                }
            }
        }
Esempio n. 2
0
 internal void AddFfuToRepository(string FFUPath)
 {
     try
     {
         FFU NewFFU = new FFU(FFUPath);
         AddFfuToRepository(FFUPath, NewFFU.PlatformID, NewFFU.GetFirmwareVersion(), NewFFU.GetOSVersion());
     }
     catch (Exception Ex)
     {
         LogFile.LogException(Ex, LogType.FileAndConsole);
     }
 }
        private void SetProfileFFUPath()
        {
            FFU ProfileFFU;

            try
            {
                if (_ProfileFFUPath != null)
                {
                    ProfileFFU = new FFU(_ProfileFFUPath);
                    if (PlatformID.StartsWith(ProfileFFU.PlatformID, StringComparison.OrdinalIgnoreCase))
                    {
                        IsProfileFfuValid = true;
                        return;
                    }
                }
            }
            catch { }

            try
            {
                string TempProfileFFUPath = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\WPInternals", "ProfileFFUPath", null);
                if (TempProfileFFUPath != null)
                {
                    ProfileFFU = new FFU(TempProfileFFUPath);
                    if (PlatformID.StartsWith(ProfileFFU.PlatformID, StringComparison.OrdinalIgnoreCase))
                    {
                        ProfileFFUPath    = TempProfileFFUPath;
                        IsProfileFfuValid = true;
                        return;
                    }
                }
            }
            catch { }

            List <FFUEntry> FFUs = App.Config.FFURepository.Where(e => (PlatformID.StartsWith(e.PlatformID, StringComparison.OrdinalIgnoreCase) && e.Exists())).ToList();

            if (FFUs.Count() > 0)
            {
                IsProfileFfuValid = true;
                ProfileFFUPath    = FFUs[0].Path;
            }
            else
            {
                IsProfileFfuValid = false;
            }
        }
Esempio n. 4
0
        internal static async Task RewriteMBRGPT()
        {
            FFU    FFU     = new FFU(@"E:\Device Backups\Alpha\9200_1230.0025.9200.9825\RX100_9825.ffu");
            string GPTPath = @"E:\Device Backups\Alpha\9200_1230.0025.9200.9825\CorrectGPT.bin";

            PhoneNotifierViewModel Notifier = new PhoneNotifierViewModel();

            Notifier.Start();
            await SwitchModeViewModel.SwitchTo(Notifier, PhoneInterfaces.Lumia_MassStorage);

            MassStorage MassStorage = (MassStorage)Notifier.CurrentModel;

            byte[] MBR = FFU.GetSectors(0, 1);
            LogFile.Log("Writing MBR to the device.", LogType.ConsoleOnly);
            MassStorage.WriteSectors(0, MBR);

            LogFile.Log("Writing GPT to the device.", LogType.ConsoleOnly);
            MassStorage.WriteSectors(1, GPTPath);
        }
Esempio n. 5
0
        internal void DoDumpRom(string FFUPath, string EFIESPPath, bool CompressEFIESP, string MainOSPath, bool CompressMainOS, string DataPath, bool CompressData)
        {
            new Thread(() =>
            {
                bool Result = true;

                ActivateSubContext(new BusyViewModel("Initializing ROM dump..."));

                ulong TotalSizeSectors = 0;
                int PartitionCount     = 0;
                Partition Partition;
                FFU FFU = null;
                try
                {
                    FFU = new FFU(FFUPath);

                    if (EFIESPPath != null)
                    {
                        Partition         = FFU.GPT.Partitions.First(p => p.Name == "EFIESP");
                        TotalSizeSectors += Partition.SizeInSectors;
                        PartitionCount++;
                    }

                    if (MainOSPath != null)
                    {
                        Partition         = FFU.GPT.Partitions.First(p => p.Name == "MainOS");
                        TotalSizeSectors += Partition.SizeInSectors;
                        PartitionCount++;
                    }

                    if (DataPath != null)
                    {
                        Partition         = FFU.GPT.Partitions.First(p => p.Name == "Data");
                        TotalSizeSectors += Partition.SizeInSectors;
                        PartitionCount++;
                    }
                }
                catch (Exception Ex)
                {
                    LogFile.LogException(Ex);
                    Result = false;
                }

                // We are on a worker thread!
                // So we must pass the SynchronizationContext of the UI thread
                BusyViewModel Busy      = new("Dumping ROM...", MaxProgressValue: TotalSizeSectors, UIContext: UIContext);
                ProgressUpdater Updater = Busy.ProgressUpdater;
                ActivateSubContext(Busy);

                int i = 0;
                if (Result)
                {
                    try
                    {
                        if (EFIESPPath != null)
                        {
                            i++;
                            Busy.Message = "Dumping partition EFIESP (" + i.ToString() + "/" + PartitionCount.ToString() + ")";
                            FFU.WritePartition("EFIESP", EFIESPPath, Updater, CompressEFIESP);
                        }
                    }
                    catch (Exception Ex)
                    {
                        LogFile.LogException(Ex);
                        Result = false;
                    }
                }

                if (Result)
                {
                    try
                    {
                        if (MainOSPath != null)
                        {
                            i++;
                            Busy.Message = "Dumping partition MainOS (" + i.ToString() + "/" + PartitionCount.ToString() + ")";
                            FFU.WritePartition("MainOS", MainOSPath, Updater, CompressMainOS);
                        }
                    }
                    catch (Exception Ex)
                    {
                        LogFile.LogException(Ex);
                        Result = false;
                    }
                }

                if (Result)
                {
                    try
                    {
                        if (DataPath != null)
                        {
                            i++;
                            Busy.Message = "Dumping partition Data (" + i.ToString() + "/" + PartitionCount.ToString() + ")";
                            FFU.WritePartition("Data", DataPath, Updater, CompressData);
                        }
                    }
                    catch (Exception Ex)
                    {
                        LogFile.LogException(Ex);
                        Result = false;
                    }
                }

                if (!Result)
                {
                    ActivateSubContext(new MessageViewModel("Failed to dump ROM partitions!", Restart));
                    return;
                }

                ActivateSubContext(new MessageViewModel("Successfully dumped ROM partitions!", Restart));
            }).Start();
        }
        internal void FlashFFUTask(string FFUPath)
        {
            new Thread(() =>
            {
                bool Result = true;

                NokiaFlashModel Phone = (NokiaFlashModel)PhoneNotifier.CurrentModel;
                PhoneInfo Info        = Phone.ReadPhoneInfo(false);

                #region Remove bootloader changes

                // If necessary remove bootloader changes
                // In case the NV vars were redirected, and a stock FFU is flashed, then the IsFlashing flag will be cleared in the redirected NV vars
                // And after a reboot the original NV vars are active again, but the IsFlashing flag is still set from when the bootloader was unlocked
                // So we will first restore the GPT, so the original vars are active again.
                // Then IsFlashing is true and the phone boots forcibly to FlashApp again.
                // Then we start normal FFU flasing and at the end the IsFlashing flag is cleared in the original vars.

                if (Info.FlashAppProtocolVersionMajor >= 2)
                {
                    byte[] GPTChunk = LumiaUnlockBootloaderViewModel.GetGptChunk(Phone, 0x20000); // TODO: Get proper profile FFU and get ChunkSizeInBytes
                    GPT GPT         = new GPT(GPTChunk);
                    FlashPart Part;
                    List <FlashPart> FlashParts = new List <FlashPart>();

                    Partition NvBackupPartition = GPT.GetPartition("BACKUP_BS_NV");
                    if (NvBackupPartition != null)
                    {
                        // This must be a left over of a half unlocked bootloader
                        Partition NvPartition               = GPT.GetPartition("UEFI_BS_NV");
                        NvBackupPartition.Name              = "UEFI_BS_NV";
                        NvBackupPartition.PartitionGuid     = NvPartition.PartitionGuid;
                        NvBackupPartition.PartitionTypeGuid = NvPartition.PartitionTypeGuid;
                        GPT.Partitions.Remove(NvPartition);

                        GPT.Rebuild();
                        Part             = new FlashPart();
                        Part.StartSector = 0;
                        Part.Stream      = new MemoryStream(GPTChunk);
                        FlashParts.Add(Part);
                    }

                    bool ClearFlashingStatus = true;

                    // We should only clear NV if there was no backup NV to be restored and the current NV contains the SB unlock.
                    if ((NvBackupPartition == null) && !Info.UefiSecureBootEnabled)
                    {
                        // ClearNV
                        Part             = new FlashPart();
                        Partition Target = GPT.GetPartition("UEFI_BS_NV");
                        Part.StartSector = (UInt32)Target.FirstSector;
                        Part.Stream      = new MemoryStream(new byte[0x40000]);
                        FlashParts.Add(Part);

                        ClearFlashingStatus = false;
                    }

                    if (FlashParts.Count > 0)
                    {
                        ActivateSubContext(new BusyViewModel("Restoring bootloader..."));
                        WPinternalsStatus LastStatus = WPinternalsStatus.Undefined;
                        LumiaV2UnlockBootViewModel.LumiaV2CustomFlash(PhoneNotifier, FFUPath, false, false, FlashParts, true, ClearFlashingStatusAtEnd: ClearFlashingStatus,
                                                                      SetWorkingStatus: (m, s, v, a, st) =>
                        {
                            if ((st == WPinternalsStatus.Scanning) || (st == WPinternalsStatus.WaitingForManualReset))
                            {
                                SetWorkingStatus(m, s, v, a, st);
                            }
                            else if ((LastStatus == WPinternalsStatus.Scanning) || (LastStatus == WPinternalsStatus.WaitingForManualReset))
                            {
                                SetWorkingStatus("Restoring bootloader...", null, null, Status: WPinternalsStatus.Flashing);
                            }
                            LastStatus = st;
                        },
                                                                      UpdateWorkingStatus: (m, s, v, st) =>
                        {
                            if ((st == WPinternalsStatus.Scanning) || (st == WPinternalsStatus.WaitingForManualReset))
                            {
                                UpdateWorkingStatus(m, s, v, st);
                            }
                            else if ((LastStatus == WPinternalsStatus.Scanning) || (LastStatus == WPinternalsStatus.WaitingForManualReset))
                            {
                                SetWorkingStatus("Restoring bootloader...", null, null, Status: WPinternalsStatus.Flashing);
                            }
                            LastStatus = st;
                        }
                                                                      ).Wait();

                        if ((PhoneNotifier.CurrentInterface != PhoneInterfaces.Lumia_Bootloader) && (PhoneNotifier.CurrentInterface != PhoneInterfaces.Lumia_Flash))
                        {
                            PhoneNotifier.WaitForArrival().Wait();
                        }

                        if (PhoneNotifier.CurrentInterface == PhoneInterfaces.Lumia_Bootloader)
                        {
                            ((NokiaFlashModel)PhoneNotifier.CurrentModel).SwitchToFlashAppContext();
                        }
                    }
                }

                #endregion

                Phone = (NokiaFlashModel)PhoneNotifier.CurrentModel;

                ActivateSubContext(new BusyViewModel("Initializing flash..."));

                string ErrorSubMessage = null;

                try
                {
                    FFU FFU            = new FFU(FFUPath);
                    BusyViewModel Busy = new BusyViewModel("Flashing original FFU...", MaxProgressValue: FFU.TotalChunkCount, UIContext: UIContext);
                    ActivateSubContext(Busy);
                    byte Options = 0;
                    if (!Info.IsBootloaderSecure)
                    {
                        Options = (byte)((FlashOptions)Options | FlashOptions.SkipSignatureCheck);
                    }
                    Phone.FlashFFU(FFU, Busy.ProgressUpdater, true, Options);
                }
                catch (Exception Ex)
                {
                    LogFile.LogException(Ex);
                    if (Ex is WPinternalsException)
                    {
                        ErrorSubMessage = ((WPinternalsException)Ex).SubMessage;
                    }
                    Result = false;
                }


                if (!Result)
                {
                    ExitFailure("Flash failed!", ErrorSubMessage);
                    return;
                }

                ExitSuccess("Flash successful!", null);
            }).Start();
        }
        private void ValidateSupportedFfuPath()
        {
            try
            {
                if (IsSupportedFfuNeeded)
                {
                    if (SupportedFFUPath == null)
                    {
                        IsSupportedFfuValid = true; // No visible warning when there is no SupportedFFU selected yet.
                    }
                    else
                    {
                        if (!TargetHasNewFlashProtocol)
                        {
                            if (App.Config.FFURepository.Any(e => ((e.Path == SupportedFFUPath) && (App.PatchEngine.PatchDefinitions.Where(p => p.Name == "SecureBootHack-V1.1-EFIESP").First().TargetVersions.Any(v => v.Description == e.OSVersion)))))
                            {
                                IsSupportedFfuValid = true;
                            }
                            else
                            {
                                FFU SupportedFFU = new FFU(SupportedFFUPath);
                                if (App.PatchEngine.PatchDefinitions.Where(p => p.Name == "SecureBootHack-V1.1-EFIESP").First().TargetVersions.Any(v => v.Description == SupportedFFU.GetOSVersion()))
                                {
                                    IsSupportedFfuValid = true;
                                }
                                else
                                {
                                    IsSupportedFfuValid = false;
                                }
                            }
                        }
                        else
                        {
                            if (App.Config.FFURepository.Any(e => ((e.Path == SupportedFFUPath) && (App.PatchEngine.PatchDefinitions.Where(p => p.Name == "SecureBootHack-V2-EFIESP").First().TargetVersions.Any(v => v.Description == e.OSVersion)))))
                            {
                                IsSupportedFfuValid = true;
                            }
                            else
                            {
                                FFU SupportedFFU = new FFU(SupportedFFUPath);
                                if (App.PatchEngine.PatchDefinitions.Where(p => p.Name == "SecureBootHack-V2-EFIESP").First().TargetVersions.Any(v => v.Description == SupportedFFU.GetOSVersion()))
                                {
                                    IsSupportedFfuValid = true;
                                }
                                else
                                {
                                    IsSupportedFfuValid = false;
                                }
                            }
                        }
                    }
                }
                else
                {
                    IsSupportedFfuValid = true;
                }

                if (IsSupportedFfuValid && (SupportedFFUPath != null))
                {
                    ValidatedSupportedFfuPath = SupportedFFUPath;
                }
            }
            catch
            {
                IsSupportedFfuValid = false;
            }
        }
        // Potentially blocking UI. Threadsafe.
        internal override void EvaluateViewState()
        {
            if (!IsActive)
            {
                return;
            }

            if (State == MachineState.LumiaUnlockBoot)
            {
                return;
            }

            lock (EvaluateViewStateLockObject)
            {
                switch (PhoneNotifier.CurrentInterface)
                {
                case PhoneInterfaces.Lumia_Normal:
                case PhoneInterfaces.Lumia_Label:
                    IsSwitchingInterface = false;
                    if (DoUnlock)
                    {
                        // Display View to switch to Flash mode
                        LogFile.Log("Start unlock. Phone needs to switch to Flash-mode");
                        ActivateSubContext(new MessageViewModel("In order to start unlocking the bootloader, the phone needs to be switched to Flash-mode.", SwitchToFlashMode, Exit));
                    }
                    else
                    {
                        // Display View to switch to Flash mode
                        LogFile.Log("Start boot restore. Phone needs to switch to Flash-mode");
                        ActivateSubContext(new MessageViewModel("In order to start restoring the bootloader, the phone needs to be switched to Flash-mode.", SwitchToFlashMode, Exit));
                    }
                    break;

                case PhoneInterfaces.Lumia_Flash:
                    // Display View with device info and request for resources
                    // Click on "Continue" will start processing all resources
                    // Processing may fail with error message
                    // Or processing will succeed and user will again be asked to continue with Bricking-procedure (to switch to emergency mode)

                    // This code is not always invoked by OnArrival event.
                    // So this is not always in a thread from the threadpool.
                    // So we need to avoid UI blocking code here.

                    IsSwitchingInterface = false;

                    int TestPos = 0;

                    try     // In case phone reboots during the time that status is being read
                    {
                        // Some phones, like Lumia 928 verizon, do not support the Terminal interface!
                        // To read the RootKeyHash we use ReadParam("RRKH"), instead of GetTerminalResponse().RootKeyHash.
                        RootKeyHash = ((NokiaFlashModel)PhoneNotifier.CurrentModel).ReadParam("RRKH");

                        TestPos = 1;

                        UefiSecurityStatusResponse SecurityStatus = ((NokiaFlashModel)PhoneNotifier.CurrentModel).ReadSecurityStatus();
                        if (SecurityStatus != null)
                        {
                            IsBootLoaderUnlocked = (SecurityStatus.AuthenticationStatus || SecurityStatus.RdcStatus || !SecurityStatus.SecureFfuEfuseStatus);
                        }

                        TestPos = 2;

                        PhoneInfo Info = ((NokiaFlashModel)PhoneNotifier.CurrentModel).ReadPhoneInfo();
                        if (SecurityStatus == null)
                        {
                            IsBootLoaderUnlocked = !Info.IsBootloaderSecure;
                        }

                        if (RootKeyHash == null)
                        {
                            RootKeyHash = Info.RKH;

                            if (RootKeyHash == null)
                            {
                                RootKeyHash = new byte[32];
                            }
                        }

                        TestPos = 3;

                        if (Info.FlashAppProtocolVersionMajor < 2)
                        {
                            // This action is executed after the resources are selected by the user.
                            Action <string, string, string, string, string, string, bool> ReturnFunction = (FFUPath, LoadersPath, SBL3Path, ProfileFFUPath, EDEPath, SupportedFFUPath, DoFixBoot) =>
                            {
                                // Stop responding to device arrival here, because all connections are handled by subfunctions, not here.
                                IsSwitchingInterface = true;
                                State = MachineState.LumiaUnlockBoot;

                                // This is a callback on the UI thread
                                // Resources are confirmed by user
                                this.FFUPath          = FFUPath;
                                this.LoadersPath      = LoadersPath;
                                this.SBL3Path         = SBL3Path;
                                this.SupportedFFUPath = SupportedFFUPath;
                                StorePaths();

                                LogFile.Log("Processing resources:");
                                LogFile.Log("FFU: " + FFUPath);
                                LogFile.Log("Loaders: " + LoadersPath);
                                if (SBL3Path == null)
                                {
                                    LogFile.Log("No SBL3 specified");
                                }
                                else
                                {
                                    LogFile.Log("SBL3: " + SBL3Path);
                                }

                                ActivateSubContext(new BusyViewModel("Processing resources..."));

                                if (DoUnlock)
                                {
                                    Task.Run(async() =>
                                    {
                                        await LumiaUnlockBootloaderViewModel.LumiaV1UnlockFirmware(PhoneNotifier, FFUPath, LoadersPath, SBL3Path, SupportedFFUPath, SetWorkingStatus, UpdateWorkingStatus, ExitMessage, ExitMessage);
                                    });
                                }
                                else
                                {
                                    Task.Run(async() =>
                                    {
                                        await LumiaUnlockBootloaderViewModel.LumiaV1RelockFirmware(PhoneNotifier, FFUPath, LoadersPath, SetWorkingStatus, UpdateWorkingStatus, ExitMessage, ExitMessage);
                                    });
                                }
                            };

                            if (DoUnlock)
                            {
                                ActivateSubContext(new BootUnlockResourcesViewModel("Lumia Flash mode", RootKeyHash, SwitchToFlashRom, SwitchToUndoRoot, SwitchToDownload, ReturnFunction, Abort, IsBootLoaderUnlocked, false));
                            }
                            else
                            {
                                ActivateSubContext(new BootRestoreResourcesViewModel("Lumia Flash mode", RootKeyHash, SwitchToFlashRom, SwitchToUndoRoot, SwitchToDownload, ReturnFunction, Abort, IsBootLoaderUnlocked, false));
                            }
                        }
                        else
                        {
                            bool AlreadyUnlocked = false;
                            if (DoUnlock)
                            {
                                NokiaFlashModel FlashModel = (NokiaFlashModel)PhoneNotifier.CurrentModel;
                                GPT             GPT        = FlashModel.ReadGPT();
                                if ((GPT.GetPartition("IS_UNLOCKED") != null) || (GPT.GetPartition("BACKUP_EFIESP") != null))
                                {
                                    ExitMessage("Phone is already unlocked", null);
                                    return;
                                    //AlreadyUnlocked = true;
                                }
                            }

                            TestPos = 4;

                            // Stop responding to device arrival here, because all connections are handled by subfunctions, not here.
                            IsSwitchingInterface = true;

                            // This action is executed after the resources are selected by the user.
                            Action <string, string, string, string, string, string, bool> ReturnFunction = (FFUPath, LoadersPath, SBL3Path, ProfileFFUPath, EDEPath, SupportedFFUPath, DoFixBoot) =>
                            {
                                IsSwitchingInterface = true;
                                State = MachineState.LumiaUnlockBoot;
                                if (DoUnlock)
                                {
                                    // This is a callback on the UI thread
                                    // Resources are confirmed by user
                                    this.ProfileFFUPath   = ProfileFFUPath;
                                    this.EDEPath          = EDEPath;
                                    this.SupportedFFUPath = SupportedFFUPath;
                                    StorePaths();

                                    if (DoFixBoot)
                                    {
                                        LogFile.Log("Fix Boot");
                                    }
                                    else
                                    {
                                        LogFile.Log("Unlock Bootloader");
                                    }

                                    LogFile.Log("Processing resources:");
                                    LogFile.Log("Profile FFU: " + ProfileFFUPath);
                                    LogFile.Log("EDE file: " + EDEPath);
                                    if (SupportedFFUPath != null)
                                    {
                                        LogFile.Log("Donor-FFU with supported OS version: " + SupportedFFUPath);
                                    }

                                    Task.Run(async() =>
                                    {
                                        if (DoFixBoot)
                                        {
                                            await LumiaV2UnlockBootViewModel.LumiaV2FixBoot(PhoneNotifier, SetWorkingStatus, UpdateWorkingStatus, ExitMessage, ExitMessage);
                                        }
                                        else if (!AlreadyUnlocked)
                                        {
                                            await LumiaUnlockBootloaderViewModel.LumiaV2UnlockUEFI(PhoneNotifier, ProfileFFUPath, EDEPath, SupportedFFUPath, SetWorkingStatus, UpdateWorkingStatus, ExitMessage, ExitMessage);
                                        }
                                        else
                                        {
                                            await LumiaUnlockBootloaderViewModel.LumiaV2UnlockUEFI(PhoneNotifier, ProfileFFUPath, EDEPath, SupportedFFUPath, SetWorkingStatus, UpdateWorkingStatus, ExitMessage, ExitMessage, true);
                                        }
                                    });
                                }
                                else
                                {
                                    Task.Run(async() =>
                                    {
                                        FFU ProfileFFU = null;

                                        List <FFUEntry> FFUs = App.Config.FFURepository.Where(e => (Info.PlatformID.StartsWith(e.PlatformID, StringComparison.OrdinalIgnoreCase) && e.Exists())).ToList();
                                        if (FFUs.Count() > 0)
                                        {
                                            ProfileFFU = new FFU(FFUs[0].Path);
                                        }
                                        else
                                        {
                                            throw new WPinternalsException("Profile FFU missing", "No profile FFU has been found in the repository for your device. You can add a profile FFU within the download section of the tool or by using the command line.");
                                        }

                                        LogFile.Log("Profile FFU: " + ProfileFFU.Path);

                                        await LumiaUnlockBootloaderViewModel.LumiaV2RelockUEFI(PhoneNotifier, ProfileFFU.Path, true, SetWorkingStatus, UpdateWorkingStatus, ExitMessage, ExitMessage);
                                    });
                                }
                            };

                            TestPos = 5;

                            if (DoUnlock)
                            {
                                ActivateSubContext(new BootUnlockResourcesViewModel("Lumia Flash mode", RootKeyHash, SwitchToFlashRom, SwitchToUndoRoot, SwitchToDownload, ReturnFunction, Abort, IsBootLoaderUnlocked, true, Info.PlatformID, Info.Type));
                            }
                            else
                            {
                                ActivateSubContext(new BootRestoreResourcesViewModel("Lumia Flash mode", RootKeyHash, SwitchToFlashRom, SwitchToUndoRoot, SwitchToDownload, ReturnFunction, Abort, IsBootLoaderUnlocked, true, Info.PlatformID, Info.Type));
                            }
                        }
                    }
                    catch (Exception Ex)
                    {
                        LogFile.LogException(Ex, LogType.FileAndConsole, TestPos.ToString());
                    }
                    break;

                case PhoneInterfaces.Qualcomm_Download:
                    IsSwitchingInterface = false;

                    // If resources are not confirmed yet, then display view with device info and request for resources.
                    QualcommDownload Download = new QualcommDownload((QualcommSerial)PhoneNotifier.CurrentModel);
                    byte[]           QualcommRootKeyHash;

                    try
                    {
                        QualcommRootKeyHash = Download.GetRKH();
                    }
                    catch (BadConnectionException)
                    {
                        // This is a Spec B device
                        break;
                    }

                    if (RootKeyHash == null)
                    {
                        RootKeyHash = QualcommRootKeyHash;
                    }
                    else if (!StructuralComparisons.StructuralEqualityComparer.Equals(RootKeyHash, QualcommRootKeyHash))
                    {
                        LogFile.Log("Error: Root Key Hash in Qualcomm Emergency mode does not match!");
                        ActivateSubContext(new MessageViewModel("Error: Root Key Hash in Qualcomm Emergency mode does not match!", Callback));
                        return;
                    }

                    // This action is executed after the user selected the resources.
                    Action <string, string, string, string, string, string, bool> ReturnFunctionD = (FFUPath, LoadersPath, SBL3Path, ProfileFFUPath, EDEPath, SupportedFFUPath, DoFixBoot) =>
                    {
                        IsSwitchingInterface = true;
                        State = MachineState.LumiaUnlockBoot;
                        // This is a callback on the UI thread
                        // Resources are confirmed by user
                        this.FFUPath     = FFUPath;
                        this.LoadersPath = LoadersPath;
                        this.SBL3Path    = SBL3Path;
                        StorePaths();

                        LogFile.Log("Processing resources:");
                        LogFile.Log("FFU: " + FFUPath);
                        LogFile.Log("Loaders: " + LoadersPath);
                        if (SBL3Path == null)
                        {
                            LogFile.Log("No SBL3 specified");
                        }
                        else
                        {
                            LogFile.Log("SBL3: " + SBL3Path);
                        }

                        ActivateSubContext(new BusyViewModel("Processing resources..."));

                        if (DoUnlock)
                        {
                            Task.Run(async() =>
                            {
                                await LumiaUnlockBootloaderViewModel.LumiaV1UnlockFirmware(PhoneNotifier, FFUPath, LoadersPath, SBL3Path, SupportedFFUPath, SetWorkingStatus, UpdateWorkingStatus, ExitMessage, ExitMessage);
                            });
                        }
                        else
                        {
                            Task.Run(async() =>
                            {
                                await LumiaUnlockBootloaderViewModel.LumiaV1RelockFirmware(PhoneNotifier, FFUPath, LoadersPath, SetWorkingStatus, UpdateWorkingStatus, ExitMessage, ExitMessage);
                            });
                        }
                    };

                    if (DoUnlock)
                    {
                        ActivateSubContext(new BootUnlockResourcesViewModel("Qualcomm Emergency Download mode", RootKeyHash, SwitchToFlashRom, SwitchToUndoRoot, SwitchToDownload, ReturnFunctionD, Abort, IsBootLoaderUnlocked, false));
                    }
                    else
                    {
                        ActivateSubContext(new BootRestoreResourcesViewModel("Qualcomm Emergency Download mode", RootKeyHash, SwitchToFlashRom, SwitchToUndoRoot, SwitchToDownload, ReturnFunctionD, Abort, IsBootLoaderUnlocked, false));
                    }

                    break;

                case PhoneInterfaces.Qualcomm_Flash:
                {
                    IsSwitchingInterface = true;
                    State = MachineState.LumiaUnlockBoot;
                    ActivateSubContext(new BusyViewModel("Recovering resources..."));

                    LogFile.Log("Phone was unexpectedly detected in this mode while resources were not loaded yet.");
                    LogFile.Log("WPInternals tool probably crashed in previous session.");
                    LogFile.Log("Trying to recover resources from the registry.");

                    FFUPath          = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\WPInternals", "FFUPath", null);
                    SupportedFFUPath = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\WPInternals", "SupportedFFUPath", null);
                    LoadersPath      = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\WPInternals", "LoadersPath", null);
                    if (DoUnlock)
                    {
                        SBL3Path = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\WPInternals", "SBL3Path", null);
                    }
                    else
                    {
                        SBL3Path = null;
                    }

                    if (DoUnlock)
                    {
                        Task.Run(async() =>
                            {
                                await LumiaUnlockBootloaderViewModel.LumiaV1UnlockFirmware(PhoneNotifier, FFUPath, LoadersPath, SBL3Path, SupportedFFUPath, SetWorkingStatus, UpdateWorkingStatus, ExitMessage, ExitMessage);
                            });
                    }
                    else
                    {
                        Task.Run(async() =>
                            {
                                await LumiaUnlockBootloaderViewModel.LumiaV1RelockFirmware(PhoneNotifier, FFUPath, LoadersPath, SetWorkingStatus, UpdateWorkingStatus, ExitMessage, ExitMessage);
                            });
                    }
                    break;
                }

                default:
                    // Show View "Waiting for connection"
                    IsSwitchingInterface = false;
                    ActivateSubContext(null);
                    break;
                }
            }
        }
        private void SetSupportedFFUPath()
        {
            if (!TargetHasNewFlashProtocol)
            {
                if (this is BootRestoreResourcesViewModel)
                {
                    IsSupportedFfuNeeded = false;
                    return;
                }

                try
                {
                    FFU FFU = new FFU(_FFUPath);
                    IsSupportedFfuNeeded = !(App.PatchEngine.PatchDefinitions.Where(p => p.Name == "SecureBootHack-V1.1-EFIESP").First().TargetVersions.Any(v => v.Description == FFU.GetOSVersion()));
                }
                catch
                {
                    IsSupportedFfuNeeded = false;
                    return;
                }

                if (IsSupportedFfuNeeded)
                {
                    FFU SupportedFFU;

                    try
                    {
                        if (_SupportedFFUPath != null)
                        {
                            SupportedFFU = new FFU(_SupportedFFUPath);
                            if (App.PatchEngine.PatchDefinitions.Where(p => p.Name == "SecureBootHack-V1.1-EFIESP").First().TargetVersions.Any(v => v.Description == SupportedFFU.GetOSVersion()))
                            {
                                return;
                            }
                        }
                    }
                    catch { }

                    try
                    {
                        string TempSupportedFFUPath = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\WPInternals", "SupportedFFUPath", null);
                        if (TempSupportedFFUPath != null)
                        {
                            SupportedFFU = new FFU(TempSupportedFFUPath);
                            if (App.PatchEngine.PatchDefinitions.Where(p => p.Name == "SecureBootHack-V1.1-EFIESP").First().TargetVersions.Any(v => v.Description == SupportedFFU.GetOSVersion()))
                            {
                                ValidatedSupportedFfuPath = TempSupportedFFUPath;
                                SupportedFFUPath          = TempSupportedFFUPath;
                                IsSupportedFfuValid       = true;
                                return;
                            }
                        }
                    }
                    catch { }

                    List <FFUEntry> FFUs = App.Config.FFURepository.Where(e => App.PatchEngine.PatchDefinitions.Where(p => p.Name == "SecureBootHack-V1.1-EFIESP").First().TargetVersions.Any(v => v.Description == e.OSVersion)).ToList();
                    if (FFUs.Count() > 0)
                    {
                        ValidatedSupportedFfuPath = FFUs[0].Path;
                        SupportedFFUPath          = FFUs[0].Path;
                        IsSupportedFfuValid       = true;
                    }
                }
            }
            else
            {
                if ((this is BootRestoreResourcesViewModel) || (_ProfileFFUPath == null))
                {
                    IsSupportedFfuNeeded = false;
                    return;
                }

                try
                {
                    FFU ProfileFFU = new FFU(_ProfileFFUPath);
                    IsSupportedFfuNeeded = !(App.PatchEngine.PatchDefinitions.Where(p => p.Name == "SecureBootHack-V2-EFIESP").First().TargetVersions.Any(v => v.Description == ProfileFFU.GetOSVersion()));
                }
                catch
                {
                    IsSupportedFfuNeeded = false;
                    return;
                }

                if (IsSupportedFfuNeeded)
                {
                    FFU SupportedFFU;

                    try
                    {
                        if (_SupportedFFUPath != null)
                        {
                            SupportedFFU = new FFU(_SupportedFFUPath);
                            if (App.PatchEngine.PatchDefinitions.Where(p => p.Name == "SecureBootHack-V2-EFIESP").First().TargetVersions.Any(v => v.Description == SupportedFFU.GetOSVersion()))
                            {
                                return;
                            }
                        }
                    }
                    catch { }

                    try
                    {
                        string TempSupportedFFUPath = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\WPInternals", "SupportedFFUPath", null);
                        if (TempSupportedFFUPath != null)
                        {
                            SupportedFFU = new FFU(TempSupportedFFUPath);
                            if (App.PatchEngine.PatchDefinitions.Where(p => p.Name == "SecureBootHack-V2-EFIESP").First().TargetVersions.Any(v => v.Description == SupportedFFU.GetOSVersion()))
                            {
                                ValidatedSupportedFfuPath = TempSupportedFFUPath;
                                SupportedFFUPath          = TempSupportedFFUPath;
                                IsSupportedFfuValid       = true;
                                return;
                            }
                        }
                    }
                    catch { }

                    List <FFUEntry> FFUs = App.Config.FFURepository.Where(e => App.PatchEngine.PatchDefinitions.Where(p => p.Name == "SecureBootHack-V2-EFIESP").First().TargetVersions.Any(v => v.Description == e.OSVersion)).ToList();
                    if (FFUs.Count() > 0)
                    {
                        ValidatedSupportedFfuPath = FFUs[0].Path;
                        SupportedFFUPath          = FFUs[0].Path;
                        IsSupportedFfuValid       = true;
                    }
                }
            }
        }