Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/scripts/build_bootloaders.py
Views: 1798
#!/usr/bin/env python12"""3script to build all of our bootloaders using AP_Bootloader and put the resulting binaries in Tools/bootloaders4"""56import os7import shutil8import subprocess9import sys10import fnmatch11import re1213# get command line arguments14from argparse import ArgumentParser15parser = ArgumentParser(description='make_secure_bl')16parser.add_argument("--signing-key", type=str, default=None, help="signing key for secure bootloader")17parser.add_argument("--debug", action='store_true', default=False, help="build with debug symbols")18parser.add_argument("--periph-only", action='store_true', default=False, help="only build AP_Periph boards")19parser.add_argument("pattern", type=str, default='*', help="board wildcard pattern")20args = parser.parse_args()2122if args.signing_key is not None and os.path.basename(args.signing_key).lower().find("private") != -1:23# prevent the easy mistake of using private key24print("You must use the public key in the bootloader")25sys.exit(1)2627os.environ['PYTHONUNBUFFERED'] = '1'2829failed_boards = set()3031def read_hwdef(filepath):32'''read a hwdef file recursively'''33fh = open(filepath)34ret = []35text = fh.readlines()36for line in text:37m = re.match(r"^\s*include\s+(.+)\s*$", line)38if m is not None:39ret += read_hwdef(os.path.join(os.path.dirname(filepath), m.group(1)))40else:41ret += [line]42return ret4344def is_ap_periph(hwdef):45'''return True if a hwdef is for a AP_Periph board'''46lines = read_hwdef(hwdef)47for line in lines:48if line.find('AP_PERIPH') != -1:49return True50return False5152def get_board_list():53'''add boards based on existance of hwdef-bl.dat in subdirectories for ChibiOS'''54board_list = []55dirname, dirlist, filenames = next(os.walk('libraries/AP_HAL_ChibiOS/hwdef'))56for d in dirlist:57hwdef = os.path.join(dirname, d, 'hwdef-bl.dat')58if os.path.exists(hwdef):59if args.periph_only and not is_ap_periph(hwdef):60continue61board_list.append(d)62return board_list6364def run_program(cmd_list):65print("Running (%s)" % " ".join(cmd_list))66retcode = subprocess.call(cmd_list)67if retcode != 0:68print("Build failed: %s" % ' '.join(cmd_list))69return False70return True7172def build_board(board):73configure_args = "--board %s --bootloader --no-submodule-update --Werror" % board74configure_args = configure_args.split()75if args.signing_key is not None:76print("Building secure bootloader")77configure_args.append("--signed-fw")78if args.debug:79print("Building with debug symbols")80configure_args.append("--debug")81if not run_program(["./waf", "configure"] + configure_args):82return False83if not run_program(["./waf", "clean"]):84return False85if not run_program(["./waf", "bootloader"]):86return False87return True8889for board in get_board_list():90if not fnmatch.fnmatch(board, args.pattern):91continue92print("Building for %s" % board)93if not build_board(board):94failed_boards.add(board)95continue96bl_file = 'Tools/bootloaders/%s_bl.bin' % board97hex_file = 'Tools/bootloaders/%s_bl.hex' % board98elf_file = 'Tools/bootloaders/%s_bl.elf' % board99shutil.copy('build/%s/bin/AP_Bootloader.bin' % board, bl_file)100print("Created %s" % bl_file)101shutil.copy('build/%s/bootloader/AP_Bootloader' % board, elf_file)102print("Created %s" % elf_file)103if args.signing_key is not None:104print("Signing bootloader with %s" % args.signing_key)105if not run_program(["./Tools/scripts/signing/make_secure_bl.py", bl_file, args.signing_key]):106print("Failed to sign bootloader for %s" % board)107sys.exit(1)108if not run_program(["./Tools/scripts/signing/make_secure_bl.py", elf_file, args.signing_key]):109print("Failed to sign ELF bootloader for %s" % board)110sys.exit(1)111if not run_program([sys.executable, "Tools/scripts/bin2hex.py", "--offset", "0x08000000", bl_file, hex_file]):112failed_boards.add(board)113continue114print("Created %s" % hex_file)115116if len(failed_boards):117print("Failed boards: %s" % list(failed_boards))118119120