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; }
unsigned short filecheckcrc(FILE * f,unsigned long fileoffset,unsigned long size) { unsigned char crc16l,crc16h; unsigned long temp_fileptr; unsigned char buffer[512]; unsigned char crctable[32]; int i,s; CRC16_Init(&crc16h,&crc16l,(unsigned char*)crctable,0x1021,0xFFFF); temp_fileptr=ftell(f); fseek(f,fileoffset,SEEK_SET); s=size; while(s) { if(s>512) { fread(&buffer,512,1,f); for(i=0;i<512;i++) { CRC16_Update(&crc16h,&crc16l,buffer[i],(unsigned char*)crctable); } s=s-512; } else { fread(&buffer,s,1,f); for(i=0;i<s;i++) { CRC16_Update(&crc16h,&crc16l,buffer[i],(unsigned char*)crctable); } s=0; } } fseek(f,temp_fileptr,SEEK_SET); return (crc16l<<8) | crc16h; }
static int stm32l475_ota_write(hal_ota_module_t *m, volatile uint32_t* off_set, uint8_t* in_buf ,uint32_t in_buf_len) { hal_partition_t pno = HAL_PARTITION_OTA_TEMP; int ret; if (ota_info.ota_len == 0) { _off_set = 0; CRC16_Init( &contex ); memset(&ota_info, 0 , sizeof ota_info); } CRC16_Update( &contex, in_buf, in_buf_len); if (!FLASH_bank1_enabled()) { pno = HAL_PARTITION_APPLICATION; } ret = hal_flash_write(pno, &_off_set, in_buf, in_buf_len); ota_info.ota_len += in_buf_len; return ret; }
/********************* 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; }
int TeleDisk_libIsValidDiskFile(HXCFE_IMGLDR * imgldr_ctx,char * imgfile) { int pathlen,i; FILE * f; unsigned char crctable[32]; unsigned char CRC16_High,CRC16_Low; TELEDISK_HEADER td_header; unsigned char * ptr; imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"TeleDisk_libIsValidDiskFile"); if(imgfile) { pathlen=strlen(imgfile); if(pathlen!=0) { f=hxc_fopen(imgfile,"rb"); if(f==NULL) { imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"TeleDisk_libIsValidDiskFile : Cannot open %s !",imgfile); return HXCFE_ACCESSERROR; } fseek(f,0,SEEK_SET); memset(&td_header,0,sizeof(TELEDISK_HEADER)); fread( &td_header, sizeof(TELEDISK_HEADER), 1, f ); i=ftell(f); if(ftell(f)==sizeof(TELEDISK_HEADER)) { if ( ((td_header.TXT[0]!='t') || (td_header.TXT[1]!='d')) && ((td_header.TXT[0]!='T') || (td_header.TXT[1]!='D'))) { imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"TeleDisk_libIsValidDiskFile : bad header tag !"); hxc_fclose(f); return HXCFE_BADFILE; } CRC16_Init(&CRC16_High,&CRC16_Low,(unsigned char*)crctable,0xA097,0x0000); ptr=(unsigned char*)&td_header; for(i=0;i<0xA;i++) { CRC16_Update(&CRC16_High,&CRC16_Low, ptr[i],(unsigned char*)crctable ); } if(((td_header.CRC[1]<<8)|td_header.CRC[0]) != ((CRC16_High<<8)|CRC16_Low)) { imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"bad header crc !"); hxc_fclose(f); return HXCFE_BADFILE; } imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"TeleDisk_libIsValidDiskFile : it's a Tele disk file!"); hxc_fclose(f); return HXCFE_VALIDFILE; } imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"TeleDisk_libIsValidDiskFile : bad header tag !"); hxc_fclose(f); return HXCFE_BADFILE; } } return HXCFE_BADPARAMETER; }
int TeleDisk_libLoad_DiskFile(HXCFE_IMGLDR * imgldr_ctx,HXCFE_FLOPPY * floppydisk,char * imgfile,void * parameters) { FILE * f; unsigned int i; unsigned int file_offset; uint32_t tracklen; unsigned char interleave,skew,trackformat; unsigned short rpm,sectorsize; int Compress,numberoftrack,sidenumber; unsigned short * datalen; HXCFE_CYLINDER* currentcylinder; HXCFE_SIDE* currentside; TELEDISK_HEADER *td_header; TELEDISK_TRACK_HEADER *td_track_header; TELEDISK_SECTOR_HEADER *td_sector_header; TELEDISK_COMMENT * td_comment; unsigned char tempdata[8*1024]; unsigned char crctable[32]; unsigned char CRC16_High,CRC16_Low; unsigned char * ptr; uint32_t filesize; HXCFE_SECTCFG * sectorconfig; unsigned char * fileimage; uint32_t fileimage_buffer_offset; int rlen; imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"TeleDisk_libLoad_DiskFile %s",imgfile); hxcfe_imgCallProgressCallback(imgldr_ctx,0,100 ); f=hxc_fopen(imgfile,"rb"); if(f==NULL) { imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"TeleDisk_libLoad_DiskFile : Cannot open %s !",imgfile); return HXCFE_ACCESSERROR; } fseek(f,0,SEEK_END); filesize=ftell(f); if(!filesize) { imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"TeleDisk_libLoad_DiskFile : 0 byte file !"); hxc_fclose(f); return HXCFE_BADFILE; } fseek(f,0,SEEK_SET); fileimage_buffer_offset=0; fileimage=(unsigned char*)malloc(filesize+512); if(!fileimage) { imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"TeleDisk_libLoad_DiskFile : Malloc error !"); hxc_fclose(f); return HXCFE_INTERNALERROR; } memset(fileimage,0,filesize+512); fread(fileimage,filesize,1,f); hxc_fclose(f); td_header=(TELEDISK_HEADER*)&fileimage[fileimage_buffer_offset]; fileimage_buffer_offset=fileimage_buffer_offset+sizeof(TELEDISK_HEADER); if ( ((td_header->TXT[0]!='t') || (td_header->TXT[1]!='d')) && ((td_header->TXT[0]!='T') || (td_header->TXT[1]!='D'))) { imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"TeleDisk_libLoad_DiskFile : bad header tag !"); free(fileimage); return HXCFE_BADFILE; } CRC16_Init(&CRC16_High,&CRC16_Low,(unsigned char*)crctable,0xA097,0x0000); ptr=(unsigned char*)td_header; for(i=0;i<0xA;i++) { CRC16_Update(&CRC16_High,&CRC16_Low, ptr[i],(unsigned char*)crctable ); } if(((td_header->CRC[1]<<8)|td_header->CRC[0])!=((CRC16_High<<8)|CRC16_Low)) { imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"TeleDisk_libLoad_DiskFile : bad header crc !"); free(fileimage); return HXCFE_BADFILE; } imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"TeleDisk_libLoad_DiskFile : Teledisk version : %d",td_header->TDVer); if((td_header->TDVer>21) || (td_header->TDVer<10)) { imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"TeleDisk_libLoad_DiskFile : Unsupported version !"); free(fileimage); return HXCFE_BADFILE; } Compress=0; if(((td_header->TXT[0]=='T') && (td_header->TXT[1]=='D'))) { imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"TeleDisk_libLoad_DiskFile : Normal compression"); Compress=0; } if(((td_header->TXT[0]=='t') && (td_header->TXT[1]=='d'))) { imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"TeleDisk_libLoad_DiskFile : Advanced compression"); fileimage=unpack(fileimage,filesize); Compress=1; } td_header=(TELEDISK_HEADER*)&fileimage[0]; if(td_header->TrkDens&0x80) { CRC16_Init(&CRC16_High,&CRC16_Low,(unsigned char*)crctable,0xA097,0x0000); td_comment=(TELEDISK_COMMENT *)&fileimage[fileimage_buffer_offset]; fileimage_buffer_offset=fileimage_buffer_offset+sizeof(TELEDISK_COMMENT); //fread( &td_comment, sizeof(td_comment), 1, f ); ptr=(unsigned char*)td_comment; ptr=ptr+2; for(i=0;i<sizeof(TELEDISK_COMMENT)-2;i++) { CRC16_Update(&CRC16_High,&CRC16_Low, ptr[i],(unsigned char*)crctable ); } memcpy(&tempdata,&fileimage[fileimage_buffer_offset],td_comment->Len); fileimage_buffer_offset=fileimage_buffer_offset+td_comment->Len; ptr=(unsigned char*)&tempdata; for(i=0;i<td_comment->Len;i++) { CRC16_Update(&CRC16_High,&CRC16_Low, ptr[i],(unsigned char*)crctable ); } imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"TeleDisk_libLoad_DiskFile : Creation date: %.2d/%.2d/%.4d %.2d:%.2d:%.2d",td_comment->bDay,\ td_comment->bMon+1,\ td_comment->bYear+1900,\ td_comment->bHour,\ td_comment->bMin,\ td_comment->bSec); imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"TeleDisk_libLoad_DiskFile : Comment: %s",tempdata); } interleave=1; numberoftrack=0; sectorsize=512; file_offset=fileimage_buffer_offset; floppydisk->floppyNumberOfSide=td_header->Surface; td_track_header=(TELEDISK_TRACK_HEADER *)&fileimage[fileimage_buffer_offset]; fileimage_buffer_offset=fileimage_buffer_offset+sizeof(TELEDISK_TRACK_HEADER); while(td_track_header->SecPerTrk!=0xFF) { if(td_track_header->PhysCyl>numberoftrack) { numberoftrack=td_track_header->PhysCyl; } CRC16_Init(&CRC16_High,&CRC16_Low,(unsigned char*)crctable,0xA097,0x0000); ptr=(unsigned char*)td_track_header; for(i=0;i<0xA;i++) { CRC16_Update(&CRC16_High,&CRC16_Low, ptr[i],(unsigned char*)crctable ); } for ( i=0;i < td_track_header->SecPerTrk;i++ ) { td_sector_header=(TELEDISK_SECTOR_HEADER *)&fileimage[fileimage_buffer_offset]; fileimage_buffer_offset=fileimage_buffer_offset+sizeof(TELEDISK_SECTOR_HEADER); if ( (td_sector_header->Syndrome & 0x30) == 0 && (td_sector_header->SLen & 0xf8) == 0 ) { //fileimage_buffer_offset=fileimage_buffer_offset+sizeof(unsigned short); datalen=(unsigned short*)&fileimage[fileimage_buffer_offset]; fileimage_buffer_offset=fileimage_buffer_offset+(*datalen)+2; } } td_track_header=(TELEDISK_TRACK_HEADER *)&fileimage[fileimage_buffer_offset]; fileimage_buffer_offset=fileimage_buffer_offset+sizeof(TELEDISK_TRACK_HEADER); } floppydisk->floppyNumberOfTrack=numberoftrack+1; floppydisk->floppySectorPerTrack=-1; floppydisk->tracks=(HXCFE_CYLINDER**)malloc(sizeof(HXCFE_CYLINDER*)*floppydisk->floppyNumberOfTrack); memset(floppydisk->tracks,0,sizeof(HXCFE_CYLINDER*)*floppydisk->floppyNumberOfTrack); //Source disk density (0 = 250K bps, 1 = 300K bps, 2 = 500K bps ; +128 = single-density FM) switch(td_header->Dens) { case 0: floppydisk->floppyBitRate=250000; break; case 1: floppydisk->floppyBitRate=300000; break; case 2: floppydisk->floppyBitRate=500000; break; default: floppydisk->floppyBitRate=250000; break; } floppydisk->floppyiftype=GENERIC_SHUGART_DD_FLOPPYMODE; skew=1; rpm=300; // normal rpm imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"%d tracks, %d side(s), gap3:%d,rpm:%d bitrate:%d",floppydisk->floppyNumberOfTrack,floppydisk->floppyNumberOfSide,floppydisk->floppySectorPerTrack,rpm,floppydisk->floppyBitRate); tracklen=(floppydisk->floppyBitRate/(rpm/60))/4; ////////////////////////////////// fileimage_buffer_offset=file_offset; td_track_header=(TELEDISK_TRACK_HEADER *)&fileimage[fileimage_buffer_offset]; fileimage_buffer_offset=fileimage_buffer_offset+sizeof(TELEDISK_TRACK_HEADER); while(td_track_header->SecPerTrk!=0xFF) { if(td_track_header->PhysSide&0x7F) { sidenumber=1; } else { sidenumber=0; } if(td_track_header->PhysSide&0x80) { trackformat=IBMFORMAT_SD; } else { trackformat=IBMFORMAT_DD; } imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"------------- Track:%d, Side:%d, Number of Sector:%d -------------",td_track_header->PhysCyl,sidenumber,td_track_header->SecPerTrk); if(!floppydisk->tracks[td_track_header->PhysCyl]) { floppydisk->tracks[td_track_header->PhysCyl]=(HXCFE_CYLINDER*)malloc(sizeof(HXCFE_CYLINDER)); memset(floppydisk->tracks[td_track_header->PhysCyl],0,sizeof(HXCFE_CYLINDER)); } currentcylinder=floppydisk->tracks[td_track_header->PhysCyl]; currentcylinder->number_of_side=floppydisk->floppyNumberOfSide; if(!currentcylinder->sides) { currentcylinder->sides=(HXCFE_SIDE**)malloc(sizeof(HXCFE_SIDE*)*currentcylinder->number_of_side); memset(currentcylinder->sides,0,sizeof(HXCFE_SIDE*)*currentcylinder->number_of_side); } currentcylinder->floppyRPM=rpm; ////////////////////crc track header/////////////////// CRC16_Init(&CRC16_High,&CRC16_Low,(unsigned char*)crctable,0xA097,0x0000); ptr=(unsigned char*)td_track_header; for(i=0;i<0x3;i++) { CRC16_Update(&CRC16_High,&CRC16_Low, ptr[i],(unsigned char*)crctable ); } if(CRC16_Low!=td_track_header->CRC) { imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"!!!! Track header CRC Error !!!!"); } //////////////////////////////////////////////////////// sectorconfig=(HXCFE_SECTCFG *)malloc(sizeof(HXCFE_SECTCFG)*td_track_header->SecPerTrk); memset(sectorconfig,0,sizeof(HXCFE_SECTCFG)*td_track_header->SecPerTrk); for ( i=0;i < td_track_header->SecPerTrk;i++ ) { td_sector_header=(TELEDISK_SECTOR_HEADER *)&fileimage[fileimage_buffer_offset]; fileimage_buffer_offset=fileimage_buffer_offset+sizeof(TELEDISK_SECTOR_HEADER); sectorconfig[i].cylinder=td_sector_header->Cyl; sectorconfig[i].head=td_sector_header->Side; sectorconfig[i].sector=td_sector_header->SNum; sectorconfig[i].sectorsize=128<<td_sector_header->SLen; sectorconfig[i].bitrate=floppydisk->floppyBitRate; sectorconfig[i].gap3=255; sectorconfig[i].trackencoding=trackformat; if(td_sector_header->Syndrome & 0x04) { sectorconfig[i].use_alternate_datamark=1; sectorconfig[i].alternate_datamark=0xF8; } if(td_sector_header->Syndrome & 0x02) { sectorconfig[i].use_alternate_data_crc=2; } if(td_sector_header->Syndrome & 0x20) { sectorconfig[i].missingdataaddressmark=1; } sectorconfig[i].input_data=malloc(sectorconfig[i].sectorsize); if ( (td_sector_header->Syndrome & 0x30) == 0 && (td_sector_header->SLen & 0xf8) == 0 ) { //fileimage_buffer_offset=fileimage_buffer_offset+sizeof(unsigned short); datalen=(unsigned short*)&fileimage[fileimage_buffer_offset]; memcpy(&tempdata,&fileimage[fileimage_buffer_offset],(*datalen)+2); fileimage_buffer_offset=fileimage_buffer_offset+(*datalen)+2; rlen=RLEExpander(tempdata,sectorconfig[i].input_data,(int)*datalen); } else { memset(sectorconfig[i].input_data,0,sectorconfig[i].sectorsize); } imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"track:%d, side:%d, sector:%d, sectorsize:%d, flag:%.2x",sectorconfig[i].cylinder,sectorconfig[i].head,sectorconfig[i].sector,sectorconfig[i].sectorsize,td_sector_header->Syndrome); } currentside=tg_generateTrackEx((unsigned short)td_track_header->SecPerTrk,sectorconfig,interleave,0,floppydisk->floppyBitRate,rpm,trackformat,0,2500 | NO_SECTOR_UNDER_INDEX,-2500); currentcylinder->sides[sidenumber]=currentside; for ( i=0;i < td_track_header->SecPerTrk;i++ ) { if(sectorconfig[i].input_data) free(sectorconfig[i].input_data); } free(sectorconfig); td_track_header=(TELEDISK_TRACK_HEADER *)&fileimage[fileimage_buffer_offset]; fileimage_buffer_offset=fileimage_buffer_offset+sizeof(TELEDISK_TRACK_HEADER); } imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"track file successfully loaded and encoded!"); free(fileimage); hxcfe_imgCallProgressCallback(imgldr_ctx,100,100 ); hxcfe_sanityCheck(imgldr_ctx->hxcfe,floppydisk); return HXCFE_NOERROR; }
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; }
int patchtrackFM(unsigned char * trackdata, unsigned char * trackclk,int tracklen) { int i,j,k,l; int sectorsize; int nbofsector; unsigned char crctable[32]; unsigned char CRC16_High,CRC16_Low; nbofsector=0; i=0; do { if(trackdata[i]==0xFE) { trackclk[i]=0xC7; if( (trackdata[i+5]==0xF7) && (trackdata[i+6]==0xF7) ) { // calculate header crc sectorsize=128<<trackdata[i+4]; l=i; CRC16_Init(&CRC16_High,&CRC16_Low,(unsigned char*)crctable,0x1021,0xFFFF); for(k=0;k<5;k++) { CRC16_Update(&CRC16_High,&CRC16_Low, trackdata[l],(unsigned char*)crctable ); l++; } trackdata[i+5]=CRC16_High; trackdata[i+6]=CRC16_Low; j=0; do { if((trackdata[i+j]==0xFB) || (trackdata[i+j]==0xF8)) { trackclk[i+j]=0xC7; if((trackdata[i+j+sectorsize+1]==0xF7) && (trackdata[i+j+sectorsize+2]==0xF7)) { // calculate data crc //02 CRC The sector Header CRC CRC16_Init(&CRC16_High,&CRC16_Low,(unsigned char*)crctable,0x1021,0xFFFF); for(k=0;k<sectorsize+1;k++) { CRC16_Update(&CRC16_High,&CRC16_Low, trackdata[i+j+k],(unsigned char*)crctable ); } trackdata[i+j+sectorsize+1]=CRC16_High; trackdata[i+j+sectorsize+2]=CRC16_Low; i=i+j+sectorsize+3; j=32; nbofsector++; } } j++; }while(j<32); } } i++; }while(i<tracklen); return nbofsector; }
int patchtrackMFM(unsigned char * trackdata, unsigned char * trackclk,int tracklen) { int i,j,k,l; int sectorsize; int nbofsector; unsigned char crctable[32]; unsigned char CRC16_High,CRC16_Low; nbofsector=0; i=0; do { if(trackdata[i]==0xA1 && trackdata[i+1]==0xA1 && trackdata[i+2]==0xA1 && trackdata[i+3]==0xFE) { l=i; trackclk[i]=0x0A; trackclk[i+1]=0x0A; trackclk[i+2]=0x0A; trackclk[i+3]=0xFF; i=i+3; if( (trackdata[i+5]==0xF7) && (trackdata[i+6]==0xF7) ) { // calculate header crc sectorsize=128<<trackdata[i+4]; CRC16_Init(&CRC16_High,&CRC16_Low,(unsigned char*)crctable,0x1021,0xFFFF); for(k=0;k<8;k++) { CRC16_Update(&CRC16_High,&CRC16_Low, trackdata[l],(unsigned char*)crctable ); l++; } trackdata[i+5]=CRC16_High; trackdata[i+6]=CRC16_Low; j=0; do { if(trackdata[i+j]==0xA1 && trackdata[i+j+1]==0xA1 && trackdata[i+j+2]==0xA1 && trackdata[i+j+3]==0xFB) { trackclk[i+j]=0x0A; trackclk[i+j+1]=0x0A; trackclk[i+j+2]=0x0A; trackclk[i+j+3]=0xFF; i=i+j+3+1; if((trackdata[i+sectorsize]==0xF7) && (trackdata[i+sectorsize+1]==0xF7)) { // calculate data crc //02 CRC The sector Header CRC CRC16_Init(&CRC16_High,&CRC16_Low,(unsigned char*)crctable,0x1021,0xFFFF); for(k=0;k<sectorsize+4;k++) { CRC16_Update(&CRC16_High,&CRC16_Low, trackdata[i+k-4],(unsigned char*)crctable ); } trackdata[i+sectorsize]=CRC16_High; trackclk[i+sectorsize]=0xFF; trackdata[i+sectorsize+1]=CRC16_Low; trackclk[i+sectorsize+1]=0xFF; i=i+sectorsize+2; j=64; nbofsector++; } } j++; }while(j<64); } } i++; }while(i<tracklen); return nbofsector; }
int BuildEmuIITrack(HXCFLOPPYEMULATOR* floppycontext,unsigned int tracknumber,unsigned int sidenumber,unsigned char* datain,unsigned char * fmdata,unsigned long * fmsizebuffer,int trackformat) { unsigned int i,j,k; unsigned char CRC16_High; unsigned char CRC16_Low; unsigned char *tempdata; unsigned char *tempclock; unsigned long finalsize; unsigned long current_buffer_size; unsigned char crctable[32]; unsigned long sectorsize; unsigned char track_num; unsigned long buffersize; buffersize=*fmsizebuffer/8; sectorsize=3584; switch(trackformat) { case 1: track_num=tracknumber; break; case 2: track_num=tracknumber*2+sidenumber; break; } current_buffer_size=buffersize/4; finalsize=20 + 10 +8 + 6 + sectorsize + 2 + 2 + 20; if(finalsize<=current_buffer_size) { j=0; tempdata=(char *)malloc((buffersize/4)+1); tempclock=(char *)malloc((buffersize/4)+1); memset(tempclock,0xFF,(buffersize/4)+1); memset(tempdata, 0xFF,(buffersize/4)+1); ///////////////////////////////////////////////////////////////////////////////////////////// //Track GAP for(k=0;k<20;k++) { tempdata[j]=bit_inverter_emuii[0xFF]; j++; } ///////////////////////////////////////////////////////////////////////////////////////////// //Sector Header for(k=0;k<4;k++) tempdata[j++]=bit_inverter_emuii[0x00]; tempdata[j++]=bit_inverter_emuii[0xFA]; // sync tempdata[j++]=bit_inverter_emuii[0x96]; tempdata[j++]=bit_inverter_emuii[track_num]; //CRC The sector Header CRC CRC16_Init(&CRC16_High,&CRC16_Low,(unsigned char*)&crctable,0x8005,0x0000); CRC16_Update(&CRC16_High,&CRC16_Low,bit_inverter_emuii[track_num],(unsigned char*)&crctable ); tempdata[j++]=CRC16_High; tempdata[j++]=CRC16_Low; switch(trackformat) { // Emu 1 case 1: tempdata[j++]=bit_inverter_emuii[0x00]; tempdata[j++]=bit_inverter_emuii[0x00]; for(k=0;k<7;k++) tempdata[j++]=bit_inverter_emuii[0xFF]; break; // Emu 2 case 2: tempdata[j++]=bit_inverter_emuii[0x00]; for(k=0;k<8;k++) tempdata[j++]=bit_inverter_emuii[0xFF]; break; } ///////////////////////////////////////////////////////////////////////////////////////////// //Sector Data for(k=0;k<4;k++) tempdata[j++]=bit_inverter_emuii[0x00]; tempdata[j++]=bit_inverter_emuii[0xFA]; // sync tempdata[j++]=bit_inverter_emuii[0x96]; //CRC The sector data CRC CRC16_Init(&CRC16_High,&CRC16_Low,(unsigned char*)&crctable,0x8005,0x0000); for(k=0;k<sectorsize;k++) { tempdata[j++]=bit_inverter_emuii[datain[k]]; CRC16_Update(&CRC16_High,&CRC16_Low,bit_inverter_emuii[datain[k]],(unsigned char*)&crctable ); } tempdata[j++]=CRC16_High; tempdata[j++]=CRC16_Low; tempdata[j++]=bit_inverter_emuii[0x00]; tempdata[j++]=bit_inverter_emuii[0x00]; for(k=0;k<20;k++) tempdata[j++]=bit_inverter_emuii[0xFF]; ///////////////////////////////////////////////////////////////////////////////////////////// if(j<=current_buffer_size) { for(i=j;i<(current_buffer_size+1);i++) { tempdata[i]=0xFF; } } BuildFMCylinder(fmdata,buffersize,tempclock,tempdata,(buffersize)/4); free(tempdata); free(tempclock); return 0; } else { floppycontext->hxc_printf(MSG_ERROR,"BuildEmuIITrack : No enough space on this track !"); return finalsize; } }