From f0f3268924cb3ad327a6f47e865d295b3df56841 Mon Sep 17 00:00:00 2001 From: Gustave Monce Date: Sat, 31 Aug 2024 21:52:20 +0200 Subject: [PATCH] Split phone info structures for each app type --- WPinternals/CommandLine.cs | 105 +++-- WPinternals/Models/NokiaFlashModel.cs | 375 ------------------ WPinternals/Models/PhoneInfo.cs | 134 ------- .../Models/PhoneInfo/CommonPhoneInfo.cs | 42 ++ .../PhoneInfo/LumiaBootManagerPhoneInfo.cs | 59 +++ .../PhoneInfo/LumiaFlashAppPhoneInfo.cs | 96 +++++ .../PhoneInfo/LumiaPhoneInfoAppPhoneInfo.cs | 69 ++++ .../LumiaBootManagerAppModel.cs | 71 +++- .../{ => UEFIApps}/LumiaFlashAppModel.cs | 103 ++++- .../{ => UEFIApps}/LumiaPhoneInfoAppModel.cs | 104 ++++- .../Models/UEFIApps/NokiaFlashModel.cs | 230 +++++++++++ WPinternals/TestCode.cs | 2 +- WPinternals/ViewModels/DownloadsViewModel.cs | 93 +++-- .../ViewModels/LumiaFlashRomViewModel.cs | 2 +- .../ViewModels/LumiaUnlockBootViewModel.cs | 10 +- .../LumiaUnlockBootloaderViewModel.cs | 6 +- .../ViewModels/LumiaV2UnlockBootViewModel.cs | 20 +- .../ViewModels/LumiaV3FlashRomViewModel.cs | 2 +- WPinternals/ViewModels/NokiaFlashViewModel.cs | 6 +- .../NokiaModeBootloaderViewModel.cs | 4 +- .../ViewModels/NokiaModeFlashViewModel.cs | 2 +- .../ViewModels/NokiaModePhoneInfoViewModel.cs | 4 +- .../ViewModels/NokiaPhoneInfoViewModel.cs | 4 +- WPinternals/ViewModels/SwitchModeViewModel.cs | 117 ++++-- 24 files changed, 1001 insertions(+), 659 deletions(-) delete mode 100644 WPinternals/Models/NokiaFlashModel.cs delete mode 100644 WPinternals/Models/PhoneInfo.cs create mode 100644 WPinternals/Models/PhoneInfo/CommonPhoneInfo.cs create mode 100644 WPinternals/Models/PhoneInfo/LumiaBootManagerPhoneInfo.cs create mode 100644 WPinternals/Models/PhoneInfo/LumiaFlashAppPhoneInfo.cs create mode 100644 WPinternals/Models/PhoneInfo/LumiaPhoneInfoAppPhoneInfo.cs rename WPinternals/Models/{ => UEFIApps}/LumiaBootManagerAppModel.cs (79%) rename WPinternals/Models/{ => UEFIApps}/LumiaFlashAppModel.cs (89%) rename WPinternals/Models/{ => UEFIApps}/LumiaPhoneInfoAppModel.cs (53%) create mode 100644 WPinternals/Models/UEFIApps/NokiaFlashModel.cs diff --git a/WPinternals/CommandLine.cs b/WPinternals/CommandLine.cs index c932dd6..622c229 100644 --- a/WPinternals/CommandLine.cs +++ b/WPinternals/CommandLine.cs @@ -85,8 +85,11 @@ internal static async Task ParseCommandLine(System.Threading.SynchronizationCont PhoneNotifierViewModel Notifier; LumiaFlashAppModel FlashModel; LumiaBootManagerAppModel BootMgrModel; + LumiaPhoneInfoAppModel PhoneInfoModel; NokiaPhoneModel NormalModel; - PhoneInfo Info; + LumiaFlashAppPhoneInfo FlashInfo; + LumiaPhoneInfoAppPhoneInfo PhoneInfo; + LumiaBootManagerPhoneInfo BootManagerInfo; string ProductType; string ProductCode; string OperatorCode; @@ -543,8 +546,8 @@ internal static async Task ParseCommandLine(System.Threading.SynchronizationCont { UIContext.Send(s => Notifier.Start(), null); FlashModel = (LumiaFlashAppModel)await SwitchModeViewModel.SwitchTo(Notifier, PhoneInterfaces.Lumia_Flash); - Info = FlashModel.ReadPhoneInfo(); - Info.Log(LogType.ConsoleOnly); + FlashInfo = FlashModel.ReadPhoneInfo(); + FlashInfo.Log(LogType.ConsoleOnly); FFU ProfileFFU = null; FFU CurrentFFU; @@ -557,7 +560,7 @@ internal static async Task ParseCommandLine(System.Threading.SynchronizationCont string PlatformID = CurrentFFU.PlatformID; // Check if the current FFU matches the connected phone, so that the FFU can be used for profiling. - if (Info.PlatformID.StartsWith(PlatformID, StringComparison.OrdinalIgnoreCase)) + if (FlashInfo.PlatformID.StartsWith(PlatformID, StringComparison.OrdinalIgnoreCase)) { ProfileFFU = CurrentFFU; } @@ -566,7 +569,7 @@ internal static async Task ParseCommandLine(System.Threading.SynchronizationCont if (ProfileFFU == null) { - List FFUs = App.Config.FFURepository.Where(e => Info.PlatformID.StartsWith(e.PlatformID, StringComparison.OrdinalIgnoreCase) && e.Exists()).ToList(); + List FFUs = App.Config.FFURepository.Where(e => FlashInfo.PlatformID.StartsWith(e.PlatformID, StringComparison.OrdinalIgnoreCase) && e.Exists()).ToList(); ProfileFFU = FFUs.Count > 0 ? new FFU(FFUs[0].Path) : 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."); @@ -692,8 +695,8 @@ internal static async Task ParseCommandLine(System.Threading.SynchronizationCont Notifier = new PhoneNotifierViewModel(); UIContext.Send(s => Notifier.Start(), null); FlashModel = (LumiaFlashAppModel)await SwitchModeViewModel.SwitchTo(Notifier, PhoneInterfaces.Lumia_Flash); - Info = FlashModel.ReadPhoneInfo(); - Info.Log(LogType.ConsoleOnly); + FlashInfo = FlashModel.ReadPhoneInfo(); + FlashInfo.Log(LogType.ConsoleOnly); Notifier.Stop(); break; case "unlockbootloader": @@ -704,8 +707,8 @@ internal static async Task ParseCommandLine(System.Threading.SynchronizationCont Notifier = new PhoneNotifierViewModel(); UIContext.Send(s => Notifier.Start(), null); FlashModel = (LumiaFlashAppModel)await SwitchModeViewModel.SwitchTo(Notifier, PhoneInterfaces.Lumia_Flash); - Info = FlashModel.ReadPhoneInfo(); - Info.Log(LogType.ConsoleOnly); + FlashInfo = FlashModel.ReadPhoneInfo(); + FlashInfo.Log(LogType.ConsoleOnly); FFU ProfileFFU = null; FFU SupportedFFU = null; @@ -719,7 +722,7 @@ internal static async Task ParseCommandLine(System.Threading.SynchronizationCont string PlatformID = CurrentFFU.PlatformID; // Check if the current FFU matches the connected phone, so that the FFU can be used for profiling. - if (Info.PlatformID.StartsWith(PlatformID, StringComparison.OrdinalIgnoreCase)) + if (FlashInfo.PlatformID.StartsWith(PlatformID, StringComparison.OrdinalIgnoreCase)) { ProfileFFU = CurrentFFU; } @@ -734,7 +737,7 @@ internal static async Task ParseCommandLine(System.Threading.SynchronizationCont if (ProfileFFU == null) { - List FFUs = App.Config.FFURepository.Where(e => Info.PlatformID.StartsWith(e.PlatformID, StringComparison.OrdinalIgnoreCase) && e.Exists()).ToList(); + List FFUs = App.Config.FFURepository.Where(e => FlashInfo.PlatformID.StartsWith(e.PlatformID, StringComparison.OrdinalIgnoreCase) && e.Exists()).ToList(); ProfileFFU = FFUs.Count > 0 ? new FFU(FFUs[0].Path) : 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."); @@ -777,8 +780,8 @@ internal static async Task ParseCommandLine(System.Threading.SynchronizationCont Notifier = new PhoneNotifierViewModel(); UIContext.Send(s => Notifier.Start(), null); FlashModel = (LumiaFlashAppModel)await SwitchModeViewModel.SwitchTo(Notifier, PhoneInterfaces.Lumia_Flash); - Info = FlashModel.ReadPhoneInfo(); - Info.Log(LogType.ConsoleOnly); + FlashInfo = FlashModel.ReadPhoneInfo(); + FlashInfo.Log(LogType.ConsoleOnly); LogFile.Log("Preparing to flash Custom ROM", LogType.FileAndConsole); await LumiaV2UnlockBootViewModel.LumiaV2FlashArchive(Notifier, CustomRomPath); LogFile.Log("Custom ROM flashed successfully", LogType.FileAndConsole); @@ -808,10 +811,10 @@ internal static async Task ParseCommandLine(System.Threading.SynchronizationCont Notifier = new PhoneNotifierViewModel(); UIContext.Send(s => Notifier.Start(), null); FlashModel = (LumiaFlashAppModel)await SwitchModeViewModel.SwitchTo(Notifier, PhoneInterfaces.Lumia_Flash); - Info = FlashModel.ReadPhoneInfo(); - Info.Log(LogType.ConsoleOnly); + FlashInfo = FlashModel.ReadPhoneInfo(); + FlashInfo.Log(LogType.ConsoleOnly); LogFile.Log("Flashing FFU...", LogType.FileAndConsole); - await Task.Run(() => FlashModel.FlashFFU(new FFU(FFUPath), true, (byte)(!Info.IsBootloaderSecure ? FlashOptions.SkipSignatureCheck : 0))); + await Task.Run(() => FlashModel.FlashFFU(new FFU(FFUPath), true, (byte)(!FlashInfo.IsBootloaderSecure ? FlashOptions.SkipSignatureCheck : 0))); LogFile.Log("FFU flashed successfully", LogType.FileAndConsole); Notifier.Stop(); } @@ -1275,15 +1278,53 @@ internal static async Task ParseCommandLine(System.Threading.SynchronizationCont } else if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Bootloader) { - BootMgrModel = (LumiaBootManagerAppModel)Notifier.CurrentModel; - Info = BootMgrModel.ReadPhoneInfo(); - ProductCode = Info.ProductCode; + (Notifier.CurrentModel as LumiaBootManagerAppModel).SwitchToPhoneInfoAppContext(); + + if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_PhoneInfo) + { + await Notifier.WaitForArrival(); + } + + if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_PhoneInfo) + { + throw new WPinternalsException("Unexpected Mode"); + } + + PhoneInfoModel = (LumiaPhoneInfoAppModel)Notifier.CurrentModel; + PhoneInfo = PhoneInfoModel.ReadPhoneInfo(); + ProductCode = PhoneInfo.ProductCode; + } + else if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_PhoneInfo) + { + PhoneInfoModel = (LumiaPhoneInfoAppModel)Notifier.CurrentModel; + PhoneInfo = PhoneInfoModel.ReadPhoneInfo(); + ProductCode = PhoneInfo.ProductCode; } else if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Flash) { - FlashModel = (LumiaFlashAppModel)Notifier.CurrentModel; - Info = FlashModel.ReadPhoneInfo(); - ProductCode = Info.ProductCode; + bool ModernFlashApp = ((LumiaFlashAppModel)Notifier.CurrentModel).ReadPhoneInfo().FlashAppProtocolVersionMajor >= 2; + if (ModernFlashApp) + { + ((LumiaFlashAppModel)Notifier.CurrentModel).SwitchToPhoneInfoAppContext(); + } + else + { + ((LumiaFlashAppModel)Notifier.CurrentModel).SwitchToPhoneInfoAppContextLegacy(); + } + + if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_PhoneInfo) + { + await Notifier.WaitForArrival(); + } + + if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_PhoneInfo) + { + throw new WPinternalsException("Unexpected Mode"); + } + + PhoneInfoModel = (LumiaPhoneInfoAppModel)Notifier.CurrentModel; + PhoneInfo = PhoneInfoModel.ReadPhoneInfo(); + ProductCode = PhoneInfo.ProductCode; } else { @@ -1447,14 +1488,16 @@ internal static async Task ParseCommandLine(System.Threading.SynchronizationCont else if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Bootloader) { BootMgrModel = (LumiaBootManagerAppModel)Notifier.CurrentModel; - Info = BootMgrModel.ReadPhoneInfo(); - ProductType = Info.Type; + BootManagerInfo = BootMgrModel.ReadPhoneInfo(); + //ProductType = BootManagerInfo.Type; // TODO: FIXME + ProductType = ""; } else if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Flash) { FlashModel = (LumiaFlashAppModel)Notifier.CurrentModel; - Info = FlashModel.ReadPhoneInfo(); - ProductType = Info.Type; + FlashInfo = FlashModel.ReadPhoneInfo(); + //ProductType = FlashInfo.Type; // TODO: FIXME + ProductType = ""; } else { @@ -1564,14 +1607,16 @@ internal static async Task ParseCommandLine(System.Threading.SynchronizationCont else if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Bootloader) { BootMgrModel = (LumiaBootManagerAppModel)Notifier.CurrentModel; - Info = BootMgrModel.ReadPhoneInfo(); - ProductCode = Info.ProductCode; + BootManagerInfo = BootMgrModel.ReadPhoneInfo(); + //ProductCode = BootManagerInfo.ProductCode; // TODO: FIXME + ProductCode = ""; } else if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Flash) { FlashModel = (LumiaFlashAppModel)Notifier.CurrentModel; - Info = FlashModel.ReadPhoneInfo(); - ProductCode = Info.ProductCode; + FlashInfo = FlashModel.ReadPhoneInfo(); + //ProductCode = FlashInfo.ProductCode; // TODO: FIXME + ProductCode = ""; } else { diff --git a/WPinternals/Models/NokiaFlashModel.cs b/WPinternals/Models/NokiaFlashModel.cs deleted file mode 100644 index b35c7d8..0000000 --- a/WPinternals/Models/NokiaFlashModel.cs +++ /dev/null @@ -1,375 +0,0 @@ -// Copyright (c) 2018, Rene Lergner - @Heathcliff74xda -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -using System; -using System.IO; -using System.Linq; -using System.Windows.Forms; - -namespace WPinternals -{ - internal delegate void InterfaceChangedHandler(PhoneInterfaces NewInterface, string DevicePath); - - internal class NokiaFlashModel : NokiaPhoneModel - { - private string _devicePath; - - internal readonly PhoneInfo Info = new(); - - internal event InterfaceChangedHandler InterfaceChanged = delegate { }; - - // - // Not valid commands - // - /* NOK */ - private const string Signature = "NOK"; - /* NOKX */ - private const string ExtendedMessageSignature = $"{Signature}X"; - /* NOKXC */ - private const string CommonExtendedMessageSignature = $"{ExtendedMessageSignature}C"; - - // - // Common extended commands - // - /* NOKXCB */ - private const string SwitchModeSignature = $"{CommonExtendedMessageSignature}B"; - /* NOKXCE */ - private const string EchoSignature = $"{CommonExtendedMessageSignature}E"; - - public NokiaFlashModel(string DevicePath) : base(DevicePath) - { - _devicePath = DevicePath; - } - - internal void SwitchToBootManagerContext(bool DisableTimeOut = true) - { - PhoneInfo info = ReadPhoneInfoCommon(); - - bool ModernFlashApp = false; - switch (info.App) - { - case FlashAppType.FlashApp: - ModernFlashApp = info.FlashAppProtocolVersionMajor >= 2; - break; - case FlashAppType.PhoneInfoApp: - ModernFlashApp = info.PhoneInfoAppVersionMajor >= 2; - break; - case FlashAppType.BootManager: - ModernFlashApp = info.BootManagerVersionMajor >= 2; - break; - } - - byte[] Request = new byte[7]; - ByteOperations.WriteAsciiString(Request, 0, SwitchModeSignature + "B"); - byte[] Response = ExecuteRawMethod(Request); - if (ByteOperations.ReadAsciiString(Response, 0, 4) == "NOKU") - { - throw new NotSupportedException(); - } - - UInt16 Error = (UInt16)((Response[6] << 8) + Response[7]); - if (Error > 0) - { - throw new NotSupportedException("SwitchToBootManagerContext: Error 0x" + Error.ToString("X4")); - } - - if (ModernFlashApp) - { - DisableRebootTimeOut(); - - Info.App = FlashAppType.BootManager; - - // If current Info class was retrieved while in BootMgr mode, then we need to invalidate this data, because it is incomplete. - if (Info.PlatformID == null) - { - Info.State = PhoneInfoState.Empty; - } - - InterfaceChanged(PhoneInterfaces.Lumia_Bootloader, _devicePath); - } - } - - internal void SwitchToPhoneInfoAppContext() - { - PhoneInfo info = ReadPhoneInfoCommon(); - - bool ModernFlashApp = false; - switch (info.App) - { - case FlashAppType.FlashApp: - ModernFlashApp = info.FlashAppProtocolVersionMajor >= 2; - break; - case FlashAppType.PhoneInfoApp: - ModernFlashApp = info.PhoneInfoAppVersionMajor >= 2; - break; - case FlashAppType.BootManager: - ModernFlashApp = info.BootManagerVersionMajor >= 2; - break; - } - - byte[] Request = new byte[7]; - ByteOperations.WriteAsciiString(Request, 0, SwitchModeSignature + "P"); - byte[] Response = ExecuteRawMethod(Request); - if (ByteOperations.ReadAsciiString(Response, 0, 4) == "NOKU") - { - throw new NotSupportedException(); - } - - UInt16 Error = (UInt16)((Response[6] << 8) + Response[7]); - if (Error > 0) - { - throw new NotSupportedException("SwitchToPhoneInfoAppContext: Error 0x" + Error.ToString("X4")); - } - - if (ModernFlashApp) - { - DisableRebootTimeOut(); - - Info.App = FlashAppType.PhoneInfoApp; - - // If current Info class was retrieved while in BootMgr mode, then we need to invalidate this data, because it is incomplete. - if (Info.PlatformID == null) - { - Info.State = PhoneInfoState.Empty; - } - - InterfaceChanged(PhoneInterfaces.Lumia_PhoneInfo, _devicePath); - } - } - - internal void SwitchToMmosContext() - { - byte[] Request = new byte[7]; - ByteOperations.WriteAsciiString(Request, 0, SwitchModeSignature + "A"); - ExecuteRawVoidMethod(Request); - - ResetDevice(); - - Dispose(true); - } - - internal void SwitchToFlashAppContext() - { - PhoneInfo info = ReadPhoneInfoCommon(); - - bool ModernFlashApp = false; - switch (info.App) - { - case FlashAppType.FlashApp: - ModernFlashApp = info.FlashAppProtocolVersionMajor >= 2; - break; - case FlashAppType.PhoneInfoApp: - ModernFlashApp = info.PhoneInfoAppVersionMajor >= 2; - break; - case FlashAppType.BootManager: - ModernFlashApp = info.BootManagerVersionMajor >= 2; - break; - } - - byte[] Request = new byte[7]; - ByteOperations.WriteAsciiString(Request, 0, SwitchModeSignature + "F"); // This will stop charging the phone - byte[] Response = ExecuteRawMethod(Request); - if (ByteOperations.ReadAsciiString(Response, 0, 4) == "NOKU") - { - throw new NotSupportedException(); - } - - UInt16 Error = (UInt16)((Response[6] << 8) + Response[7]); - if (Error > 0) - { - throw new NotSupportedException("SwitchToFlashAppContext: Error 0x" + Error.ToString("X4")); - } - - if (ModernFlashApp) - { - DisableRebootTimeOut(); - - Info.App = FlashAppType.FlashApp; - - // If current Info class was retrieved while in BootMgr mode, then we need to invalidate this data, because it is incomplete. - if (Info.PlatformID == null) - { - Info.State = PhoneInfoState.Empty; - } - - InterfaceChanged(PhoneInterfaces.Lumia_Flash, _devicePath); - } - } - - - // - // Normal commands - // - /* NOKD */ - private const string DisableTimeoutsSignature = $"{Signature}D"; - /* NOKI */ - private const string HelloSignature = $"{Signature}I"; - /* NOKV */ - private const string InfoQuerySignature = $"{Signature}V"; - - internal FlashAppType GetFlashAppType() - { - byte[] Request = new byte[4]; - ByteOperations.WriteAsciiString(Request, 0, InfoQuerySignature); - byte[] Response = ExecuteRawMethod(Request); - if ((Response == null) || (ByteOperations.ReadAsciiString(Response, 0, 4) == "NOKU")) - { - throw new NotSupportedException(); - } - - return (FlashAppType)Response[5]; - } - - - internal PhoneInfo ReadPhoneInfoCommon() - { - // NOKH = Get Phone Info (IMEI and info from Product.dat) - Not available on some phones, like Lumia 640. - // NOKV = Info Query - - PhoneInfo Result = Info; - - if (Result.State == PhoneInfoState.Empty) - { - byte[] Request = new byte[4]; - ByteOperations.WriteAsciiString(Request, 0, InfoQuerySignature); - byte[] Response = ExecuteRawMethod(Request); - if ((Response != null) && (ByteOperations.ReadAsciiString(Response, 0, 4) != "NOKU")) - { - Result.App = (FlashAppType)Response[5]; - - switch (Result.App) - { - case FlashAppType.BootManager: - Result.BootManagerProtocolVersionMajor = Response[6]; - Result.BootManagerProtocolVersionMinor = Response[7]; - Result.BootManagerVersionMajor = Response[8]; - Result.BootManagerVersionMinor = Response[9]; - break; - case FlashAppType.FlashApp: - Result.FlashAppProtocolVersionMajor = Response[6]; - Result.FlashAppProtocolVersionMinor = Response[7]; - Result.FlashAppVersionMajor = Response[8]; - Result.FlashAppVersionMinor = Response[9]; - break; - case FlashAppType.PhoneInfoApp: - Result.PhoneInfoAppProtocolVersionMajor = Response[6]; - Result.PhoneInfoAppProtocolVersionMinor = Response[7]; - Result.PhoneInfoAppVersionMajor = Response[8]; - Result.PhoneInfoAppVersionMinor = Response[9]; - break; - } - - byte SubblockCount = Response[10]; - int SubblockOffset = 11; - - for (int i = 0; i < SubblockCount; i++) - { - byte SubblockID = Response[SubblockOffset + 0x00]; - UInt16 SubblockLength = BigEndian.ToUInt16(Response, SubblockOffset + 0x01); - int SubblockPayloadOffset = SubblockOffset + 3; - byte SubblockVersion; - switch (SubblockID) - { - case 0x01: - Result.TransferSize = BigEndian.ToUInt32(Response, SubblockPayloadOffset); - break; - case 0x02: - Result.WriteBufferSize = BigEndian.ToUInt32(Response, SubblockPayloadOffset); - break; - case 0x03: - Result.EmmcSizeInSectors = BigEndian.ToUInt32(Response, SubblockPayloadOffset); - break; - case 0x04: - if (Result.App == FlashAppType.BootManager) - { - Result.FlashAppProtocolVersionMajor = Response[SubblockPayloadOffset + 0x00]; - Result.FlashAppProtocolVersionMinor = Response[SubblockPayloadOffset + 0x01]; - Result.FlashAppVersionMajor = Response[SubblockPayloadOffset + 0x02]; - Result.FlashAppVersionMinor = Response[SubblockPayloadOffset + 0x03]; - } - else if (Result.App == FlashAppType.FlashApp) - { - Result.SdCardSizeInSectors = BigEndian.ToUInt32(Response, SubblockPayloadOffset); - } - break; - case 0x05: - Result.PlatformID = ByteOperations.ReadAsciiString(Response, (uint)SubblockPayloadOffset, SubblockLength).Trim([' ', '\0']); - break; - case 0x0D: - Result.AsyncSupport = Response[SubblockPayloadOffset + 1] == 1; - break; - case 0x0F: - SubblockVersion = Response[SubblockPayloadOffset]; // 0x03 - Result.PlatformSecureBootEnabled = Response[SubblockPayloadOffset + 0x01] == 0x01; - Result.SecureFfuEnabled = Response[SubblockPayloadOffset + 0x02] == 0x01; - Result.JtagDisabled = Response[SubblockPayloadOffset + 0x03] == 0x01; - Result.RdcPresent = Response[SubblockPayloadOffset + 0x04] == 0x01; - Result.Authenticated = (Response[SubblockPayloadOffset + 0x05] == 0x01) || (Response[SubblockPayloadOffset + 0x05] == 0x02); - Result.UefiSecureBootEnabled = Response[SubblockPayloadOffset + 0x06] == 0x01; - Result.SecondaryHardwareKeyPresent = Response[SubblockPayloadOffset + 0x07] == 0x01; - break; - case 0x10: - SubblockVersion = Response[SubblockPayloadOffset]; // 0x01 - Result.SecureFfuSupportedProtocolMask = BigEndian.ToUInt16(Response, SubblockPayloadOffset + 0x01); - break; - case 0x1F: - Result.MmosOverUsbSupported = Response[SubblockPayloadOffset] == 1; - break; - case 0x20: - // CRC header info - break; - } - SubblockOffset += SubblockLength + 3; - } - } - - Result.State = PhoneInfoState.Basic; - } - - Result.IsBootloaderSecure = !(Info.Authenticated || Info.RdcPresent || !Info.SecureFfuEnabled); - - return Result; - } - - public void DisableRebootTimeOut() - { - byte[] Request = new byte[4]; - const string Header = DisableTimeoutsSignature; - Buffer.BlockCopy(System.Text.Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length); - ExecuteRawMethod(Request); - } - - internal void Hello() - { - byte[] Request = new byte[4]; - ByteOperations.WriteAsciiString(Request, 0, HelloSignature); - byte[] Response = ExecuteRawMethod(Request); - if (Response == null) - { - throw new BadConnectionException(); - } - - if (ByteOperations.ReadAsciiString(Response, 0, 4) != HelloSignature) - { - throw new WPinternalsException("Bad response from phone!", "The phone did not answer properly to the Hello message sent."); - } - } - } -} diff --git a/WPinternals/Models/PhoneInfo.cs b/WPinternals/Models/PhoneInfo.cs deleted file mode 100644 index d8c532a..0000000 --- a/WPinternals/Models/PhoneInfo.cs +++ /dev/null @@ -1,134 +0,0 @@ -// Copyright (c) 2018, Rene Lergner - @Heathcliff74xda -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -using System; - -namespace WPinternals -{ - internal class PhoneInfo - { - public PhoneInfoState State = PhoneInfoState.Empty; - - public string Type; // Extended info - public string ProductCode; // Extended info - public string Imei; // Extended info - public string Firmware; // Extended info - public byte[] RKH; // Extended info - - public FlashAppType App; - - public byte FlashAppVersionMajor; - public byte FlashAppVersionMinor; - public byte FlashAppProtocolVersionMajor; - public byte FlashAppProtocolVersionMinor; - - public byte BootManagerVersionMajor; - public byte BootManagerVersionMinor; - public byte BootManagerProtocolVersionMajor; - public byte BootManagerProtocolVersionMinor; - - public byte PhoneInfoAppVersionMajor; - public byte PhoneInfoAppVersionMinor; - public byte PhoneInfoAppProtocolVersionMajor; - public byte PhoneInfoAppProtocolVersionMinor; - - public UInt32 TransferSize; - public bool MmosOverUsbSupported; - public UInt32 SdCardSizeInSectors; - public UInt32 WriteBufferSize; - public UInt32 EmmcSizeInSectors; - public string PlatformID; - public UInt16 SecureFfuSupportedProtocolMask; - public bool AsyncSupport; - - public bool PlatformSecureBootEnabled; - public bool SecureFfuEnabled; - public bool JtagDisabled; - public bool RdcPresent; - public bool Authenticated; - public bool UefiSecureBootEnabled; - public bool SecondaryHardwareKeyPresent; - - public bool IsBootloaderSecure; - - internal void Log(LogType Type) - { - if (State == PhoneInfoState.Extended) - { - if (this.Type != null) - { - LogFile.Log("Phone type: " + this.Type, Type); - } - - if (ProductCode != null) - { - LogFile.Log("Product code: " + ProductCode, Type); - } - - if (RKH != null) - { - LogFile.Log("Root key hash: " + Converter.ConvertHexToString(RKH, ""), Type); - } - - if (Firmware?.Length > 0) - { - LogFile.Log("Firmware version: " + Firmware, Type); - } - - if (Type != LogType.ConsoleOnly && (Imei != null)) - { - LogFile.Log("IMEI: " + Imei, LogType.FileOnly); - } - } - - switch (App) - { - case FlashAppType.BootManager: - LogFile.Log("Bootmanager: " + BootManagerVersionMajor + "." + BootManagerVersionMinor, Type); - LogFile.Log("Bootmanager protocol: " + BootManagerProtocolVersionMajor + "." + BootManagerProtocolVersionMinor, Type); - LogFile.Log("Flash app: " + FlashAppVersionMajor + "." + FlashAppVersionMinor, Type); - LogFile.Log("Flash protocol: " + FlashAppProtocolVersionMajor + "." + FlashAppProtocolVersionMinor, Type); - break; - case FlashAppType.FlashApp: - LogFile.Log("Flash app: " + FlashAppVersionMajor + "." + FlashAppVersionMinor, Type); - LogFile.Log("Flash protocol: " + FlashAppProtocolVersionMajor + "." + FlashAppProtocolVersionMinor, Type); - break; - case FlashAppType.PhoneInfoApp: - LogFile.Log("Phone info app: " + PhoneInfoAppVersionMajor + "." + PhoneInfoAppVersionMinor, Type); - LogFile.Log("Phone info protocol: " + PhoneInfoAppProtocolVersionMajor + "." + PhoneInfoAppProtocolVersionMinor, Type); - break; - } - - LogFile.Log("SecureBoot: " + ((!PlatformSecureBootEnabled || !UefiSecureBootEnabled) ? "Disabled" : "Enabled") + " (Platform Secure Boot: " + (PlatformSecureBootEnabled ? "Enabled" : "Disabled") + ", UEFI Secure Boot: " + (UefiSecureBootEnabled ? "Enabled" : "Disabled") + ")", Type); - - if ((Type == LogType.ConsoleOnly) || (Type == LogType.FileAndConsole)) - { - LogFile.Log("Flash app security: " + (!IsBootloaderSecure ? "Disabled" : "Enabled"), LogType.ConsoleOnly); - } - - if ((Type == LogType.FileOnly) || (Type == LogType.FileAndConsole)) - { - LogFile.Log("Flash app security: " + (!IsBootloaderSecure ? "Disabled" : "Enabled") + " (FFU security: " + (SecureFfuEnabled ? "Enabled" : "Disabled") + ", RDC: " + (RdcPresent ? "Present" : "Not found") + ", Authenticated: " + (Authenticated ? "True" : "False") + ")", LogType.FileOnly); - } - - LogFile.Log("JTAG: " + (JtagDisabled ? "Disabled" : "Enabled"), Type); - } - } -} diff --git a/WPinternals/Models/PhoneInfo/CommonPhoneInfo.cs b/WPinternals/Models/PhoneInfo/CommonPhoneInfo.cs new file mode 100644 index 0000000..aadf44d --- /dev/null +++ b/WPinternals/Models/PhoneInfo/CommonPhoneInfo.cs @@ -0,0 +1,42 @@ +// Copyright (c) 2018, Rene Lergner - @Heathcliff74xda +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +using System; + +namespace WPinternals +{ + internal class CommonPhoneInfo + { + public PhoneInfoState State = PhoneInfoState.Empty; + + public FlashAppType App; + + public byte VersionMajor; + public byte VersionMinor; + public byte ProtocolVersionMajor; + public byte ProtocolVersionMinor; + + internal void Log(LogType Type) + { + LogFile.Log($"App: {VersionMajor}.{VersionMinor}", Type); + LogFile.Log($"Protocol: {ProtocolVersionMajor}.{ProtocolVersionMinor}", Type); + } + } +} diff --git a/WPinternals/Models/PhoneInfo/LumiaBootManagerPhoneInfo.cs b/WPinternals/Models/PhoneInfo/LumiaBootManagerPhoneInfo.cs new file mode 100644 index 0000000..bacfa2f --- /dev/null +++ b/WPinternals/Models/PhoneInfo/LumiaBootManagerPhoneInfo.cs @@ -0,0 +1,59 @@ +// Copyright (c) 2018, Rene Lergner - @Heathcliff74xda +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +using System; + +namespace WPinternals +{ + internal class LumiaBootManagerPhoneInfo + { + public PhoneInfoState State = PhoneInfoState.Empty; + + public FlashAppType App; + + public byte FlashAppVersionMajor; + public byte FlashAppVersionMinor; + public byte FlashAppProtocolVersionMajor; + public byte FlashAppProtocolVersionMinor; + + public byte BootManagerVersionMajor; + public byte BootManagerVersionMinor; + public byte BootManagerProtocolVersionMajor; + public byte BootManagerProtocolVersionMinor; + + public UInt32 TransferSize; + public bool MmosOverUsbSupported; + + internal void Log(LogType Type) + { + switch (App) + { + case FlashAppType.BootManager: + LogFile.Log($"Bootmanager: {BootManagerVersionMajor}.{BootManagerVersionMinor}", Type); + LogFile.Log($"Bootmanager protocol: {BootManagerProtocolVersionMajor}.{BootManagerProtocolVersionMinor}", Type); + LogFile.Log($"Flash app: {FlashAppVersionMajor}.{FlashAppVersionMinor}", Type); + LogFile.Log($"Flash protocol: {FlashAppProtocolVersionMajor}.{FlashAppProtocolVersionMinor}", Type); + LogFile.Log($"Flash app: {FlashAppVersionMajor}.{FlashAppVersionMinor}", Type); + LogFile.Log($"Flash protocol: {FlashAppProtocolVersionMajor}.{FlashAppProtocolVersionMinor}", Type); + break; + } + } + } +} diff --git a/WPinternals/Models/PhoneInfo/LumiaFlashAppPhoneInfo.cs b/WPinternals/Models/PhoneInfo/LumiaFlashAppPhoneInfo.cs new file mode 100644 index 0000000..d72f427 --- /dev/null +++ b/WPinternals/Models/PhoneInfo/LumiaFlashAppPhoneInfo.cs @@ -0,0 +1,96 @@ +// Copyright (c) 2018, Rene Lergner - @Heathcliff74xda +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +using System; + +namespace WPinternals +{ + internal class LumiaFlashAppPhoneInfo + { + public PhoneInfoState State = PhoneInfoState.Empty; + + public string Firmware; // Extended info + public byte[] RKH; // Extended info + + public FlashAppType App; + + public byte FlashAppVersionMajor; + public byte FlashAppVersionMinor; + public byte FlashAppProtocolVersionMajor; + public byte FlashAppProtocolVersionMinor; + + public UInt32 TransferSize; + public bool MmosOverUsbSupported; + public UInt32 SdCardSizeInSectors; + public UInt32 WriteBufferSize; + public UInt32 EmmcSizeInSectors; + public string PlatformID; + public UInt16 SecureFfuSupportedProtocolMask; + public bool AsyncSupport; + + public bool PlatformSecureBootEnabled; + public bool SecureFfuEnabled; + public bool JtagDisabled; + public bool RdcPresent; + public bool Authenticated; + public bool UefiSecureBootEnabled; + public bool SecondaryHardwareKeyPresent; + + public bool IsBootloaderSecure; + + internal void Log(LogType Type) + { + if (State == PhoneInfoState.Extended) + { + if (RKH != null) + { + LogFile.Log($"Root key hash: {Converter.ConvertHexToString(RKH, "")}", Type); + } + + if (Firmware?.Length > 0) + { + LogFile.Log($"Firmware version: {Firmware}", Type); + } + } + + switch (App) + { + case FlashAppType.FlashApp: + LogFile.Log($"Flash app: {FlashAppVersionMajor}.{FlashAppVersionMinor}", Type); + LogFile.Log($"Flash protocol: {FlashAppProtocolVersionMajor}.{FlashAppProtocolVersionMinor}", Type); + break; + } + + LogFile.Log($"SecureBoot: {((!PlatformSecureBootEnabled || !UefiSecureBootEnabled) ? "Disabled" : "Enabled")} (Platform Secure Boot: {(PlatformSecureBootEnabled ? "Enabled" : "Disabled")}, UEFI Secure Boot: {(UefiSecureBootEnabled ? "Enabled" : "Disabled")})", Type); + + if ((Type == LogType.ConsoleOnly) || (Type == LogType.FileAndConsole)) + { + LogFile.Log($"Flash app security: {(!IsBootloaderSecure ? "Disabled" : "Enabled")}", LogType.ConsoleOnly); + } + + if ((Type == LogType.FileOnly) || (Type == LogType.FileAndConsole)) + { + LogFile.Log($"Flash app security: {(!IsBootloaderSecure ? "Disabled" : "Enabled")} (FFU security: {(SecureFfuEnabled ? "Enabled" : "Disabled")}, RDC: {(RdcPresent ? "Present" : "Not found")}, Authenticated: {(Authenticated ? "True" : "False")})", LogType.FileOnly); + } + + LogFile.Log($"JTAG: {(JtagDisabled ? "Disabled" : "Enabled")}", Type); + } + } +} diff --git a/WPinternals/Models/PhoneInfo/LumiaPhoneInfoAppPhoneInfo.cs b/WPinternals/Models/PhoneInfo/LumiaPhoneInfoAppPhoneInfo.cs new file mode 100644 index 0000000..7aa285e --- /dev/null +++ b/WPinternals/Models/PhoneInfo/LumiaPhoneInfoAppPhoneInfo.cs @@ -0,0 +1,69 @@ +// Copyright (c) 2018, Rene Lergner - @Heathcliff74xda +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +using System; + +namespace WPinternals +{ + internal class LumiaPhoneInfoAppPhoneInfo + { + public PhoneInfoState State = PhoneInfoState.Empty; + + public string Type; // Extended info + public string ProductCode; // Extended info + public string Imei; // Extended info + + public FlashAppType App; + + public byte PhoneInfoAppVersionMajor; + public byte PhoneInfoAppVersionMinor; + public byte PhoneInfoAppProtocolVersionMajor; + public byte PhoneInfoAppProtocolVersionMinor; + + internal void Log(LogType Type) + { + if (State == PhoneInfoState.Extended) + { + if (this.Type != null) + { + LogFile.Log($"Phone type: {this.Type}", Type); + } + + if (ProductCode != null) + { + LogFile.Log($"Product code: {ProductCode}", Type); + } + + if (Type != LogType.ConsoleOnly && (Imei != null)) + { + LogFile.Log($"IMEI: {Imei}", LogType.FileOnly); + } + } + + switch (App) + { + case FlashAppType.PhoneInfoApp: + LogFile.Log($"Phone info app: {PhoneInfoAppVersionMajor}.{PhoneInfoAppVersionMinor}", Type); + LogFile.Log($"Phone info protocol: {PhoneInfoAppProtocolVersionMajor}.{PhoneInfoAppProtocolVersionMinor}", Type); + break; + } + } + } +} diff --git a/WPinternals/Models/LumiaBootManagerAppModel.cs b/WPinternals/Models/UEFIApps/LumiaBootManagerAppModel.cs similarity index 79% rename from WPinternals/Models/LumiaBootManagerAppModel.cs rename to WPinternals/Models/UEFIApps/LumiaBootManagerAppModel.cs index 631cfa3..4ba8005 100644 --- a/WPinternals/Models/LumiaBootManagerAppModel.cs +++ b/WPinternals/Models/UEFIApps/LumiaBootManagerAppModel.cs @@ -26,6 +26,8 @@ namespace WPinternals { internal class LumiaBootManagerAppModel : NokiaFlashModel { + internal readonly LumiaBootManagerPhoneInfo BootManagerInfo = new(); + internal enum SecureBootKeyType : byte { Retail = 0, @@ -102,15 +104,15 @@ internal void ContinueBoot() ExecuteRawVoidMethod(Request); } - internal PhoneInfo ReadPhoneInfo(bool ExtendedInfo = true) + internal LumiaBootManagerPhoneInfo ReadPhoneInfo(bool ExtendedInfo = true) { // NOKH = Get Phone Info (IMEI and info from Product.dat) - Not available on some phones, like Lumia 640. // NOKV = Info Query - bool PhoneInfoLogged = Info.State != PhoneInfoState.Empty; - ReadPhoneInfoCommon(); + bool PhoneInfoLogged = BootManagerInfo.State != PhoneInfoState.Empty; + ReadPhoneInfoBootManager(); - PhoneInfo Result = Info; + LumiaBootManagerPhoneInfo Result = BootManagerInfo; if (!PhoneInfoLogged) { @@ -120,6 +122,67 @@ internal PhoneInfo ReadPhoneInfo(bool ExtendedInfo = true) return Result; } + internal LumiaBootManagerPhoneInfo ReadPhoneInfoBootManager() + { + // NOKH = Get Phone Info (IMEI and info from Product.dat) - Not available on some phones, like Lumia 640. + // NOKV = Info Query + + LumiaBootManagerPhoneInfo Result = BootManagerInfo; + + byte[] Request = new byte[4]; + ByteOperations.WriteAsciiString(Request, 0, InfoQuerySignature); + byte[] Response = ExecuteRawMethod(Request); + if ((Response != null) && (ByteOperations.ReadAsciiString(Response, 0, 4) != "NOKU")) + { + Result.App = (FlashAppType)Response[5]; + + switch (Result.App) + { + case FlashAppType.BootManager: + Result.BootManagerProtocolVersionMajor = Response[6]; + Result.BootManagerProtocolVersionMinor = Response[7]; + Result.BootManagerVersionMajor = Response[8]; + Result.BootManagerVersionMinor = Response[9]; + break; + } + + byte SubblockCount = Response[10]; + int SubblockOffset = 11; + + for (int i = 0; i < SubblockCount; i++) + { + byte SubblockID = Response[SubblockOffset + 0x00]; + + LogFile.Log($"{Result.App} SubblockID: 0x{SubblockID:X}"); + + UInt16 SubblockLength = BigEndian.ToUInt16(Response, SubblockOffset + 0x01); + int SubblockPayloadOffset = SubblockOffset + 3; + byte SubblockVersion; + switch (SubblockID) + { + case 0x01: + Result.TransferSize = BigEndian.ToUInt32(Response, SubblockPayloadOffset); + break; + case 0x04: + Result.FlashAppProtocolVersionMajor = Response[SubblockPayloadOffset + 0x00]; + Result.FlashAppProtocolVersionMinor = Response[SubblockPayloadOffset + 0x01]; + Result.FlashAppVersionMajor = Response[SubblockPayloadOffset + 0x02]; + Result.FlashAppVersionMinor = Response[SubblockPayloadOffset + 0x03]; + break; + case 0x1F: + Result.MmosOverUsbSupported = Response[SubblockPayloadOffset] == 1; + break; + case 0x20: + // CRC header info + break; + } + SubblockOffset += SubblockLength + 3; + } + } + + return Result; + } + internal GPT ReadGPT() { // If this function is used with a locked BootMgr v1, diff --git a/WPinternals/Models/LumiaFlashAppModel.cs b/WPinternals/Models/UEFIApps/LumiaFlashAppModel.cs similarity index 89% rename from WPinternals/Models/LumiaFlashAppModel.cs rename to WPinternals/Models/UEFIApps/LumiaFlashAppModel.cs index 5bfc896..9619dbc 100644 --- a/WPinternals/Models/LumiaFlashAppModel.cs +++ b/WPinternals/Models/UEFIApps/LumiaFlashAppModel.cs @@ -70,6 +70,7 @@ internal enum FlashOptions : byte internal class LumiaFlashAppModel : NokiaFlashModel { + internal readonly LumiaFlashAppPhoneInfo FlashAppInfo = new(); private UefiSecurityStatusResponse _SecurityStatus = null; // @@ -156,15 +157,15 @@ public LumiaFlashAppModel(string DevicePath) : base(DevicePath) { } - internal PhoneInfo ReadPhoneInfo(bool ExtendedInfo = true) + internal LumiaFlashAppPhoneInfo ReadPhoneInfo(bool ExtendedInfo = true) { // NOKH = Get Phone Info (IMEI and info from Product.dat) - Not available on some phones, like Lumia 640. // NOKV = Info Query - bool PhoneInfoLogged = Info.State != PhoneInfoState.Empty; - ReadPhoneInfoCommon(); + bool PhoneInfoLogged = FlashAppInfo.State != PhoneInfoState.Empty; + ReadPhoneInfoFlashApp(); - PhoneInfo Result = Info; + LumiaFlashAppPhoneInfo Result = FlashAppInfo; if (ExtendedInfo && (Result.State == PhoneInfoState.Basic)) { @@ -185,6 +186,92 @@ internal PhoneInfo ReadPhoneInfo(bool ExtendedInfo = true) return Result; } + internal LumiaFlashAppPhoneInfo ReadPhoneInfoFlashApp() + { + // NOKH = Get Phone Info (IMEI and info from Product.dat) - Not available on some phones, like Lumia 640. + // NOKV = Info Query + + LumiaFlashAppPhoneInfo Result = FlashAppInfo; + + byte[] Request = new byte[4]; + ByteOperations.WriteAsciiString(Request, 0, InfoQuerySignature); + byte[] Response = ExecuteRawMethod(Request); + if ((Response != null) && (ByteOperations.ReadAsciiString(Response, 0, 4) != "NOKU")) + { + Result.App = (FlashAppType)Response[5]; + + switch (Result.App) + { + case FlashAppType.FlashApp: + Result.FlashAppProtocolVersionMajor = Response[6]; + Result.FlashAppProtocolVersionMinor = Response[7]; + Result.FlashAppVersionMajor = Response[8]; + Result.FlashAppVersionMinor = Response[9]; + break; + } + + byte SubblockCount = Response[10]; + int SubblockOffset = 11; + + for (int i = 0; i < SubblockCount; i++) + { + byte SubblockID = Response[SubblockOffset + 0x00]; + + LogFile.Log($"{Result.App} SubblockID: 0x{SubblockID:X}"); + + UInt16 SubblockLength = BigEndian.ToUInt16(Response, SubblockOffset + 0x01); + int SubblockPayloadOffset = SubblockOffset + 3; + byte SubblockVersion; + switch (SubblockID) + { + case 0x01: + Result.TransferSize = BigEndian.ToUInt32(Response, SubblockPayloadOffset); + break; + case 0x02: + Result.WriteBufferSize = BigEndian.ToUInt32(Response, SubblockPayloadOffset); + break; + case 0x03: + Result.EmmcSizeInSectors = BigEndian.ToUInt32(Response, SubblockPayloadOffset); + break; + case 0x04: + Result.SdCardSizeInSectors = BigEndian.ToUInt32(Response, SubblockPayloadOffset); + break; + case 0x05: + Result.PlatformID = ByteOperations.ReadAsciiString(Response, (uint)SubblockPayloadOffset, SubblockLength).Trim([' ', '\0']); + break; + case 0x0D: + Result.AsyncSupport = Response[SubblockPayloadOffset + 1] == 1; + break; + case 0x0F: + SubblockVersion = Response[SubblockPayloadOffset]; // 0x03 + Result.PlatformSecureBootEnabled = Response[SubblockPayloadOffset + 0x01] == 0x01; + Result.SecureFfuEnabled = Response[SubblockPayloadOffset + 0x02] == 0x01; + Result.JtagDisabled = Response[SubblockPayloadOffset + 0x03] == 0x01; + Result.RdcPresent = Response[SubblockPayloadOffset + 0x04] == 0x01; + Result.Authenticated = (Response[SubblockPayloadOffset + 0x05] == 0x01) || (Response[SubblockPayloadOffset + 0x05] == 0x02); + Result.UefiSecureBootEnabled = Response[SubblockPayloadOffset + 0x06] == 0x01; + Result.SecondaryHardwareKeyPresent = Response[SubblockPayloadOffset + 0x07] == 0x01; + break; + case 0x10: + SubblockVersion = Response[SubblockPayloadOffset]; // 0x01 + Result.SecureFfuSupportedProtocolMask = BigEndian.ToUInt16(Response, SubblockPayloadOffset + 0x01); + break; + case 0x1F: + Result.MmosOverUsbSupported = Response[SubblockPayloadOffset] == 1; + break; + case 0x20: + // CRC header info + break; + } + SubblockOffset += SubblockLength + 3; + } + } + + Result.IsBootloaderSecure = !(FlashAppInfo.Authenticated || FlashAppInfo.RdcPresent || !FlashAppInfo.SecureFfuEnabled); + + return Result; + } + internal GPT ReadGPT() { // If this function is used with a locked BootMgr v1, @@ -193,7 +280,7 @@ internal GPT ReadGPT() // Only works in BootLoader-mode or on unlocked bootloaders in Flash-mode!! - PhoneInfo Info = ReadPhoneInfo(ExtendedInfo: false); + LumiaFlashAppPhoneInfo Info = ReadPhoneInfo(ExtendedInfo: false); FlashAppType OriginalAppType = Info.App; bool Switch = Info.IsBootloaderSecure; if (Switch) @@ -230,7 +317,7 @@ internal byte[] GetGptChunk(UInt32 Size) // TODO! // The dummy chunk will contain the GPT, so it can be flashed to the first sectors for testing. byte[] GPTChunk = new byte[Size]; - PhoneInfo Info = ReadPhoneInfo(ExtendedInfo: false); + LumiaFlashAppPhoneInfo Info = ReadPhoneInfo(ExtendedInfo: false); FlashAppType OriginalAppType = Info.App; bool Switch = Info.IsBootloaderSecure; if (Switch) @@ -825,7 +912,7 @@ public void FlashMMOS(string MMOSPath, ProgressUpdater UpdaterPerChunk) ProgressUpdater Progress = UpdaterPerChunk; - PhoneInfo Info = ReadPhoneInfo(); + LumiaFlashAppPhoneInfo Info = ReadPhoneInfo(); if (!Info.MmosOverUsbSupported) { throw new WPinternalsException("Flash failed!", "Protocols not supported. The device reports that loading Microsoft Manufacturing Operating System over USB is not supported."); @@ -884,7 +971,7 @@ public void FlashFFU(FFU FFU, ProgressUpdater UpdaterPerChunk, bool ResetAfterwa ProgressUpdater Progress = UpdaterPerChunk; - PhoneInfo Info = ReadPhoneInfo(); + LumiaFlashAppPhoneInfo Info = ReadPhoneInfo(); if ((Info.SecureFfuSupportedProtocolMask & ((ushort)FfuProtocol.ProtocolSyncV1 | (ushort)FfuProtocol.ProtocolSyncV2)) == 0) { throw new WPinternalsException("Flash failed!", "Protocols not supported. The device reports that both Protocol Sync v1 and Protocol Sync v2 are not supported for FFU flashing. Is this an old device?"); diff --git a/WPinternals/Models/LumiaPhoneInfoAppModel.cs b/WPinternals/Models/UEFIApps/LumiaPhoneInfoAppModel.cs similarity index 53% rename from WPinternals/Models/LumiaPhoneInfoAppModel.cs rename to WPinternals/Models/UEFIApps/LumiaPhoneInfoAppModel.cs index bc28657..19a32af 100644 --- a/WPinternals/Models/LumiaPhoneInfoAppModel.cs +++ b/WPinternals/Models/UEFIApps/LumiaPhoneInfoAppModel.cs @@ -19,6 +19,7 @@ // DEALINGS IN THE SOFTWARE. using System; +using System.Collections.Generic; using System.IO; using System.Linq; @@ -26,6 +27,8 @@ namespace WPinternals { internal class LumiaPhoneInfoAppModel : NokiaFlashModel { + internal readonly LumiaPhoneInfoAppPhoneInfo PhoneInfoAppInfo = new(); + // // Not valid commands // @@ -73,7 +76,7 @@ internal string GetPhoneInfo() // NOKH = Get Phone Info (IMEI and info from Product.dat) - Not available on some phones, like Lumia 640. // NOKV = Info Query - if (Info.FlashAppProtocolVersionMajor >= 2) + if (PhoneInfoAppInfo.PhoneInfoAppVersionMajor >= 2) { return null; } @@ -93,23 +96,58 @@ internal string GetPhoneInfo() return PhoneInfoData; } - internal PhoneInfo ReadPhoneInfo(bool ExtendedInfo = true) + internal LumiaPhoneInfoAppPhoneInfo ReadPhoneInfo(bool ExtendedInfo = true) { // NOKH = Get Phone Info (IMEI and info from Product.dat) - Not available on some phones, like Lumia 640. // NOKV = Info Query - bool PhoneInfoLogged = Info.State != PhoneInfoState.Empty; - ReadPhoneInfoCommon(); + bool PhoneInfoLogged = PhoneInfoAppInfo.State != PhoneInfoState.Empty; + ReadPhoneInfoPhoneInfoApp(); - PhoneInfo Result = Info; + LumiaPhoneInfoAppPhoneInfo Result = PhoneInfoAppInfo; if (ExtendedInfo && (Result.State == PhoneInfoState.Basic)) { try { - Result.Type = ReadPhoneInfoVariable("TYPE"); - Result.ProductCode = ReadPhoneInfoVariable("CTR"); - Result.Imei = ReadPhoneInfoVariable("IMEI"); + if (Result.PhoneInfoAppProtocolVersionMajor >= 2) + { + Result.Type = ReadPhoneInfoVariable("TYPE"); + Result.ProductCode = ReadPhoneInfoVariable("CTR"); + Result.Imei = ReadPhoneInfoVariable("IMEI"); + } + else + { + /* + * Version: 1.1.1.3 + * TYPE: RM-885 + * BTR: 059R0M0 + * LPSN: ... + * HWID: 1000 + * CTR: 059S4B1 + * MC: 0205354 + * IMEI: ... + */ + string PhoneInfoData = GetPhoneInfo(); + if (!string.IsNullOrEmpty(PhoneInfoData)) + { + string[] Variables = PhoneInfoData.Split("\n"); + Dictionary FormattedVariables = []; + foreach (string Variable in Variables) + { + if (!Variable.Contains(":")) + { + continue; + } + + FormattedVariables.Add(Variable.Split(":")[0].Trim(), Variable.Split(":")[1].Trim()); + } + + Result.Type = FormattedVariables["TYPE"]; + Result.ProductCode = FormattedVariables["CTR"]; + Result.Imei = FormattedVariables["IMEI"]; + } + } } catch (Exception ex) { @@ -127,6 +165,55 @@ internal PhoneInfo ReadPhoneInfo(bool ExtendedInfo = true) return Result; } + internal LumiaPhoneInfoAppPhoneInfo ReadPhoneInfoPhoneInfoApp() + { + // NOKH = Get Phone Info (IMEI and info from Product.dat) - Not available on some phones, like Lumia 640. + // NOKV = Info Query + + LumiaPhoneInfoAppPhoneInfo Result = PhoneInfoAppInfo; + + byte[] Request = new byte[4]; + ByteOperations.WriteAsciiString(Request, 0, InfoQuerySignature); + byte[] Response = ExecuteRawMethod(Request); + if ((Response != null) && (ByteOperations.ReadAsciiString(Response, 0, 4) != "NOKU")) + { + Result.App = (FlashAppType)Response[5]; + + switch (Result.App) + { + case FlashAppType.PhoneInfoApp: + Result.PhoneInfoAppProtocolVersionMajor = Response[6]; + Result.PhoneInfoAppProtocolVersionMinor = Response[7]; + Result.PhoneInfoAppVersionMajor = Response[8]; + Result.PhoneInfoAppVersionMinor = Response[9]; + break; + } + + byte SubblockCount = Response[10]; + int SubblockOffset = 11; + + for (int i = 0; i < SubblockCount; i++) + { + byte SubblockID = Response[SubblockOffset + 0x00]; + + LogFile.Log($"{Result.App} SubblockID: 0x{SubblockID:X}"); + + UInt16 SubblockLength = BigEndian.ToUInt16(Response, SubblockOffset + 0x01); + int SubblockPayloadOffset = SubblockOffset + 3; + byte SubblockVersion; + switch (SubblockID) + { + case 0x20: + // CRC header info + break; + } + SubblockOffset += SubblockLength + 3; + } + } + + return Result; + } + internal string ReadPhoneInfoVariable(string VariableName) { // This function assumes the phone is in Phone Info App context @@ -141,7 +228,6 @@ internal string ReadPhoneInfoVariable(string VariableName) internal string ReadProductCode() { string Result = ReadPhoneInfoVariable("CTR"); - SwitchToFlashAppContext(); return Result; } } diff --git a/WPinternals/Models/UEFIApps/NokiaFlashModel.cs b/WPinternals/Models/UEFIApps/NokiaFlashModel.cs new file mode 100644 index 0000000..4ced3f3 --- /dev/null +++ b/WPinternals/Models/UEFIApps/NokiaFlashModel.cs @@ -0,0 +1,230 @@ +// Copyright (c) 2018, Rene Lergner - @Heathcliff74xda +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +using System; + +namespace WPinternals +{ + internal delegate void InterfaceChangedHandler(PhoneInterfaces NewInterface, string DevicePath); + + internal class NokiaFlashModel : NokiaPhoneModel + { + private string _devicePath; + + private readonly CommonPhoneInfo CommonInfo = new(); + + internal event InterfaceChangedHandler InterfaceChanged = delegate { }; + + // + // Not valid commands + // + /* NOK */ + private const string Signature = "NOK"; + /* NOKX */ + private const string ExtendedMessageSignature = $"{Signature}X"; + /* NOKXC */ + private const string CommonExtendedMessageSignature = $"{ExtendedMessageSignature}C"; + + // + // Common extended commands + // + /* NOKXCB */ + private const string SwitchModeSignature = $"{CommonExtendedMessageSignature}B"; + /* NOKXCE */ + private const string EchoSignature = $"{CommonExtendedMessageSignature}E"; + + public NokiaFlashModel(string DevicePath) : base(DevicePath) + { + _devicePath = DevicePath; + } + + internal void SwitchToBootManagerContext(bool DisableTimeOut = true) + { + CommonPhoneInfo info = ReadPhoneInfoCommon(); + + bool ModernFlashApp = info.VersionMajor >= 2; + + byte[] Request = new byte[7]; + ByteOperations.WriteAsciiString(Request, 0, SwitchModeSignature + "B"); + byte[] Response = ExecuteRawMethod(Request); + if (ByteOperations.ReadAsciiString(Response, 0, 4) == "NOKU") + { + throw new NotSupportedException(); + } + + UInt16 Error = (UInt16)((Response[6] << 8) + Response[7]); + if (Error > 0) + { + throw new NotSupportedException("SwitchToBootManagerContext: Error 0x" + Error.ToString("X4")); + } + + if (ModernFlashApp) + { + DisableRebootTimeOut(); + + InterfaceChanged(PhoneInterfaces.Lumia_Bootloader, _devicePath); + } + } + + internal void SwitchToPhoneInfoAppContext() + { + CommonPhoneInfo info = ReadPhoneInfoCommon(); + + bool ModernFlashApp = info.VersionMajor >= 2; + + byte[] Request = new byte[7]; + ByteOperations.WriteAsciiString(Request, 0, SwitchModeSignature + "P"); + byte[] Response = ExecuteRawMethod(Request); + if (ByteOperations.ReadAsciiString(Response, 0, 4) == "NOKU") + { + throw new NotSupportedException(); + } + + UInt16 Error = (UInt16)((Response[6] << 8) + Response[7]); + if (Error > 0) + { + throw new NotSupportedException("SwitchToPhoneInfoAppContext: Error 0x" + Error.ToString("X4")); + } + + if (ModernFlashApp) + { + DisableRebootTimeOut(); + + CommonInfo.App = FlashAppType.PhoneInfoApp; + + InterfaceChanged(PhoneInterfaces.Lumia_PhoneInfo, _devicePath); + } + } + + internal void SwitchToMmosContext() + { + byte[] Request = new byte[7]; + ByteOperations.WriteAsciiString(Request, 0, SwitchModeSignature + "A"); + ExecuteRawVoidMethod(Request); + + ResetDevice(); + + Dispose(true); + } + + internal void SwitchToFlashAppContext() + { + CommonPhoneInfo info = ReadPhoneInfoCommon(); + + bool ModernFlashApp = info.VersionMajor >= 2; + + byte[] Request = new byte[7]; + ByteOperations.WriteAsciiString(Request, 0, SwitchModeSignature + "F"); // This will stop charging the phone + byte[] Response = ExecuteRawMethod(Request); + if (ByteOperations.ReadAsciiString(Response, 0, 4) == "NOKU") + { + throw new NotSupportedException(); + } + + UInt16 Error = (UInt16)((Response[6] << 8) + Response[7]); + if (Error > 0) + { + throw new NotSupportedException("SwitchToFlashAppContext: Error 0x" + Error.ToString("X4")); + } + + if (ModernFlashApp) + { + DisableRebootTimeOut(); + + InterfaceChanged(PhoneInterfaces.Lumia_Flash, _devicePath); + } + } + + + // + // Normal commands + // + /* NOKD */ + private const string DisableTimeoutsSignature = $"{Signature}D"; + /* NOKI */ + private const string HelloSignature = $"{Signature}I"; + /* NOKV */ + private const string InfoQuerySignature = $"{Signature}V"; + + internal FlashAppType GetFlashAppType() + { + byte[] Request = new byte[4]; + ByteOperations.WriteAsciiString(Request, 0, InfoQuerySignature); + byte[] Response = ExecuteRawMethod(Request); + if ((Response == null) || (ByteOperations.ReadAsciiString(Response, 0, 4) == "NOKU")) + { + throw new NotSupportedException(); + } + + return (FlashAppType)Response[5]; + } + + internal CommonPhoneInfo ReadPhoneInfoCommon() + { + // NOKH = Get Phone Info (IMEI and info from Product.dat) - Not available on some phones, like Lumia 640. + // NOKV = Info Query + + CommonPhoneInfo Result = CommonInfo; + + if (Result.State == PhoneInfoState.Empty) + { + byte[] Request = new byte[4]; + ByteOperations.WriteAsciiString(Request, 0, InfoQuerySignature); + byte[] Response = ExecuteRawMethod(Request); + if ((Response != null) && (ByteOperations.ReadAsciiString(Response, 0, 4) != "NOKU")) + { + Result.App = (FlashAppType)Response[5]; + Result.ProtocolVersionMajor = Response[6]; + Result.ProtocolVersionMinor = Response[7]; + Result.VersionMajor = Response[8]; + Result.VersionMinor = Response[9]; + } + + Result.State = PhoneInfoState.Basic; + } + + return Result; + } + + public void DisableRebootTimeOut() + { + byte[] Request = new byte[4]; + const string Header = DisableTimeoutsSignature; + Buffer.BlockCopy(System.Text.Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length); + ExecuteRawMethod(Request); + } + + internal void Hello() + { + byte[] Request = new byte[4]; + ByteOperations.WriteAsciiString(Request, 0, HelloSignature); + byte[] Response = ExecuteRawMethod(Request); + if (Response == null) + { + throw new BadConnectionException(); + } + + if (ByteOperations.ReadAsciiString(Response, 0, 4) != HelloSignature) + { + throw new WPinternalsException("Bad response from phone!", "The phone did not answer properly to the Hello message sent."); + } + } + } +} diff --git a/WPinternals/TestCode.cs b/WPinternals/TestCode.cs index 89eec2e..5402e22 100644 --- a/WPinternals/TestCode.cs +++ b/WPinternals/TestCode.cs @@ -258,7 +258,7 @@ internal static async Task TestProgrammer(System.Threading.SynchronizationContex { LogFile.Log("Phone needs to be switched to emergency mode.", LogType.FileAndConsole); await SwitchModeViewModel.SwitchTo(Notifier, PhoneInterfaces.Lumia_Flash); - PhoneInfo Info = ((LumiaFlashAppModel)Notifier.CurrentModel).ReadPhoneInfo(); + LumiaFlashAppPhoneInfo Info = ((LumiaFlashAppModel)Notifier.CurrentModel).ReadPhoneInfo(); Info.Log(LogType.ConsoleOnly); await SwitchModeViewModel.SwitchTo(Notifier, PhoneInterfaces.Qualcomm_Download); if (Notifier.CurrentInterface != PhoneInterfaces.Qualcomm_Download) diff --git a/WPinternals/ViewModels/DownloadsViewModel.cs b/WPinternals/ViewModels/DownloadsViewModel.cs index f97e4d8..8ccbd7e 100644 --- a/WPinternals/ViewModels/DownloadsViewModel.cs +++ b/WPinternals/ViewModels/DownloadsViewModel.cs @@ -531,39 +531,62 @@ internal override async void EvaluateViewState() if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Flash) { - PhoneInfo FlashAppInfo = ((LumiaFlashAppModel)Notifier.CurrentModel).ReadPhoneInfo(ExtendedInfo: true); + LumiaFlashAppPhoneInfo FlashAppInfo = ((LumiaFlashAppModel)Notifier.CurrentModel).ReadPhoneInfo(ExtendedInfo: true); FirmwareVersion = FlashAppInfo.Firmware; IsSwitchingInterface = true; - (Notifier.CurrentModel as LumiaFlashAppModel).SwitchToPhoneInfoAppContext(); - - if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_PhoneInfo) + try { - await Notifier.WaitForArrival(); - } + bool ModernFlashApp = ((LumiaFlashAppModel)Notifier.CurrentModel).ReadPhoneInfo().FlashAppProtocolVersionMajor >= 2; + if (ModernFlashApp) + { + ((LumiaFlashAppModel)Notifier.CurrentModel).SwitchToPhoneInfoAppContext(); + } + else + { + ((LumiaFlashAppModel)Notifier.CurrentModel).SwitchToPhoneInfoAppContextLegacy(); + } - if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_PhoneInfo) - { - throw new WPinternalsException("Unexpected Mode"); - } + if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_PhoneInfo) + { + await Notifier.WaitForArrival(); + } - LumiaPhoneInfoAppModel LumiaPhoneInfoModel = (LumiaPhoneInfoAppModel)Notifier.CurrentModel; - PhoneInfo Info = LumiaPhoneInfoModel.ReadPhoneInfo(); - ProductType = Info.Type; - OperatorCode = ""; - ProductCode = Info.ProductCode; + if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_PhoneInfo) + { + throw new WPinternalsException("Unexpected Mode"); + } - ((LumiaPhoneInfoAppModel)Notifier.CurrentModel).SwitchToFlashAppContext(); + LumiaPhoneInfoAppModel LumiaPhoneInfoModel = (LumiaPhoneInfoAppModel)Notifier.CurrentModel; + LumiaPhoneInfoAppPhoneInfo Info = LumiaPhoneInfoModel.ReadPhoneInfo(); + ProductType = Info.Type; + OperatorCode = ""; + ProductCode = Info.ProductCode; - if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash) - { - await Notifier.WaitForArrival(); - } + ModernFlashApp = Info.PhoneInfoAppVersionMajor >= 2; + if (ModernFlashApp) + { + LumiaPhoneInfoModel.SwitchToFlashAppContext(); + } + else + { + LumiaPhoneInfoModel.ContinueBoot(); + } - if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash) + if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash) + { + await Notifier.WaitForArrival(); + } + + if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash) + { + throw new WPinternalsException("Unexpected Mode"); + } + } + catch (Exception ex) { - throw new WPinternalsException("Unexpected Mode"); + LogFile.LogException(ex); } IsSwitchingInterface = false; @@ -571,14 +594,22 @@ internal override async void EvaluateViewState() else if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_PhoneInfo) { LumiaPhoneInfoAppModel LumiaPhoneInfoModel = (LumiaPhoneInfoAppModel)Notifier.CurrentModel; - PhoneInfo Info = LumiaPhoneInfoModel.ReadPhoneInfo(); + LumiaPhoneInfoAppPhoneInfo Info = LumiaPhoneInfoModel.ReadPhoneInfo(); ProductType = Info.Type; OperatorCode = ""; ProductCode = Info.ProductCode; IsSwitchingInterface = true; - ((LumiaPhoneInfoAppModel)Notifier.CurrentModel).SwitchToFlashAppContext(); + bool ModernFlashApp = Info.PhoneInfoAppVersionMajor >= 2; + if (ModernFlashApp) + { + LumiaPhoneInfoModel.SwitchToFlashAppContext(); + } + else + { + LumiaPhoneInfoModel.ContinueBoot(); + } if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash) { @@ -590,10 +621,18 @@ internal override async void EvaluateViewState() throw new WPinternalsException("Unexpected Mode"); } - PhoneInfo FlashAppInfo = ((LumiaFlashAppModel)Notifier.CurrentModel).ReadPhoneInfo(ExtendedInfo: true); + LumiaFlashAppPhoneInfo FlashAppInfo = ((LumiaFlashAppModel)Notifier.CurrentModel).ReadPhoneInfo(ExtendedInfo: true); FirmwareVersion = FlashAppInfo.Firmware; - (Notifier.CurrentModel as LumiaFlashAppModel).SwitchToPhoneInfoAppContext(); + ModernFlashApp = FlashAppInfo.FlashAppProtocolVersionMajor >= 2; + if (ModernFlashApp) + { + ((LumiaFlashAppModel)Notifier.CurrentModel).SwitchToPhoneInfoAppContext(); + } + else + { + ((LumiaFlashAppModel)Notifier.CurrentModel).SwitchToPhoneInfoAppContextLegacy(); + } if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_PhoneInfo) { @@ -619,6 +658,8 @@ internal override async void EvaluateViewState() ProductType = TempProductType; ProductCode = LumiaNormalModel.ExecuteJsonMethodAsString("ReadProductCode", "ProductCode"); // 059Q9D7 + + FirmwareVersion = LumiaNormalModel.ExecuteJsonMethodAsString("ReadSwVersion", "SwVersion"); } } public DelegateCommand AddFFUCommand { get; } = null; diff --git a/WPinternals/ViewModels/LumiaFlashRomViewModel.cs b/WPinternals/ViewModels/LumiaFlashRomViewModel.cs index 3c0be57..686b30e 100644 --- a/WPinternals/ViewModels/LumiaFlashRomViewModel.cs +++ b/WPinternals/ViewModels/LumiaFlashRomViewModel.cs @@ -506,7 +506,7 @@ internal void FlashFFUTask(string FFUPath) bool Result = true; LumiaFlashAppModel Phone = (LumiaFlashAppModel)PhoneNotifier.CurrentModel; - PhoneInfo Info = Phone.ReadPhoneInfo(false); + LumiaFlashAppPhoneInfo Info = Phone.ReadPhoneInfo(false); #region Remove bootloader changes diff --git a/WPinternals/ViewModels/LumiaUnlockBootViewModel.cs b/WPinternals/ViewModels/LumiaUnlockBootViewModel.cs index afc55ba..8436f7b 100644 --- a/WPinternals/ViewModels/LumiaUnlockBootViewModel.cs +++ b/WPinternals/ViewModels/LumiaUnlockBootViewModel.cs @@ -145,7 +145,7 @@ internal override void EvaluateViewState() TestPos = 2; - PhoneInfo Info = ((LumiaFlashAppModel)PhoneNotifier.CurrentModel).ReadPhoneInfo(); + LumiaFlashAppPhoneInfo Info = ((LumiaFlashAppModel)PhoneNotifier.CurrentModel).ReadPhoneInfo(); if (SecurityStatus == null) { IsBootLoaderUnlocked = !Info.IsBootloaderSecure; @@ -297,11 +297,11 @@ void ReturnFunction(string FFUPath, string LoadersPath, string SBL3Path, string if (DoUnlock) { - ActivateSubContext(new BootUnlockResourcesViewModel("Lumia Flash mode", RootKeyHash, SwitchToFlashRom, SwitchToUndoRoot, SwitchToDownload, ReturnFunction, Abort, IsBootLoaderUnlocked, true, Info.PlatformID, Info.Type)); + ActivateSubContext(new BootUnlockResourcesViewModel("Lumia Flash mode", RootKeyHash, SwitchToFlashRom, SwitchToUndoRoot, SwitchToDownload, ReturnFunction, Abort, IsBootLoaderUnlocked, true, Info.PlatformID, /*TODO FIXME: Info.Type*/"")); } else { - ActivateSubContext(new BootRestoreResourcesViewModel("Lumia Flash mode", RootKeyHash, SwitchToFlashRom, SwitchToUndoRoot, SwitchToDownload, ReturnFunction, Abort, IsBootLoaderUnlocked, true, Info.PlatformID, Info.Type)); + ActivateSubContext(new BootRestoreResourcesViewModel("Lumia Flash mode", RootKeyHash, SwitchToFlashRom, SwitchToUndoRoot, SwitchToDownload, ReturnFunction, Abort, IsBootLoaderUnlocked, true, Info.PlatformID, /*TODO FIXME: Info.Type*/"")); } } } @@ -511,7 +511,7 @@ private void StorePaths() if (PhoneNotifier.CurrentInterface != PhoneInterfaces.Qualcomm_Download && PhoneNotifier.CurrentInterface != PhoneInterfaces.Qualcomm_Flash) { LumiaFlashAppModel Model = (LumiaFlashAppModel)PhoneNotifier.CurrentModel; - PhoneInfo Info = Model.ReadPhoneInfo(); + LumiaFlashAppPhoneInfo Info = Model.ReadPhoneInfo(); if (EDEPath == null) { @@ -524,7 +524,7 @@ private void StorePaths() { Key.SetValue("EDEPath", EDEPath); - App.Config.AddEmergencyToRepository(Info.Type, EDEPath, null); + App.Config.AddEmergencyToRepository(/*TODO FIXME: Info.Type*/"", EDEPath, null); } } diff --git a/WPinternals/ViewModels/LumiaUnlockBootloaderViewModel.cs b/WPinternals/ViewModels/LumiaUnlockBootloaderViewModel.cs index 022f51f..9e8cc11 100644 --- a/WPinternals/ViewModels/LumiaUnlockBootloaderViewModel.cs +++ b/WPinternals/ViewModels/LumiaUnlockBootloaderViewModel.cs @@ -1510,7 +1510,7 @@ internal static async Task LumiaRelockUEFI(PhoneNotifierViewModel Notifier, stri byte[] EFIESPBackup = null; - PhoneInfo Info = ((LumiaFlashAppModel)Notifier.CurrentModel).ReadPhoneInfo(); + LumiaFlashAppPhoneInfo Info = ((LumiaFlashAppModel)Notifier.CurrentModel).ReadPhoneInfo(); bool IsSpecB = Info.FlashAppProtocolVersionMajor >= 2; bool UndoEFIESPPadding = false; @@ -1862,7 +1862,7 @@ internal static async Task LumiaUnlockUEFI(PhoneNotifierViewModel Notifier, stri try { - PhoneInfo Info = FlashModel.ReadPhoneInfo(); + LumiaFlashAppPhoneInfo Info = FlashModel.ReadPhoneInfo(); bool IsSpecB = Info.FlashAppProtocolVersionMajor >= 2; if (ProfileFFUPath == null) @@ -2589,7 +2589,7 @@ private static UInt16 LumiaGetFirstEFIESPSectorCount(GPT DeviceGPT, FFU DeviceFF private static async Task LumiaFlashParts(PhoneNotifierViewModel Notifier, string FFUPath, bool PerformFullFlashFirst, bool SkipWrite, List Parts, bool DoResetFirst = true, bool ClearFlashingStatusAtEnd = true, bool CheckSectorAlignment = true, bool ShowProgress = true, bool Experimental = false, SetWorkingStatus SetWorkingStatus = null, UpdateWorkingStatus UpdateWorkingStatus = null, ExitSuccess ExitSuccess = null, ExitFailure ExitFailure = null, string EDEPath = null) { - PhoneInfo Info = ((LumiaFlashAppModel)Notifier.CurrentModel).ReadPhoneInfo(); + LumiaFlashAppPhoneInfo Info = ((LumiaFlashAppModel)Notifier.CurrentModel).ReadPhoneInfo(); bool IsSpecA = Info.FlashAppProtocolVersionMajor < 2; if (IsSpecA) diff --git a/WPinternals/ViewModels/LumiaV2UnlockBootViewModel.cs b/WPinternals/ViewModels/LumiaV2UnlockBootViewModel.cs index 202deab..101f0b7 100644 --- a/WPinternals/ViewModels/LumiaV2UnlockBootViewModel.cs +++ b/WPinternals/ViewModels/LumiaV2UnlockBootViewModel.cs @@ -59,7 +59,7 @@ internal static async Task LumiaV2FindFlashingProfile(PhoneNotifierViewModel Not LumiaFlashAppModel FlashModel = (LumiaFlashAppModel)await SwitchModeViewModel.SwitchTo(Notifier, PhoneInterfaces.Lumia_Flash); - PhoneInfo Info; + LumiaFlashAppPhoneInfo Info; if (DoResetFirst) { // The phone will be reset before flashing, so we have the opportunity to get some more info from the phone @@ -232,7 +232,7 @@ internal static async Task LumiaV2SwitchToMassStorageMode(PhoneNotifierV if (DoResetFirst) { // The phone will be reset before flashing, so we have the opportunity to get some more info from the phone - PhoneInfo Info = FlashModel.ReadPhoneInfo(); + LumiaFlashAppPhoneInfo Info = FlashModel.ReadPhoneInfo(); Info.Log(LogType.ConsoleOnly); } @@ -338,7 +338,7 @@ internal static async Task LumiaV2FlashPartition(System.Threading.Synchronizatio LumiaFlashAppModel FlashModel = (LumiaFlashAppModel)await SwitchModeViewModel.SwitchTo(Notifier, PhoneInterfaces.Lumia_Flash); - PhoneInfo Info = FlashModel.ReadPhoneInfo(); + LumiaFlashAppPhoneInfo Info = FlashModel.ReadPhoneInfo(); // Use GetGptChunk() here instead of ReadGPT(), because ReadGPT() skips the first sector. // We need the fist sector if we want to write back the GPT. @@ -447,7 +447,7 @@ internal static async Task LumiaV2FlashRaw(System.Threading.SynchronizationConte LumiaFlashAppModel FlashModel = (LumiaFlashAppModel)await SwitchModeViewModel.SwitchTo(Notifier, PhoneInterfaces.Lumia_Flash); - PhoneInfo Info = FlashModel.ReadPhoneInfo(); + LumiaFlashAppPhoneInfo Info = FlashModel.ReadPhoneInfo(); byte[] Data = File.ReadAllBytes(DataPath); @@ -484,7 +484,7 @@ internal async static Task LumiaV2CustomFlash(PhoneNotifierViewModel Notifier, s internal async static Task LumiaV2CustomFlash(PhoneNotifierViewModel Notifier, string FFUPath, bool PerformFullFlashFirst, bool SkipWrite, List FlashParts, bool DoResetFirst = true, bool ClearFlashingStatusAtEnd = true, bool CheckSectorAlignment = true, bool ShowProgress = true, bool Experimental = false, SetWorkingStatus SetWorkingStatus = null, UpdateWorkingStatus UpdateWorkingStatus = null, ExitSuccess ExitSuccess = null, ExitFailure ExitFailure = null, string ProgrammerPath = null) { LumiaFlashAppModel Model = (LumiaFlashAppModel)Notifier.CurrentModel; - PhoneInfo Info = Model.ReadPhoneInfo(); + LumiaFlashAppPhoneInfo Info = Model.ReadPhoneInfo(); byte[] GPTChunk = Model.GetGptChunk(131072u); @@ -555,9 +555,9 @@ internal async static Task LumiaV2CustomFlashInternal(PhoneNotifierViewModel Not } LumiaFlashAppModel Model = (LumiaFlashAppModel)Notifier.CurrentModel; - PhoneInfo Info = Model.ReadPhoneInfo(); + LumiaFlashAppPhoneInfo Info = Model.ReadPhoneInfo(); - string Type = Info.Type; + string Type = /*TODO: FIXME: Info.Type*/""; if (ProgrammerPath == null) { ProgrammerPath = GetProgrammerPath(Info.RKH, Type); @@ -1293,7 +1293,7 @@ .. GetNonOptimizedPayloads(FlashParts, FFU.ChunkSize, (uint)(Info.WriteBufferSiz if (!HeadersFull) { Step = 12; - App.Config.SetProfile(Info.Type, Info.PlatformID, Info.ProductCode, Info.Firmware, FFU.GetFirmwareVersion(), CurrentGapFill, ExploitHeaderAllocationSize, AssumeImageHeaderFallsInGap, AllocateAsyncBuffersOnPhone); + App.Config.SetProfile(/*TODO: FIXME: Info.Type*/"", Info.PlatformID, /*TODO: FIXME: Info.ProductCode*/"", Info.Firmware, FFU.GetFirmwareVersion(), CurrentGapFill, ExploitHeaderAllocationSize, AssumeImageHeaderFallsInGap, AllocateAsyncBuffersOnPhone); if (ShowProgress) { LogFile.Log("Custom flash succeeded!", LogType.FileAndConsole); @@ -2161,7 +2161,7 @@ internal async static Task LumiaV2FlashArchive(PhoneNotifierViewModel Notifier, GPTChanged = true; } - PhoneInfo Info = FlashModel.ReadPhoneInfo(false); + LumiaFlashAppPhoneInfo Info = FlashModel.ReadPhoneInfo(false); // 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) @@ -2520,7 +2520,7 @@ internal async static Task LumiaV2FlashPartitions(PhoneNotifierViewModel Notifie GPTChanged = true; } - PhoneInfo Info = FlashModel.ReadPhoneInfo(false); + LumiaFlashAppPhoneInfo Info = FlashModel.ReadPhoneInfo(false); // 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) diff --git a/WPinternals/ViewModels/LumiaV3FlashRomViewModel.cs b/WPinternals/ViewModels/LumiaV3FlashRomViewModel.cs index d25601b..922bb03 100644 --- a/WPinternals/ViewModels/LumiaV3FlashRomViewModel.cs +++ b/WPinternals/ViewModels/LumiaV3FlashRomViewModel.cs @@ -193,7 +193,7 @@ internal async static Task LumiaV3CustomFlash(PhoneNotifierViewModel Notifier, L try { LumiaFlashAppModel Model = (LumiaFlashAppModel)Notifier.CurrentModel; - PhoneInfo Info = Model.ReadPhoneInfo(); + LumiaFlashAppPhoneInfo Info = Model.ReadPhoneInfo(); if ((Info.SecureFfuSupportedProtocolMask & ((ushort)FfuProtocol.ProtocolSyncV2)) == 0) // Exploit needs protocol v2 -> This check is not conclusive, because old phones also report support for this protocol, although it is really not supported. { diff --git a/WPinternals/ViewModels/NokiaFlashViewModel.cs b/WPinternals/ViewModels/NokiaFlashViewModel.cs index a85c367..4959afb 100644 --- a/WPinternals/ViewModels/NokiaFlashViewModel.cs +++ b/WPinternals/ViewModels/NokiaFlashViewModel.cs @@ -222,15 +222,15 @@ private void StartLoadDeviceInfo() LogFile.Log("Charging status: " + ChargingStatus); } - PhoneInfo Info = CurrentModel.ReadPhoneInfo(true); + LumiaFlashAppPhoneInfo Info = CurrentModel.ReadPhoneInfo(true); BootloaderDescription = Info.FlashAppProtocolVersionMajor < 2 ? "Lumia Bootloader Spec A" : "Lumia Bootloader Spec B"; LogFile.Log("Bootloader: " + BootloaderDescription); - ProductCode = Info.ProductCode; + ProductCode = "";//TODO: FIXME: Info.ProductCode; LogFile.Log("ProductCode: " + ProductCode); - ProductType = Info.Type; + ProductType = "";//TODO: FIXME: Info.Type; LogFile.Log("ProductType: " + ProductType); if (RootKeyHash == null) diff --git a/WPinternals/ViewModels/NokiaModeBootloaderViewModel.cs b/WPinternals/ViewModels/NokiaModeBootloaderViewModel.cs index c6950ce..381f958 100644 --- a/WPinternals/ViewModels/NokiaModeBootloaderViewModel.cs +++ b/WPinternals/ViewModels/NokiaModeBootloaderViewModel.cs @@ -67,9 +67,9 @@ internal void StartLoadDeviceInfo() { try { - PhoneInfo Info = CurrentModel.ReadPhoneInfo(); + LumiaBootManagerPhoneInfo Info = CurrentModel.ReadPhoneInfo(); - EffectiveBootloaderSecurityStatus = Info.UefiSecureBootEnabled; + //EffectiveBootloaderSecurityStatus = Info.UefiSecureBootEnabled; // FIXME LogFile.Log("Effective Bootloader Security Status: " + EffectiveBootloaderSecurityStatus.ToString()); } diff --git a/WPinternals/ViewModels/NokiaModeFlashViewModel.cs b/WPinternals/ViewModels/NokiaModeFlashViewModel.cs index 01f1fa6..0afb4df 100644 --- a/WPinternals/ViewModels/NokiaModeFlashViewModel.cs +++ b/WPinternals/ViewModels/NokiaModeFlashViewModel.cs @@ -67,7 +67,7 @@ internal void StartLoadDeviceInfo() { try { - PhoneInfo Info = CurrentModel.ReadPhoneInfo(); + LumiaFlashAppPhoneInfo Info = CurrentModel.ReadPhoneInfo(); if (Info.FlashAppProtocolVersionMajor < 2) { diff --git a/WPinternals/ViewModels/NokiaModePhoneInfoViewModel.cs b/WPinternals/ViewModels/NokiaModePhoneInfoViewModel.cs index 48ebd8f..8e2e9d7 100644 --- a/WPinternals/ViewModels/NokiaModePhoneInfoViewModel.cs +++ b/WPinternals/ViewModels/NokiaModePhoneInfoViewModel.cs @@ -67,9 +67,9 @@ internal void StartLoadDeviceInfo() { try { - PhoneInfo Info = CurrentModel.ReadPhoneInfo(); + LumiaPhoneInfoAppPhoneInfo Info = CurrentModel.ReadPhoneInfo(); - EffectivePhoneInfoSecurityStatus = Info.UefiSecureBootEnabled; + //EffectivePhoneInfoSecurityStatus = Info.UefiSecureBootEnabled; // FIXME LogFile.Log("Effective Bootloader Security Status: " + EffectivePhoneInfoSecurityStatus.ToString()); } diff --git a/WPinternals/ViewModels/NokiaPhoneInfoViewModel.cs b/WPinternals/ViewModels/NokiaPhoneInfoViewModel.cs index a2a7e29..fd176ca 100644 --- a/WPinternals/ViewModels/NokiaPhoneInfoViewModel.cs +++ b/WPinternals/ViewModels/NokiaPhoneInfoViewModel.cs @@ -92,8 +92,8 @@ private void StartLoadDeviceInfo() LogFile.Log("HWID: " + HWID); } - PhoneInfo Info = CurrentModel.ReadPhoneInfo(true); - BootloaderDescription = Info.FlashAppProtocolVersionMajor < 2 ? "Lumia Bootloader Spec A" : "Lumia Bootloader Spec B"; + LumiaPhoneInfoAppPhoneInfo Info = CurrentModel.ReadPhoneInfo(true); + BootloaderDescription = Info.PhoneInfoAppVersionMajor < 2 ? "Lumia Bootloader Spec A" : "Lumia Bootloader Spec B"; LogFile.Log("Bootloader: " + BootloaderDescription); diff --git a/WPinternals/ViewModels/SwitchModeViewModel.cs b/WPinternals/ViewModels/SwitchModeViewModel.cs index 152575b..e5f8f21 100644 --- a/WPinternals/ViewModels/SwitchModeViewModel.cs +++ b/WPinternals/ViewModels/SwitchModeViewModel.cs @@ -82,7 +82,7 @@ internal SwitchModeViewModel(PhoneNotifierViewModel PhoneNotifier, PhoneInterfac { if ((PhoneNotifier.CurrentInterface == PhoneInterfaces.Lumia_Bootloader) && (TargetMode == PhoneInterfaces.Lumia_Flash)) { - PhoneInfo Info = ((LumiaBootManagerAppModel)PhoneNotifier.CurrentModel).ReadPhoneInfo(false); + LumiaBootManagerPhoneInfo Info = ((LumiaBootManagerAppModel)PhoneNotifier.CurrentModel).ReadPhoneInfo(false); if (Info.BootManagerProtocolVersionMajor >= 2) { try @@ -293,7 +293,7 @@ internal void StartSwitch() Params.Add("ResetMethod", "HwReset"); try { - ((NokiaPhoneModel)CurrentModel).ExecuteJsonMethodAsync("SetDeviceMode", Params); + ((NokiaPhoneModel)PhoneNotifier.CurrentModel).ExecuteJsonMethodAsync("SetDeviceMode", Params); PhoneNotifier.NewDeviceArrived += NewDeviceArrived; } catch (Exception Ex) @@ -308,7 +308,7 @@ internal void StartSwitch() switch (TargetMode) { case null: - ((LumiaFlashAppModel)CurrentModel).Shutdown(); + ((LumiaFlashAppModel)PhoneNotifier.CurrentModel).Shutdown(); ModeSwitchProgressWrapper("Please disconnect your device. Waiting...", null); LogFile.Log("Please disconnect your device. Waiting...", LogType.FileAndConsole); new Thread(() => @@ -319,26 +319,26 @@ internal void StartSwitch() break; case PhoneInterfaces.Lumia_Normal: PhoneNotifier.NewDeviceArrived += NewDeviceArrived; - ((LumiaFlashAppModel)CurrentModel).ExecuteRawVoidMethod(RebootCommand); + ((LumiaFlashAppModel)PhoneNotifier.CurrentModel).ExecuteRawVoidMethod(RebootCommand); ModeSwitchProgressWrapper("Rebooting phone to Normal mode...", null); LogFile.Log("Rebooting phone to Normal mode", LogType.FileAndConsole); break; case PhoneInterfaces.Lumia_Bootloader: PhoneNotifier.NewDeviceArrived += NewDeviceArrived; - ((LumiaFlashAppModel)CurrentModel).ExecuteRawVoidMethod(RebootCommand); + ((LumiaFlashAppModel)PhoneNotifier.CurrentModel).ExecuteRawVoidMethod(RebootCommand); ModeSwitchProgressWrapper("Rebooting phone to Bootloader mode...", null); LogFile.Log("Rebooting phone to Bootloader mode", LogType.FileAndConsole); break; case PhoneInterfaces.Lumia_PhoneInfo: PhoneNotifier.NewDeviceArrived += NewDeviceArrived; - ModernFlashApp = ((LumiaFlashAppModel)CurrentModel).ReadPhoneInfo().FlashAppProtocolVersionMajor >= 2; + ModernFlashApp = ((LumiaFlashAppModel)PhoneNotifier.CurrentModel).ReadPhoneInfo().FlashAppProtocolVersionMajor >= 2; if (ModernFlashApp) { - ((LumiaFlashAppModel)CurrentModel).SwitchToPhoneInfoAppContext(); + ((LumiaFlashAppModel)PhoneNotifier.CurrentModel).SwitchToPhoneInfoAppContext(); } else { - ((LumiaFlashAppModel)CurrentModel).SwitchToPhoneInfoAppContextLegacy(); + ((LumiaFlashAppModel)PhoneNotifier.CurrentModel).SwitchToPhoneInfoAppContextLegacy(); } ModeSwitchProgressWrapper("Rebooting phone to Phone Info mode...", null); LogFile.Log("Rebooting phone to Phone Info mode", LogType.FileAndConsole); @@ -349,7 +349,7 @@ internal void StartSwitch() case PhoneInterfaces.Lumia_Flash: // attempt to boot from limited flash to full flash PhoneNotifier.NewDeviceArrived += NewDeviceArrived; byte[] RebootToFlashCommand = [0x4E, 0x4F, 0x4B, 0x53]; // NOKS - ((LumiaFlashAppModel)CurrentModel).ExecuteRawVoidMethod(RebootToFlashCommand); + ((LumiaFlashAppModel)PhoneNotifier.CurrentModel).ExecuteRawVoidMethod(RebootToFlashCommand); ModeSwitchProgressWrapper("Rebooting phone to Flash mode...", null); LogFile.Log("Rebooting phone to Flash mode", LogType.FileAndConsole); break; @@ -359,7 +359,7 @@ internal void StartSwitch() case PhoneInterfaces.Qualcomm_Download: PhoneNotifier.NewDeviceArrived += NewDeviceArrived; byte[] RebootToQualcommDownloadCommand = [0x4E, 0x4F, 0x4B, 0x58, 0x43, 0x42, 0x45]; // NOKXCBE // TODO - RebootCommandResult = ((LumiaFlashAppModel)CurrentModel).ExecuteRawMethod(RebootToQualcommDownloadCommand); + RebootCommandResult = ((LumiaFlashAppModel)PhoneNotifier.CurrentModel).ExecuteRawMethod(RebootToQualcommDownloadCommand); if (RebootCommandResult?.Length == 4) // This means fail: NOKU (unknow command) { IsSwitchingInterface = false; @@ -381,26 +381,26 @@ internal void StartSwitch() { case PhoneInterfaces.Lumia_Normal: PhoneNotifier.NewDeviceArrived += NewDeviceArrived; - ((LumiaPhoneInfoAppModel)CurrentModel).ExecuteRawVoidMethod(RebootCommand); + ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).ExecuteRawVoidMethod(RebootCommand); ModeSwitchProgressWrapper("Rebooting phone to Normal mode...", null); LogFile.Log("Rebooting phone to Normal mode", LogType.FileAndConsole); break; case PhoneInterfaces.Lumia_Bootloader: PhoneNotifier.NewDeviceArrived += NewDeviceArrived; - ModernFlashApp = ((LumiaPhoneInfoAppModel)CurrentModel).ReadPhoneInfo().PhoneInfoAppVersionMajor >= 2; + ModernFlashApp = ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).ReadPhoneInfo().PhoneInfoAppVersionMajor >= 2; if (ModernFlashApp) { - ((LumiaPhoneInfoAppModel)CurrentModel).SwitchToBootManagerContext(); + ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).SwitchToBootManagerContext(); } ModeSwitchProgressWrapper("Rebooting phone to Bootloader mode...", null); LogFile.Log("Rebooting phone to Bootloader mode", LogType.FileAndConsole); break; case PhoneInterfaces.Lumia_PhoneInfo: // attempt to boot from limited phone info to full phone info PhoneNotifier.NewDeviceArrived += NewDeviceArrived; - ModernFlashApp = ((LumiaPhoneInfoAppModel)CurrentModel).ReadPhoneInfo().PhoneInfoAppVersionMajor >= 2; + ModernFlashApp = ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).ReadPhoneInfo().PhoneInfoAppVersionMajor >= 2; if (ModernFlashApp) { - ((LumiaPhoneInfoAppModel)CurrentModel).SwitchToPhoneInfoAppContext(); + ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).SwitchToPhoneInfoAppContext(); } ModeSwitchProgressWrapper("Rebooting phone to Phone Info mode...", null); LogFile.Log("Rebooting phone to Phone Info mode", LogType.FileAndConsole); @@ -410,14 +410,14 @@ internal void StartSwitch() break; case PhoneInterfaces.Lumia_Flash: PhoneNotifier.NewDeviceArrived += NewDeviceArrived; - ModernFlashApp = ((LumiaPhoneInfoAppModel)CurrentModel).ReadPhoneInfo().PhoneInfoAppVersionMajor >= 2; + ModernFlashApp = ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).ReadPhoneInfo().PhoneInfoAppVersionMajor >= 2; if (ModernFlashApp) { - ((LumiaPhoneInfoAppModel)CurrentModel).SwitchToFlashAppContext(); + ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).SwitchToFlashAppContext(); } else { - ((LumiaPhoneInfoAppModel)CurrentModel).ContinueBoot(); + ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).ContinueBoot(); } ModeSwitchProgressWrapper("Rebooting phone to Flash mode...", null); LogFile.Log("Rebooting phone to Flash mode", LogType.FileAndConsole); @@ -428,7 +428,7 @@ internal void StartSwitch() case PhoneInterfaces.Qualcomm_Download: PhoneNotifier.NewDeviceArrived += NewDeviceArrived; byte[] RebootToQualcommDownloadCommand = [0x4E, 0x4F, 0x4B, 0x58, 0x43, 0x42, 0x45]; // NOKXCBE // TODO - RebootCommandResult = ((LumiaPhoneInfoAppModel)CurrentModel).ExecuteRawMethod(RebootToQualcommDownloadCommand); + RebootCommandResult = ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).ExecuteRawMethod(RebootToQualcommDownloadCommand); if (RebootCommandResult?.Length == 4) // This means fail: NOKU (unknow command) { IsSwitchingInterface = false; @@ -449,7 +449,7 @@ internal void StartSwitch() switch (TargetMode) { case null: - ((LumiaBootManagerAppModel)CurrentModel).Shutdown(); + ((LumiaBootManagerAppModel)PhoneNotifier.CurrentModel).Shutdown(); ModeSwitchProgressWrapper("Please disconnect your device. Waiting...", null); LogFile.Log("Please disconnect your device. Waiting...", LogType.FileAndConsole); new Thread(() => @@ -460,13 +460,13 @@ internal void StartSwitch() break; case PhoneInterfaces.Lumia_Normal: PhoneNotifier.NewDeviceArrived += NewDeviceArrived; - ((LumiaBootManagerAppModel)CurrentModel).ExecuteRawVoidMethod(RebootCommand); + ((LumiaBootManagerAppModel)PhoneNotifier.CurrentModel).ExecuteRawVoidMethod(RebootCommand); ModeSwitchProgressWrapper("Rebooting phone to Normal mode...", null); LogFile.Log("Rebooting phone to Normal mode", LogType.FileAndConsole); break; case PhoneInterfaces.Lumia_Bootloader: PhoneNotifier.NewDeviceArrived += NewDeviceArrived; - ((LumiaBootManagerAppModel)CurrentModel).ExecuteRawVoidMethod(RebootCommand); + ((LumiaBootManagerAppModel)PhoneNotifier.CurrentModel).ExecuteRawVoidMethod(RebootCommand); ModeSwitchProgressWrapper("Rebooting phone to Bootloader mode...", null); LogFile.Log("Rebooting phone to Bootloader mode", LogType.FileAndConsole); break; @@ -476,7 +476,7 @@ internal void StartSwitch() case PhoneInterfaces.Lumia_Flash: // attempt to boot from limited flash to full flash PhoneNotifier.NewDeviceArrived += NewDeviceArrived; byte[] RebootToFlashCommand = [0x4E, 0x4F, 0x4B, 0x53]; // NOKS - ((LumiaBootManagerAppModel)CurrentModel).ExecuteRawVoidMethod(RebootToFlashCommand); + ((LumiaBootManagerAppModel)PhoneNotifier.CurrentModel).ExecuteRawVoidMethod(RebootToFlashCommand); ModeSwitchProgressWrapper("Rebooting phone to Flash mode...", null); LogFile.Log("Rebooting phone to Flash mode", LogType.FileAndConsole); break; @@ -486,7 +486,7 @@ internal void StartSwitch() case PhoneInterfaces.Qualcomm_Download: PhoneNotifier.NewDeviceArrived += NewDeviceArrived; byte[] RebootToQualcommDownloadCommand = [0x4E, 0x4F, 0x4B, 0x58, 0x43, 0x42, 0x45]; // NOKXCBE // TODO - RebootCommandResult = ((LumiaBootManagerAppModel)CurrentModel).ExecuteRawMethod(RebootToQualcommDownloadCommand); + RebootCommandResult = ((LumiaBootManagerAppModel)PhoneNotifier.CurrentModel).ExecuteRawMethod(RebootToQualcommDownloadCommand); if (RebootCommandResult?.Length == 4) // This means fail: NOKU (unknow command) { IsSwitchingInterface = false; @@ -508,25 +508,25 @@ internal void StartSwitch() { case PhoneInterfaces.Lumia_Normal: PhoneNotifier.NewDeviceArrived += NewDeviceArrived; - ((MassStorage)CurrentModel).Reboot(); + ((MassStorage)PhoneNotifier.CurrentModel).Reboot(); ModeSwitchProgressWrapper("Rebooting phone to Normal mode...", null); LogFile.Log("Rebooting phone to Normal mode", LogType.FileAndConsole); break; case PhoneInterfaces.Lumia_Label: PhoneNotifier.NewDeviceArrived += NewDeviceArrivedFromMassStorageMode; - ((MassStorage)CurrentModel).Reboot(); + ((MassStorage)PhoneNotifier.CurrentModel).Reboot(); ModeSwitchProgressWrapper("Rebooting phone to Label mode...", null); LogFile.Log("Rebooting phone to Label mode...", LogType.FileAndConsole); break; case PhoneInterfaces.Lumia_Flash: PhoneNotifier.NewDeviceArrived += NewDeviceArrivedFromMassStorageMode; - ((MassStorage)CurrentModel).Reboot(); + ((MassStorage)PhoneNotifier.CurrentModel).Reboot(); ModeSwitchProgressWrapper("Rebooting phone to Flash mode...", null); LogFile.Log("Rebooting phone to Flash mode...", LogType.FileAndConsole); break; case null: PhoneNotifier.NewDeviceArrived += NewDeviceArrivedFromMassStorageMode; - ((MassStorage)CurrentModel).Reboot(); + ((MassStorage)PhoneNotifier.CurrentModel).Reboot(); ModeSwitchProgressWrapper("First rebooting phone to Flash mode...", null); LogFile.Log("First rebooting phone to Bootloader mode...", LogType.FileAndConsole); break; @@ -626,7 +626,7 @@ private void NewDeviceArrived(ArrivalEventArgs Args) // SwitchToFlashAppContext() will only switch context. Phone will not charge. // ResetPhoneToFlashMode() reboots to real flash app. Phone will charge. Works when in BootMgrApp, not when already in FlashApp. - ((LumiaBootManagerAppModel)CurrentModel).ResetPhoneToFlashMode(); + ((LumiaBootManagerAppModel)PhoneNotifier.CurrentModel).ResetPhoneToFlashMode(); CurrentMode = PhoneInterfaces.Lumia_Flash; PhoneNotifier.NotifyArrival(); } @@ -640,7 +640,7 @@ private void NewDeviceArrived(ArrivalEventArgs Args) { if (TargetMode == PhoneInterfaces.Lumia_Bootloader) { - ((NokiaFlashModel)CurrentModel).DisableRebootTimeOut(); + ((NokiaFlashModel)PhoneNotifier.CurrentModel).DisableRebootTimeOut(); } ModeSwitchSuccessWrapper(); @@ -669,7 +669,7 @@ private void NewDeviceArrived(ArrivalEventArgs Args) byte[] RebootToQualcommDownloadCommand = [0x4E, 0x4F, 0x4B, 0x58, 0x43, 0x42, 0x45]; // NOKXCBE // TODO IsSwitchingInterface = true; LogFile.Log("Sending command for rebooting to Emergency Download mode"); - byte[] RebootCommandResult = ((NokiaPhoneModel)CurrentModel).ExecuteRawMethod(RebootToQualcommDownloadCommand); + byte[] RebootCommandResult = ((NokiaPhoneModel)PhoneNotifier.CurrentModel).ExecuteRawMethod(RebootToQualcommDownloadCommand); if (RebootCommandResult?.Length >= 8) { int ResultCode = (RebootCommandResult[6] << 8) + RebootCommandResult[7]; @@ -722,11 +722,19 @@ private void SwitchFromPhoneInfoToLabelMode(bool Continuation = false) string ProgressText = Continuation ? "And now preparing to boot the phone to Label mode..." : "Preparing to boot the phone to Label mode..."; - PhoneInfo PhoneInfoAppInfo = ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).ReadPhoneInfo(ExtendedInfo: true); + LumiaPhoneInfoAppPhoneInfo PhoneInfoAppInfo = ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).ReadPhoneInfo(ExtendedInfo: true); new Thread(async () => { - ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).SwitchToFlashAppContext(); + bool ModernFlashApp = ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).ReadPhoneInfo().PhoneInfoAppVersionMajor >= 2; + if (ModernFlashApp) + { + ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).SwitchToFlashAppContext(); + } + else + { + ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).ContinueBoot(); + } if (PhoneNotifier.CurrentInterface != PhoneInterfaces.Lumia_Flash) { @@ -739,7 +747,7 @@ private void SwitchFromPhoneInfoToLabelMode(bool Continuation = false) } LumiaFlashAppModel FlashModel = (LumiaFlashAppModel)PhoneNotifier.CurrentModel; - PhoneInfo Info = FlashModel.ReadPhoneInfo(ExtendedInfo: true); + LumiaFlashAppPhoneInfo Info = FlashModel.ReadPhoneInfo(ExtendedInfo: true); if (Info.MmosOverUsbSupported) { @@ -760,7 +768,7 @@ private void SwitchFromPhoneInfoToLabelMode(bool Continuation = false) (string ENOSWFileUrl, string DPLFileUrl) = LumiaDownloadModel.SearchENOSW(PhoneInfoAppInfo.Type, Info.Firmware); - SetWorkingStatus($"Downloading {Info.Type} Test Mode package...", MaxProgressValue: 100); + SetWorkingStatus($"Downloading {PhoneInfoAppInfo.Type} Test Mode package...", MaxProgressValue: 100); DownloadEntry downloadEntry = new(ENOSWFileUrl, TempFolder, null, (string[] Files, object State) => { @@ -829,11 +837,19 @@ private void SwitchFromFlashToLabelMode(bool Continuation = false) throw new WPinternalsException("Unexpected Mode"); } - PhoneInfo Info = ((LumiaFlashAppModel)PhoneNotifier.CurrentModel).ReadPhoneInfo(ExtendedInfo: true); + LumiaFlashAppPhoneInfo Info = ((LumiaFlashAppModel)PhoneNotifier.CurrentModel).ReadPhoneInfo(ExtendedInfo: true); new Thread(async () => { - ((LumiaFlashAppModel)PhoneNotifier.CurrentModel).SwitchToPhoneInfoAppContext(); + bool ModernFlashApp = ((LumiaFlashAppModel)PhoneNotifier.CurrentModel).ReadPhoneInfo().FlashAppProtocolVersionMajor >= 2; + if (ModernFlashApp) + { + ((LumiaFlashAppModel)PhoneNotifier.CurrentModel).SwitchToPhoneInfoAppContext(); + } + else + { + ((LumiaFlashAppModel)PhoneNotifier.CurrentModel).SwitchToPhoneInfoAppContextLegacy(); + } if (PhoneNotifier.CurrentInterface != PhoneInterfaces.Lumia_PhoneInfo) { @@ -845,9 +861,18 @@ private void SwitchFromFlashToLabelMode(bool Continuation = false) throw new WPinternalsException("Unexpected Mode"); } - PhoneInfo PhoneInfoAppInfo = ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).ReadPhoneInfo(ExtendedInfo: true); + LumiaPhoneInfoAppModel LumiaPhoneInfoAppModel = (LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel; + LumiaPhoneInfoAppPhoneInfo PhoneInfoAppInfo = LumiaPhoneInfoAppModel.ReadPhoneInfo(ExtendedInfo: true); - ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).SwitchToFlashAppContext(); + ModernFlashApp = PhoneInfoAppInfo.PhoneInfoAppVersionMajor >= 2; + if (ModernFlashApp) + { + LumiaPhoneInfoAppModel.SwitchToFlashAppContext(); + } + else + { + LumiaPhoneInfoAppModel.ContinueBoot(); + } if (PhoneNotifier.CurrentInterface != PhoneInterfaces.Lumia_Flash) { @@ -946,11 +971,19 @@ private void SwitchFromPhoneInfoToMassStorageMode(bool Continuation = false) new Thread(async () => { - ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).SwitchToFlashAppContext(); + bool ModernFlashApp = ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).ReadPhoneInfo().PhoneInfoAppVersionMajor >= 2; + if (ModernFlashApp) + { + ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).SwitchToFlashAppContext(); + } + else + { + ((LumiaPhoneInfoAppModel)PhoneNotifier.CurrentModel).ContinueBoot(); + } await PhoneNotifier.WaitForArrival(); LumiaFlashAppModel FlashModel = (LumiaFlashAppModel)PhoneNotifier.CurrentModel; - PhoneInfo Info = FlashModel.ReadPhoneInfo(ExtendedInfo: false); + LumiaFlashAppPhoneInfo Info = FlashModel.ReadPhoneInfo(ExtendedInfo: false); MassStorageWarning = null; if (Info.FlashAppProtocolVersionMajor < 2) @@ -1159,7 +1192,7 @@ private void SwitchFromFlashToMassStorageMode(bool Continuation = false) } } LumiaFlashAppModel FlashModel = (LumiaFlashAppModel)PhoneNotifier.CurrentModel; - PhoneInfo Info = FlashModel.ReadPhoneInfo(ExtendedInfo: false); + LumiaFlashAppPhoneInfo Info = FlashModel.ReadPhoneInfo(ExtendedInfo: false); MassStorageWarning = null; if (Info.FlashAppProtocolVersionMajor < 2)