// File: /workspace/github_cpp_makefile_star2_O1/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 0x9c6d
testb %al, %al
je 0x9c6d
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0xa8(%rax)
testb $0x2, %al
jne 0x9a6c
leaq 0x1785a(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x9c6d
movq 0x17571(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xdae1(%rip), %rsi      # 0x17532
leaq 0xdb05(%rip), %rdx      # 0x1755d
xorl %r14d, %r14d
movl $0x14e, %ecx            # imm = 0x14E
xorl %eax, %eax
callq 0x5180
jmp 0x9c6d
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
movq %rax, %r14
leaq 0x17811(%rip), %rax     # 0x21291
movb (%rax), %al
testq %r14, %r14
je 0x9b44
movq %rbx, 0x8(%rsp)
testb %al, %al
je 0x9abb
movq 0x1751d(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xdacb(%rip), %rsi      # 0x17570
leaq 0xdab1(%rip), %rdx      # 0x1755d
movl $0x15a, %ecx            # imm = 0x15A
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
cmpl $0x2, %ebp
movl $0x1, %ebx
movl %ebp, 0x14(%rsp)
cmovgel %ebp, %ebx
movl %r15d, %eax
movzbl (%r14,%rax), %ebp
leaq 0x177b8(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x9b05
movq 0x174d3(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xdab3(%rip), %rsi      # 0x175a2
leaq 0xda67(%rip), %rdx      # 0x1755d
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 0x9b5c
addl %r13d, %r12d
addl $0x3, %r15d
decl %ebx
jne 0x9aca
xorl %r14d, %r14d
jmp 0x9bb4
movl $0xfffffffc, %r14d      # imm = 0xFFFFFFFC
testb %al, %al
je 0x9c6d
callq 0x54d9
jmp 0x9c6d
movl %r12d, %eax
addq %rax, %r14
leaq 0x17728(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x9bb4
movzbl (%r14), %ebp
movq 0x1743f(%rip), %rax     # 0x20fb8
movq (%rax), %r15
movq 0x8(%rsp), %rbx
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movzbl %al, %r8d
leaq 0xda3c(%rip), %rsi      # 0x175d5
leaq 0xd9bd(%rip), %rdx      # 0x1755d
movq %r15, %rdi
movl $0x166, %ecx            # imm = 0x166
movl %ebp, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x9bb9
movq 0x8(%rsp), %rbx
leaq 0x176d1(%rip), %rax     # 0x21291
movb (%rax), %al
testq %r14, %r14
je 0x9c26
testb %al, %al
je 0x9bf8
movq 0x173e6(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xda80(%rip), %rsi      # 0x1765c
leaq 0xd97a(%rip), %rdx      # 0x1755d
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 0x9c6d
movl %eax, %ebx
leaq 0x17675(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
je 0x9c7f
movl %ebx, %r14d
jmp 0x9c6d
movl $0xfffffffb, %r14d      # imm = 0xFFFFFFFB
testb %al, %al
je 0x9c6d
movq 0x17381(%rip), %rax     # 0x20fb8
movq (%rax), %r15
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movzbl %al, %r8d
leaq 0xd9c4(%rip), %rsi      # 0x17616
leaq 0xd904(%rip), %rdx      # 0x1755d
movq %r15, %rdi
movl $0x170, %ecx            # imm = 0x170
movl 0x14(%rsp), %r9d
xorl %eax, %eax
callq 0x5180
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x17352(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xd9fa(%rip), %rsi      # 0x1768a
leaq 0xd8c6(%rip), %rdx      # 0x1755d
movl $0x177, %ecx            # imm = 0x177
movl %ebx, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x9c21
nop