dump_otadata.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python
  2. #
  3. # gen_otadata prints info about the otadata partition.
  4. #
  5. # Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http:#www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. from __future__ import print_function, division
  19. import argparse
  20. import os
  21. import re
  22. import struct
  23. import sys
  24. import hashlib
  25. import binascii
  26. __version__ = '1.0'
  27. quiet = False
  28. def status(msg):
  29. """ Print status message to stderr """
  30. if not quiet:
  31. critical(msg)
  32. def critical(msg):
  33. """ Print critical message to stderr """
  34. if not quiet:
  35. sys.stderr.write(msg)
  36. sys.stderr.write('\n')
  37. def little_endian(buff, offset):
  38. data = buff[offset:offset+4]
  39. data.reverse()
  40. data = ''.join(data)
  41. return data
  42. def main():
  43. global quiet
  44. parser = argparse.ArgumentParser(description='Prints otadata partition in human readable form.')
  45. parser.add_argument('--quiet', '-q', help="Don't print status messages to stderr", action='store_true')
  46. search_type = parser.add_mutually_exclusive_group()
  47. parser.add_argument('input', help='Path to binary file containing otadata partition to parse.',
  48. type=argparse.FileType('rb'))
  49. args = parser.parse_args()
  50. quiet = args.quiet
  51. input = args.input.read()
  52. hex_input_0 = binascii.hexlify(input)
  53. hex_input_0 = map(''.join, zip(*[iter(hex_input_0)]*2))
  54. hex_input_1 = binascii.hexlify(input[4096:])
  55. hex_input_1 = map(''.join, zip(*[iter(hex_input_1)]*2))
  56. print("\t%11s\t%8s |\t%8s\t%8s" %("OTA_SEQ", "CRC", "OTA_SEQ", "CRC"))
  57. print("Firmware: 0x%s \t 0x%s |\t0x%s \t 0x%s" % (little_endian(hex_input_0, 0), little_endian(hex_input_0, 28), \
  58. little_endian(hex_input_1, 0), little_endian(hex_input_1, 28)))
  59. class InputError(RuntimeError):
  60. def __init__(self, e):
  61. super(InputError, self).__init__(e)
  62. class ValidationError(InputError):
  63. def __init__(self, partition, message):
  64. super(ValidationError, self).__init__(
  65. "Partition %s invalid: %s" % (partition.name, message))
  66. if __name__ == '__main__':
  67. try:
  68. r = main()
  69. sys.exit(r)
  70. except InputError as e:
  71. print(e, file=sys.stderr)
  72. sys.exit(2)