⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 download.cpp

📁 AliEditor_Source code
💻 CPP
📖 第 1 页 / 共 3 页
字号:
{
	/* open serial port for test */
	if(!m_SerPort.Open(m_Port+1, m_Baud))
	{
		MessageBox("Can not open serial port!","Error",MB_OK);
		return FALSE;
	}

	Sleep(100);
	CWnd *pWnd = GetDlgItem(IDC_STATIC_STATUS);
	if(pWnd != NULL)
		pWnd->ShowWindow(SW_SHOW);
	Sleep(100);
	//Send test info]
	int count = 0;
	while(!Command_comtest())
	{
		m_SerPort.SendData("\r",1);
		Sleep(100);
		count++;
		if(count > 20)
		{
			AfxMessageBox("serial port probe error!");
			return FALSE;
		}
	}

	pWnd = GetDlgItem(IDC_STATIC_OK1);
	if(pWnd != NULL)
		pWnd->ShowWindow(SW_SHOW);
	Sleep(100);
	return TRUE;
}

BOOL CDownLoad::CheckVerInfo()
{

	if(m_Path.IsEmpty())
	{
		AfxMessageBox("Can't find upgade bin file");

		return FALSE;
	}

	CWnd *pWnd = GetDlgItem(IDC_STATIC_CONNECT);
	if(pWnd != NULL)
		pWnd->ShowWindow(SW_SHOW);
	//get version info
	if(!Command_version())
	{
	//	AfxMessageBox("Command_version failed");
		
		return FALSE;
	}

	m_Type = 0;

	slave_status = 0;

	if((slave_status!=0) && m_Type != 0)
	{
		AfxMessageBox("Slave data has been destroyed, need upgrade all code!");
		
		pWnd = GetDlgItem(ID_WIZBACK);
		pWnd->EnableWindow(TRUE);

		pWnd = GetDlgItem(ID_WIZNEXT);
		pWnd->EnableWindow(FALSE);

		return FALSE;
	}
		
	if(!SlaveReorg())
		return FALSE;

	Sleep(100);
	pWnd = GetDlgItem(IDC_STATIC_OK2);
	if(pWnd != NULL)
		pWnd->ShowWindow(SW_SHOW);
    Sleep(100);
	return TRUE;
}

BOOL CDownLoad::SendAddress()
{
	if(!Command_address())
	{
		return FALSE;
	}
	return TRUE;
}

BOOL CDownLoad::StartDownload()
{

	CWnd *pWnd = GetDlgItem(IDC_STATIC_DOWNLOAD);
	CString per;
	int i;

	if(pWnd != NULL)
		pWnd->ShowWindow(SW_SHOW);

	CProgressCtrl *pPrgrss = (CProgressCtrl *)GetDlgItem(IDC_PROGRESS_SDRAM);
	if(pPrgrss != NULL)
		pPrgrss->ShowWindow(SW_SHOW);

	pWnd = GetDlgItem(IDC_STATIC_SDRAM_PERCENT);
	if(pWnd != NULL)
		pWnd->ShowWindow(SW_SHOW);
	
	pPrgrss->SetPos(0);
	per.Format("%d",0);
	per += _T("%");
	pWnd->SetWindowText(per);

	pPrgrss->SetRange(0,100);

	trans_size = 0;
	prog = 0;

	if(m_bootloader) i = 0;
	else i = 1;

	for(; i<slave_reorg_number; i++)
	{
		if(slave_reorg_blocks[i].type == 1) //move
		{
			if(!Command_move(slave_reorg_blocks[i].index, slave_reorg_blocks[i].offset))
			{
				return FALSE;
			}
		}
		else //transfer
		{
			if(!Command_transfer(slave_reorg_blocks[i].index))
			{
				return FALSE;
			}
		}
	}
	
	pWnd = GetDlgItem(IDC_STATIC_OK3);
	if(pWnd != NULL)
		pWnd->ShowWindow(SW_SHOW);

	
	return TRUE;
}

BOOL CDownLoad::StartBurnFlash()
{
	m_start_burn.ShowWindow(SW_SHOW);

	CProgressCtrl *pPrgrss = (CProgressCtrl *)GetDlgItem(IDC_PROGRESS_FLASH);
	if(pPrgrss != NULL)
		pPrgrss->ShowWindow(SW_SHOW);

	CWnd *pWnd = GetDlgItem(IDC_STATIC_FLASH_PERCENT);
	if(pWnd != NULL)
		pWnd->ShowWindow(SW_SHOW);

	CString per;

	pPrgrss->SetPos(0);
	per.Format("%d",0);
	per += _T("%");
	pWnd->SetWindowText(per);

	pPrgrss->SetRange(0,100);
	
	m_burn_ok.ShowWindow(SW_HIDE);
	
	/* Burn flash */
	if(!Command_burn())
	{
		AfxMessageBox("burn error!");

		return FALSE;
	}

	m_burn_ok.ShowWindow(SW_SHOW);

	return TRUE;
}

UINT CDownLoad::DownLoadThread(LPVOID lParam)
{
	Sleep(200);

	CDownLoad *pDownLoad = (CDownLoad *)lParam;

	pDownLoad->m_bNodifyReset = false;

	pDownLoad->slave_blocks = NULL;
	pDownLoad->slave_reorg_blocks = NULL;

	MG_Setup_CRC_Table();

	pDownLoad->block_number = pDownLoad->getBlockNumber();

	pDownLoad->getBlocks();
	
	if(!pDownLoad->CheckInputData())
	{
	//	AfxMessageBox("CheckInputData Failed");

		goto ABNORMAL;
	}

	if(!pDownLoad->CheckSerialPort())
	{
	//	AfxMessageBox("Check Serial Port Failed");

		goto ABNORMAL;
	}
	if(!pDownLoad->CheckVerInfo())
	{
	//	AfxMessageBox("Check version Info Failed");

		goto ABNORMAL;
	}
	if(!pDownLoad->SendAddress())
	{
		AfxMessageBox("Set address fail");

		goto ABNORMAL;
	}
	if(!pDownLoad->StartDownload())	
	{
		AfxMessageBox("Download sdram fail.");
		
		goto ABNORMAL;
	/* Wait the Page3 active */
	}

	if(!pDownLoad->StartBurnFlash())
	{	
		AfxMessageBox("Burn flash fail");
ABNORMAL:
		//exit(0);	

		pDownLoad->m_SerPort.Close();

		if(pDownLoad->slave_blocks != NULL)
			free(pDownLoad->slave_blocks);
		if(pDownLoad->slave_reorg_blocks != NULL)
			free(pDownLoad->slave_reorg_blocks);

		return 1;
	}
	
	if(pDownLoad->slave_blocks != NULL)
		free(pDownLoad->slave_blocks);
	if(pDownLoad->slave_reorg_blocks != NULL)
		free(pDownLoad->slave_reorg_blocks);

	Sleep(1000);

	pDownLoad->Command_reboot();

	//pDownLoad->EndDialog(1);
	pDownLoad->GetDlgItem(IDCANCEL)->EnableWindow(FALSE);
	pDownLoad->GetDlgItem(IDCANCEL)->ShowWindow(SW_HIDE);
	pDownLoad->GetDlgItem(IDOK)->EnableWindow(TRUE);
	pDownLoad->GetDlgItem(IDOK)->ShowWindow(SW_SHOW);
	

	return 1;
}

