// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
addq $0x188, %rax            # imm = 0x188
movq %rax, -0x20(%rbp)
movl $0x1, -0x74(%rbp)
movl $0x1, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x9f64
cmpq $0x0, -0x18(%rbp)
jne 0x9f70
movl $0x0, -0x4(%rbp)
jmp 0xa4f7
movq -0x20(%rbp), %rax
movl (%rax), %eax
andl $0x5001, %eax           # imm = 0x5001
cmpl $0x0, %eax
je 0x9f8c
movl $0x0, -0x4(%rbp)
jmp 0xa4f7
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
xorl %edx, %edx
callq 0x9850
cmpl $0x0, %eax
jne 0x9fb3
leaq 0x6156(%rip), %rdi      # 0x100fd
movb $0x0, %al
callq 0x2050
jmp 0xa4d1
movq -0x10(%rbp), %rax
movl 0x188(%rax), %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
je 0x9fe0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0xe400
movl %eax, -0x4(%rbp)
jmp 0xa4f7
movq -0x10(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0x9ffd
leaq 0x6128(%rip), %rdi      # 0x10119
movb $0x0, %al
callq 0x2050
jmp 0xa4d1
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x9750
cmpl $0x0, %eax
jne 0xa026
leaq 0x611f(%rip), %rdi      # 0x10139
movb $0x0, %al
callq 0x2050
jmp 0xa4d1
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
movl $0x28, %edx
callq 0x2110
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
movl $0x28, %edx
callq 0x2110
movq -0x10(%rbp), %rax
movl 0x188(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0xa1ae
movq -0x20(%rbp), %rax
cmpl $0x2, 0x94(%rax)
jne 0xa0fd
movq -0x10(%rbp), %rax
movq 0x318(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x158(%rcx), %rdi
movl $0x200, %esi            # imm = 0x200
leaq -0x48(%rbp), %rdx
callq *%rax
cmpl $0x0, %eax
jne 0xa0a6
leaq 0x60b2(%rip), %rdi      # 0x1014c
movb $0x0, %al
callq 0x24e0
jmp 0xa4d1
movq -0x10(%rbp), %rax
movq 0x318(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x158(%rcx), %rdi
movl $0x400, %esi            # imm = 0x400
leaq -0x70(%rbp), %rdx
callq *%rax
cmpl $0x0, %eax
jne 0xa0df
leaq 0x6093(%rip), %rdi      # 0x10166
movb $0x0, %al
callq 0x24e0
jmp 0xa4d1
cmpl $0x0, -0x3c(%rbp)
jne 0xa0f8
leaq 0x6095(%rip), %rdi      # 0x10181
movb $0x0, %al
callq 0x24e0
jmp 0xa4d1
jmp 0xa1a9
movq -0x10(%rbp), %rax
movq 0x318(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x158(%rcx), %rdi
movl $0x1, %esi
leaq -0x48(%rbp), %rdx
callq *%rax
cmpl $0x0, %eax
jne 0xa136
leaq 0x6070(%rip), %rdi      # 0x1019a
movb $0x0, %al
callq 0x24e0
jmp 0xa4d1
movq -0x10(%rbp), %rax
movq 0x318(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x158(%rcx), %rdi
movl $0x2, %esi
leaq -0x70(%rbp), %rdx
callq *%rax
cmpl $0x0, %eax
jne 0xa16f
leaq 0x604d(%rip), %rdi      # 0x101b0
movb $0x0, %al
callq 0x24e0
jmp 0xa4d1
cmpl $0x0, -0x3c(%rbp)
jne 0xa188
leaq 0x6005(%rip), %rdi      # 0x10181
movb $0x0, %al
callq 0x24e0
jmp 0xa4d1
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
leaq -0x48(%rbp), %rdx
callq 0x9bd0
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
jne 0xa1a7
jmp 0xa4d1
jmp 0xa1a9
jmp 0xa25a
movq -0x10(%rbp), %rax
movq 0x318(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x158(%rcx), %rdi
movl $0x1, %esi
leaq -0x48(%rbp), %rdx
callq *%rax
cmpl $0x0, %eax
jne 0xa1e7
leaq 0x5fbf(%rip), %rdi      # 0x1019a
movb $0x0, %al
callq 0x24e0
jmp 0xa4d1
movq -0x10(%rbp), %rax
movq 0x318(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x158(%rcx), %rdi
movl $0x2, %esi
leaq -0x70(%rbp), %rdx
callq *%rax
cmpl $0x0, %eax
jne 0xa220
leaq 0x5f9c(%rip), %rdi      # 0x101b0
movb $0x0, %al
callq 0x24e0
jmp 0xa4d1
cmpl $0x0, -0x3c(%rbp)
jne 0xa239
leaq 0x5f54(%rip), %rdi      # 0x10181
movb $0x0, %al
callq 0x24e0
jmp 0xa4d1
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
leaq -0x48(%rbp), %rdx
callq 0x9bd0
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
jne 0xa258
jmp 0xa4d1
jmp 0xa25a
movq -0x10(%rbp), %rax
movq 0x280(%rax), %rax
movl (%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0xa2c9
leaq 0x5f51(%rip), %rdi      # 0x101c7
movb $0x0, %al
callq 0x2050
cmpl $0x2, -0x7c(%rbp)
jne 0xa298
leaq 0x5f54(%rip), %rdi      # 0x101de
movb $0x0, %al
callq 0x2050
movl $0x0, -0x74(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
leaq -0x70(%rbp), %rdx
callq 0x9d50
movl %eax, -0x7c(%rbp)
cmpl $0x2, -0x7c(%rbp)
jne 0xa2c7
leaq 0x5f52(%rip), %rdi      # 0x1020b
movb $0x0, %al
callq 0x2050
movl $0x0, -0x78(%rbp)
jmp 0xa2c9
cmpl $0x0, -0x78(%rbp)
je 0xa3f8
cmpl $0x0, -0x70(%rbp)
je 0xa3f8
movl -0x70(%rbp), %eax
movl %eax, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq 0x280(%rax), %rax
movl (%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
jne 0xa385
movq -0x10(%rbp), %rax
movl 0x188(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0xa383
movq -0x20(%rbp), %rax
cmpl $0x2, 0x94(%rax)
jne 0xa34b
movq -0x10(%rbp), %rax
movl 0x258(%rax), %eax
movl %eax, -0x70(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x5f00(%rip), %rsi      # 0x10233
movl $0x400, %edx            # imm = 0x400
movl $0x2, %ecx
leaq -0x70(%rbp), %r8
callq 0x9df0
movl %eax, -0x7c(%rbp)
jmp 0xa376
movq -0x10(%rbp), %rax
movl 0x24c(%rax), %eax
movl %eax, -0x70(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x5ed0(%rip), %rsi      # 0x10233
movl $0x2, %ecx
leaq -0x70(%rbp), %r8
movl %ecx, %edx
callq 0x9df0
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
jne 0xa381
jmp 0xa4d1
jmp 0xa383
jmp 0xa385
movl -0x80(%rbp), %eax
movl %eax, -0x70(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x5e99(%rip), %rsi      # 0x10235
movl $0x2, %edx
movl $0x402, %ecx            # imm = 0x402
leaq -0x70(%rbp), %r8
callq 0x9df0
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
jne 0xa3bd
jmp 0xa4d1
movq -0x10(%rbp), %rdi
leaq 0x5e74(%rip), %rsi      # 0x1023c
movl $0x80, %edx
movl $0x2, %ecx
xorl %eax, %eax
movl %eax, %r8d
callq 0x9df0
cmpl $0x0, %eax
jne 0xa3e6
jmp 0xa4d1
cmpl $0x0, -0x7c(%rbp)
jne 0xa3f1
jmp 0xa4d1
movl $0x1, -0x74(%rbp)
movq -0x10(%rbp), %rax
movl 0x188(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0xa428
cmpl $0x0, -0x48(%rbp)
jne 0xa426
movl -0x48(%rbp), %esi
leaq 0x5e2a(%rip), %rdi      # 0x10244
movb $0x0, %al
callq 0x2050
jmp 0xa4d1
jmp 0xa45c
movq -0x10(%rbp), %rax
movl 0x188(%rax), %eax
andl $0x2800, %eax           # imm = 0x2800
cmpl $0x0, %eax
je 0xa45a
cmpl $0x60000, -0x48(%rbp)   # imm = 0x60000
jb 0xa458
movl -0x48(%rbp), %esi
leaq 0x5df5(%rip), %rdi      # 0x10244
movb $0x0, %al
callq 0x2050
jmp 0xa4d1
jmp 0xa45a
jmp 0xa45c
cmpl $0x0, -0x74(%rbp)
je 0xa493
cmpl $0x0, -0x3c(%rbp)
je 0xa493
movq -0x10(%rbp), %rdi
leaq 0x5de9(%rip), %rsi      # 0x1025c
movl $0x1, %edx
movl $0x2, %ecx
leaq -0x48(%rbp), %r8
callq 0x9df0
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
jne 0xa491
jmp 0xa4d1
jmp 0xa493
cmpl $0x0, -0x74(%rbp)
jne 0xa49f
cmpl $0x0, -0x78(%rbp)
je 0xa4cf
movq -0x10(%rbp), %rax
movq 0x2d8(%rax), %rax
movq -0x10(%rbp), %rdi
movl $0x1, %esi
xorl %edx, %edx
callq *%rax
leaq 0x5da7(%rip), %rdi      # 0x10265
movb $0x0, %al
callq 0x2050
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x2590
jmp 0xa4d1
movq -0x10(%rbp), %rdi
callq 0x9ba0
movq -0x10(%rbp), %rdi
callq 0x9800
cmpl $0x0, %eax
jne 0xa4f1
movl $0x0, -0x4(%rbp)
jmp 0xa4f7
movl -0x7c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)