mkuf2.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env python
  2. #
  3. # SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD
  4. # SPDX-License-Identifier: Apache-2.0
  5. # Module was moved to the esptool in ESP-IDF v5.2 and relicensed under GPL v2.0 license.
  6. from __future__ import division
  7. import argparse
  8. import json
  9. import os
  10. import subprocess
  11. import sys
  12. def main() -> None:
  13. parser = argparse.ArgumentParser()
  14. def parse_chip_id(string: str) -> str:
  15. # compatibility layer with old script
  16. print("DEPRECATED option '--chip-id'. Please consider using '--chip' instead")
  17. # DO NOT add new IDs; they are now maintained in esptool.
  18. ids = {
  19. 0x1c5f21b0: 'esp32',
  20. 0xbfdd4eee: 'esp32s2',
  21. 0xd42ba06c: 'esp32c3',
  22. 0xc47e5767: 'esp32s3',
  23. 0x332726f6: 'esp32h2',
  24. 0x2b88d29c: 'esp32c2',
  25. 0x540ddf62: 'esp32c6',
  26. 0x3d308e94: 'esp32p4',
  27. }
  28. try:
  29. return ids[int(string, 16)]
  30. except KeyError:
  31. raise argparse.ArgumentTypeError('Unknown Chip ID')
  32. # Provision to add "info" command
  33. subparsers = parser.add_subparsers(dest='command')
  34. write_parser = subparsers.add_parser('write')
  35. write_parser.add_argument('-o', '--output-file',
  36. help='Filename for storing the output UF2 image',
  37. required=True)
  38. group = write_parser.add_mutually_exclusive_group(required=True)
  39. # chip-id used just for backwards compatibility, UF2 family IDs are now stored in esptool
  40. group.add_argument('--chip-id',
  41. type=parse_chip_id,
  42. help=argparse.SUPPRESS)
  43. group.add_argument('--chip',
  44. type=str,
  45. help='Target chip type')
  46. write_parser.add_argument('--chunk-size',
  47. required=False,
  48. type=int,
  49. default=None,
  50. help='Specify the used data part of the 512 byte UF2 block. A common value is 256. By '
  51. 'default the largest possible value will be used.')
  52. write_parser.add_argument('--json',
  53. help='Optional file for loading "flash_files" dictionary with <address> <file> items')
  54. write_parser.add_argument('--bin',
  55. help='Use only a subset of binaries from the JSON file, e.g. "partition_table '
  56. 'bootloader app"',
  57. nargs='*')
  58. write_parser.add_argument('--md5-disable',
  59. help='Disable MD5 checksum. Useful for compatibility with e.g. TinyUF2',
  60. action='store_true')
  61. write_parser.add_argument('files',
  62. metavar='<address> <file>', help='Add <file> at <address>',
  63. nargs='*')
  64. args = parser.parse_args()
  65. def check_file(file_name: str) -> str:
  66. if not os.path.isfile(file_name):
  67. raise RuntimeError('{} is not a regular file!'.format(file_name))
  68. return file_name
  69. files = []
  70. if args.files:
  71. files += [(addr, check_file(f_name)) for addr, f_name in zip(args.files[::2], args.files[1::2])]
  72. if args.json:
  73. json_dir = os.path.dirname(os.path.abspath(args.json))
  74. def process_json_file(path: str) -> str:
  75. '''
  76. The input path is relative to json_dir. This function makes it relative to the current working
  77. directory.
  78. '''
  79. return check_file(os.path.abspath(os.path.join(json_dir, path)))
  80. with open(args.json) as f:
  81. json_content = json.load(f)
  82. if args.bin:
  83. try:
  84. bin_selection = [json_content[b] for b in args.bin]
  85. flash_dic = dict((x['offset'], x['file']) for x in bin_selection)
  86. except KeyError:
  87. print('Invalid binary was selected.')
  88. valid = [k if all(x in v for x in ('offset', 'file')) else None for k, v in json_content.items()]
  89. print('Valid ones:', ' '.join(x for x in valid if x))
  90. exit(1)
  91. else:
  92. flash_dic = json_content['flash_files']
  93. files += [(addr, process_json_file(f_name)) for addr, f_name in flash_dic.items()]
  94. # remove possible duplicates and sort based on the address
  95. files = sorted([(addr, f_name) for addr, f_name in dict(files).items()], key=lambda x: x[0]) # type: ignore
  96. # list of tuples to simple list
  97. files = [item for t in files for item in t]
  98. cmd = [
  99. sys.executable, '-m', 'esptool',
  100. '--chip', args.chip_id or args.chip,
  101. 'merge_bin',
  102. '--format', 'uf2',
  103. '-o', args.output_file,
  104. ]
  105. if args.chunk_size:
  106. cmd.extend(['--chunk_size', args.chunk_size])
  107. if args.md5_disable:
  108. cmd.append('--md5-disable')
  109. cmd_str = ' '.join(cmd + files)
  110. print(f'Executing: {cmd_str}')
  111. sys.exit(subprocess.run(cmd + files).returncode)
  112. if __name__ == '__main__':
  113. main()