diff --git a/WPinternals/Models/Lumia/UEFI/BootMgr/LumiaBootManagerAppModel.cs b/WPinternals/Models/Lumia/UEFI/BootMgr/LumiaBootManagerAppModel.cs index a3c6949..e2cdfe3 100644 --- a/WPinternals/Models/Lumia/UEFI/BootMgr/LumiaBootManagerAppModel.cs +++ b/WPinternals/Models/Lumia/UEFI/BootMgr/LumiaBootManagerAppModel.cs @@ -401,25 +401,5 @@ internal void SwitchToPhoneInfoAppContextLegacy() RaiseInterfaceChanged(PhoneInterfaces.Lumia_PhoneInfo); } } - - internal void RebootToFlashApp() - { - LumiaBootManagerPhoneInfo info = ReadPhoneInfoBootManager(); - - bool ModernFlashApp = info.BootManagerVersionMajor >= 2; - - byte[] Request = new byte[4]; - ByteOperations.WriteAsciiString(Request, 0, RebootToFlashAppSignature); // This will let the phone charge - ExecuteRawVoidMethod(Request); // On phone with bootloader Spec A this triggers a reboot, so DisableRebootTimeOut() cannot be called immediately. - - if (ModernFlashApp) - { - DisableRebootTimeOut(); - - info.App = FlashAppType.FlashApp; - - RaiseInterfaceChanged(PhoneInterfaces.Lumia_Flash); - } - } } } diff --git a/WPinternals/Models/QualcommLoader.cs b/WPinternals/Models/QualcommLoader.cs index 2a0ac57..00dc7ab 100644 --- a/WPinternals/Models/QualcommLoader.cs +++ b/WPinternals/Models/QualcommLoader.cs @@ -53,7 +53,7 @@ internal static List GetPossibleLoadersForRootKeyHash(string // Make sure the RootKeyHash is not blank // If the RootKeyHash is blank, this is an engineering device, and it will accept any RKH // We expect the user to know what he is doing in such case and we will ignore checks - if (!StructuralComparisons.StructuralEqualityComparer.Equals(RootKeyHash, new byte[RootKeyHash.Length])) + if (RootKeyHash != null && !StructuralComparisons.StructuralEqualityComparer.Equals(RootKeyHash, new byte[RootKeyHash.Length])) { if (StructuralComparisons.StructuralEqualityComparer.Equals(Loader.RootKeyHash, RootKeyHash) && (ByteOperations.FindUnicode(Loader.Binary, "QHSUSB_ARMPRG") != null)) // To detect that this is a loader, and not SBL1 or something. V1 loaders are QHSUSB_ARMPRG. V2 loaders are QHSUSB__BULK. Only V1 supported for now, because V2 only accepts signed payload. diff --git a/WPinternals/ViewModels/LumiaUnlockBootViewModel.cs b/WPinternals/ViewModels/LumiaUnlockBootViewModel.cs index 3bf4a43..7b128a1 100644 --- a/WPinternals/ViewModels/LumiaUnlockBootViewModel.cs +++ b/WPinternals/ViewModels/LumiaUnlockBootViewModel.cs @@ -180,7 +180,6 @@ void ReturnFunction(string FFUPath, string LoadersPath, string SBL3Path, string this.LoadersPath = LoadersPath; this.SBL3Path = SBL3Path; this.SupportedFFUPath = SupportedFFUPath; - StorePaths(); LogFile.Log("Processing resources:"); LogFile.Log("FFU: " + FFUPath); @@ -196,14 +195,19 @@ void ReturnFunction(string FFUPath, string LoadersPath, string SBL3Path, string 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 () => { - Task.Run(async () => await LumiaUnlockBootloaderViewModel.LumiaV1RelockFirmware(PhoneNotifier, FFUPath, LoadersPath, SetWorkingStatus, UpdateWorkingStatus, ExitMessage, ExitMessage)); - } + await StorePaths(); + + if (DoUnlock) + { + await LumiaUnlockBootloaderViewModel.LumiaV1UnlockFirmware(PhoneNotifier, FFUPath, LoadersPath, SBL3Path, SupportedFFUPath, SetWorkingStatus, UpdateWorkingStatus, ExitMessage, ExitMessage); + } + else + { + await LumiaUnlockBootloaderViewModel.LumiaV1RelockFirmware(PhoneNotifier, FFUPath, LoadersPath, SetWorkingStatus, UpdateWorkingStatus, ExitMessage, ExitMessage); + } + }); } if (DoUnlock) @@ -234,7 +238,6 @@ void ReturnFunction(string FFUPath, string LoadersPath, string SBL3Path, string this.ProfileFFUPath = ProfileFFUPath; this.EDEPath = EDEPath; this.SupportedFFUPath = SupportedFFUPath; - StorePaths(); if (DoFixBoot) { @@ -255,6 +258,8 @@ void ReturnFunction(string FFUPath, string LoadersPath, string SBL3Path, string Task.Run(async () => { + await StorePaths(); + bool AlreadyUnlocked = false; LumiaFlashAppModel FlashModel = (LumiaFlashAppModel)PhoneNotifier.CurrentModel; @@ -418,7 +423,6 @@ void ReturnFunction(string FFUPath, string LoadersPath, string SBL3Path, string this.FFUPath = FFUPath; this.LoadersPath = LoadersPath; this.SBL3Path = SBL3Path; - StorePaths(); LogFile.Log("Processing resources:"); LogFile.Log("FFU: " + FFUPath); @@ -434,14 +438,19 @@ void ReturnFunction(string FFUPath, string LoadersPath, string SBL3Path, string ActivateSubContext(new BusyViewModel("Processing resources...")); - if (DoUnlock) + Task.Run(async () => { - 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)); - } + await StorePaths(); + + if (DoUnlock) + { + await LumiaUnlockBootloaderViewModel.LumiaV1UnlockFirmware(PhoneNotifier, FFUPath, LoadersPath, SBL3Path, SupportedFFUPath, SetWorkingStatus, UpdateWorkingStatus, ExitMessage, ExitMessage); + } + else + { + await LumiaUnlockBootloaderViewModel.LumiaV1RelockFirmware(PhoneNotifier, FFUPath, LoadersPath, SetWorkingStatus, UpdateWorkingStatus, ExitMessage, ExitMessage); + } + }); }; if (DoUnlock) @@ -513,15 +522,15 @@ private void Abort() { // SwitchModeViewModel must be created on the UI thread IsSwitchingInterface = false; - UIContext.Post((t) => + UIContext.Post(async (t) => { - StorePaths(); + await StorePaths(); LogFile.Log("Aborting."); Exit(); }, null); } - private async void StorePaths() + private async Task StorePaths() { RegistryKey Key = Registry.CurrentUser.OpenSubKey(@"Software\WPInternals", true) ?? Registry.CurrentUser.CreateSubKey(@"Software\WPInternals"); diff --git a/WPinternals/ViewModels/LumiaUnlockBootloaderViewModel.cs b/WPinternals/ViewModels/LumiaUnlockBootloaderViewModel.cs index 25dedcf..8df17a5 100644 --- a/WPinternals/ViewModels/LumiaUnlockBootloaderViewModel.cs +++ b/WPinternals/ViewModels/LumiaUnlockBootloaderViewModel.cs @@ -30,6 +30,7 @@ using WPinternals.Models.Lumia.UEFI.Flash; using WPinternals.Models.UEFIApps.BootMgr; using WPinternals.Models.UEFIApps.Flash; +using WPinternals.Models.UEFIApps.PhoneInfo; namespace WPinternals { @@ -55,7 +56,17 @@ public static async Task ReadGPTFromFlashOrBootMgr(PhoneNotifierViewModel N if (FlashModel.CanReadGPT()) { - FlashModel.SwitchToBootManagerContext(); + bool ModernFlashApp = FlashModel.ReadPhoneInfoFlashApp().FlashAppProtocolVersionMajor >= 2; + + if (ModernFlashApp) + { + FlashModel.SwitchToFlashAppContext(); + } + else + { + FlashModel.ResetPhone(); + await Notifier.WaitForRemoval(); + } if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Bootloader) { @@ -69,7 +80,14 @@ public static async Task ReadGPTFromFlashOrBootMgr(PhoneNotifierViewModel N GPT GPT = ((LumiaBootManagerAppModel)Notifier.CurrentModel).ReadGPT(); - ((LumiaBootManagerAppModel)Notifier.CurrentModel).SwitchToFlashAppContext(); + if (ModernFlashApp) + { + ((LumiaBootManagerAppModel)Notifier.CurrentModel).SwitchToFlashAppContext(); + } + else + { + ((LumiaBootManagerAppModel)Notifier.CurrentModel).ResetPhoneToFlashMode(); + } if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash) { @@ -129,7 +147,15 @@ public static async Task GetGptChunkFromFlashOrBootMgr(PhoneNotifierView byte[] GPT = ((LumiaBootManagerAppModel)Notifier.CurrentModel).GetGptChunk(Size); - ((LumiaBootManagerAppModel)Notifier.CurrentModel).SwitchToFlashAppContext(); + bool ModernFlashApp = ((LumiaBootManagerAppModel)Notifier.CurrentModel).ReadPhoneInfo().BootManagerProtocolVersionMajor >= 2; + if (ModernFlashApp) + { + ((LumiaBootManagerAppModel)Notifier.CurrentModel).SwitchToFlashAppContext(); + } + else + { + ((LumiaBootManagerAppModel)Notifier.CurrentModel).ResetPhoneToFlashMode(); + } if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash) { @@ -851,6 +877,29 @@ await LumiaRelockUEFI(Notifier, FFUPath, true, SetWorkingStatus, UpdateWorkingSt throw new WPinternalsException("Phone is in an unexpected mode.", "The phone should have been detected in flash mode. Instead it has been detected in " + Notifier.CurrentInterface.ToString() + " mode."); } + if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Bootloader) + { + bool ModernFlashApp = ((LumiaBootManagerAppModel)Notifier.CurrentModel).ReadPhoneInfo().BootManagerProtocolVersionMajor >= 2; + if (ModernFlashApp) + { + ((LumiaBootManagerAppModel)Notifier.CurrentModel).SwitchToFlashAppContext(); + } + else + { + ((LumiaBootManagerAppModel)Notifier.CurrentModel).ResetPhoneToFlashMode(); + } + } + + if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash) + { + await Notifier.WaitForArrival(); + } + + if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash) + { + throw new WPinternalsException("Unexpected Mode"); + } + LumiaFlashAppModel FlashModel = (LumiaFlashAppModel)Notifier.CurrentModel; if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Flash && FlashModel.ReadParam("FS")[3] > 0) { @@ -1446,6 +1495,7 @@ internal static async Task LumiaV1UnlockFirmware(PhoneNotifierViewModel Notifier LogFile.Log("Flash MBR at 0x" + ((UInt32)0).ToString("X8")); Flasher.Flash(0, MBR, Progress, 0, 0x200); + // TIMEOUT LogFile.Log("Flash GPT at 0x" + ((UInt32)0x200).ToString("X8")); Flasher.Flash(0x200, GPT, Progress, 0, 0x41FF); // Bad bounds-check in the flash-loader prohibits to write the last byte. @@ -1760,6 +1810,11 @@ internal static async Task LumiaRelockUEFI(PhoneNotifierViewModel Notifier, stri await SwitchModeViewModel.SwitchToWithStatus(Notifier, PhoneInterfaces.Lumia_Flash, SetWorkingStatus, UpdateWorkingStatus); } + if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Bootloader) + { + ((LumiaBootManagerAppModel)Notifier.CurrentModel).SwitchToFlashAppContext(); + } + if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash) { await Notifier.WaitForArrival(); @@ -2583,7 +2638,29 @@ internal static async Task LumiaUnlockUEFI(PhoneNotifierViewModel Notifier, stri throw new WPinternalsException("Phone is in wrong mode", "The phone should have been detected in bootloader mode. Instead it has been detected in " + Notifier.CurrentInterface.ToString() + " mode."); } } - ((LumiaBootManagerAppModel)Notifier.CurrentModel).SwitchToFlashAppContext(); + + if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Bootloader) + { + bool ModernFlashApp = ((LumiaBootManagerAppModel)Notifier.CurrentModel).ReadPhoneInfo().BootManagerProtocolVersionMajor >= 2; + if (ModernFlashApp) + { + ((LumiaBootManagerAppModel)Notifier.CurrentModel).SwitchToFlashAppContext(); + } + else + { + ((LumiaBootManagerAppModel)Notifier.CurrentModel).ResetPhoneToFlashMode(); + } + } + + if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash) + { + await Notifier.WaitForArrival(); + } + + if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash) + { + throw new WPinternalsException("Unexpected Mode"); + } Parts = LumiaGenerateEFIESPFlashPayload(UnlockedEFIESP, GPT, ProfileFFU, IsSpecB);