// File: /workspace/github_cpp_makefile_star2_O2/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
andl $0x0, 0x11(%rsp)
movq %rdi, %r14
andl $0x0, 0x8(%rsp)
movb $0x0, 0xc(%rsp)
leaq 0xc382(%rip), %r13      # 0x20291
cmpb $0x1, (%r13)
jne 0x13f38
movq 0xc09b(%rip), %rax      # 0x1ffb8
movq (%rax), %rdi
leaq 0x5242(%rip), %rsi      # 0x19169
leaq 0x5250(%rip), %rdx      # 0x1917e
pushq $0x71
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 0x13fff
pushq $-0x4
popq %r12
leaq 0x10(%rsp), %r15
pushq $0x1
popq %rbp
incl %r12d
je 0x1402a
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x5170
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movl $0x5095, %esi           # imm = 0x5095
movq %r15, %rdx
movl %ebp, %ecx
callq *0x20(%rax)
movl %eax, %ebx
cmpl $0x1, %eax
jne 0x13f66
cmpb $-0x23, 0x10(%rsp)
jne 0x13f66
cmpb $0x1, (%r13)
jne 0x13fc6
movq 0xc00f(%rip), %rax      # 0x1ffb8
movq (%rax), %rdi
leaq 0x5244(%rip), %rsi      # 0x191f7
leaq 0x51c4(%rip), %rdx      # 0x1917e
movl $0x85, %ecx
xorl %eax, %eax
callq 0x5180
leaq 0x10(%rsp), %r15
movb $0x1, (%r15)
pushq $0x3
popq %r13
pushq $0x11
popq %r12
movl %ebp, %ebx
subl $0x1, %r13d
jb 0x1403f
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movl %r12d, %esi
movq %r15, %rdx
movl %ebp, %ecx
callq *0x48(%rax)
movl %eax, %ebx
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x5170
jmp 0x13fd9
cmpb $0x1, (%r13)
jne 0x1419e
movq 0xbfc7(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x5171(%rip), %rsi      # 0x1918c
leaq 0x515c(%rip), %rdx      # 0x1917e
pushq $0x77
popq %rcx
jmp 0x140ec
pushq $-0x16
popq %rbp
cmpb $0x1, (%r13)
je 0x141af
movl %ebp, %ebx
jmp 0x1419e
testl %ebx, %ebx
js 0x140f8
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 0x14127
movl $0x2710, %edi           # imm = 0x2710
callq 0x5170
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movl $0x29400, %esi          # imm = 0x29400
movq %r15, %rdx
movl %ebp, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0x14190
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movl $0x29400, %esi          # imm = 0x29400
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 0x14064
xorl %ebx, %ebx
leaq 0xc1cd(%rip), %rax      # 0x20291
cmpb $0x1, (%rax)
jne 0x1419e
movq 0xbee4(%rip), %rax      # 0x1ffb8
movq (%rax), %rdi
leaq 0x51ed(%rip), %rsi      # 0x192cb
leaq 0x5099(%rip), %rdx      # 0x1917e
xorl %ebx, %ebx
movl $0xa5, %ecx
xorl %eax, %eax
callq 0x5180
jmp 0x1419e
leaq 0xc192(%rip), %rax      # 0x20291
cmpb $0x1, (%rax)
jne 0x1419e
movq 0xbec9(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x5110(%rip), %rsi      # 0x19229
leaq 0x505e(%rip), %rdx      # 0x1917e
movl $0x8e, %ecx
jmp 0x140ec
pushq $-0x16
popq %rbp
leaq 0xc160(%rip), %rax      # 0x20291
cmpb $0x1, (%rax)
jne 0x14038
movq 0xbe97(%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 0x511a(%rip), %rsi      # 0x19283
leaq 0x500e(%rip), %rdx      # 0x1917e
movl $0xa1, %ecx
movl %ebx, %r8d
xorl %eax, %eax
pushq %r15
pushq %r14
pushq %r11
pushq %r10
callq 0x5180
addq $0x20, %rsp
jmp 0x14038
movl %eax, %ebx
leaq 0xc0f8(%rip), %rax      # 0x20291
cmpb $0x1, (%rax)
je 0x141e1
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xbe22(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
movzbl 0x10(%rsp), %r9d
leaq 0x4ff3(%rip), %rsi      # 0x191b9
leaq 0x4fb1(%rip), %rdx      # 0x1917e
movl $0x82, %ecx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x14038
movq 0xbdf0(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x5065(%rip), %rsi      # 0x19257
leaq 0x4f85(%rip), %rdx      # 0x1917e
movl $0x98, %ecx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x1419e