// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq %rdi, %r15
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x78(%rax)
movl %eax, %r13d
movq 0x10(%r15), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movl %eax, %ebx
movq 0x10(%r15), %rdi
movq (%rdi), %rax
callq *0x88(%rax)
testl %r13d, %r13d
je 0xfb6e
movl %eax, %ebp
movq 0x8(%r15), %rdi
movq (%rdi), %rax
leaq 0x12(%rsp), %rdx
movl $0x60dc, %esi           # imm = 0x60DC
movl $0x3, %ecx
callq *0x20(%rax)
leaq 0x11838(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xfa96
movq 0x11553(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movzbl 0x12(%rsp), %r8d
movzbl 0x13(%rsp), %r9d
movzbl 0x14(%rsp), %eax
movl %eax, (%rsp)
leaq 0x7c1b(%rip), %rsi      # 0x1769e
leaq 0x7c09(%rip), %rdx      # 0x17693
movl $0x1a7, %ecx            # imm = 0x1A7
xorl %eax, %eax
callq 0x5180
movq 0x10(%r15), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
movq %rax, %r14
leaq 0x117e7(%rip), %rax     # 0x21291
movb (%rax), %al
testq %r14, %r14
je 0xfb76
testb %al, %al
je 0xfae0
movq 0x114f8(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x7a45(%rip), %rsi      # 0x1750f
leaq 0x7bc2(%rip), %rdx      # 0x17693
movl $0x1b2, %ecx            # imm = 0x1B2
movl %r13d, %r8d
xorl %eax, %eax
callq 0x5180
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
testl %r13d, %r13d
jle 0xfb84
movq %r15, 0x18(%rsp)
movl %ebx, %eax
movzbl (%r14,%rax), %r12d
leaq 0x1178f(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xfb2e
movq 0x114aa(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x7a29(%rip), %rsi      # 0x17541
leaq 0x7b74(%rip), %rdx      # 0x17693
movl $0x1b5, %ecx            # imm = 0x1B5
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
leal 0x1(%rbx), %eax
movzbl (%r14,%rax), %eax
shll $0x8, %eax
leal 0x2(%rbx), %ecx
movzbl (%r14,%rcx), %r15d
orl %eax, %r15d
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
cmpb %r12b, %al
je 0xfb96
addl %r15d, %ebp
addl $0x3, %ebx
decl %r13d
jne 0xfaf4
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
jmp 0xfb84
movl $0xfffffffb, %r8d       # imm = 0xFFFFFFFB
jmp 0xfb84
movl $0xfffffffc, %r8d       # imm = 0xFFFFFFFC
testb %al, %al
jne 0xfe21
movl %r8d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x116f4(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xfbe9
movl %ebp, %eax
movzbl (%r14,%rax), %ebx
movq 0x11408(%rip), %rax     # 0x20fb8
movq (%rax), %r12
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movzbl %al, %r8d
leaq 0x8d98(%rip), %rsi      # 0x18968
leaq 0x7abc(%rip), %rdx      # 0x17693
movq %r12, %rdi
movl $0x1bf, %ecx            # imm = 0x1BF
movl %ebx, %r9d
xorl %eax, %eax
callq 0x5180
movl $0xfffffff6, %r12d      # imm = 0xFFFFFFF6
leaq 0x20(%rsp), %rbx
leaq 0x7a98(%rip), %r13      # 0x17693
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
movl $0x60cc, %esi           # imm = 0x60CC
movq %rbx, %rdx
movl $0x1, %ecx
callq *0x20(%rax)
testl %eax, %eax
js 0xfc8f
movzbl 0x20(%rsp), %r9d
cmpl $0xff, %r9d
je 0xfcce
leaq 0x1165c(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xfc60
movq 0x11377(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x7b08(%rip), %rsi      # 0x17753
movq %r13, %rdx
movl $0x1d2, %ecx            # imm = 0x1D2
movl $0x60cc, %r8d           # imm = 0x60CC
xorl %eax, %eax
callq 0x5180
movl $0x2710, %edi           # imm = 0x2710
callq 0x5170
incl %r12d
jne 0xfbfb
movl $0xfffffffe, %r8d       # imm = 0xFFFFFFFE
leaq 0x11615(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xfb84
callq 0x5d81
jmp 0xffd1
movl %eax, %r8d
leaq 0x115f8(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xfb84
movq 0x1132f(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0x7a70(%rip), %rsi      # 0x17723
leaq 0x79d9(%rip), %rdx      # 0x17693
movl $0x1cc, %ecx            # imm = 0x1CC
movl %r8d, %ebx
xorl %eax, %eax
callq 0x5180
jmp 0xfe19
leaq 0x20(%rsp), %rdx
movw $0x80, (%rdx)
movb $-0x80, 0x2(%rdx)
movq 0x18(%rsp), %r12
movq 0x8(%r12), %rdi
movq (%rdi), %rax
movl $0x60cc, %esi           # imm = 0x60CC
movl $0x3, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0xfdfe
movl $0x3d090, %edi          # imm = 0x3D090
callq 0x5170
movl $0xfffffffa, %ebx       # imm = 0xFFFFFFFA
leaq 0x797f(%rip), %r13      # 0x17693
movq 0x8(%r12), %rdi
movq (%rdi), %rax
movl $0x60cc, %esi           # imm = 0x60CC
leaq 0x20(%rsp), %rdx
movl $0x1, %ecx
callq *0x20(%rax)
movl %eax, %r12d
leaq 0x11559(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xfd5d
movq 0x11274(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x7a88(%rip), %rsi      # 0x177d6
movq %r13, %rdx
movl $0x1ea, %ecx            # imm = 0x1EA
xorl %eax, %eax
callq 0x5180
testl %r12d, %r12d
jns 0xfd6e
leaq 0x11528(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
je 0xfdd0
movzbl 0x20(%rsp), %r9d
leaq 0x11516(%rip), %rax     # 0x21291
movb (%rax), %al
cmpl $0x82, %r9d
je 0xfe31
testb %al, %al
movq 0x18(%rsp), %r12
je 0xfdb9
movq 0x1121e(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x7a5d(%rip), %rsi      # 0x17801
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 %ebx
jne 0xfd14
jmp 0xffb6
movq 0x11201(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0x742c(%rip), %rsi      # 0x1720d
movq %r13, %rdx
movl $0x1ec, %ecx            # imm = 0x1EC
movl $0x60cc, %r8d           # imm = 0x60CC
movl %r12d, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0xfd6e
movl %eax, %r8d
leaq 0x11489(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xfb84
movl %r8d, %ebx
callq 0x5e3b
movl %ebx, %r8d
jmp 0xfb84
callq 0x5e65
movl $0xfffffffc, %r8d       # imm = 0xFFFFFFFC
jmp 0xfb84
testb %al, %al
movq 0x18(%rsp), %rbx
je 0xfe5e
movq 0x11177(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x7a0d(%rip), %rsi      # 0x17858
leaq 0x7841(%rip), %rdx      # 0x17693
movl $0x1fa, %ecx            # imm = 0x1FA
xorl %eax, %eax
callq 0x5180
movq 0x8(%rbx), %rdi
movl %ebp, %eax
addq %rax, %r14
movq (%rdi), %rax
movl $0x60dc, %esi           # imm = 0x60DC
movq %r14, %rdx
movl %r15d, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0xffdc
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x5170
leaq 0x20(%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 0x10011
movl $0x13880, %edi          # imm = 0x13880
callq 0x5170
movl $0xfffffffa, %r12d      # imm = 0xFFFFFFFA
leaq 0x20(%rsp), %rbx
leaq 0x8ae0(%rip), %r14      # 0x189aa
leaq 0x77c2(%rip), %r15      # 0x17693
leaq 0x836f(%rip), %r13      # 0x18247
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
movl $0x60cc, %esi           # imm = 0x60CC
movq %rbx, %rdx
movl $0x1, %ecx
callq *0x20(%rax)
movl %eax, %ebp
leaq 0x11394(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xff1e
movq 0x110af(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movq %r14, %rsi
movq %r15, %rdx
movl $0x211, %ecx            # imm = 0x211
xorl %eax, %eax
callq 0x5180
testl %ebp, %ebp
jns 0xff2e
leaq 0x11368(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
je 0xff88
movzbl 0x20(%rsp), %r9d
leaq 0x11356(%rip), %rax     # 0x21291
movb (%rax), %al
cmpl $0xff, %r9d
je 0x10031
testb %al, %al
je 0xff70
movq 0x11063(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movq %r13, %rsi
movq %r15, %rdx
movl $0x218, %ecx            # imm = 0x218
movl $0x60cc, %r8d           # imm = 0x60CC
xorl %eax, %eax
callq 0x5180
movl $0x7530, %edi           # imm = 0x7530
callq 0x5170
incl %r12d
jne 0xfed8
jmp 0x100ca
movq 0x11049(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0x7274(%rip), %rsi      # 0x1720d
movq %r15, %rdx
movl $0x213, %ecx            # imm = 0x213
movl $0x60cc, %r8d           # imm = 0x60CC
movl %ebp, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0xff2e
movl $0xfffffffe, %r8d       # imm = 0xFFFFFFFE
leaq 0x112ce(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xfb84
callq 0x5db1
movl $0xfffffffe, %r8d       # imm = 0xFFFFFFFE
jmp 0xfb84
movl %eax, %r8d
leaq 0x112ab(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xfb84
movq 0x10fe2(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0x7889(%rip), %rsi      # 0x17889
leaq 0x768c(%rip), %rdx      # 0x17693
movl $0x1ff, %ecx            # imm = 0x1FF
jmp 0xfcbf
movl %eax, %r8d
leaq 0x11276(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xfb84
movl %r8d, %ebx
callq 0x5e11
jmp 0xfe19
testb %al, %al
je 0x10059
movq 0x10f7c(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x898f(%rip), %rsi      # 0x189d5
leaq 0x7646(%rip), %rdx      # 0x17693
movl $0x221, %ecx            # imm = 0x221
xorl %eax, %eax
callq 0x5180
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
leaq 0x15(%rsp), %rdx
movl $0x60dc, %esi           # imm = 0x60DC
movl $0x3, %ecx
callq *0x20(%rax)
xorl %r8d, %r8d
leaq 0x11210(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xfb84
movq 0x10f27(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movzbl 0x15(%rsp), %r8d
movzbl 0x16(%rsp), %r9d
movzbl 0x17(%rsp), %eax
movl %eax, (%rsp)
leaq 0x79bf(%rip), %rsi      # 0x17a6e
leaq 0x75dd(%rip), %rdx      # 0x17693
movl $0x226, %ecx            # imm = 0x226
xorl %eax, %eax
callq 0x5180
xorl %r8d, %r8d
jmp 0xfb84
movl $0xfffffffe, %r8d       # imm = 0xFFFFFFFE
leaq 0x111ba(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xfb84
callq 0x5de1
jmp 0xffd1
nop