diff --git a/examples/lighting-app/nxp/k32w/k32w0/main/AppTask.cpp b/examples/lighting-app/nxp/k32w/k32w0/main/AppTask.cpp index 15097eb9d5b39a..45ab2fb1002130 100644 --- a/examples/lighting-app/nxp/k32w/k32w0/main/AppTask.cpp +++ b/examples/lighting-app/nxp/k32w/k32w0/main/AppTask.cpp @@ -35,10 +35,10 @@ /* OTA related includes */ #include "OTAImageProcessorImpl.h" +#include "OtaSupport.h" #include "platform/GenericOTARequestorDriver.h" #include "src/app/clusters/ota-requestor/BDXDownloader.h" #include "src/app/clusters/ota-requestor/OTARequestor.h" -#include "OtaSupport.h" #include "Keyboard.h" #include "LED.h" @@ -78,7 +78,8 @@ extern "C" void K32WUartProcess(void); using namespace ::chip::Credentials; using namespace ::chip::DeviceLayer; -using namespace chip;; +using namespace chip; +; AppTask AppTask::sAppTask; diff --git a/src/platform/nxp/k32w/k32w0/OTAImageProcessorImpl.cpp b/src/platform/nxp/k32w/k32w0/OTAImageProcessorImpl.cpp index ba0f696c67a4f8..f377ceba34906e 100644 --- a/src/platform/nxp/k32w/k32w0/OTAImageProcessorImpl.cpp +++ b/src/platform/nxp/k32w/k32w0/OTAImageProcessorImpl.cpp @@ -28,11 +28,11 @@ namespace chip { CHIP_ERROR OTAImageProcessorImpl::PrepareDownload() { - if (mParams.imageFile.empty()) - { + if (mParams.imageFile.empty()) + { ChipLogError(SoftwareUpdate, "Invalid output image file supplied"); return CHIP_ERROR_INTERNAL; - } + } DeviceLayer::PlatformMgr().ScheduleWork(HandlePrepareDownload, reinterpret_cast(this)); return CHIP_NO_ERROR; @@ -63,20 +63,20 @@ CHIP_ERROR OTAImageProcessorImpl::Abort() CHIP_ERROR OTAImageProcessorImpl::ProcessBlock(ByteSpan & block) { - if ((block.data() == nullptr) || block.empty()) - { - return CHIP_ERROR_INVALID_ARGUMENT; - } - - // Store block data for HandleProcessBlock to access - CHIP_ERROR err = SetBlock(block); - if (err != CHIP_NO_ERROR) - { - ChipLogError(SoftwareUpdate, "Cannot set block data: %" CHIP_ERROR_FORMAT, err.Format()); - } - - DeviceLayer::PlatformMgr().ScheduleWork(HandleProcessBlock, reinterpret_cast(this)); - return CHIP_NO_ERROR; + if ((block.data() == nullptr) || block.empty()) + { + return CHIP_ERROR_INVALID_ARGUMENT; + } + + // Store block data for HandleProcessBlock to access + CHIP_ERROR err = SetBlock(block); + if (err != CHIP_NO_ERROR) + { + ChipLogError(SoftwareUpdate, "Cannot set block data: %" CHIP_ERROR_FORMAT, err.Format()); + } + + DeviceLayer::PlatformMgr().ScheduleWork(HandleProcessBlock, reinterpret_cast(this)); + return CHIP_NO_ERROR; } void OTAImageProcessorImpl::HandlePrepareDownload(intptr_t context) @@ -129,9 +129,10 @@ void OTAImageProcessorImpl::HandleProcessBlock(intptr_t context) } /* Will start an erase of 4K if necessary */ - if (gOtaSuccess_c == OTA_MakeHeadRoomForNextBlock(imageProcessor->mBlock.size(), NULL, 0)) + if (gOtaSuccess_c == OTA_MakeHeadRoomForNextBlock(imageProcessor->mBlock.size(), NULL, 0)) { - if (gOtaSuccess_c == OTA_PushImageChunk (imageProcessor->mBlock.data(), (uint16_t)imageProcessor->mBlock.size(), NULL, NULL)) + if (gOtaSuccess_c == + OTA_PushImageChunk(imageProcessor->mBlock.data(), (uint16_t) imageProcessor->mBlock.size(), NULL, NULL)) { imageProcessor->mParams.downloadedBytes += imageProcessor->mBlock.size(); return; @@ -176,11 +177,11 @@ void OTAImageProcessorImpl::HandleFinalize(intptr_t context) } OTA_CommitImage(NULL); - if(OTA_ImageAuthenticate() == gOtaImageAuthPass_c) + if (OTA_ImageAuthenticate() == gOtaImageAuthPass_c) { /* Set the necessary information to inform the SSBL that a new image is available */ - OTA_SetNewImageFlag (); - ResetMCU (); + OTA_SetNewImageFlag(); + ResetMCU(); } imageProcessor->ReleaseBlock(); diff --git a/src/platform/nxp/k32w/k32w0/OTAImageProcessorImpl.h b/src/platform/nxp/k32w/k32w0/OTAImageProcessorImpl.h index 5b80b2fc735c13..05d6beb941e8ce 100644 --- a/src/platform/nxp/k32w/k32w0/OTAImageProcessorImpl.h +++ b/src/platform/nxp/k32w/k32w0/OTAImageProcessorImpl.h @@ -19,8 +19,8 @@ #pragma once #include -#include #include +#include namespace chip { diff --git a/third_party/nxp/k32w0_sdk/sdk_fixes/OtaUtils.c b/third_party/nxp/k32w0_sdk/sdk_fixes/OtaUtils.c index 1b278cfbae4b89..c38167dcad8a6d 100644 --- a/third_party/nxp/k32w0_sdk/sdk_fixes/OtaUtils.c +++ b/third_party/nxp/k32w0_sdk/sdk_fixes/OtaUtils.c @@ -1,13 +1,13 @@ /*! ********************************************************************************* -* Copyright 2020 NXP -* All rights reserved. -* -* \file -* -* This is the header file for the OTA Programming Support. -* -** SPDX-License-Identifier: BSD-3-Clause -********************************************************************************** */ + * Copyright 2020 NXP + * All rights reserved. + * + * \file + * + * This is the header file for the OTA Programming Support. + * + ** SPDX-License-Identifier: BSD-3-Clause + ********************************************************************************** */ /*! ********************************************************************************* ************************************************************************************* @@ -22,10 +22,10 @@ #include "fsl_sha.h" #include "flash_header.h" +#include "rom_aes.h" +#include "rom_api.h" #include "rom_psector.h" #include "rom_secure.h" -#include "rom_api.h" -#include "rom_aes.h" /************************************************************************************ ************************************************************************************* @@ -33,24 +33,24 @@ ************************************************************************************* ************************************************************************************/ -#define THUMB_ENTRY(x) (void*)((x) | 1) -#define CRC_FINALIZE(x) ((x) ^ ~0UL) +#define THUMB_ENTRY(x) (void *) ((x) | 1) +#define CRC_FINALIZE(x) ((x) ^ ~0UL) #ifdef PDM_EXT_FLASH -#define BOOT_BLOCK_OFFSET_MAX_VALUE 0x9de00 +#define BOOT_BLOCK_OFFSET_MAX_VALUE 0x9de00 #else -#define BOOT_BLOCK_OFFSET_MAX_VALUE 0x96000 +#define BOOT_BLOCK_OFFSET_MAX_VALUE 0x96000 #endif -#define SIGNATURE_WRD_LEN (SIGNATURE_LEN / 4) +#define SIGNATURE_WRD_LEN (SIGNATURE_LEN / 4) -#define ROM_API_efuse_LoadUniqueKey THUMB_ENTRY(0x030016f4) -#define ROM_API_aesLoadKeyFromOTP THUMB_ENTRY(0x0300146c) -#define ROM_API_crc_update THUMB_ENTRY(0x0300229c) -#define ROM_API_boot_CheckVectorSum THUMB_ENTRY(0x03000648) -#define ROM_API_flash_GetDmaccStatus THUMB_ENTRY(0x03001f64) +#define ROM_API_efuse_LoadUniqueKey THUMB_ENTRY(0x030016f4) +#define ROM_API_aesLoadKeyFromOTP THUMB_ENTRY(0x0300146c) +#define ROM_API_crc_update THUMB_ENTRY(0x0300229c) +#define ROM_API_boot_CheckVectorSum THUMB_ENTRY(0x03000648) +#define ROM_API_flash_GetDmaccStatus THUMB_ENTRY(0x03001f64) -#define BUFFER_SHA_LENGTH 16 +#define BUFFER_SHA_LENGTH 16 #define OTA_UTILS_DEBUG(...) /************************************************************************************ @@ -59,32 +59,35 @@ ************************************************************************************* ************************************************************************************/ -typedef struct { +typedef struct +{ IMAGE_CERT_T certificate; uint8_t signature[SIGNATURE_LEN]; } ImageCertificate_t; -typedef struct { +typedef struct +{ uint8_t signature[SIGNATURE_LEN]; } ImageSignature_t; -typedef struct { +typedef struct +{ uint16_t blob_id; uint32_t blob_version; } ImageCompatibilityListElem_t; typedef union { - IMG_HEADER_T imgHeader; - BOOT_BLOCK_T imgBootBlock; + IMG_HEADER_T imgHeader; + BOOT_BLOCK_T imgBootBlock; ImageCertificate_t imgCertificate; /* will contains only the img signature if no certificate is given */ } ImageParserUnion; typedef int (*efuse_LoadUniqueKey_t)(void); typedef uint32_t (*aesLoadKeyFromOTP_t)(AES_KEY_SIZE_T keySize); -typedef uint32_t (*crc_update_t)(uint32_t crc, const void* data, size_t data_len); -typedef uint32_t (*boot_CheckVectorSum_t)(const IMG_HEADER_T *image); -typedef uint32_t (*flash_GetDmaccStatus_t)(uint8_t *address); +typedef uint32_t (*crc_update_t)(uint32_t crc, const void * data, size_t data_len); +typedef uint32_t (*boot_CheckVectorSum_t)(const IMG_HEADER_T * image); +typedef uint32_t (*flash_GetDmaccStatus_t)(uint8_t * address); /************************************************************************************ ************************************************************************************* @@ -94,9 +97,9 @@ typedef uint32_t (*flash_GetDmaccStatus_t)(uint8_t *address); static const efuse_LoadUniqueKey_t efuse_LoadUniqueKey = (efuse_LoadUniqueKey_t) ROM_API_efuse_LoadUniqueKey; static const aesLoadKeyFromOTP_t aesLoadKeyFromOTP = (aesLoadKeyFromOTP_t) ROM_API_aesLoadKeyFromOTP; -static const crc_update_t crc_update = (crc_update_t)ROM_API_crc_update; -static const boot_CheckVectorSum_t boot_CheckVectorSum = (boot_CheckVectorSum_t)ROM_API_boot_CheckVectorSum; -static const flash_GetDmaccStatus_t flash_GetDmaccStatus = (flash_GetDmaccStatus_t) ROM_API_flash_GetDmaccStatus; +static const crc_update_t crc_update = (crc_update_t) ROM_API_crc_update; +static const boot_CheckVectorSum_t boot_CheckVectorSum = (boot_CheckVectorSum_t) ROM_API_boot_CheckVectorSum; +static const flash_GetDmaccStatus_t flash_GetDmaccStatus = (flash_GetDmaccStatus_t) ROM_API_flash_GetDmaccStatus; /****************************************************************************** ******************************************************************************* @@ -107,67 +110,65 @@ static const flash_GetDmaccStatus_t flash_GetDmaccStatus = (flash_GetDmaccStat static bool_t OtaUtils_IsInternalFlashAddr(uint32_t image_addr) { uint32_t internalFlashAddrStart = 0; - uint32_t internalFlashSize = 0; + uint32_t internalFlashSize = 0; ROM_GetFlash(&internalFlashAddrStart, &internalFlashSize); - return ((image_addr >= internalFlashAddrStart) - && image_addr < (internalFlashAddrStart+internalFlashSize)); + return ((image_addr >= internalFlashAddrStart) && image_addr < (internalFlashAddrStart + internalFlashSize)); } /* In case of wrong ImgType, IMG_TYPE_NB is returned */ -static uint8_t OtaUtils_CheckImageTypeFromImgHeader(const IMG_HEADER_T *pImageHeader) +static uint8_t OtaUtils_CheckImageTypeFromImgHeader(const IMG_HEADER_T * pImageHeader) { uint8_t imgType = IMG_DIRECTORY_MAX_SIZE; - if (pImageHeader && pImageHeader->imageSignature >= IMAGE_SIGNATURE - && pImageHeader->imageSignature < IMAGE_SIGNATURE + IMG_DIRECTORY_MAX_SIZE) + if (pImageHeader && pImageHeader->imageSignature >= IMAGE_SIGNATURE && + pImageHeader->imageSignature < IMAGE_SIGNATURE + IMG_DIRECTORY_MAX_SIZE) { imgType = (pImageHeader->imageSignature - IMAGE_SIGNATURE); } return imgType; } -static otaUtilsResult_t OtaUtils_ReadFromEncryptedExtFlash(uint16_t nbBytesToRead, - uint32_t address, - uint8_t *pOutbuf, - OtaUtils_EEPROM_ReadData pFunctionEepromRead, - eEncryptionKeyType eType, - void *pParam) +static otaUtilsResult_t OtaUtils_ReadFromEncryptedExtFlash(uint16_t nbBytesToRead, uint32_t address, uint8_t * pOutbuf, + OtaUtils_EEPROM_ReadData pFunctionEepromRead, eEncryptionKeyType eType, + void * pParam) { - otaUtilsResult_t result = gOtaUtilsError_c; + otaUtilsResult_t result = gOtaUtilsError_c; otaUtilsResult_t readResult = gOtaUtilsSuccess_c; uint8_t alignedBufferStart[16]; uint8_t alignedBufferEnd[16]; uint16_t nbByteToRead = nbBytesToRead; - uint8_t *pBuf = pOutbuf; + uint8_t * pBuf = pOutbuf; - uint32_t lastAddrToRead = address+nbBytesToRead-1; + uint32_t lastAddrToRead = address + nbBytesToRead - 1; /* Encrypted reads require to have an addr aligned on 16 bytes */ - uint16_t nbByteToAlignStart = address % 16; - uint16_t nbByteToAlignEnd = (16*(lastAddrToRead/16) + 15) - lastAddrToRead; + uint16_t nbByteToAlignStart = address % 16; + uint16_t nbByteToAlignEnd = (16 * (lastAddrToRead / 16) + 15) - lastAddrToRead; uint16_t nbByteToMoveInAlignedBufferStart = 0; - uint16_t nbByteToMoveInAlignedBufferEnd = 0; + uint16_t nbByteToMoveInAlignedBufferEnd = 0; uint16_t nbByteToReadBeforeEndAlignBuffer = 0; address -= nbByteToAlignStart; - do { + do + { #ifdef DEBUG - if ((nbByteToRead + nbByteToAlignStart + nbByteToAlignEnd)%16 != 0) + if ((nbByteToRead + nbByteToAlignStart + nbByteToAlignEnd) % 16 != 0) break; #endif /* Get the number of block that we will need to read */ - int nb_blocks = (nbByteToRead + nbByteToAlignStart + nbByteToAlignEnd)/16; + int nb_blocks = (nbByteToRead + nbByteToAlignStart + nbByteToAlignEnd) / 16; if (nbByteToAlignStart) { - if ((readResult=pFunctionEepromRead(sizeof(alignedBufferStart), address, &alignedBufferStart[0])) != gOtaUtilsSuccess_c) + if ((readResult = pFunctionEepromRead(sizeof(alignedBufferStart), address, &alignedBufferStart[0])) != + gOtaUtilsSuccess_c) { result = readResult; break; } else { - address+= sizeof(alignedBufferStart); + address += sizeof(alignedBufferStart); } } @@ -185,15 +186,16 @@ static otaUtilsResult_t OtaUtils_ReadFromEncryptedExtFlash(uint16_t nbBytesToRea nbByteToMoveInAlignedBufferEnd = sizeof(alignedBufferEnd) - nbByteToAlignEnd; } nbByteToReadBeforeEndAlignBuffer = nbByteToRead - nbByteToMoveInAlignedBufferStart - nbByteToMoveInAlignedBufferEnd; - if (nbByteToReadBeforeEndAlignBuffer%16 != 0) + if (nbByteToReadBeforeEndAlignBuffer % 16 != 0) break; - if ((readResult=pFunctionEepromRead(nbByteToReadBeforeEndAlignBuffer, address, pBuf)) != gOtaUtilsSuccess_c) + if ((readResult = pFunctionEepromRead(nbByteToReadBeforeEndAlignBuffer, address, pBuf)) != gOtaUtilsSuccess_c) { result = readResult; break; } address += nbByteToReadBeforeEndAlignBuffer; - if (nbByteToAlignEnd && (readResult=pFunctionEepromRead(sizeof(alignedBufferEnd), address, alignedBufferEnd)) != gOtaUtilsSuccess_c) + if (nbByteToAlignEnd && + (readResult = pFunctionEepromRead(sizeof(alignedBufferEnd), address, alignedBufferEnd)) != gOtaUtilsSuccess_c) { result = readResult; break; @@ -207,41 +209,41 @@ static otaUtilsResult_t OtaUtils_ReadFromEncryptedExtFlash(uint16_t nbBytesToRea if (eType == eEfuseKey) { - //aesInit(); + // aesInit(); efuse_LoadUniqueKey(); aesLoadKeyFromOTP(AES_KEY_128BITS); } else if (eType == eSoftwareKey && pParam != NULL) { sOtaUtilsSoftwareKey * pSoftKey = (sOtaUtilsSoftwareKey *) pParam; - aesLoadKeyFromSW(AES_KEY_128BITS, (uint32_t*)pSoftKey->pSoftKeyAes); + aesLoadKeyFromSW(AES_KEY_128BITS, (uint32_t *) pSoftKey->pSoftKeyAes); break; } aesMode(AES_MODE_ECB_DECRYPT, AES_INT_BSWAP | AES_OUTT_BSWAP); if (nbByteToAlignStart) { - aesProcess((void*)alignedBufferStart, (void*)alignedBufferStart, 1); - nb_blocks -=1; + aesProcess((void *) alignedBufferStart, (void *) alignedBufferStart, 1); + nb_blocks -= 1; } if (nbByteToAlignEnd) { - aesProcess((void*)pBuf, (void*)pBuf, nb_blocks-1); - aesProcess((void*)alignedBufferEnd, (void*)alignedBufferEnd, 1); + aesProcess((void *) pBuf, (void *) pBuf, nb_blocks - 1); + aesProcess((void *) alignedBufferEnd, (void *) alignedBufferEnd, 1); } else { - aesProcess((void*)pBuf, (void*)pBuf, nb_blocks); + aesProcess((void *) pBuf, (void *) pBuf, nb_blocks); } /* Fill missing pBuf bytes */ - pBuf-=nbByteToMoveInAlignedBufferStart; + pBuf -= nbByteToMoveInAlignedBufferStart; if (nbByteToAlignStart) { uint16_t i; - uint16_t t=0; - for (i=nbByteToAlignStart; iAHBCLKCTRLSET[1] = SYSCON_AHBCLKCTRL1_HASH_MASK; if (SHA_Init(SHA0, &hash_ctx, kSHA_Sha256) != kStatus_Success) break; - for (i=0; iAHBCLKCTRLCLR[1] = SYSCON_AHBCLKCTRL1_HASH_MASK; /* equivalent to SHA_ClkDeinit(SHA0) */ + SYSCON->AHBCLKCTRLCLR[1] = SYSCON_AHBCLKCTRL1_HASH_MASK; /* equivalent to SHA_ClkDeinit(SHA0) */ return result; } static bool_t OtaUtils_FindBlankPage(uint32_t startAddr, uint16_t size) { - bool_t result = FALSE; - uint32_t addrIterator = startAddr; - + bool_t result = FALSE; + uint32_t addrIterator = startAddr; - while (addrIterator < startAddr+size) - { - if (flash_GetDmaccStatus((uint8_t *)addrIterator) == 0) - { - result = TRUE; - break; - } - addrIterator += FLASH_PAGE_SIZE; - } + while (addrIterator < startAddr + size) + { + if (flash_GetDmaccStatus((uint8_t *) addrIterator) == 0) + { + result = TRUE; + break; + } + addrIterator += FLASH_PAGE_SIZE; + } - /* Check the endAddr */ - if (!result && flash_GetDmaccStatus((uint8_t *)startAddr+size) == 0) - result = TRUE; + /* Check the endAddr */ + if (!result && flash_GetDmaccStatus((uint8_t *) startAddr + size) == 0) + result = TRUE; - return result; + return result; } /****************************************************************************** @@ -334,15 +334,13 @@ static bool_t OtaUtils_FindBlankPage(uint32_t startAddr, uint16_t size) ******************************************************************************* ******************************************************************************/ -otaUtilsResult_t OtaUtils_ReadFromInternalFlash(uint16_t nbBytesToRead, - uint32_t address, - uint8_t *pOutbuf, - void *pParam, +otaUtilsResult_t OtaUtils_ReadFromInternalFlash(uint16_t nbBytesToRead, uint32_t address, uint8_t * pOutbuf, void * pParam, OtaUtils_EEPROM_ReadData pFunctionEepromRead) { otaUtilsResult_t result = gOtaUtilsError_c; - do { + do + { if (!OtaUtils_IsInternalFlashAddr(address)) break; /* If one blank page is found return error */ @@ -357,12 +355,8 @@ otaUtilsResult_t OtaUtils_ReadFromInternalFlash(uint16_t nbBytesToRead, return result; } - -otaUtilsResult_t OtaUtils_ReadFromUnencryptedExtFlash(uint16_t nbBytesToRead, - uint32_t address, - uint8_t *pOutbuf, - void *pParam, - OtaUtils_EEPROM_ReadData pFunctionEepromRead) +otaUtilsResult_t OtaUtils_ReadFromUnencryptedExtFlash(uint16_t nbBytesToRead, uint32_t address, uint8_t * pOutbuf, void * pParam, + OtaUtils_EEPROM_ReadData pFunctionEepromRead) { otaUtilsResult_t result = gOtaUtilsError_c; if (pFunctionEepromRead != NULL) @@ -372,44 +366,35 @@ otaUtilsResult_t OtaUtils_ReadFromUnencryptedExtFlash(uint16_t nbBytesToRead, return result; } -otaUtilsResult_t OtaUtils_ReadFromEncryptedExtFlashEfuseKey(uint16_t nbBytesToRead, - uint32_t address, - uint8_t *pOutbuf, - void *pParam, - OtaUtils_EEPROM_ReadData pFunctionEepromRead) +otaUtilsResult_t OtaUtils_ReadFromEncryptedExtFlashEfuseKey(uint16_t nbBytesToRead, uint32_t address, uint8_t * pOutbuf, + void * pParam, OtaUtils_EEPROM_ReadData pFunctionEepromRead) { return OtaUtils_ReadFromEncryptedExtFlash(nbBytesToRead, address, pOutbuf, pFunctionEepromRead, eEfuseKey, NULL); } -otaUtilsResult_t OtaUtils_ReadFromEncryptedExtFlashSoftwareKey(uint16_t nbBytesToRead, - uint32_t address, - uint8_t *pOutbuf, - void *pParam, - OtaUtils_EEPROM_ReadData pFunctionEepromRead) +otaUtilsResult_t OtaUtils_ReadFromEncryptedExtFlashSoftwareKey(uint16_t nbBytesToRead, uint32_t address, uint8_t * pOutbuf, + void * pParam, OtaUtils_EEPROM_ReadData pFunctionEepromRead) { return OtaUtils_ReadFromEncryptedExtFlash(nbBytesToRead, address, pOutbuf, pFunctionEepromRead, eSoftwareKey, pParam); } - -uint32_t OtaUtils_ValidateImage(OtaUtils_ReadBytes pFunctionRead, - void *pReadFunctionParam, - OtaUtils_EEPROM_ReadData pFunctionEepromRead, - uint32_t imgAddr, - uint32_t minValidAddr, - const IMAGE_CERT_T * pRootCert, - bool_t inOtaCheck, bool_t isRemappable) +uint32_t OtaUtils_ValidateImage(OtaUtils_ReadBytes pFunctionRead, void * pReadFunctionParam, + OtaUtils_EEPROM_ReadData pFunctionEepromRead, uint32_t imgAddr, uint32_t minValidAddr, + const IMAGE_CERT_T * pRootCert, bool_t inOtaCheck, bool_t isRemappable) { uint32_t result_addr = OTA_UTILS_IMAGE_INVALID_ADDR; ImageParserUnion uImgParser; uint32_t headerBootBlockMarker = 0; - uint32_t runAddr = 0; - uint8_t imgType = IMG_DIRECTORY_MAX_SIZE; - uint32_t bootBlockOffsetFound = 0; - uint32_t imgSizeFound = 0; + uint32_t runAddr = 0; + uint8_t imgType = IMG_DIRECTORY_MAX_SIZE; + uint32_t bootBlockOffsetFound = 0; + uint32_t imgSizeFound = 0; - do { + do + { /* Try to extract the imageHeader */ - if (pFunctionRead(sizeof(IMG_HEADER_T), imgAddr, (uint8_t *)&uImgParser.imgHeader, pReadFunctionParam, pFunctionEepromRead) != gOtaUtilsSuccess_c) + if (pFunctionRead(sizeof(IMG_HEADER_T), imgAddr, (uint8_t *) &uImgParser.imgHeader, pReadFunctionParam, + pFunctionEepromRead) != gOtaUtilsSuccess_c) break; imgType = OtaUtils_CheckImageTypeFromImgHeader(&uImgParser.imgHeader); @@ -430,44 +415,54 @@ uint32_t OtaUtils_ValidateImage(OtaUtils_ReadBytes pFunctionRead, if (!inOtaCheck) { - if (runAddr != imgAddr) break; + if (runAddr != imgAddr) + break; } - if (uImgParser.imgHeader.bootBlockOffset % sizeof(uint32_t) ) break; + if (uImgParser.imgHeader.bootBlockOffset % sizeof(uint32_t)) + break; - if (uImgParser.imgHeader.bootBlockOffset + sizeof(BOOT_BLOCK_T) >= BOOT_BLOCK_OFFSET_MAX_VALUE) break; + if (uImgParser.imgHeader.bootBlockOffset + sizeof(BOOT_BLOCK_T) >= BOOT_BLOCK_OFFSET_MAX_VALUE) + break; /* compute CRC of the header */ uint32_t crc = ~0UL; - crc = crc_update(crc, &uImgParser.imgHeader, sizeof(IMG_HEADER_T)-sizeof(uImgParser.imgHeader.header_crc)); - crc = CRC_FINALIZE(crc); + crc = crc_update(crc, &uImgParser.imgHeader, sizeof(IMG_HEADER_T) - sizeof(uImgParser.imgHeader.header_crc)); + crc = CRC_FINALIZE(crc); - if (uImgParser.imgHeader.header_crc != crc) break; + if (uImgParser.imgHeader.header_crc != crc) + break; - if (boot_CheckVectorSum(&uImgParser.imgHeader) != 0) break; + if (boot_CheckVectorSum(&uImgParser.imgHeader) != 0) + break; /* Save data before parsing the bootblock */ bootBlockOffsetFound = uImgParser.imgHeader.bootBlockOffset; /* Try to extract the bootblock */ - if (pFunctionRead(sizeof(BOOT_BLOCK_T), bootBlockOffsetFound + imgAddr, (uint8_t *)&uImgParser.imgBootBlock, pReadFunctionParam, pFunctionEepromRead) != gOtaUtilsSuccess_c) + if (pFunctionRead(sizeof(BOOT_BLOCK_T), bootBlockOffsetFound + imgAddr, (uint8_t *) &uImgParser.imgBootBlock, + pReadFunctionParam, pFunctionEepromRead) != gOtaUtilsSuccess_c) break; headerBootBlockMarker = uImgParser.imgBootBlock.header_marker; - if (!( (headerBootBlockMarker >= BOOT_BLOCK_HDR_MARKER) && - (headerBootBlockMarker <= BOOT_BLOCK_HDR_MARKER+2))) break; + if (!((headerBootBlockMarker >= BOOT_BLOCK_HDR_MARKER) && (headerBootBlockMarker <= BOOT_BLOCK_HDR_MARKER + 2))) + break; if (!inOtaCheck) { - if (uImgParser.imgBootBlock.target_addr != runAddr) break; + if (uImgParser.imgBootBlock.target_addr != runAddr) + break; } else { runAddr = uImgParser.imgBootBlock.target_addr; } - if (runAddr < minValidAddr) break; - if (uImgParser.imgBootBlock.stated_size < (bootBlockOffsetFound + sizeof(BOOT_BLOCK_T))) break; - if (uImgParser.imgBootBlock.img_len > uImgParser.imgBootBlock.stated_size) break; + if (runAddr < minValidAddr) + break; + if (uImgParser.imgBootBlock.stated_size < (bootBlockOffsetFound + sizeof(BOOT_BLOCK_T))) + break; + if (uImgParser.imgBootBlock.img_len > uImgParser.imgBootBlock.stated_size) + break; if (uImgParser.imgBootBlock.compatibility_offset != 0) { @@ -476,10 +471,12 @@ uint32_t OtaUtils_ValidateImage(OtaUtils_ReadBytes pFunctionRead, if (uImgParser.imgBootBlock.compatibility_offset < (bootBlockOffsetFound - sizeof(uint32_t))) { /* Try to read the compatibility list size */ - if (pFunctionRead(sizeof(uint32_t), imgAddr + uImgParser.imgBootBlock.compatibility_offset, (uint8_t *)&compatibility_list_sz, pReadFunctionParam, pFunctionEepromRead) != gOtaUtilsSuccess_c) + if (pFunctionRead(sizeof(uint32_t), imgAddr + uImgParser.imgBootBlock.compatibility_offset, + (uint8_t *) &compatibility_list_sz, pReadFunctionParam, + pFunctionEepromRead) != gOtaUtilsSuccess_c) break; - if (uImgParser.imgBootBlock.compatibility_offset != bootBlockOffsetFound - - (sizeof(uint32_t) + compatibility_list_sz *sizeof(ImageCompatibilityListElem_t)) ) + if (uImgParser.imgBootBlock.compatibility_offset != + bootBlockOffsetFound - (sizeof(uint32_t) + compatibility_list_sz * sizeof(ImageCompatibilityListElem_t))) break; } else @@ -493,7 +490,7 @@ uint32_t OtaUtils_ValidateImage(OtaUtils_ReadBytes pFunctionRead, if (pRootCert != NULL) { uint32_t imgSignatureOffset = bootBlockOffsetFound + sizeof(BOOT_BLOCK_T); - const uint32_t *pKey = (const uint32_t *)&pRootCert->public_key[0]; + const uint32_t * pKey = (const uint32_t *) &pRootCert->public_key[0]; OTA_UTILS_DEBUG("==> Img authentication is enabled\n"); if (uImgParser.imgBootBlock.certificate_offset != 0) { @@ -503,24 +500,28 @@ uint32_t OtaUtils_ValidateImage(OtaUtils_ReadBytes pFunctionRead, break; /* If there is a certificate is must comply with the expectations */ /* There must be a trailing ImageAuthTrailer_t appended to boot block */ - if (pFunctionRead(sizeof(ImageCertificate_t), imgAddr + uImgParser.imgBootBlock.certificate_offset, (uint8_t *)&uImgParser.imgCertificate, pReadFunctionParam, pFunctionEepromRead) != gOtaUtilsSuccess_c) + if (pFunctionRead(sizeof(ImageCertificate_t), imgAddr + uImgParser.imgBootBlock.certificate_offset, + (uint8_t *) &uImgParser.imgCertificate, pReadFunctionParam, + pFunctionEepromRead) != gOtaUtilsSuccess_c) break; if (uImgParser.imgCertificate.certificate.certificate_marker != CERTIFICATE_MARKER) break; /* Accesses to certificate header, certificate signature and image signature fields * indirectly allow their correct presence via the Bus Fault TRY-CATCH. */ - if (uImgParser.imgCertificate.certificate.public_key[0] == uImgParser.imgCertificate.certificate.public_key[SIGNATURE_WRD_LEN-1]) + if (uImgParser.imgCertificate.certificate.public_key[0] == + uImgParser.imgCertificate.certificate.public_key[SIGNATURE_WRD_LEN - 1]) break; - const uint32_t * cert_sign = (uint32_t*)&uImgParser.imgCertificate.signature[0]; - if (cert_sign[0] == cert_sign[SIGNATURE_WRD_LEN-1]) + const uint32_t * cert_sign = (uint32_t *) &uImgParser.imgCertificate.signature[0]; + if (cert_sign[0] == cert_sign[SIGNATURE_WRD_LEN - 1]) { break; } /* Check the signature of the certificate */ - if (!secure_VerifyCertificate(&uImgParser.imgCertificate.certificate, pKey, &uImgParser.imgCertificate.signature[0])) + if (!secure_VerifyCertificate(&uImgParser.imgCertificate.certificate, pKey, + &uImgParser.imgCertificate.signature[0])) break; - pKey = (const uint32_t *)&uImgParser.imgCertificate.certificate.public_key[0]; + pKey = (const uint32_t *) &uImgParser.imgCertificate.certificate.public_key[0]; imgSignatureOffset += sizeof(ImageCertificate_t); } else if (imgSignatureOffset != imgSizeFound) @@ -529,15 +530,17 @@ uint32_t OtaUtils_ValidateImage(OtaUtils_ReadBytes pFunctionRead, } OTA_UTILS_DEBUG("Img signature found\n"); /* Read the img signature */ - if (pFunctionRead(sizeof(ImageSignature_t), imgAddr + imgSignatureOffset, (uint8_t *)&uImgParser.imgCertificate.signature, pReadFunctionParam, pFunctionEepromRead) != gOtaUtilsSuccess_c) - break; + if (pFunctionRead(sizeof(ImageSignature_t), imgAddr + imgSignatureOffset, + (uint8_t *) &uImgParser.imgCertificate.signature, pReadFunctionParam, + pFunctionEepromRead) != gOtaUtilsSuccess_c) + break; - const uint8_t * img_sign = (uint8_t*)&uImgParser.imgCertificate.signature[0]; - if (img_sign[0] == img_sign[SIGNATURE_WRD_LEN-1]) + const uint8_t * img_sign = (uint8_t *) &uImgParser.imgCertificate.signature[0]; + if (img_sign[0] == img_sign[SIGNATURE_WRD_LEN - 1]) break; - if (!OtaUtils_VerifySignature(imgAddr, imgSignatureOffset, pKey, img_sign, pFunctionRead, - pReadFunctionParam, pFunctionEepromRead)) + if (!OtaUtils_VerifySignature(imgAddr, imgSignatureOffset, pKey, img_sign, pFunctionRead, pReadFunctionParam, + pFunctionEepromRead)) break; } result_addr = runAddr; @@ -547,7 +550,8 @@ uint32_t OtaUtils_ValidateImage(OtaUtils_ReadBytes pFunctionRead, return result_addr; } -otaUtilsResult_t OtaUtils_ReconstructRootCert(IMAGE_CERT_T *pCert, const psector_page_data_t* pPage0, const psector_page_data_t* pFlashPage) +otaUtilsResult_t OtaUtils_ReconstructRootCert(IMAGE_CERT_T * pCert, const psector_page_data_t * pPage0, + const psector_page_data_t * pFlashPage) { otaUtilsResult_t result = gOtaUtilsError_c; uint32_t keyValid; @@ -566,7 +570,7 @@ otaUtilsResult_t OtaUtils_ReconstructRootCert(IMAGE_CERT_T *pCert, const psector efuse_LoadUniqueKey(); aesLoadKeyFromOTP(AES_KEY_128BITS); aesMode(AES_MODE_ECB_DECRYPT, AES_INT_BSWAP | AES_OUTT_BSWAP); - aesProcess((uint32_t*)&pPage0->page0_v3.image_pubkey[0], &pCert->public_key[0], SIGNATURE_LEN/16); + aesProcess((uint32_t *) &pPage0->page0_v3.image_pubkey[0], &pCert->public_key[0], SIGNATURE_LEN / 16); if (pFlashPage != NULL) { @@ -576,15 +580,15 @@ otaUtilsResult_t OtaUtils_ReconstructRootCert(IMAGE_CERT_T *pCert, const psector } else { - pCert->customer_id = psector_Read_CustomerId(); + pCert->customer_id = psector_Read_CustomerId(); pCert->min_device_id = psector_Read_MinDeviceId(); pCert->max_device_id = psector_Read_MaxDeviceId(); } pCert->certificate_marker = CERTIFICATE_MARKER; - pCert->certificate_id = 0UL; - pCert->usage_flags = 0UL; - result = gOtaUtilsSuccess_c; + pCert->certificate_id = 0UL; + pCert->usage_flags = 0UL; + result = gOtaUtilsSuccess_c; } while (0); return result; }