Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Windows.Forms;
- using System.IO;
- using jnsoft.ASAP2;
- using jnsoft.ELF;
- /// <summary>
- /// ASAP2ELFExample.
- ///
- /// - Loading an ELF file
- /// - Updating addresses of a corresponding A2L file.
- /// - (Re)wrie the A2L file.
- ///
- /// Usage: ASAP2ELFExample ELFFile.elf A2LFile.a2l
- /// </summary>
- class Program
- {
- /// <summary>
- /// Main function.
- /// </summary>
- /// <param name="args">The commandline arguments</param>
- /// <returns>Console application exitcode (0 if successful)</returns>
- static int Main(string[] args)
- {
- var orgColor = Console.ForegroundColor;
- try
- { // Open specified MDF file
- Console.ForegroundColor = ConsoleColor.White;
- if (args.Length != 2)
- { // no args -> present usage
- Console.WriteLine($"{Application.ProductName}({Application.ProductVersion})");
- Console.WriteLine("Read an ELF file and update the object addresses of a corresponding A2L file...");
- Console.WriteLine("Required input: An ELF (Executable and Linkable file format) file and an A2L File");
- Console.WriteLine($"Usage: {Application.ProductName} A2LFile.a2l ELFFile.elf");
- Console.WriteLine($"Example: {Application.ProductName} ASAP2Example.a2l ASAP2Example.elf");
- Console.WriteLine("Output is written to ASAP2Example.out.a2l");
- return -1;
- }
- using (var a2lParser = new A2LParser())
- { // Open A2L file
- if (!a2lParser.parse(args[0]))
- { // parsing failed
- Console.WriteLine($"File '{args[0]}' seems not to be an A2L file");
- return -1;
- }
- // Open ELF File
- using (var elfFile = ELFFile.open(args[1]))
- { // Compute synchronized values
- // Analyze DWARF
- elfFile.analyzeDwarf(a2lParser.Project?.getSymbols(), false);
- ulong dataSegStart, dataSegLen;
- var values = elfFile.getValuesToSynchronize(a2lParser, out dataSegStart, out dataSegLen);
- // Do the console output for each synchronized value
- foreach(var value in values)
- {
- switch (value.Type)
- {
- case UpdateType.AdjustAddress:
- case UpdateType.AdjustAddressAndSize:
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("Adjusting original address {0:X8} to {1:X8} on A2L Object {2}({3})"
- , value.AddressRef.Address, value.Address, value.AddressRef.Name, value.AddressRef.Type
- );
- break;
- case UpdateType.NotMatched:
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Removing not matched A2L Object {0}({1}) from target"
- , value.AddressRef.Name, value.AddressRef.Type
- );
- break;
- case UpdateType.Matched:
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("Unchanged A2L Object {0}({1})"
- , value.AddressRef.Name, value.AddressRef.Type
- );
- break;
- }
- }
- // output path
- var targetPath = Path.ChangeExtension(args[0], ".out.a2l");
- // Update A2L object model and write updated A2L file
- var countUpdates = elfFile.updateAndWriteA2L(values, targetPath, a2lParser, true);
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine("Updated {0} addresses of {1} known A2L objects.\nUpdated A2L in {2}",
- countUpdates, values.Count, targetPath
- );
- }
- }
- return 0;
- }
- catch (Exception ex)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine($"Something failed, file not found or unsupported!\nDetails:\n{ex.Message}");
- return -2;
- }
- finally { Console.ForegroundColor = orgColor; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement