// File: /workspace/github_cpp_makefile_star2_O3/goodix[P]gdix_hid_firmware_update/gtx9/gtx9_update.cpp
// Function: GTx9Update::prepareUpdate()
int GTx9Update::prepareUpdate()
{
	uint8_t tempBuf[5] = {0};
	uint8_t recvBuf[5] = {0};
	int retry = 3;
	int ret = -1;

	gdix_info("IN\n");

	/* step 1. switch mini system */
	tempBuf[0] = 0x01;
	ret = dev->SendCmd(0x10, tempBuf, 1);
	if (ret < 0) {
		gdix_err("Failed send minisystem cmd\n");
		return ret;
	}
	while (retry--) {
		usleep(200000);
		ret = dev->Read(0x10010, tempBuf, 1);
		if (ret == 1 && tempBuf[0] == 0xDD)
			break;
	}
	if (retry < 0) {
		gdix_err("Failed switch minisystem ret=%d flag=0x%02x\n", ret,
				 tempBuf[0]);
		return -EINVAL;
	}
	gdix_info("Switch mini system successfully\n");

	/* step 2. erase flash */
	tempBuf[0] = 0x01;
	ret = dev->SendCmd(0x11, tempBuf, 1);
	if (ret < 0) {
		gdix_err("Failed send erase flash cmd\n");
		return ret;
	}

	retry = 10;
	memset(tempBuf, 0x55, 5);
	while (retry--) {
		usleep(10000);
		ret = dev->Write(0x14000, tempBuf, 5);
		if (ret < 0) {
			gdix_err("Failed write sram, ret=%d\n", ret);
			return ret;
		}
		ret = dev->Read(0x14000, recvBuf, 5);
		if (!memcmp(tempBuf, recvBuf, 5))
			break;
	}
	if (retry < 0) {
		gdix_err("Read back failed, ret=%d buf:%02x %02x %02x %02x %02x\n", ret,
				 recvBuf[0], recvBuf[1], recvBuf[2], recvBuf[3], recvBuf[4]);
		return -EINVAL;
	}

	gdix_info("Updata prepare OK\n");
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
xorl %eax, %eax
movl %eax, 0x9(%rsp)
movl %eax, 0x10(%rsp)
movb $0x0, 0x14(%rsp)
leaq 0xe4fe(%rip), %r12      # 0x21291
cmpb $0x1, (%r12)
jne 0x12dbe
movq 0xe217(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x73be(%rip), %rsi      # 0x1a169
leaq 0x73cc(%rip), %rdx      # 0x1a17e
movl $0x6f, %ecx
xorl %eax, %eax
callq 0x5180
leaq 0x8(%rsp), %rdx
movb $0x1, (%rdx)
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x10, %esi
movl $0x1, %ecx
callq *0x48(%rax)
testl %eax, %eax
js 0x12e67
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
leaq 0x8(%rsp), %r14
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x5170
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x10010, %esi          # imm = 0x10010
movq %r14, %rdx
movl $0x1, %ecx
callq *0x20(%rax)
cmpl $0x1, %eax
sete %dl
movb 0x8(%rsp), %cl
cmpb $-0x23, %cl
sete %sil
testb %sil, %dl
jne 0x12e7e
incl %ebp
jne 0x12dec
movl $0xffffffea, %ebp       # imm = 0xFFFFFFEA
cmpb $0x1, (%r12)
jne 0x12fcc
movzbl %cl, %r9d
movq 0xe196(%rip), %rcx      # 0x20fd8
movq (%rcx), %rdi
leaq 0x736d(%rip), %rsi      # 0x1a1b9
leaq 0x732b(%rip), %rdx      # 0x1a17e
movl $0x80, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x12fcc
movl %eax, %ebp
cmpb $0x1, (%r12)
jne 0x12fcc
callq 0x66b2
jmp 0x12fcc
cmpb $0x1, (%r12)
jne 0x12ea9
movq 0xe12c(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x7361(%rip), %rsi      # 0x1a1f7
leaq 0x72e1(%rip), %rdx      # 0x1a17e
movl $0x83, %ecx
xorl %eax, %eax
callq 0x5180
leaq 0x8(%rsp), %rdx
movb $0x1, (%rdx)
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x11, %esi
movl $0x1, %ecx
callq *0x48(%rax)
testl %eax, %eax
js 0x12f5b
leaq 0x8(%rsp), %r14
movb $0x55, 0x4(%r14)
movl $0x55555555, (%r14)     # imm = 0x55555555
movl $0xfffffff6, %r13d      # imm = 0xFFFFFFF6
leaq 0x10(%rsp), %r15
movl $0x2710, %edi           # imm = 0x2710
callq 0x5170
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x14000, %esi          # imm = 0x14000
movq %r14, %rdx
movl $0x5, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0x12f6b
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x14000, %esi          # imm = 0x14000
movq %r15, %rdx
movl $0x5, %ecx
callq *0x20(%rax)
movl 0x8(%rsp), %ecx
xorl 0x10(%rsp), %ecx
movb 0xc(%rsp), %dl
xorb 0x14(%rsp), %dl
movzbl %dl, %edx
orl %ecx, %edx
je 0x12f9d
incl %r13d
jne 0x12ee9
movl $0xffffffea, %ebp       # imm = 0xFFFFFFEA
cmpb $0x1, (%r12)
jne 0x12fcc
leaq 0x10(%rsp), %rdi
movl %eax, %esi
callq 0x6634
jmp 0x12fcc
movl %eax, %ebp
cmpb $0x1, (%r12)
jne 0x12fcc
callq 0x6688
jmp 0x12fcc
movl %eax, %ebp
cmpb $0x1, (%r12)
jne 0x12fcc
movq 0xe05d(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
leaq 0x72d2(%rip), %rsi      # 0x1a257
leaq 0x71f2(%rip), %rdx      # 0x1a17e
movl $0x93, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x12fcc
xorl %ebp, %ebp
cmpb $0x1, (%r12)
jne 0x12fcc
movq 0xe00b(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x7314(%rip), %rsi      # 0x1a2cb
leaq 0x71c0(%rip), %rdx      # 0x1a17e
xorl %ebp, %ebp
movl $0xa0, %ecx
xorl %eax, %eax
callq 0x5180
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop