aboutsummaryrefslogtreecommitdiffstats
blob: 8a2ebd0c4ac8d63a15672324e142fa295929b734 (plain) (blame)
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
#include <stdbool.h>
#include <errno.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 get_nvs_mac(struct nl80211_state *state, struct nl_cb *cb,
			struct nl_msg *msg, int argc, char **argv)
{
	unsigned char mac_buff[12];
	char *fname;
	int fd;

	argc -= 2;
	argv += 2;

	fname = get_opt_nvsinfile(argc, argv);
	if (!fname)
		return 1;

	fd = open(fname, 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)
{
	argc -= 2;
	argv += 2;

	if ((argc != 2) || (strlen(argv[1]) != 17))
		return 1;

	nvs_set_mac(argv[0], argv[1]);
	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
	};

	argc -= 2;
	argv += 2;

	if (argc < 1 || argc > 2)
		return 1;

	if (read_ini(*argv, &cmn)) {
		fprintf(stderr, "Fail to read ini file\n");
		return 1;
	}
	argv++;
	argc--;

	cfg_nvs_ops(&cmn);

	cmn.nvs_name = get_opt_nvsoutfile(argc, argv);
	if (create_nvs_file(&cmn)) {
		fprintf(stderr, "Fail to create reference NVS file\n");
		return 1;
	}

	printf("%04X\n", cmn.arch);

	return 0;
}

COMMAND(set, ref_nvs, "<ini file> [<nvs file>]", 0, 0, CIB_NONE, set_ref_nvs,
	"Create reference NVS file");

static int set_upd_nvs(struct nl80211_state *state, struct nl_cb *cb,
	struct nl_msg *msg, int argc, char **argv)
{
	char *infname = NULL, *outfname = 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;
	}
	argc--;
	argv++;

	infname = get_opt_nvsinfile(argc, argv);
	if (!infname)
		return 1;

	if (argc) {
		argc--;
		argv++;
	}
	outfname = get_opt_nvsoutfile(argc, argv);
	if (!outfname)
		return 1;

	cfg_nvs_ops(&cmn);

	if (update_nvs_file(infname, outfname, &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 infile>] [<nvs_outfile>]", 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;

	argc -= 2;
	argv += 2;

	fname = get_opt_nvsinfile(argc, argv);
	if (!fname)
		return 1;

	if (dump_nvs_file(fname)) {
		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 get_info_nvs(struct nl80211_state *state, struct nl_cb *cb,
			struct nl_msg *msg, int argc, char **argv)
{
	char *fname;

	argc -= 2;
	argv += 2;

	fname = get_opt_nvsinfile(argc, argv);
	if(!fname)
		return 1;

	if (info_nvs_file(fname)) {
		fprintf(stderr, "Fail to read info from NVS file\n");
		return 1;
	}

	return 0;
}

COMMAND(get, info_nvs, "[<nvs file>]", 0, 0, CIB_NONE, get_info_nvs,
	"Print information from nvs file");

static int set_autofem(struct nl80211_state *state, struct nl_cb *cb,
			struct nl_msg *msg, int argc, char **argv)
{
	char *fname = NULL;
	int res;
	unsigned int 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;
	}

	res = sscanf(argv[0], "%x", &val);
	if (res != 1 || val > 1) {
		fprintf(stderr, "Invalid argument\n");
		return 1;
	}
	argv++;
	argc--;

	fname = get_opt_nvsinfile(argc, argv);

	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;
	int res;
	unsigned int 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;
	}
	res = sscanf(argv[0], "%x", &val);
	if(res != 1 || val >= WL1271_INI_FEM_MODULE_COUNT) {
		fprintf(stderr, "Invalid argument\n");
		return 1;
	}
	argv++;
	argc--;

	fname = get_opt_nvsinfile(argc, argv);

	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|2|3> [<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, 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, NULL);
	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)");