Skip to content

Commit f0ba5f5

Browse files
committed
scripts: west_commands: introduce ncs-bicr
Add a new command to assist with BICR generation. Signed-off-by: Gerard Marull-Paretas <[email protected]>
1 parent 41ee749 commit f0ba5f5

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@
657657
/scripts/tools-versions-*.txt @nrfconnect/ncs-co-build-system @nrfconnect/ncs-ci
658658
/scripts/requirements-*.txt @nrfconnect/ncs-co-build-system @nrfconnect/ncs-ci
659659
/scripts/west_commands/create_board/ @gmarull
660+
/scripts/west_commands/ncs-bicr.py @gmarull
660661
/scripts/west_commands/sbom/ @nrfconnect/ncs-si-muffin
661662
/scripts/west_commands/thingy91x_dfu.py @nrfconnect/ncs-cia
662663
/scripts/west_commands/ncs-provision.py @nrfconnect/ncs-pluto

scripts/west-commands.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,8 @@ west-commands:
3939
- name: ncs-provision
4040
class: NcsProvision
4141
help: Provision utility
42+
- file: scripts/west_commands/ncs-bicr.py
43+
commands:
44+
- name: ncs-bicr
45+
class: NcsBICR
46+
help: Assist with board BICR creation for any applicable Nordic SoC

scripts/west_commands/ncs-bicr.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)