// File: /workspace/github_cpp_makefile_star2_O3/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 0x8f0b
movq %rsi, %r15
movq %rdi, %rbx
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x98(%rax)
movl $0x1, %r12d
testb %al, %al
je 0x8eb2
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0xa8(%rax)
movl %eax, %r12d
leaq 0x183d8(%rip), %r14     # 0x21291
cmpb $0x1, (%r14)
jne 0x8ee6
movq 0x180f2(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xe188(%rip), %rsi      # 0x17058
leaq 0xe17d(%rip), %rdx      # 0x17054
movl $0x44, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
cmpb $0x0, (%r15)
jne 0x8f31
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x20(%rax)
testl %eax, %eax
je 0x8f31
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
cmpb $0x1, (%r14)
jne 0x8f20
callq 0x53b5
jmp 0x8f20
leaq 0x1837f(%rip), %rax     # 0x21291
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpb $0x1, (%rax)
je 0x9101
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb $0x1, %r12b
je 0x8ff6
movl %r12d, 0x4(%rsp)
movl $0x4, %r12d
leaq 0xe107(%rip), %r13      # 0x17054
movl 0x4(%r15), %esi
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x30(%rax)
testl %eax, %eax
je 0x8fbd
movl %eax, %ebp
cmpb $0x1, (%r14)
jne 0x8f86
movq 0x1804b(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xe13f(%rip), %rsi      # 0x170b6
movq %r13, %rdx
movl $0x53, %ecx
xorl %eax, %eax
callq 0x5180
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x5170
decl %r12d
jne 0x8f4d
cmpb $0x1, (%r14)
jne 0x8f20
movq 0x18036(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xd966(%rip), %rsi      # 0x16912
leaq 0xe0a1(%rip), %rdx      # 0x17054
movl $0x5c, %ecx
jmp 0x90b1
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5170
cmpb $0x1, (%r14)
movl 0x4(%rsp), %r12d
jne 0x8ff6
movq 0x17fdf(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xe0f3(%rip), %rsi      # 0x170d6
leaq 0xe06a(%rip), %rdx      # 0x17054
movl $0x57, %ecx
xorl %eax, %eax
callq 0x5180
xorl %ebp, %ebp
testb $0x2, %r12b
je 0x8f20
cmpb $0x0, 0x1a(%rbx)
jne 0x8f20
cmpb $0x1, (%r14)
jne 0x9036
movq 0x17f9f(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xe0d4(%rip), %rsi      # 0x170f7
leaq 0xe02a(%rip), %rdx      # 0x17054
movl $0x65, %ecx
xorl %eax, %eax
callq 0x5180
movl $0x4, %r13d
leaq 0xe0e2(%rip), %r12      # 0x17125
leaq 0xe00a(%rip), %r15      # 0x17054
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x38(%rax)
testl %eax, %eax
je 0x90c0
movl %eax, %ebp
cmpb $0x1, (%r14)
jne 0x907b
movq 0x17f52(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movq %r12, %rsi
movq %r15, %rdx
movl $0x6a, %ecx
xorl %eax, %eax
callq 0x5180
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x5170
decl %r13d
jne 0x904a
cmpb $0x1, (%r14)
jne 0x8f20
movq 0x17f3d(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xe0c9(%rip), %rsi      # 0x1716e
leaq 0xdfa8(%rip), %rdx      # 0x17054
movl $0x73, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x8f20
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5170
xorl %ebp, %ebp
cmpb $0x1, (%r14)
jne 0x8f20
movq 0x17edb(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xe062(%rip), %rsi      # 0x17149
leaq 0xdf66(%rip), %rdx      # 0x17054
xorl %ebp, %ebp
movl $0x6e, %ecx
xorl %eax, %eax
callq 0x5180
jmp 0x8f20
callq 0x5393
jmp 0x8f20
nop