// File: 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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
testb $0x1, 0x18(%rax)
jne 0x8af3
jmp 0x8ab5
leaq 0x1d7dd(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x8ae5
movq 0x1d510(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0x1459d(%rip), %rsi     # 0x1d06f
leaq 0x145d1(%rip), %rdx     # 0x1d0aa
movl $0x36, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x8ae7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8e29
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
movq %rcx, -0x30(%rbp)
movq 0x10(%rax), %rdi
movq (%rdi), %rax
callq *0x98(%rax)
testb $0x1, %al
jne 0x8b12
jmp 0x8b28
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
callq *0xa8(%rax)
movl %eax, -0x24(%rbp)
jmp 0x8b2f
movl $0x1, -0x24(%rbp)
jmp 0x8b31
leaq 0x1d761(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x8b65
movq 0x1d474(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movl -0x24(%rbp), %r8d
leaq 0x1455c(%rip), %rsi     # 0x1d0ae
leaq 0x14551(%rip), %rdx     # 0x1d0aa
movl $0x44, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x8b67
movq -0x30(%rbp), %rax
testb $0x1, (%rax)
jne 0x8bc5
movq -0x38(%rbp), %rdi
movq (%rdi), %rax
callq *0x20(%rax)
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x8bc3
jmp 0x8b85
leaq 0x1d70d(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x8bb5
movq 0x1d440(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0x14535(%rip), %rsi     # 0x1d0d7
leaq 0x14501(%rip), %rdx     # 0x1d0aa
movl $0x49, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x8bb7
movl $0xfffffffd, -0x4(%rbp) # imm = 0xFFFFFFFD
jmp 0x8e29
jmp 0x8bc5
movl -0x24(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x8cd4
movl $0x0, -0x20(%rbp)
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rax
movl 0x4(%rax), %esi
movq (%rdi), %rax
callq *0x30(%rax)
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x8c35
jmp 0x8bf7
leaq 0x1d69b(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x8c27
movq 0x1d3ae(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x144f8(%rip), %rsi     # 0x1d10c
leaq 0x1448f(%rip), %rdx     # 0x1d0aa
movl $0x53, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x8c29
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x5180
jmp 0x8c73
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5180
leaq 0x1d653(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x8c6f
movq 0x1d366(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x144d0(%rip), %rsi     # 0x1d12c
leaq 0x14447(%rip), %rdx     # 0x1d0aa
movl $0x57, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x8c71
jmp 0x8c89
jmp 0x8c75
movl -0x20(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x20(%rbp)
cmpl $0x3, %eax
jl 0x8bdb
cmpl $0x0, -0x1c(%rbp)
je 0x8cd2
jmp 0x8c91
leaq 0x1d601(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x8cc5
movq 0x1d334(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movl -0x1c(%rbp), %r8d
leaq 0x13cb1(%rip), %rsi     # 0x1c963
leaq 0x143f1(%rip), %rdx     # 0x1d0aa
movl $0x5c, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x8cc7
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x8e29
jmp 0x8cd4
movq -0x38(%rbp), %rax
movb 0x1a(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x8e22
movl -0x24(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x8e22
jmp 0x8cfa
leaq 0x1d598(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x8d2a
movq 0x1d2ab(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x14436(%rip), %rsi     # 0x1d14d
leaq 0x1438c(%rip), %rdx     # 0x1d0aa
movl $0x65, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x8d2c
movl $0x0, -0x20(%rbp)
movq -0x38(%rbp), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x8d86
jmp 0x8d48
leaq 0x1d54a(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x8d78
movq 0x1d25d(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x14416(%rip), %rsi     # 0x1d17b
leaq 0x1433e(%rip), %rdx     # 0x1d0aa
movl $0x6a, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x8d7a
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x5180
jmp 0x8dc4
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5180
leaq 0x1d502(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x8dc0
movq 0x1d215(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x143f2(%rip), %rsi     # 0x1d19f
leaq 0x142f6(%rip), %rdx     # 0x1d0aa
movl $0x6e, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x8dc2
jmp 0x8dda
jmp 0x8dc6
movl -0x20(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x20(%rbp)
cmpl $0x3, %eax
jl 0x8d33
cmpl $0x0, -0x1c(%rbp)
je 0x8e20
jmp 0x8de2
leaq 0x1d4b0(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x8e16
movq 0x1d1e3(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movl -0x1c(%rbp), %r8d
leaq 0x143c1(%rip), %rsi     # 0x1d1c4
leaq 0x142a0(%rip), %rdx     # 0x1d0aa
movl $0x73, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x8e18
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x8e29
jmp 0x8e22
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)