mirror of
https://github.com/mashed-potatoes/PotatoNV.git
synced 2024-11-10 01:02:20 +01:00
Add bootloader class & FormEventArgs
This commit is contained in:
parent
61bbfa98e5
commit
dccfa80598
6 changed files with 169 additions and 4 deletions
|
@ -5,7 +5,7 @@
|
|||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:PotatoNV_next.Controls"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="450" d:DesignWidth="800">
|
||||
d:DesignHeight="450" d:DesignWidth="800" IsEnabledChanged="NVForm_IsEnabledChanged">
|
||||
<StackPanel Style="{x:Null}">
|
||||
<StackPanel>
|
||||
<Label Content="Target device" />
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
@ -14,7 +15,6 @@ using System.Windows.Input;
|
|||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace PotatoNV_next.Controls
|
||||
{
|
||||
|
@ -23,15 +23,28 @@ namespace PotatoNV_next.Controls
|
|||
private UsbController usbController;
|
||||
private Regex nvRegex = new Regex("^[a-zA-Z0-9]{16}$");
|
||||
|
||||
public delegate void FormHandler(FormEventArgs formEventArgs);
|
||||
public static event FormHandler OnFormSubmit;
|
||||
|
||||
public NVForm()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
usbController = new UsbController();
|
||||
usbController.Notify += HandleDevices;
|
||||
usbController.StartWorker();
|
||||
}
|
||||
|
||||
public class FormEventArgs : EventArgs
|
||||
{
|
||||
public UsbController.Device.DMode TargetMode { get; set; }
|
||||
public string Target { get; set; }
|
||||
public string BoardID { get; set; }
|
||||
public string UnlockCode { get; set; }
|
||||
public string SerialNumber { get; set; }
|
||||
public bool FBLOCK { get; set; }
|
||||
public Bootloader bootloader { get; set; } = null;
|
||||
}
|
||||
|
||||
private void Assert(bool result, string message)
|
||||
{
|
||||
if (result)
|
||||
|
@ -102,6 +115,20 @@ namespace PotatoNV_next.Controls
|
|||
}
|
||||
|
||||
Log.Success("Form is valid, starting");
|
||||
IsEnabled = false;
|
||||
|
||||
//OnFormSubmit();
|
||||
}
|
||||
|
||||
private void NVForm_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
deviceList.IsEnabled = IsEnabled;
|
||||
deviceBootloader.IsEnabled = IsEnabled;
|
||||
nvBidNumber.IsEnabled = IsEnabled;
|
||||
nvSerialNumber.IsEnabled = IsEnabled;
|
||||
nvUnlockCode.IsEnabled = IsEnabled;
|
||||
disableFBLOCK.IsEnabled = IsEnabled;
|
||||
startButton.IsEnabled = IsEnabled;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
12
PotatoNV-next/Core.cs
Normal file
12
PotatoNV-next/Core.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PotatoNV_next
|
||||
{
|
||||
class Core
|
||||
{
|
||||
}
|
||||
}
|
|
@ -132,6 +132,8 @@
|
|||
<Compile Include="Controls\NVForm.xaml.cs">
|
||||
<DependentUpon>NVForm.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Core.cs" />
|
||||
<Compile Include="Utils\Bootloader.cs" />
|
||||
<Compile Include="Utils\Log.cs" />
|
||||
<Compile Include="Utils\MediaConverter.cs" />
|
||||
<Compile Include="Utils\UsbController.cs" />
|
||||
|
|
124
PotatoNV-next/Utils/Bootloader.cs
Normal file
124
PotatoNV-next/Utils/Bootloader.cs
Normal file
|
@ -0,0 +1,124 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml;
|
||||
|
||||
namespace PotatoNV_next.Utils
|
||||
{
|
||||
public class Bootloader
|
||||
{
|
||||
public class Image
|
||||
{
|
||||
private bool? valid = null;
|
||||
|
||||
public string Path { get; }
|
||||
public string Role { get; }
|
||||
public uint Address { get; }
|
||||
private string Hash { get; }
|
||||
public bool IsValid { get => valid ?? Validate(); }
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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.Info("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());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -11,7 +11,7 @@ using System.Threading.Tasks;
|
|||
|
||||
namespace PotatoNV_next.Utils
|
||||
{
|
||||
class UsbController
|
||||
public class UsbController
|
||||
{
|
||||
public struct Device
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue