From 7b7015456646e19ab3c792e1b145094c1bde7a05 Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Fri, 14 May 2021 14:22:54 +0200 Subject: [PATCH] tools: Split up large binaries into smaller chunks in the DFU binary ROM will erase the region a partition is in as soon as it receives the first bit of the data that is in the partition. For large partitions it takes more than 5 seconds to erase which is a hard-coded limit in dfu-utils. This splits large binaries and adds them by chunks which should avoid timing-out during flashing. Closes https://github.com/espressif/esp-idf/issues/6999 --- tools/idf_py_actions/dfu_ext.py | 30 +++--- tools/mkdfu.py | 138 +++++++++++++++++---------- tools/test_mkdfu/1/dfu.bin | Bin 10256 -> 10256 bytes tools/test_mkdfu/1/flasher_args.json | 7 -- tools/test_mkdfu/2/dfu.bin | Bin 0 -> 10256 bytes tools/test_mkdfu/test_mkdfu.py | 107 ++++++++++++++------- 6 files changed, 175 insertions(+), 107 deletions(-) delete mode 100644 tools/test_mkdfu/1/flasher_args.json create mode 100644 tools/test_mkdfu/2/dfu.bin diff --git a/tools/idf_py_actions/dfu_ext.py b/tools/idf_py_actions/dfu_ext.py index af8d2d7fa0..ed7092771f 100644 --- a/tools/idf_py_actions/dfu_ext.py +++ b/tools/idf_py_actions/dfu_ext.py @@ -6,9 +6,9 @@ def action_extensions(base_actions, project_path): SUPPORTED_TARGETS = ['esp32s2'] - def dfu_target(target_name, ctx, args): + def dfu_target(target_name, ctx, args, part_size): ensure_build_directory(args, ctx.info_name) - run_target(target_name, args) + run_target(target_name, args, {'ESP_DFU_PART_SIZE': part_size} if part_size else {}) def dfu_flash_target(target_name, ctx, args): ensure_build_directory(args, ctx.info_name) @@ -22,16 +22,24 @@ def action_extensions(base_actions, project_path): raise dfu_actions = { - "actions": { - "dfu": { - "callback": dfu_target, - "short_help": "Build the DFU binary", - "dependencies": ["all"], + 'actions': { + 'dfu': { + 'callback': dfu_target, + 'short_help': 'Build the DFU binary', + 'dependencies': ['all'], + 'options': [ + { + 'names': ['--part-size'], + 'help': 'Large files are split up into smaller partitions in order to avoid timeout during ' + 'erasing flash. This option allows to overwrite the default partition size of ' + 'mkdfu.py.' + } + ], }, - "dfu-flash": { - "callback": dfu_flash_target, - "short_help": "Flash the DFU binary", - "order_dependencies": ["dfu"], + 'dfu-flash': { + 'callback': dfu_flash_target, + 'short_help': 'Flash the DFU binary', + 'order_dependencies': ['dfu'], }, } } diff --git a/tools/mkdfu.py b/tools/mkdfu.py index d2e562c717..6b49da6eff 100755 --- a/tools/mkdfu.py +++ b/tools/mkdfu.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -# Copyright 2020 Espressif Systems (Shanghai) PTE LTD +# Copyright 2020-2021 Espressif Systems (Shanghai) CO LTD # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -21,14 +21,18 @@ # This file must be the first one in the archive. It contains binary structures describing each # subsequent file (for example, where the file needs to be flashed/loaded). -from collections import namedtuple -from future.utils import iteritems +from __future__ import print_function, unicode_literals + import argparse import hashlib import json import os import struct import zlib +from collections import namedtuple +from functools import partial + +from future.utils import iteritems try: import typing @@ -43,28 +47,28 @@ except ImportError: pass # CPIO ("new ASCII") format related things -CPIO_MAGIC = b"070701" -CPIO_STRUCT = b"=6s" + b"8s" * 13 +CPIO_MAGIC = b'070701' +CPIO_STRUCT = b'=6s' + b'8s' * 13 CPIOHeader = namedtuple( - "CPIOHeader", + 'CPIOHeader', [ - "magic", - "ino", - "mode", - "uid", - "gid", - "nlink", - "mtime", - "filesize", - "devmajor", - "devminor", - "rdevmajor", - "rdevminor", - "namesize", - "check", + 'magic', + 'ino', + 'mode', + 'uid', + 'gid', + 'nlink', + 'mtime', + 'filesize', + 'devmajor', + 'devminor', + 'rdevmajor', + 'rdevminor', + 'namesize', + 'check', ], ) -CPIO_TRAILER = "TRAILER!!!" +CPIO_TRAILER = 'TRAILER!!!' def make_cpio_header( @@ -73,7 +77,7 @@ def make_cpio_header( """ Returns CPIOHeader for the given file name and file size """ def as_hex(val): # type: (int) -> bytes - return "{:08x}".format(val).encode("ascii") + return '{:08x}'.format(val).encode('ascii') hex_0 = as_hex(0) mode = hex_0 if is_trailer else as_hex(0o0100644) @@ -98,19 +102,17 @@ def make_cpio_header( # DFU format related things # Structure of one entry in dfuinfo0.dat -DFUINFO_STRUCT = b" int @@ -119,39 +121,56 @@ def dfu_crc(data, crc=0): # type: (bytes, int) -> int return uint32_max - (zlib.crc32(data, crc) & uint32_max) -def pad_bytes(b, multiple, padding=b"\x00"): # type: (bytes, int, bytes) -> bytes +def pad_bytes(b, multiple, padding=b'\x00'): # type: (bytes, int, bytes) -> bytes """ Pad 'b' to a length divisible by 'multiple' """ padded_len = (len(b) + multiple - 1) // multiple * multiple return b + padding * (padded_len - len(b)) class EspDfuWriter(object): - def __init__(self, dest_file): # type: (typing.BinaryIO) -> None + def __init__(self, dest_file, pid, part_size): # type: (typing.BinaryIO, int, int) -> None self.dest = dest_file + self.pid = pid + self.part_size = part_size self.entries = [] # type: typing.List[bytes] self.index = [] # type: typing.List[DFUInfo] def add_file(self, flash_addr, path): # type: (int, str) -> None - """ Add file to be written into flash at given address """ - with open(path, "rb") as f: - self._add_cpio_flash_entry(os.path.basename(path), flash_addr, f.read()) + """ + Add file to be written into flash at given address + + Files are split up into chunks in order avoid timing-out during erasing large regions. Instead of adding + "app.bin" at flash_addr it will add: + 1. app.bin at flash_addr # sizeof(app.bin) == self.part_size + 2. app.bin.1 at flash_addr + self.part_size + 3. app.bin.2 at flash_addr + 2 * self.part_size + ... + + """ + f_name = os.path.basename(path) + with open(path, 'rb') as f: + for i, chunk in enumerate(iter(partial(f.read, self.part_size), b'')): + n = f_name if i == 0 else '.'.join([f_name, str(i)]) + self._add_cpio_flash_entry(n, flash_addr, chunk) + flash_addr += len(chunk) def finish(self): # type: () -> None """ Write DFU file """ # Prepare and add dfuinfo0.dat file - dfuinfo = b"".join([struct.pack(DFUINFO_STRUCT, *item) for item in self.index]) + dfuinfo = b''.join([struct.pack(DFUINFO_STRUCT, *item) for item in self.index]) self._add_cpio_entry(DFUINFO_FILE, dfuinfo, first=True) # Add CPIO archive trailer - self._add_cpio_entry(CPIO_TRAILER, b"", trailer=True) + self._add_cpio_entry(CPIO_TRAILER, b'', trailer=True) # Combine all the entries and pad the file - out_data = b"".join(self.entries) + out_data = b''.join(self.entries) cpio_block_size = 10240 out_data = pad_bytes(out_data, cpio_block_size) # Add DFU suffix and CRC - out_data += struct.pack(DFUSUFFIX_STRUCT, *DFUSUFFIX_DEFAULT) + dfu_suffix = DFUSuffix(0xFFFF, self.pid, ESPRESSIF_VID, 0x0100, b'UFD', 16) + out_data += struct.pack(DFUSUFFIX_STRUCT, *dfu_suffix) out_data += struct.pack(DFUCRC_STRUCT, dfu_crc(out_data)) # Finally write the entire binary @@ -166,7 +185,7 @@ class EspDfuWriter(object): DFUInfo( address=flash_addr, flags=0, - name=filename.encode("utf-8"), + name=filename.encode('utf-8'), md5=md5.digest(), ) ) @@ -175,7 +194,7 @@ class EspDfuWriter(object): def _add_cpio_entry( self, filename, data, first=False, trailer=False ): # type: (str, bytes, bool, bool) -> None - filename_b = filename.encode("utf-8") + b"\x00" + filename_b = filename.encode('utf-8') + b'\x00' cpio_header = make_cpio_header(len(filename_b), len(data), is_trailer=trailer) entry = pad_bytes( struct.pack(CPIO_STRUCT, *cpio_header) + filename_b, 4 @@ -186,30 +205,41 @@ class EspDfuWriter(object): self.entries.insert(0, entry) -def action_write(args): - writer = EspDfuWriter(args['output_file']) +def action_write(args): # type: (typing.Mapping[str, typing.Any]) -> None + writer = EspDfuWriter(args['output_file'], args['pid'], args['part_size']) for addr, f in args['files']: print('Adding {} at {:#x}'.format(f, addr)) writer.add_file(addr, f) writer.finish() print('"{}" has been written. You may proceed with DFU flashing.'.format(args['output_file'].name)) + if args['part_size'] % (4 * 1024) != 0: + print('WARNING: Partition size of DFU is not multiple of 4k (4096). You might get unexpected behavior.') def main(): parser = argparse.ArgumentParser() # Provision to add "info" command - subparsers = parser.add_subparsers(dest="command") - write_parser = subparsers.add_parser("write") - write_parser.add_argument("-o", "--output-file", + subparsers = parser.add_subparsers(dest='command') + write_parser = subparsers.add_parser('write') + write_parser.add_argument('-o', '--output-file', help='Filename for storing the output DFU image', required=True, - type=argparse.FileType("wb")) - write_parser.add_argument("--json", + type=argparse.FileType('wb')) + write_parser.add_argument('--pid', + required=False, # This ESP-IDF release supports one compatible target only + default=2, # ESP32-S2 + type=lambda h: int(h, 16), + help='Hexa-decimal product indentificator') + write_parser.add_argument('--json', help='Optional file for loading "flash_files" dictionary with
items') - write_parser.add_argument("files", - metavar="
", help='Add at
', - nargs="*") + write_parser.add_argument('--part-size', + default=os.environ.get('ESP_DFU_PART_SIZE', 512 * 1024), + type=lambda x: int(x, 0), + help='Larger files are split-up into smaller partitions of this size') + write_parser.add_argument('files', + metavar='
', help='Add at
', + nargs='*') args = parser.parse_args() @@ -236,16 +266,18 @@ def main(): files += [(int(addr, 0), process_json_file(f_name)) for addr, f_name in iteritems(json.load(f)['flash_files'])] - files = sorted([(addr, f_name) for addr, f_name in iteritems(dict(files))], + files = sorted([(addr, f_name.decode('utf-8') if isinstance(f_name, type(b'')) else f_name) for addr, f_name in iteritems(dict(files))], key=lambda x: x[0]) # remove possible duplicates and sort based on the address cmd_args = {'output_file': args.output_file, 'files': files, + 'pid': args.pid, + 'part_size': args.part_size, } {'write': action_write }[args.command](cmd_args) -if __name__ == "__main__": +if __name__ == '__main__': main() diff --git a/tools/test_mkdfu/1/dfu.bin b/tools/test_mkdfu/1/dfu.bin index 74f9fef52c94be0d0c2513169d43ce1f7d7337f0..cc28754f382510ff037315c285e00f82388e353e 100644 GIT binary patch delta 22 dcmbObFd<+AlLj9XgOvdTW2l>pz|rlcM*&7z2D<IO!lbxVUt6C@H8FH$erVQ>h0~N}(Wi(aFsd2zmjp;sHE@XYdBK z(x$Yc6)HH$|CYSuH80_7gdq%}i&XUWc_%f^Nf$Y_Ov#Zm$K80oyAql+Un@y0JU_{c zB>Ua1)>vM*LDf994)(DUHwpV{dXa|lp69-yRADR8l?ETi#6D8FJR-c$PtNYiyG3&E z&z9f47&C9_Mg84U@|bLEI5o$Xs;Xc7I<_K!00IagfB*srAb