void app_crc (char *str,int len) { mico_logic_partition_t *partition_flash; CRC16_Context mfg_context; uint8_t *mfgbuf; uint16_t crc = 0; uint32_t flash_addr = 0x0; int flash_len,buf_len; mfgbuf = malloc (1024); partition_flash = MicoFlashGetInfo (MICO_PARTITION_APPLICATION); flash_len = partition_flash->partition_length; CRC16_Init (&mfg_context); while (flash_len > 0) { if (flash_len > 1024) { buf_len = 1024; } else { buf_len = flash_len; } flash_len -= buf_len; MicoFlashRead (MICO_PARTITION_APPLICATION, &flash_addr, (uint8_t *)mfgbuf, buf_len); CRC16_Update (&mfg_context, (uint8_t *)mfgbuf, buf_len); } CRC16_Final (&mfg_context, &crc); snprintf (str, len, "%04X", crc); }
u32 MX_FirmwareUpdateFinish(u32 u32TotalLen) { #if 1 mico_logic_partition_t* part; int len; OSStatus err = kNoErr; uint32_t update_data_offset = 0x0; CRC16_Context crc_context; u32 total_len = u32TotalLen; CRC16_Init( &crc_context ); mico_logic_partition_t *para_partition_info; para_partition_info = MicoFlashGetInfo(MICO_PARTITION_OTA_TEMP); memset(&(context->flashContentInRam.bootTable), 0, sizeof(boot_table_t)); context->flashContentInRam.bootTable.length = u32TotalLen; context->flashContentInRam.bootTable.start_address = para_partition_info->partition_start_addr; context->flashContentInRam.bootTable.type = 'A'; context->flashContentInRam.bootTable.upgrade_type = 'U'; while(total_len > 0){ if( SizePerRW < total_len ){ len = SizePerRW; } else { len = total_len; } err = MicoFlashRead( MICO_PARTITION_OTA_TEMP, &update_data_offset, data , len); total_len -= len; CRC16_Update( &crc_context, data, len ); } CRC16_Final( &crc_context, &context->flashContentInRam.bootTable.crc ); MICOUpdateConfiguration(context); return ZC_RET_OK; #endif }
//----------------------------------------------------------- static uint16_t Cal_CRC16(const uint8_t* data, uint32_t size) { CRC16_Context contex; uint16_t ret; CRC16_Init( &contex ); CRC16_Update( &contex, data, size ); CRC16_Final( &contex, &ret ); return ret; }
static int stm32l475_ota_set_boot(hal_ota_module_t *m, void *something) { ota_finish_param_t *param = (ota_finish_param_t *)something; if (param==NULL){ return -1; } if (param->result_type==OTA_FINISH) { CRC16_Final( &contex, (uint16_t *)&ota_info.ota_crc ); LOG("set boot---------------\n"); hal_ota_switch_to_new_fw(); memset(&ota_info, 0 , sizeof ota_info); } else if (param->result_type==OTA_BREAKPOINT) { LOG("OTA package is incomplete!\n"); hal_ota_save_crc16(contex.crc); } return 0; }
/********************* Defination of crc16_mico_check_test() ****************/ int crc16_mico_check_test(void) { CRC16_Context crc; uint16_t Result; /* Defination of input string to be used crc16 check */ uint8_t msg[] = {0x01,0x02,0x03,0x04,0x05,0x06}; /*length is arbitrary */ CRC16_Init( &crc ); CRC16_Update( &crc, msg, sizeof(msg) ); CRC16_Final( &crc, &Result ); return Result; }
OSStatus fogCloudDevFirmwareUpdate(app_context_t* const inContext, MVDOTARequestData_t devOTARequestData) { cloud_if_log_trace(); OSStatus err = kUnknownErr; ecs_ota_flash_params_t ota_flash_params = { MICO_PARTITION_OTA_TEMP, 0x0, }; md5_context md5; unsigned char md5_16[16] = {0}; char *pmd5_32 = NULL; char rom_file_md5[32] = {0}; uint8_t data[SizePerRW] = {0}; uint32_t updateStartAddress = 0; uint32_t readLength = 0; uint32_t i = 0, size = 0; uint32_t romStringLen = 0; // crc16 CRC16_Context contex; cloud_if_log("fogCloudDevFirmwareUpdate: start ..."); //get latest rom version, file_path, md5 cloud_if_log("fogCloudDevFirmwareUpdate: get latest rom version from server ..."); err = FogCloudGetLatestRomVersion(&easyCloudContext); require_noerr_action( err, exit_with_error, cloud_if_log("ERROR: FogCloudGetLatestRomVersion failed! err=%d", err) ); //FW version compare cloud_if_log("currnt_version=%s", inContext->appConfig->fogcloudConfig.romVersion); cloud_if_log("latestRomVersion=%s", easyCloudContext.service_status.latestRomVersion); cloud_if_log("bin_file=%s", easyCloudContext.service_status.bin_file); cloud_if_log("bin_md5=%s", easyCloudContext.service_status.bin_md5); romStringLen = strlen(easyCloudContext.service_status.latestRomVersion) > strlen(inContext->appConfig->fogcloudConfig.romVersion) ? strlen(easyCloudContext.service_status.latestRomVersion):strlen(inContext->appConfig->fogcloudConfig.romVersion); if(0 == strncmp(inContext->appConfig->fogcloudConfig.romVersion, easyCloudContext.service_status.latestRomVersion, romStringLen)) { cloud_if_log("the current firmware version[%s] is up-to-date!", inContext->appConfig->fogcloudConfig.romVersion); inContext->appStatus.fogcloudStatus.RecvRomFileSize = 0; err = kNoErr; goto exit_with_no_error; } cloud_if_log("fogCloudDevFirmwareUpdate: new firmware[%s] found on server, downloading ...", easyCloudContext.service_status.latestRomVersion); inContext->appStatus.fogcloudStatus.isOTAInProgress = true; OTAWillStart(inContext); //get rom data err = FogCloudGetRomData(&easyCloudContext, ota_flash_params); require_noerr_action( err, exit_with_error, cloud_if_log("ERROR: FogCloudGetRomData failed! err=%d", err) ); //------------------------------ OTA DATA VERIFY ----------------------------- // md5 init InitMd5(&md5); CRC16_Init( &contex ); memset(rom_file_md5, 0, 32); memset(data, 0xFF, SizePerRW); updateStartAddress = ota_flash_params.update_offset; size = (easyCloudContext.service_status.bin_file_size)/SizePerRW; // read flash, md5 update for(i = 0; i <= size; i++) { if( i == size ) { if( (easyCloudContext.service_status.bin_file_size)%SizePerRW ) { readLength = (easyCloudContext.service_status.bin_file_size)%SizePerRW; } else { break; } } else { readLength = SizePerRW; } err = MicoFlashRead(ota_flash_params.update_partion, &updateStartAddress, data, readLength); require_noerr(err, exit_with_error); Md5Update(&md5, (uint8_t *)data, readLength); CRC16_Update( &contex, data, readLength ); } // read done, calc MD5 Md5Final(&md5, md5_16); CRC16_Final( &contex, &ota_crc ); pmd5_32 = ECS_DataToHexStringLowercase(md5_16, sizeof(md5_16)); //convert hex data to hex string cloud_if_log("ota_data_in_flash_md5[%d]=%s", strlen(pmd5_32), pmd5_32); if (NULL != pmd5_32) { strncpy(rom_file_md5, pmd5_32, strlen(pmd5_32)); free(pmd5_32); pmd5_32 = NULL; } else { err = kNoMemoryErr; goto exit_with_error; } // check md5 if(0 != strncmp( easyCloudContext.service_status.bin_md5, (char*)&(rom_file_md5[0]), strlen( easyCloudContext.service_status.bin_md5))) { cloud_if_log("ERROR: ota data wrote in flash md5 checksum err!!!"); err = kChecksumErr; goto exit_with_error; } else { cloud_if_log("OTA data in flash md5 check success, crc16=%d.", ota_crc); } //---------------------------------------------------------------------------- //update rom version in flash cloud_if_log("fogCloudDevFirmwareUpdate: return rom version && file size."); mico_rtos_lock_mutex(&inContext->mico_context->flashContentInRam_mutex); memset(inContext->appConfig->fogcloudConfig.romVersion, 0, MAX_SIZE_FW_VERSION); strncpy(inContext->appConfig->fogcloudConfig.romVersion, easyCloudContext.service_status.latestRomVersion, strlen(easyCloudContext.service_status.latestRomVersion)); inContext->appStatus.fogcloudStatus.RecvRomFileSize = easyCloudContext.service_status.bin_file_size; err = mico_system_context_update(inContext->mico_context); mico_rtos_unlock_mutex(&inContext->mico_context->flashContentInRam_mutex); OTASuccess(inContext); err = kNoErr; goto exit_with_no_error; exit_with_no_error: cloud_if_log("fogCloudDevFirmwareUpdate exit with no error."); inContext->appStatus.fogcloudStatus.isOTAInProgress = false; return err; exit_with_error: cloud_if_log("fogCloudDevFirmwareUpdate exit with err=%d.", err); OTAFailed(inContext); inContext->appStatus.fogcloudStatus.isOTAInProgress = false; return err; }
//----------------------------------------------------------------- int _get_paramstr(char **param_buf, char **par_end, char **var_ptr) { int psize, err = 0; psize = vm_fs_app_data_get_free_space(); if (psize > MAX_SYSTEM_PARAMS_SIZE) { // no parameters vm_log_debug("parameters not found"); return -1; } char *parambuf = vm_calloc(MAX_SYSTEM_PARAMS_SIZE+1); if (parambuf == NULL) { vm_log_error("error allocating buffer"); return -2; } VM_FS_HANDLE phandle = vm_fs_app_data_open(VM_FS_MODE_READ, VM_FALSE); if (phandle < VM_FS_SUCCESS) { vm_log_error("error opening params"); return -3; } if (vm_fs_app_data_read(phandle, parambuf, MAX_SYSTEM_PARAMS_SIZE, &psize) < 0) { vm_log_error("error reading params"); vm_fs_app_data_close(phandle); return -4; } vm_fs_app_data_close(phandle); parambuf[psize] = '\0'; // === Check crc === char *parend = strstr(parambuf, "\n__SYSVAR=\""); // end of __SYSPAR table if (parend == NULL) { err = -5; goto exit; } char *varptr = parend + 11; // start of __SSVAR char *varend = strstr(varptr, "\""); // end of __SYSVAR if (varend == NULL){ err = -6; goto exit; } if ((varend - varptr) != SYSVAR_SIZE) { err = -7; goto exit; } // calculate crc CRC16_Context contex; uint16_t parcrc; CRC16_Init( &contex ); CRC16_Update( &contex, parambuf, (varptr - parambuf) + SYSVAR_SIZE - 4); CRC16_Final( &contex, &parcrc ); // compare char crcstr[5] = {'\0'}; memcpy(crcstr, varptr + SYSVAR_SIZE - 4, 4); uint16_t crc = (uint16_t)strtol(crcstr, NULL, 16); if (crc != parcrc) { err = -8; } else { *param_buf = parambuf; *par_end = parend; *var_ptr = varptr; return 0; } exit: vm_log_error("SYSPAR wrong format"); return err; }