// File: chenhn123[P]wdt_util_src/wdt_ct/w8760_funcs.cpp
// Function: wh_w8760_dev_program_chunk_verify(WdtDevice*, ChunkInfoEx*, int)
int wh_w8760_dev_program_chunk_verify(WDT_DEV* pdev, CHUNK_INFO_EX* pInputChunk, int option)
{
	int retval = 0;

	CHUNK_INFO_EX	*pChunk = pInputChunk;
	FUNC_PTR_STRUCT_DEV_OPERATION funcs;
	UINT32	calc_checksum, read_checksum;

	memset(&funcs, 0, sizeof(FUNC_PTR_STRUCT_DEV_OPERATION));
	
	retval = wh_w8760_dev_set_n_check_device_mode(pdev, W8760_MODE_FLASH_PROGRAM, 0, 0);
	if (!retval)
		return retval;
		
	retval = wh_w8760_dev_send_commands(pdev, WH_CMD_FLASH_UNLOCK, 0);
	if (!retval)
		return retval;
	
	if(pdev->board_info.dev_type == FW_WDT8762)
	{
		if(pChunk->chuckInfo.targetStartAddr == 0x5d000)
		{
			printf("Erase Sector 0x0000 \n");
			retval = wh_w8760_dev_flash_erase(pdev, 0x0000, 0x1000);
			if (!retval)
				return retval;
		}
	}

	retval = wh_w8760_dev_flash_erase(pdev, pChunk->chuckInfo.targetStartAddr, pChunk->chuckInfo.length);
	if (!retval)
		return retval;
		
	printf("Chunk program start...\n");
	retval = wh_w8760_dev_flash_write_data(pdev, pChunk->pData, pChunk->chuckInfo.targetStartAddr, pChunk->chuckInfo.length);
	if (!retval)
		return retval;
		
	printf("Chunk program end ...\n");

	calc_checksum = misr_for_bytes(0, (BYTE*) pChunk->pData, 0,  pChunk->chuckInfo.length);
	retval = wh_w8760_dev_flash_get_checksum(pdev, &read_checksum, pChunk->chuckInfo.targetStartAddr, pChunk->chuckInfo.length);
	
	if (read_checksum != calc_checksum) {
		printf("checksum failed : %d <> %d\n", read_checksum, calc_checksum);
		retval = 0;
	}
	
	wh_w8760_dev_send_commands(pdev, WH_CMD_FLASH_LOCK, 0);
	if (!retval)
		return retval;
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
movl $0x38, %edx
callq 0x2110
movq -0x10(%rbp), %rdi
movl $0x96, %esi
xorl %ecx, %ecx
movl %ecx, %edx
callq 0x5060
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x6378
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x64e9
movq -0x10(%rbp), %rdi
movl $0x11, %esi
xorl %edx, %edx
callq 0x5930
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x639c
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x64e9
movq -0x10(%rbp), %rax
cmpl $0x2000, 0x188(%rax)    # imm = 0x2000
jne 0x63ee
movq -0x28(%rbp), %rax
cmpl $0x5d000, (%rax)        # imm = 0x5D000
jne 0x63ec
leaq 0x9674(%rip), %rdi      # 0xfa33
movb $0x0, %al
callq 0x2050
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x5b60
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x63ea
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x64e9
jmp 0x63ec
jmp 0x63ee
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movl (%rax), %esi
movq -0x28(%rbp), %rax
movl 0x4(%rax), %edx
callq 0x5b60
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x6418
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x64e9
leaq 0x962a(%rip), %rdi      # 0xfa49
movb $0x0, %al
callq 0x2050
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x28(%rbp), %rax
movl (%rax), %edx
movq -0x28(%rbp), %rax
movl 0x4(%rax), %ecx
callq 0x5bb0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x6458
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x64e9
leaq 0x9602(%rip), %rdi      # 0xfa61
movb $0x0, %al
callq 0x2050
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x28(%rbp), %rax
movl 0x4(%rax), %ecx
xorl %edx, %edx
movl %edx, %edi
callq 0xb780
movzwl %ax, %eax
movl %eax, -0x64(%rbp)
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movl (%rax), %edx
movq -0x28(%rbp), %rax
movl 0x4(%rax), %ecx
leaq -0x68(%rbp), %rsi
callq 0x5c30
movl %eax, -0x20(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x64(%rbp), %eax
je 0x64c4
movl -0x68(%rbp), %esi
movl -0x64(%rbp), %edx
leaq 0x95c2(%rip), %rdi      # 0xfa78
movb $0x0, %al
callq 0x2050
movl $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movl $0x10, %esi
xorl %edx, %edx
callq 0x5930
cmpl $0x0, -0x20(%rbp)
jne 0x64e2
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x64e9
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)