parttool.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env python
  2. #
  3. # parttool returns info about the required partition.
  4. #
  5. # This utility is used by the make system to get information
  6. # about the start addresses: partition table, factory area, phy area.
  7. #
  8. # Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  9. #
  10. # Licensed under the Apache License, Version 2.0 (the "License");
  11. # you may not use this file except in compliance with the License.
  12. # You may obtain a copy of the License at
  13. #
  14. # http:#www.apache.org/licenses/LICENSE-2.0
  15. #
  16. # Unless required by applicable law or agreed to in writing, software
  17. # distributed under the License is distributed on an "AS IS" BASIS,
  18. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. # See the License for the specific language governing permissions and
  20. # limitations under the License.
  21. from __future__ import print_function, division
  22. import argparse
  23. import os
  24. import re
  25. import struct
  26. import sys
  27. import hashlib
  28. import binascii
  29. import gen_esp32part as gen
  30. __version__ = '1.0'
  31. quiet = False
  32. def status(msg):
  33. """ Print status message to stderr """
  34. if not quiet:
  35. critical(msg)
  36. def critical(msg):
  37. """ Print critical message to stderr """
  38. if not quiet:
  39. sys.stderr.write(msg)
  40. sys.stderr.write('\n')
  41. def main():
  42. global quiet
  43. parser = argparse.ArgumentParser(description='Returns info about the required partition.')
  44. parser.add_argument('--quiet', '-q', help="Don't print status messages to stderr", action='store_true')
  45. parser.add_argument('--partition-table-offset', help='The offset of the partition table in flash. Only consulted if partition table is in CSV format.', type=str, default='0x8000')
  46. search_type = parser.add_mutually_exclusive_group()
  47. search_type.add_argument('--partition-name', '-p', help='The name of the required partition', type=str, default=None)
  48. search_type.add_argument('--type', '-t', help='The type of the required partition', type=str, default=None)
  49. search_type.add_argument('--default-boot-partition', help='Select the default boot partition, '+
  50. 'using the same fallback logic as the IDF bootloader', action="store_true")
  51. parser.add_argument('--subtype', '-s', help='The subtype of the required partition', type=str, default=None)
  52. parser.add_argument('--offset', '-o', help='Return offset of required partition', action="store_true")
  53. parser.add_argument('--size', help='Return size of required partition', action="store_true")
  54. parser.add_argument('input', help='Path to CSV or binary file to parse. Will use stdin if omitted.',
  55. type=argparse.FileType('rb'), default=sys.stdin)
  56. args = parser.parse_args()
  57. if args.type is not None and args.subtype is None:
  58. status("If --type is specified, --subtype is required")
  59. return 2
  60. if args.type is None and args.subtype is not None:
  61. status("--subtype is only used with --type")
  62. return 2
  63. quiet = args.quiet
  64. gen.offset_part_table = int(args.partition_table_offset, 0)
  65. input = args.input.read()
  66. input_is_binary = input[0:2] == gen.PartitionDefinition.MAGIC_BYTES
  67. if input_is_binary:
  68. status("Parsing binary partition input...")
  69. table = gen.PartitionTable.from_binary(input)
  70. else:
  71. input = input.decode()
  72. status("Parsing CSV input...")
  73. table = gen.PartitionTable.from_csv(input)
  74. found_partition = None
  75. if args.default_boot_partition:
  76. search = [ "factory" ] + [ "ota_%d" % d for d in range(16) ]
  77. for subtype in search:
  78. found_partition = table.find_by_type("app", subtype)
  79. if found_partition is not None:
  80. break
  81. elif args.partition_name is not None:
  82. found_partition = table.find_by_name(args.partition_name)
  83. elif args.type is not None:
  84. found_partition = table.find_by_type(args.type, args.subtype)
  85. else:
  86. raise RuntimeError("invalid partition selection choice")
  87. if found_partition is None:
  88. return 1 # nothing found
  89. if args.offset:
  90. print('0x%x' % (found_partition.offset))
  91. if args.size:
  92. print('0x%x' % (found_partition.size))
  93. return 0
  94. class InputError(RuntimeError):
  95. def __init__(self, e):
  96. super(InputError, self).__init__(e)
  97. class ValidationError(InputError):
  98. def __init__(self, partition, message):
  99. super(ValidationError, self).__init__(
  100. "Partition %s invalid: %s" % (partition.name, message))
  101. if __name__ == '__main__':
  102. try:
  103. r = main()
  104. sys.exit(r)
  105. except InputError as e:
  106. print(e, file=sys.stderr)
  107. sys.exit(2)