// File: /workspace/github_c_makefile_star2_O2/seuscq[P]rtk_hciattach/hciattach_rtk.c
// Function: rtk_parse_config_file
uint8_t *rtk_parse_config_file(RT_U8 *config_buf, size_t *plen,
			       uint8_t bt_addr[6], uint32_t *result)
{

	struct rtk_bt_vendor_config *config =
	    (struct rtk_bt_vendor_config *)config_buf;
	RT_U16 config_len = 0, temp = 0;
	struct rtk_bt_vendor_config_entry *entry = NULL;
	RT_U16 i;
	RT_U32 baudrate = 0;
	uint32_t flags = 0;
	uint32_t add_flags;
#ifdef USE_CUSTOMER_ADDRESS
	uint8_t j = 0;
	struct patch_info *pent = rtk_hw_cfg.patch_ent;
#endif
#ifdef EXTRA_CONFIG_OPTION
	uint8_t *head = config_buf;

	add_flags = config_flags;
#endif

	if (!config || !plen)
		return NULL;

	RS_INFO("Original Cfg len %u", (uint16_t)*plen);
	config_len = le16_to_cpu(config->data_len);
	entry = config->entry;

	if (le32_to_cpu(config->signature) != RTK_VENDOR_CONFIG_MAGIC) {
		RS_ERR("signature magic number(%x) is incorrect",
		     (unsigned int)config->signature);
		return NULL;
	}

	if (config_len != *plen - sizeof(struct rtk_bt_vendor_config)) {
		RS_ERR("config len(%d) is incorrect(%zd)", config_len,
		       *plen - sizeof(struct rtk_bt_vendor_config));
		return NULL;
	}

	for (i = 0; i < config_len;) {

		switch (le16_to_cpu(entry->offset)) {
#ifdef USE_CUSTOMER_ADDRESS
		case 0x003c:
		case 0x0044:
			if (!customer_bdaddr)
				break;
			if (pent->chip_type > CHIP_BEFORE &&
			    le16_to_cpu(entry->offset) != 0x44)
				break;
			if (pent->chip_type <= CHIP_BEFORE &&
			    le16_to_cpu(entry->offset) != 0x3c)
				break;
			for (j = 0; j < entry->entry_len; j++)
				entry->entry_data[j] = bt_addr[j];
			flags |= CONFIG_BTMAC;
			RS_INFO("BT MAC found %02x:%02x:%02x:%02x:%02x:%02x",
				entry->entry_data[5],
				entry->entry_data[4],
				entry->entry_data[3],
				entry->entry_data[2],
				entry->entry_data[1],
				entry->entry_data[0]);
			break;
#endif
		case 0xc:
			{
#ifdef BAUDRATE_4BYTES
				baudrate =
				    get_unaligned_le32(entry->entry_data);
#else
				baudrate =
				    get_unaligned_le16(entry->entry_data);
#endif
				RS_INFO("Config baud : %08x", baudrate);

				/* offset 0x0c - 0x18 */
				if (entry->entry_len >= 12) {
					rtk_hw_cfg.hw_flow_control = (entry->entry_data[12] & 0x4) ? 1 : 0;	//0x18 byte bit2
					RS_INFO("hw flow control: %u",
						rtk_hw_cfg.hw_flow_control);
				}
				break;
			}
#ifdef EXTRA_CONFIG_OPTION
		case 0x015b:
			if (!(add_flags & CONFIG_TXPOWER))
				break;
			add_flags &= ~CONFIG_TXPOWER;
			if (txpower_len != entry->entry_len) {
				RS_ERR("invalid tx power cfg len %u, %u",
				       txpower_len, entry->entry_len);
				break;
			}
			memcpy(entry->entry_data, txpower_cfg, txpower_len);
			RS_INFO("Replace Tx power Cfg %02x %02x %02x %02x",
				txpower_cfg[0], txpower_cfg[1], txpower_cfg[2],
				txpower_cfg[3]);
			break;
		case 0x01e6:
			if (!(add_flags & CONFIG_XTAL))
				break;
			add_flags &= ~CONFIG_XTAL;
			RS_INFO("Replace XTAL Cfg 0x%02x", xtal_cfg);
			entry->entry_data[0] = xtal_cfg;
			break;
#endif
		case 0x0018:
			if (pent->chip_type == CHIP_8723DS &&
			    rtk_hw_cfg.proto == HCI_UART_H4) {
				if (entry->entry_data[0] & (1 << 2))
					rtk_hw_cfg.hw_flow_control = 1;
				RS_INFO("8723DSH4: hw flow control %u",
					rtk_hw_cfg.hw_flow_control);
				if (entry->entry_data[0] & 0x01) {
					rtk_hw_cfg.parity_en = 1;
					if (entry->entry_data[0] & 0x02)
						rtk_hw_cfg.parity_even = 1;
					else
						rtk_hw_cfg.parity_even = 0;
				}
				RS_INFO("8723DSH4: parity %u, even %u",
					rtk_hw_cfg.parity_en,
					rtk_hw_cfg.parity_even);
			}
			break;
		default:
			RS_DBG("cfg offset (%04x),length (%02x)", entry->offset,
			       entry->entry_len);
			break;
		}
		temp = entry->entry_len +
		       sizeof(struct rtk_bt_vendor_config_entry);
		i += temp;
		entry = (struct rtk_bt_vendor_config_entry *)((RT_U8 *)entry +
							      temp);
	}

#ifdef USE_CUSTOMER_ADDRESS
	if (!(flags & CONFIG_BTMAC) && customer_bdaddr) {
		uint8_t *b;

		b = config_buf + *plen;
		if (pent->chip_type > CHIP_BEFORE) {
			b[0] = 0x44;
			b[1] = 0x00;
		} else {
			b[0] = 0x3c;
			b[1] = 0x00;
		}
		RS_INFO("add bdaddr sec, offset %02x%02x", b[1], b[0]);
		b[2] = 6;
		for (j = 0; j < 6; j++)
			b[3 + j] = bt_addr[j];

		*plen += 9;
		config_len += 9;
		temp = *plen - 6;

		config_buf[4] = (temp & 0xff);
		config_buf[5] = ((temp >> 8) & 0xff);
	}
#endif

#ifdef EXTRA_CONFIG_OPTION
	temp = *plen;
	if (add_flags & CONFIG_TXPOWER)
		temp += (2 + 1 + txpower_len);
	if ((add_flags & CONFIG_XTAL))
		temp += (2 + 1 + 1);

	if (add_flags) {
		RS_INFO("Add extra configs");
		config_buf = head;
		temp -= sizeof(struct rtk_bt_vendor_config);
		config_buf[4] = (temp & 0xff);
		config_buf[5] = ((temp >> 8) & 0xff);
		config_buf += *plen;
		if (add_flags & CONFIG_TXPOWER) {
			RS_INFO("Add Tx power Cfg");
			*config_buf++ = 0x5b;
			*config_buf++ = 0x01;
			*config_buf++ = txpower_len;
			memcpy(config_buf, txpower_cfg, txpower_len);
			config_buf += txpower_len;
		}
		if ((add_flags & CONFIG_XTAL)) {
			RS_INFO("Add XTAL Cfg");
			*config_buf++ = 0xe6;
			*config_buf++ = 0x01;
			*config_buf++ = 1;
			*config_buf++ = xtal_cfg;
		}
		*plen = config_buf - head;
		config_buf = head;
	}
#endif

done:
	*result = baudrate;
	return config_buf;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, %r14
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
je 0x37e4
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %rbx
movq 0x8f7c(%rip), %rax      # 0xc770
movq %rax, -0x38(%rbp)
movl 0x8f82(%rip), %eax      # 0xc780
movl %eax, -0x2c(%rbp)
movq 0x87f0(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movzwl (%rsi), %ecx
leaq 0x4ee0(%rip), %rsi      # 0x86f5
leaq 0x4d1d(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
movl (%rbx), %ecx
cmpl $0x8723ab55, %ecx       # imm = 0x8723AB55
jne 0x3be8
movq %r14, -0x58(%rbp)
movzwl 0x4(%rbx), %r12d
movq (%r15), %r8
addq $-0x6, %r8
cmpq %r12, %r8
jne 0x3c0e
movq %r15, -0x50(%rbp)
leaq 0x6(%rbx), %r14
xorl %eax, %eax
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x2c(%rbp), %esi
movq -0x38(%rbp), %rdi
movzwl %ax, %r15d
cmpl %r12d, %r15d
jae 0x3bbd
movzwl (%r14), %ecx
cmpl $0x18, %ecx
je 0x39dd
cmpl $0x3c, %ecx
je 0x38ea
cmpl $0x44, %ecx
je 0x38ea
cmpl $0x15b, %ecx            # imm = 0x15B
je 0x395b
cmpl $0x1e6, %ecx            # imm = 0x1E6
je 0x390f
cmpl $0xc, %ecx
je 0x3a31
cmpb $0x0, 0x8a2f(%rip)      # 0xc2e0
je 0x3ba5
movq 0x873a(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movzbl 0x2(%r14), %r8d
movl %esi, -0x2c(%rbp)
leaq 0x4fb8(%rip), %rsi      # 0x8888
leaq 0x4c62(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
jmp 0x3ba5
cmpb $0x1, 0x8e93(%rip)      # 0xc784
jne 0x3ba5
cmpb $0x70, 0x4(%rdi)
jb 0x3a0d
cmpl $0x44, %ecx
jne 0x3ba5
jmp 0x3a16
testb $0x2, %sil
je 0x3ba5
andl $-0x3, %esi
movl %esi, -0x2c(%rbp)
movq 0x86d2(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movzbl 0x8e5a(%rip), %ecx      # 0xc78a
leaq 0x4ef0(%rip), %rsi      # 0x8827
leaq 0x4bfb(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
movb 0x8e38(%rip), %al       # 0xc78a
movb %al, 0x3(%r14)
jmp 0x3ba5
testb $0x1, %sil
je 0x3ba5
andl $-0x2, %esi
movl %esi, -0x2c(%rbp)
movzbl 0x8e13(%rip), %ecx      # 0xc785
movzbl 0x2(%r14), %r8d
cmpb %r8b, %cl
jne 0x3b7f
leaq 0x3(%r14), %rdi
movl %ecx, %edx
leaq 0x8df9(%rip), %rsi      # 0xc786
callq 0x2180
movq 0x865f(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movzbl 0x8de3(%rip), %ecx      # 0xc786
movzbl 0x8ddc(%rip), %r8d      # 0xc787
movzbl 0x8dd5(%rip), %r9d      # 0xc788
movzbl 0x8dce(%rip), %r10d     # 0xc789
subq $0x8, %rsp
leaq 0x4e33(%rip), %rsi      # 0x87f9
leaq 0x4b6c(%rip), %rdx      # 0x8539
xorl %eax, %eax
pushq %r10
callq 0x2150
popq %rcx
popq %rdx
jmp 0x3b9e
cmpb $0x71, 0x4(%rdi)
jne 0x3ba5
cmpl $0x0, 0x8d8a(%rip)      # 0xc778
jne 0x3ba5
testb $0x4, 0x3(%r14)
movl %esi, -0x2c(%rbp)
jne 0x3aff
movl 0x8d1c(%rip), %ecx      # 0xc724
jmp 0x3b0c
cmpl $0x3c, %ecx
jne 0x3ba5
movzbl 0x2(%r14), %eax
xorl %ecx, %ecx
cmpq %rax, %rcx
jae 0x3a9a
movb (%r13,%rcx), %dl
movb %dl, 0x3(%r14,%rcx)
incq %rcx
jmp 0x3a1d
movl %esi, -0x2c(%rbp)
movl 0x3(%r14), %ecx
movq 0x85b9(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4d54(%rip), %rsi      # 0x879d
leaq 0x4ae9(%rip), %rdx      # 0x8539
movl %ecx, -0x3c(%rbp)
xorl %eax, %eax
callq 0x2150
cmpb $0xc, 0x2(%r14)
jb 0x3b9e
movzbl 0xf(%r14), %ecx
shrl $0x2, %ecx
andl $0x1, %ecx
movl %ecx, 0x8cae(%rip)      # 0xc724
movq 0x857b(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4d2e(%rip), %rsi      # 0x87b5
leaq 0x4aab(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
jmp 0x3b9e
movq 0x8557(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movzbl 0x8(%r14), %ecx
movzbl 0x7(%r14), %r8d
movzbl 0x6(%r14), %r9d
movzbl 0x5(%r14), %eax
movq %rax, -0x48(%rbp)
movzbl 0x4(%r14), %r11d
movzbl 0x3(%r14), %r10d
subq $0x8, %rsp
movl %esi, -0x2c(%rbp)
leaq 0x4c99(%rip), %rsi      # 0x876d
leaq 0x4a5e(%rip), %rdx      # 0x8539
xorl %eax, %eax
pushq %r10
pushq %r11
pushq -0x48(%rbp)
callq 0x2150
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
addq $0x20, %rsp
pushq $0x4
popq %rax
movl %eax, -0x48(%rbp)
jmp 0x3ba5
movl $0x1, 0x8c1b(%rip)      # 0xc724
pushq $0x1
popq %rcx
movq 0x84e5(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4d27(%rip), %rsi      # 0x8844
leaq 0x4a15(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
testb $0x1, 0x3(%r14)
movl 0x8bf1(%rip), %r8d      # 0xc728
je 0x3b64
andl $0xffff0000, %r8d       # imm = 0xFFFF0000
incl %r8d
movl %r8d, 0x8bde(%rip)      # 0xc728
movb 0x3(%r14), %al
andb $0x2, %al
shrb %al
movzbl %al, %r8d
shll $0x10, %r8d
incl %r8d
movl %r8d, 0x8bc4(%rip)      # 0xc728
movq 0x848d(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movzwl %r8w, %ecx
shrl $0x10, %r8d
leaq 0x4ce9(%rip), %rsi      # 0x8866
jmp 0x3b90
movq 0x8472(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4c3e(%rip), %rsi      # 0x87ce
leaq 0x49a2(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
movl -0x2c(%rbp), %esi
movq -0x38(%rbp), %rdi
movzbl 0x2(%r14), %ecx
leaq 0x3(%rcx), %rax
addl %r15d, %eax
addq %rcx, %r14
addq $0x3, %r14
jmp 0x3869
testb $0x4, -0x48(%rbp)
movq -0x50(%rbp), %rdx
jne 0x3bd0
testb $0x1, 0x8bb6(%rip)      # 0xc784
jne 0x3c37
testl %esi, %esi
movq -0x58(%rbp), %r12
jne 0x3cc4
movl -0x3c(%rbp), %eax
movl %eax, (%r12)
jmp 0x37d2
movq 0x8409(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4b15(%rip), %rsi      # 0x870e
leaq 0x4939(%rip), %rdx      # 0x8539
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2150
jmp 0x37d2
movq 0x83e3(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4b22(%rip), %rsi      # 0x8741
leaq 0x4913(%rip), %rdx      # 0x8539
xorl %ebx, %ebx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x2150
jmp 0x37d2
movl %esi, -0x2c(%rbp)
movq (%rdx), %r15
xorl %eax, %eax
cmpb $0x70, 0x4(%rdi)
leaq (%rbx,%r15), %r14
setae %al
leal (,%rax,8), %ecx
addb $0x3c, %cl
movb %cl, (%rbx,%r15)
movb $0x0, 0x1(%rbx,%r15)
movq 0x8393(%rip), %rcx      # 0xbff8
movq (%rcx), %rdi
leal 0x3c(,%rax,8), %r8d
leaq 0x4c36(%rip), %rsi      # 0x88ad
leaq 0x48bb(%rip), %rdx      # 0x8539
xorl %r12d, %r12d
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x2150
movb $0x6, 0x2(%rbx,%r15)
cmpq $0x6, %r12
je 0x3ca5
movb (%r13,%r12), %al
movb %al, 0x3(%r14,%r12)
incq %r12
jmp 0x3c90
movq -0x50(%rbp), %rdx
movq (%rdx), %rax
leaq 0x9(%rax), %rcx
movq %rcx, (%rdx)
addl $0x3, %eax
movb %al, 0x4(%rbx)
movb %ah, 0x5(%rbx)
movl -0x2c(%rbp), %esi
jmp 0x3bd0
movq %rdx, %r13
movl %esi, %r15d
andl $0x1, %r15d
movzbl 0x8ab0(%rip), %eax      # 0xc785
addl $0x3, %eax
testl %r15d, %r15d
cmovel %r15d, %eax
addw (%rdx), %ax
leal 0x4(%rax), %r14d
movl %esi, -0x2c(%rbp)
testb $0x2, %sil
cmovel %eax, %r14d
movq 0x8300(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4bd0(%rip), %rsi      # 0x88d2
leaq 0x4830(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
addl $-0x6, %r14d
movw %r14w, 0x4(%rbx)
movq (%r13), %r14
addq %rbx, %r14
testl %r15d, %r15d
jne 0x3d3a
testb $0x2, -0x2c(%rbp)
movq -0x50(%rbp), %r15
jne 0x3d8b
subq %rbx, %r14
movq %r14, (%r15)
jmp 0x3bdc
movq 0x82b7(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4b9e(%rip), %rsi      # 0x88e9
leaq 0x47e7(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
movw $0x15b, (%r14)          # imm = 0x15B
movzbl 0x8a1e(%rip), %r12d     # 0xc785
leaq 0x3(%r14), %rdi
movb %r12b, 0x2(%r14)
leaq 0x8a10(%rip), %rsi      # 0xc786
movq %r12, %rdx
callq 0x2180
addq %r12, %r14
addq $0x3, %r14
movq -0x58(%rbp), %r12
jmp 0x3d25
movq 0x8266(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4b63(%rip), %rsi      # 0x88ff
leaq 0x4796(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
movw $0x1e6, (%r14)          # imm = 0x1E6
movb $0x1, 0x2(%r14)
movb 0x89cf(%rip), %al       # 0xc78a
movb %al, 0x3(%r14)
addq $0x4, %r14
jmp 0x3d2f