Skip to content

Commit

Permalink
Remove ST CRC library but keep using hardware-accelerated CRC32.
Browse files Browse the repository at this point in the history
Add integrity check of the vector table.

Signed-off-by: Marcos Chaparro <[email protected]>
  • Loading branch information
nitrousnrg committed Apr 16, 2019
1 parent 3c6083c commit 68ee05e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 226 deletions.
83 changes: 0 additions & 83 deletions ChibiOS_3.0.2/ext/stdperiph_stm32f4/inc/stm32f4xx_crc.h

This file was deleted.

133 changes: 0 additions & 133 deletions ChibiOS_3.0.2/ext/stdperiph_stm32f4/src/stm32f4xx_crc.c

This file was deleted.

1 change: 0 additions & 1 deletion ChibiOS_3.0.2/ext/stdperiph_stm32f4/stm32lib.mk
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ STM32SRC = ${CHIBIOS}/ext/stdperiph_stm32f4/src/misc.c \
${CHIBIOS}/ext/stdperiph_stm32f4/src/stm32f4xx_syscfg.c \
${CHIBIOS}/ext/stdperiph_stm32f4/src/stm32f4xx_tim.c \
${CHIBIOS}/ext/stdperiph_stm32f4/src/stm32f4xx_iwdg.c \
${CHIBIOS}/ext/stdperiph_stm32f4/src/stm32f4xx_crc.c \
${CHIBIOS}/ext/stdperiph_stm32f4/src/stm32f4xx_wwdg.c

STM32INC = ${CHIBIOS}/ext/stdperiph_stm32f4/inc
Expand Down
27 changes: 27 additions & 0 deletions crc.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#include "crc.h"
#include "stm32f4xx.h"

// CRC Table
const unsigned short crc16_tab[] = { 0x0000, 0x1021, 0x2042, 0x3063, 0x4084,
Expand Down Expand Up @@ -58,3 +59,29 @@ unsigned short crc16(unsigned char *buf, unsigned int len) {
}
return cksum;
}

/**
* @brief Computes the 32-bit CRC of a given buffer of data word(32-bit) using
* Hardware Acceleration.
* @param pBuffer: pointer to the buffer containing the data to be computed
* @param BufferLength: length of the buffer to be computed
* @retval 32-bit CRC
*/
uint32_t crc32(uint32_t *pBuffer, uint32_t BufferLength) {
uint32_t index = 0;

for(index = 0; index < BufferLength; index++) {
CRC->DR = pBuffer[index];
}
return (CRC->DR);
}

/**
* @brief Resets the CRC Data register (DR).
* @param None
* @retval None
*/
void crc32_reset(void) {
/* Reset CRC generator */
CRC->CR = CRC_CR_RESET;
}
4 changes: 4 additions & 0 deletions crc.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
#ifndef CRC_H_
#define CRC_H_

#include <stdint.h>

/*
* Functions
*/
unsigned short crc16(unsigned char *buf, unsigned int len);
uint32_t crc32(uint32_t *buf, uint32_t len);
void crc32_reset(void);

#endif /* CRC_H_ */
35 changes: 26 additions & 9 deletions flash_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
#include "ch.h"
#include "hal.h"
#include "stm32f4xx_conf.h"
#include "stm32f4xx_crc.h"
#include "utils.h"
#include "mc_interface.h"
#include "timeout.h"
#include "hw.h"
#include "crc.h"
#include <string.h>

/*
Expand Down Expand Up @@ -56,6 +56,12 @@
#define APP_CRC_WAS_CALCULATED_FLAG ((uint32_t)0xAAAAAAAA)
#define APP_CRC_WAS_CALCULATED_FLAG_ADDRESS (uint32_t*)(APP_MAX_SIZE - 8)

#define VECTOR_TABLE_ADDRESS (uint32_t *)ADDR_FLASH_SECTOR_0
#define VECTOR_TABLE_SIZE (ADDR_FLASH_SECTOR_1 - ADDR_FLASH_SECTOR_0)

#define APP_START_ADDRESS (uint32_t *)(ADDR_FLASH_SECTOR_1 + EEPROM_EMULATION_SIZE)
#define APP_SIZE (APP_MAX_SIZE - VECTOR_TABLE_SIZE - EEPROM_EMULATION_SIZE)

// Private constants
static const uint32_t flash_addr[FLASH_SECTORS] = {
ADDR_FLASH_SECTOR_0,
Expand Down Expand Up @@ -214,15 +220,19 @@ uint32_t flash_helper_verify_flash_memory(void) {
if( (APP_CRC_WAS_CALCULATED_FLAG_ADDRESS)[0] == APP_CRC_WAS_CALCULATED_FLAG )
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, ENABLE);
CRC_ResetDR();
crc32_reset();

// compute vector table (sector 0)
crc32((VECTOR_TABLE_ADDRESS), (VECTOR_TABLE_SIZE)/4);

// skip emulated EEPROM (sector 1 and 2)

// A CRC over the full image should return zero. Exclude the pages used for
// storing user configuration data.
crc= CRC_CalcBlockCRC((uint32_t*)( ADDR_FLASH_SECTOR_0 + EEPROM_EMULATION_SIZE),
(APP_MAX_SIZE - EEPROM_EMULATION_SIZE)/4);
// compute application code
crc = crc32(APP_START_ADDRESS, (APP_SIZE)/4);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, DISABLE);

// A CRC over the full image should return zero.
return (crc == 0)? FAULT_CODE_NONE : FAULT_CODE_FLASH_CORRUPTION;
}
else {
Expand All @@ -239,9 +249,16 @@ uint32_t flash_helper_verify_flash_memory(void) {

// Compute flash crc including the new flag
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, ENABLE);
CRC_ResetDR();
crc= CRC_CalcBlockCRC((uint32_t*)(ADDR_FLASH_SECTOR_0 + EEPROM_EMULATION_SIZE),
(APP_MAX_SIZE - EEPROM_EMULATION_SIZE - 4)/4);
crc32_reset();

// compute vector table (sector 0)
crc32(VECTOR_TABLE_ADDRESS, (VECTOR_TABLE_SIZE)/4);

// skip emulated EEPROM (sector 1 and 2)

// compute application code
crc = crc32(APP_START_ADDRESS, (APP_SIZE - 4)/4);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, DISABLE);

//Store CRC
Expand Down

0 comments on commit 68ee05e

Please sign in to comment.