check_sizes.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/env python
  2. #
  3. # check_sizes.py is a tool run by the ESP-IDF build system
  4. # to check a particular binary fits in the available partitions of
  5. # a particular type/subtype. Can be used to check if the app binary fits in
  6. # all available app partitions, for example.
  7. #
  8. # (Can also check if the bootloader binary fits before the partition table.)
  9. #
  10. # Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  11. #
  12. # Licensed under the Apache License, Version 2.0 (the "License");
  13. # you may not use this file except in compliance with the License.
  14. # You may obtain a copy of the License at
  15. #
  16. # http://www.apache.org/licenses/LICENSE-2.0
  17. #
  18. # Unless required by applicable law or agreed to in writing, software
  19. # distributed under the License is distributed on an "AS IS" BASIS,
  20. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. # See the License for the specific language governing permissions and
  22. # limitations under the License.
  23. from __future__ import division, print_function, unicode_literals
  24. import argparse
  25. import io # noqa: F401 # pylint: disable=unused-import
  26. import os
  27. import sys
  28. try:
  29. from typing import IO # noqa: F401 # pylint: disable=unused-import
  30. except ImportError:
  31. pass # used for type hinting only
  32. import gen_esp32part
  33. from gen_esp32part import PartitionTable, get_ptype_as_int, get_subtype_as_int
  34. allow_failures = False
  35. def _file_size(f): # type: (IO) -> int
  36. before = f.tell()
  37. f.seek(0, 2) # seek to end
  38. result = f.tell()
  39. f.seek(before)
  40. return result
  41. def _fail(msg): # type: (str) -> None
  42. if allow_failures:
  43. print('Warning: {}'.format(msg))
  44. else:
  45. raise SystemExit('Error: {}'.format(msg))
  46. def check_bootloader(partition_table_offset, bootloader_offset, binary_file): # type: (int, int, IO) -> None
  47. max_size = partition_table_offset - bootloader_offset
  48. bootloader_size = _file_size(binary_file)
  49. if bootloader_size > max_size:
  50. msg = ('Bootloader binary size {:#x} bytes is too large for partition table offset {:#02x}. ' +
  51. 'Bootloader binary can be maximum {:#x} ({}) bytes unless the partition table offset ' +
  52. 'is increased in the Partition Table section of the project configuration menu.').format(
  53. bootloader_size, partition_table_offset, max_size, max_size)
  54. _fail(msg)
  55. free_size = max_size - bootloader_size
  56. print('Bootloader binary size {:#x} bytes. {:#x} bytes ({}%) free.'.format(
  57. bootloader_size, free_size, round(free_size * 100 / max_size)))
  58. def check_partition(ptype, subtype, partition_table_file, bin_file): # type: (str, str, io.IOBase, IO) -> None
  59. table, _ = PartitionTable.from_file(partition_table_file)
  60. ptype_str = str(ptype)
  61. ptype = get_ptype_as_int(ptype)
  62. partitions = [p for p in table if p.type == ptype]
  63. if subtype is not None:
  64. ptype_str += ' ({})'.format(subtype)
  65. subtype = get_subtype_as_int(ptype, subtype)
  66. partitions = [p for p in partitions if p.subtype == subtype]
  67. if len(partitions) == 0:
  68. print('WARNING: Partition table does not contain any partitions matching {}'.format(ptype_str))
  69. return
  70. bin_name = os.path.basename(bin_file.name)
  71. bin_size = _file_size(bin_file)
  72. smallest_size = min(p.size for p in partitions)
  73. if smallest_size >= bin_size:
  74. free_size = smallest_size - bin_size
  75. print('{} binary size {:#x} bytes. Smallest {} partition is {:#x} bytes. {:#x} bytes ({}%) free.'.format(
  76. bin_name, bin_size, ptype_str, smallest_size, free_size, round(free_size * 100 / smallest_size)))
  77. return
  78. too_small_partitions = [p for p in partitions if p.size < bin_size]
  79. if len(partitions) == 1:
  80. msg = '{} partition is'.format(ptype_str)
  81. elif len(partitions) == len(too_small_partitions):
  82. msg = 'All {} partitions are'.format(ptype_str)
  83. else:
  84. msg = '{}/{} {} partitions are'.format(len(too_small_partitions), len(partitions), ptype_str)
  85. msg += ' too small for binary {} size {:#x}:'.format(bin_name, bin_size)
  86. for p in too_small_partitions:
  87. msg += '\n - {} (overflow {:#x})'.format(p, bin_size - p.size)
  88. if not allow_failures and len(partitions) == len(too_small_partitions):
  89. # if some partitions can fit the binary then just print a warning
  90. raise SystemExit('Error: ' + msg)
  91. else:
  92. print('Warning: ' + msg)
  93. def main(): # type: () -> None
  94. global allow_failures # pylint: disable=global-statement
  95. parser = argparse.ArgumentParser(description='Check binary sizes against partition table entries')
  96. parser.add_argument('--target', choices=['esp32', 'esp32s2'])
  97. parser.add_argument('--allow_failures', action='store_true', help='If true, failures will print warnings but not exit with an error')
  98. parser.add_argument('--offset', '-o', help='Set partition table offset', default='0x8000')
  99. subparsers = parser.add_subparsers(dest='check_target',
  100. help='Type of binary to check against partition table layout')
  101. sp_bootloader = subparsers.add_parser('bootloader')
  102. sp_bootloader.add_argument('bootloader_offset', help='Hex offset of bootloader in flash')
  103. sp_bootloader.add_argument('bootloader_binary', type=argparse.FileType('rb'), help='Bootloader binary (.bin) file from build output')
  104. sp_part = subparsers.add_parser('partition')
  105. sp_part.add_argument('--type', type=str, help='Check the file size against all partitions of this type.', required=True)
  106. sp_part.add_argument('--subtype', type=str, help='Optional, only check the file size against all partitions of this subtype.')
  107. sp_part.add_argument('partition_table', type=argparse.FileType('rb'), help='Partition table file')
  108. sp_part.add_argument('binary', type=argparse.FileType('rb'), help='Binary file which will have the size checked')
  109. args = parser.parse_args()
  110. gen_esp32part.quiet = True
  111. args.offset = int(args.offset, 0)
  112. gen_esp32part.offset_part_table = args.offset
  113. if args.check_target is None: # add_subparsers only has a 'required' argument since Python 3
  114. parser.print_help()
  115. sys.exit(1)
  116. if args.check_target == 'bootloader':
  117. check_bootloader(args.offset, int(args.bootloader_offset, 0), args.bootloader_binary)
  118. else:
  119. check_partition(args.type, args.subtype, args.partition_table, args.binary)
  120. if __name__ == '__main__':
  121. main()