// File: /workspace/github_cpp_makefile_star2_O1/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, 0x8(%rsp)
movb %al, 0xc(%rsp)
movb %al, 0x14(%rsp)
movl %eax, 0x10(%rsp)
leaq 0xc4d3(%rip), %r12      # 0x21291
cmpb $0x1, (%r12)
jne 0x14de9
movq 0xc1ec(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x53d3(%rip), %rsi      # 0x1a1a9
leaq 0x53e1(%rip), %rdx      # 0x1a1be
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 0x14f83
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 $0x5095, %esi           # imm = 0x5095
movq %r15, %rdx
movl $0x1, %ecx
callq *0x20(%rax)
cmpl $0x1, %eax
jne 0x14e4a
cmpb $-0x23, 0x8(%rsp)
je 0x14e53
decl %ebp
jne 0x14e1d
xorl %ebp, %ebp
movl %eax, %r14d
movb (%r12), %al
testl %ebp, %ebp
jle 0x14f9a
testb %al, %al
je 0x14e87
movq 0xc14e(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x53c3(%rip), %rsi      # 0x1a237
leaq 0x5343(%rip), %rdx      # 0x1a1be
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 0x14e96
testl %ebp, %ebp
js 0x14fd2
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 $0x29400, %esi          # imm = 0x29400
movq %r14, %rdx
movl $0x5, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0x14fe0
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 0x14f41
decl %r13d
jne 0x14ee2
xorl %r13d, %r13d
movb (%r12), %cl
testl %r13d, %r13d
jle 0x15012
xorl %ebp, %ebp
testb %cl, %cl
je 0x1501b
movq 0xc059(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x53a2(%rip), %rsi      # 0x1a30b
leaq 0x524e(%rip), %rdx      # 0x1a1be
xorl %ebp, %ebp
movl $0xa5, %ecx
xorl %eax, %eax
callq 0x5180
jmp 0x1501b
movl %eax, %ebp
cmpb $0x1, (%r12)
jne 0x1501b
callq 0x6be3
jmp 0x1501b
movl $0xffffffea, %ebp       # imm = 0xFFFFFFEA
testb %al, %al
je 0x1501b
movq 0xc02e(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
movzbl 0x8(%rsp), %r9d
leaq 0x523f(%rip), %rsi      # 0x1a1f9
leaq 0x51fd(%rip), %rdx      # 0x1a1be
movl $0x82, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x1501b
cmpb $0x1, (%r12)
jne 0x1501b
callq 0x6bb9
jmp 0x1501b
movl %eax, %ebp
cmpb $0x1, (%r12)
jne 0x1501b
movq 0xbfe8(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
leaq 0x529d(%rip), %rsi      # 0x1a297
leaq 0x51bd(%rip), %rdx      # 0x1a1be
movl $0x98, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x1501b
movl $0xffffffea, %ebp       # imm = 0xFFFFFFEA
testb %cl, %cl
jne 0x1502c
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 0x6b65
jmp 0x1501b