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

📄 main.cpp

📁 MTK 最新的flashtool工具的源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
DL_HANDLE_LIST_T g_DL_HANDLE_LIST = NULL;
std::vector<MultiLoadNode> g_DL_HANDLE_LIST_nodes;
MultiLoadNode *	g_pCurNode=&g_DL_HANDLE_NODE;
DA_HANDLE_T	 g_DA_HANDLE = NULL;
RB_HANDLE_T	 g_RB_HANDLE = NULL;
AUTH_HANDLE_T	g_AUTH_HANDLE = NULL;
FLASHTOOL_API_HANDLE_T  g_ft_handle = NULL;
ParamDownloadConfig_T g_ParamDownloadConfig;
ParamReadbackConfig_T g_ParamReadbackConfig;
ParamReadbackConfig_T g_ReadbackPage_Param;
ParameterType_T g_ParameterType;
unsigned int g_CostTimeSec;
unsigned int g_ProgressTimeSec;
unsigned int g_DisplayFinishedBytes;
int g_stopflag;
EMI_Config_S		g_EMI_Config[8];
DRAM_NFI_Setting	g_EMI_NFI_Setting={ 0, false, 0, 0, 0, 0, 0, 0 };	// apply BootLoader's DRAM setting by default

AnsiString	g_asNormalModeDAPath;
AnsiString	g_asTestModeDAPath;

ListViewItemType_E	g_LV_ItemType[]={
	 LV_BOOTLOADER
	,LV_NFB_DL_ROM
	,LV_DL_ROM
	,LV_OTP
	,LV_FTCFG
	,LV_SEC_RO
	,LV_CUST_PARA
};

//int iDownloadDataNACK_Count =0;

TargetDownloadErrorCount TDEC;

 int iGlobal_Progress;
 AnsiString asGlobal_StatusBar0Text;

TTreeNode * g_rootLoad;


static bool OpenReadDA_File( AnsiString asFileName)
{
	int ret;
	if(ret=DA_Load(g_DA_HANDLE, asFileName.c_str(), g_bDebugDA?_FALSE:_TRUE)) {
		if( S_FTHND_HANDLE_BUSY_NOW == ret ) {
			ShowMessage("Program is busy! Can not reload DA right now.");
		}
		else if( S_INVALID_DA_FILE == ret ) {
			ShowMessage(" \"" + asFileName + "\" is not a valid DA format!");
		}
		else {
			ShowMessage(" Load DA \"" + asFileName + "\" fail!");
		}
		return false;
	}

	MainForm->ed_DA_File->Text = asFileName;

	return true;
}


//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
static unsigned char change_DA_BaudRate_to_BROM_BaudRate( unsigned char uc_baud_rate )
{
   switch ( uc_baud_rate )
   {
      case UART_BAUD_460800 :
      case UART_BAUD_230400 :
           return UART_BAUD_4800; // High speed
      case UART_BAUD_115200 :
      case UART_BAUD_57600 :
      case UART_BAUD_38400 :
      case UART_BAUD_19200 :
      default:
           return UART_BAUD_19200;  // Normal speed
   } // switch;
}


//----------------------------------------------------------------------
void __fastcall TMainForm::Download1Click(TObject *Sender)
{
	int i;
	int RealReadLen =0;

	g_flashtool_function = FLASHTOOL_DOWNLOAD;

	if( NULL == g_pCurNode ) {
		return;
	}

	DL_HANDLE_T &dl_handle=g_pCurNode->m_dl_handle;
    MultiLoadNode  *p_node=g_pCurNode;
    DL_INFO		dl_info;
    DL_GetInfo(p_node->m_dl_handle, &dl_info);

	if ( asBin.IsEmpty() ) {
		if (! FileOpenDownloadAgentExecute() )
		{
			ShowMessage(" You must open download agent file before downloading. ");
			return;
		}
	}

	if ( !g_bDebugDA && g_pCurNode->m_scat_file.IsEmpty() ) {
		if ( ! FileOpenLinkMapExecute(g_pCurNode) )
		{
			ShowMessage(" You must open link map file before downloading. ");
			return;
		}
	}

	if ( !g_bDebugDA && mi_MultiLoad->Checked) {
		int handleNumber = 0;
		DL_GetHandleNumber(g_DL_HANDLE_LIST,&handleNumber);
		if( 0 == handleNumber){
			ShowMessage(" You must at least add 1 load in Multi-Load mode. ");
			return ;
		}
	}

    // Check ParamListView Download Items
    if(dl_info.m_config.m_security_supported){
	    if(true == p_node->m_param.m_otp_enable){
		    if(p_node->m_param.m_otp_filepath.Length() <= 0){
                ShowMessage(" You must choose a OTP file!");
                return;
    		}
	    	if(p_node->m_param.m_otp.m_len < 0){
                ShowMessage(" You must choose a OTP file with length more than 0. ");
                return;
		    }
    	}

	    if(true == p_node->m_param.m_sec_ro_enable){
		    if(p_node->m_param.m_sec_ro_filepath.Length() <= 0){
                ShowMessage(" You must choose a Secure RO file!");
                return;
    		}
	    	if(p_node->m_param.m_sec_ro.m_len < 0){
                ShowMessage(" You must choose a Secure RO file with length more than 0. ");
                return;
		    }
    	}

	    if(true == p_node->m_param.m_cust_para_enable){
		    if(p_node->m_param.m_cust_para_filepath.Length() <= 0){
                ShowMessage(" You must choose a Parameter file!");
                return;
    		}
	    	if(p_node->m_param.m_cust_para.m_len < 0){
                ShowMessage(" You must choose a Parameter file with length more than 0. ");
                return;
		    }
    	}
    }

	// clean Flash device info
	HWStatusBarClear();

	if(UpdateDA()) {
		return;
	}

    // check if NFB enabled
    _BOOL	bIsNFB=_FALSE;
    DL_IsNFB(dl_handle, &bIsNFB);

	// check BootLoader
/*	if(bIsNFB) {
		BL_INFO	bl_info;
		if( S_FTHND_FILE_IS_UPDATED == DL_BL_IsReady(dl_handle, &bl_info, _TRUE) ) {
			DL_BL_Load(dl_handle, bl_info.m_filepath);
		}
	}*/

	int ret;
	AnsiString s1, s2;
	BL_INFO		bl_info;
	ROM_INFO	rom_info;
	while(ret=DL_IsReady(dl_handle, &rom_info, &bl_info, _TRUE)) {
		if( S_FTHND_FILE_IS_UPDATED == ret ) {
			if(DL_Rom_Load(dl_handle, rom_info.index, rom_info.filepath)) {
				s1 = rom_info.name;
				s2 = rom_info.filepath;
				if( S_DL_LOAD_REGION_IS_OVERLAP == ret ) {
#if 0
/*					// reload all multibin files
					ret = DL_AutoLoadByScatRegionName(dl_handle, rom_info.filepath, NORMAL_ROM|RESOURCE_BIN|JUMPTABLE_BIN);
					if( S_DONE == ret ) {
						break;
					}
			        else {
						ShowMessage( "\"" + s2 + "\" is too large and causes region overlap!\nTry to reload all multi-bin files but it's failed!");
						return;
			    	}
				}*/
#else
					ShowMessage( "\"" + s2 + "\" is too large and causes region overlap!");
					return;
				}
#endif
				else {
					ShowMessage(" Load " + s1 + " from file \"" + s2 + "\" fail!");
					return;
				}
			}
		}
		else {
			break;
		}
	}

	if(g_bDebugDA) {
		DebugDAWindow->Text = "";
	}

	if(!g_bDebugDA) {
		
		// construct current download set 
		DownloadSet		dl_set;
		{
			// get scatter file path 
			dl_set.m_Scatter = g_pCurNode->m_scat_file.c_str();
			// get bin file info 
			unsigned short	count=0;
			if( (S_DONE==DL_GetCount(dl_handle, &count)) && (0<count) ) {
				ROM_INFO	rom[MAX_LOAD_SECTIONS];
				if( S_DONE == DL_Rom_GetInfoAll(dl_handle, rom, MAX_LOAD_SECTIONS) ) {
					for(i=0; i<count; i++) {
						DownloadBinFile		dl_bin;
						dl_bin.m_Filepath = rom[i].filepath;
						dl_bin.m_Enable = rom[i].enable?true:false;
						dl_set.m_BinFiles.push_back(dl_bin);
					}
				}
			}
	        if(bIsNFB) {
				// get BootLoader info
				BL_INFO		bl_info;
				if( S_DONE == DL_BL_GetInfo(dl_handle, &bl_info) ) {
					dl_set.m_BootLoader = bl_info.m_filepath;
					dl_set.m_BootLoader_Enable = bl_info.m_enable?true:false;
				}
	        }
		}
		
		// update download history 
        DownloadHistoryList::iterator	iter;
		for(iter=g_DownloadHistory.begin(); iter!=g_DownloadHistory.end(); iter++) {
			if( dl_set == (*iter) ) {
				if( iter != g_DownloadHistory.begin() ) {
					// if download set already exist, move it to the top of history
					g_DownloadHistory.erase(iter);
					g_DownloadHistory.push_front(dl_set);
				}
				goto download;
			}
		}
		// delete the oldest history 
		if( MAX_DOWNLOAD_HISTORY == g_DownloadHistory.size() ) {
			g_DownloadHistory.pop_back();
		}

		// insert new download set to the top of history 
		g_DownloadHistory.push_front(dl_set);
	}

download:
   if( MT6205 != g_BBCHIP_TYPE )
	   start_DL(true);
   else
	   Form_DL_GO->ShowModal();
}
//------------------------------------------------------------------------------
void TMainForm::start_DL( bool b_start_DL )
{
	g_CostTimeSec = 0;  // reset download time
	g_ProgressTimeSec = 0;

	sb_Main->Panels->Items[4]->Text = " 0:00 sec ";

	p1 = new tboot_1( true );
	p1->FreeOnTerminate  = true;
	p1->Resume();
}
//---------------------------------------------------------------------------


bool TMainForm::FileOpenDownloadAgentExecute()
{
//    index = 0; // ??
    static AnsiString asDA_Path;
    int iLastDel=0;

    if (asDA_Path.Length()>0)
    {
       OpenDownloadAgentDialog->InitialDir = asDA_Path;
    }
    else if( 0 < ed_DA_File->Text.Length() ) {
        iLastDel = ed_DA_File->Text.LastDelimiter("\\:");
        if ( iLastDel > 0)
        {
	        OpenDownloadAgentDialog->InitialDir = ed_DA_File->Text.SubString(1, iLastDel-1 );
        }
	}

    if ( OpenDownloadAgentDialog->Execute() )    // *.bin file
    {
         if ( OpenReadDA_File( OpenDownloadAgentDialog->FileName ))
         {
            asBin = OpenDownloadAgentDialog->FileName;
			if(g_bDebugDA) {
				// save TestMode DA path 
				g_asTestModeDAPath = asBin;
			}
			else {
				// save NormalMode DA path
				g_asNormalModeDAPath = asBin;
			}
            iLastDel = asBin.LastDelimiter("\\:");

            if ( iLastDel > 0)
            {
               asDA_Path = asBin.SubString(1, iLastDel-1 );

            }

            return true;
         }
         else
           return false;
    }
    else
        return false;
}


void __fastcall TMainForm::FileOpenDownloadAgentExecute(TObject *Sender)
{
    FileOpenDownloadAgentExecute();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::OptionsSetupExecute(TObject *Sender)
{
//     OptiForm->Visible = true;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::test_tray_iconClick(TObject *Sender)
{
//   TrayIcon1->Animate = true;
}
//---------------------------------------------------------------------------

bool TMainForm::Parse(MultiLoadNode *p_node, AnsiString asMapFileName, bool bAutoLoad)
{
	int ret;
	unsigned short count;

	if( NULL == p_node ) {
		return false;
	}

	DL_HANDLE_T &dl_handle=p_node->m_dl_handle;

	// load scatter
	if(ret=DL_LoadScatter(dl_handle, asMapFileName.c_str())) {
		if( S_FTHND_HANDLE_BUSY_NOW == ret ) {
			ShowMessage("Program is busy! Can not reload scat file right now.");
		}
		else if( S_DL_SCAT_OFFSET_IS_NOT_WORD_ALIGN == ret ) {
			ShowMessage("Failed to load scatter file \"" + asMapFileName + "\"!\n\nThe offset value of one load region is not WORD alignment!");
		}
		else if( S_DL_SCAT_ADDR_IS_NOT_WORD_ALIGN == ret ) {
			ShowMessage("Failed to load scatter file \"" + asMapFileName + "\"!\n\nThe region begin address of one load region is not WORD alignment!");
		}
		else if( S_DL_SCAT_ADDR_IS_NOT_ASCENDING_ORDER == ret ) {
			ShowMessage("Failed to load scatter file \"" + asMapFileName + "\"!\n\nThe region begin address of one load region is not ascending sorting sequence!");
		}
		else {
			ShowMessage("Incorrect scatter file format!");
		}
		return false;
	}

	p_node->m_scat_file = asMapFileName;

	if(bAutoLoad) {
		// auto load download bin files
		DL_AutoLoadByScatRegionName(dl_handle, asMapFileName.c_str(), NORMAL_ROM|RESOURCE_BIN|JUMPTABLE_BIN);
	}

	RedrawReadbackList();
	RedrawDownloadList(p_node, false);

    // show Parameter List View
    DL_INFO dl_info;
    if( !DL_GetInfo(dl_handle, &dl_info) ){
        if( dl_info.m_config.m_security_supported ){

⌨️ 快捷键说明

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