// File: /workspace/github_cpp_makefile_star2_O1/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), %eax
movl 0x8(%rsi), %ecx
movl %ecx, 0x18(%rsp)
movw $0x0, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
testl %eax, %eax
je 0x1537c
movq %rsi, %r15
movq %rdi, %r14
leaq 0x7(%rsp), %r12
xorl %r13d, %r13d
movq %rsi, 0x20(%rsp)
movl $0x1000, %ecx           # imm = 0x1000
cmpl %ecx, %eax
movl $0x1000, %ebx           # imm = 0x1000
movl %eax, 0x1c(%rsp)
cmovbl %eax, %ebx
movq 0x8(%r14), %rdi
movl %r13d, %edx
addq 0x10(%r15), %rdx
movq (%rdi), %rax
movl $0x29400, %esi          # imm = 0x29400
movl %ebx, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0x15380
movq 0x10(%r15), %rcx
movl %ebx, %edx
xorl %esi, %esi
movl %r13d, %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 0x15261
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 0x15395
movl $0xa, %ebp
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5170
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movl $0x1, %r15d
movl $0x5096, %esi           # imm = 0x5096
movq %r12, %rdx
movl $0x1, %ecx
callq *0x20(%rax)
cmpl $0x1, %eax
jne 0x15321
cmpb $-0x56, 0x7(%rsp)
je 0x1532a
decl %ebp
jne 0x152ee
xorl %ebp, %ebp
movl %eax, %r15d
leaq 0xbf60(%rip), %rax      # 0x21291
movb (%rax), %al
testl %ebp, %ebp
jle 0x153aa
testb %al, %al
je 0x15364
movq 0xbc76(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x50b9(%rip), %rsi      # 0x1a405
leaq 0x5003(%rip), %rdx      # 0x1a356
movl $0xe3, %ecx
movl 0x18(%rsp), %r8d
xorl %eax, %eax
callq 0x5180
addl %ebx, %r13d
addl %ebx, 0x18(%rsp)
movl 0x1c(%rsp), %eax
subl %ebx, %eax
movq 0x20(%rsp), %r15
jne 0x15221
xorl %ebp, %ebp
jmp 0x153b3
movl %eax, %ebp
leaq 0xbf08(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
jne 0x153b3
callq 0x6c35
jmp 0x153b3
movl %eax, %ebp
leaq 0xbef3(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
jne 0x153b3
callq 0x6c0b
jmp 0x153b3
movl $0xffffffea, %ebp       # imm = 0xFFFFFFEA
testb %al, %al
jne 0x153c4
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xbc0d(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
movzbl 0x7(%rsp), %r9d
leaq 0x4fef(%rip), %rsi      # 0x1a3ca
leaq 0x4f74(%rip), %rdx      # 0x1a356
movl $0xdf, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x153b3
nop