alesi2000

ASAP2ELFExample

May 25th, 2017 (edited)
1,154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.52 KB | None | 0 0
  1. using jnsoft.ASAP2;
  2. using jnsoft.Helpers;
  3.  
  4. namespace jnsoft.Symbols.ELF.Examples;
  5.  
  6. /// <summary>
  7. /// ASAP2ELFExample.
  8. ///
  9. /// - Loading an ELF file
  10. /// - Updating addresses of a corresponding A2L file.
  11. /// - (Re)write the A2L file.
  12. ///
  13. /// Usage: ASAP2ELFExample ELFFile.elf A2LFile.a2l
  14. /// </summary>
  15. class Program
  16. {
  17.   /// <summary>
  18.   /// Main function.
  19.   /// </summary>
  20.   /// <param name="args">The commandline arguments</param>
  21.   /// <returns>Console application exitcode (0 if successful)</returns>
  22.   static int Main(string[] args)
  23.   {
  24.     var orgColor = Console.ForegroundColor;
  25.     try
  26.     {
  27.       Console.ForegroundColor = ConsoleColor.White;
  28.       if (args.Length != 2)
  29.       { // no args -> present usage
  30.         var appName = Extensions.AppName;
  31.         Console.WriteLine($"{appName}({Extensions.AppVersion})");
  32.         Console.WriteLine("Read an ELF file and update the object addresses of a corresponding A2L file...");
  33.         Console.WriteLine("Required input: An ELF (Executable and Linkable file format) file and an A2L File");
  34.         Console.WriteLine($"Usage: {appName} A2LFile.a2l ELFFile.elf");
  35.         Console.WriteLine($"Example: {appName} ASAP2Example.a2l ASAP2Example.elf");
  36.         Console.WriteLine("Output is written to ASAP2Example.out.a2l");
  37.         return -1;
  38.       }
  39.  
  40.       using var a2lParser = new A2LParser();
  41.       // Open A2L file
  42.       if (!a2lParser.parse(args[0]))
  43.       { // parsing failed
  44.         Console.WriteLine($"File '{args[0]}' seems not to be an A2L file");
  45.         return -1;
  46.       }
  47.  
  48.       // Open ELF File
  49.       using var elfFile = ELFFile.open(args[1]);
  50.  
  51.       // Compute synchronized values
  52.       var values = elfFile.getValuesToSynchronize(a2lParser
  53.         , out var dataSegStart, out var dataSegLen);
  54.  
  55.       // Do the console output for each synchronized value
  56.       foreach (var value in values)
  57.       {
  58.         switch (value.Type)
  59.         {
  60.           case UpdateType.AdjustAddress:
  61.           case UpdateType.AdjustAddressAndSize:
  62.             Console.ForegroundColor = ConsoleColor.Yellow;
  63.             Console.WriteLine("Adjusting original address {0:X8} to {1:X8} on A2L Object {2}({3})"
  64.               , value.AddressRef.Address, value.Address, value.AddressRef.Name, value.AddressRef.Type);
  65.             break;
  66.           case UpdateType.NotMatched:
  67.             Console.ForegroundColor = ConsoleColor.Red;
  68.             Console.WriteLine("Removing not matched A2L Object {0}({1}) from target"
  69.               , value.AddressRef.Name, value.AddressRef.Type);
  70.             break;
  71.           case UpdateType.Matched:
  72.             Console.ForegroundColor = ConsoleColor.Green;
  73.             Console.WriteLine("Unchanged A2L Object {0}({1})"
  74.               , value.AddressRef.Name, value.AddressRef.Type);
  75.             break;
  76.         }
  77.       }
  78.  
  79.       // output path
  80.       var targetPath = Path.ChangeExtension(args[0], ".out.a2l");
  81.  
  82.       // Update A2L object model and write updated A2L file
  83.       var countUpdates = elfFile.updateAndWriteA2L(values, targetPath, a2lParser, true);
  84.  
  85.       Console.ForegroundColor = ConsoleColor.White;
  86.       Console.WriteLine("Updated {0} addresses of {1} known A2L objects.\nUpdated A2L in {2}",
  87.         countUpdates, values.Count, targetPath);
  88.       return 0;
  89.     }
  90.     catch (Exception ex)
  91.     {
  92.       Console.ForegroundColor = ConsoleColor.Red;
  93.       Console.WriteLine($"Something failed, file not found or unsupported!\nDetails:\n{ex.Message}");
  94.       return -2;
  95.     }
  96.     finally { Console.ForegroundColor = orgColor; }
  97.   }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment