// File: goodix[P]gdix_hid_firmware_update/gtx9/gtx9_firmware_image.cpp
// Function: GTX9FirmwareImage::GetDataFromFile(char const*)
int GTX9FirmwareImage::GetDataFromFile(const char *filename)
{
	int fw_fd;
	int ret;

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

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

	m_firmwareData = new unsigned char[m_totalSize]();

	ret = read(fw_fd, m_firmwareData, m_totalSize);
	if (ret != m_totalSize) {
		gdix_err("Failed read file: %s, ret=%d\n", filename, ret);
		ret = -EINVAL;
		goto err_out;
	}

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

	if (m_firmwareSize < m_totalSize) {
		gdix_dbg("Check firmware size:%d < file size:%d\n", m_firmwareSize,
				 m_totalSize);
		gdix_dbg("This bin file may contain a config bin.\n");
		m_configSize = m_totalSize - m_firmwareSize - 64;
		gdix_dbg("config size:%d\n", m_configSize);
		hasConfig = true;
	}

	close(fw_fd);
	return 0;

err_out:
	delete[] m_firmwareData;
	close(fw_fd);
	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x51e0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0x14b55
jmp 0x14b10
leaq 0x11782(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x14b48
movq 0x114b5(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movq -0x18(%rbp), %r8
movl -0x1c(%rbp), %r9d
leaq 0x7588(%rip), %rsi      # 0x1c0bd
leaq 0x755f(%rip), %rdx      # 0x1c09b
movl $0x49, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x14b4a
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x14d58
movl -0x1c(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x50d0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movl %ecx, 0xc(%rax)
movl -0x1c(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x50d0
movq -0x28(%rbp), %rax
movslq 0xc(%rax), %rdi
movq %rdi, -0x38(%rbp)
callq 0x5030
movq -0x38(%rbp), %rdx
movq %rax, %rdi
movq %rdi, -0x30(%rbp)
xorl %esi, %esi
callq 0x50a0
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x30(%rax)
movl -0x1c(%rbp), %edi
movq 0x30(%rax), %rsi
movslq 0xc(%rax), %rdx
callq 0x51a0
movq -0x28(%rbp), %rcx
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl 0xc(%rcx), %eax
je 0x14c14
jmp 0x14bce
leaq 0x116c4(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x14c06
movq 0x113f7(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movq -0x18(%rbp), %r8
movl -0x20(%rbp), %r9d
leaq 0xa336(%rip), %rsi      # 0x1ef29
leaq 0x74a1(%rip), %rdx      # 0x1c09b
movl $0x54, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x14c08
movl $0xffffffea, -0x20(%rbp) # imm = 0xFFFFFFEA
jmp 0x14d2f
movq -0x28(%rbp), %rcx
movq 0x30(%rcx), %rax
movzbl 0x3(%rax), %eax
shll $0x18, %eax
movq 0x30(%rcx), %rdx
movzbl 0x2(%rdx), %edx
shll $0x10, %edx
orl %edx, %eax
movq 0x30(%rcx), %rdx
movzbl 0x1(%rdx), %edx
shll $0x8, %edx
orl %edx, %eax
movq 0x30(%rcx), %rdx
movzbl (%rdx), %edx
orl %edx, %eax
addl $0x8, %eax
movl %eax, 0x10(%rcx)
movl 0x10(%rcx), %eax
cmpl 0xc(%rcx), %eax
jge 0x14d1e
jmp 0x14c5a
leaq 0x11638(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x14c96
movq -0x28(%rbp), %rax
movq 0x11347(%rip), %rcx     # 0x25fb8
movq (%rcx), %rdi
movl 0x10(%rax), %r8d
movl 0xc(%rax), %r9d
leaq 0xa2d5(%rip), %rsi      # 0x1ef58
leaq 0x7411(%rip), %rdx      # 0x1c09b
movl $0x5f, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x14c98
jmp 0x14c9a
leaq 0x115f8(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x14cca
movq 0x1130b(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x7489(%rip), %rsi      # 0x1c140
leaq 0x73dd(%rip), %rdx      # 0x1c09b
movl $0x60, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x14ccc
movq -0x28(%rbp), %rax
movl 0xc(%rax), %ecx
subl 0x10(%rax), %ecx
subl $0x40, %ecx
movl %ecx, 0x14(%rax)
leaq 0x115b6(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x14d14
movq -0x28(%rbp), %rax
movq 0x112c5(%rip), %rcx     # 0x25fb8
movq (%rcx), %rdi
movl 0x14(%rax), %r8d
leaq 0xa28f(%rip), %rsi      # 0x1ef90
leaq 0x7393(%rip), %rdx      # 0x1c09b
movl $0x62, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x14d16
movq -0x28(%rbp), %rax
movb $0x1, 0x18(%rax)
movl -0x1c(%rbp), %edi
callq 0x5200
movl $0x0, -0x4(%rbp)
jmp 0x14d58
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
je 0x14d4a
movq -0x40(%rbp), %rdi
callq 0x5140
movl -0x1c(%rbp), %edi
callq 0x5200
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)