Integrity checks with Embedded Studio and SEGGER Linker

From SEGGER Wiki
Jump to: navigation, search

This article will show how the integrity check feature of the SEGGER Linker can be used with algorithms like CRC, MD5, SHA and many more.

Prerequisites

To use this feature the following software versions of Embedded Studio and SEGGER Linker must be used:

  • SEGGER Embedded Studio V5.10 or later
  • SEGGER Linker V3.00 or later (included since the above Embedded Studio version)

Available algorithms

The following algorithms are currently available:

  • CRC-7/MMC
  • CRC-8
  • CRC-8/CDMA2000
  • CRC-8/DARC
  • CRC-8/MAXIM, CRC-8/1WIRE
  • CRC-8/AUTOSAR
  • CRC-8/BLUETOOTH
  • CRC-16/CCITT:AUG
  • CRC-16/CCITT:NOAUG
  • CRC-16/KERMIT
  • CRC-16/X.25
  • CRC-16/XMODEM
  • CRC-16/ARC
  • CRC-16/UMTS
  • CRC-16/MODBUS
  • CRC-16/USB
  • CRC-32
  • CRC-32/BZIP2
  • CRC-32/MPEG2, CRC-32/STM32
  • CRC-32/POSIX
  • CRC-32/XFER
  • CRC-32C
  • CRC-32D
  • CRC-32Q
  • CRC-CCITT:AUG
  • CRC-CCITT:NOAUG
  • MD5
  • RIPEMD-160
  • SHA-1
  • SHA-224
  • SHA-256
  • SHA-384
  • SHA-512
  • SHA-512/224
  • SHA-512/256
  • SHA3-224
  • SHA3-256
  • SHA3-384
  • SHA3-512
  • SM3


How to

  • First define a region for the area you want to do the integrity check on and a region where to place the result
  • As an example we will create a CRC over a complete Flash area and place the result at the end of Flash
define region FLASH       = [0x80000000 size 512k];
define region CRC         = [end(FLASH)-4, size 4];
define region APPLICATION = FLASH - CRC; 
  • Now place your application sections in region APPLICATION
  • Next do the CRC calculation with a selected algorithm e.g. CRC-32
place in CRC {
  integrity check of APPLICATION with algorithm="CRC-32" fill=0xFF
};

Now the CRC-32 result over the region APPLICATION is saved in region CRC. You can of course use other algorithms and other approaches to utilize the integrity check feature.

For more information see the SEGGER Linker reference manual UM20005

Example project

The following example project will show an example implementation of such integrity check on a Cortex-M Trace Reference Board based on a ST STM32F407VE target device.

In the example project the SEGGER Linker integrity check result will be compared to the result of the STM32 CRC peripheral.

Prerequisites

To be able to build and debug the project the following prerequisites must be met:

Project files

STM32F407_CRC_SEGGER_LINKER.zip

Example Application Source main.c

/*********************************************************************
*                    SEGGER Microcontroller GmbH                     *
*                        The Embedded Experts                        *
**********************************************************************
*                                                                    *
*            (c) 2014 - 2020 SEGGER Microcontroller GmbH             *
*                                                                    *
*           www.segger.com     Support: support@segger.com           *
*                                                                    *
**********************************************************************
*                                                                    *
* All rights reserved.                                               *
*                                                                    *
* Redistribution and use in source and binary forms, with or         *
* without modification, are permitted provided that the following    *
* conditions are met:                                                *
*                                                                    *
* - Redistributions of source code must retain the above copyright   *
*   notice, this list of conditions and the following disclaimer.    *
*                                                                    *
* - Neither the name of SEGGER Microcontroller GmbH                  *
*   nor the names of its contributors may be used to endorse or      *
*   promote products derived from this software without specific     *
*   prior written permission.                                        *
*                                                                    *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND             *
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,        *
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF           *
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE           *
* DISCLAIMED.                                                        *
* IN NO EVENT SHALL SEGGER Microcontroller GmbH BE LIABLE FOR        *
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR           *
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  *
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;    *
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF      *
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT          *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE  *
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH   *
* DAMAGE.                                                            *
*                                                                    *
**********************************************************************

-------------------------- END-OF-HEADER -----------------------------

File    : main.c
Purpose : Example application doing a CRC check using the STM32 CRC peripheral

*/

#include <stdio.h>
#include <stdlib.h>
#include "stm32f4xx.h"

#define FLASH_IMAGE_START 0x08000000
#define FLASH_IMAGE_END   0x0807FFFC
#define FLASH_APPLICATION_END (FLASH_IMAGE_END - 0x4)

/*********************************************************************
*
*       main()
*
*  Function description
*   Application entry point.
*/
int main(void) {
  int i;
  unsigned int NumItems;
  unsigned int* pData;
  unsigned int CRCResultHW;
  unsigned int CRCResult;
  unsigned int OldValue;
  
  i = 0;
  NumItems = (FLASH_APPLICATION_END - FLASH_IMAGE_START) / 4;
  pData = (unsigned int*)FLASH_IMAGE_START;    // points to start of Flash
  CRCResult = *(unsigned int*)FLASH_IMAGE_END; // Saves CRC value calculated by SEGGER Linker
  //
  // Config CRC Module
  //
  RCC->AHB1ENR |= RCC_AHB1ENR_CRCEN;
  CRC->CR |= CRC_CR_RESET;
  //
  // Calculate CRC with ST CRC unit over complete Flash area
  //
  do {
    CRC->DR = __REV(*pData);  // ST algorithm expects words in reversed order
    pData++;
  } while (NumItems--);
  CRCResultHW = CRC->DR;
  printf("Hardware calculated CRC over Flash is: 0x%X \n", CRCResultHW);
  printf("SEGGER Linker calculated CRC over Flash is: 0x%X\n", CRCResult);
  //
  // Compare with Linker result
  //
  if (CRCResult == CRCResultHW) {
    printf("Both CRC check sums are matching!\n");
  } else {
    printf("CRC check sums are not matching. Check parameters.\n");
  }
  do {
    i++;
  } while (1);
}

/*************************** End of file ****************************/