dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
px_mkfw.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
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 #
36 # PX4 firmware image generator
37 #
38 # The PX4 firmware file is a JSON-encoded Python object, containing
39 # metadata fields and a zlib-compressed base64-encoded firmware image.
40 #
41 
42 import sys
43 import argparse
44 import json
45 import base64
46 import zlib
47 import time
48 import subprocess
49 
50 #
51 # Construct a basic firmware description
52 #
53 def mkdesc():
54  proto = {}
55  proto['magic'] = "PX4FWv1"
56  proto['board_id'] = 0
57  proto['board_revision'] = 0
58  proto['version'] = ""
59  proto['summary'] = ""
60  proto['description'] = ""
61  proto['git_identity'] = ""
62  proto['build_time'] = 0
63  proto['image'] = bytes()
64  proto['image_size'] = 0
65  return proto
66 
67 # Parse commandline
68 parser = argparse.ArgumentParser(description="Firmware generator for the PX autopilot system.")
69 parser.add_argument("--prototype", action="store", help="read a prototype description from a file")
70 parser.add_argument("--board_id", action="store", help="set the board ID required")
71 parser.add_argument("--board_revision", action="store", help="set the board revision required")
72 parser.add_argument("--version", action="store", help="set a version string")
73 parser.add_argument("--summary", action="store", help="set a brief description")
74 parser.add_argument("--description", action="store", help="set a longer description")
75 parser.add_argument("--git_identity", action="store", help="the working directory to check for git identity")
76 parser.add_argument("--image", action="store", help="the firmware image")
77 args = parser.parse_args()
78 
79 # Fetch the firmware descriptor prototype if specified
80 if args.prototype != None:
81  f = open(args.prototype,"r")
82  desc = json.load(f)
83  f.close()
84 else:
85  desc = mkdesc()
86 
87 desc['build_time'] = int(time.time())
88 
89 if args.board_id != None:
90  desc['board_id'] = int(args.board_id)
91 if args.board_revision != None:
92  desc['board_revision'] = int(args.board_revision)
93 if args.version != None:
94  desc['version'] = str(args.version)
95 if args.summary != None:
96  desc['summary'] = str(args.summary)
97 if args.description != None:
98  desc['description'] = str(args.description)
99 if args.git_identity != None:
100  cmd = " ".join(["git", "--git-dir", args.git_identity + "/.git", "describe", "--always", "--dirty"])
101  p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout
102  desc['git_identity'] = str(p.read().strip())
103  p.close()
104 if args.image != None:
105  f = open(args.image, "rb")
106  bytes = f.read()
107  desc['image_size'] = len(bytes)
108  desc['image'] = base64.b64encode(zlib.compress(bytes,9)).decode('utf-8')
109 
110 print(json.dumps(desc, indent=4))
static int print(char **out, const char *format, va_list args)
def mkdesc
Definition: px_mkfw.py:53
tuple bytes
Definition: px_mkfw.py:106