// File: /workspace/github_cpp_makefile_star2_O3/goodix[P]gdix_hid_firmware_update/gtx9/gtx9_firmware_image.cpp
// Function: GTX9FirmwareImage::ParseFirmware()
int GTX9FirmwareImage::ParseFirmware()
{
	struct firmware_summary *fw_summary = &m_firmwareSummary;
	uint32_t checksum = 0;
	uint32_t fw_offset;
	uint32_t info_offset;
	uint8_t cfg_ver = 0;
	uint8_t tmp_buf[9] = {0};
	int i;

	memcpy(fw_summary, m_firmwareData, sizeof(*fw_summary));

	/* check firmware size */
	if (m_firmwareSize != (int)(fw_summary->size + 8)) {
		gdix_err("Bad firmware, size not match, %d != %d\n", m_firmwareSize,
				 fw_summary->size + 8);
		return -EINVAL;
	}

	for (i = 8; i < m_firmwareSize; i += 2)
		checksum += m_firmwareData[i] + (m_firmwareData[i + 1] << 8);

	/* byte order change, and check */
	if (checksum != fw_summary->checksum) {
		gdix_err("Bad firmware, checksum error\n");
		return -EINVAL;
	}

	if (fw_summary->subsys_num > FW_SUBSYS_MAX_NUM) {
		gdix_err("Bad firmware, invalid subsys num:%d\n",
				 fw_summary->subsys_num);
		return -EINVAL;
	}

	/* parse subsystem info */
	fw_offset = FW_HEADER_SIZE;
	for (i = 0; i < fw_summary->subsys_num; i++) {
		info_offset = FW_SUBSYS_INFO_OFFSET + i * FW_SUBSYS_INFO_SIZE;
		fw_summary->subsys[i].type = m_firmwareData[info_offset];
		fw_summary->subsys[i].size =
			*(uint32_t *)&m_firmwareData[info_offset + 1];
		fw_summary->subsys[i].flash_addr =
			*(uint32_t *)&m_firmwareData[info_offset + 5];
		if ((int)fw_offset > m_firmwareSize) {
			gdix_err("Sybsys offset exceed Firmware size\n");
			return -EINVAL;
		}
		fw_summary->subsys[i].data = m_firmwareData + fw_offset;
		fw_offset += fw_summary->subsys[i].size;
	}

	memcpy(tmp_buf, fw_summary->fw_pid, 8);
	gdix_info("Firmware package protocol: V%u\n", fw_summary->protocol_ver);
	gdix_info("Firmware PID:GT%s\n", tmp_buf);
	gdix_info("Firmware VID:%02x %02x %02x %02x\n", fw_summary->fw_vid[0],
			  fw_summary->fw_vid[1], fw_summary->fw_vid[2],
			  fw_summary->fw_vid[3]);
	gdix_info("Firmware chip type:%02X\n", fw_summary->chip_type);
	gdix_info("Firmware size:%u\n", fw_summary->size);
	gdix_info("Firmware subsystem num:%u\n", fw_summary->subsys_num);

	memcpy(m_firmwarePID, fw_summary->fw_pid, 8);
	memcpy(m_firmwareVID, fw_summary->fw_vid, 4);

	if (hasConfig) {
		cfg_ver = m_firmwareData[m_firmwareSize + 64 + 34];
		gdix_info("cfg_ver:%02x\n", cfg_ver);
	}

	// m_firmwareVersionMajor = m_firmwareVID[1];
	m_firmwareVersionMajor = 0;
	m_firmwareVersionMinor =
		(m_firmwareVID[2] << 16) | (m_firmwareVID[3] << 8) | cfg_ver;

#if 0
	for (i = 0; i < fw_summary->subsys_num; i++) {
		gdix_dbg("------------------------------------------\n");
		gdix_dbg("Index:%d\n", i);
		gdix_dbg("Subsystem type:%02X\n", fw_summary->subsys[i].type);
		gdix_dbg("Subsystem size:%u\n", fw_summary->subsys[i].size);
		gdix_dbg("Subsystem flash_addr:%08X\n", fw_summary->subsys[i].flash_addr);
	}
#endif

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
leaq 0x48(%rdi), %r14
movb $0x0, 0x10(%rsp)
movq 0x30(%rdi), %r15
movl $0x492, %edx            # imm = 0x492
movq %r14, %rdi
movq %r15, %rsi
callq 0x5090
movl 0x10(%rbx), %r8d
movl 0x48(%rbx), %r9d
addl $0x8, %r9d
cmpl %r9d, %r8d
jne 0x10e31
xorl %eax, %eax
cmpl $0x9, %r8d
jl 0x10dda
xorl %eax, %eax
movl $0x8, %ecx
movzwl (%r15,%rcx), %edx
addl %edx, %eax
addq $0x2, %rcx
cmpq %r8, %rcx
jb 0x10dca
cmpl 0x4c(%rbx), %eax
jne 0x10e6f
movzbl 0x65(%rbx), %eax
cmpl $0x2f, %eax
jbe 0x10e8e
leaq 0x1049a(%rip), %rcx     # 0x21291
movl $0xffffffea, %ebp       # imm = 0xFFFFFFEA
cmpb $0x1, (%rcx)
jne 0x110bc
movq 0x101cc(%rip), %rcx     # 0x20fd8
movq (%rcx), %rdi
leaq 0x81d3(%rip), %rsi      # 0x18fe9
leaq 0x818f(%rip), %rdx      # 0x18fac
movl $0x8d, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x110bc
leaq 0x10459(%rip), %rax     # 0x21291
movl $0xffffffea, %ebp       # imm = 0xFFFFFFEA
cmpb $0x1, (%rax)
jne 0x110bc
movq 0x1018b(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0x811c(%rip), %rsi      # 0x18f73
leaq 0x814e(%rip), %rdx      # 0x18fac
movl $0x7e, %ecx
xorl %eax, %eax
callq 0x5180
jmp 0x110bc
leaq 0x1041b(%rip), %rax     # 0x21291
movl $0xffffffea, %ebp       # imm = 0xFFFFFFEA
cmpb $0x1, (%rax)
jne 0x110bc
callq 0x6112
jmp 0x110bc
testl %eax, %eax
je 0x10ee2
leaq 0x2a(%r15), %rcx
shll $0x3, %eax
leaq (%rax,%rax,2), %rax
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
movb (%rcx), %dil
movb %dil, 0x72(%rbx,%rsi)
movl 0x1(%rcx), %edi
movl %edi, 0x76(%rbx,%rsi)
movl 0x5(%rcx), %r9d
movl %r9d, 0x7a(%rbx,%rsi)
cmpl %r8d, %edx
jg 0x110ab
movl %edx, %r9d
addq %r15, %r9
movq %r9, 0x82(%rbx,%rsi)
addl %edi, %edx
addq $0xa, %rcx
addq $0x18, %rsi
cmpq %rsi, %rax
jne 0x10ea4
movq 0x59(%rbx), %rax
movq %rax, 0x8(%rsp)
leaq 0x1039f(%rip), %r15     # 0x21291
movb (%r15), %al
cmpb $0x1, %al
jne 0x10f25
movq 0x100b8(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movzbl 0x67(%rbx), %r8d
leaq 0x8145(%rip), %rsi      # 0x19054
leaq 0x8096(%rip), %rdx      # 0x18fac
movl $0xa3, %ecx
xorl %eax, %eax
callq 0x5180
movb (%r15), %al
testb $0x1, %al
je 0x10f55
movq 0x10088(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x814b(%rip), %rsi      # 0x19085
leaq 0x806b(%rip), %rdx      # 0x18fac
leaq 0x8(%rsp), %r8
movl $0xa4, %ecx
xorl %eax, %eax
callq 0x5180
movb (%r15), %al
testb $0x1, %al
je 0x10f9c
movq 0x10058(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movzbl 0x61(%rbx), %r8d
movzbl 0x62(%rbx), %r9d
movzbl 0x63(%rbx), %r10d
movzbl 0x64(%rbx), %r11d
leaq 0x812b(%rip), %rsi      # 0x190a9
leaq 0x8027(%rip), %rdx      # 0x18fac
movl $0xa7, %ecx
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x5180
addq $0x10, %rsp
movb (%r15), %al
testb $0x1, %al
je 0x10fcc
movq 0x10011(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movzbl 0x66(%rbx), %r8d
leaq 0x8126(%rip), %rsi      # 0x190dc
leaq 0x7fef(%rip), %rdx      # 0x18fac
movl $0xa8, %ecx
xorl %eax, %eax
callq 0x5180
movb (%r15), %al
testb $0x1, %al
je 0x10ffa
movq 0xffe1(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
movl (%r14), %r8d
leaq 0x8122(%rip), %rsi      # 0x19106
leaq 0x7fc1(%rip), %rdx      # 0x18fac
movl $0xa9, %ecx
xorl %eax, %eax
callq 0x5180
movb (%r15), %al
testb $0x1, %al
je 0x11027
movq 0xffb3(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
movzbl 0x65(%rbx), %r8d
leaq 0x8115(%rip), %rsi      # 0x19129
leaq 0x7f91(%rip), %rdx      # 0x18fac
movl $0xaa, %ecx
xorl %eax, %eax
callq 0x5180
movq 0x59(%rbx), %rax
movq %rax, 0x38(%rbx)
movl 0x61(%rbx), %eax
movl %eax, 0x40(%rbx)
movl %eax, %ecx
shrl $0x10, %ecx
shrl $0x18, %eax
xorl %ebp, %ebp
cmpb $0x1, 0x18(%rbx)
jne 0x11088
movq 0x30(%rbx), %rdx
movslq 0x10(%rbx), %rsi
movb 0x62(%rdx,%rsi), %r14b
cmpb $0x1, (%r15)
jne 0x1108b
movzbl %r14b, %r8d
movq 0xff55(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x80e8(%rip), %rsi      # 0x19155
leaq 0x7f38(%rip), %rdx      # 0x18fac
movl $0xb1, %ecx
xorl %eax, %eax
callq 0x5180
movb 0x42(%rbx), %cl
movb 0x43(%rbx), %al
jmp 0x1108b
xorl %r14d, %r14d
movl $0x0, 0x24(%rbx)
movzbl %cl, %ecx
shll $0x10, %ecx
movzbl %al, %eax
shll $0x8, %eax
movzbl %r14b, %edx
orl %ecx, %edx
orl %eax, %edx
movl %edx, 0x28(%rbx)
jmp 0x110bc
leaq 0x101df(%rip), %rax     # 0x21291
movl $0xffffffea, %ebp       # imm = 0xFFFFFFEA
cmpb $0x1, (%rax)
je 0x110c9
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
callq 0x6136
jmp 0x110bc