// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r15
testq %rsi, %rsi
sete %sil
testl %edx, %edx
sete %cl
xorl %eax, %eax
orb %sil, %cl
jne 0x961b
movq %rdi, 0x8(%rsp)
pushq $0x1
popq %rax
pushq $0xc
popq %r13
movl %edx, 0x4(%rsp)
movq %r15, 0x10(%rsp)
cmpl 0x8(%r15), %r13d
jae 0x9605
movq (%r15), %rbp
movl %r13d, %r14d
leal 0x8(%r13), %ebx
cmpl %edx, (%rbp,%r14)
jne 0x95f8
movl %ebx, %edi
addq %rbp, %rdi
callq 0x6687
movl %eax, %r12d
addl $0xc, %r13d
addq (%r15), %r13
movq %r13, %rdi
callq 0x6687
movl %eax, %r15d
movl $0x0, (%rsp)
movq 0x8(%rsp), %r13
movq %r13, %rdi
pushq $0x11
popq %rsi
xorl %edx, %edx
callq *0x2d8(%r13)
testl %eax, %eax
je 0x9608
movl %r12d, %eax
shll $0x8, %eax
andl $0xffff0000, %eax       # imm = 0xFFFF0000
movl %r15d, %ecx
shrl $0x8, %ecx
movzwl %cx, %edx
orl %eax, %edx
movq %r13, %rdi
pushq $0x17
popq %rsi
callq *0x2d8(%r13)
testl %eax, %eax
je 0x9608
movq %r13, %rdi
movl %r12d, %esi
movl %r15d, %edx
callq *0x2e0(%r13)
testl %eax, %eax
movl 0x4(%rsp), %edx
movq 0x10(%rsp), %r15
je 0x9608
addl 0x4(%rbp,%r14), %ebx
movl %ebx, %r13d
jmp 0x9557
movl %eax, (%rsp)
pushq $0x16
popq %rsi
movq 0x8(%rsp), %rdi
xorl %edx, %edx
callq *0x2d8(%rdi)
movl (%rsp), %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq