// File: /workspace/github_c_makefile_star2_O1/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, %r13
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
je 0x53dc
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movq 0xa384(%rip), %rax      # 0xf770
movq %rax, -0x38(%rbp)
movl 0xa38a(%rip), %eax      # 0xf780
movl %eax, -0x2c(%rbp)
movq 0x9bf8(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movzwl (%rsi), %ecx
leaq 0x42e8(%rip), %rsi      # 0x96f5
leaq 0x4125(%rip), %rdx      # 0x9539
xorl %eax, %eax
callq 0x2150
movl (%rbx), %ecx
cmpl $0x8723ab55, %ecx       # imm = 0x8723AB55
jne 0x5696
movq %r14, -0x50(%rbp)
movzwl 0x4(%rbx), %r12d
movq (%r15), %r8
addq $-0x6, %r8
cmpq %r12, %r8
jne 0x56bc
testq %r12, %r12
je 0x566d
movq %r13, -0x48(%rbp)
leaq 0x6(%rbx), %r14
xorl %r13d, %r13d
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x2c(%rbp), %edi
movq -0x38(%rbp), %r8
movzwl (%r14), %ecx
cmpl $0x43, %ecx
jle 0x5492
cmpl $0x1e6, %ecx            # imm = 0x1E6
je 0x54f9
cmpl $0x15b, %ecx            # imm = 0x15B
je 0x5542
cmpl $0x44, %ecx
je 0x54a4
jmp 0x5597
cmpl $0x18, %ecx
je 0x5572
cmpl $0x3c, %ecx
jne 0x5592
cmpb $0x1, 0xa2d9(%rip)      # 0xf784
jne 0x55a0
movb 0x4(%r8), %al
cmpb $0x70, %al
setae %dl
cmpl $0x44, %ecx
setne %sil
testb %dl, %sil
jne 0x55a0
cmpl $0x3c, %ecx
je 0x54d7
cmpb $0x70, %al
jb 0x55a0
movl %edi, -0x2c(%rbp)
movq %r14, %rdi
movq -0x50(%rbp), %rsi
callq 0x2996
movq -0x38(%rbp), %r8
movl -0x2c(%rbp), %edi
movl $0x4, -0x3c(%rbp)
jmp 0x55a0
testb $0x2, %dil
je 0x55a0
andl $-0x3, %edi
movl %edi, -0x2c(%rbp)
movq 0x9ae8(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movzbl 0xa270(%rip), %ecx      # 0xf78a
leaq 0x4306(%rip), %rsi      # 0x9827
leaq 0x4011(%rip), %rdx      # 0x9539
xorl %eax, %eax
callq 0x2150
movq -0x38(%rbp), %r8
movl -0x2c(%rbp), %edi
movb 0xa24e(%rip), %al       # 0xf78a
movb %al, 0x3(%r14)
jmp 0x55a0
testb $0x1, %dil
je 0x55a0
andl $-0x2, %edi
movl %edi, -0x2c(%rbp)
movzbl 0xa230(%rip), %ecx      # 0xf785
movzbl 0x2(%r14), %r8d
cmpb %r8b, %cl
jne 0x5632
movq %r14, %rdi
movl %ecx, %esi
callq 0x2900
jmp 0x5651
cmpb $0x71, 0x4(%r8)
jne 0x55a0
cmpl $0x0, 0xa1f8(%rip)      # 0xf778
jne 0x55a0
movl %edi, -0x2c(%rbp)
movq %r14, %rdi
callq 0x2872
jmp 0x5626
cmpl $0xc, %ecx
je 0x55c5
cmpb $0x0, 0x9d42(%rip)      # 0xf2e0
jne 0x55ff
movzbl 0x2(%r14), %eax
leaq 0x3(%rax), %rcx
addl %r13d, %ecx
addq %rax, %r14
addq $0x3, %r14
movzwl %cx, %r13d
cmpl %r12d, %r13d
jb 0x546b
jmp 0x565d
movl %edi, -0x2c(%rbp)
movl 0x3(%r14), %ecx
movq 0x9a25(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x41c0(%rip), %rsi      # 0x979d
leaq 0x3f55(%rip), %rdx      # 0x9539
movl %ecx, -0x30(%rbp)
xorl %eax, %eax
callq 0x2150
cmpb $0xc, 0x2(%r14)
jb 0x5651
movq %r14, %rdi
callq 0x2962
jmp 0x5651
movq 0x99f2(%rip), %rax      # 0xeff8
movl %edi, -0x2c(%rbp)
movq (%rax), %rdi
movzbl 0x2(%r14), %r8d
leaq 0x4270(%rip), %rsi      # 0x9888
leaq 0x3f1a(%rip), %rdx      # 0x9539
xorl %eax, %eax
callq 0x2150
movq -0x38(%rbp), %r8
movl -0x2c(%rbp), %edi
jmp 0x55a0
movq 0x99bf(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x418b(%rip), %rsi      # 0x97ce
leaq 0x3eef(%rip), %rdx      # 0x9539
xorl %eax, %eax
callq 0x2150
movl -0x2c(%rbp), %edi
movq -0x38(%rbp), %r8
jmp 0x55a0
testb $0x4, -0x3c(%rbp)
sete %al
movq -0x48(%rbp), %r13
movl -0x30(%rbp), %ecx
jmp 0x5678
movb $0x1, %al
xorl %ecx, %ecx
movl -0x2c(%rbp), %edi
movq -0x38(%rbp), %r8
testb %al, %al
je 0x5685
testb $0x1, 0xa101(%rip)      # 0xf784
jne 0x56e5
testl %edi, %edi
jne 0x577e
movl %ecx, (%r13)
jmp 0x53ca
movq 0x995b(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x4067(%rip), %rsi      # 0x970e
leaq 0x3e8b(%rip), %rdx      # 0x9539
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2150
jmp 0x53ca
movq 0x9935(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x4074(%rip), %rsi      # 0x9741
leaq 0x3e65(%rip), %rdx      # 0x9539
xorl %ebx, %ebx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x2150
jmp 0x53ca
movl %ecx, -0x30(%rbp)
movl %edi, -0x2c(%rbp)
movq %r13, -0x48(%rbp)
movq (%r15), %r12
xorl %eax, %eax
cmpb $0x70, 0x4(%r8)
leaq (%rbx,%r12), %r14
setae %al
leal (,%rax,8), %ecx
addb $0x3c, %cl
movb %cl, (%rbx,%r12)
movb $0x0, 0x1(%rbx,%r12)
movq 0x98dd(%rip), %rcx      # 0xeff8
movq (%rcx), %rdi
leal 0x3c(,%rax,8), %r8d
leaq 0x4180(%rip), %rsi      # 0x98ad
leaq 0x3e05(%rip), %rdx      # 0x9539
xorl %r13d, %r13d
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x2150
movb $0x6, 0x2(%rbx,%r12)
movq -0x50(%rbp), %rcx
movb (%rcx,%r13), %al
movb %al, 0x3(%r14,%r13)
incq %r13
cmpq $0x6, %r13
jne 0x574a
movq (%r15), %rax
leaq 0x9(%rax), %rcx
movq %rcx, (%r15)
addl $0x3, %eax
movb %al, 0x4(%rbx)
movb %ah, 0x5(%rbx)
movq -0x48(%rbp), %r13
movl -0x2c(%rbp), %edi
movl -0x30(%rbp), %ecx
jmp 0x5685
movl %ecx, -0x30(%rbp)
movl %edi, %r12d
andl $0x1, %r12d
movzbl 0x9ff6(%rip), %eax      # 0xf785
addl $0x3, %eax
testl %r12d, %r12d
cmovel %r12d, %eax
addw (%r15), %ax
leal 0x4(%rax), %r14d
movl %edi, -0x2c(%rbp)
testb $0x2, %dil
cmovel %eax, %r14d
movq 0x9845(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x4115(%rip), %rsi      # 0x98d2
leaq 0x3d75(%rip), %rdx      # 0x9539
xorl %eax, %eax
callq 0x2150
addl $-0x6, %r14d
movw %r14w, 0x4(%rbx)
movq (%r15), %r14
addq %rbx, %r14
testl %r12d, %r12d
jne 0x57f3
testb $0x2, -0x2c(%rbp)
jne 0x5808
subq %rbx, %r14
movq %r14, (%r15)
movl -0x30(%rbp), %ecx
jmp 0x568d
leaq -0x58(%rbp), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x2a05
movq (%r12), %r14
jmp 0x57df
movq 0x97e9(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x40e6(%rip), %rsi      # 0x98ff
leaq 0x3d19(%rip), %rdx      # 0x9539
xorl %eax, %eax
callq 0x2150
movw $0x1e6, (%r14)          # imm = 0x1E6
movb $0x1, 0x2(%r14)
movb 0x9f52(%rip), %al       # 0xf78a
movb %al, 0x3(%r14)
addq $0x4, %r14
jmp 0x57e5