// File: chenhn123[P]wdt_util_src/wdt_ct/w8790_funcs.cpp
// Function: wh_w8790_dev_read_parameters_get_checksum(WdtDevice*, ParameterTableInfo_8790_t, unsigned int*)
int wh_w8790_dev_read_parameters_get_checksum(WDT_DEV* pdev, W8790_PARAMETER_INFO parameter_info, UINT32* sum)
{
	int ret = 0;

	UINT32 primary_parameter_size;
	ret = wh_w8790_dev_set_block_access(pdev, W8790_PrimaryParameter, 0, &primary_parameter_size);
	if (!ret) {
		return 0;
	}

	if (primary_parameter_size != parameter_info.PrimarySize) {
		wh_printf("Invalid primary parameter. ({ %d }) \n", primary_parameter_size);
		ret = 0;
		return 0;

	}
	BYTE* primary_bin;

	primary_bin = (BYTE*)calloc(primary_parameter_size, sizeof(BYTE));
	if (!primary_bin)
	{
		wh_printf("calloc fail ! \n");
		ret = 0;
		goto exit_fun;
	}

	if (wh_w8790_dev_block_read(pdev, primary_bin, 0, primary_parameter_size) <= 0)
	{
		ret = 0;
		goto exit_fun;
	}
	W8790_FLASH_SECTION_HEADER primary_header;

	primary_header.Param1 = get_unaligned_le32(&primary_bin[12]);
	primary_header.Param0 = get_unaligned_le32(&primary_bin[8]);
	primary_header.PayloadSize = get_unaligned_le32(&primary_bin[4]);
	primary_header.HeaderChecksum = get_unaligned_le16(&primary_bin[2]);
	primary_header.Checksum = get_unaligned_le16(&primary_bin[0]);

	*sum = misr_for_bytes(0, primary_bin, 0, primary_parameter_size);

	if (primary_header.Checksum == 0x0000 && primary_header.HeaderChecksum == 0x0000) // for the built-in parameter table in firmware
	{
		ret = wh_w8790_dev_set_block_access(pdev, W8790_PrimaryParameter, 0, &primary_parameter_size);
		if (!ret) {
			goto exit_fun;
		}
		UINT32 expected_sum;
		ret = wh_w8790_dev_block_checksum(pdev, &expected_sum, primary_parameter_size, 0);

		if (*sum != expected_sum) {
			wh_printf("Primary parameter checksum mismatch. \n");
			ret = 0;
			goto exit_fun;
		}

	}
	else
	{
		if (primary_header.HeaderChecksum != wh_w8790_flash_section_header_checksum(primary_header))
			wh_printf("Primary parameter section header checksum failed.\n");

		if(wh_w8790_flash_section_validate(primary_bin, primary_parameter_size) == 0)
			wh_printf("Invalid primary parameter section.\n");

	}

	
	if (parameter_info.ExtendedSize > 0)
	{
		BYTE* extended_bin;
		UINT32 extent_parameter_size;
		ret = wh_w8790_dev_set_block_access(pdev, W8790_ExtendedParameter, 0, &extent_parameter_size);
		if (!ret) {
			goto exit_fun;
		}
		if (extent_parameter_size > 0)
		{
			extended_bin = (BYTE*)calloc(extent_parameter_size, sizeof(BYTE));
			if (!extended_bin)
			{
				wh_printf("calloc fail !");

				ret = 0;
				goto exit_fun;
			}

			if (wh_w8790_dev_block_read(pdev, extended_bin, 0, extent_parameter_size) <= 0)
			{
				ret = 0;
				if (extended_bin)
					free(extended_bin);
				goto exit_fun;
			}
			W8790_FLASH_SECTION_HEADER extended_header;

			extended_header.Param1 = get_unaligned_le32(&extended_bin[12]);
			extended_header.Param0 = get_unaligned_le32(&extended_bin[8]);
			extended_header.PayloadSize = get_unaligned_le32(&extended_bin[4]);
			extended_header.HeaderChecksum = get_unaligned_le16(&extended_bin[2]);
			extended_header.Checksum = get_unaligned_le16(&extended_bin[0]);

			if (extended_header.HeaderChecksum != wh_w8790_flash_section_header_checksum(extended_header))
				wh_printf("Extended parameter section header checksum failed.\n");

			if (wh_w8790_flash_section_validate(extended_bin, extent_parameter_size) == 0)
				wh_printf("Invalid extended parameter section.\n");
		}
		else
		{
			extended_bin = (BYTE*)calloc(parameter_info.ExtendedSize, sizeof(BYTE));
		}
		*sum = misr_for_bytes(*sum, extended_bin, 0, extent_parameter_size);

		if (extended_bin)
			free(extended_bin);

	}


exit_fun:
	if (primary_bin)
		free(primary_bin);

	
	return ret; 

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movw %dx, %ax
movq %rsi, -0x1e(%rbp)
movw %ax, -0x16(%rbp)
movq -0x1e(%rbp), %rax
movq %rax, -0xe(%rbp)
movw -0x16(%rbp), %ax
movw %ax, -0x6(%rbp)
movq %rdi, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x28(%rbp), %rdi
movl $0x8, %esi
xorl %edx, %edx
leaq -0x38(%rbp), %rcx
callq 0x7da0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x879e
movl $0x0, -0x4(%rbp)
jmp 0x8b2f
movl -0x38(%rbp), %eax
movzwl -0xa(%rbp), %ecx
cmpl %ecx, %eax
je 0x87cd
movl -0x38(%rbp), %esi
leaq 0x74e0(%rip), %rdi      # 0xfc93
movb $0x0, %al
callq 0x24e0
movl $0x0, -0x34(%rbp)
movl $0x0, -0x4(%rbp)
jmp 0x8b2f
movl -0x38(%rbp), %eax
movl %eax, %edi
movl $0x1, %esi
callq 0x2130
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x8801
leaq 0x74cb(%rip), %rdi      # 0xfcb9
movb $0x0, %al
callq 0x24e0
movl $0x0, -0x34(%rbp)
jmp 0x8b19
movq -0x28(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl -0x38(%rbp), %ecx
xorl %edx, %edx
callq 0x7ea0
cmpl $0x0, %eax
jg 0x8824
movl $0x0, -0x34(%rbp)
jmp 0x8b19
movq -0x40(%rbp), %rdi
addq $0xc, %rdi
callq 0x9170
movl %eax, -0x44(%rbp)
movq -0x40(%rbp), %rdi
addq $0x8, %rdi
callq 0x9170
movl %eax, -0x48(%rbp)
movq -0x40(%rbp), %rdi
addq $0x4, %rdi
callq 0x9170
movl %eax, -0x4c(%rbp)
movq -0x40(%rbp), %rdi
addq $0x2, %rdi
callq 0x9140
movw %ax, -0x4e(%rbp)
movq -0x40(%rbp), %rdi
callq 0x9140
movw %ax, -0x50(%rbp)
movq -0x40(%rbp), %rsi
movl -0x38(%rbp), %ecx
xorl %edx, %edx
movl %edx, %edi
callq 0xb780
movzwl %ax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movzwl -0x50(%rbp), %eax
cmpl $0x0, %eax
jne 0x88fb
movzwl -0x4e(%rbp), %eax
cmpl $0x0, %eax
jne 0x88fb
movq -0x28(%rbp), %rdi
movl $0x8, %esi
xorl %edx, %edx
leaq -0x38(%rbp), %rcx
callq 0x7da0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x88bf
jmp 0x8b19
movq -0x28(%rbp), %rdi
movl -0x38(%rbp), %edx
leaq -0x54(%rbp), %rsi
xorl %ecx, %ecx
callq 0x7f00
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movl (%rax), %eax
cmpl -0x54(%rbp), %eax
je 0x88f9
leaq 0x73e3(%rip), %rdi      # 0xfcc9
movb $0x0, %al
callq 0x24e0
movl $0x0, -0x34(%rbp)
jmp 0x8b19
jmp 0x8963
movzwl -0x4e(%rbp), %eax
movl %eax, -0x98(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x64(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x5c(%rbp)
movq -0x64(%rbp), %rdi
movq -0x5c(%rbp), %rsi
callq 0x8540
movw %ax, %cx
movl -0x98(%rbp), %eax
movzwl %cx, %ecx
cmpl %ecx, %eax
je 0x8940
leaq 0x73b7(%rip), %rdi      # 0xfcf0
movb $0x0, %al
callq 0x24e0
movq -0x40(%rbp), %rdi
movl -0x38(%rbp), %esi
xorl %edx, %edx
callq 0x85a0
cmpl $0x0, %eax
jne 0x8961
leaq 0x73c9(%rip), %rdi      # 0xfd23
movb $0x0, %al
callq 0x24e0
jmp 0x8963
movzwl -0x6(%rbp), %eax
cmpl $0x0, %eax
jle 0x8b17
movq -0x28(%rbp), %rdi
movl $0x9, %esi
xorl %edx, %edx
leaq -0x74(%rbp), %rcx
callq 0x7da0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x8992
jmp 0x8b19
cmpl $0x0, -0x74(%rbp)
jbe 0x8ad1
movl -0x74(%rbp), %eax
movl %eax, %edi
movl $0x1, %esi
callq 0x2130
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
jne 0x89d0
leaq 0x738a(%rip), %rdi      # 0xfd47
movb $0x0, %al
callq 0x24e0
movl $0x0, -0x34(%rbp)
jmp 0x8b19
movq -0x28(%rbp), %rdi
movq -0x70(%rbp), %rsi
movl -0x74(%rbp), %ecx
xorl %edx, %edx
callq 0x7ea0
cmpl $0x0, %eax
jg 0x8a03
movl $0x0, -0x34(%rbp)
cmpq $0x0, -0x70(%rbp)
je 0x89fe
movq -0x70(%rbp), %rdi
callq 0x21e0
jmp 0x8b19
movq -0x70(%rbp), %rdi
addq $0xc, %rdi
callq 0x9170
movl %eax, -0x78(%rbp)
movq -0x70(%rbp), %rdi
addq $0x8, %rdi
callq 0x9170
movl %eax, -0x7c(%rbp)
movq -0x70(%rbp), %rdi
addq $0x4, %rdi
callq 0x9170
movl %eax, -0x80(%rbp)
movq -0x70(%rbp), %rdi
addq $0x2, %rdi
callq 0x9140
movw %ax, -0x82(%rbp)
movq -0x70(%rbp), %rdi
callq 0x9140
movw %ax, -0x84(%rbp)
movzwl -0x82(%rbp), %eax
movl %eax, -0x9c(%rbp)
movq -0x84(%rbp), %rax
movq %rax, -0x94(%rbp)
movq -0x7c(%rbp), %rax
movq %rax, -0x8c(%rbp)
movq -0x94(%rbp), %rdi
movq -0x8c(%rbp), %rsi
callq 0x8540
movw %ax, %cx
movl -0x9c(%rbp), %eax
movzwl %cx, %ecx
cmpl %ecx, %eax
je 0x8aae
leaq 0x72ae(%rip), %rdi      # 0xfd55
movb $0x0, %al
callq 0x24e0
movq -0x70(%rbp), %rdi
movl -0x74(%rbp), %esi
xorl %edx, %edx
callq 0x85a0
cmpl $0x0, %eax
jne 0x8acf
leaq 0x72c1(%rip), %rdi      # 0xfd89
movb $0x0, %al
callq 0x24e0
jmp 0x8ae5
movzwl -0x6(%rbp), %eax
movl %eax, %edi
movl $0x1, %esi
callq 0x2130
movq %rax, -0x70(%rbp)
movq -0x30(%rbp), %rax
movl (%rax), %eax
movq -0x70(%rbp), %rsi
movl -0x74(%rbp), %ecx
xorl %edx, %edx
movzwl %ax, %edi
callq 0xb780
movzwl %ax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x70(%rbp)
je 0x8b15
movq -0x70(%rbp), %rdi
callq 0x21e0
jmp 0x8b17
jmp 0x8b19
cmpq $0x0, -0x40(%rbp)
je 0x8b29
movq -0x40(%rbp), %rdi
callq 0x21e0
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)