// File: /workspace/github_cpp_makefile_star2_O1/goodix[P]gdix_hid_firmware_update/gtx8/gtx8_update.cpp
// Function: GTx8Update::cfg_update()
int GTx8Update::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(CFG_START_ADDR, 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]);

	/* 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) {
		// wait untill ic is free
		retry = 10;
		do {
			ret = dev->Read(CMD_ADDR, temp_buf, 1);
			if (ret < 0) {
				gdix_err("Failed read cfg cmd, ret = %d\n", ret);
				return ret;
			}
			if (temp_buf[0] == 0xff)
				break;
			gdix_info("0x%x value is 0x%x != 0xff, retry\n", CMD_ADDR,
					  temp_buf[0]);
			usleep(10000);
		} while (--retry);
		if (!retry) {
			gdix_err("Reg 0x%x != 0xff\n", CMD_ADDR);
			ret = -2;
			goto update_err;
		}

		// tell ic i want to send cfg
		temp_buf[0] = 0x80;
		temp_buf[1] = 0;
		temp_buf[2] = 0x80;
		ret = dev->Write(CMD_ADDR, 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(CMD_ADDR, temp_buf, 1);
			gdix_dbg("Wait CMD_ADDR == 0x82...\n");
			if (ret < 0) {
				gdix_err("Failed read 0x%x, ret = %d\n", CMD_ADDR, ret);
			}
			if (temp_buf[0] == 0x82)
				break;
			gdix_info("0x%x value is 0x%x != 0x82, retry\n", CMD_ADDR,
					  temp_buf[0]);
			usleep(30000);
		} while (--retry);

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

		/* Start load config */
		ret = dev->Write(CFG_START_ADDR, &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(CMD_ADDR, 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(CMD_ADDR, temp_buf, 1);
			gdix_dbg("Wait CMD_ADDR == 0xFF...\n");
			if (ret < 0) {
				gdix_err("Failed read 0x%x, ret = %d\n", CMD_ADDR, ret);
			}
			if (temp_buf[0] == 0xff)
				break;
			gdix_info("0x%x value is 0x%x != 0xFF, retry\n", CMD_ADDR,
					  temp_buf[0]);
			usleep(30000);
		} while (--retry);

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

		// before update config,read curr config version
		dev->Read(CFG_START_ADDR, 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]);

		return 0;
	}
