Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 124 additions & 43 deletions BK7231Flasher/BK7231Flasher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Threading;

namespace BK7231Flasher
Expand All @@ -15,6 +16,8 @@ public class BK7231Flasher : BaseFlasher
MemoryStream ms;
string lastEncryptionKey;
public static int SECTOR_SIZE = 0x1000;
public static int BLOCK_SIZE = 0x10000;
public static int SECTORS_PER_BLOCK = BLOCK_SIZE / SECTOR_SIZE;
public static int FLASH_SIZE = 0x200000;
public static int BOOTLOADER_SIZE = 0x11000;
public static int TOTAL_SECTORS = FLASH_SIZE / SECTOR_SIZE;
Expand Down Expand Up @@ -886,7 +889,30 @@ bool doGenericSetup()
lastEncryptionKey = "";
if (chipType != BKType.BK7231T && chipType != BKType.BK7231U && chipType != BKType.BK7252)
{
if (doUnprotect())
byte[] chipIdRaw;
if(chipType == BKType.BK7236 || chipType == BKType.BK7258)
{
chipIdRaw = ReadFlashReg(0x44010000 + (0x1 << 2)) ?? new byte[] { 0, 0, 0, 0 };
}
else
{
chipIdRaw = ReadFlashReg(0x800000) ?? new byte[] { 0, 0, 0, 0 };
}
chipIdRaw = chipIdRaw.Reverse().ToArray();
string chipId = "";
// should be 0x7238 for BK7238, 0x7231c for BK7231N, 0x7236 for both BK7236 and BK7258
foreach(var ch in chipIdRaw)
{
if(ch == 0 || ch == 1)
continue;
chipId += $"{ch:x}";
}
// do something if selected type != chip id?
if(string.IsNullOrEmpty(chipId))
addWarning($"Failed to get chip ID!" + Environment.NewLine);
else
addLog($"Chip ID: 0x{chipId}" + Environment.NewLine);
if(doUnprotect())
{
return false;
}
Expand Down Expand Up @@ -940,36 +966,9 @@ bool doEraseInternal(int startSector, int sectors)
logger.setProgress(0, sectors);
logger.setState("Erasing...", Color.Transparent);
addLog("Going to do erase, start " + startSector +", sec count " + sectors +"!" + Environment.NewLine);
for (int sec = 0; sec < sectors; sec++)
if(!eraseRange(startSector, sectors))
{
int secAddr = startSector + SECTOR_SIZE * sec;
int tries = 0;
while (true)
{
tries++;
// 4K erase
bool bOk = eraseSector(secAddr, 0x20);
addLog("Erasing sector " + secAddr + "...");
if (bOk == false)
{
if(tries > 5)
{
logger.setState("Erase failed.", Color.Red);
addError(" Erasing sector " + secAddr + " failed!" + Environment.NewLine);
return false;
}
else
{
addWarning(" failed, will retry! ");
}
}
else
{
break;
}
}
addLog(" ok! ");
logger.setProgress(sec+1, sectors);
return false;
}
addLog(Environment.NewLine);
addLog("All selected sectors erased!" + Environment.NewLine);
Expand Down Expand Up @@ -1220,20 +1219,9 @@ bool doWriteInternal(int startSector, byte []data)
{
return false;
}
for (int sec = 0; sec < sectors; sec++)
if(!eraseRange(startSector, sectors))
{
int secAddr = startSector + SECTOR_SIZE * sec;
// 4K erase
bool bOk = eraseSector(secAddr, 0x20);
addLog("Erasing sector " + formatHex(secAddr) + "...");
if (bOk == false)
{
logger.setState("Erase sector failed!", Color.Red);
addError(" Erasing sector " + secAddr + " failed!" + Environment.NewLine);
return false;
}
addLog(" ok! ");

return false;
}
addLog(Environment.NewLine);
addLog("All selected sectors erased!" + Environment.NewLine);
Expand Down Expand Up @@ -1860,5 +1848,98 @@ bool setBaudrate(int baudrate, int delay_ms)
serial.BaudRate = prev;
return false;
}

bool eraseRange(int startSector, int sectors)
{
int current = startSector / SECTOR_SIZE;
int end = current + sectors;

bool erase4k()
{
int tries = 0;
int addr = current * SECTOR_SIZE;
while(true)
{
addLog("Erasing sector " + formatHex(addr) + "...");
bool bOk = eraseSector(addr, 0x20);
if(!bOk)
{
if(tries > 5)
{
logger.setState("Erase failed.", Color.Red);
addError(" Erasing sector " + addr + " failed!" + Environment.NewLine);
return false;
}
else
{
addWarning(" failed, will retry! ");
}
}
else
{
break;
}
}

current++;
addLog(" ok! ");
logger.setProgress(current + 1, sectors);
return true;
}

// erase sectors until 64KB-aligned
while(current < end &&
(current % SECTORS_PER_BLOCK) != 0)
{
if(!erase4k())
{
return false;
}
}

// erase 64k while possible
while((end - current) >= SECTORS_PER_BLOCK)
{
int tries = 0;
int addr = current * SECTOR_SIZE;
while(true)
{
addLog("Erasing block " + formatHex(addr) + "...");
bool bOk = eraseSector(addr, 0xD8);
if(!bOk)
{
if(tries > 5)
{
logger.setState("Erase failed.", Color.Red);
addError(" Erasing block " + addr + " failed!" + Environment.NewLine);
return false;
}
else
{
addWarning(" failed, will retry! ");
}
}
else
{
break;
}
}

current += SECTORS_PER_BLOCK;
addLog(" ok! ");
logger.setProgress(current + 1, sectors);
}

// erase remaining sectors
while(current < end)
{
if(!erase4k())
{
return false;
}
}

return true;
}
}
}