// File: /workspace/github_cpp_makefile_star2_O2/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rsi, %r15
movq %rdi, %r14
movq %rsi, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x51e0
movl %eax, %ebx
testl %eax, %eax
js 0xffa7
pushq $0x2
popq %rdx
movl %ebx, %edi
xorl %esi, %esi
callq 0x50b0
movl %eax, 0xc(%r14)
movl %ebx, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x50b0
movslq 0xc(%r14), %r12
movq %r12, %rdi
callq 0x5030
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x5080
movq %r13, 0x30(%r14)
movl %ebx, %edi
movq %r13, %rsi
movq %r12, %rdx
callq 0x5190
cmpl %eax, 0xc(%r14)
jne 0xffdd
movq 0x30(%r14), %rcx
movl (%rcx), %r8d
addl $0x8, %r8d
movl %r8d, 0x10(%r14)
cmpl %eax, %r8d
jge 0xff9c
leaq 0x1038b(%rip), %r15     # 0x20291
movb (%r15), %cl
cmpb $0x1, %cl
jne 0xff36
movq 0x100a3(%rip), %rcx     # 0x1ffb8
movq (%rcx), %rdi
leaq 0x7ffb(%rip), %rsi      # 0x17f1a
leaq 0x5175(%rip), %rdx      # 0x1509b
pushq $0x5f
popq %rcx
movl %eax, %r9d
xorl %eax, %eax
callq 0x5180
movb (%r15), %cl
testb $0x1, %cl
je 0xff60
movq 0x10076(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0x51f4(%rip), %rsi      # 0x15140
leaq 0x5148(%rip), %rdx      # 0x1509b
pushq $0x60
popq %rcx
xorl %eax, %eax
callq 0x5180
movb (%r15), %cl
movl 0xc(%r14), %r8d
subl 0x10(%r14), %r8d
addl $-0x40, %r8d
movl %r8d, 0x14(%r14)
testb $0x1, %cl
je 0xff97
movq 0x1003c(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0x7fcc(%rip), %rsi      # 0x17f52
leaq 0x510e(%rip), %rdx      # 0x1509b
pushq $0x62
popq %rcx
xorl %eax, %eax
callq 0x5180
movb $0x1, 0x18(%r14)
movl %ebx, %edi
callq 0x5210
xorl %ebx, %ebx
jmp 0x10001
leaq 0x102e3(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0x10001
movq 0x1001e(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0x50f9(%rip), %rsi      # 0x150bd
leaq 0x50d0(%rip), %rdx      # 0x1509b
pushq $0x49
popq %rcx
movq %r15, %r8
movl %ebx, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x10001
leaq 0x102ad(%rip), %rcx     # 0x20291
cmpb $0x1, (%rcx)
je 0x1000d
movq 0x30(%r14), %rdi
testq %rdi, %rdi
je 0xfff7
callq 0x5130
movl %ebx, %edi
callq 0x5210
pushq $-0x16
popq %rbx
movl %ebx, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq 0xffc4(%rip), %rcx      # 0x1ffd8
movq (%rcx), %rdi
leaq 0x7ecd(%rip), %rsi      # 0x17eeb
leaq 0x5076(%rip), %rdx      # 0x1509b
pushq $0x54
popq %rcx
movq %r15, %r8
movl %eax, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0xffe9
nop