Skip to content

Commit 0e7c58c

Browse files
Merge pull request #75 from NonPIayerCharacter/wmcfg
W600/W800 cfg write and W600 backup restore
2 parents 26a68d8 + 50d37e3 commit 0e7c58c

File tree

4 files changed

+128
-37
lines changed

4 files changed

+128
-37
lines changed

BK7231Flasher/FormMain.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ public bool checkFirmwareForCurType(string s)
894894
break;
895895
case BKType.W600:
896896
case BKType.W800:
897-
if(s.StartsWith($"Open{curType}_") && s.EndsWith(".fls"))
897+
if(s.StartsWith($"Open{curType}_") && (s.EndsWith(".fls") || s.EndsWith(".bin")))
898898
{
899899
return true;
900900
}

BK7231Flasher/OBKConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ public static bool isValid(byte [] raw, int extraOfs = 0, BKType type = BKType.I
579579
case BKType.BL602:
580580
crc = CRC.Tiny_CRC8_unsigned(raw, extraOfs + 4, useLen - 4);
581581
break;
582+
case BKType.W600:
582583
case BKType.W800:
583584
useLen = getLenForVersion(3);
584585
crc = CRC.Tiny_CRC8_unsigned(raw, extraOfs + 4, useLen - 4);
@@ -617,6 +618,7 @@ public void saveConfig(BKType type = BKType.Invalid)
617618
case BKType.BL602:
618619
crc = CRC.Tiny_CRC8_unsigned(raw, 4, realLen - 4);
619620
break;
621+
case BKType.W600:
620622
case BKType.W800:
621623
realLen = getLenForVersion(3);
622624
crc = CRC.Tiny_CRC8_unsigned(raw, 4, realLen - 4);

BK7231Flasher/RTLZ2Flasher.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ internal byte[] readFlash(int addr = 0, int amount = 4096)
492492
{
493493
int startAmount = amount;
494494
byte[] ret = new byte[amount];
495+
var sha256Hash = SHA256.Create();
495496
logger.setProgress(0, amount);
496497
logger.setState("Reading", Color.White);
497498
addLogLine("Starting read...");
@@ -508,18 +509,40 @@ internal byte[] readFlash(int addr = 0, int amount = 4096)
508509
}
509510
uint startAddr = (uint)addr;
510511
int progress = 0;
511-
while(startAmount > 0)
512+
int errCount = 0;
513+
while(startAmount > 0 && errCount < 10)
512514
{
513-
DumpBytes(startAddr | FLASH_MMAP_BASE, DumpAmount, out var bytes);
515+
byte[] bytes = new byte[DumpAmount];
516+
addLog($"Reading at 0x{startAddr:X6}... ");
517+
try
518+
{
519+
DumpBytes(startAddr | FLASH_MMAP_BASE, DumpAmount, out bytes);
520+
//var treadHash = HashToStr(sha256Hash.ComputeHash(bytes));
521+
//var texpectedHash = HashToStr(FlashReadHash(startAddr, DumpAmount));
522+
//if(treadHash != texpectedHash)
523+
//{
524+
// throw new Exception("Hash mismatch");
525+
//}
526+
errCount = 0;
527+
}
528+
catch(Exception ex)
529+
{
530+
addWarningLine($"Reading at 0x{startAddr:X6} failed with {ex.Message}, retrying... ");
531+
Thread.Sleep(250);
532+
errCount++;
533+
continue;
534+
}
514535
startAddr += (uint)DumpAmount;
515536
startAmount -= DumpAmount;
516537
logger.setProgress(amount - startAmount, amount);
517538
bytes.CopyTo(ret, progress);
518539
progress += DumpAmount;
519-
addLog($"Reading at 0x{startAddr:X6}... ");
540+
}
541+
if(errCount >= 10)
542+
{
543+
throw new Exception("Error count exceeded limit!");
520544
}
521545
addLogLine(Environment.NewLine + "Getting hash...");
522-
var sha256Hash = SHA256.Create();
523546
var readHash = HashToStr(sha256Hash.ComputeHash(ret));
524547
var expectedHash = HashToStr(FlashReadHash((uint)addr, amount));
525548
if(readHash != expectedHash)
@@ -543,6 +566,7 @@ internal byte[] readFlash(int addr = 0, int amount = 4096)
543566
catch(Exception ex)
544567
{
545568
addError(ex.ToString() + Environment.NewLine);
569+
logger.setState("Read error", Color.Red);
546570
ChangeBaud(115200);
547571
}
548572
closePort();

BK7231Flasher/WMFlasher.cs

Lines changed: 97 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public override void doRead(int startSector = 0x000, int sectors = 10, bool full
359359
{
360360
if(chipType == BKType.W600)
361361
{
362-
addErrorLine("W600 doesn't support read");
362+
addErrorLine("W600 doesn't support read. Use JLink for firmware backup.");
363363
return;
364364
}
365365
if(doGenericSetup() == false)
@@ -441,10 +441,18 @@ public override void closePort()
441441

442442
public override void doReadAndWrite(int startSector, int sectors, string sourceFileName, WriteMode rwMode)
443443
{
444-
if(chipType == BKType.W600 && rwMode == WriteMode.ReadAndWrite)
444+
if(chipType == BKType.W600)
445445
{
446-
addErrorLine("W600 doesn't support read");
447-
return;
446+
if(rwMode == WriteMode.ReadAndWrite)
447+
{
448+
addErrorLine("W600 doesn't support read. Use JLink for firmware backup.");
449+
return;
450+
}
451+
else if(rwMode == WriteMode.OnlyOBKConfig)
452+
{
453+
addErrorLine("Writing only OBK config is disabled for W600, use \"Automatically configure OBK on flash write\".");
454+
return;
455+
}
448456
}
449457
if(doGenericSetup() == false)
450458
{
@@ -459,11 +467,6 @@ public override void doReadAndWrite(int startSector, int sectors, string sourceF
459467
OBKConfig cfg = rwMode == WriteMode.OnlyOBKConfig ? logger.getConfig() : logger.getConfigToWrite();
460468
if(rwMode == WriteMode.ReadAndWrite)
461469
{
462-
if(chipType == BKType.W600)
463-
{
464-
addErrorLine("W600 doesn't support read");
465-
return;
466-
}
467470
sectors = flashSizeMB * 0x100000 / BK7231Flasher.SECTOR_SIZE;
468471
addLogLine($"Flash size detected: {sectors / 256}MB");
469472
ms = ReadInternal(startSector | 0x08000000, sectors);
@@ -500,39 +503,34 @@ public override void doReadAndWrite(int startSector, int sectors, string sourceF
500503
logger.setState("Write error!", Color.Red);
501504
}
502505
}
503-
else if(sourceFileName.Contains("readResult") && data.Length >= 0x100000 && chipType != BKType.W600)
506+
else if(data.Length >= 0x100000)
504507
{
505508
try
506509
{
507-
startSector = 0x2400;
510+
startSector = 0x2000;
508511
var secBootHeader = new byte[64];
509512
Array.Copy(data, 0x2000, secBootHeader, 0, secBootHeader.Length);
510513
if(secBootHeader[0] != 0x9f || secBootHeader[1] != 0xff || secBootHeader[2] != 0xff || secBootHeader[3] != 0xa0)
511514
{
512-
addErrorLine("Unknown file type, skipping.");
515+
addErrorLine("Unknown file type, no firmware header at 0x2000!");
513516
return;
514517
}
515-
var secBootLength = BitConverter.ToUInt32(secBootHeader, 12);
516-
var secBoot = new byte[secBootLength];
517-
var secBootAddr = BitConverter.ToUInt32(secBootHeader, 8);
518-
var secBootFls = new List<byte>();
519-
secBootFls.AddRange(secBootHeader);
520-
Array.Copy(data, secBootAddr ^ 0x08000000, secBoot, 0, secBootLength);
521-
secBootFls.AddRange(secBoot);
522518
var cutData = new byte[data.Length - startSector - 1];
523519
Array.Copy(data, startSector, cutData, 0, cutData.Length);
524520
startSector |= 0x08000000;
525521
var fls = GenerateW800PseudoFLSFromData(cutData, startSector);
526-
var res = xm.Send(fls);
527-
if(res == fls.Length)
522+
if(chipType == BKType.W600)
528523
{
529-
addLogLine("Restoring secboot");
530-
var sfb = xm.Send(secBootFls.ToArray());
531-
if(sfb != secBootFls.Count)
524+
if(secBootHeader[60] != 0xFF || secBootHeader[61] != 0xFF || secBootHeader[62] != 0xFF || secBootHeader[63] != 0xFF)
532525
{
533-
logger.setState("Write secboot error!", Color.Red);
526+
addErrorLine("Not W600 backup!");
534527
return;
535528
}
529+
fls = GenerateW600PseudoFLSFromData(cutData, startSector);
530+
}
531+
var res = xm.Send(fls);
532+
if(res == fls.Length)
533+
{
536534
logger.setState("Writing done", Color.DarkGreen);
537535
addLogLine("Done flash write " + data.Length);
538536
logger.setProgress(1, 1);
@@ -545,16 +543,49 @@ public override void doReadAndWrite(int startSector, int sectors, string sourceF
545543
catch(Exception ex)
546544
{
547545
addErrorLine(ex.Message);
546+
return;
548547
}
549548
}
550549
else
551550
{
552551
addErrorLine("Unknown file type, skipping.");
552+
return;
553553
}
554554
}
555555
if((rwMode == WriteMode.OnlyWrite || rwMode == WriteMode.ReadAndWrite || rwMode == WriteMode.OnlyOBKConfig) && cfg != null)
556556
{
557-
addWarningLine("Writing config is not supported!");
557+
var offset = (OBKFlashLayout.getConfigLocation(chipType, out _) | 0x08000000);
558+
cfg.saveConfig(chipType);
559+
var data = new byte[2016 + 0x303];
560+
if(chipType == BKType.W800)
561+
{
562+
MiscUtils.padArray(data, 1);
563+
Array.Copy(cfg.getData(), 0, data, 0x303, 2016);
564+
}
565+
else
566+
{
567+
data = new byte[2016];
568+
Array.Copy(cfg.getData(), data, data.Length);
569+
}
570+
addLog("Now will also write OBK config..." + Environment.NewLine);
571+
addLog("Long name from CFG: " + cfg.longDeviceName + Environment.NewLine);
572+
addLog("Short name from CFG: " + cfg.shortDeviceName + Environment.NewLine);
573+
addLog("Web Root from CFG: " + cfg.webappRoot + Environment.NewLine);
574+
var fls = chipType != BKType.W600 ? GenerateW800PseudoFLSFromData(data, offset - 0x303) : GenerateW600PseudoFLSFromData(data, offset);
575+
var res = xm.Send(fls);
576+
if(res == fls.Length)
577+
{
578+
logger.setState("OBK config write success!", Color.Green);
579+
logger.setProgress(1, 1);
580+
}
581+
else
582+
{
583+
logger.setState("OBK config write error!", Color.Red);
584+
}
585+
}
586+
else
587+
{
588+
addLog("NOTE: the OBK config writing is disabled, so not writing anything extra." + Environment.NewLine);
558589
}
559590
}
560591
catch(Exception ex)
@@ -589,12 +620,8 @@ byte[] GenerateW800PseudoFLSFromData(byte[] data, int startAddr)
589620
(byte)((data.Length >> 8) & 0xFF),
590621
(byte)((data.Length >> 16) & 0xFF),
591622
(byte)((data.Length >> 24) & 0xFF),
592-
//0x00, 0x00, 0x00, 0x00,
593-
(byte)(startAddr - 0x400 & 0xFF),
594-
(byte)(((startAddr - 0x400) >> 8) & 0xFF),
595-
(byte)(((startAddr - 0x400) >> 16) & 0xFF),
596-
(byte)(((startAddr - 0x400) >> 24) & 0xFF),
597-
0x00, 0x00, 0x01, 0x08,
623+
0x00, 0x00, 0x00, 0x00,
624+
0x00, 0x00, 0x00, 0x00,
598625
(byte)(crc & 0xFF),
599626
(byte)((crc >> 8) & 0xFF),
600627
(byte)((crc >> 16) & 0xFF),
@@ -618,6 +645,44 @@ byte[] GenerateW800PseudoFLSFromData(byte[] data, int startAddr)
618645
return fls.ToArray();
619646
}
620647

648+
byte[] GenerateW600PseudoFLSFromData(byte[] data, int startAddr)
649+
{
650+
var crc = CRC.crc32_ver2(0xFFFFFFFF, data);
651+
var fls = new List<byte>()
652+
{
653+
0x9F, 0xFF, 0xFF, 0xA0,
654+
0x00, 0x02, 0x00, 0x00,
655+
(byte)(startAddr & 0xFF),
656+
(byte)((startAddr >> 8) & 0xFF),
657+
(byte)((startAddr >> 16) & 0xFF),
658+
(byte)((startAddr >> 24) & 0xFF),
659+
(byte)(data.Length & 0xFF),
660+
(byte)((data.Length >> 8) & 0xFF),
661+
(byte)((data.Length >> 16) & 0xFF),
662+
(byte)((data.Length >> 24) & 0xFF),
663+
(byte)(crc & 0xFF),
664+
(byte)((crc >> 8) & 0xFF),
665+
(byte)((crc >> 16) & 0xFF),
666+
(byte)((crc >> 24) & 0xFF),
667+
0x00, 0x00, 0x00, 0x00,
668+
0x00, 0x00, 0x00, 0x00,
669+
0x00, 0x00, 0x00, 0x00,
670+
0x00, 0x00, 0x00, 0x00,
671+
0x31, 0x00, 0x00, 0x00,
672+
0x00, 0x00, 0x00, 0x00,
673+
0x00, 0x00, 0x00, 0x00,
674+
0x00, 0x00, 0x00, 0x00,
675+
};
676+
var crcHdr = CRC.crc32_ver2(0xFFFFFFFF, fls.ToArray());
677+
var t = Convert.ToString(crcHdr, 16);
678+
fls.Add((byte)(crcHdr & 0xFF));
679+
fls.Add((byte)((crcHdr >> 8) & 0xFF));
680+
fls.Add((byte)((crcHdr >> 16) & 0xFF));
681+
fls.Add((byte)((crcHdr >> 24) & 0xFF));
682+
fls.AddRange(data);
683+
return fls.ToArray();
684+
}
685+
621686
bool saveReadResult(string fileName)
622687
{
623688
if(ms == null)

0 commit comments

Comments
 (0)