// File: /workspace/github_cpp_makefile_star2_O2/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 0x1ae31(%rip), %r12     # 0x20291
cmpb $0x1, (%r12)
jne 0x548e
movq 0x1ab4a(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xfb8c(%rip), %rsi      # 0x15004
leaq 0xfc1c(%rip), %rdx      # 0x1509b
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 0x5714
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
movq 0x30(%r14), %rdi
testq %rdi, %rdi
je 0x54d2
callq 0x5130
andq $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 0x55ad
movq 0x30(%r14), %rcx
movl (%rcx), %edx
bswapl %edx
movl %edx, 0x10(%r14)
leal 0x6(%rdx), %r8d
cmpl %eax, %r8d
leaq 0x1ad6e(%rip), %r12     # 0x20291
je 0x5593
movb (%r12), %cl
cmpb $0x1, %cl
jne 0x5559
movq 0x1aa83(%rip), %rcx     # 0x1ffb8
movq (%rcx), %rdi
leaq 0xfbcf(%rip), %rsi      # 0x1510e
leaq 0xfb55(%rip), %rdx      # 0x1509b
movl $0xb0, %ecx
movl %eax, %r9d
xorl %eax, %eax
callq 0x5180
movb (%r12), %cl
testb $0x1, %cl
je 0x5582
movq 0x1aa53(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xfbd1(%rip), %rsi      # 0x15140
leaq 0xfb25(%rip), %rdx      # 0x1509b
movl $0xb1, %ecx
xorl %eax, %eax
callq 0x5180
movb $0x1, 0x18(%r14)
movl 0x10(%r14), %edx
movq 0x30(%r14), %rcx
leal 0x6(%rdx), %r8d
movslq %r8d, %rax
pushq $0x6
popq %rdi
xorl %esi, %esi
cmpq %rax, %rdi
jge 0x55f1
movzbl (%rcx,%rdi), %r8d
addl %r8d, %esi
incq %rdi
jmp 0x559b
pushq $-0x1
popq %rbp
leaq 0x1acda(%rip), %r12     # 0x20291
cmpb $0x1, (%r12)
jne 0x5756
movq 0x1a9ef(%rip), %rcx     # 0x1ffb8
movq (%rcx), %rdi
leaq 0xfb0c(%rip), %rsi      # 0x150df
leaq 0xfac1(%rip), %rdx      # 0x1509b
movl $0xa5, %ecx
movq %r15, %r8
movl %eax, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x5756
movzwl %si, %r8d
movzwl 0x4(%rcx), %esi
rolw $0x8, %si
movzwl %si, %r9d
cmpl %r8d, %r9d
jne 0x5655
cmpb $0x1, 0x18(%r14)
jne 0x56cf
movzbl (%rcx,%rax), %edi
shll $0x8, %edi
movslq %edx, %rax
movzbl 0x7(%rcx,%rax), %r9d
movslq 0xc(%r14), %rsi
movl %esi, %r8d
subl %eax, %r8d
addl $-0x6, %r8d
addl %edi, %r9d
addl $0x6, %r9d
cmpl %r9d, %r8d
jne 0x568c
addl $0xc, %edx
movslq %edx, %rdi
xorl %edx, %edx
cmpq %rsi, %rdi
jge 0x56b9
movzbl (%rcx,%rdi), %r8d
addl %r8d, %edx
incq %rdi
jmp 0x5643
pushq $-0x2
popq %rbp
cmpb $0x1, (%r12)
jne 0x5756
movq 0x1a94e(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xfb06(%rip), %rsi      # 0x1517a
leaq 0xfa20(%rip), %rdx      # 0x1509b
movl $0xba, %ecx
xorl %eax, %eax
callq 0x5180
jmp 0x5756
pushq $-0x3
popq %rbp
cmpb $0x1, (%r12)
jne 0x5756
movq 0x1a937(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xfafd(%rip), %rsi      # 0x151a8
leaq 0xf9e9(%rip), %rdx      # 0x1509b
movl $0xc5, %ecx
jmp 0x5680
movzwl %dx, %r8d
movzwl 0xa(%rcx,%rax), %eax
rolw $0x8, %ax
movzwl %ax, %r9d
cmpl %r8d, %r9d
jne 0x574c
movl %ebx, %edi
callq 0x5210
xorl %ebx, %ebx
cmpb $0x1, (%r12)
jne 0x57a3
movq 0x1a8ce(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xfb19(%rip), %rsi      # 0x1520d
leaq 0xf9a0(%rip), %rdx      # 0x1509b
xorl %ebx, %ebx
movl $0xd7, %ecx
movq %rdx, %r8
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x57a3
leaq 0xf990(%rip), %rdi      # 0x150ab
callq 0x50a0
cmpb $0x1, (%r12)
jne 0x57a3
movq 0x1a8aa(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xf985(%rip), %rsi      # 0x150bd
leaq 0xf95c(%rip), %rdx      # 0x1509b
movl $0x96, %ecx
movq %r15, %r8
movl %ebx, %r9d
jmp 0x5708
pushq $-0x4
popq %rbp
cmpb $0x1, (%r12)
je 0x57b4
movb $0x0, 0x8(%r14)
movq 0x30(%r14), %rdi
testq %rdi, %rdi
je 0x5769
callq 0x5130
movl %ebx, %edi
callq 0x5210
cmpb $0x1, (%r12)
jne 0x57a1
movq 0x1a83a(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xfa85(%rip), %rsi      # 0x1520d
leaq 0xf90c(%rip), %rdx      # 0x1509b
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 0x1a81d(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xfa13(%rip), %rsi      # 0x151d8
leaq 0xf8cf(%rip), %rdx      # 0x1509b
movl $0xd0, %ecx
jmp 0x5680