Skip to content

Commit a9ba7bb

Browse files
committed
Fix DumpWords: use uint.TryParse to handle word values >= 0x80000000
1 parent 830a7e6 commit a9ba7bb

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

BK7231Flasher/Flashers/RTLZ2Flasher.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class RTLZ2Flasher : BaseFlasher
3838
const int HashRetryLimit = 3;
3939
const int CommandRetryLimit = 3;
4040
const int FallbackBaudRate = 115200;
41-
const string InternalBuildId = "rtlz2-resiliency-r25";
41+
const string InternalBuildId = "rtlz2-resiliency-r26";
4242

4343
public RTLZ2Flasher(CancellationToken ct) : base(ct)
4444
{
@@ -478,11 +478,11 @@ bool DumpBytes(uint start, int count, out byte[] bytes)
478478
return true;
479479
}
480480

481-
bool DumpWords(uint start, int count, out int[] ints)
481+
bool DumpWords(uint start, int count, out uint[] words)
482482
{
483483
int bytesRead = 0;
484484
int expectedBytes = count * 4;
485-
var data = new List<int>(Math.Max(1, count));
485+
var data = new List<uint>(Math.Max(1, count));
486486
Command($"DW {start:X} {count}");
487487
// DW outputs ~11.75 ASCII chars per 4-byte word (address + 4 hex words per line + separators).
488488
// Use a baud-aware deadline matching DumpBytes to avoid false timeouts at any baud rate.
@@ -526,7 +526,10 @@ bool DumpWords(uint start, int count, out int[] ints)
526526

527527
for(int i = 1; i < 5 && bytesRead < expectedBytes; i++)
528528
{
529-
if(!int.TryParse(parts[i].Trim('\r'), System.Globalization.NumberStyles.HexNumber, null, out var value))
529+
// uint.TryParse handles the full 32-bit range (0x00000000–0xFFFFFFFF).
530+
// int.TryParse would silently fail for values >= 0x80000000, corrupting
531+
// flash data with high bits set.
532+
if(!uint.TryParse(parts[i].Trim('\r'), System.Globalization.NumberStyles.HexNumber, null, out var value))
530533
{
531534
throw new Exception("Invalid word dump data");
532535
}
@@ -542,18 +545,18 @@ bool DumpWords(uint start, int count, out int[] ints)
542545

543546
if(bytesRead != expectedBytes || data.Count != count)
544547
{
545-
ints = null;
548+
words = null;
546549
return false;
547550
}
548551

549-
ints = data.ToArray();
552+
words = data.ToArray();
550553
return true;
551554
}
552555

553556
// Flash-read variant of DumpWords: takes a byte count (must be a multiple of 4),
554557
// calls DumpWords with the mapped flash address, and converts the returned 32-bit words
555-
// to a byte array. ARM is little-endian, so BitConverter.GetBytes preserves byte order
556-
// correctly on any LE host (x86/x64 Windows or Linux).
558+
// to a byte array. ARM is little-endian, so BitConverter.GetBytes(uint) preserves byte
559+
// order correctly on any LE host (x86/x64 Windows or Linux).
557560
bool DumpFlashWords(uint start, int byteCount, out byte[] bytes)
558561
{
559562
int wordCount = byteCount / 4;
@@ -580,7 +583,7 @@ int RegisterRead(uint addr)
580583
{
581584
throw new Exception($"Register read index failed at 0x{addr:X}");
582585
}
583-
return words[index];
586+
return (int)words[index];
584587
}
585588

586589
void RegisterWrite(uint addr, uint value)

0 commit comments

Comments
 (0)