px_mkfw.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # Copyright (C) 2012, 2013 PX4 Development Team. All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. #
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in
  14. # the documentation and/or other materials provided with the
  15. # distribution.
  16. # 3. Neither the name PX4 nor the names of its contributors may be
  17. # used to endorse or promote products derived from this software
  18. # without specific prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. # POSSIBILITY OF SUCH DAMAGE.
  32. #
  33. ############################################################################
  34. #
  35. # PX4 firmware image generator
  36. #
  37. # The PX4 firmware file is a JSON-encoded Python object, containing
  38. # metadata fields and a zlib-compressed base64-encoded firmware image.
  39. #
  40. import sys
  41. import argparse
  42. import json
  43. import base64
  44. import zlib
  45. import time
  46. import subprocess
  47. #
  48. # Construct a basic firmware description
  49. #
  50. def mkdesc():
  51. proto = {}
  52. proto['magic'] = "PX4FWv1"
  53. proto['board_id'] = 0
  54. proto['board_revision'] = 0
  55. proto['version'] = ""
  56. proto['summary'] = ""
  57. proto['description'] = ""
  58. proto['git_identity'] = ""
  59. proto['build_time'] = 0
  60. proto['image'] = bytes()
  61. proto['image_size'] = 0
  62. return proto
  63. # Parse commandline
  64. parser = argparse.ArgumentParser(description="Firmware generator for the PX autopilot system.")
  65. parser.add_argument("--prototype", action="store", help="read a prototype description from a file")
  66. parser.add_argument("--board_id", action="store", help="set the board ID required")
  67. parser.add_argument("--board_revision", action="store", help="set the board revision required")
  68. parser.add_argument("--version", action="store", help="set a version string")
  69. parser.add_argument("--summary", action="store", help="set a brief description")
  70. parser.add_argument("--description", action="store", help="set a longer description")
  71. parser.add_argument("--git_identity", action="store", help="the working directory to check for git identity")
  72. parser.add_argument("--image", action="store", help="the firmware image")
  73. args = parser.parse_args()
  74. # Fetch the firmware descriptor prototype if specified
  75. if args.prototype != None:
  76. f = open(args.prototype,"r")
  77. desc = json.load(f)
  78. f.close()
  79. else:
  80. desc = mkdesc()
  81. desc['build_time'] = int(time.time())
  82. if args.board_id != None:
  83. desc['board_id'] = int(args.board_id)
  84. if args.board_revision != None:
  85. desc['board_revision'] = int(args.board_revision)
  86. if args.version != None:
  87. desc['version'] = str(args.version)
  88. if args.summary != None:
  89. desc['summary'] = str(args.summary)
  90. if args.description != None:
  91. desc['description'] = str(args.description)
  92. if args.git_identity != None:
  93. cmd = " ".join(["git", "--git-dir", args.git_identity + "/.git", "describe", "--always", "--dirty"])
  94. p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout
  95. desc['git_identity'] = str(p.read().strip())
  96. p.close()
  97. if args.image != None:
  98. f = open(args.image, "rb")
  99. bytes = f.read()
  100. desc['image_size'] = len(bytes)
  101. desc['image'] = base64.b64encode(zlib.compress(bytes,9)).decode('utf-8')
  102. print(json.dumps(desc, indent=4))