Skip to content

Commit 74e8bba

Browse files
Merge pull request #86 from NonPIayerCharacter/blockerase
Beken by block erase
2 parents fb6bec5 + 5185c6a commit 74e8bba

File tree

1 file changed

+124
-43
lines changed

1 file changed

+124
-43
lines changed

BK7231Flasher/BK7231Flasher.cs

Lines changed: 124 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Drawing;
44
using System.IO;
55
using System.IO.Ports;
6+
using System.Linq;
67
using System.Threading;
78

89
namespace BK7231Flasher
@@ -15,6 +16,8 @@ public class BK7231Flasher : BaseFlasher
1516
MemoryStream ms;
1617
string lastEncryptionKey;
1718
public static int SECTOR_SIZE = 0x1000;
19+
public static int BLOCK_SIZE = 0x10000;
20+
public static int SECTORS_PER_BLOCK = BLOCK_SIZE / SECTOR_SIZE;
1821
public static int FLASH_SIZE = 0x200000;
1922
public static int BOOTLOADER_SIZE = 0x11000;
2023
public static int TOTAL_SECTORS = FLASH_SIZE / SECTOR_SIZE;
@@ -886,7 +889,30 @@ bool doGenericSetup()
886889
lastEncryptionKey = "";
887890
if (chipType != BKType.BK7231T && chipType != BKType.BK7231U && chipType != BKType.BK7252)
888891
{
889-
if (doUnprotect())
892+
byte[] chipIdRaw;
893+
if(chipType == BKType.BK7236 || chipType == BKType.BK7258)
894+
{
895+
chipIdRaw = ReadFlashReg(0x44010000 + (0x1 << 2)) ?? new byte[] { 0, 0, 0, 0 };
896+
}
897+
else
898+
{
899+
chipIdRaw = ReadFlashReg(0x800000) ?? new byte[] { 0, 0, 0, 0 };
900+
}
901+
chipIdRaw = chipIdRaw.Reverse().ToArray();
902+
string chipId = "";
903+
// should be 0x7238 for BK7238, 0x7231c for BK7231N, 0x7236 for both BK7236 and BK7258
904+
foreach(var ch in chipIdRaw)
905+
{
906+
if(ch == 0 || ch == 1)
907+
continue;
908+
chipId += $"{ch:x}";
909+
}
910+
// do something if selected type != chip id?
911+
if(string.IsNullOrEmpty(chipId))
912+
addWarning($"Failed to get chip ID!" + Environment.NewLine);
913+
else
914+
addLog($"Chip ID: 0x{chipId}" + Environment.NewLine);
915+
if(doUnprotect())
890916
{
891917
return false;
892918
}
@@ -940,36 +966,9 @@ bool doEraseInternal(int startSector, int sectors)
940966
logger.setProgress(0, sectors);
941967
logger.setState("Erasing...", Color.Transparent);
942968
addLog("Going to do erase, start " + startSector +", sec count " + sectors +"!" + Environment.NewLine);
943-
for (int sec = 0; sec < sectors; sec++)
969+
if(!eraseRange(startSector, sectors))
944970
{
945-
int secAddr = startSector + SECTOR_SIZE * sec;
946-
int tries = 0;
947-
while (true)
948-
{
949-
tries++;
950-
// 4K erase
951-
bool bOk = eraseSector(secAddr, 0x20);
952-
addLog("Erasing sector " + secAddr + "...");
953-
if (bOk == false)
954-
{
955-
if(tries > 5)
956-
{
957-
logger.setState("Erase failed.", Color.Red);
958-
addError(" Erasing sector " + secAddr + " failed!" + Environment.NewLine);
959-
return false;
960-
}
961-
else
962-
{
963-
addWarning(" failed, will retry! ");
964-
}
965-
}
966-
else
967-
{
968-
break;
969-
}
970-
}
971-
addLog(" ok! ");
972-
logger.setProgress(sec+1, sectors);
971+
return false;
973972
}
974973
addLog(Environment.NewLine);
975974
addLog("All selected sectors erased!" + Environment.NewLine);
@@ -1220,20 +1219,9 @@ bool doWriteInternal(int startSector, byte []data)
12201219
{
12211220
return false;
12221221
}
1223-
for (int sec = 0; sec < sectors; sec++)
1222+
if(!eraseRange(startSector, sectors))
12241223
{
1225-
int secAddr = startSector + SECTOR_SIZE * sec;
1226-
// 4K erase
1227-
bool bOk = eraseSector(secAddr, 0x20);
1228-
addLog("Erasing sector " + formatHex(secAddr) + "...");
1229-
if (bOk == false)
1230-
{
1231-
logger.setState("Erase sector failed!", Color.Red);
1232-
addError(" Erasing sector " + secAddr + " failed!" + Environment.NewLine);
1233-
return false;
1234-
}
1235-
addLog(" ok! ");
1236-
1224+
return false;
12371225
}
12381226
addLog(Environment.NewLine);
12391227
addLog("All selected sectors erased!" + Environment.NewLine);
@@ -1860,5 +1848,98 @@ bool setBaudrate(int baudrate, int delay_ms)
18601848
serial.BaudRate = prev;
18611849
return false;
18621850
}
1851+
1852+
bool eraseRange(int startSector, int sectors)
1853+
{
1854+
int current = startSector / SECTOR_SIZE;
1855+
int end = current + sectors;
1856+
1857+
bool erase4k()
1858+
{
1859+
int tries = 0;
1860+
int addr = current * SECTOR_SIZE;
1861+
while(true)
1862+
{
1863+
addLog("Erasing sector " + formatHex(addr) + "...");
1864+
bool bOk = eraseSector(addr, 0x20);
1865+
if(!bOk)
1866+
{
1867+
if(tries > 5)
1868+
{
1869+
logger.setState("Erase failed.", Color.Red);
1870+
addError(" Erasing sector " + addr + " failed!" + Environment.NewLine);
1871+
return false;
1872+
}
1873+
else
1874+
{
1875+
addWarning(" failed, will retry! ");
1876+
}
1877+
}
1878+
else
1879+
{
1880+
break;
1881+
}
1882+
}
1883+
1884+
current++;
1885+
addLog(" ok! ");
1886+
logger.setProgress(current + 1, sectors);
1887+
return true;
1888+
}
1889+
1890+
// erase sectors until 64KB-aligned
1891+
while(current < end &&
1892+
(current % SECTORS_PER_BLOCK) != 0)
1893+
{
1894+
if(!erase4k())
1895+
{
1896+
return false;
1897+
}
1898+
}
1899+
1900+
// erase 64k while possible
1901+
while((end - current) >= SECTORS_PER_BLOCK)
1902+
{
1903+
int tries = 0;
1904+
int addr = current * SECTOR_SIZE;
1905+
while(true)
1906+
{
1907+
addLog("Erasing block " + formatHex(addr) + "...");
1908+
bool bOk = eraseSector(addr, 0xD8);
1909+
if(!bOk)
1910+
{
1911+
if(tries > 5)
1912+
{
1913+
logger.setState("Erase failed.", Color.Red);
1914+
addError(" Erasing block " + addr + " failed!" + Environment.NewLine);
1915+
return false;
1916+
}
1917+
else
1918+
{
1919+
addWarning(" failed, will retry! ");
1920+
}
1921+
}
1922+
else
1923+
{
1924+
break;
1925+
}
1926+
}
1927+
1928+
current += SECTORS_PER_BLOCK;
1929+
addLog(" ok! ");
1930+
logger.setProgress(current + 1, sectors);
1931+
}
1932+
1933+
// erase remaining sectors
1934+
while(current < end)
1935+
{
1936+
if(!erase4k())
1937+
{
1938+
return false;
1939+
}
1940+
}
1941+
1942+
return true;
1943+
}
18631944
}
18641945
}

0 commit comments

Comments
 (0)