// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq $0x0, -0x78(%rbp)
movl 0x12464(%rip), %ecx     # 0x1df4f
movl %ecx, -0x7e(%rbp)
movw 0x1245e(%rip), %cx      # 0x1df53
movw %cx, -0x7a(%rbp)
movl 0x12456(%rip), %ecx     # 0x1df55
movl %ecx, -0x84(%rbp)
movw 0x1244d(%rip), %cx      # 0x1df59
movw %cx, -0x80(%rbp)
movl 0x12445(%rip), %ecx     # 0x1df5b
movl %ecx, -0x8a(%rbp)
movw 0x1243c(%rip), %cx      # 0x1df5f
movw %cx, -0x86(%rbp)
movl $0x0, -0x90(%rbp)
movq 0x10(%rax), %rdi
movq (%rdi), %rax
callq *0x60(%rax)
movl %eax, %ecx
movq -0xb0(%rbp), %rax
movl %ecx, -0xa0(%rbp)
movq 0x10(%rax), %rdi
movq (%rdi), %rax
callq *0x68(%rax)
movl %eax, %ecx
movq -0xb0(%rbp), %rax
movl %ecx, -0xa4(%rbp)
movq 0x8(%rax), %rdi
leaq -0x7e(%rbp), %rsi
movq (%rdi), %rax
movl $0x6, %edx
callq *0x38(%rax)
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0xbbc1
jmp 0xbb84
leaq 0x1a70e(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xbbb4
movq 0x1a441(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0x1164e(%rip), %rsi     # 0x1d1ef
leaq 0x13654(%rip), %rdx     # 0x1f1fc
movl $0xec, %ecx
movb $0x0, %al
callq 0x5190
jmp 0xbbb6
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xc1c0
movl $0x3d090, %edi          # imm = 0x3D090
callq 0x5180
movl $0x6, -0x18(%rbp)
movq -0xb0(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x70(%rbp), %rdx
movq (%rdi), %rax
movl $0x5095, %esi           # imm = 0x5095
movl $0x1, %ecx
callq *0x20(%rax)
movl %eax, -0x1c(%rbp)
leaq 0x1a69e(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xbc24
movq 0x1a3b1(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x121cc(%rip), %rsi     # 0x1dddd
leaq 0x135e4(%rip), %rdx     # 0x1f1fc
movl $0xf4, %ecx
movb $0x0, %al
callq 0x5190
jmp 0xbc26
cmpl $0x0, -0x1c(%rbp)
jge 0xbc6f
jmp 0xbc2e
leaq 0x1a664(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xbc68
movq 0x1a397(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movl -0x1c(%rbp), %r9d
leaq 0x11614(%rip), %rsi     # 0x1d263
leaq 0x135a6(%rip), %rdx     # 0x1f1fc
movl $0xf6, %ecx
movl $0x5095, %r8d           # imm = 0x5095
movb $0x0, %al
callq 0x5190
jmp 0xbc6a
jmp 0xc105
movzbl -0x70(%rbp), %eax
cmpl $0xdd, %eax
jne 0xbc7c
jmp 0xbcd7
jmp 0xbc7e
leaq 0x1a614(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xbcb9
movq 0x1a327(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movzbl -0x70(%rbp), %r9d
leaq 0x115f0(%rip), %rsi     # 0x1d290
leaq 0x13555(%rip), %rdx     # 0x1f1fc
movl $0xfc, %ecx
movl $0x5095, %r8d           # imm = 0x5095
movb $0x0, %al
callq 0x5190
jmp 0xbcbb
movl $0x7530, %edi           # imm = 0x7530
callq 0x5180
movl -0x18(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, %eax
jne 0xbbd2
cmpl $0x0, -0x18(%rbp)
jne 0xbd23
jmp 0xbcdf
leaq 0x1a5b3(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xbd15
movq 0x1a2e6(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0x115c8(%rip), %rsi     # 0x1d2c4
leaq 0x134f9(%rip), %rdx     # 0x1f1fc
movl $0x101, %ecx            # imm = 0x101
movl $0x5095, %r8d           # imm = 0x5095
movb $0x0, %al
callq 0x5190
jmp 0xbd17
movl $0xfffffffe, -0x1c(%rbp) # imm = 0xFFFFFFFE
jmp 0xc105
movq -0xb0(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x84(%rbp), %rsi
movq (%rdi), %rax
movl $0x6, %edx
callq *0x38(%rax)
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0xbd86
jmp 0xbd4b
leaq 0x1a547(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xbd7f
movq 0x1a27a(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movl -0x1c(%rbp), %r8d
leaq 0x1157b(%rip), %rsi     # 0x1d2e7
leaq 0x13489(%rip), %rdx     # 0x1f1fc
movl $0x109, %ecx            # imm = 0x109
movb $0x0, %al
callq 0x5190
jmp 0xbd81
jmp 0xc105
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x5180
movq -0xb0(%rbp), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0xbdec
jmp 0xbdae
leaq 0x1a4e4(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xbdde
movq 0x1a217(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0x1154a(%rip), %rsi     # 0x1d315
leaq 0x1342a(%rip), %rdx     # 0x1f1fc
movl $0x111, %ecx            # imm = 0x111
movb $0x0, %al
callq 0x5190
jmp 0xbde0
movl $0xfffffffc, -0x1c(%rbp) # imm = 0xFFFFFFFC
jmp 0xc105
movq -0xb0(%rbp), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
callq *0x58(%rax)
movl %eax, -0x90(%rbp)
leaq 0x1a48f(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xbe3a
movq 0x1a1a2(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movl -0x90(%rbp), %r8d
leaq 0x11512(%rip), %rsi     # 0x1d339
leaq 0x133ce(%rip), %rdx     # 0x1f1fc
movl $0x11b, %ecx            # imm = 0x11B
movb $0x0, %al
callq 0x5190
jmp 0xbe3c
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x90(%rbp), %eax
jge 0xc027
movq -0x78(%rbp), %rax
movl -0xa0(%rbp), %ecx
movb (%rax,%rcx), %al
movb %al, -0x91(%rbp)
leaq 0x1a42d(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xbe9d
movq 0x1a140(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movzbl -0x91(%rbp), %r8d
leaq 0x114e2(%rip), %rsi     # 0x1d36c
leaq 0x1336b(%rip), %rdx     # 0x1f1fc
movl $0x11e, %ecx            # imm = 0x11E
movb $0x0, %al
callq 0x5190
jmp 0xbe9f
movq -0x78(%rbp), %rax
movl -0xa0(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
shll $0x8, %eax
movq -0x78(%rbp), %rcx
movl -0xa0(%rbp), %edx
addl $0x2, %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
orl %ecx, %eax
movl %eax, -0x98(%rbp)
movq -0x78(%rbp), %rax
movl -0xa0(%rbp), %ecx
addl $0x3, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
shll $0x8, %eax
movq -0x78(%rbp), %rcx
movl -0xa0(%rbp), %edx
addl $0x4, %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
orl %ecx, %eax
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x9c(%rbp)
movl -0x14(%rbp), %eax
movzbl -0x91(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
jne 0xbf8c
jmp 0xbf2c
leaq 0x1a366(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xbf64
movq 0x1a079(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movzbl -0x91(%rbp), %r8d
leaq 0x11451(%rip), %rsi     # 0x1d3a2
leaq 0x132a4(%rip), %rdx     # 0x1f1fc
movl $0x12a, %ecx            # imm = 0x12A
movb $0x0, %al
callq 0x5190
jmp 0xbf66
movl -0x98(%rbp), %eax
addl -0xa4(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl -0xa0(%rbp), %eax
addl $0x8, %eax
movl %eax, -0xa0(%rbp)
jmp 0xc019
movq -0xb0(%rbp), %rdi
movl -0x9c(%rbp), %esi
movq -0x78(%rbp), %rdx
movl -0xa4(%rbp), %eax
addq %rax, %rdx
movl -0x98(%rbp), %ecx
movq (%rdi), %rax
callq *0x28(%rax)
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0xbff8
jmp 0xbfbd
leaq 0x1a2d5(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xbff1
movq 0x19fe8(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movl -0x1c(%rbp), %r8d
leaq 0x11435(%rip), %rsi     # 0x1d413
leaq 0x13217(%rip), %rdx     # 0x1f1fc
movl $0x132, %ecx            # imm = 0x132
movb $0x0, %al
callq 0x5190
jmp 0xbff3
jmp 0xc105
movl -0x98(%rbp), %eax
addl -0xa4(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl -0xa0(%rbp), %eax
addl $0x8, %eax
movl %eax, -0xa0(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xbe43
cmpl $0x0, -0x90(%rbp)
jne 0xc03c
movl $0xfffffffb, -0x4(%rbp) # imm = 0xFFFFFFFB
jmp 0xc1c0
jmp 0xc03e
leaq 0x1a254(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xc06e
movq 0x19f67(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x1144e(%rip), %rsi     # 0x1d4a9
leaq 0x1319a(%rip), %rdx     # 0x1f1fc
movl $0x13b, %ecx            # imm = 0x13B
movb $0x0, %al
callq 0x5190
jmp 0xc070
movl $0x3, -0x18(%rbp)
movq -0xb0(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x8a(%rbp), %rsi
movq (%rdi), %rax
movl $0x6, %edx
callq *0x38(%rax)
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0xc0d7
jmp 0xc09f
leaq 0x1a1f3(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xc0d3
movq 0x19f06(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movl -0x1c(%rbp), %r8d
leaq 0x11404(%rip), %rsi     # 0x1d4c4
leaq 0x13135(%rip), %rdx     # 0x1f1fc
movl $0x140, %ecx            # imm = 0x140
movb $0x0, %al
callq 0x5190
jmp 0xc0d5
jmp 0xc0d7
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5180
movl -0x18(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, %eax
jne 0xc077
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5180
movl $0x0, -0x4(%rbp)
jmp 0xc1c0
jmp 0xc107
leaq 0x1a18b(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xc137
movq 0x19e9e(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x11385(%rip), %rsi     # 0x1d4a9
leaq 0x130d1(%rip), %rdx     # 0x1f1fc
movl $0x147, %ecx            # imm = 0x147
movb $0x0, %al
callq 0x5190
jmp 0xc139
movl $0x3, -0x18(%rbp)
movq -0xb0(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x8a(%rbp), %rsi
movq (%rdi), %rax
movl $0x6, %edx
callq *0x38(%rax)
cmpl $0x0, %eax
jge 0xc198
jmp 0xc164
leaq 0x1a12e(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xc194
movq 0x19e41(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
leaq 0x11c7c(%rip), %rsi     # 0x1ddfd
leaq 0x13074(%rip), %rdx     # 0x1f1fc
movl $0x14b, %ecx            # imm = 0x14B
movb $0x0, %al
callq 0x5190
jmp 0xc196
jmp 0xc198
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5180
movl -0x18(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, %eax
jne 0xc140
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5180
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax)