From 91961b0e7b5520055d5e49043cedd7ad5ed22746 Mon Sep 17 00:00:00 2001 From: Gustave Monce Date: Sun, 1 Sep 2024 12:45:46 +0200 Subject: [PATCH] fix issues with downloading files --- .../Models/UEFIApps/LumiaFlashAppModel.cs | 6 ++-- .../Models/UEFIApps/NokiaFlashModel.cs | 18 ++++++---- WPinternals/ViewModels/DownloadsViewModel.cs | 2 +- WPinternals/ViewModels/HttpDownloader.cs | 33 ++++++++++++++++--- WPinternals/ViewModels/SwitchModeViewModel.cs | 31 ++++++++++------- 5 files changed, 64 insertions(+), 26 deletions(-) diff --git a/WPinternals/Models/UEFIApps/LumiaFlashAppModel.cs b/WPinternals/Models/UEFIApps/LumiaFlashAppModel.cs index 88ece4c..4e612a8 100644 --- a/WPinternals/Models/UEFIApps/LumiaFlashAppModel.cs +++ b/WPinternals/Models/UEFIApps/LumiaFlashAppModel.cs @@ -927,15 +927,16 @@ namespace WPinternals uint length = uint.Parse(info.Length.ToString()); int offset = 0; - const int maximumBufferSize = 0x00240000; + int maximumBufferSize = (int)Info.WriteBufferSize; uint chunkCount = (uint)Math.Truncate((decimal)length / maximumBufferSize); using FileStream MMOSFile = new(MMOSPath, FileMode.Open, FileAccess.Read, FileShare.Read); - for (int i = 1; i <= (uint)Math.Truncate((decimal)length / maximumBufferSize); i++) + for (int i = 0; i < chunkCount; i++) { Progress.IncreaseProgress(1); + byte[] data = new byte[maximumBufferSize]; MMOSFile.Read(data, 0, maximumBufferSize); @@ -950,6 +951,7 @@ namespace WPinternals byte[] data = new byte[length - offset]; MMOSFile.Read(data, 0, (int)(length - offset)); + LoadMmosBinary(length, (uint)offset, false, data); } diff --git a/WPinternals/Models/UEFIApps/NokiaFlashModel.cs b/WPinternals/Models/UEFIApps/NokiaFlashModel.cs index 06806c8..b1c0c3e 100644 --- a/WPinternals/Models/UEFIApps/NokiaFlashModel.cs +++ b/WPinternals/Models/UEFIApps/NokiaFlashModel.cs @@ -116,12 +116,18 @@ namespace WPinternals internal void SwitchToMmosContext() { byte[] Request = new byte[7]; - ByteOperations.WriteAsciiString(Request, 0, SwitchModeSignature + "A"); - ExecuteRawVoidMethod(Request); + ByteOperations.WriteAsciiString(Request, 0, $"{SwitchModeSignature}A"); + byte[] Response = ExecuteRawMethod(Request); + if (ByteOperations.ReadAsciiString(Response, 0, 4) == "NOKU") + { + throw new NotSupportedException(); + } - ResetDevice(); - - Dispose(true); + UInt16 Error = (UInt16)((Response[6] << 8) + Response[7]); + if (Error > 0) + { + throw new NotSupportedException("SwitchToPhoneInfoAppContext: Error 0x" + Error.ToString("X4")); + } } internal void SwitchToFlashAppContext() @@ -131,7 +137,7 @@ namespace WPinternals bool ModernFlashApp = info.VersionMajor >= 2; byte[] Request = new byte[7]; - ByteOperations.WriteAsciiString(Request, 0, SwitchModeSignature + "F"); // This will stop charging the phone + ByteOperations.WriteAsciiString(Request, 0, $"{SwitchModeSignature}F"); // This will stop charging the phone byte[] Response = ExecuteRawMethod(Request); if (ByteOperations.ReadAsciiString(Response, 0, 4) == "NOKU") { diff --git a/WPinternals/ViewModels/DownloadsViewModel.cs b/WPinternals/ViewModels/DownloadsViewModel.cs index fafb217..c1a165e 100644 --- a/WPinternals/ViewModels/DownloadsViewModel.cs +++ b/WPinternals/ViewModels/DownloadsViewModel.cs @@ -723,7 +723,7 @@ namespace WPinternals //_ = Client.DownloadFileAsync(Uri, Path.Combine(Folder, DownloadsViewModel.GetFileNameFromURL(Uri.LocalPath)), Client_DownloadProgressChanged, Client_DownloadFileCompleted); - Client = new(Folder, 4); + Client = new(Folder, 4, false); _ = Client.DownloadAsync([new FileDownloadInformation(URL, DownloadsViewModel.GetFileNameFromURL(Uri.LocalPath), Size, null, null)], this); }).Start(); diff --git a/WPinternals/ViewModels/HttpDownloader.cs b/WPinternals/ViewModels/HttpDownloader.cs index e517d99..6cc955f 100644 --- a/WPinternals/ViewModels/HttpDownloader.cs +++ b/WPinternals/ViewModels/HttpDownloader.cs @@ -275,12 +275,11 @@ namespace WPinternals if (tmpFileInfo.Length == downloadFile.FileSize) { - /*//Decrypted file should match estimated bytes. + //Decrypted file should match estimated bytes. //Imagine if it crashed during hashing, the file may be valid. //So... lets rename this file so it can be verified. File.Move(tempFilePath, filePath); - totalBytesRead = tmpFileInfo.Length;*/ - File.Delete(tempFilePath); + totalBytesRead = tmpFileInfo.Length; } else if (tmpFileInfo.Length < downloadFile.FileSize) { @@ -384,8 +383,32 @@ namespace WPinternals //just an assumption - _ = streamToWriteTo.Seek(0, SeekOrigin.Begin); - return await HashWithProgress(streamToWriteTo); + if (verifyFiles) + { + _ = streamToWriteTo.Seek(0, SeekOrigin.Begin); + bool hashResult = await HashWithProgress(streamToWriteTo); + + if (hashResult) + { + streamToWriteTo.Close(); + File.Move(tempFilePath, filePath); + } + + return hashResult; + } + else + { + streamToWriteTo.Close(); + File.Move(tempFilePath, filePath); + + downloadProgress?.Report(new FileDownloadStatus(downloadFile) + { + DownloadedBytes = totalBytesRead, + FileStatus = FileStatus.Completed + }); + + return true; + } } while (currRange < contentLength.Value) diff --git a/WPinternals/ViewModels/SwitchModeViewModel.cs b/WPinternals/ViewModels/SwitchModeViewModel.cs index 19571b6..3799d16 100644 --- a/WPinternals/ViewModels/SwitchModeViewModel.cs +++ b/WPinternals/ViewModels/SwitchModeViewModel.cs @@ -936,26 +936,33 @@ namespace WPinternals string TempFolder = $@"{Environment.GetEnvironmentVariable("TEMP")}\WPInternals"; LumiaFlashAppModel FlashModel = (LumiaFlashAppModel)PhoneNotifier.CurrentModel; - ModeSwitchProgressWrapper("Initializing Flash...", null); + Task.Run(() => + { + ModeSwitchProgressWrapper("Initializing Flash...", null); - string MMOSPath = Path.Combine(TempFolder, Name); + string MMOSPath = Path.Combine(TempFolder, Name); - App.Config.AddSecWimToRepository(MMOSPath, Firmware); + App.Config.AddSecWimToRepository(MMOSPath, Firmware); - PhoneNotifier.NewDeviceArrived += NewDeviceArrived; + PhoneNotifier.NewDeviceArrived += NewDeviceArrived; - FileInfo info = new(MMOSPath); - uint fileLength = uint.Parse(info.Length.ToString()); - const int maximumBufferSize = 0x00240000; - uint chunkCount = (uint)Math.Truncate((decimal)fileLength / maximumBufferSize); + LumiaFlashAppPhoneInfo Info = FlashModel.ReadPhoneInfo(); - UIContext?.Post(d => SetWorkingStatus("Flashing Test Mode package...", MaxProgressValue: 100), null); + FileInfo info = new(MMOSPath); + uint length = uint.Parse(info.Length.ToString()); - ProgressUpdater progressUpdater = new(chunkCount + 1, (int i, TimeSpan? time) => UIContext?.Post(d => UpdateWorkingStatus(null, CurrentProgressValue: (ulong)i), null)); + int maximumBufferSize = (int)Info.WriteBufferSize; - FlashModel.FlashMMOS(MMOSPath, progressUpdater); + uint chunkCount = (uint)Math.Truncate((decimal)length / maximumBufferSize); - ModeSwitchProgressWrapper("And now booting phone to MMOS...", "If the phone stays on the lightning cog screen for a while, you may need to unplug and replug the phone to continue the boot process."); + UIContext?.Post(d => SetWorkingStatus("Flashing Test Mode package...", MaxProgressValue: 100), null); + + ProgressUpdater progressUpdater = new(chunkCount + 1, (int i, TimeSpan? time) => UpdateWorkingStatus(null, CurrentProgressValue: (ulong)i)); + + FlashModel.FlashMMOS(MMOSPath, progressUpdater); + + ModeSwitchProgressWrapper("And now booting phone to MMOS...", "If the phone stays on the lightning cog screen for a while, you may need to unplug and replug the phone to continue the boot process."); + }); } private void SwitchFromPhoneInfoToMassStorageMode(bool Continuation = false)