s/get_storage_section/get_user_storage_section/

more renaming to make room for future methods

Signed-off-by: Brian S. Stephan <bss@incorporeal.org>
This commit is contained in:
Brian S. Stephan 2024-01-03 19:16:00 -06:00
parent 578fd416da
commit 841c3a38c3
Signed by: bss
GPG Key ID: 3DE06D3180895FCB
3 changed files with 35 additions and 21 deletions

View File

@ -143,7 +143,7 @@ def get_config_from_file(filename: str, whole_board: bool = False, allow_no_file
return config_pb2.Config()
if whole_board:
return get_config(get_storage_section(content))
return get_config(get_user_storage_section(content))
else:
return get_config(content)
@ -173,9 +173,29 @@ def get_user_config_from_usb() -> tuple[Message, object, object]:
return get_config_from_usb(USER_CONFIG_BOOTSEL_ADDRESS)
def get_storage_section(content: bytes) -> bytes:
def get_storage_section(content: bytes, address: int) -> bytes:
"""Pull out what should be the GP2040-CE storage section from a whole board dump.
Args:
content: bytes of a GP2040-CE whole board dump
address: location of the binary file to start reading from
Returns:
the presumed storage section from the binary
Raises:
ConfigLengthError: if the provided bytes don't appear to have a storage section
"""
# a whole board must be at least as big as the known fences
logger.debug("length of content to look for storage in: %s", len(content))
if len(content) < address + STORAGE_SIZE:
raise ConfigLengthError("provided content is not large enough to have a storage section!")
logger.debug("returning bytes from %s to %s", hex(address), hex(address + STORAGE_SIZE))
return content[address:(address + STORAGE_SIZE)]
def get_user_storage_section(content: bytes) -> bytes:
"""Get the user storage area from what should be a whole board GP2040-CE dump.
Args:
content: bytes of a GP2040-CE whole board dump
Returns:
@ -183,14 +203,7 @@ def get_storage_section(content: bytes) -> bytes:
Raises:
ConfigLengthError: if the provided bytes don't appear to have a storage section
"""
# a whole board must be at least as big as the known fences
logger.debug("length of content to look for storage in: %s", len(content))
if len(content) < USER_CONFIG_BINARY_LOCATION + STORAGE_SIZE:
raise ConfigLengthError("provided content is not large enough to have a storage section!")
logger.debug("returning bytes from %s to %s", hex(USER_CONFIG_BINARY_LOCATION),
hex(USER_CONFIG_BINARY_LOCATION + STORAGE_SIZE))
return content[USER_CONFIG_BINARY_LOCATION:(USER_CONFIG_BINARY_LOCATION + STORAGE_SIZE)]
return get_storage_section(content, USER_CONFIG_BINARY_LOCATION)
def get_new_config() -> Message:

View File

@ -15,7 +15,8 @@ from gp2040ce_bintools.builder import (FirmwareLengthError, combine_firmware_and
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)
from gp2040ce_bintools.storage import get_config, get_config_footer, get_storage_section, serialize_config_with_footer
from gp2040ce_bintools.storage import (get_config, get_config_footer, get_user_storage_section,
serialize_config_with_footer)
HERE = os.path.dirname(os.path.abspath(__file__))
@ -93,7 +94,7 @@ 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)
# if this is valid, we should be able to find the storage and footer again
storage = get_storage_section(whole_board)
storage = get_user_storage_section(whole_board)
footer_size, _, _ = get_config_footer(storage)
assert footer_size == 3309
@ -102,7 +103,7 @@ def test_firmware_plus_config_binary(firmware_binary, config_binary):
"""Test that combining firmware and storage produces a valid combined binary."""
whole_board = combine_firmware_and_config(firmware_binary, config_binary)
# if this is valid, we should be able to find the storage and footer again
storage = get_storage_section(whole_board)
storage = get_user_storage_section(whole_board)
footer_size, _, _ = get_config_footer(storage)
assert footer_size == 3309
@ -111,7 +112,7 @@ def test_chunky_firmware_plus_config_binary(config_binary):
"""Test that combining giant firmware and storage produces a valid combined binary."""
whole_board = combine_firmware_and_config(bytearray(b'\x00' * 4 * 1024 * 1024), config_binary, replace_extra=True)
# if this is valid, we should be able to find the storage and footer again
storage = get_storage_section(whole_board)
storage = get_user_storage_section(whole_board)
footer_size, _, _ = get_config_footer(storage)
assert footer_size == 3309
@ -121,7 +122,7 @@ def test_replace_config_in_binary(config_binary):
whole_board = replace_config_in_binary(bytearray(b'\x00' * 3 * 1024 * 1024), config_binary)
assert len(whole_board) == 3 * 1024 * 1024
# if this is valid, we should be able to find the storage and footer again
storage = get_storage_section(whole_board)
storage = get_user_storage_section(whole_board)
footer_size, _, _ = get_config_footer(storage)
assert footer_size == 3309
@ -131,7 +132,7 @@ def test_replace_config_in_binary_not_big_enough(config_binary):
whole_board = replace_config_in_binary(bytearray(b'\x00' * 1 * 1024 * 1024), config_binary)
assert len(whole_board) == 2 * 1024 * 1024
# if this is valid, we should be able to find the storage and footer again
storage = get_storage_section(whole_board)
storage = get_user_storage_section(whole_board)
footer_size, _, _ = get_config_footer(storage)
assert footer_size == 3309
@ -152,7 +153,7 @@ def test_write_new_config_to_whole_board(whole_board_dump, tmp_path):
with open(tmp_file, 'rb') as file:
board_dump = file.read()
config = get_config(get_storage_section(board_dump))
config = get_config(get_user_storage_section(board_dump))
assert config.boardVersion == 'v0.7.5'
config.boardVersion = 'v0.7.5-COOL'
write_new_config_to_filename(config, tmp_file, inject=True)
@ -160,7 +161,7 @@ def test_write_new_config_to_whole_board(whole_board_dump, tmp_path):
# read new file
with open(tmp_file, 'rb') as file:
new_board_dump = file.read()
config = get_config(get_storage_section(new_board_dump))
config = get_config(get_user_storage_section(new_board_dump))
assert config.boardVersion == 'v0.7.5-COOL'
assert len(board_dump) == len(new_board_dump)
@ -180,7 +181,7 @@ def test_write_new_config_to_firmware(firmware_binary, tmp_path):
# read new file
with open(tmp_file, 'rb') as file:
new_board_dump = file.read()
config = get_config(get_storage_section(new_board_dump))
config = get_config(get_user_storage_section(new_board_dump))
assert config.boardVersion == 'v0.7.5-COOL'
assert len(new_board_dump) == 2 * 1024 * 1024

View File

@ -50,7 +50,7 @@ def test_config_footer_too_small(storage_dump):
def test_whole_board_too_small(whole_board_dump):
"""Test that a storage section isn't detected if the size is too small to contain where it should be."""
with pytest.raises(storage.ConfigLengthError):
_, _, _ = storage.get_storage_section(whole_board_dump[-100000:])
_, _, _ = storage.get_user_storage_section(whole_board_dump[-100000:])
def test_config_footer_bad_magic(storage_dump):
@ -121,7 +121,7 @@ def test_config_parses(storage_dump):
@with_pb2s
def test_config_from_whole_board_parses(whole_board_dump):
"""Test that we can read in a whole board and still find the config section."""
config = storage.get_config(storage.get_storage_section(whole_board_dump))
config = storage.get_config(storage.get_user_storage_section(whole_board_dump))
assert config.boardVersion == 'v0.7.5'
assert config.hotkeyOptions.hotkey01.dpadMask == 0
assert config.hotkeyOptions.hotkey02.dpadMask == 1