// File: /workspace/github_cpp_makefile_star2_O2/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 $0x40, %rsp
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, (%rsp)
testq %rsi, %rsi
je 0x3a1e
movq %rsi, %r14
movq %rdi, %rbx
andw $0x0, 0xdc(%rsi)
andq $0x0, 0xd4(%rsi)
pushq $-0x14
popq %r15
pushq $0x5
popq %rbp
movq %rbx, %rdi
movl $0x90, %esi
callq 0x2e7b
movl %ebp, %edi
callq 0x2555
movq %rbx, %rdi
callq 0x2e85
cmpb $-0x70, %al
je 0x38d2
leal 0x1(%r15), %eax
testl %r15d, %r15d
movl %eax, %r15d
jne 0x38a6
movq %rsp, %rsi
pushq $0x30
popq %rcx
movq %rbx, %rdi
movl $0x90, %edx
callq 0x37a4
testl %eax, %eax
je 0x3a22
movl %eax, %ebp
movq %rsp, %rdi
callq 0x6683
movzwl %ax, %eax
shll $0x8, %eax
movl %eax, 0xac(%r14)
leaq 0x2(%rsp), %r15
movq %r15, %rdi
callq 0x6683
movzwl %ax, %eax
shll $0x8, %eax
movl %eax, 0xb0(%r14)
leaq 0x4(%rsp), %rdi
callq 0x6683
movzwl %ax, %eax
shll $0x8, %eax
movl %eax, 0xb4(%r14)
leaq 0x6(%rsp), %rdi
callq 0x6683
movzwl %ax, %eax
shll $0x8, %eax
movl %eax, 0xb8(%r14)
leaq 0x8(%rsp), %rdi
callq 0x6683
movzwl %ax, %eax
shll $0x8, %eax
movl %eax, 0xbc(%r14)
leaq 0xa(%rsp), %rdi
callq 0x6683
movzwl %ax, %eax
shll $0x8, %eax
movl %eax, 0xc0(%r14)
leaq 0xc(%rsp), %rdi
callq 0x6683
movzwl %ax, %eax
shll $0x8, %eax
movl %eax, 0xc4(%r14)
leaq 0xe(%rsp), %rdi
callq 0x6683
movzwl %ax, %eax
shll $0x8, %eax
movl %eax, 0xc8(%r14)
cmpl $0x2, 0x94(%r14)
jne 0x3a24
cmpl $0x1010000, 0x1f8(%rbx) # imm = 0x1010000
jb 0x3a24
xorps %xmm0, %xmm0
movq %rsp, %rsi
movaps %xmm0, 0x30(%rsi)
movaps %xmm0, 0x20(%rsi)
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
pushq $0x20
popq %rcx
movq %rbx, %rdi
movl $0x1f0, %edx            # imm = 0x1F0
callq 0x37a4
movl $0x0, %ebp
testl %eax, %eax
je 0x3a24
movl %eax, %r12d
movq %rsp, %rdi
callq 0x6683
movzwl %ax, %eax
shll $0x8, %eax
movl %eax, 0xcc(%r14)
movq %r15, %rdi
callq 0x6683
movzwl %ax, %eax
shll $0x8, %eax
movl %eax, 0xd0(%r14)
movl %r12d, %ebp
jmp 0x3a24
xorl %ebp, %ebp
jmp 0x3a2f
xorl %ebp, %ebp
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x2e7b
movl %ebp, %eax
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq