// File: chenhn123[P]wdt_util_src/wdt_ct/wif2_handler.cpp
// Function: do_update_fw_by_wif2_chunk_fera(WdtDevice*, WifFile2*, unsigned int)
int do_update_fw_by_wif2_chunk_fera(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_FlashErase_Chunk pchunk_info_ex;

	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);

			ret = pdev->funcs_device_private.p_wh_send_commands(pdev, WH_CMD_FLASH_UNLOCK, 0);
			if (!ret) {
				goto finish;
			}
			// address and size align to 0x100
			UINT32 protect_off_arg = (pchunk_info_ex.SpaceToErase.Address >> 8 << 16 & 0xffff0000) |
						 (pchunk_info_ex.SpaceToErase.Size >> 8 & 0x0000ffff);

			ret = pdev->funcs_device_private.p_wh_send_commands(pdev, WH_CMD_FLASH_PROTECTION_OFF,
									    protect_off_arg);
			if (!ret) {
				goto finish;
			}
			ret = pdev->funcs_device_private.p_wh_flash_erase(pdev, pchunk_info_ex.SpaceToErase.Address,
									  pchunk_info_ex.SpaceToErase.Size);

			if (!ret) {
				goto finish;
			}

			chunk_start_pos = chunk_start_pos + pchunk_data->Size + 8;

		} 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);

	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl $0x1, -0x20(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0xdf9d
movl $0x0, -0x4(%rbp)
jmp 0xe11b
cmpl $0x0, -0x1c(%rbp)
jne 0xdfaf
movl $0x0, -0x4(%rbp)
jmp 0xe11b
movl $0xc, -0x34(%rbp)
movq $0x0, -0x40(%rbp)
movl -0x34(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jae 0xe0fb
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x34(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl (%rax), %eax
cmpl -0x1c(%rbp), %eax
jne 0xe0e6
movq -0x40(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x30(%rbp)
movq -0x40(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x2c(%rbp)
movl -0x34(%rbp), %eax
addq $0x8, %rax
movl %eax, -0x44(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movl -0x44(%rbp), %eax
addq %rax, %rdi
callq 0x9170
movl %eax, -0x28(%rbp)
movl -0x44(%rbp), %eax
addq $0x4, %rax
movl %eax, -0x44(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movl -0x44(%rbp), %eax
addq %rax, %rdi
callq 0x9170
movl %eax, -0x24(%rbp)
movl -0x44(%rbp), %eax
addq $0x4, %rax
movl %eax, -0x44(%rbp)
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 0xe06f
jmp 0xe0fd
movl -0x28(%rbp), %eax
shrl $0x8, %eax
shll $0x10, %eax
andl $0xffff0000, %eax       # imm = 0xFFFF0000
movl -0x24(%rbp), %ecx
shrl $0x8, %ecx
andl $0xffff, %ecx           # imm = 0xFFFF
orl %ecx, %eax
movl %eax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x2d8(%rax), %rax
movq -0x10(%rbp), %rdi
movl -0x48(%rbp), %edx
movl $0x17, %esi
callq *%rax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0xe0b2
jmp 0xe0fd
movq -0x10(%rbp), %rax
movq 0x2e0(%rax), %rax
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %esi
movl -0x24(%rbp), %edx
callq *%rax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0xe0d4
jmp 0xe0fd
movl -0x34(%rbp), %eax
movq -0x40(%rbp), %rcx
addl 0x4(%rcx), %eax
addl $0x8, %eax
movl %eax, -0x34(%rbp)
jmp 0xe0f6
movl -0x34(%rbp), %eax
movq -0x40(%rbp), %rcx
addl 0x4(%rcx), %eax
addl $0x8, %eax
movl %eax, -0x34(%rbp)
jmp 0xdfbe
jmp 0xe0fd
movq -0x10(%rbp), %rax
movq 0x2d8(%rax), %rax
movq -0x10(%rbp), %rdi
movl $0x16, %esi
xorl %edx, %edx
callq *%rax
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)