// File: goodix[P]gdix_hid_firmware_update/gt7868q/gt7868q_update.cpp
// Function: GT7868QUpdate::flash_cfg_with_isp()
int GT7868QUpdate::flash_cfg_with_isp()
{
	int ret = -1, i = 0;
	updateFlag flag = NO_NEED_UPDATE;
	unsigned char *fw_data = NULL;
	unsigned char *cfg = NULL;
	unsigned char sub_cfg_id;
	unsigned int sub_cfg_len;
	unsigned char cfg_ver_infile;
	int sub_cfg_num = image->GetConfigSubCfgNum();
	unsigned int sub_cfg_info_pos = image->GetConfigSubCfgInfoOffset();
	unsigned int cfg_offset = image->GetConfigSubCfgDataOffset();

	if (image->HasConfig() == false || sub_cfg_num <= 0) {
		/* no config found in the bin file */
		return 0;
	}

	flag = image->GetUpdateFlag();
	if (!(flag & NEED_UPDATE_CONFIG)) {
		/* config update flag not set */
		gdix_dbg("flag UPDATE_CONFIG unset\n");
		return 0;
	}

	/* Start load config */
	fw_data = image->GetFirmwareData();
	if (!fw_data) {
		gdix_err("No valid fw data \n");
		return -4;
	}

	// 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) {
			cfg = &fw_data[cfg_offset];
			cfg_ver_infile = cfg[0];
			gdix_dbg("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 (!cfg) {
		/* failed found config for sensorID */
		gdix_dbg("Failed found config for sensorID %d, sub_cfg_num %d\n",
				 dev->GetSensorID(), sub_cfg_num);
		return -5;
	}

	gdix_dbg("load cfg addr:0x%x,len:0x%x\n", CFG_FLASH_ADDR, sub_cfg_len);
	ret = load_sub_firmware(CFG_FLASH_ADDR, cfg, sub_cfg_len);
	if (ret < 0) {
		gdix_err("Failed flash cofig with ISP, ret=%d\n", ret);
		return ret;
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movl $0xffffffff, -0x14(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq 0x10(%rax), %rdi
movq (%rdi), %rax
callq *0x78(%rax)
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, -0x40(%rbp)
movq 0x10(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, -0x44(%rbp)
movq 0x10(%rax), %rdi
movq (%rdi), %rax
callq *0x88(%rax)
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, -0x48(%rbp)
movq 0x10(%rax), %rdi
movq (%rdi), %rax
callq *0x98(%rax)
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x9a05
cmpl $0x0, -0x40(%rbp)
jg 0x9a11
movl $0x0, -0x4(%rbp)
jmp 0x9d35
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
callq *0xa8(%rax)
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0x9a70
jmp 0x9a32
leaq 0x1c860(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x9a62
movq 0x1c573(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x13ad8(%rip), %rsi     # 0x1d527
leaq 0x13afc(%rip), %rdx     # 0x1d552
movl $0x14e, %ecx            # imm = 0x14E
movb $0x0, %al
callq 0x5190
jmp 0x9a64
movl $0x0, -0x4(%rbp)
jmp 0x9d35
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x9ac9
jmp 0x9a8b
leaq 0x1c807(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x9abb
movq 0x1c53a(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0x1386d(%rip), %rsi     # 0x1d315
leaq 0x13aa3(%rip), %rdx     # 0x1d552
movl $0x155, %ecx            # imm = 0x155
movb $0x0, %al
callq 0x5190
jmp 0x9abd
movl $0xfffffffc, -0x4(%rbp) # imm = 0xFFFFFFFC
jmp 0x9d35
jmp 0x9acb
leaq 0x1c7c7(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x9aff
movq 0x1c4da(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movl -0x40(%rbp), %r8d
leaq 0x13a79(%rip), %rsi     # 0x1d565
leaq 0x13a5f(%rip), %rdx     # 0x1d552
movl $0x15a, %ecx            # imm = 0x15A
movb $0x0, %al
callq 0x5190
jmp 0x9b01
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x40(%rbp), %eax
jge 0x9c29
movq -0x28(%rbp), %rax
movl -0x44(%rbp), %ecx
movb (%rax,%rcx), %al
movb %al, -0x31(%rbp)
leaq 0x1c771(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x9b56
movq 0x1c484(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movzbl -0x31(%rbp), %r8d
leaq 0x13a54(%rip), %rsi     # 0x1d597
leaq 0x13a08(%rip), %rdx     # 0x1d552
movl $0x15d, %ecx            # imm = 0x15D
movb $0x0, %al
callq 0x5190
jmp 0x9b58
movq -0x50(%rbp), %rax
movq -0x28(%rbp), %rcx
movl -0x44(%rbp), %edx
addl $0x1, %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
shll $0x8, %ecx
movq -0x28(%rbp), %rdx
movl -0x44(%rbp), %esi
addl $0x2, %esi
movl %esi, %esi
movzbl (%rdx,%rsi), %edx
orl %edx, %ecx
movl %ecx, -0x38(%rbp)
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movzbl %al, %eax
movzbl -0x31(%rbp), %ecx
cmpl %ecx, %eax
jne 0x9c09
movq -0x28(%rbp), %rax
movl -0x48(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movb (%rax), %al
movb %al, -0x39(%rbp)
leaq 0x1c6df(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x9c05
movq -0x50(%rbp), %rax
movq 0x1c3ee(%rip), %rcx     # 0x25fb8
movq (%rcx), %rcx
movq %rcx, -0x58(%rbp)
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movq -0x58(%rbp), %rdi
movzbl %al, %r8d
movzbl -0x39(%rbp), %r9d
leaq 0x139d8(%rip), %rsi     # 0x1d5ca
leaq 0x13959(%rip), %rdx     # 0x1d552
movl $0x166, %ecx            # imm = 0x166
movb $0x0, %al
callq 0x5190
jmp 0x9c07
jmp 0x9c29
movl -0x38(%rbp), %eax
addl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x44(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x44(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x9b08
cmpq $0x0, -0x30(%rbp)
jne 0x9c91
jmp 0x9c32
leaq 0x1c660(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x9c83
movq -0x50(%rbp), %rax
movq 0x1c36f(%rip), %rcx     # 0x25fb8
movq (%rcx), %rcx
movq %rcx, -0x60(%rbp)
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movq -0x60(%rbp), %rdi
movzbl %al, %r8d
movl -0x40(%rbp), %r9d
leaq 0x1399b(%rip), %rsi     # 0x1d60b
leaq 0x138db(%rip), %rdx     # 0x1d552
movl $0x170, %ecx            # imm = 0x170
movb $0x0, %al
callq 0x5190
jmp 0x9c85
movl $0xfffffffb, -0x4(%rbp) # imm = 0xFFFFFFFB
jmp 0x9d35
jmp 0x9c93
leaq 0x1c5ff(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x9ccd
movq 0x1c312(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movl -0x38(%rbp), %r9d
leaq 0x1399d(%rip), %rsi     # 0x1d651
leaq 0x13897(%rip), %rdx     # 0x1d552
movl $0x174, %ecx            # imm = 0x174
movl $0x19000, %r8d          # imm = 0x19000
movb $0x0, %al
callq 0x5190
jmp 0x9ccf
movq -0x50(%rbp), %rdi
movq -0x30(%rbp), %rdx
movl -0x38(%rbp), %ecx
movq (%rdi), %rax
movl $0x19000, %esi          # imm = 0x19000
callq *0x28(%rax)
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x9d2e
jmp 0x9cf0
leaq 0x1c5a2(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x9d24
movq 0x1c2d5(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movl -0x14(%rbp), %r8d
leaq 0x1396e(%rip), %rsi     # 0x1d67f
leaq 0x1383a(%rip), %rdx     # 0x1d552
movl $0x177, %ecx            # imm = 0x177
movb $0x0, %al
callq 0x5190
jmp 0x9d26
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x9d35
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop