// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: image_file_burn_data_verify(WdtDevice*, ExecParam*)
int image_file_burn_data_verify(WDT_DEV *pdev, EXEC_PARAM *pparam)
{
	BOARD_INFO	*pinfo = &pdev->board_info;
	CHUNK_INFO_EX	chunk_info_fw;
	CHUNK_INFO_EX	chunk_info_cfg;
	int		is_fw_update = 1;
	int		is_cfg_update = 1;
	int 	err = 0;


	if (!pdev || !pparam)
		return 0;

	if (pinfo->dev_type & (FW_MAYBE_ISP | FW_WDT8760_2_ISP))
		return 0;	

        if (!init_n_scan_device(pdev, pparam, 0)) {
		printf("Wdt controller not found !\n");
                goto exit_burn;
        }

	if (pdev->board_info.dev_type & FW_WDT8790) {
		return update_fw_by_wif2(pdev, (char*)pparam->image_file);
	}

        if (pdev->is_legacy) {
                printf("Not support legacy FW update !\n");
                goto exit_burn;
        }

	if (!load_wif(pdev, (char*)pparam->image_file)){
		printf("Load WIF failed !\n");
		goto exit_burn;
	}

	
	memset(&chunk_info_fw, 0, sizeof(CHUNK_INFO_EX));
	memset(&chunk_info_cfg, 0, sizeof(CHUNK_INFO_EX));
	
	if (pdev->board_info.dev_type & FW_WDT8755) {
		if (pinfo->dev_info.w8755_dev_info.boot_partition == W8755_BP_SECONDARY) {
			if (!pdev->func_wh_get_chunk_info(pdev->wif_access.wif_handle, CHUNK_ID_FRWD, &chunk_info_fw)) {
				wh_printf("Not found sec fw chunk !\n");
				goto exit_burn;
			}
			if (!pdev->func_wh_get_chunk_info(pdev->wif_access.wif_handle, CHUNK_ID_CNFD, &chunk_info_cfg)) {
				wh_printf("Not found sec cfg chunk !\n");
				goto exit_burn;
			}
			if (!chunk_info_fw.chuckInfo.versionNumber) {
				wh_printf("Wif fw version is null!\n");
				goto exit_burn;
			}
		}else{
			if (!pdev->func_wh_get_chunk_info(pdev->wif_access.wif_handle, CHUNK_ID_FRWR, &chunk_info_fw)) {
				wh_printf("Not found fw chunk !\n");
				goto exit_burn;
			}
			if (!pdev->func_wh_get_chunk_info(pdev->wif_access.wif_handle, CHUNK_ID_CNFG, &chunk_info_cfg)) {
				wh_printf("Not found cfg chunk !\n");
				goto exit_burn;
			}
			if (!chunk_info_fw.chuckInfo.versionNumber) {
				wh_printf("Wif fw version is null!\n");
				goto exit_burn;
			}
		
			err = fw_version_check(pparam, pinfo, &chunk_info_fw);
			/* fw is not matching */
			if (err == 0)	
				goto exit_burn;


		}
	}else{
		
		if (!pdev->func_wh_get_chunk_info(pdev->wif_access.wif_handle, CHUNK_ID_FRWR, &chunk_info_fw)) {
			wh_printf("Not found fw chunk !\n");
			goto exit_burn;
		}
		if (!pdev->func_wh_get_chunk_info(pdev->wif_access.wif_handle, CHUNK_ID_CNFG, &chunk_info_cfg)) {
			wh_printf("Not found cfg chunk !\n");
			goto exit_burn;
		}
		if (!chunk_info_fw.chuckInfo.versionNumber) {
			wh_printf("Wif fw version is null!\n");
			goto exit_burn;
		}
		
		err = fw_version_check(pparam, pinfo, &chunk_info_fw);
		/* fw is not matching */
		if (err == 0)	
			goto exit_burn;
		
	}


	if (pdev->pparam->argus & OPTION_NO_FORCE) {
		printf("Version checking ....\n");
		
		/* fw is most updated */
		if (err == 2)	{
			printf("The firmware in controller is most updated!\n");
			is_fw_update = 0; 
		}

		err = config_id_check(pparam, pinfo, &chunk_info_cfg);

		if (err == 2)	{
			printf("The cfg in controller is most updated!\n");
			is_cfg_update = 0; 
		}
	} 
	
	if (is_cfg_update && chunk_info_cfg.chuckInfo.targetStartAddr) {
		UINT32 tempStartAddr = chunk_info_cfg.chuckInfo.targetStartAddr;

		/*
		 *
		 * it will program a backup parameters to param_clone_address in general
		 * just specify OPTION_NO_RPARAM if don't want this backup
		 */
		if (!(pdev->pparam->argus & OPTION_NO_RPARAM)) {
			if (pdev->board_info.dev_type & FW_WDT8755) {
				if (pinfo->dev_info.w8755_dev_info.boot_partition == W8755_BP_SECONDARY) {
					chunk_info_cfg.chuckInfo.targetStartAddr = pdev->board_info.sec_header.w8755_sec_header.secondary_param_clone_addr;
					err = program_one_chunk(pdev, "r_config", CHUNK_ID_CNFD, OPTION_4K_VERIFY, &chunk_info_cfg);
				}else{
					chunk_info_cfg.chuckInfo.targetStartAddr = pdev->board_info.sec_header.w8755_sec_header.param_clone_addr;
					err = program_one_chunk(pdev, "r_config", CHUNK_ID_CNFG, OPTION_4K_VERIFY, &chunk_info_cfg);
				}

				if (!err)
					goto exit_burn;
			}
		}

		chunk_info_cfg.chuckInfo.targetStartAddr = tempStartAddr;

		/* fw should be reprogramed when parameters have been programmed */
		chunk_info_cfg.chuckInfo.temp = chunk_info_fw.chuckInfo.targetStartAddr;
		err = program_one_chunk(pdev, "config", CHUNK_ID_CNFG, OPTION_4K_VERIFY | OPTION_ERASE_TEMP,
								&chunk_info_cfg);

		if (!err)
			goto exit_burn;
	
		if (!program_one_chunk(pdev, "ext bin", CHUNK_ID_EXTB, OPTION_4K_VERIFY, NULL))
			goto exit_burn;

		if (!err)
			goto exit_burn;

		is_fw_update = 1;
	}

	if (pdev->board_info.dev_type & FW_WDT8755) {
		if (chunk_info_fw.chuckInfo.targetStartAddr == 0) {
			printf("Address can not be %d \n", chunk_info_fw.chuckInfo.targetStartAddr);
			goto exit_burn;
		}			
	} else if (pdev->board_info.dev_type & FW_WDT8760_2) {
		if (chunk_info_fw.chuckInfo.targetStartAddr >= 0x60000) {
			printf("Address can not be %d \n", chunk_info_fw.chuckInfo.targetStartAddr);
			goto exit_burn;
		}			
	}

	if (is_fw_update && (chunk_info_fw.chuckInfo.versionNumber)) {
		err = program_one_chunk(pdev, "firmware", CHUNK_ID_FRWR, OPTION_4K_VERIFY, &chunk_info_fw);

		if (!err)
			goto exit_burn;
	}
	
	if (is_fw_update || is_cfg_update) {
		pdev->funcs_device_private.p_wh_send_commands(pdev, WH_CMD_RESET, 0);
		printf("Reset device ... \n");

		wh_sleep(2000);
	}

exit_burn:	
	close_device(pdev);	

	if (!close_wif(pdev))
		return 0;
	
	return err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
xorl %ebp, %ebp
orb %al, %cl
jne 0x6dba
movq %rdi, %rbx
leaq 0x188(%rdi), %r14
movzwl (%r14), %eax
testw $0x5001, %ax            # imm = 0x5001
jne 0x6dba
movq %rsi, %r15
movq %rbx, %rdi
callq 0x692c
testl %eax, %eax
je 0x6d96
testb $0x2, 0x1(%r14)
jne 0x6dcb
cmpl $0x0, 0x4(%rbx)
je 0x6de5
leaq 0x4930(%rip), %rdi      # 0xb6c4
jmp 0x6d9d
leaq 0x487f(%rip), %rdi      # 0xb61c
callq 0x2270
xorl %ebp, %ebp
movq %rbx, %rdi
callq *0x2a0(%rbx)
movq %rbx, %rdi
callq 0x68fe
testl %eax, %eax
cmovel %eax, %ebp
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%r15), %rsi
movq %rbx, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9769
movq 0x10(%r15), %rsi
movq %rbx, %rdi
callq 0x688f
testl %eax, %eax
je 0x6e32
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movaps %xmm0, (%rsp)
andq $0x0, 0x50(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
testb $0x20, (%r14)
jne 0x6e3e
movq 0x158(%rbx), %rdi
pushq $0x1
popq %rsi
movq %rsp, %rdx
callq *0x318(%rbx)
jmp 0x6e99
leaq 0x47fe(%rip), %rdi      # 0xb637
jmp 0x6d9d
cmpl $0x2, 0x21c(%rbx)
movq 0x318(%rbx), %rax
movq 0x158(%rbx), %rdi
jne 0x6e91
movq %rsp, %rdx
movl $0x200, %esi            # imm = 0x200
callq *%rax
testl %eax, %eax
je 0x6f05
movq 0x158(%rbx), %rdi
leaq 0x30(%rsp), %rdx
movl $0x400, %esi            # imm = 0x400
callq *0x318(%rbx)
testl %eax, %eax
je 0x6f30
xorl %ebp, %ebp
cmpl $0x0, 0xc(%rsp)
jne 0x6ed5
jmp 0x6efc
pushq $0x1
popq %rsi
movq %rsp, %rdx
callq *%rax
testl %eax, %eax
je 0x6eea
movq 0x158(%rbx), %rdi
pushq $0x2
popq %rsi
leaq 0x30(%rsp), %rdx
callq *0x318(%rbx)
testl %eax, %eax
je 0x6ef3
cmpl $0x0, 0xc(%rsp)
je 0x6efc
movq %rsp, %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x6b44
testl %eax, %eax
je 0x6da2
movl %eax, %ebp
movq 0x280(%rbx), %rax
testb $0x4, (%rax)
jne 0x6f0e
pushq $0x1
popq %r12
jmp 0x6f77
leaq 0x4192(%rip), %rdi      # 0xb083
jmp 0x6f37
leaq 0x419f(%rip), %rdi      # 0xb099
jmp 0x6f37
leaq 0x4167(%rip), %rdi      # 0xb06a
jmp 0x6f37
leaq 0x4129(%rip), %rdi      # 0xb035
jmp 0x6f37
leaq 0x4734(%rip), %rdi      # 0xb649
callq 0x2270
cmpl $0x2, %ebp
jne 0x6f45
leaq 0x4739(%rip), %rdi      # 0xb65f
callq 0x2270
xorl %r12d, %r12d
jmp 0x6f49
leaq 0x4118(%rip), %rdi      # 0xb04f
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x24bf
jmp 0x6da4
pushq $0x1
popq %r12
leaq 0x30(%rsp), %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x6c1c
cmpl $0x2, %eax
jne 0x6f75
pushq $0x2
popq %rbp
leaq 0x4723(%rip), %rdi      # 0xb68b
callq 0x2270
xorl %r15d, %r15d
jmp 0x7036
movl %eax, %ebp
movl 0x30(%rsp), %r13d
movb $0x1, %r15b
testl %r13d, %r13d
je 0x7036
movq 0x280(%rbx), %rax
testb $0x20, (%rax)
jne 0x6fdf
testb $0x20, (%r14)
je 0x6fdf
leaq 0x24c(%rbx), %rax
leaq 0x258(%rbx), %rsi
cmpl $0x2, 0x21c(%rbx)
movl $0x400, %edx            # imm = 0x400
pushq $0x2
popq %rcx
cmovnel %ecx, %edx
cmoveq %rsi, %rax
movl (%rax), %eax
leaq 0x30(%rsp), %r8
movl %eax, (%r8)
leaq 0x40e1(%rip), %rsi      # 0xb0b0
movq %rbx, %rdi
callq 0x6c71
testl %eax, %eax
je 0x6da2
leaq 0x30(%rsp), %r8
movl %r13d, (%r8)
movl (%rsp), %eax
movl %eax, 0x14(%r8)
leaq 0x40bd(%rip), %rsi      # 0xb0b2
pushq $0x2
popq %rdx
movq %rbx, %rdi
movl $0x402, %ecx            # imm = 0x402
callq 0x6c71
testl %eax, %eax
je 0x6da2
leaq 0x40a5(%rip), %rsi      # 0xb0b9
pushq $0x2
popq %rcx
movq %rbx, %rdi
movl $0x80, %edx
xorl %r8d, %r8d
callq 0x6c71
pushq $0x1
popq %r12
movl %r12d, %ebp
testl %eax, %eax
je 0x6da4
movl (%r14), %eax
testb $0x20, %al
jne 0x7060
testw $0x2800, %ax            # imm = 0x2800
setne %al
movl (%rsp), %esi
cmpl $0x60000, %esi          # imm = 0x60000
setae %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x7066
leaq 0x4063(%rip), %rdi      # 0xb0c1
jmp 0x70d9
cmpl $0x0, (%rsp)
je 0x70d0
testl %r12d, %r12d
je 0x7095
cmpl $0x0, 0xc(%rsp)
je 0x7095
leaq 0x315a(%rip), %rsi      # 0xa1d3
pushq $0x1
popq %rbp
pushq $0x2
popq %rcx
movq %rsp, %r8
movq %rbx, %rdi
movl %ebp, %edx
callq 0x6c71
testl %eax, %eax
jne 0x70a7
jmp 0x6da2
testl %r12d, %r12d
sete %al
notb %r15b
testb %al, %r15b
jne 0x6da4
pushq $0x1
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq *0x2d8(%rbx)
leaq 0x45f6(%rip), %rdi      # 0xb6b2
callq 0x2270
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x2555
jmp 0x6da4
leaq 0x3fea(%rip), %rdi      # 0xb0c1
xorl %esi, %esi
xorl %eax, %eax
callq 0x2050
jmp 0x6da4