// File: /workspace/github_cpp_makefile_star2_O3/goodix[P]gdix_hid_firmware_update/gtx3/gtx3_update.cpp
// Function: GTx3Update::cfg_update()
int GTx3Update::cfg_update()
{
	int retry;
	int ret = -1, i;
	unsigned char temp_buf[65];
	unsigned char *fw_data = NULL;
	unsigned char cfg_ver_after[3];
	unsigned char cfg_ver_before[3];
	unsigned char cfg_ver_infile;
	bool findMatchCfg = false;
	unsigned char *cfg = NULL;

	int sub_cfg_num = image->GetConfigSubCfgNum();
	unsigned char sub_cfg_id;
	unsigned int sub_cfg_len;
	unsigned int sub_cfg_info_pos = image->GetConfigSubCfgInfoOffset();
	unsigned int cfg_offset = image->GetConfigSubCfgDataOffset();

	if (sub_cfg_num == 0)
		return -5;

	// before update config,read curr config version
	dev->Read(0x8050, cfg_ver_before, 3);
	gdix_dbg("Before update,cfg version is 0x%02x 0x%02x 0x%02x\n",
			 cfg_ver_before[0], cfg_ver_before[1], cfg_ver_before[2]);
	unsigned char cks =
		cfg_ver_before[0] + cfg_ver_before[1] + cfg_ver_before[2];
	if (cks != 0)
		gdix_err("Warning : cfg before cks err!\n");

	/* Start load config */
	fw_data = image->GetFirmwareData();
	if (!fw_data) {
		gdix_err("No valid fw data \n");
		ret = -4;
		goto update_err;
	}

	// fw_data[FW_IMAGE_SUB_FWNUM_OFFSET];
	gdix_dbg("load sub config, sub_cfg_num=%d\n", sub_cfg_num);
	for (i = 0; i < sub_cfg_num; i++) {
		sub_cfg_id = fw_data[sub_cfg_info_pos];
		gdix_dbg("load sub config, sub_cfg_id=0x%x\n", sub_cfg_id);

		sub_cfg_len = (fw_data[sub_cfg_info_pos + 1] << 8) |
					  fw_data[sub_cfg_info_pos + 2];

		if (dev->GetSensorID() == sub_cfg_id) {
			findMatchCfg = true;
			cfg = &fw_data[cfg_offset];
			cfg_ver_infile = cfg[0];
			gdix_info("Find a cfg match sensorID:ID=%d,cfg version=%d\n",
					  dev->GetSensorID(), cfg_ver_infile);
			break;
		}
		cfg_offset += sub_cfg_len;
		sub_cfg_info_pos += 3;
	}

	if (findMatchCfg) {
		// tell ic i want to send cfg
		temp_buf[0] = 0x80;
		temp_buf[1] = 0;
		temp_buf[2] = 0x80;
		ret = dev->Write(0x8040, temp_buf, 3);
		if (ret < 0) {
			gdix_err("Failed write send cfg cmd\n");
			return ret;
		}

		// wait ic to comfirm
		usleep(250000);
		retry = GDIX_RETRY_TIMES;
		do {
			ret = dev->Read(0x8040, temp_buf, 1);
			gdix_dbg("Wait 0x8040 == 0x82...\n");
			if (ret < 0) {
				gdix_err("Failed read 0x%x, ret = %d\n", 0x8040, ret);
			}
			if (temp_buf[0] == 0x82)
				break;
			gdix_info("0x%x value is 0x%x != 0x82, retry\n", 0x8040,
					  temp_buf[0]);
			usleep(30000);
		} while (--retry);

		if (!retry) {
			gdix_err("Reg 0x%x != 0x82\n", 0x8040);
			ret = -2;
			goto update_err;
		}
		gdix_dbg("Wait 0x8040 == 0x82 success.\n");

		/* Start load config */
		ret = dev->Write(0x8050, &fw_data[cfg_offset], sub_cfg_len);
		if (ret < 0) {
			gdix_err("Failed write cfg to xdata, ret=%d\n", ret);
			goto update_err;
		}
		usleep(100000);

		// tell ic cfg is ready in xdata
		temp_buf[0] = 0x83;
		ret = dev->Write(0x8040, temp_buf, 1);
		if (ret < 0) {
			gdix_err("Failed write send cfg finish cmd\n");
			return ret;
		}

		// check if ic is ok with the cfg
		usleep(80000);
		retry = GDIX_RETRY_TIMES;
		do {
			ret = dev->Read(0x8040, temp_buf, 1);
			gdix_dbg("Wait 0x8040 == 0xFF...\n");
			if (ret < 0) {
				gdix_err("Failed read 0x%x, ret = %d\n", 0x8040, ret);
			}
			if (temp_buf[0] == 0xff)
				break;
			gdix_info("0x%x value is 0x%x != 0xFF, retry\n", 0x8040,
					  temp_buf[0]);
			usleep(30000);
		} while (--retry);

		if (!retry) {
			gdix_err("Reg 0x%x != 0xFF\n", 0x8040);
			ret = -2;
			goto update_err;
		}
		gdix_dbg("Wait 0x8040 == 0xFF success.\n");

		// before update config,read curr config version
		dev->Read(0x8050, cfg_ver_after, 3);
		gdix_dbg("After update,cfg version is 0x%02x 0x%02x 0x%02x\n",
				 cfg_ver_after[0], cfg_ver_after[1], cfg_ver_after[2]);
		unsigned char cks =
			cfg_ver_after[0] + cfg_ver_after[1] + cfg_ver_after[2];
		if (cks != 0) {
			gdix_err("Error : cfg after cks err!\n");
			return -6;
		}

		return 0;
	}
update_err:
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %rbx
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x78(%rax)
movl %eax, %r13d
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movl %eax, %r15d
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x88(%rax)
testl %r13d, %r13d
je 0xcc9a
movl %eax, %ebp
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
leaq 0x12(%rsp), %rdx
movl $0x8050, %esi           # imm = 0x8050
movl $0x3, %ecx
callq *0x20(%rax)
leaq 0x1472d(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xcba1
movq 0x14448(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movzbl 0x12(%rsp), %r8d
movzbl 0x13(%rsp), %r9d
movzbl 0x14(%rsp), %eax
movl %eax, (%rsp)
leaq 0xab10(%rip), %rsi      # 0x1769e
leaq 0xaafe(%rip), %rdx      # 0x17693
movl $0x18d, %ecx            # imm = 0x18D
xorl %eax, %eax
callq 0x5180
movb 0x13(%rsp), %al
addb 0x12(%rsp), %al
addb 0x14(%rsp), %al
je 0xcbbf
leaq 0x146db(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
je 0xd00e
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
movq %rax, %r14
leaq 0x146be(%rip), %rax     # 0x21291
movb (%rax), %al
testq %r14, %r14
je 0xcca2
testb %al, %al
je 0xcc09
movq 0x143cf(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xa91c(%rip), %rsi      # 0x1750f
leaq 0xaa99(%rip), %rdx      # 0x17693
movl $0x19c, %ecx            # imm = 0x19C
movl %r13d, %r8d
xorl %eax, %eax
callq 0x5180
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
testl %r13d, %r13d
jle 0xccb0
movq %rbx, 0x18(%rsp)
movl %r15d, %eax
movzbl (%r14,%rax), %ebx
leaq 0x14665(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xcc58
movq 0x14380(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xa8ff(%rip), %rsi      # 0x17541
leaq 0xaa4a(%rip), %rdx      # 0x17693
movl $0x19f, %ecx            # imm = 0x19F
movl %ebx, %r8d
xorl %eax, %eax
callq 0x5180
leal 0x1(%r15), %eax
movzbl (%r14,%rax), %eax
shll $0x8, %eax
leal 0x2(%r15), %ecx
movzbl (%r14,%rcx), %r12d
orl %eax, %r12d
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
cmpb %bl, %al
je 0xccc2
addl %r12d, %ebp
addl $0x3, %r15d
decl %r13d
jne 0xcc1d
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
jmp 0xccb0
movl $0xfffffffb, %r8d       # imm = 0xFFFFFFFB
jmp 0xccb0
movl $0xfffffffc, %r8d       # imm = 0xFFFFFFFC
testb %al, %al
jne 0xd054
movl %r8d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x145c8(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xcd15
movl %ebp, %eax
movzbl (%r14,%rax), %ebx
movq 0x142dc(%rip), %rax     # 0x20fb8
movq (%rax), %r15
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movzbl %al, %r8d
leaq 0xa9e6(%rip), %rsi      # 0x176e2
leaq 0xa990(%rip), %rdx      # 0x17693
movq %r15, %rdi
movl $0x1a9, %ecx            # imm = 0x1A9
movl %ebx, %r9d
xorl %eax, %eax
callq 0x5180
leaq 0x20(%rsp), %rdx
movw $0x80, (%rdx)
movb $-0x80, 0x2(%rdx)
movq 0x18(%rsp), %rbx
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x8040, %esi           # imm = 0x8040
movl $0x3, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0xce43
movl $0x3d090, %edi          # imm = 0x3D090
callq 0x5170
movl $0xfffffffa, %r15d      # imm = 0xFFFFFFFA
leaq 0xa938(%rip), %r13      # 0x17693
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x8040, %esi           # imm = 0x8040
leaq 0x20(%rsp), %rdx
movl $0x1, %ecx
callq *0x20(%rax)
movl %eax, %ebx
leaq 0x14514(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xcda2
movq 0x1422f(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xb433(%rip), %rsi      # 0x181c6
movq %r13, %rdx
movl $0x1c0, %ecx            # imm = 0x1C0
xorl %eax, %eax
callq 0x5180
testl %ebx, %ebx
jns 0xcdb2
leaq 0x144e4(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
je 0xce15
movzbl 0x20(%rsp), %r9d
leaq 0x144d2(%rip), %rax     # 0x21291
movb (%rax), %al
cmpl $0x82, %r9d
je 0xce63
testb %al, %al
movq 0x18(%rsp), %rbx
je 0xcdfd
movq 0x141da(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xaa19(%rip), %rsi      # 0x17801
movq %r13, %rdx
movl $0x1c7, %ecx            # imm = 0x1C7
movl $0x8040, %r8d           # imm = 0x8040
xorl %eax, %eax
callq 0x5180
movl $0x7530, %edi           # imm = 0x7530
callq 0x5170
incl %r15d
jne 0xcd5b
jmp 0xcfe8
movq 0x141bc(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xa3e7(%rip), %rsi      # 0x1720d
movq %r13, %rdx
movl $0x1c2, %ecx            # imm = 0x1C2
movl $0x8040, %r8d           # imm = 0x8040
movl %ebx, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0xcdb2
movl %eax, %r8d
leaq 0x14444(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xccb0
movl %r8d, %ebx
callq 0x5a5c
jmp 0xd07f
testb %al, %al
movq 0x18(%rsp), %rbx
je 0xce90
movq 0x14145(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xb372(%rip), %rsi      # 0x181ef
leaq 0xa80f(%rip), %rdx      # 0x17693
movl $0x1d0, %ecx            # imm = 0x1D0
xorl %eax, %eax
callq 0x5180
movq 0x8(%rbx), %rdi
movl %ebp, %eax
addq %rax, %r14
movq (%rdi), %rax
movl $0x8050, %esi           # imm = 0x8050
movq %r14, %rdx
movl %r12d, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0xd018
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x5170
leaq 0x20(%rsp), %rdx
movb $-0x7d, (%rdx)
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x8040, %esi           # imm = 0x8040
movl $0x1, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0xd064
movl $0x13880, %edi          # imm = 0x13880
callq 0x5170
movl $0xfffffffa, %r13d      # imm = 0xFFFFFFFA
leaq 0x20(%rsp), %r14
leaq 0xb322(%rip), %r15      # 0x1821e
leaq 0xa790(%rip), %r12      # 0x17693
leaq 0xb33d(%rip), %rbp      # 0x18247
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x8040, %esi           # imm = 0x8040
movq %r14, %rdx
movl $0x1, %ecx
callq *0x20(%rax)
movl %eax, %ebx
leaq 0x14367(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xcf4b
movq 0x14082(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movq %r15, %rsi
movq %r12, %rdx
movl $0x1e7, %ecx            # imm = 0x1E7
xorl %eax, %eax
callq 0x5180
testl %ebx, %ebx
jns 0xcf5b
leaq 0x1433b(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
je 0xcfba
movzbl 0x20(%rsp), %r9d
leaq 0x14329(%rip), %rax     # 0x21291
movb (%rax), %al
cmpl $0xff, %r9d
je 0xd087
testb %al, %al
movq 0x18(%rsp), %rbx
je 0xcfa2
movq 0x14031(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movq %rbp, %rsi
movq %r12, %rdx
movl $0x1ee, %ecx            # imm = 0x1EE
movl $0x8040, %r8d           # imm = 0x8040
xorl %eax, %eax
callq 0x5180
movl $0x7530, %edi           # imm = 0x7530
callq 0x5170
incl %r13d
jne 0xcf0a
jmp 0xd14c
movq 0x14017(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xa242(%rip), %rsi      # 0x1720d
movq %r12, %rdx
movl $0x1e9, %ecx            # imm = 0x1E9
movl $0x8040, %r8d           # imm = 0x8040
movl %ebx, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0xcf5b
movl $0xfffffffe, %r8d       # imm = 0xFFFFFFFE
leaq 0x1429c(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xccb0
callq 0x59a8
movl $0xfffffffe, %r8d       # imm = 0xFFFFFFFE
jmp 0xccb0
callq 0x597e
jmp 0xcbbf
movl %eax, %r8d
leaq 0x1426f(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xccb0
movq 0x13fa6(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xa84d(%rip), %rsi      # 0x17889
leaq 0xa650(%rip), %rdx      # 0x17693
movl $0x1d5, %ecx            # imm = 0x1D5
movl %r8d, %ebx
xorl %eax, %eax
callq 0x5180
jmp 0xd07f
callq 0x5a86
movl $0xfffffffc, %r8d       # imm = 0xFFFFFFFC
jmp 0xccb0
movl %eax, %r8d
leaq 0x14223(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xccb0
movl %r8d, %ebx
callq 0x5a32
movl %ebx, %r8d
jmp 0xccb0
testb %al, %al
movq 0x18(%rsp), %rbx
je 0xd0b4
movq 0x13f21(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xb1fd(%rip), %rsi      # 0x1829e
leaq 0xa5eb(%rip), %rdx      # 0x17693
movl $0x1f7, %ecx            # imm = 0x1F7
xorl %eax, %eax
callq 0x5180
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
leaq 0x15(%rsp), %rdx
movl $0x8050, %esi           # imm = 0x8050
movl $0x3, %ecx
callq *0x20(%rax)
leaq 0x141bd(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xd111
movq 0x13ed8(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movzbl 0x15(%rsp), %r8d
movzbl 0x16(%rsp), %r9d
movzbl 0x17(%rsp), %eax
movl %eax, (%rsp)
leaq 0xa970(%rip), %rsi      # 0x17a6e
leaq 0xa58e(%rip), %rdx      # 0x17693
movl $0x1fc, %ecx            # imm = 0x1FC
xorl %eax, %eax
callq 0x5180
movb 0x16(%rsp), %al
addb 0x15(%rsp), %al
xorl %r8d, %r8d
addb 0x17(%rsp), %al
je 0xccb0
movl $0xfffffffa, %r8d       # imm = 0xFFFFFFFA
leaq 0x1415e(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xccb0
callq 0x5a08
movl $0xfffffffa, %r8d       # imm = 0xFFFFFFFA
jmp 0xccb0
movl $0xfffffffe, %r8d       # imm = 0xFFFFFFFE
leaq 0x14138(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xccb0
callq 0x59d8
jmp 0xd003