From 6729ffdbac4d8e5bca46a1179f1c3a1663748a85 Mon Sep 17 00:00:00 2001 From: Seth Rickard Date: Tue, 3 May 2022 11:18:02 -0500 Subject: [PATCH] [TI] Create and process OTA header (#17556) * add and process the OTA Image header * add software trip version for first run * change isFirstRun to be based on the OTA update process and target SW version * copy header and c style comments * add gitignore to ti submodule * add matter_ prefix to ota parameters fix location of oad hexfile --- .../all-clusters-app/cc13x2x7_26x2x7/BUILD.gn | 8 +- .../all-clusters-app/cc13x2x7_26x2x7/args.gni | 5 + .../main/include/CHIPProjectConfig.h | 26 +--- examples/lock-app/cc13x2x7_26x2x7/BUILD.gn | 8 +- examples/lock-app/cc13x2x7_26x2x7/args.gni | 7 ++ .../lock-app/cc13x2x7_26x2x7/main/AppTask.cpp | 5 +- .../main/include/CHIPProjectConfig.h | 28 ++--- examples/pump-app/cc13x2x7_26x2x7/BUILD.gn | 8 +- examples/pump-app/cc13x2x7_26x2x7/args.gni | 5 + .../main/include/CHIPProjectConfig.h | 34 +---- .../cc13x2x7_26x2x7/BUILD.gn | 8 +- .../cc13x2x7_26x2x7/args.gni | 5 + .../main/include/CHIPProjectConfig.h | 34 +---- .../cc13x2_26x2/OTAImageProcessorImpl.cpp | 88 ++++++++++--- .../cc13x2_26x2/OTAImageProcessorImpl.h | 9 +- .../ti_simplelink_sdk/oad_merge_tool.py | 41 +++++++ .../ti_simplelink_sdk/repo_cc13xx_cc26xx | 2 +- .../ti_simplelink_executable.gni | 116 ++++++++++++++++-- .../ti_simplelink_sdk/ti_simplelink_sdk.gni | 3 + 19 files changed, 278 insertions(+), 162 deletions(-) create mode 100644 third_party/ti_simplelink_sdk/oad_merge_tool.py diff --git a/examples/all-clusters-app/cc13x2x7_26x2x7/BUILD.gn b/examples/all-clusters-app/cc13x2x7_26x2x7/BUILD.gn index 7a74881a44d55d..33383bb4484128 100644 --- a/examples/all-clusters-app/cc13x2x7_26x2x7/BUILD.gn +++ b/examples/all-clusters-app/cc13x2x7_26x2x7/BUILD.gn @@ -30,13 +30,7 @@ project_dir = "${chip_root}/examples/all-clusters-app/cc13x2x7_26x2x7" ti_simplelink_sdk("sdk") { include_dirs = [ "${project_dir}/main/include" ] - - defines = [] - if (is_debug) { - defines += [ "BUILD_RELEASE=0" ] - } else { - defines += [ "BUILD_RELEASE=1" ] - } + public_configs = [ ":all-clusters-app_config" ] } ti_sysconfig("sysconfig") { diff --git a/examples/all-clusters-app/cc13x2x7_26x2x7/args.gni b/examples/all-clusters-app/cc13x2x7_26x2x7/args.gni index abd9bc37d56644..61eb83d4c18b2c 100644 --- a/examples/all-clusters-app/cc13x2x7_26x2x7/args.gni +++ b/examples/all-clusters-app/cc13x2x7_26x2x7/args.gni @@ -45,3 +45,8 @@ chip_config_network_layer_ble = true # Disable lock tracking, since our FreeRTOS configuration does not set # INCLUDE_xSemaphoreGetMutexHolder chip_stack_lock_tracking = "none" + +matter_device_vid = "0xFFF1" +matter_device_pid = "0x8006" +matter_software_ver = "0x0001" +matter_software_ver_str = "1.0d1" diff --git a/examples/all-clusters-app/cc13x2x7_26x2x7/main/include/CHIPProjectConfig.h b/examples/all-clusters-app/cc13x2x7_26x2x7/main/include/CHIPProjectConfig.h index 748332dc496eac..35977ee29593fa 100644 --- a/examples/all-clusters-app/cc13x2x7_26x2x7/main/include/CHIPProjectConfig.h +++ b/examples/all-clusters-app/cc13x2x7_26x2x7/main/include/CHIPProjectConfig.h @@ -47,20 +47,6 @@ */ #define CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER "TEST_SN" -/** - * CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID - * - * 0xFFF1: Test vendor - */ -#define CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID 0xFFF1 - -/** - * CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID - * - * 0x8006: example lock app - */ -#define CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID 0x8006 - /** * CHIP_DEVICE_CONFIG_DEVICE_HARDWARE_VERSION * @@ -72,15 +58,13 @@ #define CHIP_DEVICE_CONFIG_DEVICE_HARDWARE_VERSION 1 /** + * Values set by args.gni: + * CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID + * CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID * CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING - * - * A string identifying the software version running on the device. - * CHIP currently expects the software version to be in the format - * {MAJOR_VERSION}.0d{MINOR_VERSION} + * CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION */ -#ifndef CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING -#define CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING "1.0d1" -#endif + /** * CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE * diff --git a/examples/lock-app/cc13x2x7_26x2x7/BUILD.gn b/examples/lock-app/cc13x2x7_26x2x7/BUILD.gn index 2636803899e2ab..57792e12f69e0c 100644 --- a/examples/lock-app/cc13x2x7_26x2x7/BUILD.gn +++ b/examples/lock-app/cc13x2x7_26x2x7/BUILD.gn @@ -30,13 +30,7 @@ project_dir = "${chip_root}/examples/lock-app/cc13x2x7_26x2x7" ti_simplelink_sdk("sdk") { include_dirs = [ "${project_dir}/main/include" ] - - defines = [] - if (is_debug) { - defines += [ "BUILD_RELEASE=0" ] - } else { - defines += [ "BUILD_RELEASE=1" ] - } + public_configs = [ ":lock_app_config" ] } ti_sysconfig("sysconfig") { diff --git a/examples/lock-app/cc13x2x7_26x2x7/args.gni b/examples/lock-app/cc13x2x7_26x2x7/args.gni index d982dce73c11c5..ee347710e252e2 100644 --- a/examples/lock-app/cc13x2x7_26x2x7/args.gni +++ b/examples/lock-app/cc13x2x7_26x2x7/args.gni @@ -39,3 +39,10 @@ chip_config_network_layer_ble = true # Disable lock tracking, since our FreeRTOS configuration does not set # INCLUDE_xSemaphoreGetMutexHolder chip_stack_lock_tracking = "none" + +chip_openthread_ftd = true + +matter_device_vid = "0xFFF1" +matter_device_pid = "0x8006" +matter_software_ver = "0x0001" +matter_software_ver_str = "1.0d1" diff --git a/examples/lock-app/cc13x2x7_26x2x7/main/AppTask.cpp b/examples/lock-app/cc13x2x7_26x2x7/main/AppTask.cpp index 35c474c6911010..ab0bf4047af4cb 100644 --- a/examples/lock-app/cc13x2x7_26x2x7/main/AppTask.cpp +++ b/examples/lock-app/cc13x2x7_26x2x7/main/AppTask.cpp @@ -129,8 +129,11 @@ int AppTask::Init() while (1) ; } - +#if CHIP_DEVICE_CONFIG_THREAD_FTD ret = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_Router); +#else + ret = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice); +#endif if (ret != CHIP_NO_ERROR) { PLAT_LOG("ConnectivityMgr().SetThreadDeviceType() failed"); diff --git a/examples/lock-app/cc13x2x7_26x2x7/main/include/CHIPProjectConfig.h b/examples/lock-app/cc13x2x7_26x2x7/main/include/CHIPProjectConfig.h index cb80943d5abb0c..f6e0a88b752edb 100644 --- a/examples/lock-app/cc13x2x7_26x2x7/main/include/CHIPProjectConfig.h +++ b/examples/lock-app/cc13x2x7_26x2x7/main/include/CHIPProjectConfig.h @@ -47,20 +47,6 @@ */ #define CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER "TEST_SN" -/** - * CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID - * - * 0xFFF1: Test vendor - */ -#define CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID 0xFFF1 - -/** - * CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID - * - * 0x8006: example lock app - */ -#define CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID 0x8006 - /** * CHIP_DEVICE_CONFIG_DEVICE_HARDWARE_VERSION * @@ -69,18 +55,18 @@ * physical device, a change to its packaging, and/or a change to its marketing presentation. * This value is generally *not* incremented for device software versions. */ +#ifndef CHIP_DEVICE_CONFIG_DEVICE_HARDWARE_VERSION #define CHIP_DEVICE_CONFIG_DEVICE_HARDWARE_VERSION 1 +#endif /** + * Values set by args.gni: + * CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID + * CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID * CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING - * - * A string identifying the software version running on the device. - * CHIP currently expects the software version to be in the format - * {MAJOR_VERSION}.0d{MINOR_VERSION} + * CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION */ -#ifndef CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING -#define CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING "1.0d1" -#endif + /** * CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE * diff --git a/examples/pump-app/cc13x2x7_26x2x7/BUILD.gn b/examples/pump-app/cc13x2x7_26x2x7/BUILD.gn index 7ee9db88c7a72e..237b81f36ad747 100644 --- a/examples/pump-app/cc13x2x7_26x2x7/BUILD.gn +++ b/examples/pump-app/cc13x2x7_26x2x7/BUILD.gn @@ -29,13 +29,7 @@ project_dir = "${chip_root}/examples/pump-app/cc13x2x7_26x2x7" ti_simplelink_sdk("sdk") { include_dirs = [ "${project_dir}/main/include" ] - - defines = [] - if (is_debug) { - defines += [ "BUILD_RELEASE=0" ] - } else { - defines += [ "BUILD_RELEASE=1" ] - } + public_configs = [ ":pump_app_config" ] } ti_sysconfig("sysconfig") { diff --git a/examples/pump-app/cc13x2x7_26x2x7/args.gni b/examples/pump-app/cc13x2x7_26x2x7/args.gni index c2a8875510a80e..1523182c52d43a 100644 --- a/examples/pump-app/cc13x2x7_26x2x7/args.gni +++ b/examples/pump-app/cc13x2x7_26x2x7/args.gni @@ -40,3 +40,8 @@ chip_config_network_layer_ble = true # Disable lock tracking, since our FreeRTOS configuration does not set # INCLUDE_xSemaphoreGetMutexHolder chip_stack_lock_tracking = "none" + +matter_device_vid = "0xFFF1" +matter_device_pid = "0x800A" +matter_software_ver = "0x0001" +matter_software_ver_str = "1.0d1" diff --git a/examples/pump-app/cc13x2x7_26x2x7/main/include/CHIPProjectConfig.h b/examples/pump-app/cc13x2x7_26x2x7/main/include/CHIPProjectConfig.h index 40ee0c8308b74c..06dbef7ace465e 100644 --- a/examples/pump-app/cc13x2x7_26x2x7/main/include/CHIPProjectConfig.h +++ b/examples/pump-app/cc13x2x7_26x2x7/main/include/CHIPProjectConfig.h @@ -42,28 +42,6 @@ // Use a default pairing code if one hasn't been provisioned in flash. #define CHIP_DEVICE_CONFIG_USE_TEST_PAIRING_CODE "CHIPUS" -/** - * CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER - * - * Enables the use of a hard-coded default serial number if none - * is found in CHIP NV storage. - */ -#define CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER "TEST_SN" - -/** - * CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID - * - * 0xFFF1: Test vendor. - */ -#define CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID 0xFFF1 - -/** - * CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID - * - * 0x8011: example pump-app - */ -#define CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID 0x800A - /** * CHIP_DEVICE_CONFIG_DEVICE_HARDWARE_VERSION * @@ -75,15 +53,13 @@ #define CHIP_DEVICE_CONFIG_DEVICE_HARDWARE_VERSION 1 /** + * Values set by args.gni: + * CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID + * CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID * CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING - * - * A string identifying the software version running on the device. - * CHIP currently expects the software version to be in the format - * {MAJOR_VERSION}.0d{MINOR_VERSION} + * CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION */ -#ifndef CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING -#define CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING "1.0d1" -#endif + /** * CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE * diff --git a/examples/pump-controller-app/cc13x2x7_26x2x7/BUILD.gn b/examples/pump-controller-app/cc13x2x7_26x2x7/BUILD.gn index fedefd4dcb5d5d..d3031712c04a58 100644 --- a/examples/pump-controller-app/cc13x2x7_26x2x7/BUILD.gn +++ b/examples/pump-controller-app/cc13x2x7_26x2x7/BUILD.gn @@ -30,13 +30,7 @@ project_dir = "${chip_root}/examples/pump-controller-app/cc13x2x7_26x2x7" ti_simplelink_sdk("sdk") { include_dirs = [ "${project_dir}/main/include" ] - - defines = [] - if (is_debug) { - defines += [ "BUILD_RELEASE=0" ] - } else { - defines += [ "BUILD_RELEASE=1" ] - } + public_configs = [ ":pump_controller_app_config" ] } ti_sysconfig("sysconfig") { diff --git a/examples/pump-controller-app/cc13x2x7_26x2x7/args.gni b/examples/pump-controller-app/cc13x2x7_26x2x7/args.gni index ebb3a78797278f..5b9366d218222b 100644 --- a/examples/pump-controller-app/cc13x2x7_26x2x7/args.gni +++ b/examples/pump-controller-app/cc13x2x7_26x2x7/args.gni @@ -39,3 +39,8 @@ chip_config_network_layer_ble = true # Disable lock tracking, since our FreeRTOS configuration does not set # INCLUDE_xSemaphoreGetMutexHolder chip_stack_lock_tracking = "none" + +matter_device_vid = "0xFFF1" +matter_device_pid = "0x8011" +matter_software_ver = "0x0001" +matter_software_ver_str = "1.0d1" diff --git a/examples/pump-controller-app/cc13x2x7_26x2x7/main/include/CHIPProjectConfig.h b/examples/pump-controller-app/cc13x2x7_26x2x7/main/include/CHIPProjectConfig.h index 0723d21afb420c..3302d966457c7c 100644 --- a/examples/pump-controller-app/cc13x2x7_26x2x7/main/include/CHIPProjectConfig.h +++ b/examples/pump-controller-app/cc13x2x7_26x2x7/main/include/CHIPProjectConfig.h @@ -42,28 +42,6 @@ // Use a default pairing code if one hasn't been provisioned in flash. #define CHIP_DEVICE_CONFIG_USE_TEST_PAIRING_CODE "CHIPUS" -/** - * CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER - * - * Enables the use of a hard-coded default serial number if none - * is found in CHIP NV storage. - */ -#define CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER "TEST_SN" - -/** - * CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID - * - * 0xFFF1: Test vendor. - */ -#define CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID 0xFFF1 - -/** - * CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID - * - * 0x8009: example pump-controller-app - */ -#define CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID 0x8011 - /** * CHIP_DEVICE_CONFIG_DEVICE_HARDWARE_VERSION * @@ -75,15 +53,13 @@ #define CHIP_DEVICE_CONFIG_DEVICE_HARDWARE_VERSION 1 /** + * Values set by args.gni: + * CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID + * CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID * CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING - * - * A string identifying the software version running on the device. - * CHIP currently expects the software version to be in the format - * {MAJOR_VERSION}.0d{MINOR_VERSION} + * CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION */ -#ifndef CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING -#define CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING "1.0d1" -#endif + /** * CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE * diff --git a/src/platform/cc13x2_26x2/OTAImageProcessorImpl.cpp b/src/platform/cc13x2_26x2/OTAImageProcessorImpl.cpp index 65b336473d1328..09286a6aa9aedb 100644 --- a/src/platform/cc13x2_26x2/OTAImageProcessorImpl.cpp +++ b/src/platform/cc13x2_26x2/OTAImageProcessorImpl.cpp @@ -17,6 +17,8 @@ */ #include +#include +#include #include "OTAImageProcessorImpl.h" @@ -31,29 +33,32 @@ #include DeviceFamily_constructPath(driverlib/sys_ctrl.h) // clang-format on +using namespace chip::DeviceLayer; +using namespace chip::DeviceLayer::PersistedStorage; + namespace chip { CHIP_ERROR OTAImageProcessorImpl::PrepareDownload() { - DeviceLayer::PlatformMgr().ScheduleWork(HandlePrepareDownload, reinterpret_cast(this)); + PlatformMgr().ScheduleWork(HandlePrepareDownload, reinterpret_cast(this)); return CHIP_NO_ERROR; } CHIP_ERROR OTAImageProcessorImpl::Finalize() { - DeviceLayer::PlatformMgr().ScheduleWork(HandleFinalize, reinterpret_cast(this)); + PlatformMgr().ScheduleWork(HandleFinalize, reinterpret_cast(this)); return CHIP_NO_ERROR; } CHIP_ERROR OTAImageProcessorImpl::Apply() { - DeviceLayer::PlatformMgr().ScheduleWork(HandleApply, reinterpret_cast(this)); + PlatformMgr().ScheduleWork(HandleApply, reinterpret_cast(this)); return CHIP_NO_ERROR; } CHIP_ERROR OTAImageProcessorImpl::Abort() { - DeviceLayer::PlatformMgr().ScheduleWork(HandleAbort, reinterpret_cast(this)); + PlatformMgr().ScheduleWork(HandleAbort, reinterpret_cast(this)); return CHIP_NO_ERROR; } @@ -76,10 +81,26 @@ CHIP_ERROR OTAImageProcessorImpl::ProcessBlock(ByteSpan & block) ChipLogError(SoftwareUpdate, "Cannot set block data: %" CHIP_ERROR_FORMAT, err.Format()); } - DeviceLayer::PlatformMgr().ScheduleWork(HandleProcessBlock, reinterpret_cast(this)); + PlatformMgr().ScheduleWork(HandleProcessBlock, reinterpret_cast(this)); return CHIP_NO_ERROR; } +bool OTAImageProcessorImpl::IsFirstImageRun() +{ + OTARequestorInterface * requestor; + uint32_t runningSwVer; + + if (CHIP_NO_ERROR != ConfigurationMgr().GetSoftwareVersion(runningSwVer)) + { + return false; + } + + requestor = GetRequestorInstance(); + + return (requestor->GetTargetVersion() == runningSwVer) && + (requestor->GetCurrentUpdateState() == chip::app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum::kApplying); +} + /* DESIGN NOTE: The Boot Image Manager will search external flash for an * `ExtImageInfo_t` structure every 4K for 1M. This structure points to where * the executable image is in external flash with a uint32_t. It is possible to @@ -92,10 +113,10 @@ CHIP_ERROR OTAImageProcessorImpl::ProcessBlock(ByteSpan & block) #define IMG_START (4 * EFL_SIZE_META) -static bool readExtFlashImgHeader(NVS_Handle handle, imgFixedHdr_t * header) +static bool readExtFlashImgHeader(NVS_Handle handle, imgFixedHdr_t * header, size_t otaHeaderLen) { int_fast16_t status; - status = NVS_read(handle, IMG_START, header, sizeof(header)); + status = NVS_read(handle, IMG_START + otaHeaderLen, header, sizeof(imgFixedHdr_t)); return (status == NVS_STATUS_SUCCESS); } @@ -117,17 +138,23 @@ static bool writeExtFlashImgPages(NVS_Handle handle, size_t bytesWritten, Mutabl status = NVS_erase(handle, IMG_START + (erasedSectors * sectorSize), (neededSectors - erasedSectors) * sectorSize); if (status != NVS_STATUS_SUCCESS) { + ChipLogError(SoftwareUpdate, "NVS_erase failed status: %d", status); return false; } } status = NVS_write(handle, IMG_START + bytesWritten, block.data(), block.size(), NVS_WRITE_POST_VERIFY); - return (status == NVS_STATUS_SUCCESS); + if (status != NVS_STATUS_SUCCESS) + { + ChipLogError(SoftwareUpdate, "NVS_write failed status: %d", status); + return false; + } + return true; } static bool readExtFlashMetaHeader(NVS_Handle handle, ExtImageInfo_t * header) { int_fast16_t status; - status = NVS_read(handle, EFL_ADDR_META, header, sizeof(header)); + status = NVS_read(handle, EFL_ADDR_META, header, sizeof(ExtImageInfo_t)); return (status == NVS_STATUS_SUCCESS); } @@ -152,7 +179,7 @@ static bool writeExtFlashMetaHeader(NVS_Handle handle, ExtImageInfo_t * header) { return false; } - status = NVS_write(handle, EFL_ADDR_META, header, sizeof(header), NVS_WRITE_POST_VERIFY); + status = NVS_write(handle, EFL_ADDR_META, header, sizeof(ExtImageInfo_t), NVS_WRITE_POST_VERIFY); return (status == NVS_STATUS_SUCCESS); } @@ -210,20 +237,20 @@ uint32_t crc_update(uint32_t crc, const void * data, size_t data_len) return crc & 0xffffffff; } -static bool validateExtFlashImage(NVS_Handle handle) +static bool validateExtFlashImage(NVS_Handle handle, size_t otaHeaderLen) { uint32_t crc; imgFixedHdr_t header; size_t addr, endAddr; - if (!readExtFlashImgHeader(handle, &header)) + if (!readExtFlashImgHeader(handle, &header, otaHeaderLen)) { return false; } /* CRC is calculated after the CRC element of the image header */ - addr = IMG_START + IMG_DATA_OFFSET; - endAddr = IMG_START + header.len; + addr = IMG_START + otaHeaderLen + IMG_DATA_OFFSET; + endAddr = IMG_START + otaHeaderLen + header.len; crc = 0xFFFFFFFF; @@ -286,20 +313,26 @@ void OTAImageProcessorImpl::HandlePrepareDownload(intptr_t context) void OTAImageProcessorImpl::HandleFinalize(intptr_t context) { ExtImageInfo_t header; - auto * imageProcessor = reinterpret_cast(context); + auto * imageProcessor = reinterpret_cast(context); + const uint8_t extImgId[] = OAD_EFL_MAGIC; + if (imageProcessor == nullptr) { return; } - if (!readExtFlashImgHeader(imageProcessor->mNvsHandle, &(header.fixedHdr))) + if (!readExtFlashImgHeader(imageProcessor->mNvsHandle, &(header.fixedHdr), + imageProcessor->mFixedOtaHeader.headerSize + sizeof(imageProcessor->mFixedOtaHeader))) { return; } - header.extFlAddr = IMG_START; + // offset in the size of the fixed and variable OTA image headers + memcpy(&(header.fixedHdr.imgID), extImgId, sizeof(header.fixedHdr.imgID)); + header.extFlAddr = IMG_START + imageProcessor->mFixedOtaHeader.headerSize + sizeof(imageProcessor->mFixedOtaHeader); header.counter = 0x0; - if (validateExtFlashImage(imageProcessor->mNvsHandle)) + if (validateExtFlashImage(imageProcessor->mNvsHandle, + imageProcessor->mFixedOtaHeader.headerSize + sizeof(imageProcessor->mFixedOtaHeader))) { // only write the meta header if the crc check passes writeExtFlashMetaHeader(imageProcessor->mNvsHandle, &header); @@ -367,8 +400,25 @@ void OTAImageProcessorImpl::HandleProcessBlock(intptr_t context) return; } - // TODO: Process block header if any + /* Save the fixed size header */ + if (imageProcessor->mParams.downloadedBytes < sizeof(imageProcessor->mFixedOtaHeader)) + { + uint8_t * header = reinterpret_cast(&(imageProcessor->mFixedOtaHeader)); + if (imageProcessor->mBlock.size() + imageProcessor->mParams.downloadedBytes < sizeof(imageProcessor->mFixedOtaHeader)) + { + memcpy(header + imageProcessor->mParams.downloadedBytes, imageProcessor->mBlock.data(), imageProcessor->mBlock.size()); + } + else + { + memcpy(header + imageProcessor->mParams.downloadedBytes, imageProcessor->mBlock.data(), + sizeof(imageProcessor->mFixedOtaHeader) - imageProcessor->mParams.downloadedBytes); + } + } + + /* chip::OTAImageHeaderParser can be used for processing the variable size header */ + ChipLogDetail(SoftwareUpdate, "Write block %d, %d", (size_t) imageProcessor->mParams.downloadedBytes, + imageProcessor->mBlock.size()); if (!writeExtFlashImgPages(imageProcessor->mNvsHandle, imageProcessor->mParams.downloadedBytes, imageProcessor->mBlock)) { imageProcessor->mDownloader->EndDownload(CHIP_ERROR_WRITE_FAILED); diff --git a/src/platform/cc13x2_26x2/OTAImageProcessorImpl.h b/src/platform/cc13x2_26x2/OTAImageProcessorImpl.h index 700cef6141a618..daf6ea4ffde029 100644 --- a/src/platform/cc13x2_26x2/OTAImageProcessorImpl.h +++ b/src/platform/cc13x2_26x2/OTAImageProcessorImpl.h @@ -35,7 +35,7 @@ class OTAImageProcessorImpl : public OTAImageProcessorInterface CHIP_ERROR Apply() override; CHIP_ERROR Abort() override; CHIP_ERROR ProcessBlock(ByteSpan & block) override; - bool IsFirstImageRun() override { return false; } + bool IsFirstImageRun() override; CHIP_ERROR ConfirmCurrentImage() override { return CHIP_NO_ERROR; } void SetOTADownloader(OTADownloader * downloader) { mDownloader = downloader; } @@ -62,6 +62,13 @@ class OTAImageProcessorImpl : public OTAImageProcessorInterface uint16_t mMetaPage; MutableByteSpan mBlock; OTADownloader * mDownloader; + + struct fixedOtaHeader + { + uint32_t fileIdentifier; + uint64_t totalSize; + uint32_t headerSize; + } PACK_STRUCT_STRUCT mFixedOtaHeader; }; } // namespace chip diff --git a/third_party/ti_simplelink_sdk/oad_merge_tool.py b/third_party/ti_simplelink_sdk/oad_merge_tool.py new file mode 100644 index 00000000000000..262732619014b3 --- /dev/null +++ b/third_party/ti_simplelink_sdk/oad_merge_tool.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python + +# 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. + +"""Simple hexmerge script for combining the BIM and App + +This script provides a basic utility to combine the OAD application binary file with the Boot Image Manager hex file. The output is a combined hex file that can be programmed on the target and run. + +Run with: + python oad_merge_tool.py +""" + +import sys +import intelhex + +oad_bin_file = sys.argv[1] +bim_hex_file = sys.argv[2] +combined_hex = sys.argv[3] + +# merge binary executable with bim hex file +ota_image = intelhex.IntelHex() +ota_image.fromfile(oad_bin_file, format='bin') + +bim_hex = intelhex.IntelHex() +bim_hex.fromfile(bim_hex_file, format='hex') + +ota_image.merge(bim_hex) + +ota_image.tofile(combined_hex, format='hex') diff --git a/third_party/ti_simplelink_sdk/repo_cc13xx_cc26xx b/third_party/ti_simplelink_sdk/repo_cc13xx_cc26xx index f9db17b6d9c07f..55eba10c7f93d1 160000 --- a/third_party/ti_simplelink_sdk/repo_cc13xx_cc26xx +++ b/third_party/ti_simplelink_sdk/repo_cc13xx_cc26xx @@ -1 +1 @@ -Subproject commit f9db17b6d9c07f9c2d613d297a0fcfaacbea2f06 +Subproject commit 55eba10c7f93d1bdc2f875e888f76d9396c00dbf diff --git a/third_party/ti_simplelink_sdk/ti_simplelink_executable.gni b/third_party/ti_simplelink_sdk/ti_simplelink_executable.gni index e3b55cdabdefb1..53e3b437876b17 100644 --- a/third_party/ti_simplelink_sdk/ti_simplelink_executable.gni +++ b/third_party/ti_simplelink_sdk/ti_simplelink_executable.gni @@ -21,12 +21,25 @@ import("${chip_root}/src/platform/device.gni") import("ti_simplelink_board.gni") import("ti_simplelink_sdk.gni") +declare_args() { + matter_device_vid = "" + matter_device_pid = "" + matter_software_ver = "" + matter_software_ver_str = "" +} + +assert(matter_device_vid != "", "matter_device_vid must be specified") +assert(matter_device_pid != "", "matter_device_pid must be specified") +assert(matter_software_ver != "", "matter_software_ver must be specified") +assert(matter_software_ver_str != "", + "matter_software_ver_str must be specified") + template("ti_simplelink_executable") { simplelink_target_name = target_name if (chip_enable_ota_requestor) { # Generating the ota binary image - final_target = "${target_name}_ota" + final_target = "${target_name}.ota" } else { # The executable is the final target. final_target = "${simplelink_target_name}.out" @@ -37,9 +50,27 @@ template("ti_simplelink_executable") { objcopy_image_format = "ihex" objcopy = "arm-none-eabi-objcopy" + config("${simplelink_target_name}_config") { + defines = [] + + if (defined(invoker.defines)) { + defines += invoker.defines + } + + # add OTA options if available + defines += [ + "CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID=${matter_device_vid}", + "CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID=${matter_device_pid}", + "CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION=${matter_software_ver}", + "CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING=\"${matter_software_ver_str}\"", + ] + } + flashable_executable("${simplelink_target_name}.out") { forward_variables_from(invoker, "*") + public_configs = [ ":${simplelink_target_name}_config" ] + if (ti_simplelink_device_family == "cc13x2x7_26x2x7") { if (chip_enable_ota_requestor) { sources += @@ -63,21 +94,43 @@ template("ti_simplelink_executable") { if (chip_enable_ota_requestor && ti_simplelink_device_family == "cc13x2x7_26x2x7") { - pw_python_action("${target_name}_ota") { + pw_python_action("${simplelink_target_name}.bin") { public_deps = [ ":${simplelink_target_name}.out.image" ] - script = "${ti_simplelink_sdk_build_root}/run_oad_tool.py" + script = "${ti_simplelink_sdk_root}//tools/common/oad/oad_image_tool.py" sources = [ "${root_out_dir}/${objcopy_image_name}" ] - outputs = [ - "${root_out_dir}/${output_base_name}.bin", - "${root_out_dir}/${output_base_name}-bim.hex", + outputs = [ "${root_out_dir}/${output_base_name}.bin" ] + + args = [ + "--verbose", + "ccs", + rebase_path(root_out_dir, root_build_dir), + "7", + "-hex1", + rebase_path("${root_out_dir}/${output_base_name}.hex", root_out_dir), + "-o", + output_base_name, ] if (defined(invoker.pem_file)) { - pem_file = invoker.pem_file + args += [ + "-k", + rebase_path(invoker.pem_file, root_build_dir), + ] } else { - pem_file = "${ti_simplelink_sdk_root}/tools/common/oad/private.pem" + args += [ + "-k", + rebase_path("${ti_simplelink_sdk_root}/tools/common/oad/private.pem", + root_build_dir), + ] } + } + pw_python_action("${simplelink_target_name}-bim.hex") { + public_deps = [ ":${simplelink_target_name}.bin" ] + + script = "${ti_simplelink_sdk_build_root}/oad_merge_tool.py" + sources = [ "${root_out_dir}/${output_base_name}.bin" ] + outputs = [ "${root_out_dir}/${output_base_name}-bim.hex" ] if (defined(invoker.bim_hex)) { bim_hex = invoker.bim_hex @@ -86,12 +139,51 @@ template("ti_simplelink_executable") { } args = [ - rebase_path(ti_simplelink_sdk_root, root_build_dir), - rebase_path(root_out_dir, root_build_dir), - output_base_name, - rebase_path(pem_file, root_build_dir), + rebase_path("${root_out_dir}/${output_base_name}.bin", root_build_dir), rebase_path(bim_hex, root_build_dir), + rebase_path("${root_out_dir}/${output_base_name}-bim.hex", + root_build_dir), + ] + + args += [ "${root_out_dir}/${output_base_name}-bim.hex" ] + } + pw_python_action("${target_name}.ota") { + public_deps = [ ":${simplelink_target_name}-bim.hex" ] + + script = "${chip_root}/src/app/ota_image_tool.py" + sources = [ "${root_out_dir}/${output_base_name}.bin" ] + outputs = [ "${root_out_dir}/${output_base_name}.ota" ] + + args = [ + "create", + rebase_path("${root_out_dir}/${output_base_name}.bin", root_build_dir), + rebase_path("${root_out_dir}/${output_base_name}.ota", root_build_dir), + ] + + args += [ + "-v", + matter_device_vid, + "-p", + matter_device_pid, + "-vn", + matter_software_ver, + "-vs", + matter_software_ver_str, ] + if (defined(invoker.ota_digest)) { + args += [ + "-da", + matter_ota_digest, + ] + } else { + args += [ + "-da", + "sha256", + ] + } + if (defined(invoker.ota_args)) { + args += invoker.ota_args + } } } diff --git a/third_party/ti_simplelink_sdk/ti_simplelink_sdk.gni b/third_party/ti_simplelink_sdk/ti_simplelink_sdk.gni index 0d63fb6905b688..caf6d1ffeefdbd 100644 --- a/third_party/ti_simplelink_sdk/ti_simplelink_sdk.gni +++ b/third_party/ti_simplelink_sdk/ti_simplelink_sdk.gni @@ -466,5 +466,8 @@ template("ti_simplelink_sdk") { ":${sdk_target_name}_openthread_platform", ] } + if (defined(invoker.public_configs)) { + public_configs = invoker.public_configs + } } }