File: madfuload.c

package info (click to toggle)
madfuload 1.2-4.2
  • links: PTS
  • area: non-free
  • in suites: buster, sid
  • size: 416 kB
  • ctags: 131
  • sloc: sh: 804; ansic: 685; makefile: 36
file content (400 lines) | stat: -rw-r--r-- 9,157 bytes parent folder | download | duplicates (2)
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
/*
 * madfuload.c - firmware loader for M-Audio DFU devices
 *
 * For legal details, see the LICENSE file.
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <getopt.h>
#include <syslog.h>
#include <sys/ioctl.h>
#include <string.h>

/* USB stuff -- we can't rely on all Linux headers being there */

#define USB_DIR_OUT	0
#define USB_DIR_IN	0x80

#define USB_TYPE_CLASS		(0x01 << 5)
#define USB_RECIP_INTERFACE	0x01
#define USB_DT_INTERFACE	0x04
#define USB_CLASS_APP_SPEC	0xfe

struct usb_descriptor_header {
	u_int8_t bLength;
	u_int8_t bDescriptorType;
} __attribute__ ((packed));

#define USBDEVFS_CONTROL	_IOWR('U', 0, struct usbdevfs_ctrltransfer)
#define USBDEVFS_CLAIMINTERFACE	_IOR('U', 15, unsigned int)
#define USBDEVFS_RESET		_IO('U', 20)

struct usbdevfs_ctrltransfer {
	u_int8_t bRequestType;
	u_int8_t bRequest;
	u_int16_t wValue;
	u_int16_t wIndex;
	u_int16_t wLength;
	u_int32_t timeout;
	void *data;
};

/* symbols and types from the DFU standard */

#define USB_SUBCLASS_DFU	0x01
#define USB_DT_DFU_FUNCTIONAL	0x21

#define DFU_CAN_DNLOAD			0x01
#define DFU_CAN_UPLOAD			0x02
#define DFU_MANIFESTATION_TOLERANT	0x04

#define DFU_DETACH	0
#define DFU_DNLOAD	1
#define DFU_UPLOAD	2
#define DFU_GETSTATUS	3
#define DFU_CLRSTATUS	4
#define DFU_GETSTATE	5
#define DFU_ABORT	6

struct dfu_status {
	u_int8_t bStatus;
	u_int8_t bwPollTimeout[3];
	u_int8_t bState;
	u_int8_t iString;
} __attribute__((packed));


enum {
	WAIT_STANDARD,
	WAIT_IGNORE,
	WAIT_BYTE3
};

static char *command;
static int to_logger;
static int wait_type;
static char *firmware;
static int firmware_length;
static int device;
static int interface;
static int transfer_size = 64;
static int logging;

static void print(int level, const char *format, va_list ap)
{
	if (to_logger) {
		vsyslog(LOG_DAEMON | level, format, ap);
	} else {
		vfprintf(stderr, format, ap);
		putc('\n', stderr);
	}
}

static void fatal(const char *format, ...)
{
	va_list ap;

	va_start(ap, format);
	print(LOG_ERR, format, ap);
	va_end(ap);
	exit(1);
}

static void warning(const char *format, ...)
{
	va_list ap;

	va_start(ap, format);
	print(LOG_WARNING, format, ap);
	va_end(ap);
}

static void logmsg(const char *format, ...)
{
	va_list ap;

	if (!logging)
		return;
	va_start(ap, format);
	print(LOG_DEBUG, format, ap);
	va_end(ap);
}

static int usb_control(unsigned char request_type, unsigned char request,
		       unsigned short value, unsigned short index,
		       unsigned short length, void *data)
{
	struct usbdevfs_ctrltransfer ct;
	int retries, err;

	ct.bRequestType = request_type;
	ct.bRequest = request;
	ct.wValue = value;
	ct.wIndex = index;
	ct.wLength = length;
	ct.timeout = 500;
	ct.data = data;
	for (retries = 0; retries < 3; ++retries) {
		err = ioctl(device, USBDEVFS_CONTROL, &ct);
		if (err >= 0)
			break;
		warning("control transfer failed: (%d) %s", errno, strerror(errno));
	}
	return err;
}

static int usb_claim_interface(unsigned int interface)
{
	return ioctl(device, USBDEVFS_CLAIMINTERFACE, &interface);
}

static int usb_reset(void)
{
	return ioctl(device, USBDEVFS_RESET, NULL);
}

static void load_firmware(char *filename)
{
	int fd = open(filename, O_RDONLY);
	if (fd == -1)
		fatal("cannot open %s: %s", filename, strerror(errno));
	firmware_length = lseek(fd, 0, SEEK_END);
	if (firmware_length < 0)
		fatal("cannot determine length of %s: %s", filename, strerror(errno));
	if (firmware_length == 0)
		fatal("firmware file %s is empty", filename);
	lseek(fd, 0, SEEK_SET);
	firmware = malloc(firmware_length);
	if (!firmware)
		fatal("out of memory");
	if (read(fd, firmware, firmware_length) != firmware_length)
		fatal("cannot read from %s: %s", filename, strerror(errno));
	close(fd);
	logmsg("%s: %d bytes read successfully", filename, firmware_length);
}

