-
Notifications
You must be signed in to change notification settings - Fork 9
/
dfu_manager.cpp
261 lines (212 loc) · 7.38 KB
/
dfu_manager.cpp
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
/*
* This file is part of the Paparazzi UAV project.
*
* Copyright (C) 2012 Piotr Esden-Tempski <piotr@esden.net>
*
* 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 3 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "dfu_manager.h"
#include <QDebug>
#include <QFile>
#include "usb.h"
#include "upgrade/dfu.h"
#include "upgrade/stm32mem.h"
#define LOAD_ADDRESS 0x8002000
DFUManager::DFUManager(QObject *parent) :
QObject(parent)
{
usb_init();
timer.setInterval(2000);
connect(&timer, SIGNAL(timeout()), this, SLOT(findIFace()));
handle = NULL;
block_size = 1024;
flash_size_mutex.lock();
flash_size = 0x20000; /* 128kb */
flash_size_mutex.unlock();
}
DFUManager::~DFUManager()
{
if (handle) {
qDebug() << "Releasing interface on delete.";
usb_release_interface(handle, iface);
usb_close(handle);
handle = NULL;
}
}
void DFUManager::start()
{
findIFace();
timer.start();
}
void DFUManager::findIFace()
{
QString *deviceName;
qDebug() << "Releasing Interface...";
if (handle) {
usb_release_interface(handle, iface);
usb_close(handle);
handle = NULL;
}
qDebug() << "Trying to find DFU devices...";
if(!(dev = findDev()) || !(handle = getDFUIface(dev, &iface))) {
qDebug() << "FATAL: No compatible device found!\n";
emit lostDevice();
return;
}
state = dfu_getstate(handle, iface);
if((state < 0) || (state == STATE_APP_IDLE)) {
puts("Resetting device in firmware upgrade mode...");
dfu_detach(handle, iface, 1000);
usb_release_interface(handle, iface);
usb_close(handle);
handle = NULL;
emit lostDevice();
return;
}
qDebug() << "Found device at " << dev->bus->dirname << ":" << dev->filename;
deviceName = new QString("Found device at ");
deviceName->append(dev->bus->dirname);
deviceName->append(":");
deviceName->append(dev->filename);
emit foundDevice(deviceName);
}
struct usb_device *DFUManager::findDev()
{
struct usb_bus *bus;
struct usb_device *dev;
struct usb_dev_handle *handle;
char man[40];
char prod[40];
usb_find_busses();
usb_find_devices();
for(bus = usb_get_busses(); bus; bus = bus->next) {
for(dev = bus->devices; dev; dev = dev->next) {
/* Check for ST Microelectronics vendor ID */
if ((dev->descriptor.idVendor != 0x483) &&
(dev->descriptor.idVendor != 0x1d50))
continue;
handle = usb_open(dev);
usb_get_string_simple(handle, dev->descriptor.iManufacturer, man,
sizeof(man));
usb_get_string_simple(handle, dev->descriptor.iProduct, prod,
sizeof(prod));
#if 0
printf("%s:%s [%04X:%04X] %s : %s\n", bus->dirname, dev->filename,
dev->descriptor.idVendor, dev->descriptor.idProduct, man, prod);
#endif
usb_close(handle);
if (((dev->descriptor.idProduct == 0x5740) ||
(dev->descriptor.idProduct == 0x6018)) &&
!strcmp(man, "Black Sphere Technologies") &&
!strcmp(prod, "Black Magic Firmware Upgrade")) {
block_size = 1024;
flash_size_mutex.lock();
flash_size = 0x20000; /* 128kb */
flash_size_mutex.unlock();
return dev;
}
if (((dev->descriptor.idProduct == 0xDF11) ||
(dev->descriptor.idProduct == 0x6017)) &&
!strcmp(man, "Black Sphere Technologies") &&
!strcmp(prod, "Black Magic Probe (Upgrade)")) {
block_size = 1024;
flash_size_mutex.lock();
flash_size = 0x20000; /* 128kb */
flash_size_mutex.unlock();
return dev;
}
if (((dev->descriptor.idProduct == 0x600F)) &&
!strcmp(man, "Transition Robotics Inc.") &&
!strcmp(prod, "Lisa/M (Upgrade) V1.0")) {
block_size = 2048;
flash_size_mutex.lock();
flash_size = 0x40000; /* 256kb */
flash_size_mutex.unlock();
return dev;
}
}
}
return NULL;
}
struct usb_dev_handle *DFUManager::getDFUIface(struct usb_device *dev, uint16_t *interface)
{
int i, j, k;
struct usb_config_descriptor *config;
struct usb_interface_descriptor *iface;
usb_dev_handle *handle;
for(i = 0; i < dev->descriptor.bNumConfigurations; i++) {
config = &dev->config[i];
for(j = 0; j < config->bNumInterfaces; j++) {
for(k = 0; k < config->interface[j].num_altsetting; k++) {
iface = &config->interface[j].altsetting[k];
if((iface->bInterfaceClass == 0xFE) &&
(iface->bInterfaceSubClass = 0x01)) {
handle = usb_open(dev);
//usb_set_configuration(handle, i);
usb_claim_interface(handle, j);
//usb_set_altinterface(handle, k);
//*interface = j;
*interface = iface->bInterfaceNumber;
return handle;
}
}
}
}
return NULL;
}
void DFUManager::flash(QString *filename)
{
QFile binfile;
QByteArray binfile_content;
int bindata_offset;
timer.stop();
binfile.setFileName(*filename);
if(!binfile.open(QIODevice::ReadOnly)) {
timer.start();
qDebug() << "Failed to open file " << *filename;
/* TODO: need to emit an error here */
return;
}
binfile_content = binfile.readAll();
//binfile_pointer = binfile_content.data_ptr();
qDebug() << "Opened file " << *filename << " and read " << binfile_content.size() << " bytes?";
dfu_makeidle(handle, iface);
for (bindata_offset = 0; bindata_offset < binfile_content.size(); bindata_offset += block_size) {
emit flashProgressUpdate(LOAD_ADDRESS + bindata_offset, (bindata_offset*100)/binfile_content.size());
if (stm32_mem_erase(handle, iface, LOAD_ADDRESS + bindata_offset) != 0) {
/* TODO: emit error */
binfile.close();
//delete(filename);
emit finishedFlash();
timer.start();
}
stm32_mem_write(handle, iface, (void*)&binfile_content.data()[bindata_offset], block_size);
}
stm32_mem_manifest(handle, iface);
usb_release_interface(handle, iface);
usb_close(handle);
handle = NULL;
binfile.close();
delete(filename);
emit finishedFlash();
timer.start();
}
int DFUManager::get_flash_size()
{
int size;
flash_size_mutex.lock();
size = flash_size;
flash_size_mutex.unlock();
return size;
}