2018-06-22 23:02:05 -03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
script to build all of our bootloaders using AP_Bootloader and put the resulting binaries in Tools/bootloaders
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2018-07-04 08:21:09 -03:00
|
|
|
import fnmatch
|
2023-09-01 20:14:50 -03:00
|
|
|
import re
|
2018-07-04 08:21:09 -03:00
|
|
|
|
2022-08-29 01:05:47 -03:00
|
|
|
# get command line arguments
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
parser = ArgumentParser(description='make_secure_bl')
|
|
|
|
parser.add_argument("--signing-key", type=str, default=None, help="signing key for secure bootloader")
|
2022-09-01 23:26:16 -03:00
|
|
|
parser.add_argument("--debug", action='store_true', default=False, help="build with debug symbols")
|
2023-09-01 20:14:50 -03:00
|
|
|
parser.add_argument("--periph-only", action='store_true', default=False, help="only build AP_Periph boards")
|
2022-08-29 01:05:47 -03:00
|
|
|
parser.add_argument("pattern", type=str, default='*', help="board wildcard pattern")
|
|
|
|
args = parser.parse_args()
|
2018-07-04 08:21:09 -03:00
|
|
|
|
2022-08-29 01:05:47 -03:00
|
|
|
if args.signing_key is not None and os.path.basename(args.signing_key).lower().find("private") != -1:
|
|
|
|
# prevent the easy mistake of using private key
|
|
|
|
print("You must use the public key in the bootloader")
|
|
|
|
sys.exit(1)
|
2018-06-22 23:02:05 -03:00
|
|
|
|
|
|
|
os.environ['PYTHONUNBUFFERED'] = '1'
|
|
|
|
|
2020-03-10 18:29:24 -03:00
|
|
|
failed_boards = set()
|
|
|
|
|
2023-09-01 20:14:50 -03:00
|
|
|
def read_hwdef(filepath):
|
|
|
|
'''read a hwdef file recursively'''
|
|
|
|
fh = open(filepath)
|
|
|
|
ret = []
|
|
|
|
text = fh.readlines()
|
|
|
|
for line in text:
|
|
|
|
m = re.match(r"^\s*include\s+(.+)\s*$", line)
|
|
|
|
if m is not None:
|
|
|
|
ret += read_hwdef(os.path.join(os.path.dirname(filepath), m.group(1)))
|
|
|
|
else:
|
|
|
|
ret += [line]
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def is_ap_periph(hwdef):
|
|
|
|
'''return True if a hwdef is for a AP_Periph board'''
|
|
|
|
lines = read_hwdef(hwdef)
|
|
|
|
for line in lines:
|
|
|
|
if line.find('AP_PERIPH') != -1:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2018-06-22 23:02:05 -03:00
|
|
|
def get_board_list():
|
|
|
|
'''add boards based on existance of hwdef-bl.dat in subdirectories for ChibiOS'''
|
|
|
|
board_list = []
|
|
|
|
dirname, dirlist, filenames = next(os.walk('libraries/AP_HAL_ChibiOS/hwdef'))
|
|
|
|
for d in dirlist:
|
|
|
|
hwdef = os.path.join(dirname, d, 'hwdef-bl.dat')
|
|
|
|
if os.path.exists(hwdef):
|
2023-09-01 20:14:50 -03:00
|
|
|
if args.periph_only and not is_ap_periph(hwdef):
|
|
|
|
continue
|
2018-06-22 23:02:05 -03:00
|
|
|
board_list.append(d)
|
|
|
|
return board_list
|
|
|
|
|
|
|
|
def run_program(cmd_list):
|
|
|
|
print("Running (%s)" % " ".join(cmd_list))
|
|
|
|
retcode = subprocess.call(cmd_list)
|
|
|
|
if retcode != 0:
|
|
|
|
print("Build failed: %s" % ' '.join(cmd_list))
|
2020-03-10 18:29:24 -03:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def build_board(board):
|
2022-08-29 01:05:47 -03:00
|
|
|
configure_args = "--board %s --bootloader --no-submodule-update --Werror" % board
|
|
|
|
configure_args = configure_args.split()
|
|
|
|
if args.signing_key is not None:
|
|
|
|
print("Building secure bootloader")
|
|
|
|
configure_args.append("--signed-fw")
|
2022-10-04 01:36:50 -03:00
|
|
|
if args.debug:
|
2022-09-01 23:26:16 -03:00
|
|
|
print("Building with debug symbols")
|
|
|
|
configure_args.append("--debug")
|
2022-08-29 01:05:47 -03:00
|
|
|
if not run_program(["./waf", "configure"] + configure_args):
|
2020-03-10 18:29:24 -03:00
|
|
|
return False
|
|
|
|
if not run_program(["./waf", "clean"]):
|
|
|
|
return False
|
|
|
|
if not run_program(["./waf", "bootloader"]):
|
|
|
|
return False
|
|
|
|
return True
|
2018-06-22 23:02:05 -03:00
|
|
|
|
|
|
|
for board in get_board_list():
|
2022-08-29 01:05:47 -03:00
|
|
|
if not fnmatch.fnmatch(board, args.pattern):
|
2018-07-04 08:21:09 -03:00
|
|
|
continue
|
2018-06-22 23:02:05 -03:00
|
|
|
print("Building for %s" % board)
|
2020-03-10 18:29:24 -03:00
|
|
|
if not build_board(board):
|
|
|
|
failed_boards.add(board)
|
|
|
|
continue
|
2022-08-29 01:05:47 -03:00
|
|
|
bl_file = 'Tools/bootloaders/%s_bl.bin' % board
|
2022-09-01 23:26:16 -03:00
|
|
|
hex_file = 'Tools/bootloaders/%s_bl.hex' % board
|
|
|
|
elf_file = 'Tools/bootloaders/%s_bl.elf' % board
|
2022-08-29 01:05:47 -03:00
|
|
|
shutil.copy('build/%s/bin/AP_Bootloader.bin' % board, bl_file)
|
2022-09-01 23:26:16 -03:00
|
|
|
print("Created %s" % bl_file)
|
|
|
|
shutil.copy('build/%s/bootloader/AP_Bootloader' % board, elf_file)
|
|
|
|
print("Created %s" % elf_file)
|
2022-08-29 01:05:47 -03:00
|
|
|
if args.signing_key is not None:
|
|
|
|
print("Signing bootloader with %s" % args.signing_key)
|
|
|
|
if not run_program(["./Tools/scripts/signing/make_secure_bl.py", bl_file, args.signing_key]):
|
|
|
|
print("Failed to sign bootloader for %s" % board)
|
|
|
|
sys.exit(1)
|
2022-09-01 23:26:16 -03:00
|
|
|
if not run_program(["./Tools/scripts/signing/make_secure_bl.py", elf_file, args.signing_key]):
|
|
|
|
print("Failed to sign ELF bootloader for %s" % board)
|
|
|
|
sys.exit(1)
|
|
|
|
if not run_program([sys.executable, "Tools/scripts/bin2hex.py", "--offset", "0x08000000", bl_file, hex_file]):
|
2020-03-10 18:29:24 -03:00
|
|
|
failed_boards.add(board)
|
|
|
|
continue
|
2022-09-01 23:26:16 -03:00
|
|
|
print("Created %s" % hex_file)
|
2020-03-10 18:29:24 -03:00
|
|
|
|
|
|
|
if len(failed_boards):
|
|
|
|
print("Failed boards: %s" % list(failed_boards))
|