public async void ValidateDialog() { if (this.FwPath != "" && File.Exists(this.FwPath)) { try { StringWriter sw = new StringWriter(); this.ValidateData = FwTools.Validate(this.FwPath, sw); this.FwParseLog = sw.ToString(); this.FwName = Path.GetFileName(this.FwPath); } catch (Exception ex) { this.ValidateData = new FwTools.FwInfo(); this.FwParseLog = ex.GetExceptionMsg(); } this.IsDialogOpen = true; await coordinator.ShowMetroDialogAsync(this, dialogValidate, dialogSettings); } else { MessageBox.Show(ERR1, "BSL430.NET", MessageBoxButton.OK, MessageBoxImage.Warning); } this.IsDialogOpen = false; }
public void Compare() { string initPath = ""; try { initPath = Path.GetDirectoryName(this.FwPath); } catch (Exception) { } var ret = Helpers.Dialogs.OpenFileDialog(initPath, "Select Second Firmware Path", true, "", FW_PATH_FILTER); if (ret != "") { try { var(Equal, Match, BytesDiff) = FwTools.Compare(this.FwPath, ret); MessageBox.Show($"Firmwares are {((Equal) ? "" : "NOT")} equal!\n\n" + $"Match: {(Match * 100.0):F1} %\nBytes Diff: {BytesDiff}", "BSL430.NET", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ex) { MessageBox.Show(ex.GetExceptionMsg(), "BSL430.NET", MessageBoxButton.OK, MessageBoxImage.Error); } } }
public void Convert() { if (this.ConvertDestination == "" || !Directory.Exists(Path.GetDirectoryName(this.ConvertDestination))) { MessageBox.Show(ERR3, "BSL430.NET", MessageBoxButton.OK, MessageBoxImage.Warning); return; } try { var(Fw, Format) = FwTools.Convert(this.FwPath, this.ConvertFormat, this.ConvertFillFF, BslSettings.Instance.FwWriteLineLength); using (StreamWriter wr = new StreamWriter(this.ConvertDestination, false)) { wr.Write(Fw); } MessageBox.Show($"{CONVERT_SUCCESS}\n{this.ConvertDestination}", "BS430.NET", MessageBoxButton.OK, MessageBoxImage.Information); coordinator.HideMetroDialogAsync(this, dialogConvert, dialogSettings); } catch (Exception ex) { MessageBox.Show(ex.GetExceptionMsg(), "BSL430.NET", MessageBoxButton.OK, MessageBoxImage.Error); } }
public StatusEx DownloadSimple(string OutputPath, FwTools.FwFormat Format, Bsl430NetDevice Dev, byte[] Password, int AddrStart, int DataSize) { using (var dev = new BSL430NET(Dev)) { dev.ProgressChanged += new Bsl430NetEventHandler(delegate (object s, Bsl430NetEventArgs args) { Console.WriteLine($"{args.Progress} {args.Report}"); }); dev.SetBaudRate(BaudRate.BAUD_115200); dev.SetMCU(MCU.MSP430_F5xx); dev.SetInvokeMechanism(InvokeMechanism.SHARED_JTAG); StatusEx ret = dev.Download(Password, AddrStart, DataSize, out List <byte> Data); string fw = FwTools.Create(Data, AddrStart, Format); using (StreamWriter wr = new StreamWriter(OutputPath)) { wr.Write(fw); } Console.WriteLine($"{ret}\n{fw}"); return(ret); } }
public void FirmwareGetPassword(TestData.Fw fw_paths) { foreach (TestData.Fw fw in fw_paths.GetFlags()) { var ret = FwTools.GetPassword(TestData.GetFwPath(fw)); Assert.NotNull(ret); } }
public void FirmwareCombine(FwTools.FwFormat format, TestData.Fw fw_path1, TestData.Fw fw_path2) { foreach (TestData.Fw fw in fw_path1.GetFlags()) { var(Fw, Format1, Format2) = FwTools.Combine(TestData.GetFwPath(fw), TestData.GetFwPath(fw_path2), format); Assert.True(Fw != ""); } }
public void FirmwareConvertTo(FwTools.FwFormat format, TestData.Fw fw_paths) { foreach (TestData.Fw fw in fw_paths.GetFlags()) { var(Fw, Format) = FwTools.Convert(TestData.GetFwPath(fw), format); Assert.True(Fw.Length > 0); } }
public void FirmwareValidate(TestData.Fw fw_paths) { foreach (TestData.Fw fw in fw_paths.GetFlags()) { FwTools.FwInfo ret = FwTools.Validate(TestData.GetFwPath(fw)); Assert.NotNull(ret); Assert.True(ret.SizeCode > 0); } }
public void FirmwareParse(FwTools.FwFormat format, TestData.Fw fw_paths, bool fill_FF) { foreach (TestData.Fw fw in fw_paths.GetFlags()) { FwTools.Firmware ret = FwTools.Parse(TestData.GetFwPath(fw), format, fill_FF); Assert.NotNull(ret); Assert.NotNull(ret.Nodes); Assert.NotNull(ret.Info); Assert.True(ret.Nodes.Count > 0); Assert.True(ret.Info.SizeCode > 0); } }
public void FirmwareCreate(FwTools.FwFormat format, TestData.Fw fw_paths) { foreach (TestData.Fw fw in fw_paths.GetFlags()) { FwTools.Firmware parsed = FwTools.Parse(TestData.GetFwPath(fw), FwTools.FwFormat.AUTO); string ret = FwTools.Create(parsed, format); Assert.NotNull(parsed); Assert.NotNull(parsed.Nodes); Assert.NotNull(parsed.Info); Assert.True(parsed.Nodes.Count > 0); Assert.True(parsed.Info.SizeCode > 0); Assert.True(ret.Length > 0); } }
public async void GetPasswordDialog() { if (this.FwPath != "" && File.Exists(this.FwPath)) { try { var password = FwTools.GetPassword(this.FwPath); if (password == null) { MessageBox.Show(ERR4, "BSL430.NET", MessageBoxButton.OK, MessageBoxImage.Error); } else { if (this.PasswordMCU == MCU.MSP430_F1xx || this.PasswordMCU == MCU.MSP430_F2xx || this.PasswordMCU == MCU.MSP430_F4xx || this.PasswordMCU == MCU.MSP430_G2xx3) { this.PasswordHeader = "20-byte old 1xx/2xx/4xx BSL password"; this.PasswordData = BitConverter.ToString(password.Password20Byte).Replace("-", ""); } else if (this.PasswordMCU == MCU.MSP430_F543x_NON_A) { this.PasswordHeader = "16-byte special F543x (non A) BSL password"; this.PasswordData = BitConverter.ToString(password.Password16Byte).Replace("-", ""); } else { this.PasswordHeader = "32-byte standard 5xx/6xx BSL password"; this.PasswordData = BitConverter.ToString(password.Password32Byte).Replace("-", ""); } this.IsDialogOpen = true; await coordinator.ShowMetroDialogAsync(this, dialogGetPassword, dialogSettings); } } catch (Exception ex) { MessageBox.Show(ex.GetExceptionMsg(), "BSL430.NET", MessageBoxButton.OK, MessageBoxImage.Error); } } else { MessageBox.Show(ERR1, "BSL430.NET", MessageBoxButton.OK, MessageBoxImage.Warning); } this.IsDialogOpen = false; }
public void Combine() { string err = ""; if (this.CombineWith == "" || !File.Exists(this.CombineWith)) { err += $"{ERR2}\n"; } if (this.CombineDestination == "" || !Directory.Exists(Path.GetDirectoryName(this.CombineDestination))) { err += $"{ERR3}\n"; } if (err != "") { MessageBox.Show(err.TrimEnd('\n'), "BS430.NET", MessageBoxButton.OK, MessageBoxImage.Warning); return; } try { var(Fw, Format1, Format2) = FwTools.Combine(this.FwPath, this.CombineWith, this.CombineFormat, this.CombineFillFF, BslSettings.Instance.FwWriteLineLength); using (StreamWriter wr = new StreamWriter(this.CombineDestination, false)) { wr.Write(Fw); } MessageBox.Show($"{COMBINE_SUCCESS}\n{this.CombineDestination}", "BS430.NET", MessageBoxButton.OK, MessageBoxImage.Information); coordinator.HideMetroDialogAsync(this, dialogCombine, dialogSettings); } catch (Exception ex) { MessageBox.Show(ex.GetExceptionMsg(), "BSL430.NET", MessageBoxButton.OK, MessageBoxImage.Error); } }
public void HexView() { if (this.FwPath != "" && File.Exists(this.FwPath)) { try { FwTools.Firmware fw = null; if (hexViewClosed) { fw = FwTools.Parse(this.FwPath, FillFF: true); } if (this.hexView == null) { this.hexView = new HexView((IOnHexViewClose)this, themeProvider.IsDarkMode()) { DataContext = hexViewModel }; } if (hexViewClosed) { this.hexViewModel.FwPath = this.FwPath; this.hexViewModel.FwInfo = fw.Info; if (fw.Info != null && fw.Info.Format == FwTools.FwFormat.ELF) { this.hexViewModel.ELF = true; } else { this.hexViewModel.ELF = false; } this.IhexView = this.hexView; this.hexView.SetAddrOffset(fw.Info.AddrFirst); this.hexViewModel.Iview = IhexView; if (this.hexViewModel.StreamCache != null) { this.hexViewModel.StreamCache.Close(); } this.hexViewModel.StreamCache = new MemoryStream(); fw.MemoryStream.CopyTo(hexViewModel.StreamCache); if (this.hexView.HexEditor.Stream != null) { this.hexView.HexEditor.Stream.Close(); } this.hexView.HexEditor.Stream = fw.MemoryStream; } this.IsDialogOpen = true; this.hexViewClosed = false; this.hexView.ShowDialog(); //this.hexView.Activate(); } catch (Exception ex) { MessageBox.Show(ex.GetExceptionMsg(), "BSL430.NET", MessageBoxButton.OK, MessageBoxImage.Error); } } else { MessageBox.Show(ERR1, "BSL430.NET", MessageBoxButton.OK, MessageBoxImage.Warning); } this.IsDialogOpen = false; }
private Status ParseFirmware(string firmware_path, out List <Rx_Block> rx_blocks, out FwTools.FwInfo fw_info) { fw_info = null; rx_blocks = new List <Rx_Block>(); FwTools.Firmware code_to_upload = null; FwTools.Firmware code_full_for_crc = null; bool fill_FF = false; if (protocol == Protocol.USB_5_6) { fill_FF = true; } try { code_to_upload = FwTools.Parse(firmware_path, FillFF: fill_FF); code_full_for_crc = FwTools.Parse(firmware_path, FillFF: true); // slice data to bigger blocks that fit buffer size int buff_size = GetBufferSize(protocol); if (fill_FF) { for (int i = 0; i < code_to_upload.Nodes.Count; i += buff_size) { rx_blocks.Add(new Rx_Block { addr = (int)code_to_upload.Nodes[i].Addr, data = code_to_upload.Nodes.Skip(i).Take(buff_size).Select(nod => nod.Data).ToArray() }); } } else { for (int i = 0, j = 0; i < code_to_upload.Nodes.Count; i++) { if (i == code_to_upload.Nodes.Count - 1 || code_to_upload.Nodes[i + 1].Addr - code_to_upload.Nodes[i].Addr > 1 || j + 1 >= buff_size) { rx_blocks.Add(new Rx_Block { addr = (int)code_to_upload.Nodes[i - j].Addr, data = code_to_upload.Nodes.Skip(i - j).Take(j + 1).Select(nod => nod.Data).ToArray() }); j = 0; } else { j++; } } } code_to_upload.Info.Crc16 = code_full_for_crc.Info.Crc16; fw_info = code_to_upload.Info; if (rx_blocks == null || rx_blocks.Count < 1) { throw new Bsl430NetException(445); } else { return(Utils.StatusCreate(0)); } } catch (Exception ex) { if (ex is Bsl430NetException) { return(Utils.StatusCreate(450, ((Bsl430NetException)ex).Status)); } else if (ex is FirmwareToolsException) { Status fw_ex = new Status() { Error = ((FirmwareToolsException)ex).Error, Msg = ((FirmwareToolsException)ex).Msg }; return(Utils.StatusCreate(450, fw_ex)); } else { return(Utils.StatusCreate(450, ex.Message)); } } }
public async void DownloadDetailed() { // Devices string HardcodedDevice = "FTDI1"; // hardcoded device name var DeviceFromScan = new Bsl430NetDevice("USB2"); // device from Scan methods Mode GenericDevice = Mode.UART_Serial; // not know device yet // Input data // Output firmware file paths string OutputPath1 = @"firmware1.hex"; // firmware output path 1 string OutputPath2 = @"firmware2.txt"; // firmware output path 2 string OutputPath3 = @"firmware3.s19"; // firmware output path 3 // Output firmware file formats FwTools.FwFormat OutputFormat1 = FwTools.FwFormat.INTEL_HEX; // Intel-HEX FwTools.FwFormat OutputFormat2 = FwTools.FwFormat.TI_TXT; // TI-TXT FwTools.FwFormat OutputFormat3 = FwTools.FwFormat.SREC; // SREC // First address - from where to start int AddrStart1 = 0x8000; // start address 1 - 0x8000 int AddrStart2 = 0x9999; // start address 2 - 0x9999 int AddrStart3 = 0xAACC; // start address 3 - 0xAACC // Byte size - how many bytes to download int DataSize1 = 32768; // byte size 1 - 0x8000 hex int DataSize2 = 1000; // byte size 2 - 1000 dec int DataSize3 = 1; // byte size 3 - single byte // BSL password, crucial parameter (read doc) byte[] Password1 = Enumerable.Repeat((byte)0xFF, 32).ToArray(); // standard 32 len byte[] Password2 = Enumerable.Repeat((byte)0xFF, 16).ToArray(); // F543x Non A only byte[] Password3 = Enumerable.Repeat((byte)0xFF, 20).ToArray(); // 20 byte old MCUs // Dev1 and dev2 use DefaultDevice - default device is entered once into // constructor, and then doesnt need to be filled again; the usual way. // Dev3 use generic approach, that can be useful when target multiple MCUs using (var dev1 = new BSL430NET(HardcodedDevice)) using (var dev2 = new BSL430NET(DeviceFromScan)) using (var dev3 = new BSL430NET(GenericDevice)) { // create simple event handler, prints progress (0-100) and report var BslEventHandler = new Bsl430NetEventHandler(delegate (object s, Bsl430NetEventArgs args) { Console.WriteLine($"{args.Progress} {args.Report}"); }); // assign same event handler for all devices dev1.ProgressChanged += BslEventHandler; dev2.ProgressChanged += BslEventHandler; dev3.ProgressChanged += BslEventHandler; // dev1 settings: fastest speed, F6xx MCU, standard 32 byte password Status stat1Baud = dev1.SetBaudRate(BaudRate.BAUD_115200); Status stat1Mcu = dev1.SetMCU(MCU.MSP430_F6xx); Status stat1Invoke = dev1.SetInvokeMechanism(InvokeMechanism.DEDICATED_JTAG); // dev2 settings: slowest speed, F543x MCU Non A -> 16 byte password! Status stat2Baud = dev2.SetBaudRate(BaudRate.BAUD_9600); Status stat2Mcu = dev2.SetMCU(MCU.MSP430_F543x_NON_A); Status stat2Invoke = dev2.SetInvokeMechanism(InvokeMechanism.SHARED_JTAG); // dev3 settings: middle speed, old G2xx3 MCU -> 20 byte password, old protocol Status stat3Baud = dev3.SetBaudRate(BaudRate.BAUD_38400); Status stat3Mcu = dev3.SetMCU(MCU.MSP430_G2xx3); Status stat3Invoke = dev3.SetInvokeMechanism(InvokeMechanism.SHARED_JTAG); // Run Download of 3 firmwares to 3 MCUs, BSL password is required, // Beware when 1xx/2xx/4xx old MCU is used, incorrect password could // wipe also Info A with calibration data. This is not case when // LOCK A bit is set, preventing erase, or if modern 5xx/6xx MCUs used var result1 = Task.FromResult <StatusEx>( dev1.Download(Password1, AddrStart1, DataSize1, out List <byte> Data1)); var result2 = Task.FromResult <StatusEx>( dev2.Download(Password2, AddrStart2, DataSize2, out List <byte> Data2)); var result3 = await Task.FromResult <StatusEx>( dev3.Download(Password3, AddrStart3, DataSize3, out List <byte> Data3, "COM1")); // After download create firmare string from raw data string fw1 = FwTools.Create(Data1, AddrStart1, OutputFormat1); string fw2 = FwTools.Create(Data2, AddrStart2, OutputFormat2); string fw3 = FwTools.Create(Data3, AddrStart3, OutputFormat3); // Finally write ready firmwares to disk using (StreamWriter wr1 = new StreamWriter(OutputPath1)) using (StreamWriter wr2 = new StreamWriter(OutputPath2)) using (StreamWriter wr3 = new StreamWriter(OutputPath3)) { wr1.Write(fw1); wr2.Write(fw2); wr3.Write(fw3); } // use overiden ToString method to output all important result data Console.WriteLine($"Dev1: {result1}\n{fw1}\n\n"); Console.WriteLine($"Dev2: {result2}\n{fw2}\n\n"); Console.WriteLine($"Dev3: {result3}\n{fw3}"); } }