Skip to content

Commit

Permalink
24f
Browse files Browse the repository at this point in the history
  • Loading branch information
werasik2aa committed Jan 2, 2024
1 parent ae259d9 commit 222e05d
Show file tree
Hide file tree
Showing 11 changed files with 507 additions and 176 deletions.
159 changes: 159 additions & 0 deletions HuaweiUnlock/DIAGNOS/Bootloader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Xml;
using static HuaweiUnlocker.LangProc;

namespace HuaweiUnlocker.DIAGNOS
{
public class Bootloader
{
public class Image
{
private bool? valid = null;
private int? size = null;

public string Path { get; }
public string Role { get; }
public uint Address { get; }
private string Hash { get; }
public bool IsValid { get => valid ?? Validate(); }
public int Size { get => size ?? GetSize(); }

private bool Validate()
{
if (Hash == null)
{
return true;
}

using (var stream = File.OpenRead(Path)) {
using (var sha1 = SHA1.Create())
{
stream.Position = 0;
byte[] hash = sha1.ComputeHash(stream);
stream.Close();
valid = BitConverter.ToString(hash).Replace("-", "").ToLower() == Hash;
}
}

return valid.Value;
}

private int GetSize()
{
size = (int)new FileInfo(Path).Length;

return size.Value;
}

public Image(string path, string role, uint address, string hash = null)
{
Path = path;
Role = role;
Address = address;
Hash = hash;
}
}

public Image[] Images { get; }
public string Title { get; }
public string Name { get; }

public Bootloader(string name, string title, Image[] images)
{
Title = title;
Name = name;
Images = images;
}

private static uint ParseAddress(string str) => Convert.ToUInt32(str, str.StartsWith("0x") ? 16 : 10);

private static XmlElement GetRootFromFile(string filename)
{
var xml = new XmlDocument();
xml.Load(filename);

var root = xml.DocumentElement;

if (root.Name != "bootloader")
{
throw new Exception("XML root name is invalid.");
}

return root;
}

private readonly static string[] requiredStrings = new[] { "path", "role", "hash", "address" };

public static Bootloader ParseBootloader(string filename)
{
var root = GetRootFromFile(filename);
var dir = Path.GetDirectoryName(filename);

var title = root.GetAttribute("name");

if (string.IsNullOrEmpty(title))
{
LOG(2, "Name attribute is invalid!");
title = "Unknown bootloader";
}

var images = new List<Image>();

foreach (XmlNode node in root)
{
bool isBad = node.Name != "image";

foreach (var key in requiredStrings)
{
var item = node.Attributes.GetNamedItem(key);
isBad |= item == null || string.IsNullOrWhiteSpace(item.Value);
}

if (isBad)
{
throw new Exception("Failed to parse image");
}

images.Add(new Image(
Path.Combine(dir, node.Attributes.GetNamedItem("path").Value),
node.Attributes.GetNamedItem("role").Value,
ParseAddress(node.Attributes.GetNamedItem("address").Value),
node.Attributes.GetNamedItem("hash").Value
));
}

return new Bootloader(Path.GetFileName(dir), title, images.ToArray());
}

public static Bootloader[] GetBootloaders()
{
var bootloaders = new List<Bootloader>();
var root = Path.Combine(Environment.CurrentDirectory, "bootloaders");

if (!Directory.Exists(root))
{
return new Bootloader[] { };
}

var dirs = Directory.GetDirectories(root);

foreach (var dir in dirs)
{
var manifest = Path.Combine(dir, "manifest.xml");

if (!File.Exists(manifest))
{
LOG(2, $"{Path.GetFileName(dir)}: Manifest not found");
continue;
}

bootloaders.Add(ParseBootloader(manifest));
}

return bootloaders.ToArray();
}
}
}
2 changes: 2 additions & 0 deletions HuaweiUnlock/DIAGNOS/DIAG.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.Threading;

