// File: /workspace/github_cpp_makefile_star2_O3/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 0x1120f
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
cmpl %eax, 0xc(%r14)
jne 0x11247
movq 0x30(%r14), %rcx
movl (%rcx), %r8d
addl $0x8, %r8d
movl %r8d, 0x10(%r14)
cmpl %eax, %r8d
jge 0x11204
leaq 0x10129(%rip), %r15     # 0x21291
movb (%r15), %cl
cmpb $0x1, %cl
jne 0x1119a
movq 0xfe41(%rip), %rcx      # 0x20fb8
movq (%rcx), %rdi
leaq 0x7d99(%rip), %rsi      # 0x18f1a
leaq 0x4f13(%rip), %rdx      # 0x1609b
movl $0x5f, %ecx
movl %eax, %r9d
xorl %eax, %eax
callq 0x5180
movb (%r15), %cl
testb $0x1, %cl
je 0x111c6
movq 0xfe12(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x4f90(%rip), %rsi      # 0x16140
leaq 0x4ee4(%rip), %rdx      # 0x1609b
movl $0x60, %ecx
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 0x111ff
movq 0xfdd6(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x7d66(%rip), %rsi      # 0x18f52
leaq 0x4ea8(%rip), %rdx      # 0x1609b
movl $0x62, %ecx
xorl %eax, %eax
callq 0x5180
movb $0x1, 0x18(%r14)
movl %ebx, %edi
callq 0x5210
xorl %ebx, %ebx
jmp 0x1126d
leaq 0x1007b(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x1126d
movq 0xfdb6(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
leaq 0x4e91(%rip), %rsi      # 0x160bd
leaq 0x4e68(%rip), %rdx      # 0x1609b
movl $0x49, %ecx
movq %r15, %r8
movl %ebx, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x1126d
leaq 0x10043(%rip), %rcx     # 0x21291
cmpb $0x1, (%rcx)
je 0x11279
movq 0x30(%r14), %rdi
testq %rdi, %rdi
je 0x11261
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 0xfd58(%rip), %rcx      # 0x20fd8
movq (%rcx), %rdi
leaq 0x7c61(%rip), %rsi      # 0x18eeb
leaq 0x4e0a(%rip), %rdx      # 0x1609b
movl $0x54, %ecx
movq %r15, %r8
movl %eax, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x11253
nop