Skip to content

Commit

Permalink
Various Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gus33000 committed Oct 12, 2024
1 parent 303c2d4 commit 7c18a16
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 44 deletions.
4 changes: 2 additions & 2 deletions WPinternals/TestCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ internal static async Task RecoverBadGPT(string GPTPath, string LoadersPath)
byte[] RootKeyHash = null;
if (PhoneNotifier.CurrentInterface == PhoneInterfaces.Qualcomm_Download)
{
QualcommDownload Download2 = new((QualcommSerial)PhoneNotifier.CurrentModel);
RootKeyHash = Download2.GetRKH();
RootKeyHash = new QualcommDownload((QualcommSerial)PhoneNotifier.CurrentModel).GetRKH();
}

List<QualcommPartition> PossibleLoaders = null;
Expand All @@ -76,6 +75,7 @@ internal static async Task RecoverBadGPT(string GPTPath, string LoadersPath)

QualcommSerial Serial = (QualcommSerial)PhoneNotifier.CurrentModel;
QualcommDownload Download = new(Serial);

if (Download.IsAlive())
{
int Attempt = 1;
Expand Down
71 changes: 66 additions & 5 deletions WPinternals/ViewModels/LumiaUnlockBootloaderViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,16 @@ await LumiaRelockUEFI(Notifier, FFUPath, true, SetWorkingStatus, UpdateWorkingSt
if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Bootloader)
{
await SwitchModeViewModel.SwitchTo(Notifier, PhoneInterfaces.Lumia_Flash);

if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash)
{
await Notifier.WaitForArrival();
}

if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash)
{
throw new WPinternalsException("Unexpected Mode");
}
}
}

Expand Down Expand Up @@ -811,6 +821,16 @@ await LumiaRelockUEFI(Notifier, FFUPath, true, SetWorkingStatus, UpdateWorkingSt
if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Bootloader)
{
await SwitchModeViewModel.SwitchToWithStatus(Notifier, PhoneInterfaces.Lumia_Flash, SetWorkingStatus, UpdateWorkingStatus);

if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash)
{
await Notifier.WaitForArrival();
}

if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash)
{
throw new WPinternalsException("Unexpected Mode");
}
}
}
else
Expand Down Expand Up @@ -1478,6 +1498,16 @@ internal static async Task LumiaV1UnlockFirmware(PhoneNotifierViewModel Notifier
if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Bootloader)
{
await SwitchModeViewModel.SwitchToWithStatus(Notifier, PhoneInterfaces.Lumia_Flash, SetWorkingStatus, UpdateWorkingStatus);

if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash)
{
await Notifier.WaitForArrival();
}

if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash)
{
throw new WPinternalsException("Unexpected Mode");
}
}

// phone is in flash mode, we can exit
Expand Down Expand Up @@ -1725,14 +1755,17 @@ internal static async Task LumiaRelockUEFI(PhoneNotifierViewModel Notifier, stri
await SwitchModeViewModel.SwitchToWithStatus(Notifier, PhoneInterfaces.Lumia_Flash, SetWorkingStatus, UpdateWorkingStatus);
}

if ((Notifier.CurrentInterface != PhoneInterfaces.Lumia_Bootloader) && (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash))
if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash)
{
await Notifier.WaitForArrival();
}

SetWorkingStatus("Flashing...", "The phone may reboot a couple of times. Just wait for it.", null, Status: WPinternalsStatus.Initializing);
if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash)
{
throw new WPinternalsException("Unexpected Mode");
}

((LumiaBootManagerAppModel)Notifier.CurrentModel).SwitchToFlashAppContext();
SetWorkingStatus("Flashing...", "The phone may reboot a couple of times. Just wait for it.", null, Status: WPinternalsStatus.Initializing);

List<FlashPart> FlashParts = [];

