// File: 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
movq %rsp, %rbp
pushq %rbx
subq $0x58, %rsp
movq %rdi, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
leaq -0x1d(%rbp), %rdi
xorl %esi, %esi
movl $0x5, %edx
callq 0x50a0
leaq -0x22(%rbp), %rdi
xorl %esi, %esi
movl $0x5, %edx
callq 0x50a0
movl $0x3, -0x28(%rbp)
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
leaq 0xe8cf(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x179f3
movq 0xe5e2(%rip), %rax      # 0x25fb8
movq (%rax), %rdi
leaq 0x87cf(%rip), %rsi      # 0x201af
leaq 0x87dd(%rip), %rdx      # 0x201c4
movl $0x6f, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x179f5
movq -0x38(%rbp), %rax
movb $0x1, -0x1d(%rbp)
movq 0x8(%rax), %rdi
leaq -0x1d(%rbp), %rdx
movq (%rdi), %rax
movl $0x10, %esi
movl $0x1, %ecx
callq *0x48(%rax)
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jge 0x17a5d
jmp 0x17a20
leaq 0xe872(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x17a50
movq 0xe5a5(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
leaq 0x8795(%rip), %rsi      # 0x201d2
leaq 0x8780(%rip), %rdx      # 0x201c4
movl $0x75, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x17a52
movl -0x2c(%rbp), %eax
movl %eax, -0xc(%rbp)
jmp 0x17d1f
jmp 0x17a5f
movl -0x28(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x28(%rbp)
cmpl $0x0, %eax
je 0x17aad
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x5180
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x1d(%rbp), %rdx
movq (%rdi), %rax
movl $0x10010, %esi          # imm = 0x10010
movl $0x1, %ecx
callq *0x20(%rax)
movl %eax, -0x2c(%rbp)
cmpl $0x1, -0x2c(%rbp)
jne 0x17aab
movzbl -0x1d(%rbp), %eax
cmpl $0xdd, %eax
jne 0x17aab
jmp 0x17aad
jmp 0x17a5f
cmpl $0x0, -0x28(%rbp)
jge 0x17afc
jmp 0x17ab5
leaq 0xe7dd(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x17aee
movq 0xe510(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
movl -0x2c(%rbp), %r8d
movzbl -0x1d(%rbp), %r9d
leaq 0x8724(%rip), %rsi      # 0x201ff
leaq 0x86e2(%rip), %rdx      # 0x201c4
movl $0x80, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x17af0
movl $0xffffffea, -0xc(%rbp) # imm = 0xFFFFFFEA
jmp 0x17d1f
jmp 0x17afe
leaq 0xe794(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x17b2e
movq 0xe4a7(%rip), %rax      # 0x25fb8
movq (%rax), %rdi
leaq 0x8722(%rip), %rsi      # 0x2023d
leaq 0x86a2(%rip), %rdx      # 0x201c4
movl $0x83, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x17b30
movq -0x38(%rbp), %rax
movb $0x1, -0x1d(%rbp)
movq 0x8(%rax), %rdi
leaq -0x1d(%rbp), %rdx
movq (%rdi), %rax
movl $0x11, %esi
movl $0x1, %ecx
callq *0x48(%rax)
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jge 0x17b98
jmp 0x17b5b
leaq 0xe737(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x17b8b
movq 0xe46a(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
leaq 0x86f7(%rip), %rsi      # 0x2026f
leaq 0x8645(%rip), %rdx      # 0x201c4
movl $0x89, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x17b8d
movl -0x2c(%rbp), %eax
movl %eax, -0xc(%rbp)
jmp 0x17d1f
movl $0xa, -0x28(%rbp)
leaq -0x1d(%rbp), %rdi
movl $0x55, %esi
movl $0x5, %edx
callq 0x50a0
movl -0x28(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x28(%rbp)
cmpl $0x0, %eax
je 0x17c75
movl $0x2710, %edi           # imm = 0x2710
callq 0x5180
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x1d(%rbp), %rdx
movq (%rdi), %rax
movl $0x14000, %esi          # imm = 0x14000
movl $0x5, %ecx
callq *0x30(%rax)
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jge 0x17c38
jmp 0x17bf7
leaq 0xe69b(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x17c2b
movq 0xe3ce(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
movl -0x2c(%rbp), %r8d
leaq 0x8685(%rip), %rsi      # 0x2029d
leaq 0x85a5(%rip), %rdx      # 0x201c4
movl $0x93, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x17c2d
movl -0x2c(%rbp), %eax
movl %eax, -0xc(%rbp)
jmp 0x17d1f
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x22(%rbp), %rdx
movq (%rdi), %rax
movl $0x14000, %esi          # imm = 0x14000
movl $0x5, %ecx
callq *0x20(%rax)
movl %eax, -0x2c(%rbp)
leaq -0x1d(%rbp), %rdi
leaq -0x22(%rbp), %rsi
movl $0x5, %edx
callq 0x5090
cmpl $0x0, %eax
jne 0x17c70
jmp 0x17c75
jmp 0x17bb2
cmpl $0x0, -0x28(%rbp)
jge 0x17ce4
jmp 0x17c7d
leaq 0xe615(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x17cd9
movq 0xe348(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
movl -0x2c(%rbp), %r8d
movzbl -0x22(%rbp), %r9d
movzbl -0x21(%rbp), %ebx
movzbl -0x20(%rbp), %r11d
movzbl -0x1f(%rbp), %r10d
movzbl -0x1e(%rbp), %eax
leaq 0x8614(%rip), %rsi      # 0x202c9
leaq 0x8508(%rip), %rdx      # 0x201c4
movl $0x9c, %ecx
movl %ebx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movl %eax, 0x18(%rsp)
movb $0x0, %al
callq 0x5190
jmp 0x17cdb
movl $0xffffffea, -0xc(%rbp) # imm = 0xFFFFFFEA
jmp 0x17d1f
jmp 0x17ce6
leaq 0xe5ac(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x17d16
movq 0xe2bf(%rip), %rax      # 0x25fb8
movq (%rax), %rdi
leaq 0x860e(%rip), %rsi      # 0x20311
leaq 0x84ba(%rip), %rdx      # 0x201c4
movl $0xa0, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x17d18
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
addq $0x58, %rsp
popq %rbx
popq %rbp
retq
nopl (%rax)