// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
leaq 0x20d7a(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x554b
movq 0x20a8d(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x16acf(%rip), %rsi     # 0x1c004
leaq 0x16b5f(%rip), %r8      # 0x1c09b
movl $0x8c, %ecx
movq %r8, %rdx
movb $0x0, %al
callq 0x5190
jmp 0x554d
movw $0x0, -0x26(%rbp)
movq -0x18(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x51e0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x55ba
leaq 0x16b3b(%rip), %rdi     # 0x1c0ab
callq 0x50c0
leaq 0x20d1d(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x55ad
movq 0x20a50(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movq -0x18(%rbp), %r8
movl -0x24(%rbp), %r9d
leaq 0x16b23(%rip), %rsi     # 0x1c0bd
leaq 0x16afa(%rip), %rdx     # 0x1c09b
movl $0x96, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x55af
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x5a83
movl -0x24(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x50d0
movq %rax, %rcx
movq -0x38(%rbp), %rax
movl %ecx, 0xc(%rax)
movl -0x24(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x50d0
movq -0x38(%rbp), %rcx
xorl %eax, %eax
cmpq 0x30(%rcx), %rax
je 0x5616
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
je 0x560a
movq -0x40(%rbp), %rdi
callq 0x5140
movq -0x38(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x38(%rbp), %rax
movslq 0xc(%rax), %rdi
movq %rdi, -0x50(%rbp)
callq 0x5030
movq -0x50(%rbp), %rdx
movq %rax, %rdi
movq %rdi, -0x48(%rbp)
xorl %esi, %esi
callq 0x50a0
movq -0x48(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x30(%rax)
movl -0x24(%rbp), %edi
movq 0x30(%rax), %rsi
movslq 0xc(%rax), %rdx
callq 0x51a0
movq -0x38(%rbp), %rcx
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl 0xc(%rcx), %eax
je 0x56ac
jmp 0x5666
leaq 0x20c2c(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x569e
movq 0x2093f(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movq -0x18(%rbp), %r8
movl -0x1c(%rbp), %r9d
leaq 0x16a54(%rip), %rsi     # 0x1c0df
leaq 0x16a09(%rip), %rdx     # 0x1c09b
movl $0xa5, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x56a0
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
jmp 0x5a1d
movq -0x38(%rbp), %rcx
movq 0x30(%rcx), %rax
movzbl (%rax), %eax
shll $0x18, %eax
movq 0x30(%rcx), %rdx
movzbl 0x1(%rdx), %edx
shll $0x10, %edx
orl %edx, %eax
movq 0x30(%rcx), %rdx
movzbl 0x2(%rdx), %edx
shll $0x8, %edx
orl %edx, %eax
movq 0x30(%rcx), %rdx
movzbl 0x3(%rdx), %edx
orl %edx, %eax
movl %eax, 0x10(%rcx)
movl 0x10(%rcx), %eax
addl $0x6, %eax
cmpl 0xc(%rcx), %eax
je 0x5770
jmp 0x56f2
leaq 0x20ba0(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x5732
movq -0x38(%rbp), %rax
movq 0x208af(%rip), %rcx     # 0x25fb8
movq (%rcx), %rdi
movl 0x10(%rax), %r8d
addl $0x6, %r8d
movl 0xc(%rax), %r9d
leaq 0x169ef(%rip), %rsi     # 0x1c10e
leaq 0x16975(%rip), %rdx     # 0x1c09b
movl $0xb0, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x5734
jmp 0x5736
leaq 0x20b5c(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x5766
movq 0x2086f(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x169ed(%rip), %rsi     # 0x1c140
leaq 0x16941(%rip), %rdx     # 0x1c09b
movl $0xb1, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x5768
movq -0x38(%rbp), %rax
movb $0x1, 0x18(%rax)
movl $0x6, -0x20(%rbp)
movw $0x0, -0x26(%rbp)
movq -0x38(%rbp), %rcx
movl -0x20(%rbp), %eax
movl 0x10(%rcx), %ecx
addl $0x6, %ecx
cmpl %ecx, %eax
jge 0x57b3
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x20(%rbp), %rcx
movzbl (%rax,%rcx), %ecx
movzwl -0x26(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x26(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x577d
movq -0x38(%rbp), %rdx
movzwl -0x26(%rbp), %eax
movq 0x30(%rdx), %rcx
movzbl 0x4(%rcx), %ecx
shll $0x8, %ecx
movq 0x30(%rdx), %rdx
movzbl 0x5(%rdx), %edx
orl %edx, %ecx
cmpl %ecx, %eax
je 0x5835
jmp 0x57d6
leaq 0x20abc(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x5827
movq -0x38(%rbp), %rax
movq 0x207cb(%rip), %rcx     # 0x25fb8
movq (%rcx), %rdi
movzwl -0x26(%rbp), %r8d
movq 0x30(%rax), %rcx
movzbl 0x4(%rcx), %r9d
shll $0x8, %r9d
movq 0x30(%rax), %rax
movzbl 0x5(%rax), %eax
orl %eax, %r9d
leaq 0x16966(%rip), %rsi     # 0x1c17a
leaq 0x16880(%rip), %rdx     # 0x1c09b
movl $0xba, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x5829
movl $0xfffffffe, -0x1c(%rbp) # imm = 0xFFFFFFFE
jmp 0x5a1d
movq -0x38(%rbp), %rax
testb $0x1, 0x18(%rax)
je 0x59d4
movq -0x38(%rbp), %rcx
movq 0x30(%rcx), %rax
movl 0x10(%rcx), %edx
addl $0x6, %edx
movslq %edx, %rdx
movzbl (%rax,%rdx), %eax
shll $0x8, %eax
movq 0x30(%rcx), %rdx
movl 0x10(%rcx), %esi
addl $0x7, %esi
movslq %esi, %rsi
movzbl (%rdx,%rsi), %edx
addl %edx, %eax
movl %eax, -0x2c(%rbp)
movl 0xc(%rcx), %eax
subl 0x10(%rcx), %eax
subl $0x6, %eax
movl -0x2c(%rbp), %ecx
addl $0x6, %ecx
cmpl %ecx, %eax
je 0x58dc
jmp 0x5886
leaq 0x20a0c(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x58ce
movq -0x38(%rbp), %rax
movq 0x2073b(%rip), %rcx     # 0x25fd8
movq (%rcx), %rdi
movl 0xc(%rax), %r8d
subl 0x10(%rax), %r8d
subl $0x6, %r8d
movl -0x2c(%rbp), %r9d
addl $0x6, %r9d
leaq 0x168ed(%rip), %rsi     # 0x1c1a8
leaq 0x167d9(%rip), %rdx     # 0x1c09b
movl $0xc5, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x58d0
movl $0xfffffffd, -0x1c(%rbp) # imm = 0xFFFFFFFD
jmp 0x5a1d
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
addl $0x6, %eax
addl $0x6, %eax
movl %eax, -0x20(%rbp)
movw $0x0, -0x26(%rbp)
movq -0x38(%rbp), %rcx
movl -0x20(%rbp), %eax
cmpl 0xc(%rcx), %eax
jge 0x5923
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x20(%rbp), %rcx
movzbl (%rax,%rcx), %ecx
movzwl -0x26(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x26(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x58f2
movq -0x38(%rbp), %rsi
movzwl -0x26(%rbp), %eax
movq 0x30(%rsi), %rcx
movl 0x10(%rsi), %edx
addl $0x6, %edx
addl $0x4, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
shll $0x8, %ecx
movq 0x30(%rsi), %rdx
movl 0x10(%rsi), %esi
addl $0x6, %esi
addl $0x5, %esi
movslq %esi, %rsi
movzbl (%rdx,%rsi), %edx
addl %edx, %ecx
cmpl %ecx, %eax
je 0x59d2
jmp 0x595e
leaq 0x20934(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x59c7
movq -0x38(%rbp), %rcx
movq 0x20663(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movzwl -0x26(%rbp), %r8d
movq 0x30(%rcx), %rax
movl 0x10(%rcx), %edx
addl $0x6, %edx
addl $0x4, %edx
movslq %edx, %rdx
movzbl (%rax,%rdx), %r9d
shll $0x8, %r9d
movq 0x30(%rcx), %rax
movl 0x10(%rcx), %ecx
addl $0x6, %ecx
addl $0x5, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
addl %eax, %r9d
leaq 0x16824(%rip), %rsi     # 0x1c1d8
leaq 0x166e0(%rip), %rdx     # 0x1c09b
movl $0xd0, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x59c9
movl $0xfffffffc, -0x1c(%rbp) # imm = 0xFFFFFFFC
jmp 0x5a1d
jmp 0x59d4
movl -0x24(%rbp), %edi
callq 0x5200
leaq 0x208b6(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x5a12
movq 0x205c9(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x16814(%rip), %rsi     # 0x1c20d
leaq 0x1669b(%rip), %r8      # 0x1c09b
movl $0xd7, %ecx
xorl %r9d, %r9d
movq %r8, %rdx
movb $0x0, %al
callq 0x5190
jmp 0x5a14
movl $0x0, -0x4(%rbp)
jmp 0x5a83
movq -0x38(%rbp), %rax
movb $0x0, 0x8(%rax)
movq 0x30(%rax), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
je 0x5a3c
movq -0x58(%rbp), %rdi
callq 0x5140
movl -0x24(%rbp), %edi
callq 0x5200
leaq 0x2084e(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x5a7b
movq 0x20561(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movl -0x1c(%rbp), %r9d
leaq 0x167a8(%rip), %rsi     # 0x1c20d
leaq 0x1662f(%rip), %r8      # 0x1c09b
movl $0xde, %ecx
movq %r8, %rdx
movb $0x0, %al
callq 0x5190
jmp 0x5a7d
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)