From ea4d4be7099d933127451e7d0dd84dbed6e499de Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Sun, 14 Apr 2024 11:21:55 -0500 Subject: [PATCH] fix bad loading of .uf2 files in summarize-gp2040ce the UF2 file wasn't converted to binary format before searching for the board/user config sections, so it was reading the middle of the UF2 file instead of the end of the binary file and returning that there were no configs Signed-off-by: Brian S. Stephan --- gp2040ce_bintools/builder.py | 3 +-- gp2040ce_bintools/storage.py | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/gp2040ce_bintools/builder.py b/gp2040ce_bintools/builder.py index 5eb0bfe..fee7551 100644 --- a/gp2040ce_bintools/builder.py +++ b/gp2040ce_bintools/builder.py @@ -352,8 +352,7 @@ def summarize_gp2040ce(): content, endpoint, _ = get_gp2040ce_from_usb() print(f"USB device {hex(endpoint.device.idVendor)}:{hex(endpoint.device.idProduct)}:\n") else: - with open(args.filename, 'rb') as file_: - content = file_.read() + content = storage.get_binary_from_file(args.filename) print(f"File {args.filename}:\n") gp2040ce_version = find_version_string_in_binary(content) diff --git a/gp2040ce_bintools/storage.py b/gp2040ce_bintools/storage.py index c308775..b4bf517 100644 --- a/gp2040ce_bintools/storage.py +++ b/gp2040ce_bintools/storage.py @@ -202,6 +202,25 @@ def get_config_footer(content: bytes) -> tuple[int, int, str]: return config_size, config_crc, config_magic +def get_binary_from_file(filename: str) -> bytes: + """Read the specified file (.bin or .uf2) and get back its raw binary contents. + + Args: + filename: the filename of the file to open and read + Returns: + the file's content, in raw binary format + Raises: + FileNotFoundError: if the file was not found + """ + with open(filename, 'rb') as dump: + if filename[-4:] == '.uf2': + content = bytes(convert_uf2_to_binary(bytearray(dump.read()))) + else: + content = dump.read() + + return content + + def get_config_from_file(filename: str, whole_board: bool = False, allow_no_file: bool = False, board_config: bool = False) -> Message: """Read the specified file (memory dump or whole board dump) and get back its config section. @@ -215,11 +234,7 @@ def get_config_from_file(filename: str, whole_board: bool = False, allow_no_file the parsed configuration """ try: - with open(filename, 'rb') as dump: - if filename[-4:] == '.uf2': - content = bytes(convert_uf2_to_binary(bytearray(dump.read()))) - else: - content = dump.read() + content = get_binary_from_file(filename) except FileNotFoundError: if not allow_no_file: raise