// File: /workspace/github_cpp_makefile_star2_O2/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
movslq 0x10(%rbx), %r8
movl 0x48(%rbx), %r9d
addl $0x8, %r9d
cmpl %r9d, %r8d
jne 0xfba9
xorl %eax, %eax
pushq $0x8
popq %rcx
cmpq %r8, %rcx
jge 0xfb54
movzwl (%r15,%rcx), %edx
addl %edx, %eax
addq $0x2, %rcx
jmp 0xfb42
cmpl 0x4c(%rbx), %eax
jne 0xfbe3
movzbl 0x65(%rbx), %eax
cmpl $0x2f, %eax
jbe 0xfc1f
leaq 0x10720(%rip), %rcx     # 0x20291
pushq $-0x16
popq %rbp
cmpb $0x1, (%rcx)
jne 0xfe63
movq 0x10454(%rip), %rcx     # 0x1ffd8
movq (%rcx), %rdi
leaq 0x845b(%rip), %rsi      # 0x17fe9
leaq 0x8417(%rip), %rdx      # 0x17fac
movl $0x8d, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0xfe63
leaq 0x106e1(%rip), %rax     # 0x20291
pushq $-0x16
popq %rbp
cmpb $0x1, (%rax)
jne 0xfe63
movq 0x10415(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0x83a6(%rip), %rsi      # 0x17f73
leaq 0x83d8(%rip), %rdx      # 0x17fac
pushq $0x7e
popq %rcx
xorl %eax, %eax
callq 0x5180
jmp 0xfe63
leaq 0x106a7(%rip), %rax     # 0x20291
pushq $-0x16
popq %rbp
cmpb $0x1, (%rax)
jne 0xfe63
movq 0x103db(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0x83b3(%rip), %rsi      # 0x17fba
leaq 0x839e(%rip), %rdx      # 0x17fac
movl $0x87, %ecx
xorl %eax, %eax
callq 0x5180
jmp 0xfe63
movl %eax, %eax
imulq $0xa, %rax, %rax
leaq 0x82(%rbx), %rcx
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
cmpq %rsi, %rax
je 0xfc70
movb 0x2a(%r15,%rsi), %dil
movb %dil, -0x10(%rcx)
movl 0x2b(%r15,%rsi), %edi
movl %edi, -0xc(%rcx)
movl 0x2f(%r15,%rsi), %r9d
movl %r9d, -0x8(%rcx)
cmpl %r8d, %edx
jg 0xfe14
movl %edx, %r9d
addq %r15, %r9
movq %r9, (%rcx)
addl %edi, %edx
addq $0xa, %rsi
addq $0x18, %rcx
jmp 0xfc33
movq 0x59(%rbx), %rax
movq %rax, 0x8(%rsp)
leaq 0x10611(%rip), %r15     # 0x20291
movb (%r15), %al
cmpb $0x1, %al
jne 0xfcb3
movq 0x1032a(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
movzbl 0x67(%rbx), %r8d
leaq 0x83b7(%rip), %rsi      # 0x18054
leaq 0x8308(%rip), %rdx      # 0x17fac
movl $0xa3, %ecx
xorl %eax, %eax
callq 0x5180
movb (%r15), %al
testb $0x1, %al
je 0xfce3
movq 0x102fa(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0x83bd(%rip), %rsi      # 0x18085
leaq 0x82dd(%rip), %rdx      # 0x17fac
leaq 0x8(%rsp), %r8
movl $0xa4, %ecx
xorl %eax, %eax
callq 0x5180
movb (%r15), %al
testb $0x1, %al
je 0xfd28
movq 0x102ca(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
movzbl 0x61(%rbx), %r8d
movzbl 0x62(%rbx), %r9d
movzbl 0x63(%rbx), %r10d
movzbl 0x64(%rbx), %r11d
leaq 0x839d(%rip), %rsi      # 0x180a9
leaq 0x8299(%rip), %rdx      # 0x17fac
movl $0xa7, %ecx
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x5180
popq %rcx
popq %rdx
movb (%r15), %al
testb $0x1, %al
je 0xfd58
movq 0x10285(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
movzbl 0x66(%rbx), %r8d
leaq 0x839a(%rip), %rsi      # 0x180dc
leaq 0x8263(%rip), %rdx      # 0x17fac
movl $0xa8, %ecx
xorl %eax, %eax
callq 0x5180
movb (%r15), %al
testb $0x1, %al
je 0xfd86
movq 0x10255(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
movl (%r14), %r8d
leaq 0x8396(%rip), %rsi      # 0x18106
leaq 0x8235(%rip), %rdx      # 0x17fac
movl $0xa9, %ecx
xorl %eax, %eax
callq 0x5180
movb (%r15), %al
testb $0x1, %al
je 0xfdb3
movq 0x10227(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
movzbl 0x65(%rbx), %r8d
leaq 0x8389(%rip), %rsi      # 0x18129
leaq 0x8205(%rip), %rdx      # 0x17fac
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 0xfe45
movq 0x30(%rbx), %rdx
movslq 0x10(%rbx), %rsi
movb 0x62(%rdx,%rsi), %r14b
cmpb $0x1, (%r15)
jne 0xfe48
movzbl %r14b, %r8d
movq 0x101c9(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0x835c(%rip), %rsi      # 0x18155
leaq 0x81ac(%rip), %rdx      # 0x17fac
movl $0xb1, %ecx
xorl %eax, %eax
callq 0x5180
movb 0x42(%rbx), %cl
movb 0x43(%rbx), %al
jmp 0xfe48
leaq 0x10476(%rip), %rax     # 0x20291
pushq $-0x16
popq %rbp
cmpb $0x1, (%rax)
jne 0xfe63
movq 0x101ae(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0x81eb(%rip), %rsi      # 0x1801f
leaq 0x8171(%rip), %rdx      # 0x17fac
movl $0x9b, %ecx
jmp 0xfc13
xorl %r14d, %r14d
andl $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)
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq