Skip to content

Commit

Permalink
fix issues with downloading files
Browse files Browse the repository at this point in the history
  • Loading branch information
gus33000 committed Sep 1, 2024
1 parent 29c6ab0 commit 91961b0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 26 deletions.
6 changes: 4 additions & 2 deletions WPinternals/Models/UEFIApps/LumiaFlashAppModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -927,15 +927,16 @@ public void FlashMMOS(string MMOSPath, ProgressUpdater UpdaterPerChunk)
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);

Expand All @@ -950,6 +951,7 @@ public void FlashMMOS(string MMOSPath, ProgressUpdater UpdaterPerChunk)

byte[] data = new byte[length - offset];
MMOSFile.Read(data, 0, (int)(length - offset));

LoadMmosBinary(length, (uint)offset, false, data);
}

Expand Down
18 changes: 12 additions & 6 deletions WPinternals/Models/UEFIApps/NokiaFlashModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,18 @@ internal void SwitchToPhoneInfoAppContext()
internal void SwitchToMmosContext()
{
byte[] Request = new byte[7];
ByteOperations.WriteAsciiString(Request, 0, SwitchModeSignature + "A");
ExecuteRawVoidMethod(Request);

ResetDevice();
ByteOperations.WriteAsciiString(Request, 0, $"{SwitchModeSignature}A");
byte[] Response = ExecuteRawMethod(Request);
if (ByteOperations.ReadAsciiString(Response, 0, 4) == "NOKU")
{
throw new NotSupportedException();
}

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()
Expand All @@ -131,7 +137,7 @@ internal void SwitchToFlashAppContext()
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")
{
Expand Down
2 changes: 1 addition & 1 deletion WPinternals/ViewModels/DownloadsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ internal DownloadEntry(string URL, string Folder, string[] URLCollection, Action

//_ = 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();
Expand Down
33 changes: 28 additions & 5 deletions WPinternals/ViewModels/HttpDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,11 @@ private static async ValueTask<bool> HttpDownload(string basePath, FileDownloadI

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)
{
Expand Down Expand Up @@ -384,8 +383,32 @@ private static async ValueTask<bool> HttpDownload(string basePath, FileDownloadI

//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)
Expand Down
31 changes: 19 additions & 12 deletions WPinternals/ViewModels/SwitchModeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -936,26 +936,33 @@ private void ENOSWDownloadCompleted(string[] URLs, object State)
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);

App.Config.AddSecWimToRepository(MMOSPath, Firmware);

string MMOSPath = Path.Combine(TempFolder, Name);
PhoneNotifier.NewDeviceArrived += NewDeviceArrived;

LumiaFlashAppPhoneInfo Info = FlashModel.ReadPhoneInfo();

App.Config.AddSecWimToRepository(MMOSPath, Firmware);
FileInfo info = new(MMOSPath);
uint length = uint.Parse(info.Length.ToString());

PhoneNotifier.NewDeviceArrived += NewDeviceArrived;
int maximumBufferSize = (int)Info.WriteBufferSize;

FileInfo info = new(MMOSPath);
uint fileLength = uint.Parse(info.Length.ToString());
const int maximumBufferSize = 0x00240000;
uint chunkCount = (uint)Math.Truncate((decimal)fileLength / maximumBufferSize);
uint chunkCount = (uint)Math.Truncate((decimal)length / maximumBufferSize);

UIContext?.Post(d => SetWorkingStatus("Flashing Test Mode package...", MaxProgressValue: 100), null);
UIContext?.Post(d => SetWorkingStatus("Flashing Test Mode package...", MaxProgressValue: 100), null);

ProgressUpdater progressUpdater = new(chunkCount + 1, (int i, TimeSpan? time) => UIContext?.Post(d => UpdateWorkingStatus(null, CurrentProgressValue: (ulong)i), null));
ProgressUpdater progressUpdater = new(chunkCount + 1, (int i, TimeSpan? time) => UpdateWorkingStatus(null, CurrentProgressValue: (ulong)i));

FlashModel.FlashMMOS(MMOSPath, progressUpdater);
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.");
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)
Expand Down

0 comments on commit 91961b0

Please sign in to comment.