// File: /workspace/github_c_makefile_star2_O3/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 0x8648(%rip), %r14      # 0xf748
testl %esi, %esi
setne %cl
movzwl 0x85fe(%rip), %eax      # 0xf70a
cmpl $0x8723, %eax           # imm = 0x8723
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x72a6
cmpl $0x2, %esi
sete %cl
cmpl $0x1200, %eax           # imm = 0x1200
sete %al
andb %cl, %al
cmpb $0x1, %al
je 0x72a6
movabsq $0x686365746c616552, %rax # imm = 0x686365746C616552
cmpq %rax, (%r14)
je 0x71c4
cmpb $0x0, 0x8193(%rip)      # 0xf2e0
jne 0x72f1
movb $0x0, 0x85c2(%rip)      # 0xf71c
xorl %ebx, %ebx
cmpl $0x0, 0x85d9(%rip)      # 0xf73c
jle 0x717b
movq 0x85dc(%rip), %rdi      # 0xf748
callq 0x2030
movl $0x0, 0x85c1(%rip)      # 0xf73c
cmpl $0x0, 0x85be(%rip)      # 0xf740
jle 0x719a
movq 0x85c5(%rip), %rdi      # 0xf750
callq 0x2030
movl $0x0, 0x85a6(%rip)      # 0xf740
testq %rbx, %rbx
je 0x71b5
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2030
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movslq 0x8571(%rip), %rdi      # 0xf73c
cmpl $0x77fd0451, -0x4(%r14,%rdi) # imm = 0x77FD0451
jne 0x730a
addq %r14, %rdi
addq $-0x5, %rdi
callq 0x6e67
movzwl 0x851d(%rip), %ecx      # 0xf70a
cmpl $0x8703, %ecx           # imm = 0x8703
je 0x720e
movzbl %al, %eax
leaq 0x80f1(%rip), %rdx      # 0xf2f0
movzwl (%rdx,%rax,2), %r8d
cmpw %r8w, %cx
jne 0x731e
callq 0x6f0b
testq %rax, %rax
je 0x7153
movq %rax, %rbx
movzwl 0x2(%rax), %r12d
movslq 0x8515(%rip), %r15      # 0xf740
leaq (%r15,%r12), %rdi
movl %edi, 0x850f(%rip)      # 0xf744
callq 0x2190
movq %rax, 0x8517(%rip)      # 0xf758
testq %rax, %rax
je 0x7314
movq %rax, %r13
movl 0x4(%rbx), %esi
addq 0x84f1(%rip), %rsi      # 0xf748
movq %rax, %rdi
movq %r12, %rdx
callq 0x2180
movl 0x8(%r14), %eax
movl %eax, -0x4(%r13,%r12)
testl %r15d, %r15d
je 0x7285
addq %r12, %r13
movq 0x84d6(%rip), %rsi      # 0xf750
movq %r13, %rdi
movq %r15, %rdx
callq 0x2180
movb $0x1, 0x8490(%rip)      # 0xf71c
cmpb $0x0, 0x804d(%rip)      # 0xf2e0
je 0x715c
movl %r15d, %edi
callq 0x34b0
jmp 0x715c
movabsq $0x686365746c616552, %rax # imm = 0x686365746C616552
cmpq %rax, (%r14)
jne 0x72bf
callq 0x3671
jmp 0x715a
movslq 0x847a(%rip), %rdi      # 0xf740
movslq 0x846f(%rip), %rbx      # 0xf73c
addq %rbx, %rdi
movl %edi, 0x846e(%rip)      # 0xf744
callq 0x2190
movq %rax, 0x8476(%rip)      # 0xf758
testq %rax, %rax
jne 0x72fb
callq 0x362d
jmp 0x715a
callq 0x3460
jmp 0x7153
movl %ebx, %edi
movq %r14, %rsi
callq 0x353d
jmp 0x715a
callq 0x3484
jmp 0x715a
callq 0x34f9
jmp 0x715c
movq 0x7cd3(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x2bf5(%rip), %rsi      # 0x9f24
leaq 0x2203(%rip), %rdx      # 0x9539
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2150
movb $0x0, 0x83d6(%rip)      # 0xf71c
jmp 0x715c