Debian Codegraph @

src/dfu_load.c

raw
1 /*
2 * DFU transfer routines
3 *
4 * This is supposed to be a general DFU implementation, as specified in the
5 * USB DFU 1.0 and 1.1 specification.
6 *
7 * The code was originally intended to interface with a USB device running the
8 * "sam7dfu" firmware (see http://www.openpcd.org/) on an AT91SAM7 processor.
9 *
10 * Copyright 2007-2008 Harald Welte <[email protected]>
11 * Copyright 2013 Hans Petter Selasky <[email protected]>
12 * Copyright 2014-2016 Tormod Volden <[email protected]>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #define __USE_MINGW_ANSI_STDIO 1
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <errno.h>
37 #include <string.h>
38
39 #include <libusb.h>
40
41 #include "portable.h"
42 #include "dfu.h"
43 #include "usb_dfu.h"
44 #include "dfu_file.h"
45 #include "dfu_load.h"
46 #include "quirks.h"
47
48 int dfuload_do_upload(struct dfu_if *dif, int xfer_size,
49 int expected_size, int fd)
50 {
51 off_t total_bytes = 0;
52 unsigned short transaction = 0;
53 unsigned char *buf;
54 int ret;
55
56 buf = dfu_malloc(xfer_size);
57
58 printf("Copying data from DFU device to PC\n");
59
60 while (1) {
61 int rc;
62 dfu_progress_bar("Upload", total_bytes, expected_size);
63 rc = dfu_upload(dif->dev_handle, dif->interface,
64 xfer_size, transaction++, buf);
65 if (rc < 0) {
66 warnx("\nError during upload (%s)",
67 libusb_error_name(rc));
68 ret = rc;
69 break;
70 }
71
72 dfu_file_write_crc(fd, 0, buf, rc);
73 total_bytes += rc;
74
75 if (total_bytes < 0)
76 errx(EX_SOFTWARE, "\nReceived too many bytes (wraparound)");
77
78 if (rc < xfer_size) {
79 /* last block, return */
80 ret = 0;
81 break;
82 }
83 }
84 free(buf);
85 if (ret == 0) {
86 dfu_progress_bar("Upload", total_bytes, total_bytes);
87 } else {
88 dfu_progress_bar("Upload", total_bytes, expected_size);
89 printf("\n");
90 }
91 if (total_bytes == 0)
92 printf("\nFailed.\n");
93 else
94 printf("Received a total of %lli bytes\n", (long long) total_bytes);
95
96 if (expected_size != 0 && total_bytes != expected_size)
97 warnx("Unexpected number of bytes uploaded from device");
98 return ret;
99 }
100
101 int dfuload_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file)
102 {
103 off_t bytes_sent;
104 off_t expected_size;
105 unsigned char *buf;
106 unsigned short transaction = 0;
107 struct dfu_status dst;
108 int ret;
109
110 printf("Copying data from PC to DFU device\n");
111
112 buf = file->firmware;
113 expected_size = file->size.total - file->size.suffix;
114 bytes_sent = 0;
115
116 dfu_progress_bar("Download", 0, 1);
117 while (bytes_sent < expected_size) {
118 off_t bytes_left;
119 int chunk_size;
120
121 bytes_left = expected_size - bytes_sent;
122 if (bytes_left < xfer_size)
123 chunk_size = (int) bytes_left;
124 else
125 chunk_size = xfer_size;
126
127 ret = dfu_download(dif->dev_handle, dif->interface,
128 chunk_size, transaction++, chunk_size ? buf : NULL);
129 if (ret < 0) {
130 warnx("Error during download (%s)",
131 libusb_error_name(ret));
132 goto out;
133 }
134 bytes_sent += chunk_size;
135 buf += chunk_size;
136
137 do {
138 ret = dfu_get_status(dif, &dst);
139 if (ret < 0) {
140 errx(EX_IOERR, "Error during download get_status (%s)",
141 libusb_error_name(ret));
142 goto out;
143 }
144
145 if (dst.bState == DFU_STATE_dfuDNLOAD_IDLE ||
146 dst.bState == DFU_STATE_dfuERROR)
147 break;
148
149 /* Wait while device executes flashing */
150 milli_sleep(dst.bwPollTimeout);
151 if (verbose > 1)
152 fprintf(stderr, "Poll timeout %i ms\n", dst.bwPollTimeout);
153
154 } while (1);
155
156 if (dst.bStatus != DFU_STATUS_OK) {
157 printf(" failed!\n");
158 printf("DFU state(%u) = %s, status(%u) = %s\n", dst.bState,
159 dfu_state_to_string(dst.bState), dst.bStatus,
160 dfu_status_to_string(dst.bStatus));
161 ret = -1;
162 goto out;
163 }
164 dfu_progress_bar("Download", bytes_sent, bytes_sent + bytes_left);
165 }
166
167 /* send one zero sized download request to signalize end */
168 ret = dfu_download(dif->dev_handle, dif->interface, 0, transaction, NULL);
169 if (ret < 0) {
170 errx(EX_IOERR, "Error sending completion packet (%s)",
171 libusb_error_name(ret));
172 goto out;
173 }
174
175 dfu_progress_bar("Download", bytes_sent, bytes_sent);
176
177 if (verbose)
178 printf("Sent a total of %lli bytes\n", (long long) bytes_sent);
179
180 get_status:
181 /* Transition to MANIFEST_SYNC state */
182 ret = dfu_get_status(dif, &dst);
183 if (ret < 0) {
184 warnx("unable to read DFU status after completion (%s)",
185 libusb_error_name(ret));
186 goto out;
187 }
188 printf("DFU state(%u) = %s, status(%u) = %s\n", dst.bState,
189 dfu_state_to_string(dst.bState), dst.bStatus,
190 dfu_status_to_string(dst.bStatus));
191
192 milli_sleep(dst.bwPollTimeout);
193
194 /* FIXME: deal correctly with ManifestationTolerant=0 / WillDetach bits */
195 switch (dst.bState) {
196 case DFU_STATE_dfuMANIFEST_SYNC:
197 case DFU_STATE_dfuMANIFEST:
198 /* some devices (e.g. TAS1020b) need some time before we
199 * can obtain the status */
200 milli_sleep(1000);
201 goto get_status;
202 break;
203 case DFU_STATE_dfuMANIFEST_WAIT_RST:
204 printf("Resetting USB to switch back to runtime mode\n");
205 ret = libusb_reset_device(dif->dev_handle);
206 if (ret < 0 && ret != LIBUSB_ERROR_NOT_FOUND) {
207 fprintf(stderr, "error resetting after download (%s)\n",
208 libusb_error_name(ret));
209 }
210 break;
211 case DFU_STATE_dfuIDLE:
212 break;
213 }
214 printf("Done!\n");
215
216 out:
217 return ret;
218 }