// File: /workspace/github_cpp_makefile_star2_O1/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 $0x0, 0x8(%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 0x10f82
xorl %eax, %eax
cmpl $0x9, %r8d
jl 0x10f2b
xorl %eax, %eax
movl $0x8, %ecx
movzwl (%r15,%rcx), %edx
addl %edx, %eax
addq $0x2, %rcx
cmpq %r8, %rcx
jb 0x10f1b
cmpl 0x4c(%rbx), %eax
jne 0x10fc0
movzbl 0x65(%rbx), %eax
cmpl $0x2f, %eax
jbe 0x10fdf
leaq 0x10349(%rip), %rcx     # 0x21291
movl $0xffffffea, %ebp       # imm = 0xFFFFFFEA
cmpb $0x1, (%rcx)
jne 0x111fc
movq 0x1007b(%rip), %rcx     # 0x20fd8
movq (%rcx), %rdi
leaq 0x80bc(%rip), %rsi      # 0x19023
leaq 0x8078(%rip), %rdx      # 0x18fe6
movl $0x8d, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x111fc
leaq 0x10308(%rip), %rax     # 0x21291
movl $0xffffffea, %ebp       # imm = 0xFFFFFFEA
cmpb $0x1, (%rax)
jne 0x111fc
movq 0x1003a(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0x8005(%rip), %rsi      # 0x18fad
leaq 0x8037(%rip), %rdx      # 0x18fe6
movl $0x7e, %ecx
xorl %eax, %eax
callq 0x5180
jmp 0x111fc
leaq 0x102ca(%rip), %rax     # 0x21291
movl $0xffffffea, %ebp       # imm = 0xFFFFFFEA
cmpb $0x1, (%rax)
jne 0x111fc
callq 0x6166
jmp 0x111fc
testl %eax, %eax
je 0x11033
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 0x111eb
movl %edx, %r9d
addq %r15, %r9
movq %r9, 0x82(%rbx,%rsi)
addl %edi, %edx
addq $0xa, %rcx
addq $0x18, %rsi
cmpq %rsi, %rax
jne 0x10ff5
movq 0x59(%rbx), %rax
movq %rax, 0x8(%rsp)
leaq 0x1024e(%rip), %r15     # 0x21291
cmpb $0x1, (%r15)
jne 0x11072
movq 0xff68(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
movzbl 0x67(%rbx), %r8d
leaq 0x802f(%rip), %rsi      # 0x1908e
leaq 0x7f80(%rip), %rdx      # 0x18fe6
movl $0xa3, %ecx
xorl %eax, %eax
callq 0x5180
cmpb $0x1, (%r15)
jne 0x110a1
movq 0xff39(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x8036(%rip), %rsi      # 0x190bf
leaq 0x7f56(%rip), %rdx      # 0x18fe6
leaq 0x8(%rsp), %r8
movl $0xa4, %ecx
xorl %eax, %eax
callq 0x5180
cmpb $0x1, (%r15)
jne 0x110e7
movq 0xff0a(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
movzbl 0x61(%rbx), %r8d
movzbl 0x62(%rbx), %r9d
movzbl 0x63(%rbx), %r10d
movzbl 0x64(%rbx), %r11d
leaq 0x8017(%rip), %rsi      # 0x190e3
leaq 0x7f13(%rip), %rdx      # 0x18fe6
movl $0xa7, %ecx
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x5180
addq $0x10, %rsp
cmpb $0x1, (%r15)
jne 0x11116
movq 0xfec4(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
movzbl 0x66(%rbx), %r8d
leaq 0x8013(%rip), %rsi      # 0x19116
leaq 0x7edc(%rip), %rdx      # 0x18fe6
movl $0xa8, %ecx
xorl %eax, %eax
callq 0x5180
cmpb $0x1, (%r15)
jne 0x11143
movq 0xfe95(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
movl (%r14), %r8d
leaq 0x8010(%rip), %rsi      # 0x19140
leaq 0x7eaf(%rip), %rdx      # 0x18fe6
movl $0xa9, %ecx
xorl %eax, %eax
callq 0x5180
cmpb $0x1, (%r15)
jne 0x11172
movq 0xfe68(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
movzbl 0x65(%rbx), %r8d
leaq 0x8004(%rip), %rsi      # 0x19163
leaq 0x7e80(%rip), %rdx      # 0x18fe6
movl $0xaa, %ecx
xorl %eax, %eax
callq 0x5180
movq 0x59(%rbx), %rax
movq %rax, 0x38(%rbx)
movl 0x61(%rbx), %eax
movl %eax, 0x40(%rbx)
xorl %ebp, %ebp
cmpb $0x1, 0x18(%rbx)
movl $0x0, %r14d
jne 0x111c9
movq 0x30(%rbx), %rax
movslq 0x10(%rbx), %rcx
movb 0x62(%rax,%rcx), %r14b
cmpb $0x1, (%r15)
jne 0x111c9
movzbl %r14b, %r8d
movq 0xfe0c(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x7fd9(%rip), %rsi      # 0x1918f
leaq 0x7e29(%rip), %rdx      # 0x18fe6
movl $0xb1, %ecx
xorl %eax, %eax
callq 0x5180
movl $0x0, 0x24(%rbx)
movzbl 0x42(%rbx), %eax
shll $0x10, %eax
movzbl 0x43(%rbx), %ecx
shll $0x8, %ecx
movzbl %r14b, %edx
orl %eax, %edx
orl %ecx, %edx
movl %edx, 0x28(%rbx)
jmp 0x111fc
leaq 0x1009f(%rip), %rax     # 0x21291
movl $0xffffffea, %ebp       # imm = 0xFFFFFFEA
cmpb $0x1, (%rax)
je 0x11209
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
callq 0x618a
jmp 0x111fc