// File: /workspace/github_c_makefile_star2_O3/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 0x53da
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 0xa386(%rip), %rax      # 0xf770
movq %rax, -0x38(%rbp)
movl 0xa38c(%rip), %eax      # 0xf780
movl %eax, -0x2c(%rbp)
movq 0x9bfa(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movzwl (%rsi), %ecx
leaq 0x42ea(%rip), %rsi      # 0x96f5
leaq 0x4127(%rip), %rdx      # 0x9539
xorl %eax, %eax
callq 0x2150
movl (%rbx), %ecx
cmpl $0x8723ab55, %ecx       # imm = 0x8723AB55
jne 0x568b
movq %r14, -0x50(%rbp)
movzwl 0x4(%rbx), %r12d
movq (%r15), %r8
addq $-0x6, %r8
cmpq %r12, %r8
jne 0x56b1
testq %r12, %r12
je 0x5662
movq %r13, -0x48(%rbp)
leaq 0x6(%rbx), %r14
xorl %r13d, %r13d
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x2c(%rbp), %edx
movq -0x38(%rbp), %rsi
movzwl (%r14), %ecx
cmpl $0x43, %ecx
jle 0x5490
cmpl $0x1e6, %ecx            # imm = 0x1E6
je 0x54c7
cmpl $0x15b, %ecx            # imm = 0x15B
je 0x5512
cmpl $0x44, %ecx
je 0x54a2
jmp 0x556e
cmpl $0x18, %ecx
je 0x5545
cmpl $0x3c, %ecx
jne 0x5569
cmpb $0x1, 0xa2db(%rip)      # 0xf784
jne 0x5632
cmpb $0x70, 0x4(%rsi)
jb 0x55ae
cmpl $0x44, %ecx
jne 0x5632
jmp 0x55b3
testb $0x2, %dl
je 0x5632
andl $-0x3, %edx
movl %edx, -0x2c(%rbp)
movq 0x9b1b(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movzbl 0xa2a3(%rip), %ecx      # 0xf78a
leaq 0x4339(%rip), %rsi      # 0x9827
leaq 0x4044(%rip), %rdx      # 0x9539
xorl %eax, %eax
callq 0x2150
movq -0x38(%rbp), %rsi
movl -0x2c(%rbp), %edx
movb 0xa281(%rip), %al       # 0xf78a
movb %al, 0x3(%r14)
jmp 0x5632
testb $0x1, %dl
je 0x5632
andl $-0x2, %edx
movl %edx, -0x2c(%rbp)
movzbl 0xa25d(%rip), %ecx      # 0xf785
movzbl 0x2(%r14), %r8d
cmpb %r8b, %cl
jne 0x560c
movq %r14, %rdi
movl %ecx, %esi
callq 0x28f6
jmp 0x562b
cmpb $0x71, 0x4(%rsi)
jne 0x5632
cmpl $0x0, 0xa222(%rip)      # 0xf778
jne 0x5632
movq %r14, %rdi
movl %edx, -0x2c(%rbp)
callq 0x2857
jmp 0x55a2
cmpl $0xc, %ecx
je 0x55d2
cmpb $0x0, 0x9d6b(%rip)      # 0xf2e0
je 0x5632
movq 0x9a76(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movzbl 0x2(%r14), %r8d
leaq 0x42f7(%rip), %rsi      # 0x9888
movl %edx, -0x2c(%rbp)
leaq 0x3f9e(%rip), %rdx      # 0x9539
xorl %eax, %eax
callq 0x2150
movq -0x38(%rbp), %rsi
movl -0x2c(%rbp), %edx
jmp 0x5632
cmpl $0x3c, %ecx
jne 0x5632
movq %r14, %rdi
movq -0x50(%rbp), %rsi
movl %edx, -0x2c(%rbp)
callq 0x298c
movq -0x38(%rbp), %rsi
movl -0x2c(%rbp), %edx
movl $0x4, -0x3c(%rbp)
jmp 0x5632
movl %edx, -0x2c(%rbp)
movl 0x3(%r14), %ecx
movq 0x9a18(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x41b3(%rip), %rsi      # 0x979d
leaq 0x3f48(%rip), %rdx      # 0x9539
movl %ecx, -0x30(%rbp)
xorl %eax, %eax
callq 0x2150
cmpb $0xc, 0x2(%r14)
jb 0x562b
movq %r14, %rdi
callq 0x2958
jmp 0x562b
movq 0x99e5(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x41b1(%rip), %rsi      # 0x97ce
leaq 0x3f15(%rip), %rdx      # 0x9539
xorl %eax, %eax
callq 0x2150
movl -0x2c(%rbp), %edx
movq -0x38(%rbp), %rsi
movzbl 0x2(%r14), %eax
leaq 0x3(%rax), %rcx
addl %r13d, %ecx
addq %rax, %r14
addq $0x3, %r14
movzwl %cx, %r13d
cmpl %r12d, %r13d
jb 0x5469
testb $0x4, -0x3c(%rbp)
sete %al
movq -0x48(%rbp), %r13
movl -0x30(%rbp), %ecx
jmp 0x566d
movb $0x1, %al
xorl %ecx, %ecx
movl -0x2c(%rbp), %edx
movq -0x38(%rbp), %rsi
testb %al, %al
je 0x567a
testb $0x1, 0xa10c(%rip)      # 0xf784
jne 0x56da
testl %edx, %edx
jne 0x5772
movl %ecx, (%r13)
jmp 0x53c8
movq 0x9966(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x4072(%rip), %rsi      # 0x970e
leaq 0x3e96(%rip), %rdx      # 0x9539
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2150
jmp 0x53c8
movq 0x9940(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x407f(%rip), %rsi      # 0x9741
leaq 0x3e70(%rip), %rdx      # 0x9539
xorl %ebx, %ebx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x2150
jmp 0x53c8
movl %ecx, -0x30(%rbp)
movl %edx, -0x2c(%rbp)
movq %r13, -0x48(%rbp)
movq (%r15), %r12
xorl %eax, %eax
cmpb $0x70, 0x4(%rsi)
leaq (%rbx,%r12), %r14
setae %al
leal (,%rax,8), %ecx
addb $0x3c, %cl
movb %cl, (%rbx,%r12)
movb $0x0, 0x1(%rbx,%r12)
movq 0x98e9(%rip), %rcx      # 0xeff8
movq (%rcx), %rdi
leal 0x3c(,%rax,8), %r8d
leaq 0x418c(%rip), %rsi      # 0x98ad
leaq 0x3e11(%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 0x573e
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), %edx
movl -0x30(%rbp), %ecx
jmp 0x567a
movl %ecx, -0x30(%rbp)
movl %edx, %r12d
andl $0x1, %r12d
movzbl 0xa002(%rip), %eax      # 0xf785
addl $0x3, %eax
testl %r12d, %r12d
cmovel %r12d, %eax
addw (%r15), %ax
leal 0x4(%rax), %r14d
movl %edx, -0x2c(%rbp)
testb $0x2, %dl
cmovel %eax, %r14d
movq 0x9852(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x4122(%rip), %rsi      # 0x98d2
leaq 0x3d82(%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 0x57e6
testb $0x2, -0x2c(%rbp)
jne 0x57fb
subq %rbx, %r14
movq %r14, (%r15)
movl -0x30(%rbp), %ecx
jmp 0x5682
leaq -0x58(%rbp), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x29fa
movq (%r12), %r14
jmp 0x57d2
movq 0x97f6(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x40f3(%rip), %rsi      # 0x98ff
leaq 0x3d26(%rip), %rdx      # 0x9539
xorl %eax, %eax
callq 0x2150
movw $0x1e6, (%r14)          # imm = 0x1E6
movb $0x1, 0x2(%r14)
movb 0x9f5f(%rip), %al       # 0xf78a
movb %al, 0x3(%r14)
addq $0x4, %r14
jmp 0x57d8