// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0x8
popq %rsi
xorl %r12d, %r12d
leaq 0x8(%rsp), %rcx
xorl %edx, %edx
callq 0x5a37
testl %eax, %eax
je 0x6178
shrq $0x20, %rbx
movl 0x8(%rsp), %r13d
movzwl %bx, %eax
cmpl %eax, %r13d
jne 0x6149
movl %r14d, 0xc(%rsp)
pushq $0x1
popq %rsi
movq %r13, %rdi
callq 0x2130
movq %rax, %r14
testq %rax, %rax
je 0x615f
xorl %r12d, %r12d
movq %r15, %rdi
movq %r14, %rsi
xorl %edx, %edx
movl %r13d, %ecx
callq 0x5406
testl %eax, %eax
je 0x6170
leaq 0xc(%r14), %rdi
callq 0x6687
movq %rax, 0x38(%rsp)
leaq 0x8(%r14), %rdi
callq 0x6687
movl %eax, 0x1c(%rsp)
leaq 0x4(%r14), %rdi
callq 0x6687
movq %rax, 0x30(%rsp)
movq %r14, %rdi
addq $0x2, %rdi
callq 0x6683
movl %eax, 0x20(%rsp)
movq %r14, %rdi
callq 0x6683
movl %eax, %ebx
xorl %r12d, %r12d
xorl %edi, %edi
movq %r14, 0x10(%rsp)
movq %r14, %rsi
xorl %edx, %edx
movl %r13d, %ecx
callq 0x7ddb
movzwl %ax, %eax
movq %rbp, 0x28(%rsp)
movl %eax, (%rbp)
movl 0x20(%rsp), %ebp
movl %ebx, %eax
orw %bp, %ax
je 0x618a
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 0x5e90
cmpw %ax, %bp
je 0x6118
leaq 0x4ba7(%rip), %rdi      # 0xacb8
xorl %eax, %eax
callq 0x24bf
pushq $0x1
popq %r12
movq 0x10(%rsp), %r14
movq %r14, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0x5ec8
testl %eax, %eax
jne 0x61d2
leaq 0x4bae(%rip), %rdi      # 0xaceb
xorl %eax, %eax
callq 0x24bf
jmp 0x61d2
leaq 0x4b0b(%rip), %rdi      # 0xac5b
xorl %r12d, %r12d
movl %r13d, %esi
xorl %eax, %eax
callq 0x24bf
jmp 0x6178
leaq 0x4b1b(%rip), %rdi      # 0xac81
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x24bf
movq %r14, %rdi
callq 0x21e0
movl %r12d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x8
popq %rsi
leaq 0x8(%rsp), %rcx
movq %r15, %rdi
xorl %edx, %edx
callq 0x5a37
testl %eax, %eax
movq 0x10(%rsp), %r14
movq 0x28(%rsp), %r13
je 0x6170
movl 0x8(%rsp), %edx
leaq 0x44(%rsp), %rbx
xorl %r12d, %r12d
movq %r15, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x5acf
movl (%r13), %ecx
cmpl (%rbx), %ecx
jne 0x62cd
movl %eax, %r12d
cmpw $0x0, 0xc(%rsp)
je 0x6170
pushq $0x9
popq %rsi
xorl %r12d, %r12d
leaq 0x24(%rsp), %rcx
movq %r15, %rdi
xorl %edx, %edx
callq 0x5a37
testl %eax, %eax
je 0x6170
movl 0x24(%rsp), %r13d
testq %r13, %r13
je 0x62d9
pushq $0x1
popq %rsi
movq %r13, %rdi
callq 0x2130
testq %rax, %rax
je 0x6314
movq %rax, %rbp
xorl %r12d, %r12d
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
movl %r13d, %ecx
callq 0x5406
testl %eax, %eax
je 0x6320
leaq 0xc(%rbp), %rdi
callq 0x6687
movl %eax, %ebx
shlq $0x20, %rbx
leaq 0x8(%rbp), %rdi
callq 0x6687
movl %eax, %r14d
orq %rbx, %r14
leaq 0x4(%rbp), %rdi
callq 0x6687
movl %eax, %ebx
shlq $0x20, %rbx
movq %rbp, %rdi
addq $0x2, %rdi
callq 0x6683
movl %eax, %r15d
movl %r15d, %r12d
shll $0x10, %r12d
movq %rbp, %rdi
callq 0x6683
movzwl %ax, %edi
orq %rbx, %rdi
orq %r12, %rdi
movq %r14, %rsi
callq 0x5e90
cmpw %ax, %r15w
je 0x62a7
leaq 0x4a7d(%rip), %rdi      # 0xad1d
xorl %eax, %eax
callq 0x24bf
movq %rbp, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0x5ec8
testl %eax, %eax
jne 0x62c6
leaq 0x4a92(%rip), %rdi      # 0xad51
xorl %eax, %eax
callq 0x24bf
movq 0x10(%rsp), %r14
jmp 0x62e9
leaq 0x49bd(%rip), %rdi      # 0xac91
jmp 0x6169
movzwl 0xc(%rsp), %edi
pushq $0x1
popq %rsi
callq 0x2130
movq %rax, %rbp
movq 0x28(%rsp), %rbx
movzwl (%rbx), %edi
movq %rbp, %rsi
xorl %edx, %edx
movl %r13d, %ecx
callq 0x7ddb
movzwl %ax, %eax
movl %eax, (%rbx)
movq %rbp, %rdi
callq 0x21e0
pushq $0x1
popq %r12
jmp 0x6170
leaq 0x49f4(%rip), %rdi      # 0xad0f
jmp 0x6166
movq %rbp, %rdi
callq 0x21e0
jmp 0x6170