public byte[] GetSection(FirmwareSection section) { byte[] ret = null; if (_sections.ContainsKey(section)) { ret = _sections[section]; } return ret; }
public bool FindPattern(byte?[] pattern, int startingOffset, out FirmwareSection section, out int address) { bool ret = false; section = FirmwareSection.Base; address = 0; foreach (var s in _sections) { for (int i = startingOffset; i < s.Value.Length; i++) { bool match = true; for (int j = 0; j < pattern.Length; j++) { if (((i + j) >= s.Value.Length) || ((s.Value[i + j] != pattern[j]) && (pattern[j].HasValue))) { match = false; break; } } if (match) { section = s.Key; address = i; ret = true; break; } } if (ret) { break; } } return ret; }
static void Main(string[] args) { try { _codeFiles = new Dictionary<FirmwareSection, string>(); _rstFiles = new Dictionary<FirmwareSection, string>(); //Assume success to start with Environment.ExitCode = (int)ExitCode.Success; var action = Action.None; //Parse command line arguments foreach (var arg in args) { var parts = arg.TrimStart(new char[] { '/' }).Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); switch (parts[0].ToLower()) { case "action": { action = (Action)Enum.Parse(typeof(Action), parts[1]); Console.WriteLine("Action: " + action.ToString()); break; } case "section": { _section = (FirmwareSection)Enum.Parse(typeof(FirmwareSection), parts[1]); Console.WriteLine("Section: " + _section.ToString()); break; } case "firmware": { _firmwareImage = parts[1]; Console.WriteLine("Firmware image: " + _firmwareImage); _CheckFirmwareImage(); break; } case "output": { _outputFile = parts[1]; Console.WriteLine("Output file: " + _outputFile); break; } default: { _ParseFileNames(ref _codeFiles, "code", parts[0], parts[1]); _ParseFileNames(ref _rstFiles, "rst", parts[0], parts[1]); break; } } } //Firmware image file name is always required if (string.IsNullOrEmpty(_firmwareImage)) { throw new ArgumentException("No/Invalid firmware image file name specified"); } switch (action) { case Action.GenerateHFile: { if (string.IsNullOrEmpty(_outputFile)) { throw new ArgumentException("No/Invalid output file name specified"); } Console.WriteLine("Generating .h file..."); _GenerateHFile(); break; } case Action.ApplyPatches: { //Check required arguments for this action if (string.IsNullOrEmpty(_outputFile)) { throw new ArgumentException("No/Invalid output file name specified"); } if (_codeFiles.Count == 0) { throw new ArgumentException("No code file name(s) specified"); } if (_rstFiles.Count == 0) { throw new ArgumentException("No/Invalid RST file name specified"); } Console.WriteLine("Applying patches..."); _ApplyPatches(); break; } case Action.FindFreeBlock: { //Check required arguments for this action if (_section == FirmwareSection.None) { throw new ArgumentException("No/Invalid section specified"); } Console.WriteLine("Retriving free space..."); _GetFreeSpaceToFile(); break; } default: throw new ArgumentException("No/Invalid action specified"); } Console.WriteLine("Done."); } catch (Exception ex) { //Uh-oh... Environment.ExitCode = (int)ExitCode.Failure; var asm = System.Reflection.Assembly.GetExecutingAssembly(); var asmName = asm.GetName(); Console.WriteLine(asmName.Name + " v" + asmName.Version.ToString(3)); Console.WriteLine("Actions:"); Console.WriteLine("\tGenerateHFile\tGenerates C .h file of common XRAM & function equates."); Console.WriteLine("\tFindFreeBlock\tWrites amount of free space for a section to file."); Console.WriteLine("\tApplyPatches\tApplies available patches from code into firmware image."); Console.WriteLine(); Console.WriteLine("FATAL: " + ex.ToString()); } }
public bool FindPattern(byte?[] pattern, out FirmwareSection section, out int address) { return FindPattern(pattern, 0, out section, out address); }
public int FindLastFreeChunk(FirmwareSection section) { int ret = -1; if (_sections.ContainsKey(section)) { var data = _sections[section]; var repeating = data[data.Length - 1]; ret = data.Length - 2; while (data[ret] == repeating) { ret--; if (ret < 0) { break; } } } return ++ret; }