// File: /workspace/github_cpp_makefile_star2_O1/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
movl 0x4(%rsi), %r15d
movl 0x8(%rsi), %r12d
movw $0x0, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
testl %r15d, %r15d
je 0x13604
movq %rsi, %rbp
movq %rdi, %r14
xorl %ecx, %ecx
movq %rsi, 0x20(%rsp)
movl $0x1000, %eax           # imm = 0x1000
cmpl %eax, %r15d
movl $0x1000, %r13d          # imm = 0x1000
cmovbl %r15d, %r13d
movl %ecx, 0x1c(%rsp)
movl %ecx, %eax
movq 0x8(%r14), %rdi
movq 0x10(%rbp), %rdx
movq %rax, 0x40(%rsp)
addq %rax, %rdx
movq (%rdi), %rax
movl $0x14000, %esi          # imm = 0x14000
movl %r13d, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0x13608
movl %r15d, 0x38(%rsp)
movl %r13d, %r9d
shrl $0x8, %r9d
movl %r12d, %r10d
shrl $0x18, %r10d
movl %r12d, %r11d
shrl $0x10, %r11d
movl %r12d, 0x18(%rsp)
shrl $0x8, %r12d
movl %r12d, 0x3c(%rsp)
movl %r13d, %r12d
movl $0x3, %r15d
movl %r9d, 0x34(%rsp)
movl %r10d, 0x30(%rsp)
movl %r11d, 0x2c(%rsp)
movq 0x10(%rbp), %rcx
xorl %edx, %edx
movl 0x1c(%rsp), %eax
movl %eax, %esi
xorl %eax, %eax
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
cmpq %r12, %rdx
jb 0x13412
movb %r9b, 0x8(%rsp)
movb %r13b, 0x9(%rsp)
movb %r10b, 0xa(%rsp)
movb %r11b, 0xb(%rsp)
movl 0x3c(%rsp), %ecx
movb %cl, 0xc(%rsp)
movl 0x18(%rsp), %ecx
movb %cl, 0xd(%rsp)
movl %eax, %ecx
shrl $0x18, %ecx
movb %cl, 0xe(%rsp)
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, 0xf(%rsp)
movb %ah, 0x10(%rsp)
movb %al, 0x11(%rsp)
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movl $0x12, %esi
leaq 0x8(%rsp), %rdx
movl $0xa, %ecx
callq *0x48(%rax)
testl %eax, %eax
js 0x1361d
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5170
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movl $0x1, %ebx
movl $0x10011, %esi          # imm = 0x10011
leaq 0x7(%rsp), %rdx
movl $0x1, %ecx
callq *0x20(%rax)
cmpl $0x1, %eax
sete %cl
movb 0x7(%rsp), %dl
cmpb $-0x56, %dl
sete %sil
testb %sil, %cl
jne 0x13600
movl $0xa, %ebp
movl %r15d, %ebx
cmpb $-0x45, %dl
sete %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x134f8
leal -0x1(%rbx), %r15d
testl %ebx, %ebx
jg 0x1353e
movl %r15d, %ebx
cmpl $0x1, %ebp
je 0x1358f
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5170
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movl $0x10011, %esi          # imm = 0x10011
leaq 0x7(%rsp), %rdx
movl $0x1, %ecx
callq *0x20(%rax)
cmpl $0x1, %eax
sete %cl
movb 0x7(%rsp), %dl
cmpb $-0x56, %dl
sete %sil
decl %ebp
testb %sil, %cl
je 0x134e0
jmp 0x1359a
leaq 0xdd4c(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
je 0x13586
movq 0x8(%r14), %rdi
movq 0x20(%rsp), %rbp
movq 0x10(%rbp), %rdx
addq 0x40(%rsp), %rdx
movq (%rdi), %rax
movl $0x14000, %esi          # imm = 0x14000
movl %r13d, %ecx
callq *0x30(%rax)
testl %eax, %eax
movl 0x34(%rsp), %r9d
movl 0x30(%rsp), %r10d
movl 0x2c(%rsp), %r11d
jns 0x13404
jmp 0x13608
movl %ebx, %edi
callq 0x672e
jmp 0x1354a
movb $0x1, %cl
movl %eax, %ebx
movq 0x20(%rsp), %rbp
jmp 0x135a9
testl %ebp, %ebp
setle %cl
movq 0x20(%rsp), %rbp
movl $0x1, %ebx
leaq 0xdce1(%rip), %rax      # 0x21291
movb (%rax), %al
testb %cl, %cl
movl 0x38(%rsp), %r15d
movl 0x18(%rsp), %r12d
jne 0x13632
testb %al, %al
je 0x135eb
movq 0xd9ed(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x6e30(%rip), %rsi      # 0x1a405
leaq 0x6d7a(%rip), %rdx      # 0x1a356
movl $0xe5, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
movl 0x1c(%rsp), %ecx
addl %r13d, %ecx
addl %r13d, %r12d
subl %r13d, %r15d
jne 0x13386
jmp 0x13604
xorl %ecx, %ecx
jmp 0x135a9
xorl %ebx, %ebx
jmp 0x1363e
movl %eax, %ebx
leaq 0xdc80(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
jne 0x1363e
callq 0x678c
jmp 0x1363e
movl %eax, %ebx
leaq 0xdc6b(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
jne 0x1363e
callq 0x6762
jmp 0x1363e
movl %ebx, %r8d
movl $0xffffffea, %ebx       # imm = 0xFFFFFFEA
testb %al, %al
jne 0x1364f
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xd982(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
movzbl 0x7(%rsp), %r9d
leaq 0x6d64(%rip), %rsi      # 0x1a3ca
leaq 0x6ce9(%rip), %rdx      # 0x1a356
movl $0xe1, %ecx
xorl %eax, %eax
callq 0x5180
jmp 0x1363e
nop