// File: goodix[P]gdix_hid_firmware_update/gtx9/gtx9_i2c_update.cpp
// Function: gdix_read_firmware(char const*)
static int gdix_read_firmware(const char *filename)
{
	int ret;
	int fw_fd;
	int totalSize;
	int fwSize;
	int cfgSize;
	uint8_t *firmwareData;

	fw_fd = open(filename, O_RDONLY);
	if (fw_fd < 0) {
		gdix_err("file:%s, ret:%d\n", filename, fw_fd);
		return fw_fd;
	}

	totalSize = lseek(fw_fd, 0, SEEK_END);
	lseek(fw_fd, 0, SEEK_SET);

	firmwareData = new unsigned char[totalSize]();
	ret = read(fw_fd, firmwareData, totalSize);
	if (ret != totalSize) {
		gdix_err("Failed read file: %s, ret=%d\n", filename, ret);
		ret = -1;
		goto err_out;
	}

	fwSize = ((firmwareData[3] << 24) | (firmwareData[2] << 16) |
			  (firmwareData[1] << 8) | firmwareData[0]) +
			 8;

	g_firmware.size = fwSize;
	memcpy(g_firmware.data, firmwareData, fwSize);
	goodix_fw_update_ctrl.fw_data.firmware = &g_firmware;

	if (fwSize < totalSize) {
		gdix_dbg("Check firmware size:%d < file size:%d\n", fwSize, totalSize);
		gdix_dbg("This bin file may contain a config bin.\n");
		cfgSize = totalSize - fwSize - 64;
		gdix_dbg("config size:%d\n", cfgSize);
		g_config.len = cfgSize;
		memcpy(g_config.data, firmwareData + fwSize + 64, cfgSize);
		goodix_fw_update_ctrl.ic_config = &g_config;
	}

	gdix_dbg("read firmware success\n");
	close(fw_fd);
	return 0;
err_out:
	delete[] firmwareData;
	close(fw_fd);
	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x51e0
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jge 0x15459
jmp 0x15414
leaq 0x10e7e(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x1544c
movq 0x10bb1(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movq -0x10(%rbp), %r8
movl -0x18(%rbp), %r9d
leaq 0x6c84(%rip), %rsi      # 0x1c0bd
leaq 0x9f37(%rip), %rdx      # 0x1f377
movl $0x140, %ecx            # imm = 0x140
movb $0x0, %al
callq 0x5190
jmp 0x1544e
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x156ca
movl -0x18(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x50d0
movl %eax, -0x1c(%rbp)
movl -0x18(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x50d0
movslq -0x1c(%rbp), %rdi
movq %rdi, -0x40(%rbp)
callq 0x5030
movq -0x40(%rbp), %rdx
movq %rax, %rdi
movq %rdi, -0x38(%rbp)
xorl %esi, %esi
callq 0x50a0
movq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0x18(%rbp), %edi
movq -0x30(%rbp), %rsi
movslq -0x1c(%rbp), %rdx
callq 0x51a0
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x1c(%rbp), %eax
je 0x15505
jmp 0x154bf
leaq 0x10dd3(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x154f7
movq 0x10b06(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movq -0x10(%rbp), %r8
movl -0x14(%rbp), %r9d
leaq 0x9a45(%rip), %rsi      # 0x1ef29
leaq 0x9e8c(%rip), %rdx      # 0x1f377
movl $0x14a, %ecx            # imm = 0x14A
movb $0x0, %al
callq 0x5190
jmp 0x154f9
movl $0xffffffff, -0x14(%rbp) # imm = 0xFFFFFFFF
jmp 0x156a5
movq -0x30(%rbp), %rax
movzbl 0x3(%rax), %eax
shll $0x18, %eax
movq -0x30(%rbp), %rcx
movzbl 0x2(%rcx), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movq -0x30(%rbp), %rcx
movzbl 0x1(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movq -0x30(%rbp), %rcx
movzbl (%rcx), %ecx
orl %ecx, %eax
addl $0x8, %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movl %eax, 0x10d62(%rip)     # 0x262a4
movq -0x30(%rbp), %rsi
movslq -0x20(%rbp), %rdx
leaq 0x10d53(%rip), %rdi     # 0x262a4
addq $0x4, %rdi
callq 0x50b0
leaq 0x10d43(%rip), %rax     # 0x262a4
movq %rax, 0x521d8(%rip)     # 0x67740
movl -0x20(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x15660
jmp 0x15576
leaq 0x10d1c(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x155ae
movq 0x10a2f(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movl -0x20(%rbp), %r8d
movl -0x1c(%rbp), %r9d
leaq 0x99bd(%rip), %rsi      # 0x1ef58
leaq 0x9dd5(%rip), %rdx      # 0x1f377
movl $0x158, %ecx            # imm = 0x158
movb $0x0, %al
callq 0x5190
jmp 0x155b0
jmp 0x155b2
leaq 0x10ce0(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x155e2
movq 0x109f3(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x6b71(%rip), %rsi      # 0x1c140
leaq 0x9da1(%rip), %rdx      # 0x1f377
movl $0x159, %ecx            # imm = 0x159
movb $0x0, %al
callq 0x5190
jmp 0x155e4
movl -0x1c(%rbp), %eax
subl -0x20(%rbp), %eax
subl $0x40, %eax
movl %eax, -0x24(%rbp)
leaq 0x10ca2(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x15624
movq 0x109b5(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movl -0x24(%rbp), %r8d
leaq 0x997f(%rip), %rsi      # 0x1ef90
leaq 0x9d5f(%rip), %rdx      # 0x1f377
movl $0x15b, %ecx            # imm = 0x15B
movb $0x0, %al
callq 0x5190
jmp 0x15626
movl -0x24(%rbp), %eax
movl %eax, 0x50c79(%rip)     # 0x662a8
movq -0x30(%rbp), %rsi
movslq -0x20(%rbp), %rax
addq %rax, %rsi
addq $0x40, %rsi
movslq -0x24(%rbp), %rdx
leaq 0x50c5f(%rip), %rdi     # 0x662a8
addq $0x4, %rdi
callq 0x50b0
leaq 0x50c4f(%rip), %rax     # 0x662a8
movq %rax, 0x520e8(%rip)     # 0x67748
jmp 0x15662
leaq 0x10c30(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x15692
movq 0x10943(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x9d0b(%rip), %rsi      # 0x1f38a
leaq 0x9cf1(%rip), %rdx      # 0x1f377
movl $0x161, %ecx            # imm = 0x161
movb $0x0, %al
callq 0x5190
jmp 0x15694
movl -0x18(%rbp), %edi
callq 0x5200
movl $0x0, -0x4(%rbp)
jmp 0x156ca
movq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
je 0x156bc
movq -0x48(%rbp), %rdi
callq 0x5140
movl -0x18(%rbp), %edi
callq 0x5200
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)