static void parse_descriptors(void)
{
	struct usb_descriptor_header desc;
	int interface_found = 0;

	logmsg("reading device descriptor ...");
	for (;;) {
		if (read(device, &desc, 2) != 2)
			return;
		if (desc.bLength < 2) {
			logmsg("invalid descriptor length %d", desc.bLength);
			return;
		}
		if (desc.bDescriptorType == USB_DT_INTERFACE &&
		    desc.bLength >= 9) {
			unsigned char d[7];
			if (read(device, d, 7) != 7)
				return;
			logmsg("interface descriptor %d:%d", d[0], d[1]);
			if (d[3] == USB_CLASS_APP_SPEC &&
			    d[4] == USB_SUBCLASS_DFU) {
				interface_found = 1;
				interface = d[0];
				logmsg("DFU interface is %d", interface);
			} else {
				interface_found = 0;
			}
			desc.bLength -= 7;
		} else if (desc.bDescriptorType == USB_DT_DFU_FUNCTIONAL &&
			   desc.bLength >= 7 &&
			   interface_found) {
			unsigned char d[5];
			logmsg("DFU descriptor found");
			if (read(device, d, 5) != 5)
				return;
			transfer_size = d[3] | (d[4] << 8);
			if (transfer_size < 1)
				fatal("invalid transfer size %d", transfer_size);
			logmsg("transfer size is %d", transfer_size);
			desc.bLength -= 5;
		}
		/* skip (rest of) descriptor */
		if (desc.bLength > 2)
			if (lseek(device, desc.bLength - 2, SEEK_CUR) == (off_t)-1)
				return;
	}
}

static void dfu_dnload(void *buf, int length, int block)
{
	int err;
	
	err = usb_control(USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
			  DFU_DNLOAD, block, interface,
			  length, buf);
	if (err < 0)
		fatal("downloading block %d failed", block);
}

static void dfu_status_wait()
{
	struct dfu_status status;
	int wait, err;

	err = usb_control(USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
			  DFU_GETSTATUS, 0, interface,
			  6, &status);
	if (err < 0)
		fatal("cannot get device status");
	switch (wait_type) {
#if 0
	case WAIT_STANDARD:
	/*
	 * This will be implemented when^H^H^H^Hif M-Audio builds a device
	 * that at least partially conforms to the DFU specification.
	 */
#endif
	case WAIT_IGNORE:
	default:
		wait = 0;
		break;
	case WAIT_BYTE3:
		wait = status.bwPollTimeout[2];
		break;
	}
	if (wait > 0) {
		struct timespec tv;
		logmsg("waiting %d ms", wait);
		tv.tv_sec = wait / 1000;
		tv.tv_nsec = (wait % 1000) * 1000000;
		if (nanosleep(&tv, NULL) == -1)
			fatal("sleep failed: %s", strerror(errno));
	}
}

static void download_firmware(char *device_file)
{
	unsigned short block = 0;
	int pos, length, err;

	device = open(device_file, O_RDWR);
	if (device == -1)
		fatal("cannot open %s: %s", device_file, strerror(errno));
	parse_descriptors();
	if (usb_claim_interface(interface) == -1)
		fatal("cannot claim interface %d: (%d) %s", interface, errno, strerror(errno));

	for (pos = 0; pos < firmware_length; pos += transfer_size) {
		if (firmware_length - pos > transfer_size)
			length = transfer_size;
		else
			length = firmware_length - pos;
		dfu_dnload(firmware + pos, length, block);
		++block;

		dfu_status_wait();
	}
	dfu_dnload(firmware, 0, block);
	dfu_status_wait();

	if (usb_reset() == -1)
		/* might fail because the device has already reset itself ... */
		warning("cannot reset device: (%d) %s", errno, strerror(errno));
	close(device);
}

static void help()
{
	fprintf(stderr,
		"Usage: %s options...\n"
		"\n"
		"-h, --help           help\n"
		"-V, --version        print version\n"
		"-f, --firmware=path  file containing the firmware to download\n"
		"-D, --device=path    usbfs file of the device (default: $DEVICE)\n"
		"-v, --verbose        output debugging messages\n"
		"-l, --logger         output to system logger instead of standard output\n"
		"-n, --nowait         don't wait between download requests\n"
		"-3, --waitbyte3      use only the third byte of the bwPollTimeout field\n",
		command);
}

static void version()
{
	fprintf(stderr, "%s version " VERSION "\n", command);
}

int main(int argc, char *argv[])
{
	static char short_options[] = "hVf:D:vln3";
	static struct option long_options[] = {
		{"help",      0, NULL, 'h'},
		{"version",   0, NULL, 'V'},
		{"firmware",  1, NULL, 'f'},
		{"device",    1, NULL, 'D'},
		{"verbose",   0, NULL, 'v'},
		{"logger",    0, NULL, 'l'},
		{"nowait",    0, NULL, 'n'},
		{"waitbyte3", 0, NULL, '3'},
		{ }
	};
	char *firmware = NULL;
	char *device = getenv("DEVICE");
	int c;

	command = argv[0];
	while ((c = getopt_long(argc, argv, short_options,
				long_options, NULL)) != -1) {
		switch (c) {
		case 'h':
			help();
			return 0;
		case 'V':
			version();
			return 0;
		case 'f':
			firmware = optarg;
			break;
		case 'D':
			device = optarg;
			break;
		case 'v':
			logging = 1;
			break;
		case 'l':
			to_logger = 1;
			break;
		case 'n':
			wait_type = WAIT_IGNORE;
			break;
		case '3':
			wait_type = WAIT_BYTE3;
			break;
		default:
			fprintf(stderr, "Try %s --help for more information.\n", command);
			return 1;
		}
	}
	if (!firmware) {
		fputs("Please specify a firmware file.\n", stderr);
		return 1;
	}
	if (wait_type == WAIT_STANDARD) {
		fputs("Please specify --nowait or --waitbyte3.\n", stderr);
		return 1;
	}
	if (to_logger)
		openlog(PACKAGE, 0, LOG_DAEMON);
	if (!device)
		fatal("--device not given, and DEVICE not set");

	load_firmware(firmware);
	download_firmware(device);

	return 0;
}