From fc87eb708ebc0efa5505f824e79946a33f68437b Mon Sep 17 00:00:00 2001 From: Frank Voorburg Date: Wed, 27 Sep 2017 07:24:27 +0000 Subject: [PATCH] Refs #104. Implemented configurable FlashCryptoDecryptDataHook() in all flash drivers. It can be used for implementing program code decryption logic. git-svn-id: https://svn.code.sf.net/p/openblt/code/trunk@368 5dc33758-31d5-4daf-9ae8-b24bf3d40d73 --- Target/Source/ARM7_LPC2000/flash.c | 35 +++++++++++++++++++++++++++ Target/Source/ARMCM0_STM32F0/flash.c | 35 +++++++++++++++++++++++++++ Target/Source/ARMCM0_XMC1/flash.c | 36 +++++++++++++++++++++++++++- Target/Source/ARMCM3_EFM32/flash.c | 35 ++++++++++++++++++++++++++- Target/Source/ARMCM3_LM3S/flash.c | 35 +++++++++++++++++++++++++++ Target/Source/ARMCM3_STM32F1/flash.c | 35 +++++++++++++++++++++++++++ Target/Source/ARMCM3_STM32F2/flash.c | 35 +++++++++++++++++++++++++++ Target/Source/ARMCM4_STM32F3/flash.c | 34 ++++++++++++++++++++++++++ Target/Source/ARMCM4_STM32F4/flash.c | 35 +++++++++++++++++++++++++++ Target/Source/ARMCM4_TM4C/flash.c | 34 ++++++++++++++++++++++++++ Target/Source/ARMCM4_XMC4/flash.c | 34 ++++++++++++++++++++++++++ Target/Source/HCS12/flash.c | 35 +++++++++++++++++++++++++++ Target/Source/TRICORE_TC1798/flash.c | 35 +++++++++++++++++++++++++++ Target/Source/plausibility.h | 12 ++++++++++ 14 files changed, 463 insertions(+), 2 deletions(-) diff --git a/Target/Source/ARM7_LPC2000/flash.c b/Target/Source/ARM7_LPC2000/flash.c index 0476cff6..8676b9a2 100644 --- a/Target/Source/ARM7_LPC2000/flash.c +++ b/Target/Source/ARM7_LPC2000/flash.c @@ -99,6 +99,14 @@ typedef struct } tFlashBlockInfo; +/**************************************************************************************** +* Hook functions +****************************************************************************************/ +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) +extern blt_bool FlashCryptoDecryptDataHook(blt_int8u * data, blt_int32u size); +#endif + + /**************************************************************************************** * Function prototypes ****************************************************************************************/ @@ -352,6 +360,16 @@ blt_bool FlashWriteChecksum(void) return BLT_TRUE; } +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + /* perform decryption of the bootblock, before calculating the checksum and writing it + * to flash memory. + */ + if (FlashCryptoDecryptDataHook(bootBlockInfo.data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } +#endif + /* compute the checksum. note that the user program's vectors are not yet written * to flash but are present in the bootblock data structure at this point. */ @@ -617,6 +635,23 @@ static blt_bool FlashWriteBlock(tFlashBlockInfo *block) { return BLT_FALSE; } + +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + #if (BOOT_NVM_CHECKSUM_HOOKS_ENABLE == 0) + /* note that the bootblock is already decrypted in FlashWriteChecksum(), if the + * internal checksum mechanism is used. Therefore don't decrypt it again. + */ + if (block != &bootBlockInfo) + #endif + { + /* perform decryption of the program data before writing it to flash memory. */ + if (FlashCryptoDecryptDataHook(block->data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } + } +#endif + /* send the prepare sector command for just this one sector */ iap_command[0] = IAP_CMD_PREPARE_SECTORS; iap_command[1] = sector_num; diff --git a/Target/Source/ARMCM0_STM32F0/flash.c b/Target/Source/ARMCM0_STM32F0/flash.c index 4b8789c0..4db666ea 100644 --- a/Target/Source/ARMCM0_STM32F0/flash.c +++ b/Target/Source/ARMCM0_STM32F0/flash.c @@ -100,6 +100,14 @@ typedef struct } tFlashBlockInfo; +/**************************************************************************************** +* Hook functions +****************************************************************************************/ +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) +extern blt_bool FlashCryptoDecryptDataHook(blt_int8u * data, blt_int32u size); +#endif + + /**************************************************************************************** * Function prototypes ****************************************************************************************/ @@ -349,6 +357,16 @@ blt_bool FlashWriteChecksum(void) return BLT_TRUE; } +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + /* perform decryption of the bootblock, before calculating the checksum and writing it + * to flash memory. + */ + if (FlashCryptoDecryptDataHook(bootBlockInfo.data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } +#endif + /* compute the checksum. note that the user program's vectors are not yet written * to flash but are present in the bootblock data structure at this point. */ @@ -614,6 +632,23 @@ static blt_bool FlashWriteBlock(tFlashBlockInfo *block) { return BLT_FALSE; } + +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + #if (BOOT_NVM_CHECKSUM_HOOKS_ENABLE == 0) + /* note that the bootblock is already decrypted in FlashWriteChecksum(), if the + * internal checksum mechanism is used. Therefore don't decrypt it again. + */ + if (block != &bootBlockInfo) + #endif + { + /* perform decryption of the program data before writing it to flash memory. */ + if (FlashCryptoDecryptDataHook(block->data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } + } +#endif + /* unlock the flash array */ FLASH_Unlock(); /* clear pending flags (if any) */ diff --git a/Target/Source/ARMCM0_XMC1/flash.c b/Target/Source/ARMCM0_XMC1/flash.c index 2963e0b7..0a057ab7 100644 --- a/Target/Source/ARMCM0_XMC1/flash.c +++ b/Target/Source/ARMCM0_XMC1/flash.c @@ -106,6 +106,14 @@ typedef struct } tFlashBlockInfo; +/**************************************************************************************** +* Hook functions +****************************************************************************************/ +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) +extern blt_bool FlashCryptoDecryptDataHook(blt_int8u * data, blt_int32u size); +#endif + + /**************************************************************************************** * Function prototypes ****************************************************************************************/ @@ -374,6 +382,16 @@ blt_bool FlashWriteChecksum(void) return BLT_TRUE; } +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + /* perform decryption of the bootblock, before calculating the checksum and writing it + * to flash memory. + */ + if (FlashCryptoDecryptDataHook(bootBlockInfo.data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } +#endif + /* compute the checksum. note that the user program's vectors are not yet written * to flash but are present in the bootblock data structure at this point. */ @@ -633,7 +651,23 @@ static blt_bool FlashWriteBlock(tFlashBlockInfo *block) { return BLT_FALSE; } - + +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + #if (BOOT_NVM_CHECKSUM_HOOKS_ENABLE == 0) + /* note that the bootblock is already decrypted in FlashWriteChecksum(), if the + * internal checksum mechanism is used. Therefore don't decrypt it again. + */ + if (block != &bootBlockInfo) + #endif + { + /* perform decryption of the program data before writing it to flash memory. */ + if (FlashCryptoDecryptDataHook(block->data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } + } +#endif + /* clear the previous error states. should always be done before program/erase */ XMC_FLASH_ClearStatus(); /* determine timeout time of the operation */ diff --git a/Target/Source/ARMCM3_EFM32/flash.c b/Target/Source/ARMCM3_EFM32/flash.c index abea9476..37bd261a 100644 --- a/Target/Source/ARMCM3_EFM32/flash.c +++ b/Target/Source/ARMCM3_EFM32/flash.c @@ -93,6 +93,14 @@ typedef struct } tFlashBlockInfo; +/**************************************************************************************** +* Hook functions +****************************************************************************************/ +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) +extern blt_bool FlashCryptoDecryptDataHook(blt_int8u * data, blt_int32u size); +#endif + + /**************************************************************************************** * Function prototypes ****************************************************************************************/ @@ -346,6 +354,16 @@ blt_bool FlashWriteChecksum(void) return BLT_TRUE; } +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + /* perform decryption of the bootblock, before calculating the checksum and writing it + * to flash memory. + */ + if (FlashCryptoDecryptDataHook(bootBlockInfo.data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } +#endif + /* compute the checksum. note that the user program's vectors are not yet written * to flash but are present in the bootblock data structure at this point. */ @@ -607,7 +625,6 @@ static blt_bool FlashWriteBlock(tFlashBlockInfo *block) blt_int32u prog_data; blt_int32u word_cnt; - /* check that address is actually within flash */ sector_num = FlashGetSector(block->base_addr); if (sector_num == FLASH_INVALID_SECTOR) @@ -615,6 +632,22 @@ static blt_bool FlashWriteBlock(tFlashBlockInfo *block) return BLT_FALSE; } +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + #if (BOOT_NVM_CHECKSUM_HOOKS_ENABLE == 0) + /* note that the bootblock is already decrypted in FlashWriteChecksum(), if the + * internal checksum mechanism is used. Therefore don't decrypt it again. + */ + if (block != &bootBlockInfo) + #endif + { + /* perform decryption of the program data before writing it to flash memory. */ + if (FlashCryptoDecryptDataHook(block->data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } + } +#endif + /* program all words in the block one by one */ for (word_cnt=0; word_cnt<(FLASH_WRITE_BLOCK_SIZE/sizeof(blt_int32u)); word_cnt++) { diff --git a/Target/Source/ARMCM3_LM3S/flash.c b/Target/Source/ARMCM3_LM3S/flash.c index 4bcf16ce..d5f23e3c 100644 --- a/Target/Source/ARMCM3_LM3S/flash.c +++ b/Target/Source/ARMCM3_LM3S/flash.c @@ -97,6 +97,14 @@ typedef struct } tFlashBlockInfo; +/**************************************************************************************** +* Hook functions +****************************************************************************************/ +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) +extern blt_bool FlashCryptoDecryptDataHook(blt_int8u * data, blt_int32u size); +#endif + + /**************************************************************************************** * Function prototypes ****************************************************************************************/ @@ -347,6 +355,16 @@ blt_bool FlashWriteChecksum(void) return BLT_TRUE; } +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + /* perform decryption of the bootblock, before calculating the checksum and writing it + * to flash memory. + */ + if (FlashCryptoDecryptDataHook(bootBlockInfo.data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } +#endif + /* compute the checksum. note that the user program's vectors are not yet written * to flash but are present in the bootblock data structure at this point. */ @@ -612,6 +630,23 @@ static blt_bool FlashWriteBlock(tFlashBlockInfo *block) { return BLT_FALSE; } + +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + #if (BOOT_NVM_CHECKSUM_HOOKS_ENABLE == 0) + /* note that the bootblock is already decrypted in FlashWriteChecksum(), if the + * internal checksum mechanism is used. Therefore don't decrypt it again. + */ + if (block != &bootBlockInfo) + #endif + { + /* perform decryption of the program data before writing it to flash memory. */ + if (FlashCryptoDecryptDataHook(block->data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } + } +#endif + /* program all words in the block one by one */ for (word_cnt=0; word_cnt<(FLASH_WRITE_BLOCK_SIZE/sizeof(blt_int32u)); word_cnt++) { diff --git a/Target/Source/ARMCM3_STM32F1/flash.c b/Target/Source/ARMCM3_STM32F1/flash.c index 0d2e4cf3..5e91a5ba 100644 --- a/Target/Source/ARMCM3_STM32F1/flash.c +++ b/Target/Source/ARMCM3_STM32F1/flash.c @@ -126,6 +126,14 @@ typedef struct } tFlashRegs; +/**************************************************************************************** +* Hook functions +****************************************************************************************/ +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) +extern blt_bool FlashCryptoDecryptDataHook(blt_int8u * data, blt_int32u size); +#endif + + /**************************************************************************************** * Function prototypes ****************************************************************************************/ @@ -378,6 +386,16 @@ blt_bool FlashWriteChecksum(void) return BLT_TRUE; } +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + /* perform decryption of the bootblock, before calculating the checksum and writing it + * to flash memory. + */ + if (FlashCryptoDecryptDataHook(bootBlockInfo.data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } +#endif + /* compute the checksum. note that the user program's vectors are not yet written * to flash but are present in the bootblock data structure at this point. */ @@ -643,6 +661,23 @@ static blt_bool FlashWriteBlock(tFlashBlockInfo *block) { return BLT_FALSE; } + +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + #if (BOOT_NVM_CHECKSUM_HOOKS_ENABLE == 0) + /* note that the bootblock is already decrypted in FlashWriteChecksum(), if the + * internal checksum mechanism is used. Therefore don't decrypt it again. + */ + if (block != &bootBlockInfo) + #endif + { + /* perform decryption of the program data before writing it to flash memory. */ + if (FlashCryptoDecryptDataHook(block->data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } + } +#endif + /* unlock the flash array */ FlashUnlock(); /* check that the flash peripheral is not busy */ diff --git a/Target/Source/ARMCM3_STM32F2/flash.c b/Target/Source/ARMCM3_STM32F2/flash.c index e7386fdf..d4419093 100644 --- a/Target/Source/ARMCM3_STM32F2/flash.c +++ b/Target/Source/ARMCM3_STM32F2/flash.c @@ -93,6 +93,14 @@ typedef struct } tFlashBlockInfo; +/**************************************************************************************** +* Hook functions +****************************************************************************************/ +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) +extern blt_bool FlashCryptoDecryptDataHook(blt_int8u * data, blt_int32u size); +#endif + + /**************************************************************************************** * Function prototypes ****************************************************************************************/ @@ -339,6 +347,16 @@ blt_bool FlashWriteChecksum(void) return BLT_TRUE; } +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + /* perform decryption of the bootblock, before calculating the checksum and writing it + * to flash memory. + */ + if (FlashCryptoDecryptDataHook(bootBlockInfo.data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } +#endif + /* compute the checksum. note that the user program's vectors are not yet written * to flash but are present in the bootblock data structure at this point. */ @@ -604,6 +622,23 @@ static blt_bool FlashWriteBlock(tFlashBlockInfo *block) { return BLT_FALSE; } + +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + #if (BOOT_NVM_CHECKSUM_HOOKS_ENABLE == 0) + /* note that the bootblock is already decrypted in FlashWriteChecksum(), if the + * internal checksum mechanism is used. Therefore don't decrypt it again. + */ + if (block != &bootBlockInfo) + #endif + { + /* perform decryption of the program data before writing it to flash memory. */ + if (FlashCryptoDecryptDataHook(block->data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } + } +#endif + /* unlock the flash array */ FLASH_Unlock(); /* clear pending flags (if any) */ diff --git a/Target/Source/ARMCM4_STM32F3/flash.c b/Target/Source/ARMCM4_STM32F3/flash.c index a655f3a3..c58d7773 100644 --- a/Target/Source/ARMCM4_STM32F3/flash.c +++ b/Target/Source/ARMCM4_STM32F3/flash.c @@ -99,6 +99,14 @@ typedef struct } tFlashBlockInfo; +/**************************************************************************************** +* Hook functions +****************************************************************************************/ +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) +extern blt_bool FlashCryptoDecryptDataHook(blt_int8u * data, blt_int32u size); +#endif + + /**************************************************************************************** * Function prototypes ****************************************************************************************/ @@ -382,6 +390,16 @@ blt_bool FlashWriteChecksum(void) return BLT_TRUE; } +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + /* perform decryption of the bootblock, before calculating the checksum and writing it + * to flash memory. + */ + if (FlashCryptoDecryptDataHook(bootBlockInfo.data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } +#endif + /* compute the checksum. note that the user program's vectors are not yet written * to flash but are present in the bootblock data structure at this point. */ @@ -640,6 +658,22 @@ static blt_bool FlashWriteBlock(tFlashBlockInfo *block) blt_int32u word_cnt; blt_bool result = BLT_TRUE; +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + #if (BOOT_NVM_CHECKSUM_HOOKS_ENABLE == 0) + /* note that the bootblock is already decrypted in FlashWriteChecksum(), if the + * internal checksum mechanism is used. Therefore don't decrypt it again. + */ + if (block != &bootBlockInfo) + #endif + { + /* perform decryption of the program data before writing it to flash memory. */ + if (FlashCryptoDecryptDataHook(block->data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } + } +#endif + /* unlock the flash array */ FLASH_Unlock(); diff --git a/Target/Source/ARMCM4_STM32F4/flash.c b/Target/Source/ARMCM4_STM32F4/flash.c index 4c0d56aa..b0b28b71 100644 --- a/Target/Source/ARMCM4_STM32F4/flash.c +++ b/Target/Source/ARMCM4_STM32F4/flash.c @@ -94,6 +94,14 @@ typedef struct } tFlashBlockInfo; +/**************************************************************************************** +* Hook functions +****************************************************************************************/ +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) +extern blt_bool FlashCryptoDecryptDataHook(blt_int8u * data, blt_int32u size); +#endif + + /**************************************************************************************** * Function prototypes ****************************************************************************************/ @@ -362,6 +370,16 @@ blt_bool FlashWriteChecksum(void) return BLT_TRUE; } +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + /* perform decryption of the bootblock, before calculating the checksum and writing it + * to flash memory. + */ + if (FlashCryptoDecryptDataHook(bootBlockInfo.data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } +#endif + /* compute the checksum. note that the user program's vectors are not yet written * to flash but are present in the bootblock data structure at this point. */ @@ -627,6 +645,23 @@ static blt_bool FlashWriteBlock(tFlashBlockInfo *block) { return BLT_FALSE; } + +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + #if (BOOT_NVM_CHECKSUM_HOOKS_ENABLE == 0) + /* note that the bootblock is already decrypted in FlashWriteChecksum(), if the + * internal checksum mechanism is used. Therefore don't decrypt it again. + */ + if (block != &bootBlockInfo) + #endif + { + /* perform decryption of the program data before writing it to flash memory. */ + if (FlashCryptoDecryptDataHook(block->data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } + } +#endif + /* unlock the flash array */ FLASH_Unlock(); /* clear pending flags (if any) */ diff --git a/Target/Source/ARMCM4_TM4C/flash.c b/Target/Source/ARMCM4_TM4C/flash.c index e5626bad..62130df3 100644 --- a/Target/Source/ARMCM4_TM4C/flash.c +++ b/Target/Source/ARMCM4_TM4C/flash.c @@ -99,6 +99,14 @@ typedef struct } tFlashBlockInfo; +/**************************************************************************************** +* Hook functions +****************************************************************************************/ +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) +extern blt_bool FlashCryptoDecryptDataHook(blt_int8u * data, blt_int32u size); +#endif + + /**************************************************************************************** * Function prototypes ****************************************************************************************/ @@ -349,6 +357,16 @@ blt_bool FlashWriteChecksum(void) return BLT_TRUE; } +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + /* perform decryption of the bootblock, before calculating the checksum and writing it + * to flash memory. + */ + if (FlashCryptoDecryptDataHook(bootBlockInfo.data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } +#endif + /* compute the checksum. note that the user program's vectors are not yet written * to flash but are present in the bootblock data structure at this point. */ @@ -615,6 +633,22 @@ static blt_bool FlashWriteBlock(tFlashBlockInfo *block) return BLT_FALSE; } +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + #if (BOOT_NVM_CHECKSUM_HOOKS_ENABLE == 0) + /* note that the bootblock is already decrypted in FlashWriteChecksum(), if the + * internal checksum mechanism is used. Therefore don't decrypt it again. + */ + if (block != &bootBlockInfo) + #endif + { + /* perform decryption of the program data before writing it to flash memory. */ + if (FlashCryptoDecryptDataHook(block->data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } + } +#endif + /* program all words in the block one by one */ for (word_cnt=0; word_cnt<(FLASH_WRITE_BLOCK_SIZE/sizeof(blt_int32u)); word_cnt++) { diff --git a/Target/Source/ARMCM4_XMC4/flash.c b/Target/Source/ARMCM4_XMC4/flash.c index a65b999d..565a97b6 100644 --- a/Target/Source/ARMCM4_XMC4/flash.c +++ b/Target/Source/ARMCM4_XMC4/flash.c @@ -122,6 +122,14 @@ typedef struct } tFlashBlockInfo; +/**************************************************************************************** +* Hook functions +****************************************************************************************/ +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) +extern blt_bool FlashCryptoDecryptDataHook(blt_int8u * data, blt_int32u size); +#endif + + /**************************************************************************************** * Function prototypes ****************************************************************************************/ @@ -364,6 +372,16 @@ blt_bool FlashWriteChecksum(void) return BLT_TRUE; } +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + /* perform decryption of the bootblock, before calculating the checksum and writing it + * to flash memory. + */ + if (FlashCryptoDecryptDataHook(bootBlockInfo.data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } +#endif + /* compute the checksum. note that the user program's vectors are not yet written * to flash but are present in the bootblock data structure at this point. */ @@ -636,6 +654,22 @@ static blt_bool FlashWriteBlock(tFlashBlockInfo *block) return BLT_FALSE; } +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + #if (BOOT_NVM_CHECKSUM_HOOKS_ENABLE == 0) + /* note that the bootblock is already decrypted in FlashWriteChecksum(), if the + * internal checksum mechanism is used. Therefore don't decrypt it again. + */ + if (block != &bootBlockInfo) + #endif + { + /* perform decryption of the program data before writing it to flash memory. */ + if (FlashCryptoDecryptDataHook(block->data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } + } +#endif + /* program all pages in the block one by one */ for (page_cnt=0; page_cnt< (FLASH_WRITE_BLOCK_SIZE/FLASH_WRITE_PAGE_SIZE); page_cnt++) { diff --git a/Target/Source/HCS12/flash.c b/Target/Source/HCS12/flash.c index 84ee43ae..fdb27f4f 100644 --- a/Target/Source/HCS12/flash.c +++ b/Target/Source/HCS12/flash.c @@ -156,6 +156,14 @@ typedef volatile struct typedef void (*pFlashExeCmdFct)(void); +/**************************************************************************************** +* Hook functions +****************************************************************************************/ +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) +extern blt_bool FlashCryptoDecryptDataHook(blt_int8u * data, blt_int32u size); +#endif + + /**************************************************************************************** * Function prototypes ****************************************************************************************/ @@ -552,6 +560,16 @@ blt_bool FlashWriteChecksum(void) return BLT_TRUE; } +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + /* perform decryption of the bootblock, before calculating the checksum and writing it + * to flash memory. + */ + if (FlashCryptoDecryptDataHook(bootBlockInfo.data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } +#endif + /* the bootblock contains the data for the last sector in flashLayout. the * user program vector table and the checkum will be located at the end * of this block. first determine the offset in the bootblock data to @@ -870,6 +888,23 @@ static blt_bool FlashWriteBlock(tFlashBlockInfo *block) { return BLT_FALSE; } + +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + #if (BOOT_NVM_CHECKSUM_HOOKS_ENABLE == 0) + /* note that the bootblock is already decrypted in FlashWriteChecksum(), if the + * internal checksum mechanism is used. Therefore don't decrypt it again. + */ + if (block != &bootBlockInfo) + #endif + { + /* perform decryption of the program data before writing it to flash memory. */ + if (FlashCryptoDecryptDataHook(block->data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } + } +#endif + /* program all words in the block one by one */ for (word_cnt=0; word_cnt<(FLASH_WRITE_BLOCK_SIZE/sizeof(blt_int16u)); word_cnt++) { diff --git a/Target/Source/TRICORE_TC1798/flash.c b/Target/Source/TRICORE_TC1798/flash.c index d1910b56..d850699a 100644 --- a/Target/Source/TRICORE_TC1798/flash.c +++ b/Target/Source/TRICORE_TC1798/flash.c @@ -133,6 +133,14 @@ typedef struct } tFlashBlockInfo; +/**************************************************************************************** +* Hook functions +****************************************************************************************/ +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) +extern blt_bool FlashCryptoDecryptDataHook(blt_int8u * data, blt_int32u size); +#endif + + /**************************************************************************************** * Function prototypes ****************************************************************************************/ @@ -361,6 +369,16 @@ blt_bool FlashWriteChecksum(void) return BLT_TRUE; } +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + /* perform decryption of the bootblock, before calculating the checksum and writing it + * to flash memory. + */ + if (FlashCryptoDecryptDataHook(bootBlockInfo.data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } +#endif + /* compute the checksum. note that the data in the checksum range is not yet written * to flash but is present in the bootblock data structure at this point. */ @@ -622,6 +640,23 @@ static blt_bool FlashWriteBlock(tFlashBlockInfo *block) { return BLT_FALSE; } + +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) + #if (BOOT_NVM_CHECKSUM_HOOKS_ENABLE == 0) + /* note that the bootblock is already decrypted in FlashWriteChecksum(), if the + * internal checksum mechanism is used. Therefore don't decrypt it again. + */ + if (block != &bootBlockInfo) + #endif + { + /* perform decryption of the program data before writing it to flash memory. */ + if (FlashCryptoDecryptDataHook(block->data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) + { + return BLT_FALSE; + } + } +#endif + /* the FLASH_WRITE_BLOCK_SIZE is configured to exactly match the size of a page in * PFLASH. so here simply need to program one page in PFLASH. */ diff --git a/Target/Source/plausibility.h b/Target/Source/plausibility.h index 6755019f..50336a9a 100644 --- a/Target/Source/plausibility.h +++ b/Target/Source/plausibility.h @@ -433,6 +433,18 @@ #endif +/**************************************************************************************** +* F L A S H D R I V E R C O N F I G U R A T I O N C H E C K +****************************************************************************************/ +#ifndef BOOT_FLASH_CRYPTO_HOOKS_ENABLE +#define BOOT_FLASH_CRYPTO_HOOKS_ENABLE (0) +#endif + +#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE < 0) || (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 1) +#error "BOOT_FLASH_CRYPTO_HOOKS_ENABLE must be 0 or 1" +#endif + + /**************************************************************************************** * N V M D R I V E R C O N F I G U R A T I O N C H E C K ****************************************************************************************/