// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
addq $0x48, %rax
movq %rax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movb $0x0, -0x25(%rbp)
leaq -0x2e(%rbp), %rdi
xorl %esi, %esi
movl $0x9, %edx
callq 0x50a0
movq -0x40(%rbp), %rax
movq -0x18(%rbp), %rdi
movq 0x30(%rax), %rsi
movl $0x492, %edx            # imm = 0x492
callq 0x50b0
movq -0x40(%rbp), %rax
movl 0x10(%rax), %eax
movq -0x18(%rbp), %rcx
movl (%rcx), %ecx
addl $0x8, %ecx
cmpl %ecx, %eax
je 0x14684
jmp 0x14633
leaq 0x11c5f(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x14676
movq -0x40(%rbp), %rax
movq 0x1198e(%rip), %rcx     # 0x25fd8
movq (%rcx), %rdi
movl 0x10(%rax), %r8d
movq -0x18(%rbp), %rax
movl (%rax), %r9d
addl $0x8, %r9d
leaq 0xa94e(%rip), %rsi      # 0x1efb1
leaq 0xa980(%rip), %rdx      # 0x1efea
movl $0x7e, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x14678
movl $0xffffffea, -0x4(%rbp) # imm = 0xFFFFFFEA
jmp 0x14ad0
movl $0x8, -0x34(%rbp)
movq -0x40(%rbp), %rcx
movl -0x34(%rbp), %eax
cmpl 0x10(%rcx), %eax
jge 0x146ce
movq -0x40(%rbp), %rcx
movq 0x30(%rcx), %rax
movslq -0x34(%rbp), %rdx
movzbl (%rax,%rdx), %eax
movq 0x30(%rcx), %rcx
movl -0x34(%rbp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x34(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x34(%rbp)
jmp 0x1468b
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x4(%rcx), %eax
je 0x1471a
jmp 0x146dc
leaq 0x11bb6(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x1470c
movq 0x118e9(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0xa8ff(%rip), %rsi      # 0x1eff8
leaq 0xa8ea(%rip), %rdx      # 0x1efea
movl $0x87, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x1470e
movl $0xffffffea, -0x4(%rbp) # imm = 0xFFFFFFEA
jmp 0x14ad0
movq -0x18(%rbp), %rax
movzbl 0x1d(%rax), %eax
cmpl $0x2f, %eax
jle 0x14770
jmp 0x14729
leaq 0x11b69(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x14762
movq 0x1189c(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movzbl 0x1d(%rax), %r8d
leaq 0xa8d8(%rip), %rsi      # 0x1f027
leaq 0xa894(%rip), %rdx      # 0x1efea
movl $0x8d, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x14764
movl $0xffffffea, -0x4(%rbp) # imm = 0xFFFFFFEA
jmp 0x14ad0
movl $0x200, -0x20(%rbp)     # imm = 0x200
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x18(%rbp), %rcx
movzbl 0x1d(%rcx), %ecx
cmpl %ecx, %eax
jge 0x1489f
movq -0x40(%rbp), %rcx
imull $0xa, -0x34(%rbp), %eax
addl $0x2a, %eax
movl %eax, -0x24(%rbp)
movq 0x30(%rcx), %rax
movl -0x24(%rbp), %edx
movb (%rax,%rdx), %dl
movq -0x18(%rbp), %rax
addq $0x2a, %rax
movslq -0x34(%rbp), %rsi
imulq $0x18, %rsi, %rsi
addq %rsi, %rax
movb %dl, (%rax)
movq 0x30(%rcx), %rax
movl -0x24(%rbp), %edx
addl $0x1, %edx
movl %edx, %edx
movl (%rax,%rdx), %edx
movq -0x18(%rbp), %rax
addq $0x2a, %rax
movslq -0x34(%rbp), %rsi
imulq $0x18, %rsi, %rsi
addq %rsi, %rax
movl %edx, 0x4(%rax)
movq 0x30(%rcx), %rax
movl -0x24(%rbp), %edx
addl $0x5, %edx
movl %edx, %edx
movl (%rax,%rdx), %edx
movq -0x18(%rbp), %rax
addq $0x2a, %rax
movslq -0x34(%rbp), %rsi
imulq $0x18, %rsi, %rsi
addq %rsi, %rax
movl %edx, 0x8(%rax)
movl -0x20(%rbp), %eax
cmpl 0x10(%rcx), %eax
jle 0x14850
jmp 0x14812
leaq 0x11a80(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x14842
movq 0x117b3(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0xa82e(%rip), %rsi      # 0x1f05d
leaq 0xa7b4(%rip), %rdx      # 0x1efea
movl $0x9b, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x14844
movl $0xffffffea, -0x4(%rbp) # imm = 0xFFFFFFEA
jmp 0x14ad0
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rcx
movl -0x20(%rbp), %eax
addq %rax, %rcx
movq -0x18(%rbp), %rax
addq $0x2a, %rax
movslq -0x34(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
addq $0x2a, %rax
movslq -0x34(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1477e
movq -0x18(%rbp), %rax
movq 0x11(%rax), %rax
movq %rax, -0x2e(%rbp)
leaq 0x119e7(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x148e4
movq 0x116fa(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movzbl 0x1f(%rax), %r8d
leaq 0xa7c1(%rip), %rsi      # 0x1f092
leaq 0xa712(%rip), %rdx      # 0x1efea
movl $0xa3, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x148e6
jmp 0x148e8
leaq 0x119aa(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x1491c
movq 0x116bd(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq -0x2e(%rbp), %r8
leaq 0xa7ba(%rip), %rsi      # 0x1f0c3
leaq 0xa6da(%rip), %rdx      # 0x1efea
movl $0xa4, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x1491e
jmp 0x14920
leaq 0x11972(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x1497b
movq 0x11685(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movzbl 0x19(%rax), %r8d
movq -0x18(%rbp), %rax
movzbl 0x1a(%rax), %r9d
movq -0x18(%rbp), %rax
movzbl 0x1b(%rax), %r10d
movq -0x18(%rbp), %rax
movzbl 0x1c(%rax), %eax
leaq 0xa787(%rip), %rsi      # 0x1f0e7
leaq 0xa683(%rip), %rdx      # 0x1efea
movl $0xa7, %ecx
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x5190
jmp 0x1497d
jmp 0x1497f
leaq 0x11913(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x149b8
movq 0x11626(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movzbl 0x1e(%rax), %r8d
leaq 0xa775(%rip), %rsi      # 0x1f11a
leaq 0xa63e(%rip), %rdx      # 0x1efea
movl $0xa8, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x149ba
jmp 0x149bc
leaq 0x118d6(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x149f3
movq 0x115e9(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %r8d
leaq 0xa764(%rip), %rsi      # 0x1f144
leaq 0xa603(%rip), %rdx      # 0x1efea
movl $0xa9, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x149f5
jmp 0x149f7
leaq 0x1189b(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x14a30
movq 0x115ae(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movzbl 0x1d(%rax), %r8d
leaq 0xa74a(%rip), %rsi      # 0x1f167
leaq 0xa5c6(%rip), %rdx      # 0x1efea
movl $0xaa, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x14a32
movq -0x40(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x11(%rcx), %rcx
movq %rcx, 0x38(%rax)
movq -0x18(%rbp), %rcx
movl 0x19(%rcx), %ecx
movl %ecx, 0x40(%rax)
testb $0x1, 0x18(%rax)
je 0x14aa5
movq -0x40(%rbp), %rcx
movq 0x30(%rcx), %rax
movl 0x10(%rcx), %ecx
addl $0x40, %ecx
addl $0x22, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, -0x25(%rbp)
leaq 0x11826(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x14aa1
movq 0x11539(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movzbl -0x25(%rbp), %r8d
leaq 0xa705(%rip), %rsi      # 0x1f193
leaq 0xa555(%rip), %rdx      # 0x1efea
movl $0xb1, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x14aa3
jmp 0x14aa5
movq -0x40(%rbp), %rax
movl $0x0, 0x24(%rax)
movzbl 0x42(%rax), %ecx
shll $0x10, %ecx
movzbl 0x43(%rax), %edx
shll $0x8, %edx
orl %edx, %ecx
movzbl -0x25(%rbp), %edx
orl %edx, %ecx
movl %ecx, 0x28(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)