// File: /workspace/github_cpp_makefile_star2_O3/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 %r12d, %r12d
leaq 0x8(%rsp), %rcx
movl $0x8, %esi
xorl %edx, %edx
callq 0x6269
testl %eax, %eax
je 0x6bed
shrq $0x20, %rbx
movl 0x8(%rsp), %r13d
movzwl %bx, %eax
cmpl %eax, %r13d
jne 0x6a0a
movl %r14d, 0xc(%rsp)
movl $0x1, %esi
movq %r13, %rdi
callq 0x2130
testq %rax, %rax
je 0x6a23
movq %rax, %r14
xorl %r12d, %r12d
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
movl %r13d, %ecx
callq 0x5aa4
testl %eax, %eax
je 0x6be5
leaq 0xc(%r14), %rdi
callq 0x6f6b
movq %rax, 0x38(%rsp)
leaq 0x8(%r14), %rdi
callq 0x6f6b
movl %eax, 0x1c(%rsp)
leaq 0x4(%r14), %rdi
callq 0x6f6b
movq %rax, 0x30(%rsp)
movq %r14, %rdi
addq $0x2, %rdi
callq 0x6f67
movl %eax, 0x20(%rsp)
movq %r14, %rdi
callq 0x6f67
movl %eax, %ebx
xorl %r12d, %r12d
xorl %edi, %edi
movq %r14, 0x10(%rsp)
movq %r14, %rsi
xorl %edx, %edx
movl %r13d, %ecx
callq 0x88e6
movzwl %ax, %eax
movq %rbp, 0x28(%rsp)
movl %eax, (%rbp)
movl 0x20(%rsp), %ebp
movl %ebx, %eax
orw %bp, %ax
je 0x6a39
movq 0x30(%rsp), %rcx
shlq $0x20, %rcx
movl %ebp, %eax
shll $0x10, %eax
orq %rcx, %rax
movzwl %bx, %edi
orq %rax, %rdi
movq 0x38(%rsp), %rax
shlq $0x20, %rax
movl 0x1c(%rsp), %esi
orq %rax, %rsi
callq 0x674c
cmpw %ax, %bp
je 0x69da
leaq 0x52e5(%rip), %rdi      # 0xbcb8
xorl %eax, %eax
callq 0x2506
movq 0x10(%rsp), %r14
movq %r14, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0x6784
movl $0x1, %r12d
testl %eax, %eax
jne 0x6a87
leaq 0x52ea(%rip), %rdi      # 0xbceb
xorl %eax, %eax
callq 0x2506
jmp 0x6a87
leaq 0x524a(%rip), %rdi      # 0xbc5b
xorl %r12d, %r12d
movl %r13d, %esi
xorl %eax, %eax
callq 0x2506
jmp 0x6bed
leaq 0x5257(%rip), %rdi      # 0xbc81
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x2506
jmp 0x6bed
leaq 0x8(%rsp), %rcx
movq %r15, %rdi
movl $0x8, %esi
xorl %edx, %edx
callq 0x6269
testl %eax, %eax
movq 0x10(%rsp), %r14
movq 0x28(%rsp), %r13
je 0x6be5
movl 0x8(%rsp), %edx
leaq 0x44(%rsp), %rbx
xorl %r12d, %r12d
movq %r15, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x6300
movl (%r13), %ecx
cmpl (%rbx), %ecx
jne 0x6b8a
movl %eax, %r12d
cmpw $0x0, 0xc(%rsp)
je 0x6be5
xorl %r12d, %r12d
leaq 0x24(%rsp), %rcx
movq %r15, %rdi
movl $0x9, %esi
xorl %edx, %edx
callq 0x6269
testl %eax, %eax
je 0x6be5
movl 0x24(%rsp), %r13d
testq %r13, %r13
je 0x6b93
movl $0x1, %esi
movq %r13, %rdi
callq 0x2130
testq %rax, %rax
je 0x6bd4
movq %rax, %rbp
xorl %r12d, %r12d
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
movl %r13d, %ecx
callq 0x5aa4
testl %eax, %eax
je 0x6bca
leaq 0xc(%rbp), %rdi
callq 0x6f6b
movl %eax, %ebx
shlq $0x20, %rbx
leaq 0x8(%rbp), %rdi
callq 0x6f6b
movl %eax, %r14d
orq %rbx, %r14
leaq 0x4(%rbp), %rdi
callq 0x6f6b
movl %eax, %ebx
shlq $0x20, %rbx
movq %rbp, %rdi
addq $0x2, %rdi
callq 0x6f67
movl %eax, %r15d
movl %r15d, %r12d
shll $0x10, %r12d
movq %rbp, %rdi
callq 0x6f67
movzwl %ax, %edi
orq %rbx, %rdi
orq %r12, %rdi
movq %r14, %rsi
callq 0x674c
cmpw %ax, %r15w
je 0x6b64
leaq 0x51c0(%rip), %rdi      # 0xbd1d
xorl %eax, %eax
callq 0x2506
movq %rbp, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0x6784
testl %eax, %eax
jne 0x6b83
leaq 0x51d5(%rip), %rdi      # 0xbd51
xorl %eax, %eax
callq 0x2506
movq 0x10(%rsp), %r14
jmp 0x6ba5
leaq 0x5100(%rip), %rdi      # 0xbc91
jmp 0x6bde
movzwl 0xc(%rsp), %edi
movl $0x1, %esi
callq 0x2130
movq %rax, %rbp
movq 0x28(%rsp), %rbx
movzwl (%rbx), %edi
movq %rbp, %rsi
xorl %edx, %edx
movl %r13d, %ecx
callq 0x88e6
movzwl %ax, %eax
movl %eax, (%rbx)
movl $0x1, %r12d
testq %rbp, %rbp
je 0x6be5
movq %rbp, %rdi
callq 0x21e0
jmp 0x6be5
leaq 0x5134(%rip), %rdi      # 0xbd0f
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x2506
movq %r14, %rdi
callq 0x21e0
movl %r12d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq