Skip to content

Commit e133abc

Browse files
committed
concatenate smaller UF2s by using indexed offsets
previous commit added the ability to pass a list of location+binary combinations to create UF2s, meaning we no longer need to pad the concatenated UF2 the same way we pad the raw binary output. this makes for a more sanely-sized UF2 and faster write Signed-off-by: Brian S. Stephan <[email protected]>
1 parent 2bb049c commit e133abc

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

gp2040ce_bintools/builder.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -101,26 +101,15 @@ def concatenate_firmware_and_storage_files(firmware_filename: str,
101101
new_binary = combine_firmware_and_config(firmware_binary, board_config_binary, user_config_binary,
102102
replace_extra=replace_extra)
103103
else:
104-
# this was kind of fine, but combining multiple calls of convert_binary_to_uf2 produced
105-
# incorrect total block counts in the file, which picotool handled with some squirrely
106-
# double-output behavior that has me worried it'd cause a real issue, so doing the
107-
# crude padding + write of empty blocks, for now...
108-
#
109-
# new_binary = convert_binary_to_uf2(firmware_binary)
110-
# if board_config_binary:
111-
# new_binary += convert_binary_to_uf2(pad_config_to_storage_size(board_config_binary),
112-
# start=BOARD_CONFIG_BINARY_LOCATION)
113-
# if user_config_binary:
114-
# new_binary += convert_binary_to_uf2(pad_config_to_storage_size(user_config_binary),
115-
# start=USER_CONFIG_BINARY_LOCATION)
116-
#
117-
# the correct way to do the above would be to pass a list of {offset,binary_data} to convert...,
118-
# and have it calculate the total block size before starting to write, and then iterating over
119-
# the three lists. doable, just not on the top of my mind right now
120-
new_binary = storage.convert_binary_to_uf2([
121-
(0, combine_firmware_and_config(firmware_binary, board_config_binary, user_config_binary,
122-
replace_extra=replace_extra)),
123-
])
104+
binary_list = [(0, firmware_binary)]
105+
# we must pad to storage start in order for the UF2 write addresses to make sense
106+
if board_config_binary:
107+
binary_list.append((storage.BOARD_CONFIG_BINARY_LOCATION,
108+
storage.pad_config_to_storage_size(board_config_binary)))
109+
if user_config_binary:
110+
binary_list.append((storage.USER_CONFIG_BINARY_LOCATION,
111+
storage.pad_config_to_storage_size(user_config_binary)))
112+
new_binary = storage.convert_binary_to_uf2(binary_list)
124113

125114
if combined_filename:
126115
with open(combined_filename, 'wb') as combined:
@@ -257,6 +246,7 @@ def write_new_config_to_filename(config: Message, filename: str, inject: bool =
257246
binary = storage.serialize_config_with_footer(config)
258247
with open(filename, 'wb') as file:
259248
if filename[-4:] == '.uf2':
249+
# we must pad to storage start in order for the UF2 write addresses to make sense
260250
file.write(storage.convert_binary_to_uf2([
261251
(storage.USER_CONFIG_BINARY_LOCATION, storage.pad_config_to_storage_size(binary)),
262252
]))

tests/test_builder.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
SPDX-FileCopyrightText: © 2023 Brian S. Stephan <[email protected]>
44
SPDX-License-Identifier: GPL-3.0-or-later
55
"""
6+
import math
67
import os
78
import sys
89
import unittest.mock as mock
@@ -117,8 +118,9 @@ def test_concatenate_to_uf2(tmp_path, firmware_binary, config_binary):
117118
combined_filename=tmp_file)
118119
with open(tmp_file, 'rb') as file:
119120
content = file.read()
120-
# size of the file should be 2x the binary version, and the binary is 2 MB
121-
assert len(content) == 2 * 2 * 1024 * 1024
121+
# size of the file should be 2x the padded firmware + 2x the board config space + 2x the user config space
122+
assert len(content) == (math.ceil(len(firmware_binary)/256) * 512 +
123+
math.ceil(STORAGE_SIZE/256) * 512 * 2)
122124

123125

124126
def test_concatenate_to_uf2_board_only(tmp_path, firmware_binary, config_binary):
@@ -130,8 +132,9 @@ def test_concatenate_to_uf2_board_only(tmp_path, firmware_binary, config_binary)
130132
combined_filename=tmp_file)
131133
with open(tmp_file, 'rb') as file:
132134
content = file.read()
133-
# size of the file should be 2x the binary version (minus user config space), and the binary is 2 MB - 16KB
134-
assert len(content) == 2 * (2 * 1024 * 1024 - 16384)
135+
# size of the file should be 2x the padded firmware + 2x the board config space
136+
assert len(content) == (math.ceil(len(firmware_binary)/256) * 512 +
137+
math.ceil(STORAGE_SIZE/256) * 512)
135138

136139

137140
def test_find_version_string(firmware_binary):

0 commit comments

Comments
 (0)