// File: /workspace/github_cpp_makefile_star2_O3/goodix[P]gdix_hid_firmware_update/firmware_image.cpp
// Function: FirmwareImage::GetDataFromFile(char const*)
int FirmwareImage::GetDataFromFile(const char *filename)
{
	gdix_dbg("FirmwareImage %s run\n", __func__);

	int ret, i;
	int fw_fd;
	unsigned short check_sum = 0;

	fw_fd = open(filename, O_RDONLY);

	if (fw_fd < 0) {
		perror("Cannot open file\n");
		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);

	if (NULL != m_firmwareData) {
		delete[] m_firmwareData;
		m_firmwareData = NULL;
	}
	m_firmwareData = new unsigned char[m_totalSize]();

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

	// firmware
	m_firmwareSize = m_firmwareData[0] << 24 | m_firmwareData[1] << 16 |
					 m_firmwareData[2] << 8 | m_firmwareData[3];

	if (m_firmwareSize + 6 != m_totalSize) {
		gdix_dbg("Check file len unequal %d != %d\n", m_firmwareSize + 6,
				 m_totalSize);
		gdix_dbg("This bin file may contain a config bin.\n");
		hasConfig = true;
	}

	for (i = 6, check_sum = 0; i < m_firmwareSize + 6; i++)
		check_sum += m_firmwareData[i];

	if (check_sum != (m_firmwareData[4] << 8 | m_firmwareData[5])) {
		gdix_dbg("Check_sum err  0x%x != 0x%x\n", check_sum,
				 (m_firmwareData[4] << 8 | m_firmwareData[5]));
		ret = -2;
		goto err_out;
	}

	// config
	if (hasConfig) {
		int cfgpackLen = (m_firmwareData[m_firmwareSize + 6] << 8) +
						 m_firmwareData[m_firmwareSize + 7];
		if (m_totalSize - m_firmwareSize - 6 != cfgpackLen + 6) {
			gdix_err("config pack len error,%d != %d",
					 m_totalSize - m_firmwareSize - 6, cfgpackLen + 6);
			ret = -3;
			goto err_out;
		}

		for (i = m_firmwareSize + 6 + 6, check_sum = 0; i < m_totalSize; i++)
			check_sum += m_firmwareData[i];
		if (check_sum != (m_firmwareData[m_firmwareSize + 6 + 4] << 8) +
							 m_firmwareData[m_firmwareSize + 6 + 5]) {
			gdix_err("config pack checksum error,%d != %d", check_sum,
					 (m_firmwareData[m_firmwareSize + 6 + 4] << 8) +
						 m_firmwareData[m_firmwareSize + 6 + 5]);
			ret = -4;
			goto err_out;
		}
	}

	close(fw_fd);
	gdix_dbg("FirmwareImage %s exit,exit code:%d\n", __func__, 0);
	return 0;

err_out:
	m_initialized = false;
	delete[] m_firmwareData;
	close(fw_fd);
	gdix_dbg("FirmwareImage %s exit,exit code:%d\n", __func__, ret);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r15
movq %rdi, %r14
leaq 0x1a413(%rip), %r12     # 0x21291
cmpb $0x1, (%r12)
jne 0x6eac
movq 0x1a12c(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xf16e(%rip), %rsi      # 0x16004
leaq 0xf1fe(%rip), %rdx      # 0x1609b
movl $0x8c, %ecx
movq %rdx, %r8
xorl %eax, %eax
callq 0x5180
movq %r15, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x51e0
movl %eax, %ebx
testl %eax, %eax
js 0x7160
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
movq 0x30(%r14), %rdi
testq %rdi, %rdi
je 0x6ef5
callq 0x5130
movq $0x0, 0x30(%r14)
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 0x6fe3
movq 0x30(%r14), %rcx
movl (%rcx), %edx
bswapl %edx
movl %edx, 0x10(%r14)
leal 0x6(%rdx), %r8d
cmpl %eax, %r8d
leaq 0x1a34b(%rip), %r12     # 0x21291
je 0x6fb6
movb (%r12), %cl
cmpb $0x1, %cl
jne 0x6f7c
movq 0x1a060(%rip), %rcx     # 0x20fb8
movq (%rcx), %rdi
leaq 0xf1ac(%rip), %rsi      # 0x1610e
leaq 0xf132(%rip), %rdx      # 0x1609b
movl $0xb0, %ecx
movl %eax, %r9d
xorl %eax, %eax
callq 0x5180
movb (%r12), %cl
testb $0x1, %cl
je 0x6fa5
movq 0x1a030(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xf1ae(%rip), %rsi      # 0x16140
leaq 0xf102(%rip), %rdx      # 0x1609b
movl $0xb1, %ecx
xorl %eax, %eax
callq 0x5180
movb $0x1, 0x18(%r14)
movl 0x10(%r14), %edx
movq 0x30(%r14), %rcx
leal 0x6(%rdx), %r8d
testl %edx, %edx
jle 0x7029
cmpl $0x8, %r8d
movl $0x7, %eax
cmovgel %r8d, %eax
movl $0x6, %edi
xorl %esi, %esi
movzbl (%rcx,%rdi), %r9d
addl %r9d, %esi
incq %rdi
cmpq %rdi, %rax
jne 0x6fce
movzwl %si, %eax
jmp 0x702b
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
leaq 0x1a2a2(%rip), %r12     # 0x21291
cmpb $0x1, (%r12)
jne 0x71ab
movq 0x19fb7(%rip), %rcx     # 0x20fb8
movq (%rcx), %rdi
leaq 0xf0d4(%rip), %rsi      # 0x160df
leaq 0xf089(%rip), %rdx      # 0x1609b
movl $0xa5, %ecx
movq %r15, %r8
movl %eax, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x71ab
xorl %eax, %eax
movzwl 0x4(%rcx), %esi
rolw $0x8, %si
movzwl %si, %r9d
cmpl %eax, %r9d
jne 0x70fc
cmpb $0x1, 0x18(%r14)
jne 0x70b7
movslq %r8d, %rax
movzbl (%rcx,%rax), %r9d
shll $0x8, %r9d
movslq %edx, %rax
movzbl 0x7(%rcx,%rax), %r10d
movl 0xc(%r14), %edi
movl %edi, %esi
subl %edx, %esi
leal -0x6(%rsi), %r8d
addl %r10d, %r9d
addl $0x6, %r9d
cmpl %r9d, %r8d
jne 0x7135
leaq 0xc(%rax), %rdx
xorl %r8d, %r8d
cmpl %edi, %edx
jge 0x70a1
addq %rcx, %rdx
addl $-0xc, %esi
xorl %edi, %edi
xorl %r8d, %r8d
movzbl (%rdx,%rdi), %r9d
addl %r9d, %r8d
incq %rdi
cmpl %edi, %esi
jne 0x708e
movzwl %r8w, %r8d
movzwl 0xa(%rcx,%rax), %eax
rolw $0x8, %ax
movzwl %ax, %r9d
cmpl %r8d, %r9d
jne 0x719f
movl %ebx, %edi
callq 0x5210
xorl %ebx, %ebx
cmpb $0x1, (%r12)
jne 0x71f8
movq 0x19ee6(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xf131(%rip), %rsi      # 0x1620d
leaq 0xefb8(%rip), %rdx      # 0x1609b
xorl %ebx, %ebx
movl $0xd7, %ecx
movq %rdx, %r8
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x71f8
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
cmpb $0x1, (%r12)
jne 0x71ab
movq 0x19ea5(%rip), %rcx     # 0x20fb8
movq (%rcx), %rdi
leaq 0xf05d(%rip), %rsi      # 0x1617a
leaq 0xef77(%rip), %rdx      # 0x1609b
movl $0xba, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x71ab
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
cmpb $0x1, (%r12)
jne 0x71ab
movq 0x19e90(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xf056(%rip), %rsi      # 0x161a8
leaq 0xef42(%rip), %rdx      # 0x1609b
movl $0xc5, %ecx
jmp 0x712c
leaq 0xef44(%rip), %rdi      # 0x160ab
callq 0x50a0
cmpb $0x1, (%r12)
jne 0x71f8
movq 0x19e5a(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xef35(%rip), %rsi      # 0x160bd
leaq 0xef0c(%rip), %rdx      # 0x1609b
movl $0x96, %ecx
movq %r15, %r8
movl %ebx, %r9d
jmp 0x70f0
movl $0xfffffffc, %ebp       # imm = 0xFFFFFFFC
cmpb $0x1, (%r12)
je 0x7209
movb $0x0, 0x8(%r14)
movq 0x30(%r14), %rdi
testq %rdi, %rdi
je 0x71be
callq 0x5130
movl %ebx, %edi
callq 0x5210
cmpb $0x1, (%r12)
jne 0x71f6
movq 0x19de5(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xf030(%rip), %rsi      # 0x1620d
leaq 0xeeb7(%rip), %rdx      # 0x1609b
movl $0xde, %ecx
movq %rdx, %r8
movl %ebp, %r9d
xorl %eax, %eax
callq 0x5180
movl %ebp, %ebx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x19dc8(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xefbe(%rip), %rsi      # 0x161d8
leaq 0xee7a(%rip), %rdx      # 0x1609b
movl $0xd0, %ecx
jmp 0x712c
nop