// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movq %rdi, %rbp
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x78(%rax)
movl %eax, %r12d
movq 0x10(%rbp), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movl %eax, %r15d
movq 0x10(%rbp), %rdi
movq (%rdi), %rax
callq *0x88(%rax)
movl %eax, %r13d
movq 0x8(%rbp), %rdi
movq (%rdi), %rax
leaq 0x30(%rsp), %rdx
movl $0x8050, %esi           # imm = 0x8050
movl $0x1, %ecx
callq *0x20(%rax)
leaq 0x15c4d(%rip), %r14     # 0x21291
cmpb $0x1, (%r14)
jne 0xb674
movzbl 0x30(%rsp), %r8d
movq 0x15961(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc7c7(%rip), %rsi      # 0x17e28
leaq 0xc08c(%rip), %rdx      # 0x176f4
movl $0x168, %ecx            # imm = 0x168
xorl %eax, %eax
callq 0x5180
movq 0x10(%rbp), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
movq %rax, %rbx
movb (%r14), %al
testq %rbx, %rbx
je 0xb79d
testb %al, %al
je 0xb6b8
movq 0x15920(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xbece(%rip), %rsi      # 0x17570
leaq 0xc04b(%rip), %rdx      # 0x176f4
movl $0x173, %ecx            # imm = 0x173
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
testl %r12d, %r12d
setg %dl
movl %r12d, 0x14(%rsp)
jle 0xb7b4
movl %r13d, 0x10(%rsp)
movq %rbp, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %rbx, %rbp
movl %r12d, %eax
negl %eax
movq %rax, 0x28(%rsp)
movl $0x1, %ebx
movb %dl, 0xf(%rsp)
movl %r15d, %eax
movzbl (%rbp,%rax), %r13d
cmpb $0x1, (%r14)
jne 0xb724
movq 0x158b4(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xbe94(%rip), %rsi      # 0x175a2
leaq 0xbfdf(%rip), %rdx      # 0x176f4
movl $0x176, %ecx            # imm = 0x176
movl %r13d, %r8d
xorl %eax, %eax
callq 0x5180
leal 0x1(%r15), %eax
movzbl (%rbp,%rax), %r14d
leal 0x2(%r15), %eax
movq %rbp, %r12
movzbl (%rbp,%rax), %ebp
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
cmpb %r13b, %al
je 0xb7c2
shll $0x8, %r14d
orl %ebp, %r14d
addl %r14d, 0x10(%rsp)
addl $0x3, %r15d
cmpl 0x14(%rsp), %ebx
setl %dl
movq 0x28(%rsp), %rax
addl %ebx, %eax
incl %eax
movl %ebx, %ecx
incl %ecx
movl %ecx, %ebx
cmpl $0x1, %eax
leaq 0x15b10(%rip), %r14     # 0x21291
movq %r12, %rbp
jne 0xb6ea
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %r15d, %r15d
movq 0x18(%rsp), %rbp
jmp 0xb845
movl $0xfffffffc, %ebx       # imm = 0xFFFFFFFC
testb %al, %al
je 0xb94d
callq 0x586b
jmp 0xb94d
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %r15d, %r15d
jmp 0xb845
movl 0x10(%rsp), %eax
movq 0x20(%rsp), %r13
leaq (%rax,%r13), %r15
addq $0xef, %r15
addq %rax, %r13
movb (%r12,%rax), %r12b
leaq 0x15aad(%rip), %r14     # 0x21291
cmpb $0x1, (%r14)
jne 0xb83c
movzbl %r12b, %eax
movl %eax, 0x10(%rsp)
movq %r13, %rbx
movq 0x157bc(%rip), %rax     # 0x20fb8
movq (%rax), %r13
movq 0x18(%rsp), %rbp
movq 0x8(%rbp), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movzbl %al, %r8d
leaq 0xbf27(%rip), %rsi      # 0x17743
leaq 0xbed1(%rip), %rdx      # 0x176f4
movq %r13, %rdi
movq %rbx, %r13
movl $0x181, %ecx            # imm = 0x181
movl 0x10(%rsp), %r9d
xorl %eax, %eax
callq 0x5180
jmp 0xb841
movq 0x18(%rsp), %rbp
movb 0xf(%rsp), %dl
cmpl $0x0, 0x14(%rsp)
je 0xb948
testb $0x1, %dl
je 0xb8ba
movl $0xfffffffc, %ebx       # imm = 0xFFFFFFFC
movl $0x1388, %edi           # imm = 0x1388
callq 0x5170
movq 0x8(%rbp), %rdi
movq (%rdi), %rax
movl $0xbf7b, %esi           # imm = 0xBF7B
movq %r15, %rdx
movl $0x80, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0xb8a7
movq 0x8(%rbp), %rdi
movq (%rdi), %rax
movl $0x8050, %esi           # imm = 0x8050
movq %r13, %rdx
movl $0xf0, %ecx
callq *0x30(%rax)
testl %eax, %eax
jns 0xb8ba
cmpb $0x1, (%r14)
jne 0xb8ad
callq 0x5817
jmp 0xb8ad
cmpb $0x1, (%r14)
je 0xb8b3
incl %ebx
jne 0xb85a
jmp 0xb8ba
callq 0x5841
jmp 0xb8ad
movl $0xf4240, %edi          # imm = 0xF4240
callq 0x5170
movq 0x8(%rbp), %rdi
movq (%rdi), %rax
leaq 0x30(%rsp), %rbx
movl $0x8050, %esi           # imm = 0x8050
movq %rbx, %rdx
movl $0x1, %ecx
callq *0x20(%rax)
movzbl (%rbx), %ebp
cmpb $0x1, (%r14)
jne 0xb910
movq 0x156c8(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc5c3(%rip), %rsi      # 0x17ebd
leaq 0xbdf3(%rip), %rdx      # 0x176f4
movl $0x1a1, %ecx            # imm = 0x1A1
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
xorl %ebx, %ebx
cmpb %r12b, %bpl
je 0xb94d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpb $0x1, (%r14)
jne 0xb94d
movq 0x1568f(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc5bb(%rip), %rsi      # 0x17eee
leaq 0xbdba(%rip), %rdx      # 0x176f4
movl $0x1a5, %ecx            # imm = 0x1A5
xorl %eax, %eax
callq 0x5180
jmp 0xb94d
movl $0xfffffffb, %ebx       # imm = 0xFFFFFFFB
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop