Skip to content

Commit

Permalink
String formatting & Loggingggg
Browse files Browse the repository at this point in the history
  • Loading branch information
kolyandex committed May 27, 2017
1 parent e0b7ae8 commit 547d291
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 81 deletions.
1 change: 1 addition & 0 deletions MEInject/MEInject/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

166 changes: 85 additions & 81 deletions MEInject/MEInject/MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
// ReSharper disable InconsistentNaming

namespace MEInject
{
Expand Down Expand Up @@ -50,25 +52,36 @@ public MainForm()
InitializeComponent();
}

void ClearFields()
private void ClearGUI()
{
MEoffsetLabel.Text = "ME offset in BIOS: -";
MEsizeInBIOSLabel.Text = "ME size: -";
MEinBIOS_ver_label.Text = "ME version: -";
MEoffsetLabel.Text = @"ME offset in BIOS: -";
MEsizeInBIOSLabel.Text = @"ME size: -";
MEinBIOS_ver_label.Text = @"ME version: -";
MEsComboBox.Items.Clear();
MEsComboBox.Text = string.Empty;
_validMEfiles.Clear();
WinKeyTextBox.Text = "-";
WinKeyTextBox.Text = @"-";
}

private void UpdateGUI()
{
MEinBIOS_ver_label.Text = $@"{_mode} version: {BIOS_ME_info.Major}.{BIOS_ME_info.Minor}.{BIOS_ME_info.Hotfix}.{BIOS_ME_info.Build}";
MEoffsetLabel.Text = $@"{_mode} offset in BIOS: 0x{BIOS_ME_start_offset:X8}";
MEsizeInBIOSLabel.Text = $@"{_mode} size: {BIOS_ME_end_offset - BIOS_ME_start_offset} bytes";
SuitableMEs.Text = $@"Suitable {_mode}s:";
if (_MEdir != string.Empty) Text = $@"Intel ME/TXE injector - {_MEdir} - {_meFiles.Count} files";
}

private void MainForm_Load(object sender, EventArgs e)
{
UpdateGUI();
_validMEfiles = new List<string>();
_MEdir = Properties.Settings.Default.MEdir;
LoadDB();
if (_MEdir != string.Empty) Text = "Intel ME/TXE injector - " + _MEdir + " - " + _meFiles.Count + " files";
Log("Ready!");
UpdateGUI();
Log("Ready!", LogLevel.Info);
MEsComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
MaximizeBox = false;

}

Expand All @@ -79,7 +92,7 @@ private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
Properties.Settings.Default.Save();
}

void UpdateDB()
private void UpdateDB()
{
var files = GetFileNames(_MEdir);
_meFiles.Clear();
Expand All @@ -91,14 +104,14 @@ void UpdateDB()
}
catch (Exception ex)
{
//Log(ex.Message);
Log(ex.Message, LogLevel.Warning);
}
}
SaveDB();
Text = "Intel ME/TXE injector - " + _MEdir + " - " + _meFiles.Count + " files";
UpdateGUI();
}

