// File: /workspace/github_c_makefile_star2_O3/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 $0x12f8, %rsp           # imm = 0x12F8
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
leaq -0x1310(%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 0x4030(%rip), %rdi      # 0x9911
leaq -0x120(%rbp), %rsi
callq 0x2160
testl %eax, %eax
js 0x5d75
movq -0xf0(%rbp), %r15
leaq 0x400e(%rip), %rdi      # 0x9911
xorl %esi, %esi
xorl %eax, %eax
callq 0x2200
cmpl $-0x1, %eax
je 0x5d8d
movl %eax, %r14d
cmpq $0x11, %r15
movl $0x11, %edx
cmovbq %r15, %rdx
xorps %xmm0, %xmm0
leaq -0x90(%rbp), %rsi
movaps %xmm0, (%rsi)
movw $0x0, 0x10(%rsi)
movl %eax, %edi
callq 0x2110
movq %rax, %r15
movl %r14d, %edi
callq 0x2100
cmpq $-0x1, %r15
je 0x5da5
leaq -0x90(%rbp), %r14
movq %r14, %rdi
callq 0x5835
testl %eax, %eax
js 0x59cf
movl $0x6, %r15d
movq %r14, %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2250
movb %al, -0x31(%rbp,%r15)
addq $0x3, %r14
decq %r15
jne 0x596d
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 0x59bb
movb $0x0, -0x2d(%rbp)
xorl %eax, %eax
cmpb $0x0, 0x991e(%rip)      # 0xf2e0
jne 0x5e26
movb $0x1, 0x9db5(%rip)      # 0xf784
movups 0x3ffa(%rip), %xmm0     # 0x99d0
leaq -0x1320(%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 -0x120(%rbp), %rsi
movq %r14, %rdi
callq 0x2160
testl %eax, %eax
js 0x5d7f
movq -0xf0(%rbp), %r14
movq %r14, -0x50(%rbp)
leaq -0x1320(%rbp), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x2200
testl %eax, %eax
js 0x5d97
movl %eax, %r15d
leaq 0x3fee(%rip), %rdi      # 0x9a34
xorl %esi, %esi
xorl %eax, %eax
callq 0x2200
cmpl $-0x1, %eax
je 0x5daf
movq %r13, -0x48(%rbp)
leaq -0x320(%rbp), %rbx
movl $0x100, %edx            # imm = 0x100
movl %eax, -0x34(%rbp)
movl %eax, %edi
movq %rbx, %rsi
callq 0x2110
movq %rax, %r13
testl %r13d, %r13d
je 0x5e15
cmpl $-0x1, %r13d
je 0x5e0b
movq %r14, -0x68(%rbp)
movq %r12, -0x70(%rbp)
testl %r13d, %r13d
jle 0x5cee
movq %rbx, %rdi
movl $0xa, %esi
callq 0x20b0
testq %rax, %rax
je 0x5cee
movq %rax, %r14
movb $0x0, (%rax)
movl %r14d, %r12d
subl %ebx, %r12d
incl %r12d
movzbl (%rbx), %eax
cmpl $0x20, %eax
je 0x5ace
cmpl $0x9, %eax
jne 0x5ad6
incq %rbx
decl %r12d
jmp 0x5ac1
cmpb $0x23, %al
je 0x5adf
testl %r12d, %r12d
jg 0x5aea
incq %r14
subl %r12d, %r13d
movq %r14, %rbx
jmp 0x5a93
movl %r15d, -0x54(%rbp)
movq 0x9503(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x3a32(%rip), %rsi      # 0x9531
leaq 0x3a33(%rip), %rdx      # 0x9539
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2150
xorl %r15d, %r15d
movq %rbx, %rax
incq %rbx
movzbl (%rax), %ecx
cmpl $0x1f, %ecx
jle 0x5b2d
cmpl $0x2c, %ecx
je 0x5b32
cmpl $0x20, %ecx
je 0x5b32
jmp 0x5b3e
cmpl $0x9, %ecx
jne 0x5b3a
incq %rax
incq %rbx
jmp 0x5b19
testl %ecx, %ecx
je 0x5b72
movq %rax, -0x220(%rbp,%r15,8)
incq %r15
movzbl -0x1(%rbx), %eax
testl %eax, %eax
je 0x5b72
cmpl $0x9, %eax
je 0x5b60
cmpl $0x20, %eax
je 0x5b60
incq %rbx
jmp 0x5b49
movb $0x0, -0x1(%rbx)
cmpq $0x20, %r15
jne 0x5b13
movl $0x20, %r15d
jmp 0x5b90
movl %r15d, %eax
movq $0x0, -0x220(%rbp,%rax,8)
cmpq $0x4, %r15
jge 0x5b90
movl -0x54(%rbp), %r15d
jmp 0x5adf
movq -0x220(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2250
movq %rax, -0x40(%rbp)
movq -0x218(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2250
movq %rax, %rbx
shlq $0x8, %rbx
orq -0x40(%rbp), %rbx
movq 0x942c(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x472a(%rip), %rsi      # 0xa300
leaq 0x395c(%rip), %rdx      # 0x9539
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2150
movq -0x210(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2250
movl %eax, %ecx
movl $0xff, %edx
andl %edx, %ecx
addl $0xfd, %r15d
andl %edx, %r15d
cmpl %r15d, %ecx
jne 0x5ca3
cmpq $0x15b, %rbx            # imm = 0x15B
jne 0x5c75
cmpl $0x4, %ecx
ja 0x5c75
movq %rax, -0x60(%rbp)
testl %ecx, %ecx
leaq 0x9b55(%rip), %rbx      # 0xf786
je 0x5c5f
movzbl -0x60(%rbp), %ecx
movq %rcx, -0x40(%rbp)
xorl %r15d, %r15d
movq -0x208(%rbp,%r15,8), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2250
movb %al, (%r15,%rbx)
incq %r15
cmpq %r15, -0x40(%rbp)
jne 0x5c3e
movq -0x60(%rbp), %rax
movb %al, 0x9b1c(%rip)       # 0xf785
orb $0x1, 0x9b10(%rip)      # 0xf780
jmp 0x5b87
cmpq $0x1e6, %rbx            # imm = 0x1E6
jne 0x5cc7
movq -0x208(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2250
movb %al, 0x9af3(%rip)       # 0xf78a
orb $0x2, 0x9ae2(%rip)      # 0xf780
jmp 0x5b87
movq 0x934e(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x466b(%rip), %rsi      # 0xa31f
leaq 0x387e(%rip), %rdx      # 0x9539
xorl %eax, %eax
callq 0x2150
jmp 0x5b87
movq 0x932a(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x4661(%rip), %rsi      # 0xa339
leaq 0x385a(%rip), %rdx      # 0x9539
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2150
jmp 0x5b87
movl -0x34(%rbp), %edi
callq 0x2100
movq -0x70(%rbp), %r12
movq -0x48(%rbp), %r13
movq -0x68(%rbp), %r14
movq 0x9a67(%rip), %rax      # 0xf770
movb 0x4(%rax), %cl
addb $-0x73, %cl
movl 0x9a6b(%rip), %eax      # 0xf780
cmpb $-0x4, %cl
ja 0x5d23
andl $-0x3, %eax
movl %eax, 0x9a5d(%rip)      # 0xf780
leaq 0x10(%r14), %rcx
addq $0x9, %r14
testb $0x1, %al
cmovneq %rcx, %r14
andl $0x2, %eax
leaq (%r14,%rax,2), %rbx
movq %rbx, %rdi
callq 0x2190
movq %rax, (%r13)
testq %rax, %rax
jne 0x5db9
cmpb $0x0, 0x9590(%rip)      # 0xf2e0
jne 0x5de4
movl %r15d, %edi
callq 0x2100
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x12f8, %rsp           # imm = 0x12F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2aec
jmp 0x59cf
leaq -0x1320(%rbp), %rdi
callq 0x2c34
jmp 0x5d5e
callq 0x2aa8
jmp 0x59cf
leaq 0x3c7c(%rip), %rdi      # 0x9a1a
callq 0x2230
jmp 0x5d5e
callq 0x2a64
jmp 0x59cf
callq 0x2b99
jmp 0x5d02
movq -0x50(%rbp), %rbx
movl %r15d, %edi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x2110
cmpq %rbx, %rax
jge 0x5e72
movq %r13, %rdi
movl %r15d, %esi
callq 0x2c08
jmp 0x5d5e
movq 0x920d(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x3c55(%rip), %rsi      # 0x9a4a
leaq 0x373d(%rip), %rdx      # 0x9539
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2150
jmp 0x5d56
movl -0x34(%rbp), %edi
callq 0x2b4c
jmp 0x5e1d
movl -0x34(%rbp), %edi
callq 0x2b17
movq -0x48(%rbp), %r13
jmp 0x5d02
movzbl %cl, %r8d
movzbl %dl, %ecx
movq 0x91c4(%rip), %rdx      # 0xeff8
movq (%rdx), %rdi
movzbl %al, %r9d
movzbl -0x2e(%rbp), %r10d
movzbl -0x2f(%rbp), %r11d
movzbl -0x30(%rbp), %r14d
subq $0x8, %rsp
leaq 0x3b4e(%rip), %rsi      # 0x99a3
leaq 0x36dd(%rip), %rdx      # 0x9539
xorl %eax, %eax
pushq %r14
pushq %r11
pushq %r10
callq 0x2150
addq $0x20, %rsp
jmp 0x59c8
movq (%r13), %rdi
leaq -0x50(%rbp), %rbx
leaq -0x30(%rbp), %rdx
movq %rbx, %rsi
movq %r12, %rcx
callq 0x53a2
movq %rax, (%r13)
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x4f7c
movq 0x9159(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movzwl (%rbx), %ecx
leaq 0x3be5(%rip), %rsi      # 0x9a91
leaq 0x3686(%rip), %rdx      # 0x9539
xorl %eax, %eax
callq 0x2150
cmpb $0x0, 0x941f(%rip)      # 0xf2e0
jne 0x5ed3
movl %r15d, %edi
callq 0x2100
movl -0x50(%rbp), %eax
jmp 0x5d63
movq %r12, %rdi
callq 0x2bde
jmp 0x5ec3