// File: /workspace/github_cpp_makefile_star2_O3/goodix[P]gdix_hid_firmware_update/gtx9/gtx9_i2c_update.cpp
// Function: gdix_flash_subsystem(fw_subsys_info*)
static int gdix_flash_subsystem(struct fw_subsys_info *subsys)
{
	uint32_t data_size, offset;
	uint32_t total_size;
	// TODO: confirm flash addr ,<< 8??
	uint32_t subsys_base_addr = subsys->flash_addr;
	uint8_t *fw_packet = NULL;
	int r = 0;

	/*
	 * if bus(i2c/spi) error occued, then exit, we will do
	 * hardware reset and re-prepare ISP and then retry
	 * flashing
	 */
	total_size = subsys->size;
	fw_packet = (uint8_t *)malloc(ISP_MAX_BUFFERSIZE + 4);
	if (!fw_packet) {
		gdix_dbg("Failed alloc memory\n");
		return -1;
	}

	offset = 0;
	while (total_size > 0) {
		data_size =
			total_size > ISP_MAX_BUFFERSIZE ? ISP_MAX_BUFFERSIZE : total_size;
		gdix_dbg("Flash firmware to %08x,size:%u bytes\n",
				 subsys_base_addr + offset, data_size);

		memcpy(fw_packet, &subsys->data[offset], data_size);
		/* set checksum for package data */
		gdix_append_checksum(fw_packet, data_size, CHECKSUM_MODE_U16_LE);

		r = gdix_flash_package(subsys->type, fw_packet,
							   subsys_base_addr + offset, data_size + 4);
		if (r) {
			gdix_err("failed flash to %08x,size:%u bytes\n",
					 subsys_base_addr + offset, data_size);
			break;
		}
		offset += data_size;
		total_size -= data_size;
	} /* end while */

	free(fw_packet);
	return r;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl 0x4(%rdi), %ebp
movq %rdi, 0x60(%rsp)
movl 0x8(%rdi), %r14d
movl $0x1004, %edi           # imm = 0x1004
callq 0x5140
testq %rax, %rax
je 0x12b55
movq %rax, %rbx
testl %ebp, %ebp
je 0x12b51
leaq 0xec43(%rip), %r15      # 0x21291
movb (%r15), %al
leaq 0x40(%rsp), %r12
xorl %r13d, %r13d
movq %r14, %rdx
movq %r14, 0x50(%rsp)
movq %rbx, 0x18(%rsp)
movl $0x1000, %ecx           # imm = 0x1000
cmpl %ecx, %ebp
movl $0x1000, %r14d          # imm = 0x1000
cmovbl %ebp, %r14d
testb $0x1, %al
movl %ebp, 0x24(%rsp)
je 0x126b2
movq 0xe932(%rip), %rbp      # 0x20fb8
movq (%rbp), %rdi
leal (%rdx,%r13), %r8d
leaq 0x7525(%rip), %rsi      # 0x19bba
leaq 0x7509(%rip), %rdx      # 0x19ba5
movl $0x2cd, %ecx            # imm = 0x2CD
movl %r8d, 0x4(%rsp)
movl %r14d, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x126c1
leal (%rdx,%r13), %eax
movl %eax, 0x4(%rsp)
movq 0xe8f7(%rip), %rbp      # 0x20fb8
movq %r13, 0x58(%rsp)
movl %r13d, %esi
movq 0x60(%rsp), %r13
addq 0x10(%r13), %rsi
movl %r14d, %edx
movq %rbx, %rdi
callq 0x5090
movq %rbx, %rdi
movl %r14d, %esi
movl $0x1, %edx
callq 0x11380
movb (%r13), %al
movb %al, 0x3(%rsp)
leal 0x4(%r14), %eax
movl %eax, 0x28(%rsp)
movzwl %ax, %eax
movl %eax, 0x2c(%rsp)
movl $0x0, 0x14(%rsp)
movl 0x4(%rsp), %r13d
movq %r14, 0x8(%rsp)
movl $0x13410, %edi          # imm = 0x13410
movq %rbx, %rsi
movl 0x2c(%rsp), %edx
callq 0x121a3
testl %eax, %eax
js 0x12b8c
movl $0xbb0b0000, 0x30(%rsp) # imm = 0xBB0B0000
movb 0x3(%rsp), %al
movb %al, 0x34(%rsp)
movl 0x28(%rsp), %eax
movw %ax, 0x35(%rsp)
movl %r13d, 0x37(%rsp)
xorl %eax, %eax
movl $0x2, %ecx
movzbl 0x30(%rsp,%rcx), %edx
addl %edx, %eax
incq %rcx
cmpq $0xb, %rcx
jne 0x12754
movw %ax, 0x3b(%rsp)
cmpb $0x1, (%r15)
jne 0x12798
movq (%rbp), %rdi
leaq 0x7529(%rip), %rsi      # 0x19ca3
leaq 0x754c(%rip), %rdx      # 0x19ccd
movl $0x24c, %ecx            # imm = 0x24C
movl $0x10, %r8d
leaq 0x30(%rsp), %r9
xorl %eax, %eax
callq 0x5180
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movl $0x13400, %edi          # imm = 0x13400
leaq 0x30(%rsp), %rsi
movl $0x10, %edx
callq 0x121a3
testl %eax, %eax
je 0x127d8
cmpb $0x1, (%r15)
je 0x129dd
testb $0x1, 0x14(%rsp)
movb $0x1, %al
movl %eax, 0x14(%rsp)
je 0x12716
jmp 0x12b92
xorl %r13d, %r13d
movl $0x13400, %edi          # imm = 0x13400
movq %r12, %rsi
movl $0x10, %edx
callq 0x124bc
movl %eax, %r14d
testl %eax, %eax
sete %cl
movb 0x41(%rsp), %al
cmpb $-0x12, %al
sete %dl
testb %dl, %cl
jne 0x128ac
movzbl %al, %ebx
movl $0x1388, %edi           # imm = 0x1388
callq 0x5170
cmpb $0x1, (%r15)
jne 0x12847
movq (%rbp), %rdi
subq $0x8, %rsp
leaq 0x74e4(%rip), %rsi      # 0x19d0c
leaq 0x749e(%rip), %rdx      # 0x19ccd
movl $0x25b, %ecx            # imm = 0x25B
movl %r13d, %r8d
movl %ebx, %r9d
xorl %eax, %eax
pushq %r14
callq 0x5180
addq $0x10, %rsp
incl %r13d
cmpl $0x5, %r13d
jne 0x127db
movb (%r15), %al
cmpl $0xee, %ebx
je 0x128af
testb $0x1, %al
jne 0x12a96
testb $0x1, %al
movq 0x18(%rsp), %rbx
movq 0x8(%rsp), %r14
movl 0x4(%rsp), %r13d
je 0x127c2
movq 0xe757(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
leaq 0x74fa(%rip), %rsi      # 0x19d85
leaq 0x743b(%rip), %rdx      # 0x19ccd
movl $0x25f, %ecx            # imm = 0x25F
movl $0x10, %r8d
movq %r12, %r9
xorl %eax, %eax
callq 0x5180
jmp 0x127c2
movb (%r15), %al
testb $0x1, %al
je 0x128d1
movq (%rbp), %rdi
leaq 0x74e3(%rip), %rsi      # 0x19da1
leaq 0x7408(%rip), %rdx      # 0x19ccd
movl $0x262, %ecx            # imm = 0x262
xorl %eax, %eax
callq 0x5180
movl $0x13880, %edi          # imm = 0x13880
callq 0x5170
xorl %r13d, %r13d
movl $0x13400, %edi          # imm = 0x13400
movq %r12, %rsi
movl $0x10, %edx
callq 0x124bc
movl %eax, %r14d
testl %eax, %eax
sete %al
movb 0x40(%rsp), %cl
movb 0x41(%rsp), %dl
cmpb $-0x12, %dl
sete %sil
andb %al, %sil
cmpb $-0x12, %cl
sete %dil
andb %sil, %dil
movb (%r15), %al
cmpb $0x1, %dil
je 0x12ac8
movzbl %dl, %ebx
movzbl %cl, %ebp
testb %al, %al
je 0x1295c
movq 0xe686(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x74ba(%rip), %rsi      # 0x19df6
leaq 0x738a(%rip), %rdx      # 0x19ccd
movl $0x271, %ecx            # imm = 0x271
movl %r13d, %r8d
movl %ebx, %r9d
xorl %eax, %eax
pushq %r14
pushq %rbp
callq 0x5180
addq $0x10, %rsp
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5170
incl %r13d
cmpl $0x14, %r13d
jne 0x128de
movb (%r15), %al
cmpb $0x1, %al
je 0x12a5a
testl %r14d, %r14d
movl 0x4(%rsp), %r13d
je 0x129ab
testb $0x1, %al
movq 0x18(%rsp), %rbx
movq 0xe622(%rip), %rbp      # 0x20fb8
movq 0x8(%rsp), %r14
je 0x127c2
callq 0x64a8
jmp 0x127c2
cmpl $0x43, %ebp
movq 0x18(%rsp), %rbx
jg 0x129e7
cmpl $0x22, %ebp
movq 0x8(%rsp), %r14
je 0x12a28
cmpl $0x33, %ebp
jne 0x12a0f
testb $0x1, %al
movq 0xe5eb(%rip), %rbp      # 0x20fb8
je 0x127c2
callq 0x6526
jmp 0x127c2
callq 0x647a
jmp 0x127c2
cmpl $0x44, %ebp
movq 0x8(%rsp), %r14
je 0x12a41
cmpl $0x55, %ebp
jne 0x12a0f
testb $0x1, %al
movq 0xe5b9(%rip), %rbp      # 0x20fb8
je 0x127c2
callq 0x64d2
jmp 0x127c2
testb $0x1, %al
movq 0xe5a0(%rip), %rbp      # 0x20fb8
je 0x127c2
callq 0x657a
jmp 0x127c2
testb $0x1, %al
movq 0xe587(%rip), %rbp      # 0x20fb8
je 0x127c2
callq 0x6550
jmp 0x127c2
testb $0x1, %al
movq 0xe56e(%rip), %rbp      # 0x20fb8
je 0x127c2
callq 0x64fc
jmp 0x127c2
movq 0xe577(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
leaq 0x73e1(%rip), %rsi      # 0x19e4c
leaq 0x725b(%rip), %rdx      # 0x19ccd
movl $0x276, %ecx            # imm = 0x276
movl $0x14, %r8d
movl %ebx, %r9d
xorl %eax, %eax
pushq %r14
pushq %rbp
callq 0x5180
addq $0x10, %rsp
movb (%r15), %al
jmp 0x1297e
movq 0xe53b(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
leaq 0x72a6(%rip), %rsi      # 0x19d4d
leaq 0x721f(%rip), %rdx      # 0x19ccd
movl $0x25e, %ecx            # imm = 0x25E
movl %ebx, %r8d
movl %r14d, %r9d
xorl %eax, %eax
callq 0x5180
movb (%r15), %al
jmp 0x12863
testb %al, %al
je 0x12af3
movq 0xe4e5(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x72ef(%rip), %rsi      # 0x19dcc
leaq 0x71e9(%rip), %rdx      # 0x19ccd
movl $0x26a, %ecx            # imm = 0x26A
xorl %eax, %eax
callq 0x5180
movb (%r15), %al
testb $0x1, %al
movq 0x18(%rsp), %rbx
movl 0x24(%rsp), %ebp
movq 0x50(%rsp), %rdx
je 0x12b3c
movq 0xe4ac(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x7155(%rip), %rsi      # 0x19c6b
movq %rdx, %r14
leaq 0x7138(%rip), %rdx      # 0x19c58
movl $0x2aa, %ecx            # imm = 0x2AA
movl 0x4(%rsp), %r8d
movq 0x8(%rsp), %r9
xorl %eax, %eax
callq 0x5180
movq %r14, %rdx
movb (%r15), %al
movq 0x8(%rsp), %rcx
movq 0x58(%rsp), %r13
addl %ecx, %r13d
subl %ecx, %ebp
jne 0x12666
xorl %ebp, %ebp
jmp 0x12b9d
leaq 0xe735(%rip), %rax      # 0x21291
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpb $0x1, (%rax)
jne 0x12ba5
movq 0xe44b(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x7008(%rip), %rsi      # 0x19b7f
leaq 0x7027(%rip), %rdx      # 0x19ba5
movl $0x2c4, %ecx            # imm = 0x2C4
xorl %eax, %eax
callq 0x5180
jmp 0x12ba5
cmpb $0x1, (%r15)
je 0x12be2
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpb $0x1, (%r15)
je 0x12bb6
movq %rbx, %rdi
callq 0x5110
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xe41b(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
leaq 0x702a(%rip), %rsi      # 0x19bf1
leaq 0x6fd7(%rip), %rdx      # 0x19ba5
movl $0x2d7, %ecx            # imm = 0x2D7
movl %r13d, %r8d
movl %r14d, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x12b9d
callq 0x65a4
jmp 0x12b92
nopl (%rax)