// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %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, %r15d
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x88(%rax)
movl %eax, %r12d
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x98(%rax)
xorl %r14d, %r14d
movl %ebp, 0xc(%rsp)
testl %ebp, %ebp
jle 0x820b
testb %al, %al
je 0x820b
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0xa8(%rax)
testb $0x2, %al
jne 0x7ff0
leaq 0x182d6(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0x820b
movq 0x17fed(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xe4fc(%rip), %rsi      # 0x164d1
leaq 0xe520(%rip), %rdx      # 0x164fc
xorl %r14d, %r14d
movl $0x14e, %ecx            # imm = 0x14E
xorl %eax, %eax
callq 0x5180
jmp 0x820b
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
movq %rax, %r14
leaq 0x1828d(%rip), %rax     # 0x20291
movb (%rax), %al
testq %r14, %r14
je 0x80c5
movq %rbx, 0x10(%rsp)
testb %al, %al
je 0x8041
movq 0x17f99(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xe4e6(%rip), %rsi      # 0x1650f
leaq 0xe4cc(%rip), %rdx      # 0x164fc
movl $0x15a, %ecx            # imm = 0x15A
movl 0xc(%rsp), %r8d
xorl %eax, %eax
callq 0x5180
movl 0xc(%rsp), %ebx
subl $0x1, %ebx
jb 0x80f3
movl %r15d, %eax
movzbl (%r14,%rax), %ebp
leaq 0x18234(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0x8089
movq 0x17f4f(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xe4ce(%rip), %rsi      # 0x16541
leaq 0xe482(%rip), %rdx      # 0x164fc
movl $0x15d, %ecx            # imm = 0x15D
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
leal 0x1(%r15), %eax
movzbl (%r14,%rax), %eax
shll $0x8, %eax
leal 0x2(%r15), %ecx
movzbl (%r14,%rcx), %r13d
orl %eax, %r13d
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
cmpb %bpl, %al
je 0x814e
addl %r13d, %r12d
addl $0x3, %r15d
jmp 0x8045
pushq $-0x4
popq %r14
testb %al, %al
je 0x820b
movq 0x17f00(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xe1dd(%rip), %rsi      # 0x162bf
leaq 0xe413(%rip), %rdx      # 0x164fc
movl $0x155, %ecx            # imm = 0x155
jmp 0x7fe4
pushq $-0x5
popq %r14
leaq 0x18193(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0x820b
movq 0x17eaa(%rip), %rax     # 0x1ffb8
movq (%rax), %r15
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movzbl %al, %r8d
leaq 0xe487(%rip), %rsi      # 0x165b5
leaq 0xe3c7(%rip), %rdx      # 0x164fc
movq %r15, %rdi
movl $0x170, %ecx            # imm = 0x170
movl 0xc(%rsp), %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x820b
movl %r12d, %eax
addq %rax, %r14
leaq 0x18136(%rip), %rax     # 0x20291
movb (%rax), %al
cmpb $0x1, %al
movq 0x10(%rsp), %rbx
jne 0x81ae
movzbl (%r14), %ebp
movq 0x17e47(%rip), %rax     # 0x1ffb8
movq (%rax), %r15
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movzbl %al, %r8d
leaq 0xe3e8(%rip), %rsi      # 0x16574
leaq 0xe369(%rip), %rdx      # 0x164fc
movq %r15, %rdi
movl $0x166, %ecx            # imm = 0x166
movl %ebp, %r9d
xorl %eax, %eax
callq 0x5180
leaq 0x180e5(%rip), %rax     # 0x20291
movb (%rax), %al
testb $0x1, %al
je 0x81df
movq 0x17dff(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xe438(%rip), %rsi      # 0x165fb
leaq 0xe332(%rip), %rdx      # 0x164fc
movl $0x174, %ecx            # imm = 0x174
movl $0x19000, %r8d          # imm = 0x19000
movl %r13d, %r9d
xorl %eax, %eax
callq 0x5180
movq (%rbx), %rax
movq %rbx, %rdi
movl $0x19000, %esi          # imm = 0x19000
movq %r14, %rdx
movl %r13d, %ecx
callq *0x28(%rax)
xorl %r14d, %r14d
testl %eax, %eax
jns 0x820b
movl %eax, %ebx
leaq 0x1808e(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
je 0x821d
movl %ebx, %r14d
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x17db4(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xe3fb(%rip), %rsi      # 0x16629
leaq 0xe2c7(%rip), %rdx      # 0x164fc
movl $0x177, %ecx            # imm = 0x177
movl %ebx, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x8208