// File: /workspace/github_cpp_makefile_star2_O3/goodix[P]gdix_hid_firmware_update/gtx2/gtx2_update.cpp
// Function: GTx2Update::fw_update(unsigned int)
int GTx2Update::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");
		return ret;
	}

	usleep(250000);
	retry = GDIX_RETRY_TIMES;
	do {
		ret = dev->Read(BL_STATE_ADDR, temp_buf, 1);
		gdix_dbg("BL_STATE_ADDR\n");
		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);
	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_len =
			(fw_data[sub_fw_info_pos + 1] << 8) | fw_data[sub_fw_info_pos + 2];

		sub_fw_flash_addr =
			(fw_data[sub_fw_info_pos + 3] << 8) | fw_data[sub_fw_info_pos + 4];
		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;
		}
		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;
	}
	if (sub_fw_num == 0)
		return -5;
	/* 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:
	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 $0x68, %rsp
movl %esi, 0xc(%rsp)
movq %rdi, %rbx
movw $0x101, %ax             # imm = 0x101
leaq 0x18(%rsp), %r15
movw %ax, 0x4(%r15)
movl $0x1000, (%r15)         # imm = 0x1000
movw %ax, 0x14(%rsp)
movl $0x1100, 0x10(%rsp)     # imm = 0x1100
movw %ax, 0x8(%rsp)
movl $0x130e, 0x4(%rsp)      # imm = 0x130E
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x60(%rax)
movl %eax, %r14d
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x68(%rax)
movl %eax, (%rsp)
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movq %r15, %rsi
movl $0x6, %edx
callq *0x38(%rax)
testl %eax, %eax
js 0xb19d
movl $0x3d090, %edi          # imm = 0x3D090
callq 0x5170
movl $0xfffffffa, %r15d      # imm = 0xFFFFFFFA
leaq 0x20(%rsp), %r13
leaq 0xe100(%rip), %rbp      # 0x191d6
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x5095, %esi           # imm = 0x5095
movq %r13, %rdx
movl $0x1, %ecx
callq *0x20(%rax)
movl %eax, %r12d
leaq 0x1619a(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb11c
movq 0x15eb5(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xcc6b(%rip), %rsi      # 0x17d78
movq %rbp, %rdx
movl $0xf4, %ecx
xorl %eax, %eax
callq 0x5180
testl %r12d, %r12d
js 0xb1ba
movzbl 0x20(%rsp), %r9d
cmpl $0xdd, %r9d
je 0xb1fc
leaq 0x16152(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb16a
movq 0x15e6d(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc0e5(%rip), %rsi      # 0x1723a
movq %rbp, %rdx
movl $0xfc, %ecx
movl $0x5095, %r8d           # imm = 0x5095
xorl %eax, %eax
callq 0x5180
movl $0x7530, %edi           # imm = 0x7530
callq 0x5170
incl %r15d
jne 0xb0d6
movl $0xfffffffe, %r12d      # imm = 0xFFFFFFFE
leaq 0x16107(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb4a8
callq 0x573f
jmp 0xb4a8
movl %eax, %r12d
leaq 0x160ea(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb546
callq 0x5799
jmp 0xb546
leaq 0x160d0(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb4a8
movq 0x15e07(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xc032(%rip), %rsi      # 0x1720d
leaq 0xdff4(%rip), %rdx      # 0x191d6
movl $0xf6, %ecx
movl $0x5095, %r8d           # imm = 0x5095
movl %r12d, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0xb4a8
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
leaq 0x10(%rsp), %rsi
movl $0x6, %edx
callq *0x38(%rax)
testl %eax, %eax
js 0xb36e
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x5170
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
testq %rax, %rax
je 0xb3a3
movq %rax, %r13
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x58(%rax)
movl %eax, %ebp
leaq 0x16046(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb277
movq 0x15d61(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc082(%rip), %rsi      # 0x172e3
leaq 0xdf6e(%rip), %rdx      # 0x191d6
movl $0x11b, %ecx            # imm = 0x11B
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
testl %ebp, %ebp
jle 0xb3c3
movl (%rsp), %edx
movl %r14d, %eax
movzbl (%r13,%rax), %r12d
leaq 0x15fff(%rip), %rax     # 0x21291
cmpb $0x0, (%rax)
je 0xb2c4
movq 0x15d1a(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc06e(%rip), %rsi      # 0x17316
movl %edx, %r15d
leaq 0xdf24(%rip), %rdx      # 0x191d6
movl $0x11e, %ecx            # imm = 0x11E
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
movl %r15d, %edx
leal 0x1(%r14), %eax
movzbl (%r13,%rax), %eax
shll $0x8, %eax
leal 0x2(%r14), %ecx
movzbl (%r13,%rcx), %r15d
orl %eax, %r15d
movl 0xc(%rsp), %eax
btl %r12d, %eax
jae 0xb324
leal 0x4(%r14), %eax
movzbl (%r13,%rax), %esi
leal 0x3(%r14), %eax
movzbl (%r13,%rax), %eax
shll $0x10, %eax
shll $0x8, %esi
orl %eax, %esi
movl %edx, %r12d
movl %edx, %edx
addq %r13, %rdx
movq (%rbx), %rax
movq %rbx, %rdi
movl %r15d, %ecx
callq *0x28(%rax)
movl %r12d, %edx
testl %eax, %eax
jns 0xb35d
jmp 0xb472
leaq 0x15f66(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb35d
movq 0x15c81(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc00b(%rip), %rsi      # 0x1734c
movl %edx, (%rsp)
leaq 0xde8b(%rip), %rdx      # 0x191d6
movl $0x12a, %ecx            # imm = 0x12A
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
movl (%rsp), %edx
addl %r15d, %edx
addl $0x8, %r14d
decl %ebp
jne 0xb282
jmp 0xb3c9
movl %eax, %r12d
leaq 0x15f19(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb4a8
movq 0x15c50(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xbeff(%rip), %rsi      # 0x17291
leaq 0xde3d(%rip), %rdx      # 0x191d6
movl $0x109, %ecx            # imm = 0x109
jmp 0xb49e
movl $0xfffffffc, %r12d      # imm = 0xFFFFFFFC
leaq 0x15ee1(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb4a8
callq 0x576f
jmp 0xb4a8
je 0xb558
leaq 0x15ec1(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb3f9
movq 0x15bdc(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc06d(%rip), %rsi      # 0x17453
leaq 0xdde9(%rip), %rdx      # 0x191d6
movl $0x13b, %ecx            # imm = 0x13B
xorl %eax, %eax
callq 0x5180
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
leaq 0x4(%rsp), %r14
leaq 0xc064(%rip), %r15      # 0x1746e
leaq 0xddc5(%rip), %r12      # 0x191d6
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movl $0x6, %edx
callq *0x38(%rax)
testl %eax, %eax
jns 0xb452
leaq 0x15e63(%rip), %rcx     # 0x21291
cmpb $0x1, (%rcx)
jne 0xb452
movq 0x15b7e(%rip), %rcx     # 0x20fb8
movq (%rcx), %rdi
movq %r15, %rsi
movq %r12, %rdx
movl $0x140, %ecx            # imm = 0x140
movl %eax, %r8d
xorl %eax, %eax
callq 0x5180
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5170
incl %ebp
jne 0xb411
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5170
xorl %r12d, %r12d
jmp 0xb546
movl %eax, %r12d
leaq 0x15e15(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb4a8
movq 0x15b30(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xbf2b(%rip), %rsi      # 0x173bd
leaq 0xdd3d(%rip), %rdx      # 0x191d6
movl $0x132, %ecx            # imm = 0x132
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
leaq 0x15de2(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb4d8
movq 0x15afd(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xbf8e(%rip), %rsi      # 0x17453
leaq 0xdd0a(%rip), %rdx      # 0x191d6
movl $0x147, %ecx            # imm = 0x147
xorl %eax, %eax
callq 0x5180
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
leaq 0x4(%rsp), %r14
leaq 0xc8af(%rip), %r15      # 0x17d98
leaq 0xdce6(%rip), %r13      # 0x191d6
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movl $0x6, %edx
callq *0x38(%rax)
testl %eax, %eax
jns 0xb52e
leaq 0x15d84(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb52e
movq 0x15a9f(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movq %r15, %rsi
movq %r13, %rdx
movl $0x14b, %ecx            # imm = 0x14B
xorl %eax, %eax
callq 0x5180
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5170
incl %ebp
jne 0xb4f0
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5170
movl %r12d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xfffffffb, %r12d      # imm = 0xFFFFFFFB
jmp 0xb546