namespace HuaweiUnlocker.DIAGNOS
{
Expand Down Expand Up @@ -332,6 +333,7 @@ public static byte[] READ_SECRET_KEY()
byte[] status = DIAG_SEND(pkt, true, true);
if (DataS.GetStatus(status))
return status.Skip(4).Take(status.Length - 11).ToArray();
Thread.Sleep(100);
return Encoding.ASCII.GetBytes("Please Auth");
}
public static byte[] AUTH_PHONE(byte[] hexdata)
Expand Down
2 changes: 1 addition & 1 deletion HuaweiUnlock/FlashTool/FlashToolQClegacy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public static async void FlashPartsXml(string rawxml, string patchxml, string lo
{
Progress(2);
string command = "Tools\\fh_loader.exe";
string subcommand = "--port=\\\\.\\" + DeviceInfo.Port.ComName + " --showpercentagecomplete --sendxml=" + '"' + rawxml + '"' + " --search_path=" + '"' + path + '"';
string subcommand = "--port=\\\\.\\" + DeviceInfo.Port.ComName + " --sendxml=" + '"' + rawxml + '"' + " --search_path=" + '"' + path + '"';
string subcommandp = "--port=\\\\.\\" + DeviceInfo.Port.ComName + " --sendxml=" + '"' + patchxml + '"' + " --search_path=" + '"' + path + '"';
if (debug) LOG(-1, "===Flash Partitions XML===" + newline + newline);
if (!LoadLoader(loader)) { DeviceInfo.loadedhose = false; LOG(2, "Fail"); CurTask.Dispose(); }
Expand Down
13 changes: 4 additions & 9 deletions HuaweiUnlock/HuaweiUnlock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,10 @@
</Reference>
<Reference Include="Microsoft.VisualBasic" />
<Reference Include="QC.QMSLPhone">
<HintPath>C:\Users\werasik2aa\Desktop\QC.QMSLPhone.dll</HintPath>
<HintPath>..\packages\add\QC.QMSLPhone.dll</HintPath>
</Reference>
<Reference Include="QMSL_Library">
<HintPath>C:\Users\werasik2aa\Desktop\QMSL_Library.dll</HintPath>
</Reference>
<Reference Include="SwDownloadDLL">
<HintPath>C:\Users\werasik2aa\Desktop\SwDownloadDLL.dll</HintPath>
<HintPath>..\packages\add\QMSL_Library.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
Expand All @@ -105,12 +102,11 @@
<Compile Include="DIAGNOS\LibCrypt.cs" />
<Compile Include="DIAGNOS\OemInfoTool.cs" />
<Compile Include="FlashTool\MTKFlash.cs" />
<Compile Include="ResourcesMNG.cs" />
<Compile Include="DIAGNOS\Bootloader.cs" />
<Compile Include="TOOLS\Guide.cs" />
<Compile Include="TOOLS\HISI.cs" />
<Compile Include="FlashTool\FlashToolHisi.cs" />
<Compile Include="TOOLS\Fastboot.cs" />
<Compile Include="TOOLS\ImageFlasher.cs" />
<Compile Include="TOOLS\HISI.cs" />
<Compile Include="TOOLS\UpdateApp.cs" />
<Compile Include="TOOLS\UpdateUtil\EventStream.cs" />
<Compile Include="TOOLS\UpdateUtil\FileHeader.cs" />
Expand Down Expand Up @@ -155,7 +151,6 @@
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<None Include="app.config" />
<EmbeddedResource Include="English.ini" />
<None Include="ILMerge.props" />
<None Include="packages.config" />
Expand Down
2 changes: 1 addition & 1 deletion HuaweiUnlock/TOOLS/Fastboot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Fastboot
private const int BLOCK_SIZE = 512 * 1024; // 512 KB

public int Timeout = 3000;
public int TimeoutWait = 50;
public int TimeoutWait = 1000;
private UsbDevice device;

public enum FastbootStatus
Expand Down
Loading

0 comments on commit 222e05d

Please sign in to comment.