// File: goodix[P]gdix_hid_firmware_update/gtx9/gtx9_update.cpp
// Function: GTx9Update::flashSubSystem(fw_subsys_info*)
int GTx9Update::flashSubSystem(struct fw_subsys_info *subsys)
{
	uint32_t data_size = 0;
	uint32_t offset = 0;
	uint32_t temp_addr = subsys->flash_addr;
	uint32_t total_size = subsys->size;
	uint32_t checksum;
	uint8_t cmdBuf[10] = {0};
	uint8_t flag;
	int resend_rty = 3;
	int retry;
	uint32_t i;
	int ret;

	while (total_size > 0) {
		data_size = total_size > 4096 ? 4096 : total_size;
resend:
		/* send fw data to dram */
		ret = dev->Write(0x14000, &subsys->data[offset], data_size);
		if (ret < 0) {
			gdix_err("Write fw data failed\n");
			return ret;
		}

		/* send checksum */
		for (i = 0, checksum = 0; i < data_size; i += 2) {
			checksum +=
				subsys->data[offset + i] + (subsys->data[offset + i + 1] << 8);
		}

		cmdBuf[0] = (data_size >> 8) & 0xFF;
		cmdBuf[1] = data_size & 0xFF;
		cmdBuf[2] = (temp_addr >> 24) & 0xFF;
		cmdBuf[3] = (temp_addr >> 16) & 0xFF;
		cmdBuf[4] = (temp_addr >> 8) & 0xFF;
		cmdBuf[5] = temp_addr & 0xFF;
		cmdBuf[6] = (checksum >> 24) & 0xFF;
		cmdBuf[7] = (checksum >> 16) & 0xFF;
		cmdBuf[8] = (checksum >> 8) & 0xFF;
		cmdBuf[9] = checksum & 0xFF;
		ret = dev->SendCmd(0x12, cmdBuf, 10);
		if (ret < 0) {
			gdix_err("Failed send start update cmd\n");
			return ret;
		}

		/* wait update finish */
		retry = 10;
		while (retry--) {
			usleep(20000);
			ret = dev->Read(0x10011, &flag, 1);
			if (ret == 1 && flag == 0xAA)
				break;
			else if (ret == 1 && flag == 0xBB) { //checksum error
				if (resend_rty-- > 0) {
					gdix_err("Flash data checksum error, retry:%d\n", 3 - resend_rty);
					goto resend;
				}
			}
		}
		if (retry < 0) {
			gdix_err("Failed get valid ack, ret=%d flag=0x%02x\n", ret, flag);
			return -EINVAL;
		}

		gdix_info("Flash package ok, addr:0x%06x\n", temp_addr);
		resend_rty = 3;
		offset += data_size;
		temp_addr += data_size;
		total_size -= data_size;
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x28(%rbp)
leaq -0x36(%rbp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x50a0
movl $0x3, -0x3c(%rbp)
cmpl $0x0, -0x28(%rbp)
jbe 0x182ff
cmpl $0x1000, -0x28(%rbp)    # imm = 0x1000
jbe 0x17fce
movl $0x1000, %eax           # imm = 0x1000
movl %eax, -0x54(%rbp)
jmp 0x17fd4
movl -0x28(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdx
movl -0x20(%rbp), %eax
addq %rax, %rdx
movl -0x1c(%rbp), %ecx
movq (%rdi), %rax
movl $0x14000, %esi          # imm = 0x14000
callq *0x30(%rax)
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
jge 0x18046
jmp 0x18009
leaq 0xe289(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x18039
movq 0xdfbc(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
leaq 0x830f(%rip), %rsi      # 0x20335
leaq 0x832f(%rip), %rdx      # 0x2035c
movl $0xb8, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x1803b
movl -0x48(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x18306
movl $0x0, -0x44(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x1809d
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x20(%rbp), %ecx
addl -0x44(%rbp), %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x20(%rbp), %edx
addl -0x44(%rbp), %edx
addl $0x1, %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x44(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x44(%rbp)
jmp 0x18054
movq -0x50(%rbp), %rax
movl -0x1c(%rbp), %ecx
shrl $0x8, %ecx
andl $0xff, %ecx
movb %cl, -0x36(%rbp)
movl -0x1c(%rbp), %ecx
andl $0xff, %ecx
movb %cl, -0x35(%rbp)
movl -0x24(%rbp), %ecx
shrl $0x18, %ecx
andl $0xff, %ecx
movb %cl, -0x34(%rbp)
movl -0x24(%rbp), %ecx
shrl $0x10, %ecx
andl $0xff, %ecx
movb %cl, -0x33(%rbp)
movl -0x24(%rbp), %ecx
shrl $0x8, %ecx
andl $0xff, %ecx
movb %cl, -0x32(%rbp)
movl -0x24(%rbp), %ecx
andl $0xff, %ecx
movb %cl, -0x31(%rbp)
movl -0x2c(%rbp), %ecx
shrl $0x18, %ecx
andl $0xff, %ecx
movb %cl, -0x30(%rbp)
movl -0x2c(%rbp), %ecx
shrl $0x10, %ecx
andl $0xff, %ecx
movb %cl, -0x2f(%rbp)
movl -0x2c(%rbp), %ecx
shrl $0x8, %ecx
andl $0xff, %ecx
movb %cl, -0x2e(%rbp)
movl -0x2c(%rbp), %ecx
andl $0xff, %ecx
movb %cl, -0x2d(%rbp)
movq 0x8(%rax), %rdi
leaq -0x36(%rbp), %rdx
movq (%rdi), %rax
movl $0x12, %esi
movl $0xa, %ecx
callq *0x48(%rax)
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
jge 0x1818e
jmp 0x18151
leaq 0xe141(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x18181
movq 0xde74(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
leaq 0x81fd(%rip), %rsi      # 0x2036b
leaq 0x81e7(%rip), %rdx      # 0x2035c
movl $0xce, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x18183
movl -0x48(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x18306
movl $0xa, -0x40(%rbp)
movl -0x40(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x40(%rbp)
cmpl $0x0, %eax
je 0x18252
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5180
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
movl $0x10011, %esi          # imm = 0x10011
leaq -0x37(%rbp), %rdx
movl $0x1, %ecx
callq *0x20(%rax)
movl %eax, -0x48(%rbp)
cmpl $0x1, -0x48(%rbp)
jne 0x181e5
movzbl -0x37(%rbp), %eax
cmpl $0xaa, %eax
jne 0x181e5
jmp 0x18252
cmpl $0x1, -0x48(%rbp)
jne 0x1824b
movzbl -0x37(%rbp), %eax
cmpl $0xbb, %eax
jne 0x1824b
movl -0x3c(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x3c(%rbp)
cmpl $0x0, %eax
jle 0x18249
jmp 0x18208
leaq 0xe08a(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x18242
movq 0xddbd(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
movl $0x3, %r8d
subl -0x3c(%rbp), %r8d
leaq 0x816b(%rip), %rsi      # 0x2039a
leaq 0x8126(%rip), %rdx      # 0x2035c
movl $0xdb, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x18244
jmp 0x17fda
jmp 0x1824b
jmp 0x1824d
jmp 0x18195
cmpl $0x0, -0x40(%rbp)
jge 0x1829e
jmp 0x1825a
leaq 0xe038(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x18293
movq 0xdd6b(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
movl -0x48(%rbp), %r8d
movzbl -0x37(%rbp), %r9d
leaq 0x8150(%rip), %rsi      # 0x203d0
leaq 0x80d5(%rip), %rdx      # 0x2035c
movl $0xe1, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x18295
movl $0xffffffea, -0x4(%rbp) # imm = 0xFFFFFFEA
jmp 0x18306
jmp 0x182a0
leaq 0xdff2(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x182d4
movq 0xdd05(%rip), %rax      # 0x25fb8
movq (%rax), %rdi
movl -0x24(%rbp), %r8d
leaq 0x814a(%rip), %rsi      # 0x2040b
leaq 0x8094(%rip), %rdx      # 0x2035c
movl $0xe5, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x182d6
movl $0x3, -0x3c(%rbp)
movl -0x1c(%rbp), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x1c(%rbp), %eax
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x1c(%rbp), %ecx
movl -0x28(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x28(%rbp)
jmp 0x17fb1
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop