Skip to content

Commit

Permalink
Fixes for Spec A unlocking
Browse files Browse the repository at this point in the history
In memories of the cyan RM-821 from a fellow care center

Rest in peace, 12th of October 2024
  • Loading branch information
gus33000 committed Oct 12, 2024
1 parent 605b19a commit e078bb1
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 45 deletions.
20 changes: 0 additions & 20 deletions WPinternals/Models/Lumia/UEFI/BootMgr/LumiaBootManagerAppModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,25 +401,5 @@ internal void SwitchToPhoneInfoAppContextLegacy()
RaiseInterfaceChanged(PhoneInterfaces.Lumia_PhoneInfo);
}
}

internal void RebootToFlashApp()
{
LumiaBootManagerPhoneInfo info = ReadPhoneInfoBootManager();

bool ModernFlashApp = info.BootManagerVersionMajor >= 2;

byte[] Request = new byte[4];
ByteOperations.WriteAsciiString(Request, 0, RebootToFlashAppSignature); // This will let the phone charge
ExecuteRawVoidMethod(Request); // On phone with bootloader Spec A this triggers a reboot, so DisableRebootTimeOut() cannot be called immediately.

if (ModernFlashApp)
{
DisableRebootTimeOut();

info.App = FlashAppType.FlashApp;

RaiseInterfaceChanged(PhoneInterfaces.Lumia_Flash);
}
}
}
}
2 changes: 1 addition & 1 deletion WPinternals/Models/QualcommLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internal static List<QualcommPartition> GetPossibleLoadersForRootKeyHash(string
// Make sure the RootKeyHash is not blank
// If the RootKeyHash is blank, this is an engineering device, and it will accept any RKH
// We expect the user to know what he is doing in such case and we will ignore checks
if (!StructuralComparisons.StructuralEqualityComparer.Equals(RootKeyHash, new byte[RootKeyHash.Length]))
if (RootKeyHash != null && !StructuralComparisons.StructuralEqualityComparer.Equals(RootKeyHash, new byte[RootKeyHash.Length]))
{
if (StructuralComparisons.StructuralEqualityComparer.Equals(Loader.RootKeyHash, RootKeyHash)
&& (ByteOperations.FindUnicode(Loader.Binary, "QHSUSB_ARMPRG") != null)) // To detect that this is a loader, and not SBL1 or something. V1 loaders are QHSUSB_ARMPRG. V2 loaders are QHSUSB__BULK. Only V1 supported for now, because V2 only accepts signed payload.
Expand Down
49 changes: 29 additions & 20 deletions WPinternals/ViewModels/LumiaUnlockBootViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ void ReturnFunction(string FFUPath, string LoadersPath, string SBL3Path, string
this.LoadersPath = LoadersPath;
this.SBL3Path = SBL3Path;
this.SupportedFFUPath = SupportedFFUPath;
StorePaths();

LogFile.Log("Processing resources:");
LogFile.Log("FFU: " + FFUPath);
Expand All @@ -196,14 +195,19 @@ void ReturnFunction(string FFUPath, string LoadersPath, string SBL3Path, string

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

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

if (DoUnlock)
Expand Down Expand Up @@ -234,7 +238,6 @@ void ReturnFunction(string FFUPath, string LoadersPath, string SBL3Path, string
this.ProfileFFUPath = ProfileFFUPath;
this.EDEPath = EDEPath;
this.SupportedFFUPath = SupportedFFUPath;
StorePaths();

if (DoFixBoot)
{
Expand All @@ -255,6 +258,8 @@ void ReturnFunction(string FFUPath, string LoadersPath, string SBL3Path, string

Task.Run(async () =>
{
await StorePaths();
bool AlreadyUnlocked = false;
LumiaFlashAppModel FlashModel = (LumiaFlashAppModel)PhoneNotifier.CurrentModel;
Expand Down Expand Up @@ -418,7 +423,6 @@ void ReturnFunction(string FFUPath, string LoadersPath, string SBL3Path, string
this.FFUPath = FFUPath;
this.LoadersPath = LoadersPath;
this.SBL3Path = SBL3Path;
StorePaths();
LogFile.Log("Processing resources:");
LogFile.Log("FFU: " + FFUPath);
Expand All @@ -434,14 +438,19 @@ void ReturnFunction(string FFUPath, string LoadersPath, string SBL3Path, string
ActivateSubContext(new BusyViewModel("Processing resources..."));
if (DoUnlock)
Task.Run(async () =>
{
Task.Run(async () => await LumiaUnlockBootloaderViewModel.LumiaV1UnlockFirmware(PhoneNotifier, FFUPath, LoadersPath, SBL3Path, SupportedFFUPath, SetWorkingStatus, UpdateWorkingStatus, ExitMessage, ExitMessage));
}
else
{
Task.Run(async () => await LumiaUnlockBootloaderViewModel.LumiaV1RelockFirmware(PhoneNotifier, FFUPath, LoadersPath, SetWorkingStatus, UpdateWorkingStatus, ExitMessage, ExitMessage));
}
await StorePaths();
if (DoUnlock)
{
await LumiaUnlockBootloaderViewModel.LumiaV1UnlockFirmware(PhoneNotifier, FFUPath, LoadersPath, SBL3Path, SupportedFFUPath, SetWorkingStatus, UpdateWorkingStatus, ExitMessage, ExitMessage);
}
else
{
await LumiaUnlockBootloaderViewModel.LumiaV1RelockFirmware(PhoneNotifier, FFUPath, LoadersPath, SetWorkingStatus, UpdateWorkingStatus, ExitMessage, ExitMessage);
}
});
};

if (DoUnlock)
Expand Down Expand Up @@ -513,15 +522,15 @@ private void Abort()
{
// SwitchModeViewModel must be created on the UI thread
IsSwitchingInterface = false;
UIContext.Post((t) =>
UIContext.Post(async (t) =>
{
StorePaths();
await StorePaths();
LogFile.Log("Aborting.");
Exit();
}, null);
}

private async void StorePaths()
private async Task StorePaths()
{
RegistryKey Key = Registry.CurrentUser.OpenSubKey(@"Software\WPInternals", true) ?? Registry.CurrentUser.CreateSubKey(@"Software\WPInternals");

Expand Down
85 changes: 81 additions & 4 deletions WPinternals/ViewModels/LumiaUnlockBootloaderViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using WPinternals.Models.Lumia.UEFI.Flash;
using WPinternals.Models.UEFIApps.BootMgr;
using WPinternals.Models.UEFIApps.Flash;
using WPinternals.Models.UEFIApps.PhoneInfo;

namespace WPinternals
{
Expand All @@ -55,7 +56,17 @@ public static async Task<GPT> ReadGPTFromFlashOrBootMgr(PhoneNotifierViewModel N

if (FlashModel.CanReadGPT())
{
FlashModel.SwitchToBootManagerContext();
bool ModernFlashApp = FlashModel.ReadPhoneInfoFlashApp().FlashAppProtocolVersionMajor >= 2;

if (ModernFlashApp)
{
FlashModel.SwitchToFlashAppContext();
}
else
{
FlashModel.ResetPhone();
await Notifier.WaitForRemoval();
}

if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Bootloader)
{
Expand All @@ -69,7 +80,14 @@ public static async Task<GPT> ReadGPTFromFlashOrBootMgr(PhoneNotifierViewModel N

GPT GPT = ((LumiaBootManagerAppModel)Notifier.CurrentModel).ReadGPT();

((LumiaBootManagerAppModel)Notifier.CurrentModel).SwitchToFlashAppContext();
if (ModernFlashApp)
{
((LumiaBootManagerAppModel)Notifier.CurrentModel).SwitchToFlashAppContext();
}
else
{
((LumiaBootManagerAppModel)Notifier.CurrentModel).ResetPhoneToFlashMode();
}

if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash)
{
Expand Down Expand Up @@ -129,7 +147,15 @@ public static async Task<byte[]> GetGptChunkFromFlashOrBootMgr(PhoneNotifierView

byte[] GPT = ((LumiaBootManagerAppModel)Notifier.CurrentModel).GetGptChunk(Size);

((LumiaBootManagerAppModel)Notifier.CurrentModel).SwitchToFlashAppContext();
bool ModernFlashApp = ((LumiaBootManagerAppModel)Notifier.CurrentModel).ReadPhoneInfo().BootManagerProtocolVersionMajor >= 2;
if (ModernFlashApp)
{
((LumiaBootManagerAppModel)Notifier.CurrentModel).SwitchToFlashAppContext();
}
else
{
((LumiaBootManagerAppModel)Notifier.CurrentModel).ResetPhoneToFlashMode();
}

if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash)
{
Expand Down Expand Up @@ -851,6 +877,29 @@ await LumiaRelockUEFI(Notifier, FFUPath, true, SetWorkingStatus, UpdateWorkingSt
throw new WPinternalsException("Phone is in an unexpected mode.", "The phone should have been detected in flash mode. Instead it has been detected in " + Notifier.CurrentInterface.ToString() + " mode.");
}

if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Bootloader)
{
bool ModernFlashApp = ((LumiaBootManagerAppModel)Notifier.CurrentModel).ReadPhoneInfo().BootManagerProtocolVersionMajor >= 2;
if (ModernFlashApp)
{
((LumiaBootManagerAppModel)Notifier.CurrentModel).SwitchToFlashAppContext();
}
else
{
((LumiaBootManagerAppModel)Notifier.CurrentModel).ResetPhoneToFlashMode();
}
}

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

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

LumiaFlashAppModel FlashModel = (LumiaFlashAppModel)Notifier.CurrentModel;
if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Flash && FlashModel.ReadParam("FS")[3] > 0)
{
Expand Down Expand Up @@ -1446,6 +1495,7 @@ internal static async Task LumiaV1UnlockFirmware(PhoneNotifierViewModel Notifier
LogFile.Log("Flash MBR at 0x" + ((UInt32)0).ToString("X8"));
Flasher.Flash(0, MBR, Progress, 0, 0x200);

// TIMEOUT
LogFile.Log("Flash GPT at 0x" + ((UInt32)0x200).ToString("X8"));
Flasher.Flash(0x200, GPT, Progress, 0, 0x41FF); // Bad bounds-check in the flash-loader prohibits to write the last byte.

Expand Down Expand Up @@ -1760,6 +1810,11 @@ internal static async Task LumiaRelockUEFI(PhoneNotifierViewModel Notifier, stri
await SwitchModeViewModel.SwitchToWithStatus(Notifier, PhoneInterfaces.Lumia_Flash, SetWorkingStatus, UpdateWorkingStatus);
}

if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Bootloader)
{
((LumiaBootManagerAppModel)Notifier.CurrentModel).SwitchToFlashAppContext();
}

if (Notifier.CurrentInterface != PhoneInterfaces.Lumia_Flash)
{
await Notifier.WaitForArrival();
Expand Down Expand Up @@ -2583,7 +2638,29 @@ internal static async Task LumiaUnlockUEFI(PhoneNotifierViewModel Notifier, stri
throw new WPinternalsException("Phone is in wrong mode", "The phone should have been detected in bootloader mode. Instead it has been detected in " + Notifier.CurrentInterface.ToString() + " mode.");
}
}
((LumiaBootManagerAppModel)Notifier.CurrentModel).SwitchToFlashAppContext();

if (Notifier.CurrentInterface == PhoneInterfaces.Lumia_Bootloader)
{
bool ModernFlashApp = ((LumiaBootManagerAppModel)Notifier.CurrentModel).ReadPhoneInfo().BootManagerProtocolVersionMajor >= 2;
if (ModernFlashApp)
{
((LumiaBootManagerAppModel)Notifier.CurrentModel).SwitchToFlashAppContext();
}
else
{
((LumiaBootManagerAppModel)Notifier.CurrentModel).ResetPhoneToFlashMode();
}
}

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

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

Parts = LumiaGenerateEFIESPFlashPayload(UnlockedEFIESP, GPT, ProfileFFU, IsSpecB);

Expand Down

0 comments on commit e078bb1

Please sign in to comment.