// File: goodix[P]gdix_hid_firmware_update/gtx2/gtx2_update.cpp
// Function: GTx2Update::Run(void*)
int GTx2Update::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("Flag = %d\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_CONFIG) {
		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;
		}
	}

	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;
		}
	}

	// before go forward,Set Basic Properties to refresh SensorID
	// dev->SetBasicProperties();

	// if (flag & NEED_UPDATE_CONFIG) {
	// 	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 0xb393
jmp 0xb355
leaq 0x1af3d(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xb385
movq 0x1ac70(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0x11cfd(%rip), %rsi     # 0x1d06f
leaq 0x11d31(%rip), %rdx     # 0x1d0aa
movl $0x35, %ecx
movb $0x0, %al
callq 0x5190
jmp 0xb387
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xb680
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 0xb3b2
jmp 0xb3c8
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
callq *0xa8(%rax)
movl %eax, -0x24(%rbp)
jmp 0xb3cf
movl $0x1, -0x24(%rbp)
jmp 0xb3d1
leaq 0x1aec1(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xb405
movq 0x1abd4(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movl -0x24(%rbp), %r8d
leaq 0x128be(%rip), %rsi     # 0x1dcb0
leaq 0x11cb1(%rip), %rdx     # 0x1d0aa
movl $0x43, %ecx
movb $0x0, %al
callq 0x5190
jmp 0xb407
movq -0x30(%rbp), %rax
testb $0x1, (%rax)
jne 0xb465
movq -0x38(%rbp), %rdi
movq (%rdi), %rax
callq *0x20(%rax)
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0xb463
jmp 0xb425
leaq 0x1ae6d(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xb455
movq 0x1aba0(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0x11c95(%rip), %rsi     # 0x1d0d7
leaq 0x11c61(%rip), %rdx     # 0x1d0aa
movl $0x48, %ecx
movb $0x0, %al
callq 0x5190
jmp 0xb457
movl $0xfffffffd, -0x4(%rbp) # imm = 0xFFFFFFFD
jmp 0xb680
jmp 0xb465
movl -0x24(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0xb56d
movl $0x0, -0x20(%rbp)
movq -0x38(%rbp), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0xb4ce
jmp 0xb490
leaq 0x1ae02(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xb4c0
movq 0x1ab15(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x11cce(%rip), %rsi     # 0x1d17b
leaq 0x11bf6(%rip), %rdx     # 0x1d0aa
movl $0x52, %ecx
movb $0x0, %al
callq 0x5190
jmp 0xb4c2
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x5180
jmp 0xb50c
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5180
leaq 0x1adba(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xb508
movq 0x1aacd(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x11caa(%rip), %rsi     # 0x1d19f
leaq 0x11bae(%rip), %rdx     # 0x1d0aa
movl $0x56, %ecx
movb $0x0, %al
callq 0x5190
jmp 0xb50a
jmp 0xb522
jmp 0xb50e
movl -0x20(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x20(%rbp)
cmpl $0x3, %eax
jl 0xb47b
cmpl $0x0, -0x1c(%rbp)
je 0xb56b
jmp 0xb52a
leaq 0x1ad68(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xb55e
movq 0x1aa9b(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movl -0x1c(%rbp), %r8d
leaq 0x11c79(%rip), %rsi     # 0x1d1c4
leaq 0x11b58(%rip), %rdx     # 0x1d0aa
movl $0x5b, %ecx
movb $0x0, %al
callq 0x5190
jmp 0xb560
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xb680
jmp 0xb56d
movl -0x24(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xb679
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 0xb5dd
jmp 0xb59f
leaq 0x1acf3(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xb5cf
movq 0x1aa06(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x11b50(%rip), %rsi     # 0x1d10c
leaq 0x11ae7(%rip), %rdx     # 0x1d0aa
movl $0x65, %ecx
movb $0x0, %al
callq 0x5190
jmp 0xb5d1
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x5180
jmp 0xb61b
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5180
leaq 0x1acab(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xb617
movq 0x1a9be(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x11b28(%rip), %rsi     # 0x1d12c
leaq 0x11a9f(%rip), %rdx     # 0x1d0aa
movl $0x69, %ecx
movb $0x0, %al
callq 0x5190
jmp 0xb619
jmp 0xb631
jmp 0xb61d
movl -0x20(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x20(%rbp)
cmpl $0x3, %eax
jl 0xb583
cmpl $0x0, -0x1c(%rbp)
je 0xb677
jmp 0xb639
leaq 0x1ac59(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xb66d
movq 0x1a98c(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movl -0x1c(%rbp), %r8d
leaq 0x11309(%rip), %rsi     # 0x1c963
leaq 0x11a49(%rip), %rdx     # 0x1d0aa
movl $0x6e, %ecx
movb $0x0, %al
callq 0x5190
jmp 0xb66f
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xb680
jmp 0xb679
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)