// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %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 0x20(%rsp), %rdx
pushq $0x1
popq %rcx
movl $0x8050, %esi           # imm = 0x8050
callq *0x20(%rax)
leaq 0x164be(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0x9e02
movzbl 0x20(%rsp), %r8d
movq 0x161d3(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xcfd8(%rip), %rsi      # 0x16dc7
leaq 0xc89d(%rip), %rdx      # 0x16693
movl $0x168, %ecx            # imm = 0x168
xorl %eax, %eax
callq 0x5180
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
movq %rax, %rcx
leaq 0x1647b(%rip), %rax     # 0x20291
movb (%rax), %al
movq %rcx, 0x10(%rsp)
testq %rcx, %rcx
je 0x9ef9
testb %al, %al
je 0x9e51
movq 0x16187(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xc6d4(%rip), %rsi      # 0x1650f
leaq 0xc851(%rip), %rdx      # 0x16693
movl $0x173, %ecx            # imm = 0x173
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
testl %ebp, %ebp
movl %ebp, %eax
movl $0x0, %ebp
movl %eax, 0x1c(%rsp)
cmovgl %eax, %ebp
subl $0x1, %ebp
jb 0x9f26
movl %r15d, 0xc(%rsp)
movq %rbx, %r12
movl %r14d, %eax
movq 0x10(%rsp), %rcx
movzbl (%rcx,%rax), %r13d
leaq 0x1640b(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0x9eb2
movq 0x16126(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xc6a5(%rip), %rsi      # 0x16541
leaq 0xc7f0(%rip), %rdx      # 0x16693
movl $0x176, %ecx            # imm = 0x176
movl %r13d, %r8d
xorl %eax, %eax
callq 0x5180
leal 0x1(%r14), %eax
movq 0x10(%rsp), %rcx
movzbl (%rcx,%rax), %ebx
leal 0x2(%r14), %eax
movzbl (%rcx,%rax), %r15d
movq 0x8(%r12), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
cmpb %r13b, %al
je 0x9fd5
shll $0x8, %ebx
orl %r15d, %ebx
movl 0xc(%rsp), %r15d
addl %ebx, %r15d
addl $0x3, %r14d
movq %r12, %rbx
jmp 0x9e61
pushq $-0x4
popq %rbx
testb %al, %al
je 0xa0f3
movq 0x160cd(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xc3aa(%rip), %rsi      # 0x162bf
leaq 0xc777(%rip), %rdx      # 0x16693
movl $0x16d, %ecx            # imm = 0x16D
jmp 0x9fc9
cmpl $0x0, 0x1c(%rsp)
je 0xa0f0
xorl %r13d, %r13d
movl $0xf4240, %edi          # imm = 0xF4240
callq 0x5170
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
leaq 0x20(%rsp), %rbx
pushq $0x1
popq %rcx
movl $0x8050, %esi           # imm = 0x8050
movq %rbx, %rdx
callq *0x20(%rax)
movzbl (%rbx), %ebp
leaq 0x1632f(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0x9f8e
movq 0x1604a(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xcee4(%rip), %rsi      # 0x16e5c
leaq 0xc714(%rip), %rdx      # 0x16693
movl $0x1a1, %ecx            # imm = 0x1A1
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
xorl %ebx, %ebx
cmpb %r13b, %bpl
je 0xa0f3
pushq $-0x1
popq %rbx
leaq 0x162ee(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xa0f3
movq 0x16005(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xced0(%rip), %rsi      # 0x16e8d
leaq 0xc6cf(%rip), %rdx      # 0x16693
movl $0x1a5, %ecx            # imm = 0x1A5
xorl %eax, %eax
callq 0x5180
jmp 0xa0f3
movl 0xc(%rsp), %r15d
movq 0x10(%rsp), %rax
movb (%rax,%r15), %r13b
leaq 0x162a7(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
movq %r12, %rbx
jne 0xa031
movzbl %r13b, %ebp
movq 0x15fbb(%rip), %rax     # 0x1ffb8
movq (%rax), %r14
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movzbl %al, %r8d
leaq 0xc6ca(%rip), %rsi      # 0x166e2
leaq 0xc674(%rip), %rdx      # 0x16693
movq %r14, %rdi
movl $0x181, %ecx            # imm = 0x181
movl %ebp, %r9d
xorl %eax, %eax
callq 0x5180
movq 0x10(%rsp), %rax
leaq (%rax,%r15), %r14
addq %rax, %r15
addq $0xef, %r15
pushq $-0x4
popq %rbp
leaq 0xc645(%rip), %r12      # 0x16693
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 0xa0b9
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 0x9f34
leaq 0x161f8(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xa0c5
movq 0x15f33(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xcd7d(%rip), %rsi      # 0x16e2c
movq %r12, %rdx
movl $0x196, %ecx            # imm = 0x196
jmp 0xa0e7
leaq 0x161d1(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
je 0xa0ce
incl %ebp
jne 0xa04e
jmp 0x9f34
movq 0x15f03(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xcd1a(%rip), %rsi      # 0x16df9
movq %r12, %rdx
movl $0x190, %ecx            # imm = 0x190
xorl %eax, %eax
callq 0x5180
jmp 0xa0c5
pushq $-0x5
popq %rbx
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq