// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movq %rdi, 0x28(%rsp)
testq %rsi, %rsi
sete %al
testl %edx, %edx
sete %cl
xorl %r12d, %r12d
orb %al, %cl
jne 0xa1d1
movl %edx, %r15d
movq %rsi, %r14
movl $0x18, %edi
callq 0x21f0
testq %rax, %rax
je 0xa1d1
movq %rax, %rbp
movq %rax, %r12
addq $0x10, %r12
movq $0x0, 0x10(%rax)
cmpl $0xd, 0x8(%r14)
jb 0xa1a3
movl $0xc, %ecx
xorl %r13d, %r13d
movq %rbp, 0x18(%rsp)
movl %r15d, 0x24(%rsp)
movq (%r14), %rbx
movl %ecx, %edx
movl (%rbx,%rdx), %eax
cmpl %r15d, %eax
jne 0xa12a
movl %eax, (%rbp)
movl 0x4(%rbx,%rdx), %eax
movl %eax, 0x4(%rbp)
leal 0x8(%rcx), %edi
addq %rbx, %rdi
movq %rcx, %r15
movq %rdx, %r13
callq 0x702b
movl %eax, 0x8(%rbp)
movq %r15, 0x10(%rsp)
leal 0xc(%r15), %edi
addq (%r14), %rdi
callq 0x702b
movl %eax, 0xc(%rbp)
movq %r13, 0x30(%rsp)
movl 0x4(%rbx,%r13), %r15d
movabsq $0x7fffffff8, %rax      # imm = 0x7FFFFFFF8
addq %rax, %r15
shrq $0x3, %r15
movq $0x0, 0x10(%rbp)
testl %r15d, %r15d
jle 0xa0c7
movq 0x10(%rsp), %rax
leal 0x10(%rax), %ebp
movl %ebp, %edi
addq (%r14), %rdi
callq 0x702b
movl %eax, %r13d
leal 0x4(%rbp), %edi
addq (%r14), %rdi
callq 0x702b
addl $0x8, %ebp
shlq $0x20, %rax
movl %r13d, %esi
orq %rax, %rsi
movq %r12, %rdi
callq 0x9f42
decl %r15d
jne 0xa095
movq 0x18(%rsp), %rbp
movl 0x8(%rbp), %eax
movl %eax, 0xc(%rsp)
movq 0x10(%rbp), %r13
testq %r13, %r13
je 0xa13e
movq 0x28(%rsp), %rdi
movl $0x11, %esi
xorl %edx, %edx
callq *0x2d8(%rdi)
movl $0x7, %r15d
testl %eax, %eax
je 0xa188
movq (%r12), %rbp
testq %rbp, %rbp
je 0xa135
movl (%rbp), %edx
movl 0x4(%rbp), %ecx
movl 0xc(%rsp), %r8d
movq 0x28(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq *0x2d0(%rdi)
testl %eax, %eax
je 0xa17f
movq 0x8(%rbp), %rbp
jmp 0xa100
movl 0x4(%rbx,%rdx), %eax
addl %eax, %ecx
addl $0x8, %ecx
jmp 0xa197
movq %rbp, (%r12)
movq 0x18(%rsp), %rbp
movl 0xc(%rsp), %esi
movl 0xc(%rbp), %edx
cmpl %edx, %esi
jne 0xa159
leaq 0x2aab(%rip), %rdi      # 0xcbfb
xorl %eax, %eax
callq 0x2050
jmp 0xa167
leaq 0x2a66(%rip), %rdi      # 0xcbc6
xorl %eax, %eax
callq 0x2050
movq 0x30(%rsp), %rax
movl 0x4(%rbx,%rax), %eax
movq 0x10(%rsp), %rcx
addl %eax, %ecx
addl $0x8, %ecx
xorl %r15d, %r15d
jmp 0xa18d
movq %rbp, (%r12)
movq 0x18(%rsp), %rbp
movq 0x10(%rsp), %rcx
testl %r15d, %r15d
jne 0xa1e3
movl 0x24(%rsp), %r15d
cmpl 0x8(%r14), %ecx
jb 0xa022
jmp 0xa1a6
xorl %r13d, %r13d
movq %r13, (%r12)
testq %r13, %r13
je 0xa1c3
movq 0x8(%r13), %rbx
movq %r13, %rdi
callq 0x21e0
movq %rbx, %r13
testq %rbx, %rbx
jne 0xa1af
movq %rbp, %rdi
callq 0x21e0
movl $0x1, %r12d
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x7, %r15d
je 0xa1a6
jmp 0xa1cb