// File: /workspace/github_cpp_makefile_star2_O3/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
testl %ebp, %ebp
jle 0x9b9c
testb %al, %al
je 0x9b9c
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0xa8(%rax)
testb $0x2, %al
jne 0x999c
leaq 0x1792a(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x9b9c
movq 0x17641(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xdb50(%rip), %rsi      # 0x174d1
leaq 0xdb74(%rip), %rdx      # 0x174fc
xorl %r14d, %r14d
movl $0x14e, %ecx            # imm = 0x14E
xorl %eax, %eax
callq 0x5180
jmp 0x9b9c
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
movq %rax, %r14
leaq 0x178e1(%rip), %rax     # 0x21291
movb (%rax), %al
testq %r14, %r14
je 0x9ac7
movq %rbx, 0x8(%rsp)
testb %al, %al
je 0x99eb
movq 0x175ed(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xdb3a(%rip), %rsi      # 0x1750f
leaq 0xdb20(%rip), %rdx      # 0x174fc
movl $0x15a, %ecx            # imm = 0x15A
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
movl %ebp, 0x14(%rsp)
movl %ebp, %ebx
movl %r15d, %eax
movzbl (%r14,%rax), %ebp
leaq 0x17891(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x9a2c
movq 0x175ac(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xdb2b(%rip), %rsi      # 0x17541
leaq 0xdadf(%rip), %rdx      # 0x174fc
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 0x8(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
cmpb %bpl, %al
je 0x9adf
addl %r13d, %r12d
addl $0x3, %r15d
decl %ebx
jne 0x99f1
movl $0xfffffffb, %r14d      # imm = 0xFFFFFFFB
leaq 0x1781a(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x9b9c
movq 0x17531(%rip), %rax     # 0x20fb8
movq (%rax), %r15
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movzbl %al, %r8d
leaq 0xdb0e(%rip), %rsi      # 0x175b5
leaq 0xda4e(%rip), %rdx      # 0x174fc
movq %r15, %rdi
movl $0x170, %ecx            # imm = 0x170
movl 0x14(%rsp), %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x9b9c
movl $0xfffffffc, %r14d      # imm = 0xFFFFFFFC
testb %al, %al
je 0x9b9c
callq 0x5485
jmp 0x9b9c
movl %r12d, %eax
addq %rax, %r14
leaq 0x177a5(%rip), %rax     # 0x21291
movb (%rax), %al
cmpb $0x1, %al
movq 0x8(%rsp), %rbx
jne 0x9b3f
movzbl (%r14), %ebp
movq 0x174b6(%rip), %rax     # 0x20fb8
movq (%rax), %r15
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movzbl %al, %r8d
leaq 0xda57(%rip), %rsi      # 0x17574
leaq 0xd9d8(%rip), %rdx      # 0x174fc
movq %r15, %rdi
movl $0x166, %ecx            # imm = 0x166
movl %ebp, %r9d
xorl %eax, %eax
callq 0x5180
leaq 0x17754(%rip), %rax     # 0x21291
movb (%rax), %al
testb $0x1, %al
je 0x9b70
movq 0x1746e(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xdaa7(%rip), %rsi      # 0x175fb
leaq 0xd9a1(%rip), %rdx      # 0x174fc
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 0x9b9c
movl %eax, %ebx
leaq 0x176fd(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
je 0x9bae
movl %ebx, %r14d
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x17423(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xda6a(%rip), %rsi      # 0x17629
leaq 0xd936(%rip), %rdx      # 0x174fc
movl $0x177, %ecx            # imm = 0x177
movl %ebx, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x9b99
nop