// File: /workspace/github_cpp_makefile_star2_O3/chenhn123[P]wdt_util_src/wdt_ct/wif2_handler.cpp
// Function: do_update_fw_by_wif2_chunk_fsum(WdtDevice*, WifFile2*, unsigned int)
int do_update_fw_by_wif2_chunk_fsum(WDT_DEV *pdev, WIF_FILE2 *pcur_wif, UINT32 chunk_four_cc)
{
	if (!pcur_wif)
		return 0;

	if (!chunk_four_cc)
		return 0;
	int ret = 1;
	WIF2_Flash_Checksum_Chunk *pchunk_info_ex;
	pchunk_info_ex = (WIF2_Flash_Checksum_Chunk *)malloc(sizeof(WIF2_Flash_Checksum_Chunk));
	if (!pchunk_info_ex)
		return 0;

	UINT32 chunk_start_pos = 0 + sizeof(ChunkHeader);
	WIF2_Chunk_Header *pchunk_data = NULL;
	WIF2FlashNode *head = pchunk_info_ex->Spaces = 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);
			pchunk_info_ex->InitSum = get_unaligned_le32(&pcur_wif->pdata[pdata_pos]);
			pdata_pos = pdata_pos + sizeof(UINT32);
			pchunk_info_ex->ExpectedSum = get_unaligned_le32(&pcur_wif->pdata[pdata_pos]);
			pdata_pos = pdata_pos + sizeof(UINT32);
			int erase_count = (pchunk_data->Size - sizeof(UINT32) * 2) / (sizeof(UINT32) * 2);
			pchunk_info_ex->Spaces = NULL;

			for (int i = 0; i < erase_count; i++) {
				WIF2_Flash_Space data;
				data.Address = get_unaligned_le32(&pcur_wif->pdata[pdata_pos]);
				pdata_pos = pdata_pos + sizeof(UINT32);
				data.Size = get_unaligned_le32(&pcur_wif->pdata[pdata_pos]);
				pdata_pos = pdata_pos + sizeof(UINT32);

				append_WIF2_flash_node(&pchunk_info_ex->Spaces, data);
			}
			UINT32 sum = pchunk_info_ex->InitSum;
			head = pchunk_info_ex->Spaces;
			if (pchunk_info_ex->Spaces) {
				ret = pdev->funcs_device_private.p_wh_send_commands(pdev, WH_CMD_FLASH_UNLOCK, 0);
				if (!ret) {
					goto finish;
				}
				while (pchunk_info_ex->Spaces) {
					// printf("address:0x%x\n",
					// pchunk_info_ex->Spaces->Space.Address);
					// printf("size:0x%x\n",
					// pchunk_info_ex->Spaces->Space.Size);
					ret = pdev->funcs_device_private.p_wh_flash_get_checksum(
						pdev, &sum, pchunk_info_ex->Spaces->Space.Address,
						pchunk_info_ex->Spaces->Space.Size, sum);
					if (!ret) {
						goto finish;
					}
					pchunk_info_ex->Spaces = pchunk_info_ex->Spaces->Next;
				}
			}
			if (sum != pchunk_info_ex->ExpectedSum) {
				printf("Checksum fails. checksum is 0x%x but Expected 0x%x \n", sum,
				       pchunk_info_ex->ExpectedSum);

			} else {
				printf("Checksum psss! (0x%x)\n", sum);
			}

			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:
	pchunk_info_ex->Spaces = head;

	if (pchunk_info_ex->Spaces) {
		free_WIF2_flash_node(pchunk_info_ex->Spaces);
	}

	if (pchunk_info_ex)
		free(pchunk_info_ex);

	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, 0x18(%rsp)
testq %rsi, %rsi
sete %al
testl %edx, %edx
sete %cl
xorl %r12d, %r12d
orb %al, %cl
jne 0xa131
movl %edx, %ebx
movq %rsi, %r14
movl $0x18, %edi
callq 0x21f0
testq %rax, %rax
je 0xa131
movq %rax, %r15
movl 0x8(%r14), %eax
cmpl $0xd, %eax
jb 0xa123
movq %r15, %r12
addq $0x10, %r12
movl $0xc, %ebp
xorl %r13d, %r13d
movq %r15, 0x10(%rsp)
movl %ebx, 0x4(%rsp)
movq (%r14), %rcx
movl %ebp, %edx
cmpl %ebx, (%rcx,%rdx)
jne 0xa0a3
movl %ebx, (%r15)
movl 0x4(%rcx,%rdx), %eax
movl %eax, 0x4(%r15)
leal 0x8(%rdx), %ebp
leaq (%rcx,%rbp), %rdi
movq %rdx, 0x8(%rsp)
movq %rcx, %rbx
callq 0x6f6b
movl %eax, %r13d
movl %eax, 0x8(%r15)
movq 0x8(%rsp), %rax
leal 0xc(%rax), %edi
addq (%r14), %rdi
callq 0x6f6b
movq 0x8(%rsp), %rdx
movl %eax, 0xc(%r15)
movq %r15, %rax
movq %rbx, 0x20(%rsp)
movl 0x4(%rbx,%rdx), %r15d
movabsq $0x7fffffff8, %rcx      # imm = 0x7FFFFFFF8
addq %rcx, %r15
shrq $0x3, %r15
movq $0x0, 0x10(%rax)
testl %r15d, %r15d
jle 0xa0ac
leal 0x10(%rdx), %ebx
movl %ebx, %edi
addq (%r14), %rdi
callq 0x6f6b
movl %eax, %r13d
leal 0x4(%rbx), %edi
addq (%r14), %rdi
callq 0x6f6b
addl $0x8, %ebx
shlq $0x20, %rax
movl %r13d, %esi
orq %rax, %rsi
movq %r12, %rdi
callq 0x9ebe
decl %r15d
jne 0xa015
movq 0x10(%rsp), %r15
movl 0x8(%r15), %eax
movq 0x10(%r15), %r13
movl %eax, (%rsp)
testq %r13, %r13
je 0xa0ba
movq 0x18(%rsp), %rdi
movl $0x11, %esi
xorl %edx, %edx
callq *0x2d8(%rdi)
testl %eax, %eax
je 0xa106
movq %r13, %rbx
movl (%rbx), %edx
movl 0x4(%rbx), %ecx
movl (%rsp), %r8d
movq 0x18(%rsp), %rdi
movq %rsp, %rsi
callq *0x2d0(%rdi)
testl %eax, %eax
je 0xa106
movq 0x8(%rbx), %rbx
movq %rbx, (%r12)
testq %rbx, %rbx
jne 0xa079
jmp 0xa0bd
movl 0x4(%rcx,%rdx), %ecx
leal 0x8(%rdx), %ebp
jmp 0xa0fc
movl %r13d, (%rsp)
xorl %r13d, %r13d
movq 0x10(%rsp), %r15
jmp 0xa0bd
xorl %r13d, %r13d
movl 0x4(%rsp), %ebx
movl (%rsp), %esi
movl 0xc(%r15), %edx
cmpl %edx, %esi
jne 0xa0dc
leaq 0x2b24(%rip), %rdi      # 0xcbf7
xorl %eax, %eax
callq 0x2050
jmp 0xa0ea
leaq 0x2adf(%rip), %rdi      # 0xcbc2
xorl %eax, %eax
callq 0x2050
movq 0x8(%rsp), %rax
movq 0x20(%rsp), %rcx
movl 0x4(%rcx,%rax), %ecx
movl 0x8(%r14), %eax
addl %ecx, %ebp
cmpl %eax, %ebp
jb 0x9f96
movq %r13, (%r12)
testq %r13, %r13
je 0xa123
movq 0x8(%r13), %rbx
movq %r13, %rdi
callq 0x21e0
movq %rbx, %r13
testq %rbx, %rbx
jne 0xa10f
movq %r15, %rdi
callq 0x21e0
movl $0x1, %r12d
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq