// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x2110
movl $0x1, -0x64(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x449d
movl $0x0, -0x4(%rbp)
jmp 0x4697
movq -0x18(%rbp), %rax
addq $0xac, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
addq $0x28, %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2110
movl $0x14, -0x74(%rbp)
movq -0x10(%rbp), %rdi
movl $0x90, %esi
callq 0x3420
movl $0x5, %edi
callq 0x2590
movq -0x10(%rbp), %rdi
callq 0x3450
movzbl %al, %ecx
xorl %eax, %eax
cmpl $0x90, %ecx
movb %al, -0x75(%rbp)
je 0x450b
movl -0x74(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x74(%rbp)
cmpl $0x0, %eax
setg %al
movb %al, -0x75(%rbp)
movb -0x75(%rbp), %al
testb $0x1, %al
jne 0x44c6
movq -0x10(%rbp), %rdi
leaq -0x60(%rbp), %rsi
movl $0x90, %edx
movl $0x30, %ecx
callq 0x4320
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
jne 0x4537
jmp 0x4683
leaq -0x60(%rbp), %rdi
callq 0x9140
movzwl %ax, %ecx
shll $0x8, %ecx
movq -0x70(%rbp), %rax
movl %ecx, (%rax)
leaq -0x60(%rbp), %rdi
addq $0x2, %rdi
callq 0x9140
movzwl %ax, %ecx
shll $0x8, %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x4(%rax)
leaq -0x60(%rbp), %rdi
addq $0x4, %rdi
callq 0x9140
movzwl %ax, %ecx
shll $0x8, %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x8(%rax)
leaq -0x60(%rbp), %rdi
addq $0x6, %rdi
callq 0x9140
movzwl %ax, %ecx
shll $0x8, %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0xc(%rax)
leaq -0x60(%rbp), %rdi
addq $0x8, %rdi
callq 0x9140
movzwl %ax, %ecx
shll $0x8, %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x10(%rax)
leaq -0x60(%rbp), %rdi
addq $0xa, %rdi
callq 0x9140
movzwl %ax, %ecx
shll $0x8, %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x14(%rax)
leaq -0x60(%rbp), %rdi
addq $0xc, %rdi
callq 0x9140
movzwl %ax, %ecx
shll $0x8, %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x18(%rax)
leaq -0x60(%rbp), %rdi
addq $0xe, %rdi
callq 0x9140
movzwl %ax, %ecx
shll $0x8, %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x18(%rbp), %rax
cmpl $0x2, 0x94(%rax)
jne 0x4681
movq -0x10(%rbp), %rax
cmpl $0x1010000, 0x1f8(%rax) # imm = 0x1010000
jb 0x4681
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x2110
movq -0x10(%rbp), %rdi
leaq -0x60(%rbp), %rsi
movl $0x1f0, %edx            # imm = 0x1F0
movl $0x20, %ecx
callq 0x4320
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
jne 0x4651
jmp 0x4683
leaq -0x60(%rbp), %rdi
callq 0x9140
movzwl %ax, %ecx
shll $0x8, %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x20(%rax)
leaq -0x60(%rbp), %rdi
addq $0x2, %rdi
callq 0x9140
movzwl %ax, %ecx
shll $0x8, %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x24(%rax)
jmp 0x4683
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x3420
movl -0x64(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)