// File: /workspace/github_cpp_makefile_star2_O3/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 0x773e
movq %rdi, %rbx
leaq 0x188(%rdi), %r14
movzwl (%r14), %eax
testl $0x5001, %eax           # imm = 0x5001
jne 0x773e
movq %rsi, %r15
movq %rbx, %rdi
callq 0x725d
testl %eax, %eax
je 0x7706
testb $0x2, 0x1(%r14)
jne 0x774f
cmpl $0x0, 0x4(%rbx)
je 0x7769
leaq 0x4fc0(%rip), %rdi      # 0xc6c4
jmp 0x770d
leaq 0x4f0f(%rip), %rdi      # 0xc61c
callq 0x2270
xorl %ebp, %ebp
movq %rbx, %rdi
callq *0x2a0(%rbx)
movq 0x158(%rbx), %rdi
callq *0x310(%rbx)
testl %eax, %eax
jne 0x773e
leaq 0x4742(%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 0xa3bd
movq 0x10(%r15), %rsi
movq %rbx, %rdi
callq 0x71bb
testl %eax, %eax
je 0x77b8
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 0x77c4
movq 0x158(%rbx), %rdi
movq %rsp, %rdx
movl $0x1, %esi
callq *0x318(%rbx)
jmp 0x781f
leaq 0x4e78(%rip), %rdi      # 0xc637
jmp 0x770d
cmpl $0x2, 0x21c(%rbx)
movq 0x318(%rbx), %rax
movq 0x158(%rbx), %rdi
movq %rsp, %rdx
jne 0x7818
movl $0x200, %esi            # imm = 0x200
callq *%rax
testl %eax, %eax
je 0x789c
movq 0x158(%rbx), %rdi
leaq 0x30(%rsp), %rdx
movl $0x400, %esi            # imm = 0x400
callq *0x318(%rbx)
testl %eax, %eax
je 0x7a5f
xorl %r15d, %r15d
cmpl $0x0, 0xc(%rsp)
jne 0x785e
jmp 0x7890
movl $0x1, %esi
callq *%rax
testl %eax, %eax
je 0x7878
movq 0x158(%rbx), %rdi
leaq 0x30(%rsp), %rdx
movl $0x2, %esi
callq *0x318(%rbx)
testl %eax, %eax
je 0x7884
cmpl $0x0, 0xc(%rsp)
je 0x7890
movq %rsp, %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x74b3
testl %eax, %eax
je 0x7712
movl %eax, %r15d
movq 0x280(%rbx), %rax
movl $0x1, %r12d
testb $0x4, (%rax)
jne 0x78a8
movl %r15d, %ebp
jmp 0x7923
leaq 0x4804(%rip), %rdi      # 0xc083
jmp 0x7a66
leaq 0x480e(%rip), %rdi      # 0xc099
jmp 0x7a66
leaq 0x47d3(%rip), %rdi      # 0xc06a
jmp 0x7a66
leaq 0x4792(%rip), %rdi      # 0xc035
jmp 0x7a66
leaq 0x4d9a(%rip), %rdi      # 0xc649
callq 0x2270
movl $0x1, %ebp
movl $0x1, %r12d
cmpl $0x2, %r15d
jne 0x78d4
leaq 0x4d93(%rip), %rdi      # 0xc65f
callq 0x2270
xorl %r12d, %r12d
movl 0x40(%rsp), %r15d
shll $0x1e, %r15d
sarl $0x1f, %r15d
andl 0x3c(%rsp), %r15d
movzwl 0x1a8(%rbx), %edx
leaq 0x46cb(%rip), %rdi      # 0xbfbf
movl %r15d, %esi
xorl %eax, %eax
callq 0x2506
movzwl 0x1a8(%rbx), %eax
cmpl %eax, %r15d
jne 0x7923
leaq 0x4d7a(%rip), %rdi      # 0xc68b
callq 0x2270
movl $0x2, %ebp
xorl %r15d, %r15d
jmp 0x79f1
movl 0x30(%rsp), %r13d
movb $0x1, %r15b
testl %r13d, %r13d
je 0x79f1
movq 0x280(%rbx), %rax
testb $0x20, (%rax)
jne 0x7992
testb $0x20, (%r14)
je 0x7992
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 0x4733(%rip), %rsi      # 0xc0b0
movq %rbx, %rdi
movl $0x2, %ecx
callq 0x75e0
testl %eax, %eax
je 0x7712
leaq 0x30(%rsp), %r8
movl %r13d, (%r8)
movl (%rsp), %eax
movl %eax, 0x14(%r8)
leaq 0x470a(%rip), %rsi      # 0xc0b2
movq %rbx, %rdi
movl $0x2, %edx
movl $0x402, %ecx            # imm = 0x402
callq 0x75e0
testl %eax, %eax
je 0x7712
leaq 0x46f0(%rip), %rsi      # 0xc0b9
movq %rbx, %rdi
movl $0x80, %edx
movl $0x2, %ecx
xorl %r8d, %r8d
callq 0x75e0
movl $0x1, %r12d
movl $0x1, %ebp
testl %eax, %eax
je 0x7714
movl (%r14), %eax
testb $0x20, %al
jne 0x7a1f
testl $0x2800, %eax           # imm = 0x2800
setne %al
movl (%rsp), %esi
cmpl $0x60000, %esi          # imm = 0x60000
setae %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x7a29
leaq 0x46a7(%rip), %rdi      # 0xc0c1
jmp 0x7aba
cmpl $0x0, (%rsp)
je 0x7ab1
testl %r12d, %r12d
je 0x7a74
cmpl $0x0, 0xc(%rsp)
je 0x7a74
leaq 0x3797(%rip), %rsi      # 0xb1d3
movl $0x1, %ebp
movq %rsp, %r8
movq %rbx, %rdi
movl $0x1, %edx
movl $0x2, %ecx
callq 0x75e0
testl %eax, %eax
jne 0x7a86
jmp 0x7712
leaq 0x45e9(%rip), %rdi      # 0xc04f
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x2506
jmp 0x7714
testl %r12d, %r12d
sete %al
notb %r15b
testb %al, %r15b
jne 0x7714
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq *0x2d8(%rbx)
leaq 0x4c15(%rip), %rdi      # 0xc6b2
callq 0x2270
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x259c
jmp 0x7714
leaq 0x4609(%rip), %rdi      # 0xc0c1
xorl %esi, %esi
xorl %eax, %eax
callq 0x2050
jmp 0x7714