// File: /workspace/github_cpp_makefile_star2_O3/goodix[P]gdix_hid_firmware_update/berlin_a/brla_update.cpp
// Function: BrlAUpdate::flashSubSystem(fw_subsys_info_a*)
int BrlAUpdate::flashSubSystem(struct fw_subsys_info_a *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 retry;
	uint32_t i;
	int ret;

	while (total_size > 0) {
		data_size = total_size > 4096 ? 4096 : total_size;

		/* send fw data to dram */
		ret = dev->Write(ISP_RAM_ADDR, &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(0x5096, &flag, 1);
			if (ret == 1 && flag == 0xAA)
				break;
		}
		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);

		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 $0x28, %rsp
movl 0x4(%rsi), %ecx
movl 0x8(%rsi), %eax
movl %eax, 0x18(%rsp)
movw $0x0, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
testl %ecx, %ecx
je 0x15197
movq %rdi, %r14
movl $0x1000, %eax           # imm = 0x1000
leaq 0x7(%rsp), %r12
xorl %r15d, %r15d
movq %rsi, 0x20(%rsp)
cmpl %eax, %ecx
movl $0x1000, %ebx           # imm = 0x1000
movl %ecx, 0x1c(%rsp)
cmovbl %ecx, %ebx
movq 0x8(%r14), %rdi
movl %r15d, %edx
addq 0x10(%rsi), %rdx
movq (%rdi), %rax
movq %rsi, %r13
movl $0x29400, %esi          # imm = 0x29400
movl %ebx, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0x151bd
movq 0x10(%r13), %rcx
movl %ebx, %edx
xorl %esi, %esi
movl %r15d, %edi
xorl %eax, %eax
movl %edi, %r8d
movzbl (%rcx,%r8), %r8d
leal 0x1(%rdi), %r9d
movzbl (%rcx,%r9), %r9d
shll $0x8, %r9d
orl %r8d, %r9d
addl %r9d, %eax
addq $0x2, %rsi
addl $0x2, %edi
cmpq %rdx, %rsi
jb 0x1507b
movb %bh, 0x8(%rsp)
movb %bl, 0x9(%rsp)
movl 0x18(%rsp), %edx
movl %edx, %ecx
shrl $0x18, %ecx
movb %cl, 0xa(%rsp)
movl %edx, %ecx
shrl $0x10, %ecx
movb %cl, 0xb(%rsp)
movb %dh, 0xc(%rsp)
movb %dl, 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 0x151d2
movl $0xfffffff6, %ebp       # imm = 0xFFFFFFF6
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5170
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movl $0x5096, %esi           # imm = 0x5096
movq %r12, %rdx
movl $0x1, %ecx
callq *0x20(%rax)
cmpl $0x1, %eax
sete %dl
movb 0x7(%rsp), %cl
cmpb $-0x56, %cl
sete %sil
testb %sil, %dl
jne 0x15145
incl %ebp
jne 0x15108
jmp 0x1519b
leaq 0xc145(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
jne 0x1517a
movq 0xbe60(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x5263(%rip), %rsi      # 0x1a3c5
leaq 0x51ad(%rip), %rdx      # 0x1a316
movl $0xe3, %ecx
movl 0x18(%rsp), %r8d
xorl %eax, %eax
callq 0x5180
addl %ebx, %r15d
addl %ebx, 0x18(%rsp)
movl 0x1c(%rsp), %ecx
subl %ebx, %ecx
movq 0x20(%rsp), %rsi
movl $0x1000, %eax           # imm = 0x1000
jne 0x1503d
xorl %ebp, %ebp
jmp 0x151ac
movl $0xffffffea, %ebp       # imm = 0xFFFFFFEA
leaq 0xc0ea(%rip), %rdx      # 0x21291
cmpb $0x1, (%rdx)
je 0x151e7
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %ebp
leaq 0xc0cb(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
jne 0x151ac
callq 0x6bb7
jmp 0x151ac
movl %eax, %ebp
leaq 0xc0b6(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
jne 0x151ac
callq 0x6b8d
jmp 0x151ac
movzbl %cl, %r9d
movq 0xbde6(%rip), %rcx      # 0x20fd8
movq (%rcx), %rdi
leaq 0x518e(%rip), %rsi      # 0x1a38a
leaq 0x5113(%rip), %rdx      # 0x1a316
movl $0xdf, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x151ac