Answer the question
In order to leave comments, you need to log in
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
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;
}
}
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;
}
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question