From d2c918f4072f98b1f195bc2d29ee3a99a35d6925 Mon Sep 17 00:00:00 2001 From: "radim.karnis" Date: Thu, 26 May 2022 16:07:54 +0200 Subject: [PATCH] mkdfu.py: Support setting flash parameters --- tools/cmake/dfu.cmake | 1 + tools/mkdfu.py | 67 +++++++++++++++++++++++++++++++-- tools/test_mkdfu/1/dfu.bin | Bin 10256 -> 10256 bytes tools/test_mkdfu/2/dfu.bin | Bin 10256 -> 10256 bytes tools/test_mkdfu/test_mkdfu.py | 7 +++- 5 files changed, 70 insertions(+), 5 deletions(-) diff --git a/tools/cmake/dfu.cmake b/tools/cmake/dfu.cmake index 69063c88b4..c6c5172dc7 100644 --- a/tools/cmake/dfu.cmake +++ b/tools/cmake/dfu.cmake @@ -29,6 +29,7 @@ function(__add_dfu_targets) -o "${CMAKE_CURRENT_BINARY_DIR}/dfu.bin" --json "${CMAKE_CURRENT_BINARY_DIR}/flasher_args.json" --pid "${dfu_pid}" + --flash-size "${CONFIG_ESPTOOLPY_FLASHSIZE}" DEPENDS gen_project_binary bootloader VERBATIM USES_TERMINAL) diff --git a/tools/mkdfu.py b/tools/mkdfu.py index fab42a36fa..4cd2dacbe5 100755 --- a/tools/mkdfu.py +++ b/tools/mkdfu.py @@ -103,6 +103,26 @@ ESPRESSIF_VID = 12346 # This CRC32 gets added after DFUSUFFIX_STRUCT DFUCRC_STRUCT = b' int """ Calculate CRC32/JAMCRC of data, with an optional initial value """ @@ -116,6 +136,17 @@ def pad_bytes(b, multiple, padding=b'\x00'): # type: (bytes, int, bytes) -> byt return b + padding * (padded_len - len(b)) +def flash_size_bytes(size): # type: (str) -> int + """ + Given a flash size passed in args.flash_size + (ie 4MB), return the size in bytes. + """ + try: + return int(size.rstrip('MB'), 10) * 1024 * 1024 + except ValueError: + raise argparse.ArgumentTypeError('Unknown size {}'.format(size)) + + class EspDfuWriter(object): def __init__(self, dest_file, pid, part_size): # type: (typing.BinaryIO, int, int) -> None self.dest = dest_file @@ -124,6 +155,29 @@ class EspDfuWriter(object): self.entries = [] # type: typing.List[bytes] self.index = [] # type: typing.List[DFUInfo] + def add_flash_params_file(self, flash_size): # type: (str) -> None + """ + Add a file containing flash chip parameters + + Corresponds to the "flashchip" data structure that the ROM + has in RAM. + + See flash_set_parameters() in esptool.py for more info + """ + flash_params = FlashParamsData( + ishspi=0, + legacy=0, + deviceId=0, # ignored + chip_size=flash_size_bytes(flash_size), # flash size in bytes + block_size=64 * 1024, + sector_size=4 * 1024, + page_size=256, + status_mask=0xffff, + ) + data = struct.pack(FLASH_PARAMS_STRUCT, *flash_params) + flags = DFU_INFO_FLAG_PARAM | DFU_INFO_FLAG_NOERASE | DFU_INFO_FLAG_IGNORE_MD5 + self._add_cpio_flash_entry(FLASH_PARAMS_FILE, 0, data, flags) + def add_file(self, flash_addr, path): # type: (int, str) -> None """ Add file to be written into flash at given address @@ -166,14 +220,14 @@ class EspDfuWriter(object): self.dest.write(out_data) def _add_cpio_flash_entry( - self, filename, flash_addr, data - ): # type: (str, int, bytes) -> None + self, filename, flash_addr, data, flags=0 + ): # type: (str, int, bytes, int) -> None md5 = hashlib.md5() md5.update(data) self.index.append( DFUInfo( address=flash_addr, - flags=0, + flags=flags, name=filename.encode('utf-8'), md5=md5.digest(), ) @@ -196,6 +250,8 @@ class EspDfuWriter(object): def action_write(args): # type: (typing.Mapping[str, typing.Any]) -> None writer = EspDfuWriter(args['output_file'], args['pid'], args['part_size']) + print('Adding flash chip parameters file with flash_size = {}'.format(args['flash_size'])) + writer.add_flash_params_file(args['flash_size']) for addr, f in args['files']: print('Adding {} at {:#x}'.format(f, addr)) writer.add_file(addr, f) @@ -228,6 +284,10 @@ def main(): # type: () -> None write_parser.add_argument('files', metavar='
', help='Add at
', nargs='*') + write_parser.add_argument('-fs', '--flash-size', + help='SPI Flash size in MegaBytes (1MB, 2MB, 4MB, 8MB, 16MB, 32MB, 64MB, 128MB)', + choices=['1MB', '2MB', '4MB', '8MB', '16MB', '32MB', '64MB', '128MB'], + default='2MB') args = parser.parse_args() @@ -261,6 +321,7 @@ def main(): # type: () -> None 'files': files, 'pid': args.pid, 'part_size': args.part_size, + 'flash_size': args.flash_size, } {'write': action_write diff --git a/tools/test_mkdfu/1/dfu.bin b/tools/test_mkdfu/1/dfu.bin index cc28754f382510ff037315c285e00f82388e353e..9c306c7229c21777841ef505372ff582480908d7 100644 GIT binary patch delta 146 zcmbObFd<-q4WrrQgNzyzQzgV17#L)LI4vi!I3vCwu_!UOST7~9Wa2~vfhlYjO!+r6 z*v_o!iJavj*t4<5nUTrB2&hxt#?a6J3ea@|4MPDAKprClg8+p3|DS@iNJOVH{lc5hMk!2XM)j|QYw*pBelVBY9k|+!W#kY0E2PzJk diff --git a/tools/test_mkdfu/2/dfu.bin b/tools/test_mkdfu/2/dfu.bin index 31774a80cfcbcb5206ff02925e98d32484bb9d5d..8f75cd4046817599e55563d5adc5b53ab414d894 100644 GIT binary patch delta 143 zcmbObFd<-qHM60C#pFaL^@*v{A~FmN3~4!u#ToGhiA9OI#d;};B@-ta2uxwKV9LLl z!FFa%Pvk5Q!JdgVZy1e0nrsXW4WIyBAJ8Zi-~i+?GB5~0sQ>>N7$(?k&R|@iI!S|v JWlzP@U;qznCs+Ug delta 54 zcmbObFd<;_0wx>Aq{)HI8WU5c*%^Q!DKl^KLT1~^GK@