// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, 0x10(%rsp)
testq %rsi, %rsi
sete %al
testl %edx, %edx
sete %cl
xorl %r12d, %r12d
orb %al, %cl
jne 0x9eac
movq %rsi, %r15
movl 0x8(%rsi), %eax
movl $0x1, %r12d
cmpl $0xd, %eax
jb 0x9e9a
movl $0xc, %r13d
movl %edx, 0x4(%rsp)
movq %r15, 0x18(%rsp)
movq (%r15), %rcx
movl %r13d, %r14d
cmpl %edx, (%rcx,%r14)
jne 0x9e5d
leal 0x8(%r14), %r13d
movq %rcx, 0x20(%rsp)
leaq (%rcx,%r13), %rdi
callq 0x6f6b
movl %eax, 0xc(%rsp)
leal 0xc(%r14), %edi
addq (%r15), %rdi
callq 0x6f6b
movl %eax, 0x8(%rsp)
leal 0x10(%r14), %edi
addq (%r15), %rdi
callq 0x6f6b
movl %eax, %ebp
leal 0x14(%r14), %edi
addq (%r15), %rdi
callq 0x6f6b
movl %eax, %ebx
movq %rbx, %rdi
callq 0x21f0
xorl %r12d, %r12d
testq %rax, %rax
je 0x9e9a
movq %r15, %rcx
movq %rax, %r15
leal 0x18(%r14), %esi
addq (%rcx), %rsi
movq %rax, %rdi
movq %rbx, %rdx
callq 0x2150
movq 0x10(%rsp), %r12
movq %r12, %rdi
movl $0x11, %esi
xorl %edx, %edx
callq *0x2d8(%r12)
testl %eax, %eax
je 0x9e81
movl %ebp, %eax
shll $0x8, %eax
andl $0xffff0000, %eax       # imm = 0xFFFF0000
movl %ebx, %ecx
shrl $0x8, %ecx
movzwl %cx, %edx
orl %eax, %edx
movq %r12, %rdi
movl $0x17, %esi
callq *0x2d8(%r12)
testl %eax, %eax
je 0x9e74
leaq 0x2e07(%rip), %rdi      # 0xcc0e
callq 0x2270
movq %r12, %rdi
movl 0xc(%rsp), %esi
movl 0x8(%rsp), %edx
callq *0x2e0(%r12)
testl %eax, %eax
je 0x9e74
movq %r12, %rdi
movq %r15, %rsi
movl %ebp, %edx
movl %ebx, %ecx
callq *0x2c8(%r12)
testl %eax, %eax
je 0x9e74
movl %eax, %r12d
movq 0x20(%rsp), %rax
movl 0x4(%rax,%r14), %ebx
movq %r15, %rdi
callq 0x21e0
movq 0x18(%rsp), %r15
movl 0x8(%r15), %eax
movl 0x4(%rsp), %edx
jmp 0x9e66
movl 0x4(%rcx,%r14), %ebx
leal 0x8(%r14), %r13d
addl %ebx, %r13d
cmpl %eax, %r13d
jb 0x9d3b
jmp 0x9e9a
movq %r15, %rdi
callq 0x21e0
xorl %r12d, %r12d
jmp 0x9e9a
movq %r15, %rdi
callq 0x21e0
leaq 0x2d26(%rip), %rdi      # 0xcbb6
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x2050
movq 0x10(%rsp), %rdi
movl $0x16, %esi
xorl %edx, %edx
callq *0x2d8(%rdi)
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq