|
| 1 | +#!/usr/bin/python |
| 2 | +# Copyright (c) 2017-2021 Arm Limited |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +import os |
| 19 | +from os.path import abspath, basename, dirname, splitext, isdir |
| 20 | +from os.path import join as path_join |
| 21 | +import re |
| 22 | +import subprocess |
| 23 | +import argparse |
| 24 | + |
| 25 | +SCRIPT_DIR = dirname(abspath(__file__)) |
| 26 | +MBED_OS_ROOT = abspath(path_join(SCRIPT_DIR, os.pardir, os.pardir, os.pardir, os.pardir, os.pardir, os.pardir)) |
| 27 | + |
| 28 | +def sign_and_merge_tfm_bin(target_name, target_path, non_secure_bin, secure_bin): |
| 29 | + |
| 30 | + assert os.path.isdir(target_path) |
| 31 | + assert os.path.isfile(secure_bin) |
| 32 | + assert os.path.isfile(non_secure_bin) |
| 33 | + |
| 34 | + build_dir = dirname(non_secure_bin) |
| 35 | + tempdir = path_join(build_dir, 'temp') |
| 36 | + if not isdir(tempdir): |
| 37 | + os.makedirs(tempdir) |
| 38 | + flash_layout = path_join(target_path, 'partition', 'flash_layout.h') |
| 39 | + mcuboot_bin = path_join(target_path, 'bl2.bin') |
| 40 | + image_macros_s = path_join(target_path, 'partition', 'signing_layout_s.c') |
| 41 | + image_macros_ns = path_join(target_path, 'partition', 'signing_layout_ns.c') |
| 42 | + s_bin_name, s_bin_ext = splitext(basename(secure_bin)) |
| 43 | + s_signed_bin = abspath(path_join(tempdir, s_bin_name + '_signed' + s_bin_ext)) |
| 44 | + ns_bin_name, ns_bin_ext = splitext(basename(non_secure_bin)) |
| 45 | + ns_signed_bin = abspath(path_join(tempdir, 'tfm_' + ns_bin_name + '_signed' + ns_bin_ext)) |
| 46 | + concatenated_bin = abspath(path_join(tempdir, s_bin_name + '_' + ns_bin_name + '_concat' + ns_bin_ext)) |
| 47 | + |
| 48 | + assert os.path.isfile(image_macros_s) |
| 49 | + assert os.path.isfile(image_macros_ns) |
| 50 | + |
| 51 | + #1. Run wrapper to sign the TF-M secure binary |
| 52 | + cmd = [ |
| 53 | + "python3", |
| 54 | + path_join(MBED_OS_ROOT, "tools", "psa","tfm", "bin_utils","wrapper.py"), |
| 55 | + "-v", |
| 56 | + '1.2.0', |
| 57 | + "-k", |
| 58 | + path_join(target_path, (target_name + '-root-rsa-3072.pem')), |
| 59 | + "--layout", |
| 60 | + image_macros_s, |
| 61 | + "--public-key-format", |
| 62 | + 'full', |
| 63 | + "--align", |
| 64 | + '1', |
| 65 | + "--pad", |
| 66 | + "--pad-header", |
| 67 | + "-H", |
| 68 | + '0x400', |
| 69 | + "--overwrite-only", |
| 70 | + "-s", |
| 71 | + 'auto', |
| 72 | + "-d", |
| 73 | + '(0,0.0.0+0)', |
| 74 | + abspath(secure_bin), |
| 75 | + s_signed_bin, |
| 76 | + ] |
| 77 | + |
| 78 | + retcode = run_cmd(cmd, MBED_OS_ROOT) |
| 79 | + if retcode: |
| 80 | + raise Exception("Unable to sign " + target_name + |
| 81 | + " secure binary, Error code: " + str(retcode)) |
| 82 | + |
| 83 | + #2. Run wrapper to sign the non-secure mbed binary |
| 84 | + cmd = [ |
| 85 | + "python3", |
| 86 | + path_join(MBED_OS_ROOT, "tools", "psa","tfm", "bin_utils","wrapper.py"), |
| 87 | + "-v", |
| 88 | + '1.2.0', |
| 89 | + "-k", |
| 90 | + path_join(target_path, (target_name + '-root-rsa-3072_1.pem')), |
| 91 | + "--layout", |
| 92 | + image_macros_ns, |
| 93 | + "--public-key-format", |
| 94 | + 'full', |
| 95 | + "--align", |
| 96 | + '1', |
| 97 | + "--pad", |
| 98 | + "--pad-header", |
| 99 | + "-H", |
| 100 | + '0x400', |
| 101 | + "--overwrite-only", |
| 102 | + "-s", |
| 103 | + 'auto', |
| 104 | + "-d", |
| 105 | + '(1,0.0.0+0)', |
| 106 | + abspath(non_secure_bin), |
| 107 | + ns_signed_bin, |
| 108 | + ] |
| 109 | + |
| 110 | + retcode = run_cmd(cmd, MBED_OS_ROOT) |
| 111 | + if retcode: |
| 112 | + raise Exception("Unable to sign " + target_name + |
| 113 | + " non-secure binary, Error code: " + str(retcode)) |
| 114 | + |
| 115 | + #3. Concatenate signed secure TFM and non-secure mbed binaries |
| 116 | + cmd = [ |
| 117 | + "python3", |
| 118 | + path_join(MBED_OS_ROOT, "tools", "psa","tfm", "bin_utils","assemble.py"), |
| 119 | + "--layout", |
| 120 | + image_macros_s, |
| 121 | + "-s", |
| 122 | + s_signed_bin, |
| 123 | + "-n", |
| 124 | + ns_signed_bin, |
| 125 | + "-o", |
| 126 | + concatenated_bin, |
| 127 | + ] |
| 128 | + |
| 129 | + retcode = run_cmd(cmd, MBED_OS_ROOT) |
| 130 | + if retcode: |
| 131 | + raise Exception("Unable to concatenate " + target_name + |
| 132 | + " binaries, Error code: " + str(retcode)) |
| 133 | + |
| 134 | + #4. Concatenate mcuboot and signed binary and overwrite mbed built binary file |
| 135 | + mcuboot_image_size = find_bl2_size(flash_layout) |
| 136 | + with open(mcuboot_bin, "rb") as mcuboot_fh, open(concatenated_bin, "rb") as concat_fh: |
| 137 | + with open(non_secure_bin, "w+b") as out_fh: |
| 138 | + out_fh.write(mcuboot_fh.read()) |
| 139 | + out_fh.seek(mcuboot_image_size) |
| 140 | + out_fh.write(concat_fh.read()) |
| 141 | + |
| 142 | + |
| 143 | +def find_bl2_size(configFile): |
| 144 | + bl2_size_re = re.compile(r"^#define\s+FLASH_AREA_BL2_SIZE\s+\({0,1}(0x[0-9a-fA-F]+)\){0,1}") |
| 145 | + bl2_size = None |
| 146 | + with open(configFile, 'r') as flash_layout_file: |
| 147 | + for line in flash_layout_file: |
| 148 | + m = bl2_size_re.match(line) |
| 149 | + if m is not None: |
| 150 | + bl2_size = int(m.group(1), 0) |
| 151 | + break |
| 152 | + return bl2_size |
| 153 | + |
| 154 | +def run_cmd(cmd, directory): |
| 155 | + |
| 156 | + popen_instance = subprocess.Popen( |
| 157 | + cmd, |
| 158 | + stdout=subprocess.PIPE, |
| 159 | + stderr=subprocess.STDOUT, |
| 160 | + cwd=directory, |
| 161 | + ) |
| 162 | + |
| 163 | + popen_instance.communicate() |
| 164 | + return popen_instance.returncode |
| 165 | + |
| 166 | +def parse_args(): |
| 167 | + parser = argparse.ArgumentParser() |
| 168 | + |
| 169 | + parser.add_argument( |
| 170 | + "--tfm-target", |
| 171 | + help="Name of the TF-M target", |
| 172 | + required=True |
| 173 | + ) |
| 174 | + |
| 175 | + parser.add_argument( |
| 176 | + "--target-path", |
| 177 | + help="Path containing the target's bootloader, layouts and signing keys", |
| 178 | + required=True |
| 179 | + ) |
| 180 | + |
| 181 | + parser.add_argument( |
| 182 | + "--non-secure-bin", |
| 183 | + help="Path to the non-secure binary", |
| 184 | + required=True |
| 185 | + ) |
| 186 | + |
| 187 | + parser.add_argument( |
| 188 | + "--secure-bin", |
| 189 | + help="Path to the secure binary", |
| 190 | + required=True |
| 191 | + ) |
| 192 | + |
| 193 | + return parser.parse_args() |
| 194 | + |
| 195 | +if __name__ == "__main__": |
| 196 | + args = parse_args() |
| 197 | + sign_and_merge_tfm_bin(args.tfm_target, args.target_path, args.non_secure_bin, args.secure_bin) |
0 commit comments