// File: 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
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 0xba5f(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x1a863
movq 0xb772(%rip), %rax      # 0x25fb8
movq (%rax), %rdi
leaq 0x595f(%rip), %rsi      # 0x201af
leaq 0x596d(%rip), %rdx      # 0x201c4
movl $0x71, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x1a865
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 0x1a8cd
jmp 0x1a890
leaq 0xba02(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x1a8c0
movq 0xb735(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
leaq 0x5925(%rip), %rsi      # 0x201d2
leaq 0x5910(%rip), %rdx      # 0x201c4
movl $0x77, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x1a8c2
movl -0x2c(%rbp), %eax
movl %eax, -0xc(%rbp)
jmp 0x1abb1
jmp 0x1a8cf
movl -0x28(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x28(%rbp)
cmpl $0x0, %eax
je 0x1a91d
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x5180
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x1d(%rbp), %rdx
movq (%rdi), %rax
movl $0x5095, %esi           # imm = 0x5095
movl $0x1, %ecx
callq *0x20(%rax)
movl %eax, -0x2c(%rbp)
cmpl $0x1, -0x2c(%rbp)
jne 0x1a91b
movzbl -0x1d(%rbp), %eax
cmpl $0xdd, %eax
jne 0x1a91b
jmp 0x1a91d
jmp 0x1a8cf
cmpl $0x0, -0x28(%rbp)
jge 0x1a96c
jmp 0x1a925
leaq 0xb96d(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x1a95e
movq 0xb6a0(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
movl -0x2c(%rbp), %r8d
movzbl -0x1d(%rbp), %r9d
leaq 0x58b4(%rip), %rsi      # 0x201ff
leaq 0x5872(%rip), %rdx      # 0x201c4
movl $0x82, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x1a960
movl $0xffffffea, -0xc(%rbp) # imm = 0xFFFFFFEA
jmp 0x1abb1
jmp 0x1a96e
leaq 0xb924(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x1a99e
movq 0xb637(%rip), %rax      # 0x25fb8
movq (%rax), %rdi
leaq 0x58b2(%rip), %rsi      # 0x2023d
leaq 0x5832(%rip), %rdx      # 0x201c4
movl $0x85, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x1a9a0
movb $0x1, -0x1d(%rbp)
movl $0x0, -0x30(%rbp)
cmpl $0x3, -0x30(%rbp)
jge 0x1a9e5
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x1d(%rbp), %rdx
movq (%rdi), %rax
movl $0x11, %esi
movl $0x1, %ecx
callq *0x48(%rax)
movl %eax, -0x2c(%rbp)
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x5180
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1a9ab
cmpl $0x0, -0x2c(%rbp)
jge 0x1aa2a
jmp 0x1a9ed
leaq 0xb8a5(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x1aa1d
movq 0xb5d8(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
leaq 0x5865(%rip), %rsi      # 0x2026f
leaq 0x57b3(%rip), %rdx      # 0x201c4
movl $0x8e, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x1aa1f
movl -0x2c(%rbp), %eax
movl %eax, -0xc(%rbp)
jmp 0x1abb1
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 0x1ab07
movl $0x2710, %edi           # imm = 0x2710
callq 0x5180
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x1d(%rbp), %rdx
movq (%rdi), %rax
movl $0x29400, %esi          # imm = 0x29400
movl $0x5, %ecx
callq *0x30(%rax)
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jge 0x1aaca
jmp 0x1aa89
leaq 0xb809(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x1aabd
movq 0xb53c(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
movl -0x2c(%rbp), %r8d
leaq 0x57f3(%rip), %rsi      # 0x2029d
leaq 0x5713(%rip), %rdx      # 0x201c4
movl $0x98, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x1aabf
movl -0x2c(%rbp), %eax
movl %eax, -0xc(%rbp)
jmp 0x1abb1
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x22(%rbp), %rdx
movq (%rdi), %rax
movl $0x29400, %esi          # imm = 0x29400
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 0x1ab02
jmp 0x1ab07
jmp 0x1aa44
cmpl $0x0, -0x28(%rbp)
jge 0x1ab76
jmp 0x1ab0f
leaq 0xb783(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x1ab6b
movq 0xb4b6(%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 0x5782(%rip), %rsi      # 0x202c9
leaq 0x5676(%rip), %rdx      # 0x201c4
movl $0xa1, %ecx
movl %ebx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movl %eax, 0x18(%rsp)
movb $0x0, %al
callq 0x5190
jmp 0x1ab6d
movl $0xffffffea, -0xc(%rbp) # imm = 0xFFFFFFEA
jmp 0x1abb1
jmp 0x1ab78
leaq 0xb71a(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x1aba8
movq 0xb42d(%rip), %rax      # 0x25fb8
movq (%rax), %rdi
leaq 0x577c(%rip), %rsi      # 0x20311
leaq 0x5628(%rip), %rdx      # 0x201c4
movl $0xa5, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x1abaa
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
addq $0x58, %rsp
popq %rbx
popq %rbp
retq
nopl (%rax,%rax)