📄 uploaddlg.cpp
字号:
// UploadDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AliEditor.h"
#include "UploadDlg.h"
//#include "DownLoad.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "fastcrc.h"
#define PARSE_DB_MSG (WM_USER + 1)
#define FLASH_SIZE 0x100000
#define BLOCK_HEADER_SIZE 128
#define BLOCK_BASIC_HEADER_SIZE 16
#define BLOCK_ID_OFFSET 0
#define BLOCK_LEN_OFFSET 4
#define BLOCK_OFFSET_OFFSET 8
#define BLOCK_CRC_OFFSET 12
#define BLOCK_NAME_OFFSET 16
#define BLOCK_VER_OFFSET 32
#define BLOCK_TIME_OFFSET 48
#define BLOCK_RES_OFFSET 64
#define ID_TYPE(x) (x>>16)
#define BOOTLOADER_ID 0
#define USERDB_ID 0x04FB
#define TEMPDB_ID 0x05FA
#define DEFAULT_DATABASE_ID 0x03FC0100
#define USER_DATABASE_ID 0x04FB0100
/////////////////////////////////////////////////////////////////////////////
// CUploadDlg dialog
CUploadDlg::CUploadDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUploadDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUploadDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
pThread = NULL;
m_block_numbers = 0;
m_user_data_addr = 0;
m_default_addr = 0;
m_Flash = 0x200000; //2M
}
void CUploadDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUploadDlg)
DDX_Control(pDX, IDC_STATIC_OK5, m_parse_finish);
DDX_Control(pDX, IDC_STATIC_PARSE_DB, m_parse_db);
DDX_Control(pDX, IDC_STATIC_UPLOAD, m_start_download);
DDX_Control(pDX, IDC_STATIC_OK3, m_download_ok);
DDX_Control(pDX, IDC_STATIC_OK1, m_port_ok);
DDX_Control(pDX, IDC_STATIC_PORT_STATUS, m_port_status);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUploadDlg, CDialog)
//{{AFX_MSG_MAP(CUploadDlg)
ON_MESSAGE(PARSE_DB_MSG, ParseUserDb)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUploadDlg message handlers
BOOL CUploadDlg::Command_comtest()
{
char *TestCommd = "comtest 10\r";
int TestLen = 10;
char *TestStr = "ali DVB-S";
int len;
if(m_SerPort.IsOpened == FALSE)
return FALSE;
char TmpBuff[50];
/* make read buffer empty */
while(m_SerPort.ReadData(TmpBuff,50) != 0);
/* send test info*/
EnterCriticalSection(&m_cs);
len = strlen(TestCommd);
if(!m_SerPort.Send2Back_tm(TestCommd,len,1000*100))
{
LeaveCriticalSection(&m_cs);
return FALSE;
}
m_bNodifyReset = true;
len = strlen(TestStr);
if(!m_SerPort.Send2Back_tm(TestStr,len,1000*50))
{
LeaveCriticalSection(&m_cs);
return FALSE;
}
LeaveCriticalSection(&m_cs);
return TRUE;
}
BOOL CUploadDlg::Command_version()
{
unsigned char VerBuff[BLOCK_HEADER_SIZE*50],crc_txt[4];
char * VerCmmd = "version\r";
int len = strlen(VerCmmd);
int i;
unsigned int crc,verify_crc;
unsigned int buffer_len;
unsigned char *p, *pblock;
char sVer[16], hVer[16];
/* make read buffer empty */
while(m_SerPort.ReadData(VerBuff,BLOCK_HEADER_SIZE) != 0);
Sleep(100);
/* send version command */
if(!m_SerPort.Send2Back_tm(VerCmmd,len,1000*50))
{
AfxMessageBox("failed 1");
return FALSE;
}
Sleep(1500);
memset((void *)VerBuff,0,BLOCK_HEADER_SIZE*50);
if(!m_SerPort.ReadData_tm(VerBuff,2,1000*500))
{
AfxMessageBox("failed 2");
return FALSE;
}
buffer_len = 0;
buffer_len = (VerBuff[0]<<8)+VerBuff[1];
Sleep(1500);
if(!m_SerPort.ReadData_tm(VerBuff,buffer_len,1000*500))
{
AfxMessageBox("failed 3");
return FALSE;
}
Sleep(300);
if(!m_SerPort.ReadData_tm(crc_txt,4,1000*500))
{
AfxMessageBox("failed 4");
return FALSE;
}
crc = 0;
crc = (crc_txt[0]<<24)+(crc_txt[1]<<16)+(crc_txt[2]<<8)+(crc_txt[3]<<0);
verify_crc = MG_Table_Driven_CRC(0xFFFFFFFF,VerBuff,buffer_len);
//verify_crc = gen_CRC32(0xFFFFFFFF,VerBuff,buffer_len);
if(verify_crc != crc)
{
MessageBox("STB information transfer error","Error",MB_OK);
return FALSE;
}
switch((unsigned int)VerBuff[0])
{
case 1:
case 2:
case 7:
slave_Flash_type = 0x80000;
break;
case 3:
case 4:
slave_Flash_type = 0x100000;
break;
case 5:
case 6:
slave_Flash_type = 0x200000;
break;
default:
{
MessageBox("STB flash type unknown","Error",MB_OK);
return FALSE;
}
}
slave_status = (unsigned int)VerBuff[1];
slave_blocks_number = (buffer_len-2)/BLOCK_HEADER_SIZE;
slave_blocks = (BLOCK_HEADER *)malloc(sizeof(BLOCK_HEADER)*slave_blocks_number);
memset((void *)slave_blocks,0,sizeof(BLOCK_HEADER)*slave_blocks_number);
pblock = &VerBuff[2];
for(i=0; i<slave_blocks_number; i++)
{
p = pblock + BLOCK_ID_OFFSET;
slave_blocks[i].id = (*p<<24)+(*(p+1)<<16)+(*(p+2)<<8)+(*(p+3)<<0);
p = pblock + BLOCK_LEN_OFFSET;
slave_blocks[i].len = (*p<<24)+(*(p+1)<<16)+(*(p+2)<<8)+(*(p+3)<<0);
p = pblock + BLOCK_OFFSET_OFFSET;
slave_blocks[i].offset = (*p<<24)+(*(p+1)<<16)+(*(p+2)<<8)+(*(p+3)<<0);
p = pblock + BLOCK_CRC_OFFSET;
slave_blocks[i].crc = (*p<<24)+(*(p+1)<<16)+(*(p+2)<<8)+(*(p+3)<<0);
p = pblock + BLOCK_NAME_OFFSET;
strcpy((char *)slave_blocks[i].name, (char *)p);
p = pblock + BLOCK_VER_OFFSET;
strcpy((char *)slave_blocks[i].version, (char *)p);
p = pblock + BLOCK_TIME_OFFSET;
strcpy((char *)slave_blocks[i].time, (char *)p);
pblock += BLOCK_HEADER_SIZE;
}
#ifndef BURN_BOOTLOADER
/* open file */
if(!m_File.Open(m_Path,CFile::shareDenyRead,NULL))
{
return FALSE;
}
m_File.Seek(BLOCK_VER_OFFSET, CFile::begin);
if(16 != m_File.Read(hVer,16))
{
m_File.Close();
return FALSE;
}
m_File.Close();
strcpy(sVer, (char*)slave_blocks[0].version);
// if(CheckVersion(sVer, hVer) != TRUE)
{
MessageBox("STB version is not compatible","Error",MB_OK);
return FALSE;
}
#endif
return TRUE;
}
BOOL CUploadDlg::Command_dump()
{
char * DumpCmmd = "dump\r";
int len = strlen(DumpCmmd);
unsigned char len_buff[4];
char answer;
unsigned int i,step;
CString per;
CProgressCtrl *pPrgrss = (CProgressCtrl *)GetDlgItem(IDC_DOWNLOAD_FLASH);
CWnd *pWnd = GetDlgItem(IDC_STATIC_DOWNLOAD_PERCENT);
int prog;
unsigned int crc;
/* make read buffer empty */
while(m_SerPort.ReadData(len_buff,4) != 0);
/* send dump command */
if(!m_SerPort.Send2Back_tm(DumpCmmd,len,1000*50))
return FALSE;
/* get dump len */
if(!m_SerPort.ReadData_tm(len_buff,4,1000*500))
return FALSE;
dump_len = (len_buff[0]<<24)|(len_buff[1]<<16)|(len_buff[2]<<8)|len_buff[3];
dump_buff = (unsigned char*)malloc((dump_len+4)*sizeof(unsigned char));
if(dump_buff == NULL)
answer = 'E';
else
answer = 'O';
if(1 != m_SerPort.SendData(&answer, 1))
{
return FALSE;
}
step = (dump_len+4+99)/100;
prog = 0;
for(i=0; i<dump_len+4; i++)
{
// Sleep(1);
if(!m_SerPort.ReadData_tm(dump_buff+i,1,1000*100))
{
return FALSE;
}
if(i%step == 0)
{
pPrgrss->SetPos(prog);
per.Format("%d",prog);
per += _T("%");
pWnd->SetWindowText(per);
prog++;
}
}
MG_Setup_CRC_Table();
crc = (dump_buff[dump_len]<<24)|(dump_buff[dump_len+1]<<16)|(dump_buff[dump_len+2]<<8)|dump_buff[dump_len+3];
if(crc != MG_Table_Driven_CRC(0xFFFFFFFF,dump_buff,dump_len))
{
MessageBox("Crc error!","Error",MB_ICONEXCLAMATION|MB_OK);
return FALSE;
}
pPrgrss->SetPos(100);
per.Format("%d",100);
per += _T("%");
pWnd->SetWindowText(per);
return TRUE;
}
BOOL CUploadDlg::StartDump()
{
m_bNodifyReset = false;
CWnd *pWnd;
CString per;
m_port_status.ShowWindow(SW_SHOW);
/*check file path*/
if(m_Path == "")
{
MessageBox("Please enter file!","Warning",MB_ICONEXCLAMATION|MB_OK);
return FALSE;
}
if(!m_File.Open(m_Path,CFile::modeCreate|CFile::modeWrite,NULL))
{
MessageBox("Can not open file!","Warning",MB_ICONEXCLAMATION|MB_OK);
m_Path = "";
return FALSE;
}
m_File.Close();
/*check serial port*/
if(!m_SerPort.Open(m_Port+1, m_Baud))
{
MessageBox("Can not open serial port!","Error",MB_OK);
return FALSE;
}
Sleep(50);
/*comtest*/
int count = 0;
while(!Command_comtest())
{
m_SerPort.SendData("\r",1);
Sleep(20);
count++;
if(count > 20)
{
AfxMessageBox("serial port probe error!");
m_SerPort.Close();
return FALSE;
}
}
m_port_ok.ShowWindow(SW_SHOW);
Sleep(50);
m_start_download.ShowWindow(SW_SHOW);
/*show percent bar*/
CProgressCtrl *pPrgrss = (CProgressCtrl *)GetDlgItem(IDC_DOWNLOAD_FLASH);
if(pPrgrss != NULL)
pPrgrss->ShowWindow(SW_SHOW);
/*show percent str*/
pWnd = GetDlgItem(IDC_STATIC_DOWNLOAD_PERCENT);
if(pWnd != NULL)
pWnd->ShowWindow(SW_SHOW);
/*init percent*/
pPrgrss->SetPos(0);
per.Format("%d",0);
per += _T("%");
pWnd->SetWindowText(per);
pPrgrss->SetRange(0,100);
if(!Command_dump())
{
if(dump_buff != NULL)
free(dump_buff);
m_SerPort.Close();
AfxMessageBox("Upload fail");
return FALSE;
}
// m_SerPort.Close();
Command_reboot();
if(!m_File.Open(m_Path,CFile::modeWrite|CFile::typeBinary,NULL))
{
MessageBox("Can not open file!","Warning",MB_ICONEXCLAMATION|MB_OK);
m_Path = "";
return FALSE;
}
m_File.Write(dump_buff,dump_len);
m_File.Close();
m_download_ok.ShowWindow(SW_SHOW);
return TRUE;
}
void CUploadDlg::ParseUserDb()
{
m_parse_db.ShowWindow(SW_SHOW);
m_block_numbers = getBlockNumber();
Sleep(1200);
// CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd();
// if(pFrame) pFrame->UpdateDataBase(m_Path);
m_parse_finish.ShowWindow(SW_SHOW);
// Sleep(500);
// pFrame->SaveDataBase();//
// CDialog::OnCancel();
}
UINT CUploadDlg::UploadThread(LPVOID lParam)
{
CUploadDlg *pThis = (CUploadDlg *)lParam;
Sleep(300);
if(pThis->StartDump())
{
// ::SendMessage(pThis->m_hWnd, PARSE_DB_MSG, 0, 0);
pThis->ParseUserDb();
}
else
{
Sleep(500);
//pThis->EndDialog(IDCANCEL);
//pThis->GetDlgItem(IDOK)->EnableWindow(TRUE);
return 0;
}
Sleep(500);
//pThis->EndDialog(1);
pThis->GetDlgItem(IDCANCEL)->EnableWindow(FALSE);
pThis->GetDlgItem(IDCANCEL)->ShowWindow(SW_HIDE);
pThis->GetDlgItem(IDOK)->EnableWindow(TRUE);
pThis->GetDlgItem(IDOK)->ShowWindow(SW_SHOW);
return 0;
}
BOOL CUploadDlg::Command_reboot()
{
char *RebootCommd = "reboot\r";
int TestLen = strlen(RebootCommd);
if(m_SerPort.IsOpened == FALSE)
return FALSE;
char TmpBuff[50];
/* make read buffer empty */
while(m_SerPort.ReadData(TmpBuff,50) != 0);
/* send test info*/
if(!m_SerPort.Send2Back_tm(RebootCommd,TestLen,1000*500))
return FALSE;
m_SerPort.Close();
return TRUE;
}
BOOL CUploadDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
GetDlgItem(IDOK)->EnableWindow(FALSE);
GetDlgItem(IDOK)->ShowWindow(SW_HIDE);
InitializeCriticalSection(&m_cs);
pThread = AfxBeginThread(UploadThread, this);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CUploadDlg::OnCancel()
{
// TODO: Add extra cleanup here
if(pThread)
pThread->SuspendThread();
// Command_reboot();
// char *RebootCommd = "reboot\r";
// int TestLen = strlen(RebootCommd);
// m_SerPort.SendData(RebootCommd,TestLen);
m_SerPort.Close();
if( m_bNodifyReset )
AfxMessageBox("Please reset STB.");
CDialog::OnCancel();
}
#define NCRC_FLAG(x) (x == 0x4E435243) //"NCRC"
int CUploadDlg::getBlockNumber()
{
unsigned int id,len,offset,crc,verify_crc,id1,id2;
unsigned int file_offset, file_len, read_size;
unsigned char headbuff[BLOCK_HEADER_SIZE], block_buffer[0x400];
int i,j,count=0;
// CString per;
m_Flash = 0x200000; //2M
/* open file */
if(!m_File.Open(m_Path,CFile::shareDenyRead,NULL))
{
// MessageBox("can not open file!","Error",MB_OK);
return 0;
}
MG_Setup_CRC_Table();
//seek the current file pointer by m_type
file_offset = 0;
file_len = m_File.GetLength();
while(1)
{
if(file_offset+BLOCK_HEADER_SIZE > file_len)
{
m_File.Close();
// per.Format("can not read file:0x%x",file_offset+BLOCK_HEADER_SIZE);
// MessageBox(per,"Error",MB_OK);
return 0;
}
// read head
m_File.Seek(file_offset, CFile::begin);
if(BLOCK_BASIC_HEADER_SIZE != m_File.Read(headbuff,BLOCK_BASIC_HEADER_SIZE))
{
m_File.Close();
// AfxMessageBox("Read error!");
return 0;
}
id = 0;
for(j=0; j<4; j++)
id = (id << 8) | (long) headbuff[j];
len = 0;
for(j=4; j<8; j++)
len = (len << 8) | (long) headbuff[j];
offset = 0;
for(j=8; j<12; j++)
offset = (offset << 8) | (long) headbuff[j];
crc = 0;
for(j=12; j<16; j++)
crc = (crc << 8) | (long) headbuff[j];
if(count != 0)
{
id1 = (id>>16) & 0xFF;
id2 = (id>>24) & 0xFF;
if(id1+id2 != 0xFF)
{
m_File.Close();
// per.Format("id invalid at 0x%x:0x%x",file_offset,id);
// MessageBox(per,"Error",MB_OK);
return 0;
}
}
if(offset > m_Flash)
{
m_File.Close();
// per.Format("offset invalid at 0x%x:0x%x, Flash : 0x%x",file_offset+8,offset, m_Flash);
// MessageBox(per,"Error",MB_OK);
return 0;
}
if(len>(offset-BLOCK_BASIC_HEADER_SIZE) && offset!=0)
{
m_File.Close();
// per.Format("len invalid at 0x%x:0x%x",file_offset+4,len);
// MessageBox(per,"Error",MB_OK);
return 0;
}
// check crc
if(!NCRC_FLAG(crc))
{
if(file_offset+BLOCK_BASIC_HEADER_SIZE+len > file_len)
{
m_File.Close();
// per.Format("can not read file:0x%x",file_offset+BLOCK_BASIC_HEADER_SIZE+len);
// MessageBox(per,"Error",MB_OK);
return 0;
}
verify_crc = 0xFFFFFFFF;
m_File.Seek(file_offset+BLOCK_BASIC_HEADER_SIZE, CFile::begin);
for(i=0; (unsigned int)i<len;)
{
read_size = (unsigned int)(i+0x400) <= len ? 0x400:len-i;
if(read_size != (unsigned int)m_File.Read(block_buffer,read_size))
{
m_File.Close();
return 0;
}
verify_crc = MG_Table_Driven_CRC(verify_crc,block_buffer,read_size);
i += read_size;
}
if(verify_crc != crc)
{
m_File.Close();
// per.Format("crc invalid at 0x%x:0x%x",file_offset+12, crc);
// MessageBox(per,"Error",MB_OK);
return 0;
}
}
count++;
if(id == DEFAULT_DATABASE_ID)
{
m_default_addr = file_offset;
m_default_whole_len = offset;
}
if(id == USER_DATABASE_ID)
{
m_user_data_addr = file_offset;
m_user_data_whole_len = len + 0x10;
}
if(offset == 0)
break;
file_offset += offset;
}
m_File.Close();
return count;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -