|
if (argc<=2) { printf("\tformat error! usage: CCC input_file_name version\n\texample: CCC xxx.d43 101\n"; return -1; }
if( (fin= fopen( argv[1], "rt" )) == NULL ) { printf( "The file %s was not opened\n",argv[1]); return -1; } if ((version=atoi(argv[2]))==0) { printf( "\tversion should not be ZERO!\n"; return -1; }
sprintf(fout_name,"XXX%04d.sys",version); printf("\tinput: \t%s\n\toutput: \t%s\n",argv[1],fout_name);
int linecnt=0; block.len=0; block.addr=0; while ( fgets( line, 100, fin) != NULL) { if (line[0]=='@') { if (block.len>0) write_block(&block); memcpy(addr_str,&line[1],4);//get addr block.addr=hex2uint(addr_str); block.len=0; } else if (line[0]=='q') { if (block.len>0) write_block(&block); break; //end of code } else { int len=asc2char(&line[0],&line_bin[0]); if ((block.len+len)>=MAX_BLOCK_LEN) { int num=MAX_BLOCK_LEN-block.len; memcpy(&block.buff[block.len],&line_bin[0],num); block.len=MAX_BLOCK_LEN; write_block(&block);
block.len=len-num; memcpy(&block.buff[0],&line_bin[num-1],block.len); block.addr+=MAX_BLOCK_LEN; } else { memcpy(&block.buff[block.len],&line_bin[0],len); block.len+=len; } } } //write end block (addr=len=0) block.len=0; block.addr=0; write_block(&block);
u16_t checksum=CalcChecksum(&TotalBlock[0],TotalBlock_Len); printf("\tlenght: \t%d bytes\n\tchecksum: \t%04x\n",TotalBlock_Len+2,checksum); memcpy(&TotalBlock[TotalBlock_Len],&checksum,2); TotalBlock_Len+=2;
fclose(fin); //save to file if ((fout=fopen(fout_name,"wb") ==NULL) { printf( "The file %s can not be created\n",fout_name); return -1; } fwrite(&TotalBlock[0],TotalBlock_Len,1,fout); fflush(fout); fclose(fout); printf("\tdone.(OK)\n",argv[1],fout_name);
return 0; }
b. 升級程序:將新版文件通過socket通訊來在線升級(編碼前---保護(hù)版權(quán)哦) BOOL CSystemBurner::OnSocketReceive(int iErrorCode) { try { if (iErrorCode) { printf("SocketReceive error\n";Close();return FALSE; }
char cBuffer[1024]; int iRecv=Receive(cBuffer,sizeof(cBuffer));
struAnswerHead head; memcpy((unsigned char *)&head,cBuffer,sizeof(struAnswerHead));
if (iRecv!=(head.len+sizeof(struAnswerHead))) { printf("len is not matched.\n"; return FALSE; }
char TxBuffer[128]; int len;
struct struDumpBlk_XXX{ union { char full[SMALL_BLK_SIZE]; struct { u16_t offset; char code[SMALL_BLK_SIZE-2]; }S; }U; } dumpBlk;
u16_t RxData;
switch (head.id) { case PKTID_CHANGE_MODE: m_status=CSystemBurner:UMP; len=CreatePacketOrder(TxBuffer, PKTID_SYS_DUMP, NULL,0); Send(TxBuffer,len); printf("change mode OK.\nstart dumping....% 3d/% 3d",m_small_blk_cnt+1,m_small_blk_num);
|