You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gen_imghdr.py 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env python3
  2. # Derived from pad_checksum in the Pico SDK, which carries the following
  3. # LICENSE.txt:
  4. # Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd.
  5. #
  6. # Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
  7. # following conditions are met:
  8. #
  9. # 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
  10. # disclaimer.
  11. #
  12. # 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
  13. # disclaimer in the documentation and/or other materials provided with the distribution.
  14. #
  15. # 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
  16. # derived from this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  19. # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. import argparse
  26. import binascii
  27. import struct
  28. import sys
  29. def any_int(x):
  30. try:
  31. return int(x, 0)
  32. except:
  33. raise argparse.ArgumentTypeError("expected an integer, not '{!r}'".format(x))
  34. parser = argparse.ArgumentParser()
  35. parser.add_argument("ifile", help="Input application binary (binary)")
  36. parser.add_argument("ofile", help="Output header file (binary)")
  37. parser.add_argument("-a", "--addr", help="Load address of the application image",
  38. type=any_int, default=0x10004000)
  39. args = parser.parse_args()
  40. try:
  41. idata = open(args.ifile, "rb").read()
  42. except:
  43. sys.exit("Could not open input file '{}'".format(args.ifile))
  44. vtor = args.addr
  45. size = len(idata)
  46. crc = binascii.crc32(idata)
  47. odata = vtor.to_bytes(4, byteorder='little') + size.to_bytes(4, byteorder='little') + crc.to_bytes(4, byteorder='little')
  48. try:
  49. with open(args.ofile, "wb") as ofile:
  50. ofile.write(odata)
  51. except:
  52. sys.exit("Could not open output file '{}'".format(args.ofile))