// File: /workspace/github_cpp_makefile_star2_O1/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
testq %rsi, %rsi
sete %al
testl %edx, %edx
sete %cl
xorl %r12d, %r12d
orb %al, %cl
jne 0xa30a
movq %rsi, %r14
movq %rdi, 0x10(%rsp)
movl $0x1, %r12d
cmpl $0xd, 0x8(%rsi)
jb 0xa2f8
movl $0xc, %ebp
movl %edx, 0x8(%rsp)
movq (%r14), %rbx
movl %ebp, %r15d
cmpl %edx, (%rbx,%r15)
jne 0xa2ca
leal 0x8(%rbp), %edi
addq %rbx, %rdi
callq 0x702b
movl %eax, 0xc(%rsp)
leal 0xc(%rbp), %edi
addq (%r14), %rdi
callq 0x702b
movl %eax, %r13d
movq 0x10(%rsp), %r12
movq %r12, %rdi
movl $0x11, %esi
xorl %edx, %edx
callq *0x2d8(%r12)
testl %eax, %eax
je 0xa2e1
movl 0xc(%rsp), %eax
shll $0x8, %eax
andl $0xffff0000, %eax       # imm = 0xFFFF0000
movl %r13d, %ecx
shrl $0x8, %ecx
movzwl %cx, %edx
orl %eax, %edx
movq %r12, %rdi
movl $0x17, %esi
callq *0x2d8(%r12)
testl %eax, %eax
je 0xa2d6
movq %r12, %rdi
movl 0xc(%rsp), %esi
movl %r13d, %edx
callq *0x2e0(%r12)
testl %eax, %eax
je 0xa2d6
movl %eax, %r12d
movl 0x4(%rbx,%r15), %eax
addl %eax, %ebp
addl $0x8, %ebp
movb $0x1, %al
jmp 0xa2db
movl 0x4(%rbx,%r15), %eax
addl %eax, %ebp
addl $0x8, %ebp
jmp 0xa2ee
xorl %eax, %eax
xorl %r12d, %r12d
movl 0x8(%rsp), %edx
jmp 0xa2ea
xorl %eax, %eax
movl 0x8(%rsp), %edx
xorl %r12d, %r12d
testb %al, %al
je 0xa2f8
cmpl 0x8(%r14), %ebp
jb 0xa230
movq 0x10(%rsp), %rdi
movl $0x16, %esi
xorl %edx, %edx
callq *0x2d8(%rdi)
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq