// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
testq %rsi, %rsi
sete %al
testl %edx, %edx
sete %cl
xorl %r13d, %r13d
orb %al, %cl
jne 0x9f30
movq %rsi, %r14
movq %rdi, 0x30(%rsp)
movl $0x1, %r13d
cmpl $0xd, 0x8(%rsi)
jb 0x9f1e
movl %edx, %r15d
movl $0xc, %ebp
movl %edx, 0xc(%rsp)
movq %r14, 0x20(%rsp)
movq (%r14), %rbx
movl %ebp, %r12d
cmpl %r15d, (%rbx,%r12)
jne 0x9eb4
leal 0x8(%rbp), %edi
addq %rbx, %rdi
callq 0x702b
movl %eax, 0x2c(%rsp)
leal 0xc(%rbp), %edi
addq (%r14), %rdi
callq 0x702b
movl %eax, 0x28(%rsp)
leal 0x10(%rbp), %edi
addq (%r14), %rdi
callq 0x702b
movl %eax, 0x1c(%rsp)
leal 0x14(%rbp), %edi
addq (%r14), %rdi
callq 0x702b
movl %eax, %r13d
movq %r13, %rdi
callq 0x21f0
testq %rax, %rax
je 0x9ec0
leal 0x18(%rbp), %esi
addq (%r14), %rsi
movq %rax, %r14
movq %rax, %rdi
movq %r13, %rdx
callq 0x2150
movq 0x30(%rsp), %r15
movq %r15, %rdi
movl $0x11, %esi
xorl %edx, %edx
callq *0x2d8(%r15)
testl %eax, %eax
je 0x9ec7
movq %r14, 0x10(%rsp)
movl 0x1c(%rsp), %eax
shll $0x8, %eax
andl $0xffff0000, %eax       # imm = 0xFFFF0000
movl %r13d, %ecx
shrl $0x8, %ecx
movzwl %cx, %edx
orl %eax, %edx
movq %r15, %rdi
movl $0x17, %esi
callq *0x2d8(%r15)
testl %eax, %eax
je 0x9ef7
leaq 0x2db3(%rip), %rdi      # 0xcc12
callq 0x2270
movq %r15, %rdi
movl 0x2c(%rsp), %esi
movl 0x28(%rsp), %edx
callq *0x2e0(%r15)
testl %eax, %eax
movq 0x20(%rsp), %r14
je 0x9ee1
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movl 0x1c(%rsp), %edx
movl %r13d, %ecx
callq *0x2c8(%r15)
testl %eax, %eax
je 0x9ee1
movl %eax, %r13d
movl 0x4(%rbx,%r12), %eax
addl %eax, %ebp
addl $0x8, %ebp
movq 0x10(%rsp), %rdi
callq 0x21e0
movb $0x1, %bl
jmp 0x9ef0
movl 0x4(%rbx,%r12), %eax
addl %eax, %ebp
addl $0x8, %ebp
jmp 0x9f14
xorl %ebx, %ebx
xorl %r13d, %r13d
jmp 0x9f10
movq %r14, %rdi
callq 0x21e0
xorl %ebx, %ebx
leaq 0x2ce2(%rip), %rdi      # 0xcbba
xorl %eax, %eax
callq 0x2050
jmp 0x9f03
movq 0x10(%rsp), %rdi
callq 0x21e0
xorl %ebx, %ebx
xorl %r13d, %r13d
movl 0xc(%rsp), %r15d
jmp 0x9f10
movq 0x10(%rsp), %rdi
callq 0x21e0
xorl %ebx, %ebx
xorl %r13d, %r13d
movl 0xc(%rsp), %r15d
movq 0x20(%rsp), %r14
testb %bl, %bl
je 0x9f1e
cmpl 0x8(%r14), %ebp
jb 0x9d97
movq 0x30(%rsp), %rdi
movl $0x16, %esi
xorl %edx, %edx
callq *0x2d8(%rdi)
movl %r13d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq