Skip to content

Commit 9626c27

Browse files
joshannepeterbarker
authored andcommitted
Tools: Improvements to building of signed bootloaders
Adds ability to pass --omit-ardupilot-keys to build_bootloaders.py Adds ability to pass multiple public keys to the signing of the bootloader. This extends the functionality of the single key that was previously possible. All keys are prefixed with --signing-key and are appended to the args.signing_key array. All keys are checked for presense, and type before being used to sign the bootloader. General tidy up of the argument parser, prints a proper description of the role of the file.
1 parent 3faf677 commit 9626c27

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

Tools/scripts/build_bootloaders.py

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,14 @@
1515

1616
# get command line arguments
1717
from argparse import ArgumentParser
18-
parser = ArgumentParser(description='make_secure_bl')
19-
parser.add_argument("--signing-key", type=str, default=None, help="signing key for secure bootloader")
18+
parser = ArgumentParser(description='This Program is used to build ArduPilot bootloaders for boards.')
19+
parser.add_argument("--signing-key", type=str, action='append', help="signing key for secure bootloader (can be used multiple times)")
20+
parser.add_argument("--omit-ardupilot-keys", action='store_true', default=False, help="omit ArduPilot signing keys")
2021
parser.add_argument("--debug", action='store_true', default=False, help="build with debug symbols")
2122
parser.add_argument("--periph-only", action='store_true', default=False, help="only build AP_Periph boards")
2223
parser.add_argument("pattern", type=str, default='*', help="board wildcard pattern", nargs='?')
2324
args = parser.parse_args()
2425

25-
if args.signing_key is not None and os.path.basename(args.signing_key).lower().find("private") != -1:
26-
# prevent the easy mistake of using private key
27-
print("You must use the public key in the bootloader")
28-
sys.exit(1)
29-
3026
os.environ['PYTHONUNBUFFERED'] = '1'
3127

3228
failed_boards = set()
@@ -70,6 +66,32 @@ def get_board_list():
7066
board_list.append(d)
7167
return board_list
7268

69+
def validate_signing_keys(keys):
70+
"""Validate that all signing key files exist and are not private keys"""
71+
missing_keys = []
72+
private_keys = []
73+
74+
for key in keys:
75+
if not os.path.isfile(key):
76+
missing_keys.append(key)
77+
elif os.path.basename(key).lower().find("private") != -1:
78+
private_keys.append(key)
79+
80+
if missing_keys:
81+
print("Error: The following files were not found:")
82+
for key in missing_keys:
83+
print(f" {key}")
84+
sys.exit(1)
85+
86+
if private_keys:
87+
print("Error: You must use the public key in the bootloader. Check the following files:")
88+
for key in private_keys:
89+
print(f" {key}")
90+
sys.exit(1)
91+
92+
if args.signing_key is not None:
93+
validate_signing_keys(args.signing_key)
94+
7395
def run_program(cmd_list):
7496
print("Running (%s)" % " ".join(cmd_list))
7597
retcode = subprocess.call(cmd_list)
@@ -122,6 +144,10 @@ def get_all_board_dirs():
122144
if b not in board_list:
123145
print(f"Skipping {b}: no hwdef-bl.dat (no bootloader for this board)")
124146

147+
additional_args = []
148+
if args.omit_ardupilot_keys:
149+
# If the user has requested to omit ardupilot keys, ensure it is forwarded to the make_secure_bl program
150+
additional_args.append("--omit-ardupilot-keys")
125151

126152
# check that the user-supplied board pattern matches something; if not, warn and exit
127153
for board in board_list:
@@ -142,11 +168,11 @@ def get_all_board_dirs():
142168
shutil.copy('build/%s/bootloader/AP_Bootloader' % board, elf_file)
143169
print("Created %s" % elf_file)
144170
if args.signing_key is not None:
145-
print("Signing bootloader with %s" % args.signing_key)
146-
if not run_program(["./Tools/scripts/signing/make_secure_bl.py", bl_file, args.signing_key]):
171+
print("Signing bootloader with %s" % ", ".join(args.signing_key))
172+
if not run_program(["./Tools/scripts/signing/make_secure_bl.py", *additional_args, bl_file] + args.signing_key):
147173
print("Failed to sign bootloader for %s" % board)
148174
sys.exit(1)
149-
if not run_program(["./Tools/scripts/signing/make_secure_bl.py", elf_file, args.signing_key]):
175+
if not run_program(["./Tools/scripts/signing/make_secure_bl.py", *additional_args, elf_file] + args.signing_key):
150176
print("Failed to sign ELF bootloader for %s" % board)
151177
sys.exit(1)
152178
if not run_program([sys.executable, "Tools/scripts/bin2hex.py", "--offset", "0x08000000", bl_file, hex_file]):

0 commit comments

Comments
 (0)