// File: /workspace/github_cpp_makefile_star2_O2/goodix[P]gdix_hid_firmware_update/gt7868q/gt7868q_update.cpp
// Function: GT7868QUpdate::Run(void*)
int GT7868QUpdate::Run(void *para)
{
	int ret = 0;
	int retry = 0;
	updateFlag flag = NO_NEED_UPDATE;

	if (!m_Initialized) {
		gdix_err("Can't Process Update Before Initialized.\n");
		return -1;
	}
	// get all parameter
	pGTUpdatePara parameter = (pGTUpdatePara)para;

	// check if the image has config
	if (image->HasConfig()) {
		flag = image->GetUpdateFlag();
	} else {
		// default for firmware
		flag = NEED_UPDATE_FW;
	}

	gdix_dbg("fw update flag is 0x%x\n", flag);

	if (!parameter->force) {
		ret = check_update();
		if (ret) {
			gdix_err("Doesn't meet the update conditions\n");
			return -3;
		}
	}

	if (flag & NEED_UPDATE_FW) {
		retry = 0;
		do {
			ret = fw_update(parameter->firmwareFlag);
			if (ret) {
				gdix_dbg("Update failed\n");
				usleep(200000);
			} else {
				usleep(300000);
				gdix_dbg("Update success\n");
				break;
			}
		} while (retry++ < 3);
		if (ret) {
			gdix_err("Firmware update err:ret=%d\n", ret);
			return ret;
		}
	}

	/* do interactive config update when is_cfg_flashed_with_isp is false.
	 * Otherwise the config is written to flash by ISP.
	 */
	if (this->is_cfg_flashed_with_isp == false && flag & NEED_UPDATE_CONFIG) {
		gdix_dbg("Update config interactively\n");
		retry = 0;
		do {
			ret = cfg_update();
			if (ret) {
				gdix_dbg("Update cfg failed\n");
				usleep(200000);
			} else {
				usleep(300000);
				gdix_dbg("Update cfg success\n");
				break;
			}
		} while (retry++ < 3);
		if (ret) {
			gdix_err("config update err:ret=%d\n", ret);
			return ret;
		}
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpb $0x0, 0x18(%rdi)
je 0x745c
movq %rsi, %r15
movq %rdi, %rbx
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x98(%rax)
testb %al, %al
je 0x748e
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0xa8(%rax)
movl %eax, %r14d
jmp 0x7492
leaq 0x18e2e(%rip), %rax     # 0x20291
pushq $-0x1
popq %rbp
cmpb $0x1, (%rax)
jne 0x76bf
movq 0x18b62(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xeb99(%rip), %rsi      # 0x16019
leaq 0xebcd(%rip), %rdx      # 0x16054
pushq $0x36
jmp 0x76b7
pushq $0x1
popq %r14
leaq 0x18df8(%rip), %r12     # 0x20291
cmpb $0x1, (%r12)
jne 0x74c5
movq 0x18b11(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xeba7(%rip), %rsi      # 0x16058
leaq 0xeb9c(%rip), %rdx      # 0x16054
pushq $0x44
popq %rcx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x5180
cmpb $0x0, (%r15)
jne 0x7505
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x20(%rax)
testl %eax, %eax
je 0x7505
pushq $-0x3
popq %rbp
cmpb $0x1, (%r12)
jne 0x76bf
movq 0x18aeb(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xeb8a(%rip), %rsi      # 0x16081
leaq 0xeb56(%rip), %rdx      # 0x16054
pushq $0x49
jmp 0x76b7
testb $0x1, %r14b
je 0x75c9
movl %r14d, 0x4(%rsp)
pushq $0x4
popq %r13
pushq $0x53
popq %r14
movl 0x4(%r15), %esi
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x30(%rax)
testl %eax, %eax
je 0x7591
movl %eax, %ebp
cmpb $0x1, (%r12)
jne 0x7558
movq 0x18a7b(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xeb6f(%rip), %rsi      # 0x160b6
leaq 0xeb06(%rip), %rdx      # 0x16054
movl %r14d, %ecx
xorl %eax, %eax
callq 0x5180
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x5170
decl %r13d
jne 0x751c
cmpb $0x1, (%r12)
jne 0x76bf
movq 0x18a5f(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xe38f(%rip), %rsi      # 0x15912
leaq 0xeaca(%rip), %rdx      # 0x16054
pushq $0x5c
jmp 0x767b
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5170
cmpb $0x1, (%r12)
movl 0x4(%rsp), %r14d
jne 0x75c9
movq 0x18a0a(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xeb1e(%rip), %rsi      # 0x160d6
leaq 0xea95(%rip), %rdx      # 0x16054
pushq $0x57
popq %rcx
xorl %eax, %eax
callq 0x5180
xorl %ebp, %ebp
testb $0x2, %r14b
je 0x76bf
cmpb $0x0, 0x1a(%rbx)
jne 0x76bf
cmpb $0x1, (%r12)
jne 0x7608
movq 0x189cb(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xeb00(%rip), %rsi      # 0x160f7
leaq 0xea56(%rip), %rdx      # 0x16054
pushq $0x65
popq %rcx
xorl %eax, %eax
callq 0x5180
pushq $0x4
popq %r14
leaq 0xea41(%rip), %r15      # 0x16054
pushq $0x6a
popq %r13
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x38(%rax)
testl %eax, %eax
je 0x7688
movl %eax, %ebp
cmpb $0x1, (%r12)
jne 0x764b
movq 0x18984(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xeae7(%rip), %rsi      # 0x16125
movq %r15, %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x5180
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x5170
decl %r14d
jne 0x7617
cmpb $0x1, (%r12)
jne 0x76bf
movq 0x18970(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xeafc(%rip), %rsi      # 0x1616e
leaq 0xe9db(%rip), %rdx      # 0x16054
pushq $0x73
popq %rcx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x76bf
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5170
xorl %ebp, %ebp
cmpb $0x1, (%r12)
jne 0x76bf
movq 0x18916(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xea9d(%rip), %rsi      # 0x16149
leaq 0xe9a1(%rip), %rdx      # 0x16054
xorl %ebp, %ebp
pushq $0x6e
popq %rcx
xorl %eax, %eax
callq 0x5180
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq