// File: /workspace/github_cpp_makefile_star2_O3/chenhn123[P]wdt_util_src/wdt_ct/w8755_funcs.cpp
// Function: wh_w8755_dev_read_flash_map(WdtDevice*, BoardInfo*)
int wh_w8755_dev_read_flash_map(WDT_DEV* pdev, BOARD_INFO* p_out_board_info)
{
	BYTE	buffer[64] = {0};
	int 	retval = 1;

	if (!p_out_board_info) 
		return 0;

	W8755_SEC_ADDR_TYPE *psec_addr_type = &p_out_board_info->sec_header.w8755_sec_header;

	memset(psec_addr_type->device_id, 0, 10);

	int count = 20;
	do {
		wh_w8755_dev_set_device_mode(pdev, W8755_DM_COMMAND);
		wh_sleep(5);
	} while (wh_w8755_dev_get_device_mode(pdev) != W8755_DM_COMMAND && count-- > 0);
	
	
	retval = wh_w8755_dev_flash_read_data(pdev, buffer, W8755_SEC_ADDR_TABLE_OFFSET, 48);
	if (!retval)
		goto failed_exit;


	psec_addr_type->fastboot_addr = (get_unaligned_le16(&buffer[0]) << 8);
	psec_addr_type->library_addr = (get_unaligned_le16(&buffer[2]) << 8);
	psec_addr_type->firmware_image_addr = (get_unaligned_le16(&buffer[4]) << 8);
	psec_addr_type->parameter_addr = (get_unaligned_le16(&buffer[6]) << 8);		
	psec_addr_type->ate_firmware_addr = (get_unaligned_le16(&buffer[8]) << 8);		
	psec_addr_type->recovery_addr = (get_unaligned_le16(&buffer[10]) << 8);		
	psec_addr_type->param_clone_addr = (get_unaligned_le16(&buffer[12]) << 8);	
	psec_addr_type->secondary_image_address = (get_unaligned_le16(&buffer[14]) << 8);


	if (p_out_board_info->dev_info.w8755_dev_info.boot_partition == W8755_BP_SECONDARY && pdev->board_info.dev_info.w8755_dev_info.protocol_version >= 0x01010000)
	{
		memset(buffer, 0, sizeof(buffer));
		retval = wh_w8755_dev_flash_read_data(pdev, buffer, W8755_SEC_ADDR_TABLE_EXTENDED_OFFSET, 32);
		if (!retval)
			goto failed_exit;

		psec_addr_type->secondary_param_addr = (get_unaligned_le16(&buffer[0]) << 8);
		psec_addr_type->secondary_param_clone_addr = (get_unaligned_le16(&buffer[2]) << 8);
	}


failed_exit:
	wh_w8755_dev_set_device_mode(pdev, W8755_DM_SENSING);

	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x80, %rsp
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, (%rsp)
testq %rsi, %rsi
je 0x3cb5
movq %rsi, %r14
movq %rdi, %rbx
movw $0x0, 0xdc(%rsi)
movq $0x0, 0xd4(%rsi)
movl $0xffffffec, %r12d      # imm = 0xFFFFFFEC
leaq 0x40(%rsp), %r15
movq %rbx, %rdi
movl $0x5, %esi
movl $0x90, %edx
callq 0x2d98
movl $0x5, %edi
callq 0x259c
movq %rbx, %rdi
movl $0x73, %esi
movq %r15, %rdx
movl $0x20, %ecx
movl $0x1000c, %r8d          # imm = 0x1000C
callq 0x2b61
testl %eax, %eax
je 0x3b52
cmpb $-0x70, 0x40(%rsp)
setne %al
jmp 0x3b54
movb $0x1, %al
testb %al, %al
je 0x3b65
leal 0x1(%r12), %eax
testl %r12d, %r12d
movl %eax, %r12d
jne 0x3b0d
movq %rsp, %rsi
movq %rbx, %rdi
movl $0x90, %edx
movl $0x30, %ecx
callq 0x39f8
testl %eax, %eax
je 0x3cb9
movl %eax, %ebp
movq %rsp, %rdi
callq 0x6f67
movzwl %ax, %eax
shll $0x8, %eax
movl %eax, 0xac(%r14)
leaq 0x2(%rsp), %r15
movq %r15, %rdi
callq 0x6f67
movzwl %ax, %eax
shll $0x8, %eax
movl %eax, 0xb0(%r14)
leaq 0x4(%rsp), %rdi
callq 0x6f67
movzwl %ax, %eax
shll $0x8, %eax
movl %eax, 0xb4(%r14)
leaq 0x6(%rsp), %rdi
callq 0x6f67
movzwl %ax, %eax
shll $0x8, %eax
movl %eax, 0xb8(%r14)
leaq 0x8(%rsp), %rdi
callq 0x6f67
movzwl %ax, %eax
shll $0x8, %eax
movl %eax, 0xbc(%r14)
leaq 0xa(%rsp), %rdi
callq 0x6f67
movzwl %ax, %eax
shll $0x8, %eax
movl %eax, 0xc0(%r14)
leaq 0xc(%rsp), %rdi
callq 0x6f67
movzwl %ax, %eax
shll $0x8, %eax
movl %eax, 0xc4(%r14)
leaq 0xe(%rsp), %rdi
callq 0x6f67
movzwl %ax, %eax
shll $0x8, %eax
movl %eax, 0xc8(%r14)
cmpl $0x2, 0x94(%r14)
jne 0x3cbb
cmpl $0x1010000, 0x1f8(%rbx) # imm = 0x1010000
jb 0x3cbb
xorps %xmm0, %xmm0
movq %rsp, %rsi
movaps %xmm0, 0x30(%rsi)
movaps %xmm0, 0x20(%rsi)
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
movq %rbx, %rdi
movl $0x1f0, %edx            # imm = 0x1F0
movl $0x20, %ecx
callq 0x39f8
movl $0x0, %ebp
testl %eax, %eax
je 0x3cbb
movl %eax, %r12d
movq %rsp, %rdi
callq 0x6f67
movzwl %ax, %eax
shll $0x8, %eax
movl %eax, 0xcc(%r14)
movq %r15, %rdi
callq 0x6f67
movzwl %ax, %eax
shll $0x8, %eax
movl %eax, 0xd0(%r14)
movl %r12d, %ebp
jmp 0x3cbb
xorl %ebp, %ebp
jmp 0x3ccd
xorl %ebp, %ebp
movq %rbx, %rdi
movl $0x5, %esi
movl $0x1, %edx
callq 0x2d98
movl %ebp, %eax
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq