@@ -53,36 +53,37 @@ class ConfigMagicError(ConfigReadError):
5353 """Exception raised when the config section does not have the magic value in its footer."""
5454
5555
56- def convert_binary_to_uf2 (binary : bytearray , start : int = 0 ) -> bytearray :
56+ def convert_binary_to_uf2 (binaries : list [ tuple [ int , bytearray ]] ) -> bytearray :
5757 """Convert a GP2040-CE binary payload to Microsoft's UF2 format.
5858
5959 https://github.com/microsoft/uf2/tree/master#overview
6060
6161 Args:
62- binary: bytearray content to convert to a UF2 payload
63- start: position offset to start at rather than flash start (for creating e.g. user config UF2s)
62+ binaries: list of start,binary pairs of binary data to write at the specified memory offset in flash
6463 Returns:
6564 the content in UF2 format
6665 """
67- size = len (binary )
68- blocks = (len (binary ) // 256 ) + 1 if len (binary ) % 256 else len (binary ) // 256
69- uf2 = bytearray ()
66+ total_blocks = sum ([(len (binary ) // 256 ) + 1 if len (binary ) % 256 else len (binary ) // 256
67+ for offset , binary in binaries ])
7068
71- index = 0
72- while index < size :
73- pad_count = 476 - len (binary [index :index + 256 ])
74- uf2 += struct .pack ('<LLLLLLLL' ,
75- UF2_MAGIC_FIRST , # first magic number
76- UF2_MAGIC_SECOND , # second magic number
77- 0x00002000 , # familyID present
78- 0x10000000 + start + index , # address to write to
79- 256 , # bytes to write in this block
80- index // 256 , # sequential block number
81- blocks , # total number of blocks
82- UF2_FAMILY_ID ) # family ID
83- uf2 += binary [index :index + 256 ] + bytearray (b'\x00 ' * pad_count ) # content
84- uf2 += struct .pack ('<L' , UF2_MAGIC_FINAL ) # final magic number
85- index += 256
69+ uf2 = bytearray ()
70+ for start , binary in binaries :
71+ size = len (binary )
72+ index = 0
73+ while index < size :
74+ pad_count = 476 - len (binary [index :index + 256 ])
75+ uf2 += struct .pack ('<LLLLLLLL' ,
76+ UF2_MAGIC_FIRST , # first magic number
77+ UF2_MAGIC_SECOND , # second magic number
78+ 0x00002000 , # familyID present
79+ 0x10000000 + start + index , # address to write to
80+ 256 , # bytes to write in this block
81+ index // 256 , # sequential block number
82+ total_blocks , # total number of blocks
83+ UF2_FAMILY_ID ) # family ID
84+ uf2 += binary [index :index + 256 ] + bytearray (b'\x00 ' * pad_count ) # content
85+ uf2 += struct .pack ('<L' , UF2_MAGIC_FINAL ) # final magic number
86+ index += 256
8687 return uf2
8788
8889
@@ -393,8 +394,9 @@ def dump_config():
393394 with open (args .filename , 'wb' ) as out_file :
394395 if args .filename [- 4 :] == '.uf2' :
395396 # we must pad to storage start in order for the UF2 write addresses to make sense
396- out_file .write (convert_binary_to_uf2 (pad_config_to_storage_size (binary_config ),
397- start = USER_CONFIG_BINARY_LOCATION ))
397+ out_file .write (convert_binary_to_uf2 ([
398+ (USER_CONFIG_BINARY_LOCATION , pad_config_to_storage_size (binary_config )),
399+ ]))
398400 else :
399401 out_file .write (binary_config )
400402
0 commit comments