// File: /workspace/github_cpp_makefile_star2_O3/goodix[P]gdix_hid_firmware_update/gt7868q/gt7868q_update.cpp
// Function: GT7868QUpdate::fw_update(unsigned int)
int GT7868QUpdate::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[] = {0x32, 0x00, 0x00, 0x00, 0x32};
	unsigned char buf_dis_report_coor[] = {0x33, 0x00, 0x00, 0x00, 0x33};
	unsigned char buf_en_report_coor[] = {0x34, 0x00, 0x00, 0x00, 0x34};

	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;

	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);

	/*dis report coor*/
	gdix_info("disable report coor\n");
	retry = 3;
	do {
		ret = dev->Write(CMD_ADDR, buf_dis_report_coor,
						 sizeof(buf_dis_report_coor));
		if (ret < 0) {
			gdix_err("Failed disable report coor\n");
		}
	} while (--retry);

	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;
	}

	/* 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) {
		ret = -5;
		goto update_err;
	}

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

	/*en report coor*/
	gdix_info("enable report coor\n");
	ret = dev->Write(CMD_ADDR, buf_en_report_coor, sizeof(buf_en_report_coor));
	if (ret < 0) {
		gdix_err("Failed enable report coor\n");
	}

	/* 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->Write(CMD_ADDR, buf_switch_ptp_mode, sizeof(buf_switch_ptp_mode)) <
		0) {
		gdix_err("Failed switch to ptp mode\n");
	}
	return ret;

update_err:
	/*en report coor*/
	gdix_info("enable report coor\n");
	ret = dev->Write(CMD_ADDR, buf_en_report_coor, sizeof(buf_en_report_coor));
	if (ret < 0) {
		gdix_err("Failed enable report coor\n");
	}

	/* 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->Write(CMD_ADDR, 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 $0x88, %rsp
movl %esi, 0x2c(%rsp)
movq %rdi, %rbx
movw $0x101, %ax             # imm = 0x101
leaq 0x38(%rsp), %r14
movw %ax, 0x4(%r14)
movl $0x1000, (%r14)         # imm = 0x1000
movw %ax, 0x34(%rsp)
movl $0x1100, 0x30(%rsp)     # imm = 0x1100
movw %ax, 0x1c(%rsp)
movl $0x130e, 0x18(%rsp)     # imm = 0x130E
movb $0x32, 0x14(%rsp)
movl $0x32, 0x10(%rsp)
movb $0x33, 0x24(%rsp)
movl $0x33, 0x20(%rsp)
movb $0x34, 0xc(%rsp)
movl $0x34, 0x8(%rsp)
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x60(%rax)
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x68(%rax)
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movl $0x6, %edx
callq *0x38(%rax)
testl %eax, %eax
js 0x92fa
movl $0x3d090, %edi          # imm = 0x3D090
callq 0x5170
leaq 0x180d8(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x91e2
movq 0x17df3(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xdff3(%rip), %rsi      # 0x171c2
leaq 0x10000(%rip), %rdx     # 0x191d6
movl $0x98, %ecx
xorl %eax, %eax
callq 0x5180
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
leaq 0x20(%rsp), %r14
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x4160, %esi           # imm = 0x4160
movq %r14, %rdx
movl $0x5, %ecx
callq *0x30(%rax)
testl %eax, %eax
jns 0x9213
leaq 0x18083(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
je 0x9219
incl %ebp
jne 0x91ec
jmp 0x9220
callq 0x53d7
jmp 0x9213
movl $0xfffffffa, %r13d      # imm = 0xFFFFFFFA
leaq 0x40(%rsp), %r14
leaq 0xdfb6(%rip), %r15      # 0x171e8
leaq 0xff9d(%rip), %r12      # 0x191d6
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 0x18038(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x9280
movq 0x17d53(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movq %r15, %rsi
movq %r12, %rdx
movl $0xa5, %ecx
movl $0x5095, %r8d           # imm = 0x5095
xorl %eax, %eax
callq 0x5180
testl %ebp, %ebp
js 0x9314
movzbl 0x40(%rsp), %r9d
cmpl $0xdd, %r9d
je 0x9356
leaq 0x17fef(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x92cd
movq 0x17d0a(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xdf82(%rip), %rsi      # 0x1723a
movq %r12, %rdx
movl $0xad, %ecx
movl $0x5095, %r8d           # imm = 0x5095
xorl %eax, %eax
callq 0x5180
movl $0x7530, %edi           # imm = 0x7530
callq 0x5170
incl %r13d
jne 0x9239
leaq 0x17faa(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x9687
callq 0x5401
jmp 0x9687
leaq 0x17f90(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x9687
callq 0x545b
jmp 0x9687
leaq 0x17f76(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x9687
movq 0x17cad(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xded8(%rip), %rsi      # 0x1720d
leaq 0xfe9a(%rip), %rdx      # 0x191d6
movl $0xa7, %ecx
movl $0x5095, %r8d           # imm = 0x5095
movl %ebp, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x9687
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
leaq 0x30(%rsp), %rsi
movl $0x6, %edx
callq *0x38(%rax)
testl %eax, %eax
js 0x9649
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x5170
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
testq %rax, %rax
je 0x9677
movq %rax, %r14
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, %r12d
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x68(%rax)
movl %eax, %r13d
leaq 0x17ed2(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x93eb
movq 0x17bed(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xdf0e(%rip), %rsi      # 0x172e3
leaq 0xfdfa(%rip), %rdx      # 0x191d6
movl $0xcd, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
testl %ebp, %ebp
je 0x9687
jle 0x9529
movl %ebp, 0x28(%rsp)
movl %r12d, %eax
movzbl (%r14,%rax), %r15d
leaq 0x17e85(%rip), %rax     # 0x21291
cmpb $0x0, (%rax)
je 0x9438
movq 0x17ba0(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xdef4(%rip), %rsi      # 0x17316
leaq 0xfdad(%rip), %rdx      # 0x191d6
movl $0xd6, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x5180
leal 0x1(%r12), %eax
movzbl (%r14,%rax), %eax
shll $0x18, %eax
leal 0x2(%r12), %ecx
movzbl (%r14,%rcx), %ecx
shll $0x10, %ecx
orl %eax, %ecx
leal 0x3(%r12), %eax
movzbl (%r14,%rax), %eax
shll $0x8, %eax
orl %ecx, %eax
leal 0x4(%r12), %ecx
movzbl (%r14,%rcx), %ebp
orl %eax, %ebp
movl 0x2c(%rsp), %eax
btl %r15d, %eax
leaq 0x17e13(%rip), %rax     # 0x21291
movb (%rax), %al
jae 0x94eb
leal 0x5(%r12), %ecx
movzbl (%r14,%rcx), %ecx
leal 0x6(%r12), %edx
movzbl (%r14,%rdx), %r15d
shll $0x10, %ecx
shll $0x8, %r15d
orl %ecx, %r15d
testb %al, %al
je 0x94ce
movq 0x17b0d(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xded1(%rip), %rsi      # 0x17386
leaq 0xfd1a(%rip), %rdx      # 0x191d6
movl $0xea, %ecx
movl %r15d, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x5180
movl %r13d, %edx
addq %r14, %rdx
movq (%rbx), %rax
movq %rbx, %rdi
movl %r15d, %esi
movl %ebp, %ecx
callq *0x28(%rax)
testl %eax, %eax
jns 0x9516
jmp 0x984b
testb %al, %al
je 0x9516
movq 0x17ac2(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xde4c(%rip), %rsi      # 0x1734c
leaq 0xfccf(%rip), %rdx      # 0x191d6
movl $0xe0, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x5180
addl %ebp, %r13d
addl $0x8, %r12d
movl 0x28(%rsp), %ebp
decl %ebp
jne 0x93f9
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0xa8(%rax)
testb $0x10, %al
je 0x954f
movb $0x1, 0x1a(%rbx)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x40(%rax)
testl %eax, %eax
js 0x9887
leaq 0x17d3b(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x957f
movq 0x17a56(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xd908(%rip), %rsi      # 0x16e74
leaq 0xfc63(%rip), %rdx      # 0x191d6
movl $0x102, %ecx            # imm = 0x102
xorl %eax, %eax
callq 0x5180
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
leaq 0x8(%rsp), %rdx
movl $0x4160, %esi           # imm = 0x4160
movl $0x5, %ecx
callq *0x30(%rax)
leaq 0x17cf2(%rip), %rcx     # 0x21291
movb (%rcx), %cl
testl %eax, %eax
jns 0x95ae
testb $0x1, %cl
jne 0x98c0
testb $0x1, %cl
je 0x95d7
movq 0x179fe(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xde8f(%rip), %rsi      # 0x17453
leaq 0xfc0b(%rip), %rdx      # 0x191d6
movl $0x109, %ecx            # imm = 0x109
xorl %eax, %eax
callq 0x5180
movl $0xfffffffd, %r15d      # imm = 0xFFFFFFFD
leaq 0x18(%rsp), %r14
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movl $0x6, %edx
callq *0x38(%rax)
testl %eax, %eax
jns 0x9807
movl %eax, %ebp
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5170
incl %r15d
jne 0x95e2
leaq 0x17c7d(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x9809
movq 0x17994(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xde40(%rip), %rsi      # 0x1746e
leaq 0xfba1(%rip), %rdx      # 0x191d6
movl $0x112, %ecx            # imm = 0x112
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x9809
leaq 0x17c41(%rip), %rcx     # 0x21291
cmpb $0x1, (%rcx)
jne 0x9687
movq 0x1797c(%rip), %rcx     # 0x20fd8
movq (%rcx), %rdi
leaq 0xdc2b(%rip), %rsi      # 0x17291
leaq 0xfb69(%rip), %rdx      # 0x191d6
movl $0xba, %ecx
jmp 0x9878
leaq 0x17c13(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
je 0x98b6
leaq 0x17c03(%rip), %r12     # 0x21291
cmpb $0x1, (%r12)
jne 0x96b9
movq 0x1791c(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xd7ce(%rip), %rsi      # 0x16e74
leaq 0xfb29(%rip), %rdx      # 0x191d6
movl $0x120, %ecx            # imm = 0x120
xorl %eax, %eax
callq 0x5180
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
leaq 0x8(%rsp), %rdx
movl $0x4160, %esi           # imm = 0x4160
movl $0x5, %ecx
callq *0x30(%rax)
movl %eax, %ebp
movb (%r12), %al
testl %ebp, %ebp
jns 0x96e4
testb $0x1, %al
jne 0x97b4
testb $0x1, %al
je 0x970c
movq 0x178c9(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xdd5a(%rip), %rsi      # 0x17453
leaq 0xfad6(%rip), %rdx      # 0x191d6
movl $0x127, %ecx            # imm = 0x127
xorl %eax, %eax
callq 0x5180
movl $0xfffffffd, %r13d      # imm = 0xFFFFFFFD
leaq 0x18(%rsp), %r14
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movl $0x6, %edx
callq *0x38(%rax)
movl %eax, %r15d
testl %eax, %eax
jns 0x973f
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5170
incl %r13d
jne 0x9717
testl %ebp, %r15d
jns 0x9772
cmpb $0x1, (%r12)
jne 0x9772
movq 0x17866(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xdd12(%rip), %rsi      # 0x1746e
leaq 0xfa73(%rip), %rdx      # 0x191d6
movl $0x12f, %ecx            # imm = 0x12F
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5170
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
leaq 0x10(%rsp), %rdx
movl $0x4160, %esi           # imm = 0x4160
movl $0x5, %ecx
callq *0x30(%rax)
testl %eax, %eax
jns 0x97a0
cmpb $0x1, (%r12)
je 0x97e1
movl %ebp, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1781d(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xdc62(%rip), %rsi      # 0x17427
leaq 0xfa0a(%rip), %rdx      # 0x191d6
movl $0x123, %ecx            # imm = 0x123
xorl %eax, %eax
callq 0x5180
movb (%r12), %al
jmp 0x96e4
movl $0x134, %ecx            # imm = 0x134
movq 0x177eb(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xdcae(%rip), %rsi      # 0x174a5
leaq 0xf9d8(%rip), %rdx      # 0x191d6
xorl %eax, %eax
callq 0x5180
jmp 0x97a0
xorl %ebp, %ebp
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5170
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
leaq 0x10(%rsp), %rdx
movl $0x4160, %esi           # imm = 0x4160
movl $0x5, %ecx
callq *0x30(%rax)
testl %eax, %eax
jns 0x97a0
leaq 0x17a56(%rip), %rax     # 0x21291
cmpb $0x0, (%rax)
je 0x97a0
movl $0x11a, %ecx            # imm = 0x11A
jmp 0x97e6
leaq 0x17a3f(%rip), %rcx     # 0x21291
cmpb $0x1, (%rcx)
jne 0x9687
movq 0x17756(%rip), %rcx     # 0x20fb8
movq (%rcx), %rdi
leaq 0xdb51(%rip), %rsi      # 0x173bd
leaq 0xf963(%rip), %rdx      # 0x191d6
movl $0xee, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x9687
leaq 0x17a03(%rip), %rcx     # 0x21291
cmpb $0x1, (%rcx)
jne 0x9687
movq 0x1773a(%rip), %rcx     # 0x20fd8
movq (%rcx), %rdi
leaq 0xdb48(%rip), %rsi      # 0x173f0
leaq 0xf927(%rip), %rdx      # 0x191d6
movl $0xfc, %ecx
jmp 0x9878
callq 0x5431
jmp 0x9687
movq 0x17711(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xdb56(%rip), %rsi      # 0x17427
leaq 0xf8fe(%rip), %rdx      # 0x191d6
movl $0x105, %ecx            # imm = 0x105
xorl %eax, %eax
callq 0x5180
leaq 0x179a6(%rip), %rax     # 0x21291
movb (%rax), %cl
jmp 0x95ae