// File: chenhn123[P]wdt_util_src/wdt_ct/wif2_handler.cpp
// Function: do_update_fw_by_wif2_chunk_fbin(WdtDevice*, WifFile2*, unsigned int)
int do_update_fw_by_wif2_chunk_fbin(WDT_DEV *pdev, WIF_FILE2 *pcur_wif, UINT32 chunk_four_cc)
{
	int ret = 1;
	if (!pcur_wif)
		return 0;

	if (!chunk_four_cc)
		return 0;

	WIF2_FlashBinary_Chunk *pchunk_info_ex;
	pchunk_info_ex = (WIF2_FlashBinary_Chunk *)malloc(sizeof(WIF2_FlashBinary_Chunk));
	if (!pchunk_info_ex)
		return 0;

	UINT32 chunk_start_pos = 0 + sizeof(ChunkHeader);
	WIF2_Chunk_Header *pchunk_data = NULL;

	while (chunk_start_pos < pcur_wif->data_len) {
		pchunk_data = (WIF2_Chunk_Header *)&pcur_wif->pdata[chunk_start_pos];

		/* we got it */
		if (pchunk_data->FourCC == chunk_four_cc) {
			pchunk_info_ex->Header.FourCC = pchunk_data->FourCC;
			pchunk_info_ex->Header.Size = pchunk_data->Size;

			UINT32 pdata_pos = chunk_start_pos + sizeof(WIF2_Chunk_Header);

			// Flash space to erase
			pchunk_info_ex->SpaceToErase.Address = get_unaligned_le32(&pcur_wif->pdata[pdata_pos]);
			pdata_pos = pdata_pos + sizeof(UINT32);
			pchunk_info_ex->SpaceToErase.Size = get_unaligned_le32(&pcur_wif->pdata[pdata_pos]);
			pdata_pos = pdata_pos + sizeof(UINT32);

			// printf("SpaceToEraseAddress:0x%x\n",pchunk_info_ex->SpaceToErase.Address);
			// printf("SpaceToEraseSize:0x%x\n", pchunk_info_ex->SpaceToErase.Size);

			// Flash image
			pchunk_info_ex->Binary.Address = get_unaligned_le32(&pcur_wif->pdata[pdata_pos]);
			pdata_pos = pdata_pos + sizeof(UINT32);
			pchunk_info_ex->Binary.Size = get_unaligned_le32(&pcur_wif->pdata[pdata_pos]);
			pdata_pos = pdata_pos + sizeof(UINT32);

			// printf("BinaryAddress:0x%x\n", pchunk_info_ex->Binary.Address);
			// printf("BinarySize:0x%x\n", pchunk_info_ex->Binary.Size);

			pchunk_info_ex->BinaryData = (BYTE *)malloc(pchunk_info_ex->Binary.Size);
			if (!pchunk_info_ex->BinaryData) {
				ret = 0;
				if (pchunk_info_ex->BinaryData)
					free(pchunk_info_ex->BinaryData);
				goto finish;
			}

			memcpy((BYTE *)pchunk_info_ex->BinaryData, (BYTE *)&pcur_wif->pdata[pdata_pos],
			       pchunk_info_ex->Binary.Size);

			ret = pdev->funcs_device_private.p_wh_send_commands(pdev, WH_CMD_FLASH_UNLOCK, 0);
			if (!ret) {
				if (pchunk_info_ex->BinaryData)
					free(pchunk_info_ex->BinaryData);
				printf("unlock fail");

				goto finish;
			}
			// address and size align to 0x100
			UINT32 protect_off_arg = (pchunk_info_ex->Binary.Address >> 8 << 16 & 0xffff0000) |
						 (pchunk_info_ex->Binary.Size >> 8 & 0x0000ffff);

			ret = pdev->funcs_device_private.p_wh_send_commands(pdev, WH_CMD_FLASH_PROTECTION_OFF,
									    protect_off_arg);
			if (!ret) {
				if (pchunk_info_ex->BinaryData)
					free(pchunk_info_ex->BinaryData);

				goto finish;
			}

			printf("Use 4k program\n");
			ret = pdev->funcs_device_private.p_wh_flash_erase(pdev, pchunk_info_ex->SpaceToErase.Address,
									  pchunk_info_ex->SpaceToErase.Size);
			if (!ret) {
				if (pchunk_info_ex->BinaryData)
					free(pchunk_info_ex->BinaryData);

				goto finish;
			}

			ret = pdev->funcs_device_private.p_wh_flash_write_data(pdev, pchunk_info_ex->BinaryData,
									       pchunk_info_ex->Binary.Address,
									       pchunk_info_ex->Binary.Size);
			if (!ret) {
				if (pchunk_info_ex->BinaryData)
					free(pchunk_info_ex->BinaryData);

				goto finish;
			}

			chunk_start_pos = chunk_start_pos + pchunk_data->Size + 8;

			if (pchunk_info_ex->BinaryData)
				free(pchunk_info_ex->BinaryData);

		} else {
			/* 8 is the header size */
			chunk_start_pos = chunk_start_pos + pchunk_data->Size + 8;
		}
	}
finish:
	pdev->funcs_device_private.p_wh_send_commands(pdev, WH_CMD_FLASH_PROTECTION_ON, 0);

	if (pchunk_info_ex)
		free(pchunk_info_ex);

	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl $0x1, -0x20(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0xd80d
movl $0x0, -0x4(%rbp)
jmp 0xdb60
cmpl $0x0, -0x1c(%rbp)
jne 0xd81f
movl $0x0, -0x4(%rbp)
jmp 0xdb60
movl $0x20, %edi
callq 0x21f0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xd840
movl $0x0, -0x4(%rbp)
jmp 0xdb60
movl $0xc, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
movl -0x2c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jae 0xdb30
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x2c(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movl (%rax), %eax
cmpl -0x1c(%rbp), %eax
jne 0xdb1b
movq -0x38(%rbp), %rax
movl (%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x38(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x2c(%rbp), %eax
addq $0x8, %rax
movl %eax, -0x3c(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movl -0x3c(%rbp), %eax
addq %rax, %rdi
callq 0x9170
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x3c(%rbp), %eax
addq $0x4, %rax
movl %eax, -0x3c(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movl -0x3c(%rbp), %eax
addq %rax, %rdi
callq 0x9170
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0xc(%rax)
movl -0x3c(%rbp), %eax
addq $0x4, %rax
movl %eax, -0x3c(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movl -0x3c(%rbp), %eax
addq %rax, %rdi
callq 0x9170
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x10(%rax)
movl -0x3c(%rbp), %eax
addq $0x4, %rax
movl %eax, -0x3c(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movl -0x3c(%rbp), %eax
addq %rax, %rdi
callq 0x9170
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x14(%rax)
movl -0x3c(%rbp), %eax
addq $0x4, %rax
movl %eax, -0x3c(%rbp)
movq -0x28(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, %edi
callq 0x21f0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0xd97f
movl $0x0, -0x20(%rbp)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0xd97a
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x21e0
jmp 0xdb32
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movl -0x3c(%rbp), %eax
addq %rax, %rsi
movq -0x28(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, %edx
callq 0x2150
movq -0x10(%rbp), %rax
movq 0x2d8(%rax), %rax
movq -0x10(%rbp), %rdi
movl $0x11, %esi
xorl %edx, %edx
callq *%rax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0xd9ee
movq -0x28(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0xd9db
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x21e0
leaq 0x3277(%rip), %rdi      # 0x10c59
movb $0x0, %al
callq 0x2050
jmp 0xdb32
movq -0x28(%rbp), %rax
movl 0x10(%rax), %eax
shrl $0x8, %eax
shll $0x10, %eax
andl $0xffff0000, %eax       # imm = 0xFFFF0000
movq -0x28(%rbp), %rcx
movl 0x14(%rcx), %ecx
shrl $0x8, %ecx
andl $0xffff, %ecx           # imm = 0xFFFF
orl %ecx, %eax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x2d8(%rax), %rax
movq -0x10(%rbp), %rdi
movl -0x40(%rbp), %edx
movl $0x17, %esi
callq *%rax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0xda54
movq -0x28(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0xda4f
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x21e0
jmp 0xdb32
leaq 0x320a(%rip), %rdi      # 0x10c65
movb $0x0, %al
callq 0x2050
movq -0x10(%rbp), %rax
movq 0x2e0(%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl 0x8(%rcx), %esi
movq -0x28(%rbp), %rcx
movl 0xc(%rcx), %edx
callq *%rax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0xdaa7
movq -0x28(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0xdaa2
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x21e0
jmp 0xdb32
movq -0x10(%rbp), %rax
movq 0x2c8(%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rcx
movq 0x18(%rcx), %rsi
movq -0x28(%rbp), %rcx
movl 0x10(%rcx), %edx
movq -0x28(%rbp), %rcx
movl 0x14(%rcx), %ecx
callq *%rax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0xdaf1
movq -0x28(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0xdaef
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x21e0
jmp 0xdb32
movl -0x2c(%rbp), %eax
movq -0x38(%rbp), %rcx
addl 0x4(%rcx), %eax
addl $0x8, %eax
movl %eax, -0x2c(%rbp)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0xdb19
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x21e0
jmp 0xdb2b
movl -0x2c(%rbp), %eax
movq -0x38(%rbp), %rcx
addl 0x4(%rcx), %eax
addl $0x8, %eax
movl %eax, -0x2c(%rbp)
jmp 0xd84f
jmp 0xdb32
movq -0x10(%rbp), %rax
movq 0x2d8(%rax), %rax
movq -0x10(%rbp), %rdi
movl $0x16, %esi
xorl %edx, %edx
callq *%rax
cmpq $0x0, -0x28(%rbp)
je 0xdb5a
movq -0x28(%rbp), %rdi
callq 0x21e0
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)