diff --git a/BK7231Flasher/BK7231Flasher.cs b/BK7231Flasher/BK7231Flasher.cs index f9dbdd8..78b8fb0 100644 --- a/BK7231Flasher/BK7231Flasher.cs +++ b/BK7231Flasher/BK7231Flasher.cs @@ -15,6 +15,7 @@ public enum BKType { BK7231T, BK7231N, + BK7231M, Detect, Invalid } @@ -42,6 +43,7 @@ public class BK7231Flasher public static int BOOTLOADER_SIZE = 0x11000; public static int TOTAL_SECTORS = FLASH_SIZE / SECTOR_SIZE; public static string TUYA_ENCRYPTION_KEY = "510fb093 a3cbeadc 5993a17e c7adeb03"; + public static string EMPTY_ENCRYPTION_KEY = "00000000 00000000 00000000 00000000"; void addLog(string s) { logger.addLog(s, Color.Black); @@ -421,6 +423,17 @@ void consumePending() float cfg_readTimeOutMultForSerialClass = 1.0f; float cfg_readTimeOutMultForLoop = 1.0f; int cfg_readReplyStyle = 0; + bool bOverwriteBootloader = false; + bool bSkipKeyCheck; + + public void setSkipKeyCheck(bool b) + { + bSkipKeyCheck = b; + } + public void setOverwriteBootloader(bool b) + { + bOverwriteBootloader = b; + } public void setReadTimeOutMultForSerialClass(float f) { this.cfg_readTimeOutMultForSerialClass = f; @@ -947,7 +960,7 @@ bool doGenericSetup() } // make sure it's clear lastEncryptionKey = ""; - if (chipType == BKType.BK7231N) + if (chipType == BKType.BK7231N || chipType == BKType.BK7231M) { if (doUnprotect()) { @@ -957,12 +970,37 @@ bool doGenericSetup() string key = readEncryptionKey(); addLog("Encryption key read done!" + Environment.NewLine); addLog("Encryption key: " + key+Environment.NewLine); - if(key != TUYA_ENCRYPTION_KEY) + string otherMode; + string expectedKey; + if (chipType == BKType.BK7231N) + { + otherMode = "BK7231M"; + expectedKey = TUYA_ENCRYPTION_KEY; + } + else + { + otherMode = "BK7231N"; + expectedKey = EMPTY_ENCRYPTION_KEY; + } + if (key != expectedKey) { addError("^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^" + Environment.NewLine); addError("WARNING! Non-standard encryption key!" + Environment.NewLine); addError("Please report to forum https://www.elektroda.com/rtvforum/forum51.html " + Environment.NewLine); + + if (chipType == BKType.BK7231N) + { + addError("Or just try using BK7231M mode " + Environment.NewLine); + } + else + { + + } addError("^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^" + Environment.NewLine); + if (bSkipKeyCheck == false) + { + return false; + } } lastEncryptionKey = key; } @@ -1206,7 +1244,7 @@ bool doTestReadWriteInternal(int startSector = 0x11000, int sectors = 10) { return false; } - if (chipType == BKType.BK7231N) + if (chipType == BKType.BK7231N || chipType == BKType.BK7231M) { if (doUnprotect()) { @@ -1372,6 +1410,7 @@ bool checkCRC(int startSector, int total, byte [] array) addError("CRC mismatch!" + Environment.NewLine); addError("Send by BK " + formatHex(bk_crc) + ", our CRC " + formatHex(our_crc) + Environment.NewLine); addError("Maybe you have wrong chip type set?" + Environment.NewLine); + addError("Did you set BK7231T but have in reality BK7231N or BK7231M?" + Environment.NewLine); return false; } addSuccess("CRC matches " + formatHex(bk_crc) + "!" + Environment.NewLine); @@ -1422,14 +1461,29 @@ bool doReadAndWriteInternal(int startSector, int sectors, string sourceFileName, return false; } addSuccess("Loaded " + data.Length + " bytes from " + sourceFileName + "..." + Environment.NewLine); - if (sourceFileName.Contains("_QIO_") && this.chipType == BKType.BK7231T && startSector == BK7231Flasher.BOOTLOADER_SIZE) + bool bSkipBootloader = false; + if (sourceFileName.Contains("_QIO_")) + { + if(bOverwriteBootloader == false) + { + startSector = BK7231Flasher.BOOTLOADER_SIZE; + bSkipBootloader = true; + } + if(this.chipType == BKType.BK7231T) + { + startSector = BK7231Flasher.BOOTLOADER_SIZE; + bSkipBootloader = true; + } + } + if (bSkipBootloader && startSector == BK7231Flasher.BOOTLOADER_SIZE) { // very hacky, but skip bootloader int length = data.Length - startSector; byte[] newData = new byte[length]; Array.Copy(data, startSector, newData, 0, length); data = newData; - addWarning("Using BK7231T hack to write QIO - just skip bootloader" + Environment.NewLine); + addWarning("Using hack to write QIO - just skip bootloader..." + Environment.NewLine); + addWarning("... so bootloader will not be overwritten!" + Environment.NewLine); } } addLog("Preparing to write data file to chip - resetting bus and baud..." + Environment.NewLine); @@ -1438,7 +1492,7 @@ bool doReadAndWriteInternal(int startSector, int sectors, string sourceFileName, { return false; } - if (chipType == BKType.BK7231N) + if (chipType == BKType.BK7231N || chipType == BKType.BK7231M) { if (doUnprotect()) { @@ -1676,7 +1730,7 @@ byte[] readSector(int addr) } uint calcCRC(int start, int end) { - if (chipType == BKType.BK7231N) + if (chipType == BKType.BK7231N || chipType == BKType.BK7231M) { end = end - 1; } @@ -1710,7 +1764,7 @@ bool isSectorModificationAllowed(int addr) return false; } addr %= FLASH_SIZE; - if (chipType == BKType.BK7231N) + if (chipType == BKType.BK7231N || chipType == BKType.BK7231M) return true; if (addr >= 0 && addr < BOOTLOADER_SIZE) { diff --git a/BK7231Flasher/FormMain.Designer.cs b/BK7231Flasher/FormMain.Designer.cs index 73ba32a..10606dc 100644 --- a/BK7231Flasher/FormMain.Designer.cs +++ b/BK7231Flasher/FormMain.Designer.cs @@ -40,6 +40,7 @@ private void InitializeComponent() this.comboBoxFirmware = new System.Windows.Forms.ComboBox(); this.tabControl1 = new System.Windows.Forms.TabControl(); this.tabPage1 = new System.Windows.Forms.TabPage(); + this.checkBoxOverwriteBootloader = new System.Windows.Forms.CheckBox(); this.buttonReadOBKConfig = new System.Windows.Forms.Button(); this.buttonWriteOBKConfig = new System.Windows.Forms.Button(); this.checkBoxReadOBKConfig = new System.Windows.Forms.CheckBox(); @@ -92,6 +93,7 @@ private void InitializeComponent() this.linkLabel2 = new System.Windows.Forms.LinkLabel(); this.label13 = new System.Windows.Forms.Label(); this.tabPage2 = new System.Windows.Forms.TabPage(); + this.buttonImportConfigFileDialog = new System.Windows.Forms.Button(); this.linkLabel5 = new System.Windows.Forms.LinkLabel(); this.label32 = new System.Windows.Forms.Label(); this.buttonTuyaConfig_CopyTextToClipBoard = new System.Windows.Forms.Button(); @@ -149,7 +151,7 @@ private void InitializeComponent() this.textBox_cfg_readTimeOutMultForSerialClass = new System.Windows.Forms.TextBox(); this.label25 = new System.Windows.Forms.Label(); this.timer100ms = new System.Windows.Forms.Timer(this.components); - this.buttonImportConfigFileDialog = new System.Windows.Forms.Button(); + this.checkBoxSkipKeyCheck = new System.Windows.Forms.CheckBox(); this.tabControl1.SuspendLayout(); this.tabPage1.SuspendLayout(); this.tabPagePageTool.SuspendLayout(); @@ -256,6 +258,8 @@ private void InitializeComponent() // // tabPage1 // + this.tabPage1.Controls.Add(this.checkBoxSkipKeyCheck); + this.tabPage1.Controls.Add(this.checkBoxOverwriteBootloader); this.tabPage1.Controls.Add(this.buttonReadOBKConfig); this.tabPage1.Controls.Add(this.buttonWriteOBKConfig); this.tabPage1.Controls.Add(this.checkBoxReadOBKConfig); @@ -297,6 +301,17 @@ private void InitializeComponent() this.tabPage1.Text = "Flasher"; this.tabPage1.UseVisualStyleBackColor = true; // + // checkBoxOverwriteBootloader + // + this.checkBoxOverwriteBootloader.AutoSize = true; + this.checkBoxOverwriteBootloader.Location = new System.Drawing.Point(548, 5); + this.checkBoxOverwriteBootloader.Name = "checkBoxOverwriteBootloader"; + this.checkBoxOverwriteBootloader.Size = new System.Drawing.Size(227, 17); + this.checkBoxOverwriteBootloader.TabIndex = 34; + this.checkBoxOverwriteBootloader.Text = "Overwrite bootloader (for N/M, don\'t use it)"; + this.checkBoxOverwriteBootloader.UseVisualStyleBackColor = true; + this.checkBoxOverwriteBootloader.CheckedChanged += new System.EventHandler(this.checkBoxOverwriteBootloader_CheckedChanged); + // // buttonReadOBKConfig // this.buttonReadOBKConfig.Location = new System.Drawing.Point(658, 95); @@ -364,7 +379,7 @@ private void InitializeComponent() // checkBoxAllowBackup // this.checkBoxAllowBackup.AutoSize = true; - this.checkBoxAllowBackup.Location = new System.Drawing.Point(760, 8); + this.checkBoxAllowBackup.Location = new System.Drawing.Point(426, 5); this.checkBoxAllowBackup.Name = "checkBoxAllowBackup"; this.checkBoxAllowBackup.Size = new System.Drawing.Size(125, 17); this.checkBoxAllowBackup.TabIndex = 27; @@ -464,7 +479,7 @@ private void InitializeComponent() // checkBoxShowAdvanced // this.checkBoxShowAdvanced.AutoSize = true; - this.checkBoxShowAdvanced.Location = new System.Drawing.Point(612, 8); + this.checkBoxShowAdvanced.Location = new System.Drawing.Point(279, 6); this.checkBoxShowAdvanced.Name = "checkBoxShowAdvanced"; this.checkBoxShowAdvanced.Size = new System.Drawing.Size(141, 17); this.checkBoxShowAdvanced.TabIndex = 17; @@ -840,6 +855,16 @@ private void InitializeComponent() this.tabPage2.DragDrop += new System.Windows.Forms.DragEventHandler(this.tabPage2_DragDrop); this.tabPage2.DragEnter += new System.Windows.Forms.DragEventHandler(this.tabPage2_DragEnter); // + // buttonImportConfigFileDialog + // + this.buttonImportConfigFileDialog.Location = new System.Drawing.Point(18, 43); + this.buttonImportConfigFileDialog.Name = "buttonImportConfigFileDialog"; + this.buttonImportConfigFileDialog.Size = new System.Drawing.Size(185, 23); + this.buttonImportConfigFileDialog.TabIndex = 15; + this.buttonImportConfigFileDialog.Text = "Open config with open file dialog instead..."; + this.buttonImportConfigFileDialog.UseVisualStyleBackColor = true; + this.buttonImportConfigFileDialog.Click += new System.EventHandler(this.buttonImportConfigFileDialog_Click); + // // linkLabel5 // this.linkLabel5.AutoSize = true; @@ -1380,15 +1405,16 @@ private void InitializeComponent() this.timer100ms.Enabled = true; this.timer100ms.Tick += new System.EventHandler(this.timer100ms_Tick); // - // buttonImportConfigFileDialog + // checkBoxSkipKeyCheck // - this.buttonImportConfigFileDialog.Location = new System.Drawing.Point(18, 43); - this.buttonImportConfigFileDialog.Name = "buttonImportConfigFileDialog"; - this.buttonImportConfigFileDialog.Size = new System.Drawing.Size(185, 23); - this.buttonImportConfigFileDialog.TabIndex = 15; - this.buttonImportConfigFileDialog.Text = "Open config with open file dialog instead..."; - this.buttonImportConfigFileDialog.UseVisualStyleBackColor = true; - this.buttonImportConfigFileDialog.Click += new System.EventHandler(this.buttonImportConfigFileDialog_Click); + this.checkBoxSkipKeyCheck.AutoSize = true; + this.checkBoxSkipKeyCheck.Location = new System.Drawing.Point(772, 5); + this.checkBoxSkipKeyCheck.Name = "checkBoxSkipKeyCheck"; + this.checkBoxSkipKeyCheck.Size = new System.Drawing.Size(100, 17); + this.checkBoxSkipKeyCheck.TabIndex = 35; + this.checkBoxSkipKeyCheck.Text = "Skip key check"; + this.checkBoxSkipKeyCheck.UseVisualStyleBackColor = true; + this.checkBoxSkipKeyCheck.CheckedChanged += new System.EventHandler(this.checkBoxSkipKeyCheck_CheckedChanged); // // FormMain // @@ -1543,6 +1569,8 @@ private void InitializeComponent() private System.Windows.Forms.LinkLabel linkLabel5; private System.Windows.Forms.Label label32; private System.Windows.Forms.Button buttonImportConfigFileDialog; + private System.Windows.Forms.CheckBox checkBoxOverwriteBootloader; + private System.Windows.Forms.CheckBox checkBoxSkipKeyCheck; } } diff --git a/BK7231Flasher/FormMain.cs b/BK7231Flasher/FormMain.cs index f6f0391..109a407 100644 --- a/BK7231Flasher/FormMain.cs +++ b/BK7231Flasher/FormMain.cs @@ -142,6 +142,7 @@ private void Form1_Load(object sender, EventArgs e) comboBoxChipType.Items.Add(BKType.BK7231T); comboBoxChipType.Items.Add(BKType.BK7231N); + comboBoxChipType.Items.Add(BKType.BK7231M); comboBoxChipType.SelectedIndex = 0; @@ -397,7 +398,10 @@ void createFlasher() flasher.setReadReplyStyle(cfg_readReplyStyle); flasher.setReadTimeOutMultForLoop(cfg_readTimeOutMultForLoop); flasher.setReadTimeOutMultForSerialClass(cfg_readTimeOutMultForSerialClass); + flasher.setOverwriteBootloader(checkBoxOverwriteBootloader.Checked); + flasher.setSkipKeyCheck(checkBoxSkipKeyCheck.Checked); } + void testWrite() { clearUp(); @@ -567,6 +571,10 @@ public static string getFirmwarePrefix(BKType t) { return ("OpenBK7231T_UA_"); } + if (t == BKType.BK7231M) + { + return ("OpenBK7231M_QIO_"); + } return "Error_Firmware"; } public bool checkFirmwareForCurType(string s) @@ -594,6 +602,17 @@ public bool checkFirmwareForCurType(string s) return true; } } + if (curType == BKType.BK7231M) + { + if (s.StartsWith("OpenBK7231M_UA_")) + { + return true; + } + if (s.StartsWith("OpenBK7231M_QIO_")) + { + return true; + } + } /*string prefix = getFirmwarePrefix(curType); if (s.StartsWith(prefix)) { @@ -1257,5 +1276,22 @@ private void FormMain_FormClosing(object sender, FormClosingEventArgs e) killScanner(); } + private void checkBoxOverwriteBootloader_CheckedChanged(object sender, EventArgs e) + { + if(checkBoxOverwriteBootloader.Checked == true) + { + DialogResult res = MessageBox.Show("This will break bootloader if used incorrectly, do you have a backup? Are you sure?", + "Are you sure?", MessageBoxButtons.YesNo); + if(res == DialogResult.No) + { + checkBoxOverwriteBootloader.Checked = false; + } + } + } + + private void checkBoxSkipKeyCheck_CheckedChanged(object sender, EventArgs e) + { + + } } }