// File: /workspace/github_cpp_makefile_star2_O3/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 0xa24b
movq %rsi, %rbx
movq %rdi, 0x8(%rsp)
movl 0x8(%rsi), %ecx
movl $0x1, %r12d
cmpl $0xd, %ecx
jb 0xa239
movl $0xc, %ebp
movl %edx, 0x4(%rsp)
movq %rbx, 0x10(%rsp)
movq (%rbx), %r14
movl %ebp, %r15d
addl $0x8, %ebp
cmpl %edx, (%r14,%r15)
jne 0xa227
movl %ebp, %edi
addq %r14, %rdi
callq 0x6f6b
movl %eax, %r12d
leal 0xc(%r15), %edi
addq (%rbx), %rdi
callq 0x6f6b
movl %eax, %r13d
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movl $0x11, %esi
xorl %edx, %edx
callq *0x2d8(%rbx)
testl %eax, %eax
je 0xa236
movl %r12d, %eax
shll $0x8, %eax
andl $0xffff0000, %eax       # imm = 0xFFFF0000
movl %r13d, %ecx
shrl $0x8, %ecx
movzwl %cx, %edx
orl %eax, %edx
movq %rbx, %rdi
movl $0x17, %esi
callq *0x2d8(%rbx)
testl %eax, %eax
je 0xa236
movq %rbx, %rdi
movl %r12d, %esi
movl %r13d, %edx
callq *0x2e0(%rbx)
testl %eax, %eax
movl 0x4(%rsp), %edx
movq 0x10(%rsp), %rbx
movl $0x0, %r12d
je 0xa239
movl 0x8(%rbx), %ecx
movl %eax, %r12d
addl 0x4(%r14,%r15), %ebp
cmpl %ecx, %ebp
jb 0xa18f
jmp 0xa239
xorl %r12d, %r12d
movq 0x8(%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