// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0xdc96
movl $0x0, -0x4(%rbp)
jmp 0xdf64
cmpl $0x0, -0x1c(%rbp)
jne 0xdca8
movl $0x0, -0x4(%rbp)
jmp 0xdf64
movl $0x1, -0x20(%rbp)
movl $0x18, %edi
callq 0x21f0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xdcd0
movl $0x0, -0x4(%rbp)
jmp 0xdf64
movl $0xc, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq $0x0, 0x10(%rax)
movq $0x0, -0x40(%rbp)
movl -0x2c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jae 0xdf27
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x2c(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movl (%rax), %eax
cmpl -0x1c(%rbp), %eax
jne 0xdf12
movq -0x38(%rbp), %rax
movl (%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x38(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x2c(%rbp), %eax
addq $0x8, %rax
movl %eax, -0x44(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movl -0x44(%rbp), %eax
addq %rax, %rdi
callq 0x9170
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x44(%rbp), %eax
addq $0x4, %rax
movl %eax, -0x44(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movl -0x44(%rbp), %eax
addq %rax, %rdi
callq 0x9170
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0xc(%rax)
movl -0x44(%rbp), %eax
addq $0x4, %rax
movl %eax, -0x44(%rbp)
movq -0x38(%rbp), %rax
movl 0x4(%rax), %eax
subq $0x8, %rax
shrq $0x3, %rax
movl %eax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq $0x0, 0x10(%rax)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x48(%rbp), %eax
jge 0xde20
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movl -0x44(%rbp), %eax
addq %rax, %rdi
callq 0x9170
movl %eax, -0x54(%rbp)
movl -0x44(%rbp), %eax
addq $0x4, %rax
movl %eax, -0x44(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movl -0x44(%rbp), %eax
addq %rax, %rdi
callq 0x9170
movl %eax, -0x50(%rbp)
movl -0x44(%rbp), %eax
addq $0x4, %rax
movl %eax, -0x44(%rbp)
movq -0x28(%rbp), %rdi
addq $0x10, %rdi
movq -0x54(%rbp), %rax
movq %rax, -0x5c(%rbp)
movq -0x5c(%rbp), %rsi
callq 0xdb70
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0xddb6
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x60(%rbp)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0xdec9
movq -0x10(%rbp), %rax
movq 0x2d8(%rax), %rax
movq -0x10(%rbp), %rdi
movl $0x11, %esi
xorl %edx, %edx
callq *%rax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0xde6b
jmp 0xdf29
jmp 0xde6d
movq -0x28(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0xdec7
movq -0x10(%rbp), %rax
movq 0x2d0(%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl (%rcx), %edx
movq -0x28(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl 0x4(%rcx), %ecx
movl -0x60(%rbp), %r8d
leaq -0x60(%rbp), %rsi
callq *%rax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0xdeb1
jmp 0xdf29
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0xde6d
jmp 0xdec9
movl -0x60(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0xc(%rcx), %eax
je 0xdeef
movl -0x60(%rbp), %esi
movq -0x28(%rbp), %rax
movl 0xc(%rax), %edx
leaq 0x2d8f(%rip), %rdi      # 0x10c75
movb $0x0, %al
callq 0x2050
jmp 0xdf00
movl -0x60(%rbp), %esi
leaq 0x2db1(%rip), %rdi      # 0x10caa
movb $0x0, %al
callq 0x2050
movl -0x2c(%rbp), %eax
movq -0x38(%rbp), %rcx
addl 0x4(%rcx), %eax
addl $0x8, %eax
movl %eax, -0x2c(%rbp)
jmp 0xdf22
movl -0x2c(%rbp), %eax
movq -0x38(%rbp), %rcx
addl 0x4(%rcx), %eax
addl $0x8, %eax
movl %eax, -0x2c(%rbp)
jmp 0xdcf3
jmp 0xdf29
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0xdf4d
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0xdc20
cmpq $0x0, -0x28(%rbp)
je 0xdf5d
movq -0x28(%rbp), %rdi
callq 0x21e0
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)