summaryrefslogtreecommitdiff
path: root/tools/pskey.c
blob: f8d930427c23c1150bb8373c5703c17ff6892c7c (plain)
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
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2004-2005  Marcel Holtmann <marcel@holtmann.org>
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation;
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
 *  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
 *  CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 *  ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
 *  COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
 *  SOFTWARE IS DISCLAIMED.
 *
 *
 *  $Id$
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/socket.h>

#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>

#include "csr.h"

#define CSR_TYPE_NULL	0
#define CSR_TYPE_ARRAY	1
#define CSR_TYPE_UINT8	2
#define CSR_TYPE_UINT16	3

static int transient = 0;

static int write_pskey(int dd, uint16_t pskey, int type, int argc, char *argv[])
{
	uint16_t value;
	int err;

	if (type != CSR_TYPE_UINT8 && type != CSR_TYPE_UINT16) {
		errno = EFAULT;
		return -1;
	}

	if (argc != 1) {
		errno = E2BIG;
		return -1;
	}

	if (!strncasecmp(argv[0], "0x", 2))
		value = strtol(argv[0] + 2, NULL, 16);
	else
		value = atoi(argv[0]);

	err = csr_write_pskey_uint16(dd, 0x4711, pskey,
					transient ? 0x0008 : 0x0000, value);

	return err;
}

static int read_pskey(int dd, uint16_t pskey, int type)
{
	uint8_t array[64];
	uint16_t value = 0;
	int i, err, size = sizeof(array);

	memset(array, 0, sizeof(array));

	if (type != CSR_TYPE_ARRAY &&
			type != CSR_TYPE_UINT8 &&
			type != CSR_TYPE_UINT16) {
		errno = EFAULT;
		return -1;
	}

	if (type != CSR_TYPE_ARRAY) {
		err = csr_read_pskey_uint16(dd, 0x4711, pskey, 0x0000, &value);
		if (err < 0)
			return err;

		printf("%s: 0x%04x (%d)\n", csr_pskeytostr(pskey), value, value);
	} else {
		if (pskey == CSR_PSKEY_LOCAL_SUPPORTED_FEATURES)
			size = 8;

		err = csr_read_pskey_complex(dd, 0x4711, pskey, 0x0000, array, size);
		if (err < 0)
			return err;

		printf("%s:", csr_pskeytostr(pskey));
		for (i = 0; i < size; i++)
			printf(" 0x%02x", array[i]);
		printf("\n");
	}

	return err;
}

static struct {
	uint16_t pskey;
	int type;
	char *str;
} storage[] = {
	{ CSR_PSKEY_ENC_KEY_LMIN,             CSR_TYPE_UINT16, "keymin"   },
	{ CSR_PSKEY_ENC_KEY_LMAX,             CSR_TYPE_UINT16, "keymax"   },
	{ CSR_PSKEY_LOCAL_SUPPORTED_FEATURES, CSR_TYPE_ARRAY,  "features" },
	{ CSR_PSKEY_HCI_LMP_LOCAL_VERSION,    CSR_TYPE_UINT16, "version"  },
	{ CSR_PSKEY_LMP_REMOTE_VERSION,       CSR_TYPE_UINT8,  "remver"   },
	{ CSR_PSKEY_HOSTIO_USE_HCI_EXTN,      CSR_TYPE_UINT16, "hciextn"  },
	{ CSR_PSKEY_HOSTIO_MAP_SCO_PCM,       CSR_TYPE_UINT16, "mapsco"   },
	{ CSR_PSKEY_UART_BAUDRATE,            CSR_TYPE_UINT16, "baudrate" },
	{ CSR_PSKEY_HOST_INTERFACE,           CSR_TYPE_UINT16, "hostintf" },
	{ CSR_PSKEY_ANA_FREQ,                 CSR_TYPE_UINT16, "anafreq"  },
	{ CSR_PSKEY_ANA_FTRIM,                CSR_TYPE_UINT16, "anaftrim" },
	{ CSR_PSKEY_USB_VENDOR_ID,            CSR_TYPE_UINT16, "usbvid"   },
	{ CSR_PSKEY_USB_PRODUCT_ID,           CSR_TYPE_UINT16, "usbpid"   },
	{ CSR_PSKEY_USB_DFU_PRODUCT_ID,       CSR_TYPE_UINT16, "dfupid"   },
	{ CSR_PSKEY_INITIAL_BOOTMODE,         CSR_TYPE_UINT16, "bootmode" },
	{ 0x0000, CSR_TYPE_NULL, NULL },
};

static void usage(void)
{
	int i, pos = 0;

	printf("pskey - Utility for changing CSR persistent storage\n\n");
	printf("Usage:\n"
		"\tpskey [-i <dev>] [-r] [-t] <key> [value]\n\n");

	printf("Keys:\n\t");
	for (i = 0; storage[i].pskey; i++) {
		printf("%s ", storage[i].str);
		pos += strlen(storage[i].str) + 1;
		if (pos > 60) {
			printf("\n\t");
			pos = 0;
		}
	}
	printf("\n");
}

static struct option main_options[] = {
	{ "device",	1, 0, 'i' },
	{ "reset",	0, 0, 'r' },
	{ "transient",	0, 0, 't' },
	{ "help",	0, 0, 'h' },
	{ 0, 0, 0, 0 }
};

int main(int argc, char *argv[])
{
	struct hci_dev_info di;
	struct hci_version ver;
	int i, err, dd, opt, dev = 0, reset = 0;

	while ((opt=getopt_long(argc, argv, "+i:rth", main_options, NULL)) != -1) {
		switch (opt) {
		case 'i':
			dev = hci_devid(optarg);
			if (dev < 0) {
				perror("Invalid device");
				exit(1);
			}
			break;

		case 'r':
			reset = 1;
			break;

		case 't':
			transient = 1;
			break;

		case 'h':
		default:
			usage();
			exit(0);
		}
	}

	argc -= optind;
	argv += optind;
	optind = 0;

	if (argc < 1) {
		usage();
		exit(1);
	}

	dd = hci_open_dev(dev);
	if (dd < 0) {
		fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
						dev, strerror(errno), errno);
		exit(1);
	}

	if (hci_devinfo(dev, &di) < 0) {
		fprintf(stderr, "Can't get device info for hci%d: %s (%d)\n",
						dev, strerror(errno), errno);
		hci_close_dev(dd);
		exit(1);
	}

	if (hci_read_local_version(dd, &ver, 1000) < 0) {
		fprintf(stderr, "Can't read version info for hci%d: %s (%d)\n",
						dev, strerror(errno), errno);
		hci_close_dev(dd);
		exit(1);
	}

	if (ver.manufacturer != 10) {
		fprintf(stderr, "Unsupported manufacturer\n");
		hci_close_dev(dd);
		exit(1);
	}

	for (i = 0; storage[i].pskey; i++) {
		if (strcasecmp(storage[i].str, argv[0]))
			continue;

		if (argc > 1) {
			err = write_pskey(dd, storage[i].pskey,
					storage[i].type, argc - 1, argv + 1);

			if (reset)
				csr_write_varid_valueless(dd, 0x0000,
							CSR_VARID_WARM_RESET);
		} else
			err = read_pskey(dd, storage[i].pskey, storage[i].type);

		hci_close_dev(dd);

		if (err < 0) {
			fprintf(stderr, "Can't %s persistent storage: %s (%d)\n",
				argc > 1 ? "write" : "read", strerror(errno), errno);
			exit(1);
		}

		exit(0);
	}

	fprintf(stderr, "Unsupported persistent storage\n");

	hci_close_dev(dd);

	exit(1);
}