// File: chenhn123[P]wdt_util_src/wdt_ct/func_i2c.cpp
// Function: wh_i2c_prepare_data(WdtDevice*, BoardInfo*)
int wh_i2c_prepare_data(WDT_DEV *pdev, BOARD_INFO* pboard_info)
{
	BYTE				buf[80] ={0};
	BOARD_INFO			board_info;	
	int	ret_size;
	int 	ret = 0;
	int 	fw_id = 0;
	int     retryF2 = 3;

	if (!pdev || !pdev->dev_handle || !pboard_info) {
		wh_printf("device ptr is null !\n");
		return 0;
	}
	wh_printf("prepare_i2c_addr %x\n", pdev->board_info.i2c_address);

	memset(&board_info, 0, sizeof(BOARD_INFO));
	board_info.i2c_address = pdev->board_info.i2c_address;

	pdev->dev_state = DS_GET_INFO;
	int get_param_hid_ret = wh_i2c_get_param_hid(pdev, &board_info);
	if(get_param_hid_ret == 0) {
		wh_printf("i2c_get_param_hid fail ! \n");
	}
		
	

	buf[0] = VND_REQ_DEV_INFO;
	while ((retryF2 && ret <= 0) || fw_id == 0)
	{
		ret = wh_i2c_get_feature(pdev, buf, 64);	
		fw_id = get_unaligned_le32(buf + 1);
		retryF2 --;

		if (ret <= 0 || fw_id == 0) {
			pboard_info->dev_type = FW_MAYBE_ISP;	
			wh_printf("Can't get fw id, should be in ISP mode !\n");

			if ((buf[0x26] == 0x49 && buf[0x27] == 0x53 && buf[0x28] == 0x50)) {
				BYTE romSig[8];
				memset(romSig, 0, sizeof(romSig));
				wh_w8760_get_rom_signature(2, romSig);
				if(memcmp(&buf[0 + 0x18], romSig, sizeof(romSig)) == 0)
				{	
					board_info.dev_type = FW_WDT8760_2_ISP;
					wh_w8762_isp_rerun_recovery(pdev);		
				}
				memset(romSig, 0, sizeof(romSig));
				wh_w8760_get_rom_signature(1, romSig);
				if(memcmp(&buf[0 + 0x18], romSig, sizeof(romSig)) == 0)
				{
					board_info.dev_type = FW_WDT8760_2_ISP;
					wh_w8762_isp_rerun_recovery(pdev);			
				}
				memset(romSig, 0, sizeof(romSig));
				wh_w8760_get_rom_signature(0, romSig);
				if(memcmp(&buf[0 + 0x18], romSig, sizeof(romSig)) == 0)
				{
					board_info.dev_type = FW_WDT8760_2_ISP;
					wh_w8760_isp_rerun_recovery(pdev);
				}	
			
			} 
			if(retryF2 == 0){
				printf("Can't get fw id, should be in ISP mode !\n");
				return 0;
			}
		}
	}
	


	if (buf[0] != VND_REQ_DEV_INFO) {
		pboard_info->dev_type = FW_MAYBE_ISP;	
		printf("Firmware id packet error !\n");
		return 0;
	}

	board_info.firmware_id = fw_id;
	board_info.hardware_id = get_unaligned_le32(buf + 5);
	board_info.serial_no = get_unaligned_le32(buf + 9);

	board_info.dev_type = check_firmware_id(pdev, board_info.firmware_id);

	if (board_info.dev_type & FW_WDT8790) {
		wh_w8790_parse_device_info(&board_info.dev_info.w8790_feature_devinfo, buf);

		memcpy(&pdev->board_info.dev_info.w8790_feature_devinfo, &board_info.dev_info.w8790_feature_devinfo,
			sizeof(W8790_DEV_INFO));
		if (wh_w8790_prepare_data(pdev, &board_info)){
			memcpy(pboard_info, &board_info, sizeof(BOARD_INFO));
                        return 1;

		}
	}



	if (board_info.dev_type & (FW_WDT8760_2 | FW_WDT8760_2_ISP)) {
		wh_w8760_get_feature_devinfo(&board_info.dev_info.w8760_feature_devinfo, buf);	
		
		memcpy(&pdev->board_info.dev_info.w8760_feature_devinfo, &board_info.dev_info.w8760_feature_devinfo, 
			sizeof(W8760_REPORT_FEATURE_DEVINFO));

		if (wh_w8760_prepare_data(pdev, &board_info)) {			
			memcpy(pboard_info, &board_info, sizeof(BOARD_INFO));
			return 1;
		}
	}

	buf[0] = 0xf4;
	if (!wh_i2c_get_feature(pdev, buf, 56)) 
		printf("failed to get i2c cfg\n");
	else {
		if (buf[0] != 0xf4)
			wh_printf("wrong id[0xf4] of fw response: 0x%x\n", buf[0]);
		else
			board_info.i2c_dummy = buf[1];
	}

	if (buf[0] == 0xf4 && (get_unaligned_le16(buf + 2) == 0x154f)) { 

		board_info.dev_type |= FW_WDT8755;
		board_info.platform_id[1] = buf[5];

		memcpy(&board_info.sys_param, &buf[10], get_unaligned_le16(buf + 12));
		if (wh_w8755_prepare_data(pdev, &board_info, 0)) {
			memcpy(pboard_info, &board_info, sizeof(BOARD_INFO));
			return 1;
		}
	}

	if (!wh_i2c_get_desc(pdev, GD_DEVICE, 0, (BYTE*) buf, 18)) {
		board_info.dev_type = FW_MAYBE_ISP;
		printf("Get device desc error !\n");
		return 0;	
	}



	ret_size = wh_i2c_get_desc(pdev, GD_STRING, STRIDX_PARAMETERS, (BYTE*) buf, 38);
	if (!ret_size)	{
		printf("Get parameters error !\n");
		return 0;
	}

	memset((void*) &board_info.sys_param, 0, sizeof(SYS_PARAM));
	memcpy((void*) &board_info.sys_param, buf, ret_size);	
	memcpy(pboard_info, &board_info, sizeof(BOARD_INFO));
	
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
movl $0x50, %edx
callq 0x2110
movl $0x0, -0x168(%rbp)
movl $0x0, -0x16c(%rbp)
movl $0x3, -0x170(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0xc40a
movq -0x10(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0xc40a
cmpq $0x0, -0x18(%rbp)
jne 0xc424
leaq 0x4666(%rip), %rdi      # 0x10a77
movb $0x0, %al
callq 0x24e0
movl $0x0, -0x4(%rbp)
jmp 0xca00
movq -0x10(%rbp), %rax
movzbl 0x1d6(%rax), %esi
leaq 0x4657(%rip), %rdi      # 0x10a8d
movb $0x0, %al
callq 0x24e0
leaq -0x160(%rbp), %rdi
xorl %esi, %esi
movl $0xf0, %edx
callq 0x2110
movq -0x10(%rbp), %rax
movb 0x1d6(%rax), %al
movb %al, -0x112(%rbp)
movq -0x10(%rbp), %rax
movl $0x2, 0x278(%rax)
movq -0x10(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0xc180
movl %eax, -0x174(%rbp)
cmpl $0x0, -0x174(%rbp)
jne 0xc49b
leaq 0x460e(%rip), %rdi      # 0x10aa2
movb $0x0, %al
callq 0x24e0
movb $-0xe, -0x70(%rbp)
cmpl $0x0, -0x170(%rbp)
je 0xc4b9
movb $0x1, %al
cmpl $0x0, -0x168(%rbp)
movb %al, -0x17d(%rbp)
jle 0xc4c9
cmpl $0x0, -0x16c(%rbp)
sete %al
movb %al, -0x17d(%rbp)
movb -0x17d(%rbp), %al
testb $0x1, %al
jne 0xc4d8
jmp 0xc69e
movq -0x10(%rbp), %rdi
leaq -0x70(%rbp), %rsi
movl $0x40, %edx
callq 0xca10
movl %eax, -0x168(%rbp)
leaq -0x70(%rbp), %rdi
addq $0x1, %rdi
callq 0x9170
movl %eax, -0x16c(%rbp)
movl -0x170(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x170(%rbp)
cmpl $0x0, -0x168(%rbp)
jle 0xc528
cmpl $0x0, -0x16c(%rbp)
jne 0xc699
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
leaq 0x4584(%rip), %rdi      # 0x10abd
movb $0x0, %al
callq 0x24e0
movzbl -0x4a(%rbp), %eax
cmpl $0x49, %eax
jne 0xc674
movzbl -0x49(%rbp), %eax
cmpl $0x53, %eax
jne 0xc674
movzbl -0x48(%rbp), %eax
cmpl $0x50, %eax
jne 0xc674
leaq -0x17c(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x2110
leaq -0x17c(%rbp), %rsi
movl $0x2, %edi
callq 0x6570
leaq -0x70(%rbp), %rax
addq $0x18, %rax
leaq -0x17c(%rbp), %rcx
movq (%rcx), %rcx
movq (%rax), %rax
subq %rcx, %rax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0xc5c1
movl $0x5000, -0x160(%rbp)   # imm = 0x5000
movq -0x10(%rbp), %rdi
callq 0x65e0
leaq -0x17c(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x2110
leaq -0x17c(%rbp), %rsi
movl $0x1, %edi
callq 0x6570
leaq -0x70(%rbp), %rax
addq $0x18, %rax
leaq -0x17c(%rbp), %rcx
movq (%rcx), %rcx
movq (%rax), %rax
subq %rcx, %rax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0xc61b
movl $0x5000, -0x160(%rbp)   # imm = 0x5000
movq -0x10(%rbp), %rdi
callq 0x65e0
leaq -0x17c(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x2110
leaq -0x17c(%rbp), %rsi
xorl %edi, %edi
callq 0x6570
leaq -0x70(%rbp), %rax
addq $0x18, %rax
leaq -0x17c(%rbp), %rcx
movq (%rcx), %rcx
movq (%rax), %rax
subq %rcx, %rax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0xc672
movl $0x5000, -0x160(%rbp)   # imm = 0x5000
movq -0x10(%rbp), %rdi
callq 0x6640
jmp 0xc674
cmpl $0x0, -0x170(%rbp)
jne 0xc697
leaq 0x4439(%rip), %rdi      # 0x10abd
movb $0x0, %al
callq 0x2050
movl $0x0, -0x4(%rbp)
jmp 0xca00
jmp 0xc699
jmp 0xc49f
movzbl -0x70(%rbp), %eax
cmpl $0xf2, %eax
je 0xc6cd
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
leaq 0x442d(%rip), %rdi      # 0x10ae7
movb $0x0, %al
callq 0x2050
movl $0x0, -0x4(%rbp)
jmp 0xca00
movl -0x16c(%rbp), %eax
movl %eax, -0x150(%rbp)
leaq -0x70(%rbp), %rdi
addq $0x5, %rdi
callq 0x9170
movl %eax, -0x14c(%rbp)
leaq -0x70(%rbp), %rdi
addq $0x9, %rdi
callq 0x9170
movl %eax, -0x148(%rbp)
movq -0x10(%rbp), %rdi
movl -0x150(%rbp), %esi
callq 0x8ff0
movl %eax, -0x160(%rbp)
movl -0x160(%rbp), %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
je 0xc794
leaq -0x160(%rbp), %rdi
addq $0x70, %rdi
leaq -0x70(%rbp), %rsi
callq 0x6900
movq -0x10(%rbp), %rdi
addq $0x188, %rdi            # imm = 0x188
addq $0x70, %rdi
leaq -0x160(%rbp), %rsi
addq $0x70, %rsi
movl $0x34, %edx
callq 0x2150
movq -0x10(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x8b40
cmpl $0x0, %eax
je 0xc792
movq -0x18(%rbp), %rdi
leaq -0x160(%rbp), %rsi
movl $0xf0, %edx
callq 0x2150
movl $0x1, -0x4(%rbp)
jmp 0xca00
jmp 0xc794
movl -0x160(%rbp), %eax
andl $0x7800, %eax           # imm = 0x7800
cmpl $0x0, %eax
je 0xc814
leaq -0x160(%rbp), %rdi
addq $0x70, %rdi
leaq -0x70(%rbp), %rsi
callq 0x4780
movq -0x10(%rbp), %rdi
addq $0x188, %rdi            # imm = 0x188
addq $0x70, %rdi
leaq -0x160(%rbp), %rsi
addq $0x70, %rsi
movl $0x39, %edx
callq 0x2150
movq -0x10(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x5ee0
cmpl $0x0, %eax
je 0xc812
movq -0x18(%rbp), %rdi
leaq -0x160(%rbp), %rsi
movl $0xf0, %edx
callq 0x2150
movl $0x1, -0x4(%rbp)
jmp 0xca00
jmp 0xc814
movb $-0xc, -0x70(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x70(%rbp), %rsi
movl $0x38, %edx
callq 0xca10
cmpl $0x0, %eax
jne 0xc83f
leaq 0x42cd(%rip), %rdi      # 0x10b03
movb $0x0, %al
callq 0x2050
jmp 0xc86a
movzbl -0x70(%rbp), %eax
cmpl $0xf4, %eax
je 0xc85e
movzbl -0x70(%rbp), %esi
leaq 0x42c5(%rip), %rdi      # 0x10b1a
movb $0x0, %al
callq 0x24e0
jmp 0xc868
movzbl -0x6f(%rbp), %eax
movl %eax, -0x154(%rbp)
jmp 0xc86a
movzbl -0x70(%rbp), %eax
cmpl $0xf4, %eax
jne 0xc92c
leaq -0x70(%rbp), %rdi
addq $0x2, %rdi
callq 0x9140
movzwl %ax, %eax
cmpl $0x154f, %eax           # imm = 0x154F
jne 0xc92c
movl -0x160(%rbp), %eax
orl $0x20, %eax
movl %eax, -0x160(%rbp)
movb -0x6b(%rbp), %al
movb %al, -0x11d(%rbp)
leaq -0x160(%rbp), %rax
addq $0x1c, %rax
movq %rax, -0x190(%rbp)
leaq -0x70(%rbp), %rax
addq $0xa, %rax
movq %rax, -0x188(%rbp)
leaq -0x70(%rbp), %rdi
addq $0xc, %rdi
callq 0x9140
movq -0x190(%rbp), %rdi
movq -0x188(%rbp), %rsi
movzwl %ax, %eax
movl %eax, %edx
callq 0x2150
movq -0x10(%rbp), %rdi
leaq -0x160(%rbp), %rsi
xorl %edx, %edx
callq 0x46b0
cmpl $0x0, %eax
je 0xc92a
movq -0x18(%rbp), %rdi
leaq -0x160(%rbp), %rsi
movl $0xf0, %edx
callq 0x2150
movl $0x1, -0x4(%rbp)
jmp 0xca00
jmp 0xc92c
movq -0x10(%rbp), %rdi
leaq -0x70(%rbp), %rcx
movl $0x1, %esi
xorl %edx, %edx
movl $0x12, %r8d
callq 0xcce0
cmpl $0x0, %eax
jne 0xc96f
movl $0x1, -0x160(%rbp)
leaq 0x41e3(%rip), %rdi      # 0x10b3f
movb $0x0, %al
callq 0x2050
movl $0x0, -0x4(%rbp)
jmp 0xca00
movq -0x10(%rbp), %rdi
leaq -0x70(%rbp), %rcx
movl $0x3, %esi
movl $0x81, %edx
movl $0x26, %r8d
callq 0xcce0
movl %eax, -0x164(%rbp)
cmpl $0x0, -0x164(%rbp)
jne 0xc9b2
leaq 0x41b6(%rip), %rdi      # 0x10b58
movb $0x0, %al
callq 0x2050
movl $0x0, -0x4(%rbp)
jmp 0xca00
leaq -0x160(%rbp), %rdi
addq $0x1c, %rdi
xorl %esi, %esi
movl $0x26, %edx
callq 0x2110
leaq -0x160(%rbp), %rdi
addq $0x1c, %rdi
leaq -0x70(%rbp), %rsi
movslq -0x164(%rbp), %rdx
callq 0x2150
movq -0x18(%rbp), %rdi
leaq -0x160(%rbp), %rsi
movl $0xf0, %edx
callq 0x2150
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
nopl (%rax)