|
| 1 | +# Copyright (c) 2024 Nordic Semiconductor ASA |
| 2 | +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 3 | + |
| 4 | +import json |
| 5 | +import os |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import jsonschema |
| 9 | +from west.commands import WestCommand |
| 10 | + |
| 11 | + |
| 12 | +SCRIPT_DIR = Path(__file__).absolute().parent |
| 13 | +SCHEMA = SCRIPT_DIR.parents[2] / "zephyr" / "soc" / "nordic" / "nrf54h" / "bicr" / "bicr-schema.json" |
| 14 | + |
| 15 | + |
| 16 | +class NcsBICR(WestCommand): |
| 17 | + def __init__(self): |
| 18 | + super().__init__( |
| 19 | + "ncs-bicr", |
| 20 | + "Assist with board BICR creation for any applicable Nordic SoC", |
| 21 | + "" |
| 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 | + parser.add_argument( |
| 30 | + "-d", "--board-dir", type=Path, help="Target board directory" |
| 31 | + ) |
| 32 | + |
| 33 | + group = parser.add_mutually_exclusive_group(required=True) |
| 34 | + group.add_argument( |
| 35 | + "-s", "--json-schema", action="store_true", help="Provide JSON schema" |
| 36 | + ) |
| 37 | + group.add_argument( |
| 38 | + "-r", "--json-schema-response", type=str, help="JSON schema response" |
| 39 | + ) |
| 40 | + |
| 41 | + return parser |
| 42 | + |
| 43 | + def do_run(self, args, unknown_args): |
| 44 | + board_dir = args.board_dir |
| 45 | + if not board_dir: |
| 46 | + board_dir = Path(os.getcwd()) |
| 47 | + |
| 48 | + board_spec = board_dir / "board.yml" |
| 49 | + if not board_spec.exists(): |
| 50 | + raise FileNotFoundError("Invalid board directory") |
| 51 | + |
| 52 | + with open(SCHEMA, "r") as f: |
| 53 | + schema = json.loads(f.read()) |
| 54 | + |
| 55 | + bicr_file = board_dir / "bicr.json" |
| 56 | + |
| 57 | + bicr = None |
| 58 | + if args.json_schema and bicr_file.exists(): |
| 59 | + with open(bicr_file, "r") as f: |
| 60 | + bicr = json.loads(f.read()) |
| 61 | + elif args.json_schema_response: |
| 62 | + bicr = json.loads(args.json_schema_response) |
| 63 | + |
| 64 | + if bicr: |
| 65 | + try: |
| 66 | + jsonschema.validate(bicr, schema) |
| 67 | + except jsonschema.ValidationError as e: |
| 68 | + raise Exception("Invalid BICR") from e |
| 69 | + |
| 70 | + if args.json_schema: |
| 71 | + schema = { |
| 72 | + "schema": schema, |
| 73 | + "state": bicr |
| 74 | + } |
| 75 | + |
| 76 | + print(json.dumps(schema)) |
| 77 | + else: |
| 78 | + with open(bicr_file, "w") as f: |
| 79 | + json.dump(bicr, f, indent=4) |
| 80 | + |
| 81 | + print(json.dumps({"commands": []})) |
0 commit comments