// File: goodix[P]gdix_hid_firmware_update/gtx9/gtx9.cpp
// Function: GTx9Device::ReadPkg(unsigned int, unsigned char*, unsigned int)
int GTx9Device::ReadPkg(unsigned int addr, unsigned char *buf, unsigned int len)
{
	uint8_t HidBuf[PACKAGE_LEN] = {0};
	int ret;
	int retry = 5;

	while (retry--) {
		HidBuf[0] = REPORT_ID;			 /* report id */
		HidBuf[1] = I2C_DIRECT_RW;		 /* cmd type */
		HidBuf[2] = 0;					 /* data flag */
		HidBuf[3] = 0;					 /* frame num */
		HidBuf[4] = 7;					 /* data length */
		HidBuf[5] = I2C_READ_FLAG;		 /* read operation flag */
		HidBuf[6] = (addr >> 24) & 0xFF; /* 32 bit addr */
		HidBuf[7] = (addr >> 16) & 0xFF;
		HidBuf[8] = (addr >> 8) & 0xFF;
		HidBuf[9] = addr & 0xFF;
		HidBuf[10] = (len >> 8) & 0xFF; /* 16 bit length */
		HidBuf[11] = len & 0xFF;
		ret = SetReport(HidBuf, 12);
		if (ret < 0) {
			gdix_err("Failed set report, ret:%d\n", ret);
			return ret;
		}
		ret = GetReport(HidBuf);
		if (ret < 0) {
			gdix_err("Failed get report, ret:%d\n", ret);
			return ret;
		}
		if (HidBuf[3] == 0 && HidBuf[4] == len) {
			memcpy(buf, &HidBuf[5], HidBuf[4]);
			return len;
		}
		usleep(1000);
	}

	return -EINVAL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x80(%rbp)
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
movl $0x41, %edx
callq 0x50a0
movl $0x5, -0x78(%rbp)
movl -0x78(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x78(%rbp)
cmpl $0x0, %eax
je 0x13486
movq -0x80(%rbp), %rdi
movb $0xe, -0x70(%rbp)
movb $0x20, -0x6f(%rbp)
movb $0x0, -0x6e(%rbp)
movb $0x0, -0x6d(%rbp)
movb $0x7, -0x6c(%rbp)
movb $0x1, -0x6b(%rbp)
movl -0x14(%rbp), %eax
shrl $0x18, %eax
andl $0xff, %eax
movb %al, -0x6a(%rbp)
movl -0x14(%rbp), %eax
shrl $0x10, %eax
andl $0xff, %eax
movb %al, -0x69(%rbp)
movl -0x14(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, -0x68(%rbp)
movl -0x14(%rbp), %eax
andl $0xff, %eax
movb %al, -0x67(%rbp)
movl -0x24(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, -0x66(%rbp)
movl -0x24(%rbp), %eax
andl $0xff, %eax
movb %al, -0x65(%rbp)
leaq -0x70(%rbp), %rsi
movl $0xc, %edx
callq 0x134a0
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
jge 0x133f0
jmp 0x133af
leaq 0x12ee3(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x133e3
movq 0x12c16(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movl -0x74(%rbp), %r8d
leaq 0xb69f(%rip), %rsi      # 0x1ea6f
leaq 0xb08b(%rip), %rdx      # 0x1e462
movl $0x5f, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x133e5
movl -0x74(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1348d
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x13560
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
jge 0x13446
jmp 0x13408
leaq 0x12e8a(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x1343c
movq 0x12bbd(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movl -0x74(%rbp), %r8d
leaq 0xb672(%rip), %rsi      # 0x1ea9b
leaq 0xb032(%rip), %rdx      # 0x1e462
movl $0x64, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x1343e
movl -0x74(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1348d
movzbl -0x6d(%rbp), %eax
cmpl $0x0, %eax
jne 0x13477
movzbl -0x6c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jne 0x13477
movq -0x20(%rbp), %rdi
leaq -0x70(%rbp), %rsi
addq $0x5, %rsi
movzbl -0x6c(%rbp), %eax
movl %eax, %edx
callq 0x50b0
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1348d
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x5180
jmp 0x13318
movl $0xffffffea, -0x4(%rbp) # imm = 0xFFFFFFEA
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)