// File: goodix[P]gdix_hid_firmware_update/gtx9/gtx9.cpp
// Function: GTx9Device::SendConfig(unsigned char*, int)
int GTx9Device::SendConfig(unsigned char *config, int len)
{
	int ret;
	uint8_t cmdBuf[5] = {0};
	uint8_t tmpBuf[4096] = {0};

	/* start write cfg */
	cmdBuf[0] = 0x04;
	ret = WriteSpeCmd(cmdBuf, 1);
	if (ret < 0) {
		gdix_err("Failed write cfg prepare cmd\n");
		return ret;
	}

	/* try send config */
	ret = Write(0x13B74, config, len);
	if (ret < 0) {
		gdix_err("Failed write config data\n");
		goto exit;
	}

	/* read back config */
	ret = Read(0x13B74, tmpBuf, len);
	if (ret < 0) {
		gdix_err("Failed read back config\n");
		goto exit;
	}

	if (memcmp(config, tmpBuf, len)) {
		gdix_err("Config data read back compare file\n");
		ret = -EINVAL;
		goto exit;
	}
	cmdBuf[0] = 0x05;
	ret = WriteSpeCmd(cmdBuf, 1);
	if (ret < 0)
		gdix_err("Failed send config data ready cmd\n");

exit:
	cmdBuf[0] = 0x06;
	ret = WriteSpeCmd(cmdBuf, 1);
	if (ret < 0)
		gdix_err("Failed send config write end command\n");

	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1040, %rsp           # imm = 0x1040
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1038(%rbp)
leaq -0x25(%rbp), %rdi
xorl %esi, %esi
movl $0x5, %edx
callq 0x50a0
leaq -0x1030(%rbp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x50a0
movq -0x1038(%rbp), %rdi
movb $0x4, -0x25(%rbp)
leaq -0x25(%rbp), %rsi
movq (%rdi), %rax
movl $0x1, %edx
callq *0x40(%rax)
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jge 0x13ca6
jmp 0x13c69
leaq 0x12629(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x13c99
movq 0x1235c(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0xaf64(%rip), %rsi      # 0x1ebea
leaq 0xaf8c(%rip), %rdx      # 0x1ec19
movl $0xf9, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x13c9b
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x13e6f
movq -0x1038(%rbp), %rdi
movq -0x18(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movq (%rdi), %rax
movl $0x13b74, %esi          # imm = 0x13B74
callq *0x30(%rax)
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jge 0x13d01
jmp 0x13cca
leaq 0x125c8(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x13cfa
movq 0x122fb(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0xaf3d(%rip), %rsi      # 0x1ec24
leaq 0xaf2b(%rip), %rdx      # 0x1ec19
movl $0x100, %ecx            # imm = 0x100
movb $0x0, %al
callq 0x5190
jmp 0x13cfc
jmp 0x13e10
movq -0x1038(%rbp), %rdi
leaq -0x1030(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movq (%rdi), %rax
movl $0x13b74, %esi          # imm = 0x13B74
callq *0x20(%rax)
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jge 0x13d5f
jmp 0x13d28
leaq 0x1256a(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x13d58
movq 0x1229d(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0xaf0a(%rip), %rsi      # 0x1ec4f
leaq 0xaecd(%rip), %rdx      # 0x1ec19
movl $0x107, %ecx            # imm = 0x107
movb $0x0, %al
callq 0x5190
jmp 0x13d5a
jmp 0x13e10
movq -0x18(%rbp), %rdi
leaq -0x1030(%rbp), %rsi
movslq -0x1c(%rbp), %rdx
callq 0x5090
cmpl $0x0, %eax
je 0x13db5
jmp 0x13d7a
leaq 0x12518(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x13daa
movq 0x1224b(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0xaee2(%rip), %rsi      # 0x1ec79
leaq 0xae7b(%rip), %rdx      # 0x1ec19
movl $0x10c, %ecx            # imm = 0x10C
movb $0x0, %al
callq 0x5190
jmp 0x13dac
movl $0xffffffea, -0x20(%rbp) # imm = 0xFFFFFFEA
jmp 0x13e10
movq -0x1038(%rbp), %rdi
movb $0x5, -0x25(%rbp)
leaq -0x25(%rbp), %rsi
movq (%rdi), %rax
movl $0x1, %edx
callq *0x40(%rax)
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jge 0x13e0e
jmp 0x13dda
leaq 0x124b8(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x13e0a
movq 0x121eb(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0xaeb7(%rip), %rsi      # 0x1ecae
leaq 0xae1b(%rip), %rdx      # 0x1ec19
movl $0x113, %ecx            # imm = 0x113
movb $0x0, %al
callq 0x5190
jmp 0x13e0c
jmp 0x13e0e
jmp 0x13e10
movq -0x1038(%rbp), %rdi
movb $0x6, -0x25(%rbp)
leaq -0x25(%rbp), %rsi
movq (%rdi), %rax
movl $0x1, %edx
callq *0x40(%rax)
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jge 0x13e69
jmp 0x13e35
leaq 0x1245d(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x13e65
movq 0x12190(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0xae90(%rip), %rsi      # 0x1ece2
leaq 0xadc0(%rip), %rdx      # 0x1ec19
movl $0x119, %ecx            # imm = 0x119
movb $0x0, %al
callq 0x5190
jmp 0x13e67
jmp 0x13e69
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1040, %rsp           # imm = 0x1040
popq %rbp
retq
nopl (%rax,%rax)