private void LoadProgramButton_Click(object sender, RoutedEventArgs e) { var dialog = new OpenFileDialog(); if (dialog.ShowDialog() == true) { try { this.firmware = new MspFirmware(dialog.FileName); this.UpdateFirmwareSource(this.firmware); this.bytesToSend = MspBoot.EncodeProgram(this.firmware); this.loadedFileName.Text = $"{dialog.FileName} ({this.bytesToSend.Length} bytes)"; Console.WriteLine($"Sending: {this.bytesToSend.ToByteString("0x", ",")} Length = {this.bytesToSend.Length}"); this.sendDataAllButton.IsEnabled = (this.bytesToSend != null && this.isConnected); this.sendDataAllButton100.IsEnabled = (this.bytesToSend != null && this.isConnected); this.UpdateEncryptedDataTextBox(); this.UpdateEnryptedKey(); } catch (Exception ex) { BasicMessageBox.Show($"{ex.Message}\n{ex.StackTrace}", "Failed to load program"); } } }
/// <summary> /// Encodes a program into MSPBoot commands /// </summary> public static byte[] EncodeProgram(MspFirmware program) { var bytes = new List <byte>(); foreach (var(address, data) in program.Segments) { EncodeSegment(address, data, bytes); } bytes.Add(HEADER); bytes.Add(1); bytes.Add((byte)Commands.JUMP2APP); return(bytes.ToArray()); }
private void UpdateFirmwareSource(MspFirmware firmware) { this.loadedBytes.Document.Blocks.Clear(); var paragraph = new Paragraph { Padding = new Thickness(0.0), Margin = new Thickness(0.0) }; foreach (var(address, data) in firmware.Segments) { paragraph.Inlines.Add(new Span(new Run($"0x{address:x}..0x{address + data.Length:x} ({data.Length} bytes)\n"))); paragraph.Inlines.Add(new Span(new Run(data.ToByteString() + "\n\n"))); } this.loadedBytes.Document.Blocks.Add(paragraph); }