// File: /workspace/github_c_makefile_star2_O2/seuscq[P]rtk_hciattach/hciattach_rtk.c
// Function: rtk_get_bt_config
int rtk_get_bt_config(struct btrtl_info *btrtl, uint8_t **config_buf,
		RT_U32 *config_baud_rate)
{
	char bt_config_file_name[PATH_MAX] = { 0 };
	RT_U8 *bt_addr_temp = NULL;
	uint8_t bt_addr[6] = { 0x00, 0xe0, 0x4c, 0x88, 0x88, 0x88 };
	struct stat st;
	size_t filelen;
	size_t tlength;
	int fd;
	int ret = 0;
	int i = 0;

#ifdef USE_CUSTOMER_ADDRESS
#define BDADDR_STRING_LEN	17
	size_t size;
	size_t result;
	uint8_t tbuf[BDADDR_STRING_LEN + 1];
	char *str;

	if (stat(BT_ADDR_FILE, &st) < 0) {
		RS_INFO("Couldnt access customer BT MAC file %s",
		        BT_ADDR_FILE);

		/* for (i = 0; i < 6; i++)
		 * 	rtk_get_ram_addr(&bt_addr[i]);
		 * rtk_write_btmac2file(bt_addr);
		 */
		goto GET_CONFIG;
	}

	size = st.st_size;
	/* Only read the first 17-byte if the file length is larger */
	if (size > BDADDR_STRING_LEN)
		size = BDADDR_STRING_LEN;

	fd = open(BT_ADDR_FILE, O_RDONLY);
	if (fd == -1) {
		RS_ERR("Couldnt open BT MAC file %s, %s", BT_ADDR_FILE,
		       strerror(errno));
	} else {
		memset(tbuf, 0, sizeof(tbuf));
		result = read(fd, tbuf, size);
		close(fd);
		if (result == -1) {
			RS_ERR("Couldnt read BT MAC file %s, err %s",
			       BT_ADDR_FILE, strerror(errno));
			goto GET_CONFIG;
		}

		if (bachk(tbuf) < 0) {
			goto GET_CONFIG;
		}

		str = tbuf;
		for (i = 5; i >= 0; i--) {
			bt_addr[i] = (uint8_t)strtoul(str, NULL, 16);
			str += 3;
		}

		/*reserve LAP addr from 0x9e8b00 to 0x9e8b3f, change to 0x008b** */
		if (0x9e == bt_addr[3] && 0x8b == bt_addr[4]
		    && (bt_addr[5] <= 0x3f)) {
			bt_addr[3] = 0x00;
		}

		RS_DBG("BT MAC is %02x:%02x:%02x:%02x:%02x:%02x",
		       bt_addr[5], bt_addr[4],
		       bt_addr[3], bt_addr[2],
		       bt_addr[1], bt_addr[0]);
		customer_bdaddr = 1;
	}
#endif

GET_CONFIG:
	//ret = sprintf(bt_config_file_name, BT_CONFIG_DIRECTORY "rtlbt_config");
	ret = sprintf(bt_config_file_name, "%s", BT_CONFIG_DIRECTORY);
	strcat(bt_config_file_name, btrtl->patch_ent->config_file);
	if (stat(bt_config_file_name, &st) < 0) {
		RS_ERR("can't access bt config file:%s, errno:%d\n",
		       bt_config_file_name, errno);
		return -1;
	}

	filelen = st.st_size;

	if ((fd = open(bt_config_file_name, O_RDONLY)) < 0) {
		perror("Can't open bt config file");
		return -1;
	}

	tlength = filelen;
#ifdef USE_CUSTOMER_ADDRESS
	tlength += 9;
#endif

#ifdef EXTRA_CONFIG_OPTION
	config_file_proc(EXTRA_CONFIG_FILE);
	if (!xtalset_supported())
		config_flags &= ~CONFIG_XTAL;
	if (config_flags & CONFIG_TXPOWER)
		tlength += 7;
	if (config_flags & CONFIG_XTAL)
		tlength += 4;
#endif

	if ((*config_buf = malloc(tlength)) == NULL) {
		RS_DBG("Couldnt malloc buffer for config (%zd)\n", tlength);
		close(fd);
		return -1;
	}
	//we may need to parse this config file.
	//for easy debug, only get need data.

	if (read(fd, *config_buf, filelen) < (ssize_t) filelen) {
		perror("Can't load bt config file");
		free(*config_buf);
		close(fd);
		return -1;
	}

	*config_buf = rtk_parse_config_file(*config_buf, &filelen, bt_addr,
					    config_baud_rate);
	util_hexdump((const uint8_t *)*config_buf, filelen);
	RS_INFO("Cfg length %u", (uint16_t)filelen);
	RS_DBG("Get config baud rate from config file:%x",
	       (unsigned int)*config_baud_rate);

	close(fd);
	return filelen;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x12e8, %rsp           # imm = 0x12E8
movq %rdx, %r13
movq %rsi, -0x68(%rbp)
movq %rdi, %rbx
leaq -0x1300(%rbp), %rdi
movl $0xff0, %edx            # imm = 0xFF0
xorl %esi, %esi
callq 0x20d0
movw $0x8888, -0x2c(%rbp)    # imm = 0x8888
movl $0x884ce000, -0x30(%rbp) # imm = 0x884CE000
leaq 0x4a9c(%rip), %rdi      # 0x8911
leaq -0x110(%rbp), %rsi
callq 0x2160
testl %eax, %eax
js 0x42e7
movq -0xe0(%rbp), %r15
leaq 0x4a7a(%rip), %rdi      # 0x8911
xorl %esi, %esi
xorl %eax, %eax
callq 0x2200
cmpl $-0x1, %eax
je 0x4345
movl %eax, %r14d
cmpq $0x11, %r15
pushq $0x11
popq %rdx
cmovbq %r15, %rdx
xorps %xmm0, %xmm0
leaq -0x80(%rbp), %rsi
movaps %xmm0, (%rsi)
andw $0x0, 0x10(%rsi)
movl %eax, %edi
callq 0x2110
movq %rax, %r15
movl %r14d, %edi
callq 0x2100
cmpq $-0x1, %r15
je 0x4375
leaq -0x80(%rbp), %r14
movq %r14, %rdi
callq 0x3dc8
testl %eax, %eax
js 0x3f61
pushq $0x5
popq %r12
pushq $0x10
popq %r15
testl %r12d, %r12d
js 0x3f1c
movq %r14, %rdi
xorl %esi, %esi
movl %r15d, %edx
callq 0x2250
movl %r12d, %ecx
movb %al, -0x30(%rbp,%rcx)
addq $0x3, %r14
decl %r12d
jmp 0x3efa
movb -0x2d(%rbp), %al
movb -0x2c(%rbp), %cl
movl %eax, %edx
xorb $-0x62, %dl
movl %ecx, %esi
xorb $-0x75, %sil
orb %dl, %sil
sete %sil
movb -0x2b(%rbp), %dl
cmpb $0x40, %dl
setb %dil
andb %sil, %dil
cmpb $0x1, %dil
jne 0x3f4d
movb $0x0, -0x2d(%rbp)
xorl %eax, %eax
cmpb $0x0, 0x838c(%rip)      # 0xc2e0
jne 0x44c0
movb $0x1, 0x8823(%rip)      # 0xc784
movups 0x4a68(%rip), %xmm0     # 0x89d0
leaq -0x1310(%rbp), %r14
movaps %xmm0, (%r14)
movabsq $0x2f74626c74722f, %rax # imm = 0x2F74626C74722F
movq %rax, 0xd(%r14)
movq 0x90(%rbx), %rax
movq 0x18(%rax), %rsi
movq %r14, %rdi
callq 0x2270
leaq -0x110(%rbp), %rsi
movq %r14, %rdi
callq 0x2160
testl %eax, %eax
js 0x4312
movq -0xe0(%rbp), %rbx
movq %rbx, -0x50(%rbp)
leaq -0x1310(%rbp), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x2200
testl %eax, %eax
js 0x4364
movl %eax, %r15d
leaq 0x4a5c(%rip), %rdi      # 0x8a34
xorl %esi, %esi
xorl %eax, %eax
callq 0x2200
cmpl $-0x1, %eax
je 0x43b2
movq %r13, -0x48(%rbp)
leaq -0x310(%rbp), %r14
movl $0x100, %edx            # imm = 0x100
movl %eax, -0x34(%rbp)
movl %eax, %edi
movq %r14, %rsi
callq 0x2110
movq %rax, %r13
testl %r13d, %r13d
je 0x4490
cmpl $-0x1, %r13d
je 0x4450
movl %r15d, -0x54(%rbp)
testl %r13d, %r13d
jle 0x425f
movq %r14, %rdi
pushq $0xa
popq %rsi
callq 0x20b0
testq %rax, %rax
je 0x425f
movq %rax, %r15
movb $0x0, (%rax)
movl %r15d, %r12d
subl %r14d, %r12d
incl %r12d
movzbl (%r14), %eax
cmpl $0x20, %eax
je 0x405b
cmpl $0x9, %eax
jne 0x4063
incq %r14
decl %r12d
jmp 0x404d
cmpb $0x23, %al
je 0x406c
testl %r12d, %r12d
jg 0x4077
incq %r15
subl %r12d, %r13d
movq %r15, %r14
jmp 0x4021
movq %rbx, -0x60(%rbp)
movq 0x7f76(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x44a5(%rip), %rsi      # 0x8531
leaq 0x44a6(%rip), %rdx      # 0x8539
movq %r14, %rcx
xorl %eax, %eax
callq 0x2150
xorl %ebx, %ebx
cmpq $0x20, %rbx
je 0x410f
leaq 0x1(%r14), %rax
movzbl (%r14), %ecx
cmpl $0x9, %ecx
je 0x40bc
cmpl $0x20, %ecx
je 0x40bc
cmpl $0x2c, %ecx
jne 0x40c4
incq %r14
incq %rax
jmp 0x40a9
testl %ecx, %ecx
je 0x40f5
movq %r14, -0x210(%rbp,%rbx,8)
incq %rbx
movq %rax, %r14
movzbl -0x1(%r14), %eax
testl %eax, %eax
je 0x40f5
cmpl $0x9, %eax
je 0x40ee
cmpl $0x20, %eax
je 0x40ee
incq %r14
jmp 0x40d6
movb $0x0, -0x1(%r14)
jmp 0x409f
movl %ebx, %eax
andq $0x0, -0x210(%rbp,%rax,8)
cmpq $0x4, %rbx
jge 0x4114
movq -0x60(%rbp), %rbx
jmp 0x406c
pushq $0x20
popq %rax
movl %eax, %ebx
movq -0x210(%rbp), %rdi
xorl %esi, %esi
pushq $0x10
popq %r14
movl %r14d, %edx
callq 0x2250
movq %rax, -0x40(%rbp)
movq -0x208(%rbp), %rdi
xorl %esi, %esi
movl %r14d, %edx
callq 0x2250
movq %rax, %r14
shlq $0x8, %r14
orq -0x40(%rbp), %r14
movq 0x7ea8(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x51a6(%rip), %rsi      # 0x9300
leaq 0x43d8(%rip), %rdx      # 0x8539
movq %r14, %rcx
xorl %eax, %eax
callq 0x2150
movq -0x200(%rbp), %rdi
xorl %esi, %esi
pushq $0x10
popq %rdx
callq 0x2250
movq %rax, %rcx
movq %rax, -0x40(%rbp)
movl $0xff, %eax
andl %eax, %ecx
addl $0xfd, %ebx
andl %eax, %ebx
cmpl %ebx, %ecx
jne 0x4214
cmpq $0x15b, %r14            # imm = 0x15B
jne 0x41d2
cmpl $0x4, %ecx
ja 0x41d2
movzbl -0x40(%rbp), %ebx
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0x41fe
movq -0x1f8(%rbp,%r14,8), %rdi
xorl %esi, %esi
pushq $0x10
popq %rdx
callq 0x2250
leaq 0x85bd(%rip), %rcx      # 0xc786
movb %al, (%r14,%rcx)
incq %r14
jmp 0x41ab
cmpq $0x1e6, %r14            # imm = 0x1E6
jne 0x4238
movq -0x1f8(%rbp), %rdi
xorl %esi, %esi
pushq $0x10
popq %rdx
callq 0x2250
movb %al, 0x8598(%rip)       # 0xc78a
orb $0x2, 0x8587(%rip)      # 0xc780
jmp 0x4106
movq -0x40(%rbp), %rax
movb %al, 0x857d(%rip)       # 0xc785
orb $0x1, 0x8571(%rip)      # 0xc780
jmp 0x4106
movq 0x7ddd(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x50fa(%rip), %rsi      # 0x931f
leaq 0x430d(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
jmp 0x4106
movq 0x7db9(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x50f0(%rip), %rsi      # 0x9339
leaq 0x42e9(%rip), %rdx      # 0x8539
movq %r14, %rcx
xorl %eax, %eax
callq 0x2150
jmp 0x4106
movl -0x34(%rbp), %edi
callq 0x2100
movq -0x48(%rbp), %r13
movl -0x54(%rbp), %r15d
movq 0x84fa(%rip), %rax      # 0xc770
movb 0x4(%rax), %cl
addb $-0x73, %cl
movl 0x84fe(%rip), %eax      # 0xc780
cmpb $-0x4, %cl
movq -0x68(%rbp), %r14
ja 0x4294
andl $-0x3, %eax
movl %eax, 0x84ec(%rip)      # 0xc780
leaq 0x10(%rbx), %rcx
addq $0x9, %rbx
testb $0x1, %al
cmovneq %rcx, %rbx
andl $0x2, %eax
leaq (%rbx,%rax,2), %rbx
movq %rbx, %rdi
callq 0x2190
movq %rax, (%r14)
testq %rax, %rax
jne 0x43f5
cmpb $0x0, 0x801c(%rip)      # 0xc2e0
jne 0x4429
movl %r15d, %edi
callq 0x2100
pushq $-0x1
popq %rax
addq $0x12e8, %rsp           # imm = 0x12E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x7d0a(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4625(%rip), %rsi      # 0x891d
leaq 0x423a(%rip), %rdx      # 0x8539
leaq 0x460b(%rip), %rcx      # 0x8911
xorl %eax, %eax
callq 0x2150
jmp 0x3f61
movq 0x7cdf(%rip), %rax      # 0xbff8
movq (%rax), %rbx
callq 0x2040
movl (%rax), %r8d
leaq 0x46ba(%rip), %rsi      # 0x89e5
leaq 0x4207(%rip), %rdx      # 0x8539
leaq -0x1310(%rbp), %rcx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2150
jmp 0x42d2
movq 0x7cac(%rip), %rax      # 0xbff8
movq (%rax), %r14
callq 0x2040
movl (%rax), %edi
callq 0x22b0
leaq 0x45e7(%rip), %rsi      # 0x8949
jmp 0x4392
leaq 0x46af(%rip), %rdi      # 0x8a1a
callq 0x2230
jmp 0x42d2
movq 0x7c7c(%rip), %rax      # 0xbff8
movq (%rax), %r14
callq 0x2040
movl (%rax), %edi
callq 0x22b0
leaq 0x45e2(%rip), %rsi      # 0x8974
leaq 0x41a0(%rip), %rdx      # 0x8539
leaq 0x4571(%rip), %rcx      # 0x8911
movq %r14, %rdi
movq %rax, %r8
xorl %eax, %eax
callq 0x2150
jmp 0x3f61
movq 0x7c3f(%rip), %rax      # 0xbff8
movq %rbx, %r14
movq (%rax), %rbx
callq 0x2040
movl (%rax), %edi
callq 0x22b0
leaq 0x4ed0(%rip), %rsi      # 0x92a2
leaq 0x4160(%rip), %rdx      # 0x8539
leaq 0x4654(%rip), %rcx      # 0x8a34
movq %rbx, %rdi
movq %r14, %rbx
movq %rax, %r8
xorl %eax, %eax
callq 0x2150
jmp 0x426f
movq -0x50(%rbp), %rbx
movl %r15d, %edi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x2110
cmpq %rbx, %rax
jge 0x450c
leaq 0x4660(%rip), %rdi      # 0x8a77
callq 0x2230
movq (%r14), %rdi
callq 0x2030
jmp 0x42ca
movq 0x7bc8(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4610(%rip), %rsi      # 0x8a4a
leaq 0x40f8(%rip), %rdx      # 0x8539
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2150
jmp 0x42ca
movq 0x7ba1(%rip), %rax      # 0xbff8
movq %rbx, %r14
movq (%rax), %rbx
callq 0x2040
movl (%rax), %edi
callq 0x22b0
leaq 0x4e58(%rip), %rsi      # 0x92c8
leaq 0x40c2(%rip), %rdx      # 0x8539
leaq 0x45b6(%rip), %rcx      # 0x8a34
movq %rbx, %rdi
movq %r14, %rbx
movq %rax, %r8
xorl %eax, %eax
callq 0x2150
jmp 0x44af
movq 0x7b61(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4e46(%rip), %rsi      # 0x92e7
leaq 0x4091(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
movl -0x34(%rbp), %edi
callq 0x2100
movq -0x48(%rbp), %r13
jmp 0x426f
movzbl %cl, %r8d
movzbl %dl, %ecx
movq 0x7b2a(%rip), %rdx      # 0xbff8
movq (%rdx), %rdi
movzbl %al, %r9d
movzbl -0x2e(%rbp), %r10d
movzbl -0x2f(%rbp), %r11d
movzbl -0x30(%rbp), %r14d
subq $0x8, %rsp
leaq 0x44b4(%rip), %rsi      # 0x89a3
leaq 0x4043(%rip), %rdx      # 0x8539
xorl %eax, %eax
pushq %r14
pushq %r11
pushq %r10
callq 0x2150
addq $0x20, %rsp
jmp 0x3f5a
movq (%r14), %rdi
leaq -0x50(%rbp), %rbx
leaq -0x30(%rbp), %rdx
movq %rbx, %rsi
movq %r13, %rcx
callq 0x37ac
movq %rax, (%r14)
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x327c
movq 0x7ac1(%rip), %r14      # 0xbff8
movq (%r14), %rdi
movzwl (%rbx), %ecx
leaq 0x454d(%rip), %rsi      # 0x8a91
leaq 0x3fee(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
cmpb $0x0, 0x7d87(%rip)      # 0xc2e0
jne 0x456b
movl %r15d, %edi
callq 0x2100
movl -0x50(%rbp), %eax
jmp 0x42d5
movq (%r14), %rdi
movl (%r13), %ecx
leaq 0x452b(%rip), %rsi      # 0x8aa4
leaq 0x3fb9(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
jmp 0x455b