// File: goodix[P]gdix_hid_firmware_update/gtx9/gtx9_i2c_update.cpp
// Function: gdix_do_fw_update(char const*, char const*, unsigned char)
int gdix_do_fw_update(const char *devname, const char *filename,
					  uint8_t i2c_addr)
{
	int ret;

	if (!devname || !filename) {
		gdix_err("devname or filename is NULL\n");
		return -1;
	}

	if (!strstr(devname, "dev/i2c")) {
		gdix_err("devname[%s] is invalid\n", devname);
		return -1;
	}

	g_client_addr = i2c_addr;
	gdix_dbg("i2c-addr:0x%02x\n", g_client_addr);
	g_fd = open(devname, O_RDWR);
	if (g_fd < 0) {
		gdix_err("failed to open %s\n", devname);
		return -1;
	}

	ret = switch_i2c_mode();
	if (ret < 0)
		goto err_out;

	ret = gdix_read_firmware(filename);
	if (ret < 0) {
		gdix_err("failed to read %s\n", filename);
		goto err_out;
	}

	gdix_fw_update_proc(&goodix_fw_update_ctrl);

err_out:
	close(g_fd);
	g_fd = 0;
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movb %dl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movb %al, -0x19(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x15133
cmpq $0x0, -0x18(%rbp)
jne 0x15173
jmp 0x15135
leaq 0x1115d(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x15165
movq 0x10e90(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0xa074(%rip), %rsi      # 0x1f1c6
leaq 0xa09b(%rip), %rdx      # 0x1f1f4
movl $0x365, %ecx            # imm = 0x365
movb $0x0, %al
callq 0x5190
jmp 0x15167
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x152f9
movq -0x10(%rbp), %rdi
leaq 0xa088(%rip), %rsi      # 0x1f206
callq 0x5060
cmpq $0x0, %rax
jne 0x151cd
jmp 0x1518b
leaq 0x11107(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x151bf
movq 0x10e3a(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movq -0x10(%rbp), %r8
leaq 0xa062(%rip), %rsi      # 0x1f20e
leaq 0xa041(%rip), %rdx      # 0x1f1f4
movl $0x36a, %ecx            # imm = 0x36A
movb $0x0, %al
callq 0x5190
jmp 0x151c1
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x152f9
movzbl -0x19(%rbp), %eax
movw %ax, 0x110b8(%rip)      # 0x26290
leaq 0x110ba(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x15210
movq 0x10dcd(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movzwl 0x1109a(%rip), %r8d     # 0x26290
leaq 0xa03a(%rip), %rsi      # 0x1f237
leaq 0x9ff0(%rip), %rdx      # 0x1f1f4
movl $0x36f, %ecx            # imm = 0x36F
movb $0x0, %al
callq 0x5190
jmp 0x15212
movq -0x10(%rbp), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x51e0
movl %eax, 0x11078(%rip)     # 0x262a0
cmpl $0x0, 0x11071(%rip)     # 0x262a0
jge 0x15275
jmp 0x15233
leaq 0x1105f(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x15267
movq 0x10d92(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movq -0x10(%rbp), %r8
leaq 0xa005(%rip), %rsi      # 0x1f259
leaq 0x9f99(%rip), %rdx      # 0x1f1f4
movl $0x372, %ecx            # imm = 0x372
movb $0x0, %al
callq 0x5190
jmp 0x15269
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x152f9
callq 0x15310
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jge 0x15285
jmp 0x152dd
movq -0x18(%rbp), %rdi
callq 0x153f0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jge 0x152d1
jmp 0x15299
leaq 0x10ff9(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x152cd
movq 0x10d2c(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movq -0x18(%rbp), %r8
leaq 0x9fc3(%rip), %rsi      # 0x1f27d
leaq 0x9f33(%rip), %rdx      # 0x1f1f4
movl $0x37c, %ecx            # imm = 0x37C
movb $0x0, %al
callq 0x5190
jmp 0x152cf
jmp 0x152dd
leaq 0x51fd8(%rip), %rdi     # 0x672b0
callq 0x156e0
movl 0x10fbd(%rip), %edi     # 0x262a0
callq 0x5200
movl $0x0, 0x10fae(%rip)     # 0x262a0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)