Skip to content

Commit dd926d2

Browse files
[scripts] Fixed nrfconnect factory generation scripts (project-chip#32892)
The factory_data.hex file is not generated as an intermediate product of factory generation process. In result, factory generation works only if merging with firmware is used or scripts are manually invoked. Added optional --size and --offset arguments to generate_nrfconnect_chip_factory_data.py script that results in calling nrfconnect_generate_partition.py internally. It solves an issue and additionally simplifies manual generation process (if selected).
1 parent 0515c61 commit dd926d2

File tree

3 files changed

+106
-89
lines changed

3 files changed

+106
-89
lines changed

config/nrfconnect/chip-module/generate_factory_data.cmake

+22-61
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,29 @@
1515
#
1616

1717

18-
# Create a JSON file based on factory data given via kConfigs.
18+
# Create a .hex file in CBOR format based on factory data given via kConfigs.
1919
#
2020
# This function creates a list of arguments for external script and then run it to write a JSON file.
2121
# Created JSON file can be checked using JSON SCHEMA file if it is provided.
22+
# Next, the resulting .hex file is generated based on previously created JSON file.
2223
#
2324
# This script can be manipulated using following kConfigs:
2425
# - To merge generated factory data with final zephyr.hex file set kConfig CONFIG_CHIP_FACTORY_DATA_MERGE_WITH_FIRMWARE=y
2526
# - To use default certification paths set CONFIG_CHIP_FACTORY_DATA_USE_DEFAULTS_CERTS_PATH=y
2627
#
27-
# During generation process a some file will be created in zephyr's build directory:
28+
# During generation process the following files will be created in zephyr's build directory:
2829
# - <factory_data_target>.json a file containing all factory data written in JSON format.
30+
# - <factory_data_target>.hex a file containing all factory data in CBOR format.
31+
# - <factory_data_target>.bin a binary file containing all raw factory data in CBOR format.
32+
# - <factory_data_target>.cbor a file containing all factory data in CBOR format.
2933
#
3034
# [Args]:
3135
# factory_data_target - a name for target to generate factory_data.
3236
# script_path - a path to script that makes a JSON factory data file from given arguments.
3337
# schema_path - a path to JSON schema file which can be used to verify generated factory data JSON file.
3438
# This argument is optional, if you don't want to verify the JSON file put it empty "".
35-
# output_path - a path to output directory, where created JSON file will be stored.
36-
function(nrfconnect_create_factory_data_json factory_data_target script_path schema_path output_path)
39+
# output_path - a path to output directory, where created hex and JSON files will be stored.
40+
function(nrfconnect_create_factory_data factory_data_target script_path schema_path output_path)
3741

3842
# set script args for future purpose
3943
set(script_args)
@@ -120,62 +124,25 @@ if(CONFIG_CHIP_DEVICE_ENABLE_KEY)
120124
string(APPEND script_args "--enable_key \"${CONFIG_CHIP_DEVICE_ENABLE_KEY}\"\n")
121125
endif()
122126

123-
# Set output JSON file and path to SCHEMA file to validate generated factory data
124-
set(factory_data_json ${output_path}/${factory_data_target}.json)
125-
string(APPEND script_args "-o \"${factory_data_json}\"\n")
127+
# Set output path and path to SCHEMA file to validate generated factory data
128+
set(factory_data_output_path ${output_path}/${factory_data_target})
129+
string(APPEND script_args "-o \"${factory_data_output_path}\"\n")
126130
string(APPEND script_args "-s \"${schema_path}\"\n")
127131

132+
# Add optional offset and size arguments to generate both .hex and .json files.
133+
string(APPEND script_args "--offset $<TARGET_PROPERTY:partition_manager,PM_FACTORY_DATA_ADDRESS>\n")
134+
string(APPEND script_args "--size $<TARGET_PROPERTY:partition_manager,PM_FACTORY_DATA_OFFSET>\n")
135+
128136
# execute first script to create a JSON file
129137
separate_arguments(separated_script_args NATIVE_COMMAND ${script_args})
130138
add_custom_command(
131-
OUTPUT ${factory_data_json}
139+
OUTPUT ${factory_data_output_path}.hex
132140
DEPENDS ${FACTORY_DATA_SCRIPT_PATH}
133141
COMMAND ${Python3_EXECUTABLE} ${FACTORY_DATA_SCRIPT_PATH} ${separated_script_args}
134142
COMMENT "Generating new Factory Data..."
135143
)
136144
add_custom_target(${factory_data_target} ALL
137-
DEPENDS ${factory_data_json}
138-
)
139-
140-
endfunction()
141-
142-
143-
# Create a .hex file with factory data in CBOR format.
144-
#
145-
# This function creates a .hex and .cbor files from given JSON factory data file.
146-
#
147-
#
148-
# During generation process some files will be created in zephyr's build directory:
149-
# - <factory_data_target>.hex a file containing all factory data in CBOR format.
150-
# - <factory_data_target>.bin a binary file containing all raw factory data in CBOR format.
151-
# - <factory_data_target>.cbor a file containing all factory data in CBOR format.
152-
#
153-
# [Args]:
154-
# factory_data_hex_target - a name for target to generate factory data HEX file.
155-
# factory_data_target - a name for target to generate factory data JSON file.
156-
# script_path - a path to script that makes a factory data .hex file from given arguments.
157-
# output_path - a path to output directory, where created JSON file will be stored.
158-
function(nrfconnect_create_factory_data_hex_file factory_data_hex_target factory_data_target script_path output_path)
159-
160-
# Pass the argument list via file
161-
set(cbor_script_args "-i ${output_path}/${factory_data_target}.json\n")
162-
string(APPEND cbor_script_args "-o ${output_path}/${factory_data_target}\n")
163-
# get partition address and offset from partition manager during compilation
164-
string(APPEND cbor_script_args "--offset $<TARGET_PROPERTY:partition_manager,PM_FACTORY_DATA_ADDRESS>\n")
165-
string(APPEND cbor_script_args "--size $<TARGET_PROPERTY:partition_manager,PM_FACTORY_DATA_OFFSET>\n")
166-
string(APPEND cbor_script_args "-r\n")
167-
168-
# execute second script to create a hex file containing factory data in cbor format
169-
separate_arguments(separated_cbor_script_args NATIVE_COMMAND ${cbor_script_args})
170-
set(factory_data_hex ${output_path}/${factory_data_target}.hex)
171-
172-
add_custom_command(OUTPUT ${factory_data_hex}
173-
COMMAND ${Python3_EXECUTABLE} ${script_path} ${separated_cbor_script_args}
174-
COMMENT "Generating factory data HEX file..."
175-
DEPENDS ${factory_data_target} ${script_path}
176-
)
177-
add_custom_target(${factory_data_hex_target}
178-
DEPENDS ${factory_data_hex}
145+
DEPENDS ${factory_data_output_path}.hex
179146
)
180147

181148
endfunction()
@@ -202,22 +169,16 @@ set(GENERATE_CBOR_SCRIPT_PATH ${CHIP_ROOT}/scripts/tools/nrfconnect/nrfconnect_g
202169
set(FACTORY_DATA_SCHEMA_PATH ${CHIP_ROOT}/scripts/tools/nrfconnect/nrfconnect_factory_data.schema)
203170
set(OUTPUT_FILE_PATH ${APPLICATION_BINARY_DIR}/zephyr)
204171

205-
# create a JSON file with all factory data
206-
nrfconnect_create_factory_data_json(factory_data
207-
${FACTORY_DATA_SCRIPT_PATH}
208-
${FACTORY_DATA_SCHEMA_PATH}
209-
${OUTPUT_FILE_PATH})
210-
211172
# create a .hex file with factory data in CBOR format based on the JSON file created previously
212-
nrfconnect_create_factory_data_hex_file(factory_data_hex
213-
factory_data
214-
${GENERATE_CBOR_SCRIPT_PATH}
215-
${OUTPUT_FILE_PATH})
173+
nrfconnect_create_factory_data(factory_data
174+
${FACTORY_DATA_SCRIPT_PATH}
175+
${FACTORY_DATA_SCHEMA_PATH}
176+
${OUTPUT_FILE_PATH})
216177

217178
if(CONFIG_CHIP_FACTORY_DATA_MERGE_WITH_FIRMWARE)
218179
# set custom target for merging factory_data hex file
219180
set_property(GLOBAL PROPERTY factory_data_PM_HEX_FILE ${OUTPUT_FILE_PATH}/factory_data.hex)
220-
set_property(GLOBAL PROPERTY factory_data_PM_TARGET factory_data_hex)
181+
set_property(GLOBAL PROPERTY factory_data_PM_TARGET factory_data)
221182
endif()
222183

223184

docs/guides/nrfconnect_factory_data_configuration.md

+58-19
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ data secure by applying hardware write protection.
3737
- [Appearance field description](#appearance-field-description)
3838
- [Enabling factory data support](#enabling-factory-data-support)
3939
- [Generating factory data](#generating-factory-data)
40-
- [Creating the factory data JSON file with the first script](#creating-the-factory-data-json-file-with-the-first-script)
40+
- [Creating the factory data JSON and HEX files with the first script](#creating-the-factory-data-json-and-hex-files-with-the-first-script)
4141
- [How to set user data](#how-to-set-user-data)
4242
- [How to handle user data](#how-to-handle-user-data)
4343
- [Verifying using the JSON Schema tool](#verifying-using-the-json-schema-tool)
@@ -218,14 +218,19 @@ file written in another way. To make sure that the JSON file is correct and the
218218
device is able to read out parameters,
219219
[verify the file using the JSON schema tool](#verifying-using-the-json-schema-tool).
220220

221-
### Creating the factory data JSON file with the first script
221+
You can also use only the first script to generate both JSON and HEX files, by
222+
providing optional `offset` and `size` arguments, which results in invoking the
223+
script internally. Such option is the recommended one, but invoking two scripts
224+
one by one is also supported to provide backward compatibility.
225+
226+
### Creating the factory data JSON and HEX files with the first script
222227

223228
A Matter device needs a proper factory data partition stored in the flash memory
224229
to read out all required parameters during startup. To simplify the factory data
225230
generation, you can use the
226231
[generate_nrfconnect_chip_factory_data.py](../../scripts/tools/nrfconnect/generate_nrfconnect_chip_factory_data.py)
227232
Python script to provide all required parameters and generate a human-readable
228-
JSON file.
233+
JSON file and save it to a HEX file.
229234

230235
To use this script, complete the following steps:
231236

@@ -245,10 +250,10 @@ To use this script, complete the following steps:
245250
--sn --vendor_id, --product_id, --vendor_name, --product_name, --date, --hw_ver, --hw_ver_str, --spake2_it, --spake2_salt, --discriminator
246251
```
247252
248-
b. Add output file path:
253+
b. Add output path to store .json file, e.g. my_dir/output:
249254
250255
```
251-
-o <path_to_output_json_file>
256+
-o <path_to_output_file>
252257
```
253258
254259
c. Generate SPAKE2 verifier using one of the following methods:
@@ -341,6 +346,34 @@ To use this script, complete the following steps:
341346
> `chip-cert` executable. See the note at the end of this section to learn
342347
> how to get it.
343348
349+
k. (optional) Partition offset that is an address in device's NVM memory,
350+
where factory data will be stored.
351+
352+
```
353+
--offset <offset>
354+
```
355+
356+
> **Note:** To generate a HEX file with factory data, you need to provide
357+
> both `offset` and `size` optional arguments. As a result,
358+
> `factory_data.hex` and `factory_data.bin` files are created in the
359+
> `output` directory. The first file contains the required memory offset.
360+
> For this reason, it can be programmed directly to the device using a
361+
> programmer (for example, `nrfjprog`).
362+
363+
l. (optional) The maximum partition size in device's NVM memory, where
364+
factory data will be stored.
365+
366+
```
367+
--size <size>
368+
```
369+
370+
> **Note:** To generate a HEX file with factory data, you need to provide
371+
> both `offset` and `size` optional arguments. As a result,
372+
> `factory_data.hex` and `factory_data.bin` files are created in the
373+
> `output` directory. The first file contains the required memory offset.
374+
> For this reason, it can be programmed directly to the device using a
375+
> programmer (for example, `nrfjprog`).
376+
344377
4. Run the script using the prepared list of arguments:
345378
346379
```
@@ -370,8 +403,10 @@ $ python scripts/tools/nrfconnect/generate_nrfconnect_chip_factory_data.py \
370403
--passcode 20202021 \
371404
--product_finish "matte" \
372405
--product_color "black" \
373-
--out "build.json" \
374-
--schema "scripts/tools/nrfconnect/nrfconnect_factory_data.schema"
406+
--out "build" \
407+
--schema "scripts/tools/nrfconnect/nrfconnect_factory_data.schema" \
408+
--offset 0xf7000 \
409+
--size 0x1000
375410
```
376411
377412
As the result of the above example, a unique ID for the rotating device ID is
@@ -570,7 +605,7 @@ To generate a manual pairing code and a QR code, complete the following steps:
570605
```
571606
572607
2. Complete steps 1, 2, and 3 from the
573-
[Creating the factory data JSON file with the first script](#creating-the-factory-data-json-file-with-the-first-script)
608+
[Creating the factory data JSON and HEX files with the first script](#creating-the-factory-data-json-and-hex-files-with-the-first-script)
574609
section to prepare the final invocation of the Python script.
575610
576611
3. Add the `--generate_onboarding` argument to the Python script final
@@ -686,10 +721,15 @@ The output will look similar to the following one:
686721
### Creating a factory data partition with the second script
687722
688723
To store the factory data set in the device's persistent storage, convert the
689-
data from the JSON file to its binary representation in the CBOR format. To do
690-
this, use the
724+
data from the JSON file to its binary representation in the CBOR format. This is
725+
done by the
726+
[generate_nrfconnect_chip_factory_data.py](../../scripts/tools/nrfconnect/generate_nrfconnect_chip_factory_data.py),
727+
if you provide optional `offset` and `size` arguments. If you provided these
728+
arguments, skip the following steps of this section.
729+
730+
You can skip these optional arguments and do this, using the
691731
[nrfconnect_generate_partition.py](../../scripts/tools/nrfconnect/nrfconnect_generate_partition.py)
692-
to generate the factory data partition:
732+
script, but this is obsolete solution kept only for backward compatibility:
693733
694734
1. Navigate to the _connectedhomeip_ root directory
695735
2. Run the following command pattern:
@@ -924,14 +964,13 @@ $ west flash
924964
## Using own factory data implementation
925965
926966
The [factory data generation process](#generating-factory-data) described above
927-
is only an example valid for the nRF Connect platform. You can also create a HEX
928-
file containing all components from the
929-
[factory data component table](#factory-data-component-table) in any format and
930-
then implement a parser to read out all parameters and pass them to a provider.
931-
Each manufacturer can implement a factory data set on its own by implementing a
932-
parser and a factory data accessor inside the Matter stack. Use the
933-
[nRF Connect Provider](../../src/platform/nrfconnect/FactoryDataProvider.h) and
934-
[FactoryDataParser](../../src/platform/nrfconnect/FactoryDataParser.h) as
967+
is only an example valid for the nRF Connect platform. You can well create a HEX
968+
file containing all [factory data components](#factory-data-component-table) in
969+
any format and then implement a parser to read out all parameters and pass them
970+
to a provider. Each manufacturer can implement a factory data set on its own by
971+
implementing a parser and a factory data accessor inside the Matter stack. Use
972+
the [nRF Connect Provider](../../src/platform/nrfconnect/FactoryDataProvider.h)
973+
and [FactoryDataParser](../../src/platform/nrfconnect/FactoryDataParser.h) as
935974
examples.
936975
937976
You can read the factory data set from the device's flash memory in different

scripts/tools/nrfconnect/generate_nrfconnect_chip_factory_data.py

+26-9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
from cryptography.hazmat.backends import default_backend
3030
from cryptography.hazmat.primitives.serialization import load_der_private_key
31+
from nrfconnect_generate_partition import PartitionCreator
3132

3233
try:
3334
import qrcode
@@ -228,6 +229,10 @@ def __init__(self, arguments) -> None:
228229
self._factory_data = list()
229230
self._user_data = dict()
230231

232+
# If .json extension is included in the output path, remove it, as script adds it automatically.
233+
if self._args.output.endswith(".json"):
234+
self._args.output = self._args.output[:-len(".json")]
235+
231236
try:
232237
self._validate_args()
233238
except AssertionError as e:
@@ -244,8 +249,6 @@ def _validate_args(self):
244249
"Cannot find Spake2+ verifier, to generate a new one please provide passcode (--passcode)"
245250
assert ((self._args.gen_certs and self._args.chip_cert_path) or (self._args.dac_cert and self._args.pai_cert and self._args.dac_key)), \
246251
"Cannot find paths to DAC or PAI certificates .der files. To generate a new ones please provide a path to chip-cert executable (--chip_cert_path) and add --gen_certs argument"
247-
assert self._args.output.endswith(".json"), \
248-
"Output path doesn't contain .json file path. ({})".format(self._args.output)
249252
assert self._args.passcode not in INVALID_PASSCODES, \
250253
"Provided invalid passcode!"
251254

@@ -310,9 +313,9 @@ def generate_json(self):
310313
sys.exit(-1)
311314

312315
try:
313-
json_file = open(self._args.output, "w+")
316+
json_file = open(self._args.output+".json", "w+")
314317
except FileNotFoundError:
315-
print("Cannot create JSON file in this location: {}".format(self._args.output))
318+
print("Cannot create JSON file in this location: {}".format(self._args.output+".json"))
316319
sys.exit(-1)
317320
with json_file:
318321
# serialize data
@@ -422,11 +425,11 @@ def _generate_onboarding_data(self):
422425
flow=CommissioningFlow.Standard,
423426
vid=self._args.vendor_id,
424427
pid=self._args.product_id)
425-
with open(self._args.output[:-len(".json")] + ".txt", "w") as manual_code_file:
428+
with open(self._args.output + ".txt", "w") as manual_code_file:
426429
manual_code_file.write("Manualcode : " + setup_payload.generate_manualcode() + "\n")
427430
manual_code_file.write("QRCode : " + setup_payload.generate_qrcode())
428431
qr = qrcode.make(setup_payload.generate_qrcode())
429-
qr.save(self._args.output[:-len(".json")] + ".png")
432+
qr.save(self._args.output + ".png")
430433

431434

432435
def main():
@@ -441,7 +444,10 @@ def base64_str(s): return base64.b64decode(s)
441444
parser.add_argument("-s", "--schema", type=str,
442445
help="JSON schema file to validate JSON output data")
443446
parser.add_argument("-o", "--output", type=str, required=True,
444-
help="Output path to store .json file, e.g. my_dir/output.json")
447+
help="Output path to store .json file, e.g. my_dir/output."
448+
"The .json extension will be automatically added by the script and does not need to be provided."
449+
"If provided, an extension will not be added."
450+
"If optional --size and --offset arguments are provided, the script also generates .hex file with factory data.")
445451
parser.add_argument("-v", "--verbose", action="store_true",
446452
help="Run this script with DEBUG logging level")
447453
parser.add_argument("--include_passcode", action="store_true",
@@ -543,6 +549,10 @@ def base64_str(s): return base64.b64decode(s)
543549
help="[string] Provide one of the product finishes")
544550
optional_arguments.add_argument("--product_color", type=str, choices=PRODUCT_COLOR_ENUM.keys(),
545551
help="[string] Provide one of the product colors.")
552+
optional_arguments.add_argument("--offset", type=allow_any_int,
553+
help="Partition offset - an address in device's NVM memory, where factory data will be stored.")
554+
optional_arguments.add_argument("--size", type=allow_any_int,
555+
help="The maximum partition size.")
546556
args = parser.parse_args()
547557

548558
if args.verbose:
@@ -551,9 +561,9 @@ def base64_str(s): return base64.b64decode(s)
551561
log.basicConfig(format='[%(levelname)s] %(message)s', level=log.INFO)
552562

553563
# check if json file already exist
554-
if (exists(args.output) and not args.overwrite):
564+
if (exists(args.output + ".json") and not args.overwrite):
555565
log.error(("Output file: {} already exist, to create a new one add argument '--overwrite'. "
556-
"By default overwriting is disabled").format(args.output))
566+
"By default overwriting is disabled").format(args.output+".json"))
557567
return
558568

559569
if args.schema and no_jsonschema_module:
@@ -572,6 +582,13 @@ def base64_str(s): return base64.b64decode(s)
572582
generator = FactoryDataGenerator(args)
573583
generator.generate_json()
574584

585+
# If optional partition's offset and size were provided, generate factory data output .hex file.
586+
if args.offset and args.size:
587+
partition_creator = PartitionCreator(args.offset, args.size, args.output + ".json", args.output)
588+
cbor_data = partition_creator.generate_cbor()
589+
partition_creator.create_hex(cbor_data)
590+
partition_creator.create_bin()
591+
575592

576593
if __name__ == "__main__":
577594
main()

0 commit comments

Comments
 (0)