-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflasher.cpp
562 lines (456 loc) · 17.3 KB
/
flasher.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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#include "flasher.h"
#include <QDebug>
#include <QThread>
#include <QtSerialPort/QSerialPortInfo>
Flasher::Flasher(QObject *parent) : QObject(parent)
{
port_name = "";
serial = 0;
protocol_type = OLD_PROTO;
readed = 0;
}
/**
* @brief Открывает последовательное соединение с портом
* @param port_name
* @return
*/
bool Flasher::openSerialPort(QString port_name)
{
this->serial = new QSerialPort();
const auto infos = QSerialPortInfo::availablePorts();
for (const QSerialPortInfo &info : infos) {
if (info.portName() == port_name) {
serial->setPort(info);
serial->setBaudRate(QSerialPort::Baud57600);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
//serial->setRequestToSend(true);
if (serial->open(QIODevice::ReadWrite)) {
//success connected
qDebug() << "serial connected";
return true;
} else {
emit infoCritical(tr("Error"), serial->errorString());
qDebug() << "serial not connected";
return false;
}
break;
}
}
emit infoCritical(tr("Error"), "COM port not found!");
qDebug() << "serial not found";
return false;
}
/**
* @brief Закрывает последовательное соединение
*/
void Flasher::closeSerialPort()
{
if (serial != 0 && serial->isOpen()) {
serial->close();
delete serial;
serial = 0;
qDebug() << "serial disconnected";
}
}
void Flasher::setPortName(QString port_name) {
this->port_name = port_name;
}
void Flasher::setProtocol(PROTOCOLS proto)
{
this->protocol_type = proto;
}
void Flasher::setBootMode(BOOT_MODE boot_mode) {
this->boot_mode = boot_mode;
}
qint64 Flasher::getReadedBytes() {
return this->readed;
}
/**
* @brief Осуществляет запись прошивки в ЭБУ из файла
* @param file
*/
void Flasher::send_flash_from_file(QString hexFilePath, int page_size) {
emit changeProgress(0);
this->readed = 0;
this->closeSerialPort();
if (!this->openSerialPort(this->port_name)) {
emit changeProgress(0);
return;
}
// Очистка старой прошивки
if (!this->erase_chip()) return;
// Открытие файла прошивки
QFile file(hexFilePath);
if(!file.open(QIODevice::ReadOnly)) {
emit infoCritical(tr("Error"), file.errorString());
this->closeSerialPort();
return;
}
qint64 hexfilesize = file.size();
W2B data;
uint16_t addressW = 0x0000;
qint64 step_bytes = 1;
uint16_t ck1=0, ck2=0;
QByteArray temp;
bool is_lsb = false;
int current_progress = 0;
int page_count = 0;
while (!file.atEnd()) {
QDebug debug = qDebug();
// Сначала пропускаем спец симовол начала строки
if (step_bytes == 1) {
temp = file.read(step_bytes);
readed += step_bytes;
if (temp.data()[0] != ':') {
continue;
}
step_bytes = 2;
}
// Потом шагаем по два символа, считывая байтики
if (step_bytes == 2) {
// length of data in line
int data_len = file.read(step_bytes).toInt(Q_NULLPTR, 16); readed += step_bytes;
// data address (not using...)
file.read(4);
readed += 4;
// type of data
temp = file.read(2);
readed += 2;
// data record type
if (QString(temp) == "00") {
int i = 0;
while (i < data_len) {
if (!is_lsb) {
data.b[0] = file.read(2).toUInt(Q_NULLPTR, 16);
readed += 2;
} else {
data.b[1] = file.read(2).toUInt(Q_NULLPTR, 16);
readed += 2;
if (page_count == 0) {
// todo: тут можно безопасно остановить процесс прошивки
// или повторно осуществить запись страницы,
// например из-за несовпадения чек-сумм
ck1 = ck2 = addressW;
if (!send_address(addressW)) {
emit infoCritical(tr("Error"), "Ошибка при отправке адреса страницы!");
file.close();
this->closeSerialPort();
return;
}
// init write page
char command[] = {'D','k','k'};
debug << QByteArray(command,3).toHex() << "\n";
#ifndef DEBUG_MODE
serial->write(command, 3);
serial->waitForBytesWritten(10000);
#endif
}
// send bytes to EFI
char rev_data[] = {char(data.b[0]),char(data.b[1])};
debug << QByteArray(rev_data,2).toHex();
#ifndef DEBUG_MODE
serial->write(rev_data, 2);
serial->waitForBytesWritten(10000);
#endif
ck1 += data.w;
ck2 ^= data.w;
addressW += 1;
if (page_count == page_size-1) {
debug << "\n";
debug = qDebug();
if (!send_checksumm(ck1,ck2)) {
emit infoCritical(tr("Error"), "Ошибка при проверке контрольной суммы!");
file.close();
this->closeSerialPort();
return; // todo: необходимо осуществлять повторные несколько попыток отправки страницы
}
page_count = 0;
} else {
page_count++;
}
}
is_lsb = !is_lsb;
i++;
}
// skip std cksum ans endline symbols
step_bytes = 1;
}
// end of file record type
else if (QString(temp) == "01") {
// fill page
while (page_count < page_size) {
data.b[0] = 0xFF;
data.b[1] = 0xFF;
debug << QByteArray((char *)data.b,2).toHex();
#ifndef DEBUG_MODE
serial->write((char *)data.b, 2);
serial->waitForBytesWritten(10000);
#endif
ck1 += data.w;
ck2 ^= data.w;
page_count++;
}
debug << "\n";
debug = qDebug();
if (!send_checksumm(ck1,ck2)) {
emit infoCritical(tr("Error"), "Ошибка при проверке контрольной суммы!");
file.close();
this->closeSerialPort();
return; // todo: необходимо осуществлять повторные несколько попыток отправки страницы
}
}
// address record type
else if (QString(temp) == "02") {
if (page_count != 0) {
emit infoWarning(tr("Warning"), "Предыдущая страница еще не окончена!");
//todo: возможно, нужно заполнить остаток страницы 0xFF
}
// data address
uint16_t addr_offset = file.read(4).toUInt(Q_NULLPTR, 16);
readed += 4;
addressW = (addr_offset << 4)/2;
// skip std cksum ans endline symbols
step_bytes = 1;
}
else {
qDebug() << "Unsupported record type: " << QString(temp) << ". Ignoring!";
file.read(data_len*2).toUInt(Q_NULLPTR, 16);
// skip std cksum ans endline symbols
step_bytes = 1;
}
}
if (current_progress != int(100*readed/hexfilesize)) {
emit changeProgress(current_progress = 100*readed/hexfilesize);
}
}
emit changeProgress(100);
file.close();
this->closeSerialPort();
}
/**
* @brief Отправляет адрес загружаемой страницы памяти ЭБУ
* @param addressW
* @return
*/
bool Flasher::send_address(uint16_t addressW) {
QDebug debug = qDebug();
debug << "send address...";
W2B addr;
addr.w = addressW;
char data[] = {0x41, char(addr.b[1]), char(addr.b[0])};
#ifdef DEBUG_MODE
debug << QByteArray(data,3).toHex();
return true;
#endif
serial->write(data, 3);
serial->waitForBytesWritten(10000);
debug << "...";
while (serial->waitForReadyRead(1000)) {
debug << "..";
QByteArray recv = serial->readAll();
if (recv.size() && recv.at(recv.size()-1) == 0x0d) {
debug << "success";
return true;
}
}
debug << "error with input buffer: " << serial->readAll();
return false;
}
/**
* @brief Отправляет чек-суммы и ждет ответа от загрузчика о их правильности
* @param ck1
* @param ck2
* @return
*/
bool Flasher::send_checksumm(uint16_t ck1, uint16_t ck2) {
qDebug() << "send checksumm...";
char data[] = {char((ck1 >> 8)),
char((ck1 & 0xFF)),
char((ck2 >> 8)),
char((ck2 & 0xFF))};
#ifdef DEBUG_MODE
qDebug() << QByteArray(data,4).toHex();
return true;
#endif
serial->write(data, 4);
serial->waitForBytesWritten(10000);
qDebug() << "msg sent...";
QByteArray recv;
while (serial->waitForReadyRead(1000)) {
qDebug() << "...";
recv.append(serial->readAll());
//uint8_t rck1 = recv.mid(0,1).toUInt(Q_NULLPTR, 16);
//uint8_t rck2 = recv.mid(1,1).toUInt(Q_NULLPTR, 16);
//uint8_t rck3 = recv.mid(2,1).toUInt(Q_NULLPTR, 16);
//uint8_t rck4 = recv.mid(3,1).toUInt(Q_NULLPTR, 16);
//if (rck1 != (ck1 >> 8)) return false;
//if (rck2 != (ck1 & 0xFF)) return false;
//if (rck3 != (ck2 >> 8)) return false;
//if (rck4 != (ck2 & 0xFF)) return false;
if (recv.size() && recv.at(recv.size()-1) == 0x0d) {
qDebug() << "checksumm right!";
return true;
} else if (recv.size() && recv.at(recv.size()-1) == 'N') {
qDebug() << "checksumm FAIL!";
return false;
}
}
qDebug() << "invalid responce: " << serial->readAll();
return false;
}
/**
* @brief Входит в режим прошивки
* @return
*/
bool Flasher::go_boot() {
qDebug() << "go boot mode...";
emit changeProgress(-1);
this->closeSerialPort();
if (!this->openSerialPort(this->port_name)) {
emit changeProgress(0);
return false;
}
if (this->protocol_type == CAN_STYLE_PROTO) {
qDebug() << "syncing...";
char data_[] = {'F'};
serial->write((char*)data_,1);
serial->waitForBytesWritten(10000);
while(serial->waitForReadyRead(1000));
qDebug() << serial->readAll();
qDebug() << "send command...";
if (this->boot_mode == BRIDGE_MODE) {
// table 0x01 - config table
// table_offset 0x19 - options field in config table
// { size, type, CANID, table, tbl_offset, tbl_size, mode, crc32}
uint8_t data[] = {0x00, 0x08, 0x77, 0x00, 0x01, 0x00, 0x19, 0x00, 0x01, 0x02, 0xd2, 0xef, 0xdc, 0x3d};
serial->write((char*)data,14);
} else {
uint8_t data[] = {0x00, 0x08, 0x77, 0x00, 0x01, 0x00, 0x19, 0x00, 0x01, 0x01, 0x4b, 0xe6, 0x8d, 0x87};
serial->write((char*)data,14);
}
serial->waitForBytesWritten(10000);
} else {
char data[] = {0x4F, 0x00, 0x19, 0x01};
if (this->boot_mode == BRIDGE_MODE) {
data[3] = 0x02;
} else {
data[3] = 0x01;
}
serial->write((char*)data,4);
serial->waitForBytesWritten(10000);
}
qDebug() << "msg sent...";
// На всякий ждем ответ от мк (не дольше 1000мс)
// Нужно на случай, если входящий буфер не пуст.
while(serial->waitForReadyRead(1000));
qDebug() << "clean input buffer: " << serial->readAll() << " ok";
// Проверка статуса (в бут режиме или нет)
QString ret = this->get_status(false);
this->closeSerialPort();
emit changeProgress(0);
return (ret != "no data");
}
/**
* @brief Выходит из режима прошивки
* @return
*/
bool Flasher::leave_boot() {
qDebug() << "leaving boot mode...";
emit changeProgress(-1);
this->closeSerialPort();
if (!this->openSerialPort(this->port_name)) {
emit changeProgress(0);
return false;
}
char data[] = {0x45};
serial->write((char*)data,1);
serial->waitForBytesWritten(10000);
qDebug() << "msg sent...";
// Ждем выхода
QByteArray recv;
while (serial->waitForReadyRead(1000)) {
qDebug() << "...";
recv.append(serial->readAll());
if (recv.size() && recv.at(recv.size()-1) == 0x0d) {
qDebug() << "success " << recv;
this->closeSerialPort();
emit changeProgress(0);
return true;
}
}
qDebug() << "failed!";
emit infoCritical(tr("Error"), "Failed to leave boot mode!");
this->closeSerialPort();
emit changeProgress(0);
return false;
}
/**
* @brief Очищает прошивку ЭБУ
* @return
*/
bool Flasher::erase_chip() {
qDebug() << "erasing chip...";
emit changeProgress(-1);
char data[] = {0x65};
#ifdef DEBUG_MODE
return true;
#endif
serial->write((char*)data,1);
serial->waitForBytesWritten(10000);
qDebug() << "msg sent...";
// Ждем очистки...
while (serial->waitForReadyRead(10000)) {
qDebug() << "...";
QByteArray recv = serial->readAll();
if (recv.size() && recv.at(recv.size()-1) == 0x0d) {
qDebug() << "success!";
emit changeProgress(0);
return true;
}
}
qDebug() << "error " << serial->readAll();
emit changeProgress(0);
return false;
}
/**
* @brief Flasher::get_status
* @return
*/
QString Flasher::get_status(bool open_serial) {
qDebug() << "getting status...";
emit changeProgress(-1);
if (open_serial) {
this->closeSerialPort();
if (!this->openSerialPort(this->port_name)) {
emit changeProgress(0);
return "no connect";
}
}
char data[] = {0x53, 0x0d}; // специально добавлен ненужный символ в конец,
// чтобы мк ругнулся на него последним символом в ответе
serial->write((char*)data,2);
serial->waitForBytesWritten(10000);
qDebug() << "msg sent...";
QByteArray recv;
while (serial->waitForReadyRead(2000)) {
qDebug() << "...";
recv.append(serial->readAll());
if (recv.size() && recv.at(recv.size()-1) == '?') { // этим символом мк ругается
// и заодно говорит, что закончил отправку :)
recv.remove(recv.size()-1,1); // удалим его из вывода
qDebug() << "success! " << recv;
if (open_serial) this->closeSerialPort();
emit changeProgress(0);
return recv;
}
}
if (recv.size() > 0) {
qDebug() << "recieve without end symbol: " << recv;
}
qDebug() << "no data!";
if (open_serial) this->closeSerialPort();
emit changeProgress(0);
return "no data";
}