// File: /workspace/github_cpp_makefile_star2_O1/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 0x11349
movl %ebx, %edi
xorl %esi, %esi
movl $0x2, %edx
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
movl 0xc(%r14), %r9d
cmpl %eax, %r9d
jne 0x11381
movq 0x30(%r14), %rax
movl (%rax), %r8d
addl $0x8, %r8d
movl %r8d, 0x10(%r14)
cmpl %r9d, %r8d
jge 0x1133e
leaq 0xffe6(%rip), %r15      # 0x21291
cmpb $0x1, (%r15)
jne 0x112d5
movq 0xfd00(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x7c92(%rip), %rsi      # 0x18f54
leaq 0x4dd2(%rip), %rdx      # 0x1609b
movl $0x5f, %ecx
xorl %eax, %eax
callq 0x5180
cmpb $0x1, (%r15)
jne 0x112ff
movq 0xfcd6(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x4e54(%rip), %rsi      # 0x16140
leaq 0x4da8(%rip), %rdx      # 0x1609b
movl $0x60, %ecx
xorl %eax, %eax
callq 0x5180
movl 0xc(%r14), %r8d
subl 0x10(%r14), %r8d
addl $-0x40, %r8d
movl %r8d, 0x14(%r14)
cmpb $0x1, (%r15)
jne 0x11339
movq 0xfc9c(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x7c66(%rip), %rsi      # 0x18f8c
leaq 0x4d6e(%rip), %rdx      # 0x1609b
movl $0x62, %ecx
xorl %eax, %eax
callq 0x5180
movb $0x1, 0x18(%r14)
movl %ebx, %edi
callq 0x5210
xorl %ebx, %ebx
jmp 0x113a7
leaq 0xff41(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
jne 0x113a7
movq 0xfc7c(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
leaq 0x4d57(%rip), %rsi      # 0x160bd
leaq 0x4d2e(%rip), %rdx      # 0x1609b
movl $0x49, %ecx
movq %r15, %r8
movl %ebx, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x113a7
leaq 0xff09(%rip), %rcx      # 0x21291
cmpb $0x1, (%rcx)
je 0x113b3
movq 0x30(%r14), %rdi
testq %rdi, %rdi
je 0x1139b
callq 0x5130
movl %ebx, %edi
callq 0x5210
movl $0xffffffea, %ebx       # imm = 0xFFFFFFEA
movl %ebx, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq 0xfc1e(%rip), %rcx      # 0x20fd8
movq (%rcx), %rdi
leaq 0x7b61(%rip), %rsi      # 0x18f25
leaq 0x4cd0(%rip), %rdx      # 0x1609b
movl $0x54, %ecx
movq %r15, %r8
movl %eax, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x1138d
nop