// File: goodix[P]gdix_hid_firmware_update/gtx9/gtx9_i2c_update.cpp
// Function: gdix_parse_firmware(firmware_data*)
static int gdix_parse_firmware(struct firmware_data *fw_data)
{
	struct firmware *firmware;
	struct firmware_summary *fw_summary;
	int i, fw_offset, info_offset;
	int r = 0;
	uint32_t checksum;

	fw_summary = &fw_data->fw_summary;

	/* copy firmware head info */
	firmware = fw_data->firmware;
	if (firmware->size < 42) {
		gdix_err("Invalid firmware size:%d\n", firmware->size);
		r = -EINVAL;
		goto err_size;
	}
	memcpy(fw_summary, firmware->data, sizeof(*fw_summary));

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

	for (i = 8, checksum = 0; i < firmware->size; i += 2)
		checksum += firmware->data[i] + (firmware->data[i + 1] << 8);

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

	if (fw_summary->subsys_num > 47) {
		gdix_err("Bad firmware, invalid subsys num: %d\n",
				 fw_summary->subsys_num);
		r = -EINVAL;
		goto err_size;
	}

	/* parse subsystem info */
	fw_offset = 512;
	for (i = 0; i < fw_summary->subsys_num; i++) {
		info_offset = 42 + i * 10;
		fw_summary->subsys[i].type = firmware->data[info_offset];
		fw_summary->subsys[i].size =
			*(uint32_t *)&firmware->data[info_offset + 1];
		fw_summary->subsys[i].flash_addr =
			*(uint32_t *)&firmware->data[info_offset + 5];
		if (fw_offset > firmware->size) {
			gdix_err("Sybsys offset exceed Firmware size\n");
			r = -EINVAL;
			goto err_size;
		}

		fw_summary->subsys[i].data = firmware->data + fw_offset;
		fw_offset += fw_summary->subsys[i].size;
	}

	gdix_dbg("Firmware package protocol: V%u\n", fw_summary->protocol_ver);
	gdix_dbg("Firmware PID:GT%s\n", fw_summary->fw_pid);
	gdix_dbg("Firmware VID:%*ph\n", 4, fw_summary->fw_vid);
	gdix_dbg("Firmware chip type:0x%02X\n", fw_summary->chip_type);
	gdix_dbg("Firmware bus type:%s\n",
			 (fw_summary->bus_type & 1) ? "SPI" : "I2C");
	gdix_dbg("Firmware size:%u\n", fw_summary->size);
	gdix_dbg("Firmware subsystem num:%u\n", fw_summary->subsys_num);

	/*
			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);
			}
	*/

err_size:
	return r;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x490(%rax), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x2a, (%rax)
jge 0x15e2a
jmp 0x15de5
leaq 0x104ad(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x15e1c
movq 0x101e0(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movl (%rax), %r8d
leaq 0x97f0(%rip), %rsi      # 0x1f5f9
leaq 0x9814(%rip), %rdx      # 0x1f624
movl $0x177, %ecx            # imm = 0x177
movb $0x0, %al
callq 0x5190
jmp 0x15e1e
movl $0xffffffea, -0x28(%rbp) # imm = 0xFFFFFFEA
jmp 0x162a6
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x4, %rsi
movl $0x48a, %edx            # imm = 0x48A
callq 0x50b0
movq -0x10(%rbp), %rax
movl (%rax), %eax
movq -0x18(%rbp), %rcx
movl (%rcx), %ecx
addl $0x8, %ecx
cmpl %ecx, %eax
je 0x15ea5
jmp 0x15e55
leaq 0x1043d(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x15e97
movq 0x10170(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movl (%rax), %r8d
movq -0x18(%rbp), %rax
movl (%rax), %r9d
addl $0x8, %r9d
leaq 0x912d(%rip), %rsi      # 0x1efb1
leaq 0x9799(%rip), %rdx      # 0x1f624
movl $0x180, %ecx            # imm = 0x180
movb $0x0, %al
callq 0x5190
jmp 0x15e99
movl $0xffffffea, -0x28(%rbp) # imm = 0xFFFFFFEA
jmp 0x162a6
movl $0x8, -0x1c(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x15ef3
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movzbl 0x4(%rax,%rcx), %eax
movq -0x10(%rbp), %rcx
movl -0x1c(%rbp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movzbl 0x4(%rcx,%rdx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x1c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x1c(%rbp)
jmp 0x15eb3
movl -0x2c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x4(%rcx), %eax
je 0x15f3f
jmp 0x15f01
leaq 0x10391(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x15f31
movq 0x100c4(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0x971a(%rip), %rsi      # 0x1f638
leaq 0x96ff(%rip), %rdx      # 0x1f624
movl $0x18a, %ecx            # imm = 0x18A
movb $0x0, %al
callq 0x5190
jmp 0x15f33
movl $0xffffffea, -0x28(%rbp) # imm = 0xFFFFFFEA
jmp 0x162a6
movq -0x18(%rbp), %rax
movzbl 0x1d(%rax), %eax
cmpl $0x2f, %eax
jle 0x15f95
jmp 0x15f4e
leaq 0x10344(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x15f87
movq 0x10077(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movzbl 0x1d(%rax), %r8d
leaq 0x96f2(%rip), %rsi      # 0x1f666
leaq 0x96a9(%rip), %rdx      # 0x1f624
movl $0x191, %ecx            # imm = 0x191
movb $0x0, %al
callq 0x5190
jmp 0x15f89
movl $0xffffffea, -0x28(%rbp) # imm = 0xFFFFFFEA
jmp 0x162a6
movl $0x200, -0x20(%rbp)     # imm = 0x200
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
movzbl 0x1d(%rcx), %ecx
cmpl %ecx, %eax
jge 0x160ca
imull $0xa, -0x1c(%rbp), %eax
addl $0x2a, %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movb 0x4(%rax,%rcx), %cl
movq -0x18(%rbp), %rax
addq $0x22, %rax
movslq -0x1c(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movb %cl, (%rax)
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl 0x4(%rax,%rcx), %ecx
movq -0x18(%rbp), %rax
addq $0x22, %rax
movslq -0x1c(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x5, %ecx
movslq %ecx, %rcx
movl 0x4(%rax,%rcx), %ecx
movq -0x18(%rbp), %rax
addq $0x22, %rax
movslq -0x1c(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jle 0x1607a
jmp 0x1603c
leaq 0x10256(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x1606c
movq 0xff89(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
leaq 0x9004(%rip), %rsi      # 0x1f05d
leaq 0x95c4(%rip), %rdx      # 0x1f624
movl $0x1a0, %ecx            # imm = 0x1A0
movb $0x0, %al
callq 0x5190
jmp 0x1606e
movl $0xffffffea, -0x28(%rbp) # imm = 0xFFFFFFEA
jmp 0x162a6
movq -0x10(%rbp), %rcx
addq $0x4, %rcx
movslq -0x20(%rbp), %rax
addq %rax, %rcx
movq -0x18(%rbp), %rax
addq $0x22, %rax
movslq -0x1c(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
addq $0x22, %rax
movslq -0x1c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x15fa3
jmp 0x160cc
leaq 0x101c6(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x16105
movq 0xfed9(%rip), %rax      # 0x25fb8
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movzbl 0x1f(%rax), %r8d
leaq 0x95ab(%rip), %rsi      # 0x1f69d
leaq 0x952b(%rip), %rdx      # 0x1f624
movl $0x1a9, %ecx            # imm = 0x1A9
movb $0x0, %al
callq 0x5190
jmp 0x16107
jmp 0x16109
leaq 0x10189(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x16141
movq 0xfe9c(%rip), %rax      # 0x25fb8
movq (%rax), %rdi
movq -0x18(%rbp), %r8
addq $0x11, %r8
leaq 0x95a0(%rip), %rsi      # 0x1f6ce
leaq 0x94ef(%rip), %rdx      # 0x1f624
movl $0x1aa, %ecx            # imm = 0x1AA
movb $0x0, %al
callq 0x5190
jmp 0x16143
jmp 0x16145
leaq 0x1014d(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x16183
movq 0xfe60(%rip), %rax      # 0x25fb8
movq (%rax), %rdi
movq -0x18(%rbp), %r9
addq $0x19, %r9
leaq 0x9588(%rip), %rsi      # 0x1f6f2
leaq 0x94b3(%rip), %rdx      # 0x1f624
movl $0x1ab, %ecx            # imm = 0x1AB
movl $0x4, %r8d
movb $0x0, %al
callq 0x5190
jmp 0x16185
jmp 0x16187
leaq 0x1010b(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x161c0
movq 0xfe1e(%rip), %rax      # 0x25fb8
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movzbl 0x1e(%rax), %r8d
leaq 0x9569(%rip), %rsi      # 0x1f716
leaq 0x9470(%rip), %rdx      # 0x1f624
movl $0x1ac, %ecx            # imm = 0x1AC
movb $0x0, %al
callq 0x5190
jmp 0x161c2
jmp 0x161c4
leaq 0x100ce(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x1622a
movq 0xfde1(%rip), %rax      # 0x25fb8
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movzbl 0x20(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x161fb
leaq 0x9574(%rip), %rax      # 0x1f769
movq %rax, -0x40(%rbp)
jmp 0x16208
leaq 0x956b(%rip), %rax      # 0x1f76d
movq %rax, -0x40(%rbp)
jmp 0x16208
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %r8
leaq 0x952b(%rip), %rsi      # 0x1f742
leaq 0x9406(%rip), %rdx      # 0x1f624
movl $0x1ae, %ecx            # imm = 0x1AE
movb $0x0, %al
callq 0x5190
jmp 0x1622c
jmp 0x1622e
leaq 0x10064(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x16265
movq 0xfd77(%rip), %rax      # 0x25fb8
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %r8d
leaq 0x951f(%rip), %rsi      # 0x1f771
leaq 0x93cb(%rip), %rdx      # 0x1f624
movl $0x1af, %ecx            # imm = 0x1AF
movb $0x0, %al
callq 0x5190
jmp 0x16267
jmp 0x16269
leaq 0x10029(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x162a2
movq 0xfd3c(%rip), %rax      # 0x25fb8
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movzbl 0x1d(%rax), %r8d
leaq 0x9505(%rip), %rsi      # 0x1f794
leaq 0x938e(%rip), %rdx      # 0x1f624
movl $0x1b0, %ecx            # imm = 0x1B0
movb $0x0, %al
callq 0x5190
jmp 0x162a4
jmp 0x162a6
movl -0x28(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nop