// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movq %rdi, %r14
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x78(%rax)
movl %eax, %r12d
movq 0x10(%r14), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movl %eax, %r13d
movq 0x10(%r14), %rdi
movq (%rdi), %rax
callq *0x88(%rax)
testl %r12d, %r12d
je 0xcda7
movl %eax, %ebp
movq 0x8(%r14), %rdi
movq (%rdi), %rax
leaq 0xa(%rsp), %rdx
movl $0x8050, %esi           # imm = 0x8050
movl $0x3, %ecx
callq *0x20(%rax)
leaq 0x14647(%rip), %rbx     # 0x21291
cmpb $0x1, (%rbx)
jne 0xcc87
movq 0x14362(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movzbl 0xa(%rsp), %r8d
movzbl 0xb(%rsp), %r9d
movzbl 0xc(%rsp), %eax
movl %eax, (%rsp)
leaq 0xaa8b(%rip), %rsi      # 0x176ff
leaq 0xaa79(%rip), %rdx      # 0x176f4
movl $0x18d, %ecx            # imm = 0x18D
xorl %eax, %eax
callq 0x5180
movb 0xb(%rsp), %al
addb 0xa(%rsp), %al
addb 0xc(%rsp), %al
je 0xcc9e
cmpb $0x1, (%rbx)
je 0xd149
movq 0x10(%r14), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
movq %rax, %r15
movb (%rbx), %al
testq %r15, %r15
je 0xcdb1
movq %r14, 0x10(%rsp)
testb %al, %al
je 0xcce6
movq 0x142f2(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xa8a0(%rip), %rsi      # 0x17570
leaq 0xaa1d(%rip), %rdx      # 0x176f4
movl $0x19c, %ecx            # imm = 0x19C
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
movq %r15, 0x18(%rsp)
testl %r12d, %r12d
movl %r12d, %eax
setg %r12b
jle 0xcdc8
movl %eax, 0x24(%rsp)
negl %eax
movq %rax, 0x28(%rsp)
movl $0x1, %r14d
movl %r13d, %eax
movq 0x18(%rsp), %r15
movzbl (%r15,%rax), %ebx
leaq 0x14571(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xcd4c
movq 0x1428c(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xa86c(%rip), %rsi      # 0x175a2
leaq 0xa9b7(%rip), %rdx      # 0x176f4
movl $0x19f, %ecx            # imm = 0x19F
movl %ebx, %r8d
xorl %eax, %eax
callq 0x5180
leal 0x1(%r13), %eax
movzbl (%r15,%rax), %eax
shll $0x8, %eax
leal 0x2(%r13), %ecx
movzbl (%r15,%rcx), %r15d
orl %eax, %r15d
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
cmpb %bl, %al
je 0xcdca
addl %r15d, %ebp
addl $0x3, %r13d
cmpl 0x24(%rsp), %r14d
setl %r12b
movq 0x28(%rsp), %rax
addl %r14d, %eax
incl %eax
movl %r14d, %ecx
incl %ecx
movl %ecx, %r14d
cmpl $0x1, %eax
jne 0xcd0c
jmp 0xce21
movl $0xfffffffb, %ebx       # imm = 0xFFFFFFFB
jmp 0xd22c
movl $0xfffffffc, %ebx       # imm = 0xFFFFFFFC
testb %al, %al
je 0xd22c
callq 0x5ada
jmp 0xd22c
jmp 0xce21
leaq 0x144c0(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xce21
movl %ebp, %eax
movq 0x18(%rsp), %rcx
movzbl (%rcx,%rax), %ebx
movq 0x141d0(%rip), %rax     # 0x20fb8
movq (%rax), %r13
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movzbl %al, %r8d
leaq 0xa93b(%rip), %rsi      # 0x17743
leaq 0xa8e5(%rip), %rdx      # 0x176f4
movq %r13, %rdi
movl $0x1a9, %ecx            # imm = 0x1A9
movl %ebx, %r9d
xorl %eax, %eax
callq 0x5180
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testb $0x1, %r12b
je 0xd22c
leaq 0x30(%rsp), %rdx
movw $0x80, (%rdx)
movb $-0x80, 0x2(%rdx)
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
movl $0x8040, %esi           # imm = 0x8040
movl $0x3, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0xcf53
movl $0x3d090, %edi          # imm = 0x3D090
callq 0x5170
movl $0xfffffffa, %r12d      # imm = 0xFFFFFFFA
leaq 0xa87e(%rip), %r13      # 0x176f4
leaq 0x14414(%rip), %r14     # 0x21291
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
movl $0x8040, %esi           # imm = 0x8040
leaq 0x30(%rsp), %rdx
movl $0x1, %ecx
callq *0x20(%rax)
movl %eax, %ebx
cmpb $0x1, (%r14)
jne 0xcec3
movq 0x1410e(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xb373(%rip), %rsi      # 0x18227
movq %r13, %rdx
movl $0x1c0, %ecx            # imm = 0x1C0
xorl %eax, %eax
callq 0x5180
testl %ebx, %ebx
jns 0xcecd
cmpb $0x1, (%r14)
je 0xcf25
movzbl 0x30(%rsp), %r9d
movb (%r14), %al
cmpl $0x82, %r9d
je 0xcf6f
testb %al, %al
je 0xcf0d
movq 0x140ca(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xa96a(%rip), %rsi      # 0x17862
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 %r12d
jne 0xce7d
jmp 0xd0f8
movq 0x140ac(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xa338(%rip), %rsi      # 0x1726e
movq %r13, %rdx
movl $0x1c2, %ecx            # imm = 0x1C2
movl $0x8040, %r8d           # imm = 0x8040
movl %ebx, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0xcecd
movl %eax, %ebx
leaq 0x14335(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xd22c
callq 0x5ab0
jmp 0xd22c
testb %al, %al
je 0xcf97
movq 0x1403e(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xb2cc(%rip), %rsi      # 0x18250
leaq 0xa769(%rip), %rdx      # 0x176f4
movl $0x1d0, %ecx            # imm = 0x1D0
xorl %eax, %eax
callq 0x5180
movq 0x10(%rsp), %r12
movq 0x8(%r12), %rdi
movl %ebp, %eax
movq 0x18(%rsp), %rdx
addq %rax, %rdx
movq (%rdi), %rax
movl $0x8050, %esi           # imm = 0x8050
movl %r15d, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0xd111
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x5170
leaq 0x30(%rsp), %rdx
movb $-0x7d, (%rdx)
movq 0x8(%r12), %rdi
movq (%rdi), %rax
movl $0x8040, %esi           # imm = 0x8040
movl $0x1, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0xd153
movl $0x13880, %edi          # imm = 0x13880
callq 0x5170
movl $0xfffffffa, %r13d      # imm = 0xFFFFFFFA
leaq 0x30(%rsp), %r14
leaq 0xb273(%rip), %r15      # 0x1827f
leaq 0xa6e1(%rip), %r12      # 0x176f4
leaq 0xb28e(%rip), %rbp      # 0x182a8
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
movl $0x8040, %esi           # imm = 0x8040
movq %r14, %rdx
movl $0x1, %ecx
callq *0x20(%rax)
movl %eax, %ebx
leaq 0x14252(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xd060
movq 0x13f6d(%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 0xd070
leaq 0x14226(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
je 0xd0ca
movzbl 0x30(%rsp), %r9d
leaq 0x14214(%rip), %rax     # 0x21291
movb (%rax), %al
cmpl $0xff, %r9d
je 0xd169
testb %al, %al
je 0xd0b2
movq 0x13f21(%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 0xd01a
jmp 0xd21b
movq 0x13f07(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xa193(%rip), %rsi      # 0x1726e
movq %r12, %rdx
movl $0x1e9, %ecx            # imm = 0x1E9
movl $0x8040, %r8d           # imm = 0x8040
movl %ebx, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0xd070
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
cmpb $0x1, (%r14)
jne 0xd22c
callq 0x59fc
jmp 0xd22c
movl %eax, %ebx
cmpb $0x1, (%r14)
jne 0xd22c
movq 0x13eb4(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xa7bc(%rip), %rsi      # 0x178ea
leaq 0xa5bf(%rip), %rdx      # 0x176f4
movl $0x1d5, %ecx            # imm = 0x1D5
movl %ebx, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0xd22c
callq 0x59d2
jmp 0xcc9e
movl %eax, %ebx
cmpb $0x1, (%r14)
jne 0xd22c
callq 0x5a86
jmp 0xd22c
testb %al, %al
je 0xd191
movq 0x13e44(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xb181(%rip), %rsi      # 0x182ff
leaq 0xa56f(%rip), %rdx      # 0x176f4
movl $0x1f7, %ecx            # imm = 0x1F7
xorl %eax, %eax
callq 0x5180
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
leaq 0xd(%rsp), %rdx
movl $0x8050, %esi           # imm = 0x8050
movl $0x3, %ecx
callq *0x20(%rax)
leaq 0x140db(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xd1f3
movq 0x13df6(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movzbl 0xd(%rsp), %r8d
movzbl 0xe(%rsp), %r9d
movzbl 0xf(%rsp), %eax
movl %eax, (%rsp)
leaq 0xa8ef(%rip), %rsi      # 0x17acf
leaq 0xa50d(%rip), %rdx      # 0x176f4
movl $0x1fc, %ecx            # imm = 0x1FC
xorl %eax, %eax
callq 0x5180
movb 0xe(%rsp), %al
addb 0xd(%rsp), %al
xorl %ebx, %ebx
addb 0xf(%rsp), %al
je 0xd22c
movl $0xfffffffa, %ebx       # imm = 0xFFFFFFFA
leaq 0x14082(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xd22c
callq 0x5a5c
jmp 0xd22c
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
leaq 0x1406a(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
je 0xd23d
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x5a2c
jmp 0xd22c