// File: /workspace/github_cpp_makefile_star2_O1/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), %ebx
movq %rdi, 0x60(%rsp)
movl 0x8(%rdi), %eax
movq %rax, 0x58(%rsp)
movl $0x1004, %edi           # imm = 0x1004
callq 0x5140
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x12d0e
testl %ebx, %ebx
je 0x12d0a
leaq 0xea6d(%rip), %r14      # 0x21291
leaq 0x10(%rsp), %r12
movq $0x0, 0x20(%rsp)
movl $0x1000, %eax           # imm = 0x1000
cmpl %eax, %ebx
movl $0x1000, %r13d          # imm = 0x1000
movl %ebx, 0x3c(%rsp)
cmovbl %ebx, %r13d
cmpb $0x1, (%r14)
jne 0x12882
movq 0xe764(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
movq 0x58(%rsp), %rax
movq 0x20(%rsp), %rcx
leal (%rcx,%rax), %r8d
leaq 0x738e(%rip), %rsi      # 0x19bfa
leaq 0x7372(%rip), %rdx      # 0x19be5
movl $0x2cd, %ecx            # imm = 0x2CD
movl %r13d, %r9d
xorl %eax, %eax
callq 0x5180
movq 0x20(%rsp), %rbp
movl %ebp, %esi
movq 0x60(%rsp), %r15
addq 0x10(%r15), %rsi
movl %r13d, %edx
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0x5090
movq %rbx, %rdi
movl %r13d, %esi
movl $0x1, %edx
callq 0x114c1
movb (%r15), %al
movb %al, 0xb(%rsp)
movq 0x58(%rsp), %rax
addl %ebp, %eax
movl %eax, 0xc(%rsp)
movq %r13, 0x28(%rsp)
leal 0x4(%r13), %eax
movl %eax, 0x40(%rsp)
movzwl %ax, %eax
movl %eax, 0x44(%rsp)
xorl %r13d, %r13d
leaq 0x742b(%rip), %rbp      # 0x19d0d
movl $0x13410, %edi          # imm = 0x13410
movq 0x30(%rsp), %rsi
movl 0x44(%rsp), %edx
callq 0x1234f
testl %eax, %eax
js 0x12c9f
movl $0xbb0b0000, 0x48(%rsp) # imm = 0xBB0B0000
movb 0xb(%rsp), %al
movb %al, 0x4c(%rsp)
movl 0x40(%rsp), %eax
movw %ax, 0x4d(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x4f(%rsp)
xorl %eax, %eax
movl $0x2, %ecx
movzbl 0x48(%rsp,%rcx), %edx
addl %edx, %eax
incq %rcx
cmpq $0xb, %rcx
jne 0x12925
movw %ax, 0x53(%rsp)
cmpb $0x1, (%r14)
jne 0x1296b
movq 0xe671(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x7392(%rip), %rsi      # 0x19ce3
movq %rbp, %rdx
movl $0x24c, %ecx            # imm = 0x24C
movl $0x10, %r8d
leaq 0x48(%rsp), %r9
xorl %eax, %eax
callq 0x5180
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movl $0x13400, %edi          # imm = 0x13400
leaq 0x48(%rsp), %rsi
movl $0x10, %edx
callq 0x1234f
testl %eax, %eax
je 0x129a4
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpb $0x1, (%r14)
jne 0x12b21
callq 0x64ce
jmp 0x12b21
xorl %r15d, %r15d
movl $0x13400, %edi          # imm = 0x13400
movq %r12, %rsi
movl $0x10, %edx
callq 0x1267f
movl %eax, %ebx
testl %eax, %eax
jne 0x129c6
cmpb $-0x12, 0x11(%rsp)
je 0x12a13
movl $0x1388, %edi           # imm = 0x1388
callq 0x5170
cmpb $0x1, (%r14)
jne 0x12a08
movq 0xe5db(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
movzbl 0x11(%rsp), %r9d
subq $0x8, %rsp
leaq 0x735b(%rip), %rsi      # 0x19d4c
movq %rbp, %rdx
movl $0x25b, %ecx            # imm = 0x25B
movl %r15d, %r8d
xorl %eax, %eax
pushq %rbx
callq 0x5180
addq $0x10, %rsp
incl %r15d
cmpl $0x5, %r15d
jne 0x129a7
jmp 0x12a15
xorl %ebx, %ebx
movzbl 0x11(%rsp), %r8d
movb (%r14), %al
cmpl $0xee, %r8d
jne 0x12b0a
testb %al, %al
je 0x12a4f
movq 0xe582(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x73a1(%rip), %rsi      # 0x19de1
movq %rbp, %rdx
movl $0x262, %ecx            # imm = 0x262
xorl %eax, %eax
callq 0x5180
movl $0x13880, %edi          # imm = 0x13880
callq 0x5170
xorl %r15d, %r15d
movl $0x13400, %edi          # imm = 0x13400
movq %r12, %rsi
movl $0x10, %edx
callq 0x1267f
movl %eax, %ebx
testl %eax, %eax
sete %dl
movb 0x10(%rsp), %al
movb 0x11(%rsp), %cl
cmpb $-0x12, %cl
sete %sil
andb %dl, %sil
cmpb $-0x12, %al
sete %dil
andb %sil, %dil
movb (%r14), %dl
cmpb $0x1, %dil
je 0x12b42
testb %dl, %dl
je 0x12ad3
movzbl %cl, %r9d
movzbl %al, %r10d
movq 0xe508(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x737c(%rip), %rsi      # 0x19e36
movq %rbp, %rdx
movl $0x271, %ecx            # imm = 0x271
movl %r15d, %r8d
xorl %eax, %eax
pushq %rbx
pushq %r10
callq 0x5180
addq $0x10, %rsp
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5170
incl %r15d
cmpl $0x14, %r15d
jne 0x12a5c
cmpb $0x1, (%r14)
je 0x12c61
testl %ebx, %ebx
je 0x12b6f
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpb $0x1, (%r14)
jne 0x12b21
callq 0x64fc
jmp 0x12b21
testb %al, %al
jne 0x12bc3
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpb $0x1, (%r14)
je 0x12beb
xorl %r15d, %r15d
testb %r15b, %r15b
jne 0x12cb1
orb %r15b, %r13b
testb $0x1, %r13b
movb $0x1, %r13b
je 0x128e2
jmp 0x12ce9
xorl %ebx, %ebx
movb $0x1, %r15b
testb %dl, %dl
je 0x12b24
movq 0xe466(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
xorl %ebx, %ebx
leaq 0x72ae(%rip), %rsi      # 0x19e0c
movq %rbp, %rdx
movl $0x26a, %ecx            # imm = 0x26A
xorl %eax, %eax
callq 0x5180
jmp 0x12b24
movzbl 0x10(%rsp), %ecx
movb (%r14), %al
cmpl $0x43, %ecx
jg 0x12b9e
cmpl $0x22, %ecx
je 0x12c33
cmpl $0x33, %ecx
jne 0x12c1c
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testb %al, %al
je 0x12b21
callq 0x657a
jmp 0x12b21
cmpl $0x44, %ecx
je 0x12c4a
cmpl $0x55, %ecx
jne 0x12c1c
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testb %al, %al
je 0x12b21
callq 0x6526
jmp 0x12b21
movq 0xe40e(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
leaq 0x71b9(%rip), %rsi      # 0x19d8d
movq %rbp, %rdx
movl $0x25e, %ecx            # imm = 0x25E
movl %ebx, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x12b12
movq 0xe3e6(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
xorl %r15d, %r15d
leaq 0x71c6(%rip), %rsi      # 0x19dc5
movq %rbp, %rdx
movl $0x25f, %ecx            # imm = 0x25F
movl $0x10, %r8d
movq %r12, %r9
xorl %eax, %eax
callq 0x5180
jmp 0x12b24
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testb %al, %al
je 0x12b21
callq 0x65ce
jmp 0x12b21
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testb %al, %al
je 0x12b21
callq 0x65a4
jmp 0x12b21
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testb %al, %al
je 0x12b21
callq 0x6550
jmp 0x12b21
movq 0xe370(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
movzbl 0x11(%rsp), %r9d
movzbl 0x10(%rsp), %r10d
leaq 0x720e(%rip), %rsi      # 0x19e8c
movq %rbp, %rdx
movl $0x276, %ecx            # imm = 0x276
movl $0x14, %r8d
xorl %eax, %eax
pushq %rbx
pushq %r10
callq 0x5180
addq $0x10, %rsp
jmp 0x12af4
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpb $0x1, (%r14)
jne 0x12ce9
callq 0x65f8
jmp 0x12ce9
xorl %ebx, %ebx
cmpb $0x1, (%r14)
jne 0x12ce9
movq 0xe2f8(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
xorl %ebx, %ebx
leaq 0x6fdf(%rip), %rsi      # 0x19cab
leaq 0x6fc5(%rip), %rdx      # 0x19c98
movl $0x2aa, %ecx            # imm = 0x2AA
movl 0xc(%rsp), %r8d
movq 0x28(%rsp), %r9
xorl %eax, %eax
callq 0x5180
testl %ebx, %ebx
jne 0x12d45
movq 0x28(%rsp), %rax
movq 0x20(%rsp), %rcx
addl %eax, %ecx
movq %rcx, 0x20(%rsp)
movl 0x3c(%rsp), %ebx
subl %eax, %ebx
jne 0x12832
xorl %ebx, %ebx
jmp 0x12d50
leaq 0xe57c(%rip), %rax      # 0x21291
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpb $0x1, (%rax)
jne 0x12d5a
movq 0xe292(%rip), %rax      # 0x20fb8
movq (%rax), %rdi
leaq 0x6e8f(%rip), %rsi      # 0x19bbf
leaq 0x6eae(%rip), %rdx      # 0x19be5
movl $0x2c4, %ecx            # imm = 0x2C4
xorl %eax, %eax
callq 0x5180
jmp 0x12d5a
cmpb $0x1, (%r14)
movq 0x28(%rsp), %r9
je 0x12d6b
movq 0x30(%rsp), %rdi
callq 0x5110
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xe266(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
leaq 0x6eb5(%rip), %rsi      # 0x19c31
leaq 0x6e62(%rip), %rdx      # 0x19be5
movl $0x2d7, %ecx            # imm = 0x2D7
movl 0xc(%rsp), %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x12d50
nop