// File: /workspace/github_cpp_makefile_star2_O2/goodix[P]gdix_hid_firmware_update/gtx3/gtx3_update.cpp
// Function: GTx3Update::fw_update(unsigned int)
int GTx3Update::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};

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

	/* flash HID subsystem */
	if (need_upgrade_hid_subsystem(dev, image)) {
		firmware_flag |= (0x1 << HID_SUBSYSTEM_TYPE_ID);
		gdix_dbg("hid subsystem updata flag setted\n");
	}

	/* 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 ||
		firmware_flag & (0x1 << HID_SUBSYSTEM_TYPE_ID)) {
		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)
			gdix_dbg("Failed write restart command, ret=%d\n", ret);
		usleep(20000);
	} while (--retry);
	usleep(300000);
	return 0;

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

	usleep(300000);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %esi, %ebp
movq %rdi, %rbx
movw $0x101, %ax             # imm = 0x101
leaq 0x28(%rsp), %r15
movw %ax, 0x4(%r15)
movl $0x1000, (%r15)         # imm = 0x1000
movw %ax, 0x24(%rsp)
movl $0x1100, 0x20(%rsp)     # imm = 0x1100
movw %ax, 0x1c(%rsp)
movl $0x130e, 0x18(%rsp)     # imm = 0x130E
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x60(%rax)
movq %rax, 0x8(%rsp)
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x68(%rax)
movl %eax, %r14d
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
pushq $0x6
popq %rdx
movq %r15, %rsi
callq *0x38(%rax)
testl %eax, %eax
js 0xac4e
movl %r14d, 0x14(%rsp)
movl %ebp, 0x10(%rsp)
movl $0x3d090, %edi          # imm = 0x3D090
callq 0x5170
pushq $-0x6
popq %r14
leaq 0x30(%rsp), %r13
pushq $0x1
popq %rbp
leaq 0xd66e(%rip), %r15      # 0x181d6
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x5095, %esi           # imm = 0x5095
movq %r13, %rdx
movl %ebp, %ecx
callq *0x20(%rax)
movl %eax, %r12d
leaq 0x1570b(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xabb1
movq 0x15426(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xb64c(%rip), %rsi      # 0x161e8
movq %r15, %rdx
movl $0xb7, %ecx
movl $0x5095, %r8d           # imm = 0x5095
xorl %eax, %eax
callq 0x5180
testl %r12d, %r12d
js 0xac8a
movzbl 0x30(%rsp), %r9d
cmpl $0xdd, %r9d
je 0xaccc
leaq 0x156bd(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xabff
movq 0x153d8(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xb650(%rip), %rsi      # 0x1623a
movq %r15, %rdx
movl $0xbf, %ecx
movl $0x5095, %r8d           # imm = 0x5095
xorl %eax, %eax
callq 0x5180
movl $0x7530, %edi           # imm = 0x7530
callq 0x5170
incl %r14d
jne 0xab68
pushq $-0x2
popq %r12
leaq 0x15674(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xb081
movq 0x153ab(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xb637(%rip), %rsi      # 0x1626e
leaq 0xd598(%rip), %rdx      # 0x181d6
movl $0xc4, %ecx
movl $0x5095, %r8d           # imm = 0x5095
jmp 0xb065
movl %eax, %r12d
leaq 0x15639(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xb081
movq 0x15370(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xb527(%rip), %rsi      # 0x16199
leaq 0xd55d(%rip), %rdx      # 0x181d6
movl $0xaf, %ecx
xorl %eax, %eax
callq 0x5180
jmp 0xb081
leaq 0x15600(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xb081
movq 0x15337(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xb562(%rip), %rsi      # 0x1620d
leaq 0xd524(%rip), %rdx      # 0x181d6
movl $0xb9, %ecx
movl $0x5095, %r8d           # imm = 0x5095
movl %r12d, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0xb081
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
leaq 0x20(%rsp), %rsi
pushq $0x6
popq %rdx
callq *0x38(%rax)
testl %eax, %eax
js 0xaef6
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x5170
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
testq %rax, %rax
je 0xaf2b
movq %rax, %r13
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x58(%rax)
movl %eax, %ebp
leaq 0x15578(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xad45
movq 0x15293(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xb5b4(%rip), %rsi      # 0x162e3
leaq 0xd4a0(%rip), %rdx      # 0x181d6
movl $0xde, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
testl %ebp, %ebp
je 0xb02d
movq 0x8(%rbx), %rdi
movq 0x10(%rbx), %rsi
callq 0xa988
testb %al, %al
je 0xad93
orl $0x20, 0x10(%rsp)
leaq 0x15527(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xad93
movq 0x15242(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xc3e3(%rip), %rsi      # 0x17163
leaq 0xd44f(%rip), %rdx      # 0x181d6
movl $0xe5, %ecx
xorl %eax, %eax
callq 0x5180
movq 0x8(%rsp), %rdx
xorl %eax, %eax
testl %ebp, %ebp
cmovlel %eax, %ebp
addl $0x6, %edx
subl $0x1, %ebp
jb 0xaf61
leal -0x6(%rdx), %eax
movl %eax, %eax
movzbl (%r13,%rax), %r12d
leaq 0x154d4(%rip), %rax     # 0x20291
cmpb $0x0, (%rax)
je 0xadef
movq 0x151ef(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xb543(%rip), %rsi      # 0x16316
movq %rdx, %r14
leaq 0xd3f9(%rip), %rdx      # 0x181d6
movl $0xeb, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
movq %r14, %rdx
leal -0x5(%rdx), %eax
movzbl (%r13,%rax), %eax
shll $0x18, %eax
leal -0x4(%rdx), %ecx
movzbl (%r13,%rcx), %ecx
shll $0x10, %ecx
orl %eax, %ecx
leal -0x3(%rdx), %eax
movzbl (%r13,%rax), %eax
shll $0x8, %eax
orl %ecx, %eax
leal -0x2(%rdx), %ecx
movzbl (%r13,%rcx), %r15d
orl %eax, %r15d
movl 0x10(%rsp), %eax
btl %r12d, %eax
leaq 0x1545f(%rip), %rax     # 0x20291
movb (%rax), %al
jae 0xaeac
leal -0x1(%rdx), %ecx
movzbl (%r13,%rcx), %ecx
movq %rdx, 0x8(%rsp)
movl %edx, %edx
movzbl (%r13,%rdx), %r12d
shll $0x10, %ecx
shll $0x8, %r12d
orl %ecx, %r12d
testb %al, %al
je 0xae84
movq 0x15157(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xb51b(%rip), %rsi      # 0x16386
leaq 0xd364(%rip), %rdx      # 0x181d6
movl $0xff, %ecx
movl %r12d, %r8d
movl %r15d, %r9d
xorl %eax, %eax
callq 0x5180
movl 0x14(%rsp), %r14d
movl %r14d, %edx
addq %r13, %rdx
movq (%rbx), %rax
movq %rbx, %rdi
movl %r12d, %esi
movl %r15d, %ecx
callq *0x28(%rax)
testl %eax, %eax
movq 0x8(%rsp), %rdx
jns 0xaee6
jmp 0xb036
testb %al, %al
movl 0x14(%rsp), %r14d
je 0xaee6
movq 0x150fc(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xb486(%rip), %rsi      # 0x1634c
movq %rdx, 0x8(%rsp)
leaq 0xd304(%rip), %rdx      # 0x181d6
movl $0xf5, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
movq 0x8(%rsp), %rdx
addl %r15d, %r14d
movl %r14d, 0x14(%rsp)
addl $0x8, %edx
jmp 0xada2
movl %eax, %r12d
leaq 0x15391(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xb081
movq 0x150c8(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xb377(%rip), %rsi      # 0x16291
leaq 0xd2b5(%rip), %rdx      # 0x181d6
movl $0xcc, %ecx
jmp 0xb062
pushq $-0x4
popq %r12
leaq 0x1535b(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xb081
movq 0x15092(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xb36f(%rip), %rsi      # 0x162bf
leaq 0xd27f(%rip), %rdx      # 0x181d6
movl $0xd4, %ecx
jmp 0xac7e
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0xa8(%rax)
andl $0x10, %eax
movl 0x10(%rsp), %ecx
andl $0x20, %ecx
orl %eax, %ecx
je 0xaf91
movb $0x1, 0x19(%rbx)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x40(%rax)
testl %eax, %eax
js 0xb06e
leaq 0x152f9(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xafc1
movq 0x15014(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xb4a5(%rip), %rsi      # 0x16453
leaq 0xd221(%rip), %rdx      # 0x181d6
movl $0x118, %ecx            # imm = 0x118
xorl %eax, %eax
callq 0x5180
pushq $-0x3
popq %r15
leaq 0x18(%rsp), %r14
pushq $0x6
popq %rbp
leaq 0xd202(%rip), %r13      # 0x181d6
xorl %r12d, %r12d
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movl %ebp, %edx
callq *0x38(%rax)
testl %eax, %eax
jns 0xb019
leaq 0x152a0(%rip), %rcx     # 0x20291
cmpb $0x1, (%rcx)
jne 0xb019
movq 0x14fbb(%rip), %rcx     # 0x1ffb8
movq (%rcx), %rdi
leaq 0xb467(%rip), %rsi      # 0x1646e
movq %r13, %rdx
movl $0x11d, %ecx            # imm = 0x11D
movl %eax, %r8d
xorl %eax, %eax
callq 0x5180
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5170
incl %r15d
jne 0xafd7
jmp 0xb10a
pushq $-0x5
popq %r12
jmp 0xb114
movl %eax, %r12d
leaq 0x15251(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xb081
movq 0x14f6c(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xb367(%rip), %rsi      # 0x163bd
leaq 0xd179(%rip), %rdx      # 0x181d6
movl $0x103, %ecx            # imm = 0x103
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0xb081
movl %eax, %r12d
leaq 0x15219(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
je 0xb126
leaq 0x15209(%rip), %r15     # 0x20291
cmpb $0x1, (%r15)
jne 0xb0b2
movq 0x14f23(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xb3b4(%rip), %rsi      # 0x16453
leaq 0xd130(%rip), %rdx      # 0x181d6
movl $0x125, %ecx            # imm = 0x125
xorl %eax, %eax
callq 0x5180
pushq $-0x3
popq %r13
leaq 0x18(%rsp), %r14
pushq $0x6
popq %rbp
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movl %ebp, %edx
callq *0x38(%rax)
testl %eax, %eax
jns 0xb0fb
cmpb $0x1, (%r15)
jne 0xb0fb
movq 0x14eda(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xbcb0(%rip), %rsi      # 0x16d98
leaq 0xd0e7(%rip), %rdx      # 0x181d6
movl $0x129, %ecx            # imm = 0x129
xorl %eax, %eax
callq 0x5180
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5170
incl %r13d
jne 0xb0be
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5170
movl %r12d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x14eab(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xb2b9(%rip), %rsi      # 0x163f0
leaq 0xd098(%rip), %rdx      # 0x181d6
movl $0x112, %ecx            # imm = 0x112
jmp 0xb062