From ee3cceaebb62ea640aef0a8fe2f5a3c1d2ee1ce2 Mon Sep 17 00:00:00 2001 From: doru91 Date: Thu, 13 Jan 2022 16:11:16 +0200 Subject: [PATCH] [K32W0] Refactor patching system (#13410) Signed-off-by: Doru Gucea --- .../nxp/k32w0_sdk/sdk_fixes/OtaUtils.c | 594 ------------ .../nxp/k32w0_sdk/sdk_fixes/OtaUtils_c.patch | 19 + third_party/nxp/k32w0_sdk/sdk_fixes/SecLib.h | 896 ------------------ .../nxp/k32w0_sdk/sdk_fixes/SecLib_h.patch | 24 + .../sdk_fixes/app_dual_mode_low_power.h | 62 -- .../sdk_fixes/app_dual_mode_low_power_h.patch | 24 + .../sdk_fixes/app_dual_mode_switch.h | 107 --- .../sdk_fixes/app_dual_mode_switch_h.patch | 21 + ...ch_ble_utils_h.patch => ble_utils_h.patch} | 6 +- .../nxp/k32w0_sdk/sdk_fixes/gpio_pins.h | 98 -- .../nxp/k32w0_sdk/sdk_fixes/gpio_pins_h.patch | 14 + .../nxp/k32w0_sdk/sdk_fixes/patch_k32w_sdk.sh | 27 +- third_party/nxp/k32w0_sdk/sdk_fixes/pin_mux.c | 201 ---- 13 files changed, 123 insertions(+), 1970 deletions(-) delete mode 100644 third_party/nxp/k32w0_sdk/sdk_fixes/OtaUtils.c create mode 100644 third_party/nxp/k32w0_sdk/sdk_fixes/OtaUtils_c.patch delete mode 100644 third_party/nxp/k32w0_sdk/sdk_fixes/SecLib.h create mode 100644 third_party/nxp/k32w0_sdk/sdk_fixes/SecLib_h.patch delete mode 100644 third_party/nxp/k32w0_sdk/sdk_fixes/app_dual_mode_low_power.h create mode 100644 third_party/nxp/k32w0_sdk/sdk_fixes/app_dual_mode_low_power_h.patch delete mode 100644 third_party/nxp/k32w0_sdk/sdk_fixes/app_dual_mode_switch.h create mode 100644 third_party/nxp/k32w0_sdk/sdk_fixes/app_dual_mode_switch_h.patch rename third_party/nxp/k32w0_sdk/sdk_fixes/{patch_ble_utils_h.patch => ble_utils_h.patch} (96%) delete mode 100644 third_party/nxp/k32w0_sdk/sdk_fixes/gpio_pins.h create mode 100644 third_party/nxp/k32w0_sdk/sdk_fixes/gpio_pins_h.patch delete mode 100644 third_party/nxp/k32w0_sdk/sdk_fixes/pin_mux.c diff --git a/third_party/nxp/k32w0_sdk/sdk_fixes/OtaUtils.c b/third_party/nxp/k32w0_sdk/sdk_fixes/OtaUtils.c deleted file mode 100644 index c38167dcad8a6d..00000000000000 --- a/third_party/nxp/k32w0_sdk/sdk_fixes/OtaUtils.c +++ /dev/null @@ -1,594 +0,0 @@ -/*! ********************************************************************************* - * Copyright 2020 NXP - * All rights reserved. - * - * \file - * - * This is the header file for the OTA Programming Support. - * - ** SPDX-License-Identifier: BSD-3-Clause - ********************************************************************************** */ - -/*! ********************************************************************************* -************************************************************************************* -* Include -************************************************************************************* -********************************************************************************** */ - -#include "OtaUtils.h" - -/* Driver includes */ -#include "fsl_flash.h" -#include "fsl_sha.h" - -#include "flash_header.h" -#include "rom_aes.h" -#include "rom_api.h" -#include "rom_psector.h" -#include "rom_secure.h" - -/************************************************************************************ -************************************************************************************* -* Private Macros -************************************************************************************* -************************************************************************************/ - -#define THUMB_ENTRY(x) (void *) ((x) | 1) -#define CRC_FINALIZE(x) ((x) ^ ~0UL) - -#ifdef PDM_EXT_FLASH -#define BOOT_BLOCK_OFFSET_MAX_VALUE 0x9de00 -#else -#define BOOT_BLOCK_OFFSET_MAX_VALUE 0x96000 -#endif - -#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 BUFFER_SHA_LENGTH 16 -#define OTA_UTILS_DEBUG(...) - -/************************************************************************************ -************************************************************************************* -* Private definitions -************************************************************************************* -************************************************************************************/ - -typedef struct -{ - IMAGE_CERT_T certificate; - uint8_t signature[SIGNATURE_LEN]; -} ImageCertificate_t; - -typedef struct -{ - uint8_t signature[SIGNATURE_LEN]; -} ImageSignature_t; - -typedef struct -{ - uint16_t blob_id; - uint32_t blob_version; -} ImageCompatibilityListElem_t; - -typedef union -{ - 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); - -/************************************************************************************ -************************************************************************************* -* Private memory declarations -************************************************************************************* -************************************************************************************/ - -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; - -/****************************************************************************** -******************************************************************************* -* Private functions -******************************************************************************* -******************************************************************************/ - -static bool_t OtaUtils_IsInternalFlashAddr(uint32_t image_addr) -{ - uint32_t internalFlashAddrStart = 0; - uint32_t internalFlashSize = 0; - ROM_GetFlash(&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) -{ - uint8_t imgType = 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) -{ - 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; - - 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 nbByteToMoveInAlignedBufferStart = 0; - uint16_t nbByteToMoveInAlignedBufferEnd = 0; - uint16_t nbByteToReadBeforeEndAlignBuffer = 0; - - address -= nbByteToAlignStart; - - do - { -#ifdef DEBUG - 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; - - if (nbByteToAlignStart) - { - if ((readResult = pFunctionEepromRead(sizeof(alignedBufferStart), address, &alignedBufferStart[0])) != - gOtaUtilsSuccess_c) - { - result = readResult; - break; - } - else - { - address += sizeof(alignedBufferStart); - } - } - - /* Check if we need to read more bytes */ - if (address < lastAddrToRead) - { - if (nbByteToAlignStart) - { - nbByteToMoveInAlignedBufferStart = sizeof(alignedBufferStart) - nbByteToAlignStart; - pBuf += nbByteToMoveInAlignedBufferStart; - } - - if (nbByteToAlignEnd) - { - nbByteToMoveInAlignedBufferEnd = sizeof(alignedBufferEnd) - nbByteToAlignEnd; - } - nbByteToReadBeforeEndAlignBuffer = nbByteToRead - nbByteToMoveInAlignedBufferStart - nbByteToMoveInAlignedBufferEnd; - if (nbByteToReadBeforeEndAlignBuffer % 16 != 0) - break; - if ((readResult = pFunctionEepromRead(nbByteToReadBeforeEndAlignBuffer, address, pBuf)) != gOtaUtilsSuccess_c) - { - result = readResult; - break; - } - address += nbByteToReadBeforeEndAlignBuffer; - if (nbByteToAlignEnd && - (readResult = pFunctionEepromRead(sizeof(alignedBufferEnd), address, alignedBufferEnd)) != gOtaUtilsSuccess_c) - { - result = readResult; - break; - } - } - else - { - /* The asked buffer is too small and can fit in alignedBufferStart */ - nbByteToAlignEnd = 0; - } - - if (eType == eEfuseKey) - { - // 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); - break; - } - - aesMode(AES_MODE_ECB_DECRYPT, AES_INT_BSWAP | AES_OUTT_BSWAP); - if (nbByteToAlignStart) - { - 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); - } - else - { - aesProcess((void *) pBuf, (void *) pBuf, nb_blocks); - } - - /* Fill missing pBuf bytes */ - pBuf -= nbByteToMoveInAlignedBufferStart; - - if (nbByteToAlignStart) - { - uint16_t i; - uint16_t t = 0; - for (i = nbByteToAlignStart; i < sizeof(alignedBufferStart); i++) - { - pBuf[t++] = alignedBufferStart[i]; - } - } - - if (nbByteToAlignEnd) - { - uint16_t i; - for (i = 0; i < nbByteToMoveInAlignedBufferEnd; i++) - { - *(pBuf + nbByteToMoveInAlignedBufferStart + nbByteToReadBeforeEndAlignBuffer + i) = alignedBufferEnd[i]; - } - } - result = gOtaUtilsSuccess_c; - } while (0); - - return result; -} - -static bool_t OtaUtils_VerifySignature(uint32_t address, uint32_t nbBytesToRead, const uint32_t * pPublicKey, - const uint8_t * pSignatureToVerify, OtaUtils_ReadBytes pFunctionRead, - void * pReadFunctionParam, OtaUtils_EEPROM_ReadData pFunctionEepromRead) -{ - bool_t result = FALSE; - uint32_t nbPageToRead = nbBytesToRead / BUFFER_SHA_LENGTH; - uint32_t lastPageNbByteNumber = nbBytesToRead - (nbPageToRead * BUFFER_SHA_LENGTH); - uint32_t i = 0; - do - { - uint8_t pageContent[BUFFER_SHA_LENGTH]; - uint8_t digest[32]; - sha_ctx_t hash_ctx; - size_t sha_sz = 32; - /* Initialise SHA clock do not call SHA_ClkInit(SHA0) because the HAL pulls in too much code */ - SYSCON->AHBCLKCTRLSET[1] = SYSCON_AHBCLKCTRL1_HASH_MASK; - if (SHA_Init(SHA0, &hash_ctx, kSHA_Sha256) != kStatus_Success) - break; - for (i = 0; i < nbPageToRead; i++) - { - if (pFunctionRead(sizeof(pageContent), address + (i * BUFFER_SHA_LENGTH), &pageContent[0], pReadFunctionParam, - pFunctionEepromRead) != gOtaUtilsSuccess_c) - break; - if (SHA_Update(SHA0, &hash_ctx, (const uint8_t *) pageContent, BUFFER_SHA_LENGTH) != kStatus_Success) - break; - } - /* Read bytes located on the last page */ - if (pFunctionRead(lastPageNbByteNumber, address + (i * BUFFER_SHA_LENGTH), &pageContent[0], pReadFunctionParam, - pFunctionEepromRead) != gOtaUtilsSuccess_c) - break; - if (SHA_Update(SHA0, &hash_ctx, (const uint8_t *) pageContent, lastPageNbByteNumber) != kStatus_Success) - break; - if (SHA_Finish(SHA0, &hash_ctx, &digest[0], &sha_sz) != kStatus_Success) - break; - if (!secure_VerifySignature(digest, pSignatureToVerify, pPublicKey)) - break; - result = TRUE; - } while (0); - - 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; - - 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; - - return result; -} - -/****************************************************************************** -******************************************************************************* -* Public functions -******************************************************************************* -******************************************************************************/ - -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 - { - if (!OtaUtils_IsInternalFlashAddr(address)) - break; - /* If one blank page is found return error */ - if (OtaUtils_FindBlankPage(address, nbBytesToRead)) - break; - if (pFunctionEepromRead == NULL || pOutbuf == NULL) - break; - pFunctionEepromRead(nbBytesToRead, address, pOutbuf); - result = gOtaUtilsSuccess_c; - } while (0); - - return result; -} - -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) - { - result = pFunctionEepromRead(nbBytesToRead, address, pOutbuf); - } - return result; -} - -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) -{ - 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 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; - - do - { - /* Try to extract the imageHeader */ - if (pFunctionRead(sizeof(IMG_HEADER_T), imgAddr, (uint8_t *) &uImgParser.imgHeader, pReadFunctionParam, - pFunctionEepromRead) != gOtaUtilsSuccess_c) - break; - - imgType = OtaUtils_CheckImageTypeFromImgHeader(&uImgParser.imgHeader); - if (imgType == IMG_DIRECTORY_MAX_SIZE) - { - break; - } - - if (isRemappable) - { - /* Check that entry point is within tested archive */ - runAddr = (uImgParser.imgHeader.vectors[1] & ~0xfffUL); - } - else - { - runAddr = imgAddr; - } - - if (!inOtaCheck) - { - if (runAddr != imgAddr) - break; - } - - if (uImgParser.imgHeader.bootBlockOffset % sizeof(uint32_t)) - 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); - - if (uImgParser.imgHeader.header_crc != crc) - 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) - break; - - headerBootBlockMarker = uImgParser.imgBootBlock.header_marker; - - if (!((headerBootBlockMarker >= BOOT_BLOCK_HDR_MARKER) && (headerBootBlockMarker <= BOOT_BLOCK_HDR_MARKER + 2))) - break; - if (!inOtaCheck) - { - 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 (uImgParser.imgBootBlock.compatibility_offset != 0) - { - uint32_t compatibility_list_sz = 0; - OTA_UTILS_DEBUG("Compatibility list found\n"); - 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) - break; - if (uImgParser.imgBootBlock.compatibility_offset != - bootBlockOffsetFound - (sizeof(uint32_t) + compatibility_list_sz * sizeof(ImageCompatibilityListElem_t))) - break; - } - else - break; - } - - /* Save bootblock data */ - imgSizeFound = uImgParser.imgBootBlock.img_len; - - /* Security check */ - if (pRootCert != NULL) - { - uint32_t imgSignatureOffset = bootBlockOffsetFound + sizeof(BOOT_BLOCK_T); - 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) - { - OTA_UTILS_DEBUG("Certificate found\n"); - /* Check that the certificate is inside the img */ - if ((uImgParser.imgBootBlock.certificate_offset + sizeof(ImageCertificate_t)) != imgSizeFound) - 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) - 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]) - break; - 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])) - break; - pKey = (const uint32_t *) &uImgParser.imgCertificate.certificate.public_key[0]; - imgSignatureOffset += sizeof(ImageCertificate_t); - } - else if (imgSignatureOffset != imgSizeFound) - { - break; - } - 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; - - 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)) - break; - } - result_addr = runAddr; - - } while (0); - OTA_UTILS_DEBUG("OTA_Utils => OtaUtils_ValidateImage result addr = 0x%x\n", result_addr); - 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 result = gOtaUtilsError_c; - uint32_t keyValid; - do - { - if (pCert == NULL || pPage0 == NULL) - break; - - keyValid = pPage0->page0_v3.img_pk_valid; - if (keyValid < 2) - { - result = gOtaUtilsInvalidKey_c; - break; - } - /* Decrypt the public key using the efuse key */ - 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); - - if (pFlashPage != NULL) - { - pCert->customer_id = pFlashPage->pFlash.customer_id; - pCert->min_device_id = pFlashPage->pFlash.min_device_id; - pCert->max_device_id = pFlashPage->pFlash.max_device_id; - } - else - { - 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; - } while (0); - return result; -} diff --git a/third_party/nxp/k32w0_sdk/sdk_fixes/OtaUtils_c.patch b/third_party/nxp/k32w0_sdk/sdk_fixes/OtaUtils_c.patch new file mode 100644 index 00000000000000..1814f74bb15502 --- /dev/null +++ b/third_party/nxp/k32w0_sdk/sdk_fixes/OtaUtils_c.patch @@ -0,0 +1,19 @@ +--- a/OtaUtils.c ++++ b/OtaUtils.c +@@ -92,11 +92,11 @@ typedef uint32_t (*flash_GetDmaccStatus_t)(uint8_t *address); + ************************************************************************************* + ************************************************************************************/ + +-const static efuse_LoadUniqueKey_t efuse_LoadUniqueKey = (efuse_LoadUniqueKey_t) ROM_API_efuse_LoadUniqueKey; +-const static aesLoadKeyFromOTP_t aesLoadKeyFromOTP = (aesLoadKeyFromOTP_t) ROM_API_aesLoadKeyFromOTP; +-const static crc_update_t crc_update = (crc_update_t)ROM_API_crc_update; +-const static boot_CheckVectorSum_t boot_CheckVectorSum = (boot_CheckVectorSum_t)ROM_API_boot_CheckVectorSum; +-const static flash_GetDmaccStatus_t flash_GetDmaccStatus = (flash_GetDmaccStatus_t) ROM_API_flash_GetDmaccStatus; ++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; + + /****************************************************************************** + ******************************************************************************* diff --git a/third_party/nxp/k32w0_sdk/sdk_fixes/SecLib.h b/third_party/nxp/k32w0_sdk/sdk_fixes/SecLib.h deleted file mode 100644 index 534f8c7186ccbc..00000000000000 --- a/third_party/nxp/k32w0_sdk/sdk_fixes/SecLib.h +++ /dev/null @@ -1,896 +0,0 @@ -/*! ********************************************************************************* - * Copyright (c) 2015, Freescale Semiconductor, Inc. - * Copyright 2016-2018 NXP - * All rights reserved. - * - * \file - * - * This is the header file for the security module. - * - * SPDX-License-Identifier: BSD-3-Clause - ********************************************************************************** */ - -#ifndef _SEC_LIB_H_ -#define _SEC_LIB_H_ - -/*! ********************************************************************************* -************************************************************************************* -* Include -************************************************************************************* -********************************************************************************** */ -#include "EmbeddedTypes.h" - -#ifndef gSecLibUseMutex_c -#define gSecLibUseMutex_c TRUE -#endif - -#define mRevertEcdhKeys_d 1 - -/*! ********************************************************************************* -************************************************************************************* -* Public macros -************************************************************************************* -********************************************************************************** */ -/* AES 128 */ -#define AES_128_KEY_BITS 128 -#define AES_128_BLOCK_SIZE 16 /* [bytes] */ - -/* CCM */ -#define gSecLib_CCM_Encrypt_c 0 -#define gSecLib_CCM_Decrypt_c 1 - -/* Hashes */ -#ifndef SHA1_HASH_SIZE -#define SHA1_HASH_SIZE 20 /* [bytes] */ -#endif -#ifndef SHA1_BLOCK_SIZE -#define SHA1_BLOCK_SIZE 64 /* [bytes] */ -#endif - -#ifndef SHA256_HASH_SIZE -#define SHA256_HASH_SIZE 32 /* [bytes] */ -#endif - -#ifndef SHA256_BLOCK_SIZE -#define SHA256_BLOCK_SIZE 64 /* [bytes] */ -#endif - -#define gHmacIpad_c 0x36 -#define gHmacOpad_c 0x5C - -/*! Enable or disable SHA-1 functionality in the SecLib module. */ -#ifndef gSecLibSha1Enable_d -#define gSecLibSha1Enable_d 1 -#endif - -/*! Enable or disable SHA-256 functionality in the SecLib module. */ -#ifndef gSecLibSha256Enable_d -#define gSecLibSha256Enable_d 1 -#endif - -#ifndef AES_BLOCK_SIZE -#define AES_BLOCK_SIZE 16 -#endif - -#ifndef AESSW_BLK_SIZE -#define AESSW_BLK_SIZE 16 -#endif - -/*! How many steps to use for the EC multiplication procedure */ -#ifndef gSecLibEcStepsAtATime -#define gSecLibEcStepsAtATime 16U -#endif - -/*! ********************************************************************************* -************************************************************************************* -* Public type definitions -************************************************************************************* -********************************************************************************** */ -typedef enum -{ - gSecSuccess_c = 0u, - gSecAllocError_c = 1u, - gSecError_c = 2u, - gSecInvalidPublicKey_c = 3u, - gSecResultPending_c = 4u -} secResultType_t; - -typedef enum ecdhStatus_tag -{ - gEcdhSuccess_c, - gEcdhBadParameters_c, - gEcdhOutOfMemory_c, - gEcdhRngError_c, - gEcdhInvalidState_c, - gEcdhInvalidPublicKey_c -} ecdhStatus_t; - -typedef union ecdhPrivateKey_tag -{ - uint8_t raw_8bit[32]; - uint32_t raw_32bit[8]; -} ecdhPrivateKey_t; - -#if !defined(__IAR_SYSTEMS_ICC__) -#pragma GCC diagnostic ignored "-Wpacked" -#pragma GCC diagnostic ignored "-Wattributes" -#endif -typedef PACKED_UNION ecdhPoint_tag -{ - uint8_t raw[64]; - PACKED_STRUCT - { - uint8_t x[32]; - uint8_t y[32]; - } - components_8bit; - PACKED_STRUCT - { - uint32_t x[8]; - uint32_t y[8]; - } - components_32bit; -} -ecdhPoint_t; -#if !defined(__IAR_SYSTEMS_ICC__) -#pragma GCC diagnostic pop -#endif -typedef ecdhPoint_t ecdhPublicKey_t; - -typedef ecdhPoint_t ecdhDhKey_t; - -/* Security block definition */ -typedef union -{ - uint8_t au8[AES_BLOCK_SIZE]; - uint32_t au32[AES_BLOCK_SIZE / 4]; -} tuAES_Block; - -/* For backwards compatibility */ -typedef tuAES_Block AESSW_Block_u; - -typedef struct -{ - uint32_t u32register0; - uint32_t u32register1; - uint32_t u32register2; - uint32_t u32register3; -} tsReg128; - -typedef struct computeDhKeyParams_tag -{ - ecdhPrivateKey_t privateKey; /*!< Secret */ - ecdhPublicKey_t peerPublicKey; /*!< Peer public key */ - ecdhPoint_t outPoint; /*!< The resulting point */ - void * pWorkBuffer; /*!< Pointer to the buffer used for computation */ - uint8_t procStep; /*!< The step used for segmented computation */ - uint32_t aUserData[1]; /*!< Hold upper layer private data */ -} computeDhKeyParam_t; - -typedef void (*secLibCallback_t)(computeDhKeyParam_t * pData); - -/************************************************************************************ -************************************************************************************* -* Public memory declarations -************************************************************************************* -************************************************************************************/ - -/************************************************************************************ -************************************************************************************* -* Public functions -************************************************************************************* -************************************************************************************/ - -#ifdef __cplusplus -extern "C" { -#endif -/*! ********************************************************************************* - * \brief This function performs initialization of the cryptographic HW acceleration. - * - ********************************************************************************** */ -void SecLib_Init(void); - -/*! ********************************************************************************* - * \brief This function performs initialization of the callback used to offload - * elliptic curve multiplication. - * - * \param[in] pfCallback Pointer to the function used to handle multiplication. - * - ********************************************************************************** */ -void SecLib_SetExternalMultiplicationCb(secLibCallback_t pfCallback); - -/*! ********************************************************************************* - * \brief This function performs calls the multiplication Callback. - * - * \param[in] pMsg Pointer to the data used in multiplication. - * - ********************************************************************************** */ -void SecLib_ExecMultiplicationCb(computeDhKeyParam_t * pMsg); - -/*! ********************************************************************************* - * \brief This function performs AES-128 encryption on a 16-byte block. - * - * \param[in] pInput Pointer to the location of the 16-byte plain text block. - * - * \param[in] pKey Pointer to the location of the 128-bit key. - * - * \param[out] pOutput Pointer to the location to store the 16-byte ciphered output. - * - * \pre All Input/Output pointers must refer to a memory address aligned to 4 bytes! - * - ********************************************************************************** */ -void AES_128_Encrypt(const uint8_t * pInput, const uint8_t * pKey, uint8_t * pOutput); - -/*! ********************************************************************************* - * \brief This function performs AES-128 decryption on a 16-byte block. - * - * \param[in] pInput Pointer to the location of the 16-byte plain text block. - * - * \param[in] pKey Pointer to the location of the 128-bit key. - * - * \param[out] pOutput Pointer to the location to store the 16-byte ciphered output. - * - * \pre All Input/Output pointers must refer to a memory address aligned to 4 bytes! - * - ********************************************************************************** */ -void AES_128_Decrypt(const uint8_t * pInput, const uint8_t * pKey, uint8_t * pOutput); - -/*! ********************************************************************************* - * \brief This function performs AES-128-ECB encryption on a message block. - * This function only accepts input lengths which are multiple - * of 16 bytes (AES 128 block size). - * - * \param[in] pInput Pointer to the location of the input message. - * - * \param[in] inputLen Input message length in bytes. - * - * \param[in] pKey Pointer to the location of the 128-bit key. - * - * \param[out] pOutput Pointer to the location to store the ciphered output. - * - ********************************************************************************** */ -void AES_128_ECB_Encrypt(uint8_t * pInput, uint32_t inputLen, uint8_t * pKey, uint8_t * pOutput); - -/*! ********************************************************************************* - * \brief This function performs AES-128-CBC encryption on a message block. - * This function only accepts input lengths which are multiple - * of 16 bytes (AES 128 block size). - * - * \param[in] pInput Pointer to the location of the input message. - * \param[in] inputLen Input message length in bytes. - * \param[in] pInitVector Pointer to the location of the 128-bit initialization vector. - * \param[in] pKey Pointer to the location of the 128-bit key. - * \param[out] pOutput Pointer to the location to store the ciphered output. - * - ********************************************************************************** */ -void AES_128_CBC_Encrypt(uint8_t * pInput, uint32_t inputLen, uint8_t * pInitVector, uint8_t * pKey, uint8_t * pOutput); - -/*! ********************************************************************************* - * \brief This function performs AES-128-CBC encryption on a message block after - * padding it with 1 bit of 1 and 0 bits trail. - * - * \param[in] pInput Pointer to the location of the input message. - * - * \param[in] inputLen Input message length in bytes. - * - * IMPORTANT: User must make sure that input and output - * buffers have at least inputLen + 16 bytes size - * - * \param[in] pInitVector Pointer to the location of the 128-bit initialization vector. - * - * \param[in] pKey Pointer to the location of the 128-bit key. - * - * \param[out] pOutput Pointer to the location to store the ciphered output. - * - * Return value: size of output buffer (after padding) - * - ********************************************************************************** */ -uint32_t AES_128_CBC_Encrypt_And_Pad(uint8_t * pInput, uint32_t inputLen, uint8_t * pInitVector, uint8_t * pKey, uint8_t * pOutput); - -/*! ********************************************************************************* - * \brief This function performs AES-128-CBC decryption on a message block. - * - * \param[in] pInput Pointer to the location of the input message. - * - * \param[in] inputLen Input message length in bytes. - * - * \param[in] pInitVector Pointer to the location of the 128-bit initialization vector. - * - * \param[in] pKey Pointer to the location of the 128-bit key. - * - * \param[out] pOutput Pointer to the location to store the ciphered output. - * - * Return value: size of output buffer (after depadding the 0x80 0x00 ... padding sequence) - * - ********************************************************************************** */ -uint32_t AES_128_CBC_Decrypt_And_Depad(uint8_t * pInput, uint32_t inputLen, uint8_t * pInitVector, uint8_t * pKey, - uint8_t * pOutput); - -/*! ********************************************************************************* - * \brief This function performs AES-128-CTR encryption on a message block. - * This function only accepts input lengths which are multiple - * of 16 bytes (AES 128 block size). - * - * \param[in] pInput Pointer to the location of the input message. - * - * \param[in] inputLen Input message length in bytes. - * - * \param[in] pCounter Pointer to the location of the 128-bit counter. - * - * \param[in] pKey Pointer to the location of the 128-bit key. - * - * \param[out] pOutput Pointer to the location to store the ciphered output. - * - ********************************************************************************** */ -void AES_128_CTR(uint8_t * pInput, uint32_t inputLen, uint8_t * pCounter, uint8_t * pKey, uint8_t * pOutput); - -/*! ********************************************************************************* - * \brief This function performs AES-128-OFB encryption on a message block. - * This function only accepts input lengths which are multiple - * of 16 bytes (AES 128 block size). - * - * \param[in] pInput Pointer to the location of the input message. - * - * \param[in] inputLen Input message length in bytes. - * - * \param[in] pInitVector Pointer to the location of the 128-bit initialization vector. - * - * \param[in] pKey Pointer to the location of the 128-bit key. - * - * \param[out] pOutput Pointer to the location to store the ciphered output. - * - ********************************************************************************** */ -void AES_128_OFB(uint8_t * pInput, uint32_t inputLen, uint8_t * pInitVector, uint8_t * pKey, uint8_t * pOutput); - -/*! ********************************************************************************* - * \brief This function performs AES-128-CMAC on a message block. - * - * \param[in] pInput Pointer to the location of the input message. - * - * \param[in] inputLen Length of the input message in bytes. The input data must be provided MSB first. - * - * \param[in] pKey Pointer to the location of the 128-bit key. The key must be provided MSB first. - * - * \param[out] pOutput Pointer to the location to store the 16-byte authentication code. The code will be generated MSB first. - * - * \remarks This is public open source code! Terms of use must be checked before use! - * - ********************************************************************************** */ -void AES_128_CMAC(uint8_t * pInput, uint32_t inputLen, uint8_t * pKey, uint8_t * pOutput); - -/*! ********************************************************************************* - * \brief This function performs AES-128-CMAC on a message block accepting input data - * which is in LSB first format and computing the authentication code starting from the end of the data. - * - * \param[in] pInput Pointer to the location of the input message. - * - * \param[in] inputLen Length of the input message in bytes. The input data must be provided LSB first. - * - * \param[in] pKey Pointer to the location of the 128-bit key. The key must be provided MSB first. - * - * \param[out] pOutput Pointer to the location to store the 16-byte authentication code. The code will be generated MSB first. - * - ********************************************************************************** */ -void AES_128_CMAC_LsbFirstInput(const uint8_t * pInput, const uint32_t inputLen, const uint8_t * pKey, uint8_t * pOutput); - -/*! ********************************************************************************* - * \brief This function performs AES 128 CMAC Pseudo-Random Function (AES-CMAC-PRF-128), - * according to rfc4615, on a message block. - * - * \details The AES-CMAC-PRF-128 algorithm behaves similar to the AES CMAC 128 algorithm - * but removes 128 bit key size restriction. - * - * \param[in] pInput Pointer to the location of the input message. - * - * \param[in] inputLen Length of the input message in bytes. - * - * \param[in] pVarKey Pointer to the location of the variable length key. - * - * \param[in] varKeyLen Length of the input key in bytes - * - * \param[out] pOutput Pointer to the location to store the 16-byte pseudo random variable. - * - ********************************************************************************** */ -void AES_CMAC_PRF_128(uint8_t * pInput, uint32_t inputLen, uint8_t * pVarKey, uint32_t varKeyLen, uint8_t * pOutput); - -/*! ********************************************************************************* - * \brief This function performs AES-128-EAX encryption on a message block. - * - * \param[in] pInput Pointer to the location of the input message. - * - * \param[in] inputLen Length of the input message in bytes. - * - * \param[in] pNonce Pointer to the location of the nonce. - * - * \param[in] nonceLen Nonce length in bytes. - * - * \param[in] pHeader Pointer to the location of header. - * - * \param[in] headerLen Header length in bytes. - * - * \param[in] pKey Pointer to the location of the 128-bit key. - * - * \param[out] pOutput Pointer to the location to store the 16-byte authentication code. - * - * \param[out] pTag Pointer to the location to store the 128-bit tag. - * - ********************************************************************************** */ -secResultType_t AES_128_EAX_Encrypt(uint8_t * pInput, uint32_t inputLen, uint8_t * pNonce, uint32_t nonceLen, uint8_t * pHeader, - uint8_t headerLen, uint8_t * pKey, uint8_t * pOutput, uint8_t * pTag); - -/*! ********************************************************************************* - * \brief This function performs AES-128-EAX decryption on a message block. - * - * \param[in] pInput Pointer to the location of the input message. - * - * \param[in] inputLen Length of the input message in bytes. - * - * \param[in] pNonce Pointer to the location of the nonce. - * - * \param[in] nonceLen Nonce length in bytes. - * - * \param[in] pHeader Pointer to the location of header. - * - * \param[in] headerLen Header length in bytes. - * - * \param[in] pKey Pointer to the location of the 128-bit key. - * - * \param[out] pOutput Pointer to the location to store the 16-byte authentication code. - * - * \param[out] pTag Pointer to the location to store the 128-bit tag. - * - ********************************************************************************** */ -secResultType_t AES_128_EAX_Decrypt(uint8_t * pInput, uint32_t inputLen, uint8_t * pNonce, uint32_t nonceLen, uint8_t * pHeader, - uint8_t headerLen, uint8_t * pKey, uint8_t * pOutput, uint8_t * pTag); - -/*! ********************************************************************************* - * \brief This function performs AES-128-CCM on a message block. - * - * \param[in] pInput Pointer to the location of the input message (plaintext or cyphertext). - * - * \param[in] inputLen Length of the input plaintext in bytes when encrypting. - * Length of the input cyphertext without the MAC length when decrypting. - * - * \param[in] pAuthData Pointer to the additional authentication data. - * - * \param[in] authDataLen Length of additional authentication data. - * - * \param[in] pNonce Pointer to the Nonce. - * - * \param[in] nonceSize The size of the nonce (7-13). - * - * \param[in] pKey Pointer to the location of the 128-bit key. - * - * \param[out] pOutput Pointer to the location to store the plaintext data when encrypting. - * Pointer to the location to store the cyphertext data when encrypting. - * - * \param[out] pCbcMac Pointer to the location to store the Message Authentication Code (MAC) when encrypting. - * Pointer to the location where the received MAC can be found when decrypting. - * - * \param[out] macSize The size of the MAC. - * - * \param[out] flags Select encrypt/decrypt operations (gSecLib_CCM_Encrypt_c, gSecLib_CCM_Decrypt_c) - * - ********************************************************************************** */ -uint8_t AES_128_CCM(uint8_t * pInput, uint16_t inputLen, uint8_t * pAuthData, uint16_t authDataLen, uint8_t * pNonce, - uint8_t nonceSize, uint8_t * pKey, uint8_t * pOutput, uint8_t * pCbcMac, uint8_t macSize, uint32_t flags); - -/*! ********************************************************************************* - * \brief This function allocates a memory buffer for a SHA1 context structure - * - * \return Address of the SHA1 context buffer - * Deallocate using SHA1_FreeCtx() - * - ********************************************************************************** */ -void * SHA1_AllocCtx(void); - -/*! ********************************************************************************* - * \brief This function deallocates the memory buffer for the SHA1 context structure - * - * \param [in] pContext Address of the SHA1 context buffer - * - ********************************************************************************** */ -void SHA1_FreeCtx(void * pContext); - -/*! ********************************************************************************* - * \brief This function clones a SHA1 context. - * Make sure the size of the allocated destination context buffer is appropriate. - * - * \param [in] pDestCtx Address of the destination SHA1 context - * \param [in] pSourceCtx Address of the source SHA1 context - * - ********************************************************************************** */ -void SHA1_CloneCtx(void * pDestCtx, void * pSourceCtx); - -/*! ********************************************************************************* - * \brief This function initializes the SHA1 context data - * - * \param [in] pContext Pointer to the SHA1 context data - * Allocated using SHA1_AllocCtx() - * - ********************************************************************************** */ -void SHA1_Init(void * pContext); - -/*! ********************************************************************************* - * \brief This function performs SHA1 on multiple bytes and updates the context data - * - * \param [in] pContext Pointer to the SHA1 context data - * Allocated using SHA1_AllocCtx() - * \param [in] pData Pointer to the input data - * \param [in] numBytes Number of bytes to hash - * - ********************************************************************************** */ -void SHA1_HashUpdate(void * pContext, const uint8_t * pData, uint32_t numBytes); - -/*! ********************************************************************************* - * \brief This function finalizes the SHA1 hash computation and clears the context data. - * The final hash value is stored at the provided output location. - * - * \param [in] pContext Pointer to the SHA1 context data - * Allocated using SHA1_AllocCtx() - * \param [out] pOutput Pointer to the output location - * - ********************************************************************************** */ -void SHA1_HashFinish(void * pContext, uint8_t * pOutput); - -/*! ********************************************************************************* - * \brief This function performs all SHA1 steps on multiple bytes: initialize, - * update and finish. - * The final hash value is stored at the provided output location. - * - * \param [in] pData Pointer to the input data - * \param [in] numBytes Number of bytes to hash - * \param [out] pOutput Pointer to the output location - * - ********************************************************************************** */ -void SHA1_Hash(const uint8_t * pData, const uint32_t numBytes, uint8_t * pOutput); - -/*! ********************************************************************************* - * \brief This function allocates a memory buffer for a SHA256 context structure - * - * \return Address of the SHA256 context buffer - * Deallocate using SHA256_FreeCtx() - * - ********************************************************************************** */ -void * SHA256_AllocCtx(void); - -/*! ********************************************************************************* - * \brief This function deallocates the memory buffer for the SHA256 context structure - * - * \param [in] pContext Address of the SHA256 context buffer - * - ********************************************************************************** */ -void SHA256_FreeCtx(void * pContext); - -/*! ********************************************************************************* - * \brief This function clones a SHA256 context. - * Make sure the size of the allocated destination context buffer is appropriate. - * - * \param [in] pDestCtx Address of the destination SHA256 context - * \param [in] pSourceCtx Address of the source SHA256 context - * - ********************************************************************************** */ -void SHA256_CloneCtx(void * pDestCtx, void * pSourceCtx); - -/*! ********************************************************************************* - * \brief This function initializes the SHA256 context data - * - * \param [in] pContext Pointer to the SHA256 context data - * Allocated using SHA256_AllocCtx() - * - ********************************************************************************** */ -void SHA256_Init(void * pContext); - -/*! ********************************************************************************* - * \brief This function performs SHA256 on multiple bytes and updates the context data - * - * \param [in] pContext Pointer to the SHA256 context data - * Allocated using SHA256_AllocCtx() - * \param [in] pData Pointer to the input data - * \param [in] numBytes Number of bytes to hash - * - ********************************************************************************** */ -void SHA256_HashUpdate(void * pContext, const uint8_t * pData, uint32_t numBytes); - -/*! ********************************************************************************* - * \brief This function finalizes the SHA256 hash computation and clears the context data. - * The final hash value is stored at the provided output location. - * - * \param [in] pContext Pointer to the SHA256 context data - * Allocated using SHA256_AllocCtx() - * \param [out] pOutput Pointer to the output location - * - ********************************************************************************** */ -void SHA256_HashFinish(void * pContext, uint8_t * pOutput); - -/*! ********************************************************************************* - * \brief This function performs all SHA256 steps on multiple bytes: initialize, - * update and finish. - * The final hash value is stored at the provided output location. - * - * \param [in] pData Pointer to the input data - * \param [in] numBytes Number of bytes to hash - * \param [out] pOutput Pointer to the output location - * - ********************************************************************************** */ -void SHA256_Hash(const uint8_t * pData, const uint32_t numBytes, uint8_t * pOutput); - -/*! ********************************************************************************* - * \brief This function allocates a memory buffer for a HMAC SHA256 context structure - * - * \return Address of the HMAC SHA256 context buffer - * Deallocate using HMAC_SHA256_FreeCtx() - * - ********************************************************************************** */ -void * HMAC_SHA256_AllocCtx(void); - -/*! ********************************************************************************* - * \brief This function deallocates the memory buffer for the HMAC SHA256 context structure - * - * \param [in] pContext Address of the HMAC SHA256 context buffer - * - ********************************************************************************** */ -void HMAC_SHA256_FreeCtx(void * pContext); - -/*! ********************************************************************************* - * \brief This function performs the initialization of the HMAC SHA256 context data - * - * \param [in] pContext Pointer to the HMAC SHA256 context data - * Allocated using HMAC_SHA256_AllocCtx() - * \param [in] pKey Pointer to the HMAC key - * \param [in] keyLen Length of the HMAC key in bytes - * - ********************************************************************************** */ -void HMAC_SHA256_Init(void * pContext, uint8_t * pKey, uint32_t keyLen); - -/*! ********************************************************************************* - * \brief This function performs HMAC update with the input data. - * - * \param [in] pContext Pointer to the HMAC SHA256 context data - * Allocated using HMAC_SHA256_AllocCtx() - * \param [in] pData Pointer to the input data - * \param [in] numBytes Number of bytes to hash - * - ********************************************************************************** */ -void HMAC_SHA256_Update(void * pContext, uint8_t * pData, uint32_t numBytes); - -/*! ********************************************************************************* - * \brief This function finalizes the HMAC SHA256 computation and clears the context data. - * The final hash value is stored at the provided output location. - * - * \param [in] pContext Pointer to the HMAC SHA256 context data - * Allocated using HMAC_SHA256_AllocCtx() - * \param [out] pOutput Pointer to the output location - * - ********************************************************************************** */ -void HMAC_SHA256_Finish(void * pContext, uint8_t * pOutput); - -/*! ********************************************************************************* - * \brief This function performs all HMAC SHA256 steps on multiple bytes: initialize, - * update, finish, and update context data. - * The final HMAC value is stored at the provided output location. - * - * \param [in] pKey Pointer to the HMAC key - * \param [in] keyLen Length of the HMAC key in bytes - * \param [in] pData Pointer to the input data - * \param [in] numBytes Number of bytes to perform HMAC on - * \param [out] pOutput Pointer to the output location - * - ********************************************************************************** */ -void HMAC_SHA256(uint8_t * pKey, uint32_t keyLen, uint8_t * pData, uint32_t numBytes, uint8_t * pOutput); - -/*! ********************************************************************************* - * \brief This function calculates XOR of individual byte pairs in two uint8_t arrays. - * pDst[i] := pDst[i] ^ pSrc[i] for i=0 to n-1 - * - * \param[in, out] pDst First byte array operand for XOR and destination byte array - * - * \param[in] pSrc Second byte array operand for XOR - * - * \param[in] n Length of the byte arrays which will be XORed - * - ********************************************************************************** */ -void SecLib_XorN(uint8_t * pDst, /* First operand and result of XOR operation */ - uint8_t * pSrc, /* Second operand. Not modified. */ - uint8_t n); /* Number of bytes in input arrays. */ - -/************************************************************************************ - * \brief Generates a new ECDH P256 Private/Public key pair - * - * \return gSecSuccess_c or error - * - ************************************************************************************/ -secResultType_t ECDH_P256_GenerateKeys(ecdhPublicKey_t * pOutPublicKey, ecdhPrivateKey_t * pOutPrivateKey); - -/************************************************************************************ - * \brief Generates a new ECDH P256 Private/Public key pair. This function starts the - * ECDH generate procedure. The pDhKeyData must be allocated and kept - * allocated for the time of the computation procedure. - * When the result is gSecResultPending_c the memory should be kept until the - * last step. - * In any other result messages the data shall be cleared after this call. - * - * \param[in] pDhKeyData Pointer to the structure holding information about the - * multiplication - * - * \return gSecSuccess_c, gSecResultPending_c or error - * - ************************************************************************************/ -secResultType_t ECDH_P256_GenerateKeysSeg(computeDhKeyParam_t * pDhKeyData); - -/************************************************************************************ - * \brief Computes the Diffie-Hellman Key for an ECDH P256 key pair. - * - * \return gSecSuccess_c or error - * - ************************************************************************************/ -secResultType_t ECDH_P256_ComputeDhKey(ecdhPrivateKey_t * pInPrivateKey, ecdhPublicKey_t * pInPeerPublicKey, - ecdhDhKey_t * pOutDhKey); -/************************************************************************************ - * \brief Handle one step of ECDH multiplication depending on the number of steps at - * a time according to gSecLibEcStepsAtATime. After the last step is completed - * the function returns TRUE and the upper layer is responsible for clearing - * pData. - * - * \param[in] pData Pointer to the structure holding information about the - * multiplication - * - * \return TRUE if the multiplication is completed - * FALSE when the function needs to be called again - * - ************************************************************************************/ -bool_t SecLib_HandleMultiplyStep(computeDhKeyParam_t * pData); -/************************************************************************************ - * \brief Computes the Diffie-Hellman Key for an ECDH P256 key pair. This function - * starts the ECDH key pair generate procedure. The pDhKeyData must be - * allocated and kept allocated for the time of the computation procedure. - * When the result is gSecResultPending_c the memory should be kept until the - * last step, when it can be safely freed. - * In any other result messages the data shall be cleared after this call. - * - * \param[in] pDhKeyData Pointer to the structure holding information about the - * multiplication - * - * \return gSecSuccess_c or error - * - ************************************************************************************/ -secResultType_t ECDH_P256_ComputeDhKeySeg(computeDhKeyParam_t * pDhKeyData); -/**************************************************************************** - * \brief Perform an MMO Block Update on the hash - * H[j] = E(H[j-1], M[j]) ^ M[j] - * where E(K,x) = AES-128 block cipher, K=key, x=text - * - * \param [in] psMMO MMO object - * \param [in] puBlock Block to hash - * - ****************************************************************************/ -void AESSW_vMMOBlockUpdate(AESSW_Block_u * puHash, AESSW_Block_u * puBlock); - -/**************************************************************************** - * \brief Perform the final update on the MMO, running text through it - * H[0] = 0; H[j] = E(H[j-1], M[j]) ^ M[j] for j=1..t - * E(K,x) = AES-128 block cipher, K=key, x=text - * - * \param [in] psMMO MMO object - * \param [in] pu8Data Text to run through - * \param [in] iDataLen Length of text to run through - * \param [in] iFinalLen Final length of buffer - * - ****************************************************************************/ -void AESSW_vMMOFinalUpdate(AESSW_Block_u * puHash, uint8_t * pu8Data, int iDataLen, int iFinalLen); - -/**************************************************************************** - * \brief Perform the HMAC-MMO Keyed Hash Function for Message Authentication - * Specified in B.1.4 in ZigBee specification (053474r17) - * - * \param [in] pu8Data Pointer to data stream - * \param [in] iDataLen Length of data stream - * \param [in] puKey Key - * \param [out] puHash Output hash - * - ****************************************************************************/ -void AESSW_vHMAC_MMO(uint8_t * pu8Data, int iDataLen, AESSW_Block_u * puKeyData, AESSW_Block_u * puHash); - -/*! ********************************************************************************* - * \brief This function performs AES-128-CTR decryption on a message block. - * - * \param[in] pInput Pointer to the location of the input message. - * \param[in] inputLen Input message length in bytes. - * \param[in] pCounter Pointer to the location of the 128-bit counter. - * \param[in] pKey Pointer to the location of the 128-bit key. - * \param[out] pOutput Pointer to the location to store the ciphered output. - * - ********************************************************************************** */ -void AES_128_CTR_Decrypt(uint8_t * pInput, uint32_t inputLen, uint8_t * pCounter, uint8_t * pKey, uint8_t * pOutput); - -/*! ********************************************************************************* - * \brief This function performs AES-128-CTR decryption on a message block. - * - * \param[in] pInput Pointer to the location of the input message. - * \param[in] inputLen Input message length in bytes. - * \param[in] pCounter Pointer to the location of the 128-bit counter. - * \param[in] pKey Pointer to the location of the 128-bit key. - * \param[out] pOutput Pointer to the location to store the ciphered output. - * - ********************************************************************************** */ -void AES_128_CTR_Decrypt(uint8_t * pInput, uint32_t inputLen, uint8_t * pCounter, uint8_t * pKey, uint8_t * pOutput); - -/**************************************************************************** - * \brief This function setups the key in the AES. - * - * \param [in] psKeyData tsReg128 key split over 4 words (16 bytes) - * - ****************************************************************************/ -bool_t bACI_WriteKey(tsReg128 * psKeyData); - -/**************************************************************************** - * \brief This function performs CCM* AES encryption/decryption with checksum - * generation/verification. Assumes input buffers are aligned and nonce has a - * free byte at the start for the flags field - * - * \param [in] bEncrypt TRUE to encrypt, FALSE to decrypt - * \param [in] u8M Required number of checksum bytes - * \param [in] u8alength Length of authentication data, in bytes - * \param [in] u8mlength Length of input data, in bytes - * \param [in] puNonce Pre-allocated pointer to a structure - * containing 128-bit Nonce data - * \param [in] pau8authenticationData Pre-allocated pointer to byte array of - * authentication data - * \param [in] pau8Data Pre-allocated pointer to byte array of input - * and output data - * \param [in] pau8checksumData Pre-allocated pointer to byte array of - * checksum data - * \param [in] pbChecksumVerify Pre-allocated pointer to boolean which in CCM - * decode mode stores the result of the checksum - * verification operation - * - ****************************************************************************/ -void vACI_OptimisedCcmStar(bool_t bEncrypt, uint8_t u8M, uint8_t u8alength, uint8_t u8mlength, tuAES_Block * puNonce, - uint8_t * pau8authenticationData, uint8_t * pau8Data, uint8_t * pau8checksumData, - bool_t * pbChecksumVerify); -/**************************************************************************** - * - * NAME: u32Reverse - */ -/** - * Reverses 32bit data - * - * @ingroup grp_aes_sw - * - * @param u32InWord Input 32-bit data - * - * @return u32OutWord Output reversed 32- bit data - * - * @note - * - ****************************************************************************/ -uint32_t u32Reverse(uint32_t u32InWord); - -/**************************************************************************** - * - * NAME: vSwipeEndian - */ -/** - * Reverses 1282bit data between AESSW_Block_u and tsReg128 - * - * @ingroup grp_aes_sw - * - * @param puBlock 128 bit data of type AESSW_Block_u - * - * @param psReg 128 bit data of type tsReg128 - * - * @param bBlockToReg direction of converesion - * 1 = AESSW_Block_u to tsReg128 - * 0 = tsReg128 to AESSW_Block_u - * - * @return - * - * @note - * - ****************************************************************************/ -void vSwipeEndian(AESSW_Block_u * puBlock, tsReg128 * psReg, bool_t bBlockToReg); - -#ifdef __cplusplus -} -#endif -#endif /* _SEC_LIB_H_ */ diff --git a/third_party/nxp/k32w0_sdk/sdk_fixes/SecLib_h.patch b/third_party/nxp/k32w0_sdk/sdk_fixes/SecLib_h.patch new file mode 100644 index 00000000000000..a65b5c6537271e --- /dev/null +++ b/third_party/nxp/k32w0_sdk/sdk_fixes/SecLib_h.patch @@ -0,0 +1,24 @@ +--- a/SecLib.h ++++ b/SecLib.h +@@ -174,6 +174,10 @@ typedef void(*secLibCallback_t)(computeDhKeyParam_t* pData); + ************************************************************************************* + ************************************************************************************/ + ++#ifdef __cplusplus ++extern "C" { ++#endif ++ + /*! ********************************************************************************* + * \brief This function performs initialization of the cryptographic HW acceleration. + * +@@ -999,5 +1003,10 @@ uint32_t u32Reverse(uint32_t u32InWord); + void vSwipeEndian( AESSW_Block_u *puBlock, + tsReg128 *psReg, + bool_t bBlockToReg ); ++ ++#ifdef __cplusplus ++} ++#endif ++ + #endif /* _SEC_LIB_H_ */ + diff --git a/third_party/nxp/k32w0_sdk/sdk_fixes/app_dual_mode_low_power.h b/third_party/nxp/k32w0_sdk/sdk_fixes/app_dual_mode_low_power.h deleted file mode 100644 index 53b2701e18e66d..00000000000000 --- a/third_party/nxp/k32w0_sdk/sdk_fixes/app_dual_mode_low_power.h +++ /dev/null @@ -1,62 +0,0 @@ -/*! ********************************************************************************* - * Copyright 2020 NXP - * - * SPDX-License-Identifier: BSD-3-Clause - ********************************************************************************** */ - -#ifndef _APP_DUAL_MODE_LOW_POWER_H_ -#define _APP_DUAL_MODE_LOW_POWER_H_ - -/*! ********************************************************************************* -************************************************************************************* -* Include -************************************************************************************* -********************************************************************************** */ -#include "EmbeddedTypes.h" - -/*! ********************************************************************************* -************************************************************************************* -* Public type definitions -************************************************************************************* -********************************************************************************** */ - -typedef enum -{ - eBleWakeUpEnded = 0, - e15_4WakeUpEnded, -} eDualModeLpEvent; - -/*! ********************************************************************************* -************************************************************************************* -* Public macros -************************************************************************************* -********************************************************************************** */ - -/*! ********************************************************************************* -************************************************************************************* -* Public memory declarations -************************************************************************************* -********************************************************************************** */ - -/*! ********************************************************************************* -************************************************************************************* -* Public prototypes -************************************************************************************* -********************************************************************************** */ -#ifdef __cplusplus -extern "C" { -#endif - -void dm_lp_init(void); - -void dm_lp_preSleep(void); - -void dm_lp_wakeup(void); - -void dm_lp_processEvent(void * pParam); - -#ifdef __cplusplus -} -#endif - -#endif /* _APP_DUAL_MODE_LOW_POWER_H_ */ diff --git a/third_party/nxp/k32w0_sdk/sdk_fixes/app_dual_mode_low_power_h.patch b/third_party/nxp/k32w0_sdk/sdk_fixes/app_dual_mode_low_power_h.patch new file mode 100644 index 00000000000000..54a6eb7190697b --- /dev/null +++ b/third_party/nxp/k32w0_sdk/sdk_fixes/app_dual_mode_low_power_h.patch @@ -0,0 +1,24 @@ +--- a/app_dual_mode_low_power.h ++++ b/app_dual_mode_low_power.h +@@ -43,6 +43,9 @@ typedef enum + * Public prototypes + ************************************************************************************* + ********************************************************************************** */ ++#ifdef __cplusplus ++extern "C" { ++#endif + + void dm_lp_init(void); + +@@ -50,6 +53,10 @@ void dm_lp_preSleep(void); + + void dm_lp_wakeup(void); + +-void dm_lp_processEvent(void *pParam); ++void dm_lp_processEvent(void * pParam); ++ ++#ifdef __cplusplus ++} ++#endif + + #endif /* _APP_DUAL_MODE_LOW_POWER_H_ */ diff --git a/third_party/nxp/k32w0_sdk/sdk_fixes/app_dual_mode_switch.h b/third_party/nxp/k32w0_sdk/sdk_fixes/app_dual_mode_switch.h deleted file mode 100644 index cedc9b3565175a..00000000000000 --- a/third_party/nxp/k32w0_sdk/sdk_fixes/app_dual_mode_switch.h +++ /dev/null @@ -1,107 +0,0 @@ -/*! ********************************************************************************* - * Copyright (c) 2014, Freescale Semiconductor, Inc. - * Copyright 2016-2020 NXP - * - * \file - * - * This is a include file for the dual mode switch support Zigbee to BLE + BLE to Zigbee. - * - * SPDX-License-Identifier: BSD-3-Clause - ********************************************************************************** */ - -#ifndef _APP_DUAL_MODE_SWITCH_H_ -#define _APP_DUAL_MODE_SWITCH_H_ - -/*! ********************************************************************************* -************************************************************************************* -* Include -************************************************************************************* -********************************************************************************** */ -#include "EmbeddedTypes.h" - -/*! ********************************************************************************* -************************************************************************************* -* Public type definitions -************************************************************************************* -********************************************************************************** */ - -typedef enum -{ - eBleAdvStopEvent, - eBleDisconnectionEvent, - eBleNotRunningEvent, - e15_4FactoryResetEvent, - e15_4WakeUpReinitContinueEvent, -} eDualModeEvent; - -/*! ********************************************************************************* -************************************************************************************* -* Public macros -************************************************************************************* -********************************************************************************** */ - -#if defined gLoggingActive_d && (gLoggingActive_d > 0) -#include "dbg_logging.h" -#ifndef DBG_SWITCH -#define DBG_SWITCH 0 -#endif -#define SWITCH_DBG_LOG(fmt, ...) \ - if (DBG_SWITCH) \ - do \ - { \ - DbgLogAdd(__FUNCTION__, fmt, VA_NUM_ARGS(__VA_ARGS__), ##__VA_ARGS__); \ - } while (0); -#else -#define SWITCH_DBG_LOG(...) -#endif - -/*! ********************************************************************************* -************************************************************************************* -* Public memory declarations -************************************************************************************* -********************************************************************************** */ - -/*! ********************************************************************************* -************************************************************************************* -* Public prototypes -************************************************************************************* -********************************************************************************** */ - -#ifdef __cplusplus -extern "C" { -#endif -/*! ********************************************************************************* - * \brief Runs the dual mode switch Idle Task - * - ********************************************************************************** */ -void dm_switch_IdleTask(void); - -/*! ********************************************************************************* - * \brief Process any event and takes action depending of the event - * - * \param[in] pParam - * - ********************************************************************************** */ -void dm_switch_processEvent(void * pParam); - -/*! ********************************************************************************* - * \brief Returns the duration of the 15.4 Init stage after wake up - * Duration of the function dm_switch_init15_4AfterWakeUp in us - * - * \return uint32_t - * - ********************************************************************************** */ -uint32_t dm_switch_get15_4InitWakeUpTime(void); - -/*! ********************************************************************************* - * \brief Runs the init 15.4 stage after wake up which should contains radio init calls - * if any - * - ********************************************************************************** */ -void dm_switch_init15_4AfterWakeUp(void); - -#ifdef __cplusplus -} -#endif - -#endif /* _APP_DUAL_MODE_SWITCH_H_ */ diff --git a/third_party/nxp/k32w0_sdk/sdk_fixes/app_dual_mode_switch_h.patch b/third_party/nxp/k32w0_sdk/sdk_fixes/app_dual_mode_switch_h.patch new file mode 100644 index 00000000000000..8ec94c488ea5d9 --- /dev/null +++ b/third_party/nxp/k32w0_sdk/sdk_fixes/app_dual_mode_switch_h.patch @@ -0,0 +1,21 @@ +--- a/app_dual_mode_switch.h ++++ b/app_dual_mode_switch.h +@@ -61,6 +61,9 @@ typedef enum { + ************************************************************************************* + ********************************************************************************** */ + ++#ifdef __cplusplus ++extern "C" { ++#endif + /*! ********************************************************************************* + * \brief Runs the dual mode switch Idle Task + * +@@ -91,4 +94,8 @@ uint32_t dm_switch_get15_4InitWakeUpTime(void); + ********************************************************************************** */ + void dm_switch_init15_4AfterWakeUp(void); + ++#ifdef __cplusplus ++} ++#endif ++ + #endif /* _APP_DUAL_MODE_SWITCH_H_ */ diff --git a/third_party/nxp/k32w0_sdk/sdk_fixes/patch_ble_utils_h.patch b/third_party/nxp/k32w0_sdk/sdk_fixes/ble_utils_h.patch similarity index 96% rename from third_party/nxp/k32w0_sdk/sdk_fixes/patch_ble_utils_h.patch rename to third_party/nxp/k32w0_sdk/sdk_fixes/ble_utils_h.patch index bb2051b158ecf9..a3fff72f0f33ce 100644 --- a/third_party/nxp/k32w0_sdk/sdk_fixes/patch_ble_utils_h.patch +++ b/third_party/nxp/k32w0_sdk/sdk_fixes/ble_utils_h.patch @@ -1,6 +1,6 @@ ---- a/ble_utils.h 2022-01-07 16:41:34.017433835 +0000 -+++ b/ble_utils.h 2022-01-07 16:42:09.797788620 +0000 -@@ -73,11 +73,6 @@ +--- a/ble_utils.h 2022-01-07 16:41:34.017433835 +0000 ++++ b/ble_utils.h 2022-01-07 16:42:09.797788620 +0000 +@@ -73,11 +73,6 @@ #define ALIGN_64BIT #pragma pack(8) #endif diff --git a/third_party/nxp/k32w0_sdk/sdk_fixes/gpio_pins.h b/third_party/nxp/k32w0_sdk/sdk_fixes/gpio_pins.h deleted file mode 100644 index 96c4477f677d47..00000000000000 --- a/third_party/nxp/k32w0_sdk/sdk_fixes/gpio_pins.h +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright 2020 NXP - * All rights reserved. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef __GPIO_PINS_H__ -#define __GPIO_PINS_H__ - -#include "GPIO_Adapter.h" - -/*! @file */ -/*!*/ -/*! This file contains gpio pin definitions used by gpio peripheral driver.*/ -/*! The enums in _gpio_pins map to the real gpio pin numbers defined in*/ -/*! gpioPinLookupTable. And this might be different in different board.*/ - -/******************************************************************************* - * Definitions - ******************************************************************************/ - -#if (defined OM15082) -#define BOARD_LED_RED1_GPIO GPIO -#define BOARD_LED_RED1_GPIO_PORT 0U -#define BOARD_LED_RED1_GPIO_PIN 6U -#define IOCON_LED_RED1_PIN BOARD_LED_RED1_GPIO_PIN - -#define BOARD_LED_RED2_GPIO GPIO -#define BOARD_LED_RED2_GPIO_PORT 0U -#define BOARD_LED_RED2_GPIO_PIN 3U -#define IOCON_LED_RED2_PIN BOARD_LED_RED2_GPIO_PIN - -#define IOCON_LED_MODE_FUNC (0U) -#else -/* There are 2 red LEDs on DK6 board: PIO0 and PIO3 */ -#define BOARD_LED_RED1_GPIO GPIO -#define BOARD_LED_RED1_GPIO_PORT 0U -#define BOARD_LED_RED1_GPIO_PIN 0U -#define IOCON_LED_RED1_PIN BOARD_LED_RED1_GPIO_PIN - -#define BOARD_LED_RED2_GPIO GPIO -#define BOARD_LED_RED2_GPIO_PORT 0U -#define BOARD_LED_RED2_GPIO_PIN 3U -#define IOCON_LED_RED2_PIN BOARD_LED_RED2_GPIO_PIN - -#define IOCON_LED_MODE_FUNC (0U) -#endif /* OM15082 */ - -#if !(defined OM15082) -/* We have 2 switch push-buttons on DK6 board */ -#define BOARD_USER_BUTTON1_GPIO GPIO -#define BOARD_USER_BUTTON1_GPIO_PORT 0U -#define BOARD_USER_BUTTON1_GPIO_PIN 1U -#define IOCON_USER_BUTTON1_PIN BOARD_USER_BUTTON1_GPIO_PIN - -#define BOARD_USER_BUTTON2_GPIO GPIO -#define BOARD_USER_BUTTON2_GPIO_PORT 0U -#define BOARD_USER_BUTTON2_GPIO_PIN 5U /* shared with ISP entry */ -#define IOCON_USER_BUTTON2_PIN BOARD_USER_BUTTON2_GPIO_PIN - -#else -/* We have 3 extra switch push-buttons on OM15082 board */ -#define BOARD_USER_BUTTON1_GPIO GPIO -#define BOARD_USER_BUTTON1_GPIO_PORT 0U -#define BOARD_USER_BUTTON1_GPIO_PIN 15U -#define IOCON_USER_BUTTON1_PIN BOARD_USER_BUTTON1_GPIO_PIN - -#define BOARD_USER_BUTTON2_GPIO GPIO -#define BOARD_USER_BUTTON2_GPIO_PORT 0U -#define BOARD_USER_BUTTON2_GPIO_PIN 7U -#define IOCON_USER_BUTTON2_PIN BOARD_USER_BUTTON2_GPIO_PIN - -#define BOARD_USER_BUTTON3_GPIO GPIO -#define BOARD_USER_BUTTON3_GPIO_PORT 0U -#define BOARD_USER_BUTTON3_GPIO_PIN 4U -#define IOCON_USER_BUTTON3_PIN BOARD_USER_BUTTON3_GPIO_PIN - -#define BOARD_USER_BUTTON4_GPIO GPIO -#define BOARD_USER_BUTTON4_GPIO_PORT 0U -#define BOARD_USER_BUTTON4_GPIO_PIN 1U -#define IOCON_USER_BUTTON4_PIN BOARD_USER_BUTTON4_GPIO_PIN - -#endif /* OM15082 */ - -#define IOCON_USER_BUTTON_MODE_FUNC (0U) - -/* Battery level input */ -#define BOARD_BAT_LEVEL_GPIO_PORT (0U) -#define BOARD_BAT_LEVEL_GPIO_PIN (14U) - -extern const gpioInputPinConfig_t dk6_button_io_pins[]; -extern const gpioOutputPinConfig_t dk6_leds_io_pins[]; - -#define ledPins dk6_leds_io_pins -#define switchPins dk6_button_io_pins - -#endif /* __GPIO_PINS_H__ */ diff --git a/third_party/nxp/k32w0_sdk/sdk_fixes/gpio_pins_h.patch b/third_party/nxp/k32w0_sdk/sdk_fixes/gpio_pins_h.patch new file mode 100644 index 00000000000000..7dd6f75fa8bdc7 --- /dev/null +++ b/third_party/nxp/k32w0_sdk/sdk_fixes/gpio_pins_h.patch @@ -0,0 +1,14 @@ +--- a/gpio_pins.h ++++ b/gpio_pins.h +@@ -76,6 +76,11 @@ + #define BOARD_USER_BUTTON3_GPIO_PIN 4U + #define IOCON_USER_BUTTON3_PIN BOARD_USER_BUTTON3_GPIO_PIN + ++#define BOARD_USER_BUTTON4_GPIO GPIO ++#define BOARD_USER_BUTTON4_GPIO_PORT 0U ++#define BOARD_USER_BUTTON4_GPIO_PIN 1U ++#define IOCON_USER_BUTTON4_PIN BOARD_USER_BUTTON4_GPIO_PIN ++ + #endif /* OM15082 */ + + #define IOCON_USER_BUTTON_MODE_FUNC (0U) diff --git a/third_party/nxp/k32w0_sdk/sdk_fixes/patch_k32w_sdk.sh b/third_party/nxp/k32w0_sdk/sdk_fixes/patch_k32w_sdk.sh index d3f262f11595e6..4737db6534e45e 100755 --- a/third_party/nxp/k32w0_sdk/sdk_fixes/patch_k32w_sdk.sh +++ b/third_party/nxp/k32w0_sdk/sdk_fixes/patch_k32w_sdk.sh @@ -5,22 +5,31 @@ if [[ ! -d $NXP_K32W061_SDK_ROOT ]]; then exit 1 fi +convert_to_dos() { + + [[ $(file -b - <$1) != *"CRLF"* ]] && unix2dos "$1" +} + SOURCE=${BASH_SOURCE[0]} SOURCE_DIR=$(cd "$(dirname "$SOURCE")" >/dev/null 2>&1 && pwd) -cp ./third_party/nxp/k32w0_sdk/sdk_fixes/gpio_pins.h "$NXP_K32W061_SDK_ROOT"/boards/k32w061dk6/wireless_examples/openthread/reed/bm/ -cp ./third_party/nxp/k32w0_sdk/sdk_fixes/pin_mux.c "$NXP_K32W061_SDK_ROOT"/boards/k32w061dk6/wireless_examples/openthread/enablement/ +convert_to_dos "$NXP_K32W061_SDK_ROOT"/boards/k32w061dk6/wireless_examples/openthread/reed/bm/gpio_pins.h +patch --binary -d "$NXP_K32W061_SDK_ROOT"/boards/k32w061dk6/wireless_examples/openthread/reed/bm -p1 <"$SOURCE_DIR/gpio_pins_h.patch" + +convert_to_dos "$NXP_K32W061_SDK_ROOT"/boards/k32w061dk6/wireless_examples/hybrid/ble_ot/lped_ble_wuart/ble_802_15_4_common/app_dual_mode_low_power.h +patch --binary -d "$NXP_K32W061_SDK_ROOT"/boards/k32w061dk6/wireless_examples/hybrid/ble_ot/lped_ble_wuart/ble_802_15_4_common -p1 <"$SOURCE_DIR/app_dual_mode_low_power_h.patch" -#TODO internal: https://jira.sw.nxp.com/browse/MCB-2678 -cp ./third_party/nxp/k32w0_sdk/sdk_fixes/SecLib.h "$NXP_K32W061_SDK_ROOT"/middleware/wireless/framework/SecLib/ +convert_to_dos "$NXP_K32W061_SDK_ROOT"/boards/k32w061dk6/wireless_examples/hybrid/ble_ot/lped_ble_wuart/ble_802_15_4_common/app_dual_mode_switch.h +patch --binary -d "$NXP_K32W061_SDK_ROOT"/boards/k32w061dk6/wireless_examples/hybrid/ble_ot/lped_ble_wuart/ble_802_15_4_common -p1 <"$SOURCE_DIR/app_dual_mode_switch_h.patch" -# TODO internal: https://jira.sw.nxp.com/browse/MCB-2675 -cp -r ./third_party/nxp/k32w0_sdk/sdk_fixes/app_dual_mode_low_power.h "$NXP_K32W061_SDK_ROOT"/boards/k32w061dk6/wireless_examples/hybrid/ble_ot/lped_ble_wuart/ble_802_15_4_common/ -cp -r ./third_party/nxp/k32w0_sdk/sdk_fixes/app_dual_mode_switch.h "$NXP_K32W061_SDK_ROOT"/boards/k32w061dk6/wireless_examples/hybrid/ble_ot/lped_ble_wuart/ble_802_15_4_common/ +convert_to_dos "$NXP_K32W061_SDK_ROOT"/middleware/wireless/framework/SecLib/SecLib.h +patch --binary -d "$NXP_K32W061_SDK_ROOT"/middleware/wireless/framework/SecLib -p1 <"$SOURCE_DIR/SecLib_h.patch" -cp -r ./third_party/nxp/k32w0_sdk/sdk_fixes/OtaUtils.c "$NXP_K32W061_SDK_ROOT"/middleware/wireless/framework/OtaSupport/Source/ +convert_to_dos "$NXP_K32W061_SDK_ROOT"/middleware/wireless/framework/OtaSupport/Source/OtaUtils.c +patch --binary -d "$NXP_K32W061_SDK_ROOT"/middleware/wireless/framework/OtaSupport/Source -p1 <"$SOURCE_DIR/OtaUtils_c.patch" -patch -d "$NXP_K32W061_SDK_ROOT"/middleware/wireless/bluetooth/host/interface -p1 <"$SOURCE_DIR/patch_ble_utils_h.patch" +convert_to_dos "$NXP_K32W061_SDK_ROOT"/middleware/wireless/bluetooth/host/interface/ble_utils.h +patch --binary -d "$NXP_K32W061_SDK_ROOT"/middleware/wireless/bluetooth/host/interface -p1 <"$SOURCE_DIR/ble_utils_h.patch" echo "K32W SDK MR3 QP1 was patched!" exit 0 diff --git a/third_party/nxp/k32w0_sdk/sdk_fixes/pin_mux.c b/third_party/nxp/k32w0_sdk/sdk_fixes/pin_mux.c deleted file mode 100644 index 4c9d1f9d5a2d8e..00000000000000 --- a/third_party/nxp/k32w0_sdk/sdk_fixes/pin_mux.c +++ /dev/null @@ -1,201 +0,0 @@ -/* - * Copyright (c) 2014 - 2015, Freescale Semiconductor, Inc. - * Copyright 2016-2019 NXP - * All rights reserved. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#include "pin_mux.h" -#include "fsl_common.h" -#include "fsl_gpio.h" -#include "fsl_iocon.h" - -/***************************************************************************** - * Private types/enumerations/variables - ****************************************************************************/ - -#ifndef BOARD_USECLKINSRC -#define BOARD_USECLKINSRC (0) -#endif - -/***************************************************************************** - * Public types/enumerations/variables - ****************************************************************************/ - -/***************************************************************************** - * Local Prototypes - ****************************************************************************/ -/***************************************************************************** - * Private functions - ****************************************************************************/ - -/* clang-format off */ -/* - * TEXT BELOW IS USED AS SETTING FOR TOOLS ************************************* -BOARD_InitAppUARTPins: -- options: {callFromInitBoot: 'false', coreID: cm4, enableClock: 'true'} -- pin_list: - - {pin_num: '11', peripheral: USART0, signal: TXD, pin_signal: PIO0_8/SPI0_MOSI/USART0_TXD/CT32B0_MAT0/PWM8/ANA_COMP_OUT/RFTX/PDM1_DATA, mode: pullUp, invert: disabled, - open_drain: disabled, filter_off: disabled, slew_rate_0: standard, slew_rate_1: standard, ssel: disabled} - - {pin_num: '12', peripheral: USART0, signal: RXD, pin_signal: PIO0_9/SPI0_SSELN/USART0_RXD/CT32B1_CAP1/PWM9/USART1_SCK/ADO/PDM1_CLK, mode: pullUp, invert: disabled, - open_drain: disabled, filter_off: disabled, slew_rate_0: standard, slew_rate_1: standard, ssel: disabled} - * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS *********** - */ -/* clang-format on */ - -/* FUNCTION ************************************************************************************************************ - * - * Function Name : BOARD_InitAppUARTPins - * Description : Configures pin routing and optionally pin electrical features. - * - * END ****************************************************************************************************************/ -/* Function assigned for the Cortex-M4 */ -void BOARD_InitAppUARTPins(void) -{ - /* Enables the clock for the I/O controller block. 0: Disable. 1: Enable.: 0x01u */ - CLOCK_EnableClock(kCLOCK_Iocon); - - const uint32_t port0_pin8_config = (/* Pin is configured as USART0_TXD */ - IOCON_PIO_FUNC2 | - /* Selects pull-up function */ - IOCON_PIO_MODE_PULLUP | - /* Standard mode, output slew rate control is disabled */ - IOCON_PIO_SLEW0_STANDARD | - /* Input function is not inverted */ - IOCON_PIO_INV_DI | - /* Enables digital function */ - IOCON_PIO_DIGITAL_EN | - /* Input filter disabled */ - IOCON_PIO_INPFILT_OFF | - /* Standard mode, output slew rate control is disabled */ - IOCON_PIO_SLEW1_STANDARD | - /* Open drain is disabled */ - IOCON_PIO_OPENDRAIN_DI | - /* SSEL is disabled */ - IOCON_PIO_SSEL_DI); - /* PORT0 PIN8 (coords: 11) is configured as USART0_TXD */ - IOCON_PinMuxSet(IOCON, 0U, 8U, port0_pin8_config); - - const uint32_t port0_pin9_config = (/* Pin is configured as USART0_RXD */ - IOCON_PIO_FUNC2 | - /* Selects pull-up function */ - IOCON_PIO_MODE_PULLUP | - /* Standard mode, output slew rate control is disabled */ - IOCON_PIO_SLEW0_STANDARD | - /* Input function is not inverted */ - IOCON_PIO_INV_DI | - /* Enables digital function */ - IOCON_PIO_DIGITAL_EN | - /* Input filter disabled */ - IOCON_PIO_INPFILT_OFF | - /* Standard mode, output slew rate control is disabled */ - IOCON_PIO_SLEW1_STANDARD | - /* Open drain is disabled */ - IOCON_PIO_OPENDRAIN_DI | - /* SSEL is disabled */ - IOCON_PIO_SSEL_DI); - /* PORT0 PIN9 (coords: 12) is configured as USART0_RXD */ - IOCON_PinMuxSet(IOCON, 0U, 9U, port0_pin9_config); -} - -static void ConfigureDebugPort(void) -{ - /* SWD SWCLK/SWDIO pins */ - IOCON_PinMuxSet(IOCON, 0, 12, IOCON_FUNC2 | IOCON_MODE_INACT | IOCON_DIGITAL_EN); - IOCON_PinMuxSet(IOCON, 0, 13, IOCON_FUNC2 | IOCON_MODE_INACT | IOCON_DIGITAL_EN); -#ifdef ENABLE_DEBUG_PORT_SWO - /* SWD SWO pin (optional) */ - IOCON_PinMuxSet(IOCON, 0, 14, IOCON_FUNC5 | IOCON_MODE_INACT | IOCON_DIGITAL_EN); - SYSCON->TRACECLKDIV = 0; /* Clear HALT bit */ -#endif -} - -static void ConfigureDongleLEDs(void) -{ - const uint32_t port0_pin4_config = (/* Pin is configured as PIO0_4 */ - IOCON_PIO_FUNC0 | - /* Selects pull-up function */ - IOCON_PIO_MODE_PULLUP | - /* Standard mode, output slew rate control is disabled */ - IOCON_PIO_SLEW0_STANDARD | - /* Input function is not inverted */ - IOCON_PIO_INV_DI | - /* Enables digital function */ - IOCON_PIO_DIGITAL_EN | - /* Input filter disabled */ - IOCON_PIO_INPFILT_OFF | - /* Standard mode, output slew rate control is disabled */ - IOCON_PIO_SLEW1_STANDARD | - /* Open drain is disabled */ - IOCON_PIO_OPENDRAIN_DI | - /* SSEL is disabled */ - IOCON_PIO_SSEL_DI); - /* PORT0 PIN4 (coords: 7) is configured as PIO0_4 */ - IOCON_PinMuxSet(IOCON, 0U, 4U, port0_pin4_config); - - const uint32_t port0_pin10_config = - (/* Pin is configured as PIO0_10 */ - IOCON_PIO_FUNC0 | - /* GPIO mode */ - IOCON_PIO_EGP_GPIO | - /* IO is an open drain cell */ - IOCON_PIO_ECS_DI | - /* High speed IO for GPIO mode, IIC not */ - IOCON_PIO_EHS_DI | - /* Input function is not inverted */ - IOCON_PIO_INV_DI | - /* Enables digital function */ - IOCON_PIO_DIGITAL_EN | - /* Input filter disabled */ - IOCON_PIO_INPFILT_OFF | - /* IIC mode:Noise pulses below approximately 50ns are filtered out. GPIO mode:a 3ns filter */ - IOCON_PIO_FSEL_DI | - /* Open drain is disabled */ - IOCON_PIO_OPENDRAIN_DI | - /* IO_CLAMP disabled */ - IOCON_PIO_IO_CLAMP_DI); - /* PORT0 PIN10 (coords: 13) is configured as PIO0_10 */ - IOCON_PinMuxSet(IOCON, 0U, 10U, port0_pin10_config); -} - -/******************************************************************************* - * Code - ******************************************************************************/ -void BOARD_InitPins(void) -{ - /* Define the init structure for the output LED pin*/ - gpio_pin_config_t led_config = { - kGPIO_DigitalOutput, - 0, - }; - - /* Enable IOCON clock */ - CLOCK_EnableClock(kCLOCK_Iocon); - CLOCK_EnableClock(kCLOCK_InputMux); - - /* Debugger signals */ - ConfigureDebugPort(); - - BOARD_InitAppUARTPins(); - - ConfigureDongleLEDs(); - - /* IOCON clock left on, this is needed if CLKIN is used. */ - /* Initialize GPIO */ - CLOCK_EnableClock(kCLOCK_Gpio0); - RESET_PeripheralReset(kGPIO0_RST_SHIFT_RSTn); - - /* Init output LED GPIO. */ - GPIO_PortInit(BOARD_LED_USB_DONGLE_GPIO, BOARD_LED_USB_DONGLE_GPIO_PORT); - GPIO_PinInit(BOARD_LED_USB_DONGLE_GPIO, BOARD_LED_USB_DONGLE_GPIO_PORT, BOARD_LED_USB_DONGLE1_GPIO_PIN, &led_config); - GPIO_PinInit(BOARD_LED_USB_DONGLE_GPIO, BOARD_LED_USB_DONGLE_GPIO_PORT, BOARD_LED_USB_DONGLE2_GPIO_PIN, &led_config); - - GPIO_PortToggle(BOARD_LED_USB_DONGLE_GPIO, BOARD_LED_USB_DONGLE_GPIO_PORT, 1u << BOARD_LED_USB_DONGLE1_GPIO_PIN); -} - -void BOARD_LedDongleToggle(void) -{ - GPIO_PortToggle(BOARD_LED_USB_DONGLE_GPIO, BOARD_LED_USB_DONGLE_GPIO_PORT, 1u << BOARD_LED_USB_DONGLE1_GPIO_PIN); - GPIO_PortToggle(BOARD_LED_USB_DONGLE_GPIO, BOARD_LED_USB_DONGLE_GPIO_PORT, 1u << BOARD_LED_USB_DONGLE2_GPIO_PIN); -}