add method to search for version strings in binaries

Signed-off-by: Brian S. Stephan <bss@incorporeal.org>
This commit is contained in:
Brian S. Stephan 2024-04-12 18:55:54 -05:00
parent 1f65f23a4f
commit a0734c9b48
Signed by: bss
GPG Key ID: 3DE06D3180895FCB
2 changed files with 25 additions and 0 deletions

View File

@ -6,6 +6,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
import argparse
import copy
import logging
import re
from typing import Optional
from google.protobuf.message import Message
@ -130,6 +131,20 @@ def concatenate_firmware_and_storage_files(firmware_filename: str,
write(endpoint_out, endpoint_in, GP2040CE_START_ADDRESS, bytes(new_binary))
def find_version_string_in_binary(binary: bytes) -> str:
"""Search for a git describe style version string in a binary file.
Args:
binary: the binary to search
Returns:
the first found string, or None
"""
match = re.search(b'v[0-9]+.[0-9]+.[0-9]+[A-Za-z0-9-+.]*', binary)
if match:
return match.group(0).decode(encoding='ascii')
return None
def get_gp2040ce_from_usb() -> tuple[bytes, object, object]:
"""Read the firmware + config sections from a USB device.

View File

@ -134,6 +134,16 @@ def test_concatenate_to_uf2_board_only(tmp_path, firmware_binary, config_binary)
assert len(content) == 2 * (2 * 1024 * 1024 - 16384)
def test_find_version_string(firmware_binary):
"""Test that we can find a version string in a binary."""
assert builder.find_version_string_in_binary(firmware_binary) == 'v0.7.5'
def test_dont_always_find_version_string(firmware_binary):
"""Test that we can find a version string in a binary."""
assert builder.find_version_string_in_binary(b'\x00') is None
def test_padding_firmware(firmware_binary):
"""Test that firmware is padded to the expected size."""
padded = builder.pad_binary_up_to_user_config(firmware_binary)