BOOL CDownLoad::CheckInputData()
{
	int i;

	/*check file path*/
	if(m_Path == "")
	{
		MessageBox("Please enter file!","Warning",MB_ICONEXCLAMATION|MB_OK);
		return FALSE;
	}

	if(m_Type == -1)
	{
		MessageBox("Please select upgrade type!","Warning",MB_ICONEXCLAMATION|MB_OK);
		return FALSE;
	}

	/*clear blocks upgrade flag*/
	for(i=0; i< block_number; i++)
	{
		blocks[i].upg_flag = 0;
	}

	/*set block upgrade flag*/
	if(m_Type == 0)  //all code
	{
		if(m_bootloader) i = 0;
		else i = 1;

		for(; i<block_number; i++)
		{
		//	if(blocks[i].id == DEFAULT_DATABASE_ID || blocks[i].id == MAINCODE_ID || blocks[i].id == USER_DATABASE_ID)
			if( ( blocks[i].id == DEFAULT_DATABASE_ID && m_bDownLoadDdb) 
				|| ( blocks[i].id == USER_DATABASE_ID && m_bDownLoadUdb) )
		//	if( blocks[i].id == USER_DATABASE_ID )
			{
				blocks[i].upg_flag = 1;//temp solution
			}
		}
	}
	else
	{
		blocks[m_Type].upg_flag = 1;
	}

	return TRUE;
}

#define NCRC_FLAG(x)    (x == 0x4E435243) //"NCRC"

int CDownLoad::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;

	/* 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();
			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",file_offset+8,offset);
			//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(offset == 0)
			break;
		file_offset += offset;
	}

	m_File.Close();
	return count;
}

int CDownLoad::getBlocks()
{
	unsigned int id,len,offset;
	CString name;
	unsigned int file_offset, file_len;
	unsigned char headbuff[BLOCK_HEADER_SIZE];
	int i,j,count=0;

	/* open file */
	if(!m_File.Open(m_Path,CFile::shareDenyRead,NULL))
	{
		return 0;
	}

	//seek the current file pointer by m_type
	file_offset = 0;
	file_len = m_File.GetLength();

	for(i=0; i< block_number; i++)
	{
		// read head
		m_File.Seek(file_offset, CFile::begin);
		if(BLOCK_HEADER_SIZE != m_File.Read(headbuff,BLOCK_HEADER_SIZE))
		{
			m_File.Close();
			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];

		name = &headbuff[16];
	
		blocks[i].id = id;
		blocks[i].offset = offset;
		if(i == 0)
		{
			//pSheet->blocks[i].id = 0;
			blocks[i].len = 0;
			blocks[i].name = "allcode";
			blocks[i].addr = 0;
		}
		else
		{
			//pSheet->blocks[i].id = id;
			blocks[i].len = len;
			blocks[i].name = name;
			blocks[i].addr = blocks[i-1].addr + blocks[i-1].offset;
		}

		if(offset == 0)
			break;
		file_offset += offset;
	}

	if(m_bootloader)
	{
		blocks[0].len = blocks[0].offset-BLOCK_BASIC_HEADER_SIZE;
	}
	else
	{

		blocks[0].addr = blocks[0].offset;
		//pSheet->blocks[0].len = pSheet->blocks[pSheet->block_number-2].addr + pSheet->blocks[pSheet->block_number-2].len - pSheet->blocks[0].offset+BLOCK_BASIC_HEADER_SIZE;
		blocks[0].len = blocks[block_number-2].addr + blocks[block_number-2].len - blocks[0].offset+BLOCK_HEADER_SIZE;
	}

	m_File.Close();
	return count;
}

BOOL CDownLoad::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	GetDlgItem(IDOK)->EnableWindow(FALSE);
	GetDlgItem(IDOK)->ShowWindow(SW_HIDE);

	m_Type = 0;
	m_Flash = 0x200000;  //2M
	m_Bootloader = 0x10000;  //64K

	InitializeCriticalSection(&m_cs);

	pThread = AfxBeginThread(DownLoadThread, this);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDownLoad::OnCancel() 
{
	// TODO: Add extra cleanup here
	OnCancelDownload(0, 0);

	//Command_reboot();
	if( m_bNodifyReset )
		AfxMessageBox("Please reset STB.");
	
	CDialog::OnCancel();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -