// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %rbp
movl %edx, %r14d
movq %rsi, %rbx
movq %rdi, %r15
xorl %r13d, %r13d
leaq 0x8(%rsp), %rcx
movl $0x8, %esi
xorl %edx, %edx
callq 0x62dc
testl %eax, %eax
je 0x6c8a
movl %eax, %r12d
movl %r14d, 0x18(%rsp)
shrq $0x20, %rbx
movl 0x8(%rsp), %r14d
movzwl %bx, %eax
cmpl %eax, %r14d
jne 0x6a9c
movl $0x1, %esi
movq %r14, %rdi
callq 0x2130
movq %rax, %rbx
testq %rax, %rax
je 0x6ab5
xorl %r13d, %r13d
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
movl %r14d, %ecx
callq 0x5afb
testl %eax, %eax
je 0x6c7d
leaq 0xc(%rbx), %rdi
callq 0x702b
movq %rax, 0x38(%rsp)
leaq 0x8(%rbx), %rdi
callq 0x702b
movl %eax, 0x1c(%rsp)
leaq 0x4(%rbx), %rdi
callq 0x702b
movq %rax, 0x30(%rsp)
leaq 0x2(%rbx), %rdi
callq 0x7027
movl %eax, 0x20(%rsp)
movq %rbx, %rdi
callq 0x7027
movw %ax, 0xe(%rsp)
xorl %r13d, %r13d
xorl %edi, %edi
movq %rbx, 0x10(%rsp)
movq %rbx, %rsi
movl 0x20(%rsp), %ebx
xorl %edx, %edx
movl %r14d, %ecx
callq 0x8966
movzwl 0xe(%rsp), %edx
movzwl %ax, %eax
movq %rbp, 0x28(%rsp)
movl %eax, (%rbp)
movl %edx, %eax
orw %bx, %ax
je 0x6acb
movq 0x30(%rsp), %rcx
shlq $0x20, %rcx
movl %ebx, %eax
shll $0x10, %eax
orq %rcx, %rax
movzwl %dx, %edi
orq %rax, %rdi
movq 0x38(%rsp), %rax
shlq $0x20, %rax
movl 0x1c(%rsp), %esi
orq %rax, %rsi
callq 0x67d9
cmpw %ax, %bx
je 0x6a6f
leaq 0x5250(%rip), %rdi      # 0xbcb8
xorl %eax, %eax
callq 0x2506
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movl %r14d, %esi
xorl %edx, %edx
callq 0x6811
testl %eax, %eax
jne 0x6b36
leaq 0x525b(%rip), %rdi      # 0xbceb
xorl %eax, %eax
callq 0x2506
jmp 0x6b36
leaq 0x51b8(%rip), %rdi      # 0xbc5b
xorl %r13d, %r13d
movl %r14d, %esi
xorl %eax, %eax
callq 0x2506
jmp 0x6c8a
leaq 0x51c5(%rip), %rdi      # 0xbc81
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x2506
jmp 0x6c7d
leaq 0x8(%rsp), %rcx
movq %r15, %rdi
movl $0x8, %esi
xorl %edx, %edx
callq 0x62dc
testl %eax, %eax
movq 0x10(%rsp), %rbx
movq 0x28(%rsp), %r14
je 0x6c7d
movl 0x8(%rsp), %edx
leaq 0x44(%rsp), %rbx
xorl %r12d, %r12d
movq %r15, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x6380
movl (%r14), %ebp
movl (%rbx), %ebx
cmpl %ebx, %ebp
jne 0x6b18
movl %eax, %r12d
jmp 0x6b26
leaq 0x5172(%rip), %rdi      # 0xbc91
xorl %eax, %eax
callq 0x2506
movl %r12d, %r13d
cmpl %ebx, %ebp
movq 0x10(%rsp), %rbx
jne 0x6c7d
movl %r12d, %r13d
cmpw $0x0, 0x18(%rsp)
je 0x6c7d
xorl %r13d, %r13d
leaq 0x24(%rsp), %rcx
movq %r15, %rdi
movl $0x9, %esi
xorl %edx, %edx
callq 0x62dc
testl %eax, %eax
je 0x6c7d
movl 0x24(%rsp), %r12d
testq %r12, %r12
je 0x6c3c
movl $0x1, %esi
movq %r12, %rdi
callq 0x2130
testq %rax, %rax
je 0x6c9c
movq %rax, %rbp
xorl %r13d, %r13d
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
movl %r12d, %ecx
callq 0x5afb
testl %eax, %eax
je 0x6c75
leaq 0xc(%rbp), %rdi
callq 0x702b
movl %eax, %ebx
shlq $0x20, %rbx
leaq 0x8(%rbp), %rdi
callq 0x702b
movl %eax, %r14d
orq %rbx, %r14
leaq 0x4(%rbp), %rdi
callq 0x702b
movl %eax, %ebx
shlq $0x20, %rbx
movq %rbp, %rdi
addq $0x2, %rdi
callq 0x7027
movl %eax, %r15d
movl %r15d, %r13d
shll $0x10, %r13d
movq %rbp, %rdi
callq 0x7027
movzwl %ax, %edi
orq %rbx, %rdi
orq %r13, %rdi
movq %r14, %rsi
callq 0x67d9
cmpw %ax, %r15w
je 0x6c16
leaq 0x510e(%rip), %rdi      # 0xbd1d
xorl %eax, %eax
callq 0x2506
movq %rbp, %rdi
movl %r12d, %esi
xorl %edx, %edx
callq 0x6811
testl %eax, %eax
jne 0x6c35
leaq 0x5123(%rip), %rdi      # 0xbd51
xorl %eax, %eax
callq 0x2506
movq 0x10(%rsp), %rbx
jmp 0x6c4e
movzwl 0x18(%rsp), %edi
movl $0x1, %esi
callq 0x2130
movq %rax, %rbp
movq 0x28(%rsp), %r14
movzwl (%r14), %edi
movq %rbp, %rsi
xorl %edx, %edx
movl %r12d, %ecx
callq 0x8966
movzwl %ax, %eax
movl %eax, (%r14)
movl $0x1, %r13d
testq %rbp, %rbp
je 0x6c7d
movq %rbp, %rdi
callq 0x21e0
testq %rbx, %rbx
je 0x6c8a
movq %rbx, %rdi
callq 0x21e0
movl %r13d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x506c(%rip), %rdi      # 0xbd0f
jmp 0x6abc