hc
2023-02-14 0cc9b7c44253c93447ddf73e206fbdbb3d9f16b1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
diff --git a/realtek/rtk_hciattach/hciattach_rtk.c b/realtek/rtk_hciattach/hciattach_rtk.c
old mode 100644
new mode 100755
index b195b1b..b33040b
--- a/realtek/rtk_hciattach/hciattach_rtk.c
+++ b/realtek/rtk_hciattach/hciattach_rtk.c
@@ -67,6 +67,7 @@
 #define BT_CONFIG_DIRECTORY "/lib/firmware/rtlbt/"
 
 #ifdef USE_CUSTOMER_ADDRESS
+#define BT_ADDR_FROM_VENDOR_STORAGE
 #define BT_ADDR_FILE        "/opt/bdaddr"
 static uint8_t customer_bdaddr = 0;
 #endif
@@ -2216,14 +2217,13 @@ int bachk(const char *str)
 * @param bt_addr where bt addr is stored
 *
 */
-/* static void rtk_get_ram_addr(char bt_addr[0])
- * {
- *     srand(time(NULL) + getpid() + getpid() * 987654 + rand());
- * 
- *     RT_U32 addr = rand();
- *     memcpy(bt_addr, &addr, sizeof(RT_U8));
- * }
- */
+static void rtk_get_ram_addr(char bt_addr[0])
+{
+    srand(time(NULL) + getpid() + getpid() * 987654 + rand());
+
+    RT_U32 addr = rand();
+    memcpy(bt_addr, &addr, sizeof(RT_U8));
+}
 
 /**
 * Write the random bt addr to the file /data/misc/bluetoothd/bt_mac/btmac.txt.
@@ -2249,6 +2249,154 @@ int bachk(const char *str)
  *     }
  * }
  */
+#ifdef BT_ADDR_FROM_VENDOR_STORAGE
+
+typedef         unsigned short      uint16;
+typedef         unsigned int        uint32;
+typedef         unsigned char       uint8;
+
+#define VENDOR_REQ_TAG          0x56524551
+#define VENDOR_READ_IO          _IOW('v', 0x01, unsigned int)
+#define VENDOR_WRITE_IO         _IOW('v', 0x02, unsigned int)
+
+#define VENDOR_ID_MAX   5
+static char *vendor_id_table[] = {
+        "VENDOR_SN_ID",
+        "VENDOR_WIFI_MAC_ID",
+        "VENDOR_LAN_MAC_ID",
+        "VENDOR_BT_MAC_ID",
+        "VENDOR_IMEI_ID",
+};
+
+#define VENDOR_SN_ID            1
+#define VENDOR_WIFI_MAC_ID      2
+#define VENDOR_LAN_MAC_ID       3
+#define VENDOR_BT_MAC_ID        4
+#define VENDOR_IMEI_ID          5
+
+#define VENDOR_STORAGE_DEBUG
+
+struct rk_vendor_req {
+        uint32 tag;
+        uint16 id;
+        uint16 len;
+        uint8 data[1024];
+};
+
+static void rknand_get_randeom_btaddr(char *bt_addr)
+{
+    int i;
+    /* No autogen BDA. Generate one now. */
+    bt_addr[4] = 0x22;
+    bt_addr[5] = 0x22;
+    for (i = 0; i < 4; i++)
+        rtk_get_ram_addr(&bt_addr[i]);
+}
+
+static void rknand_print_hex_data(uint8 *s, struct rk_vendor_req *buf, uint32 len)
+{
+        unsigned char i = 0;
+
+#ifdef VENDOR_STORAGE_DEBUG
+        fprintf(stdout, "%s\n",s);
+        fprintf(stdout, "tag = %d // id = %d // len = %d // data = 0x%p\n", buf->tag, buf->id, buf->len, buf->data);
+#endif
+
+        printf("%s: ", vendor_id_table[buf->id - 1]);
+        if (buf->id == VENDOR_SN_ID ||
+            buf->id == VENDOR_IMEI_ID) {
+                for (i = 0; i < len; i++)
+                        printf("%c", buf->data[i]);
+        } else {
+                for (i = 0; i < len; i++)
+                        printf("%02x", buf->data[i]);
+        }
+        fprintf(stdout, "\n");
+}
+
+static int vendor_storage_read(int cmd, char *buf, int buf_len)
+{
+        uint32 i;
+        int ret ;
+        uint8 p_buf[100]; /* malloc req buffer or used extern buffer */
+        struct rk_vendor_req *req;
+
+        req = (struct rk_vendor_req *)p_buf;
+        memset(p_buf, 0, 100);
+        int sys_fd = open("/dev/vendor_storage", O_RDWR, 0);
+        if(sys_fd < 0){
+                printf("vendor_storage open fail\n");
+                return -1;
+        }
+
+        req->tag = VENDOR_REQ_TAG;
+        req->id = cmd;
+        req->len = 50;
+
+        ret = ioctl(sys_fd, VENDOR_READ_IO, req);
+
+        if(ret){
+                printf("vendor read error %d\n", ret);
+                return -1;
+        }
+        close(sys_fd);
+
+        rknand_print_hex_data("vendor read:", req, req->len);
+    memcpy(buf, req->data, req->len);
+        return req->len;
+}
+static int vendor_storage_write(int cmd, char *num)
+{
+        uint32 i;
+        int ret ;
+        uint8 p_buf[100]; /* malloc req buffer or used extern buffer */
+        struct rk_vendor_req *req;
+
+        req = (struct rk_vendor_req *)p_buf;
+        int sys_fd = open("/dev/vendor_storage",O_RDWR,0);
+        if(sys_fd < 0){
+                printf("vendor_storage open fail\n");
+                return -1;
+        }
+
+        req->tag = VENDOR_REQ_TAG;
+        req->id = cmd;
+
+        if (cmd != VENDOR_SN_ID && cmd != VENDOR_IMEI_ID)
+                req->len = 6;
+        else
+                req->len = strlen(num);
+        memcpy(req->data, num, req->len);
+
+        ret = ioctl(sys_fd, VENDOR_WRITE_IO, req);
+        if(ret){
+                printf("vendor write error\n");
+                return -1;
+        }
+
+        rknand_print_hex_data("vendor write:", req, req->len);
+        return 0;
+}
+static int vendor_storage_read_bt_addr(uint8_t *tbuf)
+{
+    uint8 raw_buf[100] = {0};
+    int raw_len = 0;
+    int i;
+
+    raw_len = vendor_storage_read(VENDOR_BT_MAC_ID, raw_buf, 100);
+    if (raw_len < 0)
+        return -1;
+    for (i = 0; i < 6; i++) {
+        sprintf(tbuf + i * 3, "%02x:", raw_buf[i]);
+    }
+    tbuf[17] = '\0';
+    return 17;
+}
+static int vendor_storage_write_bt_addr(char *tbuf)
+{
+    return vendor_storage_write(VENDOR_BT_MAC_ID, tbuf);
+}
+#endif
 #endif
 
 /**
@@ -2280,6 +2428,38 @@ int rtk_get_bt_config(struct btrtl_info *btrtl, uint8_t **config_buf,
     uint8_t tbuf[BDADDR_STRING_LEN + 1];
     char *str;
 
+#ifdef BT_ADDR_FROM_VENDOR_STORAGE
+    ret = vendor_storage_read_bt_addr(tbuf);
+    if (ret >= 0 && bachk(tbuf) < 0) {
+        ret = -1;
+        RS_ERR("vendor_storage bt addr chechk failed");
+    }
+    if (ret < 0) {
+        RS_ERR("vendor storage read bt addr failed, generate one");
+
+        rknand_get_randeom_btaddr(bt_addr);
+        vendor_storage_write_bt_addr(bt_addr);
+        customer_bdaddr = 1;
+    } else {
+        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;
+    }
+#else
     if (stat(BT_ADDR_FILE, &st) < 0) {
         RS_INFO("Couldnt access customer BT MAC file %s",
                 BT_ADDR_FILE);
@@ -2333,6 +2513,7 @@ int rtk_get_bt_config(struct btrtl_info *btrtl, uint8_t **config_buf,
         customer_bdaddr = 1;
     }
 #endif
+#endif
 
 GET_CONFIG:
     //ret = sprintf(bt_config_file_name, BT_CONFIG_DIRECTORY "rtlbt_config");