// File: /workspace/github_cpp_makefile_star2_O2/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, 0x14(%rsp)
leaq 0x8(%rsp), %rcx
andw $0x0, 0x8(%rcx)
andq $0x0, (%rcx)
movq %rdi, %r15
movl $0x1000, %ecx           # imm = 0x1000
leaq 0x7(%rsp), %r13
xorl %r12d, %r12d
movq %rsi, 0x20(%rsp)
testl %eax, %eax
je 0x14570
movq %r13, %rbp
cmpl %ecx, %eax
movl $0x1000, %ebx           # imm = 0x1000
movl %eax, 0x1c(%rsp)
cmovbl %eax, %ebx
movq 0x8(%r15), %rdi
movl %r12d, %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 0x14575
movq 0x10(%r13), %rcx
movl %ebx, %edx
xorl %esi, %esi
movl %r12d, 0x18(%rsp)
movl %r12d, %edi
xorl %eax, %eax
movq %rbp, %r13
pushq $0x1
popq %rbp
cmpq %rdx, %rsi
jae 0x1445c
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
jmp 0x14433
movb %bh, 0x8(%rsp)
movb %bl, 0x9(%rsp)
movl 0x14(%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(%r15), %rdi
movq (%rdi), %rax
pushq $0x12
popq %rsi
leaq 0x8(%rsp), %rdx
pushq $0xa
popq %rcx
callq *0x48(%rax)
movl %eax, %r14d
testl %eax, %eax
js 0x145a3
pushq $-0xb
popq %r12
incl %r12d
je 0x1454c
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5170
movq 0x8(%r15), %rdi
movq (%rdi), %rax
movl $0x5096, %esi           # imm = 0x5096
movq %r13, %rdx
movl %ebp, %ecx
callq *0x20(%rax)
movl %eax, %r14d
cmpl $0x1, %eax
jne 0x144c0
cmpb $-0x56, 0x7(%rsp)
jne 0x144c0
leaq 0xbd94(%rip), %rax      # 0x20291
cmpb $0x1, (%rax)
jne 0x1452b
movq 0xbaaf(%rip), %rax      # 0x1ffb8
movq (%rax), %rdi
leaq 0x4eb2(%rip), %rsi      # 0x193c5
leaq 0x4dfc(%rip), %rdx      # 0x19316
movl $0xe3, %ecx
movl 0x14(%rsp), %r8d
xorl %eax, %eax
callq 0x5180
movl 0x18(%rsp), %r12d
addl %ebx, %r12d
addl %ebx, 0x14(%rsp)
movl 0x1c(%rsp), %eax
subl %ebx, %eax
movq 0x20(%rsp), %rsi
movl $0x1000, %ecx           # imm = 0x1000
jmp 0x143df
pushq $-0x16
popq %rbx
leaq 0xbd3b(%rip), %rax      # 0x20291
cmpb $0x1, (%rax)
je 0x145d5
movl %ebx, %r14d
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r14d, %r14d
jmp 0x1455e
movl %eax, %r14d
leaq 0xbd12(%rip), %rax      # 0x20291
cmpb $0x1, (%rax)
jne 0x1455e
movq 0xba4d(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x4d5a(%rip), %rsi      # 0x192ef
leaq 0x4d7a(%rip), %rdx      # 0x19316
movl $0xbc, %ecx
jmp 0x145cc
leaq 0xbce7(%rip), %rax      # 0x20291
cmpb $0x1, (%rax)
jne 0x1455e
movq 0xba22(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x4d65(%rip), %rsi      # 0x19325
leaq 0x4d4f(%rip), %rdx      # 0x19316
movl $0xd2, %ecx
xorl %eax, %eax
callq 0x5180
jmp 0x1455e
movq 0xb9fc(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
movzbl 0x7(%rsp), %r9d
leaq 0x4d9e(%rip), %rsi      # 0x1938a
leaq 0x4d23(%rip), %rdx      # 0x19316
movl $0xdf, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x1455b
nop