// File: /workspace/github_cpp_makefile_star2_O1/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 0x905b
movq %rsi, %r15
movq %rdi, %rbx
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x98(%rax)
movl $0x1, %r12d
testb %al, %al
je 0x9002
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0xa8(%rax)
movl %eax, %r12d
leaq 0x18288(%rip), %r14     # 0x21291
cmpb $0x1, (%r14)
jne 0x9036
movq 0x17fa2(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xe099(%rip), %rsi      # 0x170b9
leaq 0xe08e(%rip), %rdx      # 0x170b5
movl $0x44, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
cmpb $0x0, (%r15)
jne 0x9081
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x20(%rax)
testl %eax, %eax
je 0x9081
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
cmpb $0x1, (%r14)
jne 0x9070
callq 0x53b5
jmp 0x9070
leaq 0x1822f(%rip), %rax     # 0x21291
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpb $0x1, (%rax)
je 0x925b
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb $0x1, %r12b
je 0x9150
movl %r12d, 0x4(%rsp)
movl $0x4, %r12d
leaq 0xe018(%rip), %r13      # 0x170b5
movl 0x4(%r15), %esi
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x30(%rax)
movl %eax, %ebp
testl %eax, %eax
je 0x90e7
cmpb $0x1, (%r14)
jne 0x90d6
movq 0x17efb(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xe050(%rip), %rsi      # 0x17117
movq %r13, %rdx
movl $0x53, %ecx
xorl %eax, %eax
callq 0x5180
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x5170
decl %r12d
jne 0x909d
jmp 0x911b
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5170
cmpb $0x1, (%r14)
jne 0x911b
movq 0x17eba(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xe02f(%rip), %rsi      # 0x17137
leaq 0xdfa6(%rip), %rdx      # 0x170b5
movl $0x57, %ecx
xorl %eax, %eax
callq 0x5180
testl %ebp, %ebp
movl 0x4(%rsp), %r12d
je 0x9150
cmpb $0x1, (%r14)
jne 0x9070
movq 0x17ea3(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xd834(%rip), %rsi      # 0x16973
leaq 0xdf6f(%rip), %rdx      # 0x170b5
movl $0x5c, %ecx
jmp 0x9245
xorl %ebp, %ebp
testb $0x2, %r12b
je 0x9070
cmpb $0x0, 0x1a(%rbx)
jne 0x9070
cmpb $0x1, (%r14)
jne 0x9190
movq 0x17e45(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xdfdb(%rip), %rsi      # 0x17158
leaq 0xdf31(%rip), %rdx      # 0x170b5
movl $0x65, %ecx
xorl %eax, %eax
callq 0x5180
movl $0x4, %r13d
leaq 0xdfe9(%rip), %r12      # 0x17186
leaq 0xdf11(%rip), %r15      # 0x170b5
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x38(%rax)
movl %eax, %ebp
testl %eax, %eax
je 0x91e6
cmpb $0x1, (%r14)
jne 0x91d5
movq 0x17df8(%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 0x91a4
jmp 0x921a
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5170
cmpb $0x1, (%r14)
jne 0x921a
movq 0x17dbb(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xdfa3(%rip), %rsi      # 0x171aa
leaq 0xdea7(%rip), %rdx      # 0x170b5
movl $0x6e, %ecx
xorl %eax, %eax
callq 0x5180
testl %ebp, %ebp
je 0x9254
cmpb $0x1, (%r14)
jne 0x9070
movq 0x17da9(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xdf96(%rip), %rsi      # 0x171cf
leaq 0xde75(%rip), %rdx      # 0x170b5
movl $0x73, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x9070
xorl %ebp, %ebp
jmp 0x9070
callq 0x5393
jmp 0x9070
nop