// File: /workspace/github_cpp_makefile_star2_O2/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, %r12d
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movl %eax, %r13d
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x88(%rax)
testl %r12d, %r12d
je 0xb5df
movl %eax, %ebp
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
leaq 0xa(%rsp), %rdx
pushq $0x3
popq %rcx
movl $0x8050, %esi           # imm = 0x8050
callq *0x20(%rax)
leaq 0x14de1(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xb4ed
movq 0x14afc(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
movzbl 0xa(%rsp), %r8d
movzbl 0xb(%rsp), %r9d
movzbl 0xc(%rsp), %eax
movl %eax, (%rsp)
leaq 0xb1c4(%rip), %rsi      # 0x1669e
leaq 0xb1b2(%rip), %rdx      # 0x16693
movl $0x18d, %ecx            # imm = 0x18D
xorl %eax, %eax
callq 0x5180
movb 0xb(%rsp), %al
addb 0xa(%rsp), %al
addb 0xc(%rsp), %al
je 0xb50b
leaq 0x14d8f(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
je 0xb9d2
movq %rbx, 0x10(%rsp)
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
movq %rax, %r14
leaq 0x14d6d(%rip), %rax     # 0x20291
movb (%rax), %al
testq %r14, %r14
je 0xb5e3
testb %al, %al
je 0xb55a
movq 0x14a7e(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xafcb(%rip), %rsi      # 0x1650f
leaq 0xb148(%rip), %rdx      # 0x16693
movl $0x19c, %ecx            # imm = 0x19C
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
xorl %eax, %eax
testl %r12d, %r12d
cmovlel %eax, %r12d
subl $0x1, %r12d
jb 0xb60c
movl %r13d, %eax
movzbl (%r14,%rax), %ebx
leaq 0x14d15(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xb5a8
movq 0x14a30(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xafaf(%rip), %rsi      # 0x16541
leaq 0xb0fa(%rip), %rdx      # 0x16693
movl $0x19f, %ecx            # imm = 0x19F
movl %ebx, %r8d
xorl %eax, %eax
callq 0x5180
leal 0x1(%r13), %eax
movzbl (%r14,%rax), %eax
shll $0x8, %eax
leal 0x2(%r13), %ecx
movzbl (%r14,%rcx), %r15d
orl %eax, %r15d
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
cmpb %bl, %al
je 0xb620
addl %r15d, %ebp
addl $0x3, %r13d
jmp 0xb563
pushq $-0x5
jmp 0xb60e
pushq $-0x4
popq %rbp
testb %al, %al
je 0xb60f
movq 0x149e7(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xacc4(%rip), %rsi      # 0x162bf
leaq 0xb091(%rip), %rdx      # 0x16693
movl $0x196, %ecx            # imm = 0x196
jmp 0xb7d2
pushq $-0x1
popq %rbp
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x14c6a(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
movl %ebp, %r13d
jne 0xb674
movzbl (%r14,%r13), %ebx
movq 0x1497d(%rip), %rax     # 0x1ffb8
movq (%rax), %r12
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movzbl %al, %r8d
leaq 0xb087(%rip), %rsi      # 0x166e2
leaq 0xb031(%rip), %rdx      # 0x16693
movq %r12, %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 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
pushq $0x3
popq %rcx
movl $0x8040, %esi           # imm = 0x8040
callq *0x30(%rax)
testl %eax, %eax
js 0xb7a3
movq %r13, 0x18(%rsp)
movl $0x3d090, %edi          # imm = 0x3D090
callq 0x5170
pushq $-0x6
popq %r12
pushq $0x1
popq %rbp
leaq 0xafd5(%rip), %r13      # 0x16693
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
movl $0x8040, %esi           # imm = 0x8040
leaq 0x20(%rsp), %rdx
movl %ebp, %ecx
callq *0x20(%rax)
movl %eax, %ebx
leaq 0x14baf(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xb707
movq 0x148ca(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xbace(%rip), %rsi      # 0x171c6
movq %r13, %rdx
movl $0x1c0, %ecx            # imm = 0x1C0
xorl %eax, %eax
callq 0x5180
testl %ebx, %ebx
jns 0xb717
leaq 0x14b7f(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
je 0xb775
movzbl 0x20(%rsp), %r9d
leaq 0x14b6d(%rip), %rax     # 0x20291
movb (%rax), %al
cmpl $0x82, %r9d
je 0xb7de
testb %al, %al
je 0xb75d
movq 0x1487a(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xb0b9(%rip), %rsi      # 0x16801
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 0xb6be
jmp 0xb95c
movq 0x1485c(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xaa87(%rip), %rsi      # 0x1620d
movq %r13, %rdx
movl $0x1c2, %ecx            # imm = 0x1C2
movl $0x8040, %r8d           # imm = 0x8040
movl %ebx, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0xb717
movl %eax, %ebp
leaq 0x14ae5(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xb60f
movq 0x1481c(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xafe4(%rip), %rsi      # 0x167aa
leaq 0xaec6(%rip), %rdx      # 0x16693
movl $0x1b7, %ecx            # imm = 0x1B7
xorl %eax, %eax
callq 0x5180
jmp 0xb60f
testb %al, %al
je 0xb806
movq 0x147cf(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xb9fc(%rip), %rsi      # 0x171ef
leaq 0xae99(%rip), %rdx      # 0x16693
movl $0x1d0, %ecx            # imm = 0x1D0
xorl %eax, %eax
callq 0x5180
movq 0x10(%rsp), %rbx
movq 0x8(%rbx), %rdi
addq 0x18(%rsp), %r14
movq (%rdi), %rax
movl $0x8050, %esi           # imm = 0x8050
movq %r14, %rdx
movl %r15d, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0xb994
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x5170
leaq 0x20(%rsp), %rdx
movb $-0x7d, (%rdx)
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
pushq $0x1
popq %rcx
movl $0x8040, %esi           # imm = 0x8040
callq *0x30(%rax)
testl %eax, %eax
js 0xb9fb
movl $0x13880, %edi          # imm = 0x13880
callq 0x5170
pushq $-0x6
popq %r15
leaq 0x20(%rsp), %r14
pushq $0x1
popq %rbp
leaq 0xae1d(%rip), %r12      # 0x16693
leaq 0xb9ca(%rip), %r13      # 0x17247
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
movl $0x8040, %esi           # imm = 0x8040
movq %r14, %rdx
movl %ebp, %ecx
callq *0x20(%rax)
movl %eax, %ebx
leaq 0x149f2(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xb8c4
movq 0x1470d(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xb969(%rip), %rsi      # 0x1721e
movq %r12, %rdx
movl $0x1e7, %ecx            # imm = 0x1E7
xorl %eax, %eax
callq 0x5180
testl %ebx, %ebx
jns 0xb8d4
leaq 0x149c2(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
je 0xb92e
movzbl 0x20(%rsp), %r9d
leaq 0x149b0(%rip), %rax     # 0x20291
movb (%rax), %al
cmpl $0xff, %r9d
je 0xba2f
testb %al, %al
je 0xb916
movq 0x146bd(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
movq %r13, %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 %r15d
jne 0xb87d
jmp 0xbb00
movq 0x146a3(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xa8ce(%rip), %rsi      # 0x1620d
movq %r12, %rdx
movl $0x1e9, %ecx            # imm = 0x1E9
movl $0x8040, %r8d           # imm = 0x8040
movl %ebx, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0xb8d4
pushq $-0x2
popq %rbp
leaq 0x1492b(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xb60f
movq 0x14662(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xaeb5(%rip), %rsi      # 0x16835
leaq 0xad0c(%rip), %rdx      # 0x16693
movl $0x1cc, %ecx            # imm = 0x1CC
movl $0x8040, %r8d           # imm = 0x8040
jmp 0xb9c6
movl %eax, %ebp
leaq 0x148f4(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xb60f
movq 0x1462b(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xaed2(%rip), %rsi      # 0x16889
leaq 0xacd5(%rip), %rdx      # 0x16693
movl $0x1d5, %ecx            # imm = 0x1D5
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0xb60f
movq 0x145ff(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xb7b3(%rip), %rsi      # 0x17196
leaq 0xaca9(%rip), %rdx      # 0x16693
movl $0x191, %ecx            # imm = 0x191
xorl %eax, %eax
callq 0x5180
jmp 0xb50b
movl %eax, %ebp
leaq 0x1488d(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xb60f
movq 0x145c4(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xae9f(%rip), %rsi      # 0x168bd
leaq 0xac6e(%rip), %rdx      # 0x16693
movl $0x1de, %ecx            # imm = 0x1DE
jmp 0xb7d2
testb %al, %al
je 0xba57
movq 0x1457e(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xb85a(%rip), %rsi      # 0x1729e
leaq 0xac48(%rip), %rdx      # 0x16693
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
pushq $0x3
popq %rcx
movl $0x8050, %esi           # imm = 0x8050
callq *0x20(%rax)
leaq 0x14817(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xbab7
movq 0x14532(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
movzbl 0xd(%rsp), %r8d
movzbl 0xe(%rsp), %r9d
movzbl 0xf(%rsp), %eax
movl %eax, (%rsp)
leaq 0xafca(%rip), %rsi      # 0x16a6e
leaq 0xabe8(%rip), %rdx      # 0x16693
movl $0x1fc, %ecx            # imm = 0x1FC
xorl %eax, %eax
callq 0x5180
movb 0xe(%rsp), %al
addb 0xd(%rsp), %al
xorl %ebp, %ebp
addb 0xf(%rsp), %al
je 0xb60f
pushq $-0x6
popq %rbp
leaq 0x147bc(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xb60f
movq 0x144f3(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xb7de(%rip), %rsi      # 0x172cd
leaq 0xab9d(%rip), %rdx      # 0x16693
movl $0x200, %ecx            # imm = 0x200
jmp 0xb7d2
pushq $-0x2
popq %rbp
leaq 0x14787(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xb60f
movq 0x144be(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xb757(%rip), %rsi      # 0x1727b
leaq 0xab68(%rip), %rdx      # 0x16693
movl $0x1f3, %ecx            # imm = 0x1F3
jmp 0xb98c
nopl (%rax)