aboutsummaryrefslogtreecommitdiffstats
path: root/librazer/cypress_bootloader.c
blob: 0a52ee319a249028248b247937e2b184c182efae (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
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
/*
 *   Cypress bootloader driver
 *   Firmware update support for Cypress based devices
 *
 *   Copyright (C) 2009 Michael Buesch <m@bues.ch>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation; either version 2
 *   of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 */

#include "cypress_bootloader.h"
#include "razer_private.h"


struct cypress_command {
	be16_t command;
	uint8_t key[8];
	uint8_t payload[54];
} _packed;

#define CYPRESS_CMD_ENTERBL	cpu_to_be16(0xFF38) /* Enter bootloader */
#define CYPRESS_CMD_WRITEFL	cpu_to_be16(0xFF39) /* Write flash */
#define CYPRESS_CMD_VERIFYFL	cpu_to_be16(0xFF3A) /* Verify flash */
#define CYPRESS_CMD_EXITBL	cpu_to_be16(0xFF3B) /* Exit bootloader */
#define CYPRESS_CMD_UPCHK	cpu_to_be16(0xFF3C) /* Update checksum */

struct cypress_status {
	uint8_t status0;
	uint8_t status1;
	uint8_t _padding[62];
} _packed;

#define CYPRESS_STAT_BLMODE	0x20 /* Bootload mode (success) */
#define CYPRESS_STAT_BOOTOK	0x01 /* Boot completed OK */
#define CYPRESS_STAT_IMAGERR	0x02 /* Image verify error */
#define CYPRESS_STAT_FLCHK	0x04 /* Flash checksum error */
#define CYPRESS_STAT_FLPROT	0x08 /* Flash protection error */
#define CYPRESS_STAT_COMCHK	0x10 /* Communication checksum error */
#define CYPRESS_STAT_INVALKEY	0x40 /* Invalid bootloader key */
#define CYPRESS_STAT_INVALCMD	0x80 /* Invalid command error */
#define CYPRESS_STAT_ALL	0xFF


static void cypress_print_one_status(int *ctx, char *buf, const char *message)
{
	if (*ctx)
		strcat(buf, ", ");
	strcat(buf, message);
	(*ctx)++;
}

static void cypress_print_status(uint8_t status, int error)
{
	char buf[512] = { 0, }; /* big enough for all messages */
	int ctx = 0;

	if (!(status & CYPRESS_STAT_BLMODE))
		cypress_print_one_status(&ctx, buf, "Not in bootloader mode");
	if (status & CYPRESS_STAT_IMAGERR)
		cypress_print_one_status(&ctx, buf, "Image verify error");
	if (status & CYPRESS_STAT_FLCHK)
		cypress_print_one_status(&ctx, buf, "Flash checksum error");
	if (status & CYPRESS_STAT_FLPROT)
		cypress_print_one_status(&ctx, buf, "Flash protection error");
	if (status & CYPRESS_STAT_COMCHK)
		cypress_print_one_status(&ctx, buf, "Communication checksum error");
	if (status & CYPRESS_STAT_INVALKEY)
		cypress_print_one_status(&ctx, buf, "Invalid bootloader key");
	if (status & CYPRESS_STAT_INVALCMD)
		cypress_print_one_status(&ctx, buf, "Invalid command");

	if (error)
		razer_error("Bootloader status: %s\n", buf);
	else
		razer_info("Bootloader status: %s\n", buf);
}

static void cmd_checksum(struct cypress_command *_cmd)
{
	char *cmd = (char *)_cmd;
	unsigned int i, sum = 0;

	for (i = 0; i < 45; i++)
		sum += cmd[i];
	cmd[45] = sum & 0xFF;
}

static int cypress_send_command(struct cypress *c,
				struct cypress_command *command,
				size_t command_size, uint8_t status_mask)
{
	struct cypress_status status;
	int err, transferred;
	uint8_t stat;

	cmd_checksum(command);

razer_dump("cypress command", command, sizeof(*command));
	err = libusb_bulk_transfer(c->usb.h, c->ep_out,
				   (unsigned char *)command, command_size,
				   &transferred, RAZER_USB_TIMEOUT);
	if (err || transferred < 0 || (size_t)transferred != command_size) {
		razer_error("cypress: Failed to send command 0x%02X\n",
			    be16_to_cpu(command->command));
		return -1;
	}
	razer_msleep(100);
	err = libusb_bulk_transfer(c->usb.h, c->ep_in,
				   (unsigned char *)&status, sizeof(status),
				   &transferred, RAZER_USB_TIMEOUT);
	if (err || transferred != sizeof(status)) {
		razer_error("cypress: Failed to receive status report\n");
		return -1;
	}
	status_mask |= CYPRESS_STAT_BLMODE; /* Always check the blmode bit */
	status_mask &= ~CYPRESS_STAT_BOOTOK; /* Always ignore the bootok bit */
	stat = (status.status0 | status.status1) & status_mask;
	if (stat != CYPRESS_STAT_BLMODE) {
		razer_error("cypress: Command 0x%04X failed with "
			    "status0=0x%02X status1=0x%02X\n",
			    be16_to_cpu(command->command),
			    status.status0, status.status1);
		cypress_print_status(stat, 1);
		return -1;
	}

	return 0;
}

static void cypress_assign_default_key(uint8_t *key)
{
	unsigned int i;

	for (i = 0; i < 8; i++)
		key[i] = i;
}

static int cypress_cmd_enterbl(struct cypress *c)
{
	struct cypress_command cmd;

	memset(&cmd, 0, sizeof(cmd));
	cmd.command = CYPRESS_CMD_ENTERBL;
	c->assign_key(cmd.key);

	return cypress_send_command(c, &cmd, sizeof(cmd),
				    CYPRESS_STAT_INVALKEY |
				    CYPRESS_STAT_INVALCMD);
}

static int cypress_cmd_exitbl(struct cypress *c)
{
	struct cypress_command cmd;

	memset(&cmd, 0, sizeof(cmd));
	cmd.command = CYPRESS_CMD_EXITBL;
	c->assign_key(cmd.key);

	return cypress_send_command(c, &cmd, sizeof(cmd),
				    CYPRESS_STAT_ALL);
}

static int cypress_cmd_verifyfl(struct cypress *c)
{
	struct cypress_command cmd;

	memset(&cmd, 0, sizeof(cmd));
	cmd.command = CYPRESS_CMD_VERIFYFL;
	c->assign_key(cmd.key);

	return cypress_send_command(c, &cmd, sizeof(cmd),
				    CYPRESS_STAT_ALL);
}

static int cypress_cmd_updatechksum(struct cypress *c)
{
	struct cypress_command cmd;

	memset(&cmd, 0, sizeof(cmd));
	cmd.command = CYPRESS_CMD_UPCHK;
	c->assign_key(cmd.key);

	return cypress_send_command(c, &cmd, sizeof(cmd),
				    CYPRESS_STAT_ALL);
}

static int cypress_cmd_writefl(struct cypress *c, uint16_t blocknr,
			       uint8_t segment, const char *data)
{
	struct cypress_command cmd;

	memset(&cmd, 0, sizeof(cmd));
	cmd.command = CYPRESS_CMD_WRITEFL;
	c->assign_key(cmd.key);

	cmd.payload[0] = blocknr >> 8;
	cmd.payload[1] = blocknr;
	cmd.payload[2] = segment;
	memcpy(&cmd.payload[3], data, 32);

	return cypress_send_command(c, &cmd, sizeof(cmd),
				    CYPRESS_STAT_ALL);
}

static int cypress_writeflash(struct cypress *c,
			      const char *image, size_t len)
{
	unsigned int block;
	int err;

	if (len % 64) {
		razer_error("cypress_writeflash: internal error\n");
		return -EINVAL;
	}

	for (block = 0; block < len / 64; block++) {
		/* First 32 bytes */
		err = cypress_cmd_writefl(c, block, 0, image);
		if (err) {
			razer_error("cypress: Failed to write image "
				    "(block %u, segment 0)\n", block);
			return -EIO;
		}
		image += 32;
		/* Last 32 bytes */
		err = cypress_cmd_writefl(c, block, 1, image);
		if (err) {
			razer_error("cypress: Failed to write image "
				    "(block %u, segment 1)\n", block);
			return -EIO;
		}
		image += 32;
	}

	return 0;
}

int cypress_open(struct cypress *c, struct libusb_device *dev,
		 void (*assign_key)(uint8_t *key))
{
	int err;

	BUILD_BUG_ON(sizeof(struct cypress_command) != 64);
	BUILD_BUG_ON(sizeof(struct cypress_status) != 64);

return -1; //FIXME: Does not work, yet.

	memset(c, 0, sizeof(*c));
	if (!assign_key)
		assign_key = cypress_assign_default_key;
	c->assign_key = assign_key;

	c->usb.dev = dev;
	c->usb.bConfigurationValue = 1;
	err = razer_usb_add_used_interface(&c->usb, 0, 0);
	if (err)
		return err;
	err = razer_generic_usb_claim(&c->usb);
	if (err) {
		razer_error("cypress: Failed to open and claim device\n");
		return err;
	}

	/* EP numbers are hardcoded */
	c->ep_in = 0x81;
	c->ep_out = 0x02;

	return 0;
}

void cypress_close(struct cypress *c)
{
	razer_generic_usb_release(&c->usb);
	memset(c, 0, sizeof(*c));
}

int cypress_upload_image(struct cypress *c,
			 const char *image, size_t len)
{
	int err;
	int result = 0;

	if (len % 64) {
		razer_error("cypress: Image size is not a multiple "
			    "of the block size (64)\n");
		return -EINVAL;
	}

razer_dump("image", image, len);
	err = cypress_cmd_enterbl(c);
	if (err) {
		razer_error("cypress: Failed to enter bootloader\n");
		result = err;
		goto out;
	}
	err = cypress_writeflash(c, image, len);
	if (err) {
		razer_error("cypress: Failed to write flash image\n");
		result = err;
		goto out;
	}
	err = cypress_cmd_verifyfl(c);
	if (err) {
		razer_error("cypress: Failed to verify the flash\n");
		result = err;
		goto out;
	}
	err = cypress_cmd_updatechksum(c);
	if (err) {
		razer_error("cypress: Failed to update the checksum\n");
		result = err;
		goto out;
	}
	err = cypress_cmd_exitbl(c);
	if (err) {
		razer_error("cypress: Failed to exit bootloader\n");
		result = err;
	}
out:

	return result;
}
bues.ch cgit interface