forked from gxk/ti-utils
-
Notifications
You must be signed in to change notification settings - Fork 2
/
misc_cmds.c
456 lines (349 loc) · 8.69 KB
/
misc_cmds.c
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include <stdbool.h>
#include <errno.h>
#include <time.h>
#include <net/if.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "calibrator.h"
#include "plt.h"
#include "ini.h"
#include "nvs.h"
SECTION(get);
SECTION(set);
static int handle_push_nvs(struct nl80211_state *state,
struct nl_cb *cb,
struct nl_msg *msg,
int argc, char **argv)
{
void *map = MAP_FAILED;
int fd, retval = 0;
struct nlattr *key;
struct stat filestat;
if (argc != 1)
return 1;
fd = open(argv[0], O_RDONLY);
if (fd < 0) {
perror("Error opening file for reading");
return 1;
}
if (fstat(fd, &filestat) < 0) {
perror("Error stating file");
return 1;
}
map = mmap(0, filestat.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
perror("Error mmapping the file");
goto nla_put_failure;
}
key = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
if (!key)
goto nla_put_failure;
NLA_PUT_U32(msg, WL1271_TM_ATTR_CMD_ID, WL1271_TM_CMD_NVS_PUSH);
NLA_PUT(msg, WL1271_TM_ATTR_DATA, filestat.st_size, map);
nla_nest_end(msg, key);
goto cleanup;
nla_put_failure:
retval = -ENOBUFS;
cleanup:
if (map != MAP_FAILED)
munmap(map, filestat.st_size);
close(fd);
return retval;
}
COMMAND(set, push_nvs, "<nvs filename>",
NL80211_CMD_TESTMODE, 0, CIB_PHY, handle_push_nvs,
"Push NVS file into the system");
#if 0
static int handle_fetch_nvs(struct nl80211_state *state,
struct nl_cb *cb,
struct nl_msg *msg,
int argc, char **argv)
{
char *end;
void *map = MAP_FAILED;
int fd, retval = 0;
struct nlattr *key;
struct stat filestat;
if (argc != 0)
return 1;
key = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
if (!key)
goto nla_put_failure;
NLA_PUT_U32(msg, WL1271_TM_ATTR_CMD_ID, WL1271_TM_CMD_NVS_PUSH);
NLA_PUT_U32(msg, WL1271_TM_ATTR_IE_ID, WL1271_TM_CMD_NVS_PUSH);
nla_nest_end(msg, key);
goto cleanup;
nla_put_failure:
retval = -ENOBUFS;
cleanup:
if (map != MAP_FAILED)
munmap(map, filestat.st_size);
close(fd);
return retval;
}
COMMAND(set, fetch_nvs, NULL,
NL80211_CMD_TESTMODE, 0, CIB_NETDEV, handle_fetch_nvs,
"Send command to fetch NVS file");
#endif
static int get_nvs_mac(struct nl80211_state *state, struct nl_cb *cb,
struct nl_msg *msg, int argc, char **argv)
{
unsigned char mac_buff[12];
int fd;
argc -= 2;
argv += 2;
if (argc != 1)
return 2;
fd = open(argv[0], O_RDONLY);
if (fd < 0) {
perror("Error opening file for reading");
return 1;
}
read(fd, mac_buff, 12);
printf("MAC addr from NVS: %02x:%02x:%02x:%02x:%02x:%02x\n",
mac_buff[11], mac_buff[10], mac_buff[6],
mac_buff[5], mac_buff[4], mac_buff[3]);
close(fd);
return 0;
}
COMMAND(get, nvs_mac, "<nvs filename>", 0, 0, CIB_NONE, get_nvs_mac,
"Get MAC addr from NVS file (offline)");
/*
* Sets MAC address in NVS.
* The default value for MAC is random where 1 byte zero.
*/
static int set_nvs_mac(struct nl80211_state *state, struct nl_cb *cb,
struct nl_msg *msg, int argc, char **argv)
{
unsigned char mac_buff[12];
unsigned int in_mac[6];
int fd;
argc -= 2;
argv += 2;
if (argc < 1 || (argc == 2 && (strlen(argv[1]) != 17)))
return 2;
if (argc == 2)
sscanf(argv[1], "%2x:%2x:%2x:%2x:%2x:%2x",
&in_mac[0], &in_mac[1],
&in_mac[2], &in_mac[3],
&in_mac[4], &in_mac[5]);
else {
srand((unsigned)time(NULL));
in_mac[0] = 0x0;
in_mac[1] = rand();
in_mac[2] = rand();
in_mac[3] = rand();
in_mac[4] = rand();
in_mac[5] = rand();
}
fd = open(argv[0], O_RDWR);
if (fd < 0) {
perror("Error opening file for reading");
return 1;
}
read(fd, mac_buff, 12);
mac_buff[11] = in_mac[0];
mac_buff[10] = in_mac[1];
mac_buff[6] = in_mac[2];
mac_buff[5] = in_mac[3];
mac_buff[4] = in_mac[4];
mac_buff[3] = in_mac[5];
lseek(fd, 0L, 0);
write(fd, mac_buff, 12);
close(fd);
return 0;
}
COMMAND(set, nvs_mac, "<nvs file> [<mac addr>]", 0, 0, CIB_NONE, set_nvs_mac,
"Set MAC addr in NVS file (offline), like XX:XX:XX:XX:XX:XX");
static int set_ref_nvs(struct nl80211_state *state, struct nl_cb *cb,
struct nl_msg *msg, int argc, char **argv)
{
struct wl12xx_common cmn = {
.arch = UNKNOWN_ARCH,
.parse_ops = NULL,
.dual_mode = DUAL_MODE_UNSET,
.done_fem = NO_FEM_PARSED
};
argc -= 2;
argv += 2;
if (argc != 1)
return 1;
if (read_ini(*argv, &cmn)) {
fprintf(stderr, "Fail to read ini file\n");
return 1;
}
cfg_nvs_ops(&cmn);
if (create_nvs_file(&cmn)) {
fprintf(stderr, "Fail to create reference NVS file\n");
return 1;
}
printf("%04X", cmn.arch);
return 0;
}
COMMAND(set, ref_nvs, "<ini file>", 0, 0, CIB_NONE, set_ref_nvs,
"Create reference NVS file");
static int set_ref_nvs2(struct nl80211_state *state, struct nl_cb *cb,
struct nl_msg *msg, int argc, char **argv)
{
struct wl12xx_common cmn = {
.arch = UNKNOWN_ARCH,
.parse_ops = NULL,
.dual_mode = DUAL_MODE_UNSET,
.done_fem = NO_FEM_PARSED
};
argc -= 2;
argv += 2;
if (argc != 2)
return 1;
if (read_ini(*argv, &cmn))
return 1;
argv++;
if (read_ini(*argv, &cmn))
return 1;
cfg_nvs_ops(&cmn);
if (create_nvs_file(&cmn)) {
fprintf(stderr, "Fail to create reference NVS file\n");
return 1;
}
printf("%04X", cmn.arch);
return 0;
}
COMMAND(set, ref_nvs2, "<ini file> <ini file>", 0, 0, CIB_NONE, set_ref_nvs2,
"Create reference NVS file for 2 FEMs");
static int set_upd_nvs(struct nl80211_state *state, struct nl_cb *cb,
struct nl_msg *msg, int argc, char **argv)
{
char *fname = NULL;
struct wl12xx_common cmn = {
.arch = UNKNOWN_ARCH,
.parse_ops = NULL
};
argc -= 2;
argv += 2;
if (argc < 1)
return 1;
if (read_ini(*argv, &cmn)) {
fprintf(stderr, "Fail to read ini file\n");
return 1;
}
cfg_nvs_ops(&cmn);
if (argc == 2)
fname = *++argv;
if (update_nvs_file(fname, &cmn)) {
fprintf(stderr, "Fail to update NVS file\n");
return 1;
}
#if 0
printf("\n\tThe updated NVS file (%s) is ready\n\tCopy it to %s and "
"reboot the system\n\n", NEW_NVS_NAME, CURRENT_NVS_NAME);
#endif
return 0;
}
COMMAND(set, upd_nvs, "<ini file> [<nvs file>]", 0, 0, CIB_NONE, set_upd_nvs,
"Update values of a NVS from INI file");
static int get_dump_nvs(struct nl80211_state *state, struct nl_cb *cb,
struct nl_msg *msg, int argc, char **argv)
{
char *fname = NULL;
struct wl12xx_common cmn = {
.arch = UNKNOWN_ARCH,
.parse_ops = NULL
};
argc -= 2;
argv += 2;
if (argc > 0)
fname = *argv;
if (dump_nvs_file(fname, &cmn)) {
fprintf(stderr, "Fail to dump NVS file\n");
return 1;
}
return 0;
}
COMMAND(get, dump_nvs, "[<nvs file>]", 0, 0, CIB_NONE, get_dump_nvs,
"Dump NVS file, specified by option or current");
static int set_autofem(struct nl80211_state *state, struct nl_cb *cb,
struct nl_msg *msg, int argc, char **argv)
{
char *fname = NULL;
unsigned char val;
struct wl12xx_common cmn = {
.arch = UNKNOWN_ARCH,
.parse_ops = NULL
};
argc -= 2;
argv += 2;
if (argc < 1) {
fprintf(stderr, "Missing argument\n");
return 2;
}
sscanf(argv[0], "%2x", (unsigned int *)&val);
if (argc == 2)
fname = argv[1];
if (set_nvs_file_autofem(fname, val, &cmn)) {
fprintf(stderr, "Fail to set AutoFEM\n");
return 1;
}
return 0;
}
COMMAND(set, autofem, "<0-manual|1-auto> [<nvs file>]", 0, 0, CIB_NONE, set_autofem,
"Set Auto FEM detection, where 0 - manual, 1 - auto detection");
static int set_fem_manuf(struct nl80211_state *state, struct nl_cb *cb,
struct nl_msg *msg, int argc, char **argv)
{
char *fname = NULL;
unsigned char val;
struct wl12xx_common cmn = {
.arch = UNKNOWN_ARCH,
.parse_ops = NULL
};
argc -= 2;
argv += 2;
if (argc < 1) {
fprintf(stderr, "Missing argument\n");
return 2;
}
sscanf(argv[0], "%2x", (unsigned int *)&val);
if (argc == 2)
fname = argv[1];
if (set_nvs_file_fem_manuf(fname, val, &cmn)) {
fprintf(stderr, "Fail to set AutoFEM\n");
return 1;
}
return 0;
}
COMMAND(set, fem_manuf, "<0|1> [<nvs file>]", 0, 0, CIB_NONE, set_fem_manuf,
"Set FEM manufacturer");
static int get_drv_info(struct nl80211_state *state, struct nl_cb *cb,
struct nl_msg *msg, int argc, char **argv)
{
argc -= 2;
argv += 2;
if (argc < 1) {
fprintf(stderr, "Missing argument (device name)\n");
return 2;
}
return do_get_drv_info(argv[0], NULL);
}
COMMAND(get, drv_info, "<device name>", 0, 0, CIB_NONE, get_drv_info,
"Get driver information: PG version");
static int get_hw_version(struct nl80211_state *state, struct nl_cb *cb,
struct nl_msg *msg, int argc, char **argv)
{
int ret, chip_id = 0;
argc -= 2;
argv += 2;
if (argc < 1) {
fprintf(stderr, "Missing argument (device name)\n");
return 2;
}
ret = do_get_drv_info(argv[0], &chip_id);
if (!ret)
printf("%08X\n", chip_id);
return ret;
}
COMMAND(get, hw_version, "<device name>", 0, 0, CIB_NONE, get_hw_version,
"Get HW version (chip id)");