// File: /workspace/github_cpp_makefile_star2_O2/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
testq %rsi, %rsi
sete %al
testl %edx, %edx
sete %cl
xorl %ebp, %ebp
orb %al, %cl
jne 0x92ba
movq %rsi, %r15
movq %rdi, (%rsp)
pushq $0xc
popq %rbx
pushq $0x1
popq %rbp
movl %edx, 0xc(%rsp)
movq %rsi, 0x18(%rsp)
cmpl 0x8(%r15), %ebx
jae 0x9286
movq (%r15), %r14
movl %ebx, %eax
cmpl %edx, (%r14,%rax)
jne 0x9266
movq %rax, 0x20(%rsp)
leal 0x8(%rbx), %r13d
leaq (%r14,%r13), %rdi
callq 0x6687
movl %eax, 0x14(%rsp)
leal 0xc(%rbx), %edi
addq (%r15), %rdi
callq 0x6687
movl %eax, 0x10(%rsp)
leal 0x10(%rbx), %edi
addq (%r15), %rdi
callq 0x6687
movl %eax, %ebp
leal 0x14(%rbx), %edi
addq (%r15), %rdi
callq 0x6687
movl %eax, %r12d
movq %r12, %rdi
callq 0x21f0
testq %rax, %rax
je 0x928c
movq %r15, %rcx
movq %rax, %r15
addl $0x18, %ebx
addq (%rcx), %rbx
movq %rax, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2150
movq (%rsp), %rbx
movq %rbx, %rdi
pushq $0x11
popq %rsi
xorl %edx, %edx
callq *0x2d8(%rbx)
testl %eax, %eax
je 0x9294
movl %ebp, %eax
shll $0x8, %eax
andl $0xffff0000, %eax       # imm = 0xFFFF0000
movl %r12d, %ecx
shrl $0x8, %ecx
movzwl %cx, %edx
orl %eax, %edx
movq %rbx, %rdi
pushq $0x17
popq %rsi
callq *0x2d8(%rbx)
testl %eax, %eax
je 0x927c
leaq 0x29f6(%rip), %rdi      # 0xbc0e
callq 0x2270
movq %rbx, %rdi
movl 0x14(%rsp), %esi
movl 0x10(%rsp), %edx
callq *0x2e0(%rbx)
testl %eax, %eax
je 0x927c
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %edx
movl %r12d, %ecx
callq *0x2c8(%rbx)
testl %eax, %eax
je 0x927c
movl %eax, %ebp
movq 0x20(%rsp), %rax
movl 0x4(%r14,%rax), %r14d
movq %r15, %rdi
callq 0x21e0
movl 0xc(%rsp), %edx
movq 0x18(%rsp), %r15
jmp 0x9271
movl 0x4(%r14,%rax), %r14d
addl $0x8, %ebx
movl %ebx, %r13d
addl %r14d, %r13d
movl %r13d, %ebx
jmp 0x914e
movq %r15, %rdi
callq 0x21e0
jmp 0x9290
movq (%rsp), %rbx
jmp 0x92ac
movq (%rsp), %rbx
xorl %ebp, %ebp
jmp 0x92ac
movq %r15, %rdi
callq 0x21e0
leaq 0x2913(%rip), %rdi      # 0xbbb6
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x2050
pushq $0x16
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq *0x2d8(%rbx)
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq