diff --git a/gp2040ce_bintools/builder.py b/gp2040ce_bintools/builder.py index 37e5b91..d723c10 100644 --- a/gp2040ce_bintools/builder.py +++ b/gp2040ce_bintools/builder.py @@ -12,8 +12,9 @@ from google.protobuf.message import Message from gp2040ce_bintools import core_parser from gp2040ce_bintools.rp2040 import get_bootsel_endpoints, read, write -from gp2040ce_bintools.storage import (STORAGE_SIZE, USER_CONFIG_BINARY_LOCATION, USER_CONFIG_BOOTSEL_ADDRESS, - get_config_from_json, pad_config_to_storage_size, serialize_config_with_footer) +from gp2040ce_bintools.storage import (BOARD_CONFIG_BINARY_LOCATION, STORAGE_SIZE, USER_CONFIG_BINARY_LOCATION, + USER_CONFIG_BOOTSEL_ADDRESS, get_config_from_json, pad_config_to_storage_size, + serialize_config_with_footer) logger = logging.getLogger(__name__) @@ -118,8 +119,22 @@ def pad_binary_up_to_address(binary: bytes, position: int, or_truncate: bool = F return bytearray(binary) + bytearray(b'\x00' * bytes_to_pad) +def pad_binary_up_to_board_config(firmware: bytes, or_truncate: bool = False) -> bytearray: + """Provide a copy of the firmware padded with zero bytes up to the board config position. + + Args: + firmware: the firmware binary to process + or_truncate: if the firmware is longer than expected, just return the max size + Returns: + the resulting padded binary as a bytearray + Raises: + FirmwareLengthError: if the firmware is larger than the storage location + """ + return pad_binary_up_to_address(firmware, BOARD_CONFIG_BINARY_LOCATION, or_truncate) + + def pad_binary_up_to_user_config(firmware: bytes, or_truncate: bool = False) -> bytearray: - """Provide a copy of the firmware padded with zero bytes up to the provided position. + """Provide a copy of the firmware padded with zero bytes up to the user config position. Args: firmware: the firmware binary to process diff --git a/gp2040ce_bintools/storage.py b/gp2040ce_bintools/storage.py index 61093bb..dc5887d 100644 --- a/gp2040ce_bintools/storage.py +++ b/gp2040ce_bintools/storage.py @@ -16,6 +16,7 @@ from gp2040ce_bintools.rp2040 import get_bootsel_endpoints, read logger = logging.getLogger(__name__) +BOARD_CONFIG_BINARY_LOCATION = 0x1F8000 STORAGE_SIZE = 16384 USER_CONFIG_BINARY_LOCATION = 0x1FC000 USER_CONFIG_BOOTSEL_ADDRESS = 0x10000000 + USER_CONFIG_BINARY_LOCATION diff --git a/tests/test_builder.py b/tests/test_builder.py index 860e161..8cf33db 100644 --- a/tests/test_builder.py +++ b/tests/test_builder.py @@ -13,8 +13,8 @@ from decorator import decorator from gp2040ce_bintools import get_config_pb2 from gp2040ce_bintools.builder import (FirmwareLengthError, combine_firmware_and_config, concatenate_firmware_and_storage_files, get_gp2040ce_from_usb, - pad_binary_up_to_user_config, replace_config_in_binary, - write_new_config_to_filename, write_new_config_to_usb) + pad_binary_up_to_board_config, pad_binary_up_to_user_config, + replace_config_in_binary, write_new_config_to_filename, write_new_config_to_usb) from gp2040ce_bintools.storage import (get_config, get_config_footer, get_user_storage_section, serialize_config_with_footer) @@ -90,6 +90,12 @@ def test_padding_firmware_can_truncate(): assert len(padded) == 2080768 +def test_padding_firmware_to_board(firmware_binary): + """Test that firmware is padded to the expected size.""" + padded = pad_binary_up_to_board_config(firmware_binary) + assert len(padded) == 2080768 - (16 * 1024) + + def test_firmware_plus_storage(firmware_binary, storage_dump): """Test that combining firmware and storage produces a valid combined binary.""" whole_board = combine_firmware_and_config(firmware_binary, storage_dump)