// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq $0x0, -0x68(%rbp)
movb $0x0, -0x6b(%rbp)
movb $0x0, -0x6c(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq 0x10(%rax), %rdi
movq (%rdi), %rax
callq *0x78(%rax)
movl %eax, %ecx
movq -0xa0(%rbp), %rax
movl %ecx, -0x84(%rbp)
movq 0x10(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movl %eax, %ecx
movq -0xa0(%rbp), %rax
movl %ecx, -0x90(%rbp)
movq 0x10(%rax), %rdi
movq (%rdi), %rax
callq *0x88(%rax)
movl %eax, %ecx
movq -0xa0(%rbp), %rax
movl %ecx, -0x94(%rbp)
movq 0x8(%rax), %rdi
leaq -0x60(%rbp), %rdx
movq (%rdi), %rax
movl $0x8050, %esi           # imm = 0x8050
movl $0x1, %ecx
callq *0x20(%rax)
movb -0x60(%rbp), %al
movb %al, -0x6a(%rbp)
leaq 0x1a019(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xc2ae
movq 0x19d2c(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movzbl -0x6a(%rbp), %r8d
leaq 0x11b91(%rip), %rsi     # 0x1de2c
leaq 0x11447(%rip), %rdx     # 0x1d6e9
movl $0x168, %ecx            # imm = 0x168
movb $0x0, %al
callq 0x5190
jmp 0xc2b0
movq -0xa0(%rbp), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0xc30c
jmp 0xc2ce
leaq 0x19fc4(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xc2fe
movq 0x19cf7(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0x1102a(%rip), %rsi     # 0x1d315
leaq 0x113f7(%rip), %rdx     # 0x1d6e9
movl $0x16d, %ecx            # imm = 0x16D
movb $0x0, %al
callq 0x5190
jmp 0xc300
movl $0xfffffffc, -0x18(%rbp) # imm = 0xFFFFFFFC
jmp 0xc677
jmp 0xc30e
leaq 0x19f84(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xc345
movq 0x19c97(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movl -0x84(%rbp), %r8d
leaq 0x11233(%rip), %rsi     # 0x1d565
leaq 0x113b0(%rip), %rdx     # 0x1d6e9
movl $0x173, %ecx            # imm = 0x173
movb $0x0, %al
callq 0x5190
jmp 0xc347
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x84(%rbp), %eax
jge 0xc4bb
movq -0x68(%rbp), %rax
movl -0x90(%rbp), %ecx
movb (%rax,%rcx), %al
movb %al, -0x85(%rbp)
leaq 0x19f22(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xc3a8
movq 0x19c35(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movzbl -0x85(%rbp), %r8d
leaq 0x11202(%rip), %rsi     # 0x1d597
leaq 0x1134d(%rip), %rdx     # 0x1d6e9
movl $0x176, %ecx            # imm = 0x176
movb $0x0, %al
callq 0x5190
jmp 0xc3aa
movq -0xa0(%rbp), %rax
movq -0x68(%rbp), %rcx
movl -0x90(%rbp), %edx
addl $0x1, %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
shll $0x8, %ecx
movq -0x68(%rbp), %rdx
movl -0x90(%rbp), %esi
addl $0x2, %esi
movl %esi, %esi
movzbl (%rdx,%rsi), %edx
orl %edx, %ecx
movl %ecx, -0x8c(%rbp)
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movzbl %al, %eax
movzbl -0x85(%rbp), %ecx
cmpl %ecx, %eax
jne 0xc48c
movb $0x1, -0x6c(%rbp)
movq -0x68(%rbp), %rax
movl -0x94(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
addq $0xef, %rax
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rax
movb (%rax), %al
movb %al, -0x6b(%rbp)
leaq 0x19e65(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xc488
movq -0xa0(%rbp), %rax
movq 0x19b71(%rip), %rcx     # 0x25fb8
movq (%rcx), %rcx
movq %rcx, -0xa8(%rbp)
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movq -0xa8(%rbp), %rdi
movzbl %al, %r8d
movzbl -0x6b(%rbp), %r9d
leaq 0x112c3(%rip), %rsi     # 0x1d738
leaq 0x1126d(%rip), %rdx     # 0x1d6e9
movl $0x181, %ecx            # imm = 0x181
movb $0x0, %al
callq 0x5190
jmp 0xc48a
jmp 0xc4bb
movl -0x8c(%rbp), %eax
addl -0x94(%rbp), %eax
movl %eax, -0x94(%rbp)
movl -0x90(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x90(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xc34e
cmpl $0x0, -0x84(%rbp)
jne 0xc4d0
movl $0xfffffffb, -0x4(%rbp) # imm = 0xFFFFFFFB
jmp 0xc67d
testb $0x1, -0x6c(%rbp)
je 0xc5bf
movl $0x3, -0x14(%rbp)
movl $0x1388, %edi           # imm = 0x1388
callq 0x5180
movq -0xa0(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x80(%rbp), %rdx
movq (%rdi), %rax
movl $0xbf7b, %esi           # imm = 0xBF7B
movl $0x80, %ecx
callq *0x30(%rax)
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jge 0xc549
jmp 0xc515
leaq 0x19d7d(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xc545
movq 0x19ab0(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0x1192c(%rip), %rsi     # 0x1de5e
leaq 0x111b0(%rip), %rdx     # 0x1d6e9
movl $0x190, %ecx            # imm = 0x190
movb $0x0, %al
callq 0x5190
jmp 0xc547
jmp 0xc5a9
movq -0xa0(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x78(%rbp), %rdx
movq (%rdi), %rax
movl $0x8050, %esi           # imm = 0x8050
movl $0xf0, %ecx
callq *0x30(%rax)
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jge 0xc5a7
jmp 0xc573
leaq 0x19d1f(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xc5a3
movq 0x19a52(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0x11901(%rip), %rsi     # 0x1de91
leaq 0x11152(%rip), %rdx     # 0x1d6e9
movl $0x196, %ecx            # imm = 0x196
movb $0x0, %al
callq 0x5190
jmp 0xc5a5
jmp 0xc5a9
jmp 0xc5bd
movl -0x14(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x14(%rbp)
cmpl $0x0, %eax
jg 0xc4e1
jmp 0xc5bf
movl $0xf4240, %edi          # imm = 0xF4240
callq 0x5180
movq -0xa0(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x60(%rbp), %rdx
movq (%rdi), %rax
movl $0x8050, %esi           # imm = 0x8050
movl $0x1, %ecx
callq *0x20(%rax)
movb -0x60(%rbp), %al
movb %al, -0x69(%rbp)
leaq 0x19ca4(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xc623
movq 0x199b7(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movzbl -0x69(%rbp), %r8d
leaq 0x118b1(%rip), %rsi     # 0x1dec1
leaq 0x110d2(%rip), %rdx     # 0x1d6e9
movl $0x1a1, %ecx            # imm = 0x1A1
movb $0x0, %al
callq 0x5190
jmp 0xc625
movzbl -0x69(%rbp), %eax
movzbl -0x6b(%rbp), %ecx
cmpl %ecx, %eax
je 0xc66e
jmp 0xc633
leaq 0x19c5f(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xc663
movq 0x19972(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x118a2(%rip), %rsi     # 0x1def2
leaq 0x11092(%rip), %rdx     # 0x1d6e9
movl $0x1a5, %ecx            # imm = 0x1A5
movb $0x0, %al
callq 0x5190
jmp 0xc665
movl $0xffffffff, -0x18(%rbp) # imm = 0xFFFFFFFF
jmp 0xc677
movl $0x0, -0x4(%rbp)
jmp 0xc67d
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax)