//! @brief Calculate crc on a range of flash, specified in the bootloader configuration area. static uint32_t calculate_application_crc32(bootloader_configuration_data_t * config) { uint32_t crc32; // Initialize the CRC32 information crc32_data_t crcInfo; crc32_init(&crcInfo); // Run CRC, Considering skip crcExpectedValue address uint32_t bypassStartAddress = kBootloaderConfigAreaAddress + ((uint32_t)&config->crcExpectedValue - (uint32_t)&config->tag); uint32_t bypassEndAddress = bypassStartAddress + sizeof(config->crcExpectedValue); if ((config->crcStartAddress >= bypassEndAddress) || (config->crcStartAddress + config->crcByteCount <= bypassStartAddress)) { crc32_update(&crcInfo, (uint8_t *)config->crcStartAddress, config->crcByteCount); } else { // Assume that crcExpectedValue address (4 byte) resides in crc addresses completely crc32_update(&crcInfo, (uint8_t *)config->crcStartAddress, bypassStartAddress - config->crcStartAddress); crc32_update(&crcInfo, (uint8_t *)bypassEndAddress, config->crcStartAddress + config->crcByteCount - bypassEndAddress); } // Finalize the CRC calculations crc32_finalize(&crcInfo, &crc32); return crc32; }
int main(int argc, char ** argv){ if(argc != 2){ return 1; } char * str = argv[1]; uint8_t crc_val[4]; { // generating CRC struct CRC32 crc; crc32_init(&crc, GENPOLY); uint8_t * p; for(p = str; *p; p += 1){ crc32_update(&crc, *p); } crc32_finalize(&crc); uint8_t i; for(i=0;i<4;i+=1){ crc_val[i] = crc32_get_reversed(&crc, i); } } int _return; { // checking crc struct CRC32 crc; crc32_init(&crc, GENPOLY); uint8_t * p; for(p = str; *p; p += 1){ crc32_update(&crc, *p); } uint8_t i; for(i=0;i<4;i+=1){ crc32_update(&crc, crc_val[i]); } if(crc32_check_zero(&crc)){ fprintf(stderr, "success!\n"); _return = 0; }else{ fprintf(stderr, "fail =(\n"); _return = 1; } } return _return; }
/* mode = 0/1/2 0=init, 1=update, 2=finalize */ uint32_t update_crc_ne(uint32_t old_crc, int crclen, void *data, int datasiz, int datalen, int mode) { uint32_t seed=old_crc; uint32_t new_crc; crc16_t crc16; crc24_t crc24; crc32_t crc32; unsigned int mask=0 ; int data_len=datasiz*datalen ; new_crc = seed ; if( (*little_endian) & (datasiz>1)){ /* multibyte tokens on little endian machine */ mask = datasiz-1 ; /* 0/1/3/7 magic mask to get stitching order count on little endian machines */ } if (crclen==16) { crc16 = seed & 0xFFFF ; if(mode==0) crc16 = crc16_init() ; crc16 = crc16_update_le(crc16, (const unsigned char *) data, data_len, mask) ; if(mode==2) crc16 = crc16_finalize(crc16) ; new_crc = crc16 ; } else if (crclen==24) { crc24 = seed & 0xFFFFFF ; if(mode==0) crc24 = crc24_init() ; crc24 = crc24_update_le(crc24, (const unsigned char *) data, data_len, mask) ; if(mode==2) crc24 = crc24_finalize(crc24) ; new_crc = crc24 ; } else if (crclen==32) { crc32 = seed & 0xFFFFFFFF ; if(mode==0) crc32 = crc32_init() ; crc32 = crc32_update_le(crc32, (const unsigned char *) data, data_len, mask) ; if(mode==2) crc32 = crc32_finalize(crc32) ; new_crc = crc32 ; } else { new_crc = seed ; } return new_crc; }
int app_ok(){ if (!APP_RANGE_VALID(APP_START, app_info->image_size)) { return 0; } uint32_t crc = crc32_init(); crc = crc32_update(crc, (void*)APP_START, app_info->image_size); crc = crc32_finalize(crc); if (crc != 0) { return 0; } return 1; }