// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movl 0x4(%rdi), %ebp
movq %rdi, 0x70(%rsp)
movl 0x8(%rdi), %eax
movq %rax, 0x58(%rsp)
movl $0x1004, %edi           # imm = 0x1004
callq 0x5140
testq %rax, %rax
je 0x11b7f
movq %rax, %rbx
leaq 0xec9f(%rip), %r14      # 0x20291
pushq $0x10
popq %r12
leaq 0x40(%rsp), %r13
xorl %ecx, %ecx
movq %rax, 0x28(%rsp)
testl %ebp, %ebp
je 0x11bdc
movl $0x1000, %eax           # imm = 0x1000
cmpl %eax, %ebp
movl $0x1000, %r15d          # imm = 0x1000
cmovbl %ebp, %r15d
cmpb $0x0, (%r14)
movq %rcx, 0x68(%rsp)
movl %ebp, 0x34(%rsp)
je 0x11666
movq 0xe987(%rip), %rax      # 0x1ffb8
movq (%rax), %rdi
movq 0x58(%rsp), %rax
leal (%rcx,%rax), %r8d
leaq 0x7576(%rip), %rsi      # 0x18bba
leaq 0x755a(%rip), %rdx      # 0x18ba5
movl $0x2cd, %ecx            # imm = 0x2CD
movl %r8d, 0x10(%rsp)
movl %r15d, %r9d
xorl %eax, %eax
callq 0x5180
movq 0x68(%rsp), %rcx
jmp 0x11671
movq 0x58(%rsp), %rax
addl %ecx, %eax
movl %eax, 0x10(%rsp)
movq %r15, %rbp
movl %ecx, %esi
movq 0x70(%rsp), %r15
addq 0x10(%r15), %rsi
movl %ebp, %edx
movq %rbx, %rdi
callq 0x5090
movq %rbx, %rdi
movl %ebp, %esi
pushq $0x1
popq %rdx
callq 0x100ed
movb (%r15), %al
movb %al, 0xf(%rsp)
movq %rbp, 0x60(%rsp)
leal 0x4(%rbp), %eax
movl %eax, 0x38(%rsp)
movzwl %ax, %eax
movl %eax, 0x3c(%rsp)
movl $0x0, 0x14(%rsp)
leaq 0x1a(%rsp), %r15
pushq $0x9
popq %rbp
movl $0x13410, %edi          # imm = 0x13410
movq %rbx, %rsi
movl 0x3c(%rsp), %edx
callq 0x1102c
testl %eax, %eax
js 0x11bb4
movl $0xbb0b0000, 0x18(%rsp) # imm = 0xBB0B0000
movb 0xf(%rsp), %al
movb %al, 0x1c(%rsp)
movl 0x38(%rsp), %eax
movw %ax, 0x1d(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x1f(%rsp)
movq %r15, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x100ed
cmpb $0x1, (%r14)
jne 0x11738
movq 0xe8a5(%rip), %rax      # 0x1ffb8
movq (%rax), %rdi
leaq 0x7586(%rip), %rsi      # 0x18ca3
leaq 0x75a9(%rip), %rdx      # 0x18ccd
movl $0x24c, %ecx            # imm = 0x24C
movl %r12d, %r8d
leaq 0x18(%rsp), %r9
xorl %eax, %eax
callq 0x5180
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movl $0x13400, %edi          # imm = 0x13400
leaq 0x18(%rsp), %rsi
movl %r12d, %edx
callq 0x1102c
testl %eax, %eax
je 0x11776
cmpb $0x1, (%r14)
je 0x119c0
testb $0x1, 0x14(%rsp)
movb $0x1, %al
movl %eax, 0x14(%rsp)
je 0x116c0
jmp 0x11bba
xorl %ebp, %ebp
xorl %ebx, %ebx
xorl %r15d, %r15d
cmpl $0x5, %ebx
je 0x117f5
movl $0x13400, %edi          # imm = 0x13400
movq %r13, %rsi
movl %r12d, %edx
callq 0x113f6
movl %eax, %r15d
testl %eax, %eax
sete %al
movb 0x41(%rsp), %bpl
cmpb $-0x12, %bpl
sete %cl
testb %cl, %al
jne 0x1184a
movl $0x1388, %edi           # imm = 0x1388
callq 0x5170
cmpb $0x1, (%r14)
jne 0x117f1
movzbl %bpl, %r9d
movq 0xe7ef(%rip), %rax      # 0x1ffb8
movq (%rax), %rdi
subq $0x8, %rsp
leaq 0x7535(%rip), %rsi      # 0x18d0c
leaq 0x74ef(%rip), %rdx      # 0x18ccd
movl $0x25b, %ecx            # imm = 0x25B
movl %ebx, %r8d
xorl %eax, %eax
pushq %r15
callq 0x5180
popq %rcx
popq %rdx
incl %ebx
jmp 0x1177d
movb (%r14), %al
cmpb $-0x12, %bpl
je 0x11850
testb $0x1, %al
movq 0x28(%rsp), %rbx
jne 0x11ad2
testb $0x1, %al
leaq 0x1a(%rsp), %r15
pushq $0x9
popq %rbp
je 0x11760
movq 0xe7b6(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x7559(%rip), %rsi      # 0x18d85
leaq 0x749a(%rip), %rdx      # 0x18ccd
movl $0x25f, %ecx            # imm = 0x25F
movl %r12d, %r8d
movq %r13, %r9
xorl %eax, %eax
callq 0x5180
jmp 0x11760
movb (%r14), %al
xorl %r15d, %r15d
testb $0x1, %al
je 0x11878
movq 0xe75d(%rip), %rax      # 0x1ffb8
movq (%rax), %rdi
leaq 0x753c(%rip), %rsi      # 0x18da1
leaq 0x7461(%rip), %rdx      # 0x18ccd
movl $0x262, %ecx            # imm = 0x262
xorl %eax, %eax
callq 0x5180
movl $0x13880, %edi          # imm = 0x13880
callq 0x5170
xorl %ebx, %ebx
movb $-0x12, %bpl
cmpl $0x14, %ebx
je 0x1191a
movl $0x13400, %edi          # imm = 0x13400
movq %r13, %rsi
movl %r12d, %edx
callq 0x113f6
movl %eax, %r15d
testl %eax, %eax
sete %cl
movb 0x40(%rsp), %al
movb 0x41(%rsp), %bpl
cmpb $-0x12, %bpl
sete %dl
andb %cl, %dl
cmpb $-0x12, %al
sete %sil
andb %dl, %sil
movb (%r14), %cl
cmpb $0x1, %sil
je 0x11b05
testb %cl, %cl
je 0x11909
movzbl %bpl, %r9d
movzbl %al, %r10d
movq 0xe6d5(%rip), %rax      # 0x1ffb8
movq (%rax), %rdi
leaq 0x7509(%rip), %rsi      # 0x18df6
leaq 0x73d9(%rip), %rdx      # 0x18ccd
movl $0x271, %ecx            # imm = 0x271
movl %ebx, %r8d
xorl %eax, %eax
pushq %r15
pushq %r10
callq 0x5180
popq %rcx
popq %rdx
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5170
incl %ebx
jmp 0x11887
cmpb $0x1, (%r14)
je 0x11a95
testl %r15d, %r15d
movq 0x28(%rsp), %rbx
je 0x11969
cmpb $0x1, (%r14)
leaq 0x1a(%rsp), %r15
pushq $0x9
popq %rbp
jne 0x11760
movq 0xe691(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x7546(%rip), %rsi      # 0x18e97
leaq 0x7375(%rip), %rdx      # 0x18ccd
movl $0x278, %ecx            # imm = 0x278
xorl %eax, %eax
callq 0x5180
jmp 0x11760
movzbl 0x40(%rsp), %ecx
movb (%r14), %al
cmpl $0x55, %ecx
leaq 0x1a(%rsp), %r15
pushq $0x9
popq %rbp
je 0x11a41
cmpl $0x33, %ecx
je 0x119ed
cmpl $0x44, %ecx
je 0x11a17
cmpl $0x22, %ecx
jne 0x11a6b
testb %al, %al
je 0x11760
movq 0xe630(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x7515(%rip), %rsi      # 0x18ec7
leaq 0x7314(%rip), %rdx      # 0x18ccd
movl $0x27e, %ecx            # imm = 0x27E
jmp 0x1195d
movq 0xe611(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x7310(%rip), %rsi      # 0x18ce1
leaq 0x72f5(%rip), %rdx      # 0x18ccd
movl $0x250, %ecx            # imm = 0x250
pushq $-0x1
popq %r8
xorl %eax, %eax
callq 0x5180
jmp 0x11760
testb %al, %al
je 0x11760
movq 0xe5dc(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x7505(%rip), %rsi      # 0x18f0b
leaq 0x72c0(%rip), %rdx      # 0x18ccd
movl $0x281, %ecx            # imm = 0x281
jmp 0x1195d
testb %al, %al
je 0x11760
movq 0xe5b2(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x750d(%rip), %rsi      # 0x18f3d
leaq 0x7296(%rip), %rdx      # 0x18ccd
movl $0x284, %ecx            # imm = 0x284
jmp 0x1195d
testb %al, %al
je 0x11760
movq 0xe588(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x751b(%rip), %rsi      # 0x18f75
leaq 0x726c(%rip), %rdx      # 0x18ccd
movl $0x287, %ecx            # imm = 0x287
jmp 0x1195d
testb %al, %al
je 0x11760
movq 0xe55e(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x7526(%rip), %rsi      # 0x18faa
leaq 0x7242(%rip), %rdx      # 0x18ccd
movl $0x28a, %ecx            # imm = 0x28A
jmp 0x1195d
movq 0xe53c(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
movzbl %bpl, %r9d
movzbl 0x40(%rsp), %r10d
leaq 0x739c(%rip), %rsi      # 0x18e4c
leaq 0x7216(%rip), %rdx      # 0x18ccd
movl $0x276, %ecx            # imm = 0x276
pushq $0x14
popq %r8
xorl %eax, %eax
pushq %r15
pushq %r10
callq 0x5180
popq %rcx
popq %rdx
jmp 0x11924
movzbl %bpl, %r8d
movq 0xe4fb(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x7266(%rip), %rsi      # 0x18d4d
leaq 0x71df(%rip), %rdx      # 0x18ccd
movl $0x25e, %ecx            # imm = 0x25E
movl %r15d, %r9d
xorl %eax, %eax
callq 0x5180
movb (%r14), %al
jmp 0x1180b
testb %cl, %cl
je 0x11b30
movq 0xe4a8(%rip), %rax      # 0x1ffb8
movq (%rax), %rdi
leaq 0x72b2(%rip), %rsi      # 0x18dcc
leaq 0x71ac(%rip), %rdx      # 0x18ccd
movl $0x26a, %ecx            # imm = 0x26A
xorl %eax, %eax
callq 0x5180
movb (%r14), %cl
testb $0x1, %cl
movq 0x28(%rsp), %rbx
movl 0x34(%rsp), %ebp
movq 0x60(%rsp), %r15
je 0x11b6f
movq 0xe46e(%rip), %rax      # 0x1ffb8
movq (%rax), %rdi
leaq 0x7117(%rip), %rsi      # 0x18c6b
leaq 0x70fd(%rip), %rdx      # 0x18c58
movl $0x2aa, %ecx            # imm = 0x2AA
movl 0x10(%rsp), %r8d
movl %r15d, %r9d
xorl %eax, %eax
callq 0x5180
movq 0x68(%rsp), %rcx
addl %r15d, %ecx
subl %r15d, %ebp
jmp 0x11602
pushq $-0x1
popq %rbp
leaq 0xe708(%rip), %rax      # 0x20291
cmpb $0x1, (%rax)
jne 0x11bcb
movq 0xe423(%rip), %rax      # 0x1ffb8
movq (%rax), %rdi
leaq 0x6fe0(%rip), %rsi      # 0x18b7f
leaq 0x6fff(%rip), %rdx      # 0x18ba5
movl $0x2c4, %ecx            # imm = 0x2C4
xorl %eax, %eax
callq 0x5180
jmp 0x11bcb
cmpb $0x1, (%r14)
je 0x11c10
cmpb $0x1, (%r14)
je 0x11be0
pushq $-0x1
popq %rbp
movq %rbx, %rdi
callq 0x5110
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
jmp 0x11bc3
movq 0xe3f1(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x7000(%rip), %rsi      # 0x18bf1
leaq 0x6fad(%rip), %rdx      # 0x18ba5
movl $0x2d7, %ecx            # imm = 0x2D7
movl 0x10(%rsp), %r8d
movq 0x60(%rsp), %r9
xorl %eax, %eax
callq 0x5180
jmp 0x11bc0
movq 0xe3c1(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x7005(%rip), %rsi      # 0x18c26
leaq 0x7030(%rip), %rdx      # 0x18c58
movl $0x299, %ecx            # imm = 0x299
xorl %eax, %eax
callq 0x5180
jmp 0x11bba
nop