using System; using System.Collections.Generic; using System.Text; namespace Pack89KData { public class Startup { static void Main(string[] args) { var asm = System.Reflection.Assembly.GetExecutingAssembly(); var asmName = asm.GetName(); Console.WriteLine(String.Format("{0} v{1}", asmName.Name, asmName.Version.ToString(3))); Console.WriteLine("Brandon Wilson"); Console.WriteLine(); Console.WriteLine("Injects a binary file of data at the end of the specified 68k FlashApp file."); Console.WriteLine("Resulting XXK must be re-signed, and only 8-character names should be used."); Console.WriteLine(); Console.WriteLine(String.Format("Usage: {0} [input dummy XXK filename] [output XXK filename]", asmName.Name)); Console.WriteLine(" [old name (8 chars)] [new name (8 chars)] [BIN file]"); try { if (args.Length != 5) { throw new InvalidOperationException("Invalid number of arguments!"); } else { string inputFileName = args[0]; string outputFileName = args[1]; string strToReplace = args[2]; string newName = args[3]; string binFile = args[4]; //Open the input file and load it var file = new ApplicationFile(); file.Load(inputFileName); //Load the binary file's data var bin = new System.IO.FileStream(binFile, System.IO.FileMode.Open); var dataToInject = new byte[bin.Length]; bin.Read(dataToInject, 0, dataToInject.Length); bin.Close(); //Do the replaceements _ReplaceData(file.Fields, strToReplace, newName, dataToInject); file.ApplicationName = newName; //Save the output file file.Save(outputFileName); Console.WriteLine(String.Format("File {0} written successfully.", outputFileName)); } } catch (Exception ex) { Console.WriteLine("ERROR: " + ex.ToString()); } } static void _ReplaceData(List fields, string strToReplace, string newName, byte[] dataToInject) { foreach (ApplicationField item in fields) { if (item.IsContainerField) { _ReplaceData(item.Fields, strToReplace, newName, dataToInject); } else { //Insert the data at the end of field 0x8070 (application data) if ((item.FieldId & 0xF0F0) == 0x8070) { byte[] temp = item.Contents; byte[] n = new byte[temp.Length + dataToInject.Length]; for (int i = 0; i < temp.Length; i++) n[i] = temp[i]; for (int i = 0; i < dataToInject.Length; i++) n[i + temp.Length] = dataToInject[i]; item.Contents = n; } //Replace all instances of strToReplace in this array with newName int offset = 0; while (offset < item.Contents.Length) { bool found = true; for (int i = 0; i < strToReplace.Length; i++) { if (item.Contents[offset+i] != strToReplace[i]) { found = false; break; } } if (found) { //Do the replace for (int j = 0; j < strToReplace.Length; j++) item.Contents[offset + j] = (byte)newName[j]; } offset++; } } } } } }