// File: /workspace/github_cpp_makefile_star2_O1/goodix[P]gdix_hid_firmware_update/gtx8/gtx8_update.cpp
// Function: GTx8Update::fw_update(unsigned int)
int GTx8Update::fw_update(unsigned int firmware_flag)
{
	int retry;
	int ret, i;
	unsigned char temp_buf[65];
	unsigned char *fw_data = NULL;
	unsigned char buf_switch_to_patch[] = {0x00, 0x10, 0x00, 0x00, 0x01, 0x01};
	unsigned char buf_start_update[] = {0x00, 0x11, 0x00, 0x00, 0x01, 0x01};
	unsigned char buf_restart[] = {0x0E, 0x13, 0x00, 0x00, 0x01, 0x01};
	unsigned char buf_switch_ptp_mode[] = {0x03, 0x03, 0x00, 0x00, 0x01, 0x01};

	int sub_fw_num = 0;
	unsigned char sub_fw_type;
	unsigned int sub_fw_len;
	unsigned int sub_fw_flash_addr;
	unsigned int sub_fw_info_pos =
		image->GetFirmwareSubFwInfoOffset(); // SUB_FW_INFO_OFFSET;
	unsigned int fw_image_offset =
		image->GetFirmwareSubFwDataOffset(); // SUB_FW_DATA_OFFSET;

	/* close report */
	ret = this->DisableReport();
	if (ret < 0)
		return ret;

	ret = dev->Write(buf_switch_to_patch, sizeof(buf_switch_to_patch));
	if (ret < 0) {
		gdix_err("Failed switch to patch\n");
		goto update_err;
	}

	usleep(250000);
	retry = GDIX_RETRY_TIMES;
	do {
		ret = dev->Read(BL_STATE_ADDR, temp_buf, 1);
		gdix_dbg("BL_STATE_ADDR:0x%x\n", BL_STATE_ADDR);
		if (ret < 0) {
			gdix_err("Failed read 0x%x, ret = %d\n", BL_STATE_ADDR, ret);
			goto update_err;
		}
		if (temp_buf[0] == 0xDD)
			break;
		gdix_info("0x%x value is 0x%x != 0xDD, retry\n", BL_STATE_ADDR,
				  temp_buf[0]);
		usleep(30000);
	} while (--retry);

	if (!retry) {
		gdix_err("Reg 0x%x != 0xDD\n", BL_STATE_ADDR);
		ret = -2;
		goto update_err;
	}

	ret = this->DisableReport();
	if (ret < 0)
		goto update_err;

	/* Start update */
	ret = dev->Write(buf_start_update, sizeof(buf_start_update));
	if (ret < 0) {
		gdix_err("Failed start update, ret=%d\n", ret);
		goto update_err;
	}
	usleep(100000);

	/* Start load firmware */
	fw_data = image->GetFirmwareData();
	if (!fw_data) {
		gdix_err("No valid fw data \n");
		ret = -4;
		goto update_err;
	}

	sub_fw_num =
		image->GetFirmwareSubFwNum(); // fw_data[FW_IMAGE_SUB_FWNUM_OFFSET];
	sub_fw_info_pos =
		image->GetFirmwareSubFwInfoOffset(); // SUB_FW_INFO_OFFSET;
	fw_image_offset =
		image->GetFirmwareSubFwDataOffset(); // SUB_FW_DATA_OFFSET;
	gdix_dbg("load sub firmware, sub_fw_num=%d\n", sub_fw_num);
	if (sub_fw_num == 0)
		return -5;

	/* load normal firmware package */
	for (i = 0; i < sub_fw_num; i++) {
		sub_fw_type = fw_data[sub_fw_info_pos];
		gdix_dbg("load sub firmware, sub_fw_type=0x%x\n", sub_fw_type);
		sub_fw_len = (fw_data[sub_fw_info_pos + 1] << 24) |
					 (fw_data[sub_fw_info_pos + 2] << 16) |
					 (fw_data[sub_fw_info_pos + 3] << 8) |
					 fw_data[sub_fw_info_pos + 4];

		sub_fw_flash_addr =
			(fw_data[sub_fw_info_pos + 5] << 8) | fw_data[sub_fw_info_pos + 6];
		sub_fw_flash_addr = sub_fw_flash_addr << 8;
		if (!(firmware_flag & (0x01 << sub_fw_type))) {
			gdix_info("Sub firmware type does not math:type=%d\n", sub_fw_type);
			fw_image_offset += sub_fw_len;
			sub_fw_info_pos += 8;
			continue;
		}

		/* if sub fw type is HID subsystem we need compare version before update
		 */
		// TODO update hid subsystem
		gdix_dbg("load sub firmware addr:0x%x,len:0x%x\n", sub_fw_flash_addr,
				 sub_fw_len);
		ret = load_sub_firmware(sub_fw_flash_addr, &fw_data[fw_image_offset],
								sub_fw_len);
		if (ret < 0) {
			gdix_dbg("Failed load sub firmware, ret=%d\n", ret);
			goto update_err;
		}
		fw_image_offset += sub_fw_len;
		sub_fw_info_pos += 8;
	}

	/* flash config with isp if NEED_UPDATE_CONFIG_WITH_ISP flag is setted or
	 * hid subsystem updated.
	 */
	if (image->GetUpdateFlag() & NEED_UPDATE_CONFIG_WITH_ISP) {
		this->is_cfg_flashed_with_isp = true;
		ret = flash_cfg_with_isp();
		if (ret < 0) {
			gdix_err("failed flash config with isp, ret %d\n", ret);
			goto update_err;
		}
	}

	/* reset IC */
	gdix_dbg("reset ic\n");
	retry = 3;
	do {
		ret = dev->Write(buf_restart, sizeof(buf_restart));
		if (ret >= 0)
			break;
		usleep(20000);
	} while (--retry);
	if (retry == 0 && ret < 0)
		gdix_dbg("Failed write restart command, ret=%d\n", ret);
	else
		ret = 0;

	usleep(300000);

	if (dev->WriteSpeCmd(buf_switch_ptp_mode, sizeof(buf_switch_ptp_mode)) <
		0) {
		gdix_err("Failed switch to ptp mode\n");
	}
	return ret;

update_err:
	/* reset IC */
	gdix_dbg("reset ic\n");
	retry = 3;
	do {
		if (dev->Write(buf_restart, sizeof(buf_restart)) >= 0)
			break;
		usleep(20000);
	} while (--retry);
	if (retry == 0 && ret < 0)
		gdix_dbg("Failed write restart command, ret=%d\n", ret);

	usleep(300000);
	if (dev->WriteSpeCmd(buf_switch_ptp_mode, sizeof(buf_switch_ptp_mode)) <
		0) {
		gdix_err("Failed switch to ptp mode\n");
	}
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %esi, %r14d
movq %rdi, %rbx
movw $0x101, %ax             # imm = 0x101
movw %ax, 0x2c(%rsp)
movl $0x1000, 0x28(%rsp)     # imm = 0x1000
movw %ax, 0x24(%rsp)
movl $0x1100, 0x20(%rsp)     # imm = 0x1100
movw %ax, 0x14(%rsp)
movl $0x130e, 0x10(%rsp)     # imm = 0x130E
movw %ax, 0xc(%rsp)
movl $0x303, 0x8(%rsp)       # imm = 0x303
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x60(%rax)
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x68(%rax)
movq %rbx, %rdi
callq 0xf00e
testl %eax, %eax
js 0xf324
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
leaq 0x28(%rsp), %rsi
movl $0x6, %edx
callq *0x38(%rax)
testl %eax, %eax
js 0xf32b
movl %r14d, 0x1c(%rsp)
movl $0x3d090, %edi          # imm = 0x3D090
callq 0x5170
movl $0xfffffffa, %r13d      # imm = 0xFFFFFFFA
leaq 0x30(%rsp), %r14
leaq 0x7ff2(%rip), %r15      # 0x17249
leaq 0x9fb8(%rip), %r12      # 0x19216
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x5095, %esi           # imm = 0x5095
movq %r14, %rdx
movl $0x1, %ecx
callq *0x20(%rax)
movl %eax, %ebp
leaq 0x12013(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xf2a5
movq 0x11d2e(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movq %r15, %rsi
movq %r12, %rdx
movl $0xc3, %ecx
movl $0x5095, %r8d           # imm = 0x5095
xorl %eax, %eax
callq 0x5180
testl %ebp, %ebp
js 0xf347
movzbl 0x30(%rsp), %r9d
cmpl $0xdd, %r9d
je 0xf389
leaq 0x11fca(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xf2f2
movq 0x11ce5(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x7fbe(%rip), %rsi      # 0x1729b
movq %r12, %rdx
movl $0xcb, %ecx
movl $0x5095, %r8d           # imm = 0x5095
xorl %eax, %eax
callq 0x5180
movl $0x7530, %edi           # imm = 0x7530
callq 0x5170
incl %r13d
jne 0xf25e
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
leaq 0x11f80(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xf6b3
callq 0x5d2d
jmp 0xf6b3
movl %eax, %ebp
jmp 0xf769
movl %eax, %ebp
leaq 0x11f5d(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xf6b3
callq 0x5d87
jmp 0xf6b3
leaq 0x11f43(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xf6b3
movq 0x11c7a(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0x7f06(%rip), %rsi      # 0x1726e
leaq 0x9ea7(%rip), %rdx      # 0x19216
movl $0xc5, %ecx
movl $0x5095, %r8d           # imm = 0x5095
movl %ebp, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0xf6b3
movq %rbx, %rdi
callq 0xf00e
testl %eax, %eax
js 0xf66a
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
leaq 0x20(%rsp), %rsi
movl $0x6, %edx
callq *0x38(%rax)
testl %eax, %eax
js 0xf66e
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x5170
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
testq %rax, %rax
je 0xf69e
movq %rax, %r15
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x58(%rax)
movl %eax, %ebp
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x60(%rax)
movl %eax, %r13d
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x68(%rax)
movl %eax, %r14d
leaq 0x11e8f(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xf42e
movq 0x11baa(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x7f2c(%rip), %rsi      # 0x17344
leaq 0x9df7(%rip), %rdx      # 0x19216
movl $0xef, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
testl %ebp, %ebp
je 0xf7a0
jle 0xf564
movl %ebp, 0x18(%rsp)
movl %r13d, %eax
movzbl (%r15,%rax), %ebp
leaq 0x11e42(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xf47b
movq 0x11b5d(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x7f12(%rip), %rsi      # 0x17377
leaq 0x9daa(%rip), %rdx      # 0x19216
movl $0xf6, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
leal 0x1(%r13), %eax
movzbl (%r15,%rax), %eax
shll $0x18, %eax
leal 0x2(%r13), %ecx
movzbl (%r15,%rcx), %ecx
shll $0x10, %ecx
orl %eax, %ecx
leal 0x3(%r13), %eax
movzbl (%r15,%rax), %eax
shll $0x8, %eax
orl %ecx, %eax
leal 0x4(%r13), %ecx
movzbl (%r15,%rcx), %r12d
orl %eax, %r12d
movl 0x1c(%rsp), %eax
btl %ebp, %eax
leaq 0x11dd4(%rip), %rax     # 0x21291
movb (%rax), %al
jae 0xf526
leal 0x5(%r13), %ecx
movzbl (%r15,%rcx), %ecx
leal 0x6(%r13), %edx
movzbl (%r15,%rdx), %ebp
shll $0x10, %ecx
shll $0x8, %ebp
orl %ecx, %ebp
testb %al, %al
je 0xf509
movq 0x11ad2(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x7ef7(%rip), %rsi      # 0x173e7
leaq 0x9d1f(%rip), %rdx      # 0x19216
movl $0x10a, %ecx            # imm = 0x10A
movl %ebp, %r8d
movl %r12d, %r9d
xorl %eax, %eax
callq 0x5180
movl %r14d, %edx
addq %r15, %rdx
movq (%rbx), %rax
movq %rbx, %rdi
movl %ebp, %esi
movl %r12d, %ecx
callq *0x28(%rax)
testl %eax, %eax
jns 0xf551
jmp 0xf7a7
testb %al, %al
je 0xf551
movq 0x11a87(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x7e72(%rip), %rsi      # 0x173ad
leaq 0x9cd4(%rip), %rdx      # 0x19216
movl $0x100, %ecx            # imm = 0x100
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
addl %r12d, %r14d
addl $0x8, %r13d
movl 0x18(%rsp), %ebp
decl %ebp
jne 0xf43c
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0xa8(%rax)
testb $0x10, %al
je 0xf58a
movb $0x1, 0x1a(%rbx)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x40(%rax)
testl %eax, %eax
js 0xf7e5
leaq 0x11d00(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xf5ba
movq 0x11a1b(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x7f0d(%rip), %rsi      # 0x174b4
leaq 0x9c68(%rip), %rdx      # 0x19216
movl $0x122, %ecx            # imm = 0x122
xorl %eax, %eax
callq 0x5180
movl $0xfffffffd, %r12d      # imm = 0xFFFFFFFD
leaq 0x10(%rsp), %r15
xorl %ebp, %ebp
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movq %r15, %rsi
movl $0x6, %edx
callq *0x38(%rax)
testl %eax, %eax
jns 0xf62a
movl %eax, %r14d
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5170
incl %r12d
jne 0xf5c7
testl %r14d, %r14d
jns 0xf62a
leaq 0x11c96(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xf627
movq 0x119b1(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x7ebe(%rip), %rsi      # 0x174cf
leaq 0x9bfe(%rip), %rdx      # 0x19216
movl $0x12b, %ecx            # imm = 0x12B
movl %r14d, %r8d
xorl %eax, %eax
callq 0x5180
movl %r14d, %ebp
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5170
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
leaq 0x8(%rsp), %rsi
movl $0x6, %edx
callq *0x40(%rax)
testl %eax, %eax
jns 0xf769
leaq 0x11c3a(%rip), %rax     # 0x21291
cmpb $0x0, (%rax)
je 0xf769
movl $0x133, %ecx            # imm = 0x133
jmp 0xf77f
movl %eax, %ebp
jmp 0xf6b3
movl %eax, %ebp
leaq 0x11c1a(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xf6b3
movq 0x11955(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0x7c65(%rip), %rsi      # 0x172f2
leaq 0x9b82(%rip), %rdx      # 0x19216
movl $0xdc, %ecx
jmp 0xf7d6
movl $0xfffffffc, %ebp       # imm = 0xFFFFFFFC
leaq 0x11be7(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
je 0xf816
leaq 0x11bd7(%rip), %r15     # 0x21291
cmpb $0x1, (%r15)
jne 0xf6e4
movq 0x118f1(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x7de3(%rip), %rsi      # 0x174b4
leaq 0x9b3e(%rip), %rdx      # 0x19216
movl $0x139, %ecx            # imm = 0x139
xorl %eax, %eax
callq 0x5180
movl $0xfffffffd, %r12d      # imm = 0xFFFFFFFD
leaq 0x10(%rsp), %r14
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movl $0x6, %edx
callq *0x38(%rax)
testl %eax, %eax
jns 0xf741
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5170
incl %r12d
jne 0xf6ef
cmpb $0x1, (%r15)
jne 0xf741
movq 0x11897(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x7da4(%rip), %rsi      # 0x174cf
leaq 0x9ae4(%rip), %rdx      # 0x19216
movl $0x141, %ecx            # imm = 0x141
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5170
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
leaq 0x8(%rsp), %rsi
movl $0x6, %edx
callq *0x40(%rax)
testl %eax, %eax
jns 0xf769
cmpb $0x1, (%r15)
je 0xf77a
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x146, %ecx            # imm = 0x146
movq 0x11852(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0x7d76(%rip), %rsi      # 0x17506
leaq 0x9a7f(%rip), %rdx      # 0x19216
xorl %eax, %eax
callq 0x5180
jmp 0xf769
movl $0xfffffffb, %ebp       # imm = 0xFFFFFFFB
jmp 0xf769
movl %eax, %ebp
leaq 0x11ae1(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xf6b3
movq 0x117f8(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0x7c54(%rip), %rsi      # 0x1741e
leaq 0x9a45(%rip), %rdx      # 0x19216
movl $0x10e, %ecx            # imm = 0x10E
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0xf6b3
movl %eax, %ebp
leaq 0x11aa3(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xf6b3
movq 0x117da(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0x7c49(%rip), %rsi      # 0x17451
leaq 0x9a07(%rip), %rdx      # 0x19216
movl $0x11c, %ecx            # imm = 0x11C
jmp 0xf7d6
callq 0x5d5d
jmp 0xf6b3