Expand Down Expand Up @@ -1871,12 +1904,19 @@ await LumiaFlashParts(Notifier, FFUPath, false, false, FlashParts, DoResetFirst,
if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Bootloader)
{
((LumiaBootManagerAppModel)Notifier.CurrentModel).SwitchToFlashAppContext();

if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash)
{
await Notifier.WaitForArrival();
}
}

if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Flash)
if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash)
{
await LumiaFlashParts(Notifier, FFUPath, false, false, null, DoResetFirst, ClearFlashingStatusAtEnd: true, ShowProgress: false);
throw new WPinternalsException("Unexpected Mode");
}

await LumiaFlashParts(Notifier, FFUPath, false, false, null, DoResetFirst, ClearFlashingStatusAtEnd: true, ShowProgress: false);
}

LogFile.Log("Phone is relocked", LogType.FileAndConsole);
Expand Down Expand Up @@ -2265,6 +2305,17 @@ internal static async Task LumiaUnlockUEFI(PhoneNotifierViewModel Notifier, stri

SetWorkingStatus("Problem detected, rolling back...", ErrorMessage);
await SwitchModeViewModel.SwitchTo(Notifier, PhoneInterfaces.Lumia_Flash);

if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash)
{
await Notifier.WaitForArrival();
}

if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash)
{
throw new WPinternalsException("Unexpected Mode");
}

Parts = [];

// Restore original GPT, which will also reference the original NV.
Expand Down Expand Up @@ -2495,6 +2546,16 @@ internal static async Task LumiaUnlockUEFI(PhoneNotifierViewModel Notifier, stri
{
SetWorkingStatus("Rebooting phone...");
await SwitchModeViewModel.SwitchTo(Notifier, PhoneInterfaces.Lumia_Flash);

if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash)
{
await Notifier.WaitForArrival();
}

if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash)
{
throw new WPinternalsException("Unexpected Mode");
}
}
else
{
Expand Down
18 changes: 16 additions & 2 deletions WPinternals/ViewModels/LumiaV2UnlockBootViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1564,8 +1564,22 @@ .. GetNonOptimizedPayloads(FlashParts, FFU.ChunkSize, (uint)(FlashInfo.WriteBuff
break;
}

// In case we are on an Engineering phone which isn't stuck in flashmode and booted to BootMgrApp
((LumiaBootManagerAppModel)Notifier.CurrentModel).SwitchToFlashAppContext();
if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Bootloader)
{
// In case we are on an Engineering phone which isn't stuck in flashmode and booted to BootMgrApp
((LumiaBootManagerAppModel)Notifier.CurrentModel).SwitchToFlashAppContext();

if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash)
{
await Notifier.WaitForArrival();
}
}

if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash)
{
throw new WPinternalsException("Unexpected Mode");
}

((LumiaFlashAppModel)Notifier.CurrentModel).DisableRebootTimeOut();
}

