// File: /workspace/github_cpp_makefile_star2_O3/goodix[P]gdix_hid_firmware_update/gtx9/gtx9_update.cpp
// Function: GTx9Update::fw_update(unsigned int)
int GTx9Update::fw_update(unsigned int firmware_flag)
{
	struct firmware_summary *fw_info =
		(struct firmware_summary *)image->GetFirmwareSummary();
	struct fw_subsys_info *fw_x;
	struct fw_subsys_info subsys_cfg;
	int i;
	int ret;
	uint8_t buf[1];
	int minorVer;
	int majorVer;
	uint8_t cfgVer;
	uint8_t temp_buf[CFG_MAX_SIZE] = {0};

	gdix_info("IN\n");
	/* flash config */
	if (image->HasConfig()) {
		memcpy(temp_buf,
			   image->GetFirmwareData() + image->GetFirmwareSize() + 64,
			   image->GetConfigSize());

		subsys_cfg.data = temp_buf;
		subsys_cfg.size = CFG_MAX_SIZE;
		subsys_cfg.flash_addr = 0x40000;
		subsys_cfg.type = 4;
		ret = flashSubSystem(&subsys_cfg);
		if (ret < 0) {
			gdix_err("failed flash config with ISP\n");
			return ret;
		}
		gdix_info("success flash config with ISP\n");
		usleep(20000);
	}

	for (i = 1; i < fw_info->subsys_num; i++) {
		fw_x = &fw_info->subsys[i];
		if (fw_x->type == (uint8_t)firmware_flag) {
			gdix_info("skip type[%02X] subsystem[%d]\n", fw_x->type, i);
			continue;
		}
		ret = flashSubSystem(fw_x);
		if (ret < 0) {
			gdix_err("-------- Failed flash subsystem %d --------\n", i);
			return ret;
		}
		gdix_info("-------- Success flash subsystem %d --------\n", i);
	}

	/* reset IC */
	gdix_info("Reset IC\n");
	buf[0] = 1;
	ret = dev->SendCmd(0x13, buf, 1);
	if (ret < 0) {
		gdix_err("Failed reset IC\n");
		return ret;
	}
	usleep(100000);

	/* compare version */
	dev->SetBasicProperties();
	majorVer = image->GetFirmwareVersionMajor();
	minorVer = image->GetFirmwareVersionMinor();
	if (image->HasConfig() == false) {
		cfgVer = (uint8_t)dev->GetFirmwareVersionMinor();
		minorVer &= 0xffffff00;
		minorVer |= cfgVer;
	}

	if ((dev->GetFirmwareVersionMajor() != majorVer) ||
		(dev->GetFirmwareVersionMinor() != minorVer)) {
		gdix_err("update failed\n");
		gdix_info("Current version:%d.%d\n", dev->GetFirmwareVersionMajor(),
				  dev->GetFirmwareVersionMinor());
		gdix_info("Firmware version:%d.%d\n", majorVer, minorVer);
		return -1;
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1028, %rsp           # imm = 0x1028
movl %esi, %ebp
movq %rdi, %rbx
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x70(%rax)
movq %rax, %r14
leaq 0x20(%rsp), %r15
movl $0x1000, %edx           # imm = 0x1000
movq %r15, %rdi
xorl %esi, %esi
callq 0x5080
leaq 0xdde9(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
jne 0x134d1
movq 0xdb04(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x6cab(%rip), %rsi      # 0x1a169
leaq 0x5d11(%rip), %rdx      # 0x191d6
movl $0xfe, %ecx
xorl %eax, %eax
callq 0x5180
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x98(%rax)
testb %al, %al
je 0x1358a
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
movq %rax, %r12
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movl %eax, %eax
addq %rax, %r12
addq $0x40, %r12
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0xa0(%rax)
movslq %eax, %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x5090
leaq 0x8(%rsp), %rsi
movq %r15, 0x10(%rsi)
movabsq $0x4000000001000, %rax  # imm = 0x4000000001000
movq %rax, 0x4(%rsi)
movb $0x4, (%rsi)
movq %rbx, %rdi
callq 0x13184
movl %eax, %r15d
leaq 0xdd44(%rip), %rax      # 0x21291
movb (%rax), %al
testl %r15d, %r15d
js 0x137bf
testb %al, %al
je 0x13580
movq 0xda55(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x6eb7(%rip), %rsi      # 0x1a424
leaq 0x5c62(%rip), %rdx      # 0x191d6
movl $0x10e, %ecx            # imm = 0x10E
xorl %eax, %eax
callq 0x5180
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5170
cmpb $0x2, 0x1d(%r14)
jb 0x13641
movq %r14, %r13
addq $0x42, %r13
movl $0x1, %r12d
movzbl (%r13), %r8d
cmpb %bpl, %r8b
jne 0x135e1
leaq 0xdcde(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
jne 0x1362c
movq 0xd9f9(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x6e8b(%rip), %rsi      # 0x1a454
leaq 0x5c06(%rip), %rdx      # 0x191d6
movl $0x115, %ecx            # imm = 0x115
movl %r12d, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x1362c
movq %rbx, %rdi
movq %r13, %rsi
callq 0x13184
movl %eax, %r15d
leaq 0xdc9b(%rip), %rax      # 0x21291
movb (%rax), %al
testl %r15d, %r15d
js 0x137ca
testb %al, %al
je 0x1362c
movq 0xd9ac(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x6eac(%rip), %rsi      # 0x1a4c2
leaq 0x5bb9(%rip), %rdx      # 0x191d6
movl $0x11d, %ecx            # imm = 0x11D
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
incq %r12
movzbl 0x1d(%r14), %eax
addq $0x18, %r13
cmpq %rax, %r12
jb 0x135a2
leaq 0xdc49(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
jne 0x13671
movq 0xd964(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x6ea3(%rip), %rsi      # 0x1a501
leaq 0x5b71(%rip), %rdx      # 0x191d6
movl $0x121, %ecx            # imm = 0x121
xorl %eax, %eax
callq 0x5180
leaq 0x7(%rsp), %rdx
movb $0x1, (%rdx)
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x13, %esi
movl $0x1, %ecx
callq *0x48(%rax)
testl %eax, %eax
js 0x137a9
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x5170
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
callq *0xa0(%rax)
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x28(%rax)
movl %eax, %ebp
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x30(%rax)
movl %eax, %r14d
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x98(%rax)
testb %al, %al
jne 0x136ed
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
callq *0x68(%rax)
andl $0xffffff00, %r14d      # imm = 0xFFFFFF00
movzbl %al, %eax
orl %eax, %r14d
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
callq *0x60(%rax)
cmpl %ebp, %eax
jne 0x13711
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
callq *0x68(%rax)
xorl %r15d, %r15d
cmpl %r14d, %eax
je 0x137ce
leaq 0xdb79(%rip), %rax      # 0x21291
movb (%rax), %al
cmpb $0x1, %al
je 0x137e3
testb $0x1, %al
je 0x13773
movq 0xd88b(%rip), %rax      # 0x20fb8
movq (%rax), %r15
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
callq *0x60(%rax)
movl %eax, %r12d
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
callq *0x68(%rax)
leaq 0x69a5(%rip), %rsi      # 0x1a0f3
leaq 0x5a81(%rip), %rdx      # 0x191d6
movq %r15, %rdi
movl $0x138, %ecx            # imm = 0x138
movl %r12d, %r8d
movl %eax, %r9d
xorl %eax, %eax
callq 0x5180
leaq 0xdb20(%rip), %rax      # 0x21291
movb (%rax), %al
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testb $0x1, %al
je 0x137ce
movq 0xd834(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x698d(%rip), %rsi      # 0x1a11b
leaq 0x5a41(%rip), %rdx      # 0x191d6
movl $0x139, %ecx            # imm = 0x139
movl %ebp, %r8d
movl %r14d, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x137ce
movl %eax, %r15d
leaq 0xdade(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
jne 0x137ce
callq 0x6762
jmp 0x137ce
testb %al, %al
je 0x137ce
callq 0x678c
jmp 0x137ce
testb %al, %al
jne 0x13815
movl %r15d, %eax
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xd7ee(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
leaq 0x6d4a(%rip), %rsi      # 0x1a53e
leaq 0x59db(%rip), %rdx      # 0x191d6
movl $0x136, %ecx            # imm = 0x136
xorl %eax, %eax
callq 0x5180
leaq 0xda83(%rip), %rax      # 0x21291
movb (%rax), %al
jmp 0x13722
movq 0xd7bc(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
leaq 0x6c5e(%rip), %rsi      # 0x1a484
leaq 0x59a9(%rip), %rdx      # 0x191d6
movl $0x11a, %ecx            # imm = 0x11A
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x137ce