-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[nrfconnect] Generate Nordic-specific OTA image header (#15248)
* [nrfconnect] Generate Nordic-specific OTA image header Generate Nordic-specific OTA image header describing contents of the OTA image using a TLV-based structure. * [nrfconnect] Parse OTA image content header. * Fix bugs * Address code review comments * Update docs * Restyled by prettier-markdown Co-authored-by: Restyled.io <[email protected]>
- Loading branch information
1 parent
2aab9b8
commit 4668421
Showing
12 changed files
with
533 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
config/nrfconnect/chip-module/make_ota_content_header.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# | ||
# Copyright (c) 2022 Project CHIP Authors | ||
# All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
""" | ||
Utility for creating a Matter OTA content header that describes a list of files | ||
included in a Matter OTA image. The header format is specific to the nRF Connect | ||
platform. | ||
Usage example: | ||
./make_ota_content_header.py \ | ||
--file mcuboot mcuboot/zephyr/zephyr.bin \ | ||
--file app_mcuboot zephyr/app_update.bin \ | ||
out.header | ||
""" | ||
|
||
import argparse | ||
import os | ||
import struct | ||
import sys | ||
from enum import IntEnum | ||
|
||
sys.path.insert(0, os.path.join(os.path.dirname( | ||
__file__), '../../../src/controller/python')) | ||
from chip.tlv import TLVWriter, uint # noqa: E402 | ||
|
||
|
||
# Context-specific tags describing members of the top-level header TLV structure | ||
class HeaderTag(IntEnum): | ||
FILE_INFO_LIST = 0, | ||
|
||
|
||
# Context-specific tags describing members of the file info TLV structure | ||
class FileInfoTag(IntEnum): | ||
FILE_ID = 0, | ||
FILE_SIZE = 1, | ||
|
||
|
||
# File identifiers for all the supported file types | ||
FILE_IDS = dict( | ||
mcuboot=0, | ||
app_mcuboot=1, | ||
net_mcuboot=2, | ||
) | ||
|
||
|
||
def generate_header_tlv(file_infos: list): | ||
""" | ||
Generate TLV structure describing OTA image contents: | ||
Header ::= | ||
[0] FileInfoList: [ | ||
[0]: | ||
[0] FileId: <file_id> | ||
[1] FileSize: <file_size> | ||
[1]: | ||
[0] FileId: <file_id> | ||
[1] FileSize: <file_size> | ||
... | ||
] | ||
""" | ||
|
||
writer = TLVWriter() | ||
writer.put(None, { | ||
HeaderTag.FILE_INFO_LIST: [{ | ||
FileInfoTag.FILE_ID: uint(file_id), | ||
FileInfoTag.FILE_SIZE: uint(file_size), | ||
} for file_id, file_size in file_infos] | ||
}) | ||
|
||
return writer.encoding | ||
|
||
|
||
def generate_header(file_infos: list, output_file: str): | ||
""" | ||
Generate OTA image content header and save it to file | ||
""" | ||
|
||
header_tlv = generate_header_tlv(file_infos) | ||
header = struct.pack('<I', len(header_tlv)) + header_tlv | ||
|
||
with open(output_file, 'wb') as file: | ||
file.write(header) | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser( | ||
description='nRF Connect Matter OTA image content header utility', | ||
fromfile_prefix_chars='@') | ||
|
||
file_types = '{' + ','.join(FILE_IDS.keys()) + '}' | ||
parser.add_argument('--file', required=True, nargs=2, action='append', | ||
metavar=(file_types, 'path'), | ||
help='File included in Matter OTA image') | ||
parser.add_argument('output_file', help='Path to output header file') | ||
|
||
args = parser.parse_args() | ||
|
||
file_infos = [(FILE_IDS[type], os.path.getsize(path)) | ||
for type, path in args.file] | ||
|
||
generate_header(file_infos, args.output_file) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# | ||
# Copyright (c) 2022 Project CHIP Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
find_package(Python3 REQUIRED) | ||
|
||
# | ||
# Create CMake target for building a Matter OTA content header that describes a list of files | ||
# included in a Matter OTA image. The header format is specific to the nRF Connect platform. | ||
# | ||
# Required arguments: | ||
# FILE_TYPES type, [type...] - list of file types that identify files included in the image. | ||
# Currently recognized file types are: | ||
# * mcuboot - MCUBoot bootloader image in the raw binary format | ||
# * app_mcuboot - application core image in the MCUBoot-defined format | ||
# * net_mcuboot - network core image in the MCUBoot-defined format | ||
# FILE_PATHS path, [path...] - list of paths to files included in the image | ||
# OUTPUT_FILE path - file path to write the newly created Matter OTA content header | ||
# | ||
function(chip_ota_content_header TARGET_NAME) | ||
cmake_parse_arguments(ARG "" "OUTPUT_FILE" "FILE_TYPES;FILE_PATHS" ${ARGN}) | ||
|
||
if (NOT ARG_FILE_TYPES OR NOT ARG_FILE_PATHS OR NOT ARG_OUTPUT_FILE) | ||
message(FATAL_ERROR "All FILE_TYPES, FILE_PATHS and OUTPUT_FILE arguments must be specified") | ||
endif() | ||
|
||
# Prepare make_ota_content_header.py argument list | ||
set(SCRIPT_ARGS) | ||
|
||
foreach(file_info IN ZIP_LISTS ARG_FILE_TYPES ARG_FILE_PATHS) | ||
list(APPEND SCRIPT_ARGS | ||
--file | ||
"${file_info_0}" | ||
"${file_info_1}" | ||
) | ||
endforeach() | ||
|
||
list(APPEND SCRIPT_ARGS ${ARG_OUTPUT_FILE}) | ||
|
||
# Convert the argument list to multi-line string | ||
string(REPLACE ";" "\n" SCRIPT_ARGS "${SCRIPT_ARGS}") | ||
|
||
# Pass the argument list via file to avoid hitting Windows command-line length limit | ||
file(GENERATE | ||
OUTPUT ${ARG_OUTPUT_FILE}.args | ||
CONTENT ${SCRIPT_ARGS} | ||
) | ||
|
||
add_custom_target(${TARGET_NAME} ALL | ||
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/make_ota_content_header.py @${ARG_OUTPUT_FILE}.args | ||
) | ||
endfunction() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.