Checksum section in CrossStudio

Hello Forum members,

Has anyone worked on CHECKSUM section from CrossStudio compiler? How do you implement the CHECKSUM for required sections?

How to access these checksum value from code?

How do you verify these values externally, any application?

Any type of help will be highly appreciated. Thanks in advance.

You need to set the options in the ‘Linker Options’ in the Project properties. Set “Checksum Algorithm” to “CRC16” and set the “Checksum Sections” to “CONST;CODE”.

Below is what I use to verify the checksums:

// Symbols generated by the linker for the CHECKSUM section
extern CHECKSUM_INFO_t __begin_CHECKSUM[];

************************************************
*  Main program entry
*
*************************************************/
int main(void)
{
  uint16_t	                 calculated_checksum;
  CHECKSUM_INFO_t	*p;

  // Verify the checksum of the flash memory
  for (p = __begin_CHECKSUM; p->begin || p->end; ++p)
  {
	calculated_checksum =  __checksum_crc16(p->begin, p->end);
		 
	if ( calculated_checksum != p->checksum )
	{
	    // your checksum failure procedure here
	}
  }

  // more code....
}

/* Function to compute a CRC16 checksum over the supplied memory range */
unsigned int __checksum_crc16(unsigned char *begin, unsigned char *end)
{
  unsigned short crc = 0;
  while (begin < end)
    {
      crc  = (unsigned char)(crc >> 8) | (crc << 8);
      crc ^= *begin++;
      crc ^= (unsigned char)(crc & 0xff) >> 4;
      crc ^= (crc << 8) << 4;
      crc ^= ((crc & 0xff) << 4) << 1;
    }
  return crc;
}

Thanks gm!!

Hello gm,

Thanks for your reply.

I tried the implementation but while checking the calculated CRC and algorithm CRC, it is failing. I even observed that the start addresses for CONST and CODE blocks are defferring from that defined in section placement file. If I have my CONST section starting from 0x2100 to 0x2FFF then the p->begin=0x2100 but p->end=0x0000. This is bit confusing. Do I need to make any other changes to CrossStudio 2?

Thanks in advance.

I use the following in my code… I checksum the individual sections… Be aware that the “X” architecture can create a few issues so care has to be taken what the address space is… there is also another section that the compiler creates for the stubs needed to access code across the 64k boundary…

In the project under checksum you need to enable checksumming of these sections. (CODE,ISR,CONST)

/* Symbols generated by the linker for the CHECKSUM section */

extern checksum_info __begin_CHECKSUM;

/* Symbols generated by the linker to delimit the CODE and CONST sections */

extern unsigned char __begin_CODE, __end_CODE; /* crossworks documentation had variable incorrectly as __start_CODE */

extern unsigned char __begin_ISR, __end_ISR; /* crossworks documentation had variable incorrectly as __start_ISR */

extern unsigned char __begin_CONST, __end_CONST; /* crossworks documentation had variable incorrectly as __start_CONST */

/* Symbols generated by the linker for the CODE and CONST checksums */

extern unsigned int __checksum_CODE, __checksum_CONST, __checksum_ISR;

unsigned int Calc_checksum_CODE;

unsigned int Calc_checksum_ISR;

unsigned int Calc_checksum_CONST;

Calc_checksum_CODE = __checksum_crc16(__begin_CODE, __end_CODE);

Calc_checksum_ISR = __checksum_crc16(__begin_ISR, __end_ISR);

Calc_checksum_CONST = __checksum_crc16(__begin_CONST, __end_CONST);

Oops! Forgot to give you the struct that is needed. I was also having a problem with p->end being null. After I looked at the disassembly it appeared that the pointers were 32-bit rather than the expected 16-bit ones. I compensated for those in a modified struct.

Here’s the struct that I use:

/* Checksum structure */
typedef struct {
  unsigned char *begin;
	unsigned int	pad1;		// upper word of begin
  unsigned char *end;
	unsigned int	pad2;		// upper word of end
  unsigned int	checksum;
} CHECKSUM_INFO_t;

Never did get an answer from Rowley about this but since it was resolved I just went on.

Thanks gm and jradomski. I will implement your suggestions and let you know.

Thanks again.

Thanks gm and jradomski.