| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  | #!/usr/bin/env python | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2022-05-23 15:30:13 +02:00
										 |  |  | # SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD | 
					
						
							|  |  |  | # SPDX-License-Identifier: Apache-2.0 | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | from __future__ import division | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  | import argparse | 
					
						
							|  |  |  | import hashlib | 
					
						
							|  |  |  | import json | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import struct | 
					
						
							|  |  |  | from functools import partial | 
					
						
							| 
									
										
										
										
											2022-06-03 14:46:56 +02:00
										 |  |  | from typing import Dict, List | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 14:46:56 +02:00
										 |  |  | def round_up_int_div(n: int, d: int) -> int: | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |     # equivalent to math.ceil(n / d) | 
					
						
							|  |  |  |     return (n + d - 1) // d | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class UF2Writer(object): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # The UF2 format is described here: https://github.com/microsoft/uf2 | 
					
						
							|  |  |  |     UF2_BLOCK_SIZE = 512 | 
					
						
							|  |  |  |     UF2_DATA_SIZE = 476  # max value of CHUNK_SIZE reduced by optional parts. Currently, MD5_PART only. | 
					
						
							|  |  |  |     UF2_MD5_PART_SIZE = 24 | 
					
						
							|  |  |  |     UF2_FIRST_MAGIC = 0x0A324655 | 
					
						
							|  |  |  |     UF2_SECOND_MAGIC = 0x9E5D5157 | 
					
						
							|  |  |  |     UF2_FINAL_MAGIC = 0x0AB16F30 | 
					
						
							|  |  |  |     UF2_FLAG_FAMILYID_PRESENT = 0x00002000 | 
					
						
							|  |  |  |     UF2_FLAG_MD5_PRESENT = 0x00004000 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 14:46:56 +02:00
										 |  |  |     def __init__(self, chip_id: int, output_file: os.PathLike, chunk_size: int) -> None: | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |         self.chip_id = chip_id | 
					
						
							|  |  |  |         self.CHUNK_SIZE = self.UF2_DATA_SIZE - self.UF2_MD5_PART_SIZE if chunk_size is None else chunk_size | 
					
						
							|  |  |  |         self.f = open(output_file, 'wb') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 14:46:56 +02:00
										 |  |  |     def __enter__(self) -> 'UF2Writer': | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |         return self | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 14:46:56 +02:00
										 |  |  |     def __exit__(self, exc_type: str, exc_val: int, exc_tb: List) -> None: | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |         if self.f: | 
					
						
							|  |  |  |             self.f.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @staticmethod | 
					
						
							| 
									
										
										
										
											2022-06-03 14:46:56 +02:00
										 |  |  |     def _to_uint32(num: int) -> bytes: | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |         return struct.pack('<I', num) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 14:46:56 +02:00
										 |  |  |     def _write_block(self, addr: int, chunk: bytes, len_chunk: int, block_no: int, blocks: int) -> None: | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |         assert len_chunk > 0 | 
					
						
							|  |  |  |         assert len_chunk <= self.CHUNK_SIZE | 
					
						
							|  |  |  |         assert block_no < blocks | 
					
						
							|  |  |  |         block = self._to_uint32(self.UF2_FIRST_MAGIC) | 
					
						
							|  |  |  |         block += self._to_uint32(self.UF2_SECOND_MAGIC) | 
					
						
							|  |  |  |         block += self._to_uint32(self.UF2_FLAG_FAMILYID_PRESENT | self.UF2_FLAG_MD5_PRESENT) | 
					
						
							|  |  |  |         block += self._to_uint32(addr) | 
					
						
							|  |  |  |         block += self._to_uint32(len_chunk) | 
					
						
							|  |  |  |         block += self._to_uint32(block_no) | 
					
						
							|  |  |  |         block += self._to_uint32(blocks) | 
					
						
							|  |  |  |         block += self._to_uint32(self.chip_id) | 
					
						
							|  |  |  |         block += chunk | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         md5_part = self._to_uint32(addr) | 
					
						
							|  |  |  |         md5_part += self._to_uint32(len_chunk) | 
					
						
							|  |  |  |         md5_part += hashlib.md5(chunk).digest() | 
					
						
							| 
									
										
										
										
											2022-08-10 09:01:57 +02:00
										 |  |  |         assert len(md5_part) == self.UF2_MD5_PART_SIZE | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         block += md5_part | 
					
						
							|  |  |  |         block += b'\x00' * (self.UF2_DATA_SIZE - self.UF2_MD5_PART_SIZE - len_chunk) | 
					
						
							|  |  |  |         block += self._to_uint32(self.UF2_FINAL_MAGIC) | 
					
						
							|  |  |  |         assert len(block) == self.UF2_BLOCK_SIZE | 
					
						
							|  |  |  |         self.f.write(block) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 14:46:56 +02:00
										 |  |  |     def add_file(self, addr: int, f_path: os.PathLike) -> None: | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |         blocks = round_up_int_div(os.path.getsize(f_path), self.CHUNK_SIZE) | 
					
						
							|  |  |  |         with open(f_path, 'rb') as fin: | 
					
						
							|  |  |  |             a = addr | 
					
						
							|  |  |  |             for i, chunk in enumerate(iter(partial(fin.read, self.CHUNK_SIZE), b'')): | 
					
						
							|  |  |  |                 len_chunk = len(chunk) | 
					
						
							|  |  |  |                 self._write_block(a, chunk, len_chunk, i, blocks) | 
					
						
							|  |  |  |                 a += len_chunk | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 14:46:56 +02:00
										 |  |  | def action_write(args: Dict) -> None: | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |     with UF2Writer(args['chip_id'], args['output_file'], args['chunk_size']) as writer: | 
					
						
							|  |  |  |         for addr, f in args['files']: | 
					
						
							|  |  |  |             print('Adding {} at {:#x}'.format(f, addr)) | 
					
						
							|  |  |  |             writer.add_file(addr, f) | 
					
						
							|  |  |  |     print('"{}" has been written.'.format(args['output_file'])) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 14:46:56 +02:00
										 |  |  | def main() -> None: | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |     parser = argparse.ArgumentParser() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 14:46:56 +02:00
										 |  |  |     def four_byte_aligned(integer: int) -> bool: | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |         return integer & 3 == 0 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 14:46:56 +02:00
										 |  |  |     def parse_chunk_size(string: str) -> int: | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |         num = int(string, 0) | 
					
						
							|  |  |  |         if not four_byte_aligned(num): | 
					
						
							|  |  |  |             raise argparse.ArgumentTypeError('Chunk size should be a 4-byte aligned number') | 
					
						
							|  |  |  |         return num | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 14:46:56 +02:00
										 |  |  |     def parse_chip_id(string: str) -> int: | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |         num = int(string, 16) | 
					
						
							|  |  |  |         if num < 0 or num > 0xFFFFFFFF: | 
					
						
							|  |  |  |             raise argparse.ArgumentTypeError('Chip ID should be a 4-byte unsigned integer') | 
					
						
							|  |  |  |         return num | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Provision to add "info" command | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     subparsers = parser.add_subparsers(dest='command') | 
					
						
							|  |  |  |     write_parser = subparsers.add_parser('write') | 
					
						
							|  |  |  |     write_parser.add_argument('-o', '--output-file', | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |                               help='Filename for storing the output UF2 image', | 
					
						
							|  |  |  |                               required=True) | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     write_parser.add_argument('--chip-id', | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |                               required=True, | 
					
						
							|  |  |  |                               type=parse_chip_id, | 
					
						
							|  |  |  |                               help='Hexa-decimal chip identificator') | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     write_parser.add_argument('--chunk-size', | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |                               required=False, | 
					
						
							|  |  |  |                               type=parse_chunk_size, | 
					
						
							|  |  |  |                               default=None, | 
					
						
							|  |  |  |                               help='Specify the used data part of the 512 byte UF2 block. A common value is 256. By ' | 
					
						
							|  |  |  |                                    'default the largest possible value will be used.') | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     write_parser.add_argument('--json', | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |                               help='Optional file for loading "flash_files" dictionary with <address> <file> items') | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     write_parser.add_argument('--bin', | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |                               help='Use only a subset of binaries from the JSON file, e.g. "partition_table ' | 
					
						
							|  |  |  |                                    'bootloader app"', | 
					
						
							|  |  |  |                               nargs='*') | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     write_parser.add_argument('files', | 
					
						
							|  |  |  |                               metavar='<address> <file>', help='Add <file> at <address>', | 
					
						
							|  |  |  |                               nargs='*') | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 14:46:56 +02:00
										 |  |  |     def check_file(file_name: str) -> str: | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |         if not os.path.isfile(file_name): | 
					
						
							|  |  |  |             raise RuntimeError('{} is not a regular file!'.format(file_name)) | 
					
						
							|  |  |  |         return file_name | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 14:46:56 +02:00
										 |  |  |     def parse_addr(string: str) -> int: | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |         num = int(string, 0) | 
					
						
							|  |  |  |         if not four_byte_aligned(num): | 
					
						
							|  |  |  |             raise RuntimeError('{} is not a 4-byte aligned valid address'.format(string)) | 
					
						
							|  |  |  |         return num | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     files = [] | 
					
						
							|  |  |  |     if args.files: | 
					
						
							|  |  |  |         files += [(parse_addr(addr), check_file(f_name)) for addr, f_name in zip(args.files[::2], args.files[1::2])] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if args.json: | 
					
						
							|  |  |  |         json_dir = os.path.dirname(os.path.abspath(args.json)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 14:46:56 +02:00
										 |  |  |         def process_json_file(path: str) -> str: | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |             '''
 | 
					
						
							|  |  |  |             The input path is relative to json_dir. This function makes it relative to the current working | 
					
						
							|  |  |  |             directory. | 
					
						
							|  |  |  |             '''
 | 
					
						
							|  |  |  |             return check_file(os.path.relpath(os.path.join(json_dir, path), start=os.curdir)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with open(args.json) as f: | 
					
						
							|  |  |  |             json_content = json.load(f) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if args.bin: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 bin_selection = [json_content[b] for b in args.bin] | 
					
						
							|  |  |  |                 flash_dic = dict((x['offset'], x['file']) for x in bin_selection) | 
					
						
							|  |  |  |             except KeyError: | 
					
						
							|  |  |  |                 print('Invalid binary was selected.') | 
					
						
							| 
									
										
										
										
											2022-07-07 14:45:09 +02:00
										 |  |  |                 valid = [k if all(x in v for x in ('offset', 'file')) else None for k, v in json_content.items()] | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |                 print('Valid ones:', ' '.join(x for x in valid if x)) | 
					
						
							|  |  |  |                 exit(1) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             flash_dic = json_content['flash_files'] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 14:45:09 +02:00
										 |  |  |         files += [(parse_addr(addr), process_json_file(f_name)) for addr, f_name in flash_dic.items()] | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 14:45:09 +02:00
										 |  |  |     files = sorted([(addr, f_name) for addr, f_name in dict(files).items()], | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |                    key=lambda x: x[0])  # remove possible duplicates and sort based on the address | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     cmd_args = {'output_file': args.output_file, | 
					
						
							|  |  |  |                 'files': files, | 
					
						
							|  |  |  |                 'chip_id': args.chip_id, | 
					
						
							|  |  |  |                 'chunk_size': args.chunk_size, | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     {'write': action_write | 
					
						
							|  |  |  |      }[args.command](cmd_args) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2020-12-11 16:28:11 +01:00
										 |  |  |     main() |