// File: /workspace/github_cpp_makefile_star2_O3/goodix[P]gdix_hid_firmware_update/berlin_a/brla_update.cpp
// Function: BrlAUpdate::prepareUpdate()
int BrlAUpdate::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(0x5095, 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;
	for (int i = 0; i < 3; i++) {
		ret = dev->SendCmd(0x11, tempBuf, 1);
		usleep(2000);
	}
	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(ISP_RAM_ADDR, tempBuf, 5);
		if (ret < 0) {
			gdix_err("Failed write sram, ret=%d\n", ret);
			return ret;
		}
		ret = dev->Read(ISP_RAM_ADDR, 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 0xc6a2(%rip), %r12      # 0x21291
cmpb $0x1, (%r12)
jne 0x14c1a
movq 0xc3bb(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x5562(%rip), %rsi      # 0x1a169
leaq 0x5570(%rip), %rdx      # 0x1a17e
movl $0x71, %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 0x14cc3
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
leaq 0x8(%rsp), %r14
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x5170
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x5095, %esi           # imm = 0x5095
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 0x14cda
incl %ebp
jne 0x14c48
movl $0xffffffea, %ebp       # imm = 0xFFFFFFEA
cmpb $0x1, (%r12)
jne 0x14e41
movzbl %cl, %r9d
movq 0xc33a(%rip), %rcx      # 0x20fd8
movq (%rcx), %rdi
leaq 0x5511(%rip), %rsi      # 0x1a1b9
leaq 0x54cf(%rip), %rdx      # 0x1a17e
movl $0x82, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x14e41
movl %eax, %ebp
cmpb $0x1, (%r12)
jne 0x14e41
callq 0x6b65
jmp 0x14e41
cmpb $0x1, (%r12)
jne 0x14d05
movq 0xc2d0(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x5505(%rip), %rsi      # 0x1a1f7
leaq 0x5485(%rip), %rdx      # 0x1a17e
movl $0x85, %ecx
xorl %eax, %eax
callq 0x5180
leaq 0x8(%rsp), %r14
movb $0x1, (%r14)
movl $0x3, %r15d
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x11, %esi
movq %r14, %rdx
movl $0x1, %ecx
callq *0x48(%rax)
movl %eax, %ebp
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x5170
decl %r15d
jne 0x14d14
testl %ebp, %ebp
js 0x14dd2
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 $0x29400, %esi          # imm = 0x29400
movq %r14, %rdx
movl $0x5, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0x14de0
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x29400, %esi          # imm = 0x29400
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 0x14e12
incl %r13d
jne 0x14d60
movl $0xffffffea, %ebp       # imm = 0xFFFFFFEA
cmpb $0x1, (%r12)
jne 0x14e41
leaq 0x10(%rsp), %rdi
movl %eax, %esi
callq 0x6ae7
jmp 0x14e41
cmpb $0x1, (%r12)
jne 0x14e41
callq 0x6b3b
jmp 0x14e41
movl %eax, %ebp
cmpb $0x1, (%r12)
jne 0x14e41
movq 0xc1e8(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
leaq 0x545d(%rip), %rsi      # 0x1a257
leaq 0x537d(%rip), %rdx      # 0x1a17e
movl $0x98, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x14e41
xorl %ebp, %ebp
cmpb $0x1, (%r12)
jne 0x14e41
movq 0xc196(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x549f(%rip), %rsi      # 0x1a2cb
leaq 0x534b(%rip), %rdx      # 0x1a17e
xorl %ebp, %ebp
movl $0xa5, %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