Skip to content

Commit

Permalink
[bk72xx] Support bk7231tools after flash refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba2k2 committed Mar 19, 2024
1 parent 50bc232 commit 98a8042
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 34 deletions.
91 changes: 59 additions & 32 deletions ltchiptool/soc/bk72xx/flash.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
import struct
from abc import ABC
from binascii import crc32
from logging import DEBUG, debug
from logging import DEBUG, debug, warning
from typing import IO, Generator, List, Optional, Tuple, Union

from bk7231tools.serial import BK7231Serial
from bk7231tools.serial.protocol import CHIP_BY_CRC
from bk7231tools.serial.base import BkChipType
from bk7231tools.serial.base.packets import (
BkFlashReg24ReadCmnd,
BkReadRegCmnd,
BkWriteRegCmnd,
)

from ltchiptool import SocInterface
from ltchiptool.util.flash import FlashConnection, FlashFeatures, FlashMemoryType
Expand Down Expand Up @@ -74,6 +79,7 @@ def flash_build_protocol(self, force: bool = False) -> None:
self.bk.info = lambda *args: debug(" ".join(args))
if loglevel <= VERBOSE:
self.bk.debug = lambda *args: verbose(" ".join(args))
self.bk.warn = lambda *args: warning(" ".join(args))
self.flash_change_timeout(self.conn.timeout, self.conn.link_timeout)

def flash_change_timeout(self, timeout: float = 0.0, link_timeout: float = 0.0):
Expand All @@ -98,6 +104,8 @@ def flash_connect(self) -> None:

def flash_disconnect(self) -> None:
if self.bk:
# avoid printing retry warnings
self.bk.warn = lambda *_: None
self.bk.close()
self.bk = None
if self.conn:
Expand All @@ -107,43 +115,65 @@ def flash_get_chip_info(self) -> List[Tuple[str, str]]:
if self.info:
return self.info
self.flash_connect()
crc = self.bk.read_flash_range_crc(0, 256) ^ 0xFFFFFFFF
if crc in CHIP_BY_CRC:
chip_type, boot_version = CHIP_BY_CRC[crc]
else:
chip_type = f"Unrecognized (0x{crc:08X})"
boot_version = None

self.info = [
("Chip Type", chip_type),
("Bootloader Version", boot_version or "Unspecified"),
]
if self.bk.check_protocol(0x11): # CMD_ReadBootVersion
self.info.append(("Bootloader Message", self.bk.chip_info))
if self.bk.check_protocol(0x03): # CMD_ReadReg
chip_id = hex(self.bk.register_read(0x800000)) # SCTRL_CHIP_ID
self.info.append(("Chip ID", chip_id))
self.info += [
("Protocol Type", self.bk.protocol_type.name),
(
"Chip Type",
self.bk.chip_type and self.bk.chip_type.name or "Unrecognized",
),
(
"Bootloader Type",
self.bk.bootloader
and f"{self.bk.bootloader.chip.name} {self.bk.bootloader.version or ''}"
or "Unrecognized",
),
(
"Chip ID",
self.bk.bk_chip_id and hex(self.bk.bk_chip_id) or "N/A",
),
(
"Boot Version String",
self.bk.bk_boot_version or "N/A",
),
]
if chip_type == "BK7231N":
tlv = self.bk.read_flash_4k(0x1D0000)
elif chip_type == "BK7231T":
tlv = self.bk.read_flash_4k(0x1E0000)

if self.bk.chip_type == BkChipType.BK7231N:
tlv = self.bk.flash_read_bytes(0x1D0000, 0x1000)
elif self.bk.chip_type == BkChipType.BK7231T:
tlv = self.bk.flash_read_bytes(0x1E0000, 0x1000)
else:
tlv = None
if tlv and tlv[0x1C:0x24] == b"\x02\x11\x11\x11\x06\x00\x00\x00":
self.info += [
("", ""),
("MAC Address", tlv and tlv[0x24:0x2A].hex(":").upper() or "Unknown"),
]
if self.bk.check_protocol(0x0E, True): # CMD_FlashGetMID

self.info += [
("", ""),
]
if self.bk.check_protocol(BkFlashReg24ReadCmnd):
flash_id = self.bk.flash_read_id()
self.info += [
("", ""),
("Flash ID", flash_id["id"].hex(" ").upper()),
("Flash Size (real)", sizeof(flash_id["size"])),
("Flash Size (by ID)", sizeof(flash_id["size"])),
]
if self.bk.check_protocol(0x03): # CMD_ReadReg
if self.bk.bootloader and self.bk.bootloader.flash_size:
self.info += [
("Flash Size (by BL)", sizeof(self.bk.bootloader.flash_size)),
]
if self.bk.flash_size_detected:
self.info += [
("Flash Size (detected)", sizeof(self.bk.flash_size)),
]
else:
flash_size = self.bk.flash_detect_size()
self.info += [
("Flash Size (detected)", sizeof(flash_size)),
]

if self.bk.check_protocol(BkReadRegCmnd):
efuse = gen2bytes(self.flash_read_raw(0, 16, memory=FlashMemoryType.EFUSE))
coeffs = struct.unpack("<IIII", efuse[0:16])
self.info += [
Expand All @@ -159,16 +189,13 @@ def flash_get_chip_info_string(self) -> str:
def flash_get_size(self, memory: FlashMemoryType = FlashMemoryType.FLASH) -> int:
self.flash_connect()
if memory == FlashMemoryType.FLASH:
if not self.bk.check_protocol(0x0E, True): # CMD_FlashGetMID
return 0x200000
flash_id = self.bk.flash_read_id()
return flash_id["size"]
return self.bk.flash_size
if memory == FlashMemoryType.ROM:
if not self.bk.check_protocol(0x03): # CMD_ReadReg
if not self.bk.check_protocol(BkReadRegCmnd):
raise NotImplementedError("Only BK7231N has built-in ROM")
return 16 * 1024
if memory == FlashMemoryType.EFUSE:
if not self.bk.check_protocol(0x01): # CMD_WriteReg
if not self.bk.check_protocol(BkWriteRegCmnd):
raise NotImplementedError("Only BK7231N can read eFuse via UART")
return 32
raise NotImplementedError("Memory type not readable via UART")
Expand Down Expand Up @@ -225,7 +252,7 @@ def flash_read_raw(
callback.on_update(len(chunk))
continue

debug(f"Checking CRC @ 0x{crc_offset:X}..0x{crc_offset+crc_length:X}")
debug(f"Checking CRC @ 0x{crc_offset:X}..0x{crc_offset + crc_length:X}")
crc_expected = self.bk.read_flash_range_crc(
crc_offset,
crc_offset + crc_length,
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ click = "^8.1.3"
colorama = "^0.4.5"
importlib-metadata = "*"
prettytable = "^3.3.0"
bk7231tools = "^1.5.1"
bk7231tools = "^2.0.0"
xmodem = "^0.4.6"
wxPython = {version = "^4.2.0", optional = true}
pywin32 = {version = "*", optional = true, markers = "sys_platform == 'win32'"}
Expand All @@ -46,7 +46,7 @@ markers = "platform_machine in 'armv6l,armv7l,armv8l,armv8b,aarch64'"
gui = ["wxPython", "pywin32", "zeroconf", "pyuac", "pylnk3"]

[tool.poetry.dev-dependencies]
black = "^24.1.0"
black = {version = "^24.1.0", markers = "python_version >= '3.8'"}
isort = "^5.10.1"
autoflake = "^1.4"

Expand Down

0 comments on commit 98a8042

Please sign in to comment.