// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x28(%rbp)
leaq -0x36(%rbp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x50a0
cmpl $0x0, -0x28(%rbp)
jbe 0x1b112
cmpl $0x1000, -0x28(%rbp)    # imm = 0x1000
jbe 0x1ae57
movl $0x1000, %eax           # imm = 0x1000
movl %eax, -0x54(%rbp)
jmp 0x1ae5d
movl -0x28(%rbp), %eax
movl %eax, -0x54(%rbp)
movq -0x50(%rbp), %rax
movl -0x54(%rbp), %ecx
movl %ecx, -0x1c(%rbp)
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdx
movl -0x20(%rbp), %eax
addq %rax, %rdx
movl -0x1c(%rbp), %ecx
movq (%rdi), %rax
movl $0x29400, %esi          # imm = 0x29400
callq *0x30(%rax)
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jge 0x1aecf
jmp 0x1ae92
leaq 0xb400(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x1aec2
movq 0xb133(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
leaq 0x5486(%rip), %rsi      # 0x20335
leaq 0x54a6(%rip), %rdx      # 0x2035c
movl $0xbc, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x1aec4
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1b119
movl $0x0, -0x40(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x1af26
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x20(%rbp), %ecx
addl -0x40(%rbp), %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x20(%rbp), %edx
addl -0x40(%rbp), %edx
addl $0x1, %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x40(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x40(%rbp)
jmp 0x1aedd
movq -0x50(%rbp), %rax
movl -0x1c(%rbp), %ecx
shrl $0x8, %ecx
andl $0xff, %ecx
movb %cl, -0x36(%rbp)
movl -0x1c(%rbp), %ecx
andl $0xff, %ecx
movb %cl, -0x35(%rbp)
movl -0x24(%rbp), %ecx
shrl $0x18, %ecx
andl $0xff, %ecx
movb %cl, -0x34(%rbp)
movl -0x24(%rbp), %ecx
shrl $0x10, %ecx
andl $0xff, %ecx
movb %cl, -0x33(%rbp)
movl -0x24(%rbp), %ecx
shrl $0x8, %ecx
andl $0xff, %ecx
movb %cl, -0x32(%rbp)
movl -0x24(%rbp), %ecx
andl $0xff, %ecx
movb %cl, -0x31(%rbp)
movl -0x2c(%rbp), %ecx
shrl $0x18, %ecx
andl $0xff, %ecx
movb %cl, -0x30(%rbp)
movl -0x2c(%rbp), %ecx
shrl $0x10, %ecx
andl $0xff, %ecx
movb %cl, -0x2f(%rbp)
movl -0x2c(%rbp), %ecx
shrl $0x8, %ecx
andl $0xff, %ecx
movb %cl, -0x2e(%rbp)
movl -0x2c(%rbp), %ecx
andl $0xff, %ecx
movb %cl, -0x2d(%rbp)
movq 0x8(%rax), %rdi
leaq -0x36(%rbp), %rdx
movq (%rdi), %rax
movl $0x12, %esi
movl $0xa, %ecx
callq *0x48(%rax)
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jge 0x1b017
jmp 0x1afda
leaq 0xb2b8(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x1b00a
movq 0xafeb(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
leaq 0x5374(%rip), %rsi      # 0x2036b
leaq 0x535e(%rip), %rdx      # 0x2035c
movl $0xd2, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x1b00c
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1b119
movl $0xa, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x3c(%rbp)
cmpl $0x0, %eax
je 0x1b06c
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5180
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
movl $0x5096, %esi           # imm = 0x5096
leaq -0x37(%rbp), %rdx
movl $0x1, %ecx
callq *0x20(%rax)
movl %eax, -0x44(%rbp)
cmpl $0x1, -0x44(%rbp)
jne 0x1b06a
movzbl -0x37(%rbp), %eax
cmpl $0xaa, %eax
jne 0x1b06a
jmp 0x1b06c
jmp 0x1b01e
cmpl $0x0, -0x3c(%rbp)
jge 0x1b0b8
jmp 0x1b074
leaq 0xb21e(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x1b0ad
movq 0xaf51(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
movl -0x44(%rbp), %r8d
movzbl -0x37(%rbp), %r9d
leaq 0x5336(%rip), %rsi      # 0x203d0
leaq 0x52bb(%rip), %rdx      # 0x2035c
movl $0xdf, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x1b0af
movl $0xffffffea, -0x4(%rbp) # imm = 0xFFFFFFEA
jmp 0x1b119
jmp 0x1b0ba
leaq 0xb1d8(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x1b0ee
movq 0xaeeb(%rip), %rax      # 0x25fb8
movq (%rax), %rdi
movl -0x24(%rbp), %r8d
leaq 0x5330(%rip), %rsi      # 0x2040b
leaq 0x527a(%rip), %rdx      # 0x2035c
movl $0xe3, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x1b0f0
movl -0x1c(%rbp), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x1c(%rbp), %eax
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x1c(%rbp), %ecx
movl -0x28(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x28(%rbp)
jmp 0x1ae3a
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)