using System; using System.Collections.Generic; using System.Security.Cryptography; using System.Text; namespace GenerateBootImage { class Startup { private const int _MIN_ARGUMENTS = 2; private const int _MAX_ARGUMENTS = 3; private const int _PAGE_SIZE = 0x4000; private const int _HASH_SIZE = 0x10; static void Main(string[] args) { if (args.Length < _MIN_ARGUMENTS || args.Length > _MAX_ARGUMENTS) { _DisplayUsage(); } else { try { //Open the first dump, verify its size, get the data, and hash it var buffer1 = new byte[_PAGE_SIZE]; var rdr1 = new System.IO.FileStream(args[1], System.IO.FileMode.Open); if (rdr1.Length != _PAGE_SIZE) throw new InvalidOperationException(String.Format("First dump is not {0} bytes!", _PAGE_SIZE)); rdr1.Read(buffer1, 0, _PAGE_SIZE); rdr1.Close(); var d1 = MD5.Create(); var hash1 = d1.ComputeHash(buffer1, 0, _PAGE_SIZE); //If we have it, open the second dump, verify its size, get the data, and hash it var buffer2 = new byte[_PAGE_SIZE]; var d2 = MD5.Create(); byte[] hash2 = null; if (args.Length > 2) { var rdr2 = new System.IO.FileStream(args[2], System.IO.FileMode.Open); if (rdr2.Length != _PAGE_SIZE) throw new InvalidOperationException(String.Format("Second dump is not {0} bytes!", _PAGE_SIZE)); rdr2.Read(buffer2, 0, _PAGE_SIZE); rdr2.Close(); hash2 = d2.ComputeHash(buffer2, 0, _PAGE_SIZE); } int outputFileSize = _PAGE_SIZE + _HASH_SIZE; if (args.Length > 2) outputFileSize *= 2; //Now write the output file var outfile = new System.IO.FileStream(args[0], System.IO.FileMode.Create); outfile.Write(buffer1, 0, _PAGE_SIZE); outfile.Write(hash1, 0, _HASH_SIZE); if (args.Length > 2) { outfile.Write(buffer2, 0, _PAGE_SIZE); outfile.Write(hash2, 0, _HASH_SIZE); } outfile.Close(); } catch (Exception ex) { Console.WriteLine("ERROR: " + ex.Message + "\r\n" + ex.ToString()); } } } static void _DisplayUsage() { Console.WriteLine("GenerateBootImage"); Console.WriteLine("Brandon Wilson"); Console.WriteLine(); Console.WriteLine("Generates a boot image BIN file given a page 0x1F/0x3F/0x7F (and optionally 0x2F/0x6F) dump(s)."); Console.WriteLine("Usage: GenerateBootImage.exe [output BIN filename] [page 0x1F/0x3F/0x7F image]"); Console.WriteLine(" [page 0x2F/0x6F image]"); } static void _ReverseHash(ref byte[] hash) { for (int i = 0; i < hash.Length / 2; i++) { var tmp = hash[i]; hash[i] = hash[hash.Length - i - 1]; hash[hash.Length - i - 1] = tmp; } } } }