Skip to content

Commit

Permalink
Fix CRC check on writing unaligned data
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba2k2 committed Dec 18, 2024
1 parent 38ef9c9 commit 7f139fb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
7 changes: 4 additions & 3 deletions bk7231tools/serial/cmd_hl_flash.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,15 @@ def program_flash(
# write data in 256-byte chunks
sector_end = (addr & ~0xFFF) + 4096
while addr & 0xFFF:
block = io.read(min(256, sector_end - addr))
padding_len = addr & 0xFF
block = io.read(min(256 - padding_len, sector_end - addr))
block_size = len(block)
if not block_size:
# writing finished
return
self.flash_write_bytes(
addr,
block,
addr - padding_len,
padding_len * b"\xFF" + block,
crc_check=crc_check,
dry_run=dry_run,
)
Expand Down
13 changes: 13 additions & 0 deletions bk7231tools/serial/cmd_ll_flash.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ def flash_write_bytes(
if response.written != len(data):
raise ValueError(f"Writing failed; wrote only {response.written} bytes")
if crc_check:
if start & 0xFF:
self.warn(
f"Start address (0x{start:06X} is not 256 B-aligned, "
f"can't check CRC"
)
return
if len(data) < 256:
# start address aligned, but length is less than 256
# add necessary padding
# (this is an assumption, that the block was erased prior to writing)
data += b"\xFF" * (256 - len(data))
self.check_crc(start, data)

def flash_write_4k(
Expand All @@ -145,6 +156,8 @@ def flash_write_4k(
crc_check: bool = True,
dry_run: bool = False,
) -> None:
if start & 0xFFF:
raise ValueError(f"Start address (0x{start:06X}) is not 4k-aligned")
if len(data) > 4096:
raise ValueError(f"Data too long ({len(data)} > 4096)")
if len(data) < 4096:
Expand Down
4 changes: 2 additions & 2 deletions bk7231tools/serial/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def command(
offset = self.fix_addr(offset)
setattr(packet, field, offset)

self.debug("<- TX:", shorten(str(packet), 64))
self.debug("<- TX:", shorten(str(packet), 100))

self.write(self.encode(packet))
if after_send:
Expand Down Expand Up @@ -187,7 +187,7 @@ def command(
else:
resp = response.hex()
raise ValueError(f"Couldn't deserialize response: {resp}")
self.debug(f"-> RX ({size}):", shorten(str(response), 64))
self.debug(f"-> RX ({size}):", shorten(str(response), 100))
# check response status code, if available
if response.STATUS_FIELDS:
for field in response.STATUS_FIELDS:
Expand Down

0 comments on commit 7f139fb

Please sign in to comment.