// File: /workspace/github_cpp_makefile_star2_O3/goodix[P]gdix_hid_firmware_update/gtx2/gtx2_update.cpp
// Function: GTx2Update::cfg_update()
int GTx2Update::cfg_update()
{
	int retry;
	int ret, i;
	unsigned char temp_buf[65];
	unsigned char *fw_data = NULL;
	unsigned char cfg_ver_after;
	unsigned char cfg_ver_before;
	unsigned char cfg_ver_infile = 0;
	bool findMatchCfg = false;
	unsigned char *cfg0x8050 = NULL; // 2 frame of config in memory
	unsigned char *cfg0xBF7B = 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();

	// before update config,read curr config version
	dev->Read(0x8050, temp_buf, 1);
	cfg_ver_before = temp_buf[0];
	gdix_dbg("Before update,cfg version is %d\n", cfg_ver_before);

	/* 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;
			cfg0x8050 = &fw_data[cfg_offset];
			cfg0xBF7B = &cfg0x8050[0x813f - 0x8050];
			cfg_ver_infile = cfg0x8050[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 (sub_cfg_num == 0)
		return -5;
	if (findMatchCfg) {
		retry = 3;
		do {
			usleep(5000);
			// start download cfg
			ret = dev->Write(0xBF7B, cfg0xBF7B, 0xBFFA - 0xBF7B + 1);
			if (ret < 0) {
				gdix_err("Failed to Write cfg to cfg0xBF7B\n");
				continue;
			}
			ret = dev->Write(0x8050, cfg0x8050,
							 0x813F - 0x8050 + 1); // cfg start addr = 0x8050
			if (ret < 0) {
				gdix_err("Failed to Write cfg to 0x8050\n");
				continue;
			}
			break;

		} while (retry-- > 0);
	}
	usleep(1000000);

	dev->Read(0x8050, temp_buf, 1);
	cfg_ver_after = temp_buf[0];
	gdix_dbg("After update,cfg version is %d\n", cfg_ver_after);

	if (cfg_ver_after != cfg_ver_infile) {
		gdix_dbg(
			"After update,cfg version is no equal to cg version in file.\n");
		ret = -1;
		goto update_err;
	}
	return 0;

update_err:
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x78(%rax)
movl %eax, %ebp
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movl %eax, %r14d
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x88(%rax)
movl %eax, %r15d
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
leaq 0x10(%rsp), %rdx
movl $0x8050, %esi           # imm = 0x8050
movl $0x1, %ecx
callq *0x20(%rax)
leaq 0x15cd4(%rip), %r12     # 0x21291
cmpb $0x1, (%r12)
jne 0xb5ee
movzbl 0x10(%rsp), %r8d
movq 0x159e7(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc7ec(%rip), %rsi      # 0x17dc7
leaq 0xc0b1(%rip), %rdx      # 0x17693
movl $0x168, %ecx            # imm = 0x168
xorl %eax, %eax
callq 0x5180
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
movb (%r12), %cl
movq %rax, (%rsp)
testq %rax, %rax
je 0xb6d1
testb %cl, %cl
je 0xb634
movq 0x159a4(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xbef1(%rip), %rsi      # 0x1750f
leaq 0xc06e(%rip), %rdx      # 0x17693
movl $0x173, %ecx            # imm = 0x173
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
testl %ebp, %ebp
jle 0xb6e8
movq %rbx, 0x8(%rsp)
movl %r14d, %eax
movq (%rsp), %rcx
movzbl (%rcx,%rax), %r13d
cmpb $0x1, (%r12)
jne 0xb67b
movq 0x1595d(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xbedc(%rip), %rsi      # 0x17541
leaq 0xc027(%rip), %rdx      # 0x17693
movl $0x176, %ecx            # imm = 0x176
movl %r13d, %r8d
xorl %eax, %eax
callq 0x5180
leal 0x1(%r14), %eax
movq (%rsp), %rcx
movzbl (%rcx,%rax), %ebx
leal 0x2(%r14), %eax
movzbl (%rcx,%rax), %r12d
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
cmpb %r13b, %al
je 0xb78c
shll $0x8, %ebx
orl %r12d, %ebx
addl %ebx, %r15d
addl $0x3, %r14d
decl %ebp
leaq 0x15bd0(%rip), %r12     # 0x21291
jne 0xb641
xorl %r13d, %r13d
movq 0x8(%rsp), %rbx
jmp 0xb6f1
movl $0xfffffffc, %ebx       # imm = 0xFFFFFFFC
testb %cl, %cl
je 0xb86e
callq 0x5817
jmp 0xb86e
je 0xb869
xorl %r13d, %r13d
movl $0xf4240, %edi          # imm = 0xF4240
callq 0x5170
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
leaq 0x10(%rsp), %rbx
movl $0x8050, %esi           # imm = 0x8050
movq %rbx, %rdx
movl $0x1, %ecx
callq *0x20(%rax)
movzbl (%rbx), %ebp
cmpb $0x1, (%r12)
jne 0xb748
movq 0x15890(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc72a(%rip), %rsi      # 0x17e5c
leaq 0xbf5a(%rip), %rdx      # 0x17693
movl $0x1a1, %ecx            # imm = 0x1A1
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
xorl %ebx, %ebx
cmpb %r13b, %bpl
je 0xb86e
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpb $0x1, (%r12)
jne 0xb86e
movq 0x1584e(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc719(%rip), %rsi      # 0x17e8d
leaq 0xbf18(%rip), %rdx      # 0x17693
movl $0x1a5, %ecx            # imm = 0x1A5
xorl %eax, %eax
callq 0x5180
jmp 0xb86e
movl %r15d, %r15d
movq (%rsp), %rax
movb (%rax,%r15), %r13b
leaq 0x15af3(%rip), %r12     # 0x21291
cmpb $0x1, (%r12)
movq 0x8(%rsp), %rbx
jne 0xb7e9
movzbl %r13b, %ebp
movq 0x15803(%rip), %rax     # 0x20fb8
movq (%rax), %r14
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movzbl %al, %r8d
leaq 0xbf12(%rip), %rsi      # 0x176e2
leaq 0xbebc(%rip), %rdx      # 0x17693
movq %r14, %rdi
movl $0x181, %ecx            # imm = 0x181
movl %ebp, %r9d
xorl %eax, %eax
callq 0x5180
movq (%rsp), %rax
leaq (%rax,%r15), %r14
addq %rax, %r15
addq $0xef, %r15
movl $0xfffffffc, %ebp       # imm = 0xFFFFFFFC
movl $0x1388, %edi           # imm = 0x1388
callq 0x5170
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0xbf7b, %esi           # imm = 0xBF7B
movq %r15, %rdx
movl $0x80, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0xb852
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x8050, %esi           # imm = 0x8050
movq %r14, %rdx
movl $0xf0, %ecx
callq *0x30(%rax)
testl %eax, %eax
jns 0xb6f1
cmpb $0x1, (%r12)
jne 0xb859
callq 0x57c3
jmp 0xb859
cmpb $0x1, (%r12)
je 0xb862
incl %ebp
jne 0xb800
jmp 0xb6f1
callq 0x57ed
jmp 0xb859
movl $0xfffffffb, %ebx       # imm = 0xFFFFFFFB
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop