-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfirmwareupdatetask.cpp
More file actions
252 lines (226 loc) · 6.61 KB
/
firmwareupdatetask.cpp
File metadata and controls
252 lines (226 loc) · 6.61 KB
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
#include "firmwareupdatetask.h"
#include <QCoreApplication>
#include <QFile>
#include <QJsonObject>
#include <QJsonDocument>
extern "C" {
#include "signetdev/host/signetdev.h"
}
#include <iostream>
void firmwareUpdateTask::help()
{
std::cout << "Update the device's firmware" << std::endl
<< std::endl
<< "Usage: signet-cli update-firmware <firmare file>" << std::endl << std::endl;
}
firmwareUpdateTask::firmwareUpdateTask(int argc, char *argv[]) : m_writePhase(false)
{
if (argc <= 0) {
m_canStart = false;
fprintf(stderr, "No firmware file specified\n");
return;
}
if (!m_fw.fromFile(argv[0])) {
fprintf(stderr, "Firmware file invalid\n");
m_canStart = false;
return;
}
m_writingSectionIter = m_fw.fwSections.cbegin();
m_writingAddr = m_writingSectionIter->lma;
m_writingSize = 0;
m_canStart = true;
}
bool firmwareUpdateTask::canStart()
{
return m_canStart;
}
bool firmwareUpdateTask::start()
{
int token;
if (!m_canStart)
return false;
::signetdev_get_device_state(NULL, &token);
return true;
}
void firmwareUpdateTask::sendFirmwareWriteCmd()
{
int token;
bool advance = false;
unsigned int section_lma = m_writingSectionIter->lma;
unsigned int section_size = m_writingSectionIter->size;
unsigned int section_end = section_lma + section_size;
unsigned int write_size = 1024;
if ((m_writingAddr + write_size) >= section_end) {
write_size = section_end - m_writingAddr;
advance = true;
}
const void *data = m_writingSectionIter->contents.data() + (m_writingAddr - section_lma);
::signetdev_write_flash(NULL, &token, m_writingAddr, data, write_size);
if (advance) {
m_writingSectionIter++;
if (m_writingSectionIter != m_fw.fwSections.end()) {
m_writingAddr = m_writingSectionIter->lma;
}
} else {
m_writingAddr += write_size;
}
m_writingSize = write_size;
}
void firmwareUpdateTask::cmdResponse(void *cb_user_param, int cmd_token, int cmd, int end_device_state, int messages_remaining, int resp_code, void *resp_data)
{
Q_UNUSED(cb_user_param);
Q_UNUSED(cmd_token);
Q_UNUSED(messages_remaining);
int token;
switch(cmd) {
case SIGNETDEV_CMD_GET_DEVICE_STATE: {
switch (end_device_state) {
case DISCONNECTED:
::signetdev_startup(NULL, &token);
break;
case UNINITIALIZED:
case LOGGED_IN:
::signetdev_begin_update_firmware(NULL, &token);
break;
case LOGGED_OUT:
std::cout << "You must unlock the device before updating firmware" << std::endl;
QCoreApplication::quit();
break;
default:
std::cout << "Device not in valid state for updating firmware" << std::endl;
QCoreApplication::quit();
break;
}
} break;
case SIGNETDEV_CMD_STARTUP: {
std::cout << "Press and HOLD device button to begin updating firmware" << std::endl;
::signetdev_begin_update_firmware(NULL, &token);
switch (end_device_state) {
case LOGGED_OUT:
std::cout << "You must unlock the device before updating firmware" << std::endl;
QCoreApplication::quit();
break;
case UNINITIALIZED:
::signetdev_begin_update_firmware(NULL, &token);
break;
default:
std::cout << "Device not in valid state for updating firmware" << std::endl;
QCoreApplication::quit();
break;
}
} break;
case SIGNETDEV_CMD_BEGIN_UPDATE_FIRMWARE: {
std::cout << "Firmware update started, this make take awhile. "
<< "Disconnecting your device before the update finishes will lead to device malfunction" << std::endl;
QByteArray erase_pages_;
QByteArray page_mask(512, 0);
if (resp_code != OKAY) {
printf("Begin firmware update failed. Code %d\n", resp_code);
QCoreApplication::quit();
return;
}
for (auto iter = m_fw.fwSections.begin(); iter != m_fw.fwSections.end(); iter++) {
const fwSection §ion = (*iter);
unsigned int lma = section.lma;
unsigned int lma_end = lma + section.size;
int page_begin = (lma - 0x8000000)/2048;
int page_end = (lma_end - 1 - 0x8000000)/2048;
for (int i = page_begin; i <= page_end; i++) {
if (i < 0)
continue;
if (i >= 511)
continue;
page_mask[i] = 1;
}
}
for (int i = 0; i < 512; i++) {
if (page_mask[i]) {
erase_pages_.push_back(i);
}
}
::signetdev_erase_pages(NULL, &token, erase_pages_.size(), (u8 *)erase_pages_.data());
} break;
case SIGNETDEV_CMD_ERASE_PAGES:
if (resp_code == OKAY) {
::signetdev_get_progress(NULL, &token, 0, ERASING_PAGES);
}
break;
case SIGNETDEV_CMD_WRITE_FLASH:
if (resp_code == OKAY) {
if (m_writingSectionIter == m_fw.fwSections.end()) {
::signetdev_reset_device(NULL, &token);
} else {
sendFirmwareWriteCmd();
}
}
break;
case SIGNETDEV_CMD_GET_PROGRESS: {
signetdev_get_progress_resp_data *resp = (signetdev_get_progress_resp_data *)resp_data;
if (end_device_state == FIRMWARE_UPDATE) {
m_writingSectionIter = m_fw.fwSections.begin();
m_writingAddr = m_writingSectionIter->lma;
sendFirmwareWriteCmd();
} else {
::signetdev_get_progress(NULL, &token, resp->total_progress, ERASING_PAGES);
}
} break;
}
}
bool firmware::fromFile(const QString &path)
{
QFile firmwareUpdateFile(path);
bool result = firmwareUpdateFile.open(QFile::ReadWrite);
if (!result) {
firmwareUpdateFile.close();
return false;
}
QByteArray datum = firmwareUpdateFile.readAll();
QJsonDocument doc = QJsonDocument::fromJson(datum);
firmwareUpdateFile.close();
bool validFw = !doc.isNull() && doc.isObject();
QJsonObject docObj;
QJsonObject sectionsObj;
if (validFw) {
docObj = doc.object();
QJsonValue tempVal = docObj.value("sections");
validFw = (tempVal != QJsonValue::Undefined) && tempVal.isObject();
if (validFw) {
sectionsObj = tempVal.toObject();
}
}
if (validFw) {
for (auto iter = sectionsObj.constBegin(); iter != sectionsObj.constEnd() && validFw; iter++) {
fwSection section;
section.name = iter.key();
QJsonValue temp = iter.value();
if (!temp.isObject()) {
validFw = false;
break;
}
QJsonObject sectionObj = temp.toObject();
QJsonValue lmaVal = sectionObj.value("lma");
QJsonValue sizeVal = sectionObj.value("size");
QJsonValue contentsVal = sectionObj.value("contents");
if (lmaVal == QJsonValue::Undefined ||
sizeVal == QJsonValue::Undefined ||
contentsVal == QJsonValue::Undefined ||
!lmaVal.isDouble() ||
!sizeVal.isDouble() ||
!contentsVal.isString()) {
validFw = false;
break;
}
section.lma = (unsigned int)(lmaVal.toDouble());
section.size = (unsigned int)(sizeVal.toDouble());
section.contents = QByteArray::fromBase64(contentsVal.toString().toLatin1());
if (section.contents.size() != section.size) {
validFw = false;
break;
}
fwSections.append(section);
}
}
if (!validFw)
return 0;
return validFw;
}