void SaveDB()
private void SaveDB()
{
using (Stream writer = new FileStream(_dbFile, FileMode.Create))
{
Expand All @@ -110,7 +123,7 @@ private void LoadDB()
{
if (_MEdir == string.Empty)
{
MessageBox.Show("Please, specify ME files folder first");
MessageBox.Show(@"Please, specify ME files folder first");
var fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() != DialogResult.OK)
{
Expand All @@ -127,7 +140,6 @@ private void LoadDB()
{

UpdateDB();
Text = "Intel ME/TXE injector - " + _MEdir + " - " + _meFiles.Count + " files";
return;
}

Expand All @@ -143,11 +155,11 @@ private void LoadDB()

catch (Exception exception)
{
Log(exception.Message + "");
Log(exception.Message, LogLevel.Error);
}
}

MEinfo GetMEFileInfo(Stream stream, string path, uint startoffset = 0, uint endoffset = 0)
private MEinfo GetMEFileInfo(Stream stream, string path, uint startoffset = 0, uint endoffset = 0)
{
stream.Seek(startoffset, SeekOrigin.Begin);
var meinfo = new MEinfo();
Expand Down Expand Up @@ -177,7 +189,7 @@ MEinfo GetMEFileInfo(Stream stream, string path, uint startoffset = 0, uint endo
mn2Manifests.Add((Mn2Manifest)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(Mn2Manifest)));
handle.Free();
}

if (mn2Manifests.Count < 1) throw new Exception($@"Unsupported ME version: {path.SafeFileName()}");
var manifest = mn2Manifests.First();

meinfo.Size = endoffset == 0 ? (uint)stream.Length : endoffset - startoffset;
Expand All @@ -192,23 +204,31 @@ MEinfo GetMEFileInfo(Stream stream, string path, uint startoffset = 0, uint endo
private MEinfo LoadMEinfo(string path)
{
var stream = File.Open(path, FileMode.Open);
if (stream.Length < 1024 * 100)
{
stream.Close();
throw new Exception($"Too small file size: {path.SafeFileName()}");
}

stream.Seek(0x10, SeekOrigin.Begin);

var magic = new BinaryReader(stream).ReadBytes(4);

if (magic.SequenceEqual(new byte[] { 0x24, 0x46, 0x50, 0x54 })) // $FPT - ME file
{
Log("ME file added " + path.SafeFileName());
var meinfo = GetMEFileInfo(stream, path);
Log($"ME file added {path.SafeFileName()}", LogLevel.Info);
stream.Close();
return meinfo;
}
stream.Close();
throw new Exception("Invalid input file " + path.SafeFileName());
throw new Exception($"Invalid input file: {path.SafeFileName()}");
}

private void LoadBIOS(string path)
{

Log("----------------------------------------", LogLevel.Default);
BIOSfile = File.ReadAllBytes(path);
var stream = File.Open(path, FileMode.Open);
stream.Seek(0x10, SeekOrigin.Begin);
Expand All @@ -225,7 +245,7 @@ private void LoadBIOS(string path)
//var fmba = (flmap1 & 0xff) << 4;
if (nr >= 2)
{
//Log("BIOS image detected");
Log("Intel BIOS image detected! :D", LogLevel.Info);
stream.Seek(frba, SeekOrigin.Begin);
//FLREG0 = Flash Descriptor
//FLREG1 = BIOS
Expand All @@ -247,38 +267,16 @@ private void LoadBIOS(string path)
{
throw new Exception("The ME/TXE region is corrupted or missing");
}
Log("BIOS read successful! " + path.SafeFileName());
//Log(path);
//Log(string.Format("The ME/TXE region goes from {0:X8} to {1:X8}", BIOS_ME_start_offset, BIOS_ME_end_offset));



BIOS_ME_info = GetMEFileInfo(stream, path, BIOS_ME_start_offset, BIOS_ME_end_offset);

_mode = BIOS_ME_info.Major < 3 ? Mode.TXE : Mode.ME;
MEinBIOS_ver_label.Text = _mode + " version: " + BIOS_ME_info.Major + "." + BIOS_ME_info.Minor + "." +
BIOS_ME_info.Hotfix + "." + BIOS_ME_info.Build;
MEoffsetLabel.Text = _mode + " offset in BIOS: 0x" + BIOS_ME_start_offset.ToString("X8");
MEsizeInBIOSLabel.Text = _mode + " size: " + (BIOS_ME_end_offset - BIOS_ME_start_offset) + " bytes";
SuitableMEs.Text = "Suitable " + _mode + "s:";
Log("Mode: " + _mode);
UpdateComboBox();
//Log(MEinBIOS_ver_label.Text);
/*MEsComboBox.Items.Clear();
_validMEfiles.Clear();
foreach (var mefile in _meFiles)
{
if (BIOS_ME_info.Major == mefile.Major && (BIOS_ME_info.Minor == mefile.Minor || !MinorVer_checkBox.Checked) &&
BIOS_ME_end_offset - BIOS_ME_start_offset >= mefile.Size)
{
MEsComboBox.Items.Add(mefile.Major + "." + mefile.Minor + "." + mefile.Hotfix + "." + mefile.Build + " - " + mefile.Path.SafeFileName());
_validMEfiles.Add(mefile.Path);
}
}
if (MEsComboBox.Items.Count == 0) MEsComboBox.Items.Add("--none--");
MEsComboBox.SelectedIndex = 0;*/
//GetMSDM();
UpdateGUI();

Log("BIOS read successful! " + path.SafeFileName(), LogLevel.Info);
Log($"The {_mode} region goes from {BIOS_ME_start_offset:X8} to {BIOS_ME_end_offset:X8}", LogLevel.Info);

UpdateComboBox();

var offset = Find(BIOSfile, MSDM_table_pattern) + MSDM_offset;
if (offset - MSDM_offset != -1)
{
Expand All @@ -290,23 +288,20 @@ private void LoadBIOS(string path)
}
else
{
WinKeyTextBox.Text = "none";
WinKeyTextBox.Text = @"none";
}
stream.Close();
return;
}
stream.Close();
return;
throw new Exception("Number of partitions in file is less than 2! " + path.SafeFileName());
}
stream.Close();
ClearFields();
ClearGUI();
BIOSfile = null;
throw new Exception("Invalid input file " + path.SafeFileName());
}

//void GetMSDM()
//{

//}

void UpdateComboBox()
{
MEsComboBox.Items.Clear();
Expand All @@ -316,7 +311,7 @@ void UpdateComboBox()
if (BIOS_ME_info.Major == mefile.Major && (BIOS_ME_info.Minor == mefile.Minor || !MinorVer_checkBox.Checked) &&
BIOS_ME_end_offset - BIOS_ME_start_offset >= mefile.Size)
{
MEsComboBox.Items.Add(mefile.Major + "." + mefile.Minor + "." + mefile.Hotfix + "." + mefile.Build + " - " + mefile.Path.SafeFileName());
MEsComboBox.Items.Add($@"{mefile.Major}.{mefile.Minor}.{mefile.Hotfix}.{mefile.Build} - {mefile.Path.SafeFileName()}");
_validMEfiles.Add(mefile.Path);
}
}
Expand All @@ -335,14 +330,14 @@ private void OpenBIOSbutton_Click(object sender, EventArgs e)
}
catch (Exception exception)
{
Log(exception.Message);
Log(exception.Message, LogLevel.Error);
}
}
private void SaveButton_Click(object sender, EventArgs e)
{
if (BIOSfile == null || _validMEfiles.Count == 0)
{
Log("Nothing to save :(");
Log("Nothing to save :(", LogLevel.Warning);
return;
}
MEfile = File.ReadAllBytes(_validMEfiles[MEsComboBox.SelectedIndex]);
Expand All @@ -354,18 +349,18 @@ private void SaveButton_Click(object sender, EventArgs e)
{
AddExtension = true,
DefaultExt = "bin",
FileName = BIOSfilename.Replace(".bin", string.Empty) + " + " + _mode + " " + MEsComboBox.Text
FileName = $@"{BIOSfilename.Replace(".bin", string.Empty)} + {_mode} {MEsComboBox.Text}"
};

if (sfd.ShowDialog() != DialogResult.OK) return;
try
{
File.WriteAllBytes(sfd.FileName, BIOSfile);
Log("Saved to " + sfd.FileName);
Log("Saved to " + sfd.FileName, LogLevel.Info);
}
catch (Exception exception)
{
Log(exception.Message);
Log(exception.Message, LogLevel.Error);
}
}
static int Find(IList<byte> array, IList<byte> mask)
Expand Down Expand Up @@ -395,7 +390,7 @@ static int Find(IList<byte> array, IList<byte> mask)


private readonly List<string> _filesList = new List<string>();
private List<string> GetFileNames(string path)
private IEnumerable<string> GetFileNames(string path)
{
_filesList.Clear();
FileFinder(path);
Expand All @@ -416,33 +411,42 @@ private void FileFinder(string path)
}
catch (Exception exception)
{
Log(exception.Message + " " + path);
Log(exception.Message + " " + path, LogLevel.Warning);
}
}