Expand Down
14 changes: 7 additions & 7 deletions WPinternals/ViewModels/LumiaV3FlashRomViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ internal async static Task LumiaV3CustomFlash(PhoneNotifierViewModel Notifier, L

try
{
LumiaFlashAppModel Model = (LumiaFlashAppModel)Notifier.CurrentModel;
LumiaFlashAppPhoneInfo Info = Model.ReadPhoneInfo();
LumiaFlashAppPhoneInfo Info = ((LumiaFlashAppModel)Notifier.CurrentModel).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.
{
Expand Down Expand Up @@ -443,7 +442,7 @@ .. LumiaV2UnlockBootViewModel.GetNonOptimizedPayloads(FlashParts, chunkSizes, In
LogFile.Log("Flash in progress...", LogType.ConsoleOnly);
SetWorkingStatus("Flashing...", null, (UInt64?)payloads.Length, Status: WPinternalsStatus.Flashing);

Model.SendFfuHeaderV1(FfuHeader, Options);
((LumiaFlashAppModel)Notifier.CurrentModel).SendFfuHeaderV1(FfuHeader, Options);

UInt64 counter = 0;
Int32 numberOfPayloadsToSendAtOnce = 1;
Expand Down Expand Up @@ -485,24 +484,25 @@ .. LumiaV2UnlockBootViewModel.GetNonOptimizedPayloads(FlashParts, chunkSizes, In

if ((Info.SecureFfuSupportedProtocolMask & (ushort)FfuProtocol.ProtocolSyncV2) != 0)
{
Model.SendFfuPayloadV2(payloadBuffer, Int32.Parse((counter * 100 / (UInt64)payloads.Length).ToString()));
((LumiaFlashAppModel)Notifier.CurrentModel).SendFfuPayloadV2(payloadBuffer, Int32.Parse((counter * 100 / (UInt64)payloads.Length).ToString()));
}
else
{
Model.SendFfuPayloadV1(payloadBuffer, Int32.Parse((counter * 100 / (UInt64)payloads.Length).ToString()));
((LumiaFlashAppModel)Notifier.CurrentModel).SendFfuPayloadV1(payloadBuffer, Int32.Parse((counter * 100 / (UInt64)payloads.Length).ToString()));
}

UpdateWorkingStatus(ProgressText, null, counter, WPinternalsStatus.Flashing);
}

Model.ResetPhone();
((LumiaFlashAppModel)Notifier.CurrentModel).ResetPhone();

await Notifier.WaitForRemoval();

ExitSuccess("Flash succeeded!", null);
}
catch
catch (Exception ex)
{
LogFile.Log(ex.Message);
throw new WPinternalsException("Custom flash failed");
}
}
Expand Down
28 changes: 0 additions & 28 deletions WPinternals/ViewModels/SwitchModeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,34 +87,6 @@ internal SwitchModeViewModel(PhoneNotifierViewModel PhoneNotifier, PhoneInterfac
internal SwitchModeViewModel(PhoneNotifierViewModel PhoneNotifier, PhoneInterfaces? TargetMode, ModeSwitchProgressHandler ModeSwitchProgress, ModeSwitchErrorHandler ModeSwitchError, ModeSwitchSuccessHandler ModeSwitchSuccess, SetWorkingStatus SetWorkingStatus = null, UpdateWorkingStatus UpdateWorkingStatus = null)
: base()
{
if ((PhoneNotifier.CurrentInterface == PhoneInterfaces.Lumia_Bootloader) && (TargetMode == PhoneInterfaces.Lumia_Flash))
{
LumiaBootManagerPhoneInfo Info = ((LumiaBootManagerAppModel)PhoneNotifier.CurrentModel).ReadPhoneInfo(false);
if (Info.BootManagerProtocolVersionMajor >= 2)
{
try
{
// The implementation of SwitchToFlashAppContext() is improved
// SwitchToFlashAppContext() should only be used with BootMgr v2
// For switching from BootMgr to FlashApp, it will use NOKS
// That will switch to a charging state, whereas a normal context switch will not start charging
// The implementation of NOKS in BootMgr mode has changed in BootMgr v2
// It does not disconnect / reconnect anymore and the apptype is changed immediately
// NOKS still doesnt return a status
// BootMgr v1 uses normal NOKS and waits for arrival of FlashApp
((LumiaBootManagerAppModel)PhoneNotifier.CurrentModel).SwitchToFlashAppContext();

// But this was called as a real switch, so we will raise an arrival event.
PhoneNotifier.CurrentInterface = PhoneInterfaces.Lumia_Flash;
PhoneNotifier.NotifyArrival();
}
catch (Exception ex)
{
LogFile.LogException(ex, LogType.FileOnly);
}
}
}

if (PhoneNotifier.CurrentInterface == TargetMode)
{
ModeSwitchSuccess(PhoneNotifier.CurrentModel, (PhoneInterfaces)PhoneNotifier.CurrentInterface);
Expand Down

0 comments on commit 7c18a16

Please sign in to comment.