// File: /workspace/github_c_makefile_star2_O2/seuscq[P]rtk_hciattach/hciattach_rtk.c
// Function: rtk_get_final_patch
void rtk_get_final_patch(int fd, int proto)
{
	struct btrtl_info *rtl = &rtk_hw_cfg;
	uint8_t proj_id = 0;
	struct rtk_epatch_entry *entry = NULL;
	struct rtk_epatch *patch = (struct rtk_epatch *)rtl->fw_buf;
	uint32_t svn_ver, coex_ver, tmp;

	if ((proto == HCI_UART_H4 && rtl->lmp_subver != ROM_LMP_8723b) ||
	    ((proto == HCI_UART_3WIRE) && (rtl->lmp_subver == ROM_LMP_8723a))) {
		if (memcmp(rtl->fw_buf, RTK_EPATCH_SIGNATURE, 8) == 0) {
			RS_ERR("Check signature error!");
			rtl->dl_fw_flag = 0;
			goto free_buf;
		} else {
			rtl->total_len = rtl->config_len + rtl->fw_len;
			if (!(rtl->total_buf = malloc(rtl->total_len))) {
				RS_ERR("Can't alloc mem for fw/config, errno:%d",
				       errno);
				rtl->dl_fw_flag = 0;
				rtl->total_len = 0;
				goto free_buf;
			} else {
				RS_DBG("fw copy directy");

				svn_ver = get_unaligned_le32(
						rtl->fw_buf + rtl->fw_len - 8);
				coex_ver = get_unaligned_le32(
						rtl->fw_buf + rtl->fw_len - 12);

				RS_INFO("Svn version: %8d\n", svn_ver);
				tmp = ((coex_ver >> 16) & 0x7ff) +
					(coex_ver >> 27) * 10000;
				RS_INFO("Coexistence: BTCOEX_20%06d-%04x\n", tmp,
						(coex_ver & 0xffff));

				memcpy(rtl->total_buf, rtl->fw_buf,
				       rtl->fw_len);
				if (rtl->config_len)
					memcpy(rtl->total_buf +
					       rtl->fw_len,
					       rtl->config_buf,
					       rtl->config_len);
				rtl->dl_fw_flag = 1;
				goto free_buf;
			}
		}
	}

	if (memcmp(rtl->fw_buf, RTK_EPATCH_SIGNATURE, 8)) {
		RS_DBG("check signature error!");
		rtl->dl_fw_flag = 0;
		goto free_buf;
	}

	if (memcmp
	    (rtl->fw_buf + rtl->fw_len - 4,
	     Extension_Section_SIGNATURE, 4)) {
		RS_ERR("check extension section signature error");
		rtl->dl_fw_flag = 0;
		goto free_buf;
	}

	proj_id = rtk_get_fw_project_id(rtl->fw_buf + rtl->fw_len - 5);

#ifdef RTL_8703A_SUPPORT
	if (rtl->hci_ver == 0x4 && rtl->lmp_subver == ROM_LMP_8723b) {
		RS_DBG("HCI version = 0x4, IC is 8703A.");
	} else {
		RS_ERR("error: lmp_version %x, hci_version %x, project_id %x",
		       rtl->lmp_subver, rtl->hci_ver, project_id[proj_id]);
		rtk_hw_cfg.dl_fw_flag = 0;
		goto free_buf;
	}
#else
	if (rtl->lmp_subver != ROM_LMP_8703b) {
		if (rtl->lmp_subver != project_id[proj_id]) {
			RS_ERR("lmp_subver %04x, project id %04x, mismatch\n",
			       rtl->lmp_subver, project_id[proj_id]);
			rtl->dl_fw_flag = 0;
			goto free_buf;
		}
	} else {
		/* if (rtk_hw_cfg.patch_ent->proj_id != project_id[proj_id]) {
		 * 	RS_ERR("proj_id %04x, version %04x from firmware "
		 * 	       "project_id[%u], mismatch\n",
		 * 	       rtk_hw_cfg.patch_ent->proj_id,
		 * 	       project_id[proj_id], proj_id);
		 * 	rtk_hw_cfg.dl_fw_flag = 0;
		 * 	goto free_buf;
		 * } */
	}
#endif

	entry = rtk_get_patch_entry();

	if (entry)
		rtl->total_len =
		    entry->patch_length + rtl->config_len;
	else {
		rtl->dl_fw_flag = 0;
		goto free_buf;
	}

	if (!(rtl->total_buf = malloc(rtl->total_len))) {
		RS_ERR("Can't alloc memory for multi fw&config, errno:%d",
		       errno);
		rtl->dl_fw_flag = 0;
		rtl->total_len = 0;
		goto free_buf;
	} else {
		memcpy(rtl->total_buf,
		       rtl->fw_buf + entry->start_offset,
		       entry->patch_length);
		memcpy(rtl->total_buf + entry->patch_length - 4,
		       &patch->fw_version, 4);

		if (rtl->config_len)
			memcpy(rtl->total_buf + entry->patch_length,
			       rtl->config_buf, rtl->config_len);
		rtl->dl_fw_flag = 1;
	}

	RS_DBG("fw:%s exists, config file:%s exists",
	       (rtl->fw_len > 0) ? "" : "not",
	       (rtl->config_len > 0) ? "" : "not");

free_buf:
	if (rtl->fw_len > 0) {
		free(rtl->fw_buf);
		rtl->fw_len = 0;
	}

	if (rtl->config_len > 0) {
		free(rtl->config_buf);
		rtl->config_len = 0;
	}

	if (entry)
		free(entry);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq 0x6aec(%rip), %r13      # 0xc748
testl %esi, %esi
setne %cl
movzwl 0x6aa2(%rip), %eax      # 0xc70a
cmpw $0x8723, %ax            # imm = 0x8723
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x5e21
cmpl $0x2, %esi
sete %cl
cmpw $0x1200, %ax            # imm = 0x1200
sete %al
andb %cl, %al
cmpb $0x1, %al
je 0x5e21
movabsq $0x686365746c616552, %rax # imm = 0x686365746C616552
cmpq %rax, (%r13)
je 0x5d05
cmpb $0x0, 0x6638(%rip)      # 0xc2e0
jne 0x5e95
movb $0x0, 0x6a67(%rip)      # 0xc71c
xorl %ebx, %ebx
cmpl $0x0, 0x6a7e(%rip)      # 0xc73c
jle 0x5cd3
movq 0x6a81(%rip), %rdi      # 0xc748
callq 0x2030
andl $0x0, 0x6a69(%rip)      # 0xc73c
cmpl $0x0, 0x6a66(%rip)      # 0xc740
jle 0x5cef
movq 0x6a6d(%rip), %rdi      # 0xc750
callq 0x2030
andl $0x0, 0x6a51(%rip)      # 0xc740
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2030
movslq 0x6a30(%rip), %rdi      # 0xc73c
cmpl $0x77fd0451, -0x4(%r13,%rdi) # imm = 0x77FD0451
jne 0x5f9a
addq %r13, %rdi
addq $-0x5, %rdi
callq 0x5934
movzwl 0x69dc(%rip), %ecx      # 0xc70a
cmpl $0x8703, %ecx           # imm = 0x8703
je 0x5d4f
movzbl %al, %eax
leaq 0x65b0(%rip), %rdx      # 0xc2f0
movzwl (%rdx,%rax,2), %r8d
cmpw %r8w, %cx
jne 0x6003
callq 0x59d6
testq %rax, %rax
je 0x5cae
movq %rax, %rbx
movzwl 0x2(%rax), %r15d
movslq 0x69d4(%rip), %r14      # 0xc740
leaq (%r14,%r15), %rdi
movl %edi, 0x69ce(%rip)      # 0xc744
callq 0x2190
movq %rax, 0x69d6(%rip)      # 0xc758
testq %rax, %rax
je 0x5fc7
movq %rax, %r12
movl 0x4(%rbx), %esi
addq 0x69b0(%rip), %rsi      # 0xc748
movq %rax, %rdi
movq %r15, %rdx
callq 0x2180
movl 0x8(%r13), %eax
movl %eax, -0x4(%r12,%r15)
testl %r14d, %r14d
je 0x5dc6
addq %r15, %r12
movq 0x6995(%rip), %rsi      # 0xc750
movq %r12, %rdi
movq %r14, %rdx
callq 0x2180
movb $0x1, 0x694f(%rip)      # 0xc71c
cmpb $0x0, 0x650c(%rip)      # 0xc2e0
je 0x5cb7
movq 0x6217(%rip), %rax      # 0xbff8
cmpl $0x0, 0x6954(%rip)      # 0xc73c
leaq 0x31f8(%rip), %rdx      # 0x8fe7
leaq 0x31ca(%rip), %r8       # 0x8fc0
movq %r8, %rcx
cmovgq %rdx, %rcx
movq (%rax), %rdi
testl %r14d, %r14d
cmovgq %rdx, %r8
leaq 0x3189(%rip), %rsi      # 0x8f97
leaq 0x2724(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
jmp 0x5cb7
movabsq $0x686365746c616552, %rax # imm = 0x686365746C616552
cmpq %rax, (%r13)
jne 0x5e47
movq 0x61c0(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x3029(%rip), %rsi      # 0x8e6b
jmp 0x5fab
movslq 0x68f2(%rip), %rdi      # 0xc740
movslq 0x68e7(%rip), %rbx      # 0xc73c
addq %rbx, %rdi
movl %edi, 0x68e6(%rip)      # 0xc744
callq 0x2190
movq %rax, 0x68ee(%rip)      # 0xc758
testq %rax, %rax
jne 0x5eb9
movq 0x6182(%rip), %rax      # 0xbff8
movq (%rax), %r14
callq 0x2040
movl (%rax), %ecx
leaq 0x3006(%rip), %rsi      # 0x8e8d
leaq 0x26ab(%rip), %rdx      # 0x8539
xorl %ebx, %ebx
jmp 0x5fe6
movq 0x615c(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x302f(%rip), %rsi      # 0x8ed5
leaq 0x268c(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
jmp 0x5cae
cmpb $0x0, 0x6420(%rip)      # 0xc2e0
je 0x5eee
movq 0x612f(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x2fed(%rip), %rsi      # 0x8ec0
leaq 0x265f(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
movq 0x6860(%rip), %r13      # 0xc748
movl 0x684e(%rip), %ebx      # 0xc73c
movslq %ebx, %rax
movl -0xc(%r13,%rax), %r14d
movl -0x8(%r13,%rax), %ecx
movq 0x60f6(%rip), %r15      # 0xbff8
movq (%r15), %rdi
leaq 0x2f03(%rip), %rsi      # 0x8e0f
leaq 0x2626(%rip), %rbx      # 0x8539
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2150
movl %r14d, %eax
shrl $0x10, %eax
andl $0x7ff, %eax            # imm = 0x7FF
movl %r14d, %ecx
shrl $0x1b, %ecx
imull $0x2710, %ecx, %ecx     # imm = 0x2710
addl %eax, %ecx
movq (%r15), %rdi
movzwl %r14w, %r8d
leaq 0x2ee2(%rip), %rsi      # 0x8e26
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2150
movq 0x6803(%rip), %rdi      # 0xc758
movq 0x67ec(%rip), %rsi      # 0xc748
movslq 0x67d9(%rip), %rdx      # 0xc73c
callq 0x2180
movslq 0x67d1(%rip), %rdx      # 0xc740
testq %rdx, %rdx
je 0x5f8e
movslq 0x67c1(%rip), %rdi      # 0xc73c
addq 0x67d6(%rip), %rdi      # 0xc758
movq 0x67c7(%rip), %rsi      # 0xc750
callq 0x2180
movb $0x1, 0x6787(%rip)      # 0xc71c
jmp 0x5cb5
movq 0x6057(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x2f46(%rip), %rsi      # 0x8ef1
leaq 0x2587(%rip), %rdx      # 0x8539
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2150
movb $0x0, 0x675a(%rip)      # 0xc71c
jmp 0x5cb7
movq 0x602a(%rip), %rax      # 0xbff8
movq (%rax), %r14
callq 0x2040
movl (%rax), %ecx
leaq 0x2f7c(%rip), %rsi      # 0x8f5b
leaq 0x2553(%rip), %rdx      # 0x8539
movq %r14, %rdi
xorl %eax, %eax
callq 0x2150
movb $0x0, 0x6725(%rip)      # 0xc71c
andl $0x0, 0x6746(%rip)      # 0xc744
jmp 0x5cb7
movq 0x5fee(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x2f10(%rip), %rsi      # 0x8f24
leaq 0x251e(%rip), %rdx      # 0x8539
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2150
jmp 0x5fbb