Skip to content

Commit

Permalink
bk7231m flashing logicc
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Jun 5, 2024
1 parent 96b1c1b commit c537ce2
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 19 deletions.
70 changes: 62 additions & 8 deletions BK7231Flasher/BK7231Flasher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public enum BKType
{
BK7231T,
BK7231N,
BK7231M,
Detect,
Invalid
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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())
{
Expand All @@ -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;
}
Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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())
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
{
Expand Down
50 changes: 39 additions & 11 deletions BK7231Flasher/FormMain.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions BK7231Flasher/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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)
{

}
}
}

0 comments on commit c537ce2

Please sign in to comment.