L
L
list_get2015-05-19 09:51:09
Microcontrollers
list_get, 2015-05-19 09:51:09

How to implement protection/verification of microcontroller flash memory at power on?

Good afternoon. Recently I came across on forums about the possibility of verifying source codes (during operation) into the flash memory of the microcontroller by calculating the checksum. Therefore, I would like to ask if anyone can share an example for the implementation of this task. The checksum will be calculated using CRC16.
I will be very grateful if somebody will throw a small example.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
list_get, 2015-05-19
@list_get

difficulty in misunderstanding the software aspects of implementation.
Here I sketched a piece of code editing with crc32.

#include <crc32.h>

#define  BLOCK_SIZE		0x400			// block flash
#define  START_ADDR		0x000000UL
#define  END_ADDR		0x008000UL  

void crc_flash()
{
  int result;
  uint32_t  crc;
  for(int i = (uint32_t)START_ADDR ; i < END_ADDR; i += BLOCK_SIZE)
  {			
    //			     data     len
    crc = data_crc16(&i	    , END_ADDR);
    result  += crc;                  
  }	
}

Please tell me how correct this is.
In fact, it turned out to be much easier than expected.
data = сюда помещаем адрес для начала расчета
len   = объем памяти для расчета
uint16_t data_crc16_flash(unsigned char *data, int len)
{
  unsigned short crc = 0xFFFF;
  while(len--)
    crc = (crc >> 8) ^ crc16_table[(crc & 0xff) ^ *data++];
    return crc;
}

at the output we get our checksum.

A
Alexey Cheremisin, 2015-05-19
@leahch

What is the difficulty? We go through the entire address space of flash and put its CRC sum at the very end. If this is done during firmware, then it can be done at the assembly stage in some block, for example, immediately after the interrupt table. If this is done inside the controller, then it is better to put the CRC separately in EEPROM and protect the CRC itself from being changed by the hash. If the flash is divided into blocks, which, for example, are flashed on the fly during operation (updating something), then again we store the CRC of the block and protect it with a hash. The hash is needed if suddenly someone changed the block and CRC.

A
Antony, 2015-05-19
@RiseOfDeath

CRC16 (like other CRCs) is only good for protecting against inadvertent corruption.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question