|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2024 Nordic Semiconductor ASA |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + |
| 6 | +from pathlib import Path |
| 7 | +import re |
| 8 | +import sys |
| 9 | +import subprocess |
| 10 | +from cryptography.hazmat.primitives.serialization import load_pem_private_key |
| 11 | +from west.commands import WestCommand |
| 12 | + |
| 13 | +nrf54l15_key_slots = [226, 228, 230] |
| 14 | + |
| 15 | + |
| 16 | +class NcsProvision(WestCommand): |
| 17 | + def __init__(self): |
| 18 | + super().__init__( |
| 19 | + "ncs-provision", |
| 20 | + "NCS provision", |
| 21 | + "NCS provision utility tool.", |
| 22 | + ) |
| 23 | + |
| 24 | + def do_add_parser(self, parser_adder): |
| 25 | + parser = parser_adder.add_parser( |
| 26 | + self.name, help=self.help, description=self.description |
| 27 | + ) |
| 28 | + |
| 29 | + subparsers = parser.add_subparsers( |
| 30 | + dest="command" |
| 31 | + ) |
| 32 | + upload_parser = subparsers.add_parser("upload", help="Send to KMU") |
| 33 | + upload_parser.add_argument( |
| 34 | + "-k", "--key", type=Path, action='append', dest="keys", |
| 35 | + help="Input .pem file with ED25519 private key" |
| 36 | + ) |
| 37 | + upload_parser.add_argument("-s", "--soc", type=str, help="SoC", |
| 38 | + choices=["nrf54l15"], required=True) |
| 39 | + |
| 40 | + return parser |
| 41 | + |
| 42 | + def do_run(self, args, unknown_args): |
| 43 | + if args.command == "upload": |
| 44 | + if args.soc == "nrf54l15": |
| 45 | + if len(args.keys) > len(nrf54l15_key_slots): |
| 46 | + sys.exit( |
| 47 | + "Error: requested upload of more keys than there are designated slots.") |
| 48 | + slot = 0 |
| 49 | + for keyfile in args.keys: |
| 50 | + with open(keyfile, 'rb') as f: |
| 51 | + priv_key = load_pem_private_key(f.read(), password=None) |
| 52 | + pub_key = priv_key.public_key() |
| 53 | + nrfprovision = subprocess.run( |
| 54 | + ["nrfprovision", |
| 55 | + "provision", |
| 56 | + "-r", |
| 57 | + "REVOKED", |
| 58 | + "-v", |
| 59 | + pub_key.public_bytes_raw().hex(), |
| 60 | + "-m", |
| 61 | + "0x10ba0030", |
| 62 | + "-i", |
| 63 | + str(nrf54l15_key_slots[slot]), |
| 64 | + "-a", |
| 65 | + "ED25519", |
| 66 | + "-d", |
| 67 | + "0x20000000", |
| 68 | + "--verify"], |
| 69 | + stderr=subprocess.PIPE, |
| 70 | + text=True) |
| 71 | + stderr = nrfprovision.stderr |
| 72 | + print(stderr, file=sys.stderr) |
| 73 | + if re.search('fail', stderr) or nrfprovision.returncode: |
| 74 | + sys.exit("Uploading failed!") |
| 75 | + slot += 1 |
0 commit comments