update_err:
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdi, %rbx
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x78(%rax)
movl %eax, %r15d
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movl %eax, %r13d
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x88(%rax)
testl %r15d, %r15d
je 0xfcbe
movl %eax, %ebp
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
leaq 0xa(%rsp), %rdx
movl $0x60dc, %esi           # imm = 0x60DC
movl $0x3, %ecx
callq *0x20(%rax)
leaq 0x11721(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xfbad
movq 0x1143c(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movzbl 0xa(%rsp), %r8d
movzbl 0xb(%rsp), %r9d
movzbl 0xc(%rsp), %eax
movl %eax, (%rsp)
leaq 0x7b65(%rip), %rsi      # 0x176ff
leaq 0x7b53(%rip), %rdx      # 0x176f4
movl $0x1a7, %ecx            # imm = 0x1A7
xorl %eax, %eax
callq 0x5180
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
movq %rax, %r14
leaq 0x116d0(%rip), %rax     # 0x21291
movb (%rax), %al
testq %r14, %r14
je 0xfcc9
movq %rbx, 0x10(%rsp)
testb %al, %al
je 0xfbfc
movq 0x113dc(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x798a(%rip), %rsi      # 0x17570
leaq 0x7b07(%rip), %rdx      # 0x176f4
movl $0x1b2, %ecx            # imm = 0x1B2
movl %r15d, %r8d
xorl %eax, %eax
callq 0x5180
movq %r14, 0x18(%rsp)
testl %r15d, %r15d
setg %r14b
jle 0xfce1
movl %r15d, 0x24(%rsp)
movl %r15d, %eax
negl %eax
movq %rax, 0x28(%rsp)
movl $0x1, %r12d
movl %r13d, %eax
movq 0x18(%rsp), %r15
movzbl (%r15,%rax), %ebx
leaq 0x1165a(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xfc63
movq 0x11375(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x7955(%rip), %rsi      # 0x175a2
leaq 0x7aa0(%rip), %rdx      # 0x176f4
movl $0x1b5, %ecx            # imm = 0x1B5
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 0xfce3
addl %r15d, %ebp
addl $0x3, %r13d
cmpl 0x24(%rsp), %r12d
setl %r14b
movq 0x28(%rsp), %rax
addl %r12d, %eax
incl %eax
movl %r12d, %ecx
incl %ecx
movl %ecx, %r12d
cmpl $0x1, %eax
jne 0xfc23
jmp 0xfd3a
movl $0xfffffffb, %r12d      # imm = 0xFFFFFFFB
jmp 0x10210
movl $0xfffffffc, %r12d      # imm = 0xFFFFFFFC
testb %al, %al
je 0x10210
callq 0x5eb9
jmp 0x10210
jmp 0xfd3a
leaq 0x115a7(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xfd3a
movl %ebp, %eax
movq 0x18(%rsp), %rcx
movzbl (%rcx,%rax), %ebx
movq 0x112b7(%rip), %rax     # 0x20fb8
movq (%rax), %r12
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movzbl %al, %r8d
leaq 0x8c81(%rip), %rsi      # 0x189a2
leaq 0x79cc(%rip), %rdx      # 0x176f4
movq %r12, %rdi
movl $0x1bf, %ecx            # imm = 0x1BF
movl %ebx, %r9d
xorl %eax, %eax
callq 0x5180
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testb $0x1, %r14b
je 0x10210
movl $0xfffffff6, %ebx       # imm = 0xFFFFFFF6
leaq 0x30(%rsp), %r13
movq 0x10(%rsp), %r14
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movl $0x60cc, %esi           # imm = 0x60CC
movq %r13, %rdx
movl $0x1, %ecx
callq *0x20(%rax)
testl %eax, %eax
js 0xfdeb
movzbl 0x30(%rsp), %r9d
cmpl $0xff, %r9d
je 0xfe2a
leaq 0x11503(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xfdbd
movq 0x1121e(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x7a10(%rip), %rsi      # 0x177b4
leaq 0x7949(%rip), %rdx      # 0x176f4
movl $0x1d2, %ecx            # imm = 0x1D2
movl $0x60cc, %r8d           # imm = 0x60CC
xorl %eax, %eax
callq 0x5180
movl $0x2710, %edi           # imm = 0x2710
callq 0x5170
incl %ebx
jne 0xfd59
movl $0xfffffffe, %r12d      # imm = 0xFFFFFFFE
leaq 0x114b9(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x10210
callq 0x5dd5
jmp 0x10210
movl %eax, %r12d
leaq 0x1149c(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x10210
movq 0x111d3(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0x7975(%rip), %rsi      # 0x17784
leaq 0x78de(%rip), %rdx      # 0x176f4
movl $0x1cc, %ecx            # imm = 0x1CC
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x10210
leaq 0x30(%rsp), %rdx
movw $0x80, (%rdx)
movb $-0x80, 0x2(%rdx)
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movl $0x60cc, %esi           # imm = 0x60CC
movl $0x3, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0xff56
movl $0x3d090, %edi          # imm = 0x3D090
callq 0x5170
movl $0xfffffffa, %r14d      # imm = 0xFFFFFFFA
leaq 0x30(%rsp), %r12
leaq 0x7884(%rip), %r13      # 0x176f4
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
movl $0x60cc, %esi           # imm = 0x60CC
movq %r12, %rdx
movl $0x1, %ecx
callq *0x20(%rax)
movl %eax, %ebx
leaq 0x113fc(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xfeba
movq 0x11117(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x798c(%rip), %rsi      # 0x17837
movq %r13, %rdx
movl $0x1ea, %ecx            # imm = 0x1EA
xorl %eax, %eax
callq 0x5180
testl %ebx, %ebx
jns 0xfeca
leaq 0x113cc(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
je 0xff28
movzbl 0x30(%rsp), %r9d
leaq 0x113ba(%rip), %rax     # 0x21291
movb (%rax), %al
cmpl $0x82, %r9d
je 0xff73
testb %al, %al
je 0xff10
movq 0x110c7(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x7967(%rip), %rsi      # 0x17862
movq %r13, %rdx
movl $0x1f1, %ecx            # imm = 0x1F1
movl $0x60cc, %r8d           # imm = 0x60CC
xorl %eax, %eax
callq 0x5180
movl $0x7530, %edi           # imm = 0x7530
callq 0x5170
incl %r14d
jne 0xfe70
jmp 0x100fa
movq 0x110a9(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0x7335(%rip), %rsi      # 0x1726e
movq %r13, %rdx
movl $0x1ec, %ecx            # imm = 0x1EC
movl $0x60cc, %r8d           # imm = 0x60CC
movl %ebx, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0xfeca
movl %eax, %r12d
leaq 0x11331(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x10210
callq 0x5e8f
jmp 0x10210
testb %al, %al
je 0xff9b
movq 0x1103a(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x7931(%rip), %rsi      # 0x178b9
leaq 0x7765(%rip), %rdx      # 0x176f4
movl $0x1fa, %ecx            # imm = 0x1FA
xorl %eax, %eax
callq 0x5180
movq 0x10(%rsp), %rbx
movq 0x8(%rbx), %rdi
movl %ebp, %eax
movq 0x18(%rsp), %rdx
addq %rax, %rdx
movq (%rdi), %rax
movl $0x60dc, %esi           # imm = 0x60DC
movl %r15d, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0x1011a
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x5170
leaq 0x30(%rsp), %rdx
movb $-0x7d, (%rdx)
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x60cc, %esi           # imm = 0x60CC
movl $0x1, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0x1014f
movl $0x13880, %edi          # imm = 0x13880
callq 0x5170
movl $0xfffffffa, %r13d      # imm = 0xFFFFFFFA
leaq 0x30(%rsp), %r14
leaq 0x89d6(%rip), %r15      # 0x189e4
leaq 0x76df(%rip), %r12      # 0x176f4
leaq 0x828c(%rip), %rbp      # 0x182a8
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
movl $0x60cc, %esi           # imm = 0x60CC
movq %r14, %rdx
movl $0x1, %ecx
callq *0x20(%rax)
movl %eax, %ebx
leaq 0x11250(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x10062
movq 0x10f6b(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movq %r15, %rsi
movq %r12, %rdx
movl $0x211, %ecx            # imm = 0x211
xorl %eax, %eax
callq 0x5180
testl %ebx, %ebx
jns 0x10072
leaq 0x11224(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
je 0x100cc
movzbl 0x30(%rsp), %r9d
leaq 0x11212(%rip), %rax     # 0x21291
movb (%rax), %al
cmpl $0xff, %r9d
je 0x1016c
testb %al, %al
je 0x100b4
movq 0x10f1f(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movq %rbp, %rsi
movq %r12, %rdx
movl $0x218, %ecx            # imm = 0x218
movl $0x60cc, %r8d           # imm = 0x60CC
xorl %eax, %eax
callq 0x5180
movl $0x7530, %edi           # imm = 0x7530
callq 0x5170
incl %r13d
jne 0x1001c
jmp 0x101fe
movq 0x10f05(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0x7191(%rip), %rsi      # 0x1726e
movq %r12, %rdx
movl $0x213, %ecx            # imm = 0x213
movl $0x60cc, %r8d           # imm = 0x60CC
movl %ebx, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x10072
movl $0xfffffffe, %r12d      # imm = 0xFFFFFFFE
leaq 0x1118a(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x10210
callq 0x5e05
jmp 0x10210
movl %eax, %r12d
leaq 0x1116d(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x10210
movq 0x10ea4(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0x77ac(%rip), %rsi      # 0x178ea
leaq 0x75af(%rip), %rdx      # 0x176f4
movl $0x1ff, %ecx            # imm = 0x1FF
jmp 0xfe1b
movl %eax, %r12d
leaq 0x11138(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x10210
callq 0x5e65
jmp 0x10210
testb %al, %al
je 0x10194
movq 0x10e41(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x888e(%rip), %rsi      # 0x18a0f
leaq 0x756c(%rip), %rdx      # 0x176f4
movl $0x221, %ecx            # imm = 0x221
xorl %eax, %eax
callq 0x5180
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
leaq 0xd(%rsp), %rdx
movl $0x60dc, %esi           # imm = 0x60DC
movl $0x3, %ecx
callq *0x20(%rax)
xorl %r12d, %r12d
leaq 0x110d5(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x10210
movq 0x10df0(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movzbl 0xd(%rsp), %r8d
movzbl 0xe(%rsp), %r9d
movzbl 0xf(%rsp), %eax
movl %eax, (%rsp)
leaq 0x78e9(%rip), %rsi      # 0x17acf
leaq 0x7507(%rip), %rdx      # 0x176f4
xorl %r12d, %r12d
movl $0x226, %ecx            # imm = 0x226
xorl %eax, %eax
callq 0x5180
jmp 0x10210
movl $0xfffffffe, %r12d      # imm = 0xFFFFFFFE
leaq 0x11086(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
je 0x10222
movl %r12d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x5e35
jmp 0x10210
nopl (%rax)