Advertisement
alesi2000

ASAP2ELFExample

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