private void button1_Click(object sender, EventArgs e)
{
return;
LoadDB();
return;
foreach (var file in GetFileNames(@"C:\Users\Nick\Google Диск\BIOS\"))
{
try
{
// LoadFile(file);
}
catch (Exception exception)
{
//Log(exception.Message);
}
//Log(file);
}

}
private void Log(string message)
private void Log(string message, LogLevel level)
{
DebugTextBox.Text += /*DateTime.Now + " - " + */message + "\n";
Color color;
switch (level)
{
case LogLevel.Info:
color = Color.Green;
break;
case LogLevel.Warning:
return;
color = Color.DarkOrange;
break;
case LogLevel.Error:
color = Color.Red;
break;
case LogLevel.Critical:
color = Color.Brown;
break;
case LogLevel.Default:
color = Color.Black;
break;
default:
color = Color.Black;
break;
}
DebugTextBox.AppendText(message, color);
}
private void DebugTextBox_TextChanged(object sender, EventArgs e)
{
Expand All @@ -458,7 +462,7 @@ private void ChangeMEFolderButton_Click(object sender, EventArgs e)
Properties.Settings.Default.MEdir = _MEdir;
Properties.Settings.Default.Save();
UpdateDB();
Text = "Intel ME/TXE injector - " + _MEdir + " - " + _meFiles.Count + " files";
UpdateGUI();
}

private void UpdateDB_Button_Click(object sender, EventArgs e)
Expand Down
3 changes: 3 additions & 0 deletions MEInject/MEInject/MainForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,9 @@
<metadata name="$this.Localizable" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="$this.Locked" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<data name="$this.AutoScaleDimensions" type="System.Drawing.SizeF, System.Drawing">
<value>6, 13</value>
</data>
Expand Down
Loading

0 comments on commit 547d291

Please sign in to comment.