// File: /workspace/github_cpp_makefile_star2_O1/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, 0x8(%rsp)
movb %al, 0xc(%rsp)
movb %al, 0x14(%rsp)
movl %eax, 0x10(%rsp)
leaq 0xe34f(%rip), %r12      # 0x21291
cmpb $0x1, (%r12)
jne 0x12f6d
movq 0xe068(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x724f(%rip), %rsi      # 0x1a1a9
leaq 0x725d(%rip), %rdx      # 0x1a1be
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 0x130ec
movl $0x3, %ebp
leaq 0x8(%rsp), %r15
movl $0x1, %r14d
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x5170
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x10010, %esi          # imm = 0x10010
movq %r15, %rdx
movl $0x1, %ecx
callq *0x20(%rax)
cmpl $0x1, %eax
jne 0x12fce
cmpb $-0x23, 0x8(%rsp)
je 0x12fd7
decl %ebp
jne 0x12fa1
xorl %ebp, %ebp
movl %eax, %r14d
movb (%r12), %al
testl %ebp, %ebp
jle 0x13103
testb %al, %al
je 0x1300b
movq 0xdfca(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x723f(%rip), %rsi      # 0x1a237
leaq 0x71bf(%rip), %rdx      # 0x1a1be
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 0x1313b
leaq 0x8(%rsp), %r14
movb $0x55, 0x4(%r14)
movl $0x55555555, (%r14)     # imm = 0x55555555
movl $0xa, %r13d
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 0x1314b
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 0x130aa
decl %r13d
jne 0x1304b
xorl %r13d, %r13d
movb (%r12), %cl
testl %r13d, %r13d
jle 0x1317d
xorl %ebp, %ebp
testb %cl, %cl
je 0x13186
movq 0xdef0(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x7239(%rip), %rsi      # 0x1a30b
leaq 0x70e5(%rip), %rdx      # 0x1a1be
xorl %ebp, %ebp
movl $0xa0, %ecx
xorl %eax, %eax
callq 0x5180
jmp 0x13186
movl %eax, %ebp
cmpb $0x1, (%r12)
jne 0x13186
callq 0x6706
jmp 0x13186
movl $0xffffffea, %ebp       # imm = 0xFFFFFFEA
testb %al, %al
je 0x13186
movq 0xdec5(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
movzbl 0x8(%rsp), %r9d
leaq 0x70d6(%rip), %rsi      # 0x1a1f9
leaq 0x7094(%rip), %rdx      # 0x1a1be
movl $0x80, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x13186
movl %eax, %ebp
cmpb $0x1, (%r12)
jne 0x13186
callq 0x66dc
jmp 0x13186
movl %eax, %ebp
cmpb $0x1, (%r12)
jne 0x13186
movq 0xde7d(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
leaq 0x7132(%rip), %rsi      # 0x1a297
leaq 0x7052(%rip), %rdx      # 0x1a1be
movl $0x93, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x13186
movl $0xffffffea, %ebp       # imm = 0xFFFFFFEA
testb %cl, %cl
jne 0x13197
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10(%rsp), %rdi
movl %eax, %esi
callq 0x6688
jmp 0x13186
nop