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

📄 tboot_1.cpp

📁 MTK 最新的flashtool工具的源代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    button_sentry	s;

    do
    {
        //check if user click the stop button
//        if(g_stopflag == BOOT_STOP || Terminated)
        if(g_stopflag == BOOT_STOP)
                break;

       Synchronize(ClearStatusBar);
//        MainForm->HWStatusBarClear();
        progress_hint = "";
        progress_forecolor = clBlue;
        progress_percentage = 0;
        progress_finished_bytes = 0;

        bUpdateFinishBytes = true;
        SyncInitProgress();

        if(g_bDebugDA) {
                DebugDA();
                // Memory Test cannot be operated automatically.
                break;
        }
        else {
                FlashTool();

                if( FLASHTOOL_DOWNLOAD == g_flashtool_function ) {
                        // Only Download can be operated automatically.
                        // Sleep 3 seconds for Automatic Module Test
                        Sleep(3000);
                }
                else{
                        // Format and Readback cannot be operated automatically.
                        break;
                }
        }
    } while(g_bAutomation);

    Terminate();
    g_stopflag = 0;
}

class com_sentry {
public:
  	com_sentry(HANDLE &com, unsigned int com_port): m_hCOM(com) {
		open_com_port(m_hCOM, com_port, 115200);
	}
  	~com_sentry() {
 		close_com_port(m_hCOM);
    }
private:
  	HANDLE &m_hCOM;
};

void _fastcall tboot_1::ResetFakeProgress() {
	MainForm->CGauge1->Hint = progress_hint;
	MainForm->CGauge1->ForeColor = progress_forecolor;
	MainForm->CGauge1->Progress = 0;
}

void _fastcall tboot_1::StartFakeProgress() {
	MainForm->m_FakeProgressTimer->Enabled = true;
}

void _fastcall tboot_1::StopFakeProgress() {
	MainForm->m_FakeProgressTimer->Enabled = false;
}

void _fastcall tboot_1::FinishFakeProgress() {
	MainForm->CGauge1->Progress = 100;
	MainForm->m_FakeProgressTimer->Enabled = false;
}

void _fastcall tboot_1::SyncStartFakeProgress() {
   	Synchronize(StartFakeProgress);
}

static int __stdcall cb_boot_meta_in_brom_stage(unsigned int brom_handle, HANDLE hCOM, void *usr_arg) {
	tboot_1 *t=(tboot_1 *)usr_arg;
	// start fake progress
	t->SyncStartFakeProgress();
    return 0;
}

bool _fastcall tboot_1::EnterMETA(const BBCHIP_TYPE  bbchip, const EXT_CLOCK  ext_clock) {

	static 	META_COMM_BAUDRATE  allow_baudrate[11]={
		META_BAUD115200,
		META_BAUD230400,
		META_BAUD460800,
		META_BAUD921600,
		META_BAUD_END,
		META_BAUD_END,
		META_BAUD_END,
		META_BAUD_END,
		META_BAUD_END,
		META_BAUD_END,
		META_BAUD_END
	};

    META_RESULT         meta_ret;
	META_Connect_Req	meta_req;
	META_Connect_Report	meta_report;

	meta_req.com_port = COM_PORT;
	memcpy(meta_req.baudrate, allow_baudrate, sizeof(meta_req.baudrate));
	meta_req.flowctrl = META_SW_FLOWCTRL;
	meta_req.ms_connect_timeout = 40000;
    meta_req.boot_meta_arg.m_bbchip_type = g_BBCHIP_TYPE;
    meta_req.boot_meta_arg.m_ext_clock = g_EXT_CLOCK;
    meta_req.boot_meta_arg.m_ms_boot_timeout = BOOT_INFINITE;
    meta_req.boot_meta_arg.m_max_start_cmd_retry_count = g_MaxBromStartCmdRetryCount;
	meta_req.boot_meta_arg.m_cb_in_brom_stage = cb_boot_meta_in_brom_stage;
	meta_req.boot_meta_arg.m_cb_in_brom_stage_arg = this;
    meta_req.boot_meta_arg.m_speedup_brom_baudrate = g_SpeedUpBootROMBaudrate?_TRUE:_FALSE;
	// WM_BROM_READY_TO_POWER_ON_TGT
	meta_req.boot_meta_arg.m_ready_power_on_wnd_handle	= NULL;
	meta_req.boot_meta_arg.m_ready_power_on_wparam		= NULL;
	meta_req.boot_meta_arg.m_ready_power_on_lparam		= NULL;
	// serial link authentication arg
	meta_req.boot_meta_arg.m_auth_handle = g_AUTH_HANDLE;
	meta_req.boot_meta_arg.m_cb_sla_challenge         = SLA_Challenge;
	meta_req.boot_meta_arg.m_cb_sla_challenge_arg     = this;
	meta_req.boot_meta_arg.m_cb_sla_challenge_end     = SLA_Challenge_END;
	meta_req.boot_meta_arg.m_cb_sla_challenge_end_arg = this;

	// reset fake progress
	progress_hint = "Reset NVRAM to factory default setting ...";
	progress_forecolor = clGreen;
	Synchronize(ResetFakeProgress);

	if( META_SUCCESS != (meta_ret=META_ConnectWithTarget(&meta_req, &g_stopflag, &meta_report)) ) {
		// stop fake progress 
		Synchronize(StopFakeProgress);
		asMsg = "META_ConnectWithTarget(): ERROR: " + AnsiString(META_GetErrorString(meta_ret));
		Synchronize(ShowMsg);
		return false;
	}

	if( META_SUCCESS != (meta_ret=META_NVRAM_ResetToFactoryDefault(40000)) ) {
		META_DisconnectWithTarget();
		// stop fake progress
		Synchronize(StopFakeProgress);
		asMsg = "META_NVRAM_ResetToFactoryDefault(): ERROR: " + AnsiString(META_GetErrorString(meta_ret));
		Synchronize(ShowMsg);
		return false;
	}

	META_DisconnectWithTarget();
	// finish fake progress
	Synchronize(FinishFakeProgress);
	return true;
}

void __fastcall tboot_1::FlashTool()
{
	AnsiString	s1, s2;
	int ret;

    memset(&arg, 0, sizeof(arg));
    memset(&result, 0, sizeof(result));
    memset(&dl_arg, 0, sizeof(dl_arg));
    memset(&dl_result, 0, sizeof(dl_result));
    memset(&rb_arg, 0, sizeof(rb_arg));
    memset(&rb_result, 0, sizeof(rb_result));
    memset(&fmt_arg, 0, sizeof(fmt_arg));
    memset(&fmt_result, 0, sizeof(fmt_result));

    // fill COM_PORT_SETTING
    com.com.number = COM_PORT;
    com.baudrate = BAUD_RATE;
    com.ms_read_timeout = g_COM_Read_Timeout;
	com.ms_write_timeout = g_COM_Write_Timeout;

    // fill FLASHTOOL_ARG
    arg.m_boot_arg.m_bbchip_type = g_BBCHIP_TYPE;
    arg.m_boot_arg.m_ext_clock = g_EXT_CLOCK;
    arg.m_boot_arg.m_ms_boot_timeout = BOOT_INFINITE;
    arg.m_boot_arg.m_max_start_cmd_retry_count = g_MaxBromStartCmdRetryCount;
    arg.m_boot_arg.m_p_bank0_mem_cfg = g_EMI_Config[0].m_bAuto?NULL:&(g_EMI_Config[0].m_Config);
    arg.m_boot_arg.m_p_bank1_mem_cfg = g_EMI_Config[1].m_bAuto?NULL:&(g_EMI_Config[1].m_Config);
    if(g_bSkipBootRom)
    	arg.m_boot_arg.m_da_handle = NULL;
    else
    	arg.m_boot_arg.m_da_handle = g_DA_HANDLE;
    arg.m_boot_arg.m_cb_download_da_init = cb_download_da_init;
    arg.m_boot_arg.m_cb_download_da_init_arg = this;
    arg.m_boot_arg.m_cb_download_da = cb_download_da;
    arg.m_boot_arg.m_cb_download_da_arg = this;
	arg.m_boot_arg.m_cb_in_brom_stage = cb_in_brom_stage;
	arg.m_boot_arg.m_cb_in_brom_stage_arg = &(result.m_da_report.m_bbchip_type);
    arg.m_boot_arg.m_speedup_brom_baudrate = g_SpeedUpBootROMBaudrate?_TRUE:_FALSE;
	// WM_BROM_READY_TO_POWER_ON_TGT
	arg.m_boot_arg.m_ready_power_on_wnd_handle	= NULL;
	arg.m_boot_arg.m_ready_power_on_wparam		= NULL;
	arg.m_boot_arg.m_ready_power_on_lparam		= NULL;
	// serial link authentication arg
	arg.m_boot_arg.m_auth_handle = g_AUTH_HANDLE;
	arg.m_boot_arg.m_cb_sla_challenge         = SLA_Challenge;
	arg.m_boot_arg.m_cb_sla_challenge_arg     = this;
	arg.m_boot_arg.m_cb_sla_challenge_end     = SLA_Challenge_END;
	arg.m_boot_arg.m_cb_sla_challenge_end_arg = this;

	arg.m_cb_da_report = cb_da_report;
    arg.m_cb_da_report_arg = this;
	arg.m_cb_da_report = cb_da_report;
    arg.m_cb_da_report_arg = this;
    arg.m_cb_security_pre_process_notify = cb_security_pre_process_notify;
    arg.m_cb_security_pre_process_notify_arg = this;
	arg.m_baudrate_full_sync_count = g_Baudrate_FullSync_Count;
	arg.m_nor_chip_select[0] = CS0;
    arg.m_nor_chip_select[1] = CS_WITH_DECODER;
   	arg.m_nand_chip_select = g_bNFI_SelectCS1?CS1:CS0;
	arg.m_nand_acccon = g_EMI_NFI_Setting.m_nand_acccon;



//***FLASHTOOL_DOWNLOAD===================================================
    if( FLASHTOOL_DOWNLOAD == g_flashtool_function ) {
    	// fill FLASHTOOL_DOWNLOAD_ARG
        if( MainForm->mi_MultiLoad->Checked){   //ray 2006.05.18 : Multi-Load
			dl_arg.m_dl_handle_list = g_DL_HANDLE_LIST;
			dl_arg.m_dl_handle = NULL;
        }
        else{
			dl_arg.m_dl_handle = g_DL_HANDLE_NODE.m_dl_handle;
			dl_arg.m_dl_handle_list = NULL;
        }
        dl_arg.m_download_accuracy = ACCURACY_AUTO;
        dl_arg.m_cb_download_flash_init = cb_download_flash_init;
        dl_arg.m_cb_download_flash_init_arg = this;
        dl_arg.m_cb_download_flash = cb_download_flash;
        dl_arg.m_cb_download_flash_arg = this;
	    dl_arg.m_cb_security_post_process_notify = cb_security_post_process_notify;
	    dl_arg.m_cb_security_post_process_notify_arg = this;

		if( NOR_OPERATION == g_OperationMethod ) {
		    dl_arg.m_format_cfg.m_format_begin_addr = g_ulFormatStartAddr;
	    	dl_arg.m_format_cfg.m_format_length = g_ulFormatLength;
    	}
    	else {
		    dl_arg.m_format_cfg.m_format_begin_addr = g_ulNandFormatStartAddr;
	    	dl_arg.m_format_cfg.m_format_length = g_ulNandFormatLength;
    	}
		dl_arg.m_format_cfg.m_validation = g_FormatValidation?_TRUE:_FALSE;

	switch(g_FormatAfterDownloadOption) {
		case RESET_TO_FACTORY:
			// disable format
	        dl_arg.m_format_enable = _FALSE;
		    dl_arg.m_format_cfg.m_auto_format_fat = _FALSE;
		    // enable watchdog
	        dl_arg.m_enable_wdt_timeout = _TRUE;
    	    dl_arg.m_ms_wdt_timeout_interval = 1500;
			break;
		case AUTO_FORMAT:
			// enable auto format
	        dl_arg.m_format_enable = _TRUE;
		    dl_arg.m_format_cfg.m_auto_format_fat = _TRUE;
		    // disable watchdog
	        dl_arg.m_enable_wdt_timeout = _FALSE;
    	    dl_arg.m_ms_wdt_timeout_interval = 0;
			break;
		case MANUAL_FORMAT:
			// enable manual format
	        dl_arg.m_format_enable = _TRUE;
		    dl_arg.m_format_cfg.m_auto_format_fat = _FALSE;
		    // disable watchdog
	        dl_arg.m_enable_wdt_timeout = _FALSE;
    	    dl_arg.m_ms_wdt_timeout_interval = 0;
			break;
		case DISABLE_FORMAT:
		default:
			// disable format
	        dl_arg.m_format_enable = _FALSE;
		    dl_arg.m_format_cfg.m_auto_format_fat = _FALSE;
		    // disable watchdog
	        dl_arg.m_enable_wdt_timeout = _FALSE;
    	    dl_arg.m_ms_wdt_timeout_interval = 0;
			break;
		}

        dl_arg.m_cb_format_report_init = cb_format_report_init;
        dl_arg.m_cb_format_report_init_arg = this;
        dl_arg.m_cb_format_report = cb_format_report;
        dl_arg.m_cb_format_report_arg = this;
        dl_arg.m_enable_tgt_res_layout_check = g_bCheckTgtResourceLayout?_TRUE:_FALSE;
        dl_arg.m_enable_bbchip_ver_check = g_bCheckECOVer?_TRUE:_FALSE;
        dl_arg.m_cb_format_statistics = cb_format_statistics;
        dl_arg.m_cb_format_statistics_arg = this;

		// parameter
		if(!MainForm->mi_MultiLoad->Checked) {
			dl_arg.m_otp_enable = g_DL_HANDLE_NODE.m_param.m_otp_enable;
			dl_arg.m_otp_op = g_DL_HANDLE_NODE.m_param.m_otp_op;
			dl_arg.m_otp.m_addr = g_DL_HANDLE_NODE.m_param.m_otp.m_addr;
			dl_arg.m_otp.m_data = g_DL_HANDLE_NODE.m_param.m_otp.m_data;
			dl_arg.m_otp.m_len = g_DL_HANDLE_NODE.m_param.m_otp.m_len;
			dl_arg.m_ft_cfg_enable = g_DL_HANDLE_NODE.m_param.m_ft_cfg_enable;
			dl_arg.m_ft_cfg.m_security_cfg.m_uint32 = g_DL_HANDLE_NODE.m_param.m_ft_cfg.m_security_cfg.m_uint32;
			dl_arg.m_sec_ro_enable = g_DL_HANDLE_NODE.m_param.m_sec_ro_enable;
			dl_arg.m_sec_ro.m_data = g_DL_HANDLE_NODE.m_param.m_sec_ro.m_data;
			dl_arg.m_sec_ro.m_len = g_DL_HANDLE_NODE.m_param.m_sec_ro.m_len;
	        dl_arg.m_cust_para_enable = g_DL_HANDLE_NODE.m_param.m_cust_para_enable;
			dl_arg.m_cust_para.m_data = g_DL_HANDLE_NODE.m_param.m_cust_para.m_data;
			dl_arg.m_cust_para.m_len = g_DL_HANDLE_NODE.m_param.m_cust_para.m_len;
		}
		dl_arg.m_cb_param_process_init		= cb_update_param_init;
		dl_arg.m_cb_param_process_init_arg	= this;
		dl_arg.m_cb_param_process			= cb_update_param;
		dl_arg.m_cb_param_process_arg		= this;

		dl_arg.m_cb_post_process_init		= cb_post_process_init;
		dl_arg.m_cb_post_process_init_arg	= this;
		dl_arg.m_cb_post_process			= cb_post_process;
		dl_arg.m_cb_post_process_arg		= this;

        // for NFB bloader
        dl_arg.m_cb_download_bloader_init = cb_download_bloader_init;
        dl_arg.m_cb_download_bloader_init_arg = this;
        dl_arg.m_cb_download_bloader = cb_download_bloader;
        dl_arg.m_cb_download_bloader_arg = this;

        // for FlashTool Automation
        if(g_bAutomation) {
            dl_arg.m_enable_wdt_timeout = _TRUE;
    	    dl_arg.m_ms_wdt_timeout_interval = 1500;
        }

		ret = FlashDownload(&com, &arg, &result, &dl_arg, &dl_result, &g_stopflag);
		if( S_DONE != ret ) {
			// stop timer
			Synchronize(StopTimer);
		}
	}
    else if( FLASHTOOL_READBACK == g_flashtool_function ) {
//***FLASHTOOL_READBACK===================================================    	
		// fill FLASHTOOL_READBACK_ARG 
		switch(g_OperationMethod) {
		case NFB_OPERATION:
			rb_arg.m_storage_type = HW_STORAGE_NAND;
			break;
		case NOR_OPERATION:
		default:
			rb_arg.m_storage_type = HW_STORAGE_NOR;
		}
        rb_arg.m_rb_handle = g_RB_HANDLE;
        rb_arg.m_readback_accuracy = ACCURACY_AUTO;
        rb_arg.m_readback_accuracy = ACCURACY_AUTO;
        rb_arg.m_cb_readback_flash_init = cb_readback_flash_init;
        rb_arg.m_cb_readback_flash_init_arg = this;
        rb_arg.m_cb_readback_flash = cb_readback_flash;
        rb_arg.m_cb_readback_flash_arg = this;

		ret = FlashReadback(&com, &arg, &result, &rb_arg, &rb_result, &g_stopflag);
		if( S_DONE != ret ) {
			// stop timer
			Synchronize(StopTimer);
		}
    }
    else if( FLASHTOOL_FORMAT == g_flashtool_function ) {
        if( RESET_TO_FACTORY == g_FormatOption ) {
        	// reset stop flag 
			g_stopflag = 0;
        	// go to META mode reset
        	ret = S_DONE;
    	}
    	else {
			// fill FLASHTOOL_FORMAT_ARG
			switch(g_OperationMethod) {
			case NFB_OPERATION:
				fmt_arg.m_storage_type = HW_STORAGE_NAND;
			    fmt_arg.m_format_cfg.m_format_begin_addr = g_ulNandFormatStartAddr;
		    	fmt_arg.m_format_cfg.m_format_length = g_ulNandFormatLength;
				break;
			case NOR_OPERATION:
			default:
				fmt_arg.m_storage_type = HW_STORAGE_NOR;
			    fmt_arg.m_format_cfg.m_format_begin_addr = g_ulFormatStartAddr;
		    	fmt_arg.m_format_cfg.m_format_length = g_ulFormatLength;
			}
	        fmt_arg.m_cb_format_report_init = cb_format_report_init;
	        fmt_arg.m_cb_format_report_init_arg = this;
	        fmt_arg.m_cb_format_report = cb_format_report;
	        fmt_arg.m_cb_format_report_arg = this;
	        fmt_arg.m_cb_format_statistics = cb_format_statistics;
	        fmt_arg.m_cb_format_statistics_arg = this;
	        fmt_arg.m_erase_flag = g_EraseFlag;
	        fmt_arg.m_format_cfg.m_validation = g_FormatValidation?_TRUE:_FALSE;
			fmt_arg.m_format_cfg.m_auto_format_fat = AUTO_FORMAT==g_FormatOption?_TRUE:_FALSE;

			ret = FlashFormat(&com, &arg, &result, &fmt_arg, &fmt_result, &g_stopflag);
		}

		if( S_DONE != ret ) {
			// stop timer
			Synchronize(StopTimer);
		}

⌨️ 快捷键说明

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