diff --git a/bk7231tools/serial/cmd_hl_flash.py b/bk7231tools/serial/cmd_hl_flash.py index 71a1c21..183b090 100644 --- a/bk7231tools/serial/cmd_hl_flash.py +++ b/bk7231tools/serial/cmd_hl_flash.py @@ -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, ) diff --git a/bk7231tools/serial/cmd_ll_flash.py b/bk7231tools/serial/cmd_ll_flash.py index 8945179..4aa1264 100644 --- a/bk7231tools/serial/cmd_ll_flash.py +++ b/bk7231tools/serial/cmd_ll_flash.py @@ -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( @@ -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: diff --git a/bk7231tools/serial/protocol.py b/bk7231tools/serial/protocol.py index b2241f3..5d6187d 100644 --- a/bk7231tools/serial/protocol.py +++ b/bk7231tools/serial/protocol.py @@ -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: @@ -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: