您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

gen_imghdr.py 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. parser.add_argument("-m", "--map", help="Map file to scan for application image section")
  40. parser.add_argument("-s", "--section", help="Section name to look for in map file",
  41. default=".app_bin")
  42. args = parser.parse_args()
  43. try:
  44. idata = open(args.ifile, "rb").read()
  45. except:
  46. sys.exit("Could not open input file '{}'".format(args.ifile))
  47. if args.map:
  48. vtor = None
  49. with open(args.map) as mapfile:
  50. for line in mapfile:
  51. if line.startswith(args.section):
  52. parts = line.split()
  53. if len(parts)>1:
  54. vtor = int(parts[1],0)
  55. print('found address 0x%x for %s in %s'%(vtor,args.section,args.map))
  56. break
  57. if vtor is None:
  58. sys.exit("Could not find section {} in {}".format(args.section,args.map))
  59. else:
  60. vtor = args.addr
  61. size = len(idata)
  62. crc = binascii.crc32(idata)
  63. odata = vtor.to_bytes(4, byteorder='little') + size.to_bytes(4, byteorder='little') + crc.to_bytes(4, byteorder='little')
  64. try:
  65. with open(args.ofile, "wb") as ofile:
  66. ofile.write(odata)
  67. except:
  68. sys.exit("Could not open output file '{}'".format(args.ofile))