// File: /workspace/github_cpp_makefile_star2_O2/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
testq %rsi, %rsi
sete %al
testl %edx, %edx
sete %cl
xorl %ebp, %ebp
orb %al, %cl
jne 0x9509
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
pushq $0x18
popq %rdi
callq 0x21f0
testq %rax, %rax
je 0x9509
movq %rax, 0x8(%rsp)
movq %rax, %r12
addq $0x10, %r12
pushq $0xc
popq %r13
xorl %ebp, %ebp
movl %ebx, 0x4(%rsp)
cmpl 0x8(%r14), %r13d
jae 0x94e8
movq (%r14), %rax
movl %r13d, %ecx
cmpl %ebx, (%rax,%rcx)
jne 0x943f
movq 0x8(%rsp), %rbp
movl %ebx, (%rbp)
movq %rax, %rbx
movl 0x4(%rax,%rcx), %eax
movl %eax, 0x4(%rbp)
leal 0x8(%r13), %eax
movq %rax, 0x10(%rsp)
leaq (%rax,%rbx), %rdi
movq %rcx, 0x18(%rsp)
callq 0x6687
movl %eax, 0x8(%rbp)
leal 0xc(%r13), %edi
addq (%r14), %rdi
callq 0x6687
movl %eax, 0xc(%rbp)
movq %rbx, 0x20(%rsp)
movq 0x18(%rsp), %rax
movl 0x4(%rbx,%rax), %ebx
movabsq $0x7fffffff8, %rax      # imm = 0x7FFFFFFF8
addq %rax, %rbx
shrq $0x3, %rbx
andq $0x0, 0x10(%rbp)
testl %ebx, %ebx
movl $0x0, %eax
cmovlel %eax, %ebx
addl $0x14, %r13d
subl $0x1, %ebx
jb 0x944c
leal -0x4(%r13), %eax
movl %eax, %edi
addq (%r14), %rdi
callq 0x6687
movl %eax, %ebp
movl %r13d, %edi
addq (%r14), %rdi
callq 0x6687
shlq $0x20, %rax
movl %ebp, %esi
orq %rax, %rsi
movq %r12, %rdi
callq 0x92cb
addl $0x8, %r13d
jmp 0x9408
movl 0x4(%rax,%rcx), %eax
addl $0x8, %r13d
jmp 0x94e0
movq 0x8(%rsp), %r13
movl 0x8(%r13), %esi
movl %esi, (%rsp)
movq 0x10(%r13), %rbp
testq %rbp, %rbp
je 0x94a3
movq %r15, %rdi
pushq $0x11
popq %rsi
xorl %edx, %edx
callq *0x2d8(%r15)
testl %eax, %eax
je 0x94ed
movq %rbp, %rbx
testq %rbx, %rbx
je 0x94a0
movl (%rbx), %edx
movl 0x4(%rbx), %ecx
movl (%rsp), %r8d
movq %r15, %rdi
movq %rsp, %rsi
callq *0x2d0(%r15)
testl %eax, %eax
je 0x94ed
movq 0x8(%rbx), %rbx
movq %rbx, (%r12)
jmp 0x9477
movl (%rsp), %esi
movl 0xc(%r13), %edx
cmpl %edx, %esi
jne 0x94bb
leaq 0x2745(%rip), %rdi      # 0xbbf7
xorl %eax, %eax
callq 0x2050
jmp 0x94c9
leaq 0x2700(%rip), %rdi      # 0xbbc2
xorl %eax, %eax
callq 0x2050
movl 0x4(%rsp), %ebx
movq 0x10(%rsp), %r13
movq 0x20(%rsp), %rax
movq 0x18(%rsp), %rcx
movl 0x4(%rax,%rcx), %eax
addl %eax, %r13d
jmp 0x9382
movq 0x8(%rsp), %r13
movq %rbp, (%r12)
testq %rbp, %rbp
je 0x94fe
movq %rbp, %rdi
callq 0x931a
movq %r13, %rdi
callq 0x21e0
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq