// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbp
movl 0x4(%rsi), %eax
movl 0x8(%rsi), %ebx
leaq 0x10(%rsp), %rcx
andw $0x0, 0x8(%rcx)
andq $0x0, (%rcx)
movq %rdi, %r15
movl $0x1000, %ecx           # imm = 0x1000
movl $0x0, 0x20(%rsp)
movq %rsi, 0x40(%rsp)
testl %eax, %eax
je 0x124f9
cmpl %ecx, %eax
movl $0x1000, %r14d          # imm = 0x1000
movl %eax, 0x2c(%rsp)
cmovbl %eax, %r14d
movl 0x20(%rsp), %ecx
movl %r14d, %eax
shrl $0x8, %eax
movl %eax, 0x3c(%rsp)
movl %ebx, %eax
shrl $0x18, %eax
movl %eax, 0x38(%rsp)
movl %ebx, %eax
shrl $0x10, %eax
movl %eax, 0x34(%rsp)
movl %ebx, 0x28(%rsp)
shrl $0x8, %ebx
movl %ebx, 0x30(%rsp)
pushq $0x3
popq %rax
movl %eax, 0x24(%rsp)
movq 0x8(%r15), %rdi
movq 0x10(%rbp), %rdx
movq %rcx, %r12
addq %rcx, %rdx
movq (%rdi), %rax
movl $0x14000, %esi          # imm = 0x14000
movl %r14d, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0x124fd
movq 0x10(%rbp), %rcx
xorl %edx, %edx
movl 0x20(%rsp), %eax
movl %eax, %esi
xorl %eax, %eax
movl 0x28(%rsp), %r9d
pushq $0xa
popq %rbx
cmpq %r14, %rdx
jae 0x12367
movl %esi, %edi
movzbl (%rcx,%rdi), %edi
leal 0x1(%rsi), %r8d
movzbl (%rcx,%r8), %r8d
shll $0x8, %r8d
orl %edi, %r8d
addl %r8d, %eax
addq $0x2, %rdx
addl $0x2, %esi
jmp 0x12340
movl 0x3c(%rsp), %ecx
movb %cl, 0x10(%rsp)
movb %r14b, 0x11(%rsp)
movl 0x38(%rsp), %ecx
movb %cl, 0x12(%rsp)
movl 0x34(%rsp), %ecx
movb %cl, 0x13(%rsp)
movl 0x30(%rsp), %ecx
movb %cl, 0x14(%rsp)
movb %r9b, 0x15(%rsp)
movl %eax, %ecx
shrl $0x18, %ecx
movb %cl, 0x16(%rsp)
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, 0x17(%rsp)
movb %ah, 0x18(%rsp)
movb %al, 0x19(%rsp)
movq 0x8(%r15), %rdi
movq (%rdi), %rax
pushq $0x12
popq %rsi
leaq 0x10(%rsp), %rdx
movl %ebx, %ecx
callq *0x48(%rax)
movl %ebx, %ecx
movl %eax, %ebx
testl %eax, %eax
js 0x1252a
movl %ecx, %ebp
movl 0x24(%rsp), %eax
movl %eax, %r13d
negl %ebp
incl %ebp
cmpl $0x1, %ebp
je 0x124d7
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5170
movq 0x8(%r15), %rdi
movq (%rdi), %rax
movl $0x10011, %esi          # imm = 0x10011
leaq 0xf(%rsp), %rdx
pushq $0x1
popq %rcx
callq *0x20(%rax)
movl %eax, %ebx
cmpl $0x1, %eax
sete %cl
movb 0xf(%rsp), %al
cmpb $-0x56, %al
sete %dl
testb %dl, %cl
jne 0x12482
cmpl $0x1, %ebx
sete %cl
cmpb $-0x45, %al
sete %al
andb %cl, %al
cmpb $0x1, %al
jne 0x123d6
leal -0x1(%r13), %eax
negl %ebp
testl %r13d, %r13d
jle 0x123d1
movq %r12, %rcx
movl %eax, 0x24(%rsp)
leaq 0xde50(%rip), %rax      # 0x20291
cmpb $0x1, (%rax)
movq 0x40(%rsp), %rbp
jne 0x12306
movq 0xdb82(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
pushq $0x4
popq %r8
subl %r13d, %r8d
leaq 0x6eed(%rip), %rsi      # 0x19354
leaq 0x6ea8(%rip), %rdx      # 0x19316
movl $0xdb, %ecx
xorl %eax, %eax
callq 0x5180
movq %r12, %rcx
jmp 0x12306
leaq 0xde08(%rip), %rax      # 0x20291
cmpb $0x1, (%rax)
movl 0x28(%rsp), %ebx
jne 0x124b9
movq 0xdb1f(%rip), %rax      # 0x1ffb8
movq (%rax), %rdi
leaq 0x6f22(%rip), %rsi      # 0x193c5
leaq 0x6e6c(%rip), %rdx      # 0x19316
movl $0xe5, %ecx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x5180
addl %r14d, 0x20(%rsp)
addl %r14d, %ebx
movl 0x2c(%rsp), %eax
subl %r14d, %eax
movq 0x40(%rsp), %rbp
movl $0x1000, %ecx           # imm = 0x1000
jmp 0x122bc
pushq $-0x16
popq %rbp
leaq 0xddb0(%rip), %rax      # 0x20291
cmpb $0x1, (%rax)
je 0x1255c
movl %ebp, %ebx
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebx, %ebx
jmp 0x124e8
movl %eax, %ebx
leaq 0xdd8b(%rip), %rax      # 0x20291
cmpb $0x1, (%rax)
jne 0x124e8
movq 0xdac6(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x6dd3(%rip), %rsi      # 0x192ef
leaq 0x6df3(%rip), %rdx      # 0x19316
movl $0xb8, %ecx
jmp 0x12553
leaq 0xdd60(%rip), %rax      # 0x20291
cmpb $0x1, (%rax)
jne 0x124e8
movq 0xda9b(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x6dde(%rip), %rsi      # 0x19325
leaq 0x6dc8(%rip), %rdx      # 0x19316
movl $0xce, %ecx
xorl %eax, %eax
callq 0x5180
jmp 0x124e8
movq 0xda75(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
movzbl 0xf(%rsp), %r9d
leaq 0x6e17(%rip), %rsi      # 0x1938a
leaq 0x6d9c(%rip), %rdx      # 0x19316
movl $0xe1, %ecx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x124e6