// File: /workspace/github_cpp_makefile_star2_O1/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 0x77f5
movq %rdi, %rbx
leaq 0x188(%rdi), %r14
movzwl (%r14), %eax
testl $0x5001, %eax           # imm = 0x5001
jne 0x77f5
movq %rsi, %r15
movq %rbx, %rdi
callq 0x730f
testl %eax, %eax
je 0x77b8
testb $0x2, 0x1(%r14)
jne 0x7806
cmpl $0x0, 0x4(%rbx)
je 0x7820
leaq 0x4f0e(%rip), %rdi      # 0xc6c4
jmp 0x77bf
leaq 0x4e5d(%rip), %rdi      # 0xc61c
callq 0x2270
xorl %ebp, %ebp
testq %rbx, %rbx
je 0x77d4
movq %rbx, %rdi
callq *0x2a0(%rbx)
movq 0x158(%rbx), %rdi
callq *0x310(%rbx)
testl %eax, %eax
jne 0x77f5
leaq 0x468b(%rip), %rdi      # 0xbe77
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x2506
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 0xa47b
movq 0x10(%r15), %rsi
movq %rbx, %rdi
callq 0x726d
testl %eax, %eax
je 0x786f
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, (%rsp)
xorl %eax, %eax
movq %rax, 0x20(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
movq %rax, 0x50(%rsp)
testb $0x20, (%r14)
jne 0x787b
movq 0x158(%rbx), %rdi
movq %rsp, %rdx
movl $0x1, %esi
callq *0x318(%rbx)
jmp 0x78d8
leaq 0x4dc1(%rip), %rdi      # 0xc637
jmp 0x77bf
cmpl $0x2, 0x21c(%rbx)
movq 0x318(%rbx), %rax
movq 0x158(%rbx), %rdi
movq %rsp, %rdx
jne 0x78d1
movl $0x200, %esi            # imm = 0x200
callq *%rax
testl %eax, %eax
je 0x79cd
movq 0x158(%rbx), %rdi
leaq 0x30(%rsp), %rdx
movl $0x400, %esi            # imm = 0x400
callq *0x318(%rbx)
testl %eax, %eax
je 0x79d6
xorl %ebp, %ebp
cmpl $0x0, 0xc(%rsp)
jne 0x7922
jmp 0x79c4
movl $0x1, %esi
callq *%rax
testl %eax, %eax
je 0x79b2
movq 0x158(%rbx), %rdi
leaq 0x30(%rsp), %rdx
movl $0x2, %esi
callq *0x318(%rbx)
testl %eax, %eax
je 0x79bb
cmpl $0x0, 0xc(%rsp)
je 0x79c4
movq %rsp, %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x7565
testl %eax, %eax
je 0x77c4
movl %eax, %ebp
movq 0x280(%rbx), %rax
movb $0x1, %r12b
movl $0x1, %r13d
testb $0x4, (%rax)
je 0x79f0
leaq 0x4d07(%rip), %rdi      # 0xc649
callq 0x2270
movl $0x1, %r13d
cmpl $0x2, %ebp
jne 0x7961
leaq 0x4d06(%rip), %rdi      # 0xc65f
callq 0x2270
xorl %r13d, %r13d
movl 0x40(%rsp), %r15d
shll $0x1e, %r15d
sarl $0x1f, %r15d
andl 0x3c(%rsp), %r15d
movzwl 0x1a8(%rbx), %edx
leaq 0x463e(%rip), %rdi      # 0xbfbf
movl %r15d, %esi
xorl %eax, %eax
callq 0x2506
movzwl 0x1a8(%rbx), %eax
xorl %ebp, %ebp
cmpl %eax, %r15d
sete %al
jne 0x79eb
leaq 0x4ce8(%rip), %rdi      # 0xc68b
callq 0x2270
movl $0x2, %ebp
xorl %r12d, %r12d
jmp 0x79f0
leaq 0x46ca(%rip), %rdi      # 0xc083
jmp 0x79dd
leaq 0x46d7(%rip), %rdi      # 0xc099
jmp 0x79dd
leaq 0x469f(%rip), %rdi      # 0xc06a
jmp 0x79dd
leaq 0x4661(%rip), %rdi      # 0xc035
jmp 0x79dd
leaq 0x4672(%rip), %rdi      # 0xc04f
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x2506
jmp 0x77c6
movb %al, %bpl
incl %ebp
movl 0x30(%rsp), %r15d
testl %r15d, %r15d
setne %al
andb %r12b, %al
cmpb $0x1, %al
jne 0x7ac1
movq 0x280(%rbx), %rax
testb $0x20, (%rax)
jne 0x7a60
testb $0x20, (%r14)
je 0x7a60
leaq 0x24c(%rbx), %rax
leaq 0x258(%rbx), %rcx
cmpl $0x2, 0x21c(%rbx)
movl $0x400, %edx            # imm = 0x400
movl $0x2, %esi
cmovnel %esi, %edx
cmoveq %rcx, %rax
movl (%rax), %eax
leaq 0x30(%rsp), %r8
movl %eax, (%r8)
leaq 0x4661(%rip), %rsi      # 0xc0b0
movq %rbx, %rdi
movl $0x2, %ecx
callq 0x7692
testl %eax, %eax
je 0x7ab5
leaq 0x30(%rsp), %r8
movl %r15d, (%r8)
movl (%rsp), %eax
movl %eax, 0x14(%r8)
leaq 0x463c(%rip), %rsi      # 0xc0b2
movq %rbx, %rdi
movl $0x2, %edx
movl $0x402, %ecx            # imm = 0x402
callq 0x7692
testl %eax, %eax
je 0x7ab5
leaq 0x4626(%rip), %rsi      # 0xc0b9
movq %rbx, %rdi
movl $0x80, %edx
movl $0x2, %ecx
xorl %r8d, %r8d
callq 0x7692
testl %eax, %eax
movl $0x1, %ebp
cmovnel %ebp, %r13d
jmp 0x7ab9
xorl %ebp, %ebp
xorl %eax, %eax
testb %al, %al
je 0x77c6
movl (%r14), %eax
testb $0x20, %al
jne 0x7aef
testl $0x2800, %eax           # imm = 0x2800
setne %al
movl (%rsp), %esi
cmpl $0x60000, %esi          # imm = 0x60000
setae %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x7af5
leaq 0x45d7(%rip), %rdi      # 0xc0c1
jmp 0x7b70
cmpl $0x0, (%rsp)
je 0x7b67
testl %r13d, %r13d
je 0x7b2a
cmpl $0x0, 0xc(%rsp)
je 0x7b2a
leaq 0x36cb(%rip), %rsi      # 0xb1d3
movl $0x1, %ebp
movq %rsp, %r8
movq %rbx, %rdi
movl $0x1, %edx
movl $0x2, %ecx
callq 0x7692
testl %eax, %eax
je 0x77c4
testl %r13d, %r13d
sete %al
notb %r12b
testb %al, %r12b
jne 0x77c6
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq *0x2d8(%rbx)
leaq 0x4b5f(%rip), %rdi      # 0xc6b2
callq 0x2270
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x259c
jmp 0x77c6
leaq 0x4553(%rip), %rdi      # 0xc0c1
xorl %esi, %esi
xorl %eax, %eax
callq 0x2050
jmp 0x77c6