private void PackageFolderChooseButton_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Заголовки прошивок | .Header"; ofd.DefaultExt = ".Header"; ofd.InitialDirectory = WorkDirectory; if (ofd.ShowDialog(this) == DialogResult.OK) { if (Path.GetFileName(ofd.FileName) != ".Header") { MessageBox.Show(this, "Нужно выбрать файл .Header", "Неверно выбран файл"); } else { var newScriptHolder = new ScriptElementsHolder(); if (newScriptHolder.loadFrom(ofd.FileName, PackLogger)) { ScriptHolder = newScriptHolder; FirmwareDir = Path.GetDirectoryName(ofd.FileName); PackageFolderLabel.Text = "Папка сборки : " + FirmwareDir; fillImagesPanel(ScriptHolder.getPartitions()); } PackButton.Enabled = true; PackingProtocolTextBox.Clear(); } } }
private void FirmwareChooseComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (FirmwareChooseComboBox.SelectedIndex == -1) { return; } SourceFirmwareFilename = Path.Combine(WorkDirectory, (string)FirmwareChooseComboBox.SelectedItem); ScriptHolder = new ScriptElementsHolder(); if (ScriptHolder.loadFrom(SourceFirmwareFilename, UnpackLogger)) { fillPartitionsPanel(ScriptHolder.getPartitions()); } }
public void pack(ScriptElementsHolder scriptHolder, Dictionary <string, Partition> partitions, IMessageLogger messageLogger) { ScriptElementsHolder newScriptHolder = new ScriptElementsHolder(); bool fullPackage = true; List <string> writtenPartitions = new List <string>(); string newFirmwareFilename = Directory + ".bin"; newFirmwareFilename = Path.Combine(Path.GetDirectoryName(newFirmwareFilename), "new_" + Path.GetFileName(newFirmwareFilename)); using (FileStream outputStream = new FileStream(newFirmwareFilename, FileMode.Create, FileAccess.Write)) { messageLogger.logMessage("Сборка прошивки."); for (int i = 0; i < 16 * 1024; i++) { outputStream.WriteByte(0xFF); } foreach (IHeaderScriptElement element in scriptHolder.Elements) { if (element is InformationalBlock || element is PartionsListSection) { newScriptHolder.Elements.Add(element); } else if (element is WriteFileCommand command) { if (writtenPartitions.Contains(command.PartitionName)) { continue; } writtenPartitions.Add(command.PartitionName); if (partitions.TryGetValue(command.PartitionName, out Partition partition) && File.Exists(Path.Combine(Directory, command.PartitionName + ".img"))) { var newChunks = partition.pack(Directory, outputStream, messageLogger); newScriptHolder.Elements.AddRange(newChunks); } else { fullPackage = false; } } } messageLogger.logMessage("Расчет контрольной суммы бинарной части прошивки."); newScriptHolder.setFirmwareBodyCrc(Partition.computeCrc32(outputStream.Name, 16 * 1024, outputStream.Length - 16 * 1024)); messageLogger.logMessage("Добавление штампа в заголовок."); if (!string.IsNullOrEmpty(Title)) { newScriptHolder.setTitle(Title); } messageLogger.logMessage("Запись заголовка."); outputStream.Seek(0, SeekOrigin.Begin); if (fullPackage) { newScriptHolder.writeTo(outputStream); } else { newScriptHolder.writeSelectedPartitionsTo(outputStream); } } using (FileStream outputStream = new FileStream(newFirmwareFilename, FileMode.Append, FileAccess.Write, FileShare.Read)) { messageLogger.logMessage("Расчет и запись контрольных сумм."); writeFinalCrc(outputStream, scriptHolder.CrcType, newScriptHolder.FirmwareBodyCrc); messageLogger.logMessage("Добавление финальной подписи."); string starter = (newScriptHolder.Elements[0] is InformationalBlock firstComment) ? firstComment.getStarter() : null; if (starter == null) { starter = "# Alytona FmWare"; } outputStream.Write(Encoding.ASCII.GetBytes(starter), 0, 16); } }