// File: /workspace/github_c_makefile_star2_O1/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 -0x1320(%rbp), %rdi
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x20d0
movw $0x8888, -0x2c(%rbp)    # imm = 0x8888
movl $0x884ce000, -0x30(%rbp) # imm = 0x884CE000
leaq 0x4023(%rip), %rdi      # 0x9911
leaq -0x120(%rbp), %rsi
callq 0x2160
testl %eax, %eax
js 0x5d7b
movq -0xf0(%rbp), %r15
leaq 0x4001(%rip), %rdi      # 0x9911
xorl %esi, %esi
xorl %eax, %eax
callq 0x2200
cmpl $-0x1, %eax
je 0x5d93
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 0x5dab
leaq -0x90(%rbp), %r14
movq %r14, %rdi
callq 0x5842
testl %eax, %eax
js 0x59d5
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 0x597a
movb -0x2d(%rbp), %cl
movb -0x2c(%rbp), %al
xorb $-0x62, %cl
movl %eax, %edx
xorb $-0x75, %dl
orb %cl, %dl
sete %dl
movb -0x2b(%rbp), %cl
cmpb $0x40, %cl
setb %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x59c1
movb $0x0, -0x2d(%rbp)
cmpb $0x0, 0x9918(%rip)      # 0xf2e0
jne 0x5e2a
movb $0x1, 0x9daf(%rip)      # 0xf784
movups 0x3ff4(%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 0x5d85
movq -0xf0(%rbp), %rax
movq %rax, -0x40(%rbp)
leaq -0x1320(%rbp), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x2200
testl %eax, %eax
js 0x5d9d
movl %eax, %r15d
movq -0x40(%rbp), %r14
leaq 0x3fe4(%rip), %rdi      # 0x9a34
xorl %esi, %esi
xorl %eax, %eax
callq 0x2200
cmpl $-0x1, %eax
je 0x5db5
movq %r13, -0x50(%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 0x5e19
cmpl $-0x1, %r13d
je 0x5e0f
movq %r14, -0x68(%rbp)
movq %r12, -0x70(%rbp)
testl %r13d, %r13d
jle 0x5cf8
movq %rbx, %rdi
movl $0xa, %esi
callq 0x20b0
testq %rax, %rax
je 0x5cf8
movq %rax, %r14
movb $0x0, (%rax)
movl %r14d, %r12d
subl %ebx, %r12d
incl %r12d
movzbl (%rbx), %eax
cmpl $0x20, %eax
je 0x5ad8
cmpl $0x9, %eax
jne 0x5ae0
incq %rbx
decl %r12d
jmp 0x5acb
cmpb $0x23, %al
je 0x5ae9
testl %r12d, %r12d
jg 0x5af4
incq %r14
subl %r12d, %r13d
movq %r14, %rbx
jmp 0x5a9d
movl %r15d, -0x54(%rbp)
movq 0x94f9(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x3a28(%rip), %rsi      # 0x9531
leaq 0x3a29(%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 0x5b37
cmpl $0x2c, %ecx
je 0x5b3c
cmpl $0x20, %ecx
je 0x5b3c
jmp 0x5b48
cmpl $0x9, %ecx
jne 0x5b44
incq %rax
incq %rbx
jmp 0x5b23
testl %ecx, %ecx
je 0x5b7c
movq %rax, -0x220(%rbp,%r15,8)
incq %r15
movzbl -0x1(%rbx), %eax
testl %eax, %eax
je 0x5b7c
cmpl $0x9, %eax
je 0x5b6a
cmpl $0x20, %eax
je 0x5b6a
incq %rbx
jmp 0x5b53
movb $0x0, -0x1(%rbx)
cmpq $0x20, %r15
jne 0x5b1d
movl $0x20, %r15d
jmp 0x5b8b
movl %r15d, %eax
movq $0x0, -0x220(%rbp,%rax,8)
cmpl $0x4, %r15d
jge 0x5b9a
movl -0x54(%rbp), %r15d
jmp 0x5ae9
movq -0x220(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2250
movq %rax, -0x48(%rbp)
movq -0x218(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2250
movq %rax, %rbx
shlq $0x8, %rbx
orq -0x48(%rbp), %rbx
movq 0x9422(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x4720(%rip), %rsi      # 0xa300
leaq 0x3952(%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 0x5cad
cmpq $0x15b, %rbx            # imm = 0x15B
jne 0x5c7f
cmpl $0x4, %ecx
ja 0x5c7f
movq %rax, -0x60(%rbp)
testl %ecx, %ecx
leaq 0x9b4b(%rip), %rbx      # 0xf786
je 0x5c69
movzbl -0x60(%rbp), %ecx
movq %rcx, -0x48(%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, -0x48(%rbp)
jne 0x5c48
movq -0x60(%rbp), %rax
movb %al, 0x9b12(%rip)       # 0xf785
orb $0x1, 0x9b06(%rip)      # 0xf780
jmp 0x5b91
cmpq $0x1e6, %rbx            # imm = 0x1E6
jne 0x5cd1
movq -0x208(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2250
movb %al, 0x9ae9(%rip)       # 0xf78a
orb $0x2, 0x9ad8(%rip)      # 0xf780
jmp 0x5b91
movq 0x9344(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x4661(%rip), %rsi      # 0xa31f
leaq 0x3874(%rip), %rdx      # 0x9539
xorl %eax, %eax
callq 0x2150
jmp 0x5b91
movq 0x9320(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x4657(%rip), %rsi      # 0xa339
leaq 0x3850(%rip), %rdx      # 0x9539
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2150
jmp 0x5b91
movl -0x34(%rbp), %edi
callq 0x2100
movq -0x70(%rbp), %r12
movq -0x50(%rbp), %r13
movq -0x68(%rbp), %r14
movq 0x9a5d(%rip), %rax      # 0xf770
movb 0x4(%rax), %al
addb $-0x73, %al
cmpb $-0x4, %al
ja 0x5d23
andb $-0x3, 0x9a5d(%rip)     # 0xf780
movl 0x9a57(%rip), %eax      # 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 0x5dbf
cmpb $0x0, 0x958a(%rip)      # 0xf2e0
jne 0x5de8
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 0x2af7
jmp 0x59d5
leaq -0x1320(%rbp), %rdi
callq 0x2c3f
jmp 0x5d64
callq 0x2ab3
jmp 0x59d5
leaq 0x3c76(%rip), %rdi      # 0x9a1a
callq 0x2230
jmp 0x5d64
callq 0x2a6f
jmp 0x59d5
callq 0x2ba4
jmp 0x5d0c
movq -0x40(%rbp), %rdx
movl %r15d, %edi
movq %rax, %rsi
callq 0x2110
cmpq -0x40(%rbp), %rax
jge 0x5e77
movq %r13, %rdi
movl %r15d, %esi
callq 0x2c13
jmp 0x5d64
movq 0x9209(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x3c51(%rip), %rsi      # 0x9a4a
leaq 0x3739(%rip), %rdx      # 0x9539
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2150
jmp 0x5d5c
movl -0x34(%rbp), %edi
callq 0x2b57
jmp 0x5e21
movl -0x34(%rbp), %edi
callq 0x2b22
movq -0x50(%rbp), %r13
jmp 0x5d0c
movzbl %al, %r8d
movzbl %cl, %ecx
movq 0x91c0(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movzbl -0x2d(%rbp), %r9d
movzbl -0x2e(%rbp), %r10d
movzbl -0x2f(%rbp), %r11d
movzbl -0x30(%rbp), %r14d
subq $0x8, %rsp
leaq 0x3b49(%rip), %rsi      # 0x99a3
leaq 0x36d8(%rip), %rdx      # 0x9539
xorl %eax, %eax
pushq %r14
pushq %r11
pushq %r10
callq 0x2150
addq $0x20, %rsp
jmp 0x59ce
movq (%r13), %rdi
leaq -0x40(%rbp), %rbx
leaq -0x30(%rbp), %rdx
movq %rbx, %rsi
movq %r12, %rcx
callq 0x53a4
movq %rax, (%r13)
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x4f88
movq 0x9154(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movzwl (%rbx), %ecx
leaq 0x3be0(%rip), %rsi      # 0x9a91
leaq 0x3681(%rip), %rdx      # 0x9539
xorl %eax, %eax
callq 0x2150
cmpb $0x0, 0x941a(%rip)      # 0xf2e0
jne 0x5ed8
movl %r15d, %edi
callq 0x2100
movl -0x40(%rbp), %eax
jmp 0x5d69
movq %r12, %rdi
callq 0x2be9
jmp 0x5ec8