// File: chenhn123[P]wdt_util_src/wdt_ct/w8790_funcs.cpp
// Function: wh_w8790_flash_section_validate(unsigned char*, unsigned int, int)
int wh_w8790_flash_section_validate(BYTE* data, UINT32 datalength, int start = 0)
{
	int ret = 0;
	W8790_FLASH_SECTION_HEADER header;

	header.Param1 = get_unaligned_le32(&data[12]);
	header.Param0 = get_unaligned_le32(&data[8]);
	header.PayloadSize = get_unaligned_le32(&data[4]);
	header.HeaderChecksum = get_unaligned_le16(&data[2]);
	header.Checksum = get_unaligned_le16(&data[0]);
	if (header.HeaderChecksum != wh_w8790_flash_section_header_checksum(header))
	{
		wh_printf("Invalid section header. \n");
		return 0;
	}
	if (start + sizeof(header) + header.PayloadSize > datalength)
	{
		wh_printf("Invalid section length. \n");
		return 0;
	}
	BYTE* payload;
	payload = (BYTE*)calloc(header.PayloadSize, sizeof(BYTE));
	if (!payload) {
		wh_printf("calloc fail !\n");
		return 0;
	}
	memcpy(payload, &data[16], header.PayloadSize);

	if (header.Checksum != misr_for_bytes(header.HeaderChecksum, payload, 0, header.PayloadSize / sizeof(BYTE))) {
		ret = 0;
		wh_printf("Payload checksum error.\n");
	}
	else 
	{
		ret = 1;
	}
		
	if (payload)
		free(payload);
	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x10(%rbp), %rdi
addq $0xc, %rdi
callq 0x9170
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
callq 0x9170
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rdi
addq $0x4, %rdi
callq 0x9170
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
addq $0x2, %rdi
callq 0x9140
movw %ax, -0x2a(%rbp)
movq -0x10(%rbp), %rdi
callq 0x9140
movw %ax, -0x2c(%rbp)
movzwl -0x2a(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x2c(%rbp), %rax
movq %rax, -0x3c(%rbp)
movq -0x24(%rbp), %rax
movq %rax, -0x34(%rbp)
movq -0x3c(%rbp), %rdi
movq -0x34(%rbp), %rsi
callq 0x8540
movw %ax, %cx
movl -0x4c(%rbp), %eax
movzwl %cx, %ecx
cmpl %ecx, %eax
je 0x8652
leaq 0x75f8(%rip), %rdi      # 0xfc37
movb $0x0, %al
callq 0x24e0
movl $0x0, -0x4(%rbp)
jmp 0x872a
movslq -0x18(%rbp), %rax
addq $0x10, %rax
movl -0x28(%rbp), %ecx
addq %rcx, %rax
movl -0x14(%rbp), %ecx
cmpq %rcx, %rax
jbe 0x8682
leaq 0x75e2(%rip), %rdi      # 0xfc51
movb $0x0, %al
callq 0x24e0
movl $0x0, -0x4(%rbp)
jmp 0x872a
movl -0x28(%rbp), %eax
movl %eax, %edi
movl $0x1, %esi
callq 0x2130
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x86b3
leaq 0x75c8(%rip), %rdi      # 0xfc6b
movb $0x0, %al
callq 0x24e0
movl $0x0, -0x4(%rbp)
jmp 0x872a
movq -0x48(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x10, %rsi
movl -0x28(%rbp), %eax
movl %eax, %edx
callq 0x2150
movzwl -0x2c(%rbp), %eax
movl %eax, -0x50(%rbp)
movw -0x2a(%rbp), %ax
movq -0x48(%rbp), %rsi
movl -0x28(%rbp), %ecx
shrq $0x0, %rcx
xorl %edx, %edx
movzwl %ax, %edi
callq 0xb780
movw %ax, %cx
movl -0x50(%rbp), %eax
movzwl %cx, %ecx
cmpl %ecx, %eax
je 0x870d
movl $0x0, -0x1c(%rbp)
leaq 0x7576(%rip), %rdi      # 0xfc7a
movb $0x0, %al
callq 0x24e0
jmp 0x8714
movl $0x1, -0x1c(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x8724
movq -0x48(%rbp), %rdi
callq 0x21e0
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)