|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright (c) 2024 Nordic Semiconductor ASA |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 6 | + |
| 7 | +import argparse |
| 8 | +import os |
| 9 | +import struct |
| 10 | +import errno |
| 11 | + |
| 12 | +HEADER_LENGTH = 42 |
| 13 | +HEADER_MAGIC = 0x424ad2dc |
| 14 | + |
| 15 | +class chunk_header: |
| 16 | + def __init__(self, number, is_last, offset, version_str): |
| 17 | + self.__number = number |
| 18 | + self.__is_last = is_last |
| 19 | + self.__offset = offset |
| 20 | + self.__version_str = version_str.encode('utf-8') |
| 21 | + self.__pack_format = '<IB?I32s' |
| 22 | + |
| 23 | + def encode(self): |
| 24 | + """ |
| 25 | + Encode the header, which consists of: |
| 26 | + - Header magic (4 bytes). |
| 27 | + - File number (1 byte). |
| 28 | + - Flag indicating whether the file is the last in the sequence (1 byte). |
| 29 | + - Offset of the application update data relative to the full sequence (4 bytes). |
| 30 | + - Application version string (32 bytes). |
| 31 | + """ |
| 32 | + return struct.pack(self.__pack_format, HEADER_MAGIC, self.__number, self.__is_last, self.__offset, self.__version_str) |
| 33 | + |
| 34 | +def split(image, max_chunk_size): |
| 35 | + """ |
| 36 | + Split the provided image into chunks of size of up to max_chunk_size bytes. |
| 37 | + """ |
| 38 | + chunks = [] |
| 39 | + |
| 40 | + with open(image, 'rb') as file: |
| 41 | + while True: |
| 42 | + chunk = file.read(max_chunk_size) |
| 43 | + if not chunk: |
| 44 | + break |
| 45 | + chunks.append(chunk) |
| 46 | + |
| 47 | + return chunks |
| 48 | + |
| 49 | +def parse_args(): |
| 50 | + parser = argparse.ArgumentParser( |
| 51 | + description='Split the application update image into proprietary LwM2M Carrier divided DFU files', |
| 52 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 53 | + allow_abbrev=False) |
| 54 | + |
| 55 | + parser.add_argument('-i', '--input-file', required=True, help='The application update file to be divided.') |
| 56 | + parser.add_argument('-v', '--version-str', required=True, help='The application version string to be included in the header (maximum 32 bytes including the null termination).') |
| 57 | + parser.add_argument('-s', '--max-file-size', default=100, type=int, help='(Optional) The maximum size of a divided DFU file in KB.') |
| 58 | + parser.add_argument('-o', '--output-dir', required=True, help='The directory to store the divided DFU files into.') |
| 59 | + |
| 60 | + return parser.parse_args() |
| 61 | + |
| 62 | +def main(): |
| 63 | + # Parse arguments. |
| 64 | + args = parse_args() |
| 65 | + |
| 66 | + # Validate the length of the version string. |
| 67 | + if len(args.version_str) > 31: |
| 68 | + raise OSError(errno.EINVAL, "Input version string is too long") |
| 69 | + |
| 70 | + input_file = args.input_file |
| 71 | + version_str = args.version_str |
| 72 | + max_chunk_size = (args.max_file_size * 1024) - HEADER_LENGTH |
| 73 | + output_dir = args.output_dir |
| 74 | + |
| 75 | + # Split the application update image into chunks of max_chunk_size bytes, which excludes |
| 76 | + # the length of the header from the maximum size of a divided DFU file. |
| 77 | + chunks = split(input_file, max_chunk_size) |
| 78 | + total_chunks = len(chunks) |
| 79 | + |
| 80 | + # Variable to track the offset of each chunk within the whole image. |
| 81 | + offset = 0 |
| 82 | + |
| 83 | + print("Generating LwM2M Carrier divided DFU files...") |
| 84 | + |
| 85 | + # Append headers to each chunk and store in the output directory. |
| 86 | + for i, chunk in enumerate(chunks): |
| 87 | + # Create the chunk header (1-indexed numbering). |
| 88 | + header = chunk_header(i + 1, (i == total_chunks - 1), offset, version_str) |
| 89 | + |
| 90 | + # Increase the offset (image chunk only, no header included). |
| 91 | + offset += len(chunk) |
| 92 | + |
| 93 | + # Append the header to the chunk. |
| 94 | + chunk = header.encode() + chunk |
| 95 | + |
| 96 | + # Generate file name of each chunk. |
| 97 | + chunk_version_str = version_str + '_{0:03d}.bin'.format(i + 1) |
| 98 | + |
| 99 | + # Write the chunk into the output directory. |
| 100 | + with open(os.path.join(output_dir, chunk_version_str), 'wb') as chunk_file: |
| 101 | + chunk_file.write(chunk) |
| 102 | + chunk_file.close() |
| 103 | + print("Encoded %6d bytes to %s" % (len(chunk), chunk_version_str)) |
| 104 | + |
| 105 | + print("LwM2M Carrier divided DFU files generated") |
| 106 | + |
| 107 | +if __name__ == '__main__': |
| 108 | + try: |
| 109 | + main() |
| 110 | + except Exception: |
| 111 | + print("Failed to generate LwM2M Carrier divided DFU files") |
| 112 | + exit(1) |
0 commit comments