// File: /workspace/github_cpp_makefile_star2_O3/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), %ebx
movl 0x8(%rsi), %eax
movl %eax, 0x1c(%rsp)
movw $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
testl %ebx, %ebx
je 0x133ec
movq %rsi, %r12
movq %rdi, %r14
movl $0x1000, %eax           # imm = 0x1000
xorl %ecx, %ecx
movq %rsi, 0x38(%rsp)
cmpl %eax, %ebx
movl $0x1000, %r13d          # imm = 0x1000
cmovbl %ebx, %r13d
movl %ecx, 0x20(%rsp)
movl %ecx, %eax
movq 0x8(%r14), %rdi
movq 0x10(%r12), %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 0x13415
movl %ebx, 0x30(%rsp)
movl %r13d, %r9d
shrl $0x8, %r9d
movl 0x1c(%rsp), %eax
movl %eax, %r10d
shrl $0x18, %r10d
movl %eax, %r11d
shrl $0x10, %r11d
shrl $0x8, %eax
movl %eax, 0x34(%rsp)
movl %r13d, %ebx
movl $0x3, %ebp
movl %r9d, 0x2c(%rsp)
movl %r10d, 0x28(%rsp)
movl %r11d, 0x24(%rsp)
movq 0x10(%r12), %rcx
xorl %edx, %edx
movl 0x20(%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 %rbx, %rdx
jb 0x13249
movb %r9b, 0x10(%rsp)
movb %r13b, 0x11(%rsp)
movb %r10b, 0x12(%rsp)
movb %r11b, 0x13(%rsp)
movl 0x34(%rsp), %ecx
movb %cl, 0x14(%rsp)
movl 0x1c(%rsp), %ecx
movb %cl, 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(%r14), %rdi
movq (%rdi), %rax
movl $0x12, %esi
leaq 0x10(%rsp), %rdx
movl $0xa, %ecx
callq *0x48(%rax)
testl %eax, %eax
js 0x1342b
movl $0xfffffff6, %r12d      # imm = 0xFFFFFFF6
movl %ebp, %r15d
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5170
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movl $0x10011, %esi          # imm = 0x10011
leaq 0xf(%rsp), %rdx
movl $0x1, %ecx
callq *0x20(%rax)
cmpl $0x1, %eax
sete %dl
movb 0xf(%rsp), %cl
cmpb $-0x56, %cl
sete %sil
testb %sil, %dl
jne 0x13394
movzbl %cl, %r9d
cmpl $0x1, %eax
sete %cl
cmpb $-0x45, %r9b
sete %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x13337
leal -0x1(%r15), %ebp
testl %r15d, %r15d
jg 0x13341
movl %ebp, %r15d
incl %r12d
jne 0x132d6
jmp 0x133f1
leaq 0xdf49(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
je 0x1338a
movq 0x8(%r14), %rdi
movq 0x38(%rsp), %r12
movq 0x10(%r12), %rdx
addq 0x40(%rsp), %rdx
movq (%rdi), %rax
movl $0x14000, %esi          # imm = 0x14000
movl %r13d, %ecx
callq *0x30(%rax)
testl %eax, %eax
movl 0x2c(%rsp), %r9d
movl 0x28(%rsp), %r10d
movl 0x24(%rsp), %r11d
jns 0x1323a
jmp 0x13415
movl %r15d, %edi
callq 0x66da
jmp 0x1334d
leaq 0xdef6(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
jne 0x133c9
movq 0xdc11(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x7014(%rip), %rsi      # 0x1a3c5
leaq 0x6f5e(%rip), %rdx      # 0x1a316
movl $0xe5, %ecx
movl 0x1c(%rsp), %r8d
xorl %eax, %eax
callq 0x5180
movl 0x20(%rsp), %ecx
addl %r13d, %ecx
addl %r13d, 0x1c(%rsp)
movl 0x30(%rsp), %ebx
subl %r13d, %ebx
movq 0x38(%rsp), %r12
movl $0x1000, %eax           # imm = 0x1000
jne 0x131c6
xorl %r15d, %r15d
jmp 0x13403
movl $0xffffffea, %r15d      # imm = 0xFFFFFFEA
leaq 0xde93(%rip), %rcx      # 0x21291
cmpb $0x1, (%rcx)
je 0x13441
movl %r15d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %r15d
leaq 0xde72(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
jne 0x13403
callq 0x6738
jmp 0x13403
movl %eax, %r15d
leaq 0xde5c(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
jne 0x13403
callq 0x670e
jmp 0x13403
movq 0xdb90(%rip), %rcx      # 0x20fd8
movq (%rcx), %rdi
leaq 0x6f38(%rip), %rsi      # 0x1a38a
leaq 0x6ebd(%rip), %rdx      # 0x1a316
movl $0xe1, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x13403