// File: chenhn123[P]wdt_util_src/wdt_ct/w8790_funcs.cpp
// Function: wh_w8790_dev_get_section_addr_map(WdtDevice*, FlashMapAddr_8790_t*)
int wh_w8790_dev_get_section_addr_map(WDT_DEV* pdev, W8790_FLASH_MAP* psec_map)
{
	UINT32 firmware_map_table_addr = 0x000010;
	W8790_FLASH_SECTION_HEADER	sec_header;

	if (!psec_map)
		return 0;

	if (wh_w8790_dev_set_flash_address(pdev, firmware_map_table_addr) <= 0)
		return 0;

	if (wh_w8790_dev_read_flash(pdev, (BYTE*)&sec_header, sizeof(W8790_FLASH_SECTION_HEADER)) <= 0)
		return 0;

	if (wh_w8790_section_header_valid(&sec_header) && sec_header.PayloadSize >= 32 && sec_header.PayloadSize < 1024) {
		if (wh_w8790_dev_set_flash_address(pdev, firmware_map_table_addr + sizeof(W8790_FLASH_SECTION_HEADER)) <= 0)
			return 0;
		size_t padata_buffer = sec_header.PayloadSize + 16;
		BYTE* pdata = (BYTE*)malloc(padata_buffer);
		if (!pdata) {
			printf("pdata malloc fail \n");
			return 0;
		}
		int ret = wh_w8790_dev_read_flash(pdev, (BYTE*)pdata, sec_header.PayloadSize);


		if (ret > 0) {
			psec_map->ParameterMap = get_unaligned_le32(&pdata[0]);
			psec_map->MainLoader = get_unaligned_le32(&pdata[4]);
			psec_map->ParameterPrimary = get_unaligned_le32(&pdata[8]);
			psec_map->ParameterExtended = get_unaligned_le32(&pdata[12]);
			psec_map->ParameterPrivate = get_unaligned_le32(&pdata[16]);

		}
		free(pdata);
		return ret;
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x10, -0x1c(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x6fea
movl $0x0, -0x4(%rbp)
jmp 0x714a
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x6ef0
cmpl $0x0, %eax
jg 0x7007
movl $0x0, -0x4(%rbp)
jmp 0x714a
movq -0x10(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
movl $0x10, %edx
callq 0x6de0
cmpl $0x0, %eax
jg 0x702a
movl $0x0, -0x4(%rbp)
jmp 0x714a
leaq -0x2c(%rbp), %rdi
callq 0x6f50
cmpl $0x0, %eax
je 0x7143
cmpl $0x20, -0x28(%rbp)
jb 0x7143
cmpl $0x400, -0x28(%rbp)     # imm = 0x400
jae 0x7143
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %eax
addq $0x10, %rax
movl %eax, %esi
callq 0x6ef0
cmpl $0x0, %eax
jg 0x7076
movl $0x0, -0x4(%rbp)
jmp 0x714a
movl -0x28(%rbp), %eax
addl $0x10, %eax
movl %eax, %eax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x21f0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x70b0
leaq 0x8ae0(%rip), %rdi      # 0xfb7d
movb $0x0, %al
callq 0x2050
movl $0x0, -0x4(%rbp)
jmp 0x714a
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl -0x28(%rbp), %edx
callq 0x6de0
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jle 0x7132
movq -0x40(%rbp), %rdi
callq 0x9170
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x40(%rbp), %rdi
addq $0x4, %rdi
callq 0x9170
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x40(%rbp), %rdi
addq $0x8, %rdi
callq 0x9170
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x40(%rbp), %rdi
addq $0xc, %rdi
callq 0x9170
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x40(%rbp), %rdi
addq $0x10, %rdi
callq 0x9170
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x40(%rbp), %rdi
callq 0x21e0
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x714a
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)