// File: /workspace/github_cpp_makefile_star2_O2/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
andl $0x0, 0x11(%rsp)
movq %rdi, %r14
andl $0x0, 0x8(%rsp)
movb $0x0, 0xc(%rsp)
leaq 0xe476(%rip), %r13      # 0x20291
cmpb $0x1, (%r13)
jne 0x11e44
movq 0xe18f(%rip), %rax      # 0x1ffb8
movq (%rax), %rdi
leaq 0x7336(%rip), %rsi      # 0x19169
leaq 0x7344(%rip), %rdx      # 0x1917e
pushq $0x6f
popq %rcx
xorl %eax, %eax
callq 0x5180
leaq 0x10(%rsp), %rdx
movb $0x1, (%rdx)
movq 0x8(%r14), %rdi
movq (%rdi), %rax
pushq $0x10
popq %rsi
pushq $0x1
popq %rcx
callq *0x48(%rax)
movl %eax, %ebx
testl %eax, %eax
js 0x11f9b
pushq $-0x4
popq %r12
leaq 0x10(%rsp), %r15
pushq $0x1
popq %rbp
incl %r12d
je 0x11fc3
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x5170
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movl $0x10010, %esi          # imm = 0x10010
movq %r15, %rdx
movl %ebp, %ecx
callq *0x20(%rax)
movl %eax, %ebx
cmpl $0x1, %eax
jne 0x11e72
cmpb $-0x23, 0x10(%rsp)
jne 0x11e72
cmpb $0x1, (%r13)
jne 0x11ed2
movq 0xe103(%rip), %rax      # 0x1ffb8
movq (%rax), %rdi
leaq 0x7338(%rip), %rsi      # 0x191f7
leaq 0x72b8(%rip), %rdx      # 0x1917e
movl $0x83, %ecx
xorl %eax, %eax
callq 0x5180
leaq 0x10(%rsp), %rdx
movb $0x1, (%rdx)
movq 0x8(%r14), %rdi
movq (%rdi), %rax
pushq $0x11
popq %rsi
pushq $0x1
popq %rcx
callq *0x48(%rax)
movl %eax, %ebx
testl %eax, %eax
js 0x11fd8
leaq 0x10(%rsp), %r15
movb $0x55, 0x4(%r15)
movl $0x55555555, (%r15)     # imm = 0x55555555
pushq $-0xb
popq %r13
pushq $0x5
popq %rbp
leaq 0x8(%rsp), %r12
incl %r13d
je 0x12009
movl $0x2710, %edi           # imm = 0x2710
callq 0x5170
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movl $0x14000, %esi          # imm = 0x14000
movq %r15, %rdx
movl %ebp, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0x1206e
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movl $0x14000, %esi          # imm = 0x14000
movq %r12, %rdx
movl %ebp, %ecx
callq *0x20(%rax)
movl %eax, %ebx
movq %r15, %rdi
movq %r12, %rsi
pushq $0x5
popq %rdx
callq 0x50f0
testl %eax, %eax
jne 0x11f11
xorl %ebx, %ebx
leaq 0xe320(%rip), %rax      # 0x20291
cmpb $0x1, (%rax)
jne 0x1207c
movq 0xe037(%rip), %rax      # 0x1ffb8
movq (%rax), %rdi
leaq 0x7340(%rip), %rsi      # 0x192cb
leaq 0x71ec(%rip), %rdx      # 0x1917e
xorl %ebx, %ebx
movl $0xa0, %ecx
jmp 0x12000
cmpb $0x1, (%r13)
jne 0x1207c
movq 0xe02b(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x71d5(%rip), %rsi      # 0x1918c
leaq 0x71c0(%rip), %rdx      # 0x1917e
pushq $0x75
popq %rcx
jmp 0x12000
pushq $-0x16
popq %rbp
cmpb $0x1, (%r13)
je 0x1208d
movl %ebp, %ebx
jmp 0x1207c
cmpb $0x1, (%r13)
jne 0x1207c
movq 0xdfee(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x7235(%rip), %rsi      # 0x19229
leaq 0x7183(%rip), %rdx      # 0x1917e
movl $0x89, %ecx
xorl %eax, %eax
callq 0x5180
jmp 0x1207c
pushq $-0x16
popq %rbp
leaq 0xe27e(%rip), %rax      # 0x20291
cmpb $0x1, (%rax)
jne 0x11fd1
movq 0xdfb9(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
movzbl 0x8(%rsp), %r9d
movzbl 0x9(%rsp), %r10d
movzbl 0xa(%rsp), %r11d
movzbl 0xb(%rsp), %r14d
movzbl 0xc(%rsp), %r15d
leaq 0x723c(%rip), %rsi      # 0x19283
leaq 0x7130(%rip), %rdx      # 0x1917e
movl $0x9c, %ecx
movl %ebx, %r8d
xorl %eax, %eax
pushq %r15
pushq %r14
pushq %r11
pushq %r10
callq 0x5180
addq $0x20, %rsp
jmp 0x11fd1
movl %eax, %ebx
leaq 0xe21a(%rip), %rax      # 0x20291
cmpb $0x1, (%rax)
je 0x120bf
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xdf44(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
movzbl 0x10(%rsp), %r9d
leaq 0x7115(%rip), %rsi      # 0x191b9
leaq 0x70d3(%rip), %rdx      # 0x1917e
movl $0x80, %ecx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x11fd1
movq 0xdf12(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x7187(%rip), %rsi      # 0x19257
leaq 0x70a7(%rip), %rdx      # 0x1917e
movl $0x93, %ecx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x1207c