parttool.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. search_type = parser.add_mutually_exclusive_group()
  46. search_type.add_argument('--partition-name', '-p', help='The name of the required partition', type=str, default=None)
  47. search_type.add_argument('--type', '-t', help='The type of the required partition', type=str, default=None)
  48. search_type.add_argument('--default-boot-partition', help='Select the default boot partition, '+
  49. 'using the same fallback logic as the IDF bootloader', action="store_true")
  50. parser.add_argument('--subtype', '-s', help='The subtype of the required partition', type=str, default=None)
  51. parser.add_argument('--offset', '-o', help='Return offset of required partition', action="store_true")
  52. parser.add_argument('--size', help='Return size of required partition', action="store_true")
  53. parser.add_argument('input', help='Path to CSV or binary file to parse. Will use stdin if omitted.',
  54. type=argparse.FileType('rb'), default=sys.stdin)
  55. args = parser.parse_args()
  56. if args.type is not None and args.subtype is None:
  57. status("If --type is specified, --subtype is required")
  58. return 2
  59. if args.type is None and args.subtype is not None:
  60. status("--subtype is only used with --type")
  61. return 2
  62. quiet = args.quiet
  63. input = args.input.read()
  64. input_is_binary = input[0:2] == gen.PartitionDefinition.MAGIC_BYTES
  65. if input_is_binary:
  66. status("Parsing binary partition input...")
  67. table = gen.PartitionTable.from_binary(input)
  68. else:
  69. input = input.decode()
  70. status("Parsing CSV input...")
  71. table = gen.PartitionTable.from_csv(input)
  72. found_partition = None
  73. if args.default_boot_partition:
  74. search = [ "factory" ] + [ "ota_%d" % d for d in range(16) ]
  75. for subtype in search:
  76. found_partition = table.find_by_type("app", subtype)
  77. if found_partition is not None:
  78. break
  79. elif args.partition_name is not None:
  80. found_partition = table.find_by_name(args.partition_name)
  81. elif args.type is not None:
  82. found_partition = table.find_by_type(args.type, args.subtype)
  83. else:
  84. raise RuntimeError("invalid partition selection choice")
  85. if found_partition is None:
  86. return 1 # nothing found
  87. if args.offset:
  88. print('0x%x ' % (found_partition.offset))
  89. if args.size:
  90. print('0x%x' % (found_partition.size))
  91. return 0
  92. class InputError(RuntimeError):
  93. def __init__(self, e):
  94. super(InputError, self).__init__(e)
  95. class ValidationError(InputError):
  96. def __init__(self, partition, message):
  97. super(ValidationError, self).__init__(
  98. "Partition %s invalid: %s" % (partition.name, message))
  99. if __name__ == '__main__':
  100. try:
  101. r = main()
  102. sys.exit(r)
  103. except InputError as e:
  104. print(e, file=sys.stderr)
  105. sys.exit(2)