// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movl %esi, 0x1c(%rsp)
movq %rdi, %rbx
movw $0x101, %ax             # imm = 0x101
leaq 0x28(%rsp), %r12
movw %ax, 0x4(%r12)
movl $0x1000, (%r12)         # imm = 0x1000
movw %ax, 0x24(%rsp)
movl $0x1100, 0x20(%rsp)     # imm = 0x1100
movw %ax, 0x18(%rsp)
movl $0x130e, 0x14(%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, 0xc(%rsp)
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movq %r12, %rsi
movl $0x6, %edx
callq *0x38(%rax)
testl %eax, %eax
js 0xb222
movl $0x3d090, %edi          # imm = 0x3D090
callq 0x5170
movl $0xfffffffa, %r15d      # imm = 0xFFFFFFFA
leaq 0x30(%rsp), %r13
leaq 0xe0bb(%rip), %rbp      # 0x19216
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 0x16115(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb1a1
movq 0x15e30(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xcc47(%rip), %rsi      # 0x17dd9
movq %rbp, %rdx
movl $0xf4, %ecx
xorl %eax, %eax
callq 0x5180
testl %r12d, %r12d
js 0xb23f
movzbl 0x30(%rsp), %r9d
cmpl $0xdd, %r9d
je 0xb281
leaq 0x160cd(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb1ef
movq 0x15de8(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc0c1(%rip), %rsi      # 0x1729b
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 0xb15b
movl $0xfffffffe, %r12d      # imm = 0xFFFFFFFE
leaq 0x16082(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb535
callq 0x5793
jmp 0xb535
movl %eax, %r12d
leaq 0x16065(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb5d3
callq 0x57ed
jmp 0xb5d3
leaq 0x1604b(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb535
movq 0x15d82(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xc00e(%rip), %rsi      # 0x1726e
leaq 0xdfaf(%rip), %rdx      # 0x19216
movl $0xf6, %ecx
movl $0x5095, %r8d           # imm = 0x5095
movl %r12d, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0xb535
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
leaq 0x20(%rsp), %rsi
movl $0x6, %edx
callq *0x38(%rax)
testl %eax, %eax
js 0xb4ad
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x5170
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
testq %rax, %rax
je 0xb4db
movq %rax, %r13
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x58(%rax)
movl %eax, %ebp
leaq 0x15fc1(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb2fc
movq 0x15cdc(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc05e(%rip), %rsi      # 0x17344
leaq 0xdf29(%rip), %rdx      # 0x19216
movl $0x11b, %ecx            # imm = 0x11B
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
movl %ebp, 0x10(%rsp)
testl %ebp, %ebp
movl 0xc(%rsp), %edx
jle 0xb3f9
movl 0x10(%rsp), %r15d
movl %r14d, %eax
movzbl (%r13,%rax), %r12d
leaq 0x15f70(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb351
movq 0x15c8b(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc040(%rip), %rsi      # 0x17377
movl %edx, %ebp
leaq 0xded6(%rip), %rdx      # 0x19216
movl $0x11e, %ecx            # imm = 0x11E
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
movl %ebp, %edx
leal 0x1(%r14), %eax
movzbl (%r13,%rax), %eax
shll $0x8, %eax
leal 0x2(%r14), %ecx
movzbl (%r13,%rcx), %ebp
orl %eax, %ebp
movl 0x1c(%rsp), %eax
btl %r12d, %eax
jae 0xb3af
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 %ebp, %ecx
callq *0x28(%rax)
movl %r12d, %edx
testl %eax, %eax
jns 0xb3ea
jmp 0xb4ff
leaq 0x15edb(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb3ea
movq 0x15bf6(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xbfe1(%rip), %rsi      # 0x173ad
movl %edx, 0xc(%rsp)
leaq 0xde3f(%rip), %rdx      # 0x19216
movl $0x12a, %ecx            # imm = 0x12A
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
movl 0xc(%rsp), %edx
addl %ebp, %edx
addl $0x8, %r14d
decl %r15d
jne 0xb311
cmpl $0x0, 0x10(%rsp)
je 0xb4f4
leaq 0x15e86(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb434
movq 0x15ba1(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc093(%rip), %rsi      # 0x174b4
leaq 0xddee(%rip), %rdx      # 0x19216
movl $0x13b, %ecx            # imm = 0x13B
xorl %eax, %eax
callq 0x5180
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
leaq 0x14(%rsp), %r14
leaq 0xc08a(%rip), %r15      # 0x174cf
leaq 0xddca(%rip), %r12      # 0x19216
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movl $0x6, %edx
callq *0x38(%rax)
testl %eax, %eax
jns 0xb48d
leaq 0x15e28(%rip), %rcx     # 0x21291
cmpb $0x1, (%rcx)
jne 0xb48d
movq 0x15b43(%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 0xb44c
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5170
xorl %r12d, %r12d
jmp 0xb5d3
movl %eax, %r12d
leaq 0x15dda(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb535
movq 0x15b15(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xbe25(%rip), %rsi      # 0x172f2
leaq 0xdd42(%rip), %rdx      # 0x19216
movl $0x109, %ecx            # imm = 0x109
jmp 0xb52b
movl $0xfffffffc, %r12d      # imm = 0xFFFFFFFC
leaq 0x15da9(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb535
callq 0x57c3
jmp 0xb535
movl $0xfffffffb, %r12d      # imm = 0xFFFFFFFB
jmp 0xb5d3
movl %eax, %r12d
leaq 0x15d88(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb535
movq 0x15aa3(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xbeff(%rip), %rsi      # 0x1741e
leaq 0xdcf0(%rip), %rdx      # 0x19216
movl $0x132, %ecx            # imm = 0x132
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
leaq 0x15d55(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb565
movq 0x15a70(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xbf62(%rip), %rsi      # 0x174b4
leaq 0xdcbd(%rip), %rdx      # 0x19216
movl $0x147, %ecx            # imm = 0x147
xorl %eax, %eax
callq 0x5180
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
leaq 0x14(%rsp), %r14
leaq 0xc883(%rip), %r15      # 0x17df9
leaq 0xdc99(%rip), %r13      # 0x19216
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movl $0x6, %edx
callq *0x38(%rax)
testl %eax, %eax
jns 0xb5bb
leaq 0x15cf7(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xb5bb
movq 0x15a12(%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 0xb